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