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