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.
56 // See the ABI at http://www.x86-64.org/documentation/abi.pdf
58 class Target_x86_64
: public Sized_target
<64, false>
61 // In the x86_64 ABI, it says "The AMD64 ABI architectures uses only
62 // Elf64_Rela relocation entries with explicit addends."
63 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
66 : Sized_target
<64, false>(&x86_64_info
),
67 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
68 copy_relocs_(NULL
), dynbss_(NULL
)
71 // Scan the relocations to look for symbol adjustments.
73 scan_relocs(const General_options
& options
,
76 Sized_relobj
<64, false>* object
,
77 unsigned int data_shndx
,
79 const unsigned char* prelocs
,
81 size_t local_symbol_count
,
82 const unsigned char* plocal_symbols
,
83 Symbol
** global_symbols
);
85 // Finalize the sections.
87 do_finalize_sections(Layout
*);
89 // Relocate a section.
91 relocate_section(const Relocate_info
<64, false>*,
93 const unsigned char* prelocs
,
96 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
99 // Return a string used to fill a code section with nops.
101 do_code_fill(off_t length
);
104 // The class which scans relocations.
108 local(const General_options
& options
, Symbol_table
* symtab
,
109 Layout
* layout
, Target_x86_64
* target
,
110 Sized_relobj
<64, false>* object
,
111 unsigned int data_shndx
,
112 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
113 const elfcpp::Sym
<64, false>& lsym
);
116 global(const General_options
& options
, Symbol_table
* symtab
,
117 Layout
* layout
, Target_x86_64
* target
,
118 Sized_relobj
<64, false>* object
,
119 unsigned int data_shndx
,
120 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
124 // The class which implements relocation.
129 : skip_call_tls_get_addr_(false)
134 if (this->skip_call_tls_get_addr_
)
136 // FIXME: This needs to specify the location somehow.
137 fprintf(stderr
, _("%s: missing expected TLS relocation\n"),
143 // Do a relocation. Return false if the caller should not issue
144 // any warnings about this relocation.
146 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, size_t relnum
,
147 const elfcpp::Rela
<64, false>&,
148 unsigned int r_type
, const Sized_symbol
<64>*,
149 const Symbol_value
<64>*,
150 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
154 // Do a TLS relocation.
156 relocate_tls(const Relocate_info
<64, false>*, size_t relnum
,
157 const elfcpp::Rela
<64, false>&,
158 unsigned int r_type
, const Sized_symbol
<64>*,
159 const Symbol_value
<64>*,
160 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
, off_t
);
162 // Do a TLS Initial-Exec to Local-Exec transition.
164 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
165 Output_segment
* tls_segment
,
166 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
167 elfcpp::Elf_types
<64>::Elf_Addr value
,
171 // Do a TLS Global-Dynamic to Local-Exec transition.
173 tls_gd_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 // Check the range for a TLS relocation.
182 check_range(const Relocate_info
<64, false>*, size_t relnum
,
183 const elfcpp::Rela
<64, false>&, off_t
, off_t
);
185 // Check the validity of a TLS relocation. This is like assert.
187 check_tls(const Relocate_info
<64, false>*, size_t relnum
,
188 const elfcpp::Rela
<64, false>&, bool);
190 // This is set if we should skip the next reloc, which should be a
191 // PLT32 reloc against ___tls_get_addr.
192 bool skip_call_tls_get_addr_
;
195 // Adjust TLS relocation type based on the options and whether this
196 // is a local symbol.
198 optimize_tls_reloc(bool is_final
, int r_type
);
200 // Get the GOT section, creating it if necessary.
201 Output_data_got
<64, false>*
202 got_section(Symbol_table
*, Layout
*);
204 // Create a PLT entry for a global symbol.
206 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
208 // Get the PLT section.
209 Output_data_plt_x86_64
*
212 gold_assert(this->plt_
!= NULL
);
216 // Get the dynamic reloc section, creating it if necessary.
218 rel_dyn_section(Layout
*);
220 // Copy a relocation against a global symbol.
222 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
223 Sized_relobj
<64, false>*, unsigned int,
224 Symbol
*, const elfcpp::Rela
<64, false>&);
226 // Information about this specific target which we pass to the
227 // general Target structure.
228 static const Target::Target_info x86_64_info
;
231 Output_data_got
<64, false>* got_
;
233 Output_data_plt_x86_64
* plt_
;
234 // The GOT PLT section.
235 Output_data_space
* got_plt_
;
236 // The dynamic reloc section.
237 Reloc_section
* rel_dyn_
;
238 // Relocs saved to avoid a COPY reloc.
239 Copy_relocs
<64, false>* copy_relocs_
;
240 // Space for variables copied with a COPY reloc.
241 Output_data_space
* dynbss_
;
244 const Target::Target_info
Target_x86_64::x86_64_info
=
247 false, // is_big_endian
248 elfcpp::EM_X86_64
, // machine_code
249 false, // has_make_symbol
250 false, // has_resolve
251 true, // has_code_fill
252 "/lib/ld64.so.1", // program interpreter
253 0x400000, // text_segment_address
254 0x1000, // abi_pagesize
255 0x1000 // common_pagesize
258 // Get the GOT section, creating it if necessary.
260 Output_data_got
<64, false>*
261 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
263 if (this->got_
== NULL
)
265 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
267 this->got_
= new Output_data_got
<64, false>();
269 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
270 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
273 // The old GNU linker creates a .got.plt section. We just
274 // create another set of data in the .got section. Note that we
275 // always create a PLT if we create a GOT, although the PLT
277 // TODO(csilvers): do we really need an alignment of 8?
278 this->got_plt_
= new Output_data_space(8);
279 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
280 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
283 // The first three entries are reserved.
284 this->got_plt_
->set_space_size(3 * 8);
286 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
287 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
289 0, 0, elfcpp::STT_OBJECT
,
291 elfcpp::STV_HIDDEN
, 0,
298 // Get the dynamic reloc section, creating it if necessary.
300 Target_x86_64::Reloc_section
*
301 Target_x86_64::rel_dyn_section(Layout
* layout
)
303 if (this->rel_dyn_
== NULL
)
305 gold_assert(layout
!= NULL
);
306 this->rel_dyn_
= new Reloc_section();
307 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
308 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
310 return this->rel_dyn_
;
313 // A class to handle the PLT data.
315 class Output_data_plt_x86_64
: public Output_section_data
318 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
320 Output_data_plt_x86_64(Layout
*, Output_data_space
*);
322 // Add an entry to the PLT.
324 add_entry(Symbol
* gsym
);
326 // Return the .rel.plt section data.
329 { return this->rel_
; }
333 do_adjust_output_section(Output_section
* os
);
336 // The size of an entry in the PLT.
337 static const int plt_entry_size
= 16;
339 // The first entry in the PLT.
340 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
341 // procedure linkage table for both programs and shared objects."
342 static unsigned char first_plt_entry
[plt_entry_size
];
344 // Other entries in the PLT for an executable.
345 static unsigned char plt_entry
[plt_entry_size
];
347 // Set the final size.
349 do_set_address(uint64_t, off_t
)
350 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
352 // Write out the PLT data.
354 do_write(Output_file
*);
356 // The reloc section.
358 // The .got.plt section.
359 Output_data_space
* got_plt_
;
360 // The number of PLT entries.
364 // Create the PLT section. The ordinary .got section is an argument,
365 // since we need to refer to the start. We also create our own .got
366 // section just for PLT entries.
368 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
369 Output_data_space
* got_plt
)
370 // TODO(csilvers): do we really need an alignment of 8?
371 : Output_section_data(8), got_plt_(got_plt
), count_(0)
373 this->rel_
= new Reloc_section();
374 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
375 elfcpp::SHF_ALLOC
, this->rel_
);
379 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
381 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
382 // linker, and so do we.
386 // Add an entry to the PLT.
389 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
391 gold_assert(!gsym
->has_plt_offset());
393 // Note that when setting the PLT offset we skip the initial
394 // reserved PLT entry.
395 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
399 off_t got_offset
= this->got_plt_
->data_size();
401 // Every PLT entry needs a GOT entry which points back to the PLT
402 // entry (this will be changed by the dynamic linker, normally
403 // lazily when the function is called).
404 this->got_plt_
->set_space_size(got_offset
+ 8);
406 // Every PLT entry needs a reloc.
407 gsym
->set_needs_dynsym_entry();
408 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
411 // Note that we don't need to save the symbol. The contents of the
412 // PLT are independent of which symbols are used. The symbols only
413 // appear in the relocations.
416 // The first entry in the PLT for an executable.
418 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
420 // From AMD64 ABI Draft 0.98, page 76
421 0xff, 0x35, // pushq contents of memory address
422 0, 0, 0, 0, // replaced with address of .got + 4
423 0xff, 0x25, // jmp indirect
424 0, 0, 0, 0, // replaced with address of .got + 8
425 0x90, 0x90, 0x90, 0x90 // noop (x4)
428 // Subsequent entries in the PLT for an executable.
430 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
432 // From AMD64 ABI Draft 0.98, page 76
433 0xff, 0x25, // jmpq indirect
434 0, 0, 0, 0, // replaced with address of symbol in .got
435 0x68, // pushq immediate
436 0, 0, 0, 0, // replaced with offset into relocation table
437 0xe9, // jmpq relative
438 0, 0, 0, 0 // replaced with offset to start of .plt
441 // Write out the PLT. This uses the hand-coded instructions above,
442 // and adjusts them as needed. This is specified by the AMD64 ABI.
445 Output_data_plt_x86_64::do_write(Output_file
* of
)
447 const off_t offset
= this->offset();
448 const off_t oview_size
= this->data_size();
449 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
451 const off_t got_file_offset
= this->got_plt_
->offset();
452 const off_t got_size
= this->got_plt_
->data_size();
453 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
456 unsigned char* pov
= oview
;
458 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
459 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
461 memcpy(pov
, first_plt_entry
, plt_entry_size
);
462 if (!parameters
->output_is_shared())
464 // We do a jmp relative to the PC at the end of this instruction.
465 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 8
466 - (plt_address
+ 6));
467 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 16
468 - (plt_address
+ 12));
470 pov
+= plt_entry_size
;
472 unsigned char* got_pov
= got_view
;
474 memset(got_pov
, 0, 24);
477 unsigned int plt_offset
= plt_entry_size
;
478 unsigned int got_offset
= 24;
479 const unsigned int count
= this->count_
;
480 for (unsigned int plt_index
= 0;
483 pov
+= plt_entry_size
,
485 plt_offset
+= plt_entry_size
,
488 // Set and adjust the PLT entry itself.
489 memcpy(pov
, plt_entry
, plt_entry_size
);
490 if (parameters
->output_is_shared())
491 // FIXME(csilvers): what's the right thing to write here?
492 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
494 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
495 (got_address
+ got_offset
496 - (plt_address
+ plt_offset
499 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
500 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
501 - (plt_offset
+ plt_entry_size
));
503 // Set the entry in the GOT.
504 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
507 gold_assert(pov
- oview
== oview_size
);
508 gold_assert(got_pov
- got_view
== got_size
);
510 of
->write_output_view(offset
, oview_size
, oview
);
511 of
->write_output_view(got_file_offset
, got_size
, got_view
);
514 // Create a PLT entry for a global symbol.
517 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
520 if (gsym
->has_plt_offset())
523 if (this->plt_
== NULL
)
525 // Create the GOT sections first.
526 this->got_section(symtab
, layout
);
528 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_plt_
);
529 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
531 | elfcpp::SHF_EXECINSTR
),
535 this->plt_
->add_entry(gsym
);
538 // Handle a relocation against a non-function symbol defined in a
539 // dynamic object. The traditional way to handle this is to generate
540 // a COPY relocation to copy the variable at runtime from the shared
541 // object into the executable's data segment. However, this is
542 // undesirable in general, as if the size of the object changes in the
543 // dynamic object, the executable will no longer work correctly. If
544 // this relocation is in a writable section, then we can create a
545 // dynamic reloc and the dynamic linker will resolve it to the correct
546 // address at runtime. However, we do not want do that if the
547 // relocation is in a read-only section, as it would prevent the
548 // readonly segment from being shared. And if we have to eventually
549 // generate a COPY reloc, then any dynamic relocations will be
550 // useless. So this means that if this is a writable section, we need
551 // to save the relocation until we see whether we have to create a
552 // COPY relocation for this symbol for any other relocation.
555 Target_x86_64::copy_reloc(const General_options
* options
,
556 Symbol_table
* symtab
,
558 Sized_relobj
<64, false>* object
,
559 unsigned int data_shndx
, Symbol
* gsym
,
560 const elfcpp::Rela
<64, false>& rel
)
562 Sized_symbol
<64>* ssym
;
563 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(64) (gsym
566 if (!Copy_relocs
<64, false>::need_copy_reloc(options
, object
,
569 // So far we do not need a COPY reloc. Save this relocation.
570 // If it turns out that we never need a COPY reloc for this
571 // symbol, then we will emit the relocation.
572 if (this->copy_relocs_
== NULL
)
573 this->copy_relocs_
= new Copy_relocs
<64, false>();
574 this->copy_relocs_
->save(ssym
, object
, data_shndx
, rel
);
578 // Allocate space for this symbol in the .bss section.
580 elfcpp::Elf_types
<64>::Elf_WXword symsize
= ssym
->symsize();
582 // There is no defined way to determine the required alignment
583 // of the symbol. We pick the alignment based on the size. We
584 // set an arbitrary maximum of 256.
586 for (align
= 1; align
< 512; align
<<= 1)
587 if ((symsize
& align
) != 0)
590 if (this->dynbss_
== NULL
)
592 this->dynbss_
= new Output_data_space(align
);
593 layout
->add_output_section_data(".bss",
596 | elfcpp::SHF_WRITE
),
600 Output_data_space
* dynbss
= this->dynbss_
;
602 if (align
> dynbss
->addralign())
603 dynbss
->set_space_alignment(align
);
605 off_t dynbss_size
= dynbss
->data_size();
606 dynbss_size
= align_address(dynbss_size
, align
);
607 off_t offset
= dynbss_size
;
608 dynbss
->set_space_size(dynbss_size
+ symsize
);
610 // Define the symbol in the .dynbss section.
611 symtab
->define_in_output_data(this, ssym
->name(), ssym
->version(),
612 dynbss
, offset
, symsize
, ssym
->type(),
613 ssym
->binding(), ssym
->visibility(),
614 ssym
->nonvis(), false, false);
616 // Add the COPY reloc.
617 ssym
->set_needs_dynsym_entry();
618 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
619 rel_dyn
->add_global(ssym
, elfcpp::R_X86_64_COPY
, dynbss
, offset
,
625 // Optimize the TLS relocation type based on what we know about the
626 // symbol. IS_FINAL is true if the final address of this symbol is
627 // known at link time.
630 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
632 return is_final
? r_type
: 0;
634 // If we are generating a shared library, then we can't do anything
636 if (parameters
->output_is_shared())
641 case elfcpp::R_X86_64_TLSGD
:
642 // These are Global-Dynamic which permits fully general TLS
643 // access. Since we know that we are generating an executable,
644 // we can convert this to Initial-Exec. If we also know that
645 // this is a local symbol, we can further switch to Local-Exec.
647 return elfcpp::R_X86_64_TLS_LE_64
;
648 return elfcpp::R_X86_64_TLS_IE_64
;
650 case elfcpp::R_X86_64_TLS_LDM
:
651 // This is Local-Dynamic, which refers to a local symbol in the
652 // dynamic TLS block. Since we know that we generating an
653 // executable, we can switch to Local-Exec.
654 return elfcpp::R_X86_64_TLS_LE_64
;
656 case elfcpp::R_X86_64_TLS_LDO_64
:
657 // Another type of Local-Dynamic relocation.
658 return elfcpp::R_X86_64_TLS_LE
;
660 case elfcpp::R_X86_64_TLS_IE
:
661 case elfcpp::R_X86_64_TLS_GOTIE
:
662 case elfcpp::R_X86_64_TLS_IE_64
:
663 // These are Initial-Exec relocs which get the thread offset
664 // from the GOT. If we know that we are linking against the
665 // local symbol, we can switch to Local-Exec, which links the
666 // thread offset into the instruction.
668 return elfcpp::R_X86_64_TLS_LE_64
;
671 case elfcpp::R_X86_64_TLS_LE
:
672 case elfcpp::R_X86_64_TLS_LE_64
:
673 // When we already have Local-Exec, there is nothing further we
683 // Scan a relocation for a local symbol.
686 Target_x86_64::Scan::local(const General_options
&,
687 Symbol_table
* symtab
,
689 Target_x86_64
* target
,
690 Sized_relobj
<64, false>* object
,
692 const elfcpp::Rela
<64, false>&,
694 const elfcpp::Sym
<64, false>&)
698 case elfcpp::R_X86_64_NONE
:
701 case elfcpp::R_X86_64_64
:
702 case elfcpp::R_X86_64_32
:
703 case elfcpp::R_X86_64_32S
:
704 case elfcpp::R_X86_64_16
:
705 case elfcpp::R_X86_64_8
:
706 // FIXME: If we are generating a shared object we need to copy
707 // this relocation into the object.
708 gold_assert(!parameters
->output_is_shared());
711 case elfcpp::R_X86_64_PC64
:
712 case elfcpp::R_X86_64_PC32
:
713 case elfcpp::R_X86_64_PC16
:
714 case elfcpp::R_X86_64_PC8
:
717 case elfcpp::R_X86_64_GOTOFF64
:
718 case elfcpp::R_X86_64_GOTPCREL
:
719 // We need a GOT section.
720 target
->got_section(symtab
, layout
);
723 case elfcpp::R_X86_64_COPY
:
724 case elfcpp::R_X86_64_GLOB_DAT
:
725 case elfcpp::R_X86_64_JUMP_SLOT
:
726 case elfcpp::R_X86_64_RELATIVE
:
727 case elfcpp::R_X86_64_TPOFF64
:
728 case elfcpp::R_X86_64_TPOFF32
:
729 case elfcpp::R_X86_64_DTPMOD64
:
730 case elfcpp::R_X86_64_DTPOFF64
:
731 case elfcpp::R_X86_64_DTPOFF32
:
732 case elfcpp::R_X86_64_TLSDESC
:
733 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
734 program_name
, object
->name().c_str(), r_type
);
739 case elfcpp::R_X86_64_TLS_IE
:
740 case elfcpp::R_X86_64_TLS_GOTIE
:
741 case elfcpp::R_X86_64_TLS_LE
:
742 case elfcpp::R_X86_64_TLS_GD
:
743 case elfcpp::R_X86_64_TLS_LDM
:
744 case elfcpp::R_X86_64_TLS_LDO_64
:
745 case elfcpp::R_X86_64_TLS_IE_64
:
746 case elfcpp::R_X86_64_TLS_LE_64
:
748 bool output_is_shared
= parameters
->output_is_shared();
749 r_type
= Target_x86_64::optimize_tls_reloc(!output_is_shared
,
753 case elfcpp::R_X86_64_TLS_LE
:
754 case elfcpp::R_X86_64_TLS_LE_64
:
755 // FIXME: If generating a shared object, we need to copy
756 // this relocation into the object.
757 gold_assert(!output_is_shared
);
760 case elfcpp::R_X86_64_TLS_IE
:
761 case elfcpp::R_X86_64_TLS_GOTIE
:
762 case elfcpp::R_X86_64_TLS_GD
:
763 case elfcpp::R_X86_64_TLS_LDM
:
764 case elfcpp::R_X86_64_TLS_LDO_64
:
765 case elfcpp::R_X86_64_TLS_IE_64
:
767 _("%s: %s: unsupported reloc %u against local symbol\n"),
768 program_name
, object
->name().c_str(), r_type
);
776 case elfcpp::R_X86_64_GOT32
:
777 case elfcpp::R_X86_64_PLT32
:
778 case elfcpp::R_X86_64_TLS_GD_64
:
779 case elfcpp::R_X86_64_TLS_GD_PUSH
:
780 case elfcpp::R_X86_64_TLS_GD_CALL
:
781 case elfcpp::R_X86_64_TLS_GD_POP
:
782 case elfcpp::R_X86_64_TLS_LDM_64
:
783 case elfcpp::R_X86_64_TLS_LDM_PUSH
:
784 case elfcpp::R_X86_64_TLS_LDM_CALL
:
785 case elfcpp::R_X86_64_TLS_LDM_POP
:
788 fprintf(stderr
, _("%s: %s: unsupported reloc %u against local symbol\n"),
789 program_name
, object
->name().c_str(), r_type
);
795 // Scan a relocation for a global symbol.
798 Target_x86_64::Scan::global(const General_options
& options
,
799 Symbol_table
* symtab
,
801 Target_x86_64
* target
,
802 Sized_relobj
<64, false>* object
,
803 unsigned int data_shndx
,
804 const elfcpp::Rela
<64, false>& reloc
,
810 case elfcpp::R_X86_64_NONE
:
813 case elfcpp::R_X86_64_64
:
814 case elfcpp::R_X86_64_PC64
:
815 case elfcpp::R_X86_64_32
:
816 case elfcpp::R_X86_64_32S
:
817 case elfcpp::R_X86_64_PC32
:
818 case elfcpp::R_X86_64_16
:
819 case elfcpp::R_X86_64_PC16
:
820 case elfcpp::R_X86_64_8
:
821 case elfcpp::R_X86_64_PC8
:
822 // FIXME: If we are generating a shared object we may need to
823 // copy this relocation into the object. If this symbol is
824 // defined in a shared object, we may need to copy this
825 // relocation in order to avoid a COPY relocation.
826 gold_assert(!parameters
->output_is_shared());
828 if (gsym
->is_from_dynobj())
830 // This symbol is defined in a dynamic object. If it is a
831 // function, we make a PLT entry. Otherwise we need to
832 // either generate a COPY reloc or copy this reloc.
833 if (gsym
->type() == elfcpp::STT_FUNC
)
834 target
->make_plt_entry(symtab
, layout
, gsym
);
836 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
842 case elfcpp::R_X86_64_GOT32
:
844 // The symbol requires a GOT entry.
845 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
846 if (got
->add_global(gsym
))
848 // If this symbol is not fully resolved, we need to add a
849 // dynamic relocation for it.
850 if (!gsym
->final_value_is_known())
852 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
853 rel_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
, got
,
854 gsym
->got_offset(), reloc
.get_r_addend());
860 case elfcpp::R_X86_64_PLT32
:
861 // If the symbol is fully resolved, this is just a PC32 reloc.
862 // Otherwise we need a PLT entry.
863 if (gsym
->final_value_is_known())
865 target
->make_plt_entry(symtab
, layout
, gsym
);
868 case elfcpp::R_X86_64_GOTOFF64
:
869 case elfcpp::R_X86_64_GOTPC32
:
870 case elfcpp::R_X86_64_GOTPCREL
:
871 // We need a GOT section.
872 target
->got_section(symtab
, layout
);
876 case elfcpp::R_X86_64_COPY
:
877 case elfcpp::R_X86_64_GLOB_DAT
:
878 case elfcpp::R_X86_64_JUMP_SLOT
:
879 case elfcpp::R_X86_64_RELATIVE
:
880 case elfcpp::R_X86_64_TLS_TPOFF
:
881 case elfcpp::R_X86_64_DTPMOD64
:
882 case elfcpp::R_X86_64_DTPOFF64
:
883 case elfcpp::R_X86_64_DTPOFF32
:
884 case elfcpp::R_X86_64_TLS_TPOFF64
:
885 case elfcpp::R_X86_64_TLS_DESC
:
886 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
887 program_name
, object
->name().c_str(), r_type
);
893 case elfcpp::R_X86_64_TLS_IE
:
894 case elfcpp::R_X86_64_TLS_GOTIE
:
895 case elfcpp::R_X86_64_TLS_LE
:
896 case elfcpp::R_X86_64_TLS_GD
:
897 case elfcpp::R_X86_64_TLS_LDM
:
898 case elfcpp::R_X86_64_TLS_LDO_64
:
899 case elfcpp::R_X86_64_TLS_IE_64
:
900 case elfcpp::R_X86_64_TLS_LE_64
:
902 const bool is_final
= gsym
->final_value_is_known();
903 r_type
= Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
906 case elfcpp::R_X86_64_TLS_LE
:
907 case elfcpp::R_X86_64_TLS_LE_64
:
908 // FIXME: If generating a shared object, we need to copy
909 // this relocation into the object.
910 gold_assert(!parameters
->output_is_shared());
913 case elfcpp::R_X86_64_TLS_IE
:
914 case elfcpp::R_X86_64_TLS_GOTIE
:
915 case elfcpp::R_X86_64_TLS_GD
:
916 case elfcpp::R_X86_64_TLS_LDM
:
917 case elfcpp::R_X86_64_TLS_LDO_64
:
918 case elfcpp::R_X86_64_TLS_IE_64
:
920 _("%s: %s: unsupported reloc %u "
921 "against global symbol %s\n"),
922 program_name
, object
->name().c_str(), r_type
,
931 case elfcpp::R_X86_64_TLS_GD_64
:
932 case elfcpp::R_X86_64_TLS_GD_PUSH
:
933 case elfcpp::R_X86_64_TLS_GD_CALL
:
934 case elfcpp::R_X86_64_TLS_GD_POP
:
935 case elfcpp::R_X86_64_TLS_LDM_64
:
936 case elfcpp::R_X86_64_TLS_LDM_PUSH
:
937 case elfcpp::R_X86_64_TLS_LDM_CALL
:
938 case elfcpp::R_X86_64_TLS_LDM_POP
:
942 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
943 program_name
, object
->name().c_str(), r_type
, gsym
->name());
948 // Scan relocations for a section.
951 Target_x86_64::scan_relocs(const General_options
& options
,
952 Symbol_table
* symtab
,
954 Sized_relobj
<64, false>* object
,
955 unsigned int data_shndx
,
956 unsigned int sh_type
,
957 const unsigned char* prelocs
,
959 size_t local_symbol_count
,
960 const unsigned char* plocal_symbols
,
961 Symbol
** global_symbols
)
963 if (sh_type
== elfcpp::SHT_REL
)
965 fprintf(stderr
, _("%s: %s: unsupported REL reloc section\n"),
966 program_name
, object
->name().c_str());
970 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
971 Target_x86_64::Scan
>(
985 // Finalize the sections.
988 Target_x86_64::do_finalize_sections(Layout
* layout
)
990 // Fill in some more dynamic tags.
991 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
994 if (this->got_plt_
!= NULL
)
995 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
997 if (this->plt_
!= NULL
)
999 const Output_data
* od
= this->plt_
->rel_plt();
1000 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1001 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1002 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1005 if (this->rel_dyn_
!= NULL
)
1007 const Output_data
* od
= this->rel_dyn_
;
1008 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1009 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1010 odyn
->add_constant(elfcpp::DT_RELAENT
,
1011 elfcpp::Elf_sizes
<64>::rel_size
);
1014 if (!parameters
->output_is_shared())
1016 // The value of the DT_DEBUG tag is filled in by the dynamic
1017 // linker at run time, and used by the debugger.
1018 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1022 // Emit any relocs we saved in an attempt to avoid generating COPY
1024 if (this->copy_relocs_
== NULL
)
1026 if (this->copy_relocs_
->any_to_emit())
1028 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1029 this->copy_relocs_
->emit(rel_dyn
);
1031 delete this->copy_relocs_
;
1032 this->copy_relocs_
= NULL
;
1035 // Perform a relocation.
1038 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1039 Target_x86_64
* target
,
1041 const elfcpp::Rela
<64, false>& rel
,
1042 unsigned int r_type
,
1043 const Sized_symbol
<64>* gsym
,
1044 const Symbol_value
<64>* psymval
,
1045 unsigned char* view
,
1046 elfcpp::Elf_types
<64>::Elf_Addr address
,
1049 if (this->skip_call_tls_get_addr_
)
1051 if (r_type
!= elfcpp::R_X86_64_PLT32
1053 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1055 fprintf(stderr
, _("%s: %s: missing expected TLS relocation\n"),
1057 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
1061 this->skip_call_tls_get_addr_
= false;
1066 // Pick the value to use for symbols defined in shared objects.
1067 Symbol_value
<64> symval
;
1068 if (gsym
!= NULL
&& gsym
->is_from_dynobj() && gsym
->has_plt_offset())
1070 symval
.set_output_value(target
->plt_section()->address()
1071 + gsym
->plt_offset());
1075 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1076 const elfcpp::Elf_Xword addend
= rel
.get_r_addend();
1080 case elfcpp::R_X86_64_NONE
:
1083 case elfcpp::R_X86_64_64
:
1084 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1087 case elfcpp::R_X86_64_PC64
:
1088 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1092 case elfcpp::R_X86_64_32
:
1093 // FIXME: Needs error checking.
1094 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1097 case elfcpp::R_X86_64_32S
:
1098 // FIXME: Needs error checking.
1099 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1102 case elfcpp::R_X86_64_PC32
:
1103 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1107 case elfcpp::R_X86_64_16
:
1108 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1111 case elfcpp::R_X86_64_PC16
:
1112 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1116 case elfcpp::R_X86_64_8
:
1117 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1120 case elfcpp::R_X86_64_PC8
:
1121 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1125 case elfcpp::R_X86_64_PLT32
:
1126 gold_assert(gsym
->has_plt_offset()
1127 || gsym
->final_value_is_known());
1128 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1132 case elfcpp::R_X86_64_GOT32
:
1133 // Local GOT offsets not yet supported.
1135 gold_assert(gsym
->has_got_offset());
1136 Relocate_functions
<64, false>::rela32(view
, gsym
->got_offset(), addend
);
1139 case elfcpp::R_X86_64_GOTOFF64
:
1141 elfcpp::Elf_types
<64>::Elf_Addr value
;
1142 value
= (psymval
->value(object
, 0)
1143 - target
->got_section(NULL
, NULL
)->address());
1144 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1148 case elfcpp::R_X86_64_GOTPCREL
:
1151 elfcpp::Elf_types
<64>::Elf_Addr value
;
1152 // FIXME(csilvers): this is probably totally wrong for G + GOT
1153 value
= (target
->got_section(NULL
, NULL
)->address()
1154 + (gsym
->has_got_offset() ? gsym
->got_offset() : 0));
1155 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1159 case elfcpp::R_X86_64_COPY
:
1160 case elfcpp::R_X86_64_GLOB_DAT
:
1161 case elfcpp::R_X86_64_JUMP_SLOT
:
1162 case elfcpp::R_X86_64_RELATIVE
:
1163 case elfcpp::R_X86_64_TPOFF64
:
1164 case elfcpp::R_X86_64_TPOFF32
:
1165 case elfcpp::R_X86_64_DTPMOD64
:
1166 case elfcpp::R_X86_64_DTPOFF64
:
1167 case elfcpp::R_X86_64_DTPOFF32
:
1168 case elfcpp::R_X86_64_TLSDESC
:
1169 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
1171 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1177 case elfcpp::R_X86_64_TLS_IE
:
1178 case elfcpp::R_X86_64_TLS_GOTIE
:
1179 case elfcpp::R_X86_64_TLS_LE
:
1180 case elfcpp::R_X86_64_TLS_GD
:
1181 case elfcpp::R_X86_64_TLS_LDM
:
1182 case elfcpp::R_X86_64_TLS_LDO_64
:
1183 case elfcpp::R_X86_64_TLS_IE_64
:
1184 case elfcpp::R_X86_64_TLS_LE_64
:
1185 this->relocate_tls(relinfo
, relnum
, rel
, r_type
, gsym
, psymval
, view
,
1186 address
, view_size
);
1189 view_size
++; // this is to make view_size used
1193 case elfcpp::R_X86_64_TLS_GD_64
:
1194 case elfcpp::R_X86_64_TLS_GD_PUSH
:
1195 case elfcpp::R_X86_64_TLS_GD_CALL
:
1196 case elfcpp::R_X86_64_TLS_GD_POP
:
1197 case elfcpp::R_X86_64_TLS_LDM_64
:
1198 case elfcpp::R_X86_64_TLS_LDM_PUSH
:
1199 case elfcpp::R_X86_64_TLS_LDM_CALL
:
1200 case elfcpp::R_X86_64_TLS_LDM_POP
:
1203 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
1205 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1207 // gold_exit(false);
1214 // Perform a TLS relocation.
1217 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>*, // relinfo,
1219 const elfcpp::Rela
<64, false>& , // rel,
1220 unsigned int , // r_type,
1221 const Sized_symbol
<64>* , // gsym,
1222 const Symbol_value
<64>* , // psymval,
1223 unsigned char* , // view,
1224 elfcpp::Elf_types
<64>::Elf_Addr
,
1228 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1229 if (tls_segment
== NULL
)
1231 fprintf(stderr
, _("%s: %s: TLS reloc but no TLS segment\n"),
1233 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
1237 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1239 const bool is_final
= (gsym
== NULL
1240 ? !parameters
->output_is_shared()
1241 : gsym
->final_value_is_known());
1242 const unsigned int opt_r_type
=
1243 Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1246 case elfcpp::R_X86_64_TLS_LE_64
:
1247 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1248 Relocate_functions
<64, false>::rel64(view
, value
);
1251 case elfcpp::R_X86_64_TLS_LE
:
1252 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1253 Relocate_functions
<64, false>::rel64(view
, value
);
1256 case elfcpp::R_X86_64_TLS_IE
:
1257 case elfcpp::R_X86_64_TLS_GOTIE
:
1258 case elfcpp::R_X86_64_TLS_IE_64
:
1259 if (opt_r_type
== elfcpp::R_X86_64_TLS_LE_64
)
1261 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1262 rel
, r_type
, value
, view
,
1266 fprintf(stderr
, _("%s: %s: unsupported reloc type %u\n"),
1268 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1270 // gold_exit(false);
1273 case elfcpp::R_X86_64_TLS_GD
:
1274 if (opt_r_type
== elfcpp::R_X86_64_TLS_LE_64
)
1276 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1277 rel
, r_type
, value
, view
,
1281 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
1283 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1285 // gold_exit(false);
1288 case elfcpp::R_X86_64_TLS_LDM
:
1289 case elfcpp::R_X86_64_TLS_LDO_64
:
1290 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
1292 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1294 // gold_exit(false);
1300 // Do a relocation in which we convert a TLS Initial-Exec to a
1304 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* , // relinfo,
1306 Output_segment
* , // tls_segment,
1307 const elfcpp::Rela
<64, false>& , // rel,
1308 unsigned int , // r_type,
1309 elfcpp::Elf_types
<64>::Elf_Addr
, // value,
1310 unsigned char* , // view,
1311 off_t
) // view_size)
1314 // We have to actually change the instructions, which means that we
1315 // need to examine the opcodes to figure out which instruction we
1317 if (r_type
== elfcpp::R_X86_64_TLS_IE
)
1319 // movl %gs:XX,%eax ==> movl $YY,%eax
1320 // movl %gs:XX,%reg ==> movl $YY,%reg
1321 // addl %gs:XX,%reg ==> addl $YY,%reg
1322 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -1);
1323 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, 4);
1325 unsigned char op1
= view
[-1];
1328 // movl XX,%eax ==> movl $YY,%eax
1333 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
,
1336 unsigned char op2
= view
[-2];
1339 // movl XX,%reg ==> movl $YY,%reg
1340 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
,
1341 (op1
& 0xc7) == 0x05);
1343 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1345 else if (op2
== 0x03)
1347 // addl XX,%reg ==> addl $YY,%reg
1348 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
,
1349 (op1
& 0xc7) == 0x05);
1351 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1354 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
, 0);
1359 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1360 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1361 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1362 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -2);
1363 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, 4);
1365 unsigned char op1
= view
[-1];
1366 unsigned char op2
= view
[-2];
1367 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
,
1368 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
1371 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1373 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1375 else if (op2
== 0x2b)
1377 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1379 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
1381 else if (op2
== 0x03)
1383 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1385 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1388 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
, 0);
1391 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1392 if (r_type
== elfcpp::R_X86_64_TLS_IE
|| r_type
== elfcpp::R_X86_64_TLS_GOTIE
)
1395 Relocate_functions
<64, false>::rel64(view
, value
);
1399 // Do a relocation in which we convert a TLS Global-Dynamic to a
1403 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* , // relinfo,
1405 Output_segment
* , // tls_segment,
1406 const elfcpp::Rela
<64, false>& , // rel,
1408 elfcpp::Elf_types
<64>::Elf_Addr
, // value,
1409 unsigned char* , // view,
1413 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1414 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1415 // leal foo(%reg),%eax; call ___tls_get_addr
1416 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1418 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -2);
1419 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, 9);
1421 unsigned char op1
= view
[-1];
1422 unsigned char op2
= view
[-2];
1424 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
,
1425 op2
== 0x8d || op2
== 0x04);
1426 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
,
1433 Target_x86_64::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -3);
1434 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
,
1436 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
,
1437 ((op1
& 0xc7) == 0x05
1438 && op1
!= (4 << 3)));
1439 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1443 Target_x86_64::Relocate::check_tls(relinfo
, relnum
, rel
,
1444 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1445 if (rel
.get_r_offset() + 9 < view_size
&& view
[9] == 0x90)
1447 // There is a trailing nop. Use the size byte subl.
1448 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1453 // Use the five byte subl.
1454 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1458 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1459 Relocate_functions
<64, false>::rel64(view
+ roff
, value
);
1461 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1463 this->skip_call_tls_get_addr_
= true;
1467 // Check the range for a TLS relocation.
1470 Target_x86_64::Relocate::check_range(const Relocate_info
<64, false>* relinfo
,
1472 const elfcpp::Rela
<64, false>& rel
,
1473 off_t view_size
, off_t off
)
1475 off_t offset
= rel
.get_r_offset() + off
;
1476 if (offset
< 0 || offset
> view_size
)
1478 fprintf(stderr
, _("%s: %s: TLS relocation out of range\n"),
1480 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
1485 // Check the validity of a TLS relocation. This is like assert.
1488 Target_x86_64::Relocate::check_tls(const Relocate_info
<64, false>* relinfo
,
1490 const elfcpp::Rela
<64, false>& rel
,
1496 _("%s: %s: TLS relocation against invalid instruction\n"),
1498 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
1503 // Relocate section data.
1506 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
1507 unsigned int sh_type
,
1508 const unsigned char* prelocs
,
1510 unsigned char* view
,
1511 elfcpp::Elf_types
<64>::Elf_Addr address
,
1514 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1516 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1517 Target_x86_64::Relocate
>(
1527 // Return a string used to fill a code section with nops to take up
1528 // the specified length.
1531 Target_x86_64::do_code_fill(off_t length
)
1535 // Build a jmpq instruction to skip over the bytes.
1536 unsigned char jmp
[5];
1538 elfcpp::Swap_unaligned
<64, false>::writeval(jmp
+ 1, length
- 5);
1539 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1540 + std::string(length
- 5, '\0'));
1543 // Nop sequences of various lengths.
1544 const char nop1
[1] = { 0x90 }; // nop
1545 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1546 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1547 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1548 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1549 0x00 }; // leal 0(%esi,1),%esi
1550 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1552 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1554 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1555 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1556 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1557 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1559 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1560 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1562 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1563 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1565 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1566 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1567 0x00, 0x00, 0x00, 0x00 };
1568 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1569 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1570 0x27, 0x00, 0x00, 0x00,
1572 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1573 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1574 0xbc, 0x27, 0x00, 0x00,
1576 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1577 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1578 0x90, 0x90, 0x90, 0x90,
1581 const char* nops
[16] = {
1583 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
1584 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
1587 return std::string(nops
[length
], length
);
1590 // The selector for x86_64 object files.
1592 class Target_selector_x86_64
: public Target_selector
1595 Target_selector_x86_64()
1596 : Target_selector(elfcpp::EM_X86_64
, 64, false)
1600 recognize(int machine
, int osabi
, int abiversion
);
1603 Target_x86_64
* target_
;
1606 // Recognize an x86_64 object file when we already know that the machine
1607 // number is EM_X86_64.
1610 Target_selector_x86_64::recognize(int, int, int)
1612 if (this->target_
== NULL
)
1613 this->target_
= new Target_x86_64();
1614 return this->target_
;
1617 Target_selector_x86_64 target_selector_x86_64
;
1619 } // End anonymous namespace.