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