Fix incorrect x32 overflow checking for refs to weak undef symbols.
[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{
2e702c99 1825 return this->plt_->first_plt_entry_offset();
0e70b911
CC
1826}
1827
1828// Return the size of each PLT entry.
1829
fc51264f 1830template<int size>
0e70b911 1831unsigned int
fc51264f 1832Target_x86_64<size>::plt_entry_size() const
0e70b911 1833{
2e702c99 1834 return this->plt_->get_plt_entry_size();
0e70b911
CC
1835}
1836
4829d394
CC
1837// Create the GOT and PLT sections for an incremental update.
1838
fc51264f 1839template<int size>
dd74ae06 1840Output_data_got_base*
fc51264f 1841Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
4829d394
CC
1842 Layout* layout,
1843 unsigned int got_count,
1844 unsigned int plt_count)
1845{
1846 gold_assert(this->got_ == NULL);
1847
1848 this->got_ = new Output_data_got<64, false>(got_count * 8);
1849 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1850 (elfcpp::SHF_ALLOC
1851 | elfcpp::SHF_WRITE),
1852 this->got_, ORDER_RELRO_LAST,
1853 true);
1854
1855 // Add the three reserved entries.
57b2284c 1856 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
4829d394
CC
1857 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1858 (elfcpp::SHF_ALLOC
1859 | elfcpp::SHF_WRITE),
1860 this->got_plt_, ORDER_NON_RELRO_FIRST,
1861 false);
1862
1863 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1864 this->global_offset_table_ =
1865 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1866 Symbol_table::PREDEFINED,
1867 this->got_plt_,
1868 0, 0, elfcpp::STT_OBJECT,
1869 elfcpp::STB_LOCAL,
1870 elfcpp::STV_HIDDEN, 0,
1871 false, false);
1872
1873 // If there are any TLSDESC relocations, they get GOT entries in
1874 // .got.plt after the jump slot entries.
1875 // FIXME: Get the count for TLSDESC entries.
1876 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1877 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1878 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1879 this->got_tlsdesc_,
1880 ORDER_NON_RELRO_FIRST, false);
1881
67181c72
ILT
1882 // If there are any IRELATIVE relocations, they get GOT entries in
1883 // .got.plt after the jump slot and TLSDESC entries.
1884 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1885 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1886 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1887 this->got_irelative_,
1888 ORDER_NON_RELRO_FIRST, false);
1889
4829d394 1890 // Create the PLT section.
2e702c99
RM
1891 this->plt_ = this->make_data_plt(layout, this->got_,
1892 this->got_plt_,
1893 this->got_irelative_,
1894 plt_count);
1895
1896 // Add unwind information if requested.
1897 if (parameters->options().ld_generated_unwind_info())
1898 this->plt_->add_eh_frame(layout);
1899
4829d394
CC
1900 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1901 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1902 this->plt_, ORDER_PLT, false);
1903
1904 // Make the sh_info field of .rela.plt point to .plt.
1905 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1906 rela_plt_os->set_info_section(this->plt_->output_section());
1907
6fa2a40b
CC
1908 // Create the rela_dyn section.
1909 this->rela_dyn_section(layout);
1910
4829d394
CC
1911 return this->got_;
1912}
1913
6fa2a40b
CC
1914// Reserve a GOT entry for a local symbol, and regenerate any
1915// necessary dynamic relocations.
1916
fc51264f 1917template<int size>
6fa2a40b 1918void
fc51264f 1919Target_x86_64<size>::reserve_local_got_entry(
6fa2a40b 1920 unsigned int got_index,
fc51264f 1921 Sized_relobj<size, false>* obj,
6fa2a40b
CC
1922 unsigned int r_sym,
1923 unsigned int got_type)
1924{
1925 unsigned int got_offset = got_index * 8;
1926 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1927
1928 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1929 switch (got_type)
1930 {
1931 case GOT_TYPE_STANDARD:
1932 if (parameters->options().output_is_position_independent())
1933 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
397b129b 1934 this->got_, got_offset, 0, false);
6fa2a40b
CC
1935 break;
1936 case GOT_TYPE_TLS_OFFSET:
1937 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1938 this->got_, got_offset, 0);
1939 break;
1940 case GOT_TYPE_TLS_PAIR:
1941 this->got_->reserve_slot(got_index + 1);
1942 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1943 this->got_, got_offset, 0);
1944 break;
1945 case GOT_TYPE_TLS_DESC:
1946 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1947 // this->got_->reserve_slot(got_index + 1);
1948 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1949 // this->got_, got_offset, 0);
1950 break;
1951 default:
1952 gold_unreachable();
1953 }
1954}
1955
1956// Reserve a GOT entry for a global symbol, and regenerate any
1957// necessary dynamic relocations.
1958
fc51264f 1959template<int size>
6fa2a40b 1960void
fc51264f
L
1961Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
1962 Symbol* gsym,
1963 unsigned int got_type)
6fa2a40b
CC
1964{
1965 unsigned int got_offset = got_index * 8;
1966 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1967
1968 this->got_->reserve_global(got_index, gsym, got_type);
1969 switch (got_type)
1970 {
1971 case GOT_TYPE_STANDARD:
1972 if (!gsym->final_value_is_known())
1973 {
1974 if (gsym->is_from_dynobj()
1975 || gsym->is_undefined()
1976 || gsym->is_preemptible()
1977 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1978 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1979 this->got_, got_offset, 0);
1980 else
1981 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
13cf9988 1982 this->got_, got_offset, 0, false);
6fa2a40b
CC
1983 }
1984 break;
1985 case GOT_TYPE_TLS_OFFSET:
1986 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
13cf9988 1987 this->got_, got_offset, 0, false);
6fa2a40b
CC
1988 break;
1989 case GOT_TYPE_TLS_PAIR:
1990 this->got_->reserve_slot(got_index + 1);
1991 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
13cf9988 1992 this->got_, got_offset, 0, false);
6fa2a40b 1993 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
13cf9988 1994 this->got_, got_offset + 8, 0, false);
6fa2a40b
CC
1995 break;
1996 case GOT_TYPE_TLS_DESC:
1997 this->got_->reserve_slot(got_index + 1);
1998 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
13cf9988 1999 this->got_, got_offset, 0, false);
6fa2a40b
CC
2000 break;
2001 default:
2002 gold_unreachable();
2003 }
2004}
2005
4829d394
CC
2006// Register an existing PLT entry for a global symbol.
2007
fc51264f 2008template<int size>
4829d394 2009void
fc51264f
L
2010Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
2011 Layout* layout,
2012 unsigned int plt_index,
2013 Symbol* gsym)
4829d394
CC
2014{
2015 gold_assert(this->plt_ != NULL);
2016 gold_assert(!gsym->has_plt_offset());
2017
2018 this->plt_->reserve_slot(plt_index);
2019
2020 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
2021
2022 unsigned int got_offset = (plt_index + 3) * 8;
67181c72 2023 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
4829d394
CC
2024}
2025
26d3c67d
CC
2026// Force a COPY relocation for a given symbol.
2027
fc51264f 2028template<int size>
26d3c67d 2029void
fc51264f 2030Target_x86_64<size>::emit_copy_reloc(
26d3c67d
CC
2031 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
2032{
2033 this->copy_relocs_.emit_copy_reloc(symtab,
fc51264f 2034 symtab->get_sized_symbol<size>(sym),
26d3c67d
CC
2035 os,
2036 offset,
2037 this->rela_dyn_section(NULL));
2038}
2039
9fa33bee 2040// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
edfbb029 2041
fc51264f 2042template<int size>
edfbb029 2043void
fc51264f
L
2044Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
2045 Layout* layout)
edfbb029
CC
2046{
2047 if (this->tls_base_symbol_defined_)
2048 return;
2049
2050 Output_segment* tls_segment = layout->tls_segment();
2051 if (tls_segment != NULL)
2052 {
183fd0e3 2053 bool is_exec = parameters->options().output_is_executable();
edfbb029 2054 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
99fff23b 2055 Symbol_table::PREDEFINED,
edfbb029
CC
2056 tls_segment, 0, 0,
2057 elfcpp::STT_TLS,
2058 elfcpp::STB_LOCAL,
2059 elfcpp::STV_HIDDEN, 0,
183fd0e3
AO
2060 (is_exec
2061 ? Symbol::SEGMENT_END
2062 : Symbol::SEGMENT_START),
2063 true);
edfbb029
CC
2064 }
2065 this->tls_base_symbol_defined_ = true;
2066}
2067
c2b45e22
CC
2068// Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2069
fc51264f 2070template<int size>
c2b45e22 2071void
fc51264f 2072Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
2e702c99 2073 Layout* layout)
c2b45e22
CC
2074{
2075 if (this->plt_ == NULL)
2076 this->make_plt_section(symtab, layout);
2077
2078 if (!this->plt_->has_tlsdesc_entry())
2079 {
2080 // Allocate the TLSDESC_GOT entry.
2081 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2082 unsigned int got_offset = got->add_constant(0);
2083
2084 // Allocate the TLSDESC_PLT entry.
2085 this->plt_->reserve_tlsdesc_entry(got_offset);
2086 }
2087}
2088
31d60480
ILT
2089// Create a GOT entry for the TLS module index.
2090
fc51264f 2091template<int size>
31d60480 2092unsigned int
fc51264f
L
2093Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2094 Sized_relobj_file<size, false>* object)
31d60480
ILT
2095{
2096 if (this->got_mod_index_offset_ == -1U)
2097 {
2098 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2099 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2100 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2101 unsigned int got_offset = got->add_constant(0);
2102 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
2e702c99 2103 got_offset, 0);
009a67a2 2104 got->add_constant(0);
31d60480
ILT
2105 this->got_mod_index_offset_ = got_offset;
2106 }
2107 return this->got_mod_index_offset_;
2108}
2109
2e30d253
ILT
2110// Optimize the TLS relocation type based on what we know about the
2111// symbol. IS_FINAL is true if the final address of this symbol is
2112// known at link time.
2113
fc51264f 2114template<int size>
e041f13d 2115tls::Tls_optimization
fc51264f 2116Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
2e30d253 2117{
2e30d253
ILT
2118 // If we are generating a shared library, then we can't do anything
2119 // in the linker.
8851ecca 2120 if (parameters->options().shared())
e041f13d 2121 return tls::TLSOPT_NONE;
2e30d253
ILT
2122
2123 switch (r_type)
2124 {
2125 case elfcpp::R_X86_64_TLSGD:
e041f13d
ILT
2126 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2127 case elfcpp::R_X86_64_TLSDESC_CALL:
2128 // These are General-Dynamic which permits fully general TLS
2e30d253
ILT
2129 // access. Since we know that we are generating an executable,
2130 // we can convert this to Initial-Exec. If we also know that
2131 // this is a local symbol, we can further switch to Local-Exec.
2132 if (is_final)
e041f13d
ILT
2133 return tls::TLSOPT_TO_LE;
2134 return tls::TLSOPT_TO_IE;
2e30d253 2135
d61c17ea 2136 case elfcpp::R_X86_64_TLSLD:
2e30d253
ILT
2137 // This is Local-Dynamic, which refers to a local symbol in the
2138 // dynamic TLS block. Since we know that we generating an
2139 // executable, we can switch to Local-Exec.
e041f13d 2140 return tls::TLSOPT_TO_LE;
2e30d253 2141
0ffd9845 2142 case elfcpp::R_X86_64_DTPOFF32:
0ffd9845
ILT
2143 case elfcpp::R_X86_64_DTPOFF64:
2144 // Another Local-Dynamic reloc.
e041f13d 2145 return tls::TLSOPT_TO_LE;
0ffd9845 2146
d61c17ea 2147 case elfcpp::R_X86_64_GOTTPOFF:
2e30d253
ILT
2148 // These are Initial-Exec relocs which get the thread offset
2149 // from the GOT. If we know that we are linking against the
2150 // local symbol, we can switch to Local-Exec, which links the
2151 // thread offset into the instruction.
2152 if (is_final)
e041f13d
ILT
2153 return tls::TLSOPT_TO_LE;
2154 return tls::TLSOPT_NONE;
2e30d253 2155
d61c17ea 2156 case elfcpp::R_X86_64_TPOFF32:
2e30d253
ILT
2157 // When we already have Local-Exec, there is nothing further we
2158 // can do.
e041f13d 2159 return tls::TLSOPT_NONE;
2e30d253
ILT
2160
2161 default:
2162 gold_unreachable();
2163 }
2e30d253
ILT
2164}
2165
95a2c8d6
RS
2166// Get the Reference_flags for a particular relocation.
2167
fc51264f 2168template<int size>
95a2c8d6 2169int
fc51264f 2170Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
95a2c8d6
RS
2171{
2172 switch (r_type)
2173 {
2174 case elfcpp::R_X86_64_NONE:
2175 case elfcpp::R_X86_64_GNU_VTINHERIT:
2176 case elfcpp::R_X86_64_GNU_VTENTRY:
2177 case elfcpp::R_X86_64_GOTPC32:
2178 case elfcpp::R_X86_64_GOTPC64:
2179 // No symbol reference.
2180 return 0;
2181
2182 case elfcpp::R_X86_64_64:
2183 case elfcpp::R_X86_64_32:
2184 case elfcpp::R_X86_64_32S:
2185 case elfcpp::R_X86_64_16:
2186 case elfcpp::R_X86_64_8:
2187 return Symbol::ABSOLUTE_REF;
2188
2189 case elfcpp::R_X86_64_PC64:
2190 case elfcpp::R_X86_64_PC32:
f49fe902 2191 case elfcpp::R_X86_64_PC32_BND:
95a2c8d6
RS
2192 case elfcpp::R_X86_64_PC16:
2193 case elfcpp::R_X86_64_PC8:
2194 case elfcpp::R_X86_64_GOTOFF64:
2195 return Symbol::RELATIVE_REF;
2196
2197 case elfcpp::R_X86_64_PLT32:
f49fe902 2198 case elfcpp::R_X86_64_PLT32_BND:
95a2c8d6
RS
2199 case elfcpp::R_X86_64_PLTOFF64:
2200 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2201
2202 case elfcpp::R_X86_64_GOT64:
2203 case elfcpp::R_X86_64_GOT32:
2204 case elfcpp::R_X86_64_GOTPCREL64:
2205 case elfcpp::R_X86_64_GOTPCREL:
2891b491
L
2206 case elfcpp::R_X86_64_GOTPCRELX:
2207 case elfcpp::R_X86_64_REX_GOTPCRELX:
95a2c8d6
RS
2208 case elfcpp::R_X86_64_GOTPLT64:
2209 // Absolute in GOT.
2210 return Symbol::ABSOLUTE_REF;
2211
2212 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2213 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2214 case elfcpp::R_X86_64_TLSDESC_CALL:
2215 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2216 case elfcpp::R_X86_64_DTPOFF32:
2217 case elfcpp::R_X86_64_DTPOFF64:
2218 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2219 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2220 return Symbol::TLS_REF;
2221
2222 case elfcpp::R_X86_64_COPY:
2223 case elfcpp::R_X86_64_GLOB_DAT:
2224 case elfcpp::R_X86_64_JUMP_SLOT:
2225 case elfcpp::R_X86_64_RELATIVE:
2226 case elfcpp::R_X86_64_IRELATIVE:
2227 case elfcpp::R_X86_64_TPOFF64:
2228 case elfcpp::R_X86_64_DTPMOD64:
2229 case elfcpp::R_X86_64_TLSDESC:
2230 case elfcpp::R_X86_64_SIZE32:
2231 case elfcpp::R_X86_64_SIZE64:
2232 default:
2233 // Not expected. We will give an error later.
2234 return 0;
2235 }
2236}
2237
e041f13d
ILT
2238// Report an unsupported relocation against a local symbol.
2239
fc51264f 2240template<int size>
e041f13d 2241void
fc51264f
L
2242Target_x86_64<size>::Scan::unsupported_reloc_local(
2243 Sized_relobj_file<size, false>* object,
6fa2a40b 2244 unsigned int r_type)
e041f13d 2245{
75f2446e
ILT
2246 gold_error(_("%s: unsupported reloc %u against local symbol"),
2247 object->name().c_str(), r_type);
e041f13d
ILT
2248}
2249
a036edd8
ILT
2250// We are about to emit a dynamic relocation of type R_TYPE. If the
2251// dynamic linker does not support it, issue an error. The GNU linker
2252// only issues a non-PIC error for an allocated read-only section.
2253// Here we know the section is allocated, but we don't know that it is
2254// read-only. But we check for all the relocation types which the
2255// glibc dynamic linker supports, so it seems appropriate to issue an
a29b0dad
ILT
2256// error even if the section is not read-only. If GSYM is not NULL,
2257// it is the symbol the relocation is against; if it is NULL, the
2258// relocation is against a local symbol.
a036edd8 2259
fc51264f 2260template<int size>
a036edd8 2261void
fc51264f
L
2262Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
2263 Symbol* gsym)
a036edd8
ILT
2264{
2265 switch (r_type)
2266 {
2fbb4320
ILT
2267 // These are the relocation types supported by glibc for x86_64
2268 // which should always work.
a036edd8 2269 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 2270 case elfcpp::R_X86_64_IRELATIVE:
a036edd8
ILT
2271 case elfcpp::R_X86_64_GLOB_DAT:
2272 case elfcpp::R_X86_64_JUMP_SLOT:
2273 case elfcpp::R_X86_64_DTPMOD64:
2274 case elfcpp::R_X86_64_DTPOFF64:
2275 case elfcpp::R_X86_64_TPOFF64:
2276 case elfcpp::R_X86_64_64:
2fbb4320
ILT
2277 case elfcpp::R_X86_64_COPY:
2278 return;
2279
2280 // glibc supports these reloc types, but they can overflow.
a036edd8 2281 case elfcpp::R_X86_64_PC32:
f49fe902 2282 case elfcpp::R_X86_64_PC32_BND:
a29b0dad
ILT
2283 // A PC relative reference is OK against a local symbol or if
2284 // the symbol is defined locally.
2285 if (gsym == NULL
2286 || (!gsym->is_from_dynobj()
2287 && !gsym->is_undefined()
2288 && !gsym->is_preemptible()))
2289 return;
2290 /* Fall through. */
2291 case elfcpp::R_X86_64_32:
3660ff06
L
2292 // R_X86_64_32 is OK for x32.
2293 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2294 return;
2fbb4320
ILT
2295 if (this->issued_non_pic_error_)
2296 return;
2297 gold_assert(parameters->options().output_is_position_independent());
a29b0dad
ILT
2298 if (gsym == NULL)
2299 object->error(_("requires dynamic R_X86_64_32 reloc which may "
2300 "overflow at runtime; recompile with -fPIC"));
2301 else
f49fe902
L
2302 {
2303 const char *r_name;
2304 switch (r_type)
2305 {
2306 case elfcpp::R_X86_64_32:
2307 r_name = "R_X86_64_32";
2308 break;
2309 case elfcpp::R_X86_64_PC32:
2310 r_name = "R_X86_64_PC32";
2311 break;
2312 case elfcpp::R_X86_64_PC32_BND:
2313 r_name = "R_X86_64_PC32_BND";
2314 break;
2315 default:
2316 gold_unreachable();
2317 break;
2318 }
2319 object->error(_("requires dynamic %s reloc against '%s' "
2320 "which may overflow at runtime; recompile "
2321 "with -fPIC"),
2322 r_name, gsym->name());
2323 }
2fbb4320 2324 this->issued_non_pic_error_ = true;
a036edd8
ILT
2325 return;
2326
2327 default:
2328 // This prevents us from issuing more than one error per reloc
2329 // section. But we can still wind up issuing more than one
2330 // error per object file.
2331 if (this->issued_non_pic_error_)
2e702c99 2332 return;
33aea2fd 2333 gold_assert(parameters->options().output_is_position_independent());
a29b0dad 2334 object->error(_("requires unsupported dynamic reloc %u; "
2e702c99 2335 "recompile with -fPIC"),
a29b0dad 2336 r_type);
a036edd8
ILT
2337 this->issued_non_pic_error_ = true;
2338 return;
2339
2340 case elfcpp::R_X86_64_NONE:
2341 gold_unreachable();
2342 }
2343}
2344
7223e9ca
ILT
2345// Return whether we need to make a PLT entry for a relocation of the
2346// given type against a STT_GNU_IFUNC symbol.
2347
fc51264f 2348template<int size>
7223e9ca 2349bool
fc51264f
L
2350Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
2351 Sized_relobj_file<size, false>* object,
6fa2a40b 2352 unsigned int r_type)
7223e9ca 2353{
95a2c8d6
RS
2354 int flags = Scan::get_reference_flags(r_type);
2355 if (flags & Symbol::TLS_REF)
2356 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2e702c99 2357 object->name().c_str(), r_type);
95a2c8d6 2358 return flags != 0;
7223e9ca
ILT
2359}
2360
2e30d253
ILT
2361// Scan a relocation for a local symbol.
2362
fc51264f 2363template<int size>
2e30d253 2364inline void
fc51264f
L
2365Target_x86_64<size>::Scan::local(Symbol_table* symtab,
2366 Layout* layout,
2367 Target_x86_64<size>* target,
2368 Sized_relobj_file<size, false>* object,
2369 unsigned int data_shndx,
2370 Output_section* output_section,
2371 const elfcpp::Rela<size, false>& reloc,
2372 unsigned int r_type,
bfdfa4cd
AM
2373 const elfcpp::Sym<size, false>& lsym,
2374 bool is_discarded)
2e30d253 2375{
bfdfa4cd
AM
2376 if (is_discarded)
2377 return;
2378
7223e9ca 2379 // A local STT_GNU_IFUNC symbol may require a PLT entry.
397b129b
CC
2380 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2381 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
7223e9ca 2382 {
fc51264f 2383 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7223e9ca
ILT
2384 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2385 }
2386
2e30d253
ILT
2387 switch (r_type)
2388 {
2389 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
2390 case elfcpp::R_X86_64_GNU_VTINHERIT:
2391 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
2392 break;
2393
2394 case elfcpp::R_X86_64_64:
d61c6bd4 2395 // If building a shared library (or a position-independent
dceae3c1
ILT
2396 // executable), we need to create a dynamic relocation for this
2397 // location. The relocation applied at link time will apply the
2398 // link-time value, so we flag the location with an
2399 // R_X86_64_RELATIVE relocation so the dynamic loader can
d61c6bd4 2400 // relocate it easily.
8851ecca 2401 if (parameters->options().output_is_position_independent())
2e702c99
RM
2402 {
2403 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2404 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7223e9ca 2405 rela_dyn->add_local_relative(object, r_sym,
62fe925a 2406 (size == 32
fd885f3a
L
2407 ? elfcpp::R_X86_64_RELATIVE64
2408 : elfcpp::R_X86_64_RELATIVE),
7223e9ca
ILT
2409 output_section, data_shndx,
2410 reloc.get_r_offset(),
397b129b 2411 reloc.get_r_addend(), is_ifunc);
2e702c99 2412 }
d61c6bd4
ILT
2413 break;
2414
2e30d253
ILT
2415 case elfcpp::R_X86_64_32:
2416 case elfcpp::R_X86_64_32S:
2417 case elfcpp::R_X86_64_16:
2418 case elfcpp::R_X86_64_8:
96f2030e 2419 // If building a shared library (or a position-independent
dceae3c1
ILT
2420 // executable), we need to create a dynamic relocation for this
2421 // location. We can't use an R_X86_64_RELATIVE relocation
2422 // because that is always a 64-bit relocation.
8851ecca 2423 if (parameters->options().output_is_position_independent())
2e702c99 2424 {
3660ff06
L
2425 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2426 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2427 {
2428 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2429 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2430 rela_dyn->add_local_relative(object, r_sym,
2431 elfcpp::R_X86_64_RELATIVE,
2432 output_section, data_shndx,
2433 reloc.get_r_offset(),
2434 reloc.get_r_addend(), is_ifunc);
2435 break;
2436 }
2437
2e702c99 2438 this->check_non_pic(object, r_type, NULL);
a036edd8 2439
2e702c99 2440 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
fc51264f 2441 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2e702c99 2442 if (lsym.get_st_type() != elfcpp::STT_SECTION)
d491d34e
ILT
2443 rela_dyn->add_local(object, r_sym, r_type, output_section,
2444 data_shndx, reloc.get_r_offset(),
2445 reloc.get_r_addend());
2e702c99
RM
2446 else
2447 {
2448 gold_assert(lsym.get_st_value() == 0);
d491d34e
ILT
2449 unsigned int shndx = lsym.get_st_shndx();
2450 bool is_ordinary;
2451 shndx = object->adjust_sym_shndx(r_sym, shndx,
2452 &is_ordinary);
2453 if (!is_ordinary)
2454 object->error(_("section symbol %u has bad shndx %u"),
2455 r_sym, shndx);
2456 else
2457 rela_dyn->add_local_section(object, shndx,
2458 r_type, output_section,
2459 data_shndx, reloc.get_r_offset(),
2460 reloc.get_r_addend());
2e702c99
RM
2461 }
2462 }
2e30d253
ILT
2463 break;
2464
2465 case elfcpp::R_X86_64_PC64:
2466 case elfcpp::R_X86_64_PC32:
f49fe902 2467 case elfcpp::R_X86_64_PC32_BND:
2e30d253
ILT
2468 case elfcpp::R_X86_64_PC16:
2469 case elfcpp::R_X86_64_PC8:
2470 break;
2471
f389a824 2472 case elfcpp::R_X86_64_PLT32:
f49fe902 2473 case elfcpp::R_X86_64_PLT32_BND:
f389a824
ILT
2474 // Since we know this is a local symbol, we can handle this as a
2475 // PC32 reloc.
2476 break;
2477
fdc2f80f 2478 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 2479 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
2480 case elfcpp::R_X86_64_GOTPC64:
2481 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
2482 // We need a GOT section.
2483 target->got_section(symtab, layout);
ee9e9e86
ILT
2484 // For PLTOFF64, we'd normally want a PLT section, but since we
2485 // know this is a local symbol, no PLT is needed.
2e30d253
ILT
2486 break;
2487
0ffd9845
ILT
2488 case elfcpp::R_X86_64_GOT64:
2489 case elfcpp::R_X86_64_GOT32:
2490 case elfcpp::R_X86_64_GOTPCREL64:
2491 case elfcpp::R_X86_64_GOTPCREL:
2891b491
L
2492 case elfcpp::R_X86_64_GOTPCRELX:
2493 case elfcpp::R_X86_64_REX_GOTPCRELX:
ee9e9e86 2494 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845 2495 {
1fa29f10 2496 // The symbol requires a GOT section.
2e702c99 2497 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1fa29f10
IT
2498
2499 // If the relocation symbol isn't IFUNC,
2500 // and is local, then we will convert
2501 // mov foo@GOTPCREL(%rip), %reg
2502 // to lea foo(%rip), %reg.
2503 // in Relocate::relocate.
2891b491
L
2504 if ((r_type == elfcpp::R_X86_64_GOTPCREL
2505 || r_type == elfcpp::R_X86_64_GOTPCRELX
2506 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
1fa29f10
IT
2507 && reloc.get_r_offset() >= 2
2508 && !is_ifunc)
2509 {
2510 section_size_type stype;
2511 const unsigned char* view = object->section_contents(data_shndx,
2512 &stype, true);
2513 if (view[reloc.get_r_offset() - 2] == 0x8b)
2514 break;
2515 }
2516
2517
2518 // The symbol requires a GOT entry.
2e702c99 2519 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7223e9ca
ILT
2520
2521 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2522 // lets function pointers compare correctly with shared
2523 // libraries. Otherwise we would need an IRELATIVE reloc.
2524 bool is_new;
397b129b 2525 if (is_ifunc)
7223e9ca
ILT
2526 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2527 else
2528 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2e702c99
RM
2529 if (is_new)
2530 {
2531 // If we are generating a shared object, we need to add a
2532 // dynamic relocation for this symbol's GOT entry.
2533 if (parameters->options().output_is_position_independent())
2534 {
2535 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7bf1f802
ILT
2536 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2537 if (r_type != elfcpp::R_X86_64_GOT32)
7223e9ca
ILT
2538 {
2539 unsigned int got_offset =
2540 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2541 rela_dyn->add_local_relative(object, r_sym,
2542 elfcpp::R_X86_64_RELATIVE,
397b129b 2543 got, got_offset, 0, is_ifunc);
7223e9ca 2544 }
2e702c99
RM
2545 else
2546 {
2547 this->check_non_pic(object, r_type, NULL);
2548
2549 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2550 rela_dyn->add_local(
2551 object, r_sym, r_type, got,
2552 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
2553 }
2554 }
2555 }
2556 // For GOTPLT64, we'd normally want a PLT section, but since
2557 // we know this is a local symbol, no PLT is needed.
0ffd9845
ILT
2558 }
2559 break;
2560
2e30d253
ILT
2561 case elfcpp::R_X86_64_COPY:
2562 case elfcpp::R_X86_64_GLOB_DAT:
2563 case elfcpp::R_X86_64_JUMP_SLOT:
2564 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 2565 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 2566 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 2567 case elfcpp::R_X86_64_TPOFF64:
2e30d253 2568 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 2569 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
2570 gold_error(_("%s: unexpected reloc %u in object file"),
2571 object->name().c_str(), r_type);
2e30d253
ILT
2572 break;
2573
d61c17ea 2574 // These are initial tls relocs, which are expected when linking
56622147
ILT
2575 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2576 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 2577 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 2578 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
2579 case elfcpp::R_X86_64_DTPOFF32:
2580 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
2581 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2582 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253 2583 {
8851ecca 2584 bool output_is_shared = parameters->options().shared();
e041f13d 2585 const tls::Tls_optimization optimized_type
2e702c99 2586 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
fc51264f 2587 r_type);
2e30d253
ILT
2588 switch (r_type)
2589 {
2e702c99
RM
2590 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2591 if (optimized_type == tls::TLSOPT_NONE)
2592 {
2593 // Create a pair of GOT entries for the module index and
2594 // dtv-relative offset.
2595 Output_data_got<64, false>* got
2596 = target->got_section(symtab, layout);
2597 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
d491d34e
ILT
2598 unsigned int shndx = lsym.get_st_shndx();
2599 bool is_ordinary;
2600 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2601 if (!is_ordinary)
2602 object->error(_("local symbol %u has bad shndx %u"),
2603 r_sym, shndx);
2e702c99 2604 else
83896202
ILT
2605 got->add_local_pair_with_rel(object, r_sym,
2606 shndx,
2607 GOT_TYPE_TLS_PAIR,
2608 target->rela_dyn_section(layout),
bd73a62d 2609 elfcpp::R_X86_64_DTPMOD64);
2e702c99
RM
2610 }
2611 else if (optimized_type != tls::TLSOPT_TO_LE)
7bf1f802 2612 unsupported_reloc_local(object, r_type);
2e702c99 2613 break;
7bf1f802 2614
2e702c99
RM
2615 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2616 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
2617 if (optimized_type == tls::TLSOPT_NONE)
2618 {
2e702c99
RM
2619 // Create reserved PLT and GOT entries for the resolver.
2620 target->reserve_tlsdesc_entries(symtab, layout);
2621
2622 // Generate a double GOT entry with an
2623 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2624 // is resolved lazily, so the GOT entry needs to be in
2625 // an area in .got.plt, not .got. Call got_section to
2626 // make sure the section has been created.
a8df5856 2627 target->got_section(symtab, layout);
2e702c99
RM
2628 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2629 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
e291e7b9
ILT
2630 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
2631 {
2632 unsigned int got_offset = got->add_constant(0);
2633 got->add_constant(0);
2634 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2635 got_offset);
2636 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2637 // We store the arguments we need in a vector, and
2638 // use the index into the vector as the parameter
2639 // to pass to the target specific routines.
2640 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
2641 void* arg = reinterpret_cast<void*>(intarg);
2642 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2643 got, got_offset, 0);
2644 }
c2b45e22
CC
2645 }
2646 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 2647 unsupported_reloc_local(object, r_type);
2e30d253
ILT
2648 break;
2649
2e702c99 2650 case elfcpp::R_X86_64_TLSDESC_CALL:
c2b45e22
CC
2651 break;
2652
2e702c99 2653 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
2654 if (optimized_type == tls::TLSOPT_NONE)
2655 {
2e702c99
RM
2656 // Create a GOT entry for the module index.
2657 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
2658 }
2659 else if (optimized_type != tls::TLSOPT_TO_LE)
2660 unsupported_reloc_local(object, r_type);
2661 break;
2662
2e702c99
RM
2663 case elfcpp::R_X86_64_DTPOFF32:
2664 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
2665 break;
2666
2e702c99 2667 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 2668 layout->set_has_static_tls();
2e702c99
RM
2669 if (optimized_type == tls::TLSOPT_NONE)
2670 {
2671 // Create a GOT entry for the tp-relative offset.
2672 Output_data_got<64, false>* got
2673 = target->got_section(symtab, layout);
2674 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2675 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
83896202
ILT
2676 target->rela_dyn_section(layout),
2677 elfcpp::R_X86_64_TPOFF64);
2e702c99
RM
2678 }
2679 else if (optimized_type != tls::TLSOPT_TO_LE)
2680 unsupported_reloc_local(object, r_type);
2681 break;
0ffd9845 2682
2e702c99 2683 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 2684 layout->set_has_static_tls();
2e702c99
RM
2685 if (output_is_shared)
2686 unsupported_reloc_local(object, r_type);
2e30d253 2687 break;
e041f13d 2688
2e702c99
RM
2689 default:
2690 gold_unreachable();
2e30d253
ILT
2691 }
2692 }
2693 break;
2e30d253 2694
fdc2f80f
ILT
2695 case elfcpp::R_X86_64_SIZE32:
2696 case elfcpp::R_X86_64_SIZE64:
2e30d253 2697 default:
75f2446e
ILT
2698 gold_error(_("%s: unsupported reloc %u against local symbol"),
2699 object->name().c_str(), r_type);
2e30d253
ILT
2700 break;
2701 }
2702}
2703
2704
e041f13d
ILT
2705// Report an unsupported relocation against a global symbol.
2706
fc51264f 2707template<int size>
e041f13d 2708void
fc51264f
L
2709Target_x86_64<size>::Scan::unsupported_reloc_global(
2710 Sized_relobj_file<size, false>* object,
6fa2a40b
CC
2711 unsigned int r_type,
2712 Symbol* gsym)
e041f13d 2713{
75f2446e 2714 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12 2715 object->name().c_str(), r_type, gsym->demangled_name().c_str());
e041f13d
ILT
2716}
2717
ce97fa81 2718// Returns true if this relocation type could be that of a function pointer.
fc51264f 2719template<int size>
21bb3914 2720inline bool
fc51264f 2721Target_x86_64<size>::Scan::possible_function_pointer_reloc(unsigned int r_type)
21bb3914 2722{
21bb3914
ST
2723 switch (r_type)
2724 {
2725 case elfcpp::R_X86_64_64:
2726 case elfcpp::R_X86_64_32:
2727 case elfcpp::R_X86_64_32S:
2728 case elfcpp::R_X86_64_16:
2729 case elfcpp::R_X86_64_8:
ce97fa81
ST
2730 case elfcpp::R_X86_64_GOT64:
2731 case elfcpp::R_X86_64_GOT32:
2732 case elfcpp::R_X86_64_GOTPCREL64:
2733 case elfcpp::R_X86_64_GOTPCREL:
2891b491
L
2734 case elfcpp::R_X86_64_GOTPCRELX:
2735 case elfcpp::R_X86_64_REX_GOTPCRELX:
ce97fa81 2736 case elfcpp::R_X86_64_GOTPLT64:
21bb3914 2737 {
2e702c99 2738 return true;
21bb3914
ST
2739 }
2740 }
2741 return false;
2742}
2743
2744// For safe ICF, scan a relocation for a local symbol to check if it
2745// corresponds to a function pointer being taken. In that case mark
2746// the function whose pointer was taken as not foldable.
2747
fc51264f 2748template<int size>
21bb3914 2749inline bool
fc51264f 2750Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
21bb3914
ST
2751 Symbol_table* ,
2752 Layout* ,
fc51264f
L
2753 Target_x86_64<size>* ,
2754 Sized_relobj_file<size, false>* ,
21bb3914
ST
2755 unsigned int ,
2756 Output_section* ,
fc51264f 2757 const elfcpp::Rela<size, false>& ,
21bb3914 2758 unsigned int r_type,
fc51264f 2759 const elfcpp::Sym<size, false>&)
21bb3914
ST
2760{
2761 // When building a shared library, do not fold any local symbols as it is
2762 // not possible to distinguish pointer taken versus a call by looking at
2763 // the relocation types.
2764 return (parameters->options().shared()
2e702c99 2765 || possible_function_pointer_reloc(r_type));
21bb3914
ST
2766}
2767
2768// For safe ICF, scan a relocation for a global symbol to check if it
2769// corresponds to a function pointer being taken. In that case mark
2770// the function whose pointer was taken as not foldable.
2771
fc51264f 2772template<int size>
21bb3914 2773inline bool
fc51264f 2774Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
21bb3914
ST
2775 Symbol_table*,
2776 Layout* ,
fc51264f
L
2777 Target_x86_64<size>* ,
2778 Sized_relobj_file<size, false>* ,
21bb3914
ST
2779 unsigned int ,
2780 Output_section* ,
fc51264f 2781 const elfcpp::Rela<size, false>& ,
21bb3914
ST
2782 unsigned int r_type,
2783 Symbol* gsym)
2784{
2785 // When building a shared library, do not fold symbols whose visibility
2786 // is hidden, internal or protected.
2787 return ((parameters->options().shared()
2e702c99 2788 && (gsym->visibility() == elfcpp::STV_INTERNAL
21bb3914
ST
2789 || gsym->visibility() == elfcpp::STV_PROTECTED
2790 || gsym->visibility() == elfcpp::STV_HIDDEN))
2e702c99 2791 || possible_function_pointer_reloc(r_type));
21bb3914
ST
2792}
2793
2e30d253
ILT
2794// Scan a relocation for a global symbol.
2795
fc51264f 2796template<int size>
2e30d253 2797inline void
fc51264f 2798Target_x86_64<size>::Scan::global(Symbol_table* symtab,
2e702c99
RM
2799 Layout* layout,
2800 Target_x86_64<size>* target,
2801 Sized_relobj_file<size, false>* object,
2802 unsigned int data_shndx,
2803 Output_section* output_section,
2804 const elfcpp::Rela<size, false>& reloc,
2805 unsigned int r_type,
2806 Symbol* gsym)
2e30d253 2807{
7223e9ca
ILT
2808 // A STT_GNU_IFUNC symbol may require a PLT entry.
2809 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2810 && this->reloc_needs_plt_for_ifunc(object, r_type))
2811 target->make_plt_entry(symtab, layout, gsym);
2812
2e30d253
ILT
2813 switch (r_type)
2814 {
2815 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
2816 case elfcpp::R_X86_64_GNU_VTINHERIT:
2817 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
2818 break;
2819
2820 case elfcpp::R_X86_64_64:
2e30d253
ILT
2821 case elfcpp::R_X86_64_32:
2822 case elfcpp::R_X86_64_32S:
2e30d253 2823 case elfcpp::R_X86_64_16:
2e30d253 2824 case elfcpp::R_X86_64_8:
96f2030e 2825 {
2e702c99
RM
2826 // Make a PLT entry if necessary.
2827 if (gsym->needs_plt_entry())
2828 {
2829 target->make_plt_entry(symtab, layout, gsym);
2830 // Since this is not a PC-relative relocation, we may be
2831 // taking the address of a function. In that case we need to
2832 // set the entry in the dynamic symbol table to the address of
2833 // the PLT entry.
2834 if (gsym->is_from_dynobj() && !parameters->options().shared())
2835 gsym->set_needs_dynsym_value();
2836 }
2837 // Make a dynamic relocation if necessary.
2838 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2839 {
a82bef93
ST
2840 if (!parameters->options().output_is_position_independent()
2841 && gsym->may_need_copy_reloc())
2e702c99
RM
2842 {
2843 target->copy_reloc(symtab, layout, object,
2844 data_shndx, output_section, gsym, reloc);
2845 }
1bae613c
L
2846 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2847 || (size == 32 && r_type == elfcpp::R_X86_64_32))
7223e9ca
ILT
2848 && gsym->type() == elfcpp::STT_GNU_IFUNC
2849 && gsym->can_use_relative_reloc(false)
2850 && !gsym->is_from_dynobj()
2851 && !gsym->is_undefined()
2852 && !gsym->is_preemptible())
2853 {
2854 // Use an IRELATIVE reloc for a locally defined
2855 // STT_GNU_IFUNC symbol. This makes a function
2856 // address in a PIE executable match the address in a
2857 // shared library that it links against.
67181c72
ILT
2858 Reloc_section* rela_dyn =
2859 target->rela_irelative_section(layout);
7223e9ca
ILT
2860 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2861 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2862 output_section, object,
2863 data_shndx,
2864 reloc.get_r_offset(),
2865 reloc.get_r_addend());
2866 }
b14016f0
L
2867 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2868 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2e702c99
RM
2869 && gsym->can_use_relative_reloc(false))
2870 {
2871 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7223e9ca
ILT
2872 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2873 output_section, object,
2874 data_shndx,
2875 reloc.get_r_offset(),
13cf9988 2876 reloc.get_r_addend(), false);
2e702c99
RM
2877 }
2878 else
2879 {
2880 this->check_non_pic(object, r_type, gsym);
2881 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2882 rela_dyn->add_global(gsym, r_type, output_section, object,
2883 data_shndx, reloc.get_r_offset(),
2884 reloc.get_r_addend());
2885 }
2886 }
d61c6bd4
ILT
2887 }
2888 break;
2889
2890 case elfcpp::R_X86_64_PC64:
2891 case elfcpp::R_X86_64_PC32:
f49fe902 2892 case elfcpp::R_X86_64_PC32_BND:
d61c6bd4
ILT
2893 case elfcpp::R_X86_64_PC16:
2894 case elfcpp::R_X86_64_PC8:
2895 {
2e702c99
RM
2896 // Make a PLT entry if necessary.
2897 if (gsym->needs_plt_entry())
2898 target->make_plt_entry(symtab, layout, gsym);
2899 // Make a dynamic relocation if necessary.
2900 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2901 {
a82bef93
ST
2902 if (parameters->options().output_is_executable()
2903 && gsym->may_need_copy_reloc())
2e702c99
RM
2904 {
2905 target->copy_reloc(symtab, layout, object,
2906 data_shndx, output_section, gsym, reloc);
2907 }
2908 else
2909 {
2910 this->check_non_pic(object, r_type, gsym);
2911 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2912 rela_dyn->add_global(gsym, r_type, output_section, object,
2913 data_shndx, reloc.get_r_offset(),
2914 reloc.get_r_addend());
2915 }
2916 }
d61c6bd4 2917 }
2e30d253
ILT
2918 break;
2919
ff006520 2920 case elfcpp::R_X86_64_GOT64:
2e30d253 2921 case elfcpp::R_X86_64_GOT32:
ff006520
ILT
2922 case elfcpp::R_X86_64_GOTPCREL64:
2923 case elfcpp::R_X86_64_GOTPCREL:
2891b491
L
2924 case elfcpp::R_X86_64_GOTPCRELX:
2925 case elfcpp::R_X86_64_REX_GOTPCRELX:
ff006520 2926 case elfcpp::R_X86_64_GOTPLT64:
2e30d253 2927 {
2e702c99
RM
2928 // The symbol requires a GOT entry.
2929 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1fa29f10
IT
2930
2931 // If we convert this from
2932 // mov foo@GOTPCREL(%rip), %reg
2933 // to lea foo(%rip), %reg.
2934 // in Relocate::relocate, then there is nothing to do here.
2891b491
L
2935 if ((r_type == elfcpp::R_X86_64_GOTPCREL
2936 || r_type == elfcpp::R_X86_64_GOTPCRELX
2937 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
1fa29f10
IT
2938 && reloc.get_r_offset() >= 2
2939 && Target_x86_64<size>::can_convert_mov_to_lea(gsym))
2940 {
2941 section_size_type stype;
2942 const unsigned char* view = object->section_contents(data_shndx,
2943 &stype, true);
2944 if (view[reloc.get_r_offset() - 2] == 0x8b)
2945 break;
2946 }
2947
2e702c99 2948 if (gsym->final_value_is_known())
7223e9ca
ILT
2949 {
2950 // For a STT_GNU_IFUNC symbol we want the PLT address.
2951 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2952 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2953 else
2954 got->add_global(gsym, GOT_TYPE_STANDARD);
2955 }
2e702c99
RM
2956 else
2957 {
2958 // If this symbol is not fully resolved, we need to add a
2959 // dynamic relocation for it.
2960 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
07aa62f2
ILT
2961
2962 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2963 //
2964 // 1) The symbol may be defined in some other module.
2965 //
2966 // 2) We are building a shared library and this is a
2967 // protected symbol; using GLOB_DAT means that the dynamic
2968 // linker can use the address of the PLT in the main
2969 // executable when appropriate so that function address
2970 // comparisons work.
2971 //
2972 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2973 // code, again so that function address comparisons work.
7223e9ca
ILT
2974 if (gsym->is_from_dynobj()
2975 || gsym->is_undefined()
2976 || gsym->is_preemptible()
07aa62f2
ILT
2977 || (gsym->visibility() == elfcpp::STV_PROTECTED
2978 && parameters->options().shared())
7223e9ca
ILT
2979 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2980 && parameters->options().output_is_position_independent()))
2e702c99 2981 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
83896202 2982 elfcpp::R_X86_64_GLOB_DAT);
2e702c99
RM
2983 else
2984 {
7223e9ca
ILT
2985 // For a STT_GNU_IFUNC symbol we want to write the PLT
2986 // offset into the GOT, so that function pointer
2987 // comparisons work correctly.
2988 bool is_new;
2989 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2990 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2991 else
2992 {
2993 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2994 // Tell the dynamic linker to use the PLT address
2995 // when resolving relocations.
2996 if (gsym->is_from_dynobj()
2997 && !parameters->options().shared())
2998 gsym->set_needs_dynsym_value();
2999 }
2e702c99 3000 if (is_new)
7223e9ca
ILT
3001 {
3002 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3003 rela_dyn->add_global_relative(gsym,
3004 elfcpp::R_X86_64_RELATIVE,
13cf9988 3005 got, got_off, 0, false);
7223e9ca 3006 }
2e702c99
RM
3007 }
3008 }
2e30d253
ILT
3009 }
3010 break;
3011
3012 case elfcpp::R_X86_64_PLT32:
f49fe902 3013 case elfcpp::R_X86_64_PLT32_BND:
2e30d253
ILT
3014 // If the symbol is fully resolved, this is just a PC32 reloc.
3015 // Otherwise we need a PLT entry.
3016 if (gsym->final_value_is_known())
3017 break;
96f2030e
ILT
3018 // If building a shared library, we can also skip the PLT entry
3019 // if the symbol is defined in the output file and is protected
3020 // or hidden.
3021 if (gsym->is_defined()
2e702c99
RM
3022 && !gsym->is_from_dynobj()
3023 && !gsym->is_preemptible())
96f2030e 3024 break;
2e30d253
ILT
3025 target->make_plt_entry(symtab, layout, gsym);
3026 break;
3027
fdc2f80f 3028 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 3029 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
3030 case elfcpp::R_X86_64_GOTPC64:
3031 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
3032 // We need a GOT section.
3033 target->got_section(symtab, layout);
ee9e9e86
ILT
3034 // For PLTOFF64, we also need a PLT entry (but only if the
3035 // symbol is not fully resolved).
3036 if (r_type == elfcpp::R_X86_64_PLTOFF64
3037 && !gsym->final_value_is_known())
3038 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
3039 break;
3040
2e30d253
ILT
3041 case elfcpp::R_X86_64_COPY:
3042 case elfcpp::R_X86_64_GLOB_DAT:
3043 case elfcpp::R_X86_64_JUMP_SLOT:
3044 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 3045 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 3046 // These are outstanding tls relocs, which are unexpected when linking
e822f2b1 3047 case elfcpp::R_X86_64_TPOFF64:
2e30d253 3048 case elfcpp::R_X86_64_DTPMOD64:
e822f2b1 3049 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
3050 gold_error(_("%s: unexpected reloc %u in object file"),
3051 object->name().c_str(), r_type);
2e30d253 3052 break;
2e30d253 3053
d61c17ea 3054 // These are initial tls relocs, which are expected for global()
56622147
ILT
3055 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3056 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 3057 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 3058 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
3059 case elfcpp::R_X86_64_DTPOFF32:
3060 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
3061 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3062 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253 3063 {
65d92137
CC
3064 // For the Initial-Exec model, we can treat undef symbols as final
3065 // when building an executable.
3066 const bool is_final = (gsym->final_value_is_known() ||
3067 (r_type == elfcpp::R_X86_64_GOTTPOFF &&
3068 gsym->is_undefined() &&
3069 parameters->options().output_is_executable()));
e041f13d 3070 const tls::Tls_optimization optimized_type
2e702c99 3071 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
3072 switch (r_type)
3073 {
2e702c99 3074 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
3075 if (optimized_type == tls::TLSOPT_NONE)
3076 {
2e702c99
RM
3077 // Create a pair of GOT entries for the module index and
3078 // dtv-relative offset.
3079 Output_data_got<64, false>* got
3080 = target->got_section(symtab, layout);
3081 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
83896202
ILT
3082 target->rela_dyn_section(layout),
3083 elfcpp::R_X86_64_DTPMOD64,
3084 elfcpp::R_X86_64_DTPOFF64);
7bf1f802
ILT
3085 }
3086 else if (optimized_type == tls::TLSOPT_TO_IE)
3087 {
2e702c99
RM
3088 // Create a GOT entry for the tp-relative offset.
3089 Output_data_got<64, false>* got
3090 = target->got_section(symtab, layout);
3091 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
83896202
ILT
3092 target->rela_dyn_section(layout),
3093 elfcpp::R_X86_64_TPOFF64);
7bf1f802
ILT
3094 }
3095 else if (optimized_type != tls::TLSOPT_TO_LE)
3096 unsupported_reloc_global(object, r_type, gsym);
3097 break;
3098
2e702c99
RM
3099 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3100 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
3101 if (optimized_type == tls::TLSOPT_NONE)
3102 {
2e702c99
RM
3103 // Create reserved PLT and GOT entries for the resolver.
3104 target->reserve_tlsdesc_entries(symtab, layout);
3105
3106 // Create a double GOT entry with an R_X86_64_TLSDESC
3107 // reloc. The R_X86_64_TLSDESC reloc is resolved
3108 // lazily, so the GOT entry needs to be in an area in
3109 // .got.plt, not .got. Call got_section to make sure
3110 // the section has been created.
a8df5856 3111 target->got_section(symtab, layout);
2e702c99 3112 Output_data_got<64, false>* got = target->got_tlsdesc_section();
ca09d69a 3113 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2e702c99 3114 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
83896202 3115 elfcpp::R_X86_64_TLSDESC, 0);
c2b45e22
CC
3116 }
3117 else if (optimized_type == tls::TLSOPT_TO_IE)
3118 {
2e702c99
RM
3119 // Create a GOT entry for the tp-relative offset.
3120 Output_data_got<64, false>* got
3121 = target->got_section(symtab, layout);
3122 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
83896202
ILT
3123 target->rela_dyn_section(layout),
3124 elfcpp::R_X86_64_TPOFF64);
c2b45e22
CC
3125 }
3126 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 3127 unsupported_reloc_global(object, r_type, gsym);
2e30d253
ILT
3128 break;
3129
2e702c99 3130 case elfcpp::R_X86_64_TLSDESC_CALL:
c2b45e22
CC
3131 break;
3132
2e702c99 3133 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
3134 if (optimized_type == tls::TLSOPT_NONE)
3135 {
2e702c99
RM
3136 // Create a GOT entry for the module index.
3137 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
3138 }
3139 else if (optimized_type != tls::TLSOPT_TO_LE)
3140 unsupported_reloc_global(object, r_type, gsym);
3141 break;
3142
2e702c99
RM
3143 case elfcpp::R_X86_64_DTPOFF32:
3144 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
3145 break;
3146
2e702c99 3147 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 3148 layout->set_has_static_tls();
2e702c99
RM
3149 if (optimized_type == tls::TLSOPT_NONE)
3150 {
3151 // Create a GOT entry for the tp-relative offset.
3152 Output_data_got<64, false>* got
3153 = target->got_section(symtab, layout);
3154 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
83896202
ILT
3155 target->rela_dyn_section(layout),
3156 elfcpp::R_X86_64_TPOFF64);
2e702c99
RM
3157 }
3158 else if (optimized_type != tls::TLSOPT_TO_LE)
3159 unsupported_reloc_global(object, r_type, gsym);
3160 break;
0ffd9845 3161
2e702c99 3162 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 3163 layout->set_has_static_tls();
2e702c99 3164 if (parameters->options().shared())
b1759dce 3165 unsupported_reloc_global(object, r_type, gsym);
2e30d253 3166 break;
e041f13d 3167
2e702c99
RM
3168 default:
3169 gold_unreachable();
2e30d253
ILT
3170 }
3171 }
3172 break;
fdc2f80f
ILT
3173
3174 case elfcpp::R_X86_64_SIZE32:
3175 case elfcpp::R_X86_64_SIZE64:
2e30d253 3176 default:
75f2446e 3177 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12 3178 object->name().c_str(), r_type,
2e702c99 3179 gsym->demangled_name().c_str());
2e30d253
ILT
3180 break;
3181 }
3182}
3183
fc51264f 3184template<int size>
6d03d481 3185void
fc51264f
L
3186Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
3187 Layout* layout,
3188 Sized_relobj_file<size, false>* object,
3189 unsigned int data_shndx,
3190 unsigned int sh_type,
3191 const unsigned char* prelocs,
3192 size_t reloc_count,
3193 Output_section* output_section,
3194 bool needs_special_offset_handling,
3195 size_t local_symbol_count,
3196 const unsigned char* plocal_symbols)
6d03d481 3197{
4d625b70
CC
3198 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3199 Classify_reloc;
6d03d481
ST
3200
3201 if (sh_type == elfcpp::SHT_REL)
3202 {
3203 return;
3204 }
3205
4d625b70
CC
3206 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
3207 Classify_reloc>(
6d03d481
ST
3208 symtab,
3209 layout,
3210 this,
3211 object,
3212 data_shndx,
3213 prelocs,
3214 reloc_count,
3215 output_section,
3216 needs_special_offset_handling,
3217 local_symbol_count,
3218 plocal_symbols);
2e702c99 3219
6d03d481 3220}
2e30d253
ILT
3221// Scan relocations for a section.
3222
fc51264f 3223template<int size>
2e30d253 3224void
fc51264f
L
3225Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
3226 Layout* layout,
3227 Sized_relobj_file<size, false>* object,
3228 unsigned int data_shndx,
3229 unsigned int sh_type,
3230 const unsigned char* prelocs,
3231 size_t reloc_count,
3232 Output_section* output_section,
3233 bool needs_special_offset_handling,
3234 size_t local_symbol_count,
3235 const unsigned char* plocal_symbols)
2e30d253 3236{
4d625b70
CC
3237 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3238 Classify_reloc;
3239
2e30d253
ILT
3240 if (sh_type == elfcpp::SHT_REL)
3241 {
75f2446e
ILT
3242 gold_error(_("%s: unsupported REL reloc section"),
3243 object->name().c_str());
3244 return;
2e30d253
ILT
3245 }
3246
4d625b70 3247 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
2e30d253
ILT
3248 symtab,
3249 layout,
3250 this,
3251 object,
3252 data_shndx,
3253 prelocs,
3254 reloc_count,
730cdc88
ILT
3255 output_section,
3256 needs_special_offset_handling,
2e30d253 3257 local_symbol_count,
730cdc88 3258 plocal_symbols);
2e30d253
ILT
3259}
3260
3261// Finalize the sections.
3262
fc51264f 3263template<int size>
2e30d253 3264void
fc51264f 3265Target_x86_64<size>::do_finalize_sections(
f59f41f3
DK
3266 Layout* layout,
3267 const Input_objects*,
e785ec03 3268 Symbol_table* symtab)
2e30d253 3269{
ea715a34
ILT
3270 const Reloc_section* rel_plt = (this->plt_ == NULL
3271 ? NULL
e291e7b9 3272 : this->plt_->rela_plt());
ea715a34 3273 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
612a8d3d 3274 this->rela_dyn_, true, false);
2e702c99 3275
2e30d253
ILT
3276 // Fill in some more dynamic tags.
3277 Output_data_dynamic* const odyn = layout->dynamic_data();
3278 if (odyn != NULL)
3279 {
22b127cc 3280 if (this->plt_ != NULL
ea715a34
ILT
3281 && this->plt_->output_section() != NULL
3282 && this->plt_->has_tlsdesc_entry())
2e30d253 3283 {
ea715a34
ILT
3284 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
3285 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
3286 this->got_->finalize_data_size();
3287 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
3288 this->plt_, plt_offset);
3289 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
3290 this->got_, got_offset);
2e30d253
ILT
3291 }
3292 }
3293
3294 // Emit any relocs we saved in an attempt to avoid generating COPY
3295 // relocs.
12c0daef
ILT
3296 if (this->copy_relocs_.any_saved_relocs())
3297 this->copy_relocs_.emit(this->rela_dyn_section(layout));
e785ec03
ILT
3298
3299 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3300 // the .got.plt section.
3301 Symbol* sym = this->global_offset_table_;
3302 if (sym != NULL)
3303 {
3304 uint64_t data_size = this->got_plt_->current_data_size();
fc51264f 3305 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
e785ec03 3306 }
28a13fec 3307
67181c72
ILT
3308 if (parameters->doing_static_link()
3309 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
28a13fec
ILT
3310 {
3311 // If linking statically, make sure that the __rela_iplt symbols
3312 // were defined if necessary, even if we didn't create a PLT.
3313 static const Define_symbol_in_segment syms[] =
3314 {
3315 {
3316 "__rela_iplt_start", // name
3317 elfcpp::PT_LOAD, // segment_type
3318 elfcpp::PF_W, // segment_flags_set
3319 elfcpp::PF(0), // segment_flags_clear
3320 0, // value
3321 0, // size
3322 elfcpp::STT_NOTYPE, // type
3323 elfcpp::STB_GLOBAL, // binding
3324 elfcpp::STV_HIDDEN, // visibility
3325 0, // nonvis
3326 Symbol::SEGMENT_START, // offset_from_base
3327 true // only_if_ref
3328 },
3329 {
3330 "__rela_iplt_end", // name
3331 elfcpp::PT_LOAD, // segment_type
3332 elfcpp::PF_W, // segment_flags_set
3333 elfcpp::PF(0), // segment_flags_clear
3334 0, // value
3335 0, // size
3336 elfcpp::STT_NOTYPE, // type
3337 elfcpp::STB_GLOBAL, // binding
3338 elfcpp::STV_HIDDEN, // visibility
3339 0, // nonvis
3340 Symbol::SEGMENT_START, // offset_from_base
3341 true // only_if_ref
3342 }
3343 };
3344
3345 symtab->define_symbols(layout, 2, syms,
3346 layout->script_options()->saw_sections_clause());
3347 }
2e30d253
ILT
3348}
3349
19ef3f4d
CC
3350// For x32, we need to handle PC-relative relocations using full 64-bit
3351// arithmetic, so that we can detect relocation overflows properly.
3352// This class overrides the pcrela32_check methods from the defaults in
3353// Relocate_functions in reloc.h.
3354
3355template<int size>
3356class X86_64_relocate_functions : public Relocate_functions<size, false>
3357{
3358 public:
3359 typedef Relocate_functions<size, false> Base;
3360
3361 // Do a simple PC relative relocation with the addend in the
3362 // relocation.
3363 static inline typename Base::Reloc_status
3364 pcrela32_check(unsigned char* view,
3365 typename elfcpp::Elf_types<64>::Elf_Addr value,
3366 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
3367 typename elfcpp::Elf_types<64>::Elf_Addr address)
3368 {
3369 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
3370 Valtype* wv = reinterpret_cast<Valtype*>(view);
3371 value = value + addend - address;
3372 elfcpp::Swap<32, false>::writeval(wv, value);
3373 return (Bits<32>::has_overflow(value)
3374 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
3375 }
3376
3377 // Do a simple PC relative relocation with a Symbol_value with the
3378 // addend in the relocation.
3379 static inline typename Base::Reloc_status
3380 pcrela32_check(unsigned char* view,
3381 const Sized_relobj_file<size, false>* object,
3382 const Symbol_value<size>* psymval,
3383 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
3384 typename elfcpp::Elf_types<64>::Elf_Addr address)
3385 {
3386 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
3387 Valtype* wv = reinterpret_cast<Valtype*>(view);
7c8b700c
CC
3388 typename elfcpp::Elf_types<64>::Elf_Addr value;
3389 if (addend >= 0)
3390 value = psymval->value(object, addend);
3391 else
3392 {
3393 // For negative addends, get the symbol value without
3394 // the addend, then add the addend using 64-bit arithmetic.
3395 value = psymval->value(object, 0);
3396 value += addend;
3397 }
3398 value -= address;
19ef3f4d
CC
3399 elfcpp::Swap<32, false>::writeval(wv, value);
3400 return (Bits<32>::has_overflow(value)
3401 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
3402 }
3403};
3404
2e30d253
ILT
3405// Perform a relocation.
3406
fc51264f 3407template<int size>
2e30d253 3408inline bool
fc51264f
L
3409Target_x86_64<size>::Relocate::relocate(
3410 const Relocate_info<size, false>* relinfo,
91a65d2f 3411 unsigned int,
fc51264f
L
3412 Target_x86_64<size>* target,
3413 Output_section*,
3414 size_t relnum,
91a65d2f 3415 const unsigned char* preloc,
fc51264f
L
3416 const Sized_symbol<size>* gsym,
3417 const Symbol_value<size>* psymval,
3418 unsigned char* view,
3419 typename elfcpp::Elf_types<size>::Elf_Addr address,
3420 section_size_type view_size)
2e30d253 3421{
19ef3f4d 3422 typedef X86_64_relocate_functions<size> Reloc_funcs;
91a65d2f
AM
3423 const elfcpp::Rela<size, false> rela(preloc);
3424 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
3425
2e30d253
ILT
3426 if (this->skip_call_tls_get_addr_)
3427 {
5efc7cd2 3428 if ((r_type != elfcpp::R_X86_64_PLT32
f49fe902
L
3429 && r_type != elfcpp::R_X86_64_PLT32_BND
3430 && r_type != elfcpp::R_X86_64_PC32_BND
2e702c99 3431 && r_type != elfcpp::R_X86_64_PC32)
2e30d253 3432 || gsym == NULL
0ffd9845 3433 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2e30d253 3434 {
75f2446e
ILT
3435 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3436 _("missing expected TLS relocation"));
3437 }
3438 else
3439 {
3440 this->skip_call_tls_get_addr_ = false;
3441 return false;
2e30d253 3442 }
2e30d253
ILT
3443 }
3444
0e804863
ILT
3445 if (view == NULL)
3446 return true;
3447
fc51264f 3448 const Sized_relobj_file<size, false>* object = relinfo->object;
7223e9ca
ILT
3449
3450 // Pick the value to use for symbols defined in the PLT.
fc51264f 3451 Symbol_value<size> symval;
96f2030e 3452 if (gsym != NULL
95a2c8d6 3453 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2e30d253 3454 {
19fec8c1 3455 symval.set_output_value(target->plt_address_for_global(gsym));
2e30d253
ILT
3456 psymval = &symval;
3457 }
7223e9ca
ILT
3458 else if (gsym == NULL && psymval->is_ifunc_symbol())
3459 {
fc51264f 3460 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7223e9ca
ILT
3461 if (object->local_has_plt_offset(r_sym))
3462 {
19fec8c1 3463 symval.set_output_value(target->plt_address_for_local(object, r_sym));
7223e9ca
ILT
3464 psymval = &symval;
3465 }
3466 }
2e30d253 3467
0ffd9845
ILT
3468 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3469
3470 // Get the GOT offset if needed.
96f2030e
ILT
3471 // The GOT pointer points to the end of the GOT section.
3472 // We need to subtract the size of the GOT section to get
3473 // the actual offset to use in the relocation.
0ffd9845 3474 bool have_got_offset = false;
c23dd342
L
3475 // Since the actual offset is always negative, we use signed int to
3476 // support 64-bit GOT relocations.
3477 int got_offset = 0;
0ffd9845
ILT
3478 switch (r_type)
3479 {
3480 case elfcpp::R_X86_64_GOT32:
3481 case elfcpp::R_X86_64_GOT64:
3482 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
3483 case elfcpp::R_X86_64_GOTPCREL64:
3484 if (gsym != NULL)
2e702c99
RM
3485 {
3486 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3487 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3488 }
0ffd9845 3489 else
2e702c99
RM
3490 {
3491 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3492 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3493 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3494 - target->got_size());
3495 }
0ffd9845
ILT
3496 have_got_offset = true;
3497 break;
3498
3499 default:
3500 break;
3501 }
2e30d253 3502
c34c98ed
CC
3503 typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
3504
2e30d253
ILT
3505 switch (r_type)
3506 {
3507 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
3508 case elfcpp::R_X86_64_GNU_VTINHERIT:
3509 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
3510 break;
3511
3512 case elfcpp::R_X86_64_64:
c34c98ed 3513 Reloc_funcs::rela64(view, object, psymval, addend);
2e30d253
ILT
3514 break;
3515
3516 case elfcpp::R_X86_64_PC64:
c34c98ed 3517 Reloc_funcs::pcrela64(view, object, psymval, addend,
2e702c99 3518 address);
2e30d253
ILT
3519 break;
3520
3521 case elfcpp::R_X86_64_32:
c34c98ed
CC
3522 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
3523 Reloc_funcs::CHECK_UNSIGNED);
2e30d253
ILT
3524 break;
3525
3526 case elfcpp::R_X86_64_32S:
c34c98ed
CC
3527 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
3528 Reloc_funcs::CHECK_SIGNED);
2e30d253
ILT
3529 break;
3530
3531 case elfcpp::R_X86_64_PC32:
f49fe902 3532 case elfcpp::R_X86_64_PC32_BND:
c34c98ed 3533 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
19ef3f4d 3534 address);
2e30d253
ILT
3535 break;
3536
3537 case elfcpp::R_X86_64_16:
c34c98ed 3538 Reloc_funcs::rela16(view, object, psymval, addend);
2e30d253
ILT
3539 break;
3540
3541 case elfcpp::R_X86_64_PC16:
c34c98ed 3542 Reloc_funcs::pcrela16(view, object, psymval, addend, address);
2e30d253
ILT
3543 break;
3544
3545 case elfcpp::R_X86_64_8:
c34c98ed 3546 Reloc_funcs::rela8(view, object, psymval, addend);
2e30d253
ILT
3547 break;
3548
3549 case elfcpp::R_X86_64_PC8:
c34c98ed 3550 Reloc_funcs::pcrela8(view, object, psymval, addend, address);
2e30d253
ILT
3551 break;
3552
3553 case elfcpp::R_X86_64_PLT32:
f49fe902 3554 case elfcpp::R_X86_64_PLT32_BND:
f389a824 3555 gold_assert(gsym == NULL
2e702c99 3556 || gsym->has_plt_offset()
99f8faca
ILT
3557 || gsym->final_value_is_known()
3558 || (gsym->is_defined()
3559 && !gsym->is_from_dynobj()
3560 && !gsym->is_preemptible()));
ee9e9e86
ILT
3561 // Note: while this code looks the same as for R_X86_64_PC32, it
3562 // behaves differently because psymval was set to point to
3563 // the PLT entry, rather than the symbol, in Scan::global().
c34c98ed 3564 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
19ef3f4d 3565 address);
2e30d253
ILT
3566 break;
3567
ee9e9e86
ILT
3568 case elfcpp::R_X86_64_PLTOFF64:
3569 {
2e702c99
RM
3570 gold_assert(gsym);
3571 gold_assert(gsym->has_plt_offset()
3572 || gsym->final_value_is_known());
fc51264f 3573 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
c23dd342
L
3574 // This is the address of GLOBAL_OFFSET_TABLE.
3575 got_address = target->got_plt_section()->address();
c34c98ed 3576 Reloc_funcs::rela64(view, object, psymval, addend - got_address);
ee9e9e86 3577 }
7849f6d8 3578 break;
ee9e9e86 3579
2e30d253 3580 case elfcpp::R_X86_64_GOT32:
0ffd9845 3581 gold_assert(have_got_offset);
c34c98ed 3582 Reloc_funcs::rela32(view, got_offset, addend);
2e30d253
ILT
3583 break;
3584
e822f2b1
ILT
3585 case elfcpp::R_X86_64_GOTPC32:
3586 {
2e702c99 3587 gold_assert(gsym);
fc51264f 3588 typename elfcpp::Elf_types<size>::Elf_Addr value;
96f2030e 3589 value = target->got_plt_section()->address();
19ef3f4d 3590 Reloc_funcs::pcrela32_check(view, value, addend, address);
e822f2b1
ILT
3591 }
3592 break;
3593
3594 case elfcpp::R_X86_64_GOT64:
fdc2f80f 3595 case elfcpp::R_X86_64_GOTPLT64:
e88ba8d5
L
3596 // R_X86_64_GOTPLT64 is obsolete and treated the the same as
3597 // GOT64.
0ffd9845 3598 gold_assert(have_got_offset);
c34c98ed 3599 Reloc_funcs::rela64(view, got_offset, addend);
e822f2b1
ILT
3600 break;
3601
3602 case elfcpp::R_X86_64_GOTPC64:
3603 {
2e702c99 3604 gold_assert(gsym);
fc51264f 3605 typename elfcpp::Elf_types<size>::Elf_Addr value;
96f2030e 3606 value = target->got_plt_section()->address();
c34c98ed 3607 Reloc_funcs::pcrela64(view, value, addend, address);
e822f2b1
ILT
3608 }
3609 break;
3610
2e30d253
ILT
3611 case elfcpp::R_X86_64_GOTOFF64:
3612 {
fc51264f 3613 typename elfcpp::Elf_types<size>::Elf_Addr value;
2e30d253 3614 value = (psymval->value(object, 0)
96f2030e 3615 - target->got_plt_section()->address());
c34c98ed 3616 Reloc_funcs::rela64(view, value, addend);
2e30d253
ILT
3617 }
3618 break;
3619
3620 case elfcpp::R_X86_64_GOTPCREL:
2891b491
L
3621 case elfcpp::R_X86_64_GOTPCRELX:
3622 case elfcpp::R_X86_64_REX_GOTPCRELX:
2e30d253 3623 {
1fa29f10
IT
3624 // Convert
3625 // mov foo@GOTPCREL(%rip), %reg
3626 // to lea foo(%rip), %reg.
3627 // if possible.
3628 if (rela.get_r_offset() >= 2
3629 && view[-2] == 0x8b
3630 && ((gsym == NULL && !psymval->is_ifunc_symbol())
3631 || (gsym != NULL
3632 && Target_x86_64<size>::can_convert_mov_to_lea(gsym))))
3633 {
3634 view[-2] = 0x8d;
19ef3f4d 3635 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
1fa29f10
IT
3636 }
3637 else
3638 {
3639 if (gsym != NULL)
3640 {
3641 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3642 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3643 }
3644 else
3645 {
3646 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3647 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3648 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3649 - target->got_size());
3650 }
3651 typename elfcpp::Elf_types<size>::Elf_Addr value;
3652 value = target->got_plt_section()->address() + got_offset;
19ef3f4d 3653 Reloc_funcs::pcrela32_check(view, value, addend, address);
1fa29f10 3654 }
2e30d253
ILT
3655 }
3656 break;
3657
e822f2b1
ILT
3658 case elfcpp::R_X86_64_GOTPCREL64:
3659 {
2e702c99
RM
3660 gold_assert(have_got_offset);
3661 typename elfcpp::Elf_types<size>::Elf_Addr value;
3662 value = target->got_plt_section()->address() + got_offset;
c34c98ed 3663 Reloc_funcs::pcrela64(view, value, addend, address);
e822f2b1
ILT
3664 }
3665 break;
3666
2e30d253
ILT
3667 case elfcpp::R_X86_64_COPY:
3668 case elfcpp::R_X86_64_GLOB_DAT:
3669 case elfcpp::R_X86_64_JUMP_SLOT:
3670 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 3671 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 3672 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 3673 case elfcpp::R_X86_64_TPOFF64:
2e30d253 3674 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 3675 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
3676 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3677 _("unexpected reloc %u in object file"),
3678 r_type);
2e30d253
ILT
3679 break;
3680
d61c17ea 3681 // These are initial tls relocs, which are expected when linking
56622147
ILT
3682 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3683 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 3684 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 3685 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
3686 case elfcpp::R_X86_64_DTPOFF32:
3687 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
3688 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3689 case elfcpp::R_X86_64_TPOFF32: // Local-exec
7bf1f802 3690 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2e702c99 3691 view, address, view_size);
2e30d253 3692 break;
2e30d253 3693
fdc2f80f
ILT
3694 case elfcpp::R_X86_64_SIZE32:
3695 case elfcpp::R_X86_64_SIZE64:
2e30d253 3696 default:
75f2446e
ILT
3697 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3698 _("unsupported reloc %u"),
3699 r_type);
2e30d253
ILT
3700 break;
3701 }
3702
c34c98ed
CC
3703 if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
3704 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3705 _("relocation overflow"));
3706
2e30d253
ILT
3707 return true;
3708}
3709
3710// Perform a TLS relocation.
3711
fc51264f 3712template<int size>
2e30d253 3713inline void
fc51264f
L
3714Target_x86_64<size>::Relocate::relocate_tls(
3715 const Relocate_info<size, false>* relinfo,
3716 Target_x86_64<size>* target,
3717 size_t relnum,
3718 const elfcpp::Rela<size, false>& rela,
3719 unsigned int r_type,
3720 const Sized_symbol<size>* gsym,
3721 const Symbol_value<size>* psymval,
3722 unsigned char* view,
3723 typename elfcpp::Elf_types<size>::Elf_Addr address,
3724 section_size_type view_size)
2e30d253 3725{
2e30d253 3726 Output_segment* tls_segment = relinfo->layout->tls_segment();
7bf1f802 3727
fc51264f 3728 const Sized_relobj_file<size, false>* object = relinfo->object;
6a41d30b 3729 const elfcpp::Elf_Xword addend = rela.get_r_addend();
fc51264f 3730 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
36171d64 3731 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2e30d253 3732
fc51264f 3733 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
2e30d253
ILT
3734
3735 const bool is_final = (gsym == NULL
b3705d2a 3736 ? !parameters->options().shared()
2e30d253 3737 : gsym->final_value_is_known());
36171d64 3738 tls::Tls_optimization optimized_type
fc51264f 3739 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
3740 switch (r_type)
3741 {
56622147 3742 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
36171d64
CC
3743 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3744 {
3745 // If this code sequence is used in a non-executable section,
3746 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3747 // on the assumption that it's being used by itself in a debug
3748 // section. Therefore, in the unlikely event that the code
3749 // sequence appears in a non-executable section, we simply
3750 // leave it unoptimized.
3751 optimized_type = tls::TLSOPT_NONE;
3752 }
e041f13d 3753 if (optimized_type == tls::TLSOPT_TO_LE)
2e30d253 3754 {
62855347
ILT
3755 if (tls_segment == NULL)
3756 {
191f1a2d
ILT
3757 gold_assert(parameters->errors()->error_count() > 0
3758 || issue_undefined_symbol_error(gsym));
62855347
ILT
3759 return;
3760 }
2e30d253 3761 this->tls_gd_to_le(relinfo, relnum, tls_segment,
72ec2876 3762 rela, r_type, value, view,
2e30d253
ILT
3763 view_size);
3764 break;
3765 }
7bf1f802 3766 else
2e702c99
RM
3767 {
3768 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3769 ? GOT_TYPE_TLS_OFFSET
3770 : GOT_TYPE_TLS_PAIR);
3771 unsigned int got_offset;
3772 if (gsym != NULL)
3773 {
3774 gold_assert(gsym->has_got_offset(got_type));
3775 got_offset = gsym->got_offset(got_type) - target->got_size();
3776 }
3777 else
3778 {
3779 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3780 gold_assert(object->local_has_got_offset(r_sym, got_type));
3781 got_offset = (object->local_got_offset(r_sym, got_type)
3782 - target->got_size());
3783 }
3784 if (optimized_type == tls::TLSOPT_TO_IE)
3785 {
3786 value = target->got_plt_section()->address() + got_offset;
d21f123b 3787 this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
2e702c99
RM
3788 value, view, address, view_size);
3789 break;
3790 }
3791 else if (optimized_type == tls::TLSOPT_NONE)
3792 {
3793 // Relocate the field with the offset of the pair of GOT
3794 // entries.
6a41d30b 3795 value = target->got_plt_section()->address() + got_offset;
2e702c99 3796 Relocate_functions<size, false>::pcrela32(view, value, addend,
fc51264f 3797 address);
2e702c99
RM
3798 break;
3799 }
3800 }
72ec2876 3801 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 3802 _("unsupported reloc %u"), r_type);
2e30d253
ILT
3803 break;
3804
c2b45e22
CC
3805 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3806 case elfcpp::R_X86_64_TLSDESC_CALL:
36171d64
CC
3807 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3808 {
3809 // See above comment for R_X86_64_TLSGD.
3810 optimized_type = tls::TLSOPT_NONE;
3811 }
c2b45e22
CC
3812 if (optimized_type == tls::TLSOPT_TO_LE)
3813 {
62855347
ILT
3814 if (tls_segment == NULL)
3815 {
191f1a2d
ILT
3816 gold_assert(parameters->errors()->error_count() > 0
3817 || issue_undefined_symbol_error(gsym));
62855347
ILT
3818 return;
3819 }
c2b45e22 3820 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2e702c99
RM
3821 rela, r_type, value, view,
3822 view_size);
c2b45e22
CC
3823 break;
3824 }
3825 else
2e702c99
RM
3826 {
3827 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3828 ? GOT_TYPE_TLS_OFFSET
3829 : GOT_TYPE_TLS_DESC);
3830 unsigned int got_offset = 0;
a8df5856
ILT
3831 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3832 && optimized_type == tls::TLSOPT_NONE)
3833 {
3834 // We created GOT entries in the .got.tlsdesc portion of
3835 // the .got.plt section, but the offset stored in the
3836 // symbol is the offset within .got.tlsdesc.
3837 got_offset = (target->got_size()
3838 + target->got_plt_section()->data_size());
3839 }
2e702c99
RM
3840 if (gsym != NULL)
3841 {
3842 gold_assert(gsym->has_got_offset(got_type));
3843 got_offset += gsym->got_offset(got_type) - target->got_size();
3844 }
3845 else
3846 {
3847 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3848 gold_assert(object->local_has_got_offset(r_sym, got_type));
3849 got_offset += (object->local_got_offset(r_sym, got_type)
a8df5856 3850 - target->got_size());
2e702c99
RM
3851 }
3852 if (optimized_type == tls::TLSOPT_TO_IE)
3853 {
2e702c99 3854 value = target->got_plt_section()->address() + got_offset;
d21f123b 3855 this->tls_desc_gd_to_ie(relinfo, relnum,
2e702c99
RM
3856 rela, r_type, value, view, address,
3857 view_size);
3858 break;
3859 }
3860 else if (optimized_type == tls::TLSOPT_NONE)
3861 {
3862 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3863 {
3864 // Relocate the field with the offset of the pair of GOT
3865 // entries.
3866 value = target->got_plt_section()->address() + got_offset;
3867 Relocate_functions<size, false>::pcrela32(view, value, addend,
fc51264f 3868 address);
2e702c99
RM
3869 }
3870 break;
3871 }
3872 }
c2b45e22
CC
3873 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3874 _("unsupported reloc %u"), r_type);
3875 break;
3876
56622147 3877 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
36171d64
CC
3878 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3879 {
3880 // See above comment for R_X86_64_TLSGD.
3881 optimized_type = tls::TLSOPT_NONE;
3882 }
e041f13d 3883 if (optimized_type == tls::TLSOPT_TO_LE)
2e702c99 3884 {
62855347
ILT
3885 if (tls_segment == NULL)
3886 {
191f1a2d
ILT
3887 gold_assert(parameters->errors()->error_count() > 0
3888 || issue_undefined_symbol_error(gsym));
62855347
ILT
3889 return;
3890 }
72ec2876
ILT
3891 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3892 value, view, view_size);
3893 break;
2e702c99 3894 }
7bf1f802 3895 else if (optimized_type == tls::TLSOPT_NONE)
2e702c99
RM
3896 {
3897 // Relocate the field with the offset of the GOT entry for
3898 // the module index.
3899 unsigned int got_offset;
3900 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
31d60480 3901 - target->got_size());
6a41d30b 3902 value = target->got_plt_section()->address() + got_offset;
2e702c99 3903 Relocate_functions<size, false>::pcrela32(view, value, addend,
fc51264f 3904 address);
2e702c99
RM
3905 break;
3906 }
72ec2876 3907 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 3908 _("unsupported reloc %u"), r_type);
2e30d253 3909 break;
0ffd9845
ILT
3910
3911 case elfcpp::R_X86_64_DTPOFF32:
36171d64
CC
3912 // This relocation type is used in debugging information.
3913 // In that case we need to not optimize the value. If the
3914 // section is not executable, then we assume we should not
3915 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3916 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3917 // R_X86_64_TLSLD.
3918 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3919 {
62855347
ILT
3920 if (tls_segment == NULL)
3921 {
191f1a2d
ILT
3922 gold_assert(parameters->errors()->error_count() > 0
3923 || issue_undefined_symbol_error(gsym));
62855347
ILT
3924 return;
3925 }
36171d64
CC
3926 value -= tls_segment->memsz();
3927 }
fc51264f 3928 Relocate_functions<size, false>::rela32(view, value, addend);
0ffd9845
ILT
3929 break;
3930
3931 case elfcpp::R_X86_64_DTPOFF64:
36171d64
CC
3932 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3933 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3934 {
62855347
ILT
3935 if (tls_segment == NULL)
3936 {
191f1a2d
ILT
3937 gold_assert(parameters->errors()->error_count() > 0
3938 || issue_undefined_symbol_error(gsym));
62855347
ILT
3939 return;
3940 }
36171d64
CC
3941 value -= tls_segment->memsz();
3942 }
fc51264f 3943 Relocate_functions<size, false>::rela64(view, value, addend);
0ffd9845 3944 break;
2e30d253 3945
56622147 3946 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
24dd5808
CC
3947 if (gsym != NULL
3948 && gsym->is_undefined()
3949 && parameters->options().output_is_executable())
65d92137
CC
3950 {
3951 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3952 NULL, rela,
3953 r_type, value, view,
3954 view_size);
3955 break;
3956 }
3957 else if (optimized_type == tls::TLSOPT_TO_LE)
56622147 3958 {
62855347
ILT
3959 if (tls_segment == NULL)
3960 {
191f1a2d
ILT
3961 gold_assert(parameters->errors()->error_count() > 0
3962 || issue_undefined_symbol_error(gsym));
62855347
ILT
3963 return;
3964 }
fc51264f
L
3965 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3966 tls_segment, rela,
3967 r_type, value, view,
3968 view_size);
56622147
ILT
3969 break;
3970 }
7bf1f802 3971 else if (optimized_type == tls::TLSOPT_NONE)
2e702c99
RM
3972 {
3973 // Relocate the field with the offset of the GOT entry for
3974 // the tp-relative offset of the symbol.
3975 unsigned int got_offset;
3976 if (gsym != NULL)
3977 {
3978 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3979 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3980 - target->got_size());
3981 }
3982 else
3983 {
3984 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3985 gold_assert(object->local_has_got_offset(r_sym,
3986 GOT_TYPE_TLS_OFFSET));
3987 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3988 - target->got_size());
3989 }
6a41d30b 3990 value = target->got_plt_section()->address() + got_offset;
2e702c99 3991 Relocate_functions<size, false>::pcrela32(view, value, addend,
fc51264f 3992 address);
2e702c99
RM
3993 break;
3994 }
56622147
ILT
3995 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3996 _("unsupported reloc type %u"),
3997 r_type);
3998 break;
0ffd9845 3999
56622147 4000 case elfcpp::R_X86_64_TPOFF32: // Local-exec
62855347
ILT
4001 if (tls_segment == NULL)
4002 {
191f1a2d
ILT
4003 gold_assert(parameters->errors()->error_count() > 0
4004 || issue_undefined_symbol_error(gsym));
62855347
ILT
4005 return;
4006 }
6a41d30b 4007 value -= tls_segment->memsz();
fc51264f 4008 Relocate_functions<size, false>::rela32(view, value, addend);
56622147 4009 break;
2e30d253 4010 }
2e30d253
ILT
4011}
4012
7bf1f802
ILT
4013// Do a relocation in which we convert a TLS General-Dynamic to an
4014// Initial-Exec.
4015
fc51264f 4016template<int size>
7bf1f802 4017inline void
fc51264f
L
4018Target_x86_64<size>::Relocate::tls_gd_to_ie(
4019 const Relocate_info<size, false>* relinfo,
4020 size_t relnum,
fc51264f
L
4021 const elfcpp::Rela<size, false>& rela,
4022 unsigned int,
4023 typename elfcpp::Elf_types<size>::Elf_Addr value,
4024 unsigned char* view,
4025 typename elfcpp::Elf_types<size>::Elf_Addr address,
4026 section_size_type view_size)
7bf1f802 4027{
41194d9f
L
4028 // For SIZE == 64:
4029 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4030 // .word 0x6666; rex64; call __tls_get_addr
4031 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4032 // For SIZE == 32:
4033 // leaq foo@tlsgd(%rip),%rdi;
4034 // .word 0x6666; rex64; call __tls_get_addr
4035 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
7bf1f802 4036
7bf1f802 4037 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
7bf1f802 4038 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2e702c99 4039 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
7bf1f802 4040
41194d9f
L
4041 if (size == 64)
4042 {
4043 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4044 -4);
4045 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4046 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4047 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4048 16);
4049 }
4050 else
4051 {
4052 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4053 -3);
4054 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4055 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4056 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4057 15);
4058 }
7bf1f802 4059
c2b45e22 4060 const elfcpp::Elf_Xword addend = rela.get_r_addend();
fc51264f
L
4061 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
4062 address);
7bf1f802
ILT
4063
4064 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4065 // We can skip it.
4066 this->skip_call_tls_get_addr_ = true;
4067}
4068
e041f13d 4069// Do a relocation in which we convert a TLS General-Dynamic to a
2e30d253
ILT
4070// Local-Exec.
4071
fc51264f 4072template<int size>
2e30d253 4073inline void
fc51264f
L
4074Target_x86_64<size>::Relocate::tls_gd_to_le(
4075 const Relocate_info<size, false>* relinfo,
4076 size_t relnum,
4077 Output_segment* tls_segment,
4078 const elfcpp::Rela<size, false>& rela,
4079 unsigned int,
4080 typename elfcpp::Elf_types<size>::Elf_Addr value,
4081 unsigned char* view,
4082 section_size_type view_size)
2e30d253 4083{
41194d9f
L
4084 // For SIZE == 64:
4085 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4086 // .word 0x6666; rex64; call __tls_get_addr
4087 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4088 // For SIZE == 32:
4089 // leaq foo@tlsgd(%rip),%rdi;
4090 // .word 0x6666; rex64; call __tls_get_addr
4091 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
2e30d253 4092
72ec2876 4093 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
72ec2876 4094 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
41194d9f
L
4095 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
4096
4097 if (size == 64)
4098 {
4099 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4100 -4);
4101 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4102 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4103 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4104 16);
4105 }
4106 else
4107 {
4108 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4109 -3);
4110 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4111 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
2e30d253 4112
41194d9f
L
4113 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4114 15);
4115 }
2e30d253 4116
6a41d30b 4117 value -= tls_segment->memsz();
fc51264f 4118 Relocate_functions<size, false>::rela32(view + 8, value, 0);
2e30d253
ILT
4119
4120 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4121 // We can skip it.
4122 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
4123}
4124
c2b45e22
CC
4125// Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4126
fc51264f 4127template<int size>
c2b45e22 4128inline void
fc51264f
L
4129Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
4130 const Relocate_info<size, false>* relinfo,
c2b45e22 4131 size_t relnum,
fc51264f 4132 const elfcpp::Rela<size, false>& rela,
c2b45e22 4133 unsigned int r_type,
fc51264f 4134 typename elfcpp::Elf_types<size>::Elf_Addr value,
c2b45e22 4135 unsigned char* view,
fc51264f 4136 typename elfcpp::Elf_types<size>::Elf_Addr address,
c2b45e22
CC
4137 section_size_type view_size)
4138{
4139 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4140 {
4141 // leaq foo@tlsdesc(%rip), %rax
4142 // ==> movq foo@gottpoff(%rip), %rax
4143 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4144 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4145 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2e702c99 4146 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
c2b45e22
CC
4147 view[-2] = 0x8b;
4148 const elfcpp::Elf_Xword addend = rela.get_r_addend();
fc51264f 4149 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
c2b45e22
CC
4150 }
4151 else
4152 {
4153 // call *foo@tlscall(%rax)
4154 // ==> nop; nop
4155 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4156 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4157 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2e702c99 4158 view[0] == 0xff && view[1] == 0x10);
c2b45e22
CC
4159 view[0] = 0x66;
4160 view[1] = 0x90;
4161 }
4162}
4163
4164// Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4165
fc51264f 4166template<int size>
c2b45e22 4167inline void
fc51264f
L
4168Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
4169 const Relocate_info<size, false>* relinfo,
c2b45e22
CC
4170 size_t relnum,
4171 Output_segment* tls_segment,
fc51264f 4172 const elfcpp::Rela<size, false>& rela,
c2b45e22 4173 unsigned int r_type,
fc51264f 4174 typename elfcpp::Elf_types<size>::Elf_Addr value,
c2b45e22
CC
4175 unsigned char* view,
4176 section_size_type view_size)
4177{
4178 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4179 {
4180 // leaq foo@tlsdesc(%rip), %rax
4181 // ==> movq foo@tpoff, %rax
4182 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4183 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4184 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2e702c99 4185 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
c2b45e22
CC
4186 view[-2] = 0xc7;
4187 view[-1] = 0xc0;
4188 value -= tls_segment->memsz();
fc51264f 4189 Relocate_functions<size, false>::rela32(view, value, 0);
c2b45e22
CC
4190 }
4191 else
4192 {
4193 // call *foo@tlscall(%rax)
4194 // ==> nop; nop
4195 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4196 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4197 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2e702c99 4198 view[0] == 0xff && view[1] == 0x10);
c2b45e22
CC
4199 view[0] = 0x66;
4200 view[1] = 0x90;
4201 }
4202}
4203
fc51264f 4204template<int size>
2e30d253 4205inline void
fc51264f
L
4206Target_x86_64<size>::Relocate::tls_ld_to_le(
4207 const Relocate_info<size, false>* relinfo,
4208 size_t relnum,
4209 Output_segment*,
4210 const elfcpp::Rela<size, false>& rela,
4211 unsigned int,
4212 typename elfcpp::Elf_types<size>::Elf_Addr,
4213 unsigned char* view,
4214 section_size_type view_size)
2e30d253 4215{
72ec2876 4216 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
d2cf1c6c 4217 // For SIZE == 64:
72ec2876
ILT
4218 // ... leq foo@dtpoff(%rax),%reg
4219 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
d2cf1c6c
L
4220 // For SIZE == 32:
4221 // ... leq foo@dtpoff(%rax),%reg
4222 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
2e30d253 4223
72ec2876
ILT
4224 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4225 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2e30d253 4226
72ec2876 4227 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2e702c99 4228 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
72ec2876
ILT
4229
4230 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
4231
d2cf1c6c
L
4232 if (size == 64)
4233 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4234 else
4235 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
72ec2876
ILT
4236
4237 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4238 // We can skip it.
4239 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
4240}
4241
56622147
ILT
4242// Do a relocation in which we convert a TLS Initial-Exec to a
4243// Local-Exec.
4244
fc51264f 4245template<int size>
56622147 4246inline void
fc51264f
L
4247Target_x86_64<size>::Relocate::tls_ie_to_le(
4248 const Relocate_info<size, false>* relinfo,
4249 size_t relnum,
4250 Output_segment* tls_segment,
4251 const elfcpp::Rela<size, false>& rela,
4252 unsigned int,
4253 typename elfcpp::Elf_types<size>::Elf_Addr value,
4254 unsigned char* view,
4255 section_size_type view_size)
56622147
ILT
4256{
4257 // We need to examine the opcodes to figure out which instruction we
4258 // are looking at.
4259
4260 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4261 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4262
4263 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4264 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4265
4266 unsigned char op1 = view[-3];
4267 unsigned char op2 = view[-2];
4268 unsigned char op3 = view[-1];
4269 unsigned char reg = op3 >> 3;
4270
4271 if (op2 == 0x8b)
4272 {
4273 // movq
4274 if (op1 == 0x4c)
2e702c99 4275 view[-3] = 0x49;
e749cab8
L
4276 else if (size == 32 && op1 == 0x44)
4277 view[-3] = 0x41;
56622147
ILT
4278 view[-2] = 0xc7;
4279 view[-1] = 0xc0 | reg;
4280 }
4281 else if (reg == 4)
4282 {
4283 // Special handling for %rsp.
4284 if (op1 == 0x4c)
2e702c99 4285 view[-3] = 0x49;
e749cab8
L
4286 else if (size == 32 && op1 == 0x44)
4287 view[-3] = 0x41;
56622147
ILT
4288 view[-2] = 0x81;
4289 view[-1] = 0xc0 | reg;
4290 }
4291 else
4292 {
4293 // addq
4294 if (op1 == 0x4c)
2e702c99 4295 view[-3] = 0x4d;
e749cab8
L
4296 else if (size == 32 && op1 == 0x44)
4297 view[-3] = 0x45;
56622147
ILT
4298 view[-2] = 0x8d;
4299 view[-1] = 0x80 | reg | (reg << 3);
4300 }
4301
65d92137
CC
4302 if (tls_segment != NULL)
4303 value -= tls_segment->memsz();
fc51264f 4304 Relocate_functions<size, false>::rela32(view, value, 0);
56622147
ILT
4305}
4306
2e30d253
ILT
4307// Relocate section data.
4308
fc51264f 4309template<int size>
2e30d253 4310void
fc51264f
L
4311Target_x86_64<size>::relocate_section(
4312 const Relocate_info<size, false>* relinfo,
364c7fa5
ILT
4313 unsigned int sh_type,
4314 const unsigned char* prelocs,
4315 size_t reloc_count,
4316 Output_section* output_section,
4317 bool needs_special_offset_handling,
4318 unsigned char* view,
fc51264f 4319 typename elfcpp::Elf_types<size>::Elf_Addr address,
364c7fa5
ILT
4320 section_size_type view_size,
4321 const Reloc_symbol_changes* reloc_symbol_changes)
2e30d253 4322{
4d625b70
CC
4323 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4324 Classify_reloc;
4325
2e30d253
ILT
4326 gold_assert(sh_type == elfcpp::SHT_RELA);
4327
4d625b70
CC
4328 gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
4329 gold::Default_comdat_behavior, Classify_reloc>(
2e30d253
ILT
4330 relinfo,
4331 this,
4332 prelocs,
4333 reloc_count,
730cdc88
ILT
4334 output_section,
4335 needs_special_offset_handling,
2e30d253
ILT
4336 view,
4337 address,
364c7fa5
ILT
4338 view_size,
4339 reloc_symbol_changes);
2e30d253
ILT
4340}
4341
94a3fc8b
CC
4342// Apply an incremental relocation. Incremental relocations always refer
4343// to global symbols.
4344
fc51264f 4345template<int size>
94a3fc8b 4346void
fc51264f
L
4347Target_x86_64<size>::apply_relocation(
4348 const Relocate_info<size, false>* relinfo,
4349 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
94a3fc8b 4350 unsigned int r_type,
fc51264f 4351 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
94a3fc8b
CC
4352 const Symbol* gsym,
4353 unsigned char* view,
fc51264f 4354 typename elfcpp::Elf_types<size>::Elf_Addr address,
94a3fc8b
CC
4355 section_size_type view_size)
4356{
fc51264f 4357 gold::apply_relocation<size, false, Target_x86_64<size>,
618d6666 4358 typename Target_x86_64<size>::Relocate>(
94a3fc8b
CC
4359 relinfo,
4360 this,
4361 r_offset,
4362 r_type,
4363 r_addend,
4364 gsym,
4365 view,
4366 address,
4367 view_size);
4368}
4369
4d625b70 4370// Scan the relocs during a relocatable link.
6a74a719 4371
fc51264f 4372template<int size>
4d625b70
CC
4373void
4374Target_x86_64<size>::scan_relocatable_relocs(
4375 Symbol_table* symtab,
4376 Layout* layout,
4377 Sized_relobj_file<size, false>* object,
4378 unsigned int data_shndx,
4379 unsigned int sh_type,
4380 const unsigned char* prelocs,
4381 size_t reloc_count,
4382 Output_section* output_section,
4383 bool needs_special_offset_handling,
4384 size_t local_symbol_count,
4385 const unsigned char* plocal_symbols,
4386 Relocatable_relocs* rr)
6a74a719 4387{
4d625b70
CC
4388 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4389 Classify_reloc;
4390 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
4391 Scan_relocatable_relocs;
6a74a719 4392
4d625b70 4393 gold_assert(sh_type == elfcpp::SHT_RELA);
6a74a719 4394
4d625b70
CC
4395 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
4396 symtab,
4397 layout,
4398 object,
4399 data_shndx,
4400 prelocs,
4401 reloc_count,
4402 output_section,
4403 needs_special_offset_handling,
4404 local_symbol_count,
4405 plocal_symbols,
4406 rr);
6a74a719
ILT
4407}
4408
4d625b70 4409// Scan the relocs for --emit-relocs.
6a74a719 4410
fc51264f 4411template<int size>
6a74a719 4412void
4d625b70 4413Target_x86_64<size>::emit_relocs_scan(
fc51264f
L
4414 Symbol_table* symtab,
4415 Layout* layout,
4416 Sized_relobj_file<size, false>* object,
4417 unsigned int data_shndx,
4418 unsigned int sh_type,
4419 const unsigned char* prelocs,
4420 size_t reloc_count,
4421 Output_section* output_section,
4422 bool needs_special_offset_handling,
4423 size_t local_symbol_count,
4d625b70 4424 const unsigned char* plocal_syms,
fc51264f 4425 Relocatable_relocs* rr)
6a74a719 4426{
4d625b70
CC
4427 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4428 Classify_reloc;
4429 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
4430 Emit_relocs_strategy;
6a74a719 4431
4d625b70 4432 gold_assert(sh_type == elfcpp::SHT_RELA);
6a74a719 4433
4d625b70 4434 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
6a74a719
ILT
4435 symtab,
4436 layout,
4437 object,
4438 data_shndx,
4439 prelocs,
4440 reloc_count,
4441 output_section,
4442 needs_special_offset_handling,
4443 local_symbol_count,
4d625b70 4444 plocal_syms,
6a74a719
ILT
4445 rr);
4446}
4447
4448// Relocate a section during a relocatable link.
4449
fc51264f 4450template<int size>
6a74a719 4451void
7404fe1b 4452Target_x86_64<size>::relocate_relocs(
fc51264f 4453 const Relocate_info<size, false>* relinfo,
6a74a719
ILT
4454 unsigned int sh_type,
4455 const unsigned char* prelocs,
4456 size_t reloc_count,
4457 Output_section* output_section,
62fe925a 4458 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
6a74a719 4459 unsigned char* view,
fc51264f 4460 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
6a74a719
ILT
4461 section_size_type view_size,
4462 unsigned char* reloc_view,
4463 section_size_type reloc_view_size)
4464{
4d625b70
CC
4465 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4466 Classify_reloc;
4467
6a74a719
ILT
4468 gold_assert(sh_type == elfcpp::SHT_RELA);
4469
4d625b70 4470 gold::relocate_relocs<size, false, Classify_reloc>(
6a74a719
ILT
4471 relinfo,
4472 prelocs,
4473 reloc_count,
4474 output_section,
4475 offset_in_output_section,
6a74a719
ILT
4476 view,
4477 view_address,
4478 view_size,
4479 reloc_view,
4480 reloc_view_size);
4481}
4482
4fb6c25d
ILT
4483// Return the value to use for a dynamic which requires special
4484// treatment. This is how we support equality comparisons of function
4485// pointers across shared library boundaries, as described in the
4486// processor specific ABI supplement.
4487
fc51264f 4488template<int size>
4fb6c25d 4489uint64_t
fc51264f 4490Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
4fb6c25d
ILT
4491{
4492 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
19fec8c1 4493 return this->plt_address_for_global(gsym);
4fb6c25d
ILT
4494}
4495
2e30d253
ILT
4496// Return a string used to fill a code section with nops to take up
4497// the specified length.
4498
fc51264f 4499template<int size>
2e30d253 4500std::string
fc51264f 4501Target_x86_64<size>::do_code_fill(section_size_type length) const
2e30d253
ILT
4502{
4503 if (length >= 16)
4504 {
4505 // Build a jmpq instruction to skip over the bytes.
4506 unsigned char jmp[5];
4507 jmp[0] = 0xe9;
04bf7072 4508 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2e30d253 4509 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2e702c99 4510 + std::string(length - 5, static_cast<char>(0x90)));
2e30d253
ILT
4511 }
4512
4513 // Nop sequences of various lengths.
76677ad0
CC
4514 const char nop1[1] = { '\x90' }; // nop
4515 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
4516 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4517 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
2e702c99 4518 '\x00'};
76677ad0
CC
4519 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4520 '\x00', '\x00' };
4521 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
2e702c99 4522 '\x44', '\x00', '\x00' };
76677ad0 4523 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
2e702c99 4524 '\x00', '\x00', '\x00',
76677ad0
CC
4525 '\x00' };
4526 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
2e702c99 4527 '\x00', '\x00', '\x00',
76677ad0
CC
4528 '\x00', '\x00' };
4529 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
2e702c99 4530 '\x84', '\x00', '\x00',
76677ad0
CC
4531 '\x00', '\x00', '\x00' };
4532 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
2e702c99 4533 '\x1f', '\x84', '\x00',
76677ad0
CC
4534 '\x00', '\x00', '\x00',
4535 '\x00' };
4536 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
2e702c99 4537 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
76677ad0
CC
4538 '\x00', '\x00', '\x00',
4539 '\x00', '\x00' };
4540 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
2e702c99 4541 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
76677ad0
CC
4542 '\x84', '\x00', '\x00',
4543 '\x00', '\x00', '\x00' };
4544 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
2e702c99 4545 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
76677ad0
CC
4546 '\x1f', '\x84', '\x00',
4547 '\x00', '\x00', '\x00',
2e702c99 4548 '\x00' };
76677ad0 4549 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
2e702c99 4550 '\x66', '\x66', '\x2e', // data16
76677ad0
CC
4551 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4552 '\x00', '\x00', '\x00',
2e702c99 4553 '\x00', '\x00' };
76677ad0 4554 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
2e702c99 4555 '\x66', '\x66', '\x66', // data16; data16
76677ad0
CC
4556 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4557 '\x84', '\x00', '\x00',
2e702c99 4558 '\x00', '\x00', '\x00' };
2e30d253
ILT
4559
4560 const char* nops[16] = {
4561 NULL,
4562 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
4563 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
4564 };
4565
4566 return std::string(nops[length], length);
4567}
4568
e291e7b9
ILT
4569// Return the addend to use for a target specific relocation. The
4570// only target specific relocation is R_X86_64_TLSDESC for a local
4571// symbol. We want to set the addend is the offset of the local
4572// symbol in the TLS segment.
4573
fc51264f 4574template<int size>
e291e7b9 4575uint64_t
fc51264f
L
4576Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
4577 uint64_t) const
e291e7b9
ILT
4578{
4579 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
4580 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4581 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4582 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
fc51264f 4583 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
e291e7b9
ILT
4584 gold_assert(psymval->is_tls_symbol());
4585 // The value of a TLS symbol is the offset in the TLS segment.
4586 return psymval->value(ti.object, 0);
4587}
4588
02d7cd44
ILT
4589// Return the value to use for the base of a DW_EH_PE_datarel offset
4590// in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4591// assembler can not write out the difference between two labels in
4592// different sections, so instead of using a pc-relative value they
4593// use an offset from the GOT.
4594
fc51264f 4595template<int size>
02d7cd44 4596uint64_t
fc51264f 4597Target_x86_64<size>::do_ehframe_datarel_base() const
02d7cd44
ILT
4598{
4599 gold_assert(this->global_offset_table_ != NULL);
4600 Symbol* sym = this->global_offset_table_;
fc51264f 4601 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
02d7cd44
ILT
4602 return ssym->value();
4603}
4604
364c7fa5 4605// FNOFFSET in section SHNDX in OBJECT is the start of a function
9b547ce6 4606// compiled with -fsplit-stack. The function calls non-split-stack
364c7fa5
ILT
4607// code. We have to change the function so that it always ensures
4608// that it has enough stack space to run some random function.
4609
4fc1b9d4
L
4610static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
4611static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
4612static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
4613
4614static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
4615static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
4616static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
4617
fc51264f 4618template<int size>
364c7fa5 4619void
fc51264f
L
4620Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
4621 section_offset_type fnoffset,
4622 section_size_type fnsize,
6e0813d3
CC
4623 const unsigned char*,
4624 size_t,
fc51264f
L
4625 unsigned char* view,
4626 section_size_type view_size,
4627 std::string* from,
4628 std::string* to) const
364c7fa5 4629{
4fc1b9d4
L
4630 const char* const cmp_insn = reinterpret_cast<const char*>
4631 (size == 32 ? cmp_insn_32 : cmp_insn_64);
4632 const char* const lea_r10_insn = reinterpret_cast<const char*>
4633 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
4634 const char* const lea_r11_insn = reinterpret_cast<const char*>
4635 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
4636
4637 const size_t cmp_insn_len =
4638 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
4639 const size_t lea_r10_insn_len =
4640 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
4641 const size_t lea_r11_insn_len =
4642 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
4643 const size_t nop_len = (size == 32 ? 7 : 8);
4644
364c7fa5
ILT
4645 // The function starts with a comparison of the stack pointer and a
4646 // field in the TCB. This is followed by a jump.
4647
4648 // cmp %fs:NN,%rsp
4fc1b9d4
L
4649 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
4650 && fnsize > nop_len + 1)
364c7fa5
ILT
4651 {
4652 // We will call __morestack if the carry flag is set after this
4653 // comparison. We turn the comparison into an stc instruction
4654 // and some nops.
4655 view[fnoffset] = '\xf9';
4fc1b9d4 4656 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
364c7fa5
ILT
4657 }
4658 // lea NN(%rsp),%r10
cbc999b9
ILT
4659 // lea NN(%rsp),%r11
4660 else if ((this->match_view(view, view_size, fnoffset,
4fc1b9d4 4661 lea_r10_insn, lea_r10_insn_len)
cbc999b9 4662 || this->match_view(view, view_size, fnoffset,
4fc1b9d4 4663 lea_r11_insn, lea_r11_insn_len))
364c7fa5
ILT
4664 && fnsize > 8)
4665 {
4666 // This is loading an offset from the stack pointer for a
4667 // comparison. The offset is negative, so we decrease the
4668 // offset by the amount of space we need for the stack. This
4669 // means we will avoid calling __morestack if there happens to
4670 // be plenty of space on the stack already.
4671 unsigned char* pval = view + fnoffset + 4;
4672 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
4673 val -= parameters->options().split_stack_adjust_size();
4674 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
4675 }
4676 else
4677 {
4678 if (!object->has_no_split_stack())
4679 object->error(_("failed to match split-stack sequence at "
4680 "section %u offset %0zx"),
ac33a407 4681 shndx, static_cast<size_t>(fnoffset));
364c7fa5
ILT
4682 return;
4683 }
4684
4685 // We have to change the function so that it calls
4686 // __morestack_non_split instead of __morestack. The former will
4687 // allocate additional stack space.
4688 *from = "__morestack";
4689 *to = "__morestack_non_split";
4690}
4691
2e702c99
RM
4692// The selector for x86_64 object files. Note this is never instantiated
4693// directly. It's only used in Target_selector_x86_64_nacl, below.
2e30d253 4694
fc51264f 4695template<int size>
36959681 4696class Target_selector_x86_64 : public Target_selector_freebsd
2e30d253
ILT
4697{
4698public:
4699 Target_selector_x86_64()
fc51264f 4700 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
2e702c99 4701 (size == 64
fc51264f 4702 ? "elf64-x86-64" : "elf32-x86-64"),
2e702c99 4703 (size == 64
fc51264f
L
4704 ? "elf64-x86-64-freebsd"
4705 : "elf32-x86-64-freebsd"),
4706 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
2e30d253
ILT
4707 { }
4708
4709 Target*
e96caa79 4710 do_instantiate_target()
fc51264f 4711 { return new Target_x86_64<size>(); }
36959681 4712
2e30d253
ILT
4713};
4714
2e702c99
RM
4715// NaCl variant. It uses different PLT contents.
4716
4717template<int size>
4718class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
4719{
4720 public:
4721 Output_data_plt_x86_64_nacl(Layout* layout,
4722 Output_data_got<64, false>* got,
57b2284c 4723 Output_data_got_plt_x86_64* got_plt,
2e702c99
RM
4724 Output_data_space* got_irelative)
4725 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4726 got, got_plt, got_irelative)
4727 { }
4728
4729 Output_data_plt_x86_64_nacl(Layout* layout,
4730 Output_data_got<64, false>* got,
57b2284c 4731 Output_data_got_plt_x86_64* got_plt,
2e702c99
RM
4732 Output_data_space* got_irelative,
4733 unsigned int plt_count)
4734 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4735 got, got_plt, got_irelative,
4736 plt_count)
4737 { }
4738
4739 protected:
4740 virtual unsigned int
4741 do_get_plt_entry_size() const
4742 { return plt_entry_size; }
4743
4744 virtual void
4745 do_add_eh_frame(Layout* layout)
4746 {
4747 layout->add_eh_frame_for_plt(this,
4748 this->plt_eh_frame_cie,
4749 this->plt_eh_frame_cie_size,
4750 plt_eh_frame_fde,
4751 plt_eh_frame_fde_size);
4752 }
4753
4754 virtual void
4755 do_fill_first_plt_entry(unsigned char* pov,
4756 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
4757 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
4758
4759 virtual unsigned int
4760 do_fill_plt_entry(unsigned char* pov,
4761 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4762 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4763 unsigned int got_offset,
4764 unsigned int plt_offset,
4765 unsigned int plt_index);
4766
4767 virtual void
4768 do_fill_tlsdesc_entry(unsigned char* pov,
4769 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4770 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4771 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
4772 unsigned int tlsdesc_got_offset,
4773 unsigned int plt_offset);
4774
4775 private:
4776 // The size of an entry in the PLT.
4777 static const int plt_entry_size = 64;
4778
4779 // The first entry in the PLT.
4780 static const unsigned char first_plt_entry[plt_entry_size];
4781
4782 // Other entries in the PLT for an executable.
4783 static const unsigned char plt_entry[plt_entry_size];
4784
4785 // The reserved TLSDESC entry in the PLT for an executable.
4786 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
4787
4788 // The .eh_frame unwind information for the PLT.
4789 static const int plt_eh_frame_fde_size = 32;
4790 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
4791};
4792
4793template<int size>
4794class Target_x86_64_nacl : public Target_x86_64<size>
4795{
4796 public:
4797 Target_x86_64_nacl()
4798 : Target_x86_64<size>(&x86_64_nacl_info)
4799 { }
4800
4801 virtual Output_data_plt_x86_64<size>*
4802 do_make_data_plt(Layout* layout,
4803 Output_data_got<64, false>* got,
57b2284c 4804 Output_data_got_plt_x86_64* got_plt,
2e702c99
RM
4805 Output_data_space* got_irelative)
4806 {
4807 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4808 got_irelative);
4809 }
4810
4811 virtual Output_data_plt_x86_64<size>*
4812 do_make_data_plt(Layout* layout,
4813 Output_data_got<64, false>* got,
57b2284c 4814 Output_data_got_plt_x86_64* got_plt,
2e702c99
RM
4815 Output_data_space* got_irelative,
4816 unsigned int plt_count)
4817 {
4818 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4819 got_irelative,
4820 plt_count);
4821 }
4822
93f8221c
RM
4823 virtual std::string
4824 do_code_fill(section_size_type length) const;
4825
2e702c99
RM
4826 private:
4827 static const Target::Target_info x86_64_nacl_info;
4828};
4829
4830template<>
4831const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
4832{
4833 64, // size
4834 false, // is_big_endian
4835 elfcpp::EM_X86_64, // machine_code
4836 false, // has_make_symbol
4837 false, // has_resolve
4838 true, // has_code_fill
4839 true, // is_default_stack_executable
4840 true, // can_icf_inline_merge_sections
4841 '\0', // wrap_char
4842 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4843 0x20000, // default_text_segment_address
4844 0x10000, // abi_pagesize (overridable by -z max-page-size)
4845 0x10000, // common_pagesize (overridable by -z common-page-size)
4846 true, // isolate_execinstr
4847 0x10000000, // rosegment_gap
4848 elfcpp::SHN_UNDEF, // small_common_shndx
4849 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4850 0, // small_common_section_flags
4851 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4852 NULL, // attributes_section
a67858e0 4853 NULL, // attributes_vendor
8d9743bd
MK
4854 "_start", // entry_symbol_name
4855 32, // hash_entry_size
2e702c99
RM
4856};
4857
4858template<>
4859const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
4860{
4861 32, // size
4862 false, // is_big_endian
4863 elfcpp::EM_X86_64, // machine_code
4864 false, // has_make_symbol
4865 false, // has_resolve
4866 true, // has_code_fill
4867 true, // is_default_stack_executable
4868 true, // can_icf_inline_merge_sections
4869 '\0', // wrap_char
4870 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
4871 0x20000, // default_text_segment_address
4872 0x10000, // abi_pagesize (overridable by -z max-page-size)
4873 0x10000, // common_pagesize (overridable by -z common-page-size)
4874 true, // isolate_execinstr
4875 0x10000000, // rosegment_gap
4876 elfcpp::SHN_UNDEF, // small_common_shndx
4877 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4878 0, // small_common_section_flags
4879 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4880 NULL, // attributes_section
a67858e0 4881 NULL, // attributes_vendor
8d9743bd
MK
4882 "_start", // entry_symbol_name
4883 32, // hash_entry_size
2e702c99
RM
4884};
4885
4886#define NACLMASK 0xe0 // 32-byte alignment mask.
4887
4888// The first entry in the PLT.
4889
4890template<int size>
4891const unsigned char
4892Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
4893{
4894 0xff, 0x35, // pushq contents of memory address
4895 0, 0, 0, 0, // replaced with address of .got + 8
4896 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
4897 0, 0, 0, 0, // replaced with address of .got + 16
4898 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4899 0x4d, 0x01, 0xfb, // add %r15, %r11
4900 0x41, 0xff, 0xe3, // jmpq *%r11
4901
4902 // 9-byte nop sequence to pad out to the next 32-byte boundary.
dd0845d7 4903 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
2e702c99
RM
4904
4905 // 32 bytes of nop to pad out to the standard size
4906 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4907 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4908 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4909 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4910 0x66, // excess data32 prefix
4911 0x90 // nop
4912};
4913
4914template<int size>
4915void
4916Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
4917 unsigned char* pov,
4918 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4919 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
4920{
4921 memcpy(pov, first_plt_entry, plt_entry_size);
4922 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4923 (got_address + 8
4924 - (plt_address + 2 + 4)));
4925 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
4926 (got_address + 16
4927 - (plt_address + 9 + 4)));
4928}
4929
4930// Subsequent entries in the PLT.
4931
4932template<int size>
4933const unsigned char
4934Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
4935{
4936 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
4937 0, 0, 0, 0, // replaced with address of symbol in .got
4938 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4939 0x4d, 0x01, 0xfb, // add %r15, %r11
4940 0x41, 0xff, 0xe3, // jmpq *%r11
4941
4942 // 15-byte nop sequence to pad out to the next 32-byte boundary.
4943 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4944 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4945
4946 // Lazy GOT entries point here (32-byte aligned).
4947 0x68, // pushq immediate
4948 0, 0, 0, 0, // replaced with index into relocation table
4949 0xe9, // jmp relative
4950 0, 0, 0, 0, // replaced with offset to start of .plt0
4951
4952 // 22 bytes of nop to pad out to the standard size.
4953 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4954 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4955 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
4956};
4957
4958template<int size>
4959unsigned int
4960Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
4961 unsigned char* pov,
4962 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4963 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4964 unsigned int got_offset,
4965 unsigned int plt_offset,
4966 unsigned int plt_index)
4967{
4968 memcpy(pov, plt_entry, plt_entry_size);
4969 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
4970 (got_address + got_offset
4971 - (plt_address + plt_offset
4972 + 3 + 4)));
4973
4974 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
4975 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
4976 - (plt_offset + 38 + 4));
4977
4978 return 32;
4979}
4980
4981// The reserved TLSDESC entry in the PLT.
4982
4983template<int size>
4984const unsigned char
4985Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
4986{
4987 0xff, 0x35, // pushq x(%rip)
4988 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
4989 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
4990 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
4991 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4992 0x4d, 0x01, 0xfb, // add %r15, %r11
4993 0x41, 0xff, 0xe3, // jmpq *%r11
4994
4995 // 41 bytes of nop to pad out to the standard size.
4996 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4997 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4998 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4999 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5000 0x66, 0x66, // excess data32 prefixes
5001 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5002};
5003
5004template<int size>
5005void
5006Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
5007 unsigned char* pov,
5008 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5009 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5010 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5011 unsigned int tlsdesc_got_offset,
5012 unsigned int plt_offset)
5013{
5014 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
5015 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5016 (got_address + 8
5017 - (plt_address + plt_offset
5018 + 2 + 4)));
5019 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5020 (got_base
5021 + tlsdesc_got_offset
5022 - (plt_address + plt_offset
5023 + 9 + 4)));
5024}
5025
5026// The .eh_frame unwind information for the PLT.
5027
5028template<int size>
5029const unsigned char
5030Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
5031{
5032 0, 0, 0, 0, // Replaced with offset to .plt.
5033 0, 0, 0, 0, // Replaced with size of .plt.
5034 0, // Augmentation size.
5035 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
5036 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
5037 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
5038 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
5039 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
5040 13, // Block length.
5041 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
5042 elfcpp::DW_OP_breg16, 0, // Push %rip.
5043 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
5044 elfcpp::DW_OP_and, // & (%rip & 0x3f).
5045 elfcpp::DW_OP_const1u, 37, // Push 0x25.
5046 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
5047 elfcpp::DW_OP_lit3, // Push 3.
5048 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
5049 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5050 elfcpp::DW_CFA_nop, // Align to 32 bytes.
5051 elfcpp::DW_CFA_nop
5052};
5053
93f8221c
RM
5054// Return a string used to fill a code section with nops.
5055// For NaCl, long NOPs are only valid if they do not cross
5056// bundle alignment boundaries, so keep it simple with one-byte NOPs.
5057template<int size>
5058std::string
5059Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
5060{
5061 return std::string(length, static_cast<char>(0x90));
5062}
5063
2e702c99
RM
5064// The selector for x86_64-nacl object files.
5065
5066template<int size>
5067class Target_selector_x86_64_nacl
5068 : public Target_selector_nacl<Target_selector_x86_64<size>,
5069 Target_x86_64_nacl<size> >
5070{
5071 public:
5072 Target_selector_x86_64_nacl()
5073 : Target_selector_nacl<Target_selector_x86_64<size>,
5074 Target_x86_64_nacl<size> >("x86-64",
5075 size == 64
5076 ? "elf64-x86-64-nacl"
5077 : "elf32-x86-64-nacl",
5078 size == 64
5079 ? "elf_x86_64_nacl"
5080 : "elf32_x86_64_nacl")
5081 { }
5082};
5083
5084Target_selector_x86_64_nacl<64> target_selector_x86_64;
5085Target_selector_x86_64_nacl<32> target_selector_x32;
2e30d253
ILT
5086
5087} // End anonymous namespace.
This page took 0.686073 seconds and 4 git commands to generate.