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