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
)
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 size_t local_symbol_count
,
87 const unsigned char* plocal_symbols
,
88 Symbol
** global_symbols
);
90 // Finalize the sections.
92 do_finalize_sections(Layout
*);
94 // Return the value to use for a dynamic which requires special
97 do_dynsym_value(const Symbol
*) const;
99 // Relocate a section.
101 relocate_section(const Relocate_info
<64, false>*,
102 unsigned int sh_type
,
103 const unsigned char* prelocs
,
106 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
109 // Return a string used to fill a code section with nops.
111 do_code_fill(off_t length
);
113 // Return the size of the GOT section.
117 gold_assert(this->got_
!= NULL
);
118 return this->got_
->data_size();
122 // The class which scans relocations.
126 local(const General_options
& options
, Symbol_table
* symtab
,
127 Layout
* layout
, Target_x86_64
* target
,
128 Sized_relobj
<64, false>* object
,
129 unsigned int data_shndx
,
130 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
131 const elfcpp::Sym
<64, false>& lsym
);
134 global(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 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
142 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
145 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
149 // The class which implements relocation.
154 : skip_call_tls_get_addr_(false)
159 if (this->skip_call_tls_get_addr_
)
161 // FIXME: This needs to specify the location somehow.
162 gold_error(_("missing expected TLS relocation"));
166 // Do a relocation. Return false if the caller should not issue
167 // any warnings about this relocation.
169 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, size_t relnum
,
170 const elfcpp::Rela
<64, false>&,
171 unsigned int r_type
, const Sized_symbol
<64>*,
172 const Symbol_value
<64>*,
173 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
177 // Do a TLS relocation.
179 relocate_tls(const Relocate_info
<64, false>*, 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
, off_t
);
185 // Do a TLS General-Dynamic to Local-Exec transition.
187 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
188 Output_segment
* tls_segment
,
189 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
190 elfcpp::Elf_types
<64>::Elf_Addr value
,
194 // Do a TLS Local-Dynamic to Local-Exec transition.
196 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
197 Output_segment
* tls_segment
,
198 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
199 elfcpp::Elf_types
<64>::Elf_Addr value
,
203 // Do a TLS Initial-Exec to Local-Exec transition.
205 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
206 Output_segment
* tls_segment
,
207 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
208 elfcpp::Elf_types
<64>::Elf_Addr value
,
212 // This is set if we should skip the next reloc, which should be a
213 // PLT32 reloc against ___tls_get_addr.
214 bool skip_call_tls_get_addr_
;
217 // Adjust TLS relocation type based on the options and whether this
218 // is a local symbol.
219 static tls::Tls_optimization
220 optimize_tls_reloc(bool is_final
, int r_type
);
222 // Get the GOT section, creating it if necessary.
223 Output_data_got
<64, false>*
224 got_section(Symbol_table
*, Layout
*);
226 // Get the GOT PLT section.
228 got_plt_section() const
230 gold_assert(this->got_plt_
!= NULL
);
231 return this->got_plt_
;
234 // Create a PLT entry for a global symbol.
236 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
238 // Get the PLT section.
239 Output_data_plt_x86_64
*
242 gold_assert(this->plt_
!= NULL
);
246 // Get the dynamic reloc section, creating it if necessary.
248 rela_dyn_section(Layout
*);
250 // Copy a relocation against a global symbol.
252 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
253 Sized_relobj
<64, false>*, unsigned int,
254 Symbol
*, const elfcpp::Rela
<64, false>&);
256 // Information about this specific target which we pass to the
257 // general Target structure.
258 static const Target::Target_info x86_64_info
;
261 Output_data_got
<64, false>* got_
;
263 Output_data_plt_x86_64
* plt_
;
264 // The GOT PLT section.
265 Output_data_space
* got_plt_
;
266 // The dynamic reloc section.
267 Reloc_section
* rela_dyn_
;
268 // Relocs saved to avoid a COPY reloc.
269 Copy_relocs
<64, false>* copy_relocs_
;
270 // Space for variables copied with a COPY reloc.
271 Output_data_space
* dynbss_
;
274 const Target::Target_info
Target_x86_64::x86_64_info
=
277 false, // is_big_endian
278 elfcpp::EM_X86_64
, // machine_code
279 false, // has_make_symbol
280 false, // has_resolve
281 true, // has_code_fill
282 true, // is_default_stack_executable
283 "/lib/ld64.so.1", // program interpreter
284 0x400000, // default_text_segment_address
285 0x1000, // abi_pagesize
286 0x1000 // common_pagesize
289 // Get the GOT section, creating it if necessary.
291 Output_data_got
<64, false>*
292 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
294 if (this->got_
== NULL
)
296 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
298 this->got_
= new Output_data_got
<64, false>();
300 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
301 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
304 // The old GNU linker creates a .got.plt section. We just
305 // create another set of data in the .got section. Note that we
306 // always create a PLT if we create a GOT, although the PLT
308 this->got_plt_
= new Output_data_space(8);
309 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
310 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
313 // The first three entries are reserved.
314 this->got_plt_
->set_space_size(3 * 8);
316 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
317 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
319 0, 0, elfcpp::STT_OBJECT
,
321 elfcpp::STV_HIDDEN
, 0,
328 // Get the dynamic reloc section, creating it if necessary.
330 Target_x86_64::Reloc_section
*
331 Target_x86_64::rela_dyn_section(Layout
* layout
)
333 if (this->rela_dyn_
== NULL
)
335 gold_assert(layout
!= NULL
);
336 this->rela_dyn_
= new Reloc_section();
337 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
338 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
340 return this->rela_dyn_
;
343 // A class to handle the PLT data.
345 class Output_data_plt_x86_64
: public Output_section_data
348 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
350 Output_data_plt_x86_64(Layout
*, Output_data_space
*);
352 // Add an entry to the PLT.
354 add_entry(Symbol
* gsym
);
356 // Return the .rel.plt section data.
359 { return this->rel_
; }
363 do_adjust_output_section(Output_section
* os
);
366 // The size of an entry in the PLT.
367 static const int plt_entry_size
= 16;
369 // The first entry in the PLT.
370 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
371 // procedure linkage table for both programs and shared objects."
372 static unsigned char first_plt_entry
[plt_entry_size
];
374 // Other entries in the PLT for an executable.
375 static unsigned char plt_entry
[plt_entry_size
];
377 // Set the final size.
379 do_set_address(uint64_t, off_t
)
380 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
382 // Write out the PLT data.
384 do_write(Output_file
*);
386 // The reloc section.
388 // The .got.plt section.
389 Output_data_space
* got_plt_
;
390 // The number of PLT entries.
394 // Create the PLT section. The ordinary .got section is an argument,
395 // since we need to refer to the start. We also create our own .got
396 // section just for PLT entries.
398 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
399 Output_data_space
* got_plt
)
400 : Output_section_data(8), got_plt_(got_plt
), count_(0)
402 this->rel_
= new Reloc_section();
403 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
404 elfcpp::SHF_ALLOC
, this->rel_
);
408 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
410 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
411 // linker, and so do we.
415 // Add an entry to the PLT.
418 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
420 gold_assert(!gsym
->has_plt_offset());
422 // Note that when setting the PLT offset we skip the initial
423 // reserved PLT entry.
424 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
428 off_t got_offset
= this->got_plt_
->data_size();
430 // Every PLT entry needs a GOT entry which points back to the PLT
431 // entry (this will be changed by the dynamic linker, normally
432 // lazily when the function is called).
433 this->got_plt_
->set_space_size(got_offset
+ 8);
435 // Every PLT entry needs a reloc.
436 gsym
->set_needs_dynsym_entry();
437 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
440 // Note that we don't need to save the symbol. The contents of the
441 // PLT are independent of which symbols are used. The symbols only
442 // appear in the relocations.
445 // The first entry in the PLT for an executable.
447 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
449 // From AMD64 ABI Draft 0.98, page 76
450 0xff, 0x35, // pushq contents of memory address
451 0, 0, 0, 0, // replaced with address of .got + 4
452 0xff, 0x25, // jmp indirect
453 0, 0, 0, 0, // replaced with address of .got + 8
454 0x90, 0x90, 0x90, 0x90 // noop (x4)
457 // Subsequent entries in the PLT for an executable.
459 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
461 // From AMD64 ABI Draft 0.98, page 76
462 0xff, 0x25, // jmpq indirect
463 0, 0, 0, 0, // replaced with address of symbol in .got
464 0x68, // pushq immediate
465 0, 0, 0, 0, // replaced with offset into relocation table
466 0xe9, // jmpq relative
467 0, 0, 0, 0 // replaced with offset to start of .plt
470 // Write out the PLT. This uses the hand-coded instructions above,
471 // and adjusts them as needed. This is specified by the AMD64 ABI.
474 Output_data_plt_x86_64::do_write(Output_file
* of
)
476 const off_t offset
= this->offset();
477 const off_t oview_size
= this->data_size();
478 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
480 const off_t got_file_offset
= this->got_plt_
->offset();
481 const off_t got_size
= this->got_plt_
->data_size();
482 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
485 unsigned char* pov
= oview
;
487 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
488 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
490 memcpy(pov
, first_plt_entry
, plt_entry_size
);
491 if (!parameters
->output_is_shared())
493 // We do a jmp relative to the PC at the end of this instruction.
494 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 8
495 - (plt_address
+ 6));
496 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 16
497 - (plt_address
+ 12));
499 pov
+= plt_entry_size
;
501 unsigned char* got_pov
= got_view
;
503 memset(got_pov
, 0, 24);
506 unsigned int plt_offset
= plt_entry_size
;
507 unsigned int got_offset
= 24;
508 const unsigned int count
= this->count_
;
509 for (unsigned int plt_index
= 0;
512 pov
+= plt_entry_size
,
514 plt_offset
+= plt_entry_size
,
517 // Set and adjust the PLT entry itself.
518 memcpy(pov
, plt_entry
, plt_entry_size
);
519 if (parameters
->output_is_shared())
520 // FIXME(csilvers): what's the right thing to write here?
521 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
523 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
524 (got_address
+ got_offset
525 - (plt_address
+ plt_offset
528 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
529 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
530 - (plt_offset
+ plt_entry_size
));
532 // Set the entry in the GOT.
533 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
536 gold_assert(pov
- oview
== oview_size
);
537 gold_assert(got_pov
- got_view
== got_size
);
539 of
->write_output_view(offset
, oview_size
, oview
);
540 of
->write_output_view(got_file_offset
, got_size
, got_view
);
543 // Create a PLT entry for a global symbol.
546 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
549 if (gsym
->has_plt_offset())
552 if (this->plt_
== NULL
)
554 // Create the GOT sections first.
555 this->got_section(symtab
, layout
);
557 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_plt_
);
558 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
560 | elfcpp::SHF_EXECINSTR
),
564 this->plt_
->add_entry(gsym
);
567 // Handle a relocation against a non-function symbol defined in a
568 // dynamic object. The traditional way to handle this is to generate
569 // a COPY relocation to copy the variable at runtime from the shared
570 // object into the executable's data segment. However, this is
571 // undesirable in general, as if the size of the object changes in the
572 // dynamic object, the executable will no longer work correctly. If
573 // this relocation is in a writable section, then we can create a
574 // dynamic reloc and the dynamic linker will resolve it to the correct
575 // address at runtime. However, we do not want do that if the
576 // relocation is in a read-only section, as it would prevent the
577 // readonly segment from being shared. And if we have to eventually
578 // generate a COPY reloc, then any dynamic relocations will be
579 // useless. So this means that if this is a writable section, we need
580 // to save the relocation until we see whether we have to create a
581 // COPY relocation for this symbol for any other relocation.
584 Target_x86_64::copy_reloc(const General_options
* options
,
585 Symbol_table
* symtab
,
587 Sized_relobj
<64, false>* object
,
588 unsigned int data_shndx
, Symbol
* gsym
,
589 const elfcpp::Rela
<64, false>& rela
)
591 Sized_symbol
<64>* ssym
;
592 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(64) (gsym
595 if (!Copy_relocs
<64, false>::need_copy_reloc(options
, object
,
598 // So far we do not need a COPY reloc. Save this relocation.
599 // If it turns out that we never need a COPY reloc for this
600 // symbol, then we will emit the relocation.
601 if (this->copy_relocs_
== NULL
)
602 this->copy_relocs_
= new Copy_relocs
<64, false>();
603 this->copy_relocs_
->save(ssym
, object
, data_shndx
, rela
);
607 // Allocate space for this symbol in the .bss section.
609 elfcpp::Elf_types
<64>::Elf_WXword symsize
= ssym
->symsize();
611 // There is no defined way to determine the required alignment
612 // of the symbol. We pick the alignment based on the size. We
613 // set an arbitrary maximum of 256.
615 for (align
= 1; align
< 512; align
<<= 1)
616 if ((symsize
& align
) != 0)
619 if (this->dynbss_
== NULL
)
621 this->dynbss_
= new Output_data_space(align
);
622 layout
->add_output_section_data(".bss",
625 | elfcpp::SHF_WRITE
),
629 Output_data_space
* dynbss
= this->dynbss_
;
631 if (align
> dynbss
->addralign())
632 dynbss
->set_space_alignment(align
);
634 off_t dynbss_size
= dynbss
->data_size();
635 dynbss_size
= align_address(dynbss_size
, align
);
636 off_t offset
= dynbss_size
;
637 dynbss
->set_space_size(dynbss_size
+ symsize
);
639 symtab
->define_with_copy_reloc(this, ssym
, dynbss
, offset
);
641 // Add the COPY reloc.
642 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
643 rela_dyn
->add_global(ssym
, elfcpp::R_X86_64_COPY
, dynbss
, offset
, 0);
648 // Optimize the TLS relocation type based on what we know about the
649 // symbol. IS_FINAL is true if the final address of this symbol is
650 // known at link time.
652 tls::Tls_optimization
653 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
655 // If we are generating a shared library, then we can't do anything
657 if (parameters
->output_is_shared())
658 return tls::TLSOPT_NONE
;
662 case elfcpp::R_X86_64_TLSGD
:
663 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
664 case elfcpp::R_X86_64_TLSDESC_CALL
:
665 // These are General-Dynamic which permits fully general TLS
666 // access. Since we know that we are generating an executable,
667 // we can convert this to Initial-Exec. If we also know that
668 // this is a local symbol, we can further switch to Local-Exec.
670 return tls::TLSOPT_TO_LE
;
671 return tls::TLSOPT_TO_IE
;
673 case elfcpp::R_X86_64_TLSLD
:
674 // This is Local-Dynamic, which refers to a local symbol in the
675 // dynamic TLS block. Since we know that we generating an
676 // executable, we can switch to Local-Exec.
677 return tls::TLSOPT_TO_LE
;
679 case elfcpp::R_X86_64_DTPOFF32
:
680 case elfcpp::R_X86_64_DTPOFF64
:
681 // Another Local-Dynamic reloc.
682 return tls::TLSOPT_TO_LE
;
684 case elfcpp::R_X86_64_GOTTPOFF
:
685 // These are Initial-Exec relocs which get the thread offset
686 // from the GOT. If we know that we are linking against the
687 // local symbol, we can switch to Local-Exec, which links the
688 // thread offset into the instruction.
690 return tls::TLSOPT_TO_LE
;
691 return tls::TLSOPT_NONE
;
693 case elfcpp::R_X86_64_TPOFF32
:
694 // When we already have Local-Exec, there is nothing further we
696 return tls::TLSOPT_NONE
;
703 // Report an unsupported relocation against a local symbol.
706 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
709 gold_error(_("%s: unsupported reloc %u against local symbol"),
710 object
->name().c_str(), r_type
);
713 // Scan a relocation for a local symbol.
716 Target_x86_64::Scan::local(const General_options
&,
717 Symbol_table
* symtab
,
719 Target_x86_64
* target
,
720 Sized_relobj
<64, false>* object
,
721 unsigned int data_shndx
,
722 const elfcpp::Rela
<64, false>& reloc
,
724 const elfcpp::Sym
<64, false>&)
728 case elfcpp::R_X86_64_NONE
:
729 case elfcpp::R_386_GNU_VTINHERIT
:
730 case elfcpp::R_386_GNU_VTENTRY
:
733 case elfcpp::R_X86_64_64
:
734 case elfcpp::R_X86_64_32
:
735 case elfcpp::R_X86_64_32S
:
736 case elfcpp::R_X86_64_16
:
737 case elfcpp::R_X86_64_8
:
738 // If building a shared library (or a position-independent
739 // executable), we need to create a dynamic relocation for
740 // this location. The relocation applied at link time will
741 // apply the link-time value, so we flag the location with
742 // an R_386_RELATIVE relocation so the dynamic loader can
743 // relocate it easily.
744 if (parameters
->output_is_position_independent())
746 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
747 if (r_type
== elfcpp::R_X86_64_64
)
748 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
749 data_shndx
, reloc
.get_r_offset(), 0);
752 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
753 rela_dyn
->add_local(object
, r_sym
, r_type
, data_shndx
,
754 reloc
.get_r_offset(),
755 reloc
.get_r_addend());
760 case elfcpp::R_X86_64_PC64
:
761 case elfcpp::R_X86_64_PC32
:
762 case elfcpp::R_X86_64_PC16
:
763 case elfcpp::R_X86_64_PC8
:
766 case elfcpp::R_X86_64_PLT32
:
767 // Since we know this is a local symbol, we can handle this as a
771 case elfcpp::R_X86_64_GOTPC32
:
772 case elfcpp::R_X86_64_GOTOFF64
:
773 case elfcpp::R_X86_64_GOTPC64
:
774 case elfcpp::R_X86_64_PLTOFF64
:
775 // We need a GOT section.
776 target
->got_section(symtab
, layout
);
777 // For PLTOFF64, we'd normally want a PLT section, but since we
778 // know this is a local symbol, no PLT is needed.
781 case elfcpp::R_X86_64_GOT64
:
782 case elfcpp::R_X86_64_GOT32
:
783 case elfcpp::R_X86_64_GOTPCREL64
:
784 case elfcpp::R_X86_64_GOTPCREL
:
785 case elfcpp::R_X86_64_GOTPLT64
:
787 // The symbol requires a GOT entry.
788 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
789 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
790 if (got
->add_local(object
, r_sym
))
792 // If we are generating a shared object, we need to add a
793 // dynamic RELATIVE relocation for this symbol.
794 if (parameters
->output_is_position_independent())
796 // FIXME: R_X86_64_RELATIVE assumes a 64-bit relocation.
797 gold_assert(r_type
!= elfcpp::R_X86_64_GOT32
);
799 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
800 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
801 data_shndx
, reloc
.get_r_offset(), 0);
804 // For GOTPLT64, we'd normally want a PLT section, but since
805 // we know this is a local symbol, no PLT is needed.
809 case elfcpp::R_X86_64_COPY
:
810 case elfcpp::R_X86_64_GLOB_DAT
:
811 case elfcpp::R_X86_64_JUMP_SLOT
:
812 case elfcpp::R_X86_64_RELATIVE
:
813 // These are outstanding tls relocs, which are unexpected when linking
814 case elfcpp::R_X86_64_TPOFF64
:
815 case elfcpp::R_X86_64_DTPMOD64
:
816 case elfcpp::R_X86_64_TLSDESC
:
817 gold_error(_("%s: unexpected reloc %u in object file"),
818 object
->name().c_str(), r_type
);
821 // These are initial tls relocs, which are expected when linking
822 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
823 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
824 case elfcpp::R_X86_64_TLSDESC_CALL
:
825 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
826 case elfcpp::R_X86_64_DTPOFF32
:
827 case elfcpp::R_X86_64_DTPOFF64
:
828 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
829 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
831 bool output_is_shared
= parameters
->output_is_shared();
832 const tls::Tls_optimization optimized_type
833 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
836 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
837 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
838 case elfcpp::R_X86_64_TLSDESC_CALL
:
839 // FIXME: If not relaxing to LE, we need to generate
840 // DTPMOD64 and DTPOFF64 relocs.
841 if (optimized_type
!= tls::TLSOPT_TO_LE
)
842 unsupported_reloc_local(object
, r_type
);
845 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
846 case elfcpp::R_X86_64_DTPOFF32
:
847 case elfcpp::R_X86_64_DTPOFF64
:
848 // FIXME: If not relaxing to LE, we need to generate a
850 if (optimized_type
!= tls::TLSOPT_TO_LE
)
851 unsupported_reloc_local(object
, r_type
);
854 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
855 // FIXME: If not relaxing to LE, we need to generate a
857 if (optimized_type
!= tls::TLSOPT_TO_LE
)
858 unsupported_reloc_local(object
, r_type
);
861 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
862 // FIXME: If generating a shared object, we need to copy
863 // this relocation into the object.
864 gold_assert(!output_is_shared
);
873 case elfcpp::R_X86_64_SIZE32
:
874 case elfcpp::R_X86_64_SIZE64
:
876 gold_error(_("%s: unsupported reloc %u against local symbol"),
877 object
->name().c_str(), r_type
);
883 // Report an unsupported relocation against a global symbol.
886 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
890 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
891 object
->name().c_str(), r_type
, gsym
->name());
894 // Scan a relocation for a global symbol.
897 Target_x86_64::Scan::global(const General_options
& options
,
898 Symbol_table
* symtab
,
900 Target_x86_64
* target
,
901 Sized_relobj
<64, false>* object
,
902 unsigned int data_shndx
,
903 const elfcpp::Rela
<64, false>& reloc
,
909 case elfcpp::R_X86_64_NONE
:
910 case elfcpp::R_386_GNU_VTINHERIT
:
911 case elfcpp::R_386_GNU_VTENTRY
:
914 case elfcpp::R_X86_64_64
:
915 case elfcpp::R_X86_64_PC64
:
916 case elfcpp::R_X86_64_32
:
917 case elfcpp::R_X86_64_32S
:
918 case elfcpp::R_X86_64_PC32
:
919 case elfcpp::R_X86_64_16
:
920 case elfcpp::R_X86_64_PC16
:
921 case elfcpp::R_X86_64_8
:
922 case elfcpp::R_X86_64_PC8
:
924 bool is_pcrel
= (r_type
== elfcpp::R_X86_64_PC64
925 || r_type
== elfcpp::R_X86_64_PC32
926 || r_type
== elfcpp::R_X86_64_PC16
927 || r_type
== elfcpp::R_X86_64_PC8
);
929 if (gsym
->is_from_dynobj()
930 || (parameters
->output_is_shared()
931 && gsym
->is_preemptible()))
933 // (a) This symbol is defined in a dynamic object. If it is a
934 // function, we make a PLT entry. Otherwise we need to
935 // either generate a COPY reloc or copy this reloc.
936 // (b) We are building a shared object and this symbol is
937 // preemptible. If it is a function, we make a PLT entry.
938 // Otherwise, we copy the reloc.
939 if (gsym
->type() == elfcpp::STT_FUNC
)
941 target
->make_plt_entry(symtab
, layout
, gsym
);
943 // If this is not a PC relative reference, then we may
944 // be taking the address of the function. In that case
945 // we need to set the entry in the dynamic symbol table
946 // to the address of the PLT entry. We will also need to
947 // create a dynamic relocation.
950 if (gsym
->is_from_dynobj())
951 gsym
->set_needs_dynsym_value();
952 if (parameters
->output_is_position_independent())
954 Reloc_section
* rela_dyn
=
955 target
->rela_dyn_section(layout
);
956 rela_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
957 reloc
.get_r_offset(),
958 reloc
.get_r_addend());
962 else if (parameters
->output_is_shared())
964 // We do not make COPY relocs in shared objects.
965 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
966 rela_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
967 reloc
.get_r_offset(),
968 reloc
.get_r_addend());
971 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
974 else if (!is_pcrel
&& parameters
->output_is_position_independent())
976 // This is not a PC-relative reference, so we need to generate
977 // a dynamic relocation. At this point, we know the symbol
978 // is not preemptible, so we can use the RELATIVE relocation.
979 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
980 if (r_type
== elfcpp::R_X86_64_64
)
981 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
983 reloc
.get_r_offset(), 0);
985 rela_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
986 reloc
.get_r_offset(),
987 reloc
.get_r_addend());
992 case elfcpp::R_X86_64_GOT64
:
993 case elfcpp::R_X86_64_GOT32
:
994 case elfcpp::R_X86_64_GOTPCREL64
:
995 case elfcpp::R_X86_64_GOTPCREL
:
996 case elfcpp::R_X86_64_GOTPLT64
:
998 // The symbol requires a GOT entry.
999 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1000 if (got
->add_global(gsym
))
1002 // If this symbol is not fully resolved, we need to add a
1003 // dynamic relocation for it.
1004 if (!gsym
->final_value_is_known())
1006 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1007 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
, got
,
1008 gsym
->got_offset(), 0);
1011 // For GOTPLT64, we also need a PLT entry (but only if the
1012 // symbol is not fully resolved).
1013 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1014 && !gsym
->final_value_is_known())
1015 target
->make_plt_entry(symtab
, layout
, gsym
);
1019 case elfcpp::R_X86_64_PLT32
:
1020 // If the symbol is fully resolved, this is just a PC32 reloc.
1021 // Otherwise we need a PLT entry.
1022 if (gsym
->final_value_is_known())
1024 // If building a shared library, we can also skip the PLT entry
1025 // if the symbol is defined in the output file and is protected
1027 if (gsym
->is_defined()
1028 && !gsym
->is_from_dynobj()
1029 && !gsym
->is_preemptible())
1031 target
->make_plt_entry(symtab
, layout
, gsym
);
1034 case elfcpp::R_X86_64_GOTPC32
:
1035 case elfcpp::R_X86_64_GOTOFF64
:
1036 case elfcpp::R_X86_64_GOTPC64
:
1037 case elfcpp::R_X86_64_PLTOFF64
:
1038 // We need a GOT section.
1039 target
->got_section(symtab
, layout
);
1040 // For PLTOFF64, we also need a PLT entry (but only if the
1041 // symbol is not fully resolved).
1042 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1043 && !gsym
->final_value_is_known())
1044 target
->make_plt_entry(symtab
, layout
, gsym
);
1047 case elfcpp::R_X86_64_COPY
:
1048 case elfcpp::R_X86_64_GLOB_DAT
:
1049 case elfcpp::R_X86_64_JUMP_SLOT
:
1050 case elfcpp::R_X86_64_RELATIVE
:
1051 // These are outstanding tls relocs, which are unexpected when linking
1052 case elfcpp::R_X86_64_TPOFF64
:
1053 case elfcpp::R_X86_64_DTPMOD64
:
1054 case elfcpp::R_X86_64_TLSDESC
:
1055 gold_error(_("%s: unexpected reloc %u in object file"),
1056 object
->name().c_str(), r_type
);
1059 // These are initial tls relocs, which are expected for global()
1060 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1061 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1062 case elfcpp::R_X86_64_TLSDESC_CALL
:
1063 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1064 case elfcpp::R_X86_64_DTPOFF32
:
1065 case elfcpp::R_X86_64_DTPOFF64
:
1066 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1067 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1069 const bool is_final
= gsym
->final_value_is_known();
1070 const tls::Tls_optimization optimized_type
1071 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1074 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1075 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1076 case elfcpp::R_X86_64_TLSDESC_CALL
:
1077 // FIXME: If not relaxing to LE, we need to generate
1078 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1079 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1080 unsupported_reloc_global(object
, r_type
, gsym
);
1083 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1084 case elfcpp::R_X86_64_DTPOFF32
:
1085 case elfcpp::R_X86_64_DTPOFF64
:
1086 // FIXME: If not relaxing to LE, we need to generate a
1088 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1089 unsupported_reloc_global(object
, r_type
, gsym
);
1092 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1093 // FIXME: If not relaxing to LE, we need to generate a
1095 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1096 unsupported_reloc_global(object
, r_type
, gsym
);
1099 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1100 // FIXME: If generating a shared object, we need to copy
1101 // this relocation into the object.
1102 gold_assert(is_final
);
1111 case elfcpp::R_X86_64_SIZE32
:
1112 case elfcpp::R_X86_64_SIZE64
:
1114 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1115 object
->name().c_str(), r_type
, gsym
->name());
1120 // Scan relocations for a section.
1123 Target_x86_64::scan_relocs(const General_options
& options
,
1124 Symbol_table
* symtab
,
1126 Sized_relobj
<64, false>* object
,
1127 unsigned int data_shndx
,
1128 unsigned int sh_type
,
1129 const unsigned char* prelocs
,
1131 size_t local_symbol_count
,
1132 const unsigned char* plocal_symbols
,
1133 Symbol
** global_symbols
)
1135 if (sh_type
== elfcpp::SHT_REL
)
1137 gold_error(_("%s: unsupported REL reloc section"),
1138 object
->name().c_str());
1142 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1143 Target_x86_64::Scan
>(
1157 // Finalize the sections.
1160 Target_x86_64::do_finalize_sections(Layout
* layout
)
1162 // Fill in some more dynamic tags.
1163 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1166 if (this->got_plt_
!= NULL
)
1167 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1169 if (this->plt_
!= NULL
)
1171 const Output_data
* od
= this->plt_
->rel_plt();
1172 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1173 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1174 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1177 if (this->rela_dyn_
!= NULL
)
1179 const Output_data
* od
= this->rela_dyn_
;
1180 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1181 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1182 odyn
->add_constant(elfcpp::DT_RELAENT
,
1183 elfcpp::Elf_sizes
<64>::rela_size
);
1186 if (!parameters
->output_is_shared())
1188 // The value of the DT_DEBUG tag is filled in by the dynamic
1189 // linker at run time, and used by the debugger.
1190 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1194 // Emit any relocs we saved in an attempt to avoid generating COPY
1196 if (this->copy_relocs_
== NULL
)
1198 if (this->copy_relocs_
->any_to_emit())
1200 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1201 this->copy_relocs_
->emit(rela_dyn
);
1203 delete this->copy_relocs_
;
1204 this->copy_relocs_
= NULL
;
1207 // Perform a relocation.
1210 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1211 Target_x86_64
* target
,
1213 const elfcpp::Rela
<64, false>& rela
,
1214 unsigned int r_type
,
1215 const Sized_symbol
<64>* gsym
,
1216 const Symbol_value
<64>* psymval
,
1217 unsigned char* view
,
1218 elfcpp::Elf_types
<64>::Elf_Addr address
,
1221 if (this->skip_call_tls_get_addr_
)
1223 if (r_type
!= elfcpp::R_X86_64_PLT32
1225 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1227 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1228 _("missing expected TLS relocation"));
1232 this->skip_call_tls_get_addr_
= false;
1237 // Pick the value to use for symbols defined in shared objects.
1238 Symbol_value
<64> symval
;
1240 && (gsym
->is_from_dynobj()
1241 || (parameters
->output_is_shared()
1242 && gsym
->is_preemptible()))
1243 && gsym
->has_plt_offset())
1245 symval
.set_output_value(target
->plt_section()->address()
1246 + gsym
->plt_offset());
1250 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1251 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1253 // Get the GOT offset if needed.
1254 // The GOT pointer points to the end of the GOT section.
1255 // We need to subtract the size of the GOT section to get
1256 // the actual offset to use in the relocation.
1257 bool have_got_offset
= false;
1258 unsigned int got_offset
= 0;
1261 case elfcpp::R_X86_64_GOT32
:
1262 case elfcpp::R_X86_64_GOT64
:
1263 case elfcpp::R_X86_64_GOTPLT64
:
1264 case elfcpp::R_X86_64_GOTPCREL
:
1265 case elfcpp::R_X86_64_GOTPCREL64
:
1268 gold_assert(gsym
->has_got_offset());
1269 got_offset
= gsym
->got_offset() - target
->got_size();
1273 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1274 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1276 have_got_offset
= true;
1285 case elfcpp::R_X86_64_NONE
:
1286 case elfcpp::R_386_GNU_VTINHERIT
:
1287 case elfcpp::R_386_GNU_VTENTRY
:
1290 case elfcpp::R_X86_64_64
:
1291 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1294 case elfcpp::R_X86_64_PC64
:
1295 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1299 case elfcpp::R_X86_64_32
:
1300 // FIXME: we need to verify that value + addend fits into 32 bits:
1301 // uint64_t x = value + addend;
1302 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1303 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1304 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1307 case elfcpp::R_X86_64_32S
:
1308 // FIXME: we need to verify that value + addend fits into 32 bits:
1309 // int64_t x = value + addend; // note this quantity is signed!
1310 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1311 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1314 case elfcpp::R_X86_64_PC32
:
1315 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1319 case elfcpp::R_X86_64_16
:
1320 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1323 case elfcpp::R_X86_64_PC16
:
1324 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1328 case elfcpp::R_X86_64_8
:
1329 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1332 case elfcpp::R_X86_64_PC8
:
1333 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1337 case elfcpp::R_X86_64_PLT32
:
1338 gold_assert(gsym
== NULL
1339 || gsym
->has_plt_offset()
1340 || gsym
->final_value_is_known());
1341 // Note: while this code looks the same as for R_X86_64_PC32, it
1342 // behaves differently because psymval was set to point to
1343 // the PLT entry, rather than the symbol, in Scan::global().
1344 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1348 case elfcpp::R_X86_64_PLTOFF64
:
1351 gold_assert(gsym
->has_plt_offset()
1352 || gsym
->final_value_is_known());
1353 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
1354 got_address
= target
->got_section(NULL
, NULL
)->address();
1355 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
1356 addend
- got_address
);
1359 case elfcpp::R_X86_64_GOT32
:
1360 gold_assert(have_got_offset
);
1361 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1364 case elfcpp::R_X86_64_GOTPC32
:
1367 elfcpp::Elf_types
<64>::Elf_Addr value
;
1368 value
= target
->got_plt_section()->address();
1369 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1373 case elfcpp::R_X86_64_GOT64
:
1374 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1375 // Since we always add a PLT entry, this is equivalent.
1376 case elfcpp::R_X86_64_GOTPLT64
:
1377 gold_assert(have_got_offset
);
1378 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1381 case elfcpp::R_X86_64_GOTPC64
:
1384 elfcpp::Elf_types
<64>::Elf_Addr value
;
1385 value
= target
->got_plt_section()->address();
1386 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1390 case elfcpp::R_X86_64_GOTOFF64
:
1392 elfcpp::Elf_types
<64>::Elf_Addr value
;
1393 value
= (psymval
->value(object
, 0)
1394 - target
->got_plt_section()->address());
1395 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1399 case elfcpp::R_X86_64_GOTPCREL
:
1401 gold_assert(have_got_offset
);
1402 elfcpp::Elf_types
<64>::Elf_Addr value
;
1403 value
= target
->got_plt_section()->address() + got_offset
;
1404 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1408 case elfcpp::R_X86_64_GOTPCREL64
:
1410 gold_assert(have_got_offset
);
1411 elfcpp::Elf_types
<64>::Elf_Addr value
;
1412 value
= target
->got_plt_section()->address() + got_offset
;
1413 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1417 case elfcpp::R_X86_64_COPY
:
1418 case elfcpp::R_X86_64_GLOB_DAT
:
1419 case elfcpp::R_X86_64_JUMP_SLOT
:
1420 case elfcpp::R_X86_64_RELATIVE
:
1421 // These are outstanding tls relocs, which are unexpected when linking
1422 case elfcpp::R_X86_64_TPOFF64
:
1423 case elfcpp::R_X86_64_DTPMOD64
:
1424 case elfcpp::R_X86_64_TLSDESC
:
1425 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1426 _("unexpected reloc %u in object file"),
1430 // These are initial tls relocs, which are expected when linking
1431 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1432 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1433 case elfcpp::R_X86_64_TLSDESC_CALL
:
1434 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1435 case elfcpp::R_X86_64_DTPOFF32
:
1436 case elfcpp::R_X86_64_DTPOFF64
:
1437 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1438 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1439 this->relocate_tls(relinfo
, relnum
, rela
, r_type
, gsym
, psymval
, view
,
1440 address
, view_size
);
1443 case elfcpp::R_X86_64_SIZE32
:
1444 case elfcpp::R_X86_64_SIZE64
:
1446 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1447 _("unsupported reloc %u"),
1455 // Perform a TLS relocation.
1458 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1460 const elfcpp::Rela
<64, false>& rela
,
1461 unsigned int r_type
,
1462 const Sized_symbol
<64>* gsym
,
1463 const Symbol_value
<64>* psymval
,
1464 unsigned char* view
,
1465 elfcpp::Elf_types
<64>::Elf_Addr
,
1468 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1469 if (tls_segment
== NULL
)
1471 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1472 _("TLS reloc but no TLS segment"));
1476 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1478 const bool is_final
= (gsym
== NULL
1479 ? !parameters
->output_is_position_independent()
1480 : gsym
->final_value_is_known());
1481 const tls::Tls_optimization optimized_type
1482 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1485 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1486 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1487 case elfcpp::R_X86_64_TLSDESC_CALL
:
1488 if (optimized_type
== tls::TLSOPT_TO_LE
)
1490 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1491 rela
, r_type
, value
, view
,
1495 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1496 _("unsupported reloc %u"), r_type
);
1499 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1500 if (optimized_type
== tls::TLSOPT_TO_LE
)
1502 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1503 value
, view
, view_size
);
1506 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1507 _("unsupported reloc %u"), r_type
);
1510 case elfcpp::R_X86_64_DTPOFF32
:
1511 if (optimized_type
== tls::TLSOPT_TO_LE
)
1512 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1514 value
= value
- tls_segment
->vaddr();
1515 Relocate_functions
<64, false>::rel32(view
, value
);
1518 case elfcpp::R_X86_64_DTPOFF64
:
1519 if (optimized_type
== tls::TLSOPT_TO_LE
)
1520 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1522 value
= value
- tls_segment
->vaddr();
1523 Relocate_functions
<64, false>::rel64(view
, value
);
1526 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1527 if (optimized_type
== tls::TLSOPT_TO_LE
)
1529 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1530 rela
, r_type
, value
, view
,
1534 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1535 _("unsupported reloc type %u"),
1539 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1540 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1541 Relocate_functions
<64, false>::rel32(view
, value
);
1546 // Do a relocation in which we convert a TLS General-Dynamic to a
1550 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
1552 Output_segment
* tls_segment
,
1553 const elfcpp::Rela
<64, false>& rela
,
1555 elfcpp::Elf_types
<64>::Elf_Addr value
,
1556 unsigned char* view
,
1559 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1560 // .word 0x6666; rex64; call __tls_get_addr
1561 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1563 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
1564 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
1566 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1567 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
1568 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1569 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
1571 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1573 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1574 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
1576 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1578 this->skip_call_tls_get_addr_
= true;
1582 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
1585 const elfcpp::Rela
<64, false>& rela
,
1587 elfcpp::Elf_types
<64>::Elf_Addr
,
1588 unsigned char* view
,
1591 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1592 // ... leq foo@dtpoff(%rax),%reg
1593 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1595 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1596 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
1598 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1599 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
1601 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
1603 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1605 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1607 this->skip_call_tls_get_addr_
= true;
1610 // Do a relocation in which we convert a TLS Initial-Exec to a
1614 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
1616 Output_segment
* tls_segment
,
1617 const elfcpp::Rela
<64, false>& rela
,
1619 elfcpp::Elf_types
<64>::Elf_Addr value
,
1620 unsigned char* view
,
1623 // We need to examine the opcodes to figure out which instruction we
1626 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1627 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1629 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1630 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
1632 unsigned char op1
= view
[-3];
1633 unsigned char op2
= view
[-2];
1634 unsigned char op3
= view
[-1];
1635 unsigned char reg
= op3
>> 3;
1643 view
[-1] = 0xc0 | reg
;
1647 // Special handling for %rsp.
1651 view
[-1] = 0xc0 | reg
;
1659 view
[-1] = 0x80 | reg
| (reg
<< 3);
1662 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1663 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1666 // Relocate section data.
1669 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
1670 unsigned int sh_type
,
1671 const unsigned char* prelocs
,
1673 unsigned char* view
,
1674 elfcpp::Elf_types
<64>::Elf_Addr address
,
1677 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1679 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1680 Target_x86_64::Relocate
>(
1690 // Return the value to use for a dynamic which requires special
1691 // treatment. This is how we support equality comparisons of function
1692 // pointers across shared library boundaries, as described in the
1693 // processor specific ABI supplement.
1696 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
1698 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1699 return this->plt_section()->address() + gsym
->plt_offset();
1702 // Return a string used to fill a code section with nops to take up
1703 // the specified length.
1706 Target_x86_64::do_code_fill(off_t length
)
1710 // Build a jmpq instruction to skip over the bytes.
1711 unsigned char jmp
[5];
1713 elfcpp::Swap_unaligned
<64, false>::writeval(jmp
+ 1, length
- 5);
1714 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1715 + std::string(length
- 5, '\0'));
1718 // Nop sequences of various lengths.
1719 const char nop1
[1] = { 0x90 }; // nop
1720 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1721 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1722 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1723 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1724 0x00 }; // leal 0(%esi,1),%esi
1725 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1727 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1729 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1730 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1731 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1732 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1734 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1735 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1737 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1738 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1740 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1741 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1742 0x00, 0x00, 0x00, 0x00 };
1743 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1744 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1745 0x27, 0x00, 0x00, 0x00,
1747 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1748 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1749 0xbc, 0x27, 0x00, 0x00,
1751 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1752 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1753 0x90, 0x90, 0x90, 0x90,
1756 const char* nops
[16] = {
1758 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
1759 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
1762 return std::string(nops
[length
], length
);
1765 // The selector for x86_64 object files.
1767 class Target_selector_x86_64
: public Target_selector
1770 Target_selector_x86_64()
1771 : Target_selector(elfcpp::EM_X86_64
, 64, false)
1775 recognize(int machine
, int osabi
, int abiversion
);
1778 Target_x86_64
* target_
;
1781 // Recognize an x86_64 object file when we already know that the machine
1782 // number is EM_X86_64.
1785 Target_selector_x86_64::recognize(int, int, int)
1787 if (this->target_
== NULL
)
1788 this->target_
= new Target_x86_64();
1789 return this->target_
;
1792 Target_selector_x86_64 target_selector_x86_64
;
1794 } // End anonymous namespace.