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