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