sim: bfin: implement stat_map for virtual environments (libgloss)
[deliverable/binutils-gdb.git] / gold / x86_64.cc
CommitLineData
2e30d253
ILT
1// x86_64.cc -- x86_64 target support for gold.
2
b3ce541e 3// Copyright 2006, 2007, 2008, 2009, 2010, 2011 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
200b2bb9 204class Target_x86_64 : public Sized_target<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()
200b2bb9 212 : Sized_target<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
8a5e3e08
ILT
220 // Hook for a new output section.
221 void
222 do_new_output_section(Output_section*) const;
223
6d03d481
ST
224 // Scan the relocations to look for symbol adjustments.
225 void
ad0f2072 226 gc_process_relocs(Symbol_table* symtab,
6d03d481 227 Layout* layout,
6fa2a40b 228 Sized_relobj_file<64, false>* object,
6d03d481
ST
229 unsigned int data_shndx,
230 unsigned int sh_type,
231 const unsigned char* prelocs,
232 size_t reloc_count,
233 Output_section* output_section,
234 bool needs_special_offset_handling,
235 size_t local_symbol_count,
236 const unsigned char* plocal_symbols);
237
2e30d253
ILT
238 // Scan the relocations to look for symbol adjustments.
239 void
ad0f2072 240 scan_relocs(Symbol_table* symtab,
2e30d253 241 Layout* layout,
6fa2a40b 242 Sized_relobj_file<64, false>* object,
2e30d253
ILT
243 unsigned int data_shndx,
244 unsigned int sh_type,
245 const unsigned char* prelocs,
246 size_t reloc_count,
730cdc88
ILT
247 Output_section* output_section,
248 bool needs_special_offset_handling,
2e30d253 249 size_t local_symbol_count,
730cdc88 250 const unsigned char* plocal_symbols);
2e30d253
ILT
251
252 // Finalize the sections.
253 void
f59f41f3 254 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2e30d253 255
4fb6c25d
ILT
256 // Return the value to use for a dynamic which requires special
257 // treatment.
258 uint64_t
259 do_dynsym_value(const Symbol*) const;
260
2e30d253
ILT
261 // Relocate a section.
262 void
263 relocate_section(const Relocate_info<64, false>*,
264 unsigned int sh_type,
265 const unsigned char* prelocs,
266 size_t reloc_count,
730cdc88
ILT
267 Output_section* output_section,
268 bool needs_special_offset_handling,
2e30d253
ILT
269 unsigned char* view,
270 elfcpp::Elf_types<64>::Elf_Addr view_address,
364c7fa5
ILT
271 section_size_type view_size,
272 const Reloc_symbol_changes*);
2e30d253 273
6a74a719
ILT
274 // Scan the relocs during a relocatable link.
275 void
ad0f2072 276 scan_relocatable_relocs(Symbol_table* symtab,
6a74a719 277 Layout* layout,
6fa2a40b 278 Sized_relobj_file<64, false>* object,
6a74a719
ILT
279 unsigned int data_shndx,
280 unsigned int sh_type,
281 const unsigned char* prelocs,
282 size_t reloc_count,
283 Output_section* output_section,
284 bool needs_special_offset_handling,
285 size_t local_symbol_count,
286 const unsigned char* plocal_symbols,
287 Relocatable_relocs*);
288
289 // Relocate a section during a relocatable link.
290 void
291 relocate_for_relocatable(const Relocate_info<64, false>*,
292 unsigned int sh_type,
293 const unsigned char* prelocs,
294 size_t reloc_count,
295 Output_section* output_section,
296 off_t offset_in_output_section,
297 const Relocatable_relocs*,
298 unsigned char* view,
299 elfcpp::Elf_types<64>::Elf_Addr view_address,
300 section_size_type view_size,
301 unsigned char* reloc_view,
302 section_size_type reloc_view_size);
303
2e30d253
ILT
304 // Return a string used to fill a code section with nops.
305 std::string
8851ecca 306 do_code_fill(section_size_type length) const;
2e30d253 307
9a2d6984
ILT
308 // Return whether SYM is defined by the ABI.
309 bool
9c2d0ef9 310 do_is_defined_by_abi(const Symbol* sym) const
9a2d6984
ILT
311 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
312
e291e7b9
ILT
313 // Return the symbol index to use for a target specific relocation.
314 // The only target specific relocation is R_X86_64_TLSDESC for a
315 // local symbol, which is an absolute reloc.
316 unsigned int
317 do_reloc_symbol_index(void*, unsigned int r_type) const
318 {
319 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
320 return 0;
321 }
322
323 // Return the addend to use for a target specific relocation.
324 uint64_t
325 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
326
7223e9ca
ILT
327 // Return the PLT section.
328 Output_data*
329 do_plt_section_for_global(const Symbol*) const
330 { return this->plt_section(); }
331
332 Output_data*
333 do_plt_section_for_local(const Relobj*, unsigned int) const
334 { return this->plt_section(); }
335
b3ce541e
ILT
336 // This function should be defined in targets that can use relocation
337 // types to determine (implemented in local_reloc_may_be_function_pointer
338 // and global_reloc_may_be_function_pointer)
339 // if a function's pointer is taken. ICF uses this in safe mode to only
340 // fold those functions whose pointer is defintely not taken. For x86_64
341 // pie binaries, safe ICF cannot be done by looking at relocation types.
342 bool
343 do_can_check_for_function_pointers() const
344 { return !parameters->options().pie(); }
345
9b547ce6 346 // Adjust -fsplit-stack code which calls non-split-stack code.
364c7fa5
ILT
347 void
348 do_calls_non_split(Relobj* object, unsigned int shndx,
349 section_offset_type fnoffset, section_size_type fnsize,
350 unsigned char* view, section_size_type view_size,
351 std::string* from, std::string* to) const;
352
96f2030e 353 // Return the size of the GOT section.
fe8718a4 354 section_size_type
0e70b911 355 got_size() const
96f2030e
ILT
356 {
357 gold_assert(this->got_ != NULL);
358 return this->got_->data_size();
359 }
360
0e70b911
CC
361 // Return the number of entries in the GOT.
362 unsigned int
363 got_entry_count() const
364 {
365 if (this->got_ == NULL)
366 return 0;
367 return this->got_size() / 8;
368 }
369
370 // Return the number of entries in the PLT.
371 unsigned int
372 plt_entry_count() const;
373
374 // Return the offset of the first non-reserved PLT entry.
375 unsigned int
376 first_plt_entry_offset() const;
377
378 // Return the size of each PLT entry.
379 unsigned int
380 plt_entry_size() const;
381
4829d394
CC
382 // Create the GOT section for an incremental update.
383 Output_data_got<64, false>*
384 init_got_plt_for_update(Symbol_table* symtab,
385 Layout* layout,
386 unsigned int got_count,
387 unsigned int plt_count);
388
6fa2a40b
CC
389 // Reserve a GOT entry for a local symbol, and regenerate any
390 // necessary dynamic relocations.
391 void
392 reserve_local_got_entry(unsigned int got_index,
393 Sized_relobj<64, false>* obj,
394 unsigned int r_sym,
395 unsigned int got_type);
396
397 // Reserve a GOT entry for a global symbol, and regenerate any
398 // necessary dynamic relocations.
399 void
400 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
401 unsigned int got_type);
402
4829d394 403 // Register an existing PLT entry for a global symbol.
4829d394
CC
404 void
405 register_global_plt_entry(unsigned int plt_index, Symbol* gsym);
406
26d3c67d
CC
407 // Force a COPY relocation for a given symbol.
408 void
409 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
410
94a3fc8b
CC
411 // Apply an incremental relocation.
412 void
413 apply_relocation(const Relocate_info<64, false>* relinfo,
414 elfcpp::Elf_types<64>::Elf_Addr r_offset,
415 unsigned int r_type,
416 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
417 const Symbol* gsym,
418 unsigned char* view,
419 elfcpp::Elf_types<64>::Elf_Addr address,
420 section_size_type view_size);
421
e291e7b9
ILT
422 // Add a new reloc argument, returning the index in the vector.
423 size_t
6fa2a40b 424 add_tlsdesc_info(Sized_relobj_file<64, false>* object, unsigned int r_sym)
e291e7b9
ILT
425 {
426 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
427 return this->tlsdesc_reloc_info_.size() - 1;
428 }
429
2e30d253
ILT
430 private:
431 // The class which scans relocations.
a036edd8 432 class Scan
2e30d253 433 {
a036edd8
ILT
434 public:
435 Scan()
436 : issued_non_pic_error_(false)
437 { }
438
95a2c8d6
RS
439 static inline int
440 get_reference_flags(unsigned int r_type);
441
2e30d253 442 inline void
ad0f2072 443 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
6fa2a40b 444 Sized_relobj_file<64, false>* object,
2e30d253 445 unsigned int data_shndx,
07f397ab 446 Output_section* output_section,
2e30d253
ILT
447 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
448 const elfcpp::Sym<64, false>& lsym);
449
450 inline void
ad0f2072 451 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
6fa2a40b 452 Sized_relobj_file<64, false>* object,
2e30d253 453 unsigned int data_shndx,
07f397ab 454 Output_section* output_section,
2e30d253
ILT
455 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
456 Symbol* gsym);
e041f13d 457
21bb3914
ST
458 inline bool
459 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
460 Target_x86_64* target,
6fa2a40b 461 Sized_relobj_file<64, false>* object,
21bb3914
ST
462 unsigned int data_shndx,
463 Output_section* output_section,
464 const elfcpp::Rela<64, false>& reloc,
465 unsigned int r_type,
466 const elfcpp::Sym<64, false>& lsym);
467
468 inline bool
469 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
470 Target_x86_64* target,
6fa2a40b 471 Sized_relobj_file<64, false>* object,
21bb3914
ST
472 unsigned int data_shndx,
473 Output_section* output_section,
474 const elfcpp::Rela<64, false>& reloc,
475 unsigned int r_type,
476 Symbol* gsym);
477
a036edd8 478 private:
e041f13d 479 static void
6fa2a40b 480 unsupported_reloc_local(Sized_relobj_file<64, false>*, unsigned int r_type);
e041f13d
ILT
481
482 static void
6fa2a40b 483 unsupported_reloc_global(Sized_relobj_file<64, false>*, unsigned int r_type,
e041f13d 484 Symbol*);
a036edd8
ILT
485
486 void
a29b0dad 487 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
a036edd8 488
21bb3914
ST
489 inline bool
490 possible_function_pointer_reloc(unsigned int r_type);
491
7223e9ca 492 bool
6fa2a40b
CC
493 reloc_needs_plt_for_ifunc(Sized_relobj_file<64, false>*,
494 unsigned int r_type);
7223e9ca 495
a036edd8
ILT
496 // Whether we have issued an error about a non-PIC compilation.
497 bool issued_non_pic_error_;
2e30d253
ILT
498 };
499
500 // The class which implements relocation.
501 class Relocate
502 {
503 public:
504 Relocate()
36171d64 505 : skip_call_tls_get_addr_(false)
2e30d253
ILT
506 { }
507
508 ~Relocate()
509 {
510 if (this->skip_call_tls_get_addr_)
511 {
512 // FIXME: This needs to specify the location somehow.
a0c4fb0a 513 gold_error(_("missing expected TLS relocation"));
2e30d253
ILT
514 }
515 }
516
517 // Do a relocation. Return false if the caller should not issue
518 // any warnings about this relocation.
519 inline bool
031cdbed
ILT
520 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
521 size_t relnum, const elfcpp::Rela<64, false>&,
2e30d253
ILT
522 unsigned int r_type, const Sized_symbol<64>*,
523 const Symbol_value<64>*,
524 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
fe8718a4 525 section_size_type);
2e30d253
ILT
526
527 private:
528 // Do a TLS relocation.
529 inline void
7bf1f802
ILT
530 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
531 size_t relnum, const elfcpp::Rela<64, false>&,
2e30d253
ILT
532 unsigned int r_type, const Sized_symbol<64>*,
533 const Symbol_value<64>*,
fe8718a4
ILT
534 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
535 section_size_type);
2e30d253 536
c2b45e22 537 // Do a TLS General-Dynamic to Initial-Exec transition.
7bf1f802
ILT
538 inline void
539 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
540 Output_segment* tls_segment,
541 const elfcpp::Rela<64, false>&, unsigned int r_type,
542 elfcpp::Elf_types<64>::Elf_Addr value,
543 unsigned char* view,
c2b45e22 544 elfcpp::Elf_types<64>::Elf_Addr,
fe8718a4 545 section_size_type view_size);
7bf1f802 546
56622147
ILT
547 // Do a TLS General-Dynamic to Local-Exec transition.
548 inline void
549 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
2e30d253
ILT
550 Output_segment* tls_segment,
551 const elfcpp::Rela<64, false>&, unsigned int r_type,
552 elfcpp::Elf_types<64>::Elf_Addr value,
553 unsigned char* view,
fe8718a4 554 section_size_type view_size);
2e30d253 555
c2b45e22
CC
556 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
557 inline void
558 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
559 Output_segment* tls_segment,
560 const elfcpp::Rela<64, false>&, unsigned int r_type,
561 elfcpp::Elf_types<64>::Elf_Addr value,
562 unsigned char* view,
563 elfcpp::Elf_types<64>::Elf_Addr,
564 section_size_type view_size);
565
566 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
567 inline void
568 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
569 Output_segment* tls_segment,
570 const elfcpp::Rela<64, false>&, unsigned int r_type,
571 elfcpp::Elf_types<64>::Elf_Addr value,
572 unsigned char* view,
573 section_size_type view_size);
574
56622147 575 // Do a TLS Local-Dynamic to Local-Exec transition.
2e30d253 576 inline void
56622147 577 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
2e30d253
ILT
578 Output_segment* tls_segment,
579 const elfcpp::Rela<64, false>&, unsigned int r_type,
580 elfcpp::Elf_types<64>::Elf_Addr value,
581 unsigned char* view,
fe8718a4 582 section_size_type view_size);
2e30d253 583
56622147
ILT
584 // Do a TLS Initial-Exec to Local-Exec transition.
585 static inline void
586 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
72ec2876
ILT
587 Output_segment* tls_segment,
588 const elfcpp::Rela<64, false>&, unsigned int r_type,
589 elfcpp::Elf_types<64>::Elf_Addr value,
590 unsigned char* view,
fe8718a4 591 section_size_type view_size);
2e30d253
ILT
592
593 // This is set if we should skip the next reloc, which should be a
594 // PLT32 reloc against ___tls_get_addr.
595 bool skip_call_tls_get_addr_;
596 };
597
6a74a719
ILT
598 // A class which returns the size required for a relocation type,
599 // used while scanning relocs during a relocatable link.
600 class Relocatable_size_for_reloc
601 {
602 public:
603 unsigned int
604 get_size_for_reloc(unsigned int, Relobj*);
605 };
606
2e30d253
ILT
607 // Adjust TLS relocation type based on the options and whether this
608 // is a local symbol.
e041f13d 609 static tls::Tls_optimization
2e30d253
ILT
610 optimize_tls_reloc(bool is_final, int r_type);
611
612 // Get the GOT section, creating it if necessary.
613 Output_data_got<64, false>*
614 got_section(Symbol_table*, Layout*);
615
96f2030e
ILT
616 // Get the GOT PLT section.
617 Output_data_space*
618 got_plt_section() const
619 {
620 gold_assert(this->got_plt_ != NULL);
621 return this->got_plt_;
622 }
623
a8df5856
ILT
624 // Get the GOT section for TLSDESC entries.
625 Output_data_got<64, false>*
626 got_tlsdesc_section() const
627 {
628 gold_assert(this->got_tlsdesc_ != NULL);
629 return this->got_tlsdesc_;
630 }
631
c2b45e22
CC
632 // Create the PLT section.
633 void
634 make_plt_section(Symbol_table* symtab, Layout* layout);
635
2e30d253
ILT
636 // Create a PLT entry for a global symbol.
637 void
638 make_plt_entry(Symbol_table*, Layout*, Symbol*);
639
7223e9ca
ILT
640 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
641 void
642 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
6fa2a40b 643 Sized_relobj_file<64, false>* relobj,
7223e9ca
ILT
644 unsigned int local_sym_index);
645
9fa33bee 646 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
edfbb029
CC
647 void
648 define_tls_base_symbol(Symbol_table*, Layout*);
649
c2b45e22
CC
650 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
651 void
652 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
653
31d60480
ILT
654 // Create a GOT entry for the TLS module index.
655 unsigned int
656 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
6fa2a40b 657 Sized_relobj_file<64, false>* object);
31d60480 658
2e30d253
ILT
659 // Get the PLT section.
660 Output_data_plt_x86_64*
661 plt_section() const
662 {
663 gold_assert(this->plt_ != NULL);
664 return this->plt_;
665 }
666
667 // Get the dynamic reloc section, creating it if necessary.
668 Reloc_section*
0ffd9845 669 rela_dyn_section(Layout*);
2e30d253 670
e291e7b9
ILT
671 // Get the section to use for TLSDESC relocations.
672 Reloc_section*
673 rela_tlsdesc_section(Layout*) const;
674
12c0daef 675 // Add a potential copy relocation.
2e30d253 676 void
ef9beddf 677 copy_reloc(Symbol_table* symtab, Layout* layout,
6fa2a40b 678 Sized_relobj_file<64, false>* object,
12c0daef
ILT
679 unsigned int shndx, Output_section* output_section,
680 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
681 {
682 this->copy_relocs_.copy_reloc(symtab, layout,
683 symtab->get_sized_symbol<64>(sym),
684 object, shndx, output_section,
685 reloc, this->rela_dyn_section(layout));
686 }
2e30d253
ILT
687
688 // Information about this specific target which we pass to the
689 // general Target structure.
690 static const Target::Target_info x86_64_info;
691
0e70b911
CC
692 // The types of GOT entries needed for this platform.
693 // These values are exposed to the ABI in an incremental link.
694 // Do not renumber existing values without changing the version
695 // number of the .gnu_incremental_inputs section.
0a65a3a7
CC
696 enum Got_type
697 {
698 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
699 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
700 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
701 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
702 };
703
e291e7b9
ILT
704 // This type is used as the argument to the target specific
705 // relocation routines. The only target specific reloc is
706 // R_X86_64_TLSDESC against a local symbol.
707 struct Tlsdesc_info
708 {
6fa2a40b 709 Tlsdesc_info(Sized_relobj_file<64, false>* a_object, unsigned int a_r_sym)
e291e7b9
ILT
710 : object(a_object), r_sym(a_r_sym)
711 { }
712
713 // The object in which the local symbol is defined.
6fa2a40b 714 Sized_relobj_file<64, false>* object;
e291e7b9
ILT
715 // The local symbol index in the object.
716 unsigned int r_sym;
717 };
718
2e30d253
ILT
719 // The GOT section.
720 Output_data_got<64, false>* got_;
721 // The PLT section.
722 Output_data_plt_x86_64* plt_;
723 // The GOT PLT section.
724 Output_data_space* got_plt_;
a8df5856
ILT
725 // The GOT section for TLSDESC relocations.
726 Output_data_got<64, false>* got_tlsdesc_;
e785ec03
ILT
727 // The _GLOBAL_OFFSET_TABLE_ symbol.
728 Symbol* global_offset_table_;
2e30d253 729 // The dynamic reloc section.
0ffd9845 730 Reloc_section* rela_dyn_;
2e30d253 731 // Relocs saved to avoid a COPY reloc.
12c0daef 732 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
2e30d253
ILT
733 // Space for variables copied with a COPY reloc.
734 Output_data_space* dynbss_;
c2b45e22 735 // Offset of the GOT entry for the TLS module index.
31d60480 736 unsigned int got_mod_index_offset_;
e291e7b9
ILT
737 // We handle R_X86_64_TLSDESC against a local symbol as a target
738 // specific relocation. Here we store the object and local symbol
739 // index for the relocation.
740 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
edfbb029
CC
741 // True if the _TLS_MODULE_BASE_ symbol has been defined.
742 bool tls_base_symbol_defined_;
2e30d253
ILT
743};
744
745const Target::Target_info Target_x86_64::x86_64_info =
746{
747 64, // size
748 false, // is_big_endian
749 elfcpp::EM_X86_64, // machine_code
750 false, // has_make_symbol
751 false, // has_resolve
752 true, // has_code_fill
35cdfc9a 753 true, // is_default_stack_executable
b3ce541e 754 true, // can_icf_inline_merge_sections
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)
e6455dfb
CC
915 gold_fallback(_("out of patch space (PLT);"
916 " relink with --incremental-full"));
4829d394
CC
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
26d3c67d
CC
1392// Force a COPY relocation for a given symbol.
1393
1394void
1395Target_x86_64::emit_copy_reloc(
1396 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
1397{
1398 this->copy_relocs_.emit_copy_reloc(symtab,
1399 symtab->get_sized_symbol<64>(sym),
1400 os,
1401 offset,
1402 this->rela_dyn_section(NULL));
1403}
1404
9fa33bee 1405// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
edfbb029
CC
1406
1407void
1408Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1409{
1410 if (this->tls_base_symbol_defined_)
1411 return;
1412
1413 Output_segment* tls_segment = layout->tls_segment();
1414 if (tls_segment != NULL)
1415 {
183fd0e3 1416 bool is_exec = parameters->options().output_is_executable();
edfbb029 1417 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
99fff23b 1418 Symbol_table::PREDEFINED,
edfbb029
CC
1419 tls_segment, 0, 0,
1420 elfcpp::STT_TLS,
1421 elfcpp::STB_LOCAL,
1422 elfcpp::STV_HIDDEN, 0,
183fd0e3
AO
1423 (is_exec
1424 ? Symbol::SEGMENT_END
1425 : Symbol::SEGMENT_START),
1426 true);
edfbb029
CC
1427 }
1428 this->tls_base_symbol_defined_ = true;
1429}
1430
c2b45e22
CC
1431// Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1432
1433void
1434Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1435 Layout* layout)
1436{
1437 if (this->plt_ == NULL)
1438 this->make_plt_section(symtab, layout);
1439
1440 if (!this->plt_->has_tlsdesc_entry())
1441 {
1442 // Allocate the TLSDESC_GOT entry.
1443 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1444 unsigned int got_offset = got->add_constant(0);
1445
1446 // Allocate the TLSDESC_PLT entry.
1447 this->plt_->reserve_tlsdesc_entry(got_offset);
1448 }
1449}
1450
31d60480
ILT
1451// Create a GOT entry for the TLS module index.
1452
1453unsigned int
1454Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
6fa2a40b 1455 Sized_relobj_file<64, false>* object)
31d60480
ILT
1456{
1457 if (this->got_mod_index_offset_ == -1U)
1458 {
1459 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1460 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1461 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1462 unsigned int got_offset = got->add_constant(0);
1463 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1464 got_offset, 0);
009a67a2 1465 got->add_constant(0);
31d60480
ILT
1466 this->got_mod_index_offset_ = got_offset;
1467 }
1468 return this->got_mod_index_offset_;
1469}
1470
2e30d253
ILT
1471// Optimize the TLS relocation type based on what we know about the
1472// symbol. IS_FINAL is true if the final address of this symbol is
1473// known at link time.
1474
e041f13d 1475tls::Tls_optimization
2e30d253
ILT
1476Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1477{
2e30d253
ILT
1478 // If we are generating a shared library, then we can't do anything
1479 // in the linker.
8851ecca 1480 if (parameters->options().shared())
e041f13d 1481 return tls::TLSOPT_NONE;
2e30d253
ILT
1482
1483 switch (r_type)
1484 {
1485 case elfcpp::R_X86_64_TLSGD:
e041f13d
ILT
1486 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1487 case elfcpp::R_X86_64_TLSDESC_CALL:
1488 // These are General-Dynamic which permits fully general TLS
2e30d253
ILT
1489 // access. Since we know that we are generating an executable,
1490 // we can convert this to Initial-Exec. If we also know that
1491 // this is a local symbol, we can further switch to Local-Exec.
1492 if (is_final)
e041f13d
ILT
1493 return tls::TLSOPT_TO_LE;
1494 return tls::TLSOPT_TO_IE;
2e30d253 1495
d61c17ea 1496 case elfcpp::R_X86_64_TLSLD:
2e30d253
ILT
1497 // This is Local-Dynamic, which refers to a local symbol in the
1498 // dynamic TLS block. Since we know that we generating an
1499 // executable, we can switch to Local-Exec.
e041f13d 1500 return tls::TLSOPT_TO_LE;
2e30d253 1501
0ffd9845 1502 case elfcpp::R_X86_64_DTPOFF32:
0ffd9845
ILT
1503 case elfcpp::R_X86_64_DTPOFF64:
1504 // Another Local-Dynamic reloc.
e041f13d 1505 return tls::TLSOPT_TO_LE;
0ffd9845 1506
d61c17ea 1507 case elfcpp::R_X86_64_GOTTPOFF:
2e30d253
ILT
1508 // These are Initial-Exec relocs which get the thread offset
1509 // from the GOT. If we know that we are linking against the
1510 // local symbol, we can switch to Local-Exec, which links the
1511 // thread offset into the instruction.
1512 if (is_final)
e041f13d
ILT
1513 return tls::TLSOPT_TO_LE;
1514 return tls::TLSOPT_NONE;
2e30d253 1515
d61c17ea 1516 case elfcpp::R_X86_64_TPOFF32:
2e30d253
ILT
1517 // When we already have Local-Exec, there is nothing further we
1518 // can do.
e041f13d 1519 return tls::TLSOPT_NONE;
2e30d253
ILT
1520
1521 default:
1522 gold_unreachable();
1523 }
2e30d253
ILT
1524}
1525
95a2c8d6
RS
1526// Get the Reference_flags for a particular relocation.
1527
1528int
1529Target_x86_64::Scan::get_reference_flags(unsigned int r_type)
1530{
1531 switch (r_type)
1532 {
1533 case elfcpp::R_X86_64_NONE:
1534 case elfcpp::R_X86_64_GNU_VTINHERIT:
1535 case elfcpp::R_X86_64_GNU_VTENTRY:
1536 case elfcpp::R_X86_64_GOTPC32:
1537 case elfcpp::R_X86_64_GOTPC64:
1538 // No symbol reference.
1539 return 0;
1540
1541 case elfcpp::R_X86_64_64:
1542 case elfcpp::R_X86_64_32:
1543 case elfcpp::R_X86_64_32S:
1544 case elfcpp::R_X86_64_16:
1545 case elfcpp::R_X86_64_8:
1546 return Symbol::ABSOLUTE_REF;
1547
1548 case elfcpp::R_X86_64_PC64:
1549 case elfcpp::R_X86_64_PC32:
1550 case elfcpp::R_X86_64_PC16:
1551 case elfcpp::R_X86_64_PC8:
1552 case elfcpp::R_X86_64_GOTOFF64:
1553 return Symbol::RELATIVE_REF;
1554
1555 case elfcpp::R_X86_64_PLT32:
1556 case elfcpp::R_X86_64_PLTOFF64:
1557 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1558
1559 case elfcpp::R_X86_64_GOT64:
1560 case elfcpp::R_X86_64_GOT32:
1561 case elfcpp::R_X86_64_GOTPCREL64:
1562 case elfcpp::R_X86_64_GOTPCREL:
1563 case elfcpp::R_X86_64_GOTPLT64:
1564 // Absolute in GOT.
1565 return Symbol::ABSOLUTE_REF;
1566
1567 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1568 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1569 case elfcpp::R_X86_64_TLSDESC_CALL:
1570 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1571 case elfcpp::R_X86_64_DTPOFF32:
1572 case elfcpp::R_X86_64_DTPOFF64:
1573 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1574 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1575 return Symbol::TLS_REF;
1576
1577 case elfcpp::R_X86_64_COPY:
1578 case elfcpp::R_X86_64_GLOB_DAT:
1579 case elfcpp::R_X86_64_JUMP_SLOT:
1580 case elfcpp::R_X86_64_RELATIVE:
1581 case elfcpp::R_X86_64_IRELATIVE:
1582 case elfcpp::R_X86_64_TPOFF64:
1583 case elfcpp::R_X86_64_DTPMOD64:
1584 case elfcpp::R_X86_64_TLSDESC:
1585 case elfcpp::R_X86_64_SIZE32:
1586 case elfcpp::R_X86_64_SIZE64:
1587 default:
1588 // Not expected. We will give an error later.
1589 return 0;
1590 }
1591}
1592
e041f13d
ILT
1593// Report an unsupported relocation against a local symbol.
1594
1595void
6fa2a40b
CC
1596Target_x86_64::Scan::unsupported_reloc_local(
1597 Sized_relobj_file<64, false>* object,
1598 unsigned int r_type)
e041f13d 1599{
75f2446e
ILT
1600 gold_error(_("%s: unsupported reloc %u against local symbol"),
1601 object->name().c_str(), r_type);
e041f13d
ILT
1602}
1603
a036edd8
ILT
1604// We are about to emit a dynamic relocation of type R_TYPE. If the
1605// dynamic linker does not support it, issue an error. The GNU linker
1606// only issues a non-PIC error for an allocated read-only section.
1607// Here we know the section is allocated, but we don't know that it is
1608// read-only. But we check for all the relocation types which the
1609// glibc dynamic linker supports, so it seems appropriate to issue an
a29b0dad
ILT
1610// error even if the section is not read-only. If GSYM is not NULL,
1611// it is the symbol the relocation is against; if it is NULL, the
1612// relocation is against a local symbol.
a036edd8
ILT
1613
1614void
a29b0dad
ILT
1615Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type,
1616 Symbol* gsym)
a036edd8
ILT
1617{
1618 switch (r_type)
1619 {
2fbb4320
ILT
1620 // These are the relocation types supported by glibc for x86_64
1621 // which should always work.
a036edd8 1622 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 1623 case elfcpp::R_X86_64_IRELATIVE:
a036edd8
ILT
1624 case elfcpp::R_X86_64_GLOB_DAT:
1625 case elfcpp::R_X86_64_JUMP_SLOT:
1626 case elfcpp::R_X86_64_DTPMOD64:
1627 case elfcpp::R_X86_64_DTPOFF64:
1628 case elfcpp::R_X86_64_TPOFF64:
1629 case elfcpp::R_X86_64_64:
2fbb4320
ILT
1630 case elfcpp::R_X86_64_COPY:
1631 return;
1632
1633 // glibc supports these reloc types, but they can overflow.
a036edd8 1634 case elfcpp::R_X86_64_PC32:
a29b0dad
ILT
1635 // A PC relative reference is OK against a local symbol or if
1636 // the symbol is defined locally.
1637 if (gsym == NULL
1638 || (!gsym->is_from_dynobj()
1639 && !gsym->is_undefined()
1640 && !gsym->is_preemptible()))
1641 return;
1642 /* Fall through. */
1643 case elfcpp::R_X86_64_32:
2fbb4320
ILT
1644 if (this->issued_non_pic_error_)
1645 return;
1646 gold_assert(parameters->options().output_is_position_independent());
a29b0dad
ILT
1647 if (gsym == NULL)
1648 object->error(_("requires dynamic R_X86_64_32 reloc which may "
1649 "overflow at runtime; recompile with -fPIC"));
1650 else
1651 object->error(_("requires dynamic %s reloc against '%s' which may "
1652 "overflow at runtime; recompile with -fPIC"),
1653 (r_type == elfcpp::R_X86_64_32
1654 ? "R_X86_64_32"
1655 : "R_X86_64_PC32"),
1656 gsym->name());
2fbb4320 1657 this->issued_non_pic_error_ = true;
a036edd8
ILT
1658 return;
1659
1660 default:
1661 // This prevents us from issuing more than one error per reloc
1662 // section. But we can still wind up issuing more than one
1663 // error per object file.
1664 if (this->issued_non_pic_error_)
1665 return;
33aea2fd 1666 gold_assert(parameters->options().output_is_position_independent());
a29b0dad
ILT
1667 object->error(_("requires unsupported dynamic reloc %u; "
1668 "recompile with -fPIC"),
1669 r_type);
a036edd8
ILT
1670 this->issued_non_pic_error_ = true;
1671 return;
1672
1673 case elfcpp::R_X86_64_NONE:
1674 gold_unreachable();
1675 }
1676}
1677
7223e9ca
ILT
1678// Return whether we need to make a PLT entry for a relocation of the
1679// given type against a STT_GNU_IFUNC symbol.
1680
1681bool
6fa2a40b
CC
1682Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1683 Sized_relobj_file<64, false>* object,
1684 unsigned int r_type)
7223e9ca 1685{
95a2c8d6
RS
1686 int flags = Scan::get_reference_flags(r_type);
1687 if (flags & Symbol::TLS_REF)
1688 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1689 object->name().c_str(), r_type);
1690 return flags != 0;
7223e9ca
ILT
1691}
1692
2e30d253
ILT
1693// Scan a relocation for a local symbol.
1694
1695inline void
ad0f2072 1696Target_x86_64::Scan::local(Symbol_table* symtab,
d61c17ea
ILT
1697 Layout* layout,
1698 Target_x86_64* target,
6fa2a40b 1699 Sized_relobj_file<64, false>* object,
0ffd9845 1700 unsigned int data_shndx,
4f4c5f80 1701 Output_section* output_section,
0ffd9845 1702 const elfcpp::Rela<64, false>& reloc,
d61c17ea 1703 unsigned int r_type,
7bf1f802 1704 const elfcpp::Sym<64, false>& lsym)
2e30d253 1705{
7223e9ca
ILT
1706 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1707 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1708 && this->reloc_needs_plt_for_ifunc(object, r_type))
1709 {
1710 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1711 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1712 }
1713
2e30d253
ILT
1714 switch (r_type)
1715 {
1716 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
1717 case elfcpp::R_X86_64_GNU_VTINHERIT:
1718 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
1719 break;
1720
1721 case elfcpp::R_X86_64_64:
d61c6bd4 1722 // If building a shared library (or a position-independent
dceae3c1
ILT
1723 // executable), we need to create a dynamic relocation for this
1724 // location. The relocation applied at link time will apply the
1725 // link-time value, so we flag the location with an
1726 // R_X86_64_RELATIVE relocation so the dynamic loader can
d61c6bd4 1727 // relocate it easily.
8851ecca 1728 if (parameters->options().output_is_position_independent())
d61c6bd4 1729 {
e8c846c3 1730 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d61c6bd4 1731 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7223e9ca
ILT
1732 rela_dyn->add_local_relative(object, r_sym,
1733 elfcpp::R_X86_64_RELATIVE,
1734 output_section, data_shndx,
1735 reloc.get_r_offset(),
1736 reloc.get_r_addend());
d61c6bd4
ILT
1737 }
1738 break;
1739
2e30d253
ILT
1740 case elfcpp::R_X86_64_32:
1741 case elfcpp::R_X86_64_32S:
1742 case elfcpp::R_X86_64_16:
1743 case elfcpp::R_X86_64_8:
96f2030e 1744 // If building a shared library (or a position-independent
dceae3c1
ILT
1745 // executable), we need to create a dynamic relocation for this
1746 // location. We can't use an R_X86_64_RELATIVE relocation
1747 // because that is always a 64-bit relocation.
8851ecca 1748 if (parameters->options().output_is_position_independent())
96f2030e 1749 {
a29b0dad 1750 this->check_non_pic(object, r_type, NULL);
a036edd8 1751
96f2030e 1752 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
d491d34e 1753 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
dceae3c1 1754 if (lsym.get_st_type() != elfcpp::STT_SECTION)
d491d34e
ILT
1755 rela_dyn->add_local(object, r_sym, r_type, output_section,
1756 data_shndx, reloc.get_r_offset(),
1757 reloc.get_r_addend());
dceae3c1
ILT
1758 else
1759 {
1760 gold_assert(lsym.get_st_value() == 0);
d491d34e
ILT
1761 unsigned int shndx = lsym.get_st_shndx();
1762 bool is_ordinary;
1763 shndx = object->adjust_sym_shndx(r_sym, shndx,
1764 &is_ordinary);
1765 if (!is_ordinary)
1766 object->error(_("section symbol %u has bad shndx %u"),
1767 r_sym, shndx);
1768 else
1769 rela_dyn->add_local_section(object, shndx,
1770 r_type, output_section,
1771 data_shndx, reloc.get_r_offset(),
1772 reloc.get_r_addend());
dceae3c1 1773 }
96f2030e 1774 }
2e30d253
ILT
1775 break;
1776
1777 case elfcpp::R_X86_64_PC64:
1778 case elfcpp::R_X86_64_PC32:
1779 case elfcpp::R_X86_64_PC16:
1780 case elfcpp::R_X86_64_PC8:
1781 break;
1782
f389a824
ILT
1783 case elfcpp::R_X86_64_PLT32:
1784 // Since we know this is a local symbol, we can handle this as a
1785 // PC32 reloc.
1786 break;
1787
fdc2f80f 1788 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1789 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1790 case elfcpp::R_X86_64_GOTPC64:
1791 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1792 // We need a GOT section.
1793 target->got_section(symtab, layout);
ee9e9e86
ILT
1794 // For PLTOFF64, we'd normally want a PLT section, but since we
1795 // know this is a local symbol, no PLT is needed.
2e30d253
ILT
1796 break;
1797
0ffd9845
ILT
1798 case elfcpp::R_X86_64_GOT64:
1799 case elfcpp::R_X86_64_GOT32:
1800 case elfcpp::R_X86_64_GOTPCREL64:
1801 case elfcpp::R_X86_64_GOTPCREL:
ee9e9e86 1802 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
1803 {
1804 // The symbol requires a GOT entry.
1805 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1806 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
7223e9ca
ILT
1807
1808 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1809 // lets function pointers compare correctly with shared
1810 // libraries. Otherwise we would need an IRELATIVE reloc.
1811 bool is_new;
1812 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1813 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1814 else
1815 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1816 if (is_new)
0ffd9845
ILT
1817 {
1818 // If we are generating a shared object, we need to add a
7bf1f802 1819 // dynamic relocation for this symbol's GOT entry.
8851ecca 1820 if (parameters->options().output_is_position_independent())
0ffd9845
ILT
1821 {
1822 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7bf1f802
ILT
1823 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1824 if (r_type != elfcpp::R_X86_64_GOT32)
7223e9ca
ILT
1825 {
1826 unsigned int got_offset =
1827 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1828 rela_dyn->add_local_relative(object, r_sym,
1829 elfcpp::R_X86_64_RELATIVE,
1830 got, got_offset, 0);
1831 }
7bf1f802 1832 else
dceae3c1 1833 {
a29b0dad 1834 this->check_non_pic(object, r_type, NULL);
a036edd8 1835
dceae3c1 1836 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
0a65a3a7
CC
1837 rela_dyn->add_local(
1838 object, r_sym, r_type, got,
1839 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
dceae3c1 1840 }
0ffd9845
ILT
1841 }
1842 }
ee9e9e86
ILT
1843 // For GOTPLT64, we'd normally want a PLT section, but since
1844 // we know this is a local symbol, no PLT is needed.
0ffd9845
ILT
1845 }
1846 break;
1847
2e30d253
ILT
1848 case elfcpp::R_X86_64_COPY:
1849 case elfcpp::R_X86_64_GLOB_DAT:
1850 case elfcpp::R_X86_64_JUMP_SLOT:
1851 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 1852 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 1853 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 1854 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1855 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 1856 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1857 gold_error(_("%s: unexpected reloc %u in object file"),
1858 object->name().c_str(), r_type);
2e30d253
ILT
1859 break;
1860
d61c17ea 1861 // These are initial tls relocs, which are expected when linking
56622147
ILT
1862 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1863 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1864 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1865 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1866 case elfcpp::R_X86_64_DTPOFF32:
1867 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1868 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1869 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253 1870 {
8851ecca 1871 bool output_is_shared = parameters->options().shared();
e041f13d
ILT
1872 const tls::Tls_optimization optimized_type
1873 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
2e30d253
ILT
1874 switch (r_type)
1875 {
56622147 1876 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
1877 if (optimized_type == tls::TLSOPT_NONE)
1878 {
1879 // Create a pair of GOT entries for the module index and
1880 // dtv-relative offset.
1881 Output_data_got<64, false>* got
1882 = target->got_section(symtab, layout);
1883 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d491d34e
ILT
1884 unsigned int shndx = lsym.get_st_shndx();
1885 bool is_ordinary;
1886 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1887 if (!is_ordinary)
1888 object->error(_("local symbol %u has bad shndx %u"),
1889 r_sym, shndx);
1890 else
1891 got->add_local_pair_with_rela(object, r_sym,
1892 shndx,
1893 GOT_TYPE_TLS_PAIR,
1894 target->rela_dyn_section(layout),
1895 elfcpp::R_X86_64_DTPMOD64, 0);
7bf1f802
ILT
1896 }
1897 else if (optimized_type != tls::TLSOPT_TO_LE)
1898 unsupported_reloc_local(object, r_type);
1899 break;
1900
56622147 1901 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 1902 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
1903 if (optimized_type == tls::TLSOPT_NONE)
1904 {
1905 // Create reserved PLT and GOT entries for the resolver.
1906 target->reserve_tlsdesc_entries(symtab, layout);
1907
a8df5856
ILT
1908 // Generate a double GOT entry with an
1909 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1910 // is resolved lazily, so the GOT entry needs to be in
1911 // an area in .got.plt, not .got. Call got_section to
1912 // make sure the section has been created.
1913 target->got_section(symtab, layout);
1914 Output_data_got<64, false>* got = target->got_tlsdesc_section();
c2b45e22 1915 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
e291e7b9
ILT
1916 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1917 {
1918 unsigned int got_offset = got->add_constant(0);
1919 got->add_constant(0);
1920 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1921 got_offset);
1922 Reloc_section* rt = target->rela_tlsdesc_section(layout);
1923 // We store the arguments we need in a vector, and
1924 // use the index into the vector as the parameter
1925 // to pass to the target specific routines.
1926 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1927 void* arg = reinterpret_cast<void*>(intarg);
1928 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1929 got, got_offset, 0);
1930 }
c2b45e22
CC
1931 }
1932 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 1933 unsupported_reloc_local(object, r_type);
2e30d253
ILT
1934 break;
1935
c2b45e22
CC
1936 case elfcpp::R_X86_64_TLSDESC_CALL:
1937 break;
1938
e041f13d 1939 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
1940 if (optimized_type == tls::TLSOPT_NONE)
1941 {
1942 // Create a GOT entry for the module index.
31d60480 1943 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
1944 }
1945 else if (optimized_type != tls::TLSOPT_TO_LE)
1946 unsupported_reloc_local(object, r_type);
1947 break;
1948
0ffd9845
ILT
1949 case elfcpp::R_X86_64_DTPOFF32:
1950 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
1951 break;
1952
56622147 1953 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 1954 layout->set_has_static_tls();
7bf1f802
ILT
1955 if (optimized_type == tls::TLSOPT_NONE)
1956 {
1957 // Create a GOT entry for the tp-relative offset.
1958 Output_data_got<64, false>* got
1959 = target->got_section(symtab, layout);
1960 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7 1961 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1962 target->rela_dyn_section(layout),
1963 elfcpp::R_X86_64_TPOFF64);
1964 }
1965 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
1966 unsupported_reloc_local(object, r_type);
1967 break;
0ffd9845 1968
56622147 1969 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 1970 layout->set_has_static_tls();
7bf1f802
ILT
1971 if (output_is_shared)
1972 unsupported_reloc_local(object, r_type);
2e30d253 1973 break;
e041f13d
ILT
1974
1975 default:
1976 gold_unreachable();
2e30d253
ILT
1977 }
1978 }
1979 break;
2e30d253 1980
fdc2f80f
ILT
1981 case elfcpp::R_X86_64_SIZE32:
1982 case elfcpp::R_X86_64_SIZE64:
2e30d253 1983 default:
75f2446e
ILT
1984 gold_error(_("%s: unsupported reloc %u against local symbol"),
1985 object->name().c_str(), r_type);
2e30d253
ILT
1986 break;
1987 }
1988}
1989
1990
e041f13d
ILT
1991// Report an unsupported relocation against a global symbol.
1992
1993void
6fa2a40b
CC
1994Target_x86_64::Scan::unsupported_reloc_global(
1995 Sized_relobj_file<64, false>* object,
1996 unsigned int r_type,
1997 Symbol* gsym)
e041f13d 1998{
75f2446e 1999 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12 2000 object->name().c_str(), r_type, gsym->demangled_name().c_str());
e041f13d
ILT
2001}
2002
ce97fa81 2003// Returns true if this relocation type could be that of a function pointer.
21bb3914
ST
2004inline bool
2005Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
2006{
21bb3914
ST
2007 switch (r_type)
2008 {
2009 case elfcpp::R_X86_64_64:
2010 case elfcpp::R_X86_64_32:
2011 case elfcpp::R_X86_64_32S:
2012 case elfcpp::R_X86_64_16:
2013 case elfcpp::R_X86_64_8:
ce97fa81
ST
2014 case elfcpp::R_X86_64_GOT64:
2015 case elfcpp::R_X86_64_GOT32:
2016 case elfcpp::R_X86_64_GOTPCREL64:
2017 case elfcpp::R_X86_64_GOTPCREL:
2018 case elfcpp::R_X86_64_GOTPLT64:
21bb3914
ST
2019 {
2020 return true;
2021 }
2022 }
2023 return false;
2024}
2025
2026// For safe ICF, scan a relocation for a local symbol to check if it
2027// corresponds to a function pointer being taken. In that case mark
2028// the function whose pointer was taken as not foldable.
2029
2030inline bool
2031Target_x86_64::Scan::local_reloc_may_be_function_pointer(
2032 Symbol_table* ,
2033 Layout* ,
2034 Target_x86_64* ,
6fa2a40b 2035 Sized_relobj_file<64, false>* ,
21bb3914
ST
2036 unsigned int ,
2037 Output_section* ,
2038 const elfcpp::Rela<64, false>& ,
2039 unsigned int r_type,
2040 const elfcpp::Sym<64, false>&)
2041{
2042 // When building a shared library, do not fold any local symbols as it is
2043 // not possible to distinguish pointer taken versus a call by looking at
2044 // the relocation types.
2045 return (parameters->options().shared()
2046 || possible_function_pointer_reloc(r_type));
2047}
2048
2049// For safe ICF, scan a relocation for a global symbol to check if it
2050// corresponds to a function pointer being taken. In that case mark
2051// the function whose pointer was taken as not foldable.
2052
2053inline bool
2054Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2055 Symbol_table*,
2056 Layout* ,
2057 Target_x86_64* ,
6fa2a40b 2058 Sized_relobj_file<64, false>* ,
21bb3914
ST
2059 unsigned int ,
2060 Output_section* ,
2061 const elfcpp::Rela<64, false>& ,
2062 unsigned int r_type,
2063 Symbol* gsym)
2064{
2065 // When building a shared library, do not fold symbols whose visibility
2066 // is hidden, internal or protected.
2067 return ((parameters->options().shared()
2068 && (gsym->visibility() == elfcpp::STV_INTERNAL
2069 || gsym->visibility() == elfcpp::STV_PROTECTED
2070 || gsym->visibility() == elfcpp::STV_HIDDEN))
2071 || possible_function_pointer_reloc(r_type));
2072}
2073
2e30d253
ILT
2074// Scan a relocation for a global symbol.
2075
2076inline void
ad0f2072 2077Target_x86_64::Scan::global(Symbol_table* symtab,
d61c17ea
ILT
2078 Layout* layout,
2079 Target_x86_64* target,
6fa2a40b 2080 Sized_relobj_file<64, false>* object,
d61c17ea 2081 unsigned int data_shndx,
4f4c5f80 2082 Output_section* output_section,
d61c17ea
ILT
2083 const elfcpp::Rela<64, false>& reloc,
2084 unsigned int r_type,
2085 Symbol* gsym)
2e30d253 2086{
7223e9ca
ILT
2087 // A STT_GNU_IFUNC symbol may require a PLT entry.
2088 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2089 && this->reloc_needs_plt_for_ifunc(object, r_type))
2090 target->make_plt_entry(symtab, layout, gsym);
2091
2e30d253
ILT
2092 switch (r_type)
2093 {
2094 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
2095 case elfcpp::R_X86_64_GNU_VTINHERIT:
2096 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
2097 break;
2098
2099 case elfcpp::R_X86_64_64:
2e30d253
ILT
2100 case elfcpp::R_X86_64_32:
2101 case elfcpp::R_X86_64_32S:
2e30d253 2102 case elfcpp::R_X86_64_16:
2e30d253 2103 case elfcpp::R_X86_64_8:
96f2030e 2104 {
d61c6bd4
ILT
2105 // Make a PLT entry if necessary.
2106 if (gsym->needs_plt_entry())
2107 {
2108 target->make_plt_entry(symtab, layout, gsym);
2109 // Since this is not a PC-relative relocation, we may be
2110 // taking the address of a function. In that case we need to
2111 // set the entry in the dynamic symbol table to the address of
2112 // the PLT entry.
8851ecca 2113 if (gsym->is_from_dynobj() && !parameters->options().shared())
d61c6bd4
ILT
2114 gsym->set_needs_dynsym_value();
2115 }
2116 // Make a dynamic relocation if necessary.
95a2c8d6 2117 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
d61c6bd4 2118 {
966d4097 2119 if (gsym->may_need_copy_reloc())
d61c6bd4 2120 {
12c0daef 2121 target->copy_reloc(symtab, layout, object,
7bf1f802 2122 data_shndx, output_section, gsym, reloc);
d61c6bd4 2123 }
7223e9ca
ILT
2124 else if (r_type == elfcpp::R_X86_64_64
2125 && gsym->type() == elfcpp::STT_GNU_IFUNC
2126 && gsym->can_use_relative_reloc(false)
2127 && !gsym->is_from_dynobj()
2128 && !gsym->is_undefined()
2129 && !gsym->is_preemptible())
2130 {
2131 // Use an IRELATIVE reloc for a locally defined
2132 // STT_GNU_IFUNC symbol. This makes a function
2133 // address in a PIE executable match the address in a
2134 // shared library that it links against.
2135 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2136 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2137 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2138 output_section, object,
2139 data_shndx,
2140 reloc.get_r_offset(),
2141 reloc.get_r_addend());
2142 }
d61c6bd4
ILT
2143 else if (r_type == elfcpp::R_X86_64_64
2144 && gsym->can_use_relative_reloc(false))
2145 {
2146 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7223e9ca
ILT
2147 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2148 output_section, object,
2149 data_shndx,
2150 reloc.get_r_offset(),
2151 reloc.get_r_addend());
d61c6bd4
ILT
2152 }
2153 else
2154 {
a29b0dad 2155 this->check_non_pic(object, r_type, gsym);
96f2030e 2156 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
2157 rela_dyn->add_global(gsym, r_type, output_section, object,
2158 data_shndx, reloc.get_r_offset(),
96f2030e 2159 reloc.get_r_addend());
d61c6bd4
ILT
2160 }
2161 }
2162 }
2163 break;
2164
2165 case elfcpp::R_X86_64_PC64:
2166 case elfcpp::R_X86_64_PC32:
2167 case elfcpp::R_X86_64_PC16:
2168 case elfcpp::R_X86_64_PC8:
2169 {
2170 // Make a PLT entry if necessary.
2171 if (gsym->needs_plt_entry())
2172 target->make_plt_entry(symtab, layout, gsym);
2173 // Make a dynamic relocation if necessary.
95a2c8d6 2174 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
86849f1f 2175 {
966d4097 2176 if (gsym->may_need_copy_reloc())
d61c6bd4 2177 {
12c0daef 2178 target->copy_reloc(symtab, layout, object,
7bf1f802 2179 data_shndx, output_section, gsym, reloc);
d61c6bd4 2180 }
86849f1f 2181 else
d61c6bd4 2182 {
a29b0dad 2183 this->check_non_pic(object, r_type, gsym);
d61c6bd4 2184 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
2185 rela_dyn->add_global(gsym, r_type, output_section, object,
2186 data_shndx, reloc.get_r_offset(),
d61c6bd4
ILT
2187 reloc.get_r_addend());
2188 }
86849f1f 2189 }
d61c6bd4 2190 }
2e30d253
ILT
2191 break;
2192
ff006520 2193 case elfcpp::R_X86_64_GOT64:
2e30d253 2194 case elfcpp::R_X86_64_GOT32:
ff006520
ILT
2195 case elfcpp::R_X86_64_GOTPCREL64:
2196 case elfcpp::R_X86_64_GOTPCREL:
2197 case elfcpp::R_X86_64_GOTPLT64:
2e30d253
ILT
2198 {
2199 // The symbol requires a GOT entry.
2200 Output_data_got<64, false>* got = target->got_section(symtab, layout);
7bf1f802 2201 if (gsym->final_value_is_known())
7223e9ca
ILT
2202 {
2203 // For a STT_GNU_IFUNC symbol we want the PLT address.
2204 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2205 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2206 else
2207 got->add_global(gsym, GOT_TYPE_STANDARD);
2208 }
7bf1f802
ILT
2209 else
2210 {
2e30d253
ILT
2211 // If this symbol is not fully resolved, we need to add a
2212 // dynamic relocation for it.
7bf1f802 2213 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7223e9ca
ILT
2214 if (gsym->is_from_dynobj()
2215 || gsym->is_undefined()
2216 || gsym->is_preemptible()
2217 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2218 && parameters->options().output_is_position_independent()))
0a65a3a7 2219 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
7bf1f802
ILT
2220 elfcpp::R_X86_64_GLOB_DAT);
2221 else
2e30d253 2222 {
7223e9ca
ILT
2223 // For a STT_GNU_IFUNC symbol we want to write the PLT
2224 // offset into the GOT, so that function pointer
2225 // comparisons work correctly.
2226 bool is_new;
2227 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2228 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2229 else
2230 {
2231 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2232 // Tell the dynamic linker to use the PLT address
2233 // when resolving relocations.
2234 if (gsym->is_from_dynobj()
2235 && !parameters->options().shared())
2236 gsym->set_needs_dynsym_value();
2237 }
2238 if (is_new)
2239 {
2240 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2241 rela_dyn->add_global_relative(gsym,
2242 elfcpp::R_X86_64_RELATIVE,
2243 got, got_off, 0);
2244 }
2e30d253
ILT
2245 }
2246 }
ee9e9e86
ILT
2247 // For GOTPLT64, we also need a PLT entry (but only if the
2248 // symbol is not fully resolved).
2249 if (r_type == elfcpp::R_X86_64_GOTPLT64
2250 && !gsym->final_value_is_known())
2251 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
2252 }
2253 break;
2254
2255 case elfcpp::R_X86_64_PLT32:
2256 // If the symbol is fully resolved, this is just a PC32 reloc.
2257 // Otherwise we need a PLT entry.
2258 if (gsym->final_value_is_known())
2259 break;
96f2030e
ILT
2260 // If building a shared library, we can also skip the PLT entry
2261 // if the symbol is defined in the output file and is protected
2262 // or hidden.
2263 if (gsym->is_defined()
2264 && !gsym->is_from_dynobj()
2265 && !gsym->is_preemptible())
2266 break;
2e30d253
ILT
2267 target->make_plt_entry(symtab, layout, gsym);
2268 break;
2269
fdc2f80f 2270 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 2271 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
2272 case elfcpp::R_X86_64_GOTPC64:
2273 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
2274 // We need a GOT section.
2275 target->got_section(symtab, layout);
ee9e9e86
ILT
2276 // For PLTOFF64, we also need a PLT entry (but only if the
2277 // symbol is not fully resolved).
2278 if (r_type == elfcpp::R_X86_64_PLTOFF64
2279 && !gsym->final_value_is_known())
2280 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
2281 break;
2282
2e30d253
ILT
2283 case elfcpp::R_X86_64_COPY:
2284 case elfcpp::R_X86_64_GLOB_DAT:
2285 case elfcpp::R_X86_64_JUMP_SLOT:
2286 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 2287 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 2288 // These are outstanding tls relocs, which are unexpected when linking
e822f2b1 2289 case elfcpp::R_X86_64_TPOFF64:
2e30d253 2290 case elfcpp::R_X86_64_DTPMOD64:
e822f2b1 2291 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
2292 gold_error(_("%s: unexpected reloc %u in object file"),
2293 object->name().c_str(), r_type);
2e30d253 2294 break;
2e30d253 2295
d61c17ea 2296 // These are initial tls relocs, which are expected for global()
56622147
ILT
2297 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2298 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 2299 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 2300 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
2301 case elfcpp::R_X86_64_DTPOFF32:
2302 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
2303 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2304 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253
ILT
2305 {
2306 const bool is_final = gsym->final_value_is_known();
e041f13d
ILT
2307 const tls::Tls_optimization optimized_type
2308 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
2309 switch (r_type)
2310 {
56622147 2311 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
2312 if (optimized_type == tls::TLSOPT_NONE)
2313 {
2314 // Create a pair of GOT entries for the module index and
2315 // dtv-relative offset.
2316 Output_data_got<64, false>* got
2317 = target->got_section(symtab, layout);
0a65a3a7
CC
2318 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2319 target->rela_dyn_section(layout),
2320 elfcpp::R_X86_64_DTPMOD64,
2321 elfcpp::R_X86_64_DTPOFF64);
7bf1f802
ILT
2322 }
2323 else if (optimized_type == tls::TLSOPT_TO_IE)
2324 {
2325 // Create a GOT entry for the tp-relative offset.
2326 Output_data_got<64, false>* got
2327 = target->got_section(symtab, layout);
0a65a3a7 2328 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
2329 target->rela_dyn_section(layout),
2330 elfcpp::R_X86_64_TPOFF64);
2331 }
2332 else if (optimized_type != tls::TLSOPT_TO_LE)
2333 unsupported_reloc_global(object, r_type, gsym);
2334 break;
2335
56622147 2336 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 2337 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
2338 if (optimized_type == tls::TLSOPT_NONE)
2339 {
2340 // Create reserved PLT and GOT entries for the resolver.
2341 target->reserve_tlsdesc_entries(symtab, layout);
2342
a8df5856
ILT
2343 // Create a double GOT entry with an R_X86_64_TLSDESC
2344 // reloc. The R_X86_64_TLSDESC reloc is resolved
2345 // lazily, so the GOT entry needs to be in an area in
2346 // .got.plt, not .got. Call got_section to make sure
2347 // the section has been created.
2348 target->got_section(symtab, layout);
2349 Output_data_got<64, false>* got = target->got_tlsdesc_section();
ca09d69a 2350 Reloc_section* rt = target->rela_tlsdesc_section(layout);
e291e7b9 2351 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
c2b45e22
CC
2352 elfcpp::R_X86_64_TLSDESC, 0);
2353 }
2354 else if (optimized_type == tls::TLSOPT_TO_IE)
2355 {
2356 // Create a GOT entry for the tp-relative offset.
2357 Output_data_got<64, false>* got
2358 = target->got_section(symtab, layout);
2359 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2360 target->rela_dyn_section(layout),
2361 elfcpp::R_X86_64_TPOFF64);
2362 }
2363 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 2364 unsupported_reloc_global(object, r_type, gsym);
2e30d253
ILT
2365 break;
2366
c2b45e22
CC
2367 case elfcpp::R_X86_64_TLSDESC_CALL:
2368 break;
2369
e041f13d 2370 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
2371 if (optimized_type == tls::TLSOPT_NONE)
2372 {
2373 // Create a GOT entry for the module index.
31d60480 2374 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
2375 }
2376 else if (optimized_type != tls::TLSOPT_TO_LE)
2377 unsupported_reloc_global(object, r_type, gsym);
2378 break;
2379
0ffd9845
ILT
2380 case elfcpp::R_X86_64_DTPOFF32:
2381 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
2382 break;
2383
56622147 2384 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 2385 layout->set_has_static_tls();
7bf1f802
ILT
2386 if (optimized_type == tls::TLSOPT_NONE)
2387 {
2388 // Create a GOT entry for the tp-relative offset.
2389 Output_data_got<64, false>* got
2390 = target->got_section(symtab, layout);
0a65a3a7 2391 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
2392 target->rela_dyn_section(layout),
2393 elfcpp::R_X86_64_TPOFF64);
2394 }
2395 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
2396 unsupported_reloc_global(object, r_type, gsym);
2397 break;
0ffd9845 2398
56622147 2399 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 2400 layout->set_has_static_tls();
8851ecca 2401 if (parameters->options().shared())
7bf1f802 2402 unsupported_reloc_local(object, r_type);
2e30d253 2403 break;
e041f13d
ILT
2404
2405 default:
2406 gold_unreachable();
2e30d253
ILT
2407 }
2408 }
2409 break;
fdc2f80f
ILT
2410
2411 case elfcpp::R_X86_64_SIZE32:
2412 case elfcpp::R_X86_64_SIZE64:
2e30d253 2413 default:
75f2446e 2414 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12
ILT
2415 object->name().c_str(), r_type,
2416 gsym->demangled_name().c_str());
2e30d253
ILT
2417 break;
2418 }
2419}
2420
6d03d481 2421void
ad0f2072 2422Target_x86_64::gc_process_relocs(Symbol_table* symtab,
6d03d481 2423 Layout* layout,
6fa2a40b 2424 Sized_relobj_file<64, false>* object,
6d03d481
ST
2425 unsigned int data_shndx,
2426 unsigned int sh_type,
2427 const unsigned char* prelocs,
2428 size_t reloc_count,
2429 Output_section* output_section,
2430 bool needs_special_offset_handling,
2431 size_t local_symbol_count,
2432 const unsigned char* plocal_symbols)
2433{
2434
2435 if (sh_type == elfcpp::SHT_REL)
2436 {
2437 return;
2438 }
2439
2440 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
41cbeecc
ST
2441 Target_x86_64::Scan,
2442 Target_x86_64::Relocatable_size_for_reloc>(
6d03d481
ST
2443 symtab,
2444 layout,
2445 this,
2446 object,
2447 data_shndx,
2448 prelocs,
2449 reloc_count,
2450 output_section,
2451 needs_special_offset_handling,
2452 local_symbol_count,
2453 plocal_symbols);
2454
2455}
2e30d253
ILT
2456// Scan relocations for a section.
2457
2458void
ad0f2072 2459Target_x86_64::scan_relocs(Symbol_table* symtab,
d61c17ea 2460 Layout* layout,
6fa2a40b 2461 Sized_relobj_file<64, false>* object,
d61c17ea
ILT
2462 unsigned int data_shndx,
2463 unsigned int sh_type,
2464 const unsigned char* prelocs,
2465 size_t reloc_count,
730cdc88
ILT
2466 Output_section* output_section,
2467 bool needs_special_offset_handling,
d61c17ea 2468 size_t local_symbol_count,
730cdc88 2469 const unsigned char* plocal_symbols)
2e30d253
ILT
2470{
2471 if (sh_type == elfcpp::SHT_REL)
2472 {
75f2446e
ILT
2473 gold_error(_("%s: unsupported REL reloc section"),
2474 object->name().c_str());
2475 return;
2e30d253
ILT
2476 }
2477
2478 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2479 Target_x86_64::Scan>(
2e30d253
ILT
2480 symtab,
2481 layout,
2482 this,
2483 object,
2484 data_shndx,
2485 prelocs,
2486 reloc_count,
730cdc88
ILT
2487 output_section,
2488 needs_special_offset_handling,
2e30d253 2489 local_symbol_count,
730cdc88 2490 plocal_symbols);
2e30d253
ILT
2491}
2492
2493// Finalize the sections.
2494
2495void
f59f41f3
DK
2496Target_x86_64::do_finalize_sections(
2497 Layout* layout,
2498 const Input_objects*,
e785ec03 2499 Symbol_table* symtab)
2e30d253 2500{
ea715a34
ILT
2501 const Reloc_section* rel_plt = (this->plt_ == NULL
2502 ? NULL
e291e7b9 2503 : this->plt_->rela_plt());
ea715a34 2504 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
612a8d3d 2505 this->rela_dyn_, true, false);
ea715a34 2506
2e30d253
ILT
2507 // Fill in some more dynamic tags.
2508 Output_data_dynamic* const odyn = layout->dynamic_data();
2509 if (odyn != NULL)
2510 {
22b127cc 2511 if (this->plt_ != NULL
ea715a34
ILT
2512 && this->plt_->output_section() != NULL
2513 && this->plt_->has_tlsdesc_entry())
2e30d253 2514 {
ea715a34
ILT
2515 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2516 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2517 this->got_->finalize_data_size();
2518 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2519 this->plt_, plt_offset);
2520 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2521 this->got_, got_offset);
2e30d253
ILT
2522 }
2523 }
2524
2525 // Emit any relocs we saved in an attempt to avoid generating COPY
2526 // relocs.
12c0daef
ILT
2527 if (this->copy_relocs_.any_saved_relocs())
2528 this->copy_relocs_.emit(this->rela_dyn_section(layout));
e785ec03
ILT
2529
2530 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2531 // the .got.plt section.
2532 Symbol* sym = this->global_offset_table_;
2533 if (sym != NULL)
2534 {
2535 uint64_t data_size = this->got_plt_->current_data_size();
2536 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2537 }
2e30d253
ILT
2538}
2539
2540// Perform a relocation.
2541
2542inline bool
2543Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2544 Target_x86_64* target,
031cdbed 2545 Output_section*,
2e30d253 2546 size_t relnum,
0ffd9845 2547 const elfcpp::Rela<64, false>& rela,
2e30d253
ILT
2548 unsigned int r_type,
2549 const Sized_symbol<64>* gsym,
2550 const Symbol_value<64>* psymval,
2551 unsigned char* view,
2552 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2553 section_size_type view_size)
2e30d253
ILT
2554{
2555 if (this->skip_call_tls_get_addr_)
2556 {
5efc7cd2
CC
2557 if ((r_type != elfcpp::R_X86_64_PLT32
2558 && r_type != elfcpp::R_X86_64_PC32)
2e30d253 2559 || gsym == NULL
0ffd9845 2560 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2e30d253 2561 {
75f2446e
ILT
2562 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2563 _("missing expected TLS relocation"));
2564 }
2565 else
2566 {
2567 this->skip_call_tls_get_addr_ = false;
2568 return false;
2e30d253 2569 }
2e30d253
ILT
2570 }
2571
6fa2a40b 2572 const Sized_relobj_file<64, false>* object = relinfo->object;
7223e9ca
ILT
2573
2574 // Pick the value to use for symbols defined in the PLT.
2e30d253 2575 Symbol_value<64> symval;
96f2030e 2576 if (gsym != NULL
95a2c8d6 2577 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2e30d253
ILT
2578 {
2579 symval.set_output_value(target->plt_section()->address()
2580 + gsym->plt_offset());
2581 psymval = &symval;
2582 }
7223e9ca
ILT
2583 else if (gsym == NULL && psymval->is_ifunc_symbol())
2584 {
2585 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2586 if (object->local_has_plt_offset(r_sym))
2587 {
2588 symval.set_output_value(target->plt_section()->address()
2589 + object->local_plt_offset(r_sym));
2590 psymval = &symval;
2591 }
2592 }
2e30d253 2593
0ffd9845
ILT
2594 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2595
2596 // Get the GOT offset if needed.
96f2030e
ILT
2597 // The GOT pointer points to the end of the GOT section.
2598 // We need to subtract the size of the GOT section to get
2599 // the actual offset to use in the relocation.
0ffd9845
ILT
2600 bool have_got_offset = false;
2601 unsigned int got_offset = 0;
2602 switch (r_type)
2603 {
2604 case elfcpp::R_X86_64_GOT32:
2605 case elfcpp::R_X86_64_GOT64:
2606 case elfcpp::R_X86_64_GOTPLT64:
2607 case elfcpp::R_X86_64_GOTPCREL:
2608 case elfcpp::R_X86_64_GOTPCREL64:
2609 if (gsym != NULL)
2610 {
0a65a3a7
CC
2611 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2612 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
0ffd9845
ILT
2613 }
2614 else
2615 {
2616 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
2617 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2618 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2619 - target->got_size());
0ffd9845
ILT
2620 }
2621 have_got_offset = true;
2622 break;
2623
2624 default:
2625 break;
2626 }
2e30d253
ILT
2627
2628 switch (r_type)
2629 {
2630 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
2631 case elfcpp::R_X86_64_GNU_VTINHERIT:
2632 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
2633 break;
2634
2635 case elfcpp::R_X86_64_64:
2636 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2637 break;
2638
2639 case elfcpp::R_X86_64_PC64:
2640 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2641 address);
2642 break;
2643
2644 case elfcpp::R_X86_64_32:
7bb3655e
ILT
2645 // FIXME: we need to verify that value + addend fits into 32 bits:
2646 // uint64_t x = value + addend;
2647 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2648 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2e30d253
ILT
2649 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2650 break;
2651
2652 case elfcpp::R_X86_64_32S:
7bb3655e
ILT
2653 // FIXME: we need to verify that value + addend fits into 32 bits:
2654 // int64_t x = value + addend; // note this quantity is signed!
2655 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2e30d253
ILT
2656 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2657 break;
2658
2659 case elfcpp::R_X86_64_PC32:
2660 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2661 address);
2662 break;
2663
2664 case elfcpp::R_X86_64_16:
2665 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2666 break;
2667
2668 case elfcpp::R_X86_64_PC16:
2669 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2670 address);
2671 break;
2672
2673 case elfcpp::R_X86_64_8:
2674 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2675 break;
2676
2677 case elfcpp::R_X86_64_PC8:
2678 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2679 address);
2680 break;
2681
2682 case elfcpp::R_X86_64_PLT32:
f389a824
ILT
2683 gold_assert(gsym == NULL
2684 || gsym->has_plt_offset()
99f8faca
ILT
2685 || gsym->final_value_is_known()
2686 || (gsym->is_defined()
2687 && !gsym->is_from_dynobj()
2688 && !gsym->is_preemptible()));
ee9e9e86
ILT
2689 // Note: while this code looks the same as for R_X86_64_PC32, it
2690 // behaves differently because psymval was set to point to
2691 // the PLT entry, rather than the symbol, in Scan::global().
2e30d253
ILT
2692 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2693 address);
2694 break;
2695
ee9e9e86
ILT
2696 case elfcpp::R_X86_64_PLTOFF64:
2697 {
2698 gold_assert(gsym);
2699 gold_assert(gsym->has_plt_offset()
2700 || gsym->final_value_is_known());
2701 elfcpp::Elf_types<64>::Elf_Addr got_address;
2702 got_address = target->got_section(NULL, NULL)->address();
c1866bd5
ILT
2703 Relocate_functions<64, false>::rela64(view, object, psymval,
2704 addend - got_address);
ee9e9e86
ILT
2705 }
2706
2e30d253 2707 case elfcpp::R_X86_64_GOT32:
0ffd9845
ILT
2708 gold_assert(have_got_offset);
2709 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2e30d253
ILT
2710 break;
2711
e822f2b1
ILT
2712 case elfcpp::R_X86_64_GOTPC32:
2713 {
2714 gold_assert(gsym);
2715 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2716 value = target->got_plt_section()->address();
e822f2b1
ILT
2717 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2718 }
2719 break;
2720
2721 case elfcpp::R_X86_64_GOT64:
2722 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2723 // Since we always add a PLT entry, this is equivalent.
fdc2f80f 2724 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
2725 gold_assert(have_got_offset);
2726 Relocate_functions<64, false>::rela64(view, got_offset, addend);
e822f2b1
ILT
2727 break;
2728
2729 case elfcpp::R_X86_64_GOTPC64:
2730 {
2731 gold_assert(gsym);
2732 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2733 value = target->got_plt_section()->address();
e822f2b1
ILT
2734 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2735 }
2736 break;
2737
2e30d253
ILT
2738 case elfcpp::R_X86_64_GOTOFF64:
2739 {
2740 elfcpp::Elf_types<64>::Elf_Addr value;
2741 value = (psymval->value(object, 0)
96f2030e 2742 - target->got_plt_section()->address());
2e30d253
ILT
2743 Relocate_functions<64, false>::rela64(view, value, addend);
2744 }
2745 break;
2746
2747 case elfcpp::R_X86_64_GOTPCREL:
2748 {
0ffd9845
ILT
2749 gold_assert(have_got_offset);
2750 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2751 value = target->got_plt_section()->address() + got_offset;
0ffd9845 2752 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2e30d253
ILT
2753 }
2754 break;
2755
e822f2b1
ILT
2756 case elfcpp::R_X86_64_GOTPCREL64:
2757 {
0ffd9845
ILT
2758 gold_assert(have_got_offset);
2759 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2760 value = target->got_plt_section()->address() + got_offset;
0ffd9845 2761 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
e822f2b1
ILT
2762 }
2763 break;
2764
2e30d253
ILT
2765 case elfcpp::R_X86_64_COPY:
2766 case elfcpp::R_X86_64_GLOB_DAT:
2767 case elfcpp::R_X86_64_JUMP_SLOT:
2768 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 2769 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 2770 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 2771 case elfcpp::R_X86_64_TPOFF64:
2e30d253 2772 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 2773 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
2774 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2775 _("unexpected reloc %u in object file"),
2776 r_type);
2e30d253
ILT
2777 break;
2778
d61c17ea 2779 // These are initial tls relocs, which are expected when linking
56622147
ILT
2780 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2781 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 2782 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 2783 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
2784 case elfcpp::R_X86_64_DTPOFF32:
2785 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
2786 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2787 case elfcpp::R_X86_64_TPOFF32: // Local-exec
7bf1f802
ILT
2788 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2789 view, address, view_size);
2e30d253 2790 break;
2e30d253 2791
fdc2f80f
ILT
2792 case elfcpp::R_X86_64_SIZE32:
2793 case elfcpp::R_X86_64_SIZE64:
2e30d253 2794 default:
75f2446e
ILT
2795 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2796 _("unsupported reloc %u"),
2797 r_type);
2e30d253
ILT
2798 break;
2799 }
2800
2801 return true;
2802}
2803
2804// Perform a TLS relocation.
2805
2806inline void
d61c17ea 2807Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
7bf1f802 2808 Target_x86_64* target,
d61c17ea 2809 size_t relnum,
72ec2876 2810 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
2811 unsigned int r_type,
2812 const Sized_symbol<64>* gsym,
2813 const Symbol_value<64>* psymval,
2814 unsigned char* view,
6a41d30b 2815 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2816 section_size_type view_size)
2e30d253 2817{
2e30d253 2818 Output_segment* tls_segment = relinfo->layout->tls_segment();
7bf1f802 2819
6fa2a40b 2820 const Sized_relobj_file<64, false>* object = relinfo->object;
6a41d30b 2821 const elfcpp::Elf_Xword addend = rela.get_r_addend();
36171d64
CC
2822 elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
2823 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2e30d253
ILT
2824
2825 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2826
2827 const bool is_final = (gsym == NULL
b3705d2a 2828 ? !parameters->options().shared()
2e30d253 2829 : gsym->final_value_is_known());
36171d64 2830 tls::Tls_optimization optimized_type
e041f13d 2831 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
2832 switch (r_type)
2833 {
56622147 2834 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
36171d64
CC
2835 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2836 {
2837 // If this code sequence is used in a non-executable section,
2838 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2839 // on the assumption that it's being used by itself in a debug
2840 // section. Therefore, in the unlikely event that the code
2841 // sequence appears in a non-executable section, we simply
2842 // leave it unoptimized.
2843 optimized_type = tls::TLSOPT_NONE;
2844 }
e041f13d 2845 if (optimized_type == tls::TLSOPT_TO_LE)
2e30d253 2846 {
7bf1f802 2847 gold_assert(tls_segment != NULL);
2e30d253 2848 this->tls_gd_to_le(relinfo, relnum, tls_segment,
72ec2876 2849 rela, r_type, value, view,
2e30d253
ILT
2850 view_size);
2851 break;
2852 }
7bf1f802
ILT
2853 else
2854 {
c2b45e22
CC
2855 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2856 ? GOT_TYPE_TLS_OFFSET
2857 : GOT_TYPE_TLS_PAIR);
7bf1f802
ILT
2858 unsigned int got_offset;
2859 if (gsym != NULL)
2860 {
c2b45e22
CC
2861 gold_assert(gsym->has_got_offset(got_type));
2862 got_offset = gsym->got_offset(got_type) - target->got_size();
7bf1f802
ILT
2863 }
2864 else
2865 {
2866 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
c2b45e22
CC
2867 gold_assert(object->local_has_got_offset(r_sym, got_type));
2868 got_offset = (object->local_got_offset(r_sym, got_type)
7bf1f802
ILT
2869 - target->got_size());
2870 }
2871 if (optimized_type == tls::TLSOPT_TO_IE)
2872 {
2873 gold_assert(tls_segment != NULL);
c2b45e22 2874 value = target->got_plt_section()->address() + got_offset;
7bf1f802 2875 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
c2b45e22 2876 value, view, address, view_size);
7bf1f802
ILT
2877 break;
2878 }
2879 else if (optimized_type == tls::TLSOPT_NONE)
2880 {
2881 // Relocate the field with the offset of the pair of GOT
2882 // entries.
6a41d30b
ILT
2883 value = target->got_plt_section()->address() + got_offset;
2884 Relocate_functions<64, false>::pcrela32(view, value, addend,
2885 address);
7bf1f802
ILT
2886 break;
2887 }
2888 }
72ec2876 2889 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 2890 _("unsupported reloc %u"), r_type);
2e30d253
ILT
2891 break;
2892
c2b45e22
CC
2893 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2894 case elfcpp::R_X86_64_TLSDESC_CALL:
36171d64
CC
2895 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2896 {
2897 // See above comment for R_X86_64_TLSGD.
2898 optimized_type = tls::TLSOPT_NONE;
2899 }
c2b45e22
CC
2900 if (optimized_type == tls::TLSOPT_TO_LE)
2901 {
2902 gold_assert(tls_segment != NULL);
2903 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2904 rela, r_type, value, view,
2905 view_size);
2906 break;
2907 }
2908 else
2909 {
2910 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2911 ? GOT_TYPE_TLS_OFFSET
2912 : GOT_TYPE_TLS_DESC);
a8df5856
ILT
2913 unsigned int got_offset = 0;
2914 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2915 && optimized_type == tls::TLSOPT_NONE)
2916 {
2917 // We created GOT entries in the .got.tlsdesc portion of
2918 // the .got.plt section, but the offset stored in the
2919 // symbol is the offset within .got.tlsdesc.
2920 got_offset = (target->got_size()
2921 + target->got_plt_section()->data_size());
2922 }
c2b45e22
CC
2923 if (gsym != NULL)
2924 {
2925 gold_assert(gsym->has_got_offset(got_type));
a8df5856 2926 got_offset += gsym->got_offset(got_type) - target->got_size();
c2b45e22
CC
2927 }
2928 else
2929 {
2930 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2931 gold_assert(object->local_has_got_offset(r_sym, got_type));
a8df5856
ILT
2932 got_offset += (object->local_got_offset(r_sym, got_type)
2933 - target->got_size());
c2b45e22
CC
2934 }
2935 if (optimized_type == tls::TLSOPT_TO_IE)
2936 {
2937 gold_assert(tls_segment != NULL);
2938 value = target->got_plt_section()->address() + got_offset;
2939 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2940 rela, r_type, value, view, address,
2941 view_size);
2942 break;
2943 }
2944 else if (optimized_type == tls::TLSOPT_NONE)
2945 {
2946 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2947 {
2948 // Relocate the field with the offset of the pair of GOT
2949 // entries.
2950 value = target->got_plt_section()->address() + got_offset;
2951 Relocate_functions<64, false>::pcrela32(view, value, addend,
2952 address);
2953 }
2954 break;
2955 }
2956 }
2957 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2958 _("unsupported reloc %u"), r_type);
2959 break;
2960
56622147 2961 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
36171d64
CC
2962 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2963 {
2964 // See above comment for R_X86_64_TLSGD.
2965 optimized_type = tls::TLSOPT_NONE;
2966 }
e041f13d
ILT
2967 if (optimized_type == tls::TLSOPT_TO_LE)
2968 {
7bf1f802 2969 gold_assert(tls_segment != NULL);
72ec2876
ILT
2970 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2971 value, view, view_size);
2972 break;
e041f13d 2973 }
7bf1f802
ILT
2974 else if (optimized_type == tls::TLSOPT_NONE)
2975 {
2976 // Relocate the field with the offset of the GOT entry for
2977 // the module index.
2978 unsigned int got_offset;
31d60480
ILT
2979 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2980 - target->got_size());
6a41d30b
ILT
2981 value = target->got_plt_section()->address() + got_offset;
2982 Relocate_functions<64, false>::pcrela32(view, value, addend,
2983 address);
7bf1f802
ILT
2984 break;
2985 }
72ec2876 2986 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 2987 _("unsupported reloc %u"), r_type);
2e30d253 2988 break;
0ffd9845
ILT
2989
2990 case elfcpp::R_X86_64_DTPOFF32:
36171d64
CC
2991 // This relocation type is used in debugging information.
2992 // In that case we need to not optimize the value. If the
2993 // section is not executable, then we assume we should not
2994 // optimize this reloc. See comments above for R_X86_64_TLSGD,
2995 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
2996 // R_X86_64_TLSLD.
2997 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2998 {
2999 gold_assert(tls_segment != NULL);
3000 value -= tls_segment->memsz();
3001 }
d85c80a3 3002 Relocate_functions<64, false>::rela32(view, value, addend);
0ffd9845
ILT
3003 break;
3004
3005 case elfcpp::R_X86_64_DTPOFF64:
36171d64
CC
3006 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3007 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3008 {
3009 gold_assert(tls_segment != NULL);
3010 value -= tls_segment->memsz();
3011 }
d85c80a3 3012 Relocate_functions<64, false>::rela64(view, value, addend);
0ffd9845 3013 break;
2e30d253 3014
56622147
ILT
3015 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3016 if (optimized_type == tls::TLSOPT_TO_LE)
3017 {
7bf1f802 3018 gold_assert(tls_segment != NULL);
56622147
ILT
3019 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3020 rela, r_type, value, view,
3021 view_size);
3022 break;
3023 }
7bf1f802
ILT
3024 else if (optimized_type == tls::TLSOPT_NONE)
3025 {
3026 // Relocate the field with the offset of the GOT entry for
3027 // the tp-relative offset of the symbol.
3028 unsigned int got_offset;
3029 if (gsym != NULL)
3030 {
0a65a3a7
CC
3031 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3032 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3033 - target->got_size());
7bf1f802
ILT
3034 }
3035 else
3036 {
3037 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
3038 gold_assert(object->local_has_got_offset(r_sym,
3039 GOT_TYPE_TLS_OFFSET));
3040 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
7bf1f802
ILT
3041 - target->got_size());
3042 }
6a41d30b
ILT
3043 value = target->got_plt_section()->address() + got_offset;
3044 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
7bf1f802
ILT
3045 break;
3046 }
56622147
ILT
3047 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3048 _("unsupported reloc type %u"),
3049 r_type);
3050 break;
0ffd9845 3051
56622147 3052 case elfcpp::R_X86_64_TPOFF32: // Local-exec
6a41d30b 3053 value -= tls_segment->memsz();
d85c80a3 3054 Relocate_functions<64, false>::rela32(view, value, addend);
56622147 3055 break;
2e30d253 3056 }
2e30d253
ILT
3057}
3058
7bf1f802
ILT
3059// Do a relocation in which we convert a TLS General-Dynamic to an
3060// Initial-Exec.
3061
3062inline void
3063Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
3064 size_t relnum,
c2b45e22 3065 Output_segment*,
7bf1f802
ILT
3066 const elfcpp::Rela<64, false>& rela,
3067 unsigned int,
3068 elfcpp::Elf_types<64>::Elf_Addr value,
3069 unsigned char* view,
c2b45e22 3070 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 3071 section_size_type view_size)
7bf1f802
ILT
3072{
3073 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3074 // .word 0x6666; rex64; call __tls_get_addr
3075 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3076
3077 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3078 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3079
3080 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3081 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3082 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3083 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3084
3085 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3086
c2b45e22
CC
3087 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3088 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
7bf1f802
ILT
3089
3090 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3091 // We can skip it.
3092 this->skip_call_tls_get_addr_ = true;
3093}
3094
e041f13d 3095// Do a relocation in which we convert a TLS General-Dynamic to a
2e30d253
ILT
3096// Local-Exec.
3097
3098inline void
d61c17ea
ILT
3099Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
3100 size_t relnum,
3101 Output_segment* tls_segment,
72ec2876 3102 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
3103 unsigned int,
3104 elfcpp::Elf_types<64>::Elf_Addr value,
3105 unsigned char* view,
fe8718a4 3106 section_size_type view_size)
2e30d253 3107{
0ffd9845
ILT
3108 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3109 // .word 0x6666; rex64; call __tls_get_addr
3110 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2e30d253 3111
72ec2876
ILT
3112 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3113 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2e30d253 3114
72ec2876
ILT
3115 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3116 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3117 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3118 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2e30d253 3119
0ffd9845 3120 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2e30d253 3121
6a41d30b 3122 value -= tls_segment->memsz();
0ffd9845 3123 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2e30d253
ILT
3124
3125 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3126 // We can skip it.
3127 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
3128}
3129
c2b45e22
CC
3130// Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3131
3132inline void
3133Target_x86_64::Relocate::tls_desc_gd_to_ie(
3134 const Relocate_info<64, false>* relinfo,
3135 size_t relnum,
3136 Output_segment*,
3137 const elfcpp::Rela<64, false>& rela,
3138 unsigned int r_type,
3139 elfcpp::Elf_types<64>::Elf_Addr value,
3140 unsigned char* view,
3141 elfcpp::Elf_types<64>::Elf_Addr address,
3142 section_size_type view_size)
3143{
3144 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3145 {
3146 // leaq foo@tlsdesc(%rip), %rax
3147 // ==> movq foo@gottpoff(%rip), %rax
3148 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3149 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3150 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3151 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3152 view[-2] = 0x8b;
3153 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3154 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3155 }
3156 else
3157 {
3158 // call *foo@tlscall(%rax)
3159 // ==> nop; nop
3160 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3161 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3162 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3163 view[0] == 0xff && view[1] == 0x10);
3164 view[0] = 0x66;
3165 view[1] = 0x90;
3166 }
3167}
3168
3169// Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3170
3171inline void
3172Target_x86_64::Relocate::tls_desc_gd_to_le(
3173 const Relocate_info<64, false>* relinfo,
3174 size_t relnum,
3175 Output_segment* tls_segment,
3176 const elfcpp::Rela<64, false>& rela,
3177 unsigned int r_type,
3178 elfcpp::Elf_types<64>::Elf_Addr value,
3179 unsigned char* view,
3180 section_size_type view_size)
3181{
3182 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3183 {
3184 // leaq foo@tlsdesc(%rip), %rax
3185 // ==> movq foo@tpoff, %rax
3186 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3187 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3188 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3189 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3190 view[-2] = 0xc7;
3191 view[-1] = 0xc0;
3192 value -= tls_segment->memsz();
3193 Relocate_functions<64, false>::rela32(view, value, 0);
3194 }
3195 else
3196 {
3197 // call *foo@tlscall(%rax)
3198 // ==> nop; nop
3199 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3200 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3201 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3202 view[0] == 0xff && view[1] == 0x10);
3203 view[0] = 0x66;
3204 view[1] = 0x90;
3205 }
3206}
3207
2e30d253 3208inline void
72ec2876
ILT
3209Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
3210 size_t relnum,
3211 Output_segment*,
3212 const elfcpp::Rela<64, false>& rela,
3213 unsigned int,
3214 elfcpp::Elf_types<64>::Elf_Addr,
3215 unsigned char* view,
fe8718a4 3216 section_size_type view_size)
2e30d253 3217{
72ec2876
ILT
3218 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3219 // ... leq foo@dtpoff(%rax),%reg
3220 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2e30d253 3221
72ec2876
ILT
3222 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3223 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2e30d253 3224
72ec2876
ILT
3225 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3226 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3227
3228 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3229
3230 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3231
3232 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3233 // We can skip it.
3234 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
3235}
3236
56622147
ILT
3237// Do a relocation in which we convert a TLS Initial-Exec to a
3238// Local-Exec.
3239
3240inline void
3241Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
3242 size_t relnum,
3243 Output_segment* tls_segment,
3244 const elfcpp::Rela<64, false>& rela,
3245 unsigned int,
3246 elfcpp::Elf_types<64>::Elf_Addr value,
3247 unsigned char* view,
fe8718a4 3248 section_size_type view_size)
56622147
ILT
3249{
3250 // We need to examine the opcodes to figure out which instruction we
3251 // are looking at.
3252
3253 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3254 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3255
3256 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3257 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3258
3259 unsigned char op1 = view[-3];
3260 unsigned char op2 = view[-2];
3261 unsigned char op3 = view[-1];
3262 unsigned char reg = op3 >> 3;
3263
3264 if (op2 == 0x8b)
3265 {
3266 // movq
3267 if (op1 == 0x4c)
3268 view[-3] = 0x49;
3269 view[-2] = 0xc7;
3270 view[-1] = 0xc0 | reg;
3271 }
3272 else if (reg == 4)
3273 {
3274 // Special handling for %rsp.
3275 if (op1 == 0x4c)
3276 view[-3] = 0x49;
3277 view[-2] = 0x81;
3278 view[-1] = 0xc0 | reg;
3279 }
3280 else
3281 {
3282 // addq
3283 if (op1 == 0x4c)
3284 view[-3] = 0x4d;
3285 view[-2] = 0x8d;
3286 view[-1] = 0x80 | reg | (reg << 3);
3287 }
3288
6a41d30b 3289 value -= tls_segment->memsz();
56622147
ILT
3290 Relocate_functions<64, false>::rela32(view, value, 0);
3291}
3292
2e30d253
ILT
3293// Relocate section data.
3294
3295void
364c7fa5
ILT
3296Target_x86_64::relocate_section(
3297 const Relocate_info<64, false>* relinfo,
3298 unsigned int sh_type,
3299 const unsigned char* prelocs,
3300 size_t reloc_count,
3301 Output_section* output_section,
3302 bool needs_special_offset_handling,
3303 unsigned char* view,
3304 elfcpp::Elf_types<64>::Elf_Addr address,
3305 section_size_type view_size,
3306 const Reloc_symbol_changes* reloc_symbol_changes)
2e30d253
ILT
3307{
3308 gold_assert(sh_type == elfcpp::SHT_RELA);
3309
3310 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3311 Target_x86_64::Relocate>(
3312 relinfo,
3313 this,
3314 prelocs,
3315 reloc_count,
730cdc88
ILT
3316 output_section,
3317 needs_special_offset_handling,
2e30d253
ILT
3318 view,
3319 address,
364c7fa5
ILT
3320 view_size,
3321 reloc_symbol_changes);
2e30d253
ILT
3322}
3323
94a3fc8b
CC
3324// Apply an incremental relocation. Incremental relocations always refer
3325// to global symbols.
3326
3327void
3328Target_x86_64::apply_relocation(
3329 const Relocate_info<64, false>* relinfo,
3330 elfcpp::Elf_types<64>::Elf_Addr r_offset,
3331 unsigned int r_type,
3332 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
3333 const Symbol* gsym,
3334 unsigned char* view,
3335 elfcpp::Elf_types<64>::Elf_Addr address,
3336 section_size_type view_size)
3337{
3338 gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>(
3339 relinfo,
3340 this,
3341 r_offset,
3342 r_type,
3343 r_addend,
3344 gsym,
3345 view,
3346 address,
3347 view_size);
3348}
3349
6a74a719
ILT
3350// Return the size of a relocation while scanning during a relocatable
3351// link.
3352
3353unsigned int
3354Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3355 unsigned int r_type,
3356 Relobj* object)
3357{
3358 switch (r_type)
3359 {
3360 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
3361 case elfcpp::R_X86_64_GNU_VTINHERIT:
3362 case elfcpp::R_X86_64_GNU_VTENTRY:
6a74a719
ILT
3363 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3364 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3365 case elfcpp::R_X86_64_TLSDESC_CALL:
3366 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3367 case elfcpp::R_X86_64_DTPOFF32:
3368 case elfcpp::R_X86_64_DTPOFF64:
3369 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3370 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3371 return 0;
3372
3373 case elfcpp::R_X86_64_64:
3374 case elfcpp::R_X86_64_PC64:
3375 case elfcpp::R_X86_64_GOTOFF64:
3376 case elfcpp::R_X86_64_GOTPC64:
3377 case elfcpp::R_X86_64_PLTOFF64:
3378 case elfcpp::R_X86_64_GOT64:
3379 case elfcpp::R_X86_64_GOTPCREL64:
3380 case elfcpp::R_X86_64_GOTPCREL:
3381 case elfcpp::R_X86_64_GOTPLT64:
3382 return 8;
3383
3384 case elfcpp::R_X86_64_32:
3385 case elfcpp::R_X86_64_32S:
3386 case elfcpp::R_X86_64_PC32:
3387 case elfcpp::R_X86_64_PLT32:
3388 case elfcpp::R_X86_64_GOTPC32:
3389 case elfcpp::R_X86_64_GOT32:
3390 return 4;
3391
3392 case elfcpp::R_X86_64_16:
3393 case elfcpp::R_X86_64_PC16:
3394 return 2;
3395
3396 case elfcpp::R_X86_64_8:
3397 case elfcpp::R_X86_64_PC8:
3398 return 1;
3399
3400 case elfcpp::R_X86_64_COPY:
3401 case elfcpp::R_X86_64_GLOB_DAT:
3402 case elfcpp::R_X86_64_JUMP_SLOT:
3403 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 3404 case elfcpp::R_X86_64_IRELATIVE:
6a74a719
ILT
3405 // These are outstanding tls relocs, which are unexpected when linking
3406 case elfcpp::R_X86_64_TPOFF64:
3407 case elfcpp::R_X86_64_DTPMOD64:
3408 case elfcpp::R_X86_64_TLSDESC:
3409 object->error(_("unexpected reloc %u in object file"), r_type);
3410 return 0;
3411
3412 case elfcpp::R_X86_64_SIZE32:
3413 case elfcpp::R_X86_64_SIZE64:
3414 default:
3415 object->error(_("unsupported reloc %u against local symbol"), r_type);
3416 return 0;
3417 }
3418}
3419
3420// Scan the relocs during a relocatable link.
3421
3422void
ad0f2072 3423Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
6a74a719 3424 Layout* layout,
6fa2a40b 3425 Sized_relobj_file<64, false>* object,
6a74a719
ILT
3426 unsigned int data_shndx,
3427 unsigned int sh_type,
3428 const unsigned char* prelocs,
3429 size_t reloc_count,
3430 Output_section* output_section,
3431 bool needs_special_offset_handling,
3432 size_t local_symbol_count,
3433 const unsigned char* plocal_symbols,
3434 Relocatable_relocs* rr)
3435{
3436 gold_assert(sh_type == elfcpp::SHT_RELA);
3437
3438 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3439 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3440
7019cd25 3441 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
6a74a719 3442 Scan_relocatable_relocs>(
6a74a719
ILT
3443 symtab,
3444 layout,
3445 object,
3446 data_shndx,
3447 prelocs,
3448 reloc_count,
3449 output_section,
3450 needs_special_offset_handling,
3451 local_symbol_count,
3452 plocal_symbols,
3453 rr);
3454}
3455
3456// Relocate a section during a relocatable link.
3457
3458void
3459Target_x86_64::relocate_for_relocatable(
3460 const Relocate_info<64, false>* relinfo,
3461 unsigned int sh_type,
3462 const unsigned char* prelocs,
3463 size_t reloc_count,
3464 Output_section* output_section,
3465 off_t offset_in_output_section,
3466 const Relocatable_relocs* rr,
3467 unsigned char* view,
3468 elfcpp::Elf_types<64>::Elf_Addr view_address,
3469 section_size_type view_size,
3470 unsigned char* reloc_view,
3471 section_size_type reloc_view_size)
3472{
3473 gold_assert(sh_type == elfcpp::SHT_RELA);
3474
7019cd25 3475 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
6a74a719
ILT
3476 relinfo,
3477 prelocs,
3478 reloc_count,
3479 output_section,
3480 offset_in_output_section,
3481 rr,
3482 view,
3483 view_address,
3484 view_size,
3485 reloc_view,
3486 reloc_view_size);
3487}
3488
4fb6c25d
ILT
3489// Return the value to use for a dynamic which requires special
3490// treatment. This is how we support equality comparisons of function
3491// pointers across shared library boundaries, as described in the
3492// processor specific ABI supplement.
3493
3494uint64_t
3495Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3496{
3497 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3498 return this->plt_section()->address() + gsym->plt_offset();
3499}
3500
2e30d253
ILT
3501// Return a string used to fill a code section with nops to take up
3502// the specified length.
3503
3504std::string
8851ecca 3505Target_x86_64::do_code_fill(section_size_type length) const
2e30d253
ILT
3506{
3507 if (length >= 16)
3508 {
3509 // Build a jmpq instruction to skip over the bytes.
3510 unsigned char jmp[5];
3511 jmp[0] = 0xe9;
04bf7072 3512 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2e30d253
ILT
3513 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3514 + std::string(length - 5, '\0'));
3515 }
3516
3517 // Nop sequences of various lengths.
3518 const char nop1[1] = { 0x90 }; // nop
3519 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1caf2c51
ILT
3520 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3521 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3522 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3523 0x00 };
3524 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
2e30d253 3525 0x00, 0x00 };
1caf2c51 3526 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
2e30d253 3527 0x00, 0x00, 0x00 };
1caf2c51
ILT
3528 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3529 0x00, 0x00, 0x00, 0x00 };
3530 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3531 0x00, 0x00, 0x00, 0x00,
2e30d253 3532 0x00 };
1caf2c51
ILT
3533 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3534 0x84, 0x00, 0x00, 0x00,
2e30d253 3535 0x00, 0x00 };
1caf2c51
ILT
3536 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3537 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 3538 0x00, 0x00, 0x00 };
1caf2c51
ILT
3539 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3540 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 3541 0x00, 0x00, 0x00, 0x00 };
1caf2c51
ILT
3542 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3543 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3544 0x00, 0x00, 0x00, 0x00,
2e30d253 3545 0x00 };
1caf2c51
ILT
3546 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3547 0x66, 0x2e, 0x0f, 0x1f, // data16
3548 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 3549 0x00, 0x00 };
1caf2c51
ILT
3550 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3551 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3552 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3553 0x00, 0x00, 0x00 };
2e30d253
ILT
3554
3555 const char* nops[16] = {
3556 NULL,
3557 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3558 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3559 };
3560
3561 return std::string(nops[length], length);
3562}
3563
e291e7b9
ILT
3564// Return the addend to use for a target specific relocation. The
3565// only target specific relocation is R_X86_64_TLSDESC for a local
3566// symbol. We want to set the addend is the offset of the local
3567// symbol in the TLS segment.
3568
3569uint64_t
3570Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3571 uint64_t) const
3572{
3573 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3574 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3575 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3576 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3577 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3578 gold_assert(psymval->is_tls_symbol());
3579 // The value of a TLS symbol is the offset in the TLS segment.
3580 return psymval->value(ti.object, 0);
3581}
3582
364c7fa5 3583// FNOFFSET in section SHNDX in OBJECT is the start of a function
9b547ce6 3584// compiled with -fsplit-stack. The function calls non-split-stack
364c7fa5
ILT
3585// code. We have to change the function so that it always ensures
3586// that it has enough stack space to run some random function.
3587
3588void
3589Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3590 section_offset_type fnoffset,
3591 section_size_type fnsize,
3592 unsigned char* view,
3593 section_size_type view_size,
3594 std::string* from,
3595 std::string* to) const
3596{
3597 // The function starts with a comparison of the stack pointer and a
3598 // field in the TCB. This is followed by a jump.
3599
3600 // cmp %fs:NN,%rsp
3601 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3602 && fnsize > 9)
3603 {
3604 // We will call __morestack if the carry flag is set after this
3605 // comparison. We turn the comparison into an stc instruction
3606 // and some nops.
3607 view[fnoffset] = '\xf9';
3608 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3609 }
3610 // lea NN(%rsp),%r10
cbc999b9
ILT
3611 // lea NN(%rsp),%r11
3612 else if ((this->match_view(view, view_size, fnoffset,
3613 "\x4c\x8d\x94\x24", 4)
3614 || this->match_view(view, view_size, fnoffset,
3615 "\x4c\x8d\x9c\x24", 4))
364c7fa5
ILT
3616 && fnsize > 8)
3617 {
3618 // This is loading an offset from the stack pointer for a
3619 // comparison. The offset is negative, so we decrease the
3620 // offset by the amount of space we need for the stack. This
3621 // means we will avoid calling __morestack if there happens to
3622 // be plenty of space on the stack already.
3623 unsigned char* pval = view + fnoffset + 4;
3624 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3625 val -= parameters->options().split_stack_adjust_size();
3626 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3627 }
3628 else
3629 {
3630 if (!object->has_no_split_stack())
3631 object->error(_("failed to match split-stack sequence at "
3632 "section %u offset %0zx"),
ac33a407 3633 shndx, static_cast<size_t>(fnoffset));
364c7fa5
ILT
3634 return;
3635 }
3636
3637 // We have to change the function so that it calls
3638 // __morestack_non_split instead of __morestack. The former will
3639 // allocate additional stack space.
3640 *from = "__morestack";
3641 *to = "__morestack_non_split";
3642}
3643
2e30d253
ILT
3644// The selector for x86_64 object files.
3645
36959681 3646class Target_selector_x86_64 : public Target_selector_freebsd
2e30d253
ILT
3647{
3648public:
3649 Target_selector_x86_64()
36959681 3650 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
03ef7571 3651 "elf64-x86-64-freebsd", "elf_x86_64")
2e30d253
ILT
3652 { }
3653
3654 Target*
e96caa79
ILT
3655 do_instantiate_target()
3656 { return new Target_x86_64(); }
36959681 3657
2e30d253
ILT
3658};
3659
2e30d253
ILT
3660Target_selector_x86_64 target_selector_x86_64;
3661
3662} // End anonymous namespace.
This page took 0.603379 seconds and 4 git commands to generate.