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