2011-04-22 Doug Kwan <dougkwan@google.com>
[deliverable/binutils-gdb.git] / gold / x86_64.cc
CommitLineData
2e30d253
ILT
1// x86_64.cc -- x86_64 target support for gold.
2
a8df5856 3// Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
2e30d253
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8b105e34
ILT
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
2e30d253
ILT
11// (at your option) any later version.
12
8b105e34
ILT
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.
2e30d253
ILT
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"
12c0daef 35#include "copy-relocs.h"
2e30d253
ILT
36#include "target.h"
37#include "target-reloc.h"
38#include "target-select.h"
e041f13d 39#include "tls.h"
36959681 40#include "freebsd.h"
f345227a 41#include "gc.h"
21bb3914 42#include "icf.h"
2e30d253
ILT
43
44namespace
45{
46
47using namespace gold;
48
7223e9ca
ILT
49// A class to handle the PLT data.
50
51class 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};
2e30d253
ILT
158
159// The x86_64 target class.
d61c17ea
ILT
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
0ffd9845 164// http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
2e30d253 165
36959681 166class Target_x86_64 : public Target_freebsd<64, false>
2e30d253
ILT
167{
168 public:
e822f2b1
ILT
169 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
170 // uses only Elf64_Rela relocation entries with explicit addends."
2e30d253
ILT
171 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
172
173 Target_x86_64()
36959681 174 : Target_freebsd<64, false>(&x86_64_info),
a8df5856
ILT
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),
e291e7b9
ILT
178 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
179 tls_base_symbol_defined_(false)
2e30d253
ILT
180 { }
181
21bb3914
ST
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
c95e9f27
ST
192 virtual bool
193 can_icf_inline_merge_sections () const
194 { return true; }
195
8a5e3e08
ILT
196 // Hook for a new output section.
197 void
198 do_new_output_section(Output_section*) const;
199
6d03d481
ST
200 // Scan the relocations to look for symbol adjustments.
201 void
ad0f2072 202 gc_process_relocs(Symbol_table* symtab,
6d03d481
ST
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
2e30d253
ILT
214 // Scan the relocations to look for symbol adjustments.
215 void
ad0f2072 216 scan_relocs(Symbol_table* symtab,
2e30d253
ILT
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,
730cdc88
ILT
223 Output_section* output_section,
224 bool needs_special_offset_handling,
2e30d253 225 size_t local_symbol_count,
730cdc88 226 const unsigned char* plocal_symbols);
2e30d253
ILT
227
228 // Finalize the sections.
229 void
f59f41f3 230 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2e30d253 231
4fb6c25d
ILT
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
2e30d253
ILT
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,
730cdc88
ILT
243 Output_section* output_section,
244 bool needs_special_offset_handling,
2e30d253
ILT
245 unsigned char* view,
246 elfcpp::Elf_types<64>::Elf_Addr view_address,
364c7fa5
ILT
247 section_size_type view_size,
248 const Reloc_symbol_changes*);
2e30d253 249
6a74a719
ILT
250 // Scan the relocs during a relocatable link.
251 void
ad0f2072 252 scan_relocatable_relocs(Symbol_table* symtab,
6a74a719
ILT
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
2e30d253
ILT
280 // Return a string used to fill a code section with nops.
281 std::string
8851ecca 282 do_code_fill(section_size_type length) const;
2e30d253 283
9a2d6984
ILT
284 // Return whether SYM is defined by the ABI.
285 bool
9c2d0ef9 286 do_is_defined_by_abi(const Symbol* sym) const
9a2d6984
ILT
287 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
288
e291e7b9
ILT
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
7223e9ca
ILT
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
9b547ce6 312 // Adjust -fsplit-stack code which calls non-split-stack code.
364c7fa5
ILT
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
96f2030e 319 // Return the size of the GOT section.
fe8718a4 320 section_size_type
0e70b911 321 got_size() const
96f2030e
ILT
322 {
323 gold_assert(this->got_ != NULL);
324 return this->got_->data_size();
325 }
326
0e70b911
CC
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
e291e7b9
ILT
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
2e30d253
ILT
356 private:
357 // The class which scans relocations.
a036edd8 358 class Scan
2e30d253 359 {
a036edd8
ILT
360 public:
361 Scan()
362 : issued_non_pic_error_(false)
363 { }
364
95a2c8d6
RS
365 static inline int
366 get_reference_flags(unsigned int r_type);
367
2e30d253 368 inline void
ad0f2072 369 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
2e30d253
ILT
370 Sized_relobj<64, false>* object,
371 unsigned int data_shndx,
07f397ab 372 Output_section* output_section,
2e30d253
ILT
373 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
374 const elfcpp::Sym<64, false>& lsym);
375
376 inline void
ad0f2072 377 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
2e30d253
ILT
378 Sized_relobj<64, false>* object,
379 unsigned int data_shndx,
07f397ab 380 Output_section* output_section,
2e30d253
ILT
381 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
382 Symbol* gsym);
e041f13d 383
21bb3914
ST
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
a036edd8 404 private:
e041f13d
ILT
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*);
a036edd8
ILT
411
412 void
413 check_non_pic(Relobj*, unsigned int r_type);
414
21bb3914
ST
415 inline bool
416 possible_function_pointer_reloc(unsigned int r_type);
417
7223e9ca
ILT
418 bool
419 reloc_needs_plt_for_ifunc(Sized_relobj<64, false>*, unsigned int r_type);
420
a036edd8
ILT
421 // Whether we have issued an error about a non-PIC compilation.
422 bool issued_non_pic_error_;
2e30d253
ILT
423 };
424
425 // The class which implements relocation.
426 class Relocate
427 {
428 public:
429 Relocate()
36171d64 430 : skip_call_tls_get_addr_(false)
2e30d253
ILT
431 { }
432
433 ~Relocate()
434 {
435 if (this->skip_call_tls_get_addr_)
436 {
437 // FIXME: This needs to specify the location somehow.
a0c4fb0a 438 gold_error(_("missing expected TLS relocation"));
2e30d253
ILT
439 }
440 }
441
442 // Do a relocation. Return false if the caller should not issue
443 // any warnings about this relocation.
444 inline bool
031cdbed
ILT
445 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
446 size_t relnum, const elfcpp::Rela<64, false>&,
2e30d253
ILT
447 unsigned int r_type, const Sized_symbol<64>*,
448 const Symbol_value<64>*,
449 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
fe8718a4 450 section_size_type);
2e30d253
ILT
451
452 private:
453 // Do a TLS relocation.
454 inline void
7bf1f802
ILT
455 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
456 size_t relnum, const elfcpp::Rela<64, false>&,
2e30d253
ILT
457 unsigned int r_type, const Sized_symbol<64>*,
458 const Symbol_value<64>*,
fe8718a4
ILT
459 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
460 section_size_type);
2e30d253 461
c2b45e22 462 // Do a TLS General-Dynamic to Initial-Exec transition.
7bf1f802
ILT
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,
c2b45e22 469 elfcpp::Elf_types<64>::Elf_Addr,
fe8718a4 470 section_size_type view_size);
7bf1f802 471
56622147
ILT
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,
2e30d253
ILT
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,
fe8718a4 479 section_size_type view_size);
2e30d253 480
c2b45e22
CC
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
56622147 500 // Do a TLS Local-Dynamic to Local-Exec transition.
2e30d253 501 inline void
56622147 502 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
2e30d253
ILT
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,
fe8718a4 507 section_size_type view_size);
2e30d253 508
56622147
ILT
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,
72ec2876
ILT
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,
fe8718a4 516 section_size_type view_size);
2e30d253
ILT
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
6a74a719
ILT
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
2e30d253
ILT
532 // Adjust TLS relocation type based on the options and whether this
533 // is a local symbol.
e041f13d 534 static tls::Tls_optimization
2e30d253
ILT
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
96f2030e
ILT
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
a8df5856
ILT
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
c2b45e22
CC
557 // Create the PLT section.
558 void
559 make_plt_section(Symbol_table* symtab, Layout* layout);
560
2e30d253
ILT
561 // Create a PLT entry for a global symbol.
562 void
563 make_plt_entry(Symbol_table*, Layout*, Symbol*);
564
7223e9ca
ILT
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
9fa33bee 571 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
edfbb029
CC
572 void
573 define_tls_base_symbol(Symbol_table*, Layout*);
574
c2b45e22
CC
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
31d60480
ILT
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
2e30d253
ILT
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*
0ffd9845 594 rela_dyn_section(Layout*);
2e30d253 595
e291e7b9
ILT
596 // Get the section to use for TLSDESC relocations.
597 Reloc_section*
598 rela_tlsdesc_section(Layout*) const;
599
12c0daef 600 // Add a potential copy relocation.
2e30d253 601 void
ef9beddf
ILT
602 copy_reloc(Symbol_table* symtab, Layout* layout,
603 Sized_relobj<64, false>* object,
12c0daef
ILT
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 }
2e30d253
ILT
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
0e70b911
CC
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.
0a65a3a7
CC
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
e291e7b9
ILT
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
2e30d253
ILT
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_;
a8df5856
ILT
650 // The GOT section for TLSDESC relocations.
651 Output_data_got<64, false>* got_tlsdesc_;
e785ec03
ILT
652 // The _GLOBAL_OFFSET_TABLE_ symbol.
653 Symbol* global_offset_table_;
2e30d253 654 // The dynamic reloc section.
0ffd9845 655 Reloc_section* rela_dyn_;
2e30d253 656 // Relocs saved to avoid a COPY reloc.
12c0daef 657 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
2e30d253
ILT
658 // Space for variables copied with a COPY reloc.
659 Output_data_space* dynbss_;
c2b45e22 660 // Offset of the GOT entry for the TLS module index.
31d60480 661 unsigned int got_mod_index_offset_;
e291e7b9
ILT
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_;
edfbb029
CC
666 // True if the _TLS_MODULE_BASE_ symbol has been defined.
667 bool tls_base_symbol_defined_;
2e30d253
ILT
668};
669
670const 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
35cdfc9a 678 true, // is_default_stack_executable
0864d551 679 '\0', // wrap_char
2e30d253 680 "/lib/ld64.so.1", // program interpreter
0c5e9c22 681 0x400000, // default_text_segment_address
cd72c291 682 0x1000, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
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
05a352e6
DK
687 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
688 NULL, // attributes_section
689 NULL // attributes_vendor
2e30d253
ILT
690};
691
8a5e3e08
ILT
692// This is called when a new output section is created. This is where
693// we handle the SHF_X86_64_LARGE.
694
695void
ca09d69a 696Target_x86_64::do_new_output_section(Output_section* os) const
8a5e3e08
ILT
697{
698 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
699 os->set_is_large_section();
700}
701
2e30d253
ILT
702// Get the GOT section, creating it if necessary.
703
704Output_data_got<64, false>*
705Target_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
82742395
ILT
713 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
714 (elfcpp::SHF_ALLOC
715 | elfcpp::SHF_WRITE),
22f0da72
ILT
716 this->got_, ORDER_RELRO_LAST,
717 true);
2e30d253 718
7d9e3d98 719 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
82742395
ILT
720 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
721 (elfcpp::SHF_ALLOC
722 | elfcpp::SHF_WRITE),
22f0da72
ILT
723 this->got_plt_, ORDER_NON_RELRO_FIRST,
724 false);
2e30d253
ILT
725
726 // The first three entries are reserved.
27bc2bce 727 this->got_plt_->set_current_data_size(3 * 8);
2e30d253 728
1a2dff53
ILT
729 // Those bytes can go into the relro segment.
730 layout->increase_relro(3 * 8);
731
2e30d253 732 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
e785ec03
ILT
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);
a8df5856
ILT
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),
22f0da72
ILT
748 this->got_tlsdesc_,
749 ORDER_NON_RELRO_FIRST, false);
2e30d253
ILT
750 }
751
752 return this->got_;
753}
754
755// Get the dynamic reloc section, creating it if necessary.
756
757Target_x86_64::Reloc_section*
0ffd9845 758Target_x86_64::rela_dyn_section(Layout* layout)
2e30d253 759{
0ffd9845 760 if (this->rela_dyn_ == NULL)
2e30d253
ILT
761 {
762 gold_assert(layout != NULL);
d98bc257 763 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2e30d253 764 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
765 elfcpp::SHF_ALLOC, this->rela_dyn_,
766 ORDER_DYNAMIC_RELOCS, false);
2e30d253 767 }
0ffd9845 768 return this->rela_dyn_;
2e30d253
ILT
769}
770
2e30d253
ILT
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
7223e9ca
ILT
775Output_data_plt_x86_64::Output_data_plt_x86_64(Symbol_table* symtab,
776 Layout* layout,
c2b45e22 777 Output_data_got<64, false>* got,
2e30d253 778 Output_data_space* got_plt)
e291e7b9
ILT
779 : Output_section_data(8), tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
780 count_(0), tlsdesc_got_offset_(-1U)
2e30d253 781{
d98bc257 782 this->rel_ = new Reloc_section(false);
2e30d253 783 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
22f0da72
ILT
784 elfcpp::SHF_ALLOC, this->rel_,
785 ORDER_DYNAMIC_PLT_RELOCS, false);
7223e9ca
ILT
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 }
2e30d253
ILT
804}
805
806void
807Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
808{
b0481b0b 809 os->set_entsize(plt_entry_size);
2e30d253
ILT
810}
811
812// Add an entry to the PLT.
813
814void
815Output_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
fe8718a4 825 section_offset_type got_offset = this->got_plt_->current_data_size();
2e30d253
ILT
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).
27bc2bce 830 this->got_plt_->set_current_data_size(got_offset + 8);
2e30d253
ILT
831
832 // Every PLT entry needs a reloc.
7223e9ca
ILT
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 }
2e30d253
ILT
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
7223e9ca
ILT
849// Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
850// the PLT offset.
851
852unsigned int
853Output_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
e291e7b9
ILT
873// Return where the TLSDESC relocations should go, creating it if
874// necessary. These follow the JUMP_SLOT relocations.
875
876Output_data_plt_x86_64::Reloc_section*
877Output_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_,
22f0da72 884 ORDER_DYNAMIC_PLT_RELOCS, false);
e291e7b9
ILT
885 gold_assert(this->tlsdesc_rel_->output_section() ==
886 this->rel_->output_section());
887 }
888 return this->tlsdesc_rel_;
889}
890
c2b45e22
CC
891// Set the final size.
892void
893Output_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
2e30d253
ILT
901// The first entry in the PLT for an executable.
902
903unsigned 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
2e30d253 907 0, 0, 0, 0, // replaced with address of .got + 8
78d911fd
ILT
908 0xff, 0x25, // jmp indirect
909 0, 0, 0, 0, // replaced with address of .got + 16
2e30d253
ILT
910 0x90, 0x90, 0x90, 0x90 // noop (x4)
911};
912
913// Subsequent entries in the PLT for an executable.
914
915unsigned 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
c2b45e22
CC
926// The reserved TLSDESC entry in the PLT for an executable.
927
928unsigned 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
2e30d253
ILT
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
943void
944Output_data_plt_x86_64::do_write(Output_file* of)
945{
2ea97941 946 const off_t offset = this->offset();
fe8718a4
ILT
947 const section_size_type oview_size =
948 convert_to_section_size_type(this->data_size());
2ea97941 949 unsigned char* const oview = of->get_output_view(offset, oview_size);
2e30d253
ILT
950
951 const off_t got_file_offset = this->got_plt_->offset();
fe8718a4
ILT
952 const section_size_type got_size =
953 convert_to_section_size_type(this->got_plt_->data_size());
2e30d253
ILT
954 unsigned char* const got_view = of->get_output_view(got_file_offset,
955 got_size);
956
957 unsigned char* pov = oview;
958
c2b45e22 959 // The base address of the .plt section.
a984ee1d 960 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
c2b45e22 961 // The base address of the .got section.
a984ee1d 962 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
c2b45e22
CC
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.
a984ee1d 966 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
2e30d253
ILT
967
968 memcpy(pov, first_plt_entry, plt_entry_size);
78d911fd 969 // We do a jmp relative to the PC at the end of this instruction.
a984ee1d
ILT
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)));
2e30d253
ILT
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);
78d911fd
ILT
996 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
997 (got_address + got_offset
998 - (plt_address + plt_offset
999 + 6)));
2e30d253
ILT
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
c2b45e22
CC
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
fe8718a4
ILT
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);
2e30d253 1028
2ea97941 1029 of->write_output_view(offset, oview_size, oview);
2e30d253
ILT
1030 of->write_output_view(got_file_offset, got_size, got_view);
1031}
1032
c2b45e22 1033// Create the PLT section.
2e30d253
ILT
1034
1035void
c2b45e22 1036Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
2e30d253 1037{
2e30d253
ILT
1038 if (this->plt_ == NULL)
1039 {
1040 // Create the GOT sections first.
1041 this->got_section(symtab, layout);
1042
7223e9ca 1043 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
c2b45e22 1044 this->got_plt_);
2e30d253
ILT
1045 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1046 (elfcpp::SHF_ALLOC
1047 | elfcpp::SHF_EXECINSTR),
22f0da72 1048 this->plt_, ORDER_PLT, false);
7223e9ca
ILT
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());
2e30d253 1053 }
c2b45e22
CC
1054}
1055
e291e7b9
ILT
1056// Return the section for TLSDESC relocations.
1057
1058Target_x86_64::Reloc_section*
1059Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1060{
1061 return this->plt_section()->rela_tlsdesc(layout);
1062}
1063
c2b45e22
CC
1064// Create a PLT entry for a global symbol.
1065
1066void
1067Target_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);
2e30d253
ILT
1075
1076 this->plt_->add_entry(gsym);
1077}
1078
7223e9ca
ILT
1079// Make a PLT entry for a local STT_GNU_IFUNC symbol.
1080
1081void
1082Target_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
0e70b911
CC
1095// Return the number of entries in the PLT.
1096
1097unsigned int
1098Target_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
1107unsigned int
1108Target_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
1115unsigned int
1116Target_x86_64::plt_entry_size() const
1117{
1118 return Output_data_plt_x86_64::get_plt_entry_size();
1119}
1120
9fa33bee 1121// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
edfbb029
CC
1122
1123void
1124Target_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 {
183fd0e3 1132 bool is_exec = parameters->options().output_is_executable();
edfbb029 1133 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
99fff23b 1134 Symbol_table::PREDEFINED,
edfbb029
CC
1135 tls_segment, 0, 0,
1136 elfcpp::STT_TLS,
1137 elfcpp::STB_LOCAL,
1138 elfcpp::STV_HIDDEN, 0,
183fd0e3
AO
1139 (is_exec
1140 ? Symbol::SEGMENT_END
1141 : Symbol::SEGMENT_START),
1142 true);
edfbb029
CC
1143 }
1144 this->tls_base_symbol_defined_ = true;
1145}
1146
c2b45e22
CC
1147// Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1148
1149void
1150Target_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
31d60480
ILT
1167// Create a GOT entry for the TLS module index.
1168
1169unsigned int
1170Target_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);
009a67a2 1181 got->add_constant(0);
31d60480
ILT
1182 this->got_mod_index_offset_ = got_offset;
1183 }
1184 return this->got_mod_index_offset_;
1185}
1186
2e30d253
ILT
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
e041f13d 1191tls::Tls_optimization
2e30d253
ILT
1192Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1193{
2e30d253
ILT
1194 // If we are generating a shared library, then we can't do anything
1195 // in the linker.
8851ecca 1196 if (parameters->options().shared())
e041f13d 1197 return tls::TLSOPT_NONE;
2e30d253
ILT
1198
1199 switch (r_type)
1200 {
1201 case elfcpp::R_X86_64_TLSGD:
e041f13d
ILT
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
2e30d253
ILT
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)
e041f13d
ILT
1209 return tls::TLSOPT_TO_LE;
1210 return tls::TLSOPT_TO_IE;
2e30d253 1211
d61c17ea 1212 case elfcpp::R_X86_64_TLSLD:
2e30d253
ILT
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.
e041f13d 1216 return tls::TLSOPT_TO_LE;
2e30d253 1217
0ffd9845 1218 case elfcpp::R_X86_64_DTPOFF32:
0ffd9845
ILT
1219 case elfcpp::R_X86_64_DTPOFF64:
1220 // Another Local-Dynamic reloc.
e041f13d 1221 return tls::TLSOPT_TO_LE;
0ffd9845 1222
d61c17ea 1223 case elfcpp::R_X86_64_GOTTPOFF:
2e30d253
ILT
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)
e041f13d
ILT
1229 return tls::TLSOPT_TO_LE;
1230 return tls::TLSOPT_NONE;
2e30d253 1231
d61c17ea 1232 case elfcpp::R_X86_64_TPOFF32:
2e30d253
ILT
1233 // When we already have Local-Exec, there is nothing further we
1234 // can do.
e041f13d 1235 return tls::TLSOPT_NONE;
2e30d253
ILT
1236
1237 default:
1238 gold_unreachable();
1239 }
2e30d253
ILT
1240}
1241
95a2c8d6
RS
1242// Get the Reference_flags for a particular relocation.
1243
1244int
1245Target_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
e041f13d
ILT
1309// Report an unsupported relocation against a local symbol.
1310
1311void
1312Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
1313 unsigned int r_type)
1314{
75f2446e
ILT
1315 gold_error(_("%s: unsupported reloc %u against local symbol"),
1316 object->name().c_str(), r_type);
e041f13d
ILT
1317}
1318
a036edd8
ILT
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
1327void
1328Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1329{
1330 switch (r_type)
1331 {
2fbb4320
ILT
1332 // These are the relocation types supported by glibc for x86_64
1333 // which should always work.
a036edd8 1334 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 1335 case elfcpp::R_X86_64_IRELATIVE:
a036edd8
ILT
1336 case elfcpp::R_X86_64_GLOB_DAT:
1337 case elfcpp::R_X86_64_JUMP_SLOT:
1338 case elfcpp::R_X86_64_DTPMOD64:
1339 case elfcpp::R_X86_64_DTPOFF64:
1340 case elfcpp::R_X86_64_TPOFF64:
1341 case elfcpp::R_X86_64_64:
2fbb4320
ILT
1342 case elfcpp::R_X86_64_COPY:
1343 return;
1344
1345 // glibc supports these reloc types, but they can overflow.
a036edd8
ILT
1346 case elfcpp::R_X86_64_32:
1347 case elfcpp::R_X86_64_PC32:
2fbb4320
ILT
1348 if (this->issued_non_pic_error_)
1349 return;
1350 gold_assert(parameters->options().output_is_position_independent());
1351 object->error(_("requires dynamic reloc which may overflow at runtime; "
1352 "recompile with -fPIC"));
1353 this->issued_non_pic_error_ = true;
a036edd8
ILT
1354 return;
1355
1356 default:
1357 // This prevents us from issuing more than one error per reloc
1358 // section. But we can still wind up issuing more than one
1359 // error per object file.
1360 if (this->issued_non_pic_error_)
1361 return;
33aea2fd 1362 gold_assert(parameters->options().output_is_position_independent());
a036edd8
ILT
1363 object->error(_("requires unsupported dynamic reloc; "
1364 "recompile with -fPIC"));
1365 this->issued_non_pic_error_ = true;
1366 return;
1367
1368 case elfcpp::R_X86_64_NONE:
1369 gold_unreachable();
1370 }
1371}
1372
7223e9ca
ILT
1373// Return whether we need to make a PLT entry for a relocation of the
1374// given type against a STT_GNU_IFUNC symbol.
1375
1376bool
1377Target_x86_64::Scan::reloc_needs_plt_for_ifunc(Sized_relobj<64, false>* object,
1378 unsigned int r_type)
1379{
95a2c8d6
RS
1380 int flags = Scan::get_reference_flags(r_type);
1381 if (flags & Symbol::TLS_REF)
1382 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1383 object->name().c_str(), r_type);
1384 return flags != 0;
7223e9ca
ILT
1385}
1386
2e30d253
ILT
1387// Scan a relocation for a local symbol.
1388
1389inline void
ad0f2072 1390Target_x86_64::Scan::local(Symbol_table* symtab,
d61c17ea
ILT
1391 Layout* layout,
1392 Target_x86_64* target,
1393 Sized_relobj<64, false>* object,
0ffd9845 1394 unsigned int data_shndx,
4f4c5f80 1395 Output_section* output_section,
0ffd9845 1396 const elfcpp::Rela<64, false>& reloc,
d61c17ea 1397 unsigned int r_type,
7bf1f802 1398 const elfcpp::Sym<64, false>& lsym)
2e30d253 1399{
7223e9ca
ILT
1400 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1401 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1402 && this->reloc_needs_plt_for_ifunc(object, r_type))
1403 {
1404 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1405 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1406 }
1407
2e30d253
ILT
1408 switch (r_type)
1409 {
1410 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
1411 case elfcpp::R_X86_64_GNU_VTINHERIT:
1412 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
1413 break;
1414
1415 case elfcpp::R_X86_64_64:
d61c6bd4 1416 // If building a shared library (or a position-independent
dceae3c1
ILT
1417 // executable), we need to create a dynamic relocation for this
1418 // location. The relocation applied at link time will apply the
1419 // link-time value, so we flag the location with an
1420 // R_X86_64_RELATIVE relocation so the dynamic loader can
d61c6bd4 1421 // relocate it easily.
8851ecca 1422 if (parameters->options().output_is_position_independent())
d61c6bd4 1423 {
e8c846c3 1424 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d61c6bd4 1425 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7223e9ca
ILT
1426 rela_dyn->add_local_relative(object, r_sym,
1427 elfcpp::R_X86_64_RELATIVE,
1428 output_section, data_shndx,
1429 reloc.get_r_offset(),
1430 reloc.get_r_addend());
d61c6bd4
ILT
1431 }
1432 break;
1433
2e30d253
ILT
1434 case elfcpp::R_X86_64_32:
1435 case elfcpp::R_X86_64_32S:
1436 case elfcpp::R_X86_64_16:
1437 case elfcpp::R_X86_64_8:
96f2030e 1438 // If building a shared library (or a position-independent
dceae3c1
ILT
1439 // executable), we need to create a dynamic relocation for this
1440 // location. We can't use an R_X86_64_RELATIVE relocation
1441 // because that is always a 64-bit relocation.
8851ecca 1442 if (parameters->options().output_is_position_independent())
96f2030e 1443 {
a036edd8
ILT
1444 this->check_non_pic(object, r_type);
1445
96f2030e 1446 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
d491d34e 1447 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
dceae3c1 1448 if (lsym.get_st_type() != elfcpp::STT_SECTION)
d491d34e
ILT
1449 rela_dyn->add_local(object, r_sym, r_type, output_section,
1450 data_shndx, reloc.get_r_offset(),
1451 reloc.get_r_addend());
dceae3c1
ILT
1452 else
1453 {
1454 gold_assert(lsym.get_st_value() == 0);
d491d34e
ILT
1455 unsigned int shndx = lsym.get_st_shndx();
1456 bool is_ordinary;
1457 shndx = object->adjust_sym_shndx(r_sym, shndx,
1458 &is_ordinary);
1459 if (!is_ordinary)
1460 object->error(_("section symbol %u has bad shndx %u"),
1461 r_sym, shndx);
1462 else
1463 rela_dyn->add_local_section(object, shndx,
1464 r_type, output_section,
1465 data_shndx, reloc.get_r_offset(),
1466 reloc.get_r_addend());
dceae3c1 1467 }
96f2030e 1468 }
2e30d253
ILT
1469 break;
1470
1471 case elfcpp::R_X86_64_PC64:
1472 case elfcpp::R_X86_64_PC32:
1473 case elfcpp::R_X86_64_PC16:
1474 case elfcpp::R_X86_64_PC8:
1475 break;
1476
f389a824
ILT
1477 case elfcpp::R_X86_64_PLT32:
1478 // Since we know this is a local symbol, we can handle this as a
1479 // PC32 reloc.
1480 break;
1481
fdc2f80f 1482 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1483 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1484 case elfcpp::R_X86_64_GOTPC64:
1485 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1486 // We need a GOT section.
1487 target->got_section(symtab, layout);
ee9e9e86
ILT
1488 // For PLTOFF64, we'd normally want a PLT section, but since we
1489 // know this is a local symbol, no PLT is needed.
2e30d253
ILT
1490 break;
1491
0ffd9845
ILT
1492 case elfcpp::R_X86_64_GOT64:
1493 case elfcpp::R_X86_64_GOT32:
1494 case elfcpp::R_X86_64_GOTPCREL64:
1495 case elfcpp::R_X86_64_GOTPCREL:
ee9e9e86 1496 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
1497 {
1498 // The symbol requires a GOT entry.
1499 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1500 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
7223e9ca
ILT
1501
1502 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1503 // lets function pointers compare correctly with shared
1504 // libraries. Otherwise we would need an IRELATIVE reloc.
1505 bool is_new;
1506 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1507 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1508 else
1509 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1510 if (is_new)
0ffd9845
ILT
1511 {
1512 // If we are generating a shared object, we need to add a
7bf1f802 1513 // dynamic relocation for this symbol's GOT entry.
8851ecca 1514 if (parameters->options().output_is_position_independent())
0ffd9845
ILT
1515 {
1516 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7bf1f802
ILT
1517 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1518 if (r_type != elfcpp::R_X86_64_GOT32)
7223e9ca
ILT
1519 {
1520 unsigned int got_offset =
1521 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1522 rela_dyn->add_local_relative(object, r_sym,
1523 elfcpp::R_X86_64_RELATIVE,
1524 got, got_offset, 0);
1525 }
7bf1f802 1526 else
dceae3c1 1527 {
a036edd8
ILT
1528 this->check_non_pic(object, r_type);
1529
dceae3c1 1530 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
0a65a3a7
CC
1531 rela_dyn->add_local(
1532 object, r_sym, r_type, got,
1533 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
dceae3c1 1534 }
0ffd9845
ILT
1535 }
1536 }
ee9e9e86
ILT
1537 // For GOTPLT64, we'd normally want a PLT section, but since
1538 // we know this is a local symbol, no PLT is needed.
0ffd9845
ILT
1539 }
1540 break;
1541
2e30d253
ILT
1542 case elfcpp::R_X86_64_COPY:
1543 case elfcpp::R_X86_64_GLOB_DAT:
1544 case elfcpp::R_X86_64_JUMP_SLOT:
1545 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 1546 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 1547 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 1548 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1549 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 1550 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1551 gold_error(_("%s: unexpected reloc %u in object file"),
1552 object->name().c_str(), r_type);
2e30d253
ILT
1553 break;
1554
d61c17ea 1555 // These are initial tls relocs, which are expected when linking
56622147
ILT
1556 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1557 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1558 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1559 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1560 case elfcpp::R_X86_64_DTPOFF32:
1561 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1562 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1563 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253 1564 {
8851ecca 1565 bool output_is_shared = parameters->options().shared();
e041f13d
ILT
1566 const tls::Tls_optimization optimized_type
1567 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
2e30d253
ILT
1568 switch (r_type)
1569 {
56622147 1570 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
1571 if (optimized_type == tls::TLSOPT_NONE)
1572 {
1573 // Create a pair of GOT entries for the module index and
1574 // dtv-relative offset.
1575 Output_data_got<64, false>* got
1576 = target->got_section(symtab, layout);
1577 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d491d34e
ILT
1578 unsigned int shndx = lsym.get_st_shndx();
1579 bool is_ordinary;
1580 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1581 if (!is_ordinary)
1582 object->error(_("local symbol %u has bad shndx %u"),
1583 r_sym, shndx);
1584 else
1585 got->add_local_pair_with_rela(object, r_sym,
1586 shndx,
1587 GOT_TYPE_TLS_PAIR,
1588 target->rela_dyn_section(layout),
1589 elfcpp::R_X86_64_DTPMOD64, 0);
7bf1f802
ILT
1590 }
1591 else if (optimized_type != tls::TLSOPT_TO_LE)
1592 unsupported_reloc_local(object, r_type);
1593 break;
1594
56622147 1595 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 1596 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
1597 if (optimized_type == tls::TLSOPT_NONE)
1598 {
1599 // Create reserved PLT and GOT entries for the resolver.
1600 target->reserve_tlsdesc_entries(symtab, layout);
1601
a8df5856
ILT
1602 // Generate a double GOT entry with an
1603 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1604 // is resolved lazily, so the GOT entry needs to be in
1605 // an area in .got.plt, not .got. Call got_section to
1606 // make sure the section has been created.
1607 target->got_section(symtab, layout);
1608 Output_data_got<64, false>* got = target->got_tlsdesc_section();
c2b45e22 1609 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
e291e7b9
ILT
1610 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1611 {
1612 unsigned int got_offset = got->add_constant(0);
1613 got->add_constant(0);
1614 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1615 got_offset);
1616 Reloc_section* rt = target->rela_tlsdesc_section(layout);
1617 // We store the arguments we need in a vector, and
1618 // use the index into the vector as the parameter
1619 // to pass to the target specific routines.
1620 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1621 void* arg = reinterpret_cast<void*>(intarg);
1622 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1623 got, got_offset, 0);
1624 }
c2b45e22
CC
1625 }
1626 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 1627 unsupported_reloc_local(object, r_type);
2e30d253
ILT
1628 break;
1629
c2b45e22
CC
1630 case elfcpp::R_X86_64_TLSDESC_CALL:
1631 break;
1632
e041f13d 1633 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
1634 if (optimized_type == tls::TLSOPT_NONE)
1635 {
1636 // Create a GOT entry for the module index.
31d60480 1637 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
1638 }
1639 else if (optimized_type != tls::TLSOPT_TO_LE)
1640 unsupported_reloc_local(object, r_type);
1641 break;
1642
0ffd9845
ILT
1643 case elfcpp::R_X86_64_DTPOFF32:
1644 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
1645 break;
1646
56622147 1647 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 1648 layout->set_has_static_tls();
7bf1f802
ILT
1649 if (optimized_type == tls::TLSOPT_NONE)
1650 {
1651 // Create a GOT entry for the tp-relative offset.
1652 Output_data_got<64, false>* got
1653 = target->got_section(symtab, layout);
1654 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7 1655 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1656 target->rela_dyn_section(layout),
1657 elfcpp::R_X86_64_TPOFF64);
1658 }
1659 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
1660 unsupported_reloc_local(object, r_type);
1661 break;
0ffd9845 1662
56622147 1663 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 1664 layout->set_has_static_tls();
7bf1f802
ILT
1665 if (output_is_shared)
1666 unsupported_reloc_local(object, r_type);
2e30d253 1667 break;
e041f13d
ILT
1668
1669 default:
1670 gold_unreachable();
2e30d253
ILT
1671 }
1672 }
1673 break;
2e30d253 1674
fdc2f80f
ILT
1675 case elfcpp::R_X86_64_SIZE32:
1676 case elfcpp::R_X86_64_SIZE64:
2e30d253 1677 default:
75f2446e
ILT
1678 gold_error(_("%s: unsupported reloc %u against local symbol"),
1679 object->name().c_str(), r_type);
2e30d253
ILT
1680 break;
1681 }
1682}
1683
1684
e041f13d
ILT
1685// Report an unsupported relocation against a global symbol.
1686
1687void
1688Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1689 unsigned int r_type,
1690 Symbol* gsym)
1691{
75f2446e 1692 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12 1693 object->name().c_str(), r_type, gsym->demangled_name().c_str());
e041f13d
ILT
1694}
1695
ce97fa81 1696// Returns true if this relocation type could be that of a function pointer.
21bb3914
ST
1697inline bool
1698Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
1699{
21bb3914
ST
1700 switch (r_type)
1701 {
1702 case elfcpp::R_X86_64_64:
1703 case elfcpp::R_X86_64_32:
1704 case elfcpp::R_X86_64_32S:
1705 case elfcpp::R_X86_64_16:
1706 case elfcpp::R_X86_64_8:
ce97fa81
ST
1707 case elfcpp::R_X86_64_GOT64:
1708 case elfcpp::R_X86_64_GOT32:
1709 case elfcpp::R_X86_64_GOTPCREL64:
1710 case elfcpp::R_X86_64_GOTPCREL:
1711 case elfcpp::R_X86_64_GOTPLT64:
21bb3914
ST
1712 {
1713 return true;
1714 }
1715 }
1716 return false;
1717}
1718
1719// For safe ICF, scan a relocation for a local symbol to check if it
1720// corresponds to a function pointer being taken. In that case mark
1721// the function whose pointer was taken as not foldable.
1722
1723inline bool
1724Target_x86_64::Scan::local_reloc_may_be_function_pointer(
1725 Symbol_table* ,
1726 Layout* ,
1727 Target_x86_64* ,
1728 Sized_relobj<64, false>* ,
1729 unsigned int ,
1730 Output_section* ,
1731 const elfcpp::Rela<64, false>& ,
1732 unsigned int r_type,
1733 const elfcpp::Sym<64, false>&)
1734{
1735 // When building a shared library, do not fold any local symbols as it is
1736 // not possible to distinguish pointer taken versus a call by looking at
1737 // the relocation types.
1738 return (parameters->options().shared()
1739 || possible_function_pointer_reloc(r_type));
1740}
1741
1742// For safe ICF, scan a relocation for a global symbol to check if it
1743// corresponds to a function pointer being taken. In that case mark
1744// the function whose pointer was taken as not foldable.
1745
1746inline bool
1747Target_x86_64::Scan::global_reloc_may_be_function_pointer(
1748 Symbol_table*,
1749 Layout* ,
1750 Target_x86_64* ,
1751 Sized_relobj<64, false>* ,
1752 unsigned int ,
1753 Output_section* ,
1754 const elfcpp::Rela<64, false>& ,
1755 unsigned int r_type,
1756 Symbol* gsym)
1757{
1758 // When building a shared library, do not fold symbols whose visibility
1759 // is hidden, internal or protected.
1760 return ((parameters->options().shared()
1761 && (gsym->visibility() == elfcpp::STV_INTERNAL
1762 || gsym->visibility() == elfcpp::STV_PROTECTED
1763 || gsym->visibility() == elfcpp::STV_HIDDEN))
1764 || possible_function_pointer_reloc(r_type));
1765}
1766
2e30d253
ILT
1767// Scan a relocation for a global symbol.
1768
1769inline void
ad0f2072 1770Target_x86_64::Scan::global(Symbol_table* symtab,
d61c17ea
ILT
1771 Layout* layout,
1772 Target_x86_64* target,
1773 Sized_relobj<64, false>* object,
1774 unsigned int data_shndx,
4f4c5f80 1775 Output_section* output_section,
d61c17ea
ILT
1776 const elfcpp::Rela<64, false>& reloc,
1777 unsigned int r_type,
1778 Symbol* gsym)
2e30d253 1779{
7223e9ca
ILT
1780 // A STT_GNU_IFUNC symbol may require a PLT entry.
1781 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1782 && this->reloc_needs_plt_for_ifunc(object, r_type))
1783 target->make_plt_entry(symtab, layout, gsym);
1784
2e30d253
ILT
1785 switch (r_type)
1786 {
1787 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
1788 case elfcpp::R_X86_64_GNU_VTINHERIT:
1789 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
1790 break;
1791
1792 case elfcpp::R_X86_64_64:
2e30d253
ILT
1793 case elfcpp::R_X86_64_32:
1794 case elfcpp::R_X86_64_32S:
2e30d253 1795 case elfcpp::R_X86_64_16:
2e30d253 1796 case elfcpp::R_X86_64_8:
96f2030e 1797 {
d61c6bd4
ILT
1798 // Make a PLT entry if necessary.
1799 if (gsym->needs_plt_entry())
1800 {
1801 target->make_plt_entry(symtab, layout, gsym);
1802 // Since this is not a PC-relative relocation, we may be
1803 // taking the address of a function. In that case we need to
1804 // set the entry in the dynamic symbol table to the address of
1805 // the PLT entry.
8851ecca 1806 if (gsym->is_from_dynobj() && !parameters->options().shared())
d61c6bd4
ILT
1807 gsym->set_needs_dynsym_value();
1808 }
1809 // Make a dynamic relocation if necessary.
95a2c8d6 1810 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
d61c6bd4 1811 {
966d4097 1812 if (gsym->may_need_copy_reloc())
d61c6bd4 1813 {
12c0daef 1814 target->copy_reloc(symtab, layout, object,
7bf1f802 1815 data_shndx, output_section, gsym, reloc);
d61c6bd4 1816 }
7223e9ca
ILT
1817 else if (r_type == elfcpp::R_X86_64_64
1818 && gsym->type() == elfcpp::STT_GNU_IFUNC
1819 && gsym->can_use_relative_reloc(false)
1820 && !gsym->is_from_dynobj()
1821 && !gsym->is_undefined()
1822 && !gsym->is_preemptible())
1823 {
1824 // Use an IRELATIVE reloc for a locally defined
1825 // STT_GNU_IFUNC symbol. This makes a function
1826 // address in a PIE executable match the address in a
1827 // shared library that it links against.
1828 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1829 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
1830 rela_dyn->add_symbolless_global_addend(gsym, r_type,
1831 output_section, object,
1832 data_shndx,
1833 reloc.get_r_offset(),
1834 reloc.get_r_addend());
1835 }
d61c6bd4
ILT
1836 else if (r_type == elfcpp::R_X86_64_64
1837 && gsym->can_use_relative_reloc(false))
1838 {
1839 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7223e9ca
ILT
1840 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1841 output_section, object,
1842 data_shndx,
1843 reloc.get_r_offset(),
1844 reloc.get_r_addend());
d61c6bd4
ILT
1845 }
1846 else
1847 {
a036edd8 1848 this->check_non_pic(object, r_type);
96f2030e 1849 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
1850 rela_dyn->add_global(gsym, r_type, output_section, object,
1851 data_shndx, reloc.get_r_offset(),
96f2030e 1852 reloc.get_r_addend());
d61c6bd4
ILT
1853 }
1854 }
1855 }
1856 break;
1857
1858 case elfcpp::R_X86_64_PC64:
1859 case elfcpp::R_X86_64_PC32:
1860 case elfcpp::R_X86_64_PC16:
1861 case elfcpp::R_X86_64_PC8:
1862 {
1863 // Make a PLT entry if necessary.
1864 if (gsym->needs_plt_entry())
1865 target->make_plt_entry(symtab, layout, gsym);
1866 // Make a dynamic relocation if necessary.
95a2c8d6 1867 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
86849f1f 1868 {
966d4097 1869 if (gsym->may_need_copy_reloc())
d61c6bd4 1870 {
12c0daef 1871 target->copy_reloc(symtab, layout, object,
7bf1f802 1872 data_shndx, output_section, gsym, reloc);
d61c6bd4 1873 }
86849f1f 1874 else
d61c6bd4 1875 {
a036edd8 1876 this->check_non_pic(object, r_type);
d61c6bd4 1877 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
1878 rela_dyn->add_global(gsym, r_type, output_section, object,
1879 data_shndx, reloc.get_r_offset(),
d61c6bd4
ILT
1880 reloc.get_r_addend());
1881 }
86849f1f 1882 }
d61c6bd4 1883 }
2e30d253
ILT
1884 break;
1885
ff006520 1886 case elfcpp::R_X86_64_GOT64:
2e30d253 1887 case elfcpp::R_X86_64_GOT32:
ff006520
ILT
1888 case elfcpp::R_X86_64_GOTPCREL64:
1889 case elfcpp::R_X86_64_GOTPCREL:
1890 case elfcpp::R_X86_64_GOTPLT64:
2e30d253
ILT
1891 {
1892 // The symbol requires a GOT entry.
1893 Output_data_got<64, false>* got = target->got_section(symtab, layout);
7bf1f802 1894 if (gsym->final_value_is_known())
7223e9ca
ILT
1895 {
1896 // For a STT_GNU_IFUNC symbol we want the PLT address.
1897 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1898 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1899 else
1900 got->add_global(gsym, GOT_TYPE_STANDARD);
1901 }
7bf1f802
ILT
1902 else
1903 {
2e30d253
ILT
1904 // If this symbol is not fully resolved, we need to add a
1905 // dynamic relocation for it.
7bf1f802 1906 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7223e9ca
ILT
1907 if (gsym->is_from_dynobj()
1908 || gsym->is_undefined()
1909 || gsym->is_preemptible()
1910 || (gsym->type() == elfcpp::STT_GNU_IFUNC
1911 && parameters->options().output_is_position_independent()))
0a65a3a7 1912 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
7bf1f802
ILT
1913 elfcpp::R_X86_64_GLOB_DAT);
1914 else
2e30d253 1915 {
7223e9ca
ILT
1916 // For a STT_GNU_IFUNC symbol we want to write the PLT
1917 // offset into the GOT, so that function pointer
1918 // comparisons work correctly.
1919 bool is_new;
1920 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
1921 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
1922 else
1923 {
1924 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1925 // Tell the dynamic linker to use the PLT address
1926 // when resolving relocations.
1927 if (gsym->is_from_dynobj()
1928 && !parameters->options().shared())
1929 gsym->set_needs_dynsym_value();
1930 }
1931 if (is_new)
1932 {
1933 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
1934 rela_dyn->add_global_relative(gsym,
1935 elfcpp::R_X86_64_RELATIVE,
1936 got, got_off, 0);
1937 }
2e30d253
ILT
1938 }
1939 }
ee9e9e86
ILT
1940 // For GOTPLT64, we also need a PLT entry (but only if the
1941 // symbol is not fully resolved).
1942 if (r_type == elfcpp::R_X86_64_GOTPLT64
1943 && !gsym->final_value_is_known())
1944 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1945 }
1946 break;
1947
1948 case elfcpp::R_X86_64_PLT32:
1949 // If the symbol is fully resolved, this is just a PC32 reloc.
1950 // Otherwise we need a PLT entry.
1951 if (gsym->final_value_is_known())
1952 break;
96f2030e
ILT
1953 // If building a shared library, we can also skip the PLT entry
1954 // if the symbol is defined in the output file and is protected
1955 // or hidden.
1956 if (gsym->is_defined()
1957 && !gsym->is_from_dynobj()
1958 && !gsym->is_preemptible())
1959 break;
2e30d253
ILT
1960 target->make_plt_entry(symtab, layout, gsym);
1961 break;
1962
fdc2f80f 1963 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1964 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1965 case elfcpp::R_X86_64_GOTPC64:
1966 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1967 // We need a GOT section.
1968 target->got_section(symtab, layout);
ee9e9e86
ILT
1969 // For PLTOFF64, we also need a PLT entry (but only if the
1970 // symbol is not fully resolved).
1971 if (r_type == elfcpp::R_X86_64_PLTOFF64
1972 && !gsym->final_value_is_known())
1973 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1974 break;
1975
2e30d253
ILT
1976 case elfcpp::R_X86_64_COPY:
1977 case elfcpp::R_X86_64_GLOB_DAT:
1978 case elfcpp::R_X86_64_JUMP_SLOT:
1979 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 1980 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 1981 // These are outstanding tls relocs, which are unexpected when linking
e822f2b1 1982 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1983 case elfcpp::R_X86_64_DTPMOD64:
e822f2b1 1984 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1985 gold_error(_("%s: unexpected reloc %u in object file"),
1986 object->name().c_str(), r_type);
2e30d253 1987 break;
2e30d253 1988
d61c17ea 1989 // These are initial tls relocs, which are expected for global()
56622147
ILT
1990 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1991 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1992 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1993 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1994 case elfcpp::R_X86_64_DTPOFF32:
1995 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1996 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1997 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253
ILT
1998 {
1999 const bool is_final = gsym->final_value_is_known();
e041f13d
ILT
2000 const tls::Tls_optimization optimized_type
2001 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
2002 switch (r_type)
2003 {
56622147 2004 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
2005 if (optimized_type == tls::TLSOPT_NONE)
2006 {
2007 // Create a pair of GOT entries for the module index and
2008 // dtv-relative offset.
2009 Output_data_got<64, false>* got
2010 = target->got_section(symtab, layout);
0a65a3a7
CC
2011 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2012 target->rela_dyn_section(layout),
2013 elfcpp::R_X86_64_DTPMOD64,
2014 elfcpp::R_X86_64_DTPOFF64);
7bf1f802
ILT
2015 }
2016 else if (optimized_type == tls::TLSOPT_TO_IE)
2017 {
2018 // Create a GOT entry for the tp-relative offset.
2019 Output_data_got<64, false>* got
2020 = target->got_section(symtab, layout);
0a65a3a7 2021 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
2022 target->rela_dyn_section(layout),
2023 elfcpp::R_X86_64_TPOFF64);
2024 }
2025 else if (optimized_type != tls::TLSOPT_TO_LE)
2026 unsupported_reloc_global(object, r_type, gsym);
2027 break;
2028
56622147 2029 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 2030 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
2031 if (optimized_type == tls::TLSOPT_NONE)
2032 {
2033 // Create reserved PLT and GOT entries for the resolver.
2034 target->reserve_tlsdesc_entries(symtab, layout);
2035
a8df5856
ILT
2036 // Create a double GOT entry with an R_X86_64_TLSDESC
2037 // reloc. The R_X86_64_TLSDESC reloc is resolved
2038 // lazily, so the GOT entry needs to be in an area in
2039 // .got.plt, not .got. Call got_section to make sure
2040 // the section has been created.
2041 target->got_section(symtab, layout);
2042 Output_data_got<64, false>* got = target->got_tlsdesc_section();
ca09d69a 2043 Reloc_section* rt = target->rela_tlsdesc_section(layout);
e291e7b9 2044 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
c2b45e22
CC
2045 elfcpp::R_X86_64_TLSDESC, 0);
2046 }
2047 else if (optimized_type == tls::TLSOPT_TO_IE)
2048 {
2049 // Create a GOT entry for the tp-relative offset.
2050 Output_data_got<64, false>* got
2051 = target->got_section(symtab, layout);
2052 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2053 target->rela_dyn_section(layout),
2054 elfcpp::R_X86_64_TPOFF64);
2055 }
2056 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 2057 unsupported_reloc_global(object, r_type, gsym);
2e30d253
ILT
2058 break;
2059
c2b45e22
CC
2060 case elfcpp::R_X86_64_TLSDESC_CALL:
2061 break;
2062
e041f13d 2063 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
2064 if (optimized_type == tls::TLSOPT_NONE)
2065 {
2066 // Create a GOT entry for the module index.
31d60480 2067 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
2068 }
2069 else if (optimized_type != tls::TLSOPT_TO_LE)
2070 unsupported_reloc_global(object, r_type, gsym);
2071 break;
2072
0ffd9845
ILT
2073 case elfcpp::R_X86_64_DTPOFF32:
2074 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
2075 break;
2076
56622147 2077 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 2078 layout->set_has_static_tls();
7bf1f802
ILT
2079 if (optimized_type == tls::TLSOPT_NONE)
2080 {
2081 // Create a GOT entry for the tp-relative offset.
2082 Output_data_got<64, false>* got
2083 = target->got_section(symtab, layout);
0a65a3a7 2084 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
2085 target->rela_dyn_section(layout),
2086 elfcpp::R_X86_64_TPOFF64);
2087 }
2088 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
2089 unsupported_reloc_global(object, r_type, gsym);
2090 break;
0ffd9845 2091
56622147 2092 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 2093 layout->set_has_static_tls();
8851ecca 2094 if (parameters->options().shared())
7bf1f802 2095 unsupported_reloc_local(object, r_type);
2e30d253 2096 break;
e041f13d
ILT
2097
2098 default:
2099 gold_unreachable();
2e30d253
ILT
2100 }
2101 }
2102 break;
fdc2f80f
ILT
2103
2104 case elfcpp::R_X86_64_SIZE32:
2105 case elfcpp::R_X86_64_SIZE64:
2e30d253 2106 default:
75f2446e 2107 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12
ILT
2108 object->name().c_str(), r_type,
2109 gsym->demangled_name().c_str());
2e30d253
ILT
2110 break;
2111 }
2112}
2113
6d03d481 2114void
ad0f2072 2115Target_x86_64::gc_process_relocs(Symbol_table* symtab,
6d03d481
ST
2116 Layout* layout,
2117 Sized_relobj<64, false>* object,
2118 unsigned int data_shndx,
2119 unsigned int sh_type,
2120 const unsigned char* prelocs,
2121 size_t reloc_count,
2122 Output_section* output_section,
2123 bool needs_special_offset_handling,
2124 size_t local_symbol_count,
2125 const unsigned char* plocal_symbols)
2126{
2127
2128 if (sh_type == elfcpp::SHT_REL)
2129 {
2130 return;
2131 }
2132
2133 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
41cbeecc
ST
2134 Target_x86_64::Scan,
2135 Target_x86_64::Relocatable_size_for_reloc>(
6d03d481
ST
2136 symtab,
2137 layout,
2138 this,
2139 object,
2140 data_shndx,
2141 prelocs,
2142 reloc_count,
2143 output_section,
2144 needs_special_offset_handling,
2145 local_symbol_count,
2146 plocal_symbols);
2147
2148}
2e30d253
ILT
2149// Scan relocations for a section.
2150
2151void
ad0f2072 2152Target_x86_64::scan_relocs(Symbol_table* symtab,
d61c17ea
ILT
2153 Layout* layout,
2154 Sized_relobj<64, false>* object,
2155 unsigned int data_shndx,
2156 unsigned int sh_type,
2157 const unsigned char* prelocs,
2158 size_t reloc_count,
730cdc88
ILT
2159 Output_section* output_section,
2160 bool needs_special_offset_handling,
d61c17ea 2161 size_t local_symbol_count,
730cdc88 2162 const unsigned char* plocal_symbols)
2e30d253
ILT
2163{
2164 if (sh_type == elfcpp::SHT_REL)
2165 {
75f2446e
ILT
2166 gold_error(_("%s: unsupported REL reloc section"),
2167 object->name().c_str());
2168 return;
2e30d253
ILT
2169 }
2170
2171 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2172 Target_x86_64::Scan>(
2e30d253
ILT
2173 symtab,
2174 layout,
2175 this,
2176 object,
2177 data_shndx,
2178 prelocs,
2179 reloc_count,
730cdc88
ILT
2180 output_section,
2181 needs_special_offset_handling,
2e30d253 2182 local_symbol_count,
730cdc88 2183 plocal_symbols);
2e30d253
ILT
2184}
2185
2186// Finalize the sections.
2187
2188void
f59f41f3
DK
2189Target_x86_64::do_finalize_sections(
2190 Layout* layout,
2191 const Input_objects*,
e785ec03 2192 Symbol_table* symtab)
2e30d253 2193{
ea715a34
ILT
2194 const Reloc_section* rel_plt = (this->plt_ == NULL
2195 ? NULL
e291e7b9 2196 : this->plt_->rela_plt());
ea715a34 2197 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
612a8d3d 2198 this->rela_dyn_, true, false);
ea715a34 2199
2e30d253
ILT
2200 // Fill in some more dynamic tags.
2201 Output_data_dynamic* const odyn = layout->dynamic_data();
2202 if (odyn != NULL)
2203 {
22b127cc 2204 if (this->plt_ != NULL
ea715a34
ILT
2205 && this->plt_->output_section() != NULL
2206 && this->plt_->has_tlsdesc_entry())
2e30d253 2207 {
ea715a34
ILT
2208 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2209 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2210 this->got_->finalize_data_size();
2211 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2212 this->plt_, plt_offset);
2213 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2214 this->got_, got_offset);
2e30d253
ILT
2215 }
2216 }
2217
2218 // Emit any relocs we saved in an attempt to avoid generating COPY
2219 // relocs.
12c0daef
ILT
2220 if (this->copy_relocs_.any_saved_relocs())
2221 this->copy_relocs_.emit(this->rela_dyn_section(layout));
e785ec03
ILT
2222
2223 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2224 // the .got.plt section.
2225 Symbol* sym = this->global_offset_table_;
2226 if (sym != NULL)
2227 {
2228 uint64_t data_size = this->got_plt_->current_data_size();
2229 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2230 }
2e30d253
ILT
2231}
2232
2233// Perform a relocation.
2234
2235inline bool
2236Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2237 Target_x86_64* target,
031cdbed 2238 Output_section*,
2e30d253 2239 size_t relnum,
0ffd9845 2240 const elfcpp::Rela<64, false>& rela,
2e30d253
ILT
2241 unsigned int r_type,
2242 const Sized_symbol<64>* gsym,
2243 const Symbol_value<64>* psymval,
2244 unsigned char* view,
2245 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2246 section_size_type view_size)
2e30d253
ILT
2247{
2248 if (this->skip_call_tls_get_addr_)
2249 {
5efc7cd2
CC
2250 if ((r_type != elfcpp::R_X86_64_PLT32
2251 && r_type != elfcpp::R_X86_64_PC32)
2e30d253 2252 || gsym == NULL
0ffd9845 2253 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2e30d253 2254 {
75f2446e
ILT
2255 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2256 _("missing expected TLS relocation"));
2257 }
2258 else
2259 {
2260 this->skip_call_tls_get_addr_ = false;
2261 return false;
2e30d253 2262 }
2e30d253
ILT
2263 }
2264
7223e9ca
ILT
2265 const Sized_relobj<64, false>* object = relinfo->object;
2266
2267 // Pick the value to use for symbols defined in the PLT.
2e30d253 2268 Symbol_value<64> symval;
96f2030e 2269 if (gsym != NULL
95a2c8d6 2270 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2e30d253
ILT
2271 {
2272 symval.set_output_value(target->plt_section()->address()
2273 + gsym->plt_offset());
2274 psymval = &symval;
2275 }
7223e9ca
ILT
2276 else if (gsym == NULL && psymval->is_ifunc_symbol())
2277 {
2278 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2279 if (object->local_has_plt_offset(r_sym))
2280 {
2281 symval.set_output_value(target->plt_section()->address()
2282 + object->local_plt_offset(r_sym));
2283 psymval = &symval;
2284 }
2285 }
2e30d253 2286
0ffd9845
ILT
2287 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2288
2289 // Get the GOT offset if needed.
96f2030e
ILT
2290 // The GOT pointer points to the end of the GOT section.
2291 // We need to subtract the size of the GOT section to get
2292 // the actual offset to use in the relocation.
0ffd9845
ILT
2293 bool have_got_offset = false;
2294 unsigned int got_offset = 0;
2295 switch (r_type)
2296 {
2297 case elfcpp::R_X86_64_GOT32:
2298 case elfcpp::R_X86_64_GOT64:
2299 case elfcpp::R_X86_64_GOTPLT64:
2300 case elfcpp::R_X86_64_GOTPCREL:
2301 case elfcpp::R_X86_64_GOTPCREL64:
2302 if (gsym != NULL)
2303 {
0a65a3a7
CC
2304 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2305 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
0ffd9845
ILT
2306 }
2307 else
2308 {
2309 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
2310 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2311 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2312 - target->got_size());
0ffd9845
ILT
2313 }
2314 have_got_offset = true;
2315 break;
2316
2317 default:
2318 break;
2319 }
2e30d253
ILT
2320
2321 switch (r_type)
2322 {
2323 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
2324 case elfcpp::R_X86_64_GNU_VTINHERIT:
2325 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
2326 break;
2327
2328 case elfcpp::R_X86_64_64:
2329 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2330 break;
2331
2332 case elfcpp::R_X86_64_PC64:
2333 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2334 address);
2335 break;
2336
2337 case elfcpp::R_X86_64_32:
7bb3655e
ILT
2338 // FIXME: we need to verify that value + addend fits into 32 bits:
2339 // uint64_t x = value + addend;
2340 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2341 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2e30d253
ILT
2342 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2343 break;
2344
2345 case elfcpp::R_X86_64_32S:
7bb3655e
ILT
2346 // FIXME: we need to verify that value + addend fits into 32 bits:
2347 // int64_t x = value + addend; // note this quantity is signed!
2348 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2e30d253
ILT
2349 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2350 break;
2351
2352 case elfcpp::R_X86_64_PC32:
2353 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2354 address);
2355 break;
2356
2357 case elfcpp::R_X86_64_16:
2358 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2359 break;
2360
2361 case elfcpp::R_X86_64_PC16:
2362 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2363 address);
2364 break;
2365
2366 case elfcpp::R_X86_64_8:
2367 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2368 break;
2369
2370 case elfcpp::R_X86_64_PC8:
2371 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2372 address);
2373 break;
2374
2375 case elfcpp::R_X86_64_PLT32:
f389a824
ILT
2376 gold_assert(gsym == NULL
2377 || gsym->has_plt_offset()
99f8faca
ILT
2378 || gsym->final_value_is_known()
2379 || (gsym->is_defined()
2380 && !gsym->is_from_dynobj()
2381 && !gsym->is_preemptible()));
ee9e9e86
ILT
2382 // Note: while this code looks the same as for R_X86_64_PC32, it
2383 // behaves differently because psymval was set to point to
2384 // the PLT entry, rather than the symbol, in Scan::global().
2e30d253
ILT
2385 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2386 address);
2387 break;
2388
ee9e9e86
ILT
2389 case elfcpp::R_X86_64_PLTOFF64:
2390 {
2391 gold_assert(gsym);
2392 gold_assert(gsym->has_plt_offset()
2393 || gsym->final_value_is_known());
2394 elfcpp::Elf_types<64>::Elf_Addr got_address;
2395 got_address = target->got_section(NULL, NULL)->address();
c1866bd5
ILT
2396 Relocate_functions<64, false>::rela64(view, object, psymval,
2397 addend - got_address);
ee9e9e86
ILT
2398 }
2399
2e30d253 2400 case elfcpp::R_X86_64_GOT32:
0ffd9845
ILT
2401 gold_assert(have_got_offset);
2402 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2e30d253
ILT
2403 break;
2404
e822f2b1
ILT
2405 case elfcpp::R_X86_64_GOTPC32:
2406 {
2407 gold_assert(gsym);
2408 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2409 value = target->got_plt_section()->address();
e822f2b1
ILT
2410 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2411 }
2412 break;
2413
2414 case elfcpp::R_X86_64_GOT64:
2415 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2416 // Since we always add a PLT entry, this is equivalent.
fdc2f80f 2417 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
2418 gold_assert(have_got_offset);
2419 Relocate_functions<64, false>::rela64(view, got_offset, addend);
e822f2b1
ILT
2420 break;
2421
2422 case elfcpp::R_X86_64_GOTPC64:
2423 {
2424 gold_assert(gsym);
2425 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2426 value = target->got_plt_section()->address();
e822f2b1
ILT
2427 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2428 }
2429 break;
2430
2e30d253
ILT
2431 case elfcpp::R_X86_64_GOTOFF64:
2432 {
2433 elfcpp::Elf_types<64>::Elf_Addr value;
2434 value = (psymval->value(object, 0)
96f2030e 2435 - target->got_plt_section()->address());
2e30d253
ILT
2436 Relocate_functions<64, false>::rela64(view, value, addend);
2437 }
2438 break;
2439
2440 case elfcpp::R_X86_64_GOTPCREL:
2441 {
0ffd9845
ILT
2442 gold_assert(have_got_offset);
2443 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2444 value = target->got_plt_section()->address() + got_offset;
0ffd9845 2445 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2e30d253
ILT
2446 }
2447 break;
2448
e822f2b1
ILT
2449 case elfcpp::R_X86_64_GOTPCREL64:
2450 {
0ffd9845
ILT
2451 gold_assert(have_got_offset);
2452 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2453 value = target->got_plt_section()->address() + got_offset;
0ffd9845 2454 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
e822f2b1
ILT
2455 }
2456 break;
2457
2e30d253
ILT
2458 case elfcpp::R_X86_64_COPY:
2459 case elfcpp::R_X86_64_GLOB_DAT:
2460 case elfcpp::R_X86_64_JUMP_SLOT:
2461 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 2462 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 2463 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 2464 case elfcpp::R_X86_64_TPOFF64:
2e30d253 2465 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 2466 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
2467 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2468 _("unexpected reloc %u in object file"),
2469 r_type);
2e30d253
ILT
2470 break;
2471
d61c17ea 2472 // These are initial tls relocs, which are expected when linking
56622147
ILT
2473 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2474 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 2475 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 2476 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
2477 case elfcpp::R_X86_64_DTPOFF32:
2478 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
2479 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2480 case elfcpp::R_X86_64_TPOFF32: // Local-exec
7bf1f802
ILT
2481 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2482 view, address, view_size);
2e30d253 2483 break;
2e30d253 2484
fdc2f80f
ILT
2485 case elfcpp::R_X86_64_SIZE32:
2486 case elfcpp::R_X86_64_SIZE64:
2e30d253 2487 default:
75f2446e
ILT
2488 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2489 _("unsupported reloc %u"),
2490 r_type);
2e30d253
ILT
2491 break;
2492 }
2493
2494 return true;
2495}
2496
2497// Perform a TLS relocation.
2498
2499inline void
d61c17ea 2500Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
7bf1f802 2501 Target_x86_64* target,
d61c17ea 2502 size_t relnum,
72ec2876 2503 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
2504 unsigned int r_type,
2505 const Sized_symbol<64>* gsym,
2506 const Symbol_value<64>* psymval,
2507 unsigned char* view,
6a41d30b 2508 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2509 section_size_type view_size)
2e30d253 2510{
2e30d253 2511 Output_segment* tls_segment = relinfo->layout->tls_segment();
7bf1f802
ILT
2512
2513 const Sized_relobj<64, false>* object = relinfo->object;
6a41d30b 2514 const elfcpp::Elf_Xword addend = rela.get_r_addend();
36171d64
CC
2515 elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
2516 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2e30d253
ILT
2517
2518 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2519
2520 const bool is_final = (gsym == NULL
b3705d2a 2521 ? !parameters->options().shared()
2e30d253 2522 : gsym->final_value_is_known());
36171d64 2523 tls::Tls_optimization optimized_type
e041f13d 2524 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
2525 switch (r_type)
2526 {
56622147 2527 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
36171d64
CC
2528 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2529 {
2530 // If this code sequence is used in a non-executable section,
2531 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2532 // on the assumption that it's being used by itself in a debug
2533 // section. Therefore, in the unlikely event that the code
2534 // sequence appears in a non-executable section, we simply
2535 // leave it unoptimized.
2536 optimized_type = tls::TLSOPT_NONE;
2537 }
e041f13d 2538 if (optimized_type == tls::TLSOPT_TO_LE)
2e30d253 2539 {
7bf1f802 2540 gold_assert(tls_segment != NULL);
2e30d253 2541 this->tls_gd_to_le(relinfo, relnum, tls_segment,
72ec2876 2542 rela, r_type, value, view,
2e30d253
ILT
2543 view_size);
2544 break;
2545 }
7bf1f802
ILT
2546 else
2547 {
c2b45e22
CC
2548 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2549 ? GOT_TYPE_TLS_OFFSET
2550 : GOT_TYPE_TLS_PAIR);
7bf1f802
ILT
2551 unsigned int got_offset;
2552 if (gsym != NULL)
2553 {
c2b45e22
CC
2554 gold_assert(gsym->has_got_offset(got_type));
2555 got_offset = gsym->got_offset(got_type) - target->got_size();
7bf1f802
ILT
2556 }
2557 else
2558 {
2559 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
c2b45e22
CC
2560 gold_assert(object->local_has_got_offset(r_sym, got_type));
2561 got_offset = (object->local_got_offset(r_sym, got_type)
7bf1f802
ILT
2562 - target->got_size());
2563 }
2564 if (optimized_type == tls::TLSOPT_TO_IE)
2565 {
2566 gold_assert(tls_segment != NULL);
c2b45e22 2567 value = target->got_plt_section()->address() + got_offset;
7bf1f802 2568 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
c2b45e22 2569 value, view, address, view_size);
7bf1f802
ILT
2570 break;
2571 }
2572 else if (optimized_type == tls::TLSOPT_NONE)
2573 {
2574 // Relocate the field with the offset of the pair of GOT
2575 // entries.
6a41d30b
ILT
2576 value = target->got_plt_section()->address() + got_offset;
2577 Relocate_functions<64, false>::pcrela32(view, value, addend,
2578 address);
7bf1f802
ILT
2579 break;
2580 }
2581 }
72ec2876 2582 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 2583 _("unsupported reloc %u"), r_type);
2e30d253
ILT
2584 break;
2585
c2b45e22
CC
2586 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2587 case elfcpp::R_X86_64_TLSDESC_CALL:
36171d64
CC
2588 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2589 {
2590 // See above comment for R_X86_64_TLSGD.
2591 optimized_type = tls::TLSOPT_NONE;
2592 }
c2b45e22
CC
2593 if (optimized_type == tls::TLSOPT_TO_LE)
2594 {
2595 gold_assert(tls_segment != NULL);
2596 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2597 rela, r_type, value, view,
2598 view_size);
2599 break;
2600 }
2601 else
2602 {
2603 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2604 ? GOT_TYPE_TLS_OFFSET
2605 : GOT_TYPE_TLS_DESC);
a8df5856
ILT
2606 unsigned int got_offset = 0;
2607 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2608 && optimized_type == tls::TLSOPT_NONE)
2609 {
2610 // We created GOT entries in the .got.tlsdesc portion of
2611 // the .got.plt section, but the offset stored in the
2612 // symbol is the offset within .got.tlsdesc.
2613 got_offset = (target->got_size()
2614 + target->got_plt_section()->data_size());
2615 }
c2b45e22
CC
2616 if (gsym != NULL)
2617 {
2618 gold_assert(gsym->has_got_offset(got_type));
a8df5856 2619 got_offset += gsym->got_offset(got_type) - target->got_size();
c2b45e22
CC
2620 }
2621 else
2622 {
2623 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2624 gold_assert(object->local_has_got_offset(r_sym, got_type));
a8df5856
ILT
2625 got_offset += (object->local_got_offset(r_sym, got_type)
2626 - target->got_size());
c2b45e22
CC
2627 }
2628 if (optimized_type == tls::TLSOPT_TO_IE)
2629 {
2630 gold_assert(tls_segment != NULL);
2631 value = target->got_plt_section()->address() + got_offset;
2632 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2633 rela, r_type, value, view, address,
2634 view_size);
2635 break;
2636 }
2637 else if (optimized_type == tls::TLSOPT_NONE)
2638 {
2639 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2640 {
2641 // Relocate the field with the offset of the pair of GOT
2642 // entries.
2643 value = target->got_plt_section()->address() + got_offset;
2644 Relocate_functions<64, false>::pcrela32(view, value, addend,
2645 address);
2646 }
2647 break;
2648 }
2649 }
2650 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2651 _("unsupported reloc %u"), r_type);
2652 break;
2653
56622147 2654 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
36171d64
CC
2655 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2656 {
2657 // See above comment for R_X86_64_TLSGD.
2658 optimized_type = tls::TLSOPT_NONE;
2659 }
e041f13d
ILT
2660 if (optimized_type == tls::TLSOPT_TO_LE)
2661 {
7bf1f802 2662 gold_assert(tls_segment != NULL);
72ec2876
ILT
2663 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2664 value, view, view_size);
2665 break;
e041f13d 2666 }
7bf1f802
ILT
2667 else if (optimized_type == tls::TLSOPT_NONE)
2668 {
2669 // Relocate the field with the offset of the GOT entry for
2670 // the module index.
2671 unsigned int got_offset;
31d60480
ILT
2672 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2673 - target->got_size());
6a41d30b
ILT
2674 value = target->got_plt_section()->address() + got_offset;
2675 Relocate_functions<64, false>::pcrela32(view, value, addend,
2676 address);
7bf1f802
ILT
2677 break;
2678 }
72ec2876 2679 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 2680 _("unsupported reloc %u"), r_type);
2e30d253 2681 break;
0ffd9845
ILT
2682
2683 case elfcpp::R_X86_64_DTPOFF32:
36171d64
CC
2684 // This relocation type is used in debugging information.
2685 // In that case we need to not optimize the value. If the
2686 // section is not executable, then we assume we should not
2687 // optimize this reloc. See comments above for R_X86_64_TLSGD,
2688 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
2689 // R_X86_64_TLSLD.
2690 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2691 {
2692 gold_assert(tls_segment != NULL);
2693 value -= tls_segment->memsz();
2694 }
d85c80a3 2695 Relocate_functions<64, false>::rela32(view, value, addend);
0ffd9845
ILT
2696 break;
2697
2698 case elfcpp::R_X86_64_DTPOFF64:
36171d64
CC
2699 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
2700 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2701 {
2702 gold_assert(tls_segment != NULL);
2703 value -= tls_segment->memsz();
2704 }
d85c80a3 2705 Relocate_functions<64, false>::rela64(view, value, addend);
0ffd9845 2706 break;
2e30d253 2707
56622147
ILT
2708 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2709 if (optimized_type == tls::TLSOPT_TO_LE)
2710 {
7bf1f802 2711 gold_assert(tls_segment != NULL);
56622147
ILT
2712 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2713 rela, r_type, value, view,
2714 view_size);
2715 break;
2716 }
7bf1f802
ILT
2717 else if (optimized_type == tls::TLSOPT_NONE)
2718 {
2719 // Relocate the field with the offset of the GOT entry for
2720 // the tp-relative offset of the symbol.
2721 unsigned int got_offset;
2722 if (gsym != NULL)
2723 {
0a65a3a7
CC
2724 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2725 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2726 - target->got_size());
7bf1f802
ILT
2727 }
2728 else
2729 {
2730 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
2731 gold_assert(object->local_has_got_offset(r_sym,
2732 GOT_TYPE_TLS_OFFSET));
2733 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
7bf1f802
ILT
2734 - target->got_size());
2735 }
6a41d30b
ILT
2736 value = target->got_plt_section()->address() + got_offset;
2737 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
7bf1f802
ILT
2738 break;
2739 }
56622147
ILT
2740 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2741 _("unsupported reloc type %u"),
2742 r_type);
2743 break;
0ffd9845 2744
56622147 2745 case elfcpp::R_X86_64_TPOFF32: // Local-exec
6a41d30b 2746 value -= tls_segment->memsz();
d85c80a3 2747 Relocate_functions<64, false>::rela32(view, value, addend);
56622147 2748 break;
2e30d253 2749 }
2e30d253
ILT
2750}
2751
7bf1f802
ILT
2752// Do a relocation in which we convert a TLS General-Dynamic to an
2753// Initial-Exec.
2754
2755inline void
2756Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2757 size_t relnum,
c2b45e22 2758 Output_segment*,
7bf1f802
ILT
2759 const elfcpp::Rela<64, false>& rela,
2760 unsigned int,
2761 elfcpp::Elf_types<64>::Elf_Addr value,
2762 unsigned char* view,
c2b45e22 2763 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2764 section_size_type view_size)
7bf1f802
ILT
2765{
2766 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2767 // .word 0x6666; rex64; call __tls_get_addr
2768 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2769
2770 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2771 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2772
2773 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2774 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2775 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2776 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2777
2778 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2779
c2b45e22
CC
2780 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2781 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
7bf1f802
ILT
2782
2783 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2784 // We can skip it.
2785 this->skip_call_tls_get_addr_ = true;
2786}
2787
e041f13d 2788// Do a relocation in which we convert a TLS General-Dynamic to a
2e30d253
ILT
2789// Local-Exec.
2790
2791inline void
d61c17ea
ILT
2792Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2793 size_t relnum,
2794 Output_segment* tls_segment,
72ec2876 2795 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
2796 unsigned int,
2797 elfcpp::Elf_types<64>::Elf_Addr value,
2798 unsigned char* view,
fe8718a4 2799 section_size_type view_size)
2e30d253 2800{
0ffd9845
ILT
2801 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2802 // .word 0x6666; rex64; call __tls_get_addr
2803 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2e30d253 2804
72ec2876
ILT
2805 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2806 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2e30d253 2807
72ec2876
ILT
2808 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2809 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2810 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2811 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2e30d253 2812
0ffd9845 2813 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2e30d253 2814
6a41d30b 2815 value -= tls_segment->memsz();
0ffd9845 2816 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2e30d253
ILT
2817
2818 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2819 // We can skip it.
2820 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
2821}
2822
c2b45e22
CC
2823// Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2824
2825inline void
2826Target_x86_64::Relocate::tls_desc_gd_to_ie(
2827 const Relocate_info<64, false>* relinfo,
2828 size_t relnum,
2829 Output_segment*,
2830 const elfcpp::Rela<64, false>& rela,
2831 unsigned int r_type,
2832 elfcpp::Elf_types<64>::Elf_Addr value,
2833 unsigned char* view,
2834 elfcpp::Elf_types<64>::Elf_Addr address,
2835 section_size_type view_size)
2836{
2837 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2838 {
2839 // leaq foo@tlsdesc(%rip), %rax
2840 // ==> movq foo@gottpoff(%rip), %rax
2841 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2842 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2843 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2844 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2845 view[-2] = 0x8b;
2846 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2847 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2848 }
2849 else
2850 {
2851 // call *foo@tlscall(%rax)
2852 // ==> nop; nop
2853 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2854 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2855 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2856 view[0] == 0xff && view[1] == 0x10);
2857 view[0] = 0x66;
2858 view[1] = 0x90;
2859 }
2860}
2861
2862// Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2863
2864inline void
2865Target_x86_64::Relocate::tls_desc_gd_to_le(
2866 const Relocate_info<64, false>* relinfo,
2867 size_t relnum,
2868 Output_segment* tls_segment,
2869 const elfcpp::Rela<64, false>& rela,
2870 unsigned int r_type,
2871 elfcpp::Elf_types<64>::Elf_Addr value,
2872 unsigned char* view,
2873 section_size_type view_size)
2874{
2875 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2876 {
2877 // leaq foo@tlsdesc(%rip), %rax
2878 // ==> movq foo@tpoff, %rax
2879 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2880 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2881 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2882 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2883 view[-2] = 0xc7;
2884 view[-1] = 0xc0;
2885 value -= tls_segment->memsz();
2886 Relocate_functions<64, false>::rela32(view, value, 0);
2887 }
2888 else
2889 {
2890 // call *foo@tlscall(%rax)
2891 // ==> nop; nop
2892 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2893 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2894 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2895 view[0] == 0xff && view[1] == 0x10);
2896 view[0] = 0x66;
2897 view[1] = 0x90;
2898 }
2899}
2900
2e30d253 2901inline void
72ec2876
ILT
2902Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2903 size_t relnum,
2904 Output_segment*,
2905 const elfcpp::Rela<64, false>& rela,
2906 unsigned int,
2907 elfcpp::Elf_types<64>::Elf_Addr,
2908 unsigned char* view,
fe8718a4 2909 section_size_type view_size)
2e30d253 2910{
72ec2876
ILT
2911 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2912 // ... leq foo@dtpoff(%rax),%reg
2913 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2e30d253 2914
72ec2876
ILT
2915 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2916 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2e30d253 2917
72ec2876
ILT
2918 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2919 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2920
2921 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2922
2923 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2924
2925 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2926 // We can skip it.
2927 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
2928}
2929
56622147
ILT
2930// Do a relocation in which we convert a TLS Initial-Exec to a
2931// Local-Exec.
2932
2933inline void
2934Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2935 size_t relnum,
2936 Output_segment* tls_segment,
2937 const elfcpp::Rela<64, false>& rela,
2938 unsigned int,
2939 elfcpp::Elf_types<64>::Elf_Addr value,
2940 unsigned char* view,
fe8718a4 2941 section_size_type view_size)
56622147
ILT
2942{
2943 // We need to examine the opcodes to figure out which instruction we
2944 // are looking at.
2945
2946 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2947 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2948
2949 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2950 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2951
2952 unsigned char op1 = view[-3];
2953 unsigned char op2 = view[-2];
2954 unsigned char op3 = view[-1];
2955 unsigned char reg = op3 >> 3;
2956
2957 if (op2 == 0x8b)
2958 {
2959 // movq
2960 if (op1 == 0x4c)
2961 view[-3] = 0x49;
2962 view[-2] = 0xc7;
2963 view[-1] = 0xc0 | reg;
2964 }
2965 else if (reg == 4)
2966 {
2967 // Special handling for %rsp.
2968 if (op1 == 0x4c)
2969 view[-3] = 0x49;
2970 view[-2] = 0x81;
2971 view[-1] = 0xc0 | reg;
2972 }
2973 else
2974 {
2975 // addq
2976 if (op1 == 0x4c)
2977 view[-3] = 0x4d;
2978 view[-2] = 0x8d;
2979 view[-1] = 0x80 | reg | (reg << 3);
2980 }
2981
6a41d30b 2982 value -= tls_segment->memsz();
56622147
ILT
2983 Relocate_functions<64, false>::rela32(view, value, 0);
2984}
2985
2e30d253
ILT
2986// Relocate section data.
2987
2988void
364c7fa5
ILT
2989Target_x86_64::relocate_section(
2990 const Relocate_info<64, false>* relinfo,
2991 unsigned int sh_type,
2992 const unsigned char* prelocs,
2993 size_t reloc_count,
2994 Output_section* output_section,
2995 bool needs_special_offset_handling,
2996 unsigned char* view,
2997 elfcpp::Elf_types<64>::Elf_Addr address,
2998 section_size_type view_size,
2999 const Reloc_symbol_changes* reloc_symbol_changes)
2e30d253
ILT
3000{
3001 gold_assert(sh_type == elfcpp::SHT_RELA);
3002
3003 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3004 Target_x86_64::Relocate>(
3005 relinfo,
3006 this,
3007 prelocs,
3008 reloc_count,
730cdc88
ILT
3009 output_section,
3010 needs_special_offset_handling,
2e30d253
ILT
3011 view,
3012 address,
364c7fa5
ILT
3013 view_size,
3014 reloc_symbol_changes);
2e30d253
ILT
3015}
3016
6a74a719
ILT
3017// Return the size of a relocation while scanning during a relocatable
3018// link.
3019
3020unsigned int
3021Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3022 unsigned int r_type,
3023 Relobj* object)
3024{
3025 switch (r_type)
3026 {
3027 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
3028 case elfcpp::R_X86_64_GNU_VTINHERIT:
3029 case elfcpp::R_X86_64_GNU_VTENTRY:
6a74a719
ILT
3030 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3031 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3032 case elfcpp::R_X86_64_TLSDESC_CALL:
3033 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3034 case elfcpp::R_X86_64_DTPOFF32:
3035 case elfcpp::R_X86_64_DTPOFF64:
3036 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3037 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3038 return 0;
3039
3040 case elfcpp::R_X86_64_64:
3041 case elfcpp::R_X86_64_PC64:
3042 case elfcpp::R_X86_64_GOTOFF64:
3043 case elfcpp::R_X86_64_GOTPC64:
3044 case elfcpp::R_X86_64_PLTOFF64:
3045 case elfcpp::R_X86_64_GOT64:
3046 case elfcpp::R_X86_64_GOTPCREL64:
3047 case elfcpp::R_X86_64_GOTPCREL:
3048 case elfcpp::R_X86_64_GOTPLT64:
3049 return 8;
3050
3051 case elfcpp::R_X86_64_32:
3052 case elfcpp::R_X86_64_32S:
3053 case elfcpp::R_X86_64_PC32:
3054 case elfcpp::R_X86_64_PLT32:
3055 case elfcpp::R_X86_64_GOTPC32:
3056 case elfcpp::R_X86_64_GOT32:
3057 return 4;
3058
3059 case elfcpp::R_X86_64_16:
3060 case elfcpp::R_X86_64_PC16:
3061 return 2;
3062
3063 case elfcpp::R_X86_64_8:
3064 case elfcpp::R_X86_64_PC8:
3065 return 1;
3066
3067 case elfcpp::R_X86_64_COPY:
3068 case elfcpp::R_X86_64_GLOB_DAT:
3069 case elfcpp::R_X86_64_JUMP_SLOT:
3070 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 3071 case elfcpp::R_X86_64_IRELATIVE:
6a74a719
ILT
3072 // These are outstanding tls relocs, which are unexpected when linking
3073 case elfcpp::R_X86_64_TPOFF64:
3074 case elfcpp::R_X86_64_DTPMOD64:
3075 case elfcpp::R_X86_64_TLSDESC:
3076 object->error(_("unexpected reloc %u in object file"), r_type);
3077 return 0;
3078
3079 case elfcpp::R_X86_64_SIZE32:
3080 case elfcpp::R_X86_64_SIZE64:
3081 default:
3082 object->error(_("unsupported reloc %u against local symbol"), r_type);
3083 return 0;
3084 }
3085}
3086
3087// Scan the relocs during a relocatable link.
3088
3089void
ad0f2072 3090Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
6a74a719
ILT
3091 Layout* layout,
3092 Sized_relobj<64, false>* object,
3093 unsigned int data_shndx,
3094 unsigned int sh_type,
3095 const unsigned char* prelocs,
3096 size_t reloc_count,
3097 Output_section* output_section,
3098 bool needs_special_offset_handling,
3099 size_t local_symbol_count,
3100 const unsigned char* plocal_symbols,
3101 Relocatable_relocs* rr)
3102{
3103 gold_assert(sh_type == elfcpp::SHT_RELA);
3104
3105 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3106 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3107
7019cd25 3108 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
6a74a719 3109 Scan_relocatable_relocs>(
6a74a719
ILT
3110 symtab,
3111 layout,
3112 object,
3113 data_shndx,
3114 prelocs,
3115 reloc_count,
3116 output_section,
3117 needs_special_offset_handling,
3118 local_symbol_count,
3119 plocal_symbols,
3120 rr);
3121}
3122
3123// Relocate a section during a relocatable link.
3124
3125void
3126Target_x86_64::relocate_for_relocatable(
3127 const Relocate_info<64, false>* relinfo,
3128 unsigned int sh_type,
3129 const unsigned char* prelocs,
3130 size_t reloc_count,
3131 Output_section* output_section,
3132 off_t offset_in_output_section,
3133 const Relocatable_relocs* rr,
3134 unsigned char* view,
3135 elfcpp::Elf_types<64>::Elf_Addr view_address,
3136 section_size_type view_size,
3137 unsigned char* reloc_view,
3138 section_size_type reloc_view_size)
3139{
3140 gold_assert(sh_type == elfcpp::SHT_RELA);
3141
7019cd25 3142 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
6a74a719
ILT
3143 relinfo,
3144 prelocs,
3145 reloc_count,
3146 output_section,
3147 offset_in_output_section,
3148 rr,
3149 view,
3150 view_address,
3151 view_size,
3152 reloc_view,
3153 reloc_view_size);
3154}
3155
4fb6c25d
ILT
3156// Return the value to use for a dynamic which requires special
3157// treatment. This is how we support equality comparisons of function
3158// pointers across shared library boundaries, as described in the
3159// processor specific ABI supplement.
3160
3161uint64_t
3162Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3163{
3164 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3165 return this->plt_section()->address() + gsym->plt_offset();
3166}
3167
2e30d253
ILT
3168// Return a string used to fill a code section with nops to take up
3169// the specified length.
3170
3171std::string
8851ecca 3172Target_x86_64::do_code_fill(section_size_type length) const
2e30d253
ILT
3173{
3174 if (length >= 16)
3175 {
3176 // Build a jmpq instruction to skip over the bytes.
3177 unsigned char jmp[5];
3178 jmp[0] = 0xe9;
04bf7072 3179 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2e30d253
ILT
3180 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3181 + std::string(length - 5, '\0'));
3182 }
3183
3184 // Nop sequences of various lengths.
3185 const char nop1[1] = { 0x90 }; // nop
3186 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1caf2c51
ILT
3187 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3188 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3189 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3190 0x00 };
3191 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
2e30d253 3192 0x00, 0x00 };
1caf2c51 3193 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
2e30d253 3194 0x00, 0x00, 0x00 };
1caf2c51
ILT
3195 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3196 0x00, 0x00, 0x00, 0x00 };
3197 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3198 0x00, 0x00, 0x00, 0x00,
2e30d253 3199 0x00 };
1caf2c51
ILT
3200 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3201 0x84, 0x00, 0x00, 0x00,
2e30d253 3202 0x00, 0x00 };
1caf2c51
ILT
3203 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3204 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 3205 0x00, 0x00, 0x00 };
1caf2c51
ILT
3206 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3207 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 3208 0x00, 0x00, 0x00, 0x00 };
1caf2c51
ILT
3209 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3210 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3211 0x00, 0x00, 0x00, 0x00,
2e30d253 3212 0x00 };
1caf2c51
ILT
3213 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3214 0x66, 0x2e, 0x0f, 0x1f, // data16
3215 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 3216 0x00, 0x00 };
1caf2c51
ILT
3217 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3218 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3219 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3220 0x00, 0x00, 0x00 };
2e30d253
ILT
3221
3222 const char* nops[16] = {
3223 NULL,
3224 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3225 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3226 };
3227
3228 return std::string(nops[length], length);
3229}
3230
e291e7b9
ILT
3231// Return the addend to use for a target specific relocation. The
3232// only target specific relocation is R_X86_64_TLSDESC for a local
3233// symbol. We want to set the addend is the offset of the local
3234// symbol in the TLS segment.
3235
3236uint64_t
3237Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3238 uint64_t) const
3239{
3240 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3241 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3242 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3243 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3244 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3245 gold_assert(psymval->is_tls_symbol());
3246 // The value of a TLS symbol is the offset in the TLS segment.
3247 return psymval->value(ti.object, 0);
3248}
3249
364c7fa5 3250// FNOFFSET in section SHNDX in OBJECT is the start of a function
9b547ce6 3251// compiled with -fsplit-stack. The function calls non-split-stack
364c7fa5
ILT
3252// code. We have to change the function so that it always ensures
3253// that it has enough stack space to run some random function.
3254
3255void
3256Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3257 section_offset_type fnoffset,
3258 section_size_type fnsize,
3259 unsigned char* view,
3260 section_size_type view_size,
3261 std::string* from,
3262 std::string* to) const
3263{
3264 // The function starts with a comparison of the stack pointer and a
3265 // field in the TCB. This is followed by a jump.
3266
3267 // cmp %fs:NN,%rsp
3268 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3269 && fnsize > 9)
3270 {
3271 // We will call __morestack if the carry flag is set after this
3272 // comparison. We turn the comparison into an stc instruction
3273 // and some nops.
3274 view[fnoffset] = '\xf9';
3275 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3276 }
3277 // lea NN(%rsp),%r10
cbc999b9
ILT
3278 // lea NN(%rsp),%r11
3279 else if ((this->match_view(view, view_size, fnoffset,
3280 "\x4c\x8d\x94\x24", 4)
3281 || this->match_view(view, view_size, fnoffset,
3282 "\x4c\x8d\x9c\x24", 4))
364c7fa5
ILT
3283 && fnsize > 8)
3284 {
3285 // This is loading an offset from the stack pointer for a
3286 // comparison. The offset is negative, so we decrease the
3287 // offset by the amount of space we need for the stack. This
3288 // means we will avoid calling __morestack if there happens to
3289 // be plenty of space on the stack already.
3290 unsigned char* pval = view + fnoffset + 4;
3291 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3292 val -= parameters->options().split_stack_adjust_size();
3293 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3294 }
3295 else
3296 {
3297 if (!object->has_no_split_stack())
3298 object->error(_("failed to match split-stack sequence at "
3299 "section %u offset %0zx"),
ac33a407 3300 shndx, static_cast<size_t>(fnoffset));
364c7fa5
ILT
3301 return;
3302 }
3303
3304 // We have to change the function so that it calls
3305 // __morestack_non_split instead of __morestack. The former will
3306 // allocate additional stack space.
3307 *from = "__morestack";
3308 *to = "__morestack_non_split";
3309}
3310
2e30d253
ILT
3311// The selector for x86_64 object files.
3312
36959681 3313class Target_selector_x86_64 : public Target_selector_freebsd
2e30d253
ILT
3314{
3315public:
3316 Target_selector_x86_64()
36959681
ILT
3317 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
3318 "elf64-x86-64-freebsd")
2e30d253
ILT
3319 { }
3320
3321 Target*
e96caa79
ILT
3322 do_instantiate_target()
3323 { return new Target_x86_64(); }
36959681 3324
2e30d253
ILT
3325};
3326
2e30d253
ILT
3327Target_selector_x86_64 target_selector_x86_64;
3328
3329} // End anonymous namespace.
This page took 0.359785 seconds and 4 git commands to generate.