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