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