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