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