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