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