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