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