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