2011-07-15 Fawzi Mohamed <fawzi.mohamed@nokia.com>
[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
9446efde
ILT
837 // When using -z now, we can treat .got.plt as a relro section.
838 // Without -z now, it is modified after program startup by lazy
839 // PLT relocations.
840 bool is_got_plt_relro = parameters->options().now();
841 Output_section_order got_order = (is_got_plt_relro
842 ? ORDER_RELRO
843 : ORDER_RELRO_LAST);
844 Output_section_order got_plt_order = (is_got_plt_relro
845 ? ORDER_RELRO
846 : ORDER_NON_RELRO_FIRST);
847
2e30d253
ILT
848 this->got_ = new Output_data_got<64, false>();
849
82742395
ILT
850 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
851 (elfcpp::SHF_ALLOC
852 | elfcpp::SHF_WRITE),
9446efde 853 this->got_, got_order, true);
2e30d253 854
7d9e3d98 855 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
82742395
ILT
856 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
857 (elfcpp::SHF_ALLOC
858 | elfcpp::SHF_WRITE),
9446efde
ILT
859 this->got_plt_, got_plt_order,
860 is_got_plt_relro);
2e30d253
ILT
861
862 // The first three entries are reserved.
27bc2bce 863 this->got_plt_->set_current_data_size(3 * 8);
2e30d253 864
9446efde
ILT
865 if (!is_got_plt_relro)
866 {
867 // Those bytes can go into the relro segment.
868 layout->increase_relro(3 * 8);
869 }
1a2dff53 870
2e30d253 871 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
e785ec03
ILT
872 this->global_offset_table_ =
873 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
874 Symbol_table::PREDEFINED,
875 this->got_plt_,
876 0, 0, elfcpp::STT_OBJECT,
877 elfcpp::STB_LOCAL,
878 elfcpp::STV_HIDDEN, 0,
879 false, false);
a8df5856 880
67181c72
ILT
881 // If there are any IRELATIVE relocations, they get GOT entries
882 // in .got.plt after the jump slot entries.
883 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
884 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
885 (elfcpp::SHF_ALLOC
886 | elfcpp::SHF_WRITE),
887 this->got_irelative_,
9446efde 888 got_plt_order, is_got_plt_relro);
67181c72 889
a8df5856 890 // If there are any TLSDESC relocations, they get GOT entries in
67181c72 891 // .got.plt after the jump slot and IRELATIVE entries.
a8df5856
ILT
892 this->got_tlsdesc_ = new Output_data_got<64, false>();
893 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
894 (elfcpp::SHF_ALLOC
895 | elfcpp::SHF_WRITE),
22f0da72 896 this->got_tlsdesc_,
9446efde 897 got_plt_order, is_got_plt_relro);
2e30d253
ILT
898 }
899
900 return this->got_;
901}
902
903// Get the dynamic reloc section, creating it if necessary.
904
905Target_x86_64::Reloc_section*
0ffd9845 906Target_x86_64::rela_dyn_section(Layout* layout)
2e30d253 907{
0ffd9845 908 if (this->rela_dyn_ == NULL)
2e30d253
ILT
909 {
910 gold_assert(layout != NULL);
d98bc257 911 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2e30d253 912 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
913 elfcpp::SHF_ALLOC, this->rela_dyn_,
914 ORDER_DYNAMIC_RELOCS, false);
2e30d253 915 }
0ffd9845 916 return this->rela_dyn_;
2e30d253
ILT
917}
918
67181c72
ILT
919// Get the section to use for IRELATIVE relocs, creating it if
920// necessary. These go in .rela.dyn, but only after all other dynamic
921// relocations. They need to follow the other dynamic relocations so
922// that they can refer to global variables initialized by those
923// relocs.
924
925Target_x86_64::Reloc_section*
926Target_x86_64::rela_irelative_section(Layout* layout)
927{
928 if (this->rela_irelative_ == NULL)
929 {
930 // Make sure we have already created the dynamic reloc section.
931 this->rela_dyn_section(layout);
932 this->rela_irelative_ = new Reloc_section(false);
933 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
934 elfcpp::SHF_ALLOC, this->rela_irelative_,
935 ORDER_DYNAMIC_RELOCS, false);
936 gold_assert(this->rela_dyn_->output_section()
937 == this->rela_irelative_->output_section());
938 }
939 return this->rela_irelative_;
940}
941
4829d394 942// Initialize the PLT section.
2e30d253 943
4829d394 944void
67181c72 945Output_data_plt_x86_64::init(Layout* layout)
2e30d253 946{
d98bc257 947 this->rel_ = new Reloc_section(false);
2e30d253 948 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
22f0da72
ILT
949 elfcpp::SHF_ALLOC, this->rel_,
950 ORDER_DYNAMIC_PLT_RELOCS, false);
7223e9ca 951
07a60597
ILT
952 // Add unwind information if requested.
953 if (parameters->options().ld_generated_unwind_info())
954 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
955 plt_eh_frame_fde, plt_eh_frame_fde_size);
2e30d253
ILT
956}
957
958void
959Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
960{
b0481b0b 961 os->set_entsize(plt_entry_size);
2e30d253
ILT
962}
963
964// Add an entry to the PLT.
965
966void
67181c72
ILT
967Output_data_plt_x86_64::add_entry(Symbol_table* symtab, Layout* layout,
968 Symbol* gsym)
2e30d253
ILT
969{
970 gold_assert(!gsym->has_plt_offset());
971
4829d394
CC
972 unsigned int plt_index;
973 off_t plt_offset;
974 section_offset_type got_offset;
2e30d253 975
67181c72
ILT
976 unsigned int* pcount;
977 unsigned int offset;
978 unsigned int reserved;
979 Output_data_space* got;
980 if (gsym->type() == elfcpp::STT_GNU_IFUNC
981 && gsym->can_use_relative_reloc(false))
982 {
983 pcount = &this->irelative_count_;
984 offset = 0;
985 reserved = 0;
986 got = this->got_irelative_;
987 }
988 else
989 {
990 pcount = &this->count_;
991 offset = 1;
992 reserved = 3;
993 got = this->got_plt_;
994 }
995
4829d394
CC
996 if (!this->is_data_size_valid())
997 {
67181c72
ILT
998 // Note that when setting the PLT offset for a non-IRELATIVE
999 // entry we skip the initial reserved PLT entry.
1000 plt_index = *pcount + offset;
4829d394 1001 plt_offset = plt_index * plt_entry_size;
2e30d253 1002
67181c72 1003 ++*pcount;
2e30d253 1004
67181c72
ILT
1005 got_offset = (plt_index - offset + reserved) * 8;
1006 gold_assert(got_offset == got->current_data_size());
2e30d253 1007
4829d394
CC
1008 // Every PLT entry needs a GOT entry which points back to the PLT
1009 // entry (this will be changed by the dynamic linker, normally
1010 // lazily when the function is called).
67181c72 1011 got->set_current_data_size(got_offset + 8);
4829d394 1012 }
7223e9ca
ILT
1013 else
1014 {
67181c72
ILT
1015 // FIXME: This is probably not correct for IRELATIVE relocs.
1016
4829d394
CC
1017 // For incremental updates, find an available slot.
1018 plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0);
1019 if (plt_offset == -1)
e6455dfb
CC
1020 gold_fallback(_("out of patch space (PLT);"
1021 " relink with --incremental-full"));
4829d394
CC
1022
1023 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1024 // can be calculated from the PLT index, adjusting for the three
1025 // reserved entries at the beginning of the GOT.
1026 plt_index = plt_offset / plt_entry_size - 1;
67181c72 1027 got_offset = (plt_index - offset + reserved) * 8;
7223e9ca 1028 }
2e30d253 1029
4829d394
CC
1030 gsym->set_plt_offset(plt_offset);
1031
1032 // Every PLT entry needs a reloc.
67181c72 1033 this->add_relocation(symtab, layout, gsym, got_offset);
4829d394 1034
2e30d253
ILT
1035 // Note that we don't need to save the symbol. The contents of the
1036 // PLT are independent of which symbols are used. The symbols only
1037 // appear in the relocations.
1038}
1039
7223e9ca
ILT
1040// Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1041// the PLT offset.
1042
1043unsigned int
6fa2a40b 1044Output_data_plt_x86_64::add_local_ifunc_entry(
67181c72
ILT
1045 Symbol_table* symtab,
1046 Layout* layout,
6fa2a40b
CC
1047 Sized_relobj_file<64, false>* relobj,
1048 unsigned int local_sym_index)
7223e9ca 1049{
67181c72
ILT
1050 unsigned int plt_offset = this->irelative_count_ * plt_entry_size;
1051 ++this->irelative_count_;
7223e9ca 1052
67181c72 1053 section_offset_type got_offset = this->got_irelative_->current_data_size();
7223e9ca
ILT
1054
1055 // Every PLT entry needs a GOT entry which points back to the PLT
1056 // entry.
67181c72 1057 this->got_irelative_->set_current_data_size(got_offset + 8);
7223e9ca
ILT
1058
1059 // Every PLT entry needs a reloc.
67181c72
ILT
1060 Reloc_section* rela = this->rela_irelative(symtab, layout);
1061 rela->add_symbolless_local_addend(relobj, local_sym_index,
1062 elfcpp::R_X86_64_IRELATIVE,
1063 this->got_irelative_, got_offset, 0);
7223e9ca
ILT
1064
1065 return plt_offset;
1066}
1067
4829d394
CC
1068// Add the relocation for a PLT entry.
1069
1070void
67181c72
ILT
1071Output_data_plt_x86_64::add_relocation(Symbol_table* symtab, Layout* layout,
1072 Symbol* gsym, unsigned int got_offset)
4829d394
CC
1073{
1074 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1075 && gsym->can_use_relative_reloc(false))
67181c72
ILT
1076 {
1077 Reloc_section* rela = this->rela_irelative(symtab, layout);
1078 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1079 this->got_irelative_, got_offset, 0);
1080 }
4829d394
CC
1081 else
1082 {
1083 gsym->set_needs_dynsym_entry();
1084 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1085 got_offset, 0);
1086 }
1087}
1088
e291e7b9
ILT
1089// Return where the TLSDESC relocations should go, creating it if
1090// necessary. These follow the JUMP_SLOT relocations.
1091
1092Output_data_plt_x86_64::Reloc_section*
1093Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
1094{
1095 if (this->tlsdesc_rel_ == NULL)
1096 {
1097 this->tlsdesc_rel_ = new Reloc_section(false);
1098 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1099 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
22f0da72 1100 ORDER_DYNAMIC_PLT_RELOCS, false);
67181c72
ILT
1101 gold_assert(this->tlsdesc_rel_->output_section()
1102 == this->rel_->output_section());
e291e7b9
ILT
1103 }
1104 return this->tlsdesc_rel_;
1105}
1106
67181c72
ILT
1107// Return where the IRELATIVE relocations should go in the PLT. These
1108// follow the JUMP_SLOT and the TLSDESC relocations.
1109
1110Output_data_plt_x86_64::Reloc_section*
1111Output_data_plt_x86_64::rela_irelative(Symbol_table* symtab, Layout* layout)
1112{
1113 if (this->irelative_rel_ == NULL)
1114 {
1115 // Make sure we have a place for the TLSDESC relocations, in
1116 // case we see any later on.
1117 this->rela_tlsdesc(layout);
1118 this->irelative_rel_ = new Reloc_section(false);
1119 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1120 elfcpp::SHF_ALLOC, this->irelative_rel_,
1121 ORDER_DYNAMIC_PLT_RELOCS, false);
1122 gold_assert(this->irelative_rel_->output_section()
1123 == this->rel_->output_section());
1124
1125 if (parameters->doing_static_link())
1126 {
1127 // A statically linked executable will only have a .rela.plt
1128 // section to hold R_X86_64_IRELATIVE relocs for
1129 // STT_GNU_IFUNC symbols. The library will use these
1130 // symbols to locate the IRELATIVE relocs at program startup
1131 // time.
1132 symtab->define_in_output_data("__rela_iplt_start", NULL,
1133 Symbol_table::PREDEFINED,
1134 this->irelative_rel_, 0, 0,
1135 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1136 elfcpp::STV_HIDDEN, 0, false, true);
1137 symtab->define_in_output_data("__rela_iplt_end", NULL,
1138 Symbol_table::PREDEFINED,
1139 this->irelative_rel_, 0, 0,
1140 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1141 elfcpp::STV_HIDDEN, 0, true, true);
1142 }
1143 }
1144 return this->irelative_rel_;
1145}
1146
1147// Return the PLT address to use for a global symbol.
1148
1149uint64_t
1150Output_data_plt_x86_64::address_for_global(const Symbol* gsym)
1151{
1152 uint64_t offset = 0;
1153 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1154 && gsym->can_use_relative_reloc(false))
1155 offset = (this->count_ + 1) * plt_entry_size;
1156 return this->address() + offset;
1157}
1158
1159// Return the PLT address to use for a local symbol. These are always
1160// IRELATIVE relocs.
1161
1162uint64_t
1163Output_data_plt_x86_64::address_for_local(const Relobj*, unsigned int)
1164{
1165 return this->address() + (this->count_ + 1) * plt_entry_size;
1166}
1167
c2b45e22
CC
1168// Set the final size.
1169void
1170Output_data_plt_x86_64::set_final_data_size()
1171{
67181c72 1172 unsigned int count = this->count_ + this->irelative_count_;
c2b45e22
CC
1173 if (this->has_tlsdesc_entry())
1174 ++count;
1175 this->set_data_size((count + 1) * plt_entry_size);
1176}
1177
2e30d253
ILT
1178// The first entry in the PLT for an executable.
1179
07a60597 1180const unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
2e30d253
ILT
1181{
1182 // From AMD64 ABI Draft 0.98, page 76
1183 0xff, 0x35, // pushq contents of memory address
2e30d253 1184 0, 0, 0, 0, // replaced with address of .got + 8
78d911fd
ILT
1185 0xff, 0x25, // jmp indirect
1186 0, 0, 0, 0, // replaced with address of .got + 16
2e30d253
ILT
1187 0x90, 0x90, 0x90, 0x90 // noop (x4)
1188};
1189
1190// Subsequent entries in the PLT for an executable.
1191
07a60597 1192const unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
2e30d253
ILT
1193{
1194 // From AMD64 ABI Draft 0.98, page 76
1195 0xff, 0x25, // jmpq indirect
1196 0, 0, 0, 0, // replaced with address of symbol in .got
1197 0x68, // pushq immediate
1198 0, 0, 0, 0, // replaced with offset into relocation table
1199 0xe9, // jmpq relative
1200 0, 0, 0, 0 // replaced with offset to start of .plt
1201};
1202
c2b45e22
CC
1203// The reserved TLSDESC entry in the PLT for an executable.
1204
07a60597 1205const unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
c2b45e22
CC
1206{
1207 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1208 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1209 0xff, 0x35, // pushq x(%rip)
1210 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1211 0xff, 0x25, // jmpq *y(%rip)
1212 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1213 0x0f, 0x1f, // nop
1214 0x40, 0
1215};
1216
07a60597
ILT
1217// The .eh_frame unwind information for the PLT.
1218
1219const unsigned char
1220Output_data_plt_x86_64::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1221{
1222 1, // CIE version.
1223 'z', // Augmentation: augmentation size included.
1224 'R', // Augmentation: FDE encoding included.
1225 '\0', // End of augmentation string.
1226 1, // Code alignment factor.
1227 0x78, // Data alignment factor.
1228 16, // Return address column.
1229 1, // Augmentation size.
1230 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1231 | elfcpp::DW_EH_PE_sdata4),
1232 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1233 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1234 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1235 elfcpp::DW_CFA_nop
1236};
1237
1238const unsigned char
1239Output_data_plt_x86_64::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1240{
1241 0, 0, 0, 0, // Replaced with offset to .plt.
1242 0, 0, 0, 0, // Replaced with size of .plt.
1243 0, // Augmentation size.
1244 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
1245 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1246 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
1247 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1248 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1249 11, // Block length.
1250 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
1251 elfcpp::DW_OP_breg16, 0, // Push %rip.
1252 elfcpp::DW_OP_lit15, // Push 0xf.
1253 elfcpp::DW_OP_and, // & (%rip & 0xf).
1254 elfcpp::DW_OP_lit11, // Push 0xb.
1255 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
1256 elfcpp::DW_OP_lit3, // Push 3.
1257 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
1258 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1259 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1260 elfcpp::DW_CFA_nop,
1261 elfcpp::DW_CFA_nop,
1262 elfcpp::DW_CFA_nop
1263};
1264
2e30d253
ILT
1265// Write out the PLT. This uses the hand-coded instructions above,
1266// and adjusts them as needed. This is specified by the AMD64 ABI.
1267
1268void
1269Output_data_plt_x86_64::do_write(Output_file* of)
1270{
2ea97941 1271 const off_t offset = this->offset();
fe8718a4
ILT
1272 const section_size_type oview_size =
1273 convert_to_section_size_type(this->data_size());
2ea97941 1274 unsigned char* const oview = of->get_output_view(offset, oview_size);
2e30d253
ILT
1275
1276 const off_t got_file_offset = this->got_plt_->offset();
67181c72
ILT
1277 gold_assert(parameters->incremental_update()
1278 || (got_file_offset + this->got_plt_->data_size()
1279 == this->got_irelative_->offset()));
fe8718a4 1280 const section_size_type got_size =
67181c72
ILT
1281 convert_to_section_size_type(this->got_plt_->data_size()
1282 + this->got_irelative_->data_size());
2e30d253
ILT
1283 unsigned char* const got_view = of->get_output_view(got_file_offset,
1284 got_size);
1285
1286 unsigned char* pov = oview;
1287
c2b45e22 1288 // The base address of the .plt section.
a984ee1d 1289 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
c2b45e22 1290 // The base address of the .got section.
a984ee1d 1291 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
c2b45e22
CC
1292 // The base address of the PLT portion of the .got section,
1293 // which is where the GOT pointer will point, and where the
1294 // three reserved GOT entries are located.
a984ee1d 1295 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
2e30d253
ILT
1296
1297 memcpy(pov, first_plt_entry, plt_entry_size);
78d911fd 1298 // We do a jmp relative to the PC at the end of this instruction.
a984ee1d
ILT
1299 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1300 (got_address + 8
1301 - (plt_address + 6)));
1302 elfcpp::Swap<32, false>::writeval(pov + 8,
1303 (got_address + 16
1304 - (plt_address + 12)));
2e30d253
ILT
1305 pov += plt_entry_size;
1306
1307 unsigned char* got_pov = got_view;
1308
1309 memset(got_pov, 0, 24);
1310 got_pov += 24;
1311
1312 unsigned int plt_offset = plt_entry_size;
1313 unsigned int got_offset = 24;
67181c72 1314 const unsigned int count = this->count_ + this->irelative_count_;
2e30d253
ILT
1315 for (unsigned int plt_index = 0;
1316 plt_index < count;
1317 ++plt_index,
1318 pov += plt_entry_size,
1319 got_pov += 8,
1320 plt_offset += plt_entry_size,
1321 got_offset += 8)
1322 {
1323 // Set and adjust the PLT entry itself.
1324 memcpy(pov, plt_entry, plt_entry_size);
78d911fd
ILT
1325 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1326 (got_address + got_offset
1327 - (plt_address + plt_offset
1328 + 6)));
2e30d253
ILT
1329
1330 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1331 elfcpp::Swap<32, false>::writeval(pov + 12,
1332 - (plt_offset + plt_entry_size));
1333
1334 // Set the entry in the GOT.
1335 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1336 }
1337
c2b45e22
CC
1338 if (this->has_tlsdesc_entry())
1339 {
1340 // Set and adjust the reserved TLSDESC PLT entry.
1341 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1342 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1343 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1344 (got_address + 8
1345 - (plt_address + plt_offset
1346 + 6)));
1347 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1348 (got_base
1349 + tlsdesc_got_offset
1350 - (plt_address + plt_offset
1351 + 12)));
1352 pov += plt_entry_size;
1353 }
1354
fe8718a4
ILT
1355 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1356 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2e30d253 1357
2ea97941 1358 of->write_output_view(offset, oview_size, oview);
2e30d253
ILT
1359 of->write_output_view(got_file_offset, got_size, got_view);
1360}
1361
c2b45e22 1362// Create the PLT section.
2e30d253
ILT
1363
1364void
c2b45e22 1365Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
2e30d253 1366{
2e30d253
ILT
1367 if (this->plt_ == NULL)
1368 {
1369 // Create the GOT sections first.
1370 this->got_section(symtab, layout);
1371
67181c72
ILT
1372 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
1373 this->got_plt_,
1374 this->got_irelative_);
2e30d253
ILT
1375 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1376 (elfcpp::SHF_ALLOC
1377 | elfcpp::SHF_EXECINSTR),
22f0da72 1378 this->plt_, ORDER_PLT, false);
7223e9ca
ILT
1379
1380 // Make the sh_info field of .rela.plt point to .plt.
1381 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1382 rela_plt_os->set_info_section(this->plt_->output_section());
2e30d253 1383 }
c2b45e22
CC
1384}
1385
e291e7b9
ILT
1386// Return the section for TLSDESC relocations.
1387
1388Target_x86_64::Reloc_section*
1389Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1390{
1391 return this->plt_section()->rela_tlsdesc(layout);
1392}
1393
c2b45e22
CC
1394// Create a PLT entry for a global symbol.
1395
1396void
1397Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
1398 Symbol* gsym)
1399{
1400 if (gsym->has_plt_offset())
1401 return;
1402
1403 if (this->plt_ == NULL)
1404 this->make_plt_section(symtab, layout);
2e30d253 1405
67181c72 1406 this->plt_->add_entry(symtab, layout, gsym);
2e30d253
ILT
1407}
1408
7223e9ca
ILT
1409// Make a PLT entry for a local STT_GNU_IFUNC symbol.
1410
1411void
1412Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
6fa2a40b 1413 Sized_relobj_file<64, false>* relobj,
7223e9ca
ILT
1414 unsigned int local_sym_index)
1415{
1416 if (relobj->local_has_plt_offset(local_sym_index))
1417 return;
1418 if (this->plt_ == NULL)
1419 this->make_plt_section(symtab, layout);
67181c72
ILT
1420 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1421 relobj,
7223e9ca
ILT
1422 local_sym_index);
1423 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1424}
1425
0e70b911
CC
1426// Return the number of entries in the PLT.
1427
1428unsigned int
1429Target_x86_64::plt_entry_count() const
1430{
1431 if (this->plt_ == NULL)
1432 return 0;
1433 return this->plt_->entry_count();
1434}
1435
1436// Return the offset of the first non-reserved PLT entry.
1437
1438unsigned int
1439Target_x86_64::first_plt_entry_offset() const
1440{
1441 return Output_data_plt_x86_64::first_plt_entry_offset();
1442}
1443
1444// Return the size of each PLT entry.
1445
1446unsigned int
1447Target_x86_64::plt_entry_size() const
1448{
1449 return Output_data_plt_x86_64::get_plt_entry_size();
1450}
1451
4829d394
CC
1452// Create the GOT and PLT sections for an incremental update.
1453
1454Output_data_got<64, false>*
1455Target_x86_64::init_got_plt_for_update(Symbol_table* symtab,
1456 Layout* layout,
1457 unsigned int got_count,
1458 unsigned int plt_count)
1459{
1460 gold_assert(this->got_ == NULL);
1461
1462 this->got_ = new Output_data_got<64, false>(got_count * 8);
1463 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1464 (elfcpp::SHF_ALLOC
1465 | elfcpp::SHF_WRITE),
1466 this->got_, ORDER_RELRO_LAST,
1467 true);
1468
1469 // Add the three reserved entries.
1470 this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT");
1471 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1472 (elfcpp::SHF_ALLOC
1473 | elfcpp::SHF_WRITE),
1474 this->got_plt_, ORDER_NON_RELRO_FIRST,
1475 false);
1476
1477 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1478 this->global_offset_table_ =
1479 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1480 Symbol_table::PREDEFINED,
1481 this->got_plt_,
1482 0, 0, elfcpp::STT_OBJECT,
1483 elfcpp::STB_LOCAL,
1484 elfcpp::STV_HIDDEN, 0,
1485 false, false);
1486
1487 // If there are any TLSDESC relocations, they get GOT entries in
1488 // .got.plt after the jump slot entries.
1489 // FIXME: Get the count for TLSDESC entries.
1490 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1491 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1492 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1493 this->got_tlsdesc_,
1494 ORDER_NON_RELRO_FIRST, false);
1495
67181c72
ILT
1496 // If there are any IRELATIVE relocations, they get GOT entries in
1497 // .got.plt after the jump slot and TLSDESC entries.
1498 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1499 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1500 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1501 this->got_irelative_,
1502 ORDER_NON_RELRO_FIRST, false);
1503
4829d394 1504 // Create the PLT section.
67181c72
ILT
1505 this->plt_ = new Output_data_plt_x86_64(layout, this->got_, this->got_plt_,
1506 this->got_irelative_, plt_count);
4829d394
CC
1507 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1508 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1509 this->plt_, ORDER_PLT, false);
1510
1511 // Make the sh_info field of .rela.plt point to .plt.
1512 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1513 rela_plt_os->set_info_section(this->plt_->output_section());
1514
6fa2a40b
CC
1515 // Create the rela_dyn section.
1516 this->rela_dyn_section(layout);
1517
4829d394
CC
1518 return this->got_;
1519}
1520
6fa2a40b
CC
1521// Reserve a GOT entry for a local symbol, and regenerate any
1522// necessary dynamic relocations.
1523
1524void
1525Target_x86_64::reserve_local_got_entry(
1526 unsigned int got_index,
1527 Sized_relobj<64, false>* obj,
1528 unsigned int r_sym,
1529 unsigned int got_type)
1530{
1531 unsigned int got_offset = got_index * 8;
1532 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1533
1534 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1535 switch (got_type)
1536 {
1537 case GOT_TYPE_STANDARD:
1538 if (parameters->options().output_is_position_independent())
1539 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1540 this->got_, got_offset, 0);
1541 break;
1542 case GOT_TYPE_TLS_OFFSET:
1543 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1544 this->got_, got_offset, 0);
1545 break;
1546 case GOT_TYPE_TLS_PAIR:
1547 this->got_->reserve_slot(got_index + 1);
1548 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1549 this->got_, got_offset, 0);
1550 break;
1551 case GOT_TYPE_TLS_DESC:
1552 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1553 // this->got_->reserve_slot(got_index + 1);
1554 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1555 // this->got_, got_offset, 0);
1556 break;
1557 default:
1558 gold_unreachable();
1559 }
1560}
1561
1562// Reserve a GOT entry for a global symbol, and regenerate any
1563// necessary dynamic relocations.
1564
1565void
1566Target_x86_64::reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
1567 unsigned int got_type)
1568{
1569 unsigned int got_offset = got_index * 8;
1570 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1571
1572 this->got_->reserve_global(got_index, gsym, got_type);
1573 switch (got_type)
1574 {
1575 case GOT_TYPE_STANDARD:
1576 if (!gsym->final_value_is_known())
1577 {
1578 if (gsym->is_from_dynobj()
1579 || gsym->is_undefined()
1580 || gsym->is_preemptible()
1581 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1582 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1583 this->got_, got_offset, 0);
1584 else
1585 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1586 this->got_, got_offset, 0);
1587 }
1588 break;
1589 case GOT_TYPE_TLS_OFFSET:
1590 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1591 this->got_, got_offset, 0);
1592 break;
1593 case GOT_TYPE_TLS_PAIR:
1594 this->got_->reserve_slot(got_index + 1);
1595 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1596 this->got_, got_offset, 0);
1597 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1598 this->got_, got_offset + 8, 0);
1599 break;
1600 case GOT_TYPE_TLS_DESC:
1601 this->got_->reserve_slot(got_index + 1);
1602 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1603 this->got_, got_offset, 0);
1604 break;
1605 default:
1606 gold_unreachable();
1607 }
1608}
1609
4829d394
CC
1610// Register an existing PLT entry for a global symbol.
1611
1612void
67181c72
ILT
1613Target_x86_64::register_global_plt_entry(Symbol_table* symtab,
1614 Layout* layout,
1615 unsigned int plt_index,
4829d394
CC
1616 Symbol* gsym)
1617{
1618 gold_assert(this->plt_ != NULL);
1619 gold_assert(!gsym->has_plt_offset());
1620
1621 this->plt_->reserve_slot(plt_index);
1622
1623 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1624
1625 unsigned int got_offset = (plt_index + 3) * 8;
67181c72 1626 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
4829d394
CC
1627}
1628
26d3c67d
CC
1629// Force a COPY relocation for a given symbol.
1630
1631void
1632Target_x86_64::emit_copy_reloc(
1633 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
1634{
1635 this->copy_relocs_.emit_copy_reloc(symtab,
1636 symtab->get_sized_symbol<64>(sym),
1637 os,
1638 offset,
1639 this->rela_dyn_section(NULL));
1640}
1641
9fa33bee 1642// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
edfbb029
CC
1643
1644void
1645Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1646{
1647 if (this->tls_base_symbol_defined_)
1648 return;
1649
1650 Output_segment* tls_segment = layout->tls_segment();
1651 if (tls_segment != NULL)
1652 {
183fd0e3 1653 bool is_exec = parameters->options().output_is_executable();
edfbb029 1654 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
99fff23b 1655 Symbol_table::PREDEFINED,
edfbb029
CC
1656 tls_segment, 0, 0,
1657 elfcpp::STT_TLS,
1658 elfcpp::STB_LOCAL,
1659 elfcpp::STV_HIDDEN, 0,
183fd0e3
AO
1660 (is_exec
1661 ? Symbol::SEGMENT_END
1662 : Symbol::SEGMENT_START),
1663 true);
edfbb029
CC
1664 }
1665 this->tls_base_symbol_defined_ = true;
1666}
1667
c2b45e22
CC
1668// Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1669
1670void
1671Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1672 Layout* layout)
1673{
1674 if (this->plt_ == NULL)
1675 this->make_plt_section(symtab, layout);
1676
1677 if (!this->plt_->has_tlsdesc_entry())
1678 {
1679 // Allocate the TLSDESC_GOT entry.
1680 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1681 unsigned int got_offset = got->add_constant(0);
1682
1683 // Allocate the TLSDESC_PLT entry.
1684 this->plt_->reserve_tlsdesc_entry(got_offset);
1685 }
1686}
1687
31d60480
ILT
1688// Create a GOT entry for the TLS module index.
1689
1690unsigned int
1691Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
6fa2a40b 1692 Sized_relobj_file<64, false>* object)
31d60480
ILT
1693{
1694 if (this->got_mod_index_offset_ == -1U)
1695 {
1696 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1697 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1698 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1699 unsigned int got_offset = got->add_constant(0);
1700 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1701 got_offset, 0);
009a67a2 1702 got->add_constant(0);
31d60480
ILT
1703 this->got_mod_index_offset_ = got_offset;
1704 }
1705 return this->got_mod_index_offset_;
1706}
1707
2e30d253
ILT
1708// Optimize the TLS relocation type based on what we know about the
1709// symbol. IS_FINAL is true if the final address of this symbol is
1710// known at link time.
1711
e041f13d 1712tls::Tls_optimization
2e30d253
ILT
1713Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1714{
2e30d253
ILT
1715 // If we are generating a shared library, then we can't do anything
1716 // in the linker.
8851ecca 1717 if (parameters->options().shared())
e041f13d 1718 return tls::TLSOPT_NONE;
2e30d253
ILT
1719
1720 switch (r_type)
1721 {
1722 case elfcpp::R_X86_64_TLSGD:
e041f13d
ILT
1723 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1724 case elfcpp::R_X86_64_TLSDESC_CALL:
1725 // These are General-Dynamic which permits fully general TLS
2e30d253
ILT
1726 // access. Since we know that we are generating an executable,
1727 // we can convert this to Initial-Exec. If we also know that
1728 // this is a local symbol, we can further switch to Local-Exec.
1729 if (is_final)
e041f13d
ILT
1730 return tls::TLSOPT_TO_LE;
1731 return tls::TLSOPT_TO_IE;
2e30d253 1732
d61c17ea 1733 case elfcpp::R_X86_64_TLSLD:
2e30d253
ILT
1734 // This is Local-Dynamic, which refers to a local symbol in the
1735 // dynamic TLS block. Since we know that we generating an
1736 // executable, we can switch to Local-Exec.
e041f13d 1737 return tls::TLSOPT_TO_LE;
2e30d253 1738
0ffd9845 1739 case elfcpp::R_X86_64_DTPOFF32:
0ffd9845
ILT
1740 case elfcpp::R_X86_64_DTPOFF64:
1741 // Another Local-Dynamic reloc.
e041f13d 1742 return tls::TLSOPT_TO_LE;
0ffd9845 1743
d61c17ea 1744 case elfcpp::R_X86_64_GOTTPOFF:
2e30d253
ILT
1745 // These are Initial-Exec relocs which get the thread offset
1746 // from the GOT. If we know that we are linking against the
1747 // local symbol, we can switch to Local-Exec, which links the
1748 // thread offset into the instruction.
1749 if (is_final)
e041f13d
ILT
1750 return tls::TLSOPT_TO_LE;
1751 return tls::TLSOPT_NONE;
2e30d253 1752
d61c17ea 1753 case elfcpp::R_X86_64_TPOFF32:
2e30d253
ILT
1754 // When we already have Local-Exec, there is nothing further we
1755 // can do.
e041f13d 1756 return tls::TLSOPT_NONE;
2e30d253
ILT
1757
1758 default:
1759 gold_unreachable();
1760 }
2e30d253
ILT
1761}
1762
95a2c8d6
RS
1763// Get the Reference_flags for a particular relocation.
1764
1765int
1766Target_x86_64::Scan::get_reference_flags(unsigned int r_type)
1767{
1768 switch (r_type)
1769 {
1770 case elfcpp::R_X86_64_NONE:
1771 case elfcpp::R_X86_64_GNU_VTINHERIT:
1772 case elfcpp::R_X86_64_GNU_VTENTRY:
1773 case elfcpp::R_X86_64_GOTPC32:
1774 case elfcpp::R_X86_64_GOTPC64:
1775 // No symbol reference.
1776 return 0;
1777
1778 case elfcpp::R_X86_64_64:
1779 case elfcpp::R_X86_64_32:
1780 case elfcpp::R_X86_64_32S:
1781 case elfcpp::R_X86_64_16:
1782 case elfcpp::R_X86_64_8:
1783 return Symbol::ABSOLUTE_REF;
1784
1785 case elfcpp::R_X86_64_PC64:
1786 case elfcpp::R_X86_64_PC32:
1787 case elfcpp::R_X86_64_PC16:
1788 case elfcpp::R_X86_64_PC8:
1789 case elfcpp::R_X86_64_GOTOFF64:
1790 return Symbol::RELATIVE_REF;
1791
1792 case elfcpp::R_X86_64_PLT32:
1793 case elfcpp::R_X86_64_PLTOFF64:
1794 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1795
1796 case elfcpp::R_X86_64_GOT64:
1797 case elfcpp::R_X86_64_GOT32:
1798 case elfcpp::R_X86_64_GOTPCREL64:
1799 case elfcpp::R_X86_64_GOTPCREL:
1800 case elfcpp::R_X86_64_GOTPLT64:
1801 // Absolute in GOT.
1802 return Symbol::ABSOLUTE_REF;
1803
1804 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1805 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1806 case elfcpp::R_X86_64_TLSDESC_CALL:
1807 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1808 case elfcpp::R_X86_64_DTPOFF32:
1809 case elfcpp::R_X86_64_DTPOFF64:
1810 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1811 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1812 return Symbol::TLS_REF;
1813
1814 case elfcpp::R_X86_64_COPY:
1815 case elfcpp::R_X86_64_GLOB_DAT:
1816 case elfcpp::R_X86_64_JUMP_SLOT:
1817 case elfcpp::R_X86_64_RELATIVE:
1818 case elfcpp::R_X86_64_IRELATIVE:
1819 case elfcpp::R_X86_64_TPOFF64:
1820 case elfcpp::R_X86_64_DTPMOD64:
1821 case elfcpp::R_X86_64_TLSDESC:
1822 case elfcpp::R_X86_64_SIZE32:
1823 case elfcpp::R_X86_64_SIZE64:
1824 default:
1825 // Not expected. We will give an error later.
1826 return 0;
1827 }
1828}
1829
e041f13d
ILT
1830// Report an unsupported relocation against a local symbol.
1831
1832void
6fa2a40b
CC
1833Target_x86_64::Scan::unsupported_reloc_local(
1834 Sized_relobj_file<64, false>* object,
1835 unsigned int r_type)
e041f13d 1836{
75f2446e
ILT
1837 gold_error(_("%s: unsupported reloc %u against local symbol"),
1838 object->name().c_str(), r_type);
e041f13d
ILT
1839}
1840
a036edd8
ILT
1841// We are about to emit a dynamic relocation of type R_TYPE. If the
1842// dynamic linker does not support it, issue an error. The GNU linker
1843// only issues a non-PIC error for an allocated read-only section.
1844// Here we know the section is allocated, but we don't know that it is
1845// read-only. But we check for all the relocation types which the
1846// glibc dynamic linker supports, so it seems appropriate to issue an
a29b0dad
ILT
1847// error even if the section is not read-only. If GSYM is not NULL,
1848// it is the symbol the relocation is against; if it is NULL, the
1849// relocation is against a local symbol.
a036edd8
ILT
1850
1851void
a29b0dad
ILT
1852Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type,
1853 Symbol* gsym)
a036edd8
ILT
1854{
1855 switch (r_type)
1856 {
2fbb4320
ILT
1857 // These are the relocation types supported by glibc for x86_64
1858 // which should always work.
a036edd8 1859 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 1860 case elfcpp::R_X86_64_IRELATIVE:
a036edd8
ILT
1861 case elfcpp::R_X86_64_GLOB_DAT:
1862 case elfcpp::R_X86_64_JUMP_SLOT:
1863 case elfcpp::R_X86_64_DTPMOD64:
1864 case elfcpp::R_X86_64_DTPOFF64:
1865 case elfcpp::R_X86_64_TPOFF64:
1866 case elfcpp::R_X86_64_64:
2fbb4320
ILT
1867 case elfcpp::R_X86_64_COPY:
1868 return;
1869
1870 // glibc supports these reloc types, but they can overflow.
a036edd8 1871 case elfcpp::R_X86_64_PC32:
a29b0dad
ILT
1872 // A PC relative reference is OK against a local symbol or if
1873 // the symbol is defined locally.
1874 if (gsym == NULL
1875 || (!gsym->is_from_dynobj()
1876 && !gsym->is_undefined()
1877 && !gsym->is_preemptible()))
1878 return;
1879 /* Fall through. */
1880 case elfcpp::R_X86_64_32:
2fbb4320
ILT
1881 if (this->issued_non_pic_error_)
1882 return;
1883 gold_assert(parameters->options().output_is_position_independent());
a29b0dad
ILT
1884 if (gsym == NULL)
1885 object->error(_("requires dynamic R_X86_64_32 reloc which may "
1886 "overflow at runtime; recompile with -fPIC"));
1887 else
1888 object->error(_("requires dynamic %s reloc against '%s' which may "
1889 "overflow at runtime; recompile with -fPIC"),
1890 (r_type == elfcpp::R_X86_64_32
1891 ? "R_X86_64_32"
1892 : "R_X86_64_PC32"),
1893 gsym->name());
2fbb4320 1894 this->issued_non_pic_error_ = true;
a036edd8
ILT
1895 return;
1896
1897 default:
1898 // This prevents us from issuing more than one error per reloc
1899 // section. But we can still wind up issuing more than one
1900 // error per object file.
1901 if (this->issued_non_pic_error_)
1902 return;
33aea2fd 1903 gold_assert(parameters->options().output_is_position_independent());
a29b0dad
ILT
1904 object->error(_("requires unsupported dynamic reloc %u; "
1905 "recompile with -fPIC"),
1906 r_type);
a036edd8
ILT
1907 this->issued_non_pic_error_ = true;
1908 return;
1909
1910 case elfcpp::R_X86_64_NONE:
1911 gold_unreachable();
1912 }
1913}
1914
7223e9ca
ILT
1915// Return whether we need to make a PLT entry for a relocation of the
1916// given type against a STT_GNU_IFUNC symbol.
1917
1918bool
6fa2a40b
CC
1919Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1920 Sized_relobj_file<64, false>* object,
1921 unsigned int r_type)
7223e9ca 1922{
95a2c8d6
RS
1923 int flags = Scan::get_reference_flags(r_type);
1924 if (flags & Symbol::TLS_REF)
1925 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1926 object->name().c_str(), r_type);
1927 return flags != 0;
7223e9ca
ILT
1928}
1929
2e30d253
ILT
1930// Scan a relocation for a local symbol.
1931
1932inline void
ad0f2072 1933Target_x86_64::Scan::local(Symbol_table* symtab,
d61c17ea
ILT
1934 Layout* layout,
1935 Target_x86_64* target,
6fa2a40b 1936 Sized_relobj_file<64, false>* object,
0ffd9845 1937 unsigned int data_shndx,
4f4c5f80 1938 Output_section* output_section,
0ffd9845 1939 const elfcpp::Rela<64, false>& reloc,
d61c17ea 1940 unsigned int r_type,
7bf1f802 1941 const elfcpp::Sym<64, false>& lsym)
2e30d253 1942{
7223e9ca
ILT
1943 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1944 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1945 && this->reloc_needs_plt_for_ifunc(object, r_type))
1946 {
1947 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1948 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1949 }
1950
2e30d253
ILT
1951 switch (r_type)
1952 {
1953 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
1954 case elfcpp::R_X86_64_GNU_VTINHERIT:
1955 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
1956 break;
1957
1958 case elfcpp::R_X86_64_64:
d61c6bd4 1959 // If building a shared library (or a position-independent
dceae3c1
ILT
1960 // executable), we need to create a dynamic relocation for this
1961 // location. The relocation applied at link time will apply the
1962 // link-time value, so we flag the location with an
1963 // R_X86_64_RELATIVE relocation so the dynamic loader can
d61c6bd4 1964 // relocate it easily.
8851ecca 1965 if (parameters->options().output_is_position_independent())
d61c6bd4 1966 {
e8c846c3 1967 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d61c6bd4 1968 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7223e9ca
ILT
1969 rela_dyn->add_local_relative(object, r_sym,
1970 elfcpp::R_X86_64_RELATIVE,
1971 output_section, data_shndx,
1972 reloc.get_r_offset(),
1973 reloc.get_r_addend());
d61c6bd4
ILT
1974 }
1975 break;
1976
2e30d253
ILT
1977 case elfcpp::R_X86_64_32:
1978 case elfcpp::R_X86_64_32S:
1979 case elfcpp::R_X86_64_16:
1980 case elfcpp::R_X86_64_8:
96f2030e 1981 // If building a shared library (or a position-independent
dceae3c1
ILT
1982 // executable), we need to create a dynamic relocation for this
1983 // location. We can't use an R_X86_64_RELATIVE relocation
1984 // because that is always a 64-bit relocation.
8851ecca 1985 if (parameters->options().output_is_position_independent())
96f2030e 1986 {
a29b0dad 1987 this->check_non_pic(object, r_type, NULL);
a036edd8 1988
96f2030e 1989 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
d491d34e 1990 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
dceae3c1 1991 if (lsym.get_st_type() != elfcpp::STT_SECTION)
d491d34e
ILT
1992 rela_dyn->add_local(object, r_sym, r_type, output_section,
1993 data_shndx, reloc.get_r_offset(),
1994 reloc.get_r_addend());
dceae3c1
ILT
1995 else
1996 {
1997 gold_assert(lsym.get_st_value() == 0);
d491d34e
ILT
1998 unsigned int shndx = lsym.get_st_shndx();
1999 bool is_ordinary;
2000 shndx = object->adjust_sym_shndx(r_sym, shndx,
2001 &is_ordinary);
2002 if (!is_ordinary)
2003 object->error(_("section symbol %u has bad shndx %u"),
2004 r_sym, shndx);
2005 else
2006 rela_dyn->add_local_section(object, shndx,
2007 r_type, output_section,
2008 data_shndx, reloc.get_r_offset(),
2009 reloc.get_r_addend());
dceae3c1 2010 }
96f2030e 2011 }
2e30d253
ILT
2012 break;
2013
2014 case elfcpp::R_X86_64_PC64:
2015 case elfcpp::R_X86_64_PC32:
2016 case elfcpp::R_X86_64_PC16:
2017 case elfcpp::R_X86_64_PC8:
2018 break;
2019
f389a824
ILT
2020 case elfcpp::R_X86_64_PLT32:
2021 // Since we know this is a local symbol, we can handle this as a
2022 // PC32 reloc.
2023 break;
2024
fdc2f80f 2025 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 2026 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
2027 case elfcpp::R_X86_64_GOTPC64:
2028 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
2029 // We need a GOT section.
2030 target->got_section(symtab, layout);
ee9e9e86
ILT
2031 // For PLTOFF64, we'd normally want a PLT section, but since we
2032 // know this is a local symbol, no PLT is needed.
2e30d253
ILT
2033 break;
2034
0ffd9845
ILT
2035 case elfcpp::R_X86_64_GOT64:
2036 case elfcpp::R_X86_64_GOT32:
2037 case elfcpp::R_X86_64_GOTPCREL64:
2038 case elfcpp::R_X86_64_GOTPCREL:
ee9e9e86 2039 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
2040 {
2041 // The symbol requires a GOT entry.
2042 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2043 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
7223e9ca
ILT
2044
2045 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2046 // lets function pointers compare correctly with shared
2047 // libraries. Otherwise we would need an IRELATIVE reloc.
2048 bool is_new;
2049 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
2050 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2051 else
2052 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2053 if (is_new)
0ffd9845
ILT
2054 {
2055 // If we are generating a shared object, we need to add a
7bf1f802 2056 // dynamic relocation for this symbol's GOT entry.
8851ecca 2057 if (parameters->options().output_is_position_independent())
0ffd9845
ILT
2058 {
2059 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7bf1f802
ILT
2060 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2061 if (r_type != elfcpp::R_X86_64_GOT32)
7223e9ca
ILT
2062 {
2063 unsigned int got_offset =
2064 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2065 rela_dyn->add_local_relative(object, r_sym,
2066 elfcpp::R_X86_64_RELATIVE,
2067 got, got_offset, 0);
2068 }
7bf1f802 2069 else
dceae3c1 2070 {
a29b0dad 2071 this->check_non_pic(object, r_type, NULL);
a036edd8 2072
dceae3c1 2073 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
0a65a3a7
CC
2074 rela_dyn->add_local(
2075 object, r_sym, r_type, got,
2076 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
dceae3c1 2077 }
0ffd9845
ILT
2078 }
2079 }
ee9e9e86
ILT
2080 // For GOTPLT64, we'd normally want a PLT section, but since
2081 // we know this is a local symbol, no PLT is needed.
0ffd9845
ILT
2082 }
2083 break;
2084
2e30d253
ILT
2085 case elfcpp::R_X86_64_COPY:
2086 case elfcpp::R_X86_64_GLOB_DAT:
2087 case elfcpp::R_X86_64_JUMP_SLOT:
2088 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 2089 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 2090 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 2091 case elfcpp::R_X86_64_TPOFF64:
2e30d253 2092 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 2093 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
2094 gold_error(_("%s: unexpected reloc %u in object file"),
2095 object->name().c_str(), r_type);
2e30d253
ILT
2096 break;
2097
d61c17ea 2098 // These are initial tls relocs, which are expected when linking
56622147
ILT
2099 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2100 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 2101 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 2102 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
2103 case elfcpp::R_X86_64_DTPOFF32:
2104 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
2105 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2106 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253 2107 {
8851ecca 2108 bool output_is_shared = parameters->options().shared();
e041f13d
ILT
2109 const tls::Tls_optimization optimized_type
2110 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
2e30d253
ILT
2111 switch (r_type)
2112 {
56622147 2113 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
2114 if (optimized_type == tls::TLSOPT_NONE)
2115 {
2116 // Create a pair of GOT entries for the module index and
2117 // dtv-relative offset.
2118 Output_data_got<64, false>* got
2119 = target->got_section(symtab, layout);
2120 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d491d34e
ILT
2121 unsigned int shndx = lsym.get_st_shndx();
2122 bool is_ordinary;
2123 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2124 if (!is_ordinary)
2125 object->error(_("local symbol %u has bad shndx %u"),
2126 r_sym, shndx);
2127 else
2128 got->add_local_pair_with_rela(object, r_sym,
2129 shndx,
2130 GOT_TYPE_TLS_PAIR,
2131 target->rela_dyn_section(layout),
2132 elfcpp::R_X86_64_DTPMOD64, 0);
7bf1f802
ILT
2133 }
2134 else if (optimized_type != tls::TLSOPT_TO_LE)
2135 unsupported_reloc_local(object, r_type);
2136 break;
2137
56622147 2138 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 2139 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
2140 if (optimized_type == tls::TLSOPT_NONE)
2141 {
2142 // Create reserved PLT and GOT entries for the resolver.
2143 target->reserve_tlsdesc_entries(symtab, layout);
2144
a8df5856
ILT
2145 // Generate a double GOT entry with an
2146 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2147 // is resolved lazily, so the GOT entry needs to be in
2148 // an area in .got.plt, not .got. Call got_section to
2149 // make sure the section has been created.
2150 target->got_section(symtab, layout);
2151 Output_data_got<64, false>* got = target->got_tlsdesc_section();
c2b45e22 2152 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
e291e7b9
ILT
2153 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
2154 {
2155 unsigned int got_offset = got->add_constant(0);
2156 got->add_constant(0);
2157 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2158 got_offset);
2159 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2160 // We store the arguments we need in a vector, and
2161 // use the index into the vector as the parameter
2162 // to pass to the target specific routines.
2163 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
2164 void* arg = reinterpret_cast<void*>(intarg);
2165 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2166 got, got_offset, 0);
2167 }
c2b45e22
CC
2168 }
2169 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 2170 unsupported_reloc_local(object, r_type);
2e30d253
ILT
2171 break;
2172
c2b45e22
CC
2173 case elfcpp::R_X86_64_TLSDESC_CALL:
2174 break;
2175
e041f13d 2176 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
2177 if (optimized_type == tls::TLSOPT_NONE)
2178 {
2179 // Create a GOT entry for the module index.
31d60480 2180 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
2181 }
2182 else if (optimized_type != tls::TLSOPT_TO_LE)
2183 unsupported_reloc_local(object, r_type);
2184 break;
2185
0ffd9845
ILT
2186 case elfcpp::R_X86_64_DTPOFF32:
2187 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
2188 break;
2189
56622147 2190 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 2191 layout->set_has_static_tls();
7bf1f802
ILT
2192 if (optimized_type == tls::TLSOPT_NONE)
2193 {
2194 // Create a GOT entry for the tp-relative offset.
2195 Output_data_got<64, false>* got
2196 = target->got_section(symtab, layout);
2197 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7 2198 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
2199 target->rela_dyn_section(layout),
2200 elfcpp::R_X86_64_TPOFF64);
2201 }
2202 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
2203 unsupported_reloc_local(object, r_type);
2204 break;
0ffd9845 2205
56622147 2206 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 2207 layout->set_has_static_tls();
7bf1f802
ILT
2208 if (output_is_shared)
2209 unsupported_reloc_local(object, r_type);
2e30d253 2210 break;
e041f13d
ILT
2211
2212 default:
2213 gold_unreachable();
2e30d253
ILT
2214 }
2215 }
2216 break;
2e30d253 2217
fdc2f80f
ILT
2218 case elfcpp::R_X86_64_SIZE32:
2219 case elfcpp::R_X86_64_SIZE64:
2e30d253 2220 default:
75f2446e
ILT
2221 gold_error(_("%s: unsupported reloc %u against local symbol"),
2222 object->name().c_str(), r_type);
2e30d253
ILT
2223 break;
2224 }
2225}
2226
2227
e041f13d
ILT
2228// Report an unsupported relocation against a global symbol.
2229
2230void
6fa2a40b
CC
2231Target_x86_64::Scan::unsupported_reloc_global(
2232 Sized_relobj_file<64, false>* object,
2233 unsigned int r_type,
2234 Symbol* gsym)
e041f13d 2235{
75f2446e 2236 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12 2237 object->name().c_str(), r_type, gsym->demangled_name().c_str());
e041f13d
ILT
2238}
2239
ce97fa81 2240// Returns true if this relocation type could be that of a function pointer.
21bb3914
ST
2241inline bool
2242Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
2243{
21bb3914
ST
2244 switch (r_type)
2245 {
2246 case elfcpp::R_X86_64_64:
2247 case elfcpp::R_X86_64_32:
2248 case elfcpp::R_X86_64_32S:
2249 case elfcpp::R_X86_64_16:
2250 case elfcpp::R_X86_64_8:
ce97fa81
ST
2251 case elfcpp::R_X86_64_GOT64:
2252 case elfcpp::R_X86_64_GOT32:
2253 case elfcpp::R_X86_64_GOTPCREL64:
2254 case elfcpp::R_X86_64_GOTPCREL:
2255 case elfcpp::R_X86_64_GOTPLT64:
21bb3914
ST
2256 {
2257 return true;
2258 }
2259 }
2260 return false;
2261}
2262
2263// For safe ICF, scan a relocation for a local symbol to check if it
2264// corresponds to a function pointer being taken. In that case mark
2265// the function whose pointer was taken as not foldable.
2266
2267inline bool
2268Target_x86_64::Scan::local_reloc_may_be_function_pointer(
2269 Symbol_table* ,
2270 Layout* ,
2271 Target_x86_64* ,
6fa2a40b 2272 Sized_relobj_file<64, false>* ,
21bb3914
ST
2273 unsigned int ,
2274 Output_section* ,
2275 const elfcpp::Rela<64, false>& ,
2276 unsigned int r_type,
2277 const elfcpp::Sym<64, false>&)
2278{
2279 // When building a shared library, do not fold any local symbols as it is
2280 // not possible to distinguish pointer taken versus a call by looking at
2281 // the relocation types.
2282 return (parameters->options().shared()
2283 || possible_function_pointer_reloc(r_type));
2284}
2285
2286// For safe ICF, scan a relocation for a global symbol to check if it
2287// corresponds to a function pointer being taken. In that case mark
2288// the function whose pointer was taken as not foldable.
2289
2290inline bool
2291Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2292 Symbol_table*,
2293 Layout* ,
2294 Target_x86_64* ,
6fa2a40b 2295 Sized_relobj_file<64, false>* ,
21bb3914
ST
2296 unsigned int ,
2297 Output_section* ,
2298 const elfcpp::Rela<64, false>& ,
2299 unsigned int r_type,
2300 Symbol* gsym)
2301{
2302 // When building a shared library, do not fold symbols whose visibility
2303 // is hidden, internal or protected.
2304 return ((parameters->options().shared()
2305 && (gsym->visibility() == elfcpp::STV_INTERNAL
2306 || gsym->visibility() == elfcpp::STV_PROTECTED
2307 || gsym->visibility() == elfcpp::STV_HIDDEN))
2308 || possible_function_pointer_reloc(r_type));
2309}
2310
2e30d253
ILT
2311// Scan a relocation for a global symbol.
2312
2313inline void
ad0f2072 2314Target_x86_64::Scan::global(Symbol_table* symtab,
d61c17ea
ILT
2315 Layout* layout,
2316 Target_x86_64* target,
6fa2a40b 2317 Sized_relobj_file<64, false>* object,
d61c17ea 2318 unsigned int data_shndx,
4f4c5f80 2319 Output_section* output_section,
d61c17ea
ILT
2320 const elfcpp::Rela<64, false>& reloc,
2321 unsigned int r_type,
2322 Symbol* gsym)
2e30d253 2323{
7223e9ca
ILT
2324 // A STT_GNU_IFUNC symbol may require a PLT entry.
2325 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2326 && this->reloc_needs_plt_for_ifunc(object, r_type))
2327 target->make_plt_entry(symtab, layout, gsym);
2328
2e30d253
ILT
2329 switch (r_type)
2330 {
2331 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
2332 case elfcpp::R_X86_64_GNU_VTINHERIT:
2333 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
2334 break;
2335
2336 case elfcpp::R_X86_64_64:
2e30d253
ILT
2337 case elfcpp::R_X86_64_32:
2338 case elfcpp::R_X86_64_32S:
2e30d253 2339 case elfcpp::R_X86_64_16:
2e30d253 2340 case elfcpp::R_X86_64_8:
96f2030e 2341 {
d61c6bd4
ILT
2342 // Make a PLT entry if necessary.
2343 if (gsym->needs_plt_entry())
2344 {
2345 target->make_plt_entry(symtab, layout, gsym);
2346 // Since this is not a PC-relative relocation, we may be
2347 // taking the address of a function. In that case we need to
2348 // set the entry in the dynamic symbol table to the address of
2349 // the PLT entry.
8851ecca 2350 if (gsym->is_from_dynobj() && !parameters->options().shared())
d61c6bd4
ILT
2351 gsym->set_needs_dynsym_value();
2352 }
2353 // Make a dynamic relocation if necessary.
95a2c8d6 2354 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
d61c6bd4 2355 {
966d4097 2356 if (gsym->may_need_copy_reloc())
d61c6bd4 2357 {
12c0daef 2358 target->copy_reloc(symtab, layout, object,
7bf1f802 2359 data_shndx, output_section, gsym, reloc);
d61c6bd4 2360 }
7223e9ca
ILT
2361 else if (r_type == elfcpp::R_X86_64_64
2362 && gsym->type() == elfcpp::STT_GNU_IFUNC
2363 && gsym->can_use_relative_reloc(false)
2364 && !gsym->is_from_dynobj()
2365 && !gsym->is_undefined()
2366 && !gsym->is_preemptible())
2367 {
2368 // Use an IRELATIVE reloc for a locally defined
2369 // STT_GNU_IFUNC symbol. This makes a function
2370 // address in a PIE executable match the address in a
2371 // shared library that it links against.
67181c72
ILT
2372 Reloc_section* rela_dyn =
2373 target->rela_irelative_section(layout);
7223e9ca
ILT
2374 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2375 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2376 output_section, object,
2377 data_shndx,
2378 reloc.get_r_offset(),
2379 reloc.get_r_addend());
2380 }
d61c6bd4
ILT
2381 else if (r_type == elfcpp::R_X86_64_64
2382 && gsym->can_use_relative_reloc(false))
2383 {
2384 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7223e9ca
ILT
2385 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2386 output_section, object,
2387 data_shndx,
2388 reloc.get_r_offset(),
2389 reloc.get_r_addend());
d61c6bd4
ILT
2390 }
2391 else
2392 {
a29b0dad 2393 this->check_non_pic(object, r_type, gsym);
96f2030e 2394 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
2395 rela_dyn->add_global(gsym, r_type, output_section, object,
2396 data_shndx, reloc.get_r_offset(),
96f2030e 2397 reloc.get_r_addend());
d61c6bd4
ILT
2398 }
2399 }
2400 }
2401 break;
2402
2403 case elfcpp::R_X86_64_PC64:
2404 case elfcpp::R_X86_64_PC32:
2405 case elfcpp::R_X86_64_PC16:
2406 case elfcpp::R_X86_64_PC8:
2407 {
2408 // Make a PLT entry if necessary.
2409 if (gsym->needs_plt_entry())
2410 target->make_plt_entry(symtab, layout, gsym);
2411 // Make a dynamic relocation if necessary.
95a2c8d6 2412 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
86849f1f 2413 {
966d4097 2414 if (gsym->may_need_copy_reloc())
d61c6bd4 2415 {
12c0daef 2416 target->copy_reloc(symtab, layout, object,
7bf1f802 2417 data_shndx, output_section, gsym, reloc);
d61c6bd4 2418 }
86849f1f 2419 else
d61c6bd4 2420 {
a29b0dad 2421 this->check_non_pic(object, r_type, gsym);
d61c6bd4 2422 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
2423 rela_dyn->add_global(gsym, r_type, output_section, object,
2424 data_shndx, reloc.get_r_offset(),
d61c6bd4
ILT
2425 reloc.get_r_addend());
2426 }
86849f1f 2427 }
d61c6bd4 2428 }
2e30d253
ILT
2429 break;
2430
ff006520 2431 case elfcpp::R_X86_64_GOT64:
2e30d253 2432 case elfcpp::R_X86_64_GOT32:
ff006520
ILT
2433 case elfcpp::R_X86_64_GOTPCREL64:
2434 case elfcpp::R_X86_64_GOTPCREL:
2435 case elfcpp::R_X86_64_GOTPLT64:
2e30d253
ILT
2436 {
2437 // The symbol requires a GOT entry.
2438 Output_data_got<64, false>* got = target->got_section(symtab, layout);
7bf1f802 2439 if (gsym->final_value_is_known())
7223e9ca
ILT
2440 {
2441 // For a STT_GNU_IFUNC symbol we want the PLT address.
2442 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2443 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2444 else
2445 got->add_global(gsym, GOT_TYPE_STANDARD);
2446 }
7bf1f802
ILT
2447 else
2448 {
2e30d253
ILT
2449 // If this symbol is not fully resolved, we need to add a
2450 // dynamic relocation for it.
7bf1f802 2451 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
07aa62f2
ILT
2452
2453 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2454 //
2455 // 1) The symbol may be defined in some other module.
2456 //
2457 // 2) We are building a shared library and this is a
2458 // protected symbol; using GLOB_DAT means that the dynamic
2459 // linker can use the address of the PLT in the main
2460 // executable when appropriate so that function address
2461 // comparisons work.
2462 //
2463 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2464 // code, again so that function address comparisons work.
7223e9ca
ILT
2465 if (gsym->is_from_dynobj()
2466 || gsym->is_undefined()
2467 || gsym->is_preemptible()
07aa62f2
ILT
2468 || (gsym->visibility() == elfcpp::STV_PROTECTED
2469 && parameters->options().shared())
7223e9ca
ILT
2470 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2471 && parameters->options().output_is_position_independent()))
0a65a3a7 2472 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
7bf1f802
ILT
2473 elfcpp::R_X86_64_GLOB_DAT);
2474 else
2e30d253 2475 {
7223e9ca
ILT
2476 // For a STT_GNU_IFUNC symbol we want to write the PLT
2477 // offset into the GOT, so that function pointer
2478 // comparisons work correctly.
2479 bool is_new;
2480 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2481 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2482 else
2483 {
2484 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2485 // Tell the dynamic linker to use the PLT address
2486 // when resolving relocations.
2487 if (gsym->is_from_dynobj()
2488 && !parameters->options().shared())
2489 gsym->set_needs_dynsym_value();
2490 }
2491 if (is_new)
2492 {
2493 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2494 rela_dyn->add_global_relative(gsym,
2495 elfcpp::R_X86_64_RELATIVE,
2496 got, got_off, 0);
2497 }
2e30d253
ILT
2498 }
2499 }
ee9e9e86
ILT
2500 // For GOTPLT64, we also need a PLT entry (but only if the
2501 // symbol is not fully resolved).
2502 if (r_type == elfcpp::R_X86_64_GOTPLT64
2503 && !gsym->final_value_is_known())
2504 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
2505 }
2506 break;
2507
2508 case elfcpp::R_X86_64_PLT32:
2509 // If the symbol is fully resolved, this is just a PC32 reloc.
2510 // Otherwise we need a PLT entry.
2511 if (gsym->final_value_is_known())
2512 break;
96f2030e
ILT
2513 // If building a shared library, we can also skip the PLT entry
2514 // if the symbol is defined in the output file and is protected
2515 // or hidden.
2516 if (gsym->is_defined()
2517 && !gsym->is_from_dynobj()
2518 && !gsym->is_preemptible())
2519 break;
2e30d253
ILT
2520 target->make_plt_entry(symtab, layout, gsym);
2521 break;
2522
fdc2f80f 2523 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 2524 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
2525 case elfcpp::R_X86_64_GOTPC64:
2526 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
2527 // We need a GOT section.
2528 target->got_section(symtab, layout);
ee9e9e86
ILT
2529 // For PLTOFF64, we also need a PLT entry (but only if the
2530 // symbol is not fully resolved).
2531 if (r_type == elfcpp::R_X86_64_PLTOFF64
2532 && !gsym->final_value_is_known())
2533 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
2534 break;
2535
2e30d253
ILT
2536 case elfcpp::R_X86_64_COPY:
2537 case elfcpp::R_X86_64_GLOB_DAT:
2538 case elfcpp::R_X86_64_JUMP_SLOT:
2539 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 2540 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 2541 // These are outstanding tls relocs, which are unexpected when linking
e822f2b1 2542 case elfcpp::R_X86_64_TPOFF64:
2e30d253 2543 case elfcpp::R_X86_64_DTPMOD64:
e822f2b1 2544 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
2545 gold_error(_("%s: unexpected reloc %u in object file"),
2546 object->name().c_str(), r_type);
2e30d253 2547 break;
2e30d253 2548
d61c17ea 2549 // These are initial tls relocs, which are expected for global()
56622147
ILT
2550 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2551 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 2552 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 2553 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
2554 case elfcpp::R_X86_64_DTPOFF32:
2555 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
2556 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2557 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253
ILT
2558 {
2559 const bool is_final = gsym->final_value_is_known();
e041f13d
ILT
2560 const tls::Tls_optimization optimized_type
2561 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
2562 switch (r_type)
2563 {
56622147 2564 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
2565 if (optimized_type == tls::TLSOPT_NONE)
2566 {
2567 // Create a pair of GOT entries for the module index and
2568 // dtv-relative offset.
2569 Output_data_got<64, false>* got
2570 = target->got_section(symtab, layout);
0a65a3a7
CC
2571 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2572 target->rela_dyn_section(layout),
2573 elfcpp::R_X86_64_DTPMOD64,
2574 elfcpp::R_X86_64_DTPOFF64);
7bf1f802
ILT
2575 }
2576 else if (optimized_type == tls::TLSOPT_TO_IE)
2577 {
2578 // Create a GOT entry for the tp-relative offset.
2579 Output_data_got<64, false>* got
2580 = target->got_section(symtab, layout);
0a65a3a7 2581 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
2582 target->rela_dyn_section(layout),
2583 elfcpp::R_X86_64_TPOFF64);
2584 }
2585 else if (optimized_type != tls::TLSOPT_TO_LE)
2586 unsupported_reloc_global(object, r_type, gsym);
2587 break;
2588
56622147 2589 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 2590 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
2591 if (optimized_type == tls::TLSOPT_NONE)
2592 {
2593 // Create reserved PLT and GOT entries for the resolver.
2594 target->reserve_tlsdesc_entries(symtab, layout);
2595
a8df5856
ILT
2596 // Create a double GOT entry with an R_X86_64_TLSDESC
2597 // reloc. The R_X86_64_TLSDESC reloc is resolved
2598 // lazily, so the GOT entry needs to be in an area in
2599 // .got.plt, not .got. Call got_section to make sure
2600 // the section has been created.
2601 target->got_section(symtab, layout);
2602 Output_data_got<64, false>* got = target->got_tlsdesc_section();
ca09d69a 2603 Reloc_section* rt = target->rela_tlsdesc_section(layout);
e291e7b9 2604 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
c2b45e22
CC
2605 elfcpp::R_X86_64_TLSDESC, 0);
2606 }
2607 else if (optimized_type == tls::TLSOPT_TO_IE)
2608 {
2609 // Create a GOT entry for the tp-relative offset.
2610 Output_data_got<64, false>* got
2611 = target->got_section(symtab, layout);
2612 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2613 target->rela_dyn_section(layout),
2614 elfcpp::R_X86_64_TPOFF64);
2615 }
2616 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 2617 unsupported_reloc_global(object, r_type, gsym);
2e30d253
ILT
2618 break;
2619
c2b45e22
CC
2620 case elfcpp::R_X86_64_TLSDESC_CALL:
2621 break;
2622
e041f13d 2623 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
2624 if (optimized_type == tls::TLSOPT_NONE)
2625 {
2626 // Create a GOT entry for the module index.
31d60480 2627 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
2628 }
2629 else if (optimized_type != tls::TLSOPT_TO_LE)
2630 unsupported_reloc_global(object, r_type, gsym);
2631 break;
2632
0ffd9845
ILT
2633 case elfcpp::R_X86_64_DTPOFF32:
2634 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
2635 break;
2636
56622147 2637 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 2638 layout->set_has_static_tls();
7bf1f802
ILT
2639 if (optimized_type == tls::TLSOPT_NONE)
2640 {
2641 // Create a GOT entry for the tp-relative offset.
2642 Output_data_got<64, false>* got
2643 = target->got_section(symtab, layout);
0a65a3a7 2644 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
2645 target->rela_dyn_section(layout),
2646 elfcpp::R_X86_64_TPOFF64);
2647 }
2648 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
2649 unsupported_reloc_global(object, r_type, gsym);
2650 break;
0ffd9845 2651
56622147 2652 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 2653 layout->set_has_static_tls();
8851ecca 2654 if (parameters->options().shared())
7bf1f802 2655 unsupported_reloc_local(object, r_type);
2e30d253 2656 break;
e041f13d
ILT
2657
2658 default:
2659 gold_unreachable();
2e30d253
ILT
2660 }
2661 }
2662 break;
fdc2f80f
ILT
2663
2664 case elfcpp::R_X86_64_SIZE32:
2665 case elfcpp::R_X86_64_SIZE64:
2e30d253 2666 default:
75f2446e 2667 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12
ILT
2668 object->name().c_str(), r_type,
2669 gsym->demangled_name().c_str());
2e30d253
ILT
2670 break;
2671 }
2672}
2673
6d03d481 2674void
ad0f2072 2675Target_x86_64::gc_process_relocs(Symbol_table* symtab,
6d03d481 2676 Layout* layout,
6fa2a40b 2677 Sized_relobj_file<64, false>* object,
6d03d481
ST
2678 unsigned int data_shndx,
2679 unsigned int sh_type,
2680 const unsigned char* prelocs,
2681 size_t reloc_count,
2682 Output_section* output_section,
2683 bool needs_special_offset_handling,
2684 size_t local_symbol_count,
2685 const unsigned char* plocal_symbols)
2686{
2687
2688 if (sh_type == elfcpp::SHT_REL)
2689 {
2690 return;
2691 }
2692
2693 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
41cbeecc
ST
2694 Target_x86_64::Scan,
2695 Target_x86_64::Relocatable_size_for_reloc>(
6d03d481
ST
2696 symtab,
2697 layout,
2698 this,
2699 object,
2700 data_shndx,
2701 prelocs,
2702 reloc_count,
2703 output_section,
2704 needs_special_offset_handling,
2705 local_symbol_count,
2706 plocal_symbols);
2707
2708}
2e30d253
ILT
2709// Scan relocations for a section.
2710
2711void
ad0f2072 2712Target_x86_64::scan_relocs(Symbol_table* symtab,
d61c17ea 2713 Layout* layout,
6fa2a40b 2714 Sized_relobj_file<64, false>* object,
d61c17ea
ILT
2715 unsigned int data_shndx,
2716 unsigned int sh_type,
2717 const unsigned char* prelocs,
2718 size_t reloc_count,
730cdc88
ILT
2719 Output_section* output_section,
2720 bool needs_special_offset_handling,
d61c17ea 2721 size_t local_symbol_count,
730cdc88 2722 const unsigned char* plocal_symbols)
2e30d253
ILT
2723{
2724 if (sh_type == elfcpp::SHT_REL)
2725 {
75f2446e
ILT
2726 gold_error(_("%s: unsupported REL reloc section"),
2727 object->name().c_str());
2728 return;
2e30d253
ILT
2729 }
2730
2731 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2732 Target_x86_64::Scan>(
2e30d253
ILT
2733 symtab,
2734 layout,
2735 this,
2736 object,
2737 data_shndx,
2738 prelocs,
2739 reloc_count,
730cdc88
ILT
2740 output_section,
2741 needs_special_offset_handling,
2e30d253 2742 local_symbol_count,
730cdc88 2743 plocal_symbols);
2e30d253
ILT
2744}
2745
2746// Finalize the sections.
2747
2748void
f59f41f3
DK
2749Target_x86_64::do_finalize_sections(
2750 Layout* layout,
2751 const Input_objects*,
e785ec03 2752 Symbol_table* symtab)
2e30d253 2753{
ea715a34
ILT
2754 const Reloc_section* rel_plt = (this->plt_ == NULL
2755 ? NULL
e291e7b9 2756 : this->plt_->rela_plt());
ea715a34 2757 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
612a8d3d 2758 this->rela_dyn_, true, false);
ea715a34 2759
2e30d253
ILT
2760 // Fill in some more dynamic tags.
2761 Output_data_dynamic* const odyn = layout->dynamic_data();
2762 if (odyn != NULL)
2763 {
22b127cc 2764 if (this->plt_ != NULL
ea715a34
ILT
2765 && this->plt_->output_section() != NULL
2766 && this->plt_->has_tlsdesc_entry())
2e30d253 2767 {
ea715a34
ILT
2768 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2769 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2770 this->got_->finalize_data_size();
2771 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2772 this->plt_, plt_offset);
2773 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2774 this->got_, got_offset);
2e30d253
ILT
2775 }
2776 }
2777
2778 // Emit any relocs we saved in an attempt to avoid generating COPY
2779 // relocs.
12c0daef
ILT
2780 if (this->copy_relocs_.any_saved_relocs())
2781 this->copy_relocs_.emit(this->rela_dyn_section(layout));
e785ec03
ILT
2782
2783 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2784 // the .got.plt section.
2785 Symbol* sym = this->global_offset_table_;
2786 if (sym != NULL)
2787 {
2788 uint64_t data_size = this->got_plt_->current_data_size();
2789 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2790 }
28a13fec 2791
67181c72
ILT
2792 if (parameters->doing_static_link()
2793 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
28a13fec
ILT
2794 {
2795 // If linking statically, make sure that the __rela_iplt symbols
2796 // were defined if necessary, even if we didn't create a PLT.
2797 static const Define_symbol_in_segment syms[] =
2798 {
2799 {
2800 "__rela_iplt_start", // name
2801 elfcpp::PT_LOAD, // segment_type
2802 elfcpp::PF_W, // segment_flags_set
2803 elfcpp::PF(0), // segment_flags_clear
2804 0, // value
2805 0, // size
2806 elfcpp::STT_NOTYPE, // type
2807 elfcpp::STB_GLOBAL, // binding
2808 elfcpp::STV_HIDDEN, // visibility
2809 0, // nonvis
2810 Symbol::SEGMENT_START, // offset_from_base
2811 true // only_if_ref
2812 },
2813 {
2814 "__rela_iplt_end", // name
2815 elfcpp::PT_LOAD, // segment_type
2816 elfcpp::PF_W, // segment_flags_set
2817 elfcpp::PF(0), // segment_flags_clear
2818 0, // value
2819 0, // size
2820 elfcpp::STT_NOTYPE, // type
2821 elfcpp::STB_GLOBAL, // binding
2822 elfcpp::STV_HIDDEN, // visibility
2823 0, // nonvis
2824 Symbol::SEGMENT_START, // offset_from_base
2825 true // only_if_ref
2826 }
2827 };
2828
2829 symtab->define_symbols(layout, 2, syms,
2830 layout->script_options()->saw_sections_clause());
2831 }
2e30d253
ILT
2832}
2833
2834// Perform a relocation.
2835
2836inline bool
2837Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2838 Target_x86_64* target,
031cdbed 2839 Output_section*,
2e30d253 2840 size_t relnum,
0ffd9845 2841 const elfcpp::Rela<64, false>& rela,
2e30d253
ILT
2842 unsigned int r_type,
2843 const Sized_symbol<64>* gsym,
2844 const Symbol_value<64>* psymval,
2845 unsigned char* view,
2846 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2847 section_size_type view_size)
2e30d253
ILT
2848{
2849 if (this->skip_call_tls_get_addr_)
2850 {
5efc7cd2
CC
2851 if ((r_type != elfcpp::R_X86_64_PLT32
2852 && r_type != elfcpp::R_X86_64_PC32)
2e30d253 2853 || gsym == NULL
0ffd9845 2854 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2e30d253 2855 {
75f2446e
ILT
2856 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2857 _("missing expected TLS relocation"));
2858 }
2859 else
2860 {
2861 this->skip_call_tls_get_addr_ = false;
2862 return false;
2e30d253 2863 }
2e30d253
ILT
2864 }
2865
6fa2a40b 2866 const Sized_relobj_file<64, false>* object = relinfo->object;
7223e9ca
ILT
2867
2868 // Pick the value to use for symbols defined in the PLT.
2e30d253 2869 Symbol_value<64> symval;
96f2030e 2870 if (gsym != NULL
95a2c8d6 2871 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2e30d253 2872 {
67181c72 2873 symval.set_output_value(target->plt_address_for_global(gsym)
2e30d253
ILT
2874 + gsym->plt_offset());
2875 psymval = &symval;
2876 }
7223e9ca
ILT
2877 else if (gsym == NULL && psymval->is_ifunc_symbol())
2878 {
2879 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2880 if (object->local_has_plt_offset(r_sym))
2881 {
67181c72 2882 symval.set_output_value(target->plt_address_for_local(object, r_sym)
7223e9ca
ILT
2883 + object->local_plt_offset(r_sym));
2884 psymval = &symval;
2885 }
2886 }
2e30d253 2887
0ffd9845
ILT
2888 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2889
2890 // Get the GOT offset if needed.
96f2030e
ILT
2891 // The GOT pointer points to the end of the GOT section.
2892 // We need to subtract the size of the GOT section to get
2893 // the actual offset to use in the relocation.
0ffd9845
ILT
2894 bool have_got_offset = false;
2895 unsigned int got_offset = 0;
2896 switch (r_type)
2897 {
2898 case elfcpp::R_X86_64_GOT32:
2899 case elfcpp::R_X86_64_GOT64:
2900 case elfcpp::R_X86_64_GOTPLT64:
2901 case elfcpp::R_X86_64_GOTPCREL:
2902 case elfcpp::R_X86_64_GOTPCREL64:
2903 if (gsym != NULL)
2904 {
0a65a3a7
CC
2905 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2906 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
0ffd9845
ILT
2907 }
2908 else
2909 {
2910 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
2911 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2912 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2913 - target->got_size());
0ffd9845
ILT
2914 }
2915 have_got_offset = true;
2916 break;
2917
2918 default:
2919 break;
2920 }
2e30d253
ILT
2921
2922 switch (r_type)
2923 {
2924 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
2925 case elfcpp::R_X86_64_GNU_VTINHERIT:
2926 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
2927 break;
2928
2929 case elfcpp::R_X86_64_64:
2930 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2931 break;
2932
2933 case elfcpp::R_X86_64_PC64:
2934 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2935 address);
2936 break;
2937
2938 case elfcpp::R_X86_64_32:
7bb3655e
ILT
2939 // FIXME: we need to verify that value + addend fits into 32 bits:
2940 // uint64_t x = value + addend;
2941 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2942 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2e30d253
ILT
2943 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2944 break;
2945
2946 case elfcpp::R_X86_64_32S:
7bb3655e
ILT
2947 // FIXME: we need to verify that value + addend fits into 32 bits:
2948 // int64_t x = value + addend; // note this quantity is signed!
2949 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2e30d253
ILT
2950 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2951 break;
2952
2953 case elfcpp::R_X86_64_PC32:
2954 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2955 address);
2956 break;
2957
2958 case elfcpp::R_X86_64_16:
2959 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2960 break;
2961
2962 case elfcpp::R_X86_64_PC16:
2963 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2964 address);
2965 break;
2966
2967 case elfcpp::R_X86_64_8:
2968 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2969 break;
2970
2971 case elfcpp::R_X86_64_PC8:
2972 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2973 address);
2974 break;
2975
2976 case elfcpp::R_X86_64_PLT32:
f389a824
ILT
2977 gold_assert(gsym == NULL
2978 || gsym->has_plt_offset()
99f8faca
ILT
2979 || gsym->final_value_is_known()
2980 || (gsym->is_defined()
2981 && !gsym->is_from_dynobj()
2982 && !gsym->is_preemptible()));
ee9e9e86
ILT
2983 // Note: while this code looks the same as for R_X86_64_PC32, it
2984 // behaves differently because psymval was set to point to
2985 // the PLT entry, rather than the symbol, in Scan::global().
2e30d253
ILT
2986 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2987 address);
2988 break;
2989
ee9e9e86
ILT
2990 case elfcpp::R_X86_64_PLTOFF64:
2991 {
2992 gold_assert(gsym);
2993 gold_assert(gsym->has_plt_offset()
2994 || gsym->final_value_is_known());
2995 elfcpp::Elf_types<64>::Elf_Addr got_address;
2996 got_address = target->got_section(NULL, NULL)->address();
c1866bd5
ILT
2997 Relocate_functions<64, false>::rela64(view, object, psymval,
2998 addend - got_address);
ee9e9e86
ILT
2999 }
3000
2e30d253 3001 case elfcpp::R_X86_64_GOT32:
0ffd9845
ILT
3002 gold_assert(have_got_offset);
3003 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2e30d253
ILT
3004 break;
3005
e822f2b1
ILT
3006 case elfcpp::R_X86_64_GOTPC32:
3007 {
3008 gold_assert(gsym);
3009 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 3010 value = target->got_plt_section()->address();
e822f2b1
ILT
3011 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3012 }
3013 break;
3014
3015 case elfcpp::R_X86_64_GOT64:
3016 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
3017 // Since we always add a PLT entry, this is equivalent.
fdc2f80f 3018 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
3019 gold_assert(have_got_offset);
3020 Relocate_functions<64, false>::rela64(view, got_offset, addend);
e822f2b1
ILT
3021 break;
3022
3023 case elfcpp::R_X86_64_GOTPC64:
3024 {
3025 gold_assert(gsym);
3026 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 3027 value = target->got_plt_section()->address();
e822f2b1
ILT
3028 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
3029 }
3030 break;
3031
2e30d253
ILT
3032 case elfcpp::R_X86_64_GOTOFF64:
3033 {
3034 elfcpp::Elf_types<64>::Elf_Addr value;
3035 value = (psymval->value(object, 0)
96f2030e 3036 - target->got_plt_section()->address());
2e30d253
ILT
3037 Relocate_functions<64, false>::rela64(view, value, addend);
3038 }
3039 break;
3040
3041 case elfcpp::R_X86_64_GOTPCREL:
3042 {
0ffd9845
ILT
3043 gold_assert(have_got_offset);
3044 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 3045 value = target->got_plt_section()->address() + got_offset;
0ffd9845 3046 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2e30d253
ILT
3047 }
3048 break;
3049
e822f2b1
ILT
3050 case elfcpp::R_X86_64_GOTPCREL64:
3051 {
0ffd9845
ILT
3052 gold_assert(have_got_offset);
3053 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 3054 value = target->got_plt_section()->address() + got_offset;
0ffd9845 3055 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
e822f2b1
ILT
3056 }
3057 break;
3058
2e30d253
ILT
3059 case elfcpp::R_X86_64_COPY:
3060 case elfcpp::R_X86_64_GLOB_DAT:
3061 case elfcpp::R_X86_64_JUMP_SLOT:
3062 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 3063 case elfcpp::R_X86_64_IRELATIVE:
d61c17ea 3064 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 3065 case elfcpp::R_X86_64_TPOFF64:
2e30d253 3066 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 3067 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
3068 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3069 _("unexpected reloc %u in object file"),
3070 r_type);
2e30d253
ILT
3071 break;
3072
d61c17ea 3073 // These are initial tls relocs, which are expected when linking
56622147
ILT
3074 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3075 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 3076 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 3077 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
3078 case elfcpp::R_X86_64_DTPOFF32:
3079 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
3080 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3081 case elfcpp::R_X86_64_TPOFF32: // Local-exec
7bf1f802
ILT
3082 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3083 view, address, view_size);
2e30d253 3084 break;
2e30d253 3085
fdc2f80f
ILT
3086 case elfcpp::R_X86_64_SIZE32:
3087 case elfcpp::R_X86_64_SIZE64:
2e30d253 3088 default:
75f2446e
ILT
3089 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3090 _("unsupported reloc %u"),
3091 r_type);
2e30d253
ILT
3092 break;
3093 }
3094
3095 return true;
3096}
3097
3098// Perform a TLS relocation.
3099
3100inline void
d61c17ea 3101Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
7bf1f802 3102 Target_x86_64* target,
d61c17ea 3103 size_t relnum,
72ec2876 3104 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
3105 unsigned int r_type,
3106 const Sized_symbol<64>* gsym,
3107 const Symbol_value<64>* psymval,
3108 unsigned char* view,
6a41d30b 3109 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 3110 section_size_type view_size)
2e30d253 3111{
2e30d253 3112 Output_segment* tls_segment = relinfo->layout->tls_segment();
7bf1f802 3113
6fa2a40b 3114 const Sized_relobj_file<64, false>* object = relinfo->object;
6a41d30b 3115 const elfcpp::Elf_Xword addend = rela.get_r_addend();
36171d64
CC
3116 elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
3117 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2e30d253
ILT
3118
3119 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
3120
3121 const bool is_final = (gsym == NULL
b3705d2a 3122 ? !parameters->options().shared()
2e30d253 3123 : gsym->final_value_is_known());
36171d64 3124 tls::Tls_optimization optimized_type
e041f13d 3125 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
3126 switch (r_type)
3127 {
56622147 3128 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
36171d64
CC
3129 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3130 {
3131 // If this code sequence is used in a non-executable section,
3132 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3133 // on the assumption that it's being used by itself in a debug
3134 // section. Therefore, in the unlikely event that the code
3135 // sequence appears in a non-executable section, we simply
3136 // leave it unoptimized.
3137 optimized_type = tls::TLSOPT_NONE;
3138 }
e041f13d 3139 if (optimized_type == tls::TLSOPT_TO_LE)
2e30d253 3140 {
62855347
ILT
3141 if (tls_segment == NULL)
3142 {
191f1a2d
ILT
3143 gold_assert(parameters->errors()->error_count() > 0
3144 || issue_undefined_symbol_error(gsym));
62855347
ILT
3145 return;
3146 }
2e30d253 3147 this->tls_gd_to_le(relinfo, relnum, tls_segment,
72ec2876 3148 rela, r_type, value, view,
2e30d253
ILT
3149 view_size);
3150 break;
3151 }
7bf1f802
ILT
3152 else
3153 {
c2b45e22
CC
3154 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3155 ? GOT_TYPE_TLS_OFFSET
3156 : GOT_TYPE_TLS_PAIR);
7bf1f802
ILT
3157 unsigned int got_offset;
3158 if (gsym != NULL)
3159 {
c2b45e22
CC
3160 gold_assert(gsym->has_got_offset(got_type));
3161 got_offset = gsym->got_offset(got_type) - target->got_size();
7bf1f802
ILT
3162 }
3163 else
3164 {
3165 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
c2b45e22
CC
3166 gold_assert(object->local_has_got_offset(r_sym, got_type));
3167 got_offset = (object->local_got_offset(r_sym, got_type)
7bf1f802
ILT
3168 - target->got_size());
3169 }
3170 if (optimized_type == tls::TLSOPT_TO_IE)
3171 {
62855347
ILT
3172 if (tls_segment == NULL)
3173 {
191f1a2d
ILT
3174 gold_assert(parameters->errors()->error_count() > 0
3175 || issue_undefined_symbol_error(gsym));
62855347
ILT
3176 return;
3177 }
c2b45e22 3178 value = target->got_plt_section()->address() + got_offset;
7bf1f802 3179 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
c2b45e22 3180 value, view, address, view_size);
7bf1f802
ILT
3181 break;
3182 }
3183 else if (optimized_type == tls::TLSOPT_NONE)
3184 {
3185 // Relocate the field with the offset of the pair of GOT
3186 // entries.
6a41d30b
ILT
3187 value = target->got_plt_section()->address() + got_offset;
3188 Relocate_functions<64, false>::pcrela32(view, value, addend,
3189 address);
7bf1f802
ILT
3190 break;
3191 }
3192 }
72ec2876 3193 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 3194 _("unsupported reloc %u"), r_type);
2e30d253
ILT
3195 break;
3196
c2b45e22
CC
3197 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3198 case elfcpp::R_X86_64_TLSDESC_CALL:
36171d64
CC
3199 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3200 {
3201 // See above comment for R_X86_64_TLSGD.
3202 optimized_type = tls::TLSOPT_NONE;
3203 }
c2b45e22
CC
3204 if (optimized_type == tls::TLSOPT_TO_LE)
3205 {
62855347
ILT
3206 if (tls_segment == NULL)
3207 {
191f1a2d
ILT
3208 gold_assert(parameters->errors()->error_count() > 0
3209 || issue_undefined_symbol_error(gsym));
62855347
ILT
3210 return;
3211 }
c2b45e22
CC
3212 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3213 rela, r_type, value, view,
3214 view_size);
3215 break;
3216 }
3217 else
3218 {
3219 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3220 ? GOT_TYPE_TLS_OFFSET
3221 : GOT_TYPE_TLS_DESC);
a8df5856
ILT
3222 unsigned int got_offset = 0;
3223 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3224 && optimized_type == tls::TLSOPT_NONE)
3225 {
3226 // We created GOT entries in the .got.tlsdesc portion of
3227 // the .got.plt section, but the offset stored in the
3228 // symbol is the offset within .got.tlsdesc.
3229 got_offset = (target->got_size()
3230 + target->got_plt_section()->data_size());
3231 }
c2b45e22
CC
3232 if (gsym != NULL)
3233 {
3234 gold_assert(gsym->has_got_offset(got_type));
a8df5856 3235 got_offset += gsym->got_offset(got_type) - target->got_size();
c2b45e22
CC
3236 }
3237 else
3238 {
3239 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3240 gold_assert(object->local_has_got_offset(r_sym, got_type));
a8df5856
ILT
3241 got_offset += (object->local_got_offset(r_sym, got_type)
3242 - target->got_size());
c2b45e22
CC
3243 }
3244 if (optimized_type == tls::TLSOPT_TO_IE)
3245 {
62855347
ILT
3246 if (tls_segment == NULL)
3247 {
191f1a2d
ILT
3248 gold_assert(parameters->errors()->error_count() > 0
3249 || issue_undefined_symbol_error(gsym));
62855347
ILT
3250 return;
3251 }
c2b45e22
CC
3252 value = target->got_plt_section()->address() + got_offset;
3253 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
3254 rela, r_type, value, view, address,
3255 view_size);
3256 break;
3257 }
3258 else if (optimized_type == tls::TLSOPT_NONE)
3259 {
3260 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3261 {
3262 // Relocate the field with the offset of the pair of GOT
3263 // entries.
3264 value = target->got_plt_section()->address() + got_offset;
3265 Relocate_functions<64, false>::pcrela32(view, value, addend,
3266 address);
3267 }
3268 break;
3269 }
3270 }
3271 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3272 _("unsupported reloc %u"), r_type);
3273 break;
3274
56622147 3275 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
36171d64
CC
3276 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3277 {
3278 // See above comment for R_X86_64_TLSGD.
3279 optimized_type = tls::TLSOPT_NONE;
3280 }
e041f13d
ILT
3281 if (optimized_type == tls::TLSOPT_TO_LE)
3282 {
62855347
ILT
3283 if (tls_segment == NULL)
3284 {
191f1a2d
ILT
3285 gold_assert(parameters->errors()->error_count() > 0
3286 || issue_undefined_symbol_error(gsym));
62855347
ILT
3287 return;
3288 }
72ec2876
ILT
3289 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3290 value, view, view_size);
3291 break;
e041f13d 3292 }
7bf1f802
ILT
3293 else if (optimized_type == tls::TLSOPT_NONE)
3294 {
3295 // Relocate the field with the offset of the GOT entry for
3296 // the module index.
3297 unsigned int got_offset;
31d60480
ILT
3298 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3299 - target->got_size());
6a41d30b
ILT
3300 value = target->got_plt_section()->address() + got_offset;
3301 Relocate_functions<64, false>::pcrela32(view, value, addend,
3302 address);
7bf1f802
ILT
3303 break;
3304 }
72ec2876 3305 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 3306 _("unsupported reloc %u"), r_type);
2e30d253 3307 break;
0ffd9845
ILT
3308
3309 case elfcpp::R_X86_64_DTPOFF32:
36171d64
CC
3310 // This relocation type is used in debugging information.
3311 // In that case we need to not optimize the value. If the
3312 // section is not executable, then we assume we should not
3313 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3314 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3315 // R_X86_64_TLSLD.
3316 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3317 {
62855347
ILT
3318 if (tls_segment == NULL)
3319 {
191f1a2d
ILT
3320 gold_assert(parameters->errors()->error_count() > 0
3321 || issue_undefined_symbol_error(gsym));
62855347
ILT
3322 return;
3323 }
36171d64
CC
3324 value -= tls_segment->memsz();
3325 }
d85c80a3 3326 Relocate_functions<64, false>::rela32(view, value, addend);
0ffd9845
ILT
3327 break;
3328
3329 case elfcpp::R_X86_64_DTPOFF64:
36171d64
CC
3330 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3331 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3332 {
62855347
ILT
3333 if (tls_segment == NULL)
3334 {
191f1a2d
ILT
3335 gold_assert(parameters->errors()->error_count() > 0
3336 || issue_undefined_symbol_error(gsym));
62855347
ILT
3337 return;
3338 }
36171d64
CC
3339 value -= tls_segment->memsz();
3340 }
d85c80a3 3341 Relocate_functions<64, false>::rela64(view, value, addend);
0ffd9845 3342 break;
2e30d253 3343
56622147
ILT
3344 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3345 if (optimized_type == tls::TLSOPT_TO_LE)
3346 {
62855347
ILT
3347 if (tls_segment == NULL)
3348 {
191f1a2d
ILT
3349 gold_assert(parameters->errors()->error_count() > 0
3350 || issue_undefined_symbol_error(gsym));
62855347
ILT
3351 return;
3352 }
56622147
ILT
3353 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3354 rela, r_type, value, view,
3355 view_size);
3356 break;
3357 }
7bf1f802
ILT
3358 else if (optimized_type == tls::TLSOPT_NONE)
3359 {
3360 // Relocate the field with the offset of the GOT entry for
3361 // the tp-relative offset of the symbol.
3362 unsigned int got_offset;
3363 if (gsym != NULL)
3364 {
0a65a3a7
CC
3365 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3366 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3367 - target->got_size());
7bf1f802
ILT
3368 }
3369 else
3370 {
3371 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
3372 gold_assert(object->local_has_got_offset(r_sym,
3373 GOT_TYPE_TLS_OFFSET));
3374 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
7bf1f802
ILT
3375 - target->got_size());
3376 }
6a41d30b
ILT
3377 value = target->got_plt_section()->address() + got_offset;
3378 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
7bf1f802
ILT
3379 break;
3380 }
56622147
ILT
3381 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3382 _("unsupported reloc type %u"),
3383 r_type);
3384 break;
0ffd9845 3385
56622147 3386 case elfcpp::R_X86_64_TPOFF32: // Local-exec
62855347
ILT
3387 if (tls_segment == NULL)
3388 {
191f1a2d
ILT
3389 gold_assert(parameters->errors()->error_count() > 0
3390 || issue_undefined_symbol_error(gsym));
62855347
ILT
3391 return;
3392 }
6a41d30b 3393 value -= tls_segment->memsz();
d85c80a3 3394 Relocate_functions<64, false>::rela32(view, value, addend);
56622147 3395 break;
2e30d253 3396 }
2e30d253
ILT
3397}
3398
7bf1f802
ILT
3399// Do a relocation in which we convert a TLS General-Dynamic to an
3400// Initial-Exec.
3401
3402inline void
3403Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
3404 size_t relnum,
c2b45e22 3405 Output_segment*,
7bf1f802
ILT
3406 const elfcpp::Rela<64, false>& rela,
3407 unsigned int,
3408 elfcpp::Elf_types<64>::Elf_Addr value,
3409 unsigned char* view,
c2b45e22 3410 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 3411 section_size_type view_size)
7bf1f802
ILT
3412{
3413 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3414 // .word 0x6666; rex64; call __tls_get_addr
3415 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3416
3417 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3418 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3419
3420 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3421 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3422 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3423 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3424
3425 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3426
c2b45e22
CC
3427 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3428 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
7bf1f802
ILT
3429
3430 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3431 // We can skip it.
3432 this->skip_call_tls_get_addr_ = true;
3433}
3434
e041f13d 3435// Do a relocation in which we convert a TLS General-Dynamic to a
2e30d253
ILT
3436// Local-Exec.
3437
3438inline void
d61c17ea
ILT
3439Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
3440 size_t relnum,
3441 Output_segment* tls_segment,
72ec2876 3442 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
3443 unsigned int,
3444 elfcpp::Elf_types<64>::Elf_Addr value,
3445 unsigned char* view,
fe8718a4 3446 section_size_type view_size)
2e30d253 3447{
0ffd9845
ILT
3448 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3449 // .word 0x6666; rex64; call __tls_get_addr
3450 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2e30d253 3451
72ec2876
ILT
3452 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3453 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2e30d253 3454
72ec2876
ILT
3455 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3456 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3457 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3458 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2e30d253 3459
0ffd9845 3460 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2e30d253 3461
6a41d30b 3462 value -= tls_segment->memsz();
0ffd9845 3463 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2e30d253
ILT
3464
3465 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3466 // We can skip it.
3467 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
3468}
3469
c2b45e22
CC
3470// Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3471
3472inline void
3473Target_x86_64::Relocate::tls_desc_gd_to_ie(
3474 const Relocate_info<64, false>* relinfo,
3475 size_t relnum,
3476 Output_segment*,
3477 const elfcpp::Rela<64, false>& rela,
3478 unsigned int r_type,
3479 elfcpp::Elf_types<64>::Elf_Addr value,
3480 unsigned char* view,
3481 elfcpp::Elf_types<64>::Elf_Addr address,
3482 section_size_type view_size)
3483{
3484 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3485 {
3486 // leaq foo@tlsdesc(%rip), %rax
3487 // ==> movq foo@gottpoff(%rip), %rax
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, 4);
3490 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3491 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3492 view[-2] = 0x8b;
3493 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3494 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3495 }
3496 else
3497 {
3498 // call *foo@tlscall(%rax)
3499 // ==> nop; nop
3500 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3501 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3502 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3503 view[0] == 0xff && view[1] == 0x10);
3504 view[0] = 0x66;
3505 view[1] = 0x90;
3506 }
3507}
3508
3509// Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3510
3511inline void
3512Target_x86_64::Relocate::tls_desc_gd_to_le(
3513 const Relocate_info<64, false>* relinfo,
3514 size_t relnum,
3515 Output_segment* tls_segment,
3516 const elfcpp::Rela<64, false>& rela,
3517 unsigned int r_type,
3518 elfcpp::Elf_types<64>::Elf_Addr value,
3519 unsigned char* view,
3520 section_size_type view_size)
3521{
3522 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3523 {
3524 // leaq foo@tlsdesc(%rip), %rax
3525 // ==> movq foo@tpoff, %rax
3526 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3527 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3528 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3529 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3530 view[-2] = 0xc7;
3531 view[-1] = 0xc0;
3532 value -= tls_segment->memsz();
3533 Relocate_functions<64, false>::rela32(view, value, 0);
3534 }
3535 else
3536 {
3537 // call *foo@tlscall(%rax)
3538 // ==> nop; nop
3539 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3540 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3541 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3542 view[0] == 0xff && view[1] == 0x10);
3543 view[0] = 0x66;
3544 view[1] = 0x90;
3545 }
3546}
3547
2e30d253 3548inline void
72ec2876
ILT
3549Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
3550 size_t relnum,
3551 Output_segment*,
3552 const elfcpp::Rela<64, false>& rela,
3553 unsigned int,
3554 elfcpp::Elf_types<64>::Elf_Addr,
3555 unsigned char* view,
fe8718a4 3556 section_size_type view_size)
2e30d253 3557{
72ec2876
ILT
3558 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3559 // ... leq foo@dtpoff(%rax),%reg
3560 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2e30d253 3561
72ec2876
ILT
3562 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3563 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2e30d253 3564
72ec2876
ILT
3565 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3566 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3567
3568 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3569
3570 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3571
3572 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3573 // We can skip it.
3574 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
3575}
3576
56622147
ILT
3577// Do a relocation in which we convert a TLS Initial-Exec to a
3578// Local-Exec.
3579
3580inline void
3581Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
3582 size_t relnum,
3583 Output_segment* tls_segment,
3584 const elfcpp::Rela<64, false>& rela,
3585 unsigned int,
3586 elfcpp::Elf_types<64>::Elf_Addr value,
3587 unsigned char* view,
fe8718a4 3588 section_size_type view_size)
56622147
ILT
3589{
3590 // We need to examine the opcodes to figure out which instruction we
3591 // are looking at.
3592
3593 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3594 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3595
3596 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3597 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3598
3599 unsigned char op1 = view[-3];
3600 unsigned char op2 = view[-2];
3601 unsigned char op3 = view[-1];
3602 unsigned char reg = op3 >> 3;
3603
3604 if (op2 == 0x8b)
3605 {
3606 // movq
3607 if (op1 == 0x4c)
3608 view[-3] = 0x49;
3609 view[-2] = 0xc7;
3610 view[-1] = 0xc0 | reg;
3611 }
3612 else if (reg == 4)
3613 {
3614 // Special handling for %rsp.
3615 if (op1 == 0x4c)
3616 view[-3] = 0x49;
3617 view[-2] = 0x81;
3618 view[-1] = 0xc0 | reg;
3619 }
3620 else
3621 {
3622 // addq
3623 if (op1 == 0x4c)
3624 view[-3] = 0x4d;
3625 view[-2] = 0x8d;
3626 view[-1] = 0x80 | reg | (reg << 3);
3627 }
3628
6a41d30b 3629 value -= tls_segment->memsz();
56622147
ILT
3630 Relocate_functions<64, false>::rela32(view, value, 0);
3631}
3632
2e30d253
ILT
3633// Relocate section data.
3634
3635void
364c7fa5
ILT
3636Target_x86_64::relocate_section(
3637 const Relocate_info<64, false>* relinfo,
3638 unsigned int sh_type,
3639 const unsigned char* prelocs,
3640 size_t reloc_count,
3641 Output_section* output_section,
3642 bool needs_special_offset_handling,
3643 unsigned char* view,
3644 elfcpp::Elf_types<64>::Elf_Addr address,
3645 section_size_type view_size,
3646 const Reloc_symbol_changes* reloc_symbol_changes)
2e30d253
ILT
3647{
3648 gold_assert(sh_type == elfcpp::SHT_RELA);
3649
3650 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3651 Target_x86_64::Relocate>(
3652 relinfo,
3653 this,
3654 prelocs,
3655 reloc_count,
730cdc88
ILT
3656 output_section,
3657 needs_special_offset_handling,
2e30d253
ILT
3658 view,
3659 address,
364c7fa5
ILT
3660 view_size,
3661 reloc_symbol_changes);
2e30d253
ILT
3662}
3663
94a3fc8b
CC
3664// Apply an incremental relocation. Incremental relocations always refer
3665// to global symbols.
3666
3667void
3668Target_x86_64::apply_relocation(
3669 const Relocate_info<64, false>* relinfo,
3670 elfcpp::Elf_types<64>::Elf_Addr r_offset,
3671 unsigned int r_type,
3672 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
3673 const Symbol* gsym,
3674 unsigned char* view,
3675 elfcpp::Elf_types<64>::Elf_Addr address,
3676 section_size_type view_size)
3677{
3678 gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>(
3679 relinfo,
3680 this,
3681 r_offset,
3682 r_type,
3683 r_addend,
3684 gsym,
3685 view,
3686 address,
3687 view_size);
3688}
3689
6a74a719
ILT
3690// Return the size of a relocation while scanning during a relocatable
3691// link.
3692
3693unsigned int
3694Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3695 unsigned int r_type,
3696 Relobj* object)
3697{
3698 switch (r_type)
3699 {
3700 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
3701 case elfcpp::R_X86_64_GNU_VTINHERIT:
3702 case elfcpp::R_X86_64_GNU_VTENTRY:
6a74a719
ILT
3703 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3704 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3705 case elfcpp::R_X86_64_TLSDESC_CALL:
3706 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3707 case elfcpp::R_X86_64_DTPOFF32:
3708 case elfcpp::R_X86_64_DTPOFF64:
3709 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3710 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3711 return 0;
3712
3713 case elfcpp::R_X86_64_64:
3714 case elfcpp::R_X86_64_PC64:
3715 case elfcpp::R_X86_64_GOTOFF64:
3716 case elfcpp::R_X86_64_GOTPC64:
3717 case elfcpp::R_X86_64_PLTOFF64:
3718 case elfcpp::R_X86_64_GOT64:
3719 case elfcpp::R_X86_64_GOTPCREL64:
3720 case elfcpp::R_X86_64_GOTPCREL:
3721 case elfcpp::R_X86_64_GOTPLT64:
3722 return 8;
3723
3724 case elfcpp::R_X86_64_32:
3725 case elfcpp::R_X86_64_32S:
3726 case elfcpp::R_X86_64_PC32:
3727 case elfcpp::R_X86_64_PLT32:
3728 case elfcpp::R_X86_64_GOTPC32:
3729 case elfcpp::R_X86_64_GOT32:
3730 return 4;
3731
3732 case elfcpp::R_X86_64_16:
3733 case elfcpp::R_X86_64_PC16:
3734 return 2;
3735
3736 case elfcpp::R_X86_64_8:
3737 case elfcpp::R_X86_64_PC8:
3738 return 1;
3739
3740 case elfcpp::R_X86_64_COPY:
3741 case elfcpp::R_X86_64_GLOB_DAT:
3742 case elfcpp::R_X86_64_JUMP_SLOT:
3743 case elfcpp::R_X86_64_RELATIVE:
7223e9ca 3744 case elfcpp::R_X86_64_IRELATIVE:
6a74a719
ILT
3745 // These are outstanding tls relocs, which are unexpected when linking
3746 case elfcpp::R_X86_64_TPOFF64:
3747 case elfcpp::R_X86_64_DTPMOD64:
3748 case elfcpp::R_X86_64_TLSDESC:
3749 object->error(_("unexpected reloc %u in object file"), r_type);
3750 return 0;
3751
3752 case elfcpp::R_X86_64_SIZE32:
3753 case elfcpp::R_X86_64_SIZE64:
3754 default:
3755 object->error(_("unsupported reloc %u against local symbol"), r_type);
3756 return 0;
3757 }
3758}
3759
3760// Scan the relocs during a relocatable link.
3761
3762void
ad0f2072 3763Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
6a74a719 3764 Layout* layout,
6fa2a40b 3765 Sized_relobj_file<64, false>* object,
6a74a719
ILT
3766 unsigned int data_shndx,
3767 unsigned int sh_type,
3768 const unsigned char* prelocs,
3769 size_t reloc_count,
3770 Output_section* output_section,
3771 bool needs_special_offset_handling,
3772 size_t local_symbol_count,
3773 const unsigned char* plocal_symbols,
3774 Relocatable_relocs* rr)
3775{
3776 gold_assert(sh_type == elfcpp::SHT_RELA);
3777
3778 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3779 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3780
7019cd25 3781 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
6a74a719 3782 Scan_relocatable_relocs>(
6a74a719
ILT
3783 symtab,
3784 layout,
3785 object,
3786 data_shndx,
3787 prelocs,
3788 reloc_count,
3789 output_section,
3790 needs_special_offset_handling,
3791 local_symbol_count,
3792 plocal_symbols,
3793 rr);
3794}
3795
3796// Relocate a section during a relocatable link.
3797
3798void
3799Target_x86_64::relocate_for_relocatable(
3800 const Relocate_info<64, false>* relinfo,
3801 unsigned int sh_type,
3802 const unsigned char* prelocs,
3803 size_t reloc_count,
3804 Output_section* output_section,
3805 off_t offset_in_output_section,
3806 const Relocatable_relocs* rr,
3807 unsigned char* view,
3808 elfcpp::Elf_types<64>::Elf_Addr view_address,
3809 section_size_type view_size,
3810 unsigned char* reloc_view,
3811 section_size_type reloc_view_size)
3812{
3813 gold_assert(sh_type == elfcpp::SHT_RELA);
3814
7019cd25 3815 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
6a74a719
ILT
3816 relinfo,
3817 prelocs,
3818 reloc_count,
3819 output_section,
3820 offset_in_output_section,
3821 rr,
3822 view,
3823 view_address,
3824 view_size,
3825 reloc_view,
3826 reloc_view_size);
3827}
3828
4fb6c25d
ILT
3829// Return the value to use for a dynamic which requires special
3830// treatment. This is how we support equality comparisons of function
3831// pointers across shared library boundaries, as described in the
3832// processor specific ABI supplement.
3833
3834uint64_t
3835Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3836{
3837 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
67181c72 3838 return this->plt_address_for_global(gsym) + gsym->plt_offset();
4fb6c25d
ILT
3839}
3840
2e30d253
ILT
3841// Return a string used to fill a code section with nops to take up
3842// the specified length.
3843
3844std::string
8851ecca 3845Target_x86_64::do_code_fill(section_size_type length) const
2e30d253
ILT
3846{
3847 if (length >= 16)
3848 {
3849 // Build a jmpq instruction to skip over the bytes.
3850 unsigned char jmp[5];
3851 jmp[0] = 0xe9;
04bf7072 3852 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2e30d253
ILT
3853 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3854 + std::string(length - 5, '\0'));
3855 }
3856
3857 // Nop sequences of various lengths.
3858 const char nop1[1] = { 0x90 }; // nop
3859 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1caf2c51
ILT
3860 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3861 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3862 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3863 0x00 };
3864 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
2e30d253 3865 0x00, 0x00 };
1caf2c51 3866 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
2e30d253 3867 0x00, 0x00, 0x00 };
1caf2c51
ILT
3868 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3869 0x00, 0x00, 0x00, 0x00 };
3870 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3871 0x00, 0x00, 0x00, 0x00,
2e30d253 3872 0x00 };
1caf2c51
ILT
3873 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3874 0x84, 0x00, 0x00, 0x00,
2e30d253 3875 0x00, 0x00 };
1caf2c51
ILT
3876 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3877 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 3878 0x00, 0x00, 0x00 };
1caf2c51
ILT
3879 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3880 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 3881 0x00, 0x00, 0x00, 0x00 };
1caf2c51
ILT
3882 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3883 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3884 0x00, 0x00, 0x00, 0x00,
2e30d253 3885 0x00 };
1caf2c51
ILT
3886 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3887 0x66, 0x2e, 0x0f, 0x1f, // data16
3888 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 3889 0x00, 0x00 };
1caf2c51
ILT
3890 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3891 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3892 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3893 0x00, 0x00, 0x00 };
2e30d253
ILT
3894
3895 const char* nops[16] = {
3896 NULL,
3897 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3898 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3899 };
3900
3901 return std::string(nops[length], length);
3902}
3903
e291e7b9
ILT
3904// Return the addend to use for a target specific relocation. The
3905// only target specific relocation is R_X86_64_TLSDESC for a local
3906// symbol. We want to set the addend is the offset of the local
3907// symbol in the TLS segment.
3908
3909uint64_t
3910Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3911 uint64_t) const
3912{
3913 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3914 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3915 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3916 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3917 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3918 gold_assert(psymval->is_tls_symbol());
3919 // The value of a TLS symbol is the offset in the TLS segment.
3920 return psymval->value(ti.object, 0);
3921}
3922
02d7cd44
ILT
3923// Return the value to use for the base of a DW_EH_PE_datarel offset
3924// in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3925// assembler can not write out the difference between two labels in
3926// different sections, so instead of using a pc-relative value they
3927// use an offset from the GOT.
3928
3929uint64_t
3930Target_x86_64::do_ehframe_datarel_base() const
3931{
3932 gold_assert(this->global_offset_table_ != NULL);
3933 Symbol* sym = this->global_offset_table_;
3934 Sized_symbol<64>* ssym = static_cast<Sized_symbol<64>*>(sym);
3935 return ssym->value();
3936}
3937
364c7fa5 3938// FNOFFSET in section SHNDX in OBJECT is the start of a function
9b547ce6 3939// compiled with -fsplit-stack. The function calls non-split-stack
364c7fa5
ILT
3940// code. We have to change the function so that it always ensures
3941// that it has enough stack space to run some random function.
3942
3943void
3944Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3945 section_offset_type fnoffset,
3946 section_size_type fnsize,
3947 unsigned char* view,
3948 section_size_type view_size,
3949 std::string* from,
3950 std::string* to) const
3951{
3952 // The function starts with a comparison of the stack pointer and a
3953 // field in the TCB. This is followed by a jump.
3954
3955 // cmp %fs:NN,%rsp
3956 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3957 && fnsize > 9)
3958 {
3959 // We will call __morestack if the carry flag is set after this
3960 // comparison. We turn the comparison into an stc instruction
3961 // and some nops.
3962 view[fnoffset] = '\xf9';
3963 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3964 }
3965 // lea NN(%rsp),%r10
cbc999b9
ILT
3966 // lea NN(%rsp),%r11
3967 else if ((this->match_view(view, view_size, fnoffset,
3968 "\x4c\x8d\x94\x24", 4)
3969 || this->match_view(view, view_size, fnoffset,
3970 "\x4c\x8d\x9c\x24", 4))
364c7fa5
ILT
3971 && fnsize > 8)
3972 {
3973 // This is loading an offset from the stack pointer for a
3974 // comparison. The offset is negative, so we decrease the
3975 // offset by the amount of space we need for the stack. This
3976 // means we will avoid calling __morestack if there happens to
3977 // be plenty of space on the stack already.
3978 unsigned char* pval = view + fnoffset + 4;
3979 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3980 val -= parameters->options().split_stack_adjust_size();
3981 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3982 }
3983 else
3984 {
3985 if (!object->has_no_split_stack())
3986 object->error(_("failed to match split-stack sequence at "
3987 "section %u offset %0zx"),
ac33a407 3988 shndx, static_cast<size_t>(fnoffset));
364c7fa5
ILT
3989 return;
3990 }
3991
3992 // We have to change the function so that it calls
3993 // __morestack_non_split instead of __morestack. The former will
3994 // allocate additional stack space.
3995 *from = "__morestack";
3996 *to = "__morestack_non_split";
3997}
3998
2e30d253
ILT
3999// The selector for x86_64 object files.
4000
36959681 4001class Target_selector_x86_64 : public Target_selector_freebsd
2e30d253
ILT
4002{
4003public:
4004 Target_selector_x86_64()
36959681 4005 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
03ef7571 4006 "elf64-x86-64-freebsd", "elf_x86_64")
2e30d253
ILT
4007 { }
4008
4009 Target*
e96caa79
ILT
4010 do_instantiate_target()
4011 { return new Target_x86_64(); }
36959681 4012
2e30d253
ILT
4013};
4014
2e30d253
ILT
4015Target_selector_x86_64 target_selector_x86_64;
4016
4017} // End anonymous namespace.
This page took 0.401994 seconds and 4 git commands to generate.