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