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