Fix the date of my last changelog entry.
[deliverable/binutils-gdb.git] / gold / x86_64.cc
CommitLineData
2e30d253
ILT
1// x86_64.cc -- x86_64 target support for gold.
2
3// Copyright 2006, 2007, Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
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.
12
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.)
21
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.
26
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
30// 02110-1301, USA.
31
32#include "gold.h"
33
34#include <cstring>
35
36#include "elfcpp.h"
37#include "parameters.h"
38#include "reloc.h"
39#include "x86_64.h"
40#include "object.h"
41#include "symtab.h"
42#include "layout.h"
43#include "output.h"
44#include "target.h"
45#include "target-reloc.h"
46#include "target-select.h"
e041f13d 47#include "tls.h"
2e30d253
ILT
48
49namespace
50{
51
52using namespace gold;
53
54class Output_data_plt_x86_64;
55
56// The x86_64 target class.
d61c17ea
ILT
57// See the ABI at
58// http://www.x86-64.org/documentation/abi.pdf
59// TLS info comes from
60// http://people.redhat.com/drepper/tls.pdf
0ffd9845 61// http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
2e30d253
ILT
62
63class Target_x86_64 : public Sized_target<64, false>
64{
65 public:
e822f2b1
ILT
66 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
67 // uses only Elf64_Rela relocation entries with explicit addends."
2e30d253
ILT
68 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
69
70 Target_x86_64()
71 : Sized_target<64, false>(&x86_64_info),
0ffd9845 72 got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL),
2e30d253
ILT
73 copy_relocs_(NULL), dynbss_(NULL)
74 { }
75
76 // Scan the relocations to look for symbol adjustments.
77 void
78 scan_relocs(const General_options& options,
79 Symbol_table* symtab,
80 Layout* layout,
81 Sized_relobj<64, false>* object,
82 unsigned int data_shndx,
83 unsigned int sh_type,
84 const unsigned char* prelocs,
85 size_t reloc_count,
730cdc88
ILT
86 Output_section* output_section,
87 bool needs_special_offset_handling,
2e30d253 88 size_t local_symbol_count,
730cdc88 89 const unsigned char* plocal_symbols);
2e30d253
ILT
90
91 // Finalize the sections.
92 void
93 do_finalize_sections(Layout*);
94
4fb6c25d
ILT
95 // Return the value to use for a dynamic which requires special
96 // treatment.
97 uint64_t
98 do_dynsym_value(const Symbol*) const;
99
2e30d253
ILT
100 // Relocate a section.
101 void
102 relocate_section(const Relocate_info<64, false>*,
103 unsigned int sh_type,
104 const unsigned char* prelocs,
105 size_t reloc_count,
730cdc88
ILT
106 Output_section* output_section,
107 bool needs_special_offset_handling,
2e30d253
ILT
108 unsigned char* view,
109 elfcpp::Elf_types<64>::Elf_Addr view_address,
110 off_t view_size);
111
112 // Return a string used to fill a code section with nops.
113 std::string
114 do_code_fill(off_t length);
115
96f2030e
ILT
116 // Return the size of the GOT section.
117 off_t
118 got_size()
119 {
120 gold_assert(this->got_ != NULL);
121 return this->got_->data_size();
122 }
123
2e30d253
ILT
124 private:
125 // The class which scans relocations.
126 struct Scan
127 {
128 inline void
129 local(const General_options& options, Symbol_table* symtab,
130 Layout* layout, Target_x86_64* target,
131 Sized_relobj<64, false>* object,
132 unsigned int data_shndx,
133 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
134 const elfcpp::Sym<64, false>& lsym);
135
136 inline void
137 global(const General_options& options, Symbol_table* symtab,
138 Layout* layout, Target_x86_64* target,
139 Sized_relobj<64, false>* object,
140 unsigned int data_shndx,
141 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
142 Symbol* gsym);
e041f13d
ILT
143
144 static void
145 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
146
147 static void
148 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
149 Symbol*);
2e30d253
ILT
150 };
151
152 // The class which implements relocation.
153 class Relocate
154 {
155 public:
156 Relocate()
157 : skip_call_tls_get_addr_(false)
158 { }
159
160 ~Relocate()
161 {
162 if (this->skip_call_tls_get_addr_)
163 {
164 // FIXME: This needs to specify the location somehow.
a0c4fb0a 165 gold_error(_("missing expected TLS relocation"));
2e30d253
ILT
166 }
167 }
168
169 // Do a relocation. Return false if the caller should not issue
170 // any warnings about this relocation.
171 inline bool
172 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
173 const elfcpp::Rela<64, false>&,
174 unsigned int r_type, const Sized_symbol<64>*,
175 const Symbol_value<64>*,
176 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
177 off_t);
178
179 private:
180 // Do a TLS relocation.
181 inline void
182 relocate_tls(const Relocate_info<64, false>*, size_t relnum,
183 const elfcpp::Rela<64, false>&,
184 unsigned int r_type, const Sized_symbol<64>*,
185 const Symbol_value<64>*,
186 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, off_t);
187
56622147
ILT
188 // Do a TLS General-Dynamic to Local-Exec transition.
189 inline void
190 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
2e30d253
ILT
191 Output_segment* tls_segment,
192 const elfcpp::Rela<64, false>&, unsigned int r_type,
193 elfcpp::Elf_types<64>::Elf_Addr value,
194 unsigned char* view,
195 off_t view_size);
196
56622147 197 // Do a TLS Local-Dynamic to Local-Exec transition.
2e30d253 198 inline void
56622147 199 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
2e30d253
ILT
200 Output_segment* tls_segment,
201 const elfcpp::Rela<64, false>&, unsigned int r_type,
202 elfcpp::Elf_types<64>::Elf_Addr value,
203 unsigned char* view,
204 off_t view_size);
205
56622147
ILT
206 // Do a TLS Initial-Exec to Local-Exec transition.
207 static inline void
208 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
72ec2876
ILT
209 Output_segment* tls_segment,
210 const elfcpp::Rela<64, false>&, unsigned int r_type,
211 elfcpp::Elf_types<64>::Elf_Addr value,
212 unsigned char* view,
213 off_t view_size);
2e30d253
ILT
214
215 // This is set if we should skip the next reloc, which should be a
216 // PLT32 reloc against ___tls_get_addr.
217 bool skip_call_tls_get_addr_;
218 };
219
220 // Adjust TLS relocation type based on the options and whether this
221 // is a local symbol.
e041f13d 222 static tls::Tls_optimization
2e30d253
ILT
223 optimize_tls_reloc(bool is_final, int r_type);
224
225 // Get the GOT section, creating it if necessary.
226 Output_data_got<64, false>*
227 got_section(Symbol_table*, Layout*);
228
96f2030e
ILT
229 // Get the GOT PLT section.
230 Output_data_space*
231 got_plt_section() const
232 {
233 gold_assert(this->got_plt_ != NULL);
234 return this->got_plt_;
235 }
236
2e30d253
ILT
237 // Create a PLT entry for a global symbol.
238 void
239 make_plt_entry(Symbol_table*, Layout*, Symbol*);
240
241 // Get the PLT section.
242 Output_data_plt_x86_64*
243 plt_section() const
244 {
245 gold_assert(this->plt_ != NULL);
246 return this->plt_;
247 }
248
249 // Get the dynamic reloc section, creating it if necessary.
250 Reloc_section*
0ffd9845 251 rela_dyn_section(Layout*);
2e30d253
ILT
252
253 // Copy a relocation against a global symbol.
254 void
255 copy_reloc(const General_options*, Symbol_table*, Layout*,
256 Sized_relobj<64, false>*, unsigned int,
257 Symbol*, const elfcpp::Rela<64, false>&);
258
259 // Information about this specific target which we pass to the
260 // general Target structure.
261 static const Target::Target_info x86_64_info;
262
263 // The GOT section.
264 Output_data_got<64, false>* got_;
265 // The PLT section.
266 Output_data_plt_x86_64* plt_;
267 // The GOT PLT section.
268 Output_data_space* got_plt_;
269 // The dynamic reloc section.
0ffd9845 270 Reloc_section* rela_dyn_;
2e30d253
ILT
271 // Relocs saved to avoid a COPY reloc.
272 Copy_relocs<64, false>* copy_relocs_;
273 // Space for variables copied with a COPY reloc.
274 Output_data_space* dynbss_;
275};
276
277const Target::Target_info Target_x86_64::x86_64_info =
278{
279 64, // size
280 false, // is_big_endian
281 elfcpp::EM_X86_64, // machine_code
282 false, // has_make_symbol
283 false, // has_resolve
284 true, // has_code_fill
35cdfc9a 285 true, // is_default_stack_executable
2e30d253 286 "/lib/ld64.so.1", // program interpreter
0c5e9c22 287 0x400000, // default_text_segment_address
2e30d253
ILT
288 0x1000, // abi_pagesize
289 0x1000 // common_pagesize
290};
291
292// Get the GOT section, creating it if necessary.
293
294Output_data_got<64, false>*
295Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
296{
297 if (this->got_ == NULL)
298 {
299 gold_assert(symtab != NULL && layout != NULL);
300
301 this->got_ = new Output_data_got<64, false>();
302
303 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
304 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
305 this->got_);
306
307 // The old GNU linker creates a .got.plt section. We just
308 // create another set of data in the .got section. Note that we
309 // always create a PLT if we create a GOT, although the PLT
310 // might be empty.
2e30d253
ILT
311 this->got_plt_ = new Output_data_space(8);
312 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
313 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
314 this->got_plt_);
315
316 // The first three entries are reserved.
317 this->got_plt_->set_space_size(3 * 8);
318
319 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
320 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
321 this->got_plt_,
322 0, 0, elfcpp::STT_OBJECT,
323 elfcpp::STB_LOCAL,
324 elfcpp::STV_HIDDEN, 0,
325 false, false);
326 }
327
328 return this->got_;
329}
330
331// Get the dynamic reloc section, creating it if necessary.
332
333Target_x86_64::Reloc_section*
0ffd9845 334Target_x86_64::rela_dyn_section(Layout* layout)
2e30d253 335{
0ffd9845 336 if (this->rela_dyn_ == NULL)
2e30d253
ILT
337 {
338 gold_assert(layout != NULL);
0ffd9845 339 this->rela_dyn_ = new Reloc_section();
2e30d253 340 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
0ffd9845 341 elfcpp::SHF_ALLOC, this->rela_dyn_);
2e30d253 342 }
0ffd9845 343 return this->rela_dyn_;
2e30d253
ILT
344}
345
346// A class to handle the PLT data.
347
348class Output_data_plt_x86_64 : public Output_section_data
349{
350 public:
351 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
352
353 Output_data_plt_x86_64(Layout*, Output_data_space*);
354
355 // Add an entry to the PLT.
356 void
357 add_entry(Symbol* gsym);
358
359 // Return the .rel.plt section data.
360 const Reloc_section*
361 rel_plt() const
362 { return this->rel_; }
363
364 protected:
365 void
366 do_adjust_output_section(Output_section* os);
367
368 private:
369 // The size of an entry in the PLT.
370 static const int plt_entry_size = 16;
371
372 // The first entry in the PLT.
373 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
374 // procedure linkage table for both programs and shared objects."
375 static unsigned char first_plt_entry[plt_entry_size];
376
377 // Other entries in the PLT for an executable.
378 static unsigned char plt_entry[plt_entry_size];
379
380 // Set the final size.
381 void
382 do_set_address(uint64_t, off_t)
383 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
384
385 // Write out the PLT data.
386 void
387 do_write(Output_file*);
388
389 // The reloc section.
390 Reloc_section* rel_;
391 // The .got.plt section.
392 Output_data_space* got_plt_;
393 // The number of PLT entries.
394 unsigned int count_;
395};
396
397// Create the PLT section. The ordinary .got section is an argument,
398// since we need to refer to the start. We also create our own .got
399// section just for PLT entries.
400
401Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
402 Output_data_space* got_plt)
2e30d253
ILT
403 : Output_section_data(8), got_plt_(got_plt), count_(0)
404{
405 this->rel_ = new Reloc_section();
406 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
407 elfcpp::SHF_ALLOC, this->rel_);
408}
409
410void
411Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
412{
413 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
414 // linker, and so do we.
415 os->set_entsize(4);
416}
417
418// Add an entry to the PLT.
419
420void
421Output_data_plt_x86_64::add_entry(Symbol* gsym)
422{
423 gold_assert(!gsym->has_plt_offset());
424
425 // Note that when setting the PLT offset we skip the initial
426 // reserved PLT entry.
427 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
428
429 ++this->count_;
430
431 off_t got_offset = this->got_plt_->data_size();
432
433 // Every PLT entry needs a GOT entry which points back to the PLT
434 // entry (this will be changed by the dynamic linker, normally
435 // lazily when the function is called).
436 this->got_plt_->set_space_size(got_offset + 8);
437
438 // Every PLT entry needs a reloc.
439 gsym->set_needs_dynsym_entry();
440 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
441 got_offset, 0);
442
443 // Note that we don't need to save the symbol. The contents of the
444 // PLT are independent of which symbols are used. The symbols only
445 // appear in the relocations.
446}
447
448// The first entry in the PLT for an executable.
449
450unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
451{
452 // From AMD64 ABI Draft 0.98, page 76
453 0xff, 0x35, // pushq contents of memory address
454 0, 0, 0, 0, // replaced with address of .got + 4
455 0xff, 0x25, // jmp indirect
456 0, 0, 0, 0, // replaced with address of .got + 8
457 0x90, 0x90, 0x90, 0x90 // noop (x4)
458};
459
460// Subsequent entries in the PLT for an executable.
461
462unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
463{
464 // From AMD64 ABI Draft 0.98, page 76
465 0xff, 0x25, // jmpq indirect
466 0, 0, 0, 0, // replaced with address of symbol in .got
467 0x68, // pushq immediate
468 0, 0, 0, 0, // replaced with offset into relocation table
469 0xe9, // jmpq relative
470 0, 0, 0, 0 // replaced with offset to start of .plt
471};
472
473// Write out the PLT. This uses the hand-coded instructions above,
474// and adjusts them as needed. This is specified by the AMD64 ABI.
475
476void
477Output_data_plt_x86_64::do_write(Output_file* of)
478{
479 const off_t offset = this->offset();
480 const off_t oview_size = this->data_size();
481 unsigned char* const oview = of->get_output_view(offset, oview_size);
482
483 const off_t got_file_offset = this->got_plt_->offset();
484 const off_t got_size = this->got_plt_->data_size();
485 unsigned char* const got_view = of->get_output_view(got_file_offset,
486 got_size);
487
488 unsigned char* pov = oview;
489
490 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
491 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
492
493 memcpy(pov, first_plt_entry, plt_entry_size);
494 if (!parameters->output_is_shared())
495 {
496 // We do a jmp relative to the PC at the end of this instruction.
497 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
498 - (plt_address + 6));
499 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
500 - (plt_address + 12));
501 }
502 pov += plt_entry_size;
503
504 unsigned char* got_pov = got_view;
505
506 memset(got_pov, 0, 24);
507 got_pov += 24;
508
509 unsigned int plt_offset = plt_entry_size;
510 unsigned int got_offset = 24;
511 const unsigned int count = this->count_;
512 for (unsigned int plt_index = 0;
513 plt_index < count;
514 ++plt_index,
515 pov += plt_entry_size,
516 got_pov += 8,
517 plt_offset += plt_entry_size,
518 got_offset += 8)
519 {
520 // Set and adjust the PLT entry itself.
521 memcpy(pov, plt_entry, plt_entry_size);
522 if (parameters->output_is_shared())
523 // FIXME(csilvers): what's the right thing to write here?
524 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
525 else
526 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
527 (got_address + got_offset
528 - (plt_address + plt_offset
529 + 6)));
530
531 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
532 elfcpp::Swap<32, false>::writeval(pov + 12,
533 - (plt_offset + plt_entry_size));
534
535 // Set the entry in the GOT.
536 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
537 }
538
539 gold_assert(pov - oview == oview_size);
540 gold_assert(got_pov - got_view == got_size);
541
542 of->write_output_view(offset, oview_size, oview);
543 of->write_output_view(got_file_offset, got_size, got_view);
544}
545
546// Create a PLT entry for a global symbol.
547
548void
549Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
550 Symbol* gsym)
551{
552 if (gsym->has_plt_offset())
553 return;
554
555 if (this->plt_ == NULL)
556 {
557 // Create the GOT sections first.
558 this->got_section(symtab, layout);
559
560 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
561 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
562 (elfcpp::SHF_ALLOC
563 | elfcpp::SHF_EXECINSTR),
564 this->plt_);
565 }
566
567 this->plt_->add_entry(gsym);
568}
569
570// Handle a relocation against a non-function symbol defined in a
571// dynamic object. The traditional way to handle this is to generate
572// a COPY relocation to copy the variable at runtime from the shared
573// object into the executable's data segment. However, this is
574// undesirable in general, as if the size of the object changes in the
575// dynamic object, the executable will no longer work correctly. If
576// this relocation is in a writable section, then we can create a
577// dynamic reloc and the dynamic linker will resolve it to the correct
578// address at runtime. However, we do not want do that if the
579// relocation is in a read-only section, as it would prevent the
580// readonly segment from being shared. And if we have to eventually
581// generate a COPY reloc, then any dynamic relocations will be
582// useless. So this means that if this is a writable section, we need
583// to save the relocation until we see whether we have to create a
584// COPY relocation for this symbol for any other relocation.
585
586void
587Target_x86_64::copy_reloc(const General_options* options,
d61c17ea
ILT
588 Symbol_table* symtab,
589 Layout* layout,
590 Sized_relobj<64, false>* object,
591 unsigned int data_shndx, Symbol* gsym,
72ec2876 592 const elfcpp::Rela<64, false>& rela)
2e30d253
ILT
593{
594 Sized_symbol<64>* ssym;
595 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
596 SELECT_SIZE(64));
597
598 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
599 data_shndx, ssym))
600 {
601 // So far we do not need a COPY reloc. Save this relocation.
602 // If it turns out that we never need a COPY reloc for this
603 // symbol, then we will emit the relocation.
604 if (this->copy_relocs_ == NULL)
605 this->copy_relocs_ = new Copy_relocs<64, false>();
72ec2876 606 this->copy_relocs_->save(ssym, object, data_shndx, rela);
2e30d253
ILT
607 }
608 else
609 {
610 // Allocate space for this symbol in the .bss section.
611
612 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
613
614 // There is no defined way to determine the required alignment
615 // of the symbol. We pick the alignment based on the size. We
616 // set an arbitrary maximum of 256.
617 unsigned int align;
618 for (align = 1; align < 512; align <<= 1)
619 if ((symsize & align) != 0)
620 break;
621
622 if (this->dynbss_ == NULL)
623 {
624 this->dynbss_ = new Output_data_space(align);
625 layout->add_output_section_data(".bss",
626 elfcpp::SHT_NOBITS,
627 (elfcpp::SHF_ALLOC
628 | elfcpp::SHF_WRITE),
629 this->dynbss_);
630 }
631
632 Output_data_space* dynbss = this->dynbss_;
633
634 if (align > dynbss->addralign())
635 dynbss->set_space_alignment(align);
636
637 off_t dynbss_size = dynbss->data_size();
638 dynbss_size = align_address(dynbss_size, align);
639 off_t offset = dynbss_size;
640 dynbss->set_space_size(dynbss_size + symsize);
641
46fe1623 642 symtab->define_with_copy_reloc(this, ssym, dynbss, offset);
2e30d253
ILT
643
644 // Add the COPY reloc.
0ffd9845
ILT
645 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
646 rela_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0);
2e30d253
ILT
647 }
648}
649
650
651// Optimize the TLS relocation type based on what we know about the
652// symbol. IS_FINAL is true if the final address of this symbol is
653// known at link time.
654
e041f13d 655tls::Tls_optimization
2e30d253
ILT
656Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
657{
2e30d253
ILT
658 // If we are generating a shared library, then we can't do anything
659 // in the linker.
660 if (parameters->output_is_shared())
e041f13d 661 return tls::TLSOPT_NONE;
2e30d253
ILT
662
663 switch (r_type)
664 {
665 case elfcpp::R_X86_64_TLSGD:
e041f13d
ILT
666 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
667 case elfcpp::R_X86_64_TLSDESC_CALL:
668 // These are General-Dynamic which permits fully general TLS
2e30d253
ILT
669 // access. Since we know that we are generating an executable,
670 // we can convert this to Initial-Exec. If we also know that
671 // this is a local symbol, we can further switch to Local-Exec.
672 if (is_final)
e041f13d
ILT
673 return tls::TLSOPT_TO_LE;
674 return tls::TLSOPT_TO_IE;
2e30d253 675
d61c17ea 676 case elfcpp::R_X86_64_TLSLD:
2e30d253
ILT
677 // This is Local-Dynamic, which refers to a local symbol in the
678 // dynamic TLS block. Since we know that we generating an
679 // executable, we can switch to Local-Exec.
e041f13d 680 return tls::TLSOPT_TO_LE;
2e30d253 681
0ffd9845 682 case elfcpp::R_X86_64_DTPOFF32:
0ffd9845
ILT
683 case elfcpp::R_X86_64_DTPOFF64:
684 // Another Local-Dynamic reloc.
e041f13d 685 return tls::TLSOPT_TO_LE;
0ffd9845 686
d61c17ea 687 case elfcpp::R_X86_64_GOTTPOFF:
2e30d253
ILT
688 // These are Initial-Exec relocs which get the thread offset
689 // from the GOT. If we know that we are linking against the
690 // local symbol, we can switch to Local-Exec, which links the
691 // thread offset into the instruction.
692 if (is_final)
e041f13d
ILT
693 return tls::TLSOPT_TO_LE;
694 return tls::TLSOPT_NONE;
2e30d253 695
d61c17ea 696 case elfcpp::R_X86_64_TPOFF32:
2e30d253
ILT
697 // When we already have Local-Exec, there is nothing further we
698 // can do.
e041f13d 699 return tls::TLSOPT_NONE;
2e30d253
ILT
700
701 default:
702 gold_unreachable();
703 }
2e30d253
ILT
704}
705
e041f13d
ILT
706// Report an unsupported relocation against a local symbol.
707
708void
709Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
710 unsigned int r_type)
711{
75f2446e
ILT
712 gold_error(_("%s: unsupported reloc %u against local symbol"),
713 object->name().c_str(), r_type);
e041f13d
ILT
714}
715
2e30d253
ILT
716// Scan a relocation for a local symbol.
717
718inline void
719Target_x86_64::Scan::local(const General_options&,
d61c17ea
ILT
720 Symbol_table* symtab,
721 Layout* layout,
722 Target_x86_64* target,
723 Sized_relobj<64, false>* object,
0ffd9845
ILT
724 unsigned int data_shndx,
725 const elfcpp::Rela<64, false>& reloc,
d61c17ea
ILT
726 unsigned int r_type,
727 const elfcpp::Sym<64, false>&)
2e30d253
ILT
728{
729 switch (r_type)
730 {
731 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
732 case elfcpp::R_386_GNU_VTINHERIT:
733 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
734 break;
735
736 case elfcpp::R_X86_64_64:
737 case elfcpp::R_X86_64_32:
738 case elfcpp::R_X86_64_32S:
739 case elfcpp::R_X86_64_16:
740 case elfcpp::R_X86_64_8:
96f2030e
ILT
741 // If building a shared library (or a position-independent
742 // executable), we need to create a dynamic relocation for
743 // this location. The relocation applied at link time will
744 // apply the link-time value, so we flag the location with
745 // an R_386_RELATIVE relocation so the dynamic loader can
746 // relocate it easily.
747 if (parameters->output_is_position_independent())
748 {
96f2030e 749 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
86849f1f
ILT
750 if (r_type == elfcpp::R_X86_64_64)
751 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_RELATIVE,
752 data_shndx, reloc.get_r_offset(), 0);
753 else
754 {
755 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
756 rela_dyn->add_local(object, r_sym, r_type, data_shndx,
757 reloc.get_r_offset(),
758 reloc.get_r_addend());
759 }
96f2030e 760 }
2e30d253
ILT
761 break;
762
763 case elfcpp::R_X86_64_PC64:
764 case elfcpp::R_X86_64_PC32:
765 case elfcpp::R_X86_64_PC16:
766 case elfcpp::R_X86_64_PC8:
767 break;
768
f389a824
ILT
769 case elfcpp::R_X86_64_PLT32:
770 // Since we know this is a local symbol, we can handle this as a
771 // PC32 reloc.
772 break;
773
fdc2f80f 774 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 775 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
776 case elfcpp::R_X86_64_GOTPC64:
777 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
778 // We need a GOT section.
779 target->got_section(symtab, layout);
ee9e9e86
ILT
780 // For PLTOFF64, we'd normally want a PLT section, but since we
781 // know this is a local symbol, no PLT is needed.
2e30d253
ILT
782 break;
783
0ffd9845
ILT
784 case elfcpp::R_X86_64_GOT64:
785 case elfcpp::R_X86_64_GOT32:
786 case elfcpp::R_X86_64_GOTPCREL64:
787 case elfcpp::R_X86_64_GOTPCREL:
ee9e9e86 788 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
789 {
790 // The symbol requires a GOT entry.
791 Output_data_got<64, false>* got = target->got_section(symtab, layout);
792 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
793 if (got->add_local(object, r_sym))
794 {
795 // If we are generating a shared object, we need to add a
796 // dynamic RELATIVE relocation for this symbol.
96f2030e 797 if (parameters->output_is_position_independent())
0ffd9845 798 {
96f2030e
ILT
799 // FIXME: R_X86_64_RELATIVE assumes a 64-bit relocation.
800 gold_assert(r_type != elfcpp::R_X86_64_GOT32);
801
0ffd9845
ILT
802 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
803 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_RELATIVE,
804 data_shndx, reloc.get_r_offset(), 0);
805 }
806 }
ee9e9e86
ILT
807 // For GOTPLT64, we'd normally want a PLT section, but since
808 // we know this is a local symbol, no PLT is needed.
0ffd9845
ILT
809 }
810 break;
811
2e30d253
ILT
812 case elfcpp::R_X86_64_COPY:
813 case elfcpp::R_X86_64_GLOB_DAT:
814 case elfcpp::R_X86_64_JUMP_SLOT:
815 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 816 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 817 case elfcpp::R_X86_64_TPOFF64:
2e30d253 818 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 819 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
820 gold_error(_("%s: unexpected reloc %u in object file"),
821 object->name().c_str(), r_type);
2e30d253
ILT
822 break;
823
d61c17ea 824 // These are initial tls relocs, which are expected when linking
56622147
ILT
825 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
826 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 827 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 828 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
829 case elfcpp::R_X86_64_DTPOFF32:
830 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
831 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
832 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253
ILT
833 {
834 bool output_is_shared = parameters->output_is_shared();
e041f13d
ILT
835 const tls::Tls_optimization optimized_type
836 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
2e30d253
ILT
837 switch (r_type)
838 {
56622147
ILT
839 case elfcpp::R_X86_64_TLSGD: // General-dynamic
840 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
841 case elfcpp::R_X86_64_TLSDESC_CALL:
842 // FIXME: If not relaxing to LE, we need to generate
843 // DTPMOD64 and DTPOFF64 relocs.
844 if (optimized_type != tls::TLSOPT_TO_LE)
845 unsupported_reloc_local(object, r_type);
2e30d253
ILT
846 break;
847
e041f13d 848 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
849 case elfcpp::R_X86_64_DTPOFF32:
850 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
851 // FIXME: If not relaxing to LE, we need to generate a
852 // DTPMOD64 reloc.
853 if (optimized_type != tls::TLSOPT_TO_LE)
854 unsupported_reloc_local(object, r_type);
855 break;
856
56622147
ILT
857 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
858 // FIXME: If not relaxing to LE, we need to generate a
859 // TPOFF64 reloc.
860 if (optimized_type != tls::TLSOPT_TO_LE)
861 unsupported_reloc_local(object, r_type);
862 break;
0ffd9845 863
56622147
ILT
864 case elfcpp::R_X86_64_TPOFF32: // Local-exec
865 // FIXME: If generating a shared object, we need to copy
866 // this relocation into the object.
867 gold_assert(!output_is_shared);
2e30d253 868 break;
e041f13d
ILT
869
870 default:
871 gold_unreachable();
2e30d253
ILT
872 }
873 }
874 break;
2e30d253 875
fdc2f80f
ILT
876 case elfcpp::R_X86_64_SIZE32:
877 case elfcpp::R_X86_64_SIZE64:
2e30d253 878 default:
75f2446e
ILT
879 gold_error(_("%s: unsupported reloc %u against local symbol"),
880 object->name().c_str(), r_type);
2e30d253
ILT
881 break;
882 }
883}
884
885
e041f13d
ILT
886// Report an unsupported relocation against a global symbol.
887
888void
889Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
890 unsigned int r_type,
891 Symbol* gsym)
892{
75f2446e
ILT
893 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
894 object->name().c_str(), r_type, gsym->name());
e041f13d
ILT
895}
896
2e30d253
ILT
897// Scan a relocation for a global symbol.
898
899inline void
900Target_x86_64::Scan::global(const General_options& options,
d61c17ea
ILT
901 Symbol_table* symtab,
902 Layout* layout,
903 Target_x86_64* target,
904 Sized_relobj<64, false>* object,
905 unsigned int data_shndx,
906 const elfcpp::Rela<64, false>& reloc,
907 unsigned int r_type,
908 Symbol* gsym)
2e30d253
ILT
909{
910 switch (r_type)
911 {
912 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
913 case elfcpp::R_386_GNU_VTINHERIT:
914 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
915 break;
916
917 case elfcpp::R_X86_64_64:
918 case elfcpp::R_X86_64_PC64:
919 case elfcpp::R_X86_64_32:
920 case elfcpp::R_X86_64_32S:
921 case elfcpp::R_X86_64_PC32:
922 case elfcpp::R_X86_64_16:
923 case elfcpp::R_X86_64_PC16:
924 case elfcpp::R_X86_64_8:
925 case elfcpp::R_X86_64_PC8:
96f2030e
ILT
926 {
927 bool is_pcrel = (r_type == elfcpp::R_X86_64_PC64
928 || r_type == elfcpp::R_X86_64_PC32
929 || r_type == elfcpp::R_X86_64_PC16
930 || r_type == elfcpp::R_X86_64_PC8);
931
932 if (gsym->is_from_dynobj()
933 || (parameters->output_is_shared()
934 && gsym->is_preemptible()))
935 {
936 // (a) This symbol is defined in a dynamic object. If it is a
937 // function, we make a PLT entry. Otherwise we need to
938 // either generate a COPY reloc or copy this reloc.
939 // (b) We are building a shared object and this symbol is
940 // preemptible. If it is a function, we make a PLT entry.
941 // Otherwise, we copy the reloc.
942 if (gsym->type() == elfcpp::STT_FUNC)
943 {
944 target->make_plt_entry(symtab, layout, gsym);
945
946 // If this is not a PC relative reference, then we may
947 // be taking the address of the function. In that case
948 // we need to set the entry in the dynamic symbol table
949 // to the address of the PLT entry. We will also need to
950 // create a dynamic relocation.
951 if (!is_pcrel)
952 {
953 if (gsym->is_from_dynobj())
954 gsym->set_needs_dynsym_value();
955 if (parameters->output_is_position_independent())
956 {
96f2030e
ILT
957 Reloc_section* rela_dyn =
958 target->rela_dyn_section(layout);
86849f1f
ILT
959 rela_dyn->add_global(gsym, r_type, object, data_shndx,
960 reloc.get_r_offset(),
961 reloc.get_r_addend());
96f2030e
ILT
962 }
963 }
964 }
965 else if (parameters->output_is_shared())
966 {
967 // We do not make COPY relocs in shared objects.
968 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
969 rela_dyn->add_global(gsym, r_type, object, data_shndx,
970 reloc.get_r_offset(),
971 reloc.get_r_addend());
972 }
973 else
974 target->copy_reloc(&options, symtab, layout, object, data_shndx,
975 gsym, reloc);
976 }
86849f1f
ILT
977 else if (!is_pcrel && parameters->output_is_position_independent())
978 {
979 // This is not a PC-relative reference, so we need to generate
980 // a dynamic relocation. At this point, we know the symbol
981 // is not preemptible, so we can use the RELATIVE relocation.
982 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
983 if (r_type == elfcpp::R_X86_64_64)
984 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_RELATIVE,
985 data_shndx,
986 reloc.get_r_offset(), 0);
987 else
988 rela_dyn->add_global(gsym, r_type, object, data_shndx,
989 reloc.get_r_offset(),
990 reloc.get_r_addend());
991 }
2e30d253 992 }
2e30d253
ILT
993 break;
994
ff006520 995 case elfcpp::R_X86_64_GOT64:
2e30d253 996 case elfcpp::R_X86_64_GOT32:
ff006520
ILT
997 case elfcpp::R_X86_64_GOTPCREL64:
998 case elfcpp::R_X86_64_GOTPCREL:
999 case elfcpp::R_X86_64_GOTPLT64:
2e30d253
ILT
1000 {
1001 // The symbol requires a GOT entry.
1002 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1003 if (got->add_global(gsym))
1004 {
1005 // If this symbol is not fully resolved, we need to add a
1006 // dynamic relocation for it.
1007 if (!gsym->final_value_is_known())
1008 {
0ffd9845
ILT
1009 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1010 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, got,
1011 gsym->got_offset(), 0);
2e30d253
ILT
1012 }
1013 }
ee9e9e86
ILT
1014 // For GOTPLT64, we also need a PLT entry (but only if the
1015 // symbol is not fully resolved).
1016 if (r_type == elfcpp::R_X86_64_GOTPLT64
1017 && !gsym->final_value_is_known())
1018 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1019 }
1020 break;
1021
1022 case elfcpp::R_X86_64_PLT32:
1023 // If the symbol is fully resolved, this is just a PC32 reloc.
1024 // Otherwise we need a PLT entry.
1025 if (gsym->final_value_is_known())
1026 break;
96f2030e
ILT
1027 // If building a shared library, we can also skip the PLT entry
1028 // if the symbol is defined in the output file and is protected
1029 // or hidden.
1030 if (gsym->is_defined()
1031 && !gsym->is_from_dynobj()
1032 && !gsym->is_preemptible())
1033 break;
2e30d253
ILT
1034 target->make_plt_entry(symtab, layout, gsym);
1035 break;
1036
fdc2f80f 1037 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1038 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1039 case elfcpp::R_X86_64_GOTPC64:
1040 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1041 // We need a GOT section.
1042 target->got_section(symtab, layout);
ee9e9e86
ILT
1043 // For PLTOFF64, we also need a PLT entry (but only if the
1044 // symbol is not fully resolved).
1045 if (r_type == elfcpp::R_X86_64_PLTOFF64
1046 && !gsym->final_value_is_known())
1047 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1048 break;
1049
2e30d253
ILT
1050 case elfcpp::R_X86_64_COPY:
1051 case elfcpp::R_X86_64_GLOB_DAT:
1052 case elfcpp::R_X86_64_JUMP_SLOT:
1053 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1054 // These are outstanding tls relocs, which are unexpected when linking
e822f2b1 1055 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1056 case elfcpp::R_X86_64_DTPMOD64:
e822f2b1 1057 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1058 gold_error(_("%s: unexpected reloc %u in object file"),
1059 object->name().c_str(), r_type);
2e30d253 1060 break;
2e30d253 1061
d61c17ea 1062 // These are initial tls relocs, which are expected for global()
56622147
ILT
1063 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1064 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1065 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1066 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1067 case elfcpp::R_X86_64_DTPOFF32:
1068 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1069 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1070 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253
ILT
1071 {
1072 const bool is_final = gsym->final_value_is_known();
e041f13d
ILT
1073 const tls::Tls_optimization optimized_type
1074 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
1075 switch (r_type)
1076 {
56622147
ILT
1077 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1078 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1079 case elfcpp::R_X86_64_TLSDESC_CALL:
1080 // FIXME: If not relaxing to LE, we need to generate
1081 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1082 if (optimized_type != tls::TLSOPT_TO_LE)
1083 unsupported_reloc_global(object, r_type, gsym);
2e30d253
ILT
1084 break;
1085
e041f13d 1086 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1087 case elfcpp::R_X86_64_DTPOFF32:
1088 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
1089 // FIXME: If not relaxing to LE, we need to generate a
1090 // DTPMOD64 reloc.
1091 if (optimized_type != tls::TLSOPT_TO_LE)
1092 unsupported_reloc_global(object, r_type, gsym);
1093 break;
1094
56622147
ILT
1095 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1096 // FIXME: If not relaxing to LE, we need to generate a
1097 // TPOFF64 reloc.
1098 if (optimized_type != tls::TLSOPT_TO_LE)
1099 unsupported_reloc_global(object, r_type, gsym);
1100 break;
0ffd9845 1101
56622147
ILT
1102 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1103 // FIXME: If generating a shared object, we need to copy
1104 // this relocation into the object.
1105 gold_assert(is_final);
2e30d253 1106 break;
e041f13d
ILT
1107
1108 default:
1109 gold_unreachable();
2e30d253
ILT
1110 }
1111 }
1112 break;
fdc2f80f
ILT
1113
1114 case elfcpp::R_X86_64_SIZE32:
1115 case elfcpp::R_X86_64_SIZE64:
2e30d253 1116 default:
75f2446e
ILT
1117 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1118 object->name().c_str(), r_type, gsym->name());
2e30d253
ILT
1119 break;
1120 }
1121}
1122
1123// Scan relocations for a section.
1124
1125void
1126Target_x86_64::scan_relocs(const General_options& options,
d61c17ea
ILT
1127 Symbol_table* symtab,
1128 Layout* layout,
1129 Sized_relobj<64, false>* object,
1130 unsigned int data_shndx,
1131 unsigned int sh_type,
1132 const unsigned char* prelocs,
1133 size_t reloc_count,
730cdc88
ILT
1134 Output_section* output_section,
1135 bool needs_special_offset_handling,
d61c17ea 1136 size_t local_symbol_count,
730cdc88 1137 const unsigned char* plocal_symbols)
2e30d253
ILT
1138{
1139 if (sh_type == elfcpp::SHT_REL)
1140 {
75f2446e
ILT
1141 gold_error(_("%s: unsupported REL reloc section"),
1142 object->name().c_str());
1143 return;
2e30d253
ILT
1144 }
1145
1146 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1147 Target_x86_64::Scan>(
1148 options,
1149 symtab,
1150 layout,
1151 this,
1152 object,
1153 data_shndx,
1154 prelocs,
1155 reloc_count,
730cdc88
ILT
1156 output_section,
1157 needs_special_offset_handling,
2e30d253 1158 local_symbol_count,
730cdc88 1159 plocal_symbols);
2e30d253
ILT
1160}
1161
1162// Finalize the sections.
1163
1164void
1165Target_x86_64::do_finalize_sections(Layout* layout)
1166{
1167 // Fill in some more dynamic tags.
1168 Output_data_dynamic* const odyn = layout->dynamic_data();
1169 if (odyn != NULL)
1170 {
1171 if (this->got_plt_ != NULL)
1172 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1173
1174 if (this->plt_ != NULL)
1175 {
1176 const Output_data* od = this->plt_->rel_plt();
1177 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1178 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1179 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1180 }
1181
0ffd9845 1182 if (this->rela_dyn_ != NULL)
2e30d253 1183 {
0ffd9845 1184 const Output_data* od = this->rela_dyn_;
2e30d253 1185 odyn->add_section_address(elfcpp::DT_RELA, od);
e84992bb 1186 odyn->add_section_size(elfcpp::DT_RELASZ, od);
2e30d253 1187 odyn->add_constant(elfcpp::DT_RELAENT,
e84992bb 1188 elfcpp::Elf_sizes<64>::rela_size);
2e30d253
ILT
1189 }
1190
1191 if (!parameters->output_is_shared())
1192 {
1193 // The value of the DT_DEBUG tag is filled in by the dynamic
1194 // linker at run time, and used by the debugger.
1195 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1196 }
1197 }
1198
1199 // Emit any relocs we saved in an attempt to avoid generating COPY
1200 // relocs.
1201 if (this->copy_relocs_ == NULL)
1202 return;
1203 if (this->copy_relocs_->any_to_emit())
1204 {
0ffd9845
ILT
1205 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1206 this->copy_relocs_->emit(rela_dyn);
2e30d253
ILT
1207 }
1208 delete this->copy_relocs_;
1209 this->copy_relocs_ = NULL;
1210}
1211
1212// Perform a relocation.
1213
1214inline bool
1215Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1216 Target_x86_64* target,
1217 size_t relnum,
0ffd9845 1218 const elfcpp::Rela<64, false>& rela,
2e30d253
ILT
1219 unsigned int r_type,
1220 const Sized_symbol<64>* gsym,
1221 const Symbol_value<64>* psymval,
1222 unsigned char* view,
1223 elfcpp::Elf_types<64>::Elf_Addr address,
1224 off_t view_size)
1225{
1226 if (this->skip_call_tls_get_addr_)
1227 {
1228 if (r_type != elfcpp::R_X86_64_PLT32
1229 || gsym == NULL
0ffd9845 1230 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2e30d253 1231 {
75f2446e
ILT
1232 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1233 _("missing expected TLS relocation"));
1234 }
1235 else
1236 {
1237 this->skip_call_tls_get_addr_ = false;
1238 return false;
2e30d253 1239 }
2e30d253
ILT
1240 }
1241
1242 // Pick the value to use for symbols defined in shared objects.
1243 Symbol_value<64> symval;
96f2030e
ILT
1244 if (gsym != NULL
1245 && (gsym->is_from_dynobj()
1246 || (parameters->output_is_shared()
1247 && gsym->is_preemptible()))
1248 && gsym->has_plt_offset())
2e30d253
ILT
1249 {
1250 symval.set_output_value(target->plt_section()->address()
1251 + gsym->plt_offset());
1252 psymval = &symval;
1253 }
1254
1255 const Sized_relobj<64, false>* object = relinfo->object;
0ffd9845
ILT
1256 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1257
1258 // Get the GOT offset if needed.
96f2030e
ILT
1259 // The GOT pointer points to the end of the GOT section.
1260 // We need to subtract the size of the GOT section to get
1261 // the actual offset to use in the relocation.
0ffd9845
ILT
1262 bool have_got_offset = false;
1263 unsigned int got_offset = 0;
1264 switch (r_type)
1265 {
1266 case elfcpp::R_X86_64_GOT32:
1267 case elfcpp::R_X86_64_GOT64:
1268 case elfcpp::R_X86_64_GOTPLT64:
1269 case elfcpp::R_X86_64_GOTPCREL:
1270 case elfcpp::R_X86_64_GOTPCREL64:
1271 if (gsym != NULL)
1272 {
1273 gold_assert(gsym->has_got_offset());
96f2030e 1274 got_offset = gsym->got_offset() - target->got_size();
0ffd9845
ILT
1275 }
1276 else
1277 {
1278 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
96f2030e 1279 got_offset = object->local_got_offset(r_sym) - target->got_size();
0ffd9845
ILT
1280 }
1281 have_got_offset = true;
1282 break;
1283
1284 default:
1285 break;
1286 }
2e30d253
ILT
1287
1288 switch (r_type)
1289 {
1290 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
1291 case elfcpp::R_386_GNU_VTINHERIT:
1292 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
1293 break;
1294
1295 case elfcpp::R_X86_64_64:
1296 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1297 break;
1298
1299 case elfcpp::R_X86_64_PC64:
1300 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1301 address);
1302 break;
1303
1304 case elfcpp::R_X86_64_32:
7bb3655e
ILT
1305 // FIXME: we need to verify that value + addend fits into 32 bits:
1306 // uint64_t x = value + addend;
1307 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1308 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2e30d253
ILT
1309 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1310 break;
1311
1312 case elfcpp::R_X86_64_32S:
7bb3655e
ILT
1313 // FIXME: we need to verify that value + addend fits into 32 bits:
1314 // int64_t x = value + addend; // note this quantity is signed!
1315 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2e30d253
ILT
1316 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1317 break;
1318
1319 case elfcpp::R_X86_64_PC32:
1320 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1321 address);
1322 break;
1323
1324 case elfcpp::R_X86_64_16:
1325 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1326 break;
1327
1328 case elfcpp::R_X86_64_PC16:
1329 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1330 address);
1331 break;
1332
1333 case elfcpp::R_X86_64_8:
1334 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1335 break;
1336
1337 case elfcpp::R_X86_64_PC8:
1338 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1339 address);
1340 break;
1341
1342 case elfcpp::R_X86_64_PLT32:
f389a824
ILT
1343 gold_assert(gsym == NULL
1344 || gsym->has_plt_offset()
2e30d253 1345 || gsym->final_value_is_known());
ee9e9e86
ILT
1346 // Note: while this code looks the same as for R_X86_64_PC32, it
1347 // behaves differently because psymval was set to point to
1348 // the PLT entry, rather than the symbol, in Scan::global().
2e30d253
ILT
1349 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1350 address);
1351 break;
1352
ee9e9e86
ILT
1353 case elfcpp::R_X86_64_PLTOFF64:
1354 {
1355 gold_assert(gsym);
1356 gold_assert(gsym->has_plt_offset()
1357 || gsym->final_value_is_known());
1358 elfcpp::Elf_types<64>::Elf_Addr got_address;
1359 got_address = target->got_section(NULL, NULL)->address();
c1866bd5
ILT
1360 Relocate_functions<64, false>::rela64(view, object, psymval,
1361 addend - got_address);
ee9e9e86
ILT
1362 }
1363
2e30d253 1364 case elfcpp::R_X86_64_GOT32:
0ffd9845
ILT
1365 gold_assert(have_got_offset);
1366 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2e30d253
ILT
1367 break;
1368
e822f2b1
ILT
1369 case elfcpp::R_X86_64_GOTPC32:
1370 {
1371 gold_assert(gsym);
1372 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1373 value = target->got_plt_section()->address();
e822f2b1
ILT
1374 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1375 }
1376 break;
1377
1378 case elfcpp::R_X86_64_GOT64:
1379 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1380 // Since we always add a PLT entry, this is equivalent.
fdc2f80f 1381 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
1382 gold_assert(have_got_offset);
1383 Relocate_functions<64, false>::rela64(view, got_offset, addend);
e822f2b1
ILT
1384 break;
1385
1386 case elfcpp::R_X86_64_GOTPC64:
1387 {
1388 gold_assert(gsym);
1389 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1390 value = target->got_plt_section()->address();
e822f2b1
ILT
1391 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1392 }
1393 break;
1394
2e30d253
ILT
1395 case elfcpp::R_X86_64_GOTOFF64:
1396 {
1397 elfcpp::Elf_types<64>::Elf_Addr value;
1398 value = (psymval->value(object, 0)
96f2030e 1399 - target->got_plt_section()->address());
2e30d253
ILT
1400 Relocate_functions<64, false>::rela64(view, value, addend);
1401 }
1402 break;
1403
1404 case elfcpp::R_X86_64_GOTPCREL:
1405 {
0ffd9845
ILT
1406 gold_assert(have_got_offset);
1407 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1408 value = target->got_plt_section()->address() + got_offset;
0ffd9845 1409 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2e30d253
ILT
1410 }
1411 break;
1412
e822f2b1
ILT
1413 case elfcpp::R_X86_64_GOTPCREL64:
1414 {
0ffd9845
ILT
1415 gold_assert(have_got_offset);
1416 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1417 value = target->got_plt_section()->address() + got_offset;
0ffd9845 1418 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
e822f2b1
ILT
1419 }
1420 break;
1421
2e30d253
ILT
1422 case elfcpp::R_X86_64_COPY:
1423 case elfcpp::R_X86_64_GLOB_DAT:
1424 case elfcpp::R_X86_64_JUMP_SLOT:
1425 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1426 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 1427 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1428 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 1429 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1430 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1431 _("unexpected reloc %u in object file"),
1432 r_type);
2e30d253
ILT
1433 break;
1434
d61c17ea 1435 // These are initial tls relocs, which are expected when linking
56622147
ILT
1436 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1437 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1438 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1439 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1440 case elfcpp::R_X86_64_DTPOFF32:
1441 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1442 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1443 case elfcpp::R_X86_64_TPOFF32: // Local-exec
0ffd9845 1444 this->relocate_tls(relinfo, relnum, rela, r_type, gsym, psymval, view,
2e30d253
ILT
1445 address, view_size);
1446 break;
2e30d253 1447
fdc2f80f
ILT
1448 case elfcpp::R_X86_64_SIZE32:
1449 case elfcpp::R_X86_64_SIZE64:
2e30d253 1450 default:
75f2446e
ILT
1451 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1452 _("unsupported reloc %u"),
1453 r_type);
2e30d253
ILT
1454 break;
1455 }
1456
1457 return true;
1458}
1459
1460// Perform a TLS relocation.
1461
1462inline void
d61c17ea
ILT
1463Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1464 size_t relnum,
72ec2876 1465 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
1466 unsigned int r_type,
1467 const Sized_symbol<64>* gsym,
1468 const Symbol_value<64>* psymval,
1469 unsigned char* view,
2e30d253 1470 elfcpp::Elf_types<64>::Elf_Addr,
d61c17ea 1471 off_t view_size)
2e30d253 1472{
2e30d253
ILT
1473 Output_segment* tls_segment = relinfo->layout->tls_segment();
1474 if (tls_segment == NULL)
1475 {
72ec2876 1476 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e
ILT
1477 _("TLS reloc but no TLS segment"));
1478 return;
2e30d253
ILT
1479 }
1480
1481 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1482
1483 const bool is_final = (gsym == NULL
96f2030e 1484 ? !parameters->output_is_position_independent()
2e30d253 1485 : gsym->final_value_is_known());
e041f13d
ILT
1486 const tls::Tls_optimization optimized_type
1487 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
1488 switch (r_type)
1489 {
56622147
ILT
1490 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1491 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d
ILT
1492 case elfcpp::R_X86_64_TLSDESC_CALL:
1493 if (optimized_type == tls::TLSOPT_TO_LE)
2e30d253
ILT
1494 {
1495 this->tls_gd_to_le(relinfo, relnum, tls_segment,
72ec2876 1496 rela, r_type, value, view,
2e30d253
ILT
1497 view_size);
1498 break;
1499 }
72ec2876 1500 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 1501 _("unsupported reloc %u"), r_type);
2e30d253
ILT
1502 break;
1503
56622147 1504 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
e041f13d
ILT
1505 if (optimized_type == tls::TLSOPT_TO_LE)
1506 {
72ec2876
ILT
1507 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
1508 value, view, view_size);
1509 break;
e041f13d 1510 }
72ec2876 1511 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 1512 _("unsupported reloc %u"), r_type);
2e30d253 1513 break;
0ffd9845
ILT
1514
1515 case elfcpp::R_X86_64_DTPOFF32:
e041f13d 1516 if (optimized_type == tls::TLSOPT_TO_LE)
0ffd9845
ILT
1517 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1518 else
1519 value = value - tls_segment->vaddr();
1520 Relocate_functions<64, false>::rel32(view, value);
1521 break;
1522
1523 case elfcpp::R_X86_64_DTPOFF64:
e041f13d 1524 if (optimized_type == tls::TLSOPT_TO_LE)
0ffd9845
ILT
1525 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1526 else
1527 value = value - tls_segment->vaddr();
1528 Relocate_functions<64, false>::rel64(view, value);
1529 break;
2e30d253 1530
56622147
ILT
1531 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1532 if (optimized_type == tls::TLSOPT_TO_LE)
1533 {
1534 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1535 rela, r_type, value, view,
1536 view_size);
1537 break;
1538 }
1539 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1540 _("unsupported reloc type %u"),
1541 r_type);
1542 break;
0ffd9845 1543
56622147
ILT
1544 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1545 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1546 Relocate_functions<64, false>::rel32(view, value);
1547 break;
2e30d253 1548 }
2e30d253
ILT
1549}
1550
e041f13d 1551// Do a relocation in which we convert a TLS General-Dynamic to a
2e30d253
ILT
1552// Local-Exec.
1553
1554inline void
d61c17ea
ILT
1555Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
1556 size_t relnum,
1557 Output_segment* tls_segment,
72ec2876 1558 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
1559 unsigned int,
1560 elfcpp::Elf_types<64>::Elf_Addr value,
1561 unsigned char* view,
1562 off_t view_size)
2e30d253 1563{
0ffd9845
ILT
1564 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1565 // .word 0x6666; rex64; call __tls_get_addr
1566 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2e30d253 1567
72ec2876
ILT
1568 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1569 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2e30d253 1570
72ec2876
ILT
1571 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1572 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1573 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1574 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2e30d253 1575
0ffd9845 1576 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2e30d253 1577
0ffd9845
ILT
1578 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1579 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2e30d253
ILT
1580
1581 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1582 // We can skip it.
1583 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
1584}
1585
2e30d253 1586inline void
72ec2876
ILT
1587Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
1588 size_t relnum,
1589 Output_segment*,
1590 const elfcpp::Rela<64, false>& rela,
1591 unsigned int,
1592 elfcpp::Elf_types<64>::Elf_Addr,
1593 unsigned char* view,
1594 off_t view_size)
2e30d253 1595{
72ec2876
ILT
1596 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1597 // ... leq foo@dtpoff(%rax),%reg
1598 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2e30d253 1599
72ec2876
ILT
1600 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1601 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2e30d253 1602
72ec2876
ILT
1603 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1604 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
1605
1606 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
1607
1608 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1609
1610 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1611 // We can skip it.
1612 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
1613}
1614
56622147
ILT
1615// Do a relocation in which we convert a TLS Initial-Exec to a
1616// Local-Exec.
1617
1618inline void
1619Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
1620 size_t relnum,
1621 Output_segment* tls_segment,
1622 const elfcpp::Rela<64, false>& rela,
1623 unsigned int,
1624 elfcpp::Elf_types<64>::Elf_Addr value,
1625 unsigned char* view,
1626 off_t view_size)
1627{
1628 // We need to examine the opcodes to figure out which instruction we
1629 // are looking at.
1630
1631 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1632 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1633
1634 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1635 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
1636
1637 unsigned char op1 = view[-3];
1638 unsigned char op2 = view[-2];
1639 unsigned char op3 = view[-1];
1640 unsigned char reg = op3 >> 3;
1641
1642 if (op2 == 0x8b)
1643 {
1644 // movq
1645 if (op1 == 0x4c)
1646 view[-3] = 0x49;
1647 view[-2] = 0xc7;
1648 view[-1] = 0xc0 | reg;
1649 }
1650 else if (reg == 4)
1651 {
1652 // Special handling for %rsp.
1653 if (op1 == 0x4c)
1654 view[-3] = 0x49;
1655 view[-2] = 0x81;
1656 view[-1] = 0xc0 | reg;
1657 }
1658 else
1659 {
1660 // addq
1661 if (op1 == 0x4c)
1662 view[-3] = 0x4d;
1663 view[-2] = 0x8d;
1664 view[-1] = 0x80 | reg | (reg << 3);
1665 }
1666
1667 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1668 Relocate_functions<64, false>::rela32(view, value, 0);
1669}
1670
2e30d253
ILT
1671// Relocate section data.
1672
1673void
1674Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
d61c17ea
ILT
1675 unsigned int sh_type,
1676 const unsigned char* prelocs,
1677 size_t reloc_count,
730cdc88
ILT
1678 Output_section* output_section,
1679 bool needs_special_offset_handling,
d61c17ea
ILT
1680 unsigned char* view,
1681 elfcpp::Elf_types<64>::Elf_Addr address,
1682 off_t view_size)
2e30d253
ILT
1683{
1684 gold_assert(sh_type == elfcpp::SHT_RELA);
1685
1686 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1687 Target_x86_64::Relocate>(
1688 relinfo,
1689 this,
1690 prelocs,
1691 reloc_count,
730cdc88
ILT
1692 output_section,
1693 needs_special_offset_handling,
2e30d253
ILT
1694 view,
1695 address,
1696 view_size);
1697}
1698
4fb6c25d
ILT
1699// Return the value to use for a dynamic which requires special
1700// treatment. This is how we support equality comparisons of function
1701// pointers across shared library boundaries, as described in the
1702// processor specific ABI supplement.
1703
1704uint64_t
1705Target_x86_64::do_dynsym_value(const Symbol* gsym) const
1706{
1707 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1708 return this->plt_section()->address() + gsym->plt_offset();
1709}
1710
2e30d253
ILT
1711// Return a string used to fill a code section with nops to take up
1712// the specified length.
1713
1714std::string
1715Target_x86_64::do_code_fill(off_t length)
1716{
1717 if (length >= 16)
1718 {
1719 // Build a jmpq instruction to skip over the bytes.
1720 unsigned char jmp[5];
1721 jmp[0] = 0xe9;
1722 elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1723 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1724 + std::string(length - 5, '\0'));
1725 }
1726
1727 // Nop sequences of various lengths.
1728 const char nop1[1] = { 0x90 }; // nop
1729 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1730 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1731 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1732 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1733 0x00 }; // leal 0(%esi,1),%esi
1734 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1735 0x00, 0x00 };
1736 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1737 0x00, 0x00, 0x00 };
1738 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1739 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1740 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1741 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1742 0x00 };
1743 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1744 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1745 0x00, 0x00 };
1746 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1747 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1748 0x00, 0x00, 0x00 };
1749 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1750 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1751 0x00, 0x00, 0x00, 0x00 };
1752 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1753 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1754 0x27, 0x00, 0x00, 0x00,
1755 0x00 };
1756 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1757 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1758 0xbc, 0x27, 0x00, 0x00,
1759 0x00, 0x00 };
1760 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1761 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1762 0x90, 0x90, 0x90, 0x90,
1763 0x90, 0x90, 0x90 };
1764
1765 const char* nops[16] = {
1766 NULL,
1767 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1768 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1769 };
1770
1771 return std::string(nops[length], length);
1772}
1773
1774// The selector for x86_64 object files.
1775
1776class Target_selector_x86_64 : public Target_selector
1777{
1778public:
1779 Target_selector_x86_64()
1780 : Target_selector(elfcpp::EM_X86_64, 64, false)
1781 { }
1782
1783 Target*
1784 recognize(int machine, int osabi, int abiversion);
1785
1786 private:
1787 Target_x86_64* target_;
1788};
1789
1790// Recognize an x86_64 object file when we already know that the machine
1791// number is EM_X86_64.
1792
1793Target*
1794Target_selector_x86_64::recognize(int, int, int)
1795{
1796 if (this->target_ == NULL)
1797 this->target_ = new Target_x86_64();
1798 return this->target_;
1799}
1800
1801Target_selector_x86_64 target_selector_x86_64;
1802
1803} // End anonymous namespace.
This page took 0.226395 seconds and 4 git commands to generate.