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