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