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