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