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