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