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"
53 class Output_data_plt_x86_64
;
55 // The x86_64 target class.
57 // http://www.x86-64.org/documentation/abi.pdf
58 // TLS info comes from
59 // http://people.redhat.com/drepper/tls.pdf
60 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
62 class Target_x86_64
: public Sized_target
<64, false>
65 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
66 // uses only Elf64_Rela relocation entries with explicit addends."
67 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
70 : Sized_target
<64, false>(&x86_64_info
),
71 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rela_dyn_(NULL
),
72 copy_relocs_(NULL
), dynbss_(NULL
)
75 // Scan the relocations to look for symbol adjustments.
77 scan_relocs(const General_options
& options
,
80 Sized_relobj
<64, false>* object
,
81 unsigned int data_shndx
,
83 const unsigned char* prelocs
,
85 size_t local_symbol_count
,
86 const unsigned char* plocal_symbols
,
87 Symbol
** global_symbols
);
89 // Finalize the sections.
91 do_finalize_sections(Layout
*);
93 // Return the value to use for a dynamic which requires special
96 do_dynsym_value(const Symbol
*) const;
98 // Relocate a section.
100 relocate_section(const Relocate_info
<64, false>*,
101 unsigned int sh_type
,
102 const unsigned char* prelocs
,
105 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
108 // Return a string used to fill a code section with nops.
110 do_code_fill(off_t length
);
113 // The class which scans relocations.
117 local(const General_options
& options
, Symbol_table
* symtab
,
118 Layout
* layout
, Target_x86_64
* target
,
119 Sized_relobj
<64, false>* object
,
120 unsigned int data_shndx
,
121 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
122 const elfcpp::Sym
<64, false>& lsym
);
125 global(const General_options
& options
, Symbol_table
* symtab
,
126 Layout
* layout
, Target_x86_64
* target
,
127 Sized_relobj
<64, false>* object
,
128 unsigned int data_shndx
,
129 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
133 // The class which implements relocation.
138 : skip_call_tls_get_addr_(false)
143 if (this->skip_call_tls_get_addr_
)
145 // FIXME: This needs to specify the location somehow.
146 fprintf(stderr
, _("%s: missing expected TLS relocation\n"),
152 // Do a relocation. Return false if the caller should not issue
153 // any warnings about this relocation.
155 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, size_t relnum
,
156 const elfcpp::Rela
<64, false>&,
157 unsigned int r_type
, const Sized_symbol
<64>*,
158 const Symbol_value
<64>*,
159 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
163 // Do a TLS relocation.
165 relocate_tls(const Relocate_info
<64, false>*, size_t relnum
,
166 const elfcpp::Rela
<64, false>&,
167 unsigned int r_type
, const Sized_symbol
<64>*,
168 const Symbol_value
<64>*,
169 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
, off_t
);
171 // Do a TLS Initial-Exec to Local-Exec transition.
173 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
174 Output_segment
* tls_segment
,
175 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
176 elfcpp::Elf_types
<64>::Elf_Addr value
,
180 // Do a TLS Global-Dynamic to Local-Exec transition.
182 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
183 Output_segment
* tls_segment
,
184 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
185 elfcpp::Elf_types
<64>::Elf_Addr value
,
189 // Check the range for a TLS relocation.
191 check_range(const Relocate_info
<64, false>*, size_t relnum
,
192 const elfcpp::Rela
<64, false>&, off_t
, off_t
);
194 // Check the validity of a TLS relocation. This is like assert.
196 check_tls(const Relocate_info
<64, false>*, size_t relnum
,
197 const elfcpp::Rela
<64, false>&, bool);
199 // This is set if we should skip the next reloc, which should be a
200 // PLT32 reloc against ___tls_get_addr.
201 bool skip_call_tls_get_addr_
;
204 // Adjust TLS relocation type based on the options and whether this
205 // is a local symbol.
207 optimize_tls_reloc(bool is_final
, int r_type
);
209 // Get the GOT section, creating it if necessary.
210 Output_data_got
<64, false>*
211 got_section(Symbol_table
*, Layout
*);
213 // Create a PLT entry for a global symbol.
215 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
217 // Get the PLT section.
218 Output_data_plt_x86_64
*
221 gold_assert(this->plt_
!= NULL
);
225 // Get the dynamic reloc section, creating it if necessary.
227 rela_dyn_section(Layout
*);
229 // Copy a relocation against a global symbol.
231 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
232 Sized_relobj
<64, false>*, unsigned int,
233 Symbol
*, const elfcpp::Rela
<64, false>&);
235 // Information about this specific target which we pass to the
236 // general Target structure.
237 static const Target::Target_info x86_64_info
;
240 Output_data_got
<64, false>* got_
;
242 Output_data_plt_x86_64
* plt_
;
243 // The GOT PLT section.
244 Output_data_space
* got_plt_
;
245 // The dynamic reloc section.
246 Reloc_section
* rela_dyn_
;
247 // Relocs saved to avoid a COPY reloc.
248 Copy_relocs
<64, false>* copy_relocs_
;
249 // Space for variables copied with a COPY reloc.
250 Output_data_space
* dynbss_
;
253 const Target::Target_info
Target_x86_64::x86_64_info
=
256 false, // is_big_endian
257 elfcpp::EM_X86_64
, // machine_code
258 false, // has_make_symbol
259 false, // has_resolve
260 true, // has_code_fill
261 "/lib/ld64.so.1", // program interpreter
262 0x400000, // text_segment_address
263 0x1000, // abi_pagesize
264 0x1000 // common_pagesize
267 // Get the GOT section, creating it if necessary.
269 Output_data_got
<64, false>*
270 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
272 if (this->got_
== NULL
)
274 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
276 this->got_
= new Output_data_got
<64, false>();
278 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
279 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
282 // The old GNU linker creates a .got.plt section. We just
283 // create another set of data in the .got section. Note that we
284 // always create a PLT if we create a GOT, although the PLT
286 // TODO(csilvers): do we really need an alignment of 8?
287 this->got_plt_
= new Output_data_space(8);
288 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
289 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
292 // The first three entries are reserved.
293 this->got_plt_
->set_space_size(3 * 8);
295 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
296 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
298 0, 0, elfcpp::STT_OBJECT
,
300 elfcpp::STV_HIDDEN
, 0,
307 // Get the dynamic reloc section, creating it if necessary.
309 Target_x86_64::Reloc_section
*
310 Target_x86_64::rela_dyn_section(Layout
* layout
)
312 if (this->rela_dyn_
== NULL
)
314 gold_assert(layout
!= NULL
);
315 this->rela_dyn_
= new Reloc_section();
316 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
317 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
319 return this->rela_dyn_
;
322 // A class to handle the PLT data.
324 class Output_data_plt_x86_64
: public Output_section_data
327 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
329 Output_data_plt_x86_64(Layout
*, Output_data_space
*);
331 // Add an entry to the PLT.
333 add_entry(Symbol
* gsym
);
335 // Return the .rel.plt section data.
338 { return this->rel_
; }
342 do_adjust_output_section(Output_section
* os
);
345 // The size of an entry in the PLT.
346 static const int plt_entry_size
= 16;
348 // The first entry in the PLT.
349 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
350 // procedure linkage table for both programs and shared objects."
351 static unsigned char first_plt_entry
[plt_entry_size
];
353 // Other entries in the PLT for an executable.
354 static unsigned char plt_entry
[plt_entry_size
];
356 // Set the final size.
358 do_set_address(uint64_t, off_t
)
359 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
361 // Write out the PLT data.
363 do_write(Output_file
*);
365 // The reloc section.
367 // The .got.plt section.
368 Output_data_space
* got_plt_
;
369 // The number of PLT entries.
373 // Create the PLT section. The ordinary .got section is an argument,
374 // since we need to refer to the start. We also create our own .got
375 // section just for PLT entries.
377 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
378 Output_data_space
* got_plt
)
379 // TODO(csilvers): do we really need an alignment of 8?
380 : Output_section_data(8), got_plt_(got_plt
), count_(0)
382 this->rel_
= new Reloc_section();
383 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
384 elfcpp::SHF_ALLOC
, this->rel_
);
388 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
390 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
391 // linker, and so do we.
395 // Add an entry to the PLT.
398 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
400 gold_assert(!gsym
->has_plt_offset());
402 // Note that when setting the PLT offset we skip the initial
403 // reserved PLT entry.
404 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
408 off_t got_offset
= this->got_plt_
->data_size();
410 // Every PLT entry needs a GOT entry which points back to the PLT
411 // entry (this will be changed by the dynamic linker, normally
412 // lazily when the function is called).
413 this->got_plt_
->set_space_size(got_offset
+ 8);
415 // Every PLT entry needs a reloc.
416 gsym
->set_needs_dynsym_entry();
417 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
420 // Note that we don't need to save the symbol. The contents of the
421 // PLT are independent of which symbols are used. The symbols only
422 // appear in the relocations.
425 // The first entry in the PLT for an executable.
427 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
429 // From AMD64 ABI Draft 0.98, page 76
430 0xff, 0x35, // pushq contents of memory address
431 0, 0, 0, 0, // replaced with address of .got + 4
432 0xff, 0x25, // jmp indirect
433 0, 0, 0, 0, // replaced with address of .got + 8
434 0x90, 0x90, 0x90, 0x90 // noop (x4)
437 // Subsequent entries in the PLT for an executable.
439 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
441 // From AMD64 ABI Draft 0.98, page 76
442 0xff, 0x25, // jmpq indirect
443 0, 0, 0, 0, // replaced with address of symbol in .got
444 0x68, // pushq immediate
445 0, 0, 0, 0, // replaced with offset into relocation table
446 0xe9, // jmpq relative
447 0, 0, 0, 0 // replaced with offset to start of .plt
450 // Write out the PLT. This uses the hand-coded instructions above,
451 // and adjusts them as needed. This is specified by the AMD64 ABI.
454 Output_data_plt_x86_64::do_write(Output_file
* of
)
456 const off_t offset
= this->offset();
457 const off_t oview_size
= this->data_size();
458 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
460 const off_t got_file_offset
= this->got_plt_
->offset();
461 const off_t got_size
= this->got_plt_
->data_size();
462 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
465 unsigned char* pov
= oview
;
467 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
468 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
470 memcpy(pov
, first_plt_entry
, plt_entry_size
);
471 if (!parameters
->output_is_shared())
473 // We do a jmp relative to the PC at the end of this instruction.
474 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 8
475 - (plt_address
+ 6));
476 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 16
477 - (plt_address
+ 12));
479 pov
+= plt_entry_size
;
481 unsigned char* got_pov
= got_view
;
483 memset(got_pov
, 0, 24);
486 unsigned int plt_offset
= plt_entry_size
;
487 unsigned int got_offset
= 24;
488 const unsigned int count
= this->count_
;
489 for (unsigned int plt_index
= 0;
492 pov
+= plt_entry_size
,
494 plt_offset
+= plt_entry_size
,
497 // Set and adjust the PLT entry itself.
498 memcpy(pov
, plt_entry
, plt_entry_size
);
499 if (parameters
->output_is_shared())
500 // FIXME(csilvers): what's the right thing to write here?
501 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
503 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
504 (got_address
+ got_offset
505 - (plt_address
+ plt_offset
508 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
509 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
510 - (plt_offset
+ plt_entry_size
));
512 // Set the entry in the GOT.
513 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
516 gold_assert(pov
- oview
== oview_size
);
517 gold_assert(got_pov
- got_view
== got_size
);
519 of
->write_output_view(offset
, oview_size
, oview
);
520 of
->write_output_view(got_file_offset
, got_size
, got_view
);
523 // Create a PLT entry for a global symbol.
526 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
529 if (gsym
->has_plt_offset())
532 if (this->plt_
== NULL
)
534 // Create the GOT sections first.
535 this->got_section(symtab
, layout
);
537 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_plt_
);
538 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
540 | elfcpp::SHF_EXECINSTR
),
544 this->plt_
->add_entry(gsym
);
547 // Handle a relocation against a non-function symbol defined in a
548 // dynamic object. The traditional way to handle this is to generate
549 // a COPY relocation to copy the variable at runtime from the shared
550 // object into the executable's data segment. However, this is
551 // undesirable in general, as if the size of the object changes in the
552 // dynamic object, the executable will no longer work correctly. If
553 // this relocation is in a writable section, then we can create a
554 // dynamic reloc and the dynamic linker will resolve it to the correct
555 // address at runtime. However, we do not want do that if the
556 // relocation is in a read-only section, as it would prevent the
557 // readonly segment from being shared. And if we have to eventually
558 // generate a COPY reloc, then any dynamic relocations will be
559 // useless. So this means that if this is a writable section, we need
560 // to save the relocation until we see whether we have to create a
561 // COPY relocation for this symbol for any other relocation.
564 Target_x86_64::copy_reloc(const General_options
* options
,
565 Symbol_table
* symtab
,
567 Sized_relobj
<64, false>* object
,
568 unsigned int data_shndx
, Symbol
* gsym
,
569 const elfcpp::Rela
<64, false>& rel
)
571 Sized_symbol
<64>* ssym
;
572 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(64) (gsym
575 if (!Copy_relocs
<64, false>::need_copy_reloc(options
, object
,
578 // So far we do not need a COPY reloc. Save this relocation.
579 // If it turns out that we never need a COPY reloc for this
580 // symbol, then we will emit the relocation.
581 if (this->copy_relocs_
== NULL
)
582 this->copy_relocs_
= new Copy_relocs
<64, false>();
583 this->copy_relocs_
->save(ssym
, object
, data_shndx
, rel
);
587 // Allocate space for this symbol in the .bss section.
589 elfcpp::Elf_types
<64>::Elf_WXword symsize
= ssym
->symsize();
591 // There is no defined way to determine the required alignment
592 // of the symbol. We pick the alignment based on the size. We
593 // set an arbitrary maximum of 256.
595 for (align
= 1; align
< 512; align
<<= 1)
596 if ((symsize
& align
) != 0)
599 if (this->dynbss_
== NULL
)
601 this->dynbss_
= new Output_data_space(align
);
602 layout
->add_output_section_data(".bss",
605 | elfcpp::SHF_WRITE
),
609 Output_data_space
* dynbss
= this->dynbss_
;
611 if (align
> dynbss
->addralign())
612 dynbss
->set_space_alignment(align
);
614 off_t dynbss_size
= dynbss
->data_size();
615 dynbss_size
= align_address(dynbss_size
, align
);
616 off_t offset
= dynbss_size
;
617 dynbss
->set_space_size(dynbss_size
+ symsize
);
619 // Define the symbol in the .dynbss section.
620 symtab
->define_in_output_data(this, ssym
->name(), ssym
->version(),
621 dynbss
, offset
, symsize
, ssym
->type(),
622 ssym
->binding(), ssym
->visibility(),
623 ssym
->nonvis(), false, false);
625 // Add the COPY reloc.
626 ssym
->set_needs_dynsym_entry();
627 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
628 rela_dyn
->add_global(ssym
, elfcpp::R_X86_64_COPY
, dynbss
, offset
, 0);
633 // Optimize the TLS relocation type based on what we know about the
634 // symbol. IS_FINAL is true if the final address of this symbol is
635 // known at link time.
638 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
640 // If we are generating a shared library, then we can't do anything
642 if (parameters
->output_is_shared())
647 case elfcpp::R_X86_64_TLSGD
:
648 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // TODO(csilvers): correct?
649 case elfcpp::R_X86_64_TLSDESC_CALL
: // TODO(csilvers): correct?
650 // These are Global-Dynamic which permits fully general TLS
651 // access. Since we know that we are generating an executable,
652 // we can convert this to Initial-Exec. If we also know that
653 // this is a local symbol, we can further switch to Local-Exec.
655 return elfcpp::R_X86_64_TPOFF32
;
656 return elfcpp::R_X86_64_GOTTPOFF
; // used for Initial-exec
658 case elfcpp::R_X86_64_TLSLD
:
659 // This is Local-Dynamic, which refers to a local symbol in the
660 // dynamic TLS block. Since we know that we generating an
661 // executable, we can switch to Local-Exec.
662 return elfcpp::R_X86_64_TPOFF32
;
664 case elfcpp::R_X86_64_DTPOFF32
:
665 // Another Local-Dynamic reloc.
666 return elfcpp::R_X86_64_TPOFF32
;
668 case elfcpp::R_X86_64_DTPOFF64
:
669 // Another Local-Dynamic reloc.
670 return elfcpp::R_X86_64_TPOFF64
;
672 case elfcpp::R_X86_64_GOTTPOFF
:
673 // These are Initial-Exec relocs which get the thread offset
674 // from the GOT. If we know that we are linking against the
675 // local symbol, we can switch to Local-Exec, which links the
676 // thread offset into the instruction.
678 return elfcpp::R_X86_64_TPOFF32
;
681 case elfcpp::R_X86_64_TPOFF32
:
682 // When we already have Local-Exec, there is nothing further we
691 // Scan a relocation for a local symbol.
694 Target_x86_64::Scan::local(const General_options
&,
695 Symbol_table
* symtab
,
697 Target_x86_64
* target
,
698 Sized_relobj
<64, false>* object
,
699 unsigned int data_shndx
,
700 const elfcpp::Rela
<64, false>& reloc
,
702 const elfcpp::Sym
<64, false>&)
706 case elfcpp::R_X86_64_NONE
:
707 case elfcpp::R_386_GNU_VTINHERIT
:
708 case elfcpp::R_386_GNU_VTENTRY
:
711 case elfcpp::R_X86_64_64
:
712 case elfcpp::R_X86_64_32
:
713 case elfcpp::R_X86_64_32S
:
714 case elfcpp::R_X86_64_16
:
715 case elfcpp::R_X86_64_8
:
716 // FIXME: If we are generating a shared object we need to copy
717 // this relocation into the object.
718 gold_assert(!parameters
->output_is_shared());
721 case elfcpp::R_X86_64_PC64
:
722 case elfcpp::R_X86_64_PC32
:
723 case elfcpp::R_X86_64_PC16
:
724 case elfcpp::R_X86_64_PC8
:
727 case elfcpp::R_X86_64_GOTPC32
: // TODO(csilvers): correct?
728 case elfcpp::R_X86_64_GOTOFF64
:
729 case elfcpp::R_X86_64_GOTPC64
: // TODO(csilvers): correct?
730 case elfcpp::R_X86_64_PLTOFF64
: // TODO(csilvers): correct?
731 // We need a GOT section.
732 target
->got_section(symtab
, layout
);
735 case elfcpp::R_X86_64_GOT64
:
736 case elfcpp::R_X86_64_GOT32
:
737 case elfcpp::R_X86_64_GOTPCREL64
:
738 case elfcpp::R_X86_64_GOTPCREL
:
740 // The symbol requires a GOT entry.
741 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
742 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
743 if (got
->add_local(object
, r_sym
))
745 // If we are generating a shared object, we need to add a
746 // dynamic RELATIVE relocation for this symbol.
747 if (parameters
->output_is_shared())
749 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
750 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
751 data_shndx
, reloc
.get_r_offset(), 0);
757 case elfcpp::R_X86_64_COPY
:
758 case elfcpp::R_X86_64_GLOB_DAT
:
759 case elfcpp::R_X86_64_JUMP_SLOT
:
760 case elfcpp::R_X86_64_RELATIVE
:
761 // These are outstanding tls relocs, which are unexpected when linking
762 case elfcpp::R_X86_64_TPOFF64
:
763 case elfcpp::R_X86_64_DTPMOD64
:
764 case elfcpp::R_X86_64_TLSDESC
:
765 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
766 program_name
, object
->name().c_str(), r_type
);
770 // These are initial tls relocs, which are expected when linking
771 case elfcpp::R_X86_64_TLSGD
: // TODO(csilvers): correct?
772 case elfcpp::R_X86_64_TLSLD
: // TODO(csilvers): correct?
773 case elfcpp::R_X86_64_GOTTPOFF
: // TODO(csilvers): correct?
774 case elfcpp::R_X86_64_TPOFF32
: // TODO(csilvers): correct?
775 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // TODO(csilvers): correct?
776 case elfcpp::R_X86_64_TLSDESC_CALL
: // TODO(csilvers): correct?
777 case elfcpp::R_X86_64_DTPOFF32
:
778 case elfcpp::R_X86_64_DTPOFF64
:
780 bool output_is_shared
= parameters
->output_is_shared();
781 r_type
= Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
784 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
785 case elfcpp::R_X86_64_TPOFF64
:
786 // FIXME: If generating a shared object, we need to copy
787 // this relocation into the object.
788 gold_assert(!output_is_shared
);
791 case elfcpp::R_X86_64_DTPOFF32
:
792 case elfcpp::R_X86_64_DTPOFF64
:
795 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
796 case elfcpp::R_X86_64_TLSGD
: // General Dynamic
797 case elfcpp::R_X86_64_TLSLD
: // Local Dynamic
798 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
799 case elfcpp::R_X86_64_TLSDESC_CALL
:
801 _("%s: %s: unsupported reloc %u against local symbol\n"),
802 program_name
, object
->name().c_str(), r_type
);
808 case elfcpp::R_X86_64_GOTPLT64
:
809 case elfcpp::R_X86_64_PLT32
:
810 case elfcpp::R_X86_64_SIZE32
: // TODO(csilvers): correct?
811 case elfcpp::R_X86_64_SIZE64
: // TODO(csilvers): correct?
813 fprintf(stderr
, _("%s: %s: unsupported reloc %u against local symbol\n"),
814 program_name
, object
->name().c_str(), r_type
);
820 // Scan a relocation for a global symbol.
823 Target_x86_64::Scan::global(const General_options
& options
,
824 Symbol_table
* symtab
,
826 Target_x86_64
* target
,
827 Sized_relobj
<64, false>* object
,
828 unsigned int data_shndx
,
829 const elfcpp::Rela
<64, false>& reloc
,
835 case elfcpp::R_X86_64_NONE
:
836 case elfcpp::R_386_GNU_VTINHERIT
:
837 case elfcpp::R_386_GNU_VTENTRY
:
840 case elfcpp::R_X86_64_64
:
841 case elfcpp::R_X86_64_PC64
:
842 case elfcpp::R_X86_64_32
:
843 case elfcpp::R_X86_64_32S
:
844 case elfcpp::R_X86_64_PC32
:
845 case elfcpp::R_X86_64_16
:
846 case elfcpp::R_X86_64_PC16
:
847 case elfcpp::R_X86_64_8
:
848 case elfcpp::R_X86_64_PC8
:
849 // FIXME: If we are generating a shared object we may need to
850 // copy this relocation into the object. If this symbol is
851 // defined in a shared object, we may need to copy this
852 // relocation in order to avoid a COPY relocation.
853 gold_assert(!parameters
->output_is_shared());
855 if (gsym
->is_from_dynobj())
857 // This symbol is defined in a dynamic object. If it is a
858 // function, we make a PLT entry. Otherwise we need to
859 // either generate a COPY reloc or copy this reloc.
860 if (gsym
->type() == elfcpp::STT_FUNC
)
862 target
->make_plt_entry(symtab
, layout
, gsym
);
864 // If this is not a PC relative reference, then we may
865 // be taking the address of the function. In that case
866 // we need to set the entry in the dynamic symbol table
867 // to the address of the PLT entry.
868 if (r_type
!= elfcpp::R_X86_64_PC64
869 && r_type
!= elfcpp::R_X86_64_PC32
870 && r_type
!= elfcpp::R_X86_64_PC16
871 && r_type
!= elfcpp::R_X86_64_PC8
)
872 gsym
->set_needs_dynsym_value();
875 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
881 case elfcpp::R_X86_64_GOT64
:
882 case elfcpp::R_X86_64_GOT32
:
883 case elfcpp::R_X86_64_GOTPCREL64
:
884 case elfcpp::R_X86_64_GOTPCREL
:
885 case elfcpp::R_X86_64_GOTPLT64
:
887 // The symbol requires a GOT entry.
888 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
889 if (got
->add_global(gsym
))
891 // If this symbol is not fully resolved, we need to add a
892 // dynamic relocation for it.
893 if (!gsym
->final_value_is_known())
895 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
896 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
, got
,
897 gsym
->got_offset(), 0);
903 case elfcpp::R_X86_64_PLT32
:
904 // If the symbol is fully resolved, this is just a PC32 reloc.
905 // Otherwise we need a PLT entry.
906 if (gsym
->final_value_is_known())
908 target
->make_plt_entry(symtab
, layout
, gsym
);
911 case elfcpp::R_X86_64_GOTPC32
: // TODO(csilvers): correct?
912 case elfcpp::R_X86_64_GOTOFF64
:
913 case elfcpp::R_X86_64_GOTPC64
: // TODO(csilvers): correct?
914 case elfcpp::R_X86_64_PLTOFF64
: // TODO(csilvers): correct?
915 // We need a GOT section.
916 target
->got_section(symtab
, layout
);
919 case elfcpp::R_X86_64_COPY
:
920 case elfcpp::R_X86_64_GLOB_DAT
:
921 case elfcpp::R_X86_64_JUMP_SLOT
:
922 case elfcpp::R_X86_64_RELATIVE
:
923 // These are outstanding tls relocs, which are unexpected when linking
924 case elfcpp::R_X86_64_TPOFF64
:
925 case elfcpp::R_X86_64_DTPMOD64
:
926 case elfcpp::R_X86_64_TLSDESC
:
927 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
928 program_name
, object
->name().c_str(), r_type
);
932 // These are initial tls relocs, which are expected for global()
933 case elfcpp::R_X86_64_TLSGD
: // TODO(csilvers): correct?
934 case elfcpp::R_X86_64_TLSLD
: // TODO(csilvers): correct?
935 case elfcpp::R_X86_64_GOTTPOFF
: // TODO(csilvers): correct?
936 case elfcpp::R_X86_64_TPOFF32
: // TODO(csilvers): correct?
937 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // TODO(csilvers): correct?
938 case elfcpp::R_X86_64_TLSDESC_CALL
: // TODO(csilvers): correct?
939 case elfcpp::R_X86_64_DTPOFF32
:
940 case elfcpp::R_X86_64_DTPOFF64
:
942 const bool is_final
= gsym
->final_value_is_known();
943 r_type
= Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
946 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
947 case elfcpp::R_X86_64_TPOFF64
:
948 // FIXME: If generating a shared object, we need to copy
949 // this relocation into the object.
950 gold_assert(!parameters
->output_is_shared());
953 case elfcpp::R_X86_64_DTPOFF32
:
954 case elfcpp::R_X86_64_DTPOFF64
:
957 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
958 case elfcpp::R_X86_64_TLSGD
: // General Dynamic
959 case elfcpp::R_X86_64_TLSLD
: // Local Dynamic
960 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
961 case elfcpp::R_X86_64_TLSDESC_CALL
:
963 _("%s: %s: unsupported reloc %u "
964 "against global symbol %s\n"),
965 program_name
, object
->name().c_str(), r_type
,
972 case elfcpp::R_X86_64_SIZE32
: // TODO(csilvers): correct?
973 case elfcpp::R_X86_64_SIZE64
: // TODO(csilvers): correct?
976 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
977 program_name
, object
->name().c_str(), r_type
, gsym
->name());
982 // Scan relocations for a section.
985 Target_x86_64::scan_relocs(const General_options
& options
,
986 Symbol_table
* symtab
,
988 Sized_relobj
<64, false>* object
,
989 unsigned int data_shndx
,
990 unsigned int sh_type
,
991 const unsigned char* prelocs
,
993 size_t local_symbol_count
,
994 const unsigned char* plocal_symbols
,
995 Symbol
** global_symbols
)
997 if (sh_type
== elfcpp::SHT_REL
)
999 fprintf(stderr
, _("%s: %s: unsupported REL reloc section\n"),
1000 program_name
, object
->name().c_str());
1004 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1005 Target_x86_64::Scan
>(
1019 // Finalize the sections.
1022 Target_x86_64::do_finalize_sections(Layout
* layout
)
1024 // Fill in some more dynamic tags.
1025 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1028 if (this->got_plt_
!= NULL
)
1029 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1031 if (this->plt_
!= NULL
)
1033 const Output_data
* od
= this->plt_
->rel_plt();
1034 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1035 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1036 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1039 if (this->rela_dyn_
!= NULL
)
1041 const Output_data
* od
= this->rela_dyn_
;
1042 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1043 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1044 odyn
->add_constant(elfcpp::DT_RELAENT
,
1045 elfcpp::Elf_sizes
<64>::rela_size
);
1048 if (!parameters
->output_is_shared())
1050 // The value of the DT_DEBUG tag is filled in by the dynamic
1051 // linker at run time, and used by the debugger.
1052 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1056 // Emit any relocs we saved in an attempt to avoid generating COPY
1058 if (this->copy_relocs_
== NULL
)
1060 if (this->copy_relocs_
->any_to_emit())
1062 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1063 this->copy_relocs_
->emit(rela_dyn
);
1065 delete this->copy_relocs_
;
1066 this->copy_relocs_
= NULL
;
1069 // Perform a relocation.
1072 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1073 Target_x86_64
* target
,
1075 const elfcpp::Rela
<64, false>& rela
,
1076 unsigned int r_type
,
1077 const Sized_symbol
<64>* gsym
,
1078 const Symbol_value
<64>* psymval
,
1079 unsigned char* view
,
1080 elfcpp::Elf_types
<64>::Elf_Addr address
,
1083 if (this->skip_call_tls_get_addr_
)
1085 if (r_type
!= elfcpp::R_X86_64_PLT32
1087 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1089 fprintf(stderr
, _("%s: %s: missing expected TLS relocation\n"),
1091 relinfo
->location(relnum
, rela
.get_r_offset()).c_str());
1095 this->skip_call_tls_get_addr_
= false;
1100 // Pick the value to use for symbols defined in shared objects.
1101 Symbol_value
<64> symval
;
1102 if (gsym
!= NULL
&& gsym
->is_from_dynobj() && gsym
->has_plt_offset())
1104 symval
.set_output_value(target
->plt_section()->address()
1105 + gsym
->plt_offset());
1109 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1110 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1112 // Get the GOT offset if needed.
1113 bool have_got_offset
= false;
1114 unsigned int got_offset
= 0;
1117 case elfcpp::R_X86_64_GOT32
:
1118 case elfcpp::R_X86_64_GOT64
:
1119 case elfcpp::R_X86_64_GOTPLT64
:
1120 case elfcpp::R_X86_64_GOTPCREL
:
1121 case elfcpp::R_X86_64_GOTPCREL64
:
1124 gold_assert(gsym
->has_got_offset());
1125 got_offset
= gsym
->got_offset();
1129 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1130 got_offset
= object
->local_got_offset(r_sym
);
1132 have_got_offset
= true;
1142 case elfcpp::R_X86_64_NONE
:
1143 case elfcpp::R_386_GNU_VTINHERIT
:
1144 case elfcpp::R_386_GNU_VTENTRY
:
1147 case elfcpp::R_X86_64_64
:
1148 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1151 case elfcpp::R_X86_64_PC64
:
1152 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1156 case elfcpp::R_X86_64_32
:
1157 // FIXME: we need to verify that value + addend fits into 32 bits:
1158 // uint64_t x = value + addend;
1159 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1160 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1161 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1164 case elfcpp::R_X86_64_32S
:
1165 // FIXME: we need to verify that value + addend fits into 32 bits:
1166 // int64_t x = value + addend; // note this quantity is signed!
1167 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1168 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1171 case elfcpp::R_X86_64_PC32
:
1172 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1176 case elfcpp::R_X86_64_16
:
1177 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1180 case elfcpp::R_X86_64_PC16
:
1181 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1185 case elfcpp::R_X86_64_8
:
1186 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1189 case elfcpp::R_X86_64_PC8
:
1190 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1194 case elfcpp::R_X86_64_PLT32
:
1195 gold_assert(gsym
->has_plt_offset()
1196 || gsym
->final_value_is_known());
1197 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1201 case elfcpp::R_X86_64_GOT32
:
1202 gold_assert(have_got_offset
);
1203 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1206 case elfcpp::R_X86_64_GOTPC32
:
1209 elfcpp::Elf_types
<64>::Elf_Addr value
;
1210 value
= target
->got_section(NULL
, NULL
)->address();
1211 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1215 case elfcpp::R_X86_64_GOT64
:
1216 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1217 // Since we always add a PLT entry, this is equivalent.
1218 case elfcpp::R_X86_64_GOTPLT64
: // TODO(csilvers): correct?
1219 gold_assert(have_got_offset
);
1220 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1223 case elfcpp::R_X86_64_GOTPC64
:
1226 elfcpp::Elf_types
<64>::Elf_Addr value
;
1227 value
= target
->got_section(NULL
, NULL
)->address();
1228 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1232 case elfcpp::R_X86_64_GOTOFF64
:
1234 elfcpp::Elf_types
<64>::Elf_Addr value
;
1235 value
= (psymval
->value(object
, 0)
1236 - target
->got_section(NULL
, NULL
)->address());
1237 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1241 case elfcpp::R_X86_64_GOTPCREL
:
1243 gold_assert(have_got_offset
);
1244 elfcpp::Elf_types
<64>::Elf_Addr value
;
1245 value
= target
->got_section(NULL
, NULL
)->address() + got_offset
;
1246 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1250 case elfcpp::R_X86_64_GOTPCREL64
:
1252 gold_assert(have_got_offset
);
1253 elfcpp::Elf_types
<64>::Elf_Addr value
;
1254 value
= target
->got_section(NULL
, NULL
)->address() + got_offset
;
1255 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1259 case elfcpp::R_X86_64_COPY
:
1260 case elfcpp::R_X86_64_GLOB_DAT
:
1261 case elfcpp::R_X86_64_JUMP_SLOT
:
1262 case elfcpp::R_X86_64_RELATIVE
:
1263 // These are outstanding tls relocs, which are unexpected when linking
1264 case elfcpp::R_X86_64_TPOFF64
:
1265 case elfcpp::R_X86_64_DTPMOD64
:
1266 case elfcpp::R_X86_64_TLSDESC
:
1267 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
1269 relinfo
->location(relnum
, rela
.get_r_offset()).c_str(),
1274 // These are initial tls relocs, which are expected when linking
1275 case elfcpp::R_X86_64_TLSGD
: // TODO(csilvers): correct?
1276 case elfcpp::R_X86_64_TLSLD
: // TODO(csilvers): correct?
1277 case elfcpp::R_X86_64_GOTTPOFF
: // TODO(csilvers): correct?
1278 case elfcpp::R_X86_64_TPOFF32
: // TODO(csilvers): correct?
1279 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // TODO(csilvers): correct?
1280 case elfcpp::R_X86_64_TLSDESC_CALL
: // TODO(csilvers): correct?
1281 case elfcpp::R_X86_64_DTPOFF32
:
1282 case elfcpp::R_X86_64_DTPOFF64
:
1283 this->relocate_tls(relinfo
, relnum
, rela
, r_type
, gsym
, psymval
, view
,
1284 address
, view_size
);
1287 case elfcpp::R_X86_64_SIZE32
: // TODO(csilvers): correct?
1288 case elfcpp::R_X86_64_SIZE64
: // TODO(csilvers): correct?
1289 case elfcpp::R_X86_64_PLTOFF64
: // TODO(csilvers): implement me!
1291 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
1293 relinfo
->location(relnum
, rela
.get_r_offset()).c_str(),
1302 // Perform a TLS relocation.
1305 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1307 const elfcpp::Rela
<64, false>& rel
,
1308 unsigned int r_type
,
1309 const Sized_symbol
<64>* gsym
,
1310 const Symbol_value
<64>* psymval
,
1311 unsigned char* view
,
1312 elfcpp::Elf_types
<64>::Elf_Addr
,
1315 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1316 if (tls_segment
== NULL
)
1318 fprintf(stderr
, _("%s: %s: TLS reloc but no TLS segment\n"),
1320 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
1324 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1326 const bool is_final
= (gsym
== NULL
1327 ? !parameters
->output_is_shared()
1328 : gsym
->final_value_is_known());
1329 const unsigned int opt_r_type
=
1330 Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1333 case elfcpp::R_X86_64_TPOFF32
: // Local-exec reloc
1334 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1335 Relocate_functions
<64, false>::rel32(view
, value
);
1338 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec reloc
1339 if (opt_r_type
== elfcpp::R_X86_64_TPOFF32
)
1341 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1342 rel
, r_type
, value
, view
,
1346 fprintf(stderr
, _("%s: %s: unsupported reloc type %u\n"),
1348 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1353 case elfcpp::R_X86_64_TLSGD
:
1354 if (opt_r_type
== elfcpp::R_X86_64_TPOFF32
)
1356 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1357 rel
, r_type
, value
, view
,
1361 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
1363 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1368 case elfcpp::R_X86_64_TLSLD
:
1369 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
1371 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1376 case elfcpp::R_X86_64_DTPOFF32
:
1377 if (opt_r_type
== elfcpp::R_X86_64_TPOFF32
)
1378 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1380 value
= value
- tls_segment
->vaddr();
1381 Relocate_functions
<64, false>::rel32(view
, value
);
1384 case elfcpp::R_X86_64_DTPOFF64
:
1385 if (opt_r_type
== elfcpp::R_X86_64_TPOFF64
)
1386 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1388 value
= value
- tls_segment
->vaddr();
1389 Relocate_functions
<64, false>::rel64(view
, value
);
1394 // Do a relocation in which we convert a TLS Initial-Exec to a
1396 // TODO(csilvers): verify this is right.
1399 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
1401 Output_segment
* tls_segment
,
1402 const elfcpp::Rela
<64, false>& rel
,
1404 elfcpp::Elf_types
<64>::Elf_Addr value
,
1405 unsigned char* view
,
1408 // We need to examine the opcodes to figure out which instruction we
1411 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1412 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1414 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -3);
1415 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, 4);
1417 unsigned char op1
= view
[-3];
1418 unsigned char op2
= view
[-2];
1419 unsigned char op3
= view
[-1];
1420 unsigned char reg
= op3
>> 3;
1428 view
[-1] = 0xc0 | reg
;
1432 // Special handling for %rsp.
1436 view
[-1] = 0xc0 | reg
;
1444 view
[-1] = 0x80 | reg
| (reg
<< 3);
1447 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1448 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1451 // Do a relocation in which we convert a TLS Global-Dynamic to a
1453 // TODO(csilvers): verify this is right.
1456 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
1458 Output_segment
* tls_segment
,
1459 const elfcpp::Rela
<64, false>& rel
,
1461 elfcpp::Elf_types
<64>::Elf_Addr value
,
1462 unsigned char* view
,
1465 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1466 // .word 0x6666; rex64; call __tls_get_addr
1467 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1469 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -4);
1470 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, 12);
1472 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
,
1473 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4)
1475 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
,
1476 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4)
1479 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1481 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1482 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
1484 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1486 this->skip_call_tls_get_addr_
= true;
1489 // Check the range for a TLS relocation.
1492 Target_x86_64::Relocate::check_range(const Relocate_info
<64, false>* relinfo
,
1494 const elfcpp::Rela
<64, false>& rel
,
1495 off_t view_size
, off_t off
)
1497 off_t offset
= rel
.get_r_offset() + off
;
1498 if (offset
< 0 || offset
> view_size
)
1500 fprintf(stderr
, _("%s: %s: TLS relocation out of range\n"),
1502 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
1507 // Check the validity of a TLS relocation. This is like assert.
1510 Target_x86_64::Relocate::check_tls(const Relocate_info
<64, false>* relinfo
,
1512 const elfcpp::Rela
<64, false>& rel
,
1518 _("%s: %s: TLS relocation against invalid instruction\n"),
1520 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
1525 // Relocate section data.
1528 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
1529 unsigned int sh_type
,
1530 const unsigned char* prelocs
,
1532 unsigned char* view
,
1533 elfcpp::Elf_types
<64>::Elf_Addr address
,
1536 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1538 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1539 Target_x86_64::Relocate
>(
1549 // Return the value to use for a dynamic which requires special
1550 // treatment. This is how we support equality comparisons of function
1551 // pointers across shared library boundaries, as described in the
1552 // processor specific ABI supplement.
1555 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
1557 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1558 return this->plt_section()->address() + gsym
->plt_offset();
1561 // Return a string used to fill a code section with nops to take up
1562 // the specified length.
1565 Target_x86_64::do_code_fill(off_t length
)
1569 // Build a jmpq instruction to skip over the bytes.
1570 unsigned char jmp
[5];
1572 elfcpp::Swap_unaligned
<64, false>::writeval(jmp
+ 1, length
- 5);
1573 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1574 + std::string(length
- 5, '\0'));
1577 // Nop sequences of various lengths.
1578 const char nop1
[1] = { 0x90 }; // nop
1579 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1580 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1581 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1582 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1583 0x00 }; // leal 0(%esi,1),%esi
1584 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1586 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1588 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1589 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1590 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1591 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1593 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1594 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1596 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1597 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1599 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1600 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1601 0x00, 0x00, 0x00, 0x00 };
1602 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1603 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1604 0x27, 0x00, 0x00, 0x00,
1606 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1607 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1608 0xbc, 0x27, 0x00, 0x00,
1610 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1611 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1612 0x90, 0x90, 0x90, 0x90,
1615 const char* nops
[16] = {
1617 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
1618 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
1621 return std::string(nops
[length
], length
);
1624 // The selector for x86_64 object files.
1626 class Target_selector_x86_64
: public Target_selector
1629 Target_selector_x86_64()
1630 : Target_selector(elfcpp::EM_X86_64
, 64, false)
1634 recognize(int machine
, int osabi
, int abiversion
);
1637 Target_x86_64
* target_
;
1640 // Recognize an x86_64 object file when we already know that the machine
1641 // number is EM_X86_64.
1644 Target_selector_x86_64::recognize(int, int, int)
1646 if (this->target_
== NULL
)
1647 this->target_
= new Target_x86_64();
1648 return this->target_
;
1651 Target_selector_x86_64 target_selector_x86_64
;
1653 } // End anonymous namespace.