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