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