1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
46 class Output_data_plt_x86_64
;
48 // The x86_64 target class.
50 // http://www.x86-64.org/documentation/abi.pdf
51 // TLS info comes from
52 // http://people.redhat.com/drepper/tls.pdf
53 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
55 class Target_x86_64
: public Sized_target
<64, false>
58 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
59 // uses only Elf64_Rela relocation entries with explicit addends."
60 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
63 : Sized_target
<64, false>(&x86_64_info
),
64 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rela_dyn_(NULL
),
65 copy_relocs_(elfcpp::R_X86_64_COPY
), dynbss_(NULL
),
66 got_mod_index_offset_(-1U)
69 // Scan the relocations to look for symbol adjustments.
71 scan_relocs(const General_options
& options
,
74 Sized_relobj
<64, false>* object
,
75 unsigned int data_shndx
,
77 const unsigned char* prelocs
,
79 Output_section
* output_section
,
80 bool needs_special_offset_handling
,
81 size_t local_symbol_count
,
82 const unsigned char* plocal_symbols
);
84 // Finalize the sections.
86 do_finalize_sections(Layout
*);
88 // Return the value to use for a dynamic which requires special
91 do_dynsym_value(const Symbol
*) const;
93 // Relocate a section.
95 relocate_section(const Relocate_info
<64, false>*,
97 const unsigned char* prelocs
,
99 Output_section
* output_section
,
100 bool needs_special_offset_handling
,
102 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
103 section_size_type view_size
);
105 // Scan the relocs during a relocatable link.
107 scan_relocatable_relocs(const General_options
& options
,
108 Symbol_table
* symtab
,
110 Sized_relobj
<64, false>* object
,
111 unsigned int data_shndx
,
112 unsigned int sh_type
,
113 const unsigned char* prelocs
,
115 Output_section
* output_section
,
116 bool needs_special_offset_handling
,
117 size_t local_symbol_count
,
118 const unsigned char* plocal_symbols
,
119 Relocatable_relocs
*);
121 // Relocate a section during a relocatable link.
123 relocate_for_relocatable(const Relocate_info
<64, false>*,
124 unsigned int sh_type
,
125 const unsigned char* prelocs
,
127 Output_section
* output_section
,
128 off_t offset_in_output_section
,
129 const Relocatable_relocs
*,
131 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
132 section_size_type view_size
,
133 unsigned char* reloc_view
,
134 section_size_type reloc_view_size
);
136 // Return a string used to fill a code section with nops.
138 do_code_fill(section_size_type length
) const;
140 // Return whether SYM is defined by the ABI.
142 do_is_defined_by_abi(Symbol
* sym
) const
143 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
145 // Return the size of the GOT section.
149 gold_assert(this->got_
!= NULL
);
150 return this->got_
->data_size();
154 // The class which scans relocations.
159 : issued_non_pic_error_(false)
163 local(const General_options
& options
, Symbol_table
* symtab
,
164 Layout
* layout
, Target_x86_64
* target
,
165 Sized_relobj
<64, false>* object
,
166 unsigned int data_shndx
,
167 Output_section
* output_section
,
168 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
169 const elfcpp::Sym
<64, false>& lsym
);
172 global(const General_options
& options
, Symbol_table
* symtab
,
173 Layout
* layout
, Target_x86_64
* target
,
174 Sized_relobj
<64, false>* object
,
175 unsigned int data_shndx
,
176 Output_section
* output_section
,
177 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
182 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
185 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
189 check_non_pic(Relobj
*, unsigned int r_type
);
191 // Whether we have issued an error about a non-PIC compilation.
192 bool issued_non_pic_error_
;
195 // The class which implements relocation.
200 : skip_call_tls_get_addr_(false)
205 if (this->skip_call_tls_get_addr_
)
207 // FIXME: This needs to specify the location somehow.
208 gold_error(_("missing expected TLS relocation"));
212 // Do a relocation. Return false if the caller should not issue
213 // any warnings about this relocation.
215 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, size_t relnum
,
216 const elfcpp::Rela
<64, false>&,
217 unsigned int r_type
, const Sized_symbol
<64>*,
218 const Symbol_value
<64>*,
219 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
223 // Do a TLS relocation.
225 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
226 size_t relnum
, const elfcpp::Rela
<64, false>&,
227 unsigned int r_type
, const Sized_symbol
<64>*,
228 const Symbol_value
<64>*,
229 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
232 // Do a TLS General-Dynamic to Initial-Exec transition.
234 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
235 Output_segment
* tls_segment
,
236 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
237 elfcpp::Elf_types
<64>::Elf_Addr value
,
239 elfcpp::Elf_types
<64>::Elf_Addr
,
240 section_size_type view_size
);
242 // Do a TLS General-Dynamic to Local-Exec transition.
244 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
245 Output_segment
* tls_segment
,
246 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
247 elfcpp::Elf_types
<64>::Elf_Addr value
,
249 section_size_type view_size
);
251 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
253 tls_desc_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
254 Output_segment
* tls_segment
,
255 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
256 elfcpp::Elf_types
<64>::Elf_Addr value
,
258 elfcpp::Elf_types
<64>::Elf_Addr
,
259 section_size_type view_size
);
261 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
263 tls_desc_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
264 Output_segment
* tls_segment
,
265 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
266 elfcpp::Elf_types
<64>::Elf_Addr value
,
268 section_size_type view_size
);
270 // Do a TLS Local-Dynamic to Local-Exec transition.
272 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
273 Output_segment
* tls_segment
,
274 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
275 elfcpp::Elf_types
<64>::Elf_Addr value
,
277 section_size_type view_size
);
279 // Do a TLS Initial-Exec to Local-Exec transition.
281 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
282 Output_segment
* tls_segment
,
283 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
284 elfcpp::Elf_types
<64>::Elf_Addr value
,
286 section_size_type view_size
);
288 // This is set if we should skip the next reloc, which should be a
289 // PLT32 reloc against ___tls_get_addr.
290 bool skip_call_tls_get_addr_
;
293 // A class which returns the size required for a relocation type,
294 // used while scanning relocs during a relocatable link.
295 class Relocatable_size_for_reloc
299 get_size_for_reloc(unsigned int, Relobj
*);
302 // Adjust TLS relocation type based on the options and whether this
303 // is a local symbol.
304 static tls::Tls_optimization
305 optimize_tls_reloc(bool is_final
, int r_type
);
307 // Get the GOT section, creating it if necessary.
308 Output_data_got
<64, false>*
309 got_section(Symbol_table
*, Layout
*);
311 // Get the GOT PLT section.
313 got_plt_section() const
315 gold_assert(this->got_plt_
!= NULL
);
316 return this->got_plt_
;
319 // Create the PLT section.
321 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
323 // Create a PLT entry for a global symbol.
325 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
327 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
329 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
331 // Create a GOT entry for the TLS module index.
333 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
334 Sized_relobj
<64, false>* object
);
336 // Get the PLT section.
337 Output_data_plt_x86_64
*
340 gold_assert(this->plt_
!= NULL
);
344 // Get the dynamic reloc section, creating it if necessary.
346 rela_dyn_section(Layout
*);
348 // Return true if the symbol may need a COPY relocation.
349 // References from an executable object to non-function symbols
350 // defined in a dynamic object may need a COPY relocation.
352 may_need_copy_reloc(Symbol
* gsym
)
354 return (!parameters
->options().shared()
355 && gsym
->is_from_dynobj()
356 && gsym
->type() != elfcpp::STT_FUNC
);
359 // Add a potential copy relocation.
361 copy_reloc(Symbol_table
* symtab
, Layout
* layout
, Relobj
* object
,
362 unsigned int shndx
, Output_section
* output_section
,
363 Symbol
* sym
, const elfcpp::Rela
<64, false>& reloc
)
365 this->copy_relocs_
.copy_reloc(symtab
, layout
,
366 symtab
->get_sized_symbol
<64>(sym
),
367 object
, shndx
, output_section
,
368 reloc
, this->rela_dyn_section(layout
));
371 // Information about this specific target which we pass to the
372 // general Target structure.
373 static const Target::Target_info x86_64_info
;
377 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
378 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
379 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
380 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
384 Output_data_got
<64, false>* got_
;
386 Output_data_plt_x86_64
* plt_
;
387 // The GOT PLT section.
388 Output_data_space
* got_plt_
;
389 // The dynamic reloc section.
390 Reloc_section
* rela_dyn_
;
391 // Relocs saved to avoid a COPY reloc.
392 Copy_relocs
<elfcpp::SHT_RELA
, 64, false> copy_relocs_
;
393 // Space for variables copied with a COPY reloc.
394 Output_data_space
* dynbss_
;
395 // Offset of the GOT entry for the TLS module index.
396 unsigned int got_mod_index_offset_
;
399 const Target::Target_info
Target_x86_64::x86_64_info
=
402 false, // is_big_endian
403 elfcpp::EM_X86_64
, // machine_code
404 false, // has_make_symbol
405 false, // has_resolve
406 true, // has_code_fill
407 true, // is_default_stack_executable
409 "/lib/ld64.so.1", // program interpreter
410 0x400000, // default_text_segment_address
411 0x1000, // abi_pagesize (overridable by -z max-page-size)
412 0x1000 // common_pagesize (overridable by -z common-page-size)
415 // Get the GOT section, creating it if necessary.
417 Output_data_got
<64, false>*
418 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
420 if (this->got_
== NULL
)
422 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
424 this->got_
= new Output_data_got
<64, false>();
426 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
427 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
430 // The old GNU linker creates a .got.plt section. We just
431 // create another set of data in the .got section. Note that we
432 // always create a PLT if we create a GOT, although the PLT
434 this->got_plt_
= new Output_data_space(8);
435 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
436 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
439 // The first three entries are reserved.
440 this->got_plt_
->set_current_data_size(3 * 8);
442 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
443 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
445 0, 0, elfcpp::STT_OBJECT
,
447 elfcpp::STV_HIDDEN
, 0,
454 // Get the dynamic reloc section, creating it if necessary.
456 Target_x86_64::Reloc_section
*
457 Target_x86_64::rela_dyn_section(Layout
* layout
)
459 if (this->rela_dyn_
== NULL
)
461 gold_assert(layout
!= NULL
);
462 this->rela_dyn_
= new Reloc_section();
463 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
464 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
466 return this->rela_dyn_
;
469 // A class to handle the PLT data.
471 class Output_data_plt_x86_64
: public Output_section_data
474 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
476 Output_data_plt_x86_64(Layout
*, Output_data_got
<64, false>*,
479 // Add an entry to the PLT.
481 add_entry(Symbol
* gsym
);
483 // Add the reserved TLSDESC_PLT entry to the PLT.
485 reserve_tlsdesc_entry(unsigned int got_offset
)
486 { this->tlsdesc_got_offset_
= got_offset
; }
488 // Return true if a TLSDESC_PLT entry has been reserved.
490 has_tlsdesc_entry() const
491 { return this->tlsdesc_got_offset_
!= -1U; }
493 // Return the GOT offset for the reserved TLSDESC_PLT entry.
495 get_tlsdesc_got_offset() const
496 { return this->tlsdesc_got_offset_
; }
498 // Return the offset of the reserved TLSDESC_PLT entry.
500 get_tlsdesc_plt_offset() const
501 { return (this->count_
+ 1) * plt_entry_size
; }
503 // Return the .rel.plt section data.
506 { return this->rel_
; }
510 do_adjust_output_section(Output_section
* os
);
513 // The size of an entry in the PLT.
514 static const int plt_entry_size
= 16;
516 // The first entry in the PLT.
517 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
518 // procedure linkage table for both programs and shared objects."
519 static unsigned char first_plt_entry
[plt_entry_size
];
521 // Other entries in the PLT for an executable.
522 static unsigned char plt_entry
[plt_entry_size
];
524 // The reserved TLSDESC entry in the PLT for an executable.
525 static unsigned char tlsdesc_plt_entry
[plt_entry_size
];
527 // Set the final size.
529 set_final_data_size();
531 // Write out the PLT data.
533 do_write(Output_file
*);
535 // The reloc section.
538 Output_data_got
<64, false>* got_
;
539 // The .got.plt section.
540 Output_data_space
* got_plt_
;
541 // The number of PLT entries.
543 // Offset of the reserved TLSDESC_GOT entry when needed.
544 unsigned int tlsdesc_got_offset_
;
547 // Create the PLT section. The ordinary .got section is an argument,
548 // since we need to refer to the start. We also create our own .got
549 // section just for PLT entries.
551 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
552 Output_data_got
<64, false>* got
,
553 Output_data_space
* got_plt
)
554 : Output_section_data(8), got_(got
), got_plt_(got_plt
), count_(0),
555 tlsdesc_got_offset_(-1U)
557 this->rel_
= new Reloc_section();
558 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
559 elfcpp::SHF_ALLOC
, this->rel_
);
563 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
565 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
566 // linker, and so do we.
570 // Add an entry to the PLT.
573 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
575 gold_assert(!gsym
->has_plt_offset());
577 // Note that when setting the PLT offset we skip the initial
578 // reserved PLT entry.
579 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
583 section_offset_type got_offset
= this->got_plt_
->current_data_size();
585 // Every PLT entry needs a GOT entry which points back to the PLT
586 // entry (this will be changed by the dynamic linker, normally
587 // lazily when the function is called).
588 this->got_plt_
->set_current_data_size(got_offset
+ 8);
590 // Every PLT entry needs a reloc.
591 gsym
->set_needs_dynsym_entry();
592 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
595 // Note that we don't need to save the symbol. The contents of the
596 // PLT are independent of which symbols are used. The symbols only
597 // appear in the relocations.
600 // Set the final size.
602 Output_data_plt_x86_64::set_final_data_size()
604 unsigned int count
= this->count_
;
605 if (this->has_tlsdesc_entry())
607 this->set_data_size((count
+ 1) * plt_entry_size
);
610 // The first entry in the PLT for an executable.
612 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
614 // From AMD64 ABI Draft 0.98, page 76
615 0xff, 0x35, // pushq contents of memory address
616 0, 0, 0, 0, // replaced with address of .got + 8
617 0xff, 0x25, // jmp indirect
618 0, 0, 0, 0, // replaced with address of .got + 16
619 0x90, 0x90, 0x90, 0x90 // noop (x4)
622 // Subsequent entries in the PLT for an executable.
624 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
626 // From AMD64 ABI Draft 0.98, page 76
627 0xff, 0x25, // jmpq indirect
628 0, 0, 0, 0, // replaced with address of symbol in .got
629 0x68, // pushq immediate
630 0, 0, 0, 0, // replaced with offset into relocation table
631 0xe9, // jmpq relative
632 0, 0, 0, 0 // replaced with offset to start of .plt
635 // The reserved TLSDESC entry in the PLT for an executable.
637 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry
[plt_entry_size
] =
639 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
640 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
641 0xff, 0x35, // pushq x(%rip)
642 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
643 0xff, 0x25, // jmpq *y(%rip)
644 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
649 // Write out the PLT. This uses the hand-coded instructions above,
650 // and adjusts them as needed. This is specified by the AMD64 ABI.
653 Output_data_plt_x86_64::do_write(Output_file
* of
)
655 const off_t offset
= this->offset();
656 const section_size_type oview_size
=
657 convert_to_section_size_type(this->data_size());
658 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
660 const off_t got_file_offset
= this->got_plt_
->offset();
661 const section_size_type got_size
=
662 convert_to_section_size_type(this->got_plt_
->data_size());
663 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
666 unsigned char* pov
= oview
;
668 // The base address of the .plt section.
669 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
670 // The base address of the .got section.
671 elfcpp::Elf_types
<32>::Elf_Addr got_base
= this->got_
->address();
672 // The base address of the PLT portion of the .got section,
673 // which is where the GOT pointer will point, and where the
674 // three reserved GOT entries are located.
675 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
677 memcpy(pov
, first_plt_entry
, plt_entry_size
);
678 // We do a jmp relative to the PC at the end of this instruction.
679 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 8
680 - (plt_address
+ 6));
681 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 16
682 - (plt_address
+ 12));
683 pov
+= plt_entry_size
;
685 unsigned char* got_pov
= got_view
;
687 memset(got_pov
, 0, 24);
690 unsigned int plt_offset
= plt_entry_size
;
691 unsigned int got_offset
= 24;
692 const unsigned int count
= this->count_
;
693 for (unsigned int plt_index
= 0;
696 pov
+= plt_entry_size
,
698 plt_offset
+= plt_entry_size
,
701 // Set and adjust the PLT entry itself.
702 memcpy(pov
, plt_entry
, plt_entry_size
);
703 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
704 (got_address
+ got_offset
705 - (plt_address
+ plt_offset
708 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
709 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
710 - (plt_offset
+ plt_entry_size
));
712 // Set the entry in the GOT.
713 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
716 if (this->has_tlsdesc_entry())
718 // Set and adjust the reserved TLSDESC PLT entry.
719 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
720 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
721 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
723 - (plt_address
+ plt_offset
725 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
728 - (plt_address
+ plt_offset
730 pov
+= plt_entry_size
;
733 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
734 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
736 of
->write_output_view(offset
, oview_size
, oview
);
737 of
->write_output_view(got_file_offset
, got_size
, got_view
);
740 // Create the PLT section.
743 Target_x86_64::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
745 if (this->plt_
== NULL
)
747 // Create the GOT sections first.
748 this->got_section(symtab
, layout
);
750 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_
,
752 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
754 | elfcpp::SHF_EXECINSTR
),
759 // Create a PLT entry for a global symbol.
762 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
765 if (gsym
->has_plt_offset())
768 if (this->plt_
== NULL
)
769 this->make_plt_section(symtab
, layout
);
771 this->plt_
->add_entry(gsym
);
774 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
777 Target_x86_64::reserve_tlsdesc_entries(Symbol_table
* symtab
,
780 if (this->plt_
== NULL
)
781 this->make_plt_section(symtab
, layout
);
783 if (!this->plt_
->has_tlsdesc_entry())
785 // Allocate the TLSDESC_GOT entry.
786 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
787 unsigned int got_offset
= got
->add_constant(0);
789 // Allocate the TLSDESC_PLT entry.
790 this->plt_
->reserve_tlsdesc_entry(got_offset
);
794 // Create a GOT entry for the TLS module index.
797 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
798 Sized_relobj
<64, false>* object
)
800 if (this->got_mod_index_offset_
== -1U)
802 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
803 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
804 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
805 unsigned int got_offset
= got
->add_constant(0);
806 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
808 got
->add_constant(0);
809 this->got_mod_index_offset_
= got_offset
;
811 return this->got_mod_index_offset_
;
814 // Optimize the TLS relocation type based on what we know about the
815 // symbol. IS_FINAL is true if the final address of this symbol is
816 // known at link time.
818 tls::Tls_optimization
819 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
821 // If we are generating a shared library, then we can't do anything
823 if (parameters
->options().shared())
824 return tls::TLSOPT_NONE
;
828 case elfcpp::R_X86_64_TLSGD
:
829 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
830 case elfcpp::R_X86_64_TLSDESC_CALL
:
831 // These are General-Dynamic which permits fully general TLS
832 // access. Since we know that we are generating an executable,
833 // we can convert this to Initial-Exec. If we also know that
834 // this is a local symbol, we can further switch to Local-Exec.
836 return tls::TLSOPT_TO_LE
;
837 return tls::TLSOPT_TO_IE
;
839 case elfcpp::R_X86_64_TLSLD
:
840 // This is Local-Dynamic, which refers to a local symbol in the
841 // dynamic TLS block. Since we know that we generating an
842 // executable, we can switch to Local-Exec.
843 return tls::TLSOPT_TO_LE
;
845 case elfcpp::R_X86_64_DTPOFF32
:
846 case elfcpp::R_X86_64_DTPOFF64
:
847 // Another Local-Dynamic reloc.
848 return tls::TLSOPT_TO_LE
;
850 case elfcpp::R_X86_64_GOTTPOFF
:
851 // These are Initial-Exec relocs which get the thread offset
852 // from the GOT. If we know that we are linking against the
853 // local symbol, we can switch to Local-Exec, which links the
854 // thread offset into the instruction.
856 return tls::TLSOPT_TO_LE
;
857 return tls::TLSOPT_NONE
;
859 case elfcpp::R_X86_64_TPOFF32
:
860 // When we already have Local-Exec, there is nothing further we
862 return tls::TLSOPT_NONE
;
869 // Report an unsupported relocation against a local symbol.
872 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
875 gold_error(_("%s: unsupported reloc %u against local symbol"),
876 object
->name().c_str(), r_type
);
879 // We are about to emit a dynamic relocation of type R_TYPE. If the
880 // dynamic linker does not support it, issue an error. The GNU linker
881 // only issues a non-PIC error for an allocated read-only section.
882 // Here we know the section is allocated, but we don't know that it is
883 // read-only. But we check for all the relocation types which the
884 // glibc dynamic linker supports, so it seems appropriate to issue an
885 // error even if the section is not read-only.
888 Target_x86_64::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
)
892 // These are the relocation types supported by glibc for x86_64.
893 case elfcpp::R_X86_64_RELATIVE
:
894 case elfcpp::R_X86_64_GLOB_DAT
:
895 case elfcpp::R_X86_64_JUMP_SLOT
:
896 case elfcpp::R_X86_64_DTPMOD64
:
897 case elfcpp::R_X86_64_DTPOFF64
:
898 case elfcpp::R_X86_64_TPOFF64
:
899 case elfcpp::R_X86_64_64
:
900 case elfcpp::R_X86_64_32
:
901 case elfcpp::R_X86_64_PC32
:
902 case elfcpp::R_X86_64_COPY
:
906 // This prevents us from issuing more than one error per reloc
907 // section. But we can still wind up issuing more than one
908 // error per object file.
909 if (this->issued_non_pic_error_
)
911 object
->error(_("requires unsupported dynamic reloc; "
912 "recompile with -fPIC"));
913 this->issued_non_pic_error_
= true;
916 case elfcpp::R_X86_64_NONE
:
921 // Scan a relocation for a local symbol.
924 Target_x86_64::Scan::local(const General_options
&,
925 Symbol_table
* symtab
,
927 Target_x86_64
* target
,
928 Sized_relobj
<64, false>* object
,
929 unsigned int data_shndx
,
930 Output_section
* output_section
,
931 const elfcpp::Rela
<64, false>& reloc
,
933 const elfcpp::Sym
<64, false>& lsym
)
937 case elfcpp::R_X86_64_NONE
:
938 case elfcpp::R_386_GNU_VTINHERIT
:
939 case elfcpp::R_386_GNU_VTENTRY
:
942 case elfcpp::R_X86_64_64
:
943 // If building a shared library (or a position-independent
944 // executable), we need to create a dynamic relocation for this
945 // location. The relocation applied at link time will apply the
946 // link-time value, so we flag the location with an
947 // R_X86_64_RELATIVE relocation so the dynamic loader can
948 // relocate it easily.
949 if (parameters
->options().output_is_position_independent())
951 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
952 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
953 rela_dyn
->add_local_relative(object
, r_sym
,
954 elfcpp::R_X86_64_RELATIVE
,
955 output_section
, data_shndx
,
956 reloc
.get_r_offset(),
957 reloc
.get_r_addend());
961 case elfcpp::R_X86_64_32
:
962 case elfcpp::R_X86_64_32S
:
963 case elfcpp::R_X86_64_16
:
964 case elfcpp::R_X86_64_8
:
965 // If building a shared library (or a position-independent
966 // executable), we need to create a dynamic relocation for this
967 // location. We can't use an R_X86_64_RELATIVE relocation
968 // because that is always a 64-bit relocation.
969 if (parameters
->options().output_is_position_independent())
971 this->check_non_pic(object
, r_type
);
973 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
974 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
976 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
977 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
978 data_shndx
, reloc
.get_r_offset(),
979 reloc
.get_r_addend());
983 gold_assert(lsym
.get_st_value() == 0);
984 rela_dyn
->add_local_section(object
, lsym
.get_st_shndx(),
985 r_type
, output_section
,
986 data_shndx
, reloc
.get_r_offset(),
987 reloc
.get_r_addend());
992 case elfcpp::R_X86_64_PC64
:
993 case elfcpp::R_X86_64_PC32
:
994 case elfcpp::R_X86_64_PC16
:
995 case elfcpp::R_X86_64_PC8
:
998 case elfcpp::R_X86_64_PLT32
:
999 // Since we know this is a local symbol, we can handle this as a
1003 case elfcpp::R_X86_64_GOTPC32
:
1004 case elfcpp::R_X86_64_GOTOFF64
:
1005 case elfcpp::R_X86_64_GOTPC64
:
1006 case elfcpp::R_X86_64_PLTOFF64
:
1007 // We need a GOT section.
1008 target
->got_section(symtab
, layout
);
1009 // For PLTOFF64, we'd normally want a PLT section, but since we
1010 // know this is a local symbol, no PLT is needed.
1013 case elfcpp::R_X86_64_GOT64
:
1014 case elfcpp::R_X86_64_GOT32
:
1015 case elfcpp::R_X86_64_GOTPCREL64
:
1016 case elfcpp::R_X86_64_GOTPCREL
:
1017 case elfcpp::R_X86_64_GOTPLT64
:
1019 // The symbol requires a GOT entry.
1020 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1021 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1022 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
1024 // If we are generating a shared object, we need to add a
1025 // dynamic relocation for this symbol's GOT entry.
1026 if (parameters
->options().output_is_position_independent())
1028 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1029 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1030 if (r_type
!= elfcpp::R_X86_64_GOT32
)
1031 rela_dyn
->add_local_relative(
1032 object
, r_sym
, elfcpp::R_X86_64_RELATIVE
, got
,
1033 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1036 this->check_non_pic(object
, r_type
);
1038 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1039 rela_dyn
->add_local(
1040 object
, r_sym
, r_type
, got
,
1041 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1045 // For GOTPLT64, we'd normally want a PLT section, but since
1046 // we know this is a local symbol, no PLT is needed.
1050 case elfcpp::R_X86_64_COPY
:
1051 case elfcpp::R_X86_64_GLOB_DAT
:
1052 case elfcpp::R_X86_64_JUMP_SLOT
:
1053 case elfcpp::R_X86_64_RELATIVE
:
1054 // These are outstanding tls relocs, which are unexpected when linking
1055 case elfcpp::R_X86_64_TPOFF64
:
1056 case elfcpp::R_X86_64_DTPMOD64
:
1057 case elfcpp::R_X86_64_TLSDESC
:
1058 gold_error(_("%s: unexpected reloc %u in object file"),
1059 object
->name().c_str(), r_type
);
1062 // These are initial tls relocs, which are expected when linking
1063 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1064 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1065 case elfcpp::R_X86_64_TLSDESC_CALL
:
1066 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1067 case elfcpp::R_X86_64_DTPOFF32
:
1068 case elfcpp::R_X86_64_DTPOFF64
:
1069 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1070 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1072 bool output_is_shared
= parameters
->options().shared();
1073 const tls::Tls_optimization optimized_type
1074 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
1077 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1078 if (optimized_type
== tls::TLSOPT_NONE
)
1080 // Create a pair of GOT entries for the module index and
1081 // dtv-relative offset.
1082 Output_data_got
<64, false>* got
1083 = target
->got_section(symtab
, layout
);
1084 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1085 got
->add_local_pair_with_rela(object
, r_sym
,
1086 lsym
.get_st_shndx(),
1088 target
->rela_dyn_section(layout
),
1089 elfcpp::R_X86_64_DTPMOD64
, 0);
1091 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1092 unsupported_reloc_local(object
, r_type
);
1095 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1096 if (optimized_type
== tls::TLSOPT_NONE
)
1098 // Create reserved PLT and GOT entries for the resolver.
1099 target
->reserve_tlsdesc_entries(symtab
, layout
);
1101 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1102 Output_data_got
<64, false>* got
1103 = target
->got_section(symtab
, layout
);
1104 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1105 got
->add_local_pair_with_rela(object
, r_sym
,
1106 lsym
.get_st_shndx(),
1108 target
->rela_dyn_section(layout
),
1109 elfcpp::R_X86_64_TLSDESC
, 0);
1111 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1112 unsupported_reloc_local(object
, r_type
);
1115 case elfcpp::R_X86_64_TLSDESC_CALL
:
1118 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1119 if (optimized_type
== tls::TLSOPT_NONE
)
1121 // Create a GOT entry for the module index.
1122 target
->got_mod_index_entry(symtab
, layout
, object
);
1124 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1125 unsupported_reloc_local(object
, r_type
);
1128 case elfcpp::R_X86_64_DTPOFF32
:
1129 case elfcpp::R_X86_64_DTPOFF64
:
1132 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1133 layout
->set_has_static_tls();
1134 if (optimized_type
== tls::TLSOPT_NONE
)
1136 // Create a GOT entry for the tp-relative offset.
1137 Output_data_got
<64, false>* got
1138 = target
->got_section(symtab
, layout
);
1139 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1140 got
->add_local_with_rela(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1141 target
->rela_dyn_section(layout
),
1142 elfcpp::R_X86_64_TPOFF64
);
1144 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1145 unsupported_reloc_local(object
, r_type
);
1148 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1149 layout
->set_has_static_tls();
1150 if (output_is_shared
)
1151 unsupported_reloc_local(object
, r_type
);
1160 case elfcpp::R_X86_64_SIZE32
:
1161 case elfcpp::R_X86_64_SIZE64
:
1163 gold_error(_("%s: unsupported reloc %u against local symbol"),
1164 object
->name().c_str(), r_type
);
1170 // Report an unsupported relocation against a global symbol.
1173 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
1174 unsigned int r_type
,
1177 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1178 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1181 // Scan a relocation for a global symbol.
1184 Target_x86_64::Scan::global(const General_options
&,
1185 Symbol_table
* symtab
,
1187 Target_x86_64
* target
,
1188 Sized_relobj
<64, false>* object
,
1189 unsigned int data_shndx
,
1190 Output_section
* output_section
,
1191 const elfcpp::Rela
<64, false>& reloc
,
1192 unsigned int r_type
,
1197 case elfcpp::R_X86_64_NONE
:
1198 case elfcpp::R_386_GNU_VTINHERIT
:
1199 case elfcpp::R_386_GNU_VTENTRY
:
1202 case elfcpp::R_X86_64_64
:
1203 case elfcpp::R_X86_64_32
:
1204 case elfcpp::R_X86_64_32S
:
1205 case elfcpp::R_X86_64_16
:
1206 case elfcpp::R_X86_64_8
:
1208 // Make a PLT entry if necessary.
1209 if (gsym
->needs_plt_entry())
1211 target
->make_plt_entry(symtab
, layout
, gsym
);
1212 // Since this is not a PC-relative relocation, we may be
1213 // taking the address of a function. In that case we need to
1214 // set the entry in the dynamic symbol table to the address of
1216 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1217 gsym
->set_needs_dynsym_value();
1219 // Make a dynamic relocation if necessary.
1220 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1222 if (target
->may_need_copy_reloc(gsym
))
1224 target
->copy_reloc(symtab
, layout
, object
,
1225 data_shndx
, output_section
, gsym
, reloc
);
1227 else if (r_type
== elfcpp::R_X86_64_64
1228 && gsym
->can_use_relative_reloc(false))
1230 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1231 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1232 output_section
, object
,
1233 data_shndx
, reloc
.get_r_offset(),
1234 reloc
.get_r_addend());
1238 this->check_non_pic(object
, r_type
);
1239 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1240 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1241 data_shndx
, reloc
.get_r_offset(),
1242 reloc
.get_r_addend());
1248 case elfcpp::R_X86_64_PC64
:
1249 case elfcpp::R_X86_64_PC32
:
1250 case elfcpp::R_X86_64_PC16
:
1251 case elfcpp::R_X86_64_PC8
:
1253 // Make a PLT entry if necessary.
1254 if (gsym
->needs_plt_entry())
1255 target
->make_plt_entry(symtab
, layout
, gsym
);
1256 // Make a dynamic relocation if necessary.
1257 int flags
= Symbol::NON_PIC_REF
;
1258 if (gsym
->type() == elfcpp::STT_FUNC
)
1259 flags
|= Symbol::FUNCTION_CALL
;
1260 if (gsym
->needs_dynamic_reloc(flags
))
1262 if (target
->may_need_copy_reloc(gsym
))
1264 target
->copy_reloc(symtab
, layout
, object
,
1265 data_shndx
, output_section
, gsym
, reloc
);
1269 this->check_non_pic(object
, r_type
);
1270 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1271 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1272 data_shndx
, reloc
.get_r_offset(),
1273 reloc
.get_r_addend());
1279 case elfcpp::R_X86_64_GOT64
:
1280 case elfcpp::R_X86_64_GOT32
:
1281 case elfcpp::R_X86_64_GOTPCREL64
:
1282 case elfcpp::R_X86_64_GOTPCREL
:
1283 case elfcpp::R_X86_64_GOTPLT64
:
1285 // The symbol requires a GOT entry.
1286 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1287 if (gsym
->final_value_is_known())
1288 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1291 // If this symbol is not fully resolved, we need to add a
1292 // dynamic relocation for it.
1293 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1294 if (gsym
->is_from_dynobj()
1295 || gsym
->is_undefined()
1296 || gsym
->is_preemptible())
1297 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1298 elfcpp::R_X86_64_GLOB_DAT
);
1301 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1302 rela_dyn
->add_global_relative(
1303 gsym
, elfcpp::R_X86_64_RELATIVE
, got
,
1304 gsym
->got_offset(GOT_TYPE_STANDARD
), 0);
1307 // For GOTPLT64, we also need a PLT entry (but only if the
1308 // symbol is not fully resolved).
1309 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1310 && !gsym
->final_value_is_known())
1311 target
->make_plt_entry(symtab
, layout
, gsym
);
1315 case elfcpp::R_X86_64_PLT32
:
1316 // If the symbol is fully resolved, this is just a PC32 reloc.
1317 // Otherwise we need a PLT entry.
1318 if (gsym
->final_value_is_known())
1320 // If building a shared library, we can also skip the PLT entry
1321 // if the symbol is defined in the output file and is protected
1323 if (gsym
->is_defined()
1324 && !gsym
->is_from_dynobj()
1325 && !gsym
->is_preemptible())
1327 target
->make_plt_entry(symtab
, layout
, gsym
);
1330 case elfcpp::R_X86_64_GOTPC32
:
1331 case elfcpp::R_X86_64_GOTOFF64
:
1332 case elfcpp::R_X86_64_GOTPC64
:
1333 case elfcpp::R_X86_64_PLTOFF64
:
1334 // We need a GOT section.
1335 target
->got_section(symtab
, layout
);
1336 // For PLTOFF64, we also need a PLT entry (but only if the
1337 // symbol is not fully resolved).
1338 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1339 && !gsym
->final_value_is_known())
1340 target
->make_plt_entry(symtab
, layout
, gsym
);
1343 case elfcpp::R_X86_64_COPY
:
1344 case elfcpp::R_X86_64_GLOB_DAT
:
1345 case elfcpp::R_X86_64_JUMP_SLOT
:
1346 case elfcpp::R_X86_64_RELATIVE
:
1347 // These are outstanding tls relocs, which are unexpected when linking
1348 case elfcpp::R_X86_64_TPOFF64
:
1349 case elfcpp::R_X86_64_DTPMOD64
:
1350 case elfcpp::R_X86_64_TLSDESC
:
1351 gold_error(_("%s: unexpected reloc %u in object file"),
1352 object
->name().c_str(), r_type
);
1355 // These are initial tls relocs, which are expected for global()
1356 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1357 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1358 case elfcpp::R_X86_64_TLSDESC_CALL
:
1359 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1360 case elfcpp::R_X86_64_DTPOFF32
:
1361 case elfcpp::R_X86_64_DTPOFF64
:
1362 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1363 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1365 const bool is_final
= gsym
->final_value_is_known();
1366 const tls::Tls_optimization optimized_type
1367 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1370 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1371 if (optimized_type
== tls::TLSOPT_NONE
)
1373 // Create a pair of GOT entries for the module index and
1374 // dtv-relative offset.
1375 Output_data_got
<64, false>* got
1376 = target
->got_section(symtab
, layout
);
1377 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_PAIR
,
1378 target
->rela_dyn_section(layout
),
1379 elfcpp::R_X86_64_DTPMOD64
,
1380 elfcpp::R_X86_64_DTPOFF64
);
1382 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1384 // Create a GOT entry for the tp-relative offset.
1385 Output_data_got
<64, false>* got
1386 = target
->got_section(symtab
, layout
);
1387 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1388 target
->rela_dyn_section(layout
),
1389 elfcpp::R_X86_64_TPOFF64
);
1391 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1392 unsupported_reloc_global(object
, r_type
, gsym
);
1395 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1396 if (optimized_type
== tls::TLSOPT_NONE
)
1398 // Create reserved PLT and GOT entries for the resolver.
1399 target
->reserve_tlsdesc_entries(symtab
, layout
);
1401 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1402 Output_data_got
<64, false>* got
1403 = target
->got_section(symtab
, layout
);
1404 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_DESC
,
1405 target
->rela_dyn_section(layout
),
1406 elfcpp::R_X86_64_TLSDESC
, 0);
1408 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1410 // Create a GOT entry for the tp-relative offset.
1411 Output_data_got
<64, false>* got
1412 = target
->got_section(symtab
, layout
);
1413 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1414 target
->rela_dyn_section(layout
),
1415 elfcpp::R_X86_64_TPOFF64
);
1417 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1418 unsupported_reloc_global(object
, r_type
, gsym
);
1421 case elfcpp::R_X86_64_TLSDESC_CALL
:
1424 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1425 if (optimized_type
== tls::TLSOPT_NONE
)
1427 // Create a GOT entry for the module index.
1428 target
->got_mod_index_entry(symtab
, layout
, object
);
1430 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1431 unsupported_reloc_global(object
, r_type
, gsym
);
1434 case elfcpp::R_X86_64_DTPOFF32
:
1435 case elfcpp::R_X86_64_DTPOFF64
:
1438 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1439 layout
->set_has_static_tls();
1440 if (optimized_type
== tls::TLSOPT_NONE
)
1442 // Create a GOT entry for the tp-relative offset.
1443 Output_data_got
<64, false>* got
1444 = target
->got_section(symtab
, layout
);
1445 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1446 target
->rela_dyn_section(layout
),
1447 elfcpp::R_X86_64_TPOFF64
);
1449 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1450 unsupported_reloc_global(object
, r_type
, gsym
);
1453 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1454 layout
->set_has_static_tls();
1455 if (parameters
->options().shared())
1456 unsupported_reloc_local(object
, r_type
);
1465 case elfcpp::R_X86_64_SIZE32
:
1466 case elfcpp::R_X86_64_SIZE64
:
1468 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1469 object
->name().c_str(), r_type
,
1470 gsym
->demangled_name().c_str());
1475 // Scan relocations for a section.
1478 Target_x86_64::scan_relocs(const General_options
& options
,
1479 Symbol_table
* symtab
,
1481 Sized_relobj
<64, false>* object
,
1482 unsigned int data_shndx
,
1483 unsigned int sh_type
,
1484 const unsigned char* prelocs
,
1486 Output_section
* output_section
,
1487 bool needs_special_offset_handling
,
1488 size_t local_symbol_count
,
1489 const unsigned char* plocal_symbols
)
1491 if (sh_type
== elfcpp::SHT_REL
)
1493 gold_error(_("%s: unsupported REL reloc section"),
1494 object
->name().c_str());
1498 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1499 Target_x86_64::Scan
>(
1509 needs_special_offset_handling
,
1514 // Finalize the sections.
1517 Target_x86_64::do_finalize_sections(Layout
* layout
)
1519 // Fill in some more dynamic tags.
1520 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1523 if (this->got_plt_
!= NULL
)
1524 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1526 if (this->plt_
!= NULL
)
1528 const Output_data
* od
= this->plt_
->rel_plt();
1529 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1530 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1531 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1532 if (this->plt_
->has_tlsdesc_entry())
1534 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
1535 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
1536 this->got_
->finalize_data_size();
1537 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
1538 this->plt_
, plt_offset
);
1539 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
1540 this->got_
, got_offset
);
1544 if (this->rela_dyn_
!= NULL
)
1546 const Output_data
* od
= this->rela_dyn_
;
1547 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1548 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1549 odyn
->add_constant(elfcpp::DT_RELAENT
,
1550 elfcpp::Elf_sizes
<64>::rela_size
);
1553 if (!parameters
->options().shared())
1555 // The value of the DT_DEBUG tag is filled in by the dynamic
1556 // linker at run time, and used by the debugger.
1557 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1561 // Emit any relocs we saved in an attempt to avoid generating COPY
1563 if (this->copy_relocs_
.any_saved_relocs())
1564 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1567 // Perform a relocation.
1570 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1571 Target_x86_64
* target
,
1573 const elfcpp::Rela
<64, false>& rela
,
1574 unsigned int r_type
,
1575 const Sized_symbol
<64>* gsym
,
1576 const Symbol_value
<64>* psymval
,
1577 unsigned char* view
,
1578 elfcpp::Elf_types
<64>::Elf_Addr address
,
1579 section_size_type view_size
)
1581 if (this->skip_call_tls_get_addr_
)
1583 if (r_type
!= elfcpp::R_X86_64_PLT32
1585 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1587 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1588 _("missing expected TLS relocation"));
1592 this->skip_call_tls_get_addr_
= false;
1597 // Pick the value to use for symbols defined in shared objects.
1598 Symbol_value
<64> symval
;
1600 && (gsym
->is_from_dynobj()
1601 || (parameters
->options().shared()
1602 && (gsym
->is_undefined() || gsym
->is_preemptible())))
1603 && gsym
->has_plt_offset())
1605 symval
.set_output_value(target
->plt_section()->address()
1606 + gsym
->plt_offset());
1610 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1611 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1613 // Get the GOT offset if needed.
1614 // The GOT pointer points to the end of the GOT section.
1615 // We need to subtract the size of the GOT section to get
1616 // the actual offset to use in the relocation.
1617 bool have_got_offset
= false;
1618 unsigned int got_offset
= 0;
1621 case elfcpp::R_X86_64_GOT32
:
1622 case elfcpp::R_X86_64_GOT64
:
1623 case elfcpp::R_X86_64_GOTPLT64
:
1624 case elfcpp::R_X86_64_GOTPCREL
:
1625 case elfcpp::R_X86_64_GOTPCREL64
:
1628 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1629 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
1633 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1634 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1635 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1636 - target
->got_size());
1638 have_got_offset
= true;
1647 case elfcpp::R_X86_64_NONE
:
1648 case elfcpp::R_386_GNU_VTINHERIT
:
1649 case elfcpp::R_386_GNU_VTENTRY
:
1652 case elfcpp::R_X86_64_64
:
1653 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1656 case elfcpp::R_X86_64_PC64
:
1657 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1661 case elfcpp::R_X86_64_32
:
1662 // FIXME: we need to verify that value + addend fits into 32 bits:
1663 // uint64_t x = value + addend;
1664 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1665 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1666 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1669 case elfcpp::R_X86_64_32S
:
1670 // FIXME: we need to verify that value + addend fits into 32 bits:
1671 // int64_t x = value + addend; // note this quantity is signed!
1672 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1673 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1676 case elfcpp::R_X86_64_PC32
:
1677 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1681 case elfcpp::R_X86_64_16
:
1682 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1685 case elfcpp::R_X86_64_PC16
:
1686 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1690 case elfcpp::R_X86_64_8
:
1691 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1694 case elfcpp::R_X86_64_PC8
:
1695 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1699 case elfcpp::R_X86_64_PLT32
:
1700 gold_assert(gsym
== NULL
1701 || gsym
->has_plt_offset()
1702 || gsym
->final_value_is_known()
1703 || (gsym
->is_defined()
1704 && !gsym
->is_from_dynobj()
1705 && !gsym
->is_preemptible()));
1706 // Note: while this code looks the same as for R_X86_64_PC32, it
1707 // behaves differently because psymval was set to point to
1708 // the PLT entry, rather than the symbol, in Scan::global().
1709 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1713 case elfcpp::R_X86_64_PLTOFF64
:
1716 gold_assert(gsym
->has_plt_offset()
1717 || gsym
->final_value_is_known());
1718 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
1719 got_address
= target
->got_section(NULL
, NULL
)->address();
1720 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
1721 addend
- got_address
);
1724 case elfcpp::R_X86_64_GOT32
:
1725 gold_assert(have_got_offset
);
1726 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1729 case elfcpp::R_X86_64_GOTPC32
:
1732 elfcpp::Elf_types
<64>::Elf_Addr value
;
1733 value
= target
->got_plt_section()->address();
1734 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1738 case elfcpp::R_X86_64_GOT64
:
1739 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1740 // Since we always add a PLT entry, this is equivalent.
1741 case elfcpp::R_X86_64_GOTPLT64
:
1742 gold_assert(have_got_offset
);
1743 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1746 case elfcpp::R_X86_64_GOTPC64
:
1749 elfcpp::Elf_types
<64>::Elf_Addr value
;
1750 value
= target
->got_plt_section()->address();
1751 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1755 case elfcpp::R_X86_64_GOTOFF64
:
1757 elfcpp::Elf_types
<64>::Elf_Addr value
;
1758 value
= (psymval
->value(object
, 0)
1759 - target
->got_plt_section()->address());
1760 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1764 case elfcpp::R_X86_64_GOTPCREL
:
1766 gold_assert(have_got_offset
);
1767 elfcpp::Elf_types
<64>::Elf_Addr value
;
1768 value
= target
->got_plt_section()->address() + got_offset
;
1769 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1773 case elfcpp::R_X86_64_GOTPCREL64
:
1775 gold_assert(have_got_offset
);
1776 elfcpp::Elf_types
<64>::Elf_Addr value
;
1777 value
= target
->got_plt_section()->address() + got_offset
;
1778 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1782 case elfcpp::R_X86_64_COPY
:
1783 case elfcpp::R_X86_64_GLOB_DAT
:
1784 case elfcpp::R_X86_64_JUMP_SLOT
:
1785 case elfcpp::R_X86_64_RELATIVE
:
1786 // These are outstanding tls relocs, which are unexpected when linking
1787 case elfcpp::R_X86_64_TPOFF64
:
1788 case elfcpp::R_X86_64_DTPMOD64
:
1789 case elfcpp::R_X86_64_TLSDESC
:
1790 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1791 _("unexpected reloc %u in object file"),
1795 // These are initial tls relocs, which are expected when linking
1796 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1797 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1798 case elfcpp::R_X86_64_TLSDESC_CALL
:
1799 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1800 case elfcpp::R_X86_64_DTPOFF32
:
1801 case elfcpp::R_X86_64_DTPOFF64
:
1802 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1803 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1804 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
1805 view
, address
, view_size
);
1808 case elfcpp::R_X86_64_SIZE32
:
1809 case elfcpp::R_X86_64_SIZE64
:
1811 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1812 _("unsupported reloc %u"),
1820 // Perform a TLS relocation.
1823 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1824 Target_x86_64
* target
,
1826 const elfcpp::Rela
<64, false>& rela
,
1827 unsigned int r_type
,
1828 const Sized_symbol
<64>* gsym
,
1829 const Symbol_value
<64>* psymval
,
1830 unsigned char* view
,
1831 elfcpp::Elf_types
<64>::Elf_Addr address
,
1832 section_size_type view_size
)
1834 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1836 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1837 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1839 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1841 const bool is_final
= (gsym
== NULL
1842 ? !parameters
->options().output_is_position_independent()
1843 : gsym
->final_value_is_known());
1844 const tls::Tls_optimization optimized_type
1845 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1848 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1849 if (optimized_type
== tls::TLSOPT_TO_LE
)
1851 gold_assert(tls_segment
!= NULL
);
1852 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1853 rela
, r_type
, value
, view
,
1859 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1860 ? GOT_TYPE_TLS_OFFSET
1861 : GOT_TYPE_TLS_PAIR
);
1862 unsigned int got_offset
;
1865 gold_assert(gsym
->has_got_offset(got_type
));
1866 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1870 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1871 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1872 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1873 - target
->got_size());
1875 if (optimized_type
== tls::TLSOPT_TO_IE
)
1877 gold_assert(tls_segment
!= NULL
);
1878 value
= target
->got_plt_section()->address() + got_offset
;
1879 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1880 value
, view
, address
, view_size
);
1883 else if (optimized_type
== tls::TLSOPT_NONE
)
1885 // Relocate the field with the offset of the pair of GOT
1887 value
= target
->got_plt_section()->address() + got_offset
;
1888 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
1893 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1894 _("unsupported reloc %u"), r_type
);
1897 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1898 case elfcpp::R_X86_64_TLSDESC_CALL
:
1899 if (optimized_type
== tls::TLSOPT_TO_LE
)
1901 gold_assert(tls_segment
!= NULL
);
1902 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
1903 rela
, r_type
, value
, view
,
1909 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1910 ? GOT_TYPE_TLS_OFFSET
1911 : GOT_TYPE_TLS_DESC
);
1912 unsigned int got_offset
;
1915 gold_assert(gsym
->has_got_offset(got_type
));
1916 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1920 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1921 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1922 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1923 - target
->got_size());
1925 if (optimized_type
== tls::TLSOPT_TO_IE
)
1927 gold_assert(tls_segment
!= NULL
);
1928 value
= target
->got_plt_section()->address() + got_offset
;
1929 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
1930 rela
, r_type
, value
, view
, address
,
1934 else if (optimized_type
== tls::TLSOPT_NONE
)
1936 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
1938 // Relocate the field with the offset of the pair of GOT
1940 value
= target
->got_plt_section()->address() + got_offset
;
1941 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
1947 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1948 _("unsupported reloc %u"), r_type
);
1951 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1952 if (optimized_type
== tls::TLSOPT_TO_LE
)
1954 gold_assert(tls_segment
!= NULL
);
1955 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1956 value
, view
, view_size
);
1959 else if (optimized_type
== tls::TLSOPT_NONE
)
1961 // Relocate the field with the offset of the GOT entry for
1962 // the module index.
1963 unsigned int got_offset
;
1964 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1965 - target
->got_size());
1966 value
= target
->got_plt_section()->address() + got_offset
;
1967 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
1971 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1972 _("unsupported reloc %u"), r_type
);
1975 case elfcpp::R_X86_64_DTPOFF32
:
1976 gold_assert(tls_segment
!= NULL
);
1977 if (optimized_type
== tls::TLSOPT_TO_LE
)
1978 value
-= tls_segment
->memsz();
1979 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1982 case elfcpp::R_X86_64_DTPOFF64
:
1983 gold_assert(tls_segment
!= NULL
);
1984 if (optimized_type
== tls::TLSOPT_TO_LE
)
1985 value
-= tls_segment
->memsz();
1986 Relocate_functions
<64, false>::rela64(view
, value
, 0);
1989 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1990 if (optimized_type
== tls::TLSOPT_TO_LE
)
1992 gold_assert(tls_segment
!= NULL
);
1993 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1994 rela
, r_type
, value
, view
,
1998 else if (optimized_type
== tls::TLSOPT_NONE
)
2000 // Relocate the field with the offset of the GOT entry for
2001 // the tp-relative offset of the symbol.
2002 unsigned int got_offset
;
2005 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
2006 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
2007 - target
->got_size());
2011 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2012 gold_assert(object
->local_has_got_offset(r_sym
,
2013 GOT_TYPE_TLS_OFFSET
));
2014 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
2015 - target
->got_size());
2017 value
= target
->got_plt_section()->address() + got_offset
;
2018 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2021 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2022 _("unsupported reloc type %u"),
2026 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2027 value
-= tls_segment
->memsz();
2028 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2033 // Do a relocation in which we convert a TLS General-Dynamic to an
2037 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
2040 const elfcpp::Rela
<64, false>& rela
,
2042 elfcpp::Elf_types
<64>::Elf_Addr value
,
2043 unsigned char* view
,
2044 elfcpp::Elf_types
<64>::Elf_Addr address
,
2045 section_size_type view_size
)
2047 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2048 // .word 0x6666; rex64; call __tls_get_addr
2049 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2051 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2052 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2054 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2055 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2056 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2057 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2059 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2061 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2062 Relocate_functions
<64, false>::pcrela32(view
+ 8, value
, addend
- 8, address
);
2064 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2066 this->skip_call_tls_get_addr_
= true;
2069 // Do a relocation in which we convert a TLS General-Dynamic to a
2073 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
2075 Output_segment
* tls_segment
,
2076 const elfcpp::Rela
<64, false>& rela
,
2078 elfcpp::Elf_types
<64>::Elf_Addr value
,
2079 unsigned char* view
,
2080 section_size_type view_size
)
2082 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2083 // .word 0x6666; rex64; call __tls_get_addr
2084 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2086 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2087 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2089 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2090 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2091 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2092 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2094 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2096 value
-= tls_segment
->memsz();
2097 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
2099 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2101 this->skip_call_tls_get_addr_
= true;
2104 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2107 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2108 const Relocate_info
<64, false>* relinfo
,
2111 const elfcpp::Rela
<64, false>& rela
,
2112 unsigned int r_type
,
2113 elfcpp::Elf_types
<64>::Elf_Addr value
,
2114 unsigned char* view
,
2115 elfcpp::Elf_types
<64>::Elf_Addr address
,
2116 section_size_type view_size
)
2118 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2120 // leaq foo@tlsdesc(%rip), %rax
2121 // ==> movq foo@gottpoff(%rip), %rax
2122 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2123 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2124 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2125 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2127 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2128 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2132 // call *foo@tlscall(%rax)
2134 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2135 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2136 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2137 view
[0] == 0xff && view
[1] == 0x10);
2143 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2146 Target_x86_64::Relocate::tls_desc_gd_to_le(
2147 const Relocate_info
<64, false>* relinfo
,
2149 Output_segment
* tls_segment
,
2150 const elfcpp::Rela
<64, false>& rela
,
2151 unsigned int r_type
,
2152 elfcpp::Elf_types
<64>::Elf_Addr value
,
2153 unsigned char* view
,
2154 section_size_type view_size
)
2156 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2158 // leaq foo@tlsdesc(%rip), %rax
2159 // ==> movq foo@tpoff, %rax
2160 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2161 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2162 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2163 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2166 value
-= tls_segment
->memsz();
2167 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2171 // call *foo@tlscall(%rax)
2173 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2174 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2175 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2176 view
[0] == 0xff && view
[1] == 0x10);
2183 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
2186 const elfcpp::Rela
<64, false>& rela
,
2188 elfcpp::Elf_types
<64>::Elf_Addr
,
2189 unsigned char* view
,
2190 section_size_type view_size
)
2192 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2193 // ... leq foo@dtpoff(%rax),%reg
2194 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2196 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2197 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
2199 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2200 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
2202 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
2204 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2206 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2208 this->skip_call_tls_get_addr_
= true;
2211 // Do a relocation in which we convert a TLS Initial-Exec to a
2215 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
2217 Output_segment
* tls_segment
,
2218 const elfcpp::Rela
<64, false>& rela
,
2220 elfcpp::Elf_types
<64>::Elf_Addr value
,
2221 unsigned char* view
,
2222 section_size_type view_size
)
2224 // We need to examine the opcodes to figure out which instruction we
2227 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2228 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2230 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2231 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2233 unsigned char op1
= view
[-3];
2234 unsigned char op2
= view
[-2];
2235 unsigned char op3
= view
[-1];
2236 unsigned char reg
= op3
>> 3;
2244 view
[-1] = 0xc0 | reg
;
2248 // Special handling for %rsp.
2252 view
[-1] = 0xc0 | reg
;
2260 view
[-1] = 0x80 | reg
| (reg
<< 3);
2263 value
-= tls_segment
->memsz();
2264 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2267 // Relocate section data.
2270 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
2271 unsigned int sh_type
,
2272 const unsigned char* prelocs
,
2274 Output_section
* output_section
,
2275 bool needs_special_offset_handling
,
2276 unsigned char* view
,
2277 elfcpp::Elf_types
<64>::Elf_Addr address
,
2278 section_size_type view_size
)
2280 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2282 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2283 Target_x86_64::Relocate
>(
2289 needs_special_offset_handling
,
2295 // Return the size of a relocation while scanning during a relocatable
2299 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2300 unsigned int r_type
,
2305 case elfcpp::R_X86_64_NONE
:
2306 case elfcpp::R_386_GNU_VTINHERIT
:
2307 case elfcpp::R_386_GNU_VTENTRY
:
2308 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2309 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2310 case elfcpp::R_X86_64_TLSDESC_CALL
:
2311 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2312 case elfcpp::R_X86_64_DTPOFF32
:
2313 case elfcpp::R_X86_64_DTPOFF64
:
2314 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2315 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2318 case elfcpp::R_X86_64_64
:
2319 case elfcpp::R_X86_64_PC64
:
2320 case elfcpp::R_X86_64_GOTOFF64
:
2321 case elfcpp::R_X86_64_GOTPC64
:
2322 case elfcpp::R_X86_64_PLTOFF64
:
2323 case elfcpp::R_X86_64_GOT64
:
2324 case elfcpp::R_X86_64_GOTPCREL64
:
2325 case elfcpp::R_X86_64_GOTPCREL
:
2326 case elfcpp::R_X86_64_GOTPLT64
:
2329 case elfcpp::R_X86_64_32
:
2330 case elfcpp::R_X86_64_32S
:
2331 case elfcpp::R_X86_64_PC32
:
2332 case elfcpp::R_X86_64_PLT32
:
2333 case elfcpp::R_X86_64_GOTPC32
:
2334 case elfcpp::R_X86_64_GOT32
:
2337 case elfcpp::R_X86_64_16
:
2338 case elfcpp::R_X86_64_PC16
:
2341 case elfcpp::R_X86_64_8
:
2342 case elfcpp::R_X86_64_PC8
:
2345 case elfcpp::R_X86_64_COPY
:
2346 case elfcpp::R_X86_64_GLOB_DAT
:
2347 case elfcpp::R_X86_64_JUMP_SLOT
:
2348 case elfcpp::R_X86_64_RELATIVE
:
2349 // These are outstanding tls relocs, which are unexpected when linking
2350 case elfcpp::R_X86_64_TPOFF64
:
2351 case elfcpp::R_X86_64_DTPMOD64
:
2352 case elfcpp::R_X86_64_TLSDESC
:
2353 object
->error(_("unexpected reloc %u in object file"), r_type
);
2356 case elfcpp::R_X86_64_SIZE32
:
2357 case elfcpp::R_X86_64_SIZE64
:
2359 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
2364 // Scan the relocs during a relocatable link.
2367 Target_x86_64::scan_relocatable_relocs(const General_options
& options
,
2368 Symbol_table
* symtab
,
2370 Sized_relobj
<64, false>* object
,
2371 unsigned int data_shndx
,
2372 unsigned int sh_type
,
2373 const unsigned char* prelocs
,
2375 Output_section
* output_section
,
2376 bool needs_special_offset_handling
,
2377 size_t local_symbol_count
,
2378 const unsigned char* plocal_symbols
,
2379 Relocatable_relocs
* rr
)
2381 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2383 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
2384 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2386 gold::scan_relocatable_relocs
<64, false, elfcpp::SHT_RELA
,
2387 Scan_relocatable_relocs
>(
2396 needs_special_offset_handling
,
2402 // Relocate a section during a relocatable link.
2405 Target_x86_64::relocate_for_relocatable(
2406 const Relocate_info
<64, false>* relinfo
,
2407 unsigned int sh_type
,
2408 const unsigned char* prelocs
,
2410 Output_section
* output_section
,
2411 off_t offset_in_output_section
,
2412 const Relocatable_relocs
* rr
,
2413 unsigned char* view
,
2414 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
2415 section_size_type view_size
,
2416 unsigned char* reloc_view
,
2417 section_size_type reloc_view_size
)
2419 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2421 gold::relocate_for_relocatable
<64, false, elfcpp::SHT_RELA
>(
2426 offset_in_output_section
,
2435 // Return the value to use for a dynamic which requires special
2436 // treatment. This is how we support equality comparisons of function
2437 // pointers across shared library boundaries, as described in the
2438 // processor specific ABI supplement.
2441 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
2443 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2444 return this->plt_section()->address() + gsym
->plt_offset();
2447 // Return a string used to fill a code section with nops to take up
2448 // the specified length.
2451 Target_x86_64::do_code_fill(section_size_type length
) const
2455 // Build a jmpq instruction to skip over the bytes.
2456 unsigned char jmp
[5];
2458 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2459 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2460 + std::string(length
- 5, '\0'));
2463 // Nop sequences of various lengths.
2464 const char nop1
[1] = { 0x90 }; // nop
2465 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2466 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2467 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2468 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2469 0x00 }; // leal 0(%esi,1),%esi
2470 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2472 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2474 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2475 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2476 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2477 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2479 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2480 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2482 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2483 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2485 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2486 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2487 0x00, 0x00, 0x00, 0x00 };
2488 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2489 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2490 0x27, 0x00, 0x00, 0x00,
2492 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2493 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2494 0xbc, 0x27, 0x00, 0x00,
2496 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2497 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2498 0x90, 0x90, 0x90, 0x90,
2501 const char* nops
[16] = {
2503 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2504 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2507 return std::string(nops
[length
], length
);
2510 // The selector for x86_64 object files.
2512 class Target_selector_x86_64
: public Target_selector
2515 Target_selector_x86_64()
2516 : Target_selector(elfcpp::EM_X86_64
, 64, false, "elf64-x86-64")
2520 do_instantiate_target()
2521 { return new Target_x86_64(); }
2524 Target_selector_x86_64 target_selector_x86_64
;
2526 } // End anonymous namespace.