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