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