Check if -mcmodel=medium works
[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 }
7223e9ca
ILT
2479 else if (r_type == elfcpp::R_X86_64_64
2480 && gsym->type() == elfcpp::STT_GNU_IFUNC
2481 && gsym->can_use_relative_reloc(false)
2482 && !gsym->is_from_dynobj()
2483 && !gsym->is_undefined()
2484 && !gsym->is_preemptible())
2485 {
2486 // Use an IRELATIVE reloc for a locally defined
2487 // STT_GNU_IFUNC symbol. This makes a function
2488 // address in a PIE executable match the address in a
2489 // shared library that it links against.
67181c72
ILT
2490 Reloc_section* rela_dyn =
2491 target->rela_irelative_section(layout);
7223e9ca
ILT
2492 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2493 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2494 output_section, object,
2495 data_shndx,
2496 reloc.get_r_offset(),
2497 reloc.get_r_addend());
2498 }
d61c6bd4
ILT
2499 else if (r_type == elfcpp::R_X86_64_64
2500 && gsym->can_use_relative_reloc(false))
2501 {
2502 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7223e9ca
ILT
2503 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2504 output_section, object,
2505 data_shndx,
2506 reloc.get_r_offset(),
2507 reloc.get_r_addend());
d61c6bd4
ILT
2508 }
2509 else
2510 {
a29b0dad 2511 this->check_non_pic(object, r_type, gsym);
96f2030e 2512 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
2513 rela_dyn->add_global(gsym, r_type, output_section, object,
2514 data_shndx, reloc.get_r_offset(),
96f2030e 2515 reloc.get_r_addend());
d61c6bd4
ILT
2516 }
2517 }
2518 }
2519 break;
2520
2521 case elfcpp::R_X86_64_PC64:
2522 case elfcpp::R_X86_64_PC32:
2523 case elfcpp::R_X86_64_PC16:
2524 case elfcpp::R_X86_64_PC8:
2525 {
2526 // Make a PLT entry if necessary.
2527 if (gsym->needs_plt_entry())
2528 target->make_plt_entry(symtab, layout, gsym);
2529 // Make a dynamic relocation if necessary.
95a2c8d6 2530 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
86849f1f 2531 {
966d4097 2532 if (gsym->may_need_copy_reloc())
d61c6bd4 2533 {
12c0daef 2534 target->copy_reloc(symtab, layout, object,
7bf1f802 2535 data_shndx, output_section, gsym, reloc);
d61c6bd4 2536 }
86849f1f 2537 else
d61c6bd4 2538 {
a29b0dad 2539 this->check_non_pic(object, r_type, gsym);
d61c6bd4 2540 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
2541 rela_dyn->add_global(gsym, r_type, output_section, object,
2542 data_shndx, reloc.get_r_offset(),
d61c6bd4
ILT
2543 reloc.get_r_addend());
2544 }
86849f1f 2545 }
d61c6bd4 2546 }
2e30d253
ILT
2547 break;
2548
ff006520 2549 case elfcpp::R_X86_64_GOT64:
2e30d253 2550 case elfcpp::R_X86_64_GOT32:
ff006520
ILT
2551 case elfcpp::R_X86_64_GOTPCREL64:
2552 case elfcpp::R_X86_64_GOTPCREL:
2553 case elfcpp::R_X86_64_GOTPLT64:
2e30d253
ILT
2554 {
2555 // The symbol requires a GOT entry.
2556 Output_data_got<64, false>* got = target->got_section(symtab, layout);
7bf1f802 2557 if (gsym->final_value_is_known())
7223e9ca
ILT
2558 {
2559 // For a STT_GNU_IFUNC symbol we want the PLT address.
2560 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2561 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2562 else
2563 got->add_global(gsym, GOT_TYPE_STANDARD);
2564 }
7bf1f802
ILT
2565 else
2566 {
2e30d253
ILT
2567 // If this symbol is not fully resolved, we need to add a
2568 // dynamic relocation for it.
7bf1f802 2569 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
07aa62f2
ILT
2570
2571 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2572 //
2573 // 1) The symbol may be defined in some other module.
2574 //
2575 // 2) We are building a shared library and this is a
2576 // protected symbol; using GLOB_DAT means that the dynamic
2577 // linker can use the address of the PLT in the main
2578 // executable when appropriate so that function address
2579 // comparisons work.
2580 //
2581 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2582 // code, again so that function address comparisons work.
7223e9ca
ILT
2583 if (gsym->is_from_dynobj()
2584 || gsym->is_undefined()
2585 || gsym->is_preemptible()
07aa62f2
ILT
2586 || (gsym->visibility() == elfcpp::STV_PROTECTED
2587 && parameters->options().shared())
7223e9ca
ILT
2588 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2589 && parameters->options().output_is_position_independent()))
83896202
ILT
2590 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2591 elfcpp::R_X86_64_GLOB_DAT);
7bf1f802 2592 else
2e30d253 2593 {
7223e9ca
ILT
2594 // For a STT_GNU_IFUNC symbol we want to write the PLT
2595 // offset into the GOT, so that function pointer
2596 // comparisons work correctly.
2597 bool is_new;
2598 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2599 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2600 else
2601 {
2602 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2603 // Tell the dynamic linker to use the PLT address
2604 // when resolving relocations.
2605 if (gsym->is_from_dynobj()
2606 && !parameters->options().shared())
2607 gsym->set_needs_dynsym_value();
2608 }
2609 if (is_new)
2610 {
2611 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2612 rela_dyn->add_global_relative(gsym,
2613 elfcpp::R_X86_64_RELATIVE,
2614 got, got_off, 0);
2615 }
2e30d253
ILT
2616 }
2617 }
ee9e9e86
ILT
2618 // For GOTPLT64, we also need a PLT entry (but only if the
2619 // symbol is not fully resolved).
2620 if (r_type == elfcpp::R_X86_64_GOTPLT64
2621 && !gsym->final_value_is_known())
2622 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
2623 }
2624 break;
2625
2626 case elfcpp::R_X86_64_PLT32:
2627 // If the symbol is fully resolved, this is just a PC32 reloc.
2628 // Otherwise we need a PLT entry.
2629 if (gsym->final_value_is_known())
2630 break;
96f2030e
ILT
2631 // If building a shared library, we can also skip the PLT entry
2632 // if the symbol is defined in the output file and is protected
2633 // or hidden.
2634 if (gsym->is_defined()
2635 && !gsym->is_from_dynobj()
2636 && !gsym->is_preemptible())
2637 break;
2e30d253
ILT
2638 target->make_plt_entry(symtab, layout, gsym);
2639 break;
2640
fdc2f80f 2641 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 2642 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
2643 case elfcpp::R_X86_64_GOTPC64:
2644 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
2645 // We need a GOT section.
2646 target->got_section(symtab, layout);
ee9e9e86
ILT
2647 // For PLTOFF64, we also need a PLT entry (but only if the
2648 // symbol is not fully resolved).
2649 if (r_type == elfcpp::R_X86_64_PLTOFF64
2650 && !gsym->final_value_is_known())
2651 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
2652 break;
2653
2e30d253
ILT
2654 case elfcpp::R_X86_64_COPY:
2655 case elfcpp::R_X86_64_GLOB_DAT:
2656 case elfcpp::R_X86_64_JUMP_SLOT:
2657 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 2658 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 2659 // These are outstanding tls relocs, which are unexpected when linking
e822f2b1 2660 case elfcpp::R_X86_64_TPOFF64:
2e30d253 2661 case elfcpp::R_X86_64_DTPMOD64:
e822f2b1 2662 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
2663 gold_error(_("%s: unexpected reloc %u in object file"),
2664 object->name().c_str(), r_type);
2e30d253 2665 break;
2e30d253 2666
d61c17ea 2667 // These are initial tls relocs, which are expected for global()
56622147
ILT
2668 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2669 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 2670 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 2671 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
2672 case elfcpp::R_X86_64_DTPOFF32:
2673 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
2674 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2675 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253
ILT
2676 {
2677 const bool is_final = gsym->final_value_is_known();
e041f13d 2678 const tls::Tls_optimization optimized_type
fc51264f 2679 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
2680 switch (r_type)
2681 {
56622147 2682 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
2683 if (optimized_type == tls::TLSOPT_NONE)
2684 {
2685 // Create a pair of GOT entries for the module index and
2686 // dtv-relative offset.
2687 Output_data_got<64, false>* got
2688 = target->got_section(symtab, layout);
83896202
ILT
2689 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2690 target->rela_dyn_section(layout),
2691 elfcpp::R_X86_64_DTPMOD64,
2692 elfcpp::R_X86_64_DTPOFF64);
7bf1f802
ILT
2693 }
2694 else if (optimized_type == tls::TLSOPT_TO_IE)
2695 {
2696 // Create a GOT entry for the tp-relative offset.
2697 Output_data_got<64, false>* got
2698 = target->got_section(symtab, layout);
83896202
ILT
2699 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2700 target->rela_dyn_section(layout),
2701 elfcpp::R_X86_64_TPOFF64);
7bf1f802
ILT
2702 }
2703 else if (optimized_type != tls::TLSOPT_TO_LE)
2704 unsupported_reloc_global(object, r_type, gsym);
2705 break;
2706
56622147 2707 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 2708 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
2709 if (optimized_type == tls::TLSOPT_NONE)
2710 {
2711 // Create reserved PLT and GOT entries for the resolver.
2712 target->reserve_tlsdesc_entries(symtab, layout);
2713
a8df5856
ILT
2714 // Create a double GOT entry with an R_X86_64_TLSDESC
2715 // reloc. The R_X86_64_TLSDESC reloc is resolved
2716 // lazily, so the GOT entry needs to be in an area in
2717 // .got.plt, not .got. Call got_section to make sure
2718 // the section has been created.
2719 target->got_section(symtab, layout);
2720 Output_data_got<64, false>* got = target->got_tlsdesc_section();
ca09d69a 2721 Reloc_section* rt = target->rela_tlsdesc_section(layout);
83896202
ILT
2722 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
2723 elfcpp::R_X86_64_TLSDESC, 0);
c2b45e22
CC
2724 }
2725 else if (optimized_type == tls::TLSOPT_TO_IE)
2726 {
2727 // Create a GOT entry for the tp-relative offset.
2728 Output_data_got<64, false>* got
2729 = target->got_section(symtab, layout);
83896202
ILT
2730 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2731 target->rela_dyn_section(layout),
2732 elfcpp::R_X86_64_TPOFF64);
c2b45e22
CC
2733 }
2734 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 2735 unsupported_reloc_global(object, r_type, gsym);
2e30d253
ILT
2736 break;
2737
c2b45e22
CC
2738 case elfcpp::R_X86_64_TLSDESC_CALL:
2739 break;
2740
e041f13d 2741 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
2742 if (optimized_type == tls::TLSOPT_NONE)
2743 {
2744 // Create a GOT entry for the module index.
31d60480 2745 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
2746 }
2747 else if (optimized_type != tls::TLSOPT_TO_LE)
2748 unsupported_reloc_global(object, r_type, gsym);
2749 break;
2750
0ffd9845
ILT
2751 case elfcpp::R_X86_64_DTPOFF32:
2752 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
2753 break;
2754
56622147 2755 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 2756 layout->set_has_static_tls();
7bf1f802
ILT
2757 if (optimized_type == tls::TLSOPT_NONE)
2758 {
2759 // Create a GOT entry for the tp-relative offset.
2760 Output_data_got<64, false>* got
2761 = target->got_section(symtab, layout);
83896202
ILT
2762 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2763 target->rela_dyn_section(layout),
2764 elfcpp::R_X86_64_TPOFF64);
7bf1f802
ILT
2765 }
2766 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
2767 unsupported_reloc_global(object, r_type, gsym);
2768 break;
0ffd9845 2769
56622147 2770 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 2771 layout->set_has_static_tls();
8851ecca 2772 if (parameters->options().shared())
7bf1f802 2773 unsupported_reloc_local(object, r_type);
2e30d253 2774 break;
e041f13d
ILT
2775
2776 default:
2777 gold_unreachable();
2e30d253
ILT
2778 }
2779 }
2780 break;
fdc2f80f
ILT
2781
2782 case elfcpp::R_X86_64_SIZE32:
2783 case elfcpp::R_X86_64_SIZE64:
2e30d253 2784 default:
75f2446e 2785 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12
ILT
2786 object->name().c_str(), r_type,
2787 gsym->demangled_name().c_str());
2e30d253
ILT
2788 break;
2789 }
2790}
2791
fc51264f 2792template<int size>
6d03d481 2793void
fc51264f
L
2794Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
2795 Layout* layout,
2796 Sized_relobj_file<size, false>* object,
2797 unsigned int data_shndx,
2798 unsigned int sh_type,
2799 const unsigned char* prelocs,
2800 size_t reloc_count,
2801 Output_section* output_section,
2802 bool needs_special_offset_handling,
2803 size_t local_symbol_count,
2804 const unsigned char* plocal_symbols)
6d03d481
ST
2805{
2806
2807 if (sh_type == elfcpp::SHT_REL)
2808 {
2809 return;
2810 }
2811
fc51264f 2812 gold::gc_process_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
618d6666
L
2813 typename Target_x86_64<size>::Scan,
2814 typename Target_x86_64<size>::Relocatable_size_for_reloc>(
6d03d481
ST
2815 symtab,
2816 layout,
2817 this,
2818 object,
2819 data_shndx,
2820 prelocs,
2821 reloc_count,
2822 output_section,
2823 needs_special_offset_handling,
2824 local_symbol_count,
2825 plocal_symbols);
2826
2827}
2e30d253
ILT
2828// Scan relocations for a section.
2829
fc51264f 2830template<int size>
2e30d253 2831void
fc51264f
L
2832Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
2833 Layout* layout,
2834 Sized_relobj_file<size, false>* object,
2835 unsigned int data_shndx,
2836 unsigned int sh_type,
2837 const unsigned char* prelocs,
2838 size_t reloc_count,
2839 Output_section* output_section,
2840 bool needs_special_offset_handling,
2841 size_t local_symbol_count,
2842 const unsigned char* plocal_symbols)
2e30d253
ILT
2843{
2844 if (sh_type == elfcpp::SHT_REL)
2845 {
75f2446e
ILT
2846 gold_error(_("%s: unsupported REL reloc section"),
2847 object->name().c_str());
2848 return;
2e30d253
ILT
2849 }
2850
fc51264f 2851 gold::scan_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
618d6666 2852 typename Target_x86_64<size>::Scan>(
2e30d253
ILT
2853 symtab,
2854 layout,
2855 this,
2856 object,
2857 data_shndx,
2858 prelocs,
2859 reloc_count,
730cdc88
ILT
2860 output_section,
2861 needs_special_offset_handling,
2e30d253 2862 local_symbol_count,
730cdc88 2863 plocal_symbols);
2e30d253
ILT
2864}
2865
2866// Finalize the sections.
2867
fc51264f 2868template<int size>
2e30d253 2869void
fc51264f 2870Target_x86_64<size>::do_finalize_sections(
f59f41f3
DK
2871 Layout* layout,
2872 const Input_objects*,
e785ec03 2873 Symbol_table* symtab)
2e30d253 2874{
ea715a34
ILT
2875 const Reloc_section* rel_plt = (this->plt_ == NULL
2876 ? NULL
e291e7b9 2877 : this->plt_->rela_plt());
ea715a34 2878 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
612a8d3d 2879 this->rela_dyn_, true, false);
ea715a34 2880
2e30d253
ILT
2881 // Fill in some more dynamic tags.
2882 Output_data_dynamic* const odyn = layout->dynamic_data();
2883 if (odyn != NULL)
2884 {
22b127cc 2885 if (this->plt_ != NULL
ea715a34
ILT
2886 && this->plt_->output_section() != NULL
2887 && this->plt_->has_tlsdesc_entry())
2e30d253 2888 {
ea715a34
ILT
2889 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2890 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2891 this->got_->finalize_data_size();
2892 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2893 this->plt_, plt_offset);
2894 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2895 this->got_, got_offset);
2e30d253
ILT
2896 }
2897 }
2898
2899 // Emit any relocs we saved in an attempt to avoid generating COPY
2900 // relocs.
12c0daef
ILT
2901 if (this->copy_relocs_.any_saved_relocs())
2902 this->copy_relocs_.emit(this->rela_dyn_section(layout));
e785ec03
ILT
2903
2904 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2905 // the .got.plt section.
2906 Symbol* sym = this->global_offset_table_;
2907 if (sym != NULL)
2908 {
2909 uint64_t data_size = this->got_plt_->current_data_size();
fc51264f 2910 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
e785ec03 2911 }
28a13fec 2912
67181c72
ILT
2913 if (parameters->doing_static_link()
2914 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
28a13fec
ILT
2915 {
2916 // If linking statically, make sure that the __rela_iplt symbols
2917 // were defined if necessary, even if we didn't create a PLT.
2918 static const Define_symbol_in_segment syms[] =
2919 {
2920 {
2921 "__rela_iplt_start", // name
2922 elfcpp::PT_LOAD, // segment_type
2923 elfcpp::PF_W, // segment_flags_set
2924 elfcpp::PF(0), // segment_flags_clear
2925 0, // value
2926 0, // size
2927 elfcpp::STT_NOTYPE, // type
2928 elfcpp::STB_GLOBAL, // binding
2929 elfcpp::STV_HIDDEN, // visibility
2930 0, // nonvis
2931 Symbol::SEGMENT_START, // offset_from_base
2932 true // only_if_ref
2933 },
2934 {
2935 "__rela_iplt_end", // name
2936 elfcpp::PT_LOAD, // segment_type
2937 elfcpp::PF_W, // segment_flags_set
2938 elfcpp::PF(0), // segment_flags_clear
2939 0, // value
2940 0, // size
2941 elfcpp::STT_NOTYPE, // type
2942 elfcpp::STB_GLOBAL, // binding
2943 elfcpp::STV_HIDDEN, // visibility
2944 0, // nonvis
2945 Symbol::SEGMENT_START, // offset_from_base
2946 true // only_if_ref
2947 }
2948 };
2949
2950 symtab->define_symbols(layout, 2, syms,
2951 layout->script_options()->saw_sections_clause());
2952 }
2e30d253
ILT
2953}
2954
2955// Perform a relocation.
2956
fc51264f 2957template<int size>
2e30d253 2958inline bool
fc51264f
L
2959Target_x86_64<size>::Relocate::relocate(
2960 const Relocate_info<size, false>* relinfo,
2961 Target_x86_64<size>* target,
2962 Output_section*,
2963 size_t relnum,
2964 const elfcpp::Rela<size, false>& rela,
2965 unsigned int r_type,
2966 const Sized_symbol<size>* gsym,
2967 const Symbol_value<size>* psymval,
2968 unsigned char* view,
2969 typename elfcpp::Elf_types<size>::Elf_Addr address,
2970 section_size_type view_size)
2e30d253
ILT
2971{
2972 if (this->skip_call_tls_get_addr_)
2973 {
5efc7cd2
CC
2974 if ((r_type != elfcpp::R_X86_64_PLT32
2975 && r_type != elfcpp::R_X86_64_PC32)
2e30d253 2976 || gsym == NULL
0ffd9845 2977 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2e30d253 2978 {
75f2446e
ILT
2979 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2980 _("missing expected TLS relocation"));
2981 }
2982 else
2983 {
2984 this->skip_call_tls_get_addr_ = false;
2985 return false;
2e30d253 2986 }
2e30d253
ILT
2987 }
2988
fc51264f 2989 const Sized_relobj_file<size, false>* object = relinfo->object;
7223e9ca
ILT
2990
2991 // Pick the value to use for symbols defined in the PLT.
fc51264f 2992 Symbol_value<size> symval;
96f2030e 2993 if (gsym != NULL
95a2c8d6 2994 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2e30d253 2995 {
67181c72 2996 symval.set_output_value(target->plt_address_for_global(gsym)
2e30d253
ILT
2997 + gsym->plt_offset());
2998 psymval = &symval;
2999 }
7223e9ca
ILT
3000 else if (gsym == NULL && psymval->is_ifunc_symbol())
3001 {
fc51264f 3002 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7223e9ca
ILT
3003 if (object->local_has_plt_offset(r_sym))
3004 {
67181c72 3005 symval.set_output_value(target->plt_address_for_local(object, r_sym)
7223e9ca
ILT
3006 + object->local_plt_offset(r_sym));
3007 psymval = &symval;
3008 }
3009 }
2e30d253 3010
0ffd9845
ILT
3011 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3012
3013 // Get the GOT offset if needed.
96f2030e
ILT
3014 // The GOT pointer points to the end of the GOT section.
3015 // We need to subtract the size of the GOT section to get
3016 // the actual offset to use in the relocation.
0ffd9845
ILT
3017 bool have_got_offset = false;
3018 unsigned int got_offset = 0;
3019 switch (r_type)
3020 {
3021 case elfcpp::R_X86_64_GOT32:
3022 case elfcpp::R_X86_64_GOT64:
3023 case elfcpp::R_X86_64_GOTPLT64:
3024 case elfcpp::R_X86_64_GOTPCREL:
3025 case elfcpp::R_X86_64_GOTPCREL64:
3026 if (gsym != NULL)
3027 {
0a65a3a7
CC
3028 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3029 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
0ffd9845
ILT
3030 }
3031 else
3032 {
fc51264f 3033 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
0a65a3a7
CC
3034 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3035 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3036 - target->got_size());
0ffd9845
ILT
3037 }
3038 have_got_offset = true;
3039 break;
3040
3041 default:
3042 break;
3043 }
2e30d253
ILT
3044
3045 switch (r_type)
3046 {
3047 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
3048 case elfcpp::R_X86_64_GNU_VTINHERIT:
3049 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
3050 break;
3051
3052 case elfcpp::R_X86_64_64:
fc51264f 3053 Relocate_functions<size, false>::rela64(view, object, psymval, addend);
2e30d253
ILT
3054 break;
3055
3056 case elfcpp::R_X86_64_PC64:
fc51264f 3057 Relocate_functions<size, false>::pcrela64(view, object, psymval, addend,
2e30d253
ILT
3058 address);
3059 break;
3060
3061 case elfcpp::R_X86_64_32:
7bb3655e
ILT
3062 // FIXME: we need to verify that value + addend fits into 32 bits:
3063 // uint64_t x = value + addend;
3064 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3065 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
fc51264f 3066 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
2e30d253
ILT
3067 break;
3068
3069 case elfcpp::R_X86_64_32S:
7bb3655e
ILT
3070 // FIXME: we need to verify that value + addend fits into 32 bits:
3071 // int64_t x = value + addend; // note this quantity is signed!
3072 // x == static_cast<int64_t>(static_cast<int32_t>(x))
fc51264f 3073 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
2e30d253
ILT
3074 break;
3075
3076 case elfcpp::R_X86_64_PC32:
fc51264f
L
3077 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3078 address);
2e30d253
ILT
3079 break;
3080
3081 case elfcpp::R_X86_64_16:
fc51264f 3082 Relocate_functions<size, false>::rela16(view, object, psymval, addend);
2e30d253
ILT
3083 break;
3084
3085 case elfcpp::R_X86_64_PC16:
fc51264f
L
3086 Relocate_functions<size, false>::pcrela16(view, object, psymval, addend,
3087 address);
2e30d253
ILT
3088 break;
3089
3090 case elfcpp::R_X86_64_8:
fc51264f 3091 Relocate_functions<size, false>::rela8(view, object, psymval, addend);
2e30d253
ILT
3092 break;
3093
3094 case elfcpp::R_X86_64_PC8:
fc51264f
L
3095 Relocate_functions<size, false>::pcrela8(view, object, psymval, addend,
3096 address);
2e30d253
ILT
3097 break;
3098
3099 case elfcpp::R_X86_64_PLT32:
f389a824
ILT
3100 gold_assert(gsym == NULL
3101 || gsym->has_plt_offset()
99f8faca
ILT
3102 || gsym->final_value_is_known()
3103 || (gsym->is_defined()
3104 && !gsym->is_from_dynobj()
3105 && !gsym->is_preemptible()));
ee9e9e86
ILT
3106 // Note: while this code looks the same as for R_X86_64_PC32, it
3107 // behaves differently because psymval was set to point to
3108 // the PLT entry, rather than the symbol, in Scan::global().
fc51264f
L
3109 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3110 address);
2e30d253
ILT
3111 break;
3112
ee9e9e86
ILT
3113 case elfcpp::R_X86_64_PLTOFF64:
3114 {
3115 gold_assert(gsym);
3116 gold_assert(gsym->has_plt_offset()
3117 || gsym->final_value_is_known());
fc51264f 3118 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
ee9e9e86 3119 got_address = target->got_section(NULL, NULL)->address();
fc51264f
L
3120 Relocate_functions<size, false>::rela64(view, object, psymval,
3121 addend - got_address);
ee9e9e86
ILT
3122 }
3123
2e30d253 3124 case elfcpp::R_X86_64_GOT32:
0ffd9845 3125 gold_assert(have_got_offset);
fc51264f 3126 Relocate_functions<size, false>::rela32(view, got_offset, addend);
2e30d253
ILT
3127 break;
3128
e822f2b1
ILT
3129 case elfcpp::R_X86_64_GOTPC32:
3130 {
3131 gold_assert(gsym);
fc51264f 3132 typename elfcpp::Elf_types<size>::Elf_Addr value;
96f2030e 3133 value = target->got_plt_section()->address();
fc51264f 3134 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
e822f2b1
ILT
3135 }
3136 break;
3137
3138 case elfcpp::R_X86_64_GOT64:
3139 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
3140 // Since we always add a PLT entry, this is equivalent.
fdc2f80f 3141 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845 3142 gold_assert(have_got_offset);
fc51264f 3143 Relocate_functions<size, false>::rela64(view, got_offset, addend);
e822f2b1
ILT
3144 break;
3145
3146 case elfcpp::R_X86_64_GOTPC64:
3147 {
3148 gold_assert(gsym);
fc51264f 3149 typename elfcpp::Elf_types<size>::Elf_Addr value;
96f2030e 3150 value = target->got_plt_section()->address();
fc51264f 3151 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
e822f2b1
ILT
3152 }
3153 break;
3154
2e30d253
ILT
3155 case elfcpp::R_X86_64_GOTOFF64:
3156 {
fc51264f 3157 typename elfcpp::Elf_types<size>::Elf_Addr value;
2e30d253 3158 value = (psymval->value(object, 0)
96f2030e 3159 - target->got_plt_section()->address());
fc51264f 3160 Relocate_functions<size, false>::rela64(view, value, addend);
2e30d253
ILT
3161 }
3162 break;
3163
3164 case elfcpp::R_X86_64_GOTPCREL:
3165 {
0ffd9845 3166 gold_assert(have_got_offset);
fc51264f 3167 typename elfcpp::Elf_types<size>::Elf_Addr value;
96f2030e 3168 value = target->got_plt_section()->address() + got_offset;
fc51264f 3169 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
2e30d253
ILT
3170 }
3171 break;
3172
e822f2b1
ILT
3173 case elfcpp::R_X86_64_GOTPCREL64:
3174 {
0ffd9845 3175 gold_assert(have_got_offset);
fc51264f 3176 typename elfcpp::Elf_types<size>::Elf_Addr value;
96f2030e 3177 value = target->got_plt_section()->address() + got_offset;
fc51264f 3178 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
e822f2b1
ILT
3179 }
3180 break;
3181
2e30d253
ILT
3182 case elfcpp::R_X86_64_COPY:
3183 case elfcpp::R_X86_64_GLOB_DAT:
3184 case elfcpp::R_X86_64_JUMP_SLOT:
3185 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 3186 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 3187 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 3188 case elfcpp::R_X86_64_TPOFF64:
2e30d253 3189 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 3190 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
3191 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3192 _("unexpected reloc %u in object file"),
3193 r_type);
2e30d253
ILT
3194 break;
3195
d61c17ea 3196 // These are initial tls relocs, which are expected when linking
56622147
ILT
3197 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3198 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 3199 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 3200 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
3201 case elfcpp::R_X86_64_DTPOFF32:
3202 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
3203 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3204 case elfcpp::R_X86_64_TPOFF32: // Local-exec
7bf1f802
ILT
3205 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3206 view, address, view_size);
2e30d253 3207 break;
2e30d253 3208
fdc2f80f
ILT
3209 case elfcpp::R_X86_64_SIZE32:
3210 case elfcpp::R_X86_64_SIZE64:
2e30d253 3211 default:
75f2446e
ILT
3212 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3213 _("unsupported reloc %u"),
3214 r_type);
2e30d253
ILT
3215 break;
3216 }
3217
3218 return true;
3219}
3220
3221// Perform a TLS relocation.
3222
fc51264f 3223template<int size>
2e30d253 3224inline void
fc51264f
L
3225Target_x86_64<size>::Relocate::relocate_tls(
3226 const Relocate_info<size, false>* relinfo,
3227 Target_x86_64<size>* target,
3228 size_t relnum,
3229 const elfcpp::Rela<size, false>& rela,
3230 unsigned int r_type,
3231 const Sized_symbol<size>* gsym,
3232 const Symbol_value<size>* psymval,
3233 unsigned char* view,
3234 typename elfcpp::Elf_types<size>::Elf_Addr address,
3235 section_size_type view_size)
2e30d253 3236{
2e30d253 3237 Output_segment* tls_segment = relinfo->layout->tls_segment();
7bf1f802 3238
fc51264f 3239 const Sized_relobj_file<size, false>* object = relinfo->object;
6a41d30b 3240 const elfcpp::Elf_Xword addend = rela.get_r_addend();
fc51264f 3241 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
36171d64 3242 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2e30d253 3243
fc51264f 3244 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
2e30d253
ILT
3245
3246 const bool is_final = (gsym == NULL
b3705d2a 3247 ? !parameters->options().shared()
2e30d253 3248 : gsym->final_value_is_known());
36171d64 3249 tls::Tls_optimization optimized_type
fc51264f 3250 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
3251 switch (r_type)
3252 {
56622147 3253 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
36171d64
CC
3254 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3255 {
3256 // If this code sequence is used in a non-executable section,
3257 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3258 // on the assumption that it's being used by itself in a debug
3259 // section. Therefore, in the unlikely event that the code
3260 // sequence appears in a non-executable section, we simply
3261 // leave it unoptimized.
3262 optimized_type = tls::TLSOPT_NONE;
3263 }
e041f13d 3264 if (optimized_type == tls::TLSOPT_TO_LE)
2e30d253 3265 {
62855347
ILT
3266 if (tls_segment == NULL)
3267 {
191f1a2d
ILT
3268 gold_assert(parameters->errors()->error_count() > 0
3269 || issue_undefined_symbol_error(gsym));
62855347
ILT
3270 return;
3271 }
2e30d253 3272 this->tls_gd_to_le(relinfo, relnum, tls_segment,
72ec2876 3273 rela, r_type, value, view,
2e30d253
ILT
3274 view_size);
3275 break;
3276 }
7bf1f802
ILT
3277 else
3278 {
c2b45e22
CC
3279 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3280 ? GOT_TYPE_TLS_OFFSET
3281 : GOT_TYPE_TLS_PAIR);
7bf1f802
ILT
3282 unsigned int got_offset;
3283 if (gsym != NULL)
3284 {
c2b45e22
CC
3285 gold_assert(gsym->has_got_offset(got_type));
3286 got_offset = gsym->got_offset(got_type) - target->got_size();
7bf1f802
ILT
3287 }
3288 else
3289 {
fc51264f 3290 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
c2b45e22
CC
3291 gold_assert(object->local_has_got_offset(r_sym, got_type));
3292 got_offset = (object->local_got_offset(r_sym, got_type)
7bf1f802
ILT
3293 - target->got_size());
3294 }
3295 if (optimized_type == tls::TLSOPT_TO_IE)
3296 {
c2b45e22 3297 value = target->got_plt_section()->address() + got_offset;
7bf1f802 3298 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
c2b45e22 3299 value, view, address, view_size);
7bf1f802
ILT
3300 break;
3301 }
3302 else if (optimized_type == tls::TLSOPT_NONE)
3303 {
3304 // Relocate the field with the offset of the pair of GOT
3305 // entries.
6a41d30b 3306 value = target->got_plt_section()->address() + got_offset;
fc51264f
L
3307 Relocate_functions<size, false>::pcrela32(view, value, addend,
3308 address);
7bf1f802
ILT
3309 break;
3310 }
3311 }
72ec2876 3312 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 3313 _("unsupported reloc %u"), r_type);
2e30d253
ILT
3314 break;
3315
c2b45e22
CC
3316 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3317 case elfcpp::R_X86_64_TLSDESC_CALL:
36171d64
CC
3318 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3319 {
3320 // See above comment for R_X86_64_TLSGD.
3321 optimized_type = tls::TLSOPT_NONE;
3322 }
c2b45e22
CC
3323 if (optimized_type == tls::TLSOPT_TO_LE)
3324 {
62855347
ILT
3325 if (tls_segment == NULL)
3326 {
191f1a2d
ILT
3327 gold_assert(parameters->errors()->error_count() > 0
3328 || issue_undefined_symbol_error(gsym));
62855347
ILT
3329 return;
3330 }
c2b45e22
CC
3331 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3332 rela, r_type, value, view,
3333 view_size);
3334 break;
3335 }
3336 else
3337 {
3338 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3339 ? GOT_TYPE_TLS_OFFSET
3340 : GOT_TYPE_TLS_DESC);
a8df5856
ILT
3341 unsigned int got_offset = 0;
3342 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3343 && optimized_type == tls::TLSOPT_NONE)
3344 {
3345 // We created GOT entries in the .got.tlsdesc portion of
3346 // the .got.plt section, but the offset stored in the
3347 // symbol is the offset within .got.tlsdesc.
3348 got_offset = (target->got_size()
3349 + target->got_plt_section()->data_size());
3350 }
c2b45e22
CC
3351 if (gsym != NULL)
3352 {
3353 gold_assert(gsym->has_got_offset(got_type));
a8df5856 3354 got_offset += gsym->got_offset(got_type) - target->got_size();
c2b45e22
CC
3355 }
3356 else
3357 {
fc51264f 3358 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
c2b45e22 3359 gold_assert(object->local_has_got_offset(r_sym, got_type));
a8df5856
ILT
3360 got_offset += (object->local_got_offset(r_sym, got_type)
3361 - target->got_size());
c2b45e22
CC
3362 }
3363 if (optimized_type == tls::TLSOPT_TO_IE)
3364 {
62855347
ILT
3365 if (tls_segment == NULL)
3366 {
191f1a2d
ILT
3367 gold_assert(parameters->errors()->error_count() > 0
3368 || issue_undefined_symbol_error(gsym));
62855347
ILT
3369 return;
3370 }
c2b45e22
CC
3371 value = target->got_plt_section()->address() + got_offset;
3372 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
3373 rela, r_type, value, view, address,
3374 view_size);
3375 break;
3376 }
3377 else if (optimized_type == tls::TLSOPT_NONE)
3378 {
3379 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3380 {
3381 // Relocate the field with the offset of the pair of GOT
3382 // entries.
3383 value = target->got_plt_section()->address() + got_offset;
fc51264f
L
3384 Relocate_functions<size, false>::pcrela32(view, value, addend,
3385 address);
c2b45e22
CC
3386 }
3387 break;
3388 }
3389 }
3390 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3391 _("unsupported reloc %u"), r_type);
3392 break;
3393
56622147 3394 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
36171d64
CC
3395 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3396 {
3397 // See above comment for R_X86_64_TLSGD.
3398 optimized_type = tls::TLSOPT_NONE;
3399 }
e041f13d
ILT
3400 if (optimized_type == tls::TLSOPT_TO_LE)
3401 {
62855347
ILT
3402 if (tls_segment == NULL)
3403 {
191f1a2d
ILT
3404 gold_assert(parameters->errors()->error_count() > 0
3405 || issue_undefined_symbol_error(gsym));
62855347
ILT
3406 return;
3407 }
72ec2876
ILT
3408 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3409 value, view, view_size);
3410 break;
e041f13d 3411 }
7bf1f802
ILT
3412 else if (optimized_type == tls::TLSOPT_NONE)
3413 {
3414 // Relocate the field with the offset of the GOT entry for
3415 // the module index.
3416 unsigned int got_offset;
31d60480
ILT
3417 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3418 - target->got_size());
6a41d30b 3419 value = target->got_plt_section()->address() + got_offset;
fc51264f
L
3420 Relocate_functions<size, false>::pcrela32(view, value, addend,
3421 address);
7bf1f802
ILT
3422 break;
3423 }
72ec2876 3424 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 3425 _("unsupported reloc %u"), r_type);
2e30d253 3426 break;
0ffd9845
ILT
3427
3428 case elfcpp::R_X86_64_DTPOFF32:
36171d64
CC
3429 // This relocation type is used in debugging information.
3430 // In that case we need to not optimize the value. If the
3431 // section is not executable, then we assume we should not
3432 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3433 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3434 // R_X86_64_TLSLD.
3435 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3436 {
62855347
ILT
3437 if (tls_segment == NULL)
3438 {
191f1a2d
ILT
3439 gold_assert(parameters->errors()->error_count() > 0
3440 || issue_undefined_symbol_error(gsym));
62855347
ILT
3441 return;
3442 }
36171d64
CC
3443 value -= tls_segment->memsz();
3444 }
fc51264f 3445 Relocate_functions<size, false>::rela32(view, value, addend);
0ffd9845
ILT
3446 break;
3447
3448 case elfcpp::R_X86_64_DTPOFF64:
36171d64
CC
3449 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3450 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3451 {
62855347
ILT
3452 if (tls_segment == NULL)
3453 {
191f1a2d
ILT
3454 gold_assert(parameters->errors()->error_count() > 0
3455 || issue_undefined_symbol_error(gsym));
62855347
ILT
3456 return;
3457 }
36171d64
CC
3458 value -= tls_segment->memsz();
3459 }
fc51264f 3460 Relocate_functions<size, false>::rela64(view, value, addend);
0ffd9845 3461 break;
2e30d253 3462
56622147
ILT
3463 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3464 if (optimized_type == tls::TLSOPT_TO_LE)
3465 {
62855347
ILT
3466 if (tls_segment == NULL)
3467 {
191f1a2d
ILT
3468 gold_assert(parameters->errors()->error_count() > 0
3469 || issue_undefined_symbol_error(gsym));
62855347
ILT
3470 return;
3471 }
fc51264f
L
3472 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3473 tls_segment, rela,
3474 r_type, value, view,
3475 view_size);
56622147
ILT
3476 break;
3477 }
7bf1f802
ILT
3478 else if (optimized_type == tls::TLSOPT_NONE)
3479 {
3480 // Relocate the field with the offset of the GOT entry for
3481 // the tp-relative offset of the symbol.
3482 unsigned int got_offset;
3483 if (gsym != NULL)
3484 {
0a65a3a7
CC
3485 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3486 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3487 - target->got_size());
7bf1f802
ILT
3488 }
3489 else
3490 {
fc51264f 3491 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
0a65a3a7
CC
3492 gold_assert(object->local_has_got_offset(r_sym,
3493 GOT_TYPE_TLS_OFFSET));
3494 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
7bf1f802
ILT
3495 - target->got_size());
3496 }
6a41d30b 3497 value = target->got_plt_section()->address() + got_offset;
fc51264f
L
3498 Relocate_functions<size, false>::pcrela32(view, value, addend,
3499 address);
7bf1f802
ILT
3500 break;
3501 }
56622147
ILT
3502 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3503 _("unsupported reloc type %u"),
3504 r_type);
3505 break;
0ffd9845 3506
56622147 3507 case elfcpp::R_X86_64_TPOFF32: // Local-exec
62855347
ILT
3508 if (tls_segment == NULL)
3509 {
191f1a2d
ILT
3510 gold_assert(parameters->errors()->error_count() > 0
3511 || issue_undefined_symbol_error(gsym));
62855347
ILT
3512 return;
3513 }
6a41d30b 3514 value -= tls_segment->memsz();
fc51264f 3515 Relocate_functions<size, false>::rela32(view, value, addend);
56622147 3516 break;
2e30d253 3517 }
2e30d253
ILT
3518}
3519
7bf1f802
ILT
3520// Do a relocation in which we convert a TLS General-Dynamic to an
3521// Initial-Exec.
3522
fc51264f 3523template<int size>
7bf1f802 3524inline void
fc51264f
L
3525Target_x86_64<size>::Relocate::tls_gd_to_ie(
3526 const Relocate_info<size, false>* relinfo,
3527 size_t relnum,
3528 Output_segment*,
3529 const elfcpp::Rela<size, false>& rela,
3530 unsigned int,
3531 typename elfcpp::Elf_types<size>::Elf_Addr value,
3532 unsigned char* view,
3533 typename elfcpp::Elf_types<size>::Elf_Addr address,
3534 section_size_type view_size)
7bf1f802
ILT
3535{
3536 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3537 // .word 0x6666; rex64; call __tls_get_addr
3538 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3539
3540 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3541 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3542
3543 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3544 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3545 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3546 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3547
3548 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3549
c2b45e22 3550 const elfcpp::Elf_Xword addend = rela.get_r_addend();
fc51264f
L
3551 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
3552 address);
7bf1f802
ILT
3553
3554 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3555 // We can skip it.
3556 this->skip_call_tls_get_addr_ = true;
3557}
3558
e041f13d 3559// Do a relocation in which we convert a TLS General-Dynamic to a
2e30d253
ILT
3560// Local-Exec.
3561
fc51264f 3562template<int size>
2e30d253 3563inline void
fc51264f
L
3564Target_x86_64<size>::Relocate::tls_gd_to_le(
3565 const Relocate_info<size, false>* relinfo,
3566 size_t relnum,
3567 Output_segment* tls_segment,
3568 const elfcpp::Rela<size, false>& rela,
3569 unsigned int,
3570 typename elfcpp::Elf_types<size>::Elf_Addr value,
3571 unsigned char* view,
3572 section_size_type view_size)
2e30d253 3573{
0ffd9845
ILT
3574 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3575 // .word 0x6666; rex64; call __tls_get_addr
3576 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2e30d253 3577
72ec2876
ILT
3578 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3579 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2e30d253 3580
72ec2876
ILT
3581 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3582 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3583 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3584 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2e30d253 3585
0ffd9845 3586 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2e30d253 3587
6a41d30b 3588 value -= tls_segment->memsz();
fc51264f 3589 Relocate_functions<size, false>::rela32(view + 8, value, 0);
2e30d253
ILT
3590
3591 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3592 // We can skip it.
3593 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
3594}
3595
c2b45e22
CC
3596// Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3597
fc51264f 3598template<int size>
c2b45e22 3599inline void
fc51264f
L
3600Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
3601 const Relocate_info<size, false>* relinfo,
c2b45e22
CC
3602 size_t relnum,
3603 Output_segment*,
fc51264f 3604 const elfcpp::Rela<size, false>& rela,
c2b45e22 3605 unsigned int r_type,
fc51264f 3606 typename elfcpp::Elf_types<size>::Elf_Addr value,
c2b45e22 3607 unsigned char* view,
fc51264f 3608 typename elfcpp::Elf_types<size>::Elf_Addr address,
c2b45e22
CC
3609 section_size_type view_size)
3610{
3611 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3612 {
3613 // leaq foo@tlsdesc(%rip), %rax
3614 // ==> movq foo@gottpoff(%rip), %rax
3615 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3616 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3617 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3618 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3619 view[-2] = 0x8b;
3620 const elfcpp::Elf_Xword addend = rela.get_r_addend();
fc51264f 3621 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
c2b45e22
CC
3622 }
3623 else
3624 {
3625 // call *foo@tlscall(%rax)
3626 // ==> nop; nop
3627 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3628 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3629 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3630 view[0] == 0xff && view[1] == 0x10);
3631 view[0] = 0x66;
3632 view[1] = 0x90;
3633 }
3634}
3635
3636// Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3637
fc51264f 3638template<int size>
c2b45e22 3639inline void
fc51264f
L
3640Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
3641 const Relocate_info<size, false>* relinfo,
c2b45e22
CC
3642 size_t relnum,
3643 Output_segment* tls_segment,
fc51264f 3644 const elfcpp::Rela<size, false>& rela,
c2b45e22 3645 unsigned int r_type,
fc51264f 3646 typename elfcpp::Elf_types<size>::Elf_Addr value,
c2b45e22
CC
3647 unsigned char* view,
3648 section_size_type view_size)
3649{
3650 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3651 {
3652 // leaq foo@tlsdesc(%rip), %rax
3653 // ==> movq foo@tpoff, %rax
3654 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3655 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3656 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3657 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3658 view[-2] = 0xc7;
3659 view[-1] = 0xc0;
3660 value -= tls_segment->memsz();
fc51264f 3661 Relocate_functions<size, false>::rela32(view, value, 0);
c2b45e22
CC
3662 }
3663 else
3664 {
3665 // call *foo@tlscall(%rax)
3666 // ==> nop; nop
3667 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3668 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3669 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3670 view[0] == 0xff && view[1] == 0x10);
3671 view[0] = 0x66;
3672 view[1] = 0x90;
3673 }
3674}
3675
fc51264f 3676template<int size>
2e30d253 3677inline void
fc51264f
L
3678Target_x86_64<size>::Relocate::tls_ld_to_le(
3679 const Relocate_info<size, false>* relinfo,
3680 size_t relnum,
3681 Output_segment*,
3682 const elfcpp::Rela<size, false>& rela,
3683 unsigned int,
3684 typename elfcpp::Elf_types<size>::Elf_Addr,
3685 unsigned char* view,
3686 section_size_type view_size)
2e30d253 3687{
72ec2876
ILT
3688 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3689 // ... leq foo@dtpoff(%rax),%reg
3690 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2e30d253 3691
72ec2876
ILT
3692 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3693 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2e30d253 3694
72ec2876
ILT
3695 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3696 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3697
3698 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3699
3700 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3701
3702 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3703 // We can skip it.
3704 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
3705}
3706
56622147
ILT
3707// Do a relocation in which we convert a TLS Initial-Exec to a
3708// Local-Exec.
3709
fc51264f 3710template<int size>
56622147 3711inline void
fc51264f
L
3712Target_x86_64<size>::Relocate::tls_ie_to_le(
3713 const Relocate_info<size, false>* relinfo,
3714 size_t relnum,
3715 Output_segment* tls_segment,
3716 const elfcpp::Rela<size, false>& rela,
3717 unsigned int,
3718 typename elfcpp::Elf_types<size>::Elf_Addr value,
3719 unsigned char* view,
3720 section_size_type view_size)
56622147
ILT
3721{
3722 // We need to examine the opcodes to figure out which instruction we
3723 // are looking at.
3724
3725 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3726 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3727
3728 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3729 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3730
3731 unsigned char op1 = view[-3];
3732 unsigned char op2 = view[-2];
3733 unsigned char op3 = view[-1];
3734 unsigned char reg = op3 >> 3;
3735
3736 if (op2 == 0x8b)
3737 {
3738 // movq
3739 if (op1 == 0x4c)
3740 view[-3] = 0x49;
3741 view[-2] = 0xc7;
3742 view[-1] = 0xc0 | reg;
3743 }
3744 else if (reg == 4)
3745 {
3746 // Special handling for %rsp.
3747 if (op1 == 0x4c)
3748 view[-3] = 0x49;
3749 view[-2] = 0x81;
3750 view[-1] = 0xc0 | reg;
3751 }
3752 else
3753 {
3754 // addq
3755 if (op1 == 0x4c)
3756 view[-3] = 0x4d;
3757 view[-2] = 0x8d;
3758 view[-1] = 0x80 | reg | (reg << 3);
3759 }
3760
6a41d30b 3761 value -= tls_segment->memsz();
fc51264f 3762 Relocate_functions<size, false>::rela32(view, value, 0);
56622147
ILT
3763}
3764
2e30d253
ILT
3765// Relocate section data.
3766
fc51264f 3767template<int size>
2e30d253 3768void
fc51264f
L
3769Target_x86_64<size>::relocate_section(
3770 const Relocate_info<size, false>* relinfo,
364c7fa5
ILT
3771 unsigned int sh_type,
3772 const unsigned char* prelocs,
3773 size_t reloc_count,
3774 Output_section* output_section,
3775 bool needs_special_offset_handling,
3776 unsigned char* view,
fc51264f 3777 typename elfcpp::Elf_types<size>::Elf_Addr address,
364c7fa5
ILT
3778 section_size_type view_size,
3779 const Reloc_symbol_changes* reloc_symbol_changes)
2e30d253
ILT
3780{
3781 gold_assert(sh_type == elfcpp::SHT_RELA);
3782
fc51264f 3783 gold::relocate_section<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
618d6666 3784 typename Target_x86_64<size>::Relocate>(
2e30d253
ILT
3785 relinfo,
3786 this,
3787 prelocs,
3788 reloc_count,
730cdc88
ILT
3789 output_section,
3790 needs_special_offset_handling,
2e30d253
ILT
3791 view,
3792 address,
364c7fa5
ILT
3793 view_size,
3794 reloc_symbol_changes);
2e30d253
ILT
3795}
3796
94a3fc8b
CC
3797// Apply an incremental relocation. Incremental relocations always refer
3798// to global symbols.
3799
fc51264f 3800template<int size>
94a3fc8b 3801void
fc51264f
L
3802Target_x86_64<size>::apply_relocation(
3803 const Relocate_info<size, false>* relinfo,
3804 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
94a3fc8b 3805 unsigned int r_type,
fc51264f 3806 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
94a3fc8b
CC
3807 const Symbol* gsym,
3808 unsigned char* view,
fc51264f 3809 typename elfcpp::Elf_types<size>::Elf_Addr address,
94a3fc8b
CC
3810 section_size_type view_size)
3811{
fc51264f 3812 gold::apply_relocation<size, false, Target_x86_64<size>,
618d6666 3813 typename Target_x86_64<size>::Relocate>(
94a3fc8b
CC
3814 relinfo,
3815 this,
3816 r_offset,
3817 r_type,
3818 r_addend,
3819 gsym,
3820 view,
3821 address,
3822 view_size);
3823}
3824
6a74a719
ILT
3825// Return the size of a relocation while scanning during a relocatable
3826// link.
3827
fc51264f 3828template<int size>
6a74a719 3829unsigned int
fc51264f 3830Target_x86_64<size>::Relocatable_size_for_reloc::get_size_for_reloc(
6a74a719
ILT
3831 unsigned int r_type,
3832 Relobj* object)
3833{
3834 switch (r_type)
3835 {
3836 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
3837 case elfcpp::R_X86_64_GNU_VTINHERIT:
3838 case elfcpp::R_X86_64_GNU_VTENTRY:
6a74a719
ILT
3839 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3840 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3841 case elfcpp::R_X86_64_TLSDESC_CALL:
3842 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3843 case elfcpp::R_X86_64_DTPOFF32:
3844 case elfcpp::R_X86_64_DTPOFF64:
3845 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3846 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3847 return 0;
3848
3849 case elfcpp::R_X86_64_64:
3850 case elfcpp::R_X86_64_PC64:
3851 case elfcpp::R_X86_64_GOTOFF64:
3852 case elfcpp::R_X86_64_GOTPC64:
3853 case elfcpp::R_X86_64_PLTOFF64:
3854 case elfcpp::R_X86_64_GOT64:
3855 case elfcpp::R_X86_64_GOTPCREL64:
3856 case elfcpp::R_X86_64_GOTPCREL:
3857 case elfcpp::R_X86_64_GOTPLT64:
3858 return 8;
3859
3860 case elfcpp::R_X86_64_32:
3861 case elfcpp::R_X86_64_32S:
3862 case elfcpp::R_X86_64_PC32:
3863 case elfcpp::R_X86_64_PLT32:
3864 case elfcpp::R_X86_64_GOTPC32:
3865 case elfcpp::R_X86_64_GOT32:
3866 return 4;
3867
3868 case elfcpp::R_X86_64_16:
3869 case elfcpp::R_X86_64_PC16:
3870 return 2;
3871
3872 case elfcpp::R_X86_64_8:
3873 case elfcpp::R_X86_64_PC8:
3874 return 1;
3875
3876 case elfcpp::R_X86_64_COPY:
3877 case elfcpp::R_X86_64_GLOB_DAT:
3878 case elfcpp::R_X86_64_JUMP_SLOT:
3879 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 3880 case elfcpp::R_X86_64_IRELATIVE:
6a74a719
ILT
3881 // These are outstanding tls relocs, which are unexpected when linking
3882 case elfcpp::R_X86_64_TPOFF64:
3883 case elfcpp::R_X86_64_DTPMOD64:
3884 case elfcpp::R_X86_64_TLSDESC:
3885 object->error(_("unexpected reloc %u in object file"), r_type);
3886 return 0;
3887
3888 case elfcpp::R_X86_64_SIZE32:
3889 case elfcpp::R_X86_64_SIZE64:
3890 default:
3891 object->error(_("unsupported reloc %u against local symbol"), r_type);
3892 return 0;
3893 }
3894}
3895
3896// Scan the relocs during a relocatable link.
3897
fc51264f 3898template<int size>
6a74a719 3899void
fc51264f
L
3900Target_x86_64<size>::scan_relocatable_relocs(
3901 Symbol_table* symtab,
3902 Layout* layout,
3903 Sized_relobj_file<size, false>* object,
3904 unsigned int data_shndx,
3905 unsigned int sh_type,
3906 const unsigned char* prelocs,
3907 size_t reloc_count,
3908 Output_section* output_section,
3909 bool needs_special_offset_handling,
3910 size_t local_symbol_count,
3911 const unsigned char* plocal_symbols,
3912 Relocatable_relocs* rr)
6a74a719
ILT
3913{
3914 gold_assert(sh_type == elfcpp::SHT_RELA);
3915
3916 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3917 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3918
fc51264f 3919 gold::scan_relocatable_relocs<size, false, elfcpp::SHT_RELA,
6a74a719 3920 Scan_relocatable_relocs>(
6a74a719
ILT
3921 symtab,
3922 layout,
3923 object,
3924 data_shndx,
3925 prelocs,
3926 reloc_count,
3927 output_section,
3928 needs_special_offset_handling,
3929 local_symbol_count,
3930 plocal_symbols,
3931 rr);
3932}
3933
3934// Relocate a section during a relocatable link.
3935
fc51264f 3936template<int size>
6a74a719 3937void
fc51264f
L
3938Target_x86_64<size>::relocate_for_relocatable(
3939 const Relocate_info<size, false>* relinfo,
6a74a719
ILT
3940 unsigned int sh_type,
3941 const unsigned char* prelocs,
3942 size_t reloc_count,
3943 Output_section* output_section,
3944 off_t offset_in_output_section,
3945 const Relocatable_relocs* rr,
3946 unsigned char* view,
fc51264f 3947 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
6a74a719
ILT
3948 section_size_type view_size,
3949 unsigned char* reloc_view,
3950 section_size_type reloc_view_size)
3951{
3952 gold_assert(sh_type == elfcpp::SHT_RELA);
3953
fc51264f 3954 gold::relocate_for_relocatable<size, false, elfcpp::SHT_RELA>(
6a74a719
ILT
3955 relinfo,
3956 prelocs,
3957 reloc_count,
3958 output_section,
3959 offset_in_output_section,
3960 rr,
3961 view,
3962 view_address,
3963 view_size,
3964 reloc_view,
3965 reloc_view_size);
3966}
3967
4fb6c25d
ILT
3968// Return the value to use for a dynamic which requires special
3969// treatment. This is how we support equality comparisons of function
3970// pointers across shared library boundaries, as described in the
3971// processor specific ABI supplement.
3972
fc51264f 3973template<int size>
4fb6c25d 3974uint64_t
fc51264f 3975Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
4fb6c25d
ILT
3976{
3977 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
67181c72 3978 return this->plt_address_for_global(gsym) + gsym->plt_offset();
4fb6c25d
ILT
3979}
3980
2e30d253
ILT
3981// Return a string used to fill a code section with nops to take up
3982// the specified length.
3983
fc51264f 3984template<int size>
2e30d253 3985std::string
fc51264f 3986Target_x86_64<size>::do_code_fill(section_size_type length) const
2e30d253
ILT
3987{
3988 if (length >= 16)
3989 {
3990 // Build a jmpq instruction to skip over the bytes.
3991 unsigned char jmp[5];
3992 jmp[0] = 0xe9;
04bf7072 3993 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2e30d253 3994 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
9dee3b3c 3995 + std::string(length - 5, static_cast<char>(0x90)));
2e30d253
ILT
3996 }
3997
3998 // Nop sequences of various lengths.
76677ad0
CC
3999 const char nop1[1] = { '\x90' }; // nop
4000 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
4001 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4002 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4003 '\x00'};
4004 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4005 '\x00', '\x00' };
4006 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4007 '\x44', '\x00', '\x00' };
4008 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4009 '\x00', '\x00', '\x00',
4010 '\x00' };
4011 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4012 '\x00', '\x00', '\x00',
4013 '\x00', '\x00' };
4014 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4015 '\x84', '\x00', '\x00',
4016 '\x00', '\x00', '\x00' };
4017 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4018 '\x1f', '\x84', '\x00',
4019 '\x00', '\x00', '\x00',
4020 '\x00' };
4021 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
4022 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4023 '\x00', '\x00', '\x00',
4024 '\x00', '\x00' };
4025 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
4026 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4027 '\x84', '\x00', '\x00',
4028 '\x00', '\x00', '\x00' };
4029 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4030 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4031 '\x1f', '\x84', '\x00',
4032 '\x00', '\x00', '\x00',
4033 '\x00' };
4034 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4035 '\x66', '\x66', '\x2e', // data16
4036 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4037 '\x00', '\x00', '\x00',
4038 '\x00', '\x00' };
4039 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4040 '\x66', '\x66', '\x66', // data16; data16
4041 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4042 '\x84', '\x00', '\x00',
4043 '\x00', '\x00', '\x00' };
2e30d253
ILT
4044
4045 const char* nops[16] = {
4046 NULL,
4047 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
4048 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
4049 };
4050
4051 return std::string(nops[length], length);
4052}
4053
e291e7b9
ILT
4054// Return the addend to use for a target specific relocation. The
4055// only target specific relocation is R_X86_64_TLSDESC for a local
4056// symbol. We want to set the addend is the offset of the local
4057// symbol in the TLS segment.
4058
fc51264f 4059template<int size>
e291e7b9 4060uint64_t
fc51264f
L
4061Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
4062 uint64_t) const
e291e7b9
ILT
4063{
4064 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
4065 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4066 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4067 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
fc51264f 4068 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
e291e7b9
ILT
4069 gold_assert(psymval->is_tls_symbol());
4070 // The value of a TLS symbol is the offset in the TLS segment.
4071 return psymval->value(ti.object, 0);
4072}
4073
02d7cd44
ILT
4074// Return the value to use for the base of a DW_EH_PE_datarel offset
4075// in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4076// assembler can not write out the difference between two labels in
4077// different sections, so instead of using a pc-relative value they
4078// use an offset from the GOT.
4079
fc51264f 4080template<int size>
02d7cd44 4081uint64_t
fc51264f 4082Target_x86_64<size>::do_ehframe_datarel_base() const
02d7cd44
ILT
4083{
4084 gold_assert(this->global_offset_table_ != NULL);
4085 Symbol* sym = this->global_offset_table_;
fc51264f 4086 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
02d7cd44
ILT
4087 return ssym->value();
4088}
4089
364c7fa5 4090// FNOFFSET in section SHNDX in OBJECT is the start of a function
9b547ce6 4091// compiled with -fsplit-stack. The function calls non-split-stack
364c7fa5
ILT
4092// code. We have to change the function so that it always ensures
4093// that it has enough stack space to run some random function.
4094
fc51264f 4095template<int size>
364c7fa5 4096void
fc51264f
L
4097Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
4098 section_offset_type fnoffset,
4099 section_size_type fnsize,
4100 unsigned char* view,
4101 section_size_type view_size,
4102 std::string* from,
4103 std::string* to) const
364c7fa5
ILT
4104{
4105 // The function starts with a comparison of the stack pointer and a
4106 // field in the TCB. This is followed by a jump.
4107
4108 // cmp %fs:NN,%rsp
4109 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
4110 && fnsize > 9)
4111 {
4112 // We will call __morestack if the carry flag is set after this
4113 // comparison. We turn the comparison into an stc instruction
4114 // and some nops.
4115 view[fnoffset] = '\xf9';
4116 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
4117 }
4118 // lea NN(%rsp),%r10
cbc999b9
ILT
4119 // lea NN(%rsp),%r11
4120 else if ((this->match_view(view, view_size, fnoffset,
4121 "\x4c\x8d\x94\x24", 4)
4122 || this->match_view(view, view_size, fnoffset,
4123 "\x4c\x8d\x9c\x24", 4))
364c7fa5
ILT
4124 && fnsize > 8)
4125 {
4126 // This is loading an offset from the stack pointer for a
4127 // comparison. The offset is negative, so we decrease the
4128 // offset by the amount of space we need for the stack. This
4129 // means we will avoid calling __morestack if there happens to
4130 // be plenty of space on the stack already.
4131 unsigned char* pval = view + fnoffset + 4;
4132 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
4133 val -= parameters->options().split_stack_adjust_size();
4134 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
4135 }
4136 else
4137 {
4138 if (!object->has_no_split_stack())
4139 object->error(_("failed to match split-stack sequence at "
4140 "section %u offset %0zx"),
ac33a407 4141 shndx, static_cast<size_t>(fnoffset));
364c7fa5
ILT
4142 return;
4143 }
4144
4145 // We have to change the function so that it calls
4146 // __morestack_non_split instead of __morestack. The former will
4147 // allocate additional stack space.
4148 *from = "__morestack";
4149 *to = "__morestack_non_split";
4150}
4151
2e30d253
ILT
4152// The selector for x86_64 object files.
4153
fc51264f 4154template<int size>
36959681 4155class Target_selector_x86_64 : public Target_selector_freebsd
2e30d253
ILT
4156{
4157public:
4158 Target_selector_x86_64()
fc51264f
L
4159 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
4160 (size == 64
4161 ? "elf64-x86-64" : "elf32-x86-64"),
4162 (size == 64
4163 ? "elf64-x86-64-freebsd"
4164 : "elf32-x86-64-freebsd"),
4165 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
2e30d253
ILT
4166 { }
4167
4168 Target*
e96caa79 4169 do_instantiate_target()
fc51264f 4170 { return new Target_x86_64<size>(); }
36959681 4171
2e30d253
ILT
4172};
4173
fc51264f
L
4174Target_selector_x86_64<64> target_selector_x86_64;
4175Target_selector_x86_64<32> target_selector_x32;
2e30d253
ILT
4176
4177} // End anonymous namespace.
This page took 0.440014 seconds and 4 git commands to generate.