*** empty log message ***
[deliverable/binutils-gdb.git] / gold / x86_64.cc
1 // x86_64.cc -- x86_64 target support for gold.
2
3 // Copyright 2006, 2007, 2008, 2009 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 modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstring>
26
27 #include "elfcpp.h"
28 #include "parameters.h"
29 #include "reloc.h"
30 #include "x86_64.h"
31 #include "object.h"
32 #include "symtab.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "copy-relocs.h"
36 #include "target.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
39 #include "tls.h"
40 #include "freebsd.h"
41
42 namespace
43 {
44
45 using namespace gold;
46
47 class Output_data_plt_x86_64;
48
49 // The x86_64 target class.
50 // See the ABI at
51 // http://www.x86-64.org/documentation/abi.pdf
52 // TLS info comes from
53 // http://people.redhat.com/drepper/tls.pdf
54 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
55
56 class Target_x86_64 : public Target_freebsd<64, false>
57 {
58 public:
59 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
60 // uses only Elf64_Rela relocation entries with explicit addends."
61 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
62
63 Target_x86_64()
64 : Target_freebsd<64, false>(&x86_64_info),
65 got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL),
66 copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
67 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
68 { }
69
70 // Scan the relocations to look for symbol adjustments.
71 void
72 gc_process_relocs(const General_options& options,
73 Symbol_table* symtab,
74 Layout* layout,
75 Sized_relobj<64, false>* object,
76 unsigned int data_shndx,
77 unsigned int sh_type,
78 const unsigned char* prelocs,
79 size_t reloc_count,
80 Output_section* output_section,
81 bool needs_special_offset_handling,
82 size_t local_symbol_count,
83 const unsigned char* plocal_symbols);
84
85 // Scan the relocations to look for symbol adjustments.
86 void
87 scan_relocs(const General_options& options,
88 Symbol_table* symtab,
89 Layout* layout,
90 Sized_relobj<64, false>* object,
91 unsigned int data_shndx,
92 unsigned int sh_type,
93 const unsigned char* prelocs,
94 size_t reloc_count,
95 Output_section* output_section,
96 bool needs_special_offset_handling,
97 size_t local_symbol_count,
98 const unsigned char* plocal_symbols);
99
100 // Finalize the sections.
101 void
102 do_finalize_sections(Layout*);
103
104 // Return the value to use for a dynamic which requires special
105 // treatment.
106 uint64_t
107 do_dynsym_value(const Symbol*) const;
108
109 // Relocate a section.
110 void
111 relocate_section(const Relocate_info<64, false>*,
112 unsigned int sh_type,
113 const unsigned char* prelocs,
114 size_t reloc_count,
115 Output_section* output_section,
116 bool needs_special_offset_handling,
117 unsigned char* view,
118 elfcpp::Elf_types<64>::Elf_Addr view_address,
119 section_size_type view_size);
120
121 // Scan the relocs during a relocatable link.
122 void
123 scan_relocatable_relocs(const General_options& options,
124 Symbol_table* symtab,
125 Layout* layout,
126 Sized_relobj<64, false>* object,
127 unsigned int data_shndx,
128 unsigned int sh_type,
129 const unsigned char* prelocs,
130 size_t reloc_count,
131 Output_section* output_section,
132 bool needs_special_offset_handling,
133 size_t local_symbol_count,
134 const unsigned char* plocal_symbols,
135 Relocatable_relocs*);
136
137 // Relocate a section during a relocatable link.
138 void
139 relocate_for_relocatable(const Relocate_info<64, false>*,
140 unsigned int sh_type,
141 const unsigned char* prelocs,
142 size_t reloc_count,
143 Output_section* output_section,
144 off_t offset_in_output_section,
145 const Relocatable_relocs*,
146 unsigned char* view,
147 elfcpp::Elf_types<64>::Elf_Addr view_address,
148 section_size_type view_size,
149 unsigned char* reloc_view,
150 section_size_type reloc_view_size);
151
152 // Return a string used to fill a code section with nops.
153 std::string
154 do_code_fill(section_size_type length) const;
155
156 // Return whether SYM is defined by the ABI.
157 bool
158 do_is_defined_by_abi(const Symbol* sym) const
159 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
160
161 // Return the size of the GOT section.
162 section_size_type
163 got_size()
164 {
165 gold_assert(this->got_ != NULL);
166 return this->got_->data_size();
167 }
168
169 private:
170 // The class which scans relocations.
171 class Scan
172 {
173 public:
174 Scan()
175 : issued_non_pic_error_(false)
176 { }
177
178 inline void
179 local(const General_options& options, Symbol_table* symtab,
180 Layout* layout, Target_x86_64* target,
181 Sized_relobj<64, false>* object,
182 unsigned int data_shndx,
183 Output_section* output_section,
184 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
185 const elfcpp::Sym<64, false>& lsym);
186
187 inline void
188 global(const General_options& options, Symbol_table* symtab,
189 Layout* layout, Target_x86_64* target,
190 Sized_relobj<64, false>* object,
191 unsigned int data_shndx,
192 Output_section* output_section,
193 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
194 Symbol* gsym);
195
196 private:
197 static void
198 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
199
200 static void
201 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
202 Symbol*);
203
204 void
205 check_non_pic(Relobj*, unsigned int r_type);
206
207 // Whether we have issued an error about a non-PIC compilation.
208 bool issued_non_pic_error_;
209 };
210
211 // The class which implements relocation.
212 class Relocate
213 {
214 public:
215 Relocate()
216 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
217 { }
218
219 ~Relocate()
220 {
221 if (this->skip_call_tls_get_addr_)
222 {
223 // FIXME: This needs to specify the location somehow.
224 gold_error(_("missing expected TLS relocation"));
225 }
226 }
227
228 // Do a relocation. Return false if the caller should not issue
229 // any warnings about this relocation.
230 inline bool
231 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
232 size_t relnum, const elfcpp::Rela<64, false>&,
233 unsigned int r_type, const Sized_symbol<64>*,
234 const Symbol_value<64>*,
235 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
236 section_size_type);
237
238 private:
239 // Do a TLS relocation.
240 inline void
241 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
242 size_t relnum, const elfcpp::Rela<64, false>&,
243 unsigned int r_type, const Sized_symbol<64>*,
244 const Symbol_value<64>*,
245 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
246 section_size_type);
247
248 // Do a TLS General-Dynamic to Initial-Exec transition.
249 inline void
250 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
251 Output_segment* tls_segment,
252 const elfcpp::Rela<64, false>&, unsigned int r_type,
253 elfcpp::Elf_types<64>::Elf_Addr value,
254 unsigned char* view,
255 elfcpp::Elf_types<64>::Elf_Addr,
256 section_size_type view_size);
257
258 // Do a TLS General-Dynamic to Local-Exec transition.
259 inline void
260 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
261 Output_segment* tls_segment,
262 const elfcpp::Rela<64, false>&, unsigned int r_type,
263 elfcpp::Elf_types<64>::Elf_Addr value,
264 unsigned char* view,
265 section_size_type view_size);
266
267 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
268 inline void
269 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
270 Output_segment* tls_segment,
271 const elfcpp::Rela<64, false>&, unsigned int r_type,
272 elfcpp::Elf_types<64>::Elf_Addr value,
273 unsigned char* view,
274 elfcpp::Elf_types<64>::Elf_Addr,
275 section_size_type view_size);
276
277 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
278 inline void
279 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
280 Output_segment* tls_segment,
281 const elfcpp::Rela<64, false>&, unsigned int r_type,
282 elfcpp::Elf_types<64>::Elf_Addr value,
283 unsigned char* view,
284 section_size_type view_size);
285
286 // Do a TLS Local-Dynamic to Local-Exec transition.
287 inline void
288 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
289 Output_segment* tls_segment,
290 const elfcpp::Rela<64, false>&, unsigned int r_type,
291 elfcpp::Elf_types<64>::Elf_Addr value,
292 unsigned char* view,
293 section_size_type view_size);
294
295 // Do a TLS Initial-Exec to Local-Exec transition.
296 static inline void
297 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
298 Output_segment* tls_segment,
299 const elfcpp::Rela<64, false>&, unsigned int r_type,
300 elfcpp::Elf_types<64>::Elf_Addr value,
301 unsigned char* view,
302 section_size_type view_size);
303
304 // This is set if we should skip the next reloc, which should be a
305 // PLT32 reloc against ___tls_get_addr.
306 bool skip_call_tls_get_addr_;
307
308 // This is set if we see a relocation which could load the address
309 // of the TLS block. Whether we see such a relocation determines
310 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
311 // in debugging sections.
312 bool saw_tls_block_reloc_;
313 };
314
315 // A class which returns the size required for a relocation type,
316 // used while scanning relocs during a relocatable link.
317 class Relocatable_size_for_reloc
318 {
319 public:
320 unsigned int
321 get_size_for_reloc(unsigned int, Relobj*);
322 };
323
324 // Adjust TLS relocation type based on the options and whether this
325 // is a local symbol.
326 static tls::Tls_optimization
327 optimize_tls_reloc(bool is_final, int r_type);
328
329 // Get the GOT section, creating it if necessary.
330 Output_data_got<64, false>*
331 got_section(Symbol_table*, Layout*);
332
333 // Get the GOT PLT section.
334 Output_data_space*
335 got_plt_section() const
336 {
337 gold_assert(this->got_plt_ != NULL);
338 return this->got_plt_;
339 }
340
341 // Create the PLT section.
342 void
343 make_plt_section(Symbol_table* symtab, Layout* layout);
344
345 // Create a PLT entry for a global symbol.
346 void
347 make_plt_entry(Symbol_table*, Layout*, Symbol*);
348
349 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
350 void
351 define_tls_base_symbol(Symbol_table*, Layout*);
352
353 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
354 void
355 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
356
357 // Create a GOT entry for the TLS module index.
358 unsigned int
359 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
360 Sized_relobj<64, false>* object);
361
362 // Get the PLT section.
363 Output_data_plt_x86_64*
364 plt_section() const
365 {
366 gold_assert(this->plt_ != NULL);
367 return this->plt_;
368 }
369
370 // Get the dynamic reloc section, creating it if necessary.
371 Reloc_section*
372 rela_dyn_section(Layout*);
373
374 // Return true if the symbol may need a COPY relocation.
375 // References from an executable object to non-function symbols
376 // defined in a dynamic object may need a COPY relocation.
377 bool
378 may_need_copy_reloc(Symbol* gsym)
379 {
380 return (!parameters->options().shared()
381 && gsym->is_from_dynobj()
382 && gsym->type() != elfcpp::STT_FUNC);
383 }
384
385 // Add a potential copy relocation.
386 void
387 copy_reloc(Symbol_table* symtab, Layout* layout,
388 Sized_relobj<64, false>* object,
389 unsigned int shndx, Output_section* output_section,
390 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
391 {
392 this->copy_relocs_.copy_reloc(symtab, layout,
393 symtab->get_sized_symbol<64>(sym),
394 object, shndx, output_section,
395 reloc, this->rela_dyn_section(layout));
396 }
397
398 // Information about this specific target which we pass to the
399 // general Target structure.
400 static const Target::Target_info x86_64_info;
401
402 enum Got_type
403 {
404 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
405 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
406 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
407 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
408 };
409
410 // The GOT section.
411 Output_data_got<64, false>* got_;
412 // The PLT section.
413 Output_data_plt_x86_64* plt_;
414 // The GOT PLT section.
415 Output_data_space* got_plt_;
416 // The dynamic reloc section.
417 Reloc_section* rela_dyn_;
418 // Relocs saved to avoid a COPY reloc.
419 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
420 // Space for variables copied with a COPY reloc.
421 Output_data_space* dynbss_;
422 // Offset of the GOT entry for the TLS module index.
423 unsigned int got_mod_index_offset_;
424 // True if the _TLS_MODULE_BASE_ symbol has been defined.
425 bool tls_base_symbol_defined_;
426 };
427
428 const Target::Target_info Target_x86_64::x86_64_info =
429 {
430 64, // size
431 false, // is_big_endian
432 elfcpp::EM_X86_64, // machine_code
433 false, // has_make_symbol
434 false, // has_resolve
435 true, // has_code_fill
436 true, // is_default_stack_executable
437 '\0', // wrap_char
438 "/lib/ld64.so.1", // program interpreter
439 0x400000, // default_text_segment_address
440 0x1000, // abi_pagesize (overridable by -z max-page-size)
441 0x1000 // common_pagesize (overridable by -z common-page-size)
442 };
443
444 // Get the GOT section, creating it if necessary.
445
446 Output_data_got<64, false>*
447 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
448 {
449 if (this->got_ == NULL)
450 {
451 gold_assert(symtab != NULL && layout != NULL);
452
453 this->got_ = new Output_data_got<64, false>();
454
455 Output_section* os;
456 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
457 (elfcpp::SHF_ALLOC
458 | elfcpp::SHF_WRITE),
459 this->got_);
460 os->set_is_relro();
461
462 // The old GNU linker creates a .got.plt section. We just
463 // create another set of data in the .got section. Note that we
464 // always create a PLT if we create a GOT, although the PLT
465 // might be empty.
466 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
467 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
468 (elfcpp::SHF_ALLOC
469 | elfcpp::SHF_WRITE),
470 this->got_plt_);
471 os->set_is_relro();
472
473 // The first three entries are reserved.
474 this->got_plt_->set_current_data_size(3 * 8);
475
476 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
477 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
478 this->got_plt_,
479 0, 0, elfcpp::STT_OBJECT,
480 elfcpp::STB_LOCAL,
481 elfcpp::STV_HIDDEN, 0,
482 false, false);
483 }
484
485 return this->got_;
486 }
487
488 // Get the dynamic reloc section, creating it if necessary.
489
490 Target_x86_64::Reloc_section*
491 Target_x86_64::rela_dyn_section(Layout* layout)
492 {
493 if (this->rela_dyn_ == NULL)
494 {
495 gold_assert(layout != NULL);
496 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
497 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
498 elfcpp::SHF_ALLOC, this->rela_dyn_);
499 }
500 return this->rela_dyn_;
501 }
502
503 // A class to handle the PLT data.
504
505 class Output_data_plt_x86_64 : public Output_section_data
506 {
507 public:
508 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
509
510 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
511 Output_data_space*);
512
513 // Add an entry to the PLT.
514 void
515 add_entry(Symbol* gsym);
516
517 // Add the reserved TLSDESC_PLT entry to the PLT.
518 void
519 reserve_tlsdesc_entry(unsigned int got_offset)
520 { this->tlsdesc_got_offset_ = got_offset; }
521
522 // Return true if a TLSDESC_PLT entry has been reserved.
523 bool
524 has_tlsdesc_entry() const
525 { return this->tlsdesc_got_offset_ != -1U; }
526
527 // Return the GOT offset for the reserved TLSDESC_PLT entry.
528 unsigned int
529 get_tlsdesc_got_offset() const
530 { return this->tlsdesc_got_offset_; }
531
532 // Return the offset of the reserved TLSDESC_PLT entry.
533 unsigned int
534 get_tlsdesc_plt_offset() const
535 { return (this->count_ + 1) * plt_entry_size; }
536
537 // Return the .rel.plt section data.
538 const Reloc_section*
539 rel_plt() const
540 { return this->rel_; }
541
542 protected:
543 void
544 do_adjust_output_section(Output_section* os);
545
546 // Write to a map file.
547 void
548 do_print_to_mapfile(Mapfile* mapfile) const
549 { mapfile->print_output_data(this, _("** PLT")); }
550
551 private:
552 // The size of an entry in the PLT.
553 static const int plt_entry_size = 16;
554
555 // The first entry in the PLT.
556 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
557 // procedure linkage table for both programs and shared objects."
558 static unsigned char first_plt_entry[plt_entry_size];
559
560 // Other entries in the PLT for an executable.
561 static unsigned char plt_entry[plt_entry_size];
562
563 // The reserved TLSDESC entry in the PLT for an executable.
564 static unsigned char tlsdesc_plt_entry[plt_entry_size];
565
566 // Set the final size.
567 void
568 set_final_data_size();
569
570 // Write out the PLT data.
571 void
572 do_write(Output_file*);
573
574 // The reloc section.
575 Reloc_section* rel_;
576 // The .got section.
577 Output_data_got<64, false>* got_;
578 // The .got.plt section.
579 Output_data_space* got_plt_;
580 // The number of PLT entries.
581 unsigned int count_;
582 // Offset of the reserved TLSDESC_GOT entry when needed.
583 unsigned int tlsdesc_got_offset_;
584 };
585
586 // Create the PLT section. The ordinary .got section is an argument,
587 // since we need to refer to the start. We also create our own .got
588 // section just for PLT entries.
589
590 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
591 Output_data_got<64, false>* got,
592 Output_data_space* got_plt)
593 : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0),
594 tlsdesc_got_offset_(-1U)
595 {
596 this->rel_ = new Reloc_section(false);
597 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
598 elfcpp::SHF_ALLOC, this->rel_);
599 }
600
601 void
602 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
603 {
604 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
605 // linker, and so do we.
606 os->set_entsize(4);
607 }
608
609 // Add an entry to the PLT.
610
611 void
612 Output_data_plt_x86_64::add_entry(Symbol* gsym)
613 {
614 gold_assert(!gsym->has_plt_offset());
615
616 // Note that when setting the PLT offset we skip the initial
617 // reserved PLT entry.
618 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
619
620 ++this->count_;
621
622 section_offset_type got_offset = this->got_plt_->current_data_size();
623
624 // Every PLT entry needs a GOT entry which points back to the PLT
625 // entry (this will be changed by the dynamic linker, normally
626 // lazily when the function is called).
627 this->got_plt_->set_current_data_size(got_offset + 8);
628
629 // Every PLT entry needs a reloc.
630 gsym->set_needs_dynsym_entry();
631 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
632 got_offset, 0);
633
634 // Note that we don't need to save the symbol. The contents of the
635 // PLT are independent of which symbols are used. The symbols only
636 // appear in the relocations.
637 }
638
639 // Set the final size.
640 void
641 Output_data_plt_x86_64::set_final_data_size()
642 {
643 unsigned int count = this->count_;
644 if (this->has_tlsdesc_entry())
645 ++count;
646 this->set_data_size((count + 1) * plt_entry_size);
647 }
648
649 // The first entry in the PLT for an executable.
650
651 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
652 {
653 // From AMD64 ABI Draft 0.98, page 76
654 0xff, 0x35, // pushq contents of memory address
655 0, 0, 0, 0, // replaced with address of .got + 8
656 0xff, 0x25, // jmp indirect
657 0, 0, 0, 0, // replaced with address of .got + 16
658 0x90, 0x90, 0x90, 0x90 // noop (x4)
659 };
660
661 // Subsequent entries in the PLT for an executable.
662
663 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
664 {
665 // From AMD64 ABI Draft 0.98, page 76
666 0xff, 0x25, // jmpq indirect
667 0, 0, 0, 0, // replaced with address of symbol in .got
668 0x68, // pushq immediate
669 0, 0, 0, 0, // replaced with offset into relocation table
670 0xe9, // jmpq relative
671 0, 0, 0, 0 // replaced with offset to start of .plt
672 };
673
674 // The reserved TLSDESC entry in the PLT for an executable.
675
676 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
677 {
678 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
679 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
680 0xff, 0x35, // pushq x(%rip)
681 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
682 0xff, 0x25, // jmpq *y(%rip)
683 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
684 0x0f, 0x1f, // nop
685 0x40, 0
686 };
687
688 // Write out the PLT. This uses the hand-coded instructions above,
689 // and adjusts them as needed. This is specified by the AMD64 ABI.
690
691 void
692 Output_data_plt_x86_64::do_write(Output_file* of)
693 {
694 const off_t offset = this->offset();
695 const section_size_type oview_size =
696 convert_to_section_size_type(this->data_size());
697 unsigned char* const oview = of->get_output_view(offset, oview_size);
698
699 const off_t got_file_offset = this->got_plt_->offset();
700 const section_size_type got_size =
701 convert_to_section_size_type(this->got_plt_->data_size());
702 unsigned char* const got_view = of->get_output_view(got_file_offset,
703 got_size);
704
705 unsigned char* pov = oview;
706
707 // The base address of the .plt section.
708 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
709 // The base address of the .got section.
710 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
711 // The base address of the PLT portion of the .got section,
712 // which is where the GOT pointer will point, and where the
713 // three reserved GOT entries are located.
714 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
715
716 memcpy(pov, first_plt_entry, plt_entry_size);
717 // We do a jmp relative to the PC at the end of this instruction.
718 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
719 (got_address + 8
720 - (plt_address + 6)));
721 elfcpp::Swap<32, false>::writeval(pov + 8,
722 (got_address + 16
723 - (plt_address + 12)));
724 pov += plt_entry_size;
725
726 unsigned char* got_pov = got_view;
727
728 memset(got_pov, 0, 24);
729 got_pov += 24;
730
731 unsigned int plt_offset = plt_entry_size;
732 unsigned int got_offset = 24;
733 const unsigned int count = this->count_;
734 for (unsigned int plt_index = 0;
735 plt_index < count;
736 ++plt_index,
737 pov += plt_entry_size,
738 got_pov += 8,
739 plt_offset += plt_entry_size,
740 got_offset += 8)
741 {
742 // Set and adjust the PLT entry itself.
743 memcpy(pov, plt_entry, plt_entry_size);
744 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
745 (got_address + got_offset
746 - (plt_address + plt_offset
747 + 6)));
748
749 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
750 elfcpp::Swap<32, false>::writeval(pov + 12,
751 - (plt_offset + plt_entry_size));
752
753 // Set the entry in the GOT.
754 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
755 }
756
757 if (this->has_tlsdesc_entry())
758 {
759 // Set and adjust the reserved TLSDESC PLT entry.
760 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
761 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
762 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
763 (got_address + 8
764 - (plt_address + plt_offset
765 + 6)));
766 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
767 (got_base
768 + tlsdesc_got_offset
769 - (plt_address + plt_offset
770 + 12)));
771 pov += plt_entry_size;
772 }
773
774 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
775 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
776
777 of->write_output_view(offset, oview_size, oview);
778 of->write_output_view(got_file_offset, got_size, got_view);
779 }
780
781 // Create the PLT section.
782
783 void
784 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
785 {
786 if (this->plt_ == NULL)
787 {
788 // Create the GOT sections first.
789 this->got_section(symtab, layout);
790
791 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
792 this->got_plt_);
793 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
794 (elfcpp::SHF_ALLOC
795 | elfcpp::SHF_EXECINSTR),
796 this->plt_);
797 }
798 }
799
800 // Create a PLT entry for a global symbol.
801
802 void
803 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
804 Symbol* gsym)
805 {
806 if (gsym->has_plt_offset())
807 return;
808
809 if (this->plt_ == NULL)
810 this->make_plt_section(symtab, layout);
811
812 this->plt_->add_entry(gsym);
813 }
814
815 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
816
817 void
818 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
819 {
820 if (this->tls_base_symbol_defined_)
821 return;
822
823 Output_segment* tls_segment = layout->tls_segment();
824 if (tls_segment != NULL)
825 {
826 bool is_exec = parameters->options().output_is_executable();
827 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
828 tls_segment, 0, 0,
829 elfcpp::STT_TLS,
830 elfcpp::STB_LOCAL,
831 elfcpp::STV_HIDDEN, 0,
832 (is_exec
833 ? Symbol::SEGMENT_END
834 : Symbol::SEGMENT_START),
835 true);
836 }
837 this->tls_base_symbol_defined_ = true;
838 }
839
840 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
841
842 void
843 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
844 Layout* layout)
845 {
846 if (this->plt_ == NULL)
847 this->make_plt_section(symtab, layout);
848
849 if (!this->plt_->has_tlsdesc_entry())
850 {
851 // Allocate the TLSDESC_GOT entry.
852 Output_data_got<64, false>* got = this->got_section(symtab, layout);
853 unsigned int got_offset = got->add_constant(0);
854
855 // Allocate the TLSDESC_PLT entry.
856 this->plt_->reserve_tlsdesc_entry(got_offset);
857 }
858 }
859
860 // Create a GOT entry for the TLS module index.
861
862 unsigned int
863 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
864 Sized_relobj<64, false>* object)
865 {
866 if (this->got_mod_index_offset_ == -1U)
867 {
868 gold_assert(symtab != NULL && layout != NULL && object != NULL);
869 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
870 Output_data_got<64, false>* got = this->got_section(symtab, layout);
871 unsigned int got_offset = got->add_constant(0);
872 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
873 got_offset, 0);
874 got->add_constant(0);
875 this->got_mod_index_offset_ = got_offset;
876 }
877 return this->got_mod_index_offset_;
878 }
879
880 // Optimize the TLS relocation type based on what we know about the
881 // symbol. IS_FINAL is true if the final address of this symbol is
882 // known at link time.
883
884 tls::Tls_optimization
885 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
886 {
887 // If we are generating a shared library, then we can't do anything
888 // in the linker.
889 if (parameters->options().shared())
890 return tls::TLSOPT_NONE;
891
892 switch (r_type)
893 {
894 case elfcpp::R_X86_64_TLSGD:
895 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
896 case elfcpp::R_X86_64_TLSDESC_CALL:
897 // These are General-Dynamic which permits fully general TLS
898 // access. Since we know that we are generating an executable,
899 // we can convert this to Initial-Exec. If we also know that
900 // this is a local symbol, we can further switch to Local-Exec.
901 if (is_final)
902 return tls::TLSOPT_TO_LE;
903 return tls::TLSOPT_TO_IE;
904
905 case elfcpp::R_X86_64_TLSLD:
906 // This is Local-Dynamic, which refers to a local symbol in the
907 // dynamic TLS block. Since we know that we generating an
908 // executable, we can switch to Local-Exec.
909 return tls::TLSOPT_TO_LE;
910
911 case elfcpp::R_X86_64_DTPOFF32:
912 case elfcpp::R_X86_64_DTPOFF64:
913 // Another Local-Dynamic reloc.
914 return tls::TLSOPT_TO_LE;
915
916 case elfcpp::R_X86_64_GOTTPOFF:
917 // These are Initial-Exec relocs which get the thread offset
918 // from the GOT. If we know that we are linking against the
919 // local symbol, we can switch to Local-Exec, which links the
920 // thread offset into the instruction.
921 if (is_final)
922 return tls::TLSOPT_TO_LE;
923 return tls::TLSOPT_NONE;
924
925 case elfcpp::R_X86_64_TPOFF32:
926 // When we already have Local-Exec, there is nothing further we
927 // can do.
928 return tls::TLSOPT_NONE;
929
930 default:
931 gold_unreachable();
932 }
933 }
934
935 // Report an unsupported relocation against a local symbol.
936
937 void
938 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
939 unsigned int r_type)
940 {
941 gold_error(_("%s: unsupported reloc %u against local symbol"),
942 object->name().c_str(), r_type);
943 }
944
945 // We are about to emit a dynamic relocation of type R_TYPE. If the
946 // dynamic linker does not support it, issue an error. The GNU linker
947 // only issues a non-PIC error for an allocated read-only section.
948 // Here we know the section is allocated, but we don't know that it is
949 // read-only. But we check for all the relocation types which the
950 // glibc dynamic linker supports, so it seems appropriate to issue an
951 // error even if the section is not read-only.
952
953 void
954 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
955 {
956 switch (r_type)
957 {
958 // These are the relocation types supported by glibc for x86_64.
959 case elfcpp::R_X86_64_RELATIVE:
960 case elfcpp::R_X86_64_GLOB_DAT:
961 case elfcpp::R_X86_64_JUMP_SLOT:
962 case elfcpp::R_X86_64_DTPMOD64:
963 case elfcpp::R_X86_64_DTPOFF64:
964 case elfcpp::R_X86_64_TPOFF64:
965 case elfcpp::R_X86_64_64:
966 case elfcpp::R_X86_64_32:
967 case elfcpp::R_X86_64_PC32:
968 case elfcpp::R_X86_64_COPY:
969 return;
970
971 default:
972 // This prevents us from issuing more than one error per reloc
973 // section. But we can still wind up issuing more than one
974 // error per object file.
975 if (this->issued_non_pic_error_)
976 return;
977 gold_assert(parameters->options().output_is_position_independent());
978 object->error(_("requires unsupported dynamic reloc; "
979 "recompile with -fPIC"));
980 this->issued_non_pic_error_ = true;
981 return;
982
983 case elfcpp::R_X86_64_NONE:
984 gold_unreachable();
985 }
986 }
987
988 // Scan a relocation for a local symbol.
989
990 inline void
991 Target_x86_64::Scan::local(const General_options&,
992 Symbol_table* symtab,
993 Layout* layout,
994 Target_x86_64* target,
995 Sized_relobj<64, false>* object,
996 unsigned int data_shndx,
997 Output_section* output_section,
998 const elfcpp::Rela<64, false>& reloc,
999 unsigned int r_type,
1000 const elfcpp::Sym<64, false>& lsym)
1001 {
1002 switch (r_type)
1003 {
1004 case elfcpp::R_X86_64_NONE:
1005 case elfcpp::R_386_GNU_VTINHERIT:
1006 case elfcpp::R_386_GNU_VTENTRY:
1007 break;
1008
1009 case elfcpp::R_X86_64_64:
1010 // If building a shared library (or a position-independent
1011 // executable), we need to create a dynamic relocation for this
1012 // location. The relocation applied at link time will apply the
1013 // link-time value, so we flag the location with an
1014 // R_X86_64_RELATIVE relocation so the dynamic loader can
1015 // relocate it easily.
1016 if (parameters->options().output_is_position_independent())
1017 {
1018 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1019 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1020 rela_dyn->add_local_relative(object, r_sym,
1021 elfcpp::R_X86_64_RELATIVE,
1022 output_section, data_shndx,
1023 reloc.get_r_offset(),
1024 reloc.get_r_addend());
1025 }
1026 break;
1027
1028 case elfcpp::R_X86_64_32:
1029 case elfcpp::R_X86_64_32S:
1030 case elfcpp::R_X86_64_16:
1031 case elfcpp::R_X86_64_8:
1032 // If building a shared library (or a position-independent
1033 // executable), we need to create a dynamic relocation for this
1034 // location. We can't use an R_X86_64_RELATIVE relocation
1035 // because that is always a 64-bit relocation.
1036 if (parameters->options().output_is_position_independent())
1037 {
1038 this->check_non_pic(object, r_type);
1039
1040 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1041 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1042 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1043 rela_dyn->add_local(object, r_sym, r_type, output_section,
1044 data_shndx, reloc.get_r_offset(),
1045 reloc.get_r_addend());
1046 else
1047 {
1048 gold_assert(lsym.get_st_value() == 0);
1049 unsigned int shndx = lsym.get_st_shndx();
1050 bool is_ordinary;
1051 shndx = object->adjust_sym_shndx(r_sym, shndx,
1052 &is_ordinary);
1053 if (!is_ordinary)
1054 object->error(_("section symbol %u has bad shndx %u"),
1055 r_sym, shndx);
1056 else
1057 rela_dyn->add_local_section(object, shndx,
1058 r_type, output_section,
1059 data_shndx, reloc.get_r_offset(),
1060 reloc.get_r_addend());
1061 }
1062 }
1063 break;
1064
1065 case elfcpp::R_X86_64_PC64:
1066 case elfcpp::R_X86_64_PC32:
1067 case elfcpp::R_X86_64_PC16:
1068 case elfcpp::R_X86_64_PC8:
1069 break;
1070
1071 case elfcpp::R_X86_64_PLT32:
1072 // Since we know this is a local symbol, we can handle this as a
1073 // PC32 reloc.
1074 break;
1075
1076 case elfcpp::R_X86_64_GOTPC32:
1077 case elfcpp::R_X86_64_GOTOFF64:
1078 case elfcpp::R_X86_64_GOTPC64:
1079 case elfcpp::R_X86_64_PLTOFF64:
1080 // We need a GOT section.
1081 target->got_section(symtab, layout);
1082 // For PLTOFF64, we'd normally want a PLT section, but since we
1083 // know this is a local symbol, no PLT is needed.
1084 break;
1085
1086 case elfcpp::R_X86_64_GOT64:
1087 case elfcpp::R_X86_64_GOT32:
1088 case elfcpp::R_X86_64_GOTPCREL64:
1089 case elfcpp::R_X86_64_GOTPCREL:
1090 case elfcpp::R_X86_64_GOTPLT64:
1091 {
1092 // The symbol requires a GOT entry.
1093 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1094 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1095 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1096 {
1097 // If we are generating a shared object, we need to add a
1098 // dynamic relocation for this symbol's GOT entry.
1099 if (parameters->options().output_is_position_independent())
1100 {
1101 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1102 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1103 if (r_type != elfcpp::R_X86_64_GOT32)
1104 rela_dyn->add_local_relative(
1105 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1106 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1107 else
1108 {
1109 this->check_non_pic(object, r_type);
1110
1111 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1112 rela_dyn->add_local(
1113 object, r_sym, r_type, got,
1114 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1115 }
1116 }
1117 }
1118 // For GOTPLT64, we'd normally want a PLT section, but since
1119 // we know this is a local symbol, no PLT is needed.
1120 }
1121 break;
1122
1123 case elfcpp::R_X86_64_COPY:
1124 case elfcpp::R_X86_64_GLOB_DAT:
1125 case elfcpp::R_X86_64_JUMP_SLOT:
1126 case elfcpp::R_X86_64_RELATIVE:
1127 // These are outstanding tls relocs, which are unexpected when linking
1128 case elfcpp::R_X86_64_TPOFF64:
1129 case elfcpp::R_X86_64_DTPMOD64:
1130 case elfcpp::R_X86_64_TLSDESC:
1131 gold_error(_("%s: unexpected reloc %u in object file"),
1132 object->name().c_str(), r_type);
1133 break;
1134
1135 // These are initial tls relocs, which are expected when linking
1136 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1137 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1138 case elfcpp::R_X86_64_TLSDESC_CALL:
1139 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1140 case elfcpp::R_X86_64_DTPOFF32:
1141 case elfcpp::R_X86_64_DTPOFF64:
1142 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1143 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1144 {
1145 bool output_is_shared = parameters->options().shared();
1146 const tls::Tls_optimization optimized_type
1147 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1148 switch (r_type)
1149 {
1150 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1151 if (optimized_type == tls::TLSOPT_NONE)
1152 {
1153 // Create a pair of GOT entries for the module index and
1154 // dtv-relative offset.
1155 Output_data_got<64, false>* got
1156 = target->got_section(symtab, layout);
1157 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1158 unsigned int shndx = lsym.get_st_shndx();
1159 bool is_ordinary;
1160 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1161 if (!is_ordinary)
1162 object->error(_("local symbol %u has bad shndx %u"),
1163 r_sym, shndx);
1164 else
1165 got->add_local_pair_with_rela(object, r_sym,
1166 shndx,
1167 GOT_TYPE_TLS_PAIR,
1168 target->rela_dyn_section(layout),
1169 elfcpp::R_X86_64_DTPMOD64, 0);
1170 }
1171 else if (optimized_type != tls::TLSOPT_TO_LE)
1172 unsupported_reloc_local(object, r_type);
1173 break;
1174
1175 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1176 target->define_tls_base_symbol(symtab, layout);
1177 if (optimized_type == tls::TLSOPT_NONE)
1178 {
1179 // Create reserved PLT and GOT entries for the resolver.
1180 target->reserve_tlsdesc_entries(symtab, layout);
1181
1182 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1183 Output_data_got<64, false>* got
1184 = target->got_section(symtab, layout);
1185 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1186 unsigned int shndx = lsym.get_st_shndx();
1187 bool is_ordinary;
1188 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1189 if (!is_ordinary)
1190 object->error(_("local symbol %u has bad shndx %u"),
1191 r_sym, shndx);
1192 else
1193 got->add_local_pair_with_rela(object, r_sym,
1194 shndx,
1195 GOT_TYPE_TLS_DESC,
1196 target->rela_dyn_section(layout),
1197 elfcpp::R_X86_64_TLSDESC, 0);
1198 }
1199 else if (optimized_type != tls::TLSOPT_TO_LE)
1200 unsupported_reloc_local(object, r_type);
1201 break;
1202
1203 case elfcpp::R_X86_64_TLSDESC_CALL:
1204 break;
1205
1206 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1207 if (optimized_type == tls::TLSOPT_NONE)
1208 {
1209 // Create a GOT entry for the module index.
1210 target->got_mod_index_entry(symtab, layout, object);
1211 }
1212 else if (optimized_type != tls::TLSOPT_TO_LE)
1213 unsupported_reloc_local(object, r_type);
1214 break;
1215
1216 case elfcpp::R_X86_64_DTPOFF32:
1217 case elfcpp::R_X86_64_DTPOFF64:
1218 break;
1219
1220 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1221 layout->set_has_static_tls();
1222 if (optimized_type == tls::TLSOPT_NONE)
1223 {
1224 // Create a GOT entry for the tp-relative offset.
1225 Output_data_got<64, false>* got
1226 = target->got_section(symtab, layout);
1227 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1228 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1229 target->rela_dyn_section(layout),
1230 elfcpp::R_X86_64_TPOFF64);
1231 }
1232 else if (optimized_type != tls::TLSOPT_TO_LE)
1233 unsupported_reloc_local(object, r_type);
1234 break;
1235
1236 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1237 layout->set_has_static_tls();
1238 if (output_is_shared)
1239 unsupported_reloc_local(object, r_type);
1240 break;
1241
1242 default:
1243 gold_unreachable();
1244 }
1245 }
1246 break;
1247
1248 case elfcpp::R_X86_64_SIZE32:
1249 case elfcpp::R_X86_64_SIZE64:
1250 default:
1251 gold_error(_("%s: unsupported reloc %u against local symbol"),
1252 object->name().c_str(), r_type);
1253 break;
1254 }
1255 }
1256
1257
1258 // Report an unsupported relocation against a global symbol.
1259
1260 void
1261 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1262 unsigned int r_type,
1263 Symbol* gsym)
1264 {
1265 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1266 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1267 }
1268
1269 // Scan a relocation for a global symbol.
1270
1271 inline void
1272 Target_x86_64::Scan::global(const General_options&,
1273 Symbol_table* symtab,
1274 Layout* layout,
1275 Target_x86_64* target,
1276 Sized_relobj<64, false>* object,
1277 unsigned int data_shndx,
1278 Output_section* output_section,
1279 const elfcpp::Rela<64, false>& reloc,
1280 unsigned int r_type,
1281 Symbol* gsym)
1282 {
1283 switch (r_type)
1284 {
1285 case elfcpp::R_X86_64_NONE:
1286 case elfcpp::R_386_GNU_VTINHERIT:
1287 case elfcpp::R_386_GNU_VTENTRY:
1288 break;
1289
1290 case elfcpp::R_X86_64_64:
1291 case elfcpp::R_X86_64_32:
1292 case elfcpp::R_X86_64_32S:
1293 case elfcpp::R_X86_64_16:
1294 case elfcpp::R_X86_64_8:
1295 {
1296 // Make a PLT entry if necessary.
1297 if (gsym->needs_plt_entry())
1298 {
1299 target->make_plt_entry(symtab, layout, gsym);
1300 // Since this is not a PC-relative relocation, we may be
1301 // taking the address of a function. In that case we need to
1302 // set the entry in the dynamic symbol table to the address of
1303 // the PLT entry.
1304 if (gsym->is_from_dynobj() && !parameters->options().shared())
1305 gsym->set_needs_dynsym_value();
1306 }
1307 // Make a dynamic relocation if necessary.
1308 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1309 {
1310 if (target->may_need_copy_reloc(gsym))
1311 {
1312 target->copy_reloc(symtab, layout, object,
1313 data_shndx, output_section, gsym, reloc);
1314 }
1315 else if (r_type == elfcpp::R_X86_64_64
1316 && gsym->can_use_relative_reloc(false))
1317 {
1318 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1319 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1320 output_section, object,
1321 data_shndx, reloc.get_r_offset(),
1322 reloc.get_r_addend());
1323 }
1324 else
1325 {
1326 this->check_non_pic(object, r_type);
1327 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1328 rela_dyn->add_global(gsym, r_type, output_section, object,
1329 data_shndx, reloc.get_r_offset(),
1330 reloc.get_r_addend());
1331 }
1332 }
1333 }
1334 break;
1335
1336 case elfcpp::R_X86_64_PC64:
1337 case elfcpp::R_X86_64_PC32:
1338 case elfcpp::R_X86_64_PC16:
1339 case elfcpp::R_X86_64_PC8:
1340 {
1341 // Make a PLT entry if necessary.
1342 if (gsym->needs_plt_entry())
1343 target->make_plt_entry(symtab, layout, gsym);
1344 // Make a dynamic relocation if necessary.
1345 int flags = Symbol::NON_PIC_REF;
1346 if (gsym->type() == elfcpp::STT_FUNC)
1347 flags |= Symbol::FUNCTION_CALL;
1348 if (gsym->needs_dynamic_reloc(flags))
1349 {
1350 if (target->may_need_copy_reloc(gsym))
1351 {
1352 target->copy_reloc(symtab, layout, object,
1353 data_shndx, output_section, gsym, reloc);
1354 }
1355 else
1356 {
1357 this->check_non_pic(object, r_type);
1358 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1359 rela_dyn->add_global(gsym, r_type, output_section, object,
1360 data_shndx, reloc.get_r_offset(),
1361 reloc.get_r_addend());
1362 }
1363 }
1364 }
1365 break;
1366
1367 case elfcpp::R_X86_64_GOT64:
1368 case elfcpp::R_X86_64_GOT32:
1369 case elfcpp::R_X86_64_GOTPCREL64:
1370 case elfcpp::R_X86_64_GOTPCREL:
1371 case elfcpp::R_X86_64_GOTPLT64:
1372 {
1373 // The symbol requires a GOT entry.
1374 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1375 if (gsym->final_value_is_known())
1376 got->add_global(gsym, GOT_TYPE_STANDARD);
1377 else
1378 {
1379 // If this symbol is not fully resolved, we need to add a
1380 // dynamic relocation for it.
1381 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1382 if (gsym->is_from_dynobj()
1383 || gsym->is_undefined()
1384 || gsym->is_preemptible())
1385 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1386 elfcpp::R_X86_64_GLOB_DAT);
1387 else
1388 {
1389 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1390 rela_dyn->add_global_relative(
1391 gsym, elfcpp::R_X86_64_RELATIVE, got,
1392 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1393 }
1394 }
1395 // For GOTPLT64, we also need a PLT entry (but only if the
1396 // symbol is not fully resolved).
1397 if (r_type == elfcpp::R_X86_64_GOTPLT64
1398 && !gsym->final_value_is_known())
1399 target->make_plt_entry(symtab, layout, gsym);
1400 }
1401 break;
1402
1403 case elfcpp::R_X86_64_PLT32:
1404 // If the symbol is fully resolved, this is just a PC32 reloc.
1405 // Otherwise we need a PLT entry.
1406 if (gsym->final_value_is_known())
1407 break;
1408 // If building a shared library, we can also skip the PLT entry
1409 // if the symbol is defined in the output file and is protected
1410 // or hidden.
1411 if (gsym->is_defined()
1412 && !gsym->is_from_dynobj()
1413 && !gsym->is_preemptible())
1414 break;
1415 target->make_plt_entry(symtab, layout, gsym);
1416 break;
1417
1418 case elfcpp::R_X86_64_GOTPC32:
1419 case elfcpp::R_X86_64_GOTOFF64:
1420 case elfcpp::R_X86_64_GOTPC64:
1421 case elfcpp::R_X86_64_PLTOFF64:
1422 // We need a GOT section.
1423 target->got_section(symtab, layout);
1424 // For PLTOFF64, we also need a PLT entry (but only if the
1425 // symbol is not fully resolved).
1426 if (r_type == elfcpp::R_X86_64_PLTOFF64
1427 && !gsym->final_value_is_known())
1428 target->make_plt_entry(symtab, layout, gsym);
1429 break;
1430
1431 case elfcpp::R_X86_64_COPY:
1432 case elfcpp::R_X86_64_GLOB_DAT:
1433 case elfcpp::R_X86_64_JUMP_SLOT:
1434 case elfcpp::R_X86_64_RELATIVE:
1435 // These are outstanding tls relocs, which are unexpected when linking
1436 case elfcpp::R_X86_64_TPOFF64:
1437 case elfcpp::R_X86_64_DTPMOD64:
1438 case elfcpp::R_X86_64_TLSDESC:
1439 gold_error(_("%s: unexpected reloc %u in object file"),
1440 object->name().c_str(), r_type);
1441 break;
1442
1443 // These are initial tls relocs, which are expected for global()
1444 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1445 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1446 case elfcpp::R_X86_64_TLSDESC_CALL:
1447 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1448 case elfcpp::R_X86_64_DTPOFF32:
1449 case elfcpp::R_X86_64_DTPOFF64:
1450 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1451 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1452 {
1453 const bool is_final = gsym->final_value_is_known();
1454 const tls::Tls_optimization optimized_type
1455 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1456 switch (r_type)
1457 {
1458 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1459 if (optimized_type == tls::TLSOPT_NONE)
1460 {
1461 // Create a pair of GOT entries for the module index and
1462 // dtv-relative offset.
1463 Output_data_got<64, false>* got
1464 = target->got_section(symtab, layout);
1465 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1466 target->rela_dyn_section(layout),
1467 elfcpp::R_X86_64_DTPMOD64,
1468 elfcpp::R_X86_64_DTPOFF64);
1469 }
1470 else if (optimized_type == tls::TLSOPT_TO_IE)
1471 {
1472 // Create a GOT entry for the tp-relative offset.
1473 Output_data_got<64, false>* got
1474 = target->got_section(symtab, layout);
1475 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1476 target->rela_dyn_section(layout),
1477 elfcpp::R_X86_64_TPOFF64);
1478 }
1479 else if (optimized_type != tls::TLSOPT_TO_LE)
1480 unsupported_reloc_global(object, r_type, gsym);
1481 break;
1482
1483 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1484 target->define_tls_base_symbol(symtab, layout);
1485 if (optimized_type == tls::TLSOPT_NONE)
1486 {
1487 // Create reserved PLT and GOT entries for the resolver.
1488 target->reserve_tlsdesc_entries(symtab, layout);
1489
1490 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1491 Output_data_got<64, false>* got
1492 = target->got_section(symtab, layout);
1493 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1494 target->rela_dyn_section(layout),
1495 elfcpp::R_X86_64_TLSDESC, 0);
1496 }
1497 else if (optimized_type == tls::TLSOPT_TO_IE)
1498 {
1499 // Create a GOT entry for the tp-relative offset.
1500 Output_data_got<64, false>* got
1501 = target->got_section(symtab, layout);
1502 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1503 target->rela_dyn_section(layout),
1504 elfcpp::R_X86_64_TPOFF64);
1505 }
1506 else if (optimized_type != tls::TLSOPT_TO_LE)
1507 unsupported_reloc_global(object, r_type, gsym);
1508 break;
1509
1510 case elfcpp::R_X86_64_TLSDESC_CALL:
1511 break;
1512
1513 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1514 if (optimized_type == tls::TLSOPT_NONE)
1515 {
1516 // Create a GOT entry for the module index.
1517 target->got_mod_index_entry(symtab, layout, object);
1518 }
1519 else if (optimized_type != tls::TLSOPT_TO_LE)
1520 unsupported_reloc_global(object, r_type, gsym);
1521 break;
1522
1523 case elfcpp::R_X86_64_DTPOFF32:
1524 case elfcpp::R_X86_64_DTPOFF64:
1525 break;
1526
1527 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1528 layout->set_has_static_tls();
1529 if (optimized_type == tls::TLSOPT_NONE)
1530 {
1531 // Create a GOT entry for the tp-relative offset.
1532 Output_data_got<64, false>* got
1533 = target->got_section(symtab, layout);
1534 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1535 target->rela_dyn_section(layout),
1536 elfcpp::R_X86_64_TPOFF64);
1537 }
1538 else if (optimized_type != tls::TLSOPT_TO_LE)
1539 unsupported_reloc_global(object, r_type, gsym);
1540 break;
1541
1542 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1543 layout->set_has_static_tls();
1544 if (parameters->options().shared())
1545 unsupported_reloc_local(object, r_type);
1546 break;
1547
1548 default:
1549 gold_unreachable();
1550 }
1551 }
1552 break;
1553
1554 case elfcpp::R_X86_64_SIZE32:
1555 case elfcpp::R_X86_64_SIZE64:
1556 default:
1557 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1558 object->name().c_str(), r_type,
1559 gsym->demangled_name().c_str());
1560 break;
1561 }
1562 }
1563
1564 void
1565 Target_x86_64::gc_process_relocs(const General_options& options,
1566 Symbol_table* symtab,
1567 Layout* layout,
1568 Sized_relobj<64, false>* object,
1569 unsigned int data_shndx,
1570 unsigned int sh_type,
1571 const unsigned char* prelocs,
1572 size_t reloc_count,
1573 Output_section* output_section,
1574 bool needs_special_offset_handling,
1575 size_t local_symbol_count,
1576 const unsigned char* plocal_symbols)
1577 {
1578
1579 if (sh_type == elfcpp::SHT_REL)
1580 {
1581 return;
1582 }
1583
1584 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1585 Target_x86_64::Scan>(
1586 options,
1587 symtab,
1588 layout,
1589 this,
1590 object,
1591 data_shndx,
1592 prelocs,
1593 reloc_count,
1594 output_section,
1595 needs_special_offset_handling,
1596 local_symbol_count,
1597 plocal_symbols);
1598
1599 }
1600 // Scan relocations for a section.
1601
1602 void
1603 Target_x86_64::scan_relocs(const General_options& options,
1604 Symbol_table* symtab,
1605 Layout* layout,
1606 Sized_relobj<64, false>* object,
1607 unsigned int data_shndx,
1608 unsigned int sh_type,
1609 const unsigned char* prelocs,
1610 size_t reloc_count,
1611 Output_section* output_section,
1612 bool needs_special_offset_handling,
1613 size_t local_symbol_count,
1614 const unsigned char* plocal_symbols)
1615 {
1616 if (sh_type == elfcpp::SHT_REL)
1617 {
1618 gold_error(_("%s: unsupported REL reloc section"),
1619 object->name().c_str());
1620 return;
1621 }
1622
1623 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1624 Target_x86_64::Scan>(
1625 options,
1626 symtab,
1627 layout,
1628 this,
1629 object,
1630 data_shndx,
1631 prelocs,
1632 reloc_count,
1633 output_section,
1634 needs_special_offset_handling,
1635 local_symbol_count,
1636 plocal_symbols);
1637 }
1638
1639 // Finalize the sections.
1640
1641 void
1642 Target_x86_64::do_finalize_sections(Layout* layout)
1643 {
1644 // Fill in some more dynamic tags.
1645 Output_data_dynamic* const odyn = layout->dynamic_data();
1646 if (odyn != NULL)
1647 {
1648 if (this->got_plt_ != NULL)
1649 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1650
1651 if (this->plt_ != NULL)
1652 {
1653 const Output_data* od = this->plt_->rel_plt();
1654 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1655 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1656 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1657 if (this->plt_->has_tlsdesc_entry())
1658 {
1659 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1660 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1661 this->got_->finalize_data_size();
1662 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1663 this->plt_, plt_offset);
1664 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1665 this->got_, got_offset);
1666 }
1667 }
1668
1669 if (this->rela_dyn_ != NULL)
1670 {
1671 const Output_data* od = this->rela_dyn_;
1672 odyn->add_section_address(elfcpp::DT_RELA, od);
1673 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1674 odyn->add_constant(elfcpp::DT_RELAENT,
1675 elfcpp::Elf_sizes<64>::rela_size);
1676 }
1677
1678 if (!parameters->options().shared())
1679 {
1680 // The value of the DT_DEBUG tag is filled in by the dynamic
1681 // linker at run time, and used by the debugger.
1682 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1683 }
1684 }
1685
1686 // Emit any relocs we saved in an attempt to avoid generating COPY
1687 // relocs.
1688 if (this->copy_relocs_.any_saved_relocs())
1689 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1690 }
1691
1692 // Perform a relocation.
1693
1694 inline bool
1695 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1696 Target_x86_64* target,
1697 Output_section*,
1698 size_t relnum,
1699 const elfcpp::Rela<64, false>& rela,
1700 unsigned int r_type,
1701 const Sized_symbol<64>* gsym,
1702 const Symbol_value<64>* psymval,
1703 unsigned char* view,
1704 elfcpp::Elf_types<64>::Elf_Addr address,
1705 section_size_type view_size)
1706 {
1707 if (this->skip_call_tls_get_addr_)
1708 {
1709 if ((r_type != elfcpp::R_X86_64_PLT32
1710 && r_type != elfcpp::R_X86_64_PC32)
1711 || gsym == NULL
1712 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1713 {
1714 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1715 _("missing expected TLS relocation"));
1716 }
1717 else
1718 {
1719 this->skip_call_tls_get_addr_ = false;
1720 return false;
1721 }
1722 }
1723
1724 // Pick the value to use for symbols defined in shared objects.
1725 Symbol_value<64> symval;
1726 if (gsym != NULL
1727 && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
1728 || r_type == elfcpp::R_X86_64_PC32
1729 || r_type == elfcpp::R_X86_64_PC16
1730 || r_type == elfcpp::R_X86_64_PC8))
1731 {
1732 symval.set_output_value(target->plt_section()->address()
1733 + gsym->plt_offset());
1734 psymval = &symval;
1735 }
1736
1737 const Sized_relobj<64, false>* object = relinfo->object;
1738 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1739
1740 // Get the GOT offset if needed.
1741 // The GOT pointer points to the end of the GOT section.
1742 // We need to subtract the size of the GOT section to get
1743 // the actual offset to use in the relocation.
1744 bool have_got_offset = false;
1745 unsigned int got_offset = 0;
1746 switch (r_type)
1747 {
1748 case elfcpp::R_X86_64_GOT32:
1749 case elfcpp::R_X86_64_GOT64:
1750 case elfcpp::R_X86_64_GOTPLT64:
1751 case elfcpp::R_X86_64_GOTPCREL:
1752 case elfcpp::R_X86_64_GOTPCREL64:
1753 if (gsym != NULL)
1754 {
1755 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1756 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1757 }
1758 else
1759 {
1760 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1761 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1762 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1763 - target->got_size());
1764 }
1765 have_got_offset = true;
1766 break;
1767
1768 default:
1769 break;
1770 }
1771
1772 switch (r_type)
1773 {
1774 case elfcpp::R_X86_64_NONE:
1775 case elfcpp::R_386_GNU_VTINHERIT:
1776 case elfcpp::R_386_GNU_VTENTRY:
1777 break;
1778
1779 case elfcpp::R_X86_64_64:
1780 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1781 break;
1782
1783 case elfcpp::R_X86_64_PC64:
1784 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1785 address);
1786 break;
1787
1788 case elfcpp::R_X86_64_32:
1789 // FIXME: we need to verify that value + addend fits into 32 bits:
1790 // uint64_t x = value + addend;
1791 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1792 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1793 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1794 break;
1795
1796 case elfcpp::R_X86_64_32S:
1797 // FIXME: we need to verify that value + addend fits into 32 bits:
1798 // int64_t x = value + addend; // note this quantity is signed!
1799 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1800 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1801 break;
1802
1803 case elfcpp::R_X86_64_PC32:
1804 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1805 address);
1806 break;
1807
1808 case elfcpp::R_X86_64_16:
1809 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1810 break;
1811
1812 case elfcpp::R_X86_64_PC16:
1813 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1814 address);
1815 break;
1816
1817 case elfcpp::R_X86_64_8:
1818 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1819 break;
1820
1821 case elfcpp::R_X86_64_PC8:
1822 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1823 address);
1824 break;
1825
1826 case elfcpp::R_X86_64_PLT32:
1827 gold_assert(gsym == NULL
1828 || gsym->has_plt_offset()
1829 || gsym->final_value_is_known()
1830 || (gsym->is_defined()
1831 && !gsym->is_from_dynobj()
1832 && !gsym->is_preemptible()));
1833 // Note: while this code looks the same as for R_X86_64_PC32, it
1834 // behaves differently because psymval was set to point to
1835 // the PLT entry, rather than the symbol, in Scan::global().
1836 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1837 address);
1838 break;
1839
1840 case elfcpp::R_X86_64_PLTOFF64:
1841 {
1842 gold_assert(gsym);
1843 gold_assert(gsym->has_plt_offset()
1844 || gsym->final_value_is_known());
1845 elfcpp::Elf_types<64>::Elf_Addr got_address;
1846 got_address = target->got_section(NULL, NULL)->address();
1847 Relocate_functions<64, false>::rela64(view, object, psymval,
1848 addend - got_address);
1849 }
1850
1851 case elfcpp::R_X86_64_GOT32:
1852 gold_assert(have_got_offset);
1853 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1854 break;
1855
1856 case elfcpp::R_X86_64_GOTPC32:
1857 {
1858 gold_assert(gsym);
1859 elfcpp::Elf_types<64>::Elf_Addr value;
1860 value = target->got_plt_section()->address();
1861 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1862 }
1863 break;
1864
1865 case elfcpp::R_X86_64_GOT64:
1866 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1867 // Since we always add a PLT entry, this is equivalent.
1868 case elfcpp::R_X86_64_GOTPLT64:
1869 gold_assert(have_got_offset);
1870 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1871 break;
1872
1873 case elfcpp::R_X86_64_GOTPC64:
1874 {
1875 gold_assert(gsym);
1876 elfcpp::Elf_types<64>::Elf_Addr value;
1877 value = target->got_plt_section()->address();
1878 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1879 }
1880 break;
1881
1882 case elfcpp::R_X86_64_GOTOFF64:
1883 {
1884 elfcpp::Elf_types<64>::Elf_Addr value;
1885 value = (psymval->value(object, 0)
1886 - target->got_plt_section()->address());
1887 Relocate_functions<64, false>::rela64(view, value, addend);
1888 }
1889 break;
1890
1891 case elfcpp::R_X86_64_GOTPCREL:
1892 {
1893 gold_assert(have_got_offset);
1894 elfcpp::Elf_types<64>::Elf_Addr value;
1895 value = target->got_plt_section()->address() + got_offset;
1896 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1897 }
1898 break;
1899
1900 case elfcpp::R_X86_64_GOTPCREL64:
1901 {
1902 gold_assert(have_got_offset);
1903 elfcpp::Elf_types<64>::Elf_Addr value;
1904 value = target->got_plt_section()->address() + got_offset;
1905 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1906 }
1907 break;
1908
1909 case elfcpp::R_X86_64_COPY:
1910 case elfcpp::R_X86_64_GLOB_DAT:
1911 case elfcpp::R_X86_64_JUMP_SLOT:
1912 case elfcpp::R_X86_64_RELATIVE:
1913 // These are outstanding tls relocs, which are unexpected when linking
1914 case elfcpp::R_X86_64_TPOFF64:
1915 case elfcpp::R_X86_64_DTPMOD64:
1916 case elfcpp::R_X86_64_TLSDESC:
1917 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1918 _("unexpected reloc %u in object file"),
1919 r_type);
1920 break;
1921
1922 // These are initial tls relocs, which are expected when linking
1923 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1924 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1925 case elfcpp::R_X86_64_TLSDESC_CALL:
1926 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1927 case elfcpp::R_X86_64_DTPOFF32:
1928 case elfcpp::R_X86_64_DTPOFF64:
1929 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1930 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1931 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1932 view, address, view_size);
1933 break;
1934
1935 case elfcpp::R_X86_64_SIZE32:
1936 case elfcpp::R_X86_64_SIZE64:
1937 default:
1938 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1939 _("unsupported reloc %u"),
1940 r_type);
1941 break;
1942 }
1943
1944 return true;
1945 }
1946
1947 // Perform a TLS relocation.
1948
1949 inline void
1950 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1951 Target_x86_64* target,
1952 size_t relnum,
1953 const elfcpp::Rela<64, false>& rela,
1954 unsigned int r_type,
1955 const Sized_symbol<64>* gsym,
1956 const Symbol_value<64>* psymval,
1957 unsigned char* view,
1958 elfcpp::Elf_types<64>::Elf_Addr address,
1959 section_size_type view_size)
1960 {
1961 Output_segment* tls_segment = relinfo->layout->tls_segment();
1962
1963 const Sized_relobj<64, false>* object = relinfo->object;
1964 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1965
1966 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1967
1968 const bool is_final = (gsym == NULL
1969 ? !parameters->options().output_is_position_independent()
1970 : gsym->final_value_is_known());
1971 const tls::Tls_optimization optimized_type
1972 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1973 switch (r_type)
1974 {
1975 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1976 this->saw_tls_block_reloc_ = true;
1977 if (optimized_type == tls::TLSOPT_TO_LE)
1978 {
1979 gold_assert(tls_segment != NULL);
1980 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1981 rela, r_type, value, view,
1982 view_size);
1983 break;
1984 }
1985 else
1986 {
1987 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1988 ? GOT_TYPE_TLS_OFFSET
1989 : GOT_TYPE_TLS_PAIR);
1990 unsigned int got_offset;
1991 if (gsym != NULL)
1992 {
1993 gold_assert(gsym->has_got_offset(got_type));
1994 got_offset = gsym->got_offset(got_type) - target->got_size();
1995 }
1996 else
1997 {
1998 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1999 gold_assert(object->local_has_got_offset(r_sym, got_type));
2000 got_offset = (object->local_got_offset(r_sym, got_type)
2001 - target->got_size());
2002 }
2003 if (optimized_type == tls::TLSOPT_TO_IE)
2004 {
2005 gold_assert(tls_segment != NULL);
2006 value = target->got_plt_section()->address() + got_offset;
2007 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2008 value, view, address, view_size);
2009 break;
2010 }
2011 else if (optimized_type == tls::TLSOPT_NONE)
2012 {
2013 // Relocate the field with the offset of the pair of GOT
2014 // entries.
2015 value = target->got_plt_section()->address() + got_offset;
2016 Relocate_functions<64, false>::pcrela32(view, value, addend,
2017 address);
2018 break;
2019 }
2020 }
2021 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2022 _("unsupported reloc %u"), r_type);
2023 break;
2024
2025 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2026 case elfcpp::R_X86_64_TLSDESC_CALL:
2027 this->saw_tls_block_reloc_ = true;
2028 if (optimized_type == tls::TLSOPT_TO_LE)
2029 {
2030 gold_assert(tls_segment != NULL);
2031 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2032 rela, r_type, value, view,
2033 view_size);
2034 break;
2035 }
2036 else
2037 {
2038 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2039 ? GOT_TYPE_TLS_OFFSET
2040 : GOT_TYPE_TLS_DESC);
2041 unsigned int got_offset;
2042 if (gsym != NULL)
2043 {
2044 gold_assert(gsym->has_got_offset(got_type));
2045 got_offset = gsym->got_offset(got_type) - target->got_size();
2046 }
2047 else
2048 {
2049 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2050 gold_assert(object->local_has_got_offset(r_sym, got_type));
2051 got_offset = (object->local_got_offset(r_sym, got_type)
2052 - target->got_size());
2053 }
2054 if (optimized_type == tls::TLSOPT_TO_IE)
2055 {
2056 gold_assert(tls_segment != NULL);
2057 value = target->got_plt_section()->address() + got_offset;
2058 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2059 rela, r_type, value, view, address,
2060 view_size);
2061 break;
2062 }
2063 else if (optimized_type == tls::TLSOPT_NONE)
2064 {
2065 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2066 {
2067 // Relocate the field with the offset of the pair of GOT
2068 // entries.
2069 value = target->got_plt_section()->address() + got_offset;
2070 Relocate_functions<64, false>::pcrela32(view, value, addend,
2071 address);
2072 }
2073 break;
2074 }
2075 }
2076 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2077 _("unsupported reloc %u"), r_type);
2078 break;
2079
2080 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2081 this->saw_tls_block_reloc_ = true;
2082 if (optimized_type == tls::TLSOPT_TO_LE)
2083 {
2084 gold_assert(tls_segment != NULL);
2085 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2086 value, view, view_size);
2087 break;
2088 }
2089 else if (optimized_type == tls::TLSOPT_NONE)
2090 {
2091 // Relocate the field with the offset of the GOT entry for
2092 // the module index.
2093 unsigned int got_offset;
2094 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2095 - target->got_size());
2096 value = target->got_plt_section()->address() + got_offset;
2097 Relocate_functions<64, false>::pcrela32(view, value, addend,
2098 address);
2099 break;
2100 }
2101 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2102 _("unsupported reloc %u"), r_type);
2103 break;
2104
2105 case elfcpp::R_X86_64_DTPOFF32:
2106 gold_assert(tls_segment != NULL);
2107 if (optimized_type == tls::TLSOPT_TO_LE)
2108 {
2109 // This relocation type is used in debugging information.
2110 // In that case we need to not optimize the value. If we
2111 // haven't seen a TLSLD reloc, then we assume we should not
2112 // optimize this reloc.
2113 if (this->saw_tls_block_reloc_)
2114 value -= tls_segment->memsz();
2115 }
2116 Relocate_functions<64, false>::rela32(view, value, addend);
2117 break;
2118
2119 case elfcpp::R_X86_64_DTPOFF64:
2120 gold_assert(tls_segment != NULL);
2121 if (optimized_type == tls::TLSOPT_TO_LE)
2122 {
2123 // See R_X86_64_DTPOFF32, just above, for why we test this.
2124 if (this->saw_tls_block_reloc_)
2125 value -= tls_segment->memsz();
2126 }
2127 Relocate_functions<64, false>::rela64(view, value, addend);
2128 break;
2129
2130 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2131 if (optimized_type == tls::TLSOPT_TO_LE)
2132 {
2133 gold_assert(tls_segment != NULL);
2134 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2135 rela, r_type, value, view,
2136 view_size);
2137 break;
2138 }
2139 else if (optimized_type == tls::TLSOPT_NONE)
2140 {
2141 // Relocate the field with the offset of the GOT entry for
2142 // the tp-relative offset of the symbol.
2143 unsigned int got_offset;
2144 if (gsym != NULL)
2145 {
2146 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2147 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2148 - target->got_size());
2149 }
2150 else
2151 {
2152 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2153 gold_assert(object->local_has_got_offset(r_sym,
2154 GOT_TYPE_TLS_OFFSET));
2155 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2156 - target->got_size());
2157 }
2158 value = target->got_plt_section()->address() + got_offset;
2159 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2160 break;
2161 }
2162 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2163 _("unsupported reloc type %u"),
2164 r_type);
2165 break;
2166
2167 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2168 value -= tls_segment->memsz();
2169 Relocate_functions<64, false>::rela32(view, value, addend);
2170 break;
2171 }
2172 }
2173
2174 // Do a relocation in which we convert a TLS General-Dynamic to an
2175 // Initial-Exec.
2176
2177 inline void
2178 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2179 size_t relnum,
2180 Output_segment*,
2181 const elfcpp::Rela<64, false>& rela,
2182 unsigned int,
2183 elfcpp::Elf_types<64>::Elf_Addr value,
2184 unsigned char* view,
2185 elfcpp::Elf_types<64>::Elf_Addr address,
2186 section_size_type view_size)
2187 {
2188 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2189 // .word 0x6666; rex64; call __tls_get_addr
2190 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2191
2192 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2193 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2194
2195 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2196 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2197 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2198 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2199
2200 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2201
2202 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2203 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2204
2205 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2206 // We can skip it.
2207 this->skip_call_tls_get_addr_ = true;
2208 }
2209
2210 // Do a relocation in which we convert a TLS General-Dynamic to a
2211 // Local-Exec.
2212
2213 inline void
2214 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2215 size_t relnum,
2216 Output_segment* tls_segment,
2217 const elfcpp::Rela<64, false>& rela,
2218 unsigned int,
2219 elfcpp::Elf_types<64>::Elf_Addr value,
2220 unsigned char* view,
2221 section_size_type view_size)
2222 {
2223 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2224 // .word 0x6666; rex64; call __tls_get_addr
2225 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2226
2227 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2228 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2229
2230 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2231 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2232 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2233 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2234
2235 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2236
2237 value -= tls_segment->memsz();
2238 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2239
2240 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2241 // We can skip it.
2242 this->skip_call_tls_get_addr_ = true;
2243 }
2244
2245 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2246
2247 inline void
2248 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2249 const Relocate_info<64, false>* relinfo,
2250 size_t relnum,
2251 Output_segment*,
2252 const elfcpp::Rela<64, false>& rela,
2253 unsigned int r_type,
2254 elfcpp::Elf_types<64>::Elf_Addr value,
2255 unsigned char* view,
2256 elfcpp::Elf_types<64>::Elf_Addr address,
2257 section_size_type view_size)
2258 {
2259 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2260 {
2261 // leaq foo@tlsdesc(%rip), %rax
2262 // ==> movq foo@gottpoff(%rip), %rax
2263 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2264 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2265 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2266 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2267 view[-2] = 0x8b;
2268 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2269 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2270 }
2271 else
2272 {
2273 // call *foo@tlscall(%rax)
2274 // ==> nop; nop
2275 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2276 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2277 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2278 view[0] == 0xff && view[1] == 0x10);
2279 view[0] = 0x66;
2280 view[1] = 0x90;
2281 }
2282 }
2283
2284 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2285
2286 inline void
2287 Target_x86_64::Relocate::tls_desc_gd_to_le(
2288 const Relocate_info<64, false>* relinfo,
2289 size_t relnum,
2290 Output_segment* tls_segment,
2291 const elfcpp::Rela<64, false>& rela,
2292 unsigned int r_type,
2293 elfcpp::Elf_types<64>::Elf_Addr value,
2294 unsigned char* view,
2295 section_size_type view_size)
2296 {
2297 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2298 {
2299 // leaq foo@tlsdesc(%rip), %rax
2300 // ==> movq foo@tpoff, %rax
2301 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2302 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2303 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2304 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2305 view[-2] = 0xc7;
2306 view[-1] = 0xc0;
2307 value -= tls_segment->memsz();
2308 Relocate_functions<64, false>::rela32(view, value, 0);
2309 }
2310 else
2311 {
2312 // call *foo@tlscall(%rax)
2313 // ==> nop; nop
2314 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2315 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2316 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2317 view[0] == 0xff && view[1] == 0x10);
2318 view[0] = 0x66;
2319 view[1] = 0x90;
2320 }
2321 }
2322
2323 inline void
2324 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2325 size_t relnum,
2326 Output_segment*,
2327 const elfcpp::Rela<64, false>& rela,
2328 unsigned int,
2329 elfcpp::Elf_types<64>::Elf_Addr,
2330 unsigned char* view,
2331 section_size_type view_size)
2332 {
2333 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2334 // ... leq foo@dtpoff(%rax),%reg
2335 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2336
2337 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2338 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2339
2340 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2341 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2342
2343 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2344
2345 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2346
2347 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2348 // We can skip it.
2349 this->skip_call_tls_get_addr_ = true;
2350 }
2351
2352 // Do a relocation in which we convert a TLS Initial-Exec to a
2353 // Local-Exec.
2354
2355 inline void
2356 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2357 size_t relnum,
2358 Output_segment* tls_segment,
2359 const elfcpp::Rela<64, false>& rela,
2360 unsigned int,
2361 elfcpp::Elf_types<64>::Elf_Addr value,
2362 unsigned char* view,
2363 section_size_type view_size)
2364 {
2365 // We need to examine the opcodes to figure out which instruction we
2366 // are looking at.
2367
2368 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2369 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2370
2371 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2372 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2373
2374 unsigned char op1 = view[-3];
2375 unsigned char op2 = view[-2];
2376 unsigned char op3 = view[-1];
2377 unsigned char reg = op3 >> 3;
2378
2379 if (op2 == 0x8b)
2380 {
2381 // movq
2382 if (op1 == 0x4c)
2383 view[-3] = 0x49;
2384 view[-2] = 0xc7;
2385 view[-1] = 0xc0 | reg;
2386 }
2387 else if (reg == 4)
2388 {
2389 // Special handling for %rsp.
2390 if (op1 == 0x4c)
2391 view[-3] = 0x49;
2392 view[-2] = 0x81;
2393 view[-1] = 0xc0 | reg;
2394 }
2395 else
2396 {
2397 // addq
2398 if (op1 == 0x4c)
2399 view[-3] = 0x4d;
2400 view[-2] = 0x8d;
2401 view[-1] = 0x80 | reg | (reg << 3);
2402 }
2403
2404 value -= tls_segment->memsz();
2405 Relocate_functions<64, false>::rela32(view, value, 0);
2406 }
2407
2408 // Relocate section data.
2409
2410 void
2411 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
2412 unsigned int sh_type,
2413 const unsigned char* prelocs,
2414 size_t reloc_count,
2415 Output_section* output_section,
2416 bool needs_special_offset_handling,
2417 unsigned char* view,
2418 elfcpp::Elf_types<64>::Elf_Addr address,
2419 section_size_type view_size)
2420 {
2421 gold_assert(sh_type == elfcpp::SHT_RELA);
2422
2423 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2424 Target_x86_64::Relocate>(
2425 relinfo,
2426 this,
2427 prelocs,
2428 reloc_count,
2429 output_section,
2430 needs_special_offset_handling,
2431 view,
2432 address,
2433 view_size);
2434 }
2435
2436 // Return the size of a relocation while scanning during a relocatable
2437 // link.
2438
2439 unsigned int
2440 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2441 unsigned int r_type,
2442 Relobj* object)
2443 {
2444 switch (r_type)
2445 {
2446 case elfcpp::R_X86_64_NONE:
2447 case elfcpp::R_386_GNU_VTINHERIT:
2448 case elfcpp::R_386_GNU_VTENTRY:
2449 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2450 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2451 case elfcpp::R_X86_64_TLSDESC_CALL:
2452 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2453 case elfcpp::R_X86_64_DTPOFF32:
2454 case elfcpp::R_X86_64_DTPOFF64:
2455 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2456 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2457 return 0;
2458
2459 case elfcpp::R_X86_64_64:
2460 case elfcpp::R_X86_64_PC64:
2461 case elfcpp::R_X86_64_GOTOFF64:
2462 case elfcpp::R_X86_64_GOTPC64:
2463 case elfcpp::R_X86_64_PLTOFF64:
2464 case elfcpp::R_X86_64_GOT64:
2465 case elfcpp::R_X86_64_GOTPCREL64:
2466 case elfcpp::R_X86_64_GOTPCREL:
2467 case elfcpp::R_X86_64_GOTPLT64:
2468 return 8;
2469
2470 case elfcpp::R_X86_64_32:
2471 case elfcpp::R_X86_64_32S:
2472 case elfcpp::R_X86_64_PC32:
2473 case elfcpp::R_X86_64_PLT32:
2474 case elfcpp::R_X86_64_GOTPC32:
2475 case elfcpp::R_X86_64_GOT32:
2476 return 4;
2477
2478 case elfcpp::R_X86_64_16:
2479 case elfcpp::R_X86_64_PC16:
2480 return 2;
2481
2482 case elfcpp::R_X86_64_8:
2483 case elfcpp::R_X86_64_PC8:
2484 return 1;
2485
2486 case elfcpp::R_X86_64_COPY:
2487 case elfcpp::R_X86_64_GLOB_DAT:
2488 case elfcpp::R_X86_64_JUMP_SLOT:
2489 case elfcpp::R_X86_64_RELATIVE:
2490 // These are outstanding tls relocs, which are unexpected when linking
2491 case elfcpp::R_X86_64_TPOFF64:
2492 case elfcpp::R_X86_64_DTPMOD64:
2493 case elfcpp::R_X86_64_TLSDESC:
2494 object->error(_("unexpected reloc %u in object file"), r_type);
2495 return 0;
2496
2497 case elfcpp::R_X86_64_SIZE32:
2498 case elfcpp::R_X86_64_SIZE64:
2499 default:
2500 object->error(_("unsupported reloc %u against local symbol"), r_type);
2501 return 0;
2502 }
2503 }
2504
2505 // Scan the relocs during a relocatable link.
2506
2507 void
2508 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2509 Symbol_table* symtab,
2510 Layout* layout,
2511 Sized_relobj<64, false>* object,
2512 unsigned int data_shndx,
2513 unsigned int sh_type,
2514 const unsigned char* prelocs,
2515 size_t reloc_count,
2516 Output_section* output_section,
2517 bool needs_special_offset_handling,
2518 size_t local_symbol_count,
2519 const unsigned char* plocal_symbols,
2520 Relocatable_relocs* rr)
2521 {
2522 gold_assert(sh_type == elfcpp::SHT_RELA);
2523
2524 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2525 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2526
2527 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2528 Scan_relocatable_relocs>(
2529 options,
2530 symtab,
2531 layout,
2532 object,
2533 data_shndx,
2534 prelocs,
2535 reloc_count,
2536 output_section,
2537 needs_special_offset_handling,
2538 local_symbol_count,
2539 plocal_symbols,
2540 rr);
2541 }
2542
2543 // Relocate a section during a relocatable link.
2544
2545 void
2546 Target_x86_64::relocate_for_relocatable(
2547 const Relocate_info<64, false>* relinfo,
2548 unsigned int sh_type,
2549 const unsigned char* prelocs,
2550 size_t reloc_count,
2551 Output_section* output_section,
2552 off_t offset_in_output_section,
2553 const Relocatable_relocs* rr,
2554 unsigned char* view,
2555 elfcpp::Elf_types<64>::Elf_Addr view_address,
2556 section_size_type view_size,
2557 unsigned char* reloc_view,
2558 section_size_type reloc_view_size)
2559 {
2560 gold_assert(sh_type == elfcpp::SHT_RELA);
2561
2562 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2563 relinfo,
2564 prelocs,
2565 reloc_count,
2566 output_section,
2567 offset_in_output_section,
2568 rr,
2569 view,
2570 view_address,
2571 view_size,
2572 reloc_view,
2573 reloc_view_size);
2574 }
2575
2576 // Return the value to use for a dynamic which requires special
2577 // treatment. This is how we support equality comparisons of function
2578 // pointers across shared library boundaries, as described in the
2579 // processor specific ABI supplement.
2580
2581 uint64_t
2582 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2583 {
2584 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2585 return this->plt_section()->address() + gsym->plt_offset();
2586 }
2587
2588 // Return a string used to fill a code section with nops to take up
2589 // the specified length.
2590
2591 std::string
2592 Target_x86_64::do_code_fill(section_size_type length) const
2593 {
2594 if (length >= 16)
2595 {
2596 // Build a jmpq instruction to skip over the bytes.
2597 unsigned char jmp[5];
2598 jmp[0] = 0xe9;
2599 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2600 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2601 + std::string(length - 5, '\0'));
2602 }
2603
2604 // Nop sequences of various lengths.
2605 const char nop1[1] = { 0x90 }; // nop
2606 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2607 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2608 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2609 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2610 0x00 }; // leal 0(%esi,1),%esi
2611 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2612 0x00, 0x00 };
2613 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2614 0x00, 0x00, 0x00 };
2615 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2616 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2617 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2618 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2619 0x00 };
2620 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2621 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2622 0x00, 0x00 };
2623 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2624 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2625 0x00, 0x00, 0x00 };
2626 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2627 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2628 0x00, 0x00, 0x00, 0x00 };
2629 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2630 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2631 0x27, 0x00, 0x00, 0x00,
2632 0x00 };
2633 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2634 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2635 0xbc, 0x27, 0x00, 0x00,
2636 0x00, 0x00 };
2637 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2638 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2639 0x90, 0x90, 0x90, 0x90,
2640 0x90, 0x90, 0x90 };
2641
2642 const char* nops[16] = {
2643 NULL,
2644 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2645 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2646 };
2647
2648 return std::string(nops[length], length);
2649 }
2650
2651 // The selector for x86_64 object files.
2652
2653 class Target_selector_x86_64 : public Target_selector_freebsd
2654 {
2655 public:
2656 Target_selector_x86_64()
2657 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
2658 "elf64-x86-64-freebsd")
2659 { }
2660
2661 Target*
2662 do_instantiate_target()
2663 { return new Target_x86_64(); }
2664
2665 };
2666
2667 Target_selector_x86_64 target_selector_x86_64;
2668
2669 } // End anonymous namespace.
This page took 0.084238 seconds and 4 git commands to generate.