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