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