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