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