1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 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
9 // modify it under the terms of the GNU Library General Public License
10 // as published by the Free Software Foundation; either version 2, or
11 // (at your option) any later version.
13 // In addition to the permissions in the GNU Library General Public
14 // License, the Free Software Foundation gives you unlimited
15 // permission to link the compiled version of this file into
16 // combinations with other programs, and to distribute those
17 // combinations without any restriction coming from the use of this
18 // file. (The Library Public License restrictions do apply in other
19 // respects; for example, they cover modification of the file, and
20 /// distribution when not linked into a combined executable.)
22 // This program is distributed in the hope that it will be useful, but
23 // WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 // Library General Public License for more details.
27 // You should have received a copy of the GNU Library General Public
28 // License along with this program; if not, write to the Free Software
29 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
37 #include "parameters.h"
45 #include "target-reloc.h"
46 #include "target-select.h"
54 class Output_data_plt_x86_64
;
56 // The x86_64 target class.
58 // http://www.x86-64.org/documentation/abi.pdf
59 // TLS info comes from
60 // http://people.redhat.com/drepper/tls.pdf
61 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
63 class Target_x86_64
: public Sized_target
<64, false>
66 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
67 // uses only Elf64_Rela relocation entries with explicit addends."
68 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
71 : Sized_target
<64, false>(&x86_64_info
),
72 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rela_dyn_(NULL
),
73 copy_relocs_(NULL
), dynbss_(NULL
), got_mod_index_offset_(-1U)
76 // Scan the relocations to look for symbol adjustments.
78 scan_relocs(const General_options
& options
,
81 Sized_relobj
<64, false>* object
,
82 unsigned int data_shndx
,
84 const unsigned char* prelocs
,
86 Output_section
* output_section
,
87 bool needs_special_offset_handling
,
88 size_t local_symbol_count
,
89 const unsigned char* plocal_symbols
);
91 // Finalize the sections.
93 do_finalize_sections(Layout
*);
95 // Return the value to use for a dynamic which requires special
98 do_dynsym_value(const Symbol
*) const;
100 // Relocate a section.
102 relocate_section(const Relocate_info
<64, false>*,
103 unsigned int sh_type
,
104 const unsigned char* prelocs
,
106 Output_section
* output_section
,
107 bool needs_special_offset_handling
,
109 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
110 section_size_type view_size
);
112 // Return a string used to fill a code section with nops.
114 do_code_fill(section_size_type length
);
116 // Return whether SYM is defined by the ABI.
118 do_is_defined_by_abi(Symbol
* sym
) const
119 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
121 // Return the size of the GOT section.
125 gold_assert(this->got_
!= NULL
);
126 return this->got_
->data_size();
130 // The class which scans relocations.
134 local(const General_options
& options
, Symbol_table
* symtab
,
135 Layout
* layout
, Target_x86_64
* target
,
136 Sized_relobj
<64, false>* object
,
137 unsigned int data_shndx
,
138 Output_section
* output_section
,
139 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
140 const elfcpp::Sym
<64, false>& lsym
);
143 global(const General_options
& options
, Symbol_table
* symtab
,
144 Layout
* layout
, Target_x86_64
* target
,
145 Sized_relobj
<64, false>* object
,
146 unsigned int data_shndx
,
147 Output_section
* output_section
,
148 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
152 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
155 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
159 // The class which implements relocation.
164 : skip_call_tls_get_addr_(false)
169 if (this->skip_call_tls_get_addr_
)
171 // FIXME: This needs to specify the location somehow.
172 gold_error(_("missing expected TLS relocation"));
176 // Do a relocation. Return false if the caller should not issue
177 // any warnings about this relocation.
179 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, size_t relnum
,
180 const elfcpp::Rela
<64, false>&,
181 unsigned int r_type
, const Sized_symbol
<64>*,
182 const Symbol_value
<64>*,
183 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
187 // Do a TLS relocation.
189 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
190 size_t relnum
, const elfcpp::Rela
<64, false>&,
191 unsigned int r_type
, const Sized_symbol
<64>*,
192 const Symbol_value
<64>*,
193 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
196 // Do a TLS General-Dynamic to Local-Exec transition.
198 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
199 Output_segment
* tls_segment
,
200 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
201 elfcpp::Elf_types
<64>::Elf_Addr value
,
203 section_size_type view_size
);
205 // Do a TLS General-Dynamic to Local-Exec transition.
207 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
208 Output_segment
* tls_segment
,
209 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
210 elfcpp::Elf_types
<64>::Elf_Addr value
,
212 section_size_type view_size
);
214 // Do a TLS Local-Dynamic to Local-Exec transition.
216 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
217 Output_segment
* tls_segment
,
218 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
219 elfcpp::Elf_types
<64>::Elf_Addr value
,
221 section_size_type view_size
);
223 // Do a TLS Initial-Exec to Local-Exec transition.
225 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
226 Output_segment
* tls_segment
,
227 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
228 elfcpp::Elf_types
<64>::Elf_Addr value
,
230 section_size_type view_size
);
232 // This is set if we should skip the next reloc, which should be a
233 // PLT32 reloc against ___tls_get_addr.
234 bool skip_call_tls_get_addr_
;
237 // Adjust TLS relocation type based on the options and whether this
238 // is a local symbol.
239 static tls::Tls_optimization
240 optimize_tls_reloc(bool is_final
, int r_type
);
242 // Get the GOT section, creating it if necessary.
243 Output_data_got
<64, false>*
244 got_section(Symbol_table
*, Layout
*);
246 // Get the GOT PLT section.
248 got_plt_section() const
250 gold_assert(this->got_plt_
!= NULL
);
251 return this->got_plt_
;
254 // Create a PLT entry for a global symbol.
256 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
258 // Create a GOT entry for the TLS module index.
260 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
261 Sized_relobj
<64, false>* object
);
263 // Get the PLT section.
264 Output_data_plt_x86_64
*
267 gold_assert(this->plt_
!= NULL
);
271 // Get the dynamic reloc section, creating it if necessary.
273 rela_dyn_section(Layout
*);
275 // Return true if the symbol may need a COPY relocation.
276 // References from an executable object to non-function symbols
277 // defined in a dynamic object may need a COPY relocation.
279 may_need_copy_reloc(Symbol
* gsym
)
281 return (!parameters
->output_is_shared()
282 && gsym
->is_from_dynobj()
283 && gsym
->type() != elfcpp::STT_FUNC
);
286 // Copy a relocation against a global symbol.
288 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
289 Sized_relobj
<64, false>*, unsigned int,
290 Output_section
*, Symbol
*, const elfcpp::Rela
<64, false>&);
292 // Information about this specific target which we pass to the
293 // general Target structure.
294 static const Target::Target_info x86_64_info
;
297 Output_data_got
<64, false>* got_
;
299 Output_data_plt_x86_64
* plt_
;
300 // The GOT PLT section.
301 Output_data_space
* got_plt_
;
302 // The dynamic reloc section.
303 Reloc_section
* rela_dyn_
;
304 // Relocs saved to avoid a COPY reloc.
305 Copy_relocs
<64, false>* copy_relocs_
;
306 // Space for variables copied with a COPY reloc.
307 Output_data_space
* dynbss_
;
308 // Offset of the GOT entry for the TLS module index;
309 unsigned int got_mod_index_offset_
;
312 const Target::Target_info
Target_x86_64::x86_64_info
=
315 false, // is_big_endian
316 elfcpp::EM_X86_64
, // machine_code
317 false, // has_make_symbol
318 false, // has_resolve
319 true, // has_code_fill
320 true, // is_default_stack_executable
321 "/lib/ld64.so.1", // program interpreter
322 0x400000, // default_text_segment_address
323 0x1000, // abi_pagesize
324 0x1000 // common_pagesize
327 // Get the GOT section, creating it if necessary.
329 Output_data_got
<64, false>*
330 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
332 if (this->got_
== NULL
)
334 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
336 this->got_
= new Output_data_got
<64, false>();
338 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
339 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
342 // The old GNU linker creates a .got.plt section. We just
343 // create another set of data in the .got section. Note that we
344 // always create a PLT if we create a GOT, although the PLT
346 this->got_plt_
= new Output_data_space(8);
347 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
348 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
351 // The first three entries are reserved.
352 this->got_plt_
->set_current_data_size(3 * 8);
354 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
355 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
357 0, 0, elfcpp::STT_OBJECT
,
359 elfcpp::STV_HIDDEN
, 0,
366 // Get the dynamic reloc section, creating it if necessary.
368 Target_x86_64::Reloc_section
*
369 Target_x86_64::rela_dyn_section(Layout
* layout
)
371 if (this->rela_dyn_
== NULL
)
373 gold_assert(layout
!= NULL
);
374 this->rela_dyn_
= new Reloc_section();
375 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
376 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
378 return this->rela_dyn_
;
381 // A class to handle the PLT data.
383 class Output_data_plt_x86_64
: public Output_section_data
386 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
388 Output_data_plt_x86_64(Layout
*, Output_data_space
*);
390 // Add an entry to the PLT.
392 add_entry(Symbol
* gsym
);
394 // Return the .rel.plt section data.
397 { return this->rel_
; }
401 do_adjust_output_section(Output_section
* os
);
404 // The size of an entry in the PLT.
405 static const int plt_entry_size
= 16;
407 // The first entry in the PLT.
408 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
409 // procedure linkage table for both programs and shared objects."
410 static unsigned char first_plt_entry
[plt_entry_size
];
412 // Other entries in the PLT for an executable.
413 static unsigned char plt_entry
[plt_entry_size
];
415 // Set the final size.
417 set_final_data_size()
418 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
420 // Write out the PLT data.
422 do_write(Output_file
*);
424 // The reloc section.
426 // The .got.plt section.
427 Output_data_space
* got_plt_
;
428 // The number of PLT entries.
432 // Create the PLT section. The ordinary .got section is an argument,
433 // since we need to refer to the start. We also create our own .got
434 // section just for PLT entries.
436 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
437 Output_data_space
* got_plt
)
438 : Output_section_data(8), got_plt_(got_plt
), count_(0)
440 this->rel_
= new Reloc_section();
441 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
442 elfcpp::SHF_ALLOC
, this->rel_
);
446 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
448 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
449 // linker, and so do we.
453 // Add an entry to the PLT.
456 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
458 gold_assert(!gsym
->has_plt_offset());
460 // Note that when setting the PLT offset we skip the initial
461 // reserved PLT entry.
462 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
466 section_offset_type got_offset
= this->got_plt_
->current_data_size();
468 // Every PLT entry needs a GOT entry which points back to the PLT
469 // entry (this will be changed by the dynamic linker, normally
470 // lazily when the function is called).
471 this->got_plt_
->set_current_data_size(got_offset
+ 8);
473 // Every PLT entry needs a reloc.
474 gsym
->set_needs_dynsym_entry();
475 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
478 // Note that we don't need to save the symbol. The contents of the
479 // PLT are independent of which symbols are used. The symbols only
480 // appear in the relocations.
483 // The first entry in the PLT for an executable.
485 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
487 // From AMD64 ABI Draft 0.98, page 76
488 0xff, 0x35, // pushq contents of memory address
489 0, 0, 0, 0, // replaced with address of .got + 8
490 0xff, 0x25, // jmp indirect
491 0, 0, 0, 0, // replaced with address of .got + 16
492 0x90, 0x90, 0x90, 0x90 // noop (x4)
495 // Subsequent entries in the PLT for an executable.
497 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
499 // From AMD64 ABI Draft 0.98, page 76
500 0xff, 0x25, // jmpq indirect
501 0, 0, 0, 0, // replaced with address of symbol in .got
502 0x68, // pushq immediate
503 0, 0, 0, 0, // replaced with offset into relocation table
504 0xe9, // jmpq relative
505 0, 0, 0, 0 // replaced with offset to start of .plt
508 // Write out the PLT. This uses the hand-coded instructions above,
509 // and adjusts them as needed. This is specified by the AMD64 ABI.
512 Output_data_plt_x86_64::do_write(Output_file
* of
)
514 const off_t offset
= this->offset();
515 const section_size_type oview_size
=
516 convert_to_section_size_type(this->data_size());
517 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
519 const off_t got_file_offset
= this->got_plt_
->offset();
520 const section_size_type got_size
=
521 convert_to_section_size_type(this->got_plt_
->data_size());
522 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
525 unsigned char* pov
= oview
;
527 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
528 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
530 memcpy(pov
, first_plt_entry
, plt_entry_size
);
531 // We do a jmp relative to the PC at the end of this instruction.
532 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 8
533 - (plt_address
+ 6));
534 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 16
535 - (plt_address
+ 12));
536 pov
+= plt_entry_size
;
538 unsigned char* got_pov
= got_view
;
540 memset(got_pov
, 0, 24);
543 unsigned int plt_offset
= plt_entry_size
;
544 unsigned int got_offset
= 24;
545 const unsigned int count
= this->count_
;
546 for (unsigned int plt_index
= 0;
549 pov
+= plt_entry_size
,
551 plt_offset
+= plt_entry_size
,
554 // Set and adjust the PLT entry itself.
555 memcpy(pov
, plt_entry
, plt_entry_size
);
556 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
557 (got_address
+ got_offset
558 - (plt_address
+ plt_offset
561 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
562 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
563 - (plt_offset
+ plt_entry_size
));
565 // Set the entry in the GOT.
566 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
569 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
570 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
572 of
->write_output_view(offset
, oview_size
, oview
);
573 of
->write_output_view(got_file_offset
, got_size
, got_view
);
576 // Create a PLT entry for a global symbol.
579 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
582 if (gsym
->has_plt_offset())
585 if (this->plt_
== NULL
)
587 // Create the GOT sections first.
588 this->got_section(symtab
, layout
);
590 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_plt_
);
591 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
593 | elfcpp::SHF_EXECINSTR
),
597 this->plt_
->add_entry(gsym
);
600 // Create a GOT entry for the TLS module index.
603 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
604 Sized_relobj
<64, false>* object
)
606 if (this->got_mod_index_offset_
== -1U)
608 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
609 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
610 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
611 unsigned int got_offset
= got
->add_constant(0);
612 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
614 got
->add_constant(0);
615 this->got_mod_index_offset_
= got_offset
;
617 return this->got_mod_index_offset_
;
620 // Handle a relocation against a non-function symbol defined in a
621 // dynamic object. The traditional way to handle this is to generate
622 // a COPY relocation to copy the variable at runtime from the shared
623 // object into the executable's data segment. However, this is
624 // undesirable in general, as if the size of the object changes in the
625 // dynamic object, the executable will no longer work correctly. If
626 // this relocation is in a writable section, then we can create a
627 // dynamic reloc and the dynamic linker will resolve it to the correct
628 // address at runtime. However, we do not want do that if the
629 // relocation is in a read-only section, as it would prevent the
630 // readonly segment from being shared. And if we have to eventually
631 // generate a COPY reloc, then any dynamic relocations will be
632 // useless. So this means that if this is a writable section, we need
633 // to save the relocation until we see whether we have to create a
634 // COPY relocation for this symbol for any other relocation.
637 Target_x86_64::copy_reloc(const General_options
* options
,
638 Symbol_table
* symtab
,
640 Sized_relobj
<64, false>* object
,
641 unsigned int data_shndx
,
642 Output_section
* output_section
,
644 const elfcpp::Rela
<64, false>& rela
)
646 Sized_symbol
<64>* ssym
;
647 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(64) (gsym
650 if (!Copy_relocs
<64, false>::need_copy_reloc(options
, object
,
653 // So far we do not need a COPY reloc. Save this relocation.
654 // If it turns out that we never need a COPY reloc for this
655 // symbol, then we will emit the relocation.
656 if (this->copy_relocs_
== NULL
)
657 this->copy_relocs_
= new Copy_relocs
<64, false>();
658 this->copy_relocs_
->save(ssym
, object
, data_shndx
, output_section
, rela
);
662 // Allocate space for this symbol in the .bss section.
664 elfcpp::Elf_types
<64>::Elf_WXword symsize
= ssym
->symsize();
666 // There is no defined way to determine the required alignment
667 // of the symbol. We pick the alignment based on the size. We
668 // set an arbitrary maximum of 256.
670 for (align
= 1; align
< 512; align
<<= 1)
671 if ((symsize
& align
) != 0)
674 if (this->dynbss_
== NULL
)
676 this->dynbss_
= new Output_data_space(align
);
677 layout
->add_output_section_data(".bss",
680 | elfcpp::SHF_WRITE
),
684 Output_data_space
* dynbss
= this->dynbss_
;
686 if (align
> dynbss
->addralign())
687 dynbss
->set_space_alignment(align
);
689 section_size_type dynbss_size
= dynbss
->current_data_size();
690 dynbss_size
= align_address(dynbss_size
, align
);
691 section_size_type offset
= dynbss_size
;
692 dynbss
->set_current_data_size(dynbss_size
+ symsize
);
694 symtab
->define_with_copy_reloc(this, ssym
, dynbss
, offset
);
696 // Add the COPY reloc.
697 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
698 rela_dyn
->add_global(ssym
, elfcpp::R_X86_64_COPY
, dynbss
, offset
, 0);
703 // Optimize the TLS relocation type based on what we know about the
704 // symbol. IS_FINAL is true if the final address of this symbol is
705 // known at link time.
707 tls::Tls_optimization
708 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
710 // If we are generating a shared library, then we can't do anything
712 if (parameters
->output_is_shared())
713 return tls::TLSOPT_NONE
;
717 case elfcpp::R_X86_64_TLSGD
:
718 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
719 case elfcpp::R_X86_64_TLSDESC_CALL
:
720 // These are General-Dynamic which permits fully general TLS
721 // access. Since we know that we are generating an executable,
722 // we can convert this to Initial-Exec. If we also know that
723 // this is a local symbol, we can further switch to Local-Exec.
725 return tls::TLSOPT_TO_LE
;
726 return tls::TLSOPT_TO_IE
;
728 case elfcpp::R_X86_64_TLSLD
:
729 // This is Local-Dynamic, which refers to a local symbol in the
730 // dynamic TLS block. Since we know that we generating an
731 // executable, we can switch to Local-Exec.
732 return tls::TLSOPT_TO_LE
;
734 case elfcpp::R_X86_64_DTPOFF32
:
735 case elfcpp::R_X86_64_DTPOFF64
:
736 // Another Local-Dynamic reloc.
737 return tls::TLSOPT_TO_LE
;
739 case elfcpp::R_X86_64_GOTTPOFF
:
740 // These are Initial-Exec relocs which get the thread offset
741 // from the GOT. If we know that we are linking against the
742 // local symbol, we can switch to Local-Exec, which links the
743 // thread offset into the instruction.
745 return tls::TLSOPT_TO_LE
;
746 return tls::TLSOPT_NONE
;
748 case elfcpp::R_X86_64_TPOFF32
:
749 // When we already have Local-Exec, there is nothing further we
751 return tls::TLSOPT_NONE
;
758 // Report an unsupported relocation against a local symbol.
761 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
764 gold_error(_("%s: unsupported reloc %u against local symbol"),
765 object
->name().c_str(), r_type
);
768 // Scan a relocation for a local symbol.
771 Target_x86_64::Scan::local(const General_options
&,
772 Symbol_table
* symtab
,
774 Target_x86_64
* target
,
775 Sized_relobj
<64, false>* object
,
776 unsigned int data_shndx
,
777 Output_section
* output_section
,
778 const elfcpp::Rela
<64, false>& reloc
,
780 const elfcpp::Sym
<64, false>& lsym
)
784 case elfcpp::R_X86_64_NONE
:
785 case elfcpp::R_386_GNU_VTINHERIT
:
786 case elfcpp::R_386_GNU_VTENTRY
:
789 case elfcpp::R_X86_64_64
:
790 // If building a shared library (or a position-independent
791 // executable), we need to create a dynamic relocation for
792 // this location. The relocation applied at link time will
793 // apply the link-time value, so we flag the location with
794 // an R_386_RELATIVE relocation so the dynamic loader can
795 // relocate it easily.
796 if (parameters
->output_is_position_independent())
798 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
799 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
800 rela_dyn
->add_local_relative(object
, r_sym
,
801 elfcpp::R_X86_64_RELATIVE
,
802 output_section
, data_shndx
,
803 reloc
.get_r_offset(),
804 reloc
.get_r_addend());
808 case elfcpp::R_X86_64_32
:
809 case elfcpp::R_X86_64_32S
:
810 case elfcpp::R_X86_64_16
:
811 case elfcpp::R_X86_64_8
:
812 // If building a shared library (or a position-independent
813 // executable), we need to create a dynamic relocation for
814 // this location. The relocation applied at link time will
815 // apply the link-time value, so we flag the location with
816 // an R_386_RELATIVE relocation so the dynamic loader can
817 // relocate it easily.
818 if (parameters
->output_is_position_independent())
820 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
821 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
822 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
823 data_shndx
, reloc
.get_r_offset(),
824 reloc
.get_r_addend());
828 case elfcpp::R_X86_64_PC64
:
829 case elfcpp::R_X86_64_PC32
:
830 case elfcpp::R_X86_64_PC16
:
831 case elfcpp::R_X86_64_PC8
:
834 case elfcpp::R_X86_64_PLT32
:
835 // Since we know this is a local symbol, we can handle this as a
839 case elfcpp::R_X86_64_GOTPC32
:
840 case elfcpp::R_X86_64_GOTOFF64
:
841 case elfcpp::R_X86_64_GOTPC64
:
842 case elfcpp::R_X86_64_PLTOFF64
:
843 // We need a GOT section.
844 target
->got_section(symtab
, layout
);
845 // For PLTOFF64, we'd normally want a PLT section, but since we
846 // know this is a local symbol, no PLT is needed.
849 case elfcpp::R_X86_64_GOT64
:
850 case elfcpp::R_X86_64_GOT32
:
851 case elfcpp::R_X86_64_GOTPCREL64
:
852 case elfcpp::R_X86_64_GOTPCREL
:
853 case elfcpp::R_X86_64_GOTPLT64
:
855 // The symbol requires a GOT entry.
856 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
857 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
858 if (got
->add_local(object
, r_sym
))
860 // If we are generating a shared object, we need to add a
861 // dynamic relocation for this symbol's GOT entry.
862 if (parameters
->output_is_position_independent())
864 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
865 // R_X86_64_RELATIVE assumes a 64-bit relocation.
866 if (r_type
!= elfcpp::R_X86_64_GOT32
)
867 rela_dyn
->add_local_relative(object
, r_sym
,
868 elfcpp::R_X86_64_RELATIVE
, got
,
869 object
->local_got_offset(r_sym
),
872 rela_dyn
->add_local(object
, r_sym
, r_type
,
873 got
, object
->local_got_offset(r_sym
), 0);
876 // For GOTPLT64, we'd normally want a PLT section, but since
877 // we know this is a local symbol, no PLT is needed.
881 case elfcpp::R_X86_64_COPY
:
882 case elfcpp::R_X86_64_GLOB_DAT
:
883 case elfcpp::R_X86_64_JUMP_SLOT
:
884 case elfcpp::R_X86_64_RELATIVE
:
885 // These are outstanding tls relocs, which are unexpected when linking
886 case elfcpp::R_X86_64_TPOFF64
:
887 case elfcpp::R_X86_64_DTPMOD64
:
888 case elfcpp::R_X86_64_TLSDESC
:
889 gold_error(_("%s: unexpected reloc %u in object file"),
890 object
->name().c_str(), r_type
);
893 // These are initial tls relocs, which are expected when linking
894 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
895 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
896 case elfcpp::R_X86_64_TLSDESC_CALL
:
897 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
898 case elfcpp::R_X86_64_DTPOFF32
:
899 case elfcpp::R_X86_64_DTPOFF64
:
900 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
901 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
903 bool output_is_shared
= parameters
->output_is_shared();
904 const tls::Tls_optimization optimized_type
905 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
908 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
909 if (optimized_type
== tls::TLSOPT_NONE
)
911 // Create a pair of GOT entries for the module index and
912 // dtv-relative offset.
913 Output_data_got
<64, false>* got
914 = target
->got_section(symtab
, layout
);
915 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
916 got
->add_local_tls_with_rela(object
, r_sym
,
917 lsym
.get_st_shndx(), true,
918 target
->rela_dyn_section(layout
),
919 elfcpp::R_X86_64_DTPMOD64
);
921 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
922 unsupported_reloc_local(object
, r_type
);
925 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
926 case elfcpp::R_X86_64_TLSDESC_CALL
:
927 // FIXME: If not relaxing to LE, we need to generate
928 // a GOT entry with a R_x86_64_TLSDESC reloc.
929 if (optimized_type
!= tls::TLSOPT_TO_LE
)
930 unsupported_reloc_local(object
, r_type
);
933 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
934 if (optimized_type
== tls::TLSOPT_NONE
)
936 // Create a GOT entry for the module index.
937 target
->got_mod_index_entry(symtab
, layout
, object
);
939 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
940 unsupported_reloc_local(object
, r_type
);
943 case elfcpp::R_X86_64_DTPOFF32
:
944 case elfcpp::R_X86_64_DTPOFF64
:
947 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
948 layout
->set_has_static_tls();
949 if (optimized_type
== tls::TLSOPT_NONE
)
951 // Create a GOT entry for the tp-relative offset.
952 Output_data_got
<64, false>* got
953 = target
->got_section(symtab
, layout
);
954 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
955 got
->add_local_with_rela(object
, r_sym
,
956 target
->rela_dyn_section(layout
),
957 elfcpp::R_X86_64_TPOFF64
);
959 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
960 unsupported_reloc_local(object
, r_type
);
963 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
964 layout
->set_has_static_tls();
965 if (output_is_shared
)
966 unsupported_reloc_local(object
, r_type
);
975 case elfcpp::R_X86_64_SIZE32
:
976 case elfcpp::R_X86_64_SIZE64
:
978 gold_error(_("%s: unsupported reloc %u against local symbol"),
979 object
->name().c_str(), r_type
);
985 // Report an unsupported relocation against a global symbol.
988 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
992 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
993 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
996 // Scan a relocation for a global symbol.
999 Target_x86_64::Scan::global(const General_options
& options
,
1000 Symbol_table
* symtab
,
1002 Target_x86_64
* target
,
1003 Sized_relobj
<64, false>* object
,
1004 unsigned int data_shndx
,
1005 Output_section
* output_section
,
1006 const elfcpp::Rela
<64, false>& reloc
,
1007 unsigned int r_type
,
1012 case elfcpp::R_X86_64_NONE
:
1013 case elfcpp::R_386_GNU_VTINHERIT
:
1014 case elfcpp::R_386_GNU_VTENTRY
:
1017 case elfcpp::R_X86_64_64
:
1018 case elfcpp::R_X86_64_32
:
1019 case elfcpp::R_X86_64_32S
:
1020 case elfcpp::R_X86_64_16
:
1021 case elfcpp::R_X86_64_8
:
1023 // Make a PLT entry if necessary.
1024 if (gsym
->needs_plt_entry())
1026 target
->make_plt_entry(symtab
, layout
, gsym
);
1027 // Since this is not a PC-relative relocation, we may be
1028 // taking the address of a function. In that case we need to
1029 // set the entry in the dynamic symbol table to the address of
1031 if (gsym
->is_from_dynobj())
1032 gsym
->set_needs_dynsym_value();
1034 // Make a dynamic relocation if necessary.
1035 if (gsym
->needs_dynamic_reloc(true, false))
1037 if (target
->may_need_copy_reloc(gsym
))
1039 target
->copy_reloc(&options
, symtab
, layout
, object
,
1040 data_shndx
, output_section
, gsym
, reloc
);
1042 else if (r_type
== elfcpp::R_X86_64_64
1043 && gsym
->can_use_relative_reloc(false))
1045 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1046 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1047 output_section
, object
,
1048 data_shndx
, reloc
.get_r_offset(),
1049 reloc
.get_r_addend());
1053 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1054 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1055 data_shndx
, reloc
.get_r_offset(),
1056 reloc
.get_r_addend());
1062 case elfcpp::R_X86_64_PC64
:
1063 case elfcpp::R_X86_64_PC32
:
1064 case elfcpp::R_X86_64_PC16
:
1065 case elfcpp::R_X86_64_PC8
:
1067 // Make a PLT entry if necessary.
1068 if (gsym
->needs_plt_entry())
1069 target
->make_plt_entry(symtab
, layout
, gsym
);
1070 // Make a dynamic relocation if necessary.
1071 bool is_function_call
= (gsym
->type() == elfcpp::STT_FUNC
);
1072 if (gsym
->needs_dynamic_reloc(false, is_function_call
))
1074 if (target
->may_need_copy_reloc(gsym
))
1076 target
->copy_reloc(&options
, symtab
, layout
, object
,
1077 data_shndx
, output_section
, gsym
, reloc
);
1081 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1082 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1083 data_shndx
, reloc
.get_r_offset(),
1084 reloc
.get_r_addend());
1090 case elfcpp::R_X86_64_GOT64
:
1091 case elfcpp::R_X86_64_GOT32
:
1092 case elfcpp::R_X86_64_GOTPCREL64
:
1093 case elfcpp::R_X86_64_GOTPCREL
:
1094 case elfcpp::R_X86_64_GOTPLT64
:
1096 // The symbol requires a GOT entry.
1097 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1098 if (gsym
->final_value_is_known())
1099 got
->add_global(gsym
);
1102 // If this symbol is not fully resolved, we need to add a
1103 // dynamic relocation for it.
1104 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1105 if (gsym
->is_from_dynobj() || gsym
->is_preemptible())
1106 got
->add_global_with_rela(gsym
, rela_dyn
,
1107 elfcpp::R_X86_64_GLOB_DAT
);
1110 if (got
->add_global(gsym
))
1111 rela_dyn
->add_global_relative(gsym
,
1112 elfcpp::R_X86_64_RELATIVE
,
1113 got
, gsym
->got_offset(), 0);
1116 // For GOTPLT64, we also need a PLT entry (but only if the
1117 // symbol is not fully resolved).
1118 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1119 && !gsym
->final_value_is_known())
1120 target
->make_plt_entry(symtab
, layout
, gsym
);
1124 case elfcpp::R_X86_64_PLT32
:
1125 // If the symbol is fully resolved, this is just a PC32 reloc.
1126 // Otherwise we need a PLT entry.
1127 if (gsym
->final_value_is_known())
1129 // If building a shared library, we can also skip the PLT entry
1130 // if the symbol is defined in the output file and is protected
1132 if (gsym
->is_defined()
1133 && !gsym
->is_from_dynobj()
1134 && !gsym
->is_preemptible())
1136 target
->make_plt_entry(symtab
, layout
, gsym
);
1139 case elfcpp::R_X86_64_GOTPC32
:
1140 case elfcpp::R_X86_64_GOTOFF64
:
1141 case elfcpp::R_X86_64_GOTPC64
:
1142 case elfcpp::R_X86_64_PLTOFF64
:
1143 // We need a GOT section.
1144 target
->got_section(symtab
, layout
);
1145 // For PLTOFF64, we also need a PLT entry (but only if the
1146 // symbol is not fully resolved).
1147 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1148 && !gsym
->final_value_is_known())
1149 target
->make_plt_entry(symtab
, layout
, gsym
);
1152 case elfcpp::R_X86_64_COPY
:
1153 case elfcpp::R_X86_64_GLOB_DAT
:
1154 case elfcpp::R_X86_64_JUMP_SLOT
:
1155 case elfcpp::R_X86_64_RELATIVE
:
1156 // These are outstanding tls relocs, which are unexpected when linking
1157 case elfcpp::R_X86_64_TPOFF64
:
1158 case elfcpp::R_X86_64_DTPMOD64
:
1159 case elfcpp::R_X86_64_TLSDESC
:
1160 gold_error(_("%s: unexpected reloc %u in object file"),
1161 object
->name().c_str(), r_type
);
1164 // These are initial tls relocs, which are expected for global()
1165 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1166 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1167 case elfcpp::R_X86_64_TLSDESC_CALL
:
1168 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1169 case elfcpp::R_X86_64_DTPOFF32
:
1170 case elfcpp::R_X86_64_DTPOFF64
:
1171 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1172 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1174 const bool is_final
= gsym
->final_value_is_known();
1175 const tls::Tls_optimization optimized_type
1176 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1179 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1180 if (optimized_type
== tls::TLSOPT_NONE
)
1182 // Create a pair of GOT entries for the module index and
1183 // dtv-relative offset.
1184 Output_data_got
<64, false>* got
1185 = target
->got_section(symtab
, layout
);
1186 got
->add_global_tls_with_rela(gsym
,
1187 target
->rela_dyn_section(layout
),
1188 elfcpp::R_X86_64_DTPMOD64
,
1189 elfcpp::R_X86_64_DTPOFF64
);
1191 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1193 // Create a GOT entry for the tp-relative offset.
1194 Output_data_got
<64, false>* got
1195 = target
->got_section(symtab
, layout
);
1196 got
->add_global_with_rela(gsym
,
1197 target
->rela_dyn_section(layout
),
1198 elfcpp::R_X86_64_TPOFF64
);
1200 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1201 unsupported_reloc_global(object
, r_type
, gsym
);
1204 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1205 case elfcpp::R_X86_64_TLSDESC_CALL
:
1206 // FIXME: If not relaxing to LE, we need to generate
1207 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1208 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1209 unsupported_reloc_global(object
, r_type
, gsym
);
1212 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1213 if (optimized_type
== tls::TLSOPT_NONE
)
1215 // Create a GOT entry for the module index.
1216 target
->got_mod_index_entry(symtab
, layout
, object
);
1218 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1219 unsupported_reloc_global(object
, r_type
, gsym
);
1222 case elfcpp::R_X86_64_DTPOFF32
:
1223 case elfcpp::R_X86_64_DTPOFF64
:
1226 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1227 layout
->set_has_static_tls();
1228 if (optimized_type
== tls::TLSOPT_NONE
)
1230 // Create a GOT entry for the tp-relative offset.
1231 Output_data_got
<64, false>* got
1232 = target
->got_section(symtab
, layout
);
1233 got
->add_global_with_rela(gsym
,
1234 target
->rela_dyn_section(layout
),
1235 elfcpp::R_X86_64_TPOFF64
);
1237 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1238 unsupported_reloc_global(object
, r_type
, gsym
);
1241 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1242 layout
->set_has_static_tls();
1243 if (parameters
->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 global symbol %s"),
1257 object
->name().c_str(), r_type
,
1258 gsym
->demangled_name().c_str());
1263 // Scan relocations for a section.
1266 Target_x86_64::scan_relocs(const General_options
& options
,
1267 Symbol_table
* symtab
,
1269 Sized_relobj
<64, false>* object
,
1270 unsigned int data_shndx
,
1271 unsigned int sh_type
,
1272 const unsigned char* prelocs
,
1274 Output_section
* output_section
,
1275 bool needs_special_offset_handling
,
1276 size_t local_symbol_count
,
1277 const unsigned char* plocal_symbols
)
1279 if (sh_type
== elfcpp::SHT_REL
)
1281 gold_error(_("%s: unsupported REL reloc section"),
1282 object
->name().c_str());
1286 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1287 Target_x86_64::Scan
>(
1297 needs_special_offset_handling
,
1302 // Finalize the sections.
1305 Target_x86_64::do_finalize_sections(Layout
* layout
)
1307 // Fill in some more dynamic tags.
1308 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1311 if (this->got_plt_
!= NULL
)
1312 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1314 if (this->plt_
!= NULL
)
1316 const Output_data
* od
= this->plt_
->rel_plt();
1317 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1318 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1319 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1322 if (this->rela_dyn_
!= NULL
)
1324 const Output_data
* od
= this->rela_dyn_
;
1325 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1326 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1327 odyn
->add_constant(elfcpp::DT_RELAENT
,
1328 elfcpp::Elf_sizes
<64>::rela_size
);
1331 if (!parameters
->output_is_shared())
1333 // The value of the DT_DEBUG tag is filled in by the dynamic
1334 // linker at run time, and used by the debugger.
1335 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1339 // Emit any relocs we saved in an attempt to avoid generating COPY
1341 if (this->copy_relocs_
== NULL
)
1343 if (this->copy_relocs_
->any_to_emit())
1345 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1346 this->copy_relocs_
->emit(rela_dyn
);
1348 delete this->copy_relocs_
;
1349 this->copy_relocs_
= NULL
;
1352 // Perform a relocation.
1355 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1356 Target_x86_64
* target
,
1358 const elfcpp::Rela
<64, false>& rela
,
1359 unsigned int r_type
,
1360 const Sized_symbol
<64>* gsym
,
1361 const Symbol_value
<64>* psymval
,
1362 unsigned char* view
,
1363 elfcpp::Elf_types
<64>::Elf_Addr address
,
1364 section_size_type view_size
)
1366 if (this->skip_call_tls_get_addr_
)
1368 if (r_type
!= elfcpp::R_X86_64_PLT32
1370 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1372 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1373 _("missing expected TLS relocation"));
1377 this->skip_call_tls_get_addr_
= false;
1382 // Pick the value to use for symbols defined in shared objects.
1383 Symbol_value
<64> symval
;
1385 && (gsym
->is_from_dynobj()
1386 || (parameters
->output_is_shared()
1387 && gsym
->is_preemptible()))
1388 && gsym
->has_plt_offset())
1390 symval
.set_output_value(target
->plt_section()->address()
1391 + gsym
->plt_offset());
1395 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1396 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1398 // Get the GOT offset if needed.
1399 // The GOT pointer points to the end of the GOT section.
1400 // We need to subtract the size of the GOT section to get
1401 // the actual offset to use in the relocation.
1402 bool have_got_offset
= false;
1403 unsigned int got_offset
= 0;
1406 case elfcpp::R_X86_64_GOT32
:
1407 case elfcpp::R_X86_64_GOT64
:
1408 case elfcpp::R_X86_64_GOTPLT64
:
1409 case elfcpp::R_X86_64_GOTPCREL
:
1410 case elfcpp::R_X86_64_GOTPCREL64
:
1413 gold_assert(gsym
->has_got_offset());
1414 got_offset
= gsym
->got_offset() - target
->got_size();
1418 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1419 gold_assert(object
->local_has_got_offset(r_sym
));
1420 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1422 have_got_offset
= true;
1431 case elfcpp::R_X86_64_NONE
:
1432 case elfcpp::R_386_GNU_VTINHERIT
:
1433 case elfcpp::R_386_GNU_VTENTRY
:
1436 case elfcpp::R_X86_64_64
:
1437 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1440 case elfcpp::R_X86_64_PC64
:
1441 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1445 case elfcpp::R_X86_64_32
:
1446 // FIXME: we need to verify that value + addend fits into 32 bits:
1447 // uint64_t x = value + addend;
1448 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1449 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1450 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1453 case elfcpp::R_X86_64_32S
:
1454 // FIXME: we need to verify that value + addend fits into 32 bits:
1455 // int64_t x = value + addend; // note this quantity is signed!
1456 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1457 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1460 case elfcpp::R_X86_64_PC32
:
1461 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1465 case elfcpp::R_X86_64_16
:
1466 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1469 case elfcpp::R_X86_64_PC16
:
1470 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1474 case elfcpp::R_X86_64_8
:
1475 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1478 case elfcpp::R_X86_64_PC8
:
1479 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1483 case elfcpp::R_X86_64_PLT32
:
1484 gold_assert(gsym
== NULL
1485 || gsym
->has_plt_offset()
1486 || gsym
->final_value_is_known()
1487 || (gsym
->is_defined()
1488 && !gsym
->is_from_dynobj()
1489 && !gsym
->is_preemptible()));
1490 // Note: while this code looks the same as for R_X86_64_PC32, it
1491 // behaves differently because psymval was set to point to
1492 // the PLT entry, rather than the symbol, in Scan::global().
1493 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1497 case elfcpp::R_X86_64_PLTOFF64
:
1500 gold_assert(gsym
->has_plt_offset()
1501 || gsym
->final_value_is_known());
1502 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
1503 got_address
= target
->got_section(NULL
, NULL
)->address();
1504 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
1505 addend
- got_address
);
1508 case elfcpp::R_X86_64_GOT32
:
1509 gold_assert(have_got_offset
);
1510 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1513 case elfcpp::R_X86_64_GOTPC32
:
1516 elfcpp::Elf_types
<64>::Elf_Addr value
;
1517 value
= target
->got_plt_section()->address();
1518 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1522 case elfcpp::R_X86_64_GOT64
:
1523 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1524 // Since we always add a PLT entry, this is equivalent.
1525 case elfcpp::R_X86_64_GOTPLT64
:
1526 gold_assert(have_got_offset
);
1527 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1530 case elfcpp::R_X86_64_GOTPC64
:
1533 elfcpp::Elf_types
<64>::Elf_Addr value
;
1534 value
= target
->got_plt_section()->address();
1535 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1539 case elfcpp::R_X86_64_GOTOFF64
:
1541 elfcpp::Elf_types
<64>::Elf_Addr value
;
1542 value
= (psymval
->value(object
, 0)
1543 - target
->got_plt_section()->address());
1544 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1548 case elfcpp::R_X86_64_GOTPCREL
:
1550 gold_assert(have_got_offset
);
1551 elfcpp::Elf_types
<64>::Elf_Addr value
;
1552 value
= target
->got_plt_section()->address() + got_offset
;
1553 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1557 case elfcpp::R_X86_64_GOTPCREL64
:
1559 gold_assert(have_got_offset
);
1560 elfcpp::Elf_types
<64>::Elf_Addr value
;
1561 value
= target
->got_plt_section()->address() + got_offset
;
1562 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1566 case elfcpp::R_X86_64_COPY
:
1567 case elfcpp::R_X86_64_GLOB_DAT
:
1568 case elfcpp::R_X86_64_JUMP_SLOT
:
1569 case elfcpp::R_X86_64_RELATIVE
:
1570 // These are outstanding tls relocs, which are unexpected when linking
1571 case elfcpp::R_X86_64_TPOFF64
:
1572 case elfcpp::R_X86_64_DTPMOD64
:
1573 case elfcpp::R_X86_64_TLSDESC
:
1574 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1575 _("unexpected reloc %u in object file"),
1579 // These are initial tls relocs, which are expected when linking
1580 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1581 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1582 case elfcpp::R_X86_64_TLSDESC_CALL
:
1583 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1584 case elfcpp::R_X86_64_DTPOFF32
:
1585 case elfcpp::R_X86_64_DTPOFF64
:
1586 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1587 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1588 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
1589 view
, address
, view_size
);
1592 case elfcpp::R_X86_64_SIZE32
:
1593 case elfcpp::R_X86_64_SIZE64
:
1595 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1596 _("unsupported reloc %u"),
1604 // Perform a TLS relocation.
1607 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1608 Target_x86_64
* target
,
1610 const elfcpp::Rela
<64, false>& rela
,
1611 unsigned int r_type
,
1612 const Sized_symbol
<64>* gsym
,
1613 const Symbol_value
<64>* psymval
,
1614 unsigned char* view
,
1615 elfcpp::Elf_types
<64>::Elf_Addr address
,
1616 section_size_type view_size
)
1618 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1620 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1621 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1623 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1625 const bool is_final
= (gsym
== NULL
1626 ? !parameters
->output_is_position_independent()
1627 : gsym
->final_value_is_known());
1628 const tls::Tls_optimization optimized_type
1629 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1632 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1633 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1634 case elfcpp::R_X86_64_TLSDESC_CALL
:
1635 if (optimized_type
== tls::TLSOPT_TO_LE
)
1637 gold_assert(tls_segment
!= NULL
);
1638 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1639 rela
, r_type
, value
, view
,
1645 unsigned int got_offset
;
1648 gold_assert(gsym
->has_tls_got_offset(true));
1649 got_offset
= gsym
->tls_got_offset(true) - target
->got_size();
1653 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1654 gold_assert(object
->local_has_tls_got_offset(r_sym
, true));
1655 got_offset
= (object
->local_tls_got_offset(r_sym
, true)
1656 - target
->got_size());
1658 if (optimized_type
== tls::TLSOPT_TO_IE
)
1660 gold_assert(tls_segment
!= NULL
);
1661 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1662 got_offset
, view
, view_size
);
1665 else if (optimized_type
== tls::TLSOPT_NONE
)
1667 // Relocate the field with the offset of the pair of GOT
1669 value
= target
->got_plt_section()->address() + got_offset
;
1670 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
1675 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1676 _("unsupported reloc %u"), r_type
);
1679 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1680 if (optimized_type
== tls::TLSOPT_TO_LE
)
1682 gold_assert(tls_segment
!= NULL
);
1683 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1684 value
, view
, view_size
);
1687 else if (optimized_type
== tls::TLSOPT_NONE
)
1689 // Relocate the field with the offset of the GOT entry for
1690 // the module index.
1691 unsigned int got_offset
;
1692 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1693 - target
->got_size());
1694 value
= target
->got_plt_section()->address() + got_offset
;
1695 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
1699 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1700 _("unsupported reloc %u"), r_type
);
1703 case elfcpp::R_X86_64_DTPOFF32
:
1704 gold_assert(tls_segment
!= NULL
);
1705 if (optimized_type
== tls::TLSOPT_TO_LE
)
1706 value
-= tls_segment
->memsz();
1707 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1710 case elfcpp::R_X86_64_DTPOFF64
:
1711 gold_assert(tls_segment
!= NULL
);
1712 if (optimized_type
== tls::TLSOPT_TO_LE
)
1713 value
-= tls_segment
->memsz();
1714 Relocate_functions
<64, false>::rela64(view
, value
, 0);
1717 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1718 if (optimized_type
== tls::TLSOPT_TO_LE
)
1720 gold_assert(tls_segment
!= NULL
);
1721 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1722 rela
, r_type
, value
, view
,
1726 else if (optimized_type
== tls::TLSOPT_NONE
)
1728 // Relocate the field with the offset of the GOT entry for
1729 // the tp-relative offset of the symbol.
1730 unsigned int got_offset
;
1733 gold_assert(gsym
->has_got_offset());
1734 got_offset
= gsym
->got_offset() - target
->got_size();
1738 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1739 gold_assert(object
->local_has_got_offset(r_sym
));
1740 got_offset
= (object
->local_got_offset(r_sym
)
1741 - target
->got_size());
1743 value
= target
->got_plt_section()->address() + got_offset
;
1744 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1747 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1748 _("unsupported reloc type %u"),
1752 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1753 value
-= tls_segment
->memsz();
1754 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1759 // Do a relocation in which we convert a TLS General-Dynamic to an
1763 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
1765 Output_segment
* tls_segment
,
1766 const elfcpp::Rela
<64, false>& rela
,
1768 elfcpp::Elf_types
<64>::Elf_Addr value
,
1769 unsigned char* view
,
1770 section_size_type view_size
)
1772 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1773 // .word 0x6666; rex64; call __tls_get_addr
1774 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
1776 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
1777 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
1779 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1780 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
1781 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1782 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
1784 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
1786 value
-= tls_segment
->memsz();
1787 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
1789 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1791 this->skip_call_tls_get_addr_
= true;
1794 // Do a relocation in which we convert a TLS General-Dynamic to a
1798 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
1800 Output_segment
* tls_segment
,
1801 const elfcpp::Rela
<64, false>& rela
,
1803 elfcpp::Elf_types
<64>::Elf_Addr value
,
1804 unsigned char* view
,
1805 section_size_type view_size
)
1807 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1808 // .word 0x6666; rex64; call __tls_get_addr
1809 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1811 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
1812 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
1814 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1815 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
1816 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1817 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
1819 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1821 value
-= tls_segment
->memsz();
1822 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
1824 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1826 this->skip_call_tls_get_addr_
= true;
1830 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
1833 const elfcpp::Rela
<64, false>& rela
,
1835 elfcpp::Elf_types
<64>::Elf_Addr
,
1836 unsigned char* view
,
1837 section_size_type view_size
)
1839 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1840 // ... leq foo@dtpoff(%rax),%reg
1841 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1843 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1844 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
1846 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1847 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
1849 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
1851 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1853 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1855 this->skip_call_tls_get_addr_
= true;
1858 // Do a relocation in which we convert a TLS Initial-Exec to a
1862 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
1864 Output_segment
* tls_segment
,
1865 const elfcpp::Rela
<64, false>& rela
,
1867 elfcpp::Elf_types
<64>::Elf_Addr value
,
1868 unsigned char* view
,
1869 section_size_type view_size
)
1871 // We need to examine the opcodes to figure out which instruction we
1874 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1875 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1877 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1878 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
1880 unsigned char op1
= view
[-3];
1881 unsigned char op2
= view
[-2];
1882 unsigned char op3
= view
[-1];
1883 unsigned char reg
= op3
>> 3;
1891 view
[-1] = 0xc0 | reg
;
1895 // Special handling for %rsp.
1899 view
[-1] = 0xc0 | reg
;
1907 view
[-1] = 0x80 | reg
| (reg
<< 3);
1910 value
-= tls_segment
->memsz();
1911 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1914 // Relocate section data.
1917 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
1918 unsigned int sh_type
,
1919 const unsigned char* prelocs
,
1921 Output_section
* output_section
,
1922 bool needs_special_offset_handling
,
1923 unsigned char* view
,
1924 elfcpp::Elf_types
<64>::Elf_Addr address
,
1925 section_size_type view_size
)
1927 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1929 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1930 Target_x86_64::Relocate
>(
1936 needs_special_offset_handling
,
1942 // Return the value to use for a dynamic which requires special
1943 // treatment. This is how we support equality comparisons of function
1944 // pointers across shared library boundaries, as described in the
1945 // processor specific ABI supplement.
1948 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
1950 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1951 return this->plt_section()->address() + gsym
->plt_offset();
1954 // Return a string used to fill a code section with nops to take up
1955 // the specified length.
1958 Target_x86_64::do_code_fill(section_size_type length
)
1962 // Build a jmpq instruction to skip over the bytes.
1963 unsigned char jmp
[5];
1965 elfcpp::Swap_unaligned
<64, false>::writeval(jmp
+ 1, length
- 5);
1966 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1967 + std::string(length
- 5, '\0'));
1970 // Nop sequences of various lengths.
1971 const char nop1
[1] = { 0x90 }; // nop
1972 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1973 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1974 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1975 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1976 0x00 }; // leal 0(%esi,1),%esi
1977 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1979 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1981 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1982 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1983 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1984 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1986 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1987 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1989 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1990 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1992 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1993 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1994 0x00, 0x00, 0x00, 0x00 };
1995 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1996 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1997 0x27, 0x00, 0x00, 0x00,
1999 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2000 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2001 0xbc, 0x27, 0x00, 0x00,
2003 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2004 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2005 0x90, 0x90, 0x90, 0x90,
2008 const char* nops
[16] = {
2010 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2011 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2014 return std::string(nops
[length
], length
);
2017 // The selector for x86_64 object files.
2019 class Target_selector_x86_64
: public Target_selector
2022 Target_selector_x86_64()
2023 : Target_selector(elfcpp::EM_X86_64
, 64, false)
2027 recognize(int machine
, int osabi
, int abiversion
);
2030 Target_x86_64
* target_
;
2033 // Recognize an x86_64 object file when we already know that the machine
2034 // number is EM_X86_64.
2037 Target_selector_x86_64::recognize(int, int, int)
2039 if (this->target_
== NULL
)
2040 this->target_
= new Target_x86_64();
2041 return this->target_
;
2044 Target_selector_x86_64 target_selector_x86_64
;
2046 } // End anonymous namespace.