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