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