* readelf.c (dump_section_as_strings): Use %6tx instead of %6zx.
[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"
47
48namespace
49{
50
51using namespace gold;
52
53class Output_data_plt_x86_64;
54
55// The x86_64 target class.
56// See the ABI at http://www.x86-64.org/documentation/abi.pdf
57
58class Target_x86_64 : public Sized_target<64, false>
59{
60 public:
e822f2b1
ILT
61 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
62 // uses only Elf64_Rela relocation entries with explicit addends."
2e30d253
ILT
63 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
64
65 Target_x86_64()
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)
69 { }
70
71 // Scan the relocations to look for symbol adjustments.
72 void
73 scan_relocs(const General_options& options,
74 Symbol_table* symtab,
75 Layout* layout,
76 Sized_relobj<64, false>* object,
77 unsigned int data_shndx,
78 unsigned int sh_type,
79 const unsigned char* prelocs,
80 size_t reloc_count,
81 size_t local_symbol_count,
82 const unsigned char* plocal_symbols,
83 Symbol** global_symbols);
84
85 // Finalize the sections.
86 void
87 do_finalize_sections(Layout*);
88
89 // Relocate a section.
90 void
91 relocate_section(const Relocate_info<64, false>*,
92 unsigned int sh_type,
93 const unsigned char* prelocs,
94 size_t reloc_count,
95 unsigned char* view,
96 elfcpp::Elf_types<64>::Elf_Addr view_address,
97 off_t view_size);
98
99 // Return a string used to fill a code section with nops.
100 std::string
101 do_code_fill(off_t length);
102
103 private:
104 // The class which scans relocations.
105 struct Scan
106 {
107 inline void
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);
114
115 inline void
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,
121 Symbol* gsym);
122 };
123
124 // The class which implements relocation.
125 class Relocate
126 {
127 public:
128 Relocate()
129 : skip_call_tls_get_addr_(false)
130 { }
131
132 ~Relocate()
133 {
134 if (this->skip_call_tls_get_addr_)
135 {
136 // FIXME: This needs to specify the location somehow.
137 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
138 program_name);
139 gold_exit(false);
140 }
141 }
142
143 // Do a relocation. Return false if the caller should not issue
144 // any warnings about this relocation.
145 inline bool
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,
151 off_t);
152
153 private:
154 // Do a TLS relocation.
155 inline void
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);
161
162 // Do a TLS Initial-Exec to Local-Exec transition.
163 static inline void
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,
168 unsigned char* view,
169 off_t view_size);
170
171 // Do a TLS Global-Dynamic to Local-Exec transition.
172 inline void
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,
177 unsigned char* view,
178 off_t view_size);
179
180 // Check the range for a TLS relocation.
181 static inline void
182 check_range(const Relocate_info<64, false>*, size_t relnum,
183 const elfcpp::Rela<64, false>&, off_t, off_t);
184
185 // Check the validity of a TLS relocation. This is like assert.
186 static inline void
187 check_tls(const Relocate_info<64, false>*, size_t relnum,
188 const elfcpp::Rela<64, false>&, bool);
189
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_;
193 };
194
195 // Adjust TLS relocation type based on the options and whether this
196 // is a local symbol.
197 static unsigned int
198 optimize_tls_reloc(bool is_final, int r_type);
199
200 // Get the GOT section, creating it if necessary.
201 Output_data_got<64, false>*
202 got_section(Symbol_table*, Layout*);
203
204 // Create a PLT entry for a global symbol.
205 void
206 make_plt_entry(Symbol_table*, Layout*, Symbol*);
207
208 // Get the PLT section.
209 Output_data_plt_x86_64*
210 plt_section() const
211 {
212 gold_assert(this->plt_ != NULL);
213 return this->plt_;
214 }
215
216 // Get the dynamic reloc section, creating it if necessary.
217 Reloc_section*
218 rel_dyn_section(Layout*);
219
220 // Copy a relocation against a global symbol.
221 void
222 copy_reloc(const General_options*, Symbol_table*, Layout*,
223 Sized_relobj<64, false>*, unsigned int,
224 Symbol*, const elfcpp::Rela<64, false>&);
225
226 // Information about this specific target which we pass to the
227 // general Target structure.
228 static const Target::Target_info x86_64_info;
229
230 // The GOT section.
231 Output_data_got<64, false>* got_;
232 // The PLT section.
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_;
242};
243
244const Target::Target_info Target_x86_64::x86_64_info =
245{
246 64, // size
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
256};
257
258// Get the GOT section, creating it if necessary.
259
260Output_data_got<64, false>*
261Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
262{
263 if (this->got_ == NULL)
264 {
265 gold_assert(symtab != NULL && layout != NULL);
266
267 this->got_ = new Output_data_got<64, false>();
268
269 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
270 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
271 this->got_);
272
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
276 // might be empty.
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,
281 this->got_plt_);
282
283 // The first three entries are reserved.
284 this->got_plt_->set_space_size(3 * 8);
285
286 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
287 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
288 this->got_plt_,
289 0, 0, elfcpp::STT_OBJECT,
290 elfcpp::STB_LOCAL,
291 elfcpp::STV_HIDDEN, 0,
292 false, false);
293 }
294
295 return this->got_;
296}
297
298// Get the dynamic reloc section, creating it if necessary.
299
300Target_x86_64::Reloc_section*
301Target_x86_64::rel_dyn_section(Layout* layout)
302{
303 if (this->rel_dyn_ == NULL)
304 {
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_);
309 }
310 return this->rel_dyn_;
311}
312
313// A class to handle the PLT data.
314
315class Output_data_plt_x86_64 : public Output_section_data
316{
317 public:
318 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
319
320 Output_data_plt_x86_64(Layout*, Output_data_space*);
321
322 // Add an entry to the PLT.
323 void
324 add_entry(Symbol* gsym);
325
326 // Return the .rel.plt section data.
327 const Reloc_section*
328 rel_plt() const
329 { return this->rel_; }
330
331 protected:
332 void
333 do_adjust_output_section(Output_section* os);
334
335 private:
336 // The size of an entry in the PLT.
337 static const int plt_entry_size = 16;
338
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];
343
344 // Other entries in the PLT for an executable.
345 static unsigned char plt_entry[plt_entry_size];
346
347 // Set the final size.
348 void
349 do_set_address(uint64_t, off_t)
350 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
351
352 // Write out the PLT data.
353 void
354 do_write(Output_file*);
355
356 // The reloc section.
357 Reloc_section* rel_;
358 // The .got.plt section.
359 Output_data_space* got_plt_;
360 // The number of PLT entries.
361 unsigned int count_;
362};
363
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.
367
368Output_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)
372{
373 this->rel_ = new Reloc_section();
374 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
375 elfcpp::SHF_ALLOC, this->rel_);
376}
377
378void
379Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
380{
381 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
382 // linker, and so do we.
383 os->set_entsize(4);
384}
385
386// Add an entry to the PLT.
387
388void
389Output_data_plt_x86_64::add_entry(Symbol* gsym)
390{
391 gold_assert(!gsym->has_plt_offset());
392
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);
396
397 ++this->count_;
398
399 off_t got_offset = this->got_plt_->data_size();
400
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);
405
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_,
409 got_offset, 0);
410
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.
414}
415
416// The first entry in the PLT for an executable.
417
418unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
419{
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)
426};
427
428// Subsequent entries in the PLT for an executable.
429
430unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
431{
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
439};
440
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.
443
444void
445Output_data_plt_x86_64::do_write(Output_file* of)
446{
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);
450
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,
454 got_size);
455
456 unsigned char* pov = oview;
457
458 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
459 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
460
461 memcpy(pov, first_plt_entry, plt_entry_size);
462 if (!parameters->output_is_shared())
463 {
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));
469 }
470 pov += plt_entry_size;
471
472 unsigned char* got_pov = got_view;
473
474 memset(got_pov, 0, 24);
475 got_pov += 24;
476
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;
481 plt_index < count;
482 ++plt_index,
483 pov += plt_entry_size,
484 got_pov += 8,
485 plt_offset += plt_entry_size,
486 got_offset += 8)
487 {
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);
493 else
494 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
495 (got_address + got_offset
496 - (plt_address + plt_offset
497 + 6)));
498
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));
502
503 // Set the entry in the GOT.
504 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
505 }
506
507 gold_assert(pov - oview == oview_size);
508 gold_assert(got_pov - got_view == got_size);
509
510 of->write_output_view(offset, oview_size, oview);
511 of->write_output_view(got_file_offset, got_size, got_view);
512}
513
514// Create a PLT entry for a global symbol.
515
516void
517Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
518 Symbol* gsym)
519{
520 if (gsym->has_plt_offset())
521 return;
522
523 if (this->plt_ == NULL)
524 {
525 // Create the GOT sections first.
526 this->got_section(symtab, layout);
527
528 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
529 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
530 (elfcpp::SHF_ALLOC
531 | elfcpp::SHF_EXECINSTR),
532 this->plt_);
533 }
534
535 this->plt_->add_entry(gsym);
536}
537
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.
553
554void
555Target_x86_64::copy_reloc(const General_options* options,
556 Symbol_table* symtab,
557 Layout* layout,
558 Sized_relobj<64, false>* object,
559 unsigned int data_shndx, Symbol* gsym,
560 const elfcpp::Rela<64, false>& rel)
561{
562 Sized_symbol<64>* ssym;
563 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
564 SELECT_SIZE(64));
565
566 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
567 data_shndx, ssym))
568 {
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);
575 }
576 else
577 {
578 // Allocate space for this symbol in the .bss section.
579
580 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
581
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.
585 unsigned int align;
586 for (align = 1; align < 512; align <<= 1)
587 if ((symsize & align) != 0)
588 break;
589
590 if (this->dynbss_ == NULL)
591 {
592 this->dynbss_ = new Output_data_space(align);
593 layout->add_output_section_data(".bss",
594 elfcpp::SHT_NOBITS,
595 (elfcpp::SHF_ALLOC
596 | elfcpp::SHF_WRITE),
597 this->dynbss_);
598 }
599
600 Output_data_space* dynbss = this->dynbss_;
601
602 if (align > dynbss->addralign())
603 dynbss->set_space_alignment(align);
604
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);
609
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);
615
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,
620 rel.get_r_addend());
621 }
622}
623
624
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.
628
629unsigned int
630Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
631{
632 return is_final ? r_type : 0;
633#if 0
634 // If we are generating a shared library, then we can't do anything
635 // in the linker.
636 if (parameters->output_is_shared())
637 return r_type;
638
639 switch (r_type)
640 {
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.
646 if (is_final)
647 return elfcpp::R_X86_64_TLS_LE_64;
648 return elfcpp::R_X86_64_TLS_IE_64;
649
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;
655
656 case elfcpp::R_X86_64_TLS_LDO_64:
657 // Another type of Local-Dynamic relocation.
658 return elfcpp::R_X86_64_TLS_LE;
659
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.
667 if (is_final)
668 return elfcpp::R_X86_64_TLS_LE_64;
669 return r_type;
670
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
674 // can do.
675 return r_type;
676
677 default:
678 gold_unreachable();
679 }
680#endif
681}
682
683// Scan a relocation for a local symbol.
684
685inline void
686Target_x86_64::Scan::local(const General_options&,
687 Symbol_table* symtab,
688 Layout* layout,
689 Target_x86_64* target,
690 Sized_relobj<64, false>* object,
691 unsigned int,
692 const elfcpp::Rela<64, false>&,
693 unsigned int r_type,
694 const elfcpp::Sym<64, false>&)
695{
696 switch (r_type)
697 {
698 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
699 case elfcpp::R_386_GNU_VTINHERIT:
700 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
701 break;
702
703 case elfcpp::R_X86_64_64:
704 case elfcpp::R_X86_64_32:
705 case elfcpp::R_X86_64_32S:
706 case elfcpp::R_X86_64_16:
707 case elfcpp::R_X86_64_8:
708 // FIXME: If we are generating a shared object we need to copy
709 // this relocation into the object.
710 gold_assert(!parameters->output_is_shared());
711 break;
712
713 case elfcpp::R_X86_64_PC64:
714 case elfcpp::R_X86_64_PC32:
715 case elfcpp::R_X86_64_PC16:
716 case elfcpp::R_X86_64_PC8:
717 break;
718
2e30d253 719 case elfcpp::R_X86_64_GOTPCREL:
e822f2b1
ILT
720 case elfcpp::R_X86_64_GOTPC32: // TODO(csilvers): correct?
721 case elfcpp::R_X86_64_GOT64: // TODO(csilvers): correct?
722 case elfcpp::R_X86_64_GOTOFF64:
723 case elfcpp::R_X86_64_GOTPC64: // TODO(csilvers): correct?
724 case elfcpp::R_X86_64_GOTPCREL64: // TODO(csilvers): correct?
725 case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct?
726 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): correct?
2e30d253
ILT
727 // We need a GOT section.
728 target->got_section(symtab, layout);
729 break;
730
731 case elfcpp::R_X86_64_COPY:
732 case elfcpp::R_X86_64_GLOB_DAT:
733 case elfcpp::R_X86_64_JUMP_SLOT:
734 case elfcpp::R_X86_64_RELATIVE:
735 case elfcpp::R_X86_64_TPOFF64:
736 case elfcpp::R_X86_64_TPOFF32:
737 case elfcpp::R_X86_64_DTPMOD64:
738 case elfcpp::R_X86_64_DTPOFF64:
739 case elfcpp::R_X86_64_DTPOFF32:
e822f2b1 740 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
2e30d253 741 case elfcpp::R_X86_64_TLSDESC:
e822f2b1
ILT
742 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
743 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
2e30d253
ILT
744 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
745 program_name, object->name().c_str(), r_type);
746 gold_exit(false);
747 break;
748
749#if 0
e822f2b1
ILT
750 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
751 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
752
753 case elfcpp::R_386_TLS_IE:
754 case elfcpp::R_386_TLS_GOTIE:
755 case elfcpp::R_386_TLS_LE:
756 case elfcpp::R_386_TLS_GD:
757 case elfcpp::R_386_TLS_LDM:
758 case elfcpp::R_386_TLS_LDO_64:
759 case elfcpp::R_386_TLS_IE_64:
760 case elfcpp::R_386_TLS_LE_64:
2e30d253
ILT
761 {
762 bool output_is_shared = parameters->output_is_shared();
763 r_type = Target_x86_64::optimize_tls_reloc(!output_is_shared,
764 r_type);
765 switch (r_type)
766 {
767 case elfcpp::R_X86_64_TLS_LE:
768 case elfcpp::R_X86_64_TLS_LE_64:
769 // FIXME: If generating a shared object, we need to copy
770 // this relocation into the object.
771 gold_assert(!output_is_shared);
772 break;
773
774 case elfcpp::R_X86_64_TLS_IE:
775 case elfcpp::R_X86_64_TLS_GOTIE:
776 case elfcpp::R_X86_64_TLS_GD:
777 case elfcpp::R_X86_64_TLS_LDM:
778 case elfcpp::R_X86_64_TLS_LDO_64:
779 case elfcpp::R_X86_64_TLS_IE_64:
780 fprintf(stderr,
781 _("%s: %s: unsupported reloc %u against local symbol\n"),
782 program_name, object->name().c_str(), r_type);
783 break;
784 }
785 }
786 break;
787#endif
788
2e30d253
ILT
789 case elfcpp::R_X86_64_GOT32:
790 case elfcpp::R_X86_64_PLT32:
e822f2b1
ILT
791 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
792 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
793#if 0
794 case elfcpp::R_386_TLS_GD_64:
795 case elfcpp::R_386_TLS_GD_PUSH:
796 case elfcpp::R_386_TLS_GD_CALL:
797 case elfcpp::R_386_TLS_GD_POP:
798 case elfcpp::R_386_TLS_LDM_64:
799 case elfcpp::R_386_TLS_LDM_PUSH:
800 case elfcpp::R_386_TLS_LDM_CALL:
801 case elfcpp::R_386_TLS_LDM_POP:
2e30d253
ILT
802#endif
803 default:
804 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
805 program_name, object->name().c_str(), r_type);
806 break;
807 }
808}
809
810
811// Scan a relocation for a global symbol.
812
813inline void
814Target_x86_64::Scan::global(const General_options& options,
815 Symbol_table* symtab,
816 Layout* layout,
817 Target_x86_64* target,
818 Sized_relobj<64, false>* object,
819 unsigned int data_shndx,
820 const elfcpp::Rela<64, false>& reloc,
821 unsigned int r_type,
822 Symbol* gsym)
823{
824 switch (r_type)
825 {
826 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
827 case elfcpp::R_386_GNU_VTINHERIT:
828 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
829 break;
830
831 case elfcpp::R_X86_64_64:
832 case elfcpp::R_X86_64_PC64:
833 case elfcpp::R_X86_64_32:
834 case elfcpp::R_X86_64_32S:
835 case elfcpp::R_X86_64_PC32:
836 case elfcpp::R_X86_64_16:
837 case elfcpp::R_X86_64_PC16:
838 case elfcpp::R_X86_64_8:
839 case elfcpp::R_X86_64_PC8:
840 // FIXME: If we are generating a shared object we may need to
841 // copy this relocation into the object. If this symbol is
842 // defined in a shared object, we may need to copy this
843 // relocation in order to avoid a COPY relocation.
844 gold_assert(!parameters->output_is_shared());
845
846 if (gsym->is_from_dynobj())
847 {
848 // This symbol is defined in a dynamic object. If it is a
849 // function, we make a PLT entry. Otherwise we need to
850 // either generate a COPY reloc or copy this reloc.
851 if (gsym->type() == elfcpp::STT_FUNC)
852 target->make_plt_entry(symtab, layout, gsym);
853 else
854 target->copy_reloc(&options, symtab, layout, object, data_shndx,
855 gsym, reloc);
856 }
857
858 break;
859
860 case elfcpp::R_X86_64_GOT32:
861 {
862 // The symbol requires a GOT entry.
863 Output_data_got<64, false>* got = target->got_section(symtab, layout);
864 if (got->add_global(gsym))
865 {
866 // If this symbol is not fully resolved, we need to add a
867 // dynamic relocation for it.
868 if (!gsym->final_value_is_known())
869 {
870 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
871 rel_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, got,
872 gsym->got_offset(), reloc.get_r_addend());
873 }
874 }
875 }
876 break;
877
878 case elfcpp::R_X86_64_PLT32:
879 // If the symbol is fully resolved, this is just a PC32 reloc.
880 // Otherwise we need a PLT entry.
881 if (gsym->final_value_is_known())
882 break;
883 target->make_plt_entry(symtab, layout, gsym);
884 break;
885
2e30d253 886 case elfcpp::R_X86_64_GOTPCREL:
e822f2b1
ILT
887 case elfcpp::R_X86_64_GOTPC32: // TODO(csilvers): correct?
888 case elfcpp::R_X86_64_GOT64: // TODO(csilvers): correct?
889 case elfcpp::R_X86_64_GOTOFF64:
890 case elfcpp::R_X86_64_GOTPC64: // TODO(csilvers): correct?
891 case elfcpp::R_X86_64_GOTPCREL64: // TODO(csilvers): correct?
892 case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct?
893 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): correct?
2e30d253
ILT
894 // We need a GOT section.
895 target->got_section(symtab, layout);
896 break;
897
2e30d253
ILT
898 case elfcpp::R_X86_64_COPY:
899 case elfcpp::R_X86_64_GLOB_DAT:
900 case elfcpp::R_X86_64_JUMP_SLOT:
901 case elfcpp::R_X86_64_RELATIVE:
e822f2b1
ILT
902 case elfcpp::R_X86_64_TPOFF64:
903 case elfcpp::R_X86_64_TPOFF32:
2e30d253
ILT
904 case elfcpp::R_X86_64_DTPMOD64:
905 case elfcpp::R_X86_64_DTPOFF64:
906 case elfcpp::R_X86_64_DTPOFF32:
e822f2b1
ILT
907 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
908 case elfcpp::R_X86_64_TLSDESC:
909 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
910 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
2e30d253
ILT
911 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
912 program_name, object->name().c_str(), r_type);
913 gold_exit(false);
914 break;
2e30d253
ILT
915
916#if 0
e822f2b1
ILT
917 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
918 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
919
920 case elfcpp::R_386_TLS_IE:
921 case elfcpp::R_386_TLS_GOTIE:
922 case elfcpp::R_386_TLS_LE:
923 case elfcpp::R_386_TLS_GD:
924 case elfcpp::R_386_TLS_LDM:
925 case elfcpp::R_386_TLS_LDO_64:
926 case elfcpp::R_386_TLS_IE_64:
927 case elfcpp::R_386_TLS_LE_64:
2e30d253
ILT
928 {
929 const bool is_final = gsym->final_value_is_known();
930 r_type = Target_x86_64::optimize_tls_reloc(is_final, r_type);
931 switch (r_type)
932 {
e822f2b1
ILT
933 case elfcpp::R_386_TLS_LE:
934 case elfcpp::R_386_TLS_LE_64:
2e30d253
ILT
935 // FIXME: If generating a shared object, we need to copy
936 // this relocation into the object.
937 gold_assert(!parameters->output_is_shared());
938 break;
939
e822f2b1
ILT
940 case elfcpp::R_386_TLS_IE:
941 case elfcpp::R_386_TLS_GOTIE:
942 case elfcpp::R_386_TLS_GD:
943 case elfcpp::R_386_TLS_LDM:
944 case elfcpp::R_386_TLS_LDO_64:
945 case elfcpp::R_386_TLS_IE_64:
2e30d253
ILT
946 fprintf(stderr,
947 _("%s: %s: unsupported reloc %u "
948 "against global symbol %s\n"),
949 program_name, object->name().c_str(), r_type,
950 gsym->name());
951 break;
952 }
953 }
954 break;
955#endif
956
e822f2b1
ILT
957 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
958 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
2e30d253 959#if 0
e822f2b1
ILT
960 case elfcpp::R_386_TLS_GD_64:
961 case elfcpp::R_386_TLS_GD_PUSH:
962 case elfcpp::R_386_TLS_GD_CALL:
963 case elfcpp::R_386_TLS_GD_POP:
964 case elfcpp::R_386_TLS_LDM_64:
965 case elfcpp::R_386_TLS_LDM_PUSH:
966 case elfcpp::R_386_TLS_LDM_CALL:
967 case elfcpp::R_386_TLS_LDM_POP:
2e30d253
ILT
968#endif
969 default:
970 fprintf(stderr,
971 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
972 program_name, object->name().c_str(), r_type, gsym->name());
973 break;
974 }
975}
976
977// Scan relocations for a section.
978
979void
980Target_x86_64::scan_relocs(const General_options& options,
981 Symbol_table* symtab,
982 Layout* layout,
983 Sized_relobj<64, false>* object,
984 unsigned int data_shndx,
985 unsigned int sh_type,
986 const unsigned char* prelocs,
987 size_t reloc_count,
988 size_t local_symbol_count,
989 const unsigned char* plocal_symbols,
990 Symbol** global_symbols)
991{
992 if (sh_type == elfcpp::SHT_REL)
993 {
994 fprintf(stderr, _("%s: %s: unsupported REL reloc section\n"),
995 program_name, object->name().c_str());
996 gold_exit(false);
997 }
998
999 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1000 Target_x86_64::Scan>(
1001 options,
1002 symtab,
1003 layout,
1004 this,
1005 object,
1006 data_shndx,
1007 prelocs,
1008 reloc_count,
1009 local_symbol_count,
1010 plocal_symbols,
1011 global_symbols);
1012}
1013
1014// Finalize the sections.
1015
1016void
1017Target_x86_64::do_finalize_sections(Layout* layout)
1018{
1019 // Fill in some more dynamic tags.
1020 Output_data_dynamic* const odyn = layout->dynamic_data();
1021 if (odyn != NULL)
1022 {
1023 if (this->got_plt_ != NULL)
1024 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1025
1026 if (this->plt_ != NULL)
1027 {
1028 const Output_data* od = this->plt_->rel_plt();
1029 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1030 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1031 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1032 }
1033
1034 if (this->rel_dyn_ != NULL)
1035 {
1036 const Output_data* od = this->rel_dyn_;
1037 odyn->add_section_address(elfcpp::DT_RELA, od);
e84992bb 1038 odyn->add_section_size(elfcpp::DT_RELASZ, od);
2e30d253 1039 odyn->add_constant(elfcpp::DT_RELAENT,
e84992bb 1040 elfcpp::Elf_sizes<64>::rela_size);
2e30d253
ILT
1041 }
1042
1043 if (!parameters->output_is_shared())
1044 {
1045 // The value of the DT_DEBUG tag is filled in by the dynamic
1046 // linker at run time, and used by the debugger.
1047 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1048 }
1049 }
1050
1051 // Emit any relocs we saved in an attempt to avoid generating COPY
1052 // relocs.
1053 if (this->copy_relocs_ == NULL)
1054 return;
1055 if (this->copy_relocs_->any_to_emit())
1056 {
1057 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1058 this->copy_relocs_->emit(rel_dyn);
1059 }
1060 delete this->copy_relocs_;
1061 this->copy_relocs_ = NULL;
1062}
1063
1064// Perform a relocation.
1065
1066inline bool
1067Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1068 Target_x86_64* target,
1069 size_t relnum,
1070 const elfcpp::Rela<64, false>& rel,
1071 unsigned int r_type,
1072 const Sized_symbol<64>* gsym,
1073 const Symbol_value<64>* psymval,
1074 unsigned char* view,
1075 elfcpp::Elf_types<64>::Elf_Addr address,
1076 off_t view_size)
1077{
1078 if (this->skip_call_tls_get_addr_)
1079 {
1080 if (r_type != elfcpp::R_X86_64_PLT32
1081 || gsym == NULL
1082 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1083 {
1084 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
1085 program_name,
1086 relinfo->location(relnum, rel.get_r_offset()).c_str());
1087 gold_exit(false);
1088 }
1089
1090 this->skip_call_tls_get_addr_ = false;
1091
1092 return false;
1093 }
1094
1095 // Pick the value to use for symbols defined in shared objects.
1096 Symbol_value<64> symval;
1097 if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
1098 {
1099 symval.set_output_value(target->plt_section()->address()
1100 + gsym->plt_offset());
1101 psymval = &symval;
1102 }
1103
1104 const Sized_relobj<64, false>* object = relinfo->object;
1105 const elfcpp::Elf_Xword addend = rel.get_r_addend();
1106
1107 switch (r_type)
1108 {
1109 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
1110 case elfcpp::R_386_GNU_VTINHERIT:
1111 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
1112 break;
1113
1114 case elfcpp::R_X86_64_64:
1115 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1116 break;
1117
1118 case elfcpp::R_X86_64_PC64:
1119 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1120 address);
1121 break;
1122
1123 case elfcpp::R_X86_64_32:
7bb3655e
ILT
1124 // FIXME: we need to verify that value + addend fits into 32 bits:
1125 // uint64_t x = value + addend;
1126 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1127 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2e30d253
ILT
1128 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1129 break;
1130
1131 case elfcpp::R_X86_64_32S:
7bb3655e
ILT
1132 // FIXME: we need to verify that value + addend fits into 32 bits:
1133 // int64_t x = value + addend; // note this quantity is signed!
1134 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2e30d253
ILT
1135 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1136 break;
1137
1138 case elfcpp::R_X86_64_PC32:
1139 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1140 address);
1141 break;
1142
1143 case elfcpp::R_X86_64_16:
1144 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1145 break;
1146
1147 case elfcpp::R_X86_64_PC16:
1148 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1149 address);
1150 break;
1151
1152 case elfcpp::R_X86_64_8:
1153 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1154 break;
1155
1156 case elfcpp::R_X86_64_PC8:
1157 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1158 address);
1159 break;
1160
1161 case elfcpp::R_X86_64_PLT32:
1162 gold_assert(gsym->has_plt_offset()
1163 || gsym->final_value_is_known());
1164 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1165 address);
1166 break;
1167
1168 case elfcpp::R_X86_64_GOT32:
1169 // Local GOT offsets not yet supported.
1170 gold_assert(gsym);
1171 gold_assert(gsym->has_got_offset());
1172 Relocate_functions<64, false>::rela32(view, gsym->got_offset(), addend);
1173 break;
1174
e822f2b1
ILT
1175 case elfcpp::R_X86_64_GOTPC32:
1176 {
1177 gold_assert(gsym);
1178 elfcpp::Elf_types<64>::Elf_Addr value;
1179 value = target->got_section(NULL, NULL)->address();
1180 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1181 }
1182 break;
1183
1184 case elfcpp::R_X86_64_GOT64:
1185 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1186 // Since we always add a PLT entry, this is equivalent.
1187 case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct?
1188 // Local GOT offsets not yet supported.
1189 gold_assert(gsym);
1190 gold_assert(gsym->has_got_offset());
1191 Relocate_functions<64, false>::rela64(view, gsym->got_offset(), addend);
1192 break;
1193
1194 case elfcpp::R_X86_64_GOTPC64:
1195 {
1196 gold_assert(gsym);
1197 elfcpp::Elf_types<64>::Elf_Addr value;
1198 value = target->got_section(NULL, NULL)->address();
1199 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1200 }
1201 break;
1202
2e30d253
ILT
1203 case elfcpp::R_X86_64_GOTOFF64:
1204 {
1205 elfcpp::Elf_types<64>::Elf_Addr value;
1206 value = (psymval->value(object, 0)
1207 - target->got_section(NULL, NULL)->address());
1208 Relocate_functions<64, false>::rela64(view, value, addend);
1209 }
1210 break;
1211
1212 case elfcpp::R_X86_64_GOTPCREL:
1213 {
1214 gold_assert(gsym);
1215 elfcpp::Elf_types<64>::Elf_Addr value;
1216 // FIXME(csilvers): this is probably totally wrong for G + GOT
1217 value = (target->got_section(NULL, NULL)->address()
1218 + (gsym->has_got_offset() ? gsym->got_offset() : 0));
1219 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1220 }
1221 break;
1222
e822f2b1
ILT
1223 case elfcpp::R_X86_64_GOTPCREL64:
1224 {
1225 gold_assert(gsym);
1226 elfcpp::Elf_types<64>::Elf_Addr value;
1227 // FIXME(csilvers): this is probably totally wrong for G + GOT
1228 value = (target->got_section(NULL, NULL)->address()
1229 + (gsym->has_got_offset() ? gsym->got_offset() : 0));
1230 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1231 }
1232 break;
1233
2e30d253
ILT
1234 case elfcpp::R_X86_64_COPY:
1235 case elfcpp::R_X86_64_GLOB_DAT:
1236 case elfcpp::R_X86_64_JUMP_SLOT:
1237 case elfcpp::R_X86_64_RELATIVE:
1238 case elfcpp::R_X86_64_TPOFF64:
1239 case elfcpp::R_X86_64_TPOFF32:
1240 case elfcpp::R_X86_64_DTPMOD64:
1241 case elfcpp::R_X86_64_DTPOFF64:
1242 case elfcpp::R_X86_64_DTPOFF32:
e822f2b1 1243 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
2e30d253 1244 case elfcpp::R_X86_64_TLSDESC:
e822f2b1
ILT
1245 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
1246 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
2e30d253
ILT
1247 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1248 program_name,
1249 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1250 r_type);
1251 gold_exit(false);
1252 break;
1253
1254#if 0
e822f2b1
ILT
1255 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
1256 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
1257
1258 case elfcpp::R_386_TLS_IE:
1259 case elfcpp::R_386_TLS_GOTIE:
1260 case elfcpp::R_386_TLS_LE:
1261 case elfcpp::R_386_TLS_GD:
1262 case elfcpp::R_386_TLS_LDM:
1263 case elfcpp::R_386_TLS_LDO_64:
1264 case elfcpp::R_386_TLS_IE_64:
1265 case elfcpp::R_386_TLS_LE_64:
2e30d253
ILT
1266 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
1267 address, view_size);
1268 break;
1269#else
1270 view_size++; // this is to make view_size used
1271#endif
1272
e822f2b1
ILT
1273 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
1274 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
1275 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): implement me!
2e30d253 1276#if 0
e822f2b1
ILT
1277 case elfcpp::R_386_TLS_GD_64:
1278 case elfcpp::R_386_TLS_GD_PUSH:
1279 case elfcpp::R_386_TLS_GD_CALL:
1280 case elfcpp::R_386_TLS_GD_POP:
1281 case elfcpp::R_386_TLS_LDM_64:
1282 case elfcpp::R_386_TLS_LDM_PUSH:
1283 case elfcpp::R_386_TLS_LDM_CALL:
1284 case elfcpp::R_386_TLS_LDM_POP:
2e30d253
ILT
1285#endif
1286 default:
1287 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1288 program_name,
1289 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1290 r_type);
1291 // gold_exit(false);
1292 break;
1293 }
1294
1295 return true;
1296}
1297
1298// Perform a TLS relocation.
1299
1300inline void
1301Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>*, // relinfo,
1302 size_t , // relnum,
1303 const elfcpp::Rela<64, false>& , // rel,
1304 unsigned int , // r_type,
1305 const Sized_symbol<64>* , // gsym,
1306 const Symbol_value<64>* , // psymval,
1307 unsigned char* , // view,
1308 elfcpp::Elf_types<64>::Elf_Addr,
1309 off_t)// view_size)
1310{
1311#if 0
1312 Output_segment* tls_segment = relinfo->layout->tls_segment();
1313 if (tls_segment == NULL)
1314 {
1315 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1316 program_name,
1317 relinfo->location(relnum, rel.get_r_offset()).c_str());
1318 gold_exit(false);
1319 }
1320
1321 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1322
1323 const bool is_final = (gsym == NULL
1324 ? !parameters->output_is_shared()
1325 : gsym->final_value_is_known());
1326 const unsigned int opt_r_type =
1327 Target_x86_64::optimize_tls_reloc(is_final, r_type);
1328 switch (r_type)
1329 {
1330 case elfcpp::R_X86_64_TLS_LE_64:
1331 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1332 Relocate_functions<64, false>::rel64(view, value);
1333 break;
1334
1335 case elfcpp::R_X86_64_TLS_LE:
1336 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1337 Relocate_functions<64, false>::rel64(view, value);
1338 break;
1339
1340 case elfcpp::R_X86_64_TLS_IE:
1341 case elfcpp::R_X86_64_TLS_GOTIE:
1342 case elfcpp::R_X86_64_TLS_IE_64:
1343 if (opt_r_type == elfcpp::R_X86_64_TLS_LE_64)
1344 {
1345 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1346 rel, r_type, value, view,
1347 view_size);
1348 break;
1349 }
1350 fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
1351 program_name,
1352 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1353 r_type);
1354 // gold_exit(false);
1355 break;
1356
1357 case elfcpp::R_X86_64_TLS_GD:
1358 if (opt_r_type == elfcpp::R_X86_64_TLS_LE_64)
1359 {
1360 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1361 rel, r_type, value, view,
1362 view_size);
1363 break;
1364 }
1365 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1366 program_name,
1367 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1368 r_type);
1369 // gold_exit(false);
1370 break;
1371
1372 case elfcpp::R_X86_64_TLS_LDM:
1373 case elfcpp::R_X86_64_TLS_LDO_64:
1374 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1375 program_name,
1376 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1377 r_type);
1378 // gold_exit(false);
1379 break;
1380 }
1381#endif
1382}
1383
1384// Do a relocation in which we convert a TLS Initial-Exec to a
1385// Local-Exec.
1386
1387inline void
1388Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* , // relinfo,
1389 size_t , // relnum,
1390 Output_segment* , // tls_segment,
1391 const elfcpp::Rela<64, false>& , // rel,
1392 unsigned int , // r_type,
1393 elfcpp::Elf_types<64>::Elf_Addr , // value,
1394 unsigned char* , // view,
1395 off_t) // view_size)
1396{
1397#if 0
1398 // We have to actually change the instructions, which means that we
1399 // need to examine the opcodes to figure out which instruction we
1400 // are looking at.
1401 if (r_type == elfcpp::R_X86_64_TLS_IE)
1402 {
1403 // movl %gs:XX,%eax ==> movl $YY,%eax
1404 // movl %gs:XX,%reg ==> movl $YY,%reg
1405 // addl %gs:XX,%reg ==> addl $YY,%reg
1406 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1407 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1408
1409 unsigned char op1 = view[-1];
1410 if (op1 == 0xa1)
1411 {
1412 // movl XX,%eax ==> movl $YY,%eax
1413 view[-1] = 0xb8;
1414 }
1415 else
1416 {
1417 Target_x86_64::Relocate::check_range(relinfo, relnum, rel,
1418 view_size, -2);
1419
1420 unsigned char op2 = view[-2];
1421 if (op2 == 0x8b)
1422 {
1423 // movl XX,%reg ==> movl $YY,%reg
1424 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1425 (op1 & 0xc7) == 0x05);
1426 view[-2] = 0xc7;
1427 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1428 }
1429 else if (op2 == 0x03)
1430 {
1431 // addl XX,%reg ==> addl $YY,%reg
1432 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1433 (op1 & 0xc7) == 0x05);
1434 view[-2] = 0x81;
1435 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1436 }
1437 else
1438 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, 0);
1439 }
1440 }
1441 else
1442 {
1443 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1444 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1445 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1446 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1447 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1448
1449 unsigned char op1 = view[-1];
1450 unsigned char op2 = view[-2];
1451 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1452 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1453 if (op2 == 0x8b)
1454 {
1455 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1456 view[-2] = 0xc7;
1457 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1458 }
1459 else if (op2 == 0x2b)
1460 {
1461 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1462 view[-2] = 0x81;
1463 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1464 }
1465 else if (op2 == 0x03)
1466 {
1467 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1468 view[-2] = 0x81;
1469 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1470 }
1471 else
1472 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, 0);
1473 }
1474
1475 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1476 if (r_type == elfcpp::R_X86_64_TLS_IE || r_type == elfcpp::R_X86_64_TLS_GOTIE)
1477 value = - value;
1478
1479 Relocate_functions<64, false>::rel64(view, value);
1480#endif
1481}
1482
1483// Do a relocation in which we convert a TLS Global-Dynamic to a
1484// Local-Exec.
1485
1486inline void
1487Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* , // relinfo,
1488 size_t , // relnum,
1489 Output_segment* , // tls_segment,
1490 const elfcpp::Rela<64, false>& , // rel,
1491 unsigned int,
1492 elfcpp::Elf_types<64>::Elf_Addr , // value,
1493 unsigned char* , // view,
1494 off_t)// view_size)
1495{
1496#if 0
1497 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1498 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1499 // leal foo(%reg),%eax; call ___tls_get_addr
1500 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1501
1502 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1503 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1504
1505 unsigned char op1 = view[-1];
1506 unsigned char op2 = view[-2];
1507
1508 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1509 op2 == 0x8d || op2 == 0x04);
1510 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1511 view[4] == 0xe8);
1512
1513 int roff = 5;
1514
1515 if (op2 == 0x04)
1516 {
1517 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1518 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1519 view[-3] == 0x8d);
1520 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1521 ((op1 & 0xc7) == 0x05
1522 && op1 != (4 << 3)));
1523 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1524 }
1525 else
1526 {
1527 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1528 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1529 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1530 {
1531 // There is a trailing nop. Use the size byte subl.
1532 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1533 roff = 6;
1534 }
1535 else
1536 {
1537 // Use the five byte subl.
1538 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1539 }
1540 }
1541
1542 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1543 Relocate_functions<64, false>::rel64(view + roff, value);
1544
1545 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1546 // We can skip it.
1547 this->skip_call_tls_get_addr_ = true;
1548#endif
1549}
1550
1551// Check the range for a TLS relocation.
1552
1553inline void
1554Target_x86_64::Relocate::check_range(const Relocate_info<64, false>* relinfo,
1555 size_t relnum,
1556 const elfcpp::Rela<64, false>& rel,
1557 off_t view_size, off_t off)
1558{
1559 off_t offset = rel.get_r_offset() + off;
1560 if (offset < 0 || offset > view_size)
1561 {
1562 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1563 program_name,
1564 relinfo->location(relnum, rel.get_r_offset()).c_str());
1565 gold_exit(false);
1566 }
1567}
1568
1569// Check the validity of a TLS relocation. This is like assert.
1570
1571inline void
1572Target_x86_64::Relocate::check_tls(const Relocate_info<64, false>* relinfo,
1573 size_t relnum,
1574 const elfcpp::Rela<64, false>& rel,
1575 bool valid)
1576{
1577 if (!valid)
1578 {
1579 fprintf(stderr,
1580 _("%s: %s: TLS relocation against invalid instruction\n"),
1581 program_name,
1582 relinfo->location(relnum, rel.get_r_offset()).c_str());
1583 gold_exit(false);
1584 }
1585}
1586
1587// Relocate section data.
1588
1589void
1590Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1591 unsigned int sh_type,
1592 const unsigned char* prelocs,
1593 size_t reloc_count,
1594 unsigned char* view,
1595 elfcpp::Elf_types<64>::Elf_Addr address,
1596 off_t view_size)
1597{
1598 gold_assert(sh_type == elfcpp::SHT_RELA);
1599
1600 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1601 Target_x86_64::Relocate>(
1602 relinfo,
1603 this,
1604 prelocs,
1605 reloc_count,
1606 view,
1607 address,
1608 view_size);
1609}
1610
1611// Return a string used to fill a code section with nops to take up
1612// the specified length.
1613
1614std::string
1615Target_x86_64::do_code_fill(off_t length)
1616{
1617 if (length >= 16)
1618 {
1619 // Build a jmpq instruction to skip over the bytes.
1620 unsigned char jmp[5];
1621 jmp[0] = 0xe9;
1622 elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1623 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1624 + std::string(length - 5, '\0'));
1625 }
1626
1627 // Nop sequences of various lengths.
1628 const char nop1[1] = { 0x90 }; // nop
1629 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1630 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1631 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1632 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1633 0x00 }; // leal 0(%esi,1),%esi
1634 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1635 0x00, 0x00 };
1636 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1637 0x00, 0x00, 0x00 };
1638 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1639 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1640 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1641 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1642 0x00 };
1643 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1644 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1645 0x00, 0x00 };
1646 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1647 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1648 0x00, 0x00, 0x00 };
1649 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1650 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1651 0x00, 0x00, 0x00, 0x00 };
1652 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1653 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1654 0x27, 0x00, 0x00, 0x00,
1655 0x00 };
1656 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1657 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1658 0xbc, 0x27, 0x00, 0x00,
1659 0x00, 0x00 };
1660 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1661 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1662 0x90, 0x90, 0x90, 0x90,
1663 0x90, 0x90, 0x90 };
1664
1665 const char* nops[16] = {
1666 NULL,
1667 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1668 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1669 };
1670
1671 return std::string(nops[length], length);
1672}
1673
1674// The selector for x86_64 object files.
1675
1676class Target_selector_x86_64 : public Target_selector
1677{
1678public:
1679 Target_selector_x86_64()
1680 : Target_selector(elfcpp::EM_X86_64, 64, false)
1681 { }
1682
1683 Target*
1684 recognize(int machine, int osabi, int abiversion);
1685
1686 private:
1687 Target_x86_64* target_;
1688};
1689
1690// Recognize an x86_64 object file when we already know that the machine
1691// number is EM_X86_64.
1692
1693Target*
1694Target_selector_x86_64::recognize(int, int, int)
1695{
1696 if (this->target_ == NULL)
1697 this->target_ = new Target_x86_64();
1698 return this->target_;
1699}
1700
1701Target_selector_x86_64 target_selector_x86_64;
1702
1703} // End anonymous namespace.
This page took 0.086542 seconds and 4 git commands to generate.