* Makefile.in, aclocal.m4, testsuite/Makefile.in: Rebuild.
[deliverable/binutils-gdb.git] / gold / x86_64.cc
CommitLineData
2e30d253
ILT
1// x86_64.cc -- x86_64 target support for gold.
2
ebdbb458 3// Copyright 2006, 2007, 2008 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"
28#include "parameters.h"
29#include "reloc.h"
30#include "x86_64.h"
31#include "object.h"
32#include "symtab.h"
33#include "layout.h"
34#include "output.h"
35#include "target.h"
36#include "target-reloc.h"
37#include "target-select.h"
e041f13d 38#include "tls.h"
2e30d253
ILT
39
40namespace
41{
42
43using namespace gold;
44
45class Output_data_plt_x86_64;
46
47// The x86_64 target class.
d61c17ea
ILT
48// See the ABI at
49// http://www.x86-64.org/documentation/abi.pdf
50// TLS info comes from
51// http://people.redhat.com/drepper/tls.pdf
0ffd9845 52// http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
2e30d253
ILT
53
54class Target_x86_64 : public Sized_target<64, false>
55{
56 public:
e822f2b1
ILT
57 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
58 // uses only Elf64_Rela relocation entries with explicit addends."
2e30d253
ILT
59 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
60
61 Target_x86_64()
62 : Sized_target<64, false>(&x86_64_info),
0ffd9845 63 got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL),
31d60480 64 copy_relocs_(NULL), dynbss_(NULL), got_mod_index_offset_(-1U)
2e30d253
ILT
65 { }
66
67 // Scan the relocations to look for symbol adjustments.
68 void
69 scan_relocs(const General_options& options,
70 Symbol_table* symtab,
71 Layout* layout,
72 Sized_relobj<64, false>* object,
73 unsigned int data_shndx,
74 unsigned int sh_type,
75 const unsigned char* prelocs,
76 size_t reloc_count,
730cdc88
ILT
77 Output_section* output_section,
78 bool needs_special_offset_handling,
2e30d253 79 size_t local_symbol_count,
730cdc88 80 const unsigned char* plocal_symbols);
2e30d253
ILT
81
82 // Finalize the sections.
83 void
84 do_finalize_sections(Layout*);
85
4fb6c25d
ILT
86 // Return the value to use for a dynamic which requires special
87 // treatment.
88 uint64_t
89 do_dynsym_value(const Symbol*) const;
90
2e30d253
ILT
91 // Relocate a section.
92 void
93 relocate_section(const Relocate_info<64, false>*,
94 unsigned int sh_type,
95 const unsigned char* prelocs,
96 size_t reloc_count,
730cdc88
ILT
97 Output_section* output_section,
98 bool needs_special_offset_handling,
2e30d253
ILT
99 unsigned char* view,
100 elfcpp::Elf_types<64>::Elf_Addr view_address,
fe8718a4 101 section_size_type view_size);
2e30d253 102
6a74a719
ILT
103 // Scan the relocs during a relocatable link.
104 void
105 scan_relocatable_relocs(const General_options& options,
106 Symbol_table* symtab,
107 Layout* layout,
108 Sized_relobj<64, false>* object,
109 unsigned int data_shndx,
110 unsigned int sh_type,
111 const unsigned char* prelocs,
112 size_t reloc_count,
113 Output_section* output_section,
114 bool needs_special_offset_handling,
115 size_t local_symbol_count,
116 const unsigned char* plocal_symbols,
117 Relocatable_relocs*);
118
119 // Relocate a section during a relocatable link.
120 void
121 relocate_for_relocatable(const Relocate_info<64, false>*,
122 unsigned int sh_type,
123 const unsigned char* prelocs,
124 size_t reloc_count,
125 Output_section* output_section,
126 off_t offset_in_output_section,
127 const Relocatable_relocs*,
128 unsigned char* view,
129 elfcpp::Elf_types<64>::Elf_Addr view_address,
130 section_size_type view_size,
131 unsigned char* reloc_view,
132 section_size_type reloc_view_size);
133
2e30d253
ILT
134 // Return a string used to fill a code section with nops.
135 std::string
8851ecca 136 do_code_fill(section_size_type length) const;
2e30d253 137
9a2d6984
ILT
138 // Return whether SYM is defined by the ABI.
139 bool
140 do_is_defined_by_abi(Symbol* sym) const
141 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
142
96f2030e 143 // Return the size of the GOT section.
fe8718a4 144 section_size_type
96f2030e
ILT
145 got_size()
146 {
147 gold_assert(this->got_ != NULL);
148 return this->got_->data_size();
149 }
150
2e30d253
ILT
151 private:
152 // The class which scans relocations.
a036edd8 153 class Scan
2e30d253 154 {
a036edd8
ILT
155 public:
156 Scan()
157 : issued_non_pic_error_(false)
158 { }
159
2e30d253
ILT
160 inline void
161 local(const General_options& options, Symbol_table* symtab,
162 Layout* layout, Target_x86_64* target,
163 Sized_relobj<64, false>* object,
164 unsigned int data_shndx,
07f397ab 165 Output_section* output_section,
2e30d253
ILT
166 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
167 const elfcpp::Sym<64, false>& lsym);
168
169 inline void
170 global(const General_options& options, Symbol_table* symtab,
171 Layout* layout, Target_x86_64* target,
172 Sized_relobj<64, false>* object,
173 unsigned int data_shndx,
07f397ab 174 Output_section* output_section,
2e30d253
ILT
175 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
176 Symbol* gsym);
e041f13d 177
a036edd8 178 private:
e041f13d
ILT
179 static void
180 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
181
182 static void
183 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
184 Symbol*);
a036edd8
ILT
185
186 void
187 check_non_pic(Relobj*, unsigned int r_type);
188
189 // Whether we have issued an error about a non-PIC compilation.
190 bool issued_non_pic_error_;
2e30d253
ILT
191 };
192
193 // The class which implements relocation.
194 class Relocate
195 {
196 public:
197 Relocate()
198 : skip_call_tls_get_addr_(false)
199 { }
200
201 ~Relocate()
202 {
203 if (this->skip_call_tls_get_addr_)
204 {
205 // FIXME: This needs to specify the location somehow.
a0c4fb0a 206 gold_error(_("missing expected TLS relocation"));
2e30d253
ILT
207 }
208 }
209
210 // Do a relocation. Return false if the caller should not issue
211 // any warnings about this relocation.
212 inline bool
213 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
214 const elfcpp::Rela<64, false>&,
215 unsigned int r_type, const Sized_symbol<64>*,
216 const Symbol_value<64>*,
217 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
fe8718a4 218 section_size_type);
2e30d253
ILT
219
220 private:
221 // Do a TLS relocation.
222 inline void
7bf1f802
ILT
223 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
224 size_t relnum, const elfcpp::Rela<64, false>&,
2e30d253
ILT
225 unsigned int r_type, const Sized_symbol<64>*,
226 const Symbol_value<64>*,
fe8718a4
ILT
227 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
228 section_size_type);
2e30d253 229
c2b45e22 230 // Do a TLS General-Dynamic to Initial-Exec transition.
7bf1f802
ILT
231 inline void
232 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
233 Output_segment* tls_segment,
234 const elfcpp::Rela<64, false>&, unsigned int r_type,
235 elfcpp::Elf_types<64>::Elf_Addr value,
236 unsigned char* view,
c2b45e22 237 elfcpp::Elf_types<64>::Elf_Addr,
fe8718a4 238 section_size_type view_size);
7bf1f802 239
56622147
ILT
240 // Do a TLS General-Dynamic to Local-Exec transition.
241 inline void
242 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
2e30d253
ILT
243 Output_segment* tls_segment,
244 const elfcpp::Rela<64, false>&, unsigned int r_type,
245 elfcpp::Elf_types<64>::Elf_Addr value,
246 unsigned char* view,
fe8718a4 247 section_size_type view_size);
2e30d253 248
c2b45e22
CC
249 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
250 inline void
251 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
252 Output_segment* tls_segment,
253 const elfcpp::Rela<64, false>&, unsigned int r_type,
254 elfcpp::Elf_types<64>::Elf_Addr value,
255 unsigned char* view,
256 elfcpp::Elf_types<64>::Elf_Addr,
257 section_size_type view_size);
258
259 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
260 inline void
261 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
262 Output_segment* tls_segment,
263 const elfcpp::Rela<64, false>&, unsigned int r_type,
264 elfcpp::Elf_types<64>::Elf_Addr value,
265 unsigned char* view,
266 section_size_type view_size);
267
56622147 268 // Do a TLS Local-Dynamic to Local-Exec transition.
2e30d253 269 inline void
56622147 270 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
2e30d253
ILT
271 Output_segment* tls_segment,
272 const elfcpp::Rela<64, false>&, unsigned int r_type,
273 elfcpp::Elf_types<64>::Elf_Addr value,
274 unsigned char* view,
fe8718a4 275 section_size_type view_size);
2e30d253 276
56622147
ILT
277 // Do a TLS Initial-Exec to Local-Exec transition.
278 static inline void
279 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
72ec2876
ILT
280 Output_segment* tls_segment,
281 const elfcpp::Rela<64, false>&, unsigned int r_type,
282 elfcpp::Elf_types<64>::Elf_Addr value,
283 unsigned char* view,
fe8718a4 284 section_size_type view_size);
2e30d253
ILT
285
286 // This is set if we should skip the next reloc, which should be a
287 // PLT32 reloc against ___tls_get_addr.
288 bool skip_call_tls_get_addr_;
289 };
290
6a74a719
ILT
291 // A class which returns the size required for a relocation type,
292 // used while scanning relocs during a relocatable link.
293 class Relocatable_size_for_reloc
294 {
295 public:
296 unsigned int
297 get_size_for_reloc(unsigned int, Relobj*);
298 };
299
2e30d253
ILT
300 // Adjust TLS relocation type based on the options and whether this
301 // is a local symbol.
e041f13d 302 static tls::Tls_optimization
2e30d253
ILT
303 optimize_tls_reloc(bool is_final, int r_type);
304
305 // Get the GOT section, creating it if necessary.
306 Output_data_got<64, false>*
307 got_section(Symbol_table*, Layout*);
308
96f2030e
ILT
309 // Get the GOT PLT section.
310 Output_data_space*
311 got_plt_section() const
312 {
313 gold_assert(this->got_plt_ != NULL);
314 return this->got_plt_;
315 }
316
c2b45e22
CC
317 // Create the PLT section.
318 void
319 make_plt_section(Symbol_table* symtab, Layout* layout);
320
2e30d253
ILT
321 // Create a PLT entry for a global symbol.
322 void
323 make_plt_entry(Symbol_table*, Layout*, Symbol*);
324
c2b45e22
CC
325 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
326 void
327 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
328
31d60480
ILT
329 // Create a GOT entry for the TLS module index.
330 unsigned int
331 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
332 Sized_relobj<64, false>* object);
333
2e30d253
ILT
334 // Get the PLT section.
335 Output_data_plt_x86_64*
336 plt_section() const
337 {
338 gold_assert(this->plt_ != NULL);
339 return this->plt_;
340 }
341
342 // Get the dynamic reloc section, creating it if necessary.
343 Reloc_section*
0ffd9845 344 rela_dyn_section(Layout*);
2e30d253 345
d61c6bd4
ILT
346 // Return true if the symbol may need a COPY relocation.
347 // References from an executable object to non-function symbols
348 // defined in a dynamic object may need a COPY relocation.
349 bool
350 may_need_copy_reloc(Symbol* gsym)
351 {
8851ecca 352 return (!parameters->options().shared()
d61c6bd4
ILT
353 && gsym->is_from_dynobj()
354 && gsym->type() != elfcpp::STT_FUNC);
355 }
356
2e30d253
ILT
357 // Copy a relocation against a global symbol.
358 void
359 copy_reloc(const General_options*, Symbol_table*, Layout*,
360 Sized_relobj<64, false>*, unsigned int,
4f4c5f80 361 Output_section*, Symbol*, const elfcpp::Rela<64, false>&);
2e30d253
ILT
362
363 // Information about this specific target which we pass to the
364 // general Target structure.
365 static const Target::Target_info x86_64_info;
366
0a65a3a7
CC
367 enum Got_type
368 {
369 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
370 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
371 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
372 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
373 };
374
2e30d253
ILT
375 // The GOT section.
376 Output_data_got<64, false>* got_;
377 // The PLT section.
378 Output_data_plt_x86_64* plt_;
379 // The GOT PLT section.
380 Output_data_space* got_plt_;
381 // The dynamic reloc section.
0ffd9845 382 Reloc_section* rela_dyn_;
2e30d253
ILT
383 // Relocs saved to avoid a COPY reloc.
384 Copy_relocs<64, false>* copy_relocs_;
385 // Space for variables copied with a COPY reloc.
386 Output_data_space* dynbss_;
c2b45e22 387 // Offset of the GOT entry for the TLS module index.
31d60480 388 unsigned int got_mod_index_offset_;
2e30d253
ILT
389};
390
391const Target::Target_info Target_x86_64::x86_64_info =
392{
393 64, // size
394 false, // is_big_endian
395 elfcpp::EM_X86_64, // machine_code
396 false, // has_make_symbol
397 false, // has_resolve
398 true, // has_code_fill
35cdfc9a 399 true, // is_default_stack_executable
0864d551 400 '\0', // wrap_char
2e30d253 401 "/lib/ld64.so.1", // program interpreter
0c5e9c22 402 0x400000, // default_text_segment_address
cd72c291
ILT
403 0x1000, // abi_pagesize (overridable by -z max-page-size)
404 0x1000 // common_pagesize (overridable by -z common-page-size)
2e30d253
ILT
405};
406
407// Get the GOT section, creating it if necessary.
408
409Output_data_got<64, false>*
410Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
411{
412 if (this->got_ == NULL)
413 {
414 gold_assert(symtab != NULL && layout != NULL);
415
416 this->got_ = new Output_data_got<64, false>();
417
418 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
419 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
420 this->got_);
421
422 // The old GNU linker creates a .got.plt section. We just
423 // create another set of data in the .got section. Note that we
424 // always create a PLT if we create a GOT, although the PLT
425 // might be empty.
2e30d253
ILT
426 this->got_plt_ = new Output_data_space(8);
427 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
428 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
429 this->got_plt_);
430
431 // The first three entries are reserved.
27bc2bce 432 this->got_plt_->set_current_data_size(3 * 8);
2e30d253
ILT
433
434 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
9b07f471 435 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2e30d253
ILT
436 this->got_plt_,
437 0, 0, elfcpp::STT_OBJECT,
438 elfcpp::STB_LOCAL,
439 elfcpp::STV_HIDDEN, 0,
440 false, false);
441 }
442
443 return this->got_;
444}
445
446// Get the dynamic reloc section, creating it if necessary.
447
448Target_x86_64::Reloc_section*
0ffd9845 449Target_x86_64::rela_dyn_section(Layout* layout)
2e30d253 450{
0ffd9845 451 if (this->rela_dyn_ == NULL)
2e30d253
ILT
452 {
453 gold_assert(layout != NULL);
0ffd9845 454 this->rela_dyn_ = new Reloc_section();
2e30d253 455 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
0ffd9845 456 elfcpp::SHF_ALLOC, this->rela_dyn_);
2e30d253 457 }
0ffd9845 458 return this->rela_dyn_;
2e30d253
ILT
459}
460
461// A class to handle the PLT data.
462
463class Output_data_plt_x86_64 : public Output_section_data
464{
465 public:
466 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
467
c2b45e22
CC
468 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
469 Output_data_space*);
2e30d253
ILT
470
471 // Add an entry to the PLT.
472 void
473 add_entry(Symbol* gsym);
474
c2b45e22
CC
475 // Add the reserved TLSDESC_PLT entry to the PLT.
476 void
477 reserve_tlsdesc_entry(unsigned int got_offset)
478 { this->tlsdesc_got_offset_ = got_offset; }
479
480 // Return true if a TLSDESC_PLT entry has been reserved.
481 bool
482 has_tlsdesc_entry() const
483 { return this->tlsdesc_got_offset_ != -1U; }
484
485 // Return the GOT offset for the reserved TLSDESC_PLT entry.
486 unsigned int
487 get_tlsdesc_got_offset() const
488 { return this->tlsdesc_got_offset_; }
489
490 // Return the offset of the reserved TLSDESC_PLT entry.
491 unsigned int
492 get_tlsdesc_plt_offset() const
493 { return (this->count_ + 1) * plt_entry_size; }
494
2e30d253
ILT
495 // Return the .rel.plt section data.
496 const Reloc_section*
497 rel_plt() const
498 { return this->rel_; }
499
500 protected:
501 void
502 do_adjust_output_section(Output_section* os);
503
504 private:
505 // The size of an entry in the PLT.
506 static const int plt_entry_size = 16;
507
508 // The first entry in the PLT.
509 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
510 // procedure linkage table for both programs and shared objects."
511 static unsigned char first_plt_entry[plt_entry_size];
512
513 // Other entries in the PLT for an executable.
514 static unsigned char plt_entry[plt_entry_size];
515
c2b45e22
CC
516 // The reserved TLSDESC entry in the PLT for an executable.
517 static unsigned char tlsdesc_plt_entry[plt_entry_size];
518
2e30d253
ILT
519 // Set the final size.
520 void
c2b45e22 521 set_final_data_size();
2e30d253
ILT
522
523 // Write out the PLT data.
524 void
525 do_write(Output_file*);
526
527 // The reloc section.
528 Reloc_section* rel_;
c2b45e22
CC
529 // The .got section.
530 Output_data_got<64, false>* got_;
2e30d253
ILT
531 // The .got.plt section.
532 Output_data_space* got_plt_;
533 // The number of PLT entries.
534 unsigned int count_;
c2b45e22
CC
535 // Offset of the reserved TLSDESC_GOT entry when needed.
536 unsigned int tlsdesc_got_offset_;
2e30d253
ILT
537};
538
539// Create the PLT section. The ordinary .got section is an argument,
540// since we need to refer to the start. We also create our own .got
541// section just for PLT entries.
542
543Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
c2b45e22 544 Output_data_got<64, false>* got,
2e30d253 545 Output_data_space* got_plt)
c2b45e22
CC
546 : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0),
547 tlsdesc_got_offset_(-1U)
2e30d253
ILT
548{
549 this->rel_ = new Reloc_section();
550 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
551 elfcpp::SHF_ALLOC, this->rel_);
552}
553
554void
555Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
556{
557 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
558 // linker, and so do we.
559 os->set_entsize(4);
560}
561
562// Add an entry to the PLT.
563
564void
565Output_data_plt_x86_64::add_entry(Symbol* gsym)
566{
567 gold_assert(!gsym->has_plt_offset());
568
569 // Note that when setting the PLT offset we skip the initial
570 // reserved PLT entry.
571 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
572
573 ++this->count_;
574
fe8718a4 575 section_offset_type got_offset = this->got_plt_->current_data_size();
2e30d253
ILT
576
577 // Every PLT entry needs a GOT entry which points back to the PLT
578 // entry (this will be changed by the dynamic linker, normally
579 // lazily when the function is called).
27bc2bce 580 this->got_plt_->set_current_data_size(got_offset + 8);
2e30d253
ILT
581
582 // Every PLT entry needs a reloc.
583 gsym->set_needs_dynsym_entry();
584 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
585 got_offset, 0);
586
587 // Note that we don't need to save the symbol. The contents of the
588 // PLT are independent of which symbols are used. The symbols only
589 // appear in the relocations.
590}
591
c2b45e22
CC
592// Set the final size.
593void
594Output_data_plt_x86_64::set_final_data_size()
595{
596 unsigned int count = this->count_;
597 if (this->has_tlsdesc_entry())
598 ++count;
599 this->set_data_size((count + 1) * plt_entry_size);
600}
601
2e30d253
ILT
602// The first entry in the PLT for an executable.
603
604unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
605{
606 // From AMD64 ABI Draft 0.98, page 76
607 0xff, 0x35, // pushq contents of memory address
2e30d253 608 0, 0, 0, 0, // replaced with address of .got + 8
78d911fd
ILT
609 0xff, 0x25, // jmp indirect
610 0, 0, 0, 0, // replaced with address of .got + 16
2e30d253
ILT
611 0x90, 0x90, 0x90, 0x90 // noop (x4)
612};
613
614// Subsequent entries in the PLT for an executable.
615
616unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
617{
618 // From AMD64 ABI Draft 0.98, page 76
619 0xff, 0x25, // jmpq indirect
620 0, 0, 0, 0, // replaced with address of symbol in .got
621 0x68, // pushq immediate
622 0, 0, 0, 0, // replaced with offset into relocation table
623 0xe9, // jmpq relative
624 0, 0, 0, 0 // replaced with offset to start of .plt
625};
626
c2b45e22
CC
627// The reserved TLSDESC entry in the PLT for an executable.
628
629unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
630{
631 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
632 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
633 0xff, 0x35, // pushq x(%rip)
634 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
635 0xff, 0x25, // jmpq *y(%rip)
636 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
637 0x0f, 0x1f, // nop
638 0x40, 0
639};
640
2e30d253
ILT
641// Write out the PLT. This uses the hand-coded instructions above,
642// and adjusts them as needed. This is specified by the AMD64 ABI.
643
644void
645Output_data_plt_x86_64::do_write(Output_file* of)
646{
647 const off_t offset = this->offset();
fe8718a4
ILT
648 const section_size_type oview_size =
649 convert_to_section_size_type(this->data_size());
2e30d253
ILT
650 unsigned char* const oview = of->get_output_view(offset, oview_size);
651
652 const off_t got_file_offset = this->got_plt_->offset();
fe8718a4
ILT
653 const section_size_type got_size =
654 convert_to_section_size_type(this->got_plt_->data_size());
2e30d253
ILT
655 unsigned char* const got_view = of->get_output_view(got_file_offset,
656 got_size);
657
658 unsigned char* pov = oview;
659
c2b45e22 660 // The base address of the .plt section.
2e30d253 661 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
c2b45e22
CC
662 // The base address of the .got section.
663 elfcpp::Elf_types<32>::Elf_Addr got_base = this->got_->address();
664 // The base address of the PLT portion of the .got section,
665 // which is where the GOT pointer will point, and where the
666 // three reserved GOT entries are located.
2e30d253
ILT
667 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
668
669 memcpy(pov, first_plt_entry, plt_entry_size);
78d911fd
ILT
670 // We do a jmp relative to the PC at the end of this instruction.
671 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
672 - (plt_address + 6));
673 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
674 - (plt_address + 12));
2e30d253
ILT
675 pov += plt_entry_size;
676
677 unsigned char* got_pov = got_view;
678
679 memset(got_pov, 0, 24);
680 got_pov += 24;
681
682 unsigned int plt_offset = plt_entry_size;
683 unsigned int got_offset = 24;
684 const unsigned int count = this->count_;
685 for (unsigned int plt_index = 0;
686 plt_index < count;
687 ++plt_index,
688 pov += plt_entry_size,
689 got_pov += 8,
690 plt_offset += plt_entry_size,
691 got_offset += 8)
692 {
693 // Set and adjust the PLT entry itself.
694 memcpy(pov, plt_entry, plt_entry_size);
78d911fd
ILT
695 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
696 (got_address + got_offset
697 - (plt_address + plt_offset
698 + 6)));
2e30d253
ILT
699
700 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
701 elfcpp::Swap<32, false>::writeval(pov + 12,
702 - (plt_offset + plt_entry_size));
703
704 // Set the entry in the GOT.
705 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
706 }
707
c2b45e22
CC
708 if (this->has_tlsdesc_entry())
709 {
710 // Set and adjust the reserved TLSDESC PLT entry.
711 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
712 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
713 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
714 (got_address + 8
715 - (plt_address + plt_offset
716 + 6)));
717 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
718 (got_base
719 + tlsdesc_got_offset
720 - (plt_address + plt_offset
721 + 12)));
722 pov += plt_entry_size;
723 }
724
fe8718a4
ILT
725 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
726 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2e30d253
ILT
727
728 of->write_output_view(offset, oview_size, oview);
729 of->write_output_view(got_file_offset, got_size, got_view);
730}
731
c2b45e22 732// Create the PLT section.
2e30d253
ILT
733
734void
c2b45e22 735Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
2e30d253 736{
2e30d253
ILT
737 if (this->plt_ == NULL)
738 {
739 // Create the GOT sections first.
740 this->got_section(symtab, layout);
741
c2b45e22
CC
742 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
743 this->got_plt_);
2e30d253
ILT
744 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
745 (elfcpp::SHF_ALLOC
746 | elfcpp::SHF_EXECINSTR),
747 this->plt_);
748 }
c2b45e22
CC
749}
750
751// Create a PLT entry for a global symbol.
752
753void
754Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
755 Symbol* gsym)
756{
757 if (gsym->has_plt_offset())
758 return;
759
760 if (this->plt_ == NULL)
761 this->make_plt_section(symtab, layout);
2e30d253
ILT
762
763 this->plt_->add_entry(gsym);
764}
765
c2b45e22
CC
766// Create the reserved PLT and GOT entries for the TLS descriptor resolver.
767
768void
769Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
770 Layout* layout)
771{
772 if (this->plt_ == NULL)
773 this->make_plt_section(symtab, layout);
774
775 if (!this->plt_->has_tlsdesc_entry())
776 {
777 // Allocate the TLSDESC_GOT entry.
778 Output_data_got<64, false>* got = this->got_section(symtab, layout);
779 unsigned int got_offset = got->add_constant(0);
780
781 // Allocate the TLSDESC_PLT entry.
782 this->plt_->reserve_tlsdesc_entry(got_offset);
783 }
784}
785
31d60480
ILT
786// Create a GOT entry for the TLS module index.
787
788unsigned int
789Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
790 Sized_relobj<64, false>* object)
791{
792 if (this->got_mod_index_offset_ == -1U)
793 {
794 gold_assert(symtab != NULL && layout != NULL && object != NULL);
795 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
796 Output_data_got<64, false>* got = this->got_section(symtab, layout);
797 unsigned int got_offset = got->add_constant(0);
798 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
799 got_offset, 0);
009a67a2 800 got->add_constant(0);
31d60480
ILT
801 this->got_mod_index_offset_ = got_offset;
802 }
803 return this->got_mod_index_offset_;
804}
805
2e30d253
ILT
806// Handle a relocation against a non-function symbol defined in a
807// dynamic object. The traditional way to handle this is to generate
808// a COPY relocation to copy the variable at runtime from the shared
809// object into the executable's data segment. However, this is
810// undesirable in general, as if the size of the object changes in the
811// dynamic object, the executable will no longer work correctly. If
812// this relocation is in a writable section, then we can create a
813// dynamic reloc and the dynamic linker will resolve it to the correct
814// address at runtime. However, we do not want do that if the
815// relocation is in a read-only section, as it would prevent the
816// readonly segment from being shared. And if we have to eventually
817// generate a COPY reloc, then any dynamic relocations will be
818// useless. So this means that if this is a writable section, we need
819// to save the relocation until we see whether we have to create a
820// COPY relocation for this symbol for any other relocation.
821
822void
823Target_x86_64::copy_reloc(const General_options* options,
d61c17ea
ILT
824 Symbol_table* symtab,
825 Layout* layout,
826 Sized_relobj<64, false>* object,
4f4c5f80
ILT
827 unsigned int data_shndx,
828 Output_section* output_section,
829 Symbol* gsym,
72ec2876 830 const elfcpp::Rela<64, false>& rela)
2e30d253 831{
7d1a9ebb 832 Sized_symbol<64>* ssym = symtab->get_sized_symbol<64>(gsym);
2e30d253
ILT
833
834 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
835 data_shndx, ssym))
836 {
837 // So far we do not need a COPY reloc. Save this relocation.
838 // If it turns out that we never need a COPY reloc for this
839 // symbol, then we will emit the relocation.
840 if (this->copy_relocs_ == NULL)
841 this->copy_relocs_ = new Copy_relocs<64, false>();
4f4c5f80 842 this->copy_relocs_->save(ssym, object, data_shndx, output_section, rela);
2e30d253
ILT
843 }
844 else
845 {
846 // Allocate space for this symbol in the .bss section.
847
848 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
849
850 // There is no defined way to determine the required alignment
851 // of the symbol. We pick the alignment based on the size. We
852 // set an arbitrary maximum of 256.
853 unsigned int align;
854 for (align = 1; align < 512; align <<= 1)
855 if ((symsize & align) != 0)
856 break;
857
858 if (this->dynbss_ == NULL)
859 {
860 this->dynbss_ = new Output_data_space(align);
861 layout->add_output_section_data(".bss",
862 elfcpp::SHT_NOBITS,
863 (elfcpp::SHF_ALLOC
864 | elfcpp::SHF_WRITE),
865 this->dynbss_);
866 }
867
868 Output_data_space* dynbss = this->dynbss_;
869
870 if (align > dynbss->addralign())
871 dynbss->set_space_alignment(align);
872
fe8718a4 873 section_size_type dynbss_size = dynbss->current_data_size();
2e30d253 874 dynbss_size = align_address(dynbss_size, align);
fe8718a4 875 section_size_type offset = dynbss_size;
27bc2bce 876 dynbss->set_current_data_size(dynbss_size + symsize);
2e30d253 877
9b07f471 878 symtab->define_with_copy_reloc(ssym, dynbss, offset);
2e30d253
ILT
879
880 // Add the COPY reloc.
0ffd9845
ILT
881 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
882 rela_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0);
2e30d253
ILT
883 }
884}
885
886
887// Optimize the TLS relocation type based on what we know about the
888// symbol. IS_FINAL is true if the final address of this symbol is
889// known at link time.
890
e041f13d 891tls::Tls_optimization
2e30d253
ILT
892Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
893{
2e30d253
ILT
894 // If we are generating a shared library, then we can't do anything
895 // in the linker.
8851ecca 896 if (parameters->options().shared())
e041f13d 897 return tls::TLSOPT_NONE;
2e30d253
ILT
898
899 switch (r_type)
900 {
901 case elfcpp::R_X86_64_TLSGD:
e041f13d
ILT
902 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
903 case elfcpp::R_X86_64_TLSDESC_CALL:
904 // These are General-Dynamic which permits fully general TLS
2e30d253
ILT
905 // access. Since we know that we are generating an executable,
906 // we can convert this to Initial-Exec. If we also know that
907 // this is a local symbol, we can further switch to Local-Exec.
908 if (is_final)
e041f13d
ILT
909 return tls::TLSOPT_TO_LE;
910 return tls::TLSOPT_TO_IE;
2e30d253 911
d61c17ea 912 case elfcpp::R_X86_64_TLSLD:
2e30d253
ILT
913 // This is Local-Dynamic, which refers to a local symbol in the
914 // dynamic TLS block. Since we know that we generating an
915 // executable, we can switch to Local-Exec.
e041f13d 916 return tls::TLSOPT_TO_LE;
2e30d253 917
0ffd9845 918 case elfcpp::R_X86_64_DTPOFF32:
0ffd9845
ILT
919 case elfcpp::R_X86_64_DTPOFF64:
920 // Another Local-Dynamic reloc.
e041f13d 921 return tls::TLSOPT_TO_LE;
0ffd9845 922
d61c17ea 923 case elfcpp::R_X86_64_GOTTPOFF:
2e30d253
ILT
924 // These are Initial-Exec relocs which get the thread offset
925 // from the GOT. If we know that we are linking against the
926 // local symbol, we can switch to Local-Exec, which links the
927 // thread offset into the instruction.
928 if (is_final)
e041f13d
ILT
929 return tls::TLSOPT_TO_LE;
930 return tls::TLSOPT_NONE;
2e30d253 931
d61c17ea 932 case elfcpp::R_X86_64_TPOFF32:
2e30d253
ILT
933 // When we already have Local-Exec, there is nothing further we
934 // can do.
e041f13d 935 return tls::TLSOPT_NONE;
2e30d253
ILT
936
937 default:
938 gold_unreachable();
939 }
2e30d253
ILT
940}
941
e041f13d
ILT
942// Report an unsupported relocation against a local symbol.
943
944void
945Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
946 unsigned int r_type)
947{
75f2446e
ILT
948 gold_error(_("%s: unsupported reloc %u against local symbol"),
949 object->name().c_str(), r_type);
e041f13d
ILT
950}
951
a036edd8
ILT
952// We are about to emit a dynamic relocation of type R_TYPE. If the
953// dynamic linker does not support it, issue an error. The GNU linker
954// only issues a non-PIC error for an allocated read-only section.
955// Here we know the section is allocated, but we don't know that it is
956// read-only. But we check for all the relocation types which the
957// glibc dynamic linker supports, so it seems appropriate to issue an
958// error even if the section is not read-only.
959
960void
961Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
962{
963 switch (r_type)
964 {
965 // These are the relocation types supported by glibc for x86_64.
966 case elfcpp::R_X86_64_RELATIVE:
967 case elfcpp::R_X86_64_GLOB_DAT:
968 case elfcpp::R_X86_64_JUMP_SLOT:
969 case elfcpp::R_X86_64_DTPMOD64:
970 case elfcpp::R_X86_64_DTPOFF64:
971 case elfcpp::R_X86_64_TPOFF64:
972 case elfcpp::R_X86_64_64:
973 case elfcpp::R_X86_64_32:
974 case elfcpp::R_X86_64_PC32:
975 case elfcpp::R_X86_64_COPY:
976 return;
977
978 default:
979 // This prevents us from issuing more than one error per reloc
980 // section. But we can still wind up issuing more than one
981 // error per object file.
982 if (this->issued_non_pic_error_)
983 return;
984 object->error(_("requires unsupported dynamic reloc; "
985 "recompile with -fPIC"));
986 this->issued_non_pic_error_ = true;
987 return;
988
989 case elfcpp::R_X86_64_NONE:
990 gold_unreachable();
991 }
992}
993
2e30d253
ILT
994// Scan a relocation for a local symbol.
995
996inline void
997Target_x86_64::Scan::local(const General_options&,
d61c17ea
ILT
998 Symbol_table* symtab,
999 Layout* layout,
1000 Target_x86_64* target,
1001 Sized_relobj<64, false>* object,
0ffd9845 1002 unsigned int data_shndx,
4f4c5f80 1003 Output_section* output_section,
0ffd9845 1004 const elfcpp::Rela<64, false>& reloc,
d61c17ea 1005 unsigned int r_type,
7bf1f802 1006 const elfcpp::Sym<64, false>& lsym)
2e30d253
ILT
1007{
1008 switch (r_type)
1009 {
1010 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
1011 case elfcpp::R_386_GNU_VTINHERIT:
1012 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
1013 break;
1014
1015 case elfcpp::R_X86_64_64:
d61c6bd4 1016 // If building a shared library (or a position-independent
dceae3c1
ILT
1017 // executable), we need to create a dynamic relocation for this
1018 // location. The relocation applied at link time will apply the
1019 // link-time value, so we flag the location with an
1020 // R_X86_64_RELATIVE relocation so the dynamic loader can
d61c6bd4 1021 // relocate it easily.
8851ecca 1022 if (parameters->options().output_is_position_independent())
d61c6bd4 1023 {
e8c846c3 1024 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d61c6bd4 1025 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
e8c846c3
ILT
1026 rela_dyn->add_local_relative(object, r_sym,
1027 elfcpp::R_X86_64_RELATIVE,
1028 output_section, data_shndx,
1029 reloc.get_r_offset(),
1030 reloc.get_r_addend());
d61c6bd4
ILT
1031 }
1032 break;
1033
2e30d253
ILT
1034 case elfcpp::R_X86_64_32:
1035 case elfcpp::R_X86_64_32S:
1036 case elfcpp::R_X86_64_16:
1037 case elfcpp::R_X86_64_8:
96f2030e 1038 // If building a shared library (or a position-independent
dceae3c1
ILT
1039 // executable), we need to create a dynamic relocation for this
1040 // location. We can't use an R_X86_64_RELATIVE relocation
1041 // because that is always a 64-bit relocation.
8851ecca 1042 if (parameters->options().output_is_position_independent())
96f2030e 1043 {
a036edd8
ILT
1044 this->check_non_pic(object, r_type);
1045
96f2030e 1046 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
dceae3c1
ILT
1047 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1048 {
1049 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1050 rela_dyn->add_local(object, r_sym, r_type, output_section,
1051 data_shndx, reloc.get_r_offset(),
1052 reloc.get_r_addend());
1053 }
1054 else
1055 {
1056 gold_assert(lsym.get_st_value() == 0);
1057 rela_dyn->add_local_section(object, lsym.get_st_shndx(),
1058 r_type, output_section,
1059 data_shndx, reloc.get_r_offset(),
1060 reloc.get_r_addend());
1061 }
96f2030e 1062 }
2e30d253
ILT
1063 break;
1064
1065 case elfcpp::R_X86_64_PC64:
1066 case elfcpp::R_X86_64_PC32:
1067 case elfcpp::R_X86_64_PC16:
1068 case elfcpp::R_X86_64_PC8:
1069 break;
1070
f389a824
ILT
1071 case elfcpp::R_X86_64_PLT32:
1072 // Since we know this is a local symbol, we can handle this as a
1073 // PC32 reloc.
1074 break;
1075
fdc2f80f 1076 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1077 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1078 case elfcpp::R_X86_64_GOTPC64:
1079 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1080 // We need a GOT section.
1081 target->got_section(symtab, layout);
ee9e9e86
ILT
1082 // For PLTOFF64, we'd normally want a PLT section, but since we
1083 // know this is a local symbol, no PLT is needed.
2e30d253
ILT
1084 break;
1085
0ffd9845
ILT
1086 case elfcpp::R_X86_64_GOT64:
1087 case elfcpp::R_X86_64_GOT32:
1088 case elfcpp::R_X86_64_GOTPCREL64:
1089 case elfcpp::R_X86_64_GOTPCREL:
ee9e9e86 1090 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
1091 {
1092 // The symbol requires a GOT entry.
1093 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1094 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7 1095 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
0ffd9845
ILT
1096 {
1097 // If we are generating a shared object, we need to add a
7bf1f802 1098 // dynamic relocation for this symbol's GOT entry.
8851ecca 1099 if (parameters->options().output_is_position_independent())
0ffd9845
ILT
1100 {
1101 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7bf1f802
ILT
1102 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1103 if (r_type != elfcpp::R_X86_64_GOT32)
0a65a3a7
CC
1104 rela_dyn->add_local_relative(
1105 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1106 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
7bf1f802 1107 else
dceae3c1 1108 {
a036edd8
ILT
1109 this->check_non_pic(object, r_type);
1110
dceae3c1 1111 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
0a65a3a7
CC
1112 rela_dyn->add_local(
1113 object, r_sym, r_type, got,
1114 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
dceae3c1 1115 }
0ffd9845
ILT
1116 }
1117 }
ee9e9e86
ILT
1118 // For GOTPLT64, we'd normally want a PLT section, but since
1119 // we know this is a local symbol, no PLT is needed.
0ffd9845
ILT
1120 }
1121 break;
1122
2e30d253
ILT
1123 case elfcpp::R_X86_64_COPY:
1124 case elfcpp::R_X86_64_GLOB_DAT:
1125 case elfcpp::R_X86_64_JUMP_SLOT:
1126 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1127 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 1128 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1129 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 1130 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1131 gold_error(_("%s: unexpected reloc %u in object file"),
1132 object->name().c_str(), r_type);
2e30d253
ILT
1133 break;
1134
d61c17ea 1135 // These are initial tls relocs, which are expected when linking
56622147
ILT
1136 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1137 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1138 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1139 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1140 case elfcpp::R_X86_64_DTPOFF32:
1141 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1142 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1143 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253 1144 {
8851ecca 1145 bool output_is_shared = parameters->options().shared();
e041f13d
ILT
1146 const tls::Tls_optimization optimized_type
1147 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
2e30d253
ILT
1148 switch (r_type)
1149 {
56622147 1150 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
1151 if (optimized_type == tls::TLSOPT_NONE)
1152 {
1153 // Create a pair of GOT entries for the module index and
1154 // dtv-relative offset.
1155 Output_data_got<64, false>* got
1156 = target->got_section(symtab, layout);
1157 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7
CC
1158 got->add_local_pair_with_rela(object, r_sym,
1159 lsym.get_st_shndx(),
1160 GOT_TYPE_TLS_PAIR,
1161 target->rela_dyn_section(layout),
1162 elfcpp::R_X86_64_DTPMOD64, 0);
7bf1f802
ILT
1163 }
1164 else if (optimized_type != tls::TLSOPT_TO_LE)
1165 unsupported_reloc_local(object, r_type);
1166 break;
1167
56622147 1168 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
c2b45e22
CC
1169 if (optimized_type == tls::TLSOPT_NONE)
1170 {
1171 // Create reserved PLT and GOT entries for the resolver.
1172 target->reserve_tlsdesc_entries(symtab, layout);
1173
1174 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1175 Output_data_got<64, false>* got
1176 = target->got_section(symtab, layout);
1177 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1178 got->add_local_pair_with_rela(object, r_sym,
1179 lsym.get_st_shndx(),
1180 GOT_TYPE_TLS_DESC,
1181 target->rela_dyn_section(layout),
1182 elfcpp::R_X86_64_TLSDESC, 0);
1183 }
1184 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 1185 unsupported_reloc_local(object, r_type);
2e30d253
ILT
1186 break;
1187
c2b45e22
CC
1188 case elfcpp::R_X86_64_TLSDESC_CALL:
1189 break;
1190
e041f13d 1191 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
1192 if (optimized_type == tls::TLSOPT_NONE)
1193 {
1194 // Create a GOT entry for the module index.
31d60480 1195 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
1196 }
1197 else if (optimized_type != tls::TLSOPT_TO_LE)
1198 unsupported_reloc_local(object, r_type);
1199 break;
1200
0ffd9845
ILT
1201 case elfcpp::R_X86_64_DTPOFF32:
1202 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
1203 break;
1204
56622147 1205 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 1206 layout->set_has_static_tls();
7bf1f802
ILT
1207 if (optimized_type == tls::TLSOPT_NONE)
1208 {
1209 // Create a GOT entry for the tp-relative offset.
1210 Output_data_got<64, false>* got
1211 = target->got_section(symtab, layout);
1212 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7 1213 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1214 target->rela_dyn_section(layout),
1215 elfcpp::R_X86_64_TPOFF64);
1216 }
1217 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
1218 unsupported_reloc_local(object, r_type);
1219 break;
0ffd9845 1220
56622147 1221 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 1222 layout->set_has_static_tls();
7bf1f802
ILT
1223 if (output_is_shared)
1224 unsupported_reloc_local(object, r_type);
2e30d253 1225 break;
e041f13d
ILT
1226
1227 default:
1228 gold_unreachable();
2e30d253
ILT
1229 }
1230 }
1231 break;
2e30d253 1232
fdc2f80f
ILT
1233 case elfcpp::R_X86_64_SIZE32:
1234 case elfcpp::R_X86_64_SIZE64:
2e30d253 1235 default:
75f2446e
ILT
1236 gold_error(_("%s: unsupported reloc %u against local symbol"),
1237 object->name().c_str(), r_type);
2e30d253
ILT
1238 break;
1239 }
1240}
1241
1242
e041f13d
ILT
1243// Report an unsupported relocation against a global symbol.
1244
1245void
1246Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1247 unsigned int r_type,
1248 Symbol* gsym)
1249{
75f2446e 1250 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12 1251 object->name().c_str(), r_type, gsym->demangled_name().c_str());
e041f13d
ILT
1252}
1253
2e30d253
ILT
1254// Scan a relocation for a global symbol.
1255
1256inline void
1257Target_x86_64::Scan::global(const General_options& options,
d61c17ea
ILT
1258 Symbol_table* symtab,
1259 Layout* layout,
1260 Target_x86_64* target,
1261 Sized_relobj<64, false>* object,
1262 unsigned int data_shndx,
4f4c5f80 1263 Output_section* output_section,
d61c17ea
ILT
1264 const elfcpp::Rela<64, false>& reloc,
1265 unsigned int r_type,
1266 Symbol* gsym)
2e30d253
ILT
1267{
1268 switch (r_type)
1269 {
1270 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
1271 case elfcpp::R_386_GNU_VTINHERIT:
1272 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
1273 break;
1274
1275 case elfcpp::R_X86_64_64:
2e30d253
ILT
1276 case elfcpp::R_X86_64_32:
1277 case elfcpp::R_X86_64_32S:
2e30d253 1278 case elfcpp::R_X86_64_16:
2e30d253 1279 case elfcpp::R_X86_64_8:
96f2030e 1280 {
d61c6bd4
ILT
1281 // Make a PLT entry if necessary.
1282 if (gsym->needs_plt_entry())
1283 {
1284 target->make_plt_entry(symtab, layout, gsym);
1285 // Since this is not a PC-relative relocation, we may be
1286 // taking the address of a function. In that case we need to
1287 // set the entry in the dynamic symbol table to the address of
1288 // the PLT entry.
8851ecca 1289 if (gsym->is_from_dynobj() && !parameters->options().shared())
d61c6bd4
ILT
1290 gsym->set_needs_dynsym_value();
1291 }
1292 // Make a dynamic relocation if necessary.
0700cf32 1293 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
d61c6bd4
ILT
1294 {
1295 if (target->may_need_copy_reloc(gsym))
1296 {
7bf1f802
ILT
1297 target->copy_reloc(&options, symtab, layout, object,
1298 data_shndx, output_section, gsym, reloc);
d61c6bd4
ILT
1299 }
1300 else if (r_type == elfcpp::R_X86_64_64
1301 && gsym->can_use_relative_reloc(false))
1302 {
1303 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
e8c846c3
ILT
1304 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1305 output_section, object,
1306 data_shndx, reloc.get_r_offset(),
1307 reloc.get_r_addend());
d61c6bd4
ILT
1308 }
1309 else
1310 {
a036edd8 1311 this->check_non_pic(object, r_type);
96f2030e 1312 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
1313 rela_dyn->add_global(gsym, r_type, output_section, object,
1314 data_shndx, reloc.get_r_offset(),
96f2030e 1315 reloc.get_r_addend());
d61c6bd4
ILT
1316 }
1317 }
1318 }
1319 break;
1320
1321 case elfcpp::R_X86_64_PC64:
1322 case elfcpp::R_X86_64_PC32:
1323 case elfcpp::R_X86_64_PC16:
1324 case elfcpp::R_X86_64_PC8:
1325 {
1326 // Make a PLT entry if necessary.
1327 if (gsym->needs_plt_entry())
1328 target->make_plt_entry(symtab, layout, gsym);
1329 // Make a dynamic relocation if necessary.
0700cf32
ILT
1330 int flags = Symbol::NON_PIC_REF;
1331 if (gsym->type() == elfcpp::STT_FUNC)
1332 flags |= Symbol::FUNCTION_CALL;
1333 if (gsym->needs_dynamic_reloc(flags))
86849f1f 1334 {
d61c6bd4
ILT
1335 if (target->may_need_copy_reloc(gsym))
1336 {
7bf1f802
ILT
1337 target->copy_reloc(&options, symtab, layout, object,
1338 data_shndx, output_section, gsym, reloc);
d61c6bd4 1339 }
86849f1f 1340 else
d61c6bd4 1341 {
a036edd8 1342 this->check_non_pic(object, r_type);
d61c6bd4 1343 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
1344 rela_dyn->add_global(gsym, r_type, output_section, object,
1345 data_shndx, reloc.get_r_offset(),
d61c6bd4
ILT
1346 reloc.get_r_addend());
1347 }
86849f1f 1348 }
d61c6bd4 1349 }
2e30d253
ILT
1350 break;
1351
ff006520 1352 case elfcpp::R_X86_64_GOT64:
2e30d253 1353 case elfcpp::R_X86_64_GOT32:
ff006520
ILT
1354 case elfcpp::R_X86_64_GOTPCREL64:
1355 case elfcpp::R_X86_64_GOTPCREL:
1356 case elfcpp::R_X86_64_GOTPLT64:
2e30d253
ILT
1357 {
1358 // The symbol requires a GOT entry.
1359 Output_data_got<64, false>* got = target->got_section(symtab, layout);
7bf1f802 1360 if (gsym->final_value_is_known())
0a65a3a7 1361 got->add_global(gsym, GOT_TYPE_STANDARD);
7bf1f802
ILT
1362 else
1363 {
2e30d253
ILT
1364 // If this symbol is not fully resolved, we need to add a
1365 // dynamic relocation for it.
7bf1f802 1366 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8fc19601
ILT
1367 if (gsym->is_from_dynobj()
1368 || gsym->is_undefined()
1369 || gsym->is_preemptible())
0a65a3a7 1370 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
7bf1f802
ILT
1371 elfcpp::R_X86_64_GLOB_DAT);
1372 else
2e30d253 1373 {
0a65a3a7
CC
1374 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1375 rela_dyn->add_global_relative(
1376 gsym, elfcpp::R_X86_64_RELATIVE, got,
1377 gsym->got_offset(GOT_TYPE_STANDARD), 0);
2e30d253
ILT
1378 }
1379 }
ee9e9e86
ILT
1380 // For GOTPLT64, we also need a PLT entry (but only if the
1381 // symbol is not fully resolved).
1382 if (r_type == elfcpp::R_X86_64_GOTPLT64
1383 && !gsym->final_value_is_known())
1384 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1385 }
1386 break;
1387
1388 case elfcpp::R_X86_64_PLT32:
1389 // If the symbol is fully resolved, this is just a PC32 reloc.
1390 // Otherwise we need a PLT entry.
1391 if (gsym->final_value_is_known())
1392 break;
96f2030e
ILT
1393 // If building a shared library, we can also skip the PLT entry
1394 // if the symbol is defined in the output file and is protected
1395 // or hidden.
1396 if (gsym->is_defined()
1397 && !gsym->is_from_dynobj()
1398 && !gsym->is_preemptible())
1399 break;
2e30d253
ILT
1400 target->make_plt_entry(symtab, layout, gsym);
1401 break;
1402
fdc2f80f 1403 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1404 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1405 case elfcpp::R_X86_64_GOTPC64:
1406 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1407 // We need a GOT section.
1408 target->got_section(symtab, layout);
ee9e9e86
ILT
1409 // For PLTOFF64, we also need a PLT entry (but only if the
1410 // symbol is not fully resolved).
1411 if (r_type == elfcpp::R_X86_64_PLTOFF64
1412 && !gsym->final_value_is_known())
1413 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1414 break;
1415
2e30d253
ILT
1416 case elfcpp::R_X86_64_COPY:
1417 case elfcpp::R_X86_64_GLOB_DAT:
1418 case elfcpp::R_X86_64_JUMP_SLOT:
1419 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1420 // These are outstanding tls relocs, which are unexpected when linking
e822f2b1 1421 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1422 case elfcpp::R_X86_64_DTPMOD64:
e822f2b1 1423 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1424 gold_error(_("%s: unexpected reloc %u in object file"),
1425 object->name().c_str(), r_type);
2e30d253 1426 break;
2e30d253 1427
d61c17ea 1428 // These are initial tls relocs, which are expected for global()
56622147
ILT
1429 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1430 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1431 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1432 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1433 case elfcpp::R_X86_64_DTPOFF32:
1434 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1435 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1436 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253
ILT
1437 {
1438 const bool is_final = gsym->final_value_is_known();
e041f13d
ILT
1439 const tls::Tls_optimization optimized_type
1440 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
1441 switch (r_type)
1442 {
56622147 1443 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
1444 if (optimized_type == tls::TLSOPT_NONE)
1445 {
1446 // Create a pair of GOT entries for the module index and
1447 // dtv-relative offset.
1448 Output_data_got<64, false>* got
1449 = target->got_section(symtab, layout);
0a65a3a7
CC
1450 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1451 target->rela_dyn_section(layout),
1452 elfcpp::R_X86_64_DTPMOD64,
1453 elfcpp::R_X86_64_DTPOFF64);
7bf1f802
ILT
1454 }
1455 else if (optimized_type == tls::TLSOPT_TO_IE)
1456 {
1457 // Create a GOT entry for the tp-relative offset.
1458 Output_data_got<64, false>* got
1459 = target->got_section(symtab, layout);
0a65a3a7 1460 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1461 target->rela_dyn_section(layout),
1462 elfcpp::R_X86_64_TPOFF64);
1463 }
1464 else if (optimized_type != tls::TLSOPT_TO_LE)
1465 unsupported_reloc_global(object, r_type, gsym);
1466 break;
1467
56622147 1468 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
c2b45e22
CC
1469 if (optimized_type == tls::TLSOPT_NONE)
1470 {
1471 // Create reserved PLT and GOT entries for the resolver.
1472 target->reserve_tlsdesc_entries(symtab, layout);
1473
1474 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1475 Output_data_got<64, false>* got
1476 = target->got_section(symtab, layout);
1477 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1478 target->rela_dyn_section(layout),
1479 elfcpp::R_X86_64_TLSDESC, 0);
1480 }
1481 else if (optimized_type == tls::TLSOPT_TO_IE)
1482 {
1483 // Create a GOT entry for the tp-relative offset.
1484 Output_data_got<64, false>* got
1485 = target->got_section(symtab, layout);
1486 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1487 target->rela_dyn_section(layout),
1488 elfcpp::R_X86_64_TPOFF64);
1489 }
1490 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 1491 unsupported_reloc_global(object, r_type, gsym);
2e30d253
ILT
1492 break;
1493
c2b45e22
CC
1494 case elfcpp::R_X86_64_TLSDESC_CALL:
1495 break;
1496
e041f13d 1497 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
1498 if (optimized_type == tls::TLSOPT_NONE)
1499 {
1500 // Create a GOT entry for the module index.
31d60480 1501 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
1502 }
1503 else if (optimized_type != tls::TLSOPT_TO_LE)
1504 unsupported_reloc_global(object, r_type, gsym);
1505 break;
1506
0ffd9845
ILT
1507 case elfcpp::R_X86_64_DTPOFF32:
1508 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
1509 break;
1510
56622147 1511 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 1512 layout->set_has_static_tls();
7bf1f802
ILT
1513 if (optimized_type == tls::TLSOPT_NONE)
1514 {
1515 // Create a GOT entry for the tp-relative offset.
1516 Output_data_got<64, false>* got
1517 = target->got_section(symtab, layout);
0a65a3a7 1518 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1519 target->rela_dyn_section(layout),
1520 elfcpp::R_X86_64_TPOFF64);
1521 }
1522 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
1523 unsupported_reloc_global(object, r_type, gsym);
1524 break;
0ffd9845 1525
56622147 1526 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 1527 layout->set_has_static_tls();
8851ecca 1528 if (parameters->options().shared())
7bf1f802 1529 unsupported_reloc_local(object, r_type);
2e30d253 1530 break;
e041f13d
ILT
1531
1532 default:
1533 gold_unreachable();
2e30d253
ILT
1534 }
1535 }
1536 break;
fdc2f80f
ILT
1537
1538 case elfcpp::R_X86_64_SIZE32:
1539 case elfcpp::R_X86_64_SIZE64:
2e30d253 1540 default:
75f2446e 1541 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12
ILT
1542 object->name().c_str(), r_type,
1543 gsym->demangled_name().c_str());
2e30d253
ILT
1544 break;
1545 }
1546}
1547
1548// Scan relocations for a section.
1549
1550void
1551Target_x86_64::scan_relocs(const General_options& options,
d61c17ea
ILT
1552 Symbol_table* symtab,
1553 Layout* layout,
1554 Sized_relobj<64, false>* object,
1555 unsigned int data_shndx,
1556 unsigned int sh_type,
1557 const unsigned char* prelocs,
1558 size_t reloc_count,
730cdc88
ILT
1559 Output_section* output_section,
1560 bool needs_special_offset_handling,
d61c17ea 1561 size_t local_symbol_count,
730cdc88 1562 const unsigned char* plocal_symbols)
2e30d253
ILT
1563{
1564 if (sh_type == elfcpp::SHT_REL)
1565 {
75f2446e
ILT
1566 gold_error(_("%s: unsupported REL reloc section"),
1567 object->name().c_str());
1568 return;
2e30d253
ILT
1569 }
1570
1571 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1572 Target_x86_64::Scan>(
1573 options,
1574 symtab,
1575 layout,
1576 this,
1577 object,
1578 data_shndx,
1579 prelocs,
1580 reloc_count,
730cdc88
ILT
1581 output_section,
1582 needs_special_offset_handling,
2e30d253 1583 local_symbol_count,
730cdc88 1584 plocal_symbols);
2e30d253
ILT
1585}
1586
1587// Finalize the sections.
1588
1589void
1590Target_x86_64::do_finalize_sections(Layout* layout)
1591{
1592 // Fill in some more dynamic tags.
1593 Output_data_dynamic* const odyn = layout->dynamic_data();
1594 if (odyn != NULL)
1595 {
1596 if (this->got_plt_ != NULL)
1597 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1598
1599 if (this->plt_ != NULL)
1600 {
1601 const Output_data* od = this->plt_->rel_plt();
1602 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1603 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1604 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
c2b45e22
CC
1605 if (this->plt_->has_tlsdesc_entry())
1606 {
1607 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1608 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1609 this->got_->finalize_data_size();
1610 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1611 this->plt_, plt_offset);
1612 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1613 this->got_, got_offset);
1614 }
2e30d253
ILT
1615 }
1616
0ffd9845 1617 if (this->rela_dyn_ != NULL)
2e30d253 1618 {
0ffd9845 1619 const Output_data* od = this->rela_dyn_;
2e30d253 1620 odyn->add_section_address(elfcpp::DT_RELA, od);
e84992bb 1621 odyn->add_section_size(elfcpp::DT_RELASZ, od);
2e30d253 1622 odyn->add_constant(elfcpp::DT_RELAENT,
e84992bb 1623 elfcpp::Elf_sizes<64>::rela_size);
2e30d253
ILT
1624 }
1625
8851ecca 1626 if (!parameters->options().shared())
2e30d253
ILT
1627 {
1628 // The value of the DT_DEBUG tag is filled in by the dynamic
1629 // linker at run time, and used by the debugger.
1630 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1631 }
1632 }
1633
1634 // Emit any relocs we saved in an attempt to avoid generating COPY
1635 // relocs.
1636 if (this->copy_relocs_ == NULL)
1637 return;
1638 if (this->copy_relocs_->any_to_emit())
1639 {
0ffd9845
ILT
1640 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1641 this->copy_relocs_->emit(rela_dyn);
2e30d253
ILT
1642 }
1643 delete this->copy_relocs_;
1644 this->copy_relocs_ = NULL;
1645}
1646
1647// Perform a relocation.
1648
1649inline bool
1650Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1651 Target_x86_64* target,
1652 size_t relnum,
0ffd9845 1653 const elfcpp::Rela<64, false>& rela,
2e30d253
ILT
1654 unsigned int r_type,
1655 const Sized_symbol<64>* gsym,
1656 const Symbol_value<64>* psymval,
1657 unsigned char* view,
1658 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 1659 section_size_type view_size)
2e30d253
ILT
1660{
1661 if (this->skip_call_tls_get_addr_)
1662 {
1663 if (r_type != elfcpp::R_X86_64_PLT32
1664 || gsym == NULL
0ffd9845 1665 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2e30d253 1666 {
75f2446e
ILT
1667 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1668 _("missing expected TLS relocation"));
1669 }
1670 else
1671 {
1672 this->skip_call_tls_get_addr_ = false;
1673 return false;
2e30d253 1674 }
2e30d253
ILT
1675 }
1676
1677 // Pick the value to use for symbols defined in shared objects.
1678 Symbol_value<64> symval;
96f2030e
ILT
1679 if (gsym != NULL
1680 && (gsym->is_from_dynobj()
8851ecca 1681 || (parameters->options().shared()
8fc19601 1682 && (gsym->is_undefined() || gsym->is_preemptible())))
96f2030e 1683 && gsym->has_plt_offset())
2e30d253
ILT
1684 {
1685 symval.set_output_value(target->plt_section()->address()
1686 + gsym->plt_offset());
1687 psymval = &symval;
1688 }
1689
1690 const Sized_relobj<64, false>* object = relinfo->object;
0ffd9845
ILT
1691 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1692
1693 // Get the GOT offset if needed.
96f2030e
ILT
1694 // The GOT pointer points to the end of the GOT section.
1695 // We need to subtract the size of the GOT section to get
1696 // the actual offset to use in the relocation.
0ffd9845
ILT
1697 bool have_got_offset = false;
1698 unsigned int got_offset = 0;
1699 switch (r_type)
1700 {
1701 case elfcpp::R_X86_64_GOT32:
1702 case elfcpp::R_X86_64_GOT64:
1703 case elfcpp::R_X86_64_GOTPLT64:
1704 case elfcpp::R_X86_64_GOTPCREL:
1705 case elfcpp::R_X86_64_GOTPCREL64:
1706 if (gsym != NULL)
1707 {
0a65a3a7
CC
1708 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1709 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
0ffd9845
ILT
1710 }
1711 else
1712 {
1713 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
1714 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1715 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1716 - target->got_size());
0ffd9845
ILT
1717 }
1718 have_got_offset = true;
1719 break;
1720
1721 default:
1722 break;
1723 }
2e30d253
ILT
1724
1725 switch (r_type)
1726 {
1727 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
1728 case elfcpp::R_386_GNU_VTINHERIT:
1729 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
1730 break;
1731
1732 case elfcpp::R_X86_64_64:
1733 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1734 break;
1735
1736 case elfcpp::R_X86_64_PC64:
1737 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1738 address);
1739 break;
1740
1741 case elfcpp::R_X86_64_32:
7bb3655e
ILT
1742 // FIXME: we need to verify that value + addend fits into 32 bits:
1743 // uint64_t x = value + addend;
1744 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1745 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2e30d253
ILT
1746 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1747 break;
1748
1749 case elfcpp::R_X86_64_32S:
7bb3655e
ILT
1750 // FIXME: we need to verify that value + addend fits into 32 bits:
1751 // int64_t x = value + addend; // note this quantity is signed!
1752 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2e30d253
ILT
1753 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1754 break;
1755
1756 case elfcpp::R_X86_64_PC32:
1757 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1758 address);
1759 break;
1760
1761 case elfcpp::R_X86_64_16:
1762 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1763 break;
1764
1765 case elfcpp::R_X86_64_PC16:
1766 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1767 address);
1768 break;
1769
1770 case elfcpp::R_X86_64_8:
1771 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1772 break;
1773
1774 case elfcpp::R_X86_64_PC8:
1775 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1776 address);
1777 break;
1778
1779 case elfcpp::R_X86_64_PLT32:
f389a824
ILT
1780 gold_assert(gsym == NULL
1781 || gsym->has_plt_offset()
99f8faca
ILT
1782 || gsym->final_value_is_known()
1783 || (gsym->is_defined()
1784 && !gsym->is_from_dynobj()
1785 && !gsym->is_preemptible()));
ee9e9e86
ILT
1786 // Note: while this code looks the same as for R_X86_64_PC32, it
1787 // behaves differently because psymval was set to point to
1788 // the PLT entry, rather than the symbol, in Scan::global().
2e30d253
ILT
1789 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1790 address);
1791 break;
1792
ee9e9e86
ILT
1793 case elfcpp::R_X86_64_PLTOFF64:
1794 {
1795 gold_assert(gsym);
1796 gold_assert(gsym->has_plt_offset()
1797 || gsym->final_value_is_known());
1798 elfcpp::Elf_types<64>::Elf_Addr got_address;
1799 got_address = target->got_section(NULL, NULL)->address();
c1866bd5
ILT
1800 Relocate_functions<64, false>::rela64(view, object, psymval,
1801 addend - got_address);
ee9e9e86
ILT
1802 }
1803
2e30d253 1804 case elfcpp::R_X86_64_GOT32:
0ffd9845
ILT
1805 gold_assert(have_got_offset);
1806 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2e30d253
ILT
1807 break;
1808
e822f2b1
ILT
1809 case elfcpp::R_X86_64_GOTPC32:
1810 {
1811 gold_assert(gsym);
1812 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1813 value = target->got_plt_section()->address();
e822f2b1
ILT
1814 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1815 }
1816 break;
1817
1818 case elfcpp::R_X86_64_GOT64:
1819 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1820 // Since we always add a PLT entry, this is equivalent.
fdc2f80f 1821 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
1822 gold_assert(have_got_offset);
1823 Relocate_functions<64, false>::rela64(view, got_offset, addend);
e822f2b1
ILT
1824 break;
1825
1826 case elfcpp::R_X86_64_GOTPC64:
1827 {
1828 gold_assert(gsym);
1829 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1830 value = target->got_plt_section()->address();
e822f2b1
ILT
1831 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1832 }
1833 break;
1834
2e30d253
ILT
1835 case elfcpp::R_X86_64_GOTOFF64:
1836 {
1837 elfcpp::Elf_types<64>::Elf_Addr value;
1838 value = (psymval->value(object, 0)
96f2030e 1839 - target->got_plt_section()->address());
2e30d253
ILT
1840 Relocate_functions<64, false>::rela64(view, value, addend);
1841 }
1842 break;
1843
1844 case elfcpp::R_X86_64_GOTPCREL:
1845 {
0ffd9845
ILT
1846 gold_assert(have_got_offset);
1847 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1848 value = target->got_plt_section()->address() + got_offset;
0ffd9845 1849 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2e30d253
ILT
1850 }
1851 break;
1852
e822f2b1
ILT
1853 case elfcpp::R_X86_64_GOTPCREL64:
1854 {
0ffd9845
ILT
1855 gold_assert(have_got_offset);
1856 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1857 value = target->got_plt_section()->address() + got_offset;
0ffd9845 1858 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
e822f2b1
ILT
1859 }
1860 break;
1861
2e30d253
ILT
1862 case elfcpp::R_X86_64_COPY:
1863 case elfcpp::R_X86_64_GLOB_DAT:
1864 case elfcpp::R_X86_64_JUMP_SLOT:
1865 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1866 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 1867 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1868 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 1869 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1870 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1871 _("unexpected reloc %u in object file"),
1872 r_type);
2e30d253
ILT
1873 break;
1874
d61c17ea 1875 // These are initial tls relocs, which are expected when linking
56622147
ILT
1876 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1877 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1878 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1879 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1880 case elfcpp::R_X86_64_DTPOFF32:
1881 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1882 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1883 case elfcpp::R_X86_64_TPOFF32: // Local-exec
7bf1f802
ILT
1884 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1885 view, address, view_size);
2e30d253 1886 break;
2e30d253 1887
fdc2f80f
ILT
1888 case elfcpp::R_X86_64_SIZE32:
1889 case elfcpp::R_X86_64_SIZE64:
2e30d253 1890 default:
75f2446e
ILT
1891 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1892 _("unsupported reloc %u"),
1893 r_type);
2e30d253
ILT
1894 break;
1895 }
1896
1897 return true;
1898}
1899
1900// Perform a TLS relocation.
1901
1902inline void
d61c17ea 1903Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
7bf1f802 1904 Target_x86_64* target,
d61c17ea 1905 size_t relnum,
72ec2876 1906 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
1907 unsigned int r_type,
1908 const Sized_symbol<64>* gsym,
1909 const Symbol_value<64>* psymval,
1910 unsigned char* view,
6a41d30b 1911 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 1912 section_size_type view_size)
2e30d253 1913{
2e30d253 1914 Output_segment* tls_segment = relinfo->layout->tls_segment();
7bf1f802
ILT
1915
1916 const Sized_relobj<64, false>* object = relinfo->object;
6a41d30b 1917 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2e30d253
ILT
1918
1919 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1920
1921 const bool is_final = (gsym == NULL
8851ecca 1922 ? !parameters->options().output_is_position_independent()
2e30d253 1923 : gsym->final_value_is_known());
e041f13d
ILT
1924 const tls::Tls_optimization optimized_type
1925 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
1926 switch (r_type)
1927 {
56622147 1928 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
e041f13d 1929 if (optimized_type == tls::TLSOPT_TO_LE)
2e30d253 1930 {
7bf1f802 1931 gold_assert(tls_segment != NULL);
2e30d253 1932 this->tls_gd_to_le(relinfo, relnum, tls_segment,
72ec2876 1933 rela, r_type, value, view,
2e30d253
ILT
1934 view_size);
1935 break;
1936 }
7bf1f802
ILT
1937 else
1938 {
c2b45e22
CC
1939 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1940 ? GOT_TYPE_TLS_OFFSET
1941 : GOT_TYPE_TLS_PAIR);
7bf1f802
ILT
1942 unsigned int got_offset;
1943 if (gsym != NULL)
1944 {
c2b45e22
CC
1945 gold_assert(gsym->has_got_offset(got_type));
1946 got_offset = gsym->got_offset(got_type) - target->got_size();
7bf1f802
ILT
1947 }
1948 else
1949 {
1950 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
c2b45e22
CC
1951 gold_assert(object->local_has_got_offset(r_sym, got_type));
1952 got_offset = (object->local_got_offset(r_sym, got_type)
7bf1f802
ILT
1953 - target->got_size());
1954 }
1955 if (optimized_type == tls::TLSOPT_TO_IE)
1956 {
1957 gold_assert(tls_segment != NULL);
c2b45e22 1958 value = target->got_plt_section()->address() + got_offset;
7bf1f802 1959 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
c2b45e22 1960 value, view, address, view_size);
7bf1f802
ILT
1961 break;
1962 }
1963 else if (optimized_type == tls::TLSOPT_NONE)
1964 {
1965 // Relocate the field with the offset of the pair of GOT
1966 // entries.
6a41d30b
ILT
1967 value = target->got_plt_section()->address() + got_offset;
1968 Relocate_functions<64, false>::pcrela32(view, value, addend,
1969 address);
7bf1f802
ILT
1970 break;
1971 }
1972 }
72ec2876 1973 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 1974 _("unsupported reloc %u"), r_type);
2e30d253
ILT
1975 break;
1976
c2b45e22
CC
1977 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1978 case elfcpp::R_X86_64_TLSDESC_CALL:
1979 if (optimized_type == tls::TLSOPT_TO_LE)
1980 {
1981 gold_assert(tls_segment != NULL);
1982 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1983 rela, r_type, value, view,
1984 view_size);
1985 break;
1986 }
1987 else
1988 {
1989 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1990 ? GOT_TYPE_TLS_OFFSET
1991 : GOT_TYPE_TLS_DESC);
1992 unsigned int got_offset;
1993 if (gsym != NULL)
1994 {
1995 gold_assert(gsym->has_got_offset(got_type));
1996 got_offset = gsym->got_offset(got_type) - target->got_size();
1997 }
1998 else
1999 {
2000 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2001 gold_assert(object->local_has_got_offset(r_sym, got_type));
2002 got_offset = (object->local_got_offset(r_sym, got_type)
2003 - target->got_size());
2004 }
2005 if (optimized_type == tls::TLSOPT_TO_IE)
2006 {
2007 gold_assert(tls_segment != NULL);
2008 value = target->got_plt_section()->address() + got_offset;
2009 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2010 rela, r_type, value, view, address,
2011 view_size);
2012 break;
2013 }
2014 else if (optimized_type == tls::TLSOPT_NONE)
2015 {
2016 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2017 {
2018 // Relocate the field with the offset of the pair of GOT
2019 // entries.
2020 value = target->got_plt_section()->address() + got_offset;
2021 Relocate_functions<64, false>::pcrela32(view, value, addend,
2022 address);
2023 }
2024 break;
2025 }
2026 }
2027 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2028 _("unsupported reloc %u"), r_type);
2029 break;
2030
56622147 2031 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
e041f13d
ILT
2032 if (optimized_type == tls::TLSOPT_TO_LE)
2033 {
7bf1f802 2034 gold_assert(tls_segment != NULL);
72ec2876
ILT
2035 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2036 value, view, view_size);
2037 break;
e041f13d 2038 }
7bf1f802
ILT
2039 else if (optimized_type == tls::TLSOPT_NONE)
2040 {
2041 // Relocate the field with the offset of the GOT entry for
2042 // the module index.
2043 unsigned int got_offset;
31d60480
ILT
2044 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2045 - target->got_size());
6a41d30b
ILT
2046 value = target->got_plt_section()->address() + got_offset;
2047 Relocate_functions<64, false>::pcrela32(view, value, addend,
2048 address);
7bf1f802
ILT
2049 break;
2050 }
72ec2876 2051 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 2052 _("unsupported reloc %u"), r_type);
2e30d253 2053 break;
0ffd9845
ILT
2054
2055 case elfcpp::R_X86_64_DTPOFF32:
7bf1f802 2056 gold_assert(tls_segment != NULL);
e041f13d 2057 if (optimized_type == tls::TLSOPT_TO_LE)
6a41d30b
ILT
2058 value -= tls_segment->memsz();
2059 Relocate_functions<64, false>::rela32(view, value, 0);
0ffd9845
ILT
2060 break;
2061
2062 case elfcpp::R_X86_64_DTPOFF64:
7bf1f802 2063 gold_assert(tls_segment != NULL);
e041f13d 2064 if (optimized_type == tls::TLSOPT_TO_LE)
6a41d30b
ILT
2065 value -= tls_segment->memsz();
2066 Relocate_functions<64, false>::rela64(view, value, 0);
0ffd9845 2067 break;
2e30d253 2068
56622147
ILT
2069 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2070 if (optimized_type == tls::TLSOPT_TO_LE)
2071 {
7bf1f802 2072 gold_assert(tls_segment != NULL);
56622147
ILT
2073 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2074 rela, r_type, value, view,
2075 view_size);
2076 break;
2077 }
7bf1f802
ILT
2078 else if (optimized_type == tls::TLSOPT_NONE)
2079 {
2080 // Relocate the field with the offset of the GOT entry for
2081 // the tp-relative offset of the symbol.
2082 unsigned int got_offset;
2083 if (gsym != NULL)
2084 {
0a65a3a7
CC
2085 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2086 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2087 - target->got_size());
7bf1f802
ILT
2088 }
2089 else
2090 {
2091 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
2092 gold_assert(object->local_has_got_offset(r_sym,
2093 GOT_TYPE_TLS_OFFSET));
2094 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
7bf1f802
ILT
2095 - target->got_size());
2096 }
6a41d30b
ILT
2097 value = target->got_plt_section()->address() + got_offset;
2098 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
7bf1f802
ILT
2099 break;
2100 }
56622147
ILT
2101 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2102 _("unsupported reloc type %u"),
2103 r_type);
2104 break;
0ffd9845 2105
56622147 2106 case elfcpp::R_X86_64_TPOFF32: // Local-exec
6a41d30b
ILT
2107 value -= tls_segment->memsz();
2108 Relocate_functions<64, false>::rela32(view, value, 0);
56622147 2109 break;
2e30d253 2110 }
2e30d253
ILT
2111}
2112
7bf1f802
ILT
2113// Do a relocation in which we convert a TLS General-Dynamic to an
2114// Initial-Exec.
2115
2116inline void
2117Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2118 size_t relnum,
c2b45e22 2119 Output_segment*,
7bf1f802
ILT
2120 const elfcpp::Rela<64, false>& rela,
2121 unsigned int,
2122 elfcpp::Elf_types<64>::Elf_Addr value,
2123 unsigned char* view,
c2b45e22 2124 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2125 section_size_type view_size)
7bf1f802
ILT
2126{
2127 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2128 // .word 0x6666; rex64; call __tls_get_addr
2129 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2130
2131 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2132 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2133
2134 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2135 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2136 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2137 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2138
2139 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2140
c2b45e22
CC
2141 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2142 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
7bf1f802
ILT
2143
2144 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2145 // We can skip it.
2146 this->skip_call_tls_get_addr_ = true;
2147}
2148
e041f13d 2149// Do a relocation in which we convert a TLS General-Dynamic to a
2e30d253
ILT
2150// Local-Exec.
2151
2152inline void
d61c17ea
ILT
2153Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2154 size_t relnum,
2155 Output_segment* tls_segment,
72ec2876 2156 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
2157 unsigned int,
2158 elfcpp::Elf_types<64>::Elf_Addr value,
2159 unsigned char* view,
fe8718a4 2160 section_size_type view_size)
2e30d253 2161{
0ffd9845
ILT
2162 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2163 // .word 0x6666; rex64; call __tls_get_addr
2164 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2e30d253 2165
72ec2876
ILT
2166 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2167 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2e30d253 2168
72ec2876
ILT
2169 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2170 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2171 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2172 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2e30d253 2173
0ffd9845 2174 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2e30d253 2175
6a41d30b 2176 value -= tls_segment->memsz();
0ffd9845 2177 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2e30d253
ILT
2178
2179 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2180 // We can skip it.
2181 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
2182}
2183
c2b45e22
CC
2184// Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2185
2186inline void
2187Target_x86_64::Relocate::tls_desc_gd_to_ie(
2188 const Relocate_info<64, false>* relinfo,
2189 size_t relnum,
2190 Output_segment*,
2191 const elfcpp::Rela<64, false>& rela,
2192 unsigned int r_type,
2193 elfcpp::Elf_types<64>::Elf_Addr value,
2194 unsigned char* view,
2195 elfcpp::Elf_types<64>::Elf_Addr address,
2196 section_size_type view_size)
2197{
2198 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2199 {
2200 // leaq foo@tlsdesc(%rip), %rax
2201 // ==> movq foo@gottpoff(%rip), %rax
2202 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2203 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2204 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2205 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2206 view[-2] = 0x8b;
2207 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2208 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2209 }
2210 else
2211 {
2212 // call *foo@tlscall(%rax)
2213 // ==> nop; nop
2214 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2215 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2216 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2217 view[0] == 0xff && view[1] == 0x10);
2218 view[0] = 0x66;
2219 view[1] = 0x90;
2220 }
2221}
2222
2223// Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2224
2225inline void
2226Target_x86_64::Relocate::tls_desc_gd_to_le(
2227 const Relocate_info<64, false>* relinfo,
2228 size_t relnum,
2229 Output_segment* tls_segment,
2230 const elfcpp::Rela<64, false>& rela,
2231 unsigned int r_type,
2232 elfcpp::Elf_types<64>::Elf_Addr value,
2233 unsigned char* view,
2234 section_size_type view_size)
2235{
2236 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2237 {
2238 // leaq foo@tlsdesc(%rip), %rax
2239 // ==> movq foo@tpoff, %rax
2240 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2241 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2242 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2243 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2244 view[-2] = 0xc7;
2245 view[-1] = 0xc0;
2246 value -= tls_segment->memsz();
2247 Relocate_functions<64, false>::rela32(view, value, 0);
2248 }
2249 else
2250 {
2251 // call *foo@tlscall(%rax)
2252 // ==> nop; nop
2253 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2254 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2255 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2256 view[0] == 0xff && view[1] == 0x10);
2257 view[0] = 0x66;
2258 view[1] = 0x90;
2259 }
2260}
2261
2e30d253 2262inline void
72ec2876
ILT
2263Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2264 size_t relnum,
2265 Output_segment*,
2266 const elfcpp::Rela<64, false>& rela,
2267 unsigned int,
2268 elfcpp::Elf_types<64>::Elf_Addr,
2269 unsigned char* view,
fe8718a4 2270 section_size_type view_size)
2e30d253 2271{
72ec2876
ILT
2272 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2273 // ... leq foo@dtpoff(%rax),%reg
2274 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2e30d253 2275
72ec2876
ILT
2276 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2277 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2e30d253 2278
72ec2876
ILT
2279 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2280 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2281
2282 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2283
2284 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2285
2286 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2287 // We can skip it.
2288 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
2289}
2290
56622147
ILT
2291// Do a relocation in which we convert a TLS Initial-Exec to a
2292// Local-Exec.
2293
2294inline void
2295Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2296 size_t relnum,
2297 Output_segment* tls_segment,
2298 const elfcpp::Rela<64, false>& rela,
2299 unsigned int,
2300 elfcpp::Elf_types<64>::Elf_Addr value,
2301 unsigned char* view,
fe8718a4 2302 section_size_type view_size)
56622147
ILT
2303{
2304 // We need to examine the opcodes to figure out which instruction we
2305 // are looking at.
2306
2307 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2308 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2309
2310 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2311 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2312
2313 unsigned char op1 = view[-3];
2314 unsigned char op2 = view[-2];
2315 unsigned char op3 = view[-1];
2316 unsigned char reg = op3 >> 3;
2317
2318 if (op2 == 0x8b)
2319 {
2320 // movq
2321 if (op1 == 0x4c)
2322 view[-3] = 0x49;
2323 view[-2] = 0xc7;
2324 view[-1] = 0xc0 | reg;
2325 }
2326 else if (reg == 4)
2327 {
2328 // Special handling for %rsp.
2329 if (op1 == 0x4c)
2330 view[-3] = 0x49;
2331 view[-2] = 0x81;
2332 view[-1] = 0xc0 | reg;
2333 }
2334 else
2335 {
2336 // addq
2337 if (op1 == 0x4c)
2338 view[-3] = 0x4d;
2339 view[-2] = 0x8d;
2340 view[-1] = 0x80 | reg | (reg << 3);
2341 }
2342
6a41d30b 2343 value -= tls_segment->memsz();
56622147
ILT
2344 Relocate_functions<64, false>::rela32(view, value, 0);
2345}
2346
2e30d253
ILT
2347// Relocate section data.
2348
2349void
2350Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
d61c17ea
ILT
2351 unsigned int sh_type,
2352 const unsigned char* prelocs,
2353 size_t reloc_count,
730cdc88
ILT
2354 Output_section* output_section,
2355 bool needs_special_offset_handling,
d61c17ea
ILT
2356 unsigned char* view,
2357 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2358 section_size_type view_size)
2e30d253
ILT
2359{
2360 gold_assert(sh_type == elfcpp::SHT_RELA);
2361
2362 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2363 Target_x86_64::Relocate>(
2364 relinfo,
2365 this,
2366 prelocs,
2367 reloc_count,
730cdc88
ILT
2368 output_section,
2369 needs_special_offset_handling,
2e30d253
ILT
2370 view,
2371 address,
2372 view_size);
2373}
2374
6a74a719
ILT
2375// Return the size of a relocation while scanning during a relocatable
2376// link.
2377
2378unsigned int
2379Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2380 unsigned int r_type,
2381 Relobj* object)
2382{
2383 switch (r_type)
2384 {
2385 case elfcpp::R_X86_64_NONE:
2386 case elfcpp::R_386_GNU_VTINHERIT:
2387 case elfcpp::R_386_GNU_VTENTRY:
2388 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2389 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2390 case elfcpp::R_X86_64_TLSDESC_CALL:
2391 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2392 case elfcpp::R_X86_64_DTPOFF32:
2393 case elfcpp::R_X86_64_DTPOFF64:
2394 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2395 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2396 return 0;
2397
2398 case elfcpp::R_X86_64_64:
2399 case elfcpp::R_X86_64_PC64:
2400 case elfcpp::R_X86_64_GOTOFF64:
2401 case elfcpp::R_X86_64_GOTPC64:
2402 case elfcpp::R_X86_64_PLTOFF64:
2403 case elfcpp::R_X86_64_GOT64:
2404 case elfcpp::R_X86_64_GOTPCREL64:
2405 case elfcpp::R_X86_64_GOTPCREL:
2406 case elfcpp::R_X86_64_GOTPLT64:
2407 return 8;
2408
2409 case elfcpp::R_X86_64_32:
2410 case elfcpp::R_X86_64_32S:
2411 case elfcpp::R_X86_64_PC32:
2412 case elfcpp::R_X86_64_PLT32:
2413 case elfcpp::R_X86_64_GOTPC32:
2414 case elfcpp::R_X86_64_GOT32:
2415 return 4;
2416
2417 case elfcpp::R_X86_64_16:
2418 case elfcpp::R_X86_64_PC16:
2419 return 2;
2420
2421 case elfcpp::R_X86_64_8:
2422 case elfcpp::R_X86_64_PC8:
2423 return 1;
2424
2425 case elfcpp::R_X86_64_COPY:
2426 case elfcpp::R_X86_64_GLOB_DAT:
2427 case elfcpp::R_X86_64_JUMP_SLOT:
2428 case elfcpp::R_X86_64_RELATIVE:
2429 // These are outstanding tls relocs, which are unexpected when linking
2430 case elfcpp::R_X86_64_TPOFF64:
2431 case elfcpp::R_X86_64_DTPMOD64:
2432 case elfcpp::R_X86_64_TLSDESC:
2433 object->error(_("unexpected reloc %u in object file"), r_type);
2434 return 0;
2435
2436 case elfcpp::R_X86_64_SIZE32:
2437 case elfcpp::R_X86_64_SIZE64:
2438 default:
2439 object->error(_("unsupported reloc %u against local symbol"), r_type);
2440 return 0;
2441 }
2442}
2443
2444// Scan the relocs during a relocatable link.
2445
2446void
2447Target_x86_64::scan_relocatable_relocs(const General_options& options,
2448 Symbol_table* symtab,
2449 Layout* layout,
2450 Sized_relobj<64, false>* object,
2451 unsigned int data_shndx,
2452 unsigned int sh_type,
2453 const unsigned char* prelocs,
2454 size_t reloc_count,
2455 Output_section* output_section,
2456 bool needs_special_offset_handling,
2457 size_t local_symbol_count,
2458 const unsigned char* plocal_symbols,
2459 Relocatable_relocs* rr)
2460{
2461 gold_assert(sh_type == elfcpp::SHT_RELA);
2462
2463 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2464 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2465
7019cd25 2466 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
6a74a719
ILT
2467 Scan_relocatable_relocs>(
2468 options,
2469 symtab,
2470 layout,
2471 object,
2472 data_shndx,
2473 prelocs,
2474 reloc_count,
2475 output_section,
2476 needs_special_offset_handling,
2477 local_symbol_count,
2478 plocal_symbols,
2479 rr);
2480}
2481
2482// Relocate a section during a relocatable link.
2483
2484void
2485Target_x86_64::relocate_for_relocatable(
2486 const Relocate_info<64, false>* relinfo,
2487 unsigned int sh_type,
2488 const unsigned char* prelocs,
2489 size_t reloc_count,
2490 Output_section* output_section,
2491 off_t offset_in_output_section,
2492 const Relocatable_relocs* rr,
2493 unsigned char* view,
2494 elfcpp::Elf_types<64>::Elf_Addr view_address,
2495 section_size_type view_size,
2496 unsigned char* reloc_view,
2497 section_size_type reloc_view_size)
2498{
2499 gold_assert(sh_type == elfcpp::SHT_RELA);
2500
7019cd25 2501 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
6a74a719
ILT
2502 relinfo,
2503 prelocs,
2504 reloc_count,
2505 output_section,
2506 offset_in_output_section,
2507 rr,
2508 view,
2509 view_address,
2510 view_size,
2511 reloc_view,
2512 reloc_view_size);
2513}
2514
4fb6c25d
ILT
2515// Return the value to use for a dynamic which requires special
2516// treatment. This is how we support equality comparisons of function
2517// pointers across shared library boundaries, as described in the
2518// processor specific ABI supplement.
2519
2520uint64_t
2521Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2522{
2523 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2524 return this->plt_section()->address() + gsym->plt_offset();
2525}
2526
2e30d253
ILT
2527// Return a string used to fill a code section with nops to take up
2528// the specified length.
2529
2530std::string
8851ecca 2531Target_x86_64::do_code_fill(section_size_type length) const
2e30d253
ILT
2532{
2533 if (length >= 16)
2534 {
2535 // Build a jmpq instruction to skip over the bytes.
2536 unsigned char jmp[5];
2537 jmp[0] = 0xe9;
04bf7072 2538 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2e30d253
ILT
2539 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2540 + std::string(length - 5, '\0'));
2541 }
2542
2543 // Nop sequences of various lengths.
2544 const char nop1[1] = { 0x90 }; // nop
2545 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2546 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2547 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2548 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2549 0x00 }; // leal 0(%esi,1),%esi
2550 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2551 0x00, 0x00 };
2552 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2553 0x00, 0x00, 0x00 };
2554 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2555 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2556 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2557 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2558 0x00 };
2559 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2560 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2561 0x00, 0x00 };
2562 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2563 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2564 0x00, 0x00, 0x00 };
2565 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2566 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2567 0x00, 0x00, 0x00, 0x00 };
2568 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2569 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2570 0x27, 0x00, 0x00, 0x00,
2571 0x00 };
2572 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2573 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2574 0xbc, 0x27, 0x00, 0x00,
2575 0x00, 0x00 };
2576 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2577 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2578 0x90, 0x90, 0x90, 0x90,
2579 0x90, 0x90, 0x90 };
2580
2581 const char* nops[16] = {
2582 NULL,
2583 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2584 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2585 };
2586
2587 return std::string(nops[length], length);
2588}
2589
2590// The selector for x86_64 object files.
2591
2592class Target_selector_x86_64 : public Target_selector
2593{
2594public:
2595 Target_selector_x86_64()
e96caa79 2596 : Target_selector(elfcpp::EM_X86_64, 64, false, "elf64-x86-64")
2e30d253
ILT
2597 { }
2598
2599 Target*
e96caa79
ILT
2600 do_instantiate_target()
2601 { return new Target_x86_64(); }
2e30d253
ILT
2602};
2603
2e30d253
ILT
2604Target_selector_x86_64 target_selector_x86_64;
2605
2606} // End anonymous namespace.
This page took 0.170337 seconds and 4 git commands to generate.