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