daily update
[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);
d491d34e 1001 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
dceae3c1 1002 if (lsym.get_st_type() != elfcpp::STT_SECTION)
d491d34e
ILT
1003 rela_dyn->add_local(object, r_sym, r_type, output_section,
1004 data_shndx, reloc.get_r_offset(),
1005 reloc.get_r_addend());
dceae3c1
ILT
1006 else
1007 {
1008 gold_assert(lsym.get_st_value() == 0);
d491d34e
ILT
1009 unsigned int shndx = lsym.get_st_shndx();
1010 bool is_ordinary;
1011 shndx = object->adjust_sym_shndx(r_sym, shndx,
1012 &is_ordinary);
1013 if (!is_ordinary)
1014 object->error(_("section symbol %u has bad shndx %u"),
1015 r_sym, shndx);
1016 else
1017 rela_dyn->add_local_section(object, shndx,
1018 r_type, output_section,
1019 data_shndx, reloc.get_r_offset(),
1020 reloc.get_r_addend());
dceae3c1 1021 }
96f2030e 1022 }
2e30d253
ILT
1023 break;
1024
1025 case elfcpp::R_X86_64_PC64:
1026 case elfcpp::R_X86_64_PC32:
1027 case elfcpp::R_X86_64_PC16:
1028 case elfcpp::R_X86_64_PC8:
1029 break;
1030
f389a824
ILT
1031 case elfcpp::R_X86_64_PLT32:
1032 // Since we know this is a local symbol, we can handle this as a
1033 // PC32 reloc.
1034 break;
1035
fdc2f80f 1036 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1037 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1038 case elfcpp::R_X86_64_GOTPC64:
1039 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1040 // We need a GOT section.
1041 target->got_section(symtab, layout);
ee9e9e86
ILT
1042 // For PLTOFF64, we'd normally want a PLT section, but since we
1043 // know this is a local symbol, no PLT is needed.
2e30d253
ILT
1044 break;
1045
0ffd9845
ILT
1046 case elfcpp::R_X86_64_GOT64:
1047 case elfcpp::R_X86_64_GOT32:
1048 case elfcpp::R_X86_64_GOTPCREL64:
1049 case elfcpp::R_X86_64_GOTPCREL:
ee9e9e86 1050 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
1051 {
1052 // The symbol requires a GOT entry.
1053 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1054 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7 1055 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
0ffd9845
ILT
1056 {
1057 // If we are generating a shared object, we need to add a
7bf1f802 1058 // dynamic relocation for this symbol's GOT entry.
8851ecca 1059 if (parameters->options().output_is_position_independent())
0ffd9845
ILT
1060 {
1061 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7bf1f802
ILT
1062 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1063 if (r_type != elfcpp::R_X86_64_GOT32)
0a65a3a7
CC
1064 rela_dyn->add_local_relative(
1065 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1066 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
7bf1f802 1067 else
dceae3c1 1068 {
a036edd8
ILT
1069 this->check_non_pic(object, r_type);
1070
dceae3c1 1071 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
0a65a3a7
CC
1072 rela_dyn->add_local(
1073 object, r_sym, r_type, got,
1074 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
dceae3c1 1075 }
0ffd9845
ILT
1076 }
1077 }
ee9e9e86
ILT
1078 // For GOTPLT64, we'd normally want a PLT section, but since
1079 // we know this is a local symbol, no PLT is needed.
0ffd9845
ILT
1080 }
1081 break;
1082
2e30d253
ILT
1083 case elfcpp::R_X86_64_COPY:
1084 case elfcpp::R_X86_64_GLOB_DAT:
1085 case elfcpp::R_X86_64_JUMP_SLOT:
1086 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1087 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 1088 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1089 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 1090 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1091 gold_error(_("%s: unexpected reloc %u in object file"),
1092 object->name().c_str(), r_type);
2e30d253
ILT
1093 break;
1094
d61c17ea 1095 // These are initial tls relocs, which are expected when linking
56622147
ILT
1096 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1097 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1098 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1099 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1100 case elfcpp::R_X86_64_DTPOFF32:
1101 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1102 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1103 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253 1104 {
8851ecca 1105 bool output_is_shared = parameters->options().shared();
e041f13d
ILT
1106 const tls::Tls_optimization optimized_type
1107 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
2e30d253
ILT
1108 switch (r_type)
1109 {
56622147 1110 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
1111 if (optimized_type == tls::TLSOPT_NONE)
1112 {
1113 // Create a pair of GOT entries for the module index and
1114 // dtv-relative offset.
1115 Output_data_got<64, false>* got
1116 = target->got_section(symtab, layout);
1117 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d491d34e
ILT
1118 unsigned int shndx = lsym.get_st_shndx();
1119 bool is_ordinary;
1120 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1121 if (!is_ordinary)
1122 object->error(_("local symbol %u has bad shndx %u"),
1123 r_sym, shndx);
1124 else
1125 got->add_local_pair_with_rela(object, r_sym,
1126 shndx,
1127 GOT_TYPE_TLS_PAIR,
1128 target->rela_dyn_section(layout),
1129 elfcpp::R_X86_64_DTPMOD64, 0);
7bf1f802
ILT
1130 }
1131 else if (optimized_type != tls::TLSOPT_TO_LE)
1132 unsupported_reloc_local(object, r_type);
1133 break;
1134
56622147 1135 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 1136 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
1137 if (optimized_type == tls::TLSOPT_NONE)
1138 {
1139 // Create reserved PLT and GOT entries for the resolver.
1140 target->reserve_tlsdesc_entries(symtab, layout);
1141
1142 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1143 Output_data_got<64, false>* got
1144 = target->got_section(symtab, layout);
1145 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d491d34e
ILT
1146 unsigned int shndx = lsym.get_st_shndx();
1147 bool is_ordinary;
1148 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1149 if (!is_ordinary)
1150 object->error(_("local symbol %u has bad shndx %u"),
1151 r_sym, shndx);
1152 else
1153 got->add_local_pair_with_rela(object, r_sym,
1154 shndx,
1155 GOT_TYPE_TLS_DESC,
1156 target->rela_dyn_section(layout),
1157 elfcpp::R_X86_64_TLSDESC, 0);
c2b45e22
CC
1158 }
1159 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 1160 unsupported_reloc_local(object, r_type);
2e30d253
ILT
1161 break;
1162
c2b45e22
CC
1163 case elfcpp::R_X86_64_TLSDESC_CALL:
1164 break;
1165
e041f13d 1166 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
1167 if (optimized_type == tls::TLSOPT_NONE)
1168 {
1169 // Create a GOT entry for the module index.
31d60480 1170 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
1171 }
1172 else if (optimized_type != tls::TLSOPT_TO_LE)
1173 unsupported_reloc_local(object, r_type);
1174 break;
1175
0ffd9845
ILT
1176 case elfcpp::R_X86_64_DTPOFF32:
1177 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
1178 break;
1179
56622147 1180 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 1181 layout->set_has_static_tls();
7bf1f802
ILT
1182 if (optimized_type == tls::TLSOPT_NONE)
1183 {
1184 // Create a GOT entry for the tp-relative offset.
1185 Output_data_got<64, false>* got
1186 = target->got_section(symtab, layout);
1187 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7 1188 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1189 target->rela_dyn_section(layout),
1190 elfcpp::R_X86_64_TPOFF64);
1191 }
1192 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
1193 unsupported_reloc_local(object, r_type);
1194 break;
0ffd9845 1195
56622147 1196 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 1197 layout->set_has_static_tls();
7bf1f802
ILT
1198 if (output_is_shared)
1199 unsupported_reloc_local(object, r_type);
2e30d253 1200 break;
e041f13d
ILT
1201
1202 default:
1203 gold_unreachable();
2e30d253
ILT
1204 }
1205 }
1206 break;
2e30d253 1207
fdc2f80f
ILT
1208 case elfcpp::R_X86_64_SIZE32:
1209 case elfcpp::R_X86_64_SIZE64:
2e30d253 1210 default:
75f2446e
ILT
1211 gold_error(_("%s: unsupported reloc %u against local symbol"),
1212 object->name().c_str(), r_type);
2e30d253
ILT
1213 break;
1214 }
1215}
1216
1217
e041f13d
ILT
1218// Report an unsupported relocation against a global symbol.
1219
1220void
1221Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1222 unsigned int r_type,
1223 Symbol* gsym)
1224{
75f2446e 1225 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12 1226 object->name().c_str(), r_type, gsym->demangled_name().c_str());
e041f13d
ILT
1227}
1228
2e30d253
ILT
1229// Scan a relocation for a global symbol.
1230
1231inline void
12c0daef 1232Target_x86_64::Scan::global(const General_options&,
d61c17ea
ILT
1233 Symbol_table* symtab,
1234 Layout* layout,
1235 Target_x86_64* target,
1236 Sized_relobj<64, false>* object,
1237 unsigned int data_shndx,
4f4c5f80 1238 Output_section* output_section,
d61c17ea
ILT
1239 const elfcpp::Rela<64, false>& reloc,
1240 unsigned int r_type,
1241 Symbol* gsym)
2e30d253
ILT
1242{
1243 switch (r_type)
1244 {
1245 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
1246 case elfcpp::R_386_GNU_VTINHERIT:
1247 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
1248 break;
1249
1250 case elfcpp::R_X86_64_64:
2e30d253
ILT
1251 case elfcpp::R_X86_64_32:
1252 case elfcpp::R_X86_64_32S:
2e30d253 1253 case elfcpp::R_X86_64_16:
2e30d253 1254 case elfcpp::R_X86_64_8:
96f2030e 1255 {
d61c6bd4
ILT
1256 // Make a PLT entry if necessary.
1257 if (gsym->needs_plt_entry())
1258 {
1259 target->make_plt_entry(symtab, layout, gsym);
1260 // Since this is not a PC-relative relocation, we may be
1261 // taking the address of a function. In that case we need to
1262 // set the entry in the dynamic symbol table to the address of
1263 // the PLT entry.
8851ecca 1264 if (gsym->is_from_dynobj() && !parameters->options().shared())
d61c6bd4
ILT
1265 gsym->set_needs_dynsym_value();
1266 }
1267 // Make a dynamic relocation if necessary.
0700cf32 1268 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
d61c6bd4
ILT
1269 {
1270 if (target->may_need_copy_reloc(gsym))
1271 {
12c0daef 1272 target->copy_reloc(symtab, layout, object,
7bf1f802 1273 data_shndx, output_section, gsym, reloc);
d61c6bd4
ILT
1274 }
1275 else if (r_type == elfcpp::R_X86_64_64
1276 && gsym->can_use_relative_reloc(false))
1277 {
1278 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
e8c846c3
ILT
1279 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1280 output_section, object,
1281 data_shndx, reloc.get_r_offset(),
1282 reloc.get_r_addend());
d61c6bd4
ILT
1283 }
1284 else
1285 {
a036edd8 1286 this->check_non_pic(object, r_type);
96f2030e 1287 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
1288 rela_dyn->add_global(gsym, r_type, output_section, object,
1289 data_shndx, reloc.get_r_offset(),
96f2030e 1290 reloc.get_r_addend());
d61c6bd4
ILT
1291 }
1292 }
1293 }
1294 break;
1295
1296 case elfcpp::R_X86_64_PC64:
1297 case elfcpp::R_X86_64_PC32:
1298 case elfcpp::R_X86_64_PC16:
1299 case elfcpp::R_X86_64_PC8:
1300 {
1301 // Make a PLT entry if necessary.
1302 if (gsym->needs_plt_entry())
1303 target->make_plt_entry(symtab, layout, gsym);
1304 // Make a dynamic relocation if necessary.
0700cf32
ILT
1305 int flags = Symbol::NON_PIC_REF;
1306 if (gsym->type() == elfcpp::STT_FUNC)
1307 flags |= Symbol::FUNCTION_CALL;
1308 if (gsym->needs_dynamic_reloc(flags))
86849f1f 1309 {
d61c6bd4
ILT
1310 if (target->may_need_copy_reloc(gsym))
1311 {
12c0daef 1312 target->copy_reloc(symtab, layout, object,
7bf1f802 1313 data_shndx, output_section, gsym, reloc);
d61c6bd4 1314 }
86849f1f 1315 else
d61c6bd4 1316 {
a036edd8 1317 this->check_non_pic(object, r_type);
d61c6bd4 1318 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
1319 rela_dyn->add_global(gsym, r_type, output_section, object,
1320 data_shndx, reloc.get_r_offset(),
d61c6bd4
ILT
1321 reloc.get_r_addend());
1322 }
86849f1f 1323 }
d61c6bd4 1324 }
2e30d253
ILT
1325 break;
1326
ff006520 1327 case elfcpp::R_X86_64_GOT64:
2e30d253 1328 case elfcpp::R_X86_64_GOT32:
ff006520
ILT
1329 case elfcpp::R_X86_64_GOTPCREL64:
1330 case elfcpp::R_X86_64_GOTPCREL:
1331 case elfcpp::R_X86_64_GOTPLT64:
2e30d253
ILT
1332 {
1333 // The symbol requires a GOT entry.
1334 Output_data_got<64, false>* got = target->got_section(symtab, layout);
7bf1f802 1335 if (gsym->final_value_is_known())
0a65a3a7 1336 got->add_global(gsym, GOT_TYPE_STANDARD);
7bf1f802
ILT
1337 else
1338 {
2e30d253
ILT
1339 // If this symbol is not fully resolved, we need to add a
1340 // dynamic relocation for it.
7bf1f802 1341 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8fc19601
ILT
1342 if (gsym->is_from_dynobj()
1343 || gsym->is_undefined()
1344 || gsym->is_preemptible())
0a65a3a7 1345 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
7bf1f802
ILT
1346 elfcpp::R_X86_64_GLOB_DAT);
1347 else
2e30d253 1348 {
0a65a3a7
CC
1349 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1350 rela_dyn->add_global_relative(
1351 gsym, elfcpp::R_X86_64_RELATIVE, got,
1352 gsym->got_offset(GOT_TYPE_STANDARD), 0);
2e30d253
ILT
1353 }
1354 }
ee9e9e86
ILT
1355 // For GOTPLT64, we also need a PLT entry (but only if the
1356 // symbol is not fully resolved).
1357 if (r_type == elfcpp::R_X86_64_GOTPLT64
1358 && !gsym->final_value_is_known())
1359 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1360 }
1361 break;
1362
1363 case elfcpp::R_X86_64_PLT32:
1364 // If the symbol is fully resolved, this is just a PC32 reloc.
1365 // Otherwise we need a PLT entry.
1366 if (gsym->final_value_is_known())
1367 break;
96f2030e
ILT
1368 // If building a shared library, we can also skip the PLT entry
1369 // if the symbol is defined in the output file and is protected
1370 // or hidden.
1371 if (gsym->is_defined()
1372 && !gsym->is_from_dynobj()
1373 && !gsym->is_preemptible())
1374 break;
2e30d253
ILT
1375 target->make_plt_entry(symtab, layout, gsym);
1376 break;
1377
fdc2f80f 1378 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1379 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1380 case elfcpp::R_X86_64_GOTPC64:
1381 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1382 // We need a GOT section.
1383 target->got_section(symtab, layout);
ee9e9e86
ILT
1384 // For PLTOFF64, we also need a PLT entry (but only if the
1385 // symbol is not fully resolved).
1386 if (r_type == elfcpp::R_X86_64_PLTOFF64
1387 && !gsym->final_value_is_known())
1388 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1389 break;
1390
2e30d253
ILT
1391 case elfcpp::R_X86_64_COPY:
1392 case elfcpp::R_X86_64_GLOB_DAT:
1393 case elfcpp::R_X86_64_JUMP_SLOT:
1394 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1395 // These are outstanding tls relocs, which are unexpected when linking
e822f2b1 1396 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1397 case elfcpp::R_X86_64_DTPMOD64:
e822f2b1 1398 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1399 gold_error(_("%s: unexpected reloc %u in object file"),
1400 object->name().c_str(), r_type);
2e30d253 1401 break;
2e30d253 1402
d61c17ea 1403 // These are initial tls relocs, which are expected for global()
56622147
ILT
1404 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1405 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1406 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1407 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1408 case elfcpp::R_X86_64_DTPOFF32:
1409 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1410 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1411 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253
ILT
1412 {
1413 const bool is_final = gsym->final_value_is_known();
e041f13d
ILT
1414 const tls::Tls_optimization optimized_type
1415 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
1416 switch (r_type)
1417 {
56622147 1418 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
1419 if (optimized_type == tls::TLSOPT_NONE)
1420 {
1421 // Create a pair of GOT entries for the module index and
1422 // dtv-relative offset.
1423 Output_data_got<64, false>* got
1424 = target->got_section(symtab, layout);
0a65a3a7
CC
1425 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1426 target->rela_dyn_section(layout),
1427 elfcpp::R_X86_64_DTPMOD64,
1428 elfcpp::R_X86_64_DTPOFF64);
7bf1f802
ILT
1429 }
1430 else if (optimized_type == tls::TLSOPT_TO_IE)
1431 {
1432 // Create a GOT entry for the tp-relative offset.
1433 Output_data_got<64, false>* got
1434 = target->got_section(symtab, layout);
0a65a3a7 1435 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1436 target->rela_dyn_section(layout),
1437 elfcpp::R_X86_64_TPOFF64);
1438 }
1439 else if (optimized_type != tls::TLSOPT_TO_LE)
1440 unsupported_reloc_global(object, r_type, gsym);
1441 break;
1442
56622147 1443 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 1444 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
1445 if (optimized_type == tls::TLSOPT_NONE)
1446 {
1447 // Create reserved PLT and GOT entries for the resolver.
1448 target->reserve_tlsdesc_entries(symtab, layout);
1449
1450 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1451 Output_data_got<64, false>* got
1452 = target->got_section(symtab, layout);
1453 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1454 target->rela_dyn_section(layout),
1455 elfcpp::R_X86_64_TLSDESC, 0);
1456 }
1457 else if (optimized_type == tls::TLSOPT_TO_IE)
1458 {
1459 // Create a GOT entry for the tp-relative offset.
1460 Output_data_got<64, false>* got
1461 = target->got_section(symtab, layout);
1462 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1463 target->rela_dyn_section(layout),
1464 elfcpp::R_X86_64_TPOFF64);
1465 }
1466 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 1467 unsupported_reloc_global(object, r_type, gsym);
2e30d253
ILT
1468 break;
1469
c2b45e22
CC
1470 case elfcpp::R_X86_64_TLSDESC_CALL:
1471 break;
1472
e041f13d 1473 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
1474 if (optimized_type == tls::TLSOPT_NONE)
1475 {
1476 // Create a GOT entry for the module index.
31d60480 1477 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
1478 }
1479 else if (optimized_type != tls::TLSOPT_TO_LE)
1480 unsupported_reloc_global(object, r_type, gsym);
1481 break;
1482
0ffd9845
ILT
1483 case elfcpp::R_X86_64_DTPOFF32:
1484 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
1485 break;
1486
56622147 1487 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 1488 layout->set_has_static_tls();
7bf1f802
ILT
1489 if (optimized_type == tls::TLSOPT_NONE)
1490 {
1491 // Create a GOT entry for the tp-relative offset.
1492 Output_data_got<64, false>* got
1493 = target->got_section(symtab, layout);
0a65a3a7 1494 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1495 target->rela_dyn_section(layout),
1496 elfcpp::R_X86_64_TPOFF64);
1497 }
1498 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
1499 unsupported_reloc_global(object, r_type, gsym);
1500 break;
0ffd9845 1501
56622147 1502 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 1503 layout->set_has_static_tls();
8851ecca 1504 if (parameters->options().shared())
7bf1f802 1505 unsupported_reloc_local(object, r_type);
2e30d253 1506 break;
e041f13d
ILT
1507
1508 default:
1509 gold_unreachable();
2e30d253
ILT
1510 }
1511 }
1512 break;
fdc2f80f
ILT
1513
1514 case elfcpp::R_X86_64_SIZE32:
1515 case elfcpp::R_X86_64_SIZE64:
2e30d253 1516 default:
75f2446e 1517 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12
ILT
1518 object->name().c_str(), r_type,
1519 gsym->demangled_name().c_str());
2e30d253
ILT
1520 break;
1521 }
1522}
1523
1524// Scan relocations for a section.
1525
1526void
1527Target_x86_64::scan_relocs(const General_options& options,
d61c17ea
ILT
1528 Symbol_table* symtab,
1529 Layout* layout,
1530 Sized_relobj<64, false>* object,
1531 unsigned int data_shndx,
1532 unsigned int sh_type,
1533 const unsigned char* prelocs,
1534 size_t reloc_count,
730cdc88
ILT
1535 Output_section* output_section,
1536 bool needs_special_offset_handling,
d61c17ea 1537 size_t local_symbol_count,
730cdc88 1538 const unsigned char* plocal_symbols)
2e30d253
ILT
1539{
1540 if (sh_type == elfcpp::SHT_REL)
1541 {
75f2446e
ILT
1542 gold_error(_("%s: unsupported REL reloc section"),
1543 object->name().c_str());
1544 return;
2e30d253
ILT
1545 }
1546
1547 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1548 Target_x86_64::Scan>(
1549 options,
1550 symtab,
1551 layout,
1552 this,
1553 object,
1554 data_shndx,
1555 prelocs,
1556 reloc_count,
730cdc88
ILT
1557 output_section,
1558 needs_special_offset_handling,
2e30d253 1559 local_symbol_count,
730cdc88 1560 plocal_symbols);
2e30d253
ILT
1561}
1562
1563// Finalize the sections.
1564
1565void
1566Target_x86_64::do_finalize_sections(Layout* layout)
1567{
1568 // Fill in some more dynamic tags.
1569 Output_data_dynamic* const odyn = layout->dynamic_data();
1570 if (odyn != NULL)
1571 {
1572 if (this->got_plt_ != NULL)
1573 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1574
1575 if (this->plt_ != NULL)
1576 {
1577 const Output_data* od = this->plt_->rel_plt();
1578 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1579 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1580 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
c2b45e22
CC
1581 if (this->plt_->has_tlsdesc_entry())
1582 {
1583 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1584 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1585 this->got_->finalize_data_size();
1586 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1587 this->plt_, plt_offset);
1588 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1589 this->got_, got_offset);
1590 }
2e30d253
ILT
1591 }
1592
0ffd9845 1593 if (this->rela_dyn_ != NULL)
2e30d253 1594 {
0ffd9845 1595 const Output_data* od = this->rela_dyn_;
2e30d253 1596 odyn->add_section_address(elfcpp::DT_RELA, od);
e84992bb 1597 odyn->add_section_size(elfcpp::DT_RELASZ, od);
2e30d253 1598 odyn->add_constant(elfcpp::DT_RELAENT,
e84992bb 1599 elfcpp::Elf_sizes<64>::rela_size);
2e30d253
ILT
1600 }
1601
8851ecca 1602 if (!parameters->options().shared())
2e30d253
ILT
1603 {
1604 // The value of the DT_DEBUG tag is filled in by the dynamic
1605 // linker at run time, and used by the debugger.
1606 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1607 }
1608 }
1609
1610 // Emit any relocs we saved in an attempt to avoid generating COPY
1611 // relocs.
12c0daef
ILT
1612 if (this->copy_relocs_.any_saved_relocs())
1613 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2e30d253
ILT
1614}
1615
1616// Perform a relocation.
1617
1618inline bool
1619Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1620 Target_x86_64* target,
1621 size_t relnum,
0ffd9845 1622 const elfcpp::Rela<64, false>& rela,
2e30d253
ILT
1623 unsigned int r_type,
1624 const Sized_symbol<64>* gsym,
1625 const Symbol_value<64>* psymval,
1626 unsigned char* view,
1627 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 1628 section_size_type view_size)
2e30d253
ILT
1629{
1630 if (this->skip_call_tls_get_addr_)
1631 {
1632 if (r_type != elfcpp::R_X86_64_PLT32
1633 || gsym == NULL
0ffd9845 1634 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2e30d253 1635 {
75f2446e
ILT
1636 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1637 _("missing expected TLS relocation"));
1638 }
1639 else
1640 {
1641 this->skip_call_tls_get_addr_ = false;
1642 return false;
2e30d253 1643 }
2e30d253
ILT
1644 }
1645
1646 // Pick the value to use for symbols defined in shared objects.
1647 Symbol_value<64> symval;
96f2030e
ILT
1648 if (gsym != NULL
1649 && (gsym->is_from_dynobj()
8851ecca 1650 || (parameters->options().shared()
8fc19601 1651 && (gsym->is_undefined() || gsym->is_preemptible())))
96f2030e 1652 && gsym->has_plt_offset())
2e30d253
ILT
1653 {
1654 symval.set_output_value(target->plt_section()->address()
1655 + gsym->plt_offset());
1656 psymval = &symval;
1657 }
1658
1659 const Sized_relobj<64, false>* object = relinfo->object;
0ffd9845
ILT
1660 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1661
1662 // Get the GOT offset if needed.
96f2030e
ILT
1663 // The GOT pointer points to the end of the GOT section.
1664 // We need to subtract the size of the GOT section to get
1665 // the actual offset to use in the relocation.
0ffd9845
ILT
1666 bool have_got_offset = false;
1667 unsigned int got_offset = 0;
1668 switch (r_type)
1669 {
1670 case elfcpp::R_X86_64_GOT32:
1671 case elfcpp::R_X86_64_GOT64:
1672 case elfcpp::R_X86_64_GOTPLT64:
1673 case elfcpp::R_X86_64_GOTPCREL:
1674 case elfcpp::R_X86_64_GOTPCREL64:
1675 if (gsym != NULL)
1676 {
0a65a3a7
CC
1677 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1678 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
0ffd9845
ILT
1679 }
1680 else
1681 {
1682 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
1683 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1684 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1685 - target->got_size());
0ffd9845
ILT
1686 }
1687 have_got_offset = true;
1688 break;
1689
1690 default:
1691 break;
1692 }
2e30d253
ILT
1693
1694 switch (r_type)
1695 {
1696 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
1697 case elfcpp::R_386_GNU_VTINHERIT:
1698 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
1699 break;
1700
1701 case elfcpp::R_X86_64_64:
1702 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1703 break;
1704
1705 case elfcpp::R_X86_64_PC64:
1706 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1707 address);
1708 break;
1709
1710 case elfcpp::R_X86_64_32:
7bb3655e
ILT
1711 // FIXME: we need to verify that value + addend fits into 32 bits:
1712 // uint64_t x = value + addend;
1713 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1714 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2e30d253
ILT
1715 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1716 break;
1717
1718 case elfcpp::R_X86_64_32S:
7bb3655e
ILT
1719 // FIXME: we need to verify that value + addend fits into 32 bits:
1720 // int64_t x = value + addend; // note this quantity is signed!
1721 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2e30d253
ILT
1722 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1723 break;
1724
1725 case elfcpp::R_X86_64_PC32:
1726 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1727 address);
1728 break;
1729
1730 case elfcpp::R_X86_64_16:
1731 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1732 break;
1733
1734 case elfcpp::R_X86_64_PC16:
1735 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1736 address);
1737 break;
1738
1739 case elfcpp::R_X86_64_8:
1740 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1741 break;
1742
1743 case elfcpp::R_X86_64_PC8:
1744 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1745 address);
1746 break;
1747
1748 case elfcpp::R_X86_64_PLT32:
f389a824
ILT
1749 gold_assert(gsym == NULL
1750 || gsym->has_plt_offset()
99f8faca
ILT
1751 || gsym->final_value_is_known()
1752 || (gsym->is_defined()
1753 && !gsym->is_from_dynobj()
1754 && !gsym->is_preemptible()));
ee9e9e86
ILT
1755 // Note: while this code looks the same as for R_X86_64_PC32, it
1756 // behaves differently because psymval was set to point to
1757 // the PLT entry, rather than the symbol, in Scan::global().
2e30d253
ILT
1758 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1759 address);
1760 break;
1761
ee9e9e86
ILT
1762 case elfcpp::R_X86_64_PLTOFF64:
1763 {
1764 gold_assert(gsym);
1765 gold_assert(gsym->has_plt_offset()
1766 || gsym->final_value_is_known());
1767 elfcpp::Elf_types<64>::Elf_Addr got_address;
1768 got_address = target->got_section(NULL, NULL)->address();
c1866bd5
ILT
1769 Relocate_functions<64, false>::rela64(view, object, psymval,
1770 addend - got_address);
ee9e9e86
ILT
1771 }
1772
2e30d253 1773 case elfcpp::R_X86_64_GOT32:
0ffd9845
ILT
1774 gold_assert(have_got_offset);
1775 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2e30d253
ILT
1776 break;
1777
e822f2b1
ILT
1778 case elfcpp::R_X86_64_GOTPC32:
1779 {
1780 gold_assert(gsym);
1781 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1782 value = target->got_plt_section()->address();
e822f2b1
ILT
1783 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1784 }
1785 break;
1786
1787 case elfcpp::R_X86_64_GOT64:
1788 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1789 // Since we always add a PLT entry, this is equivalent.
fdc2f80f 1790 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
1791 gold_assert(have_got_offset);
1792 Relocate_functions<64, false>::rela64(view, got_offset, addend);
e822f2b1
ILT
1793 break;
1794
1795 case elfcpp::R_X86_64_GOTPC64:
1796 {
1797 gold_assert(gsym);
1798 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1799 value = target->got_plt_section()->address();
e822f2b1
ILT
1800 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1801 }
1802 break;
1803
2e30d253
ILT
1804 case elfcpp::R_X86_64_GOTOFF64:
1805 {
1806 elfcpp::Elf_types<64>::Elf_Addr value;
1807 value = (psymval->value(object, 0)
96f2030e 1808 - target->got_plt_section()->address());
2e30d253
ILT
1809 Relocate_functions<64, false>::rela64(view, value, addend);
1810 }
1811 break;
1812
1813 case elfcpp::R_X86_64_GOTPCREL:
1814 {
0ffd9845
ILT
1815 gold_assert(have_got_offset);
1816 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1817 value = target->got_plt_section()->address() + got_offset;
0ffd9845 1818 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2e30d253
ILT
1819 }
1820 break;
1821
e822f2b1
ILT
1822 case elfcpp::R_X86_64_GOTPCREL64:
1823 {
0ffd9845
ILT
1824 gold_assert(have_got_offset);
1825 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1826 value = target->got_plt_section()->address() + got_offset;
0ffd9845 1827 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
e822f2b1
ILT
1828 }
1829 break;
1830
2e30d253
ILT
1831 case elfcpp::R_X86_64_COPY:
1832 case elfcpp::R_X86_64_GLOB_DAT:
1833 case elfcpp::R_X86_64_JUMP_SLOT:
1834 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1835 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 1836 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1837 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 1838 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1839 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1840 _("unexpected reloc %u in object file"),
1841 r_type);
2e30d253
ILT
1842 break;
1843
d61c17ea 1844 // These are initial tls relocs, which are expected when linking
56622147
ILT
1845 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1846 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1847 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1848 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1849 case elfcpp::R_X86_64_DTPOFF32:
1850 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1851 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1852 case elfcpp::R_X86_64_TPOFF32: // Local-exec
7bf1f802
ILT
1853 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1854 view, address, view_size);
2e30d253 1855 break;
2e30d253 1856
fdc2f80f
ILT
1857 case elfcpp::R_X86_64_SIZE32:
1858 case elfcpp::R_X86_64_SIZE64:
2e30d253 1859 default:
75f2446e
ILT
1860 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1861 _("unsupported reloc %u"),
1862 r_type);
2e30d253
ILT
1863 break;
1864 }
1865
1866 return true;
1867}
1868
1869// Perform a TLS relocation.
1870
1871inline void
d61c17ea 1872Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
7bf1f802 1873 Target_x86_64* target,
d61c17ea 1874 size_t relnum,
72ec2876 1875 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
1876 unsigned int r_type,
1877 const Sized_symbol<64>* gsym,
1878 const Symbol_value<64>* psymval,
1879 unsigned char* view,
6a41d30b 1880 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 1881 section_size_type view_size)
2e30d253 1882{
2e30d253 1883 Output_segment* tls_segment = relinfo->layout->tls_segment();
7bf1f802
ILT
1884
1885 const Sized_relobj<64, false>* object = relinfo->object;
6a41d30b 1886 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2e30d253
ILT
1887
1888 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1889
1890 const bool is_final = (gsym == NULL
8851ecca 1891 ? !parameters->options().output_is_position_independent()
2e30d253 1892 : gsym->final_value_is_known());
e041f13d
ILT
1893 const tls::Tls_optimization optimized_type
1894 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
1895 switch (r_type)
1896 {
56622147 1897 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
e041f13d 1898 if (optimized_type == tls::TLSOPT_TO_LE)
2e30d253 1899 {
7bf1f802 1900 gold_assert(tls_segment != NULL);
2e30d253 1901 this->tls_gd_to_le(relinfo, relnum, tls_segment,
72ec2876 1902 rela, r_type, value, view,
2e30d253
ILT
1903 view_size);
1904 break;
1905 }
7bf1f802
ILT
1906 else
1907 {
c2b45e22
CC
1908 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1909 ? GOT_TYPE_TLS_OFFSET
1910 : GOT_TYPE_TLS_PAIR);
7bf1f802
ILT
1911 unsigned int got_offset;
1912 if (gsym != NULL)
1913 {
c2b45e22
CC
1914 gold_assert(gsym->has_got_offset(got_type));
1915 got_offset = gsym->got_offset(got_type) - target->got_size();
7bf1f802
ILT
1916 }
1917 else
1918 {
1919 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
c2b45e22
CC
1920 gold_assert(object->local_has_got_offset(r_sym, got_type));
1921 got_offset = (object->local_got_offset(r_sym, got_type)
7bf1f802
ILT
1922 - target->got_size());
1923 }
1924 if (optimized_type == tls::TLSOPT_TO_IE)
1925 {
1926 gold_assert(tls_segment != NULL);
c2b45e22 1927 value = target->got_plt_section()->address() + got_offset;
7bf1f802 1928 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
c2b45e22 1929 value, view, address, view_size);
7bf1f802
ILT
1930 break;
1931 }
1932 else if (optimized_type == tls::TLSOPT_NONE)
1933 {
1934 // Relocate the field with the offset of the pair of GOT
1935 // entries.
6a41d30b
ILT
1936 value = target->got_plt_section()->address() + got_offset;
1937 Relocate_functions<64, false>::pcrela32(view, value, addend,
1938 address);
7bf1f802
ILT
1939 break;
1940 }
1941 }
72ec2876 1942 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 1943 _("unsupported reloc %u"), r_type);
2e30d253
ILT
1944 break;
1945
c2b45e22
CC
1946 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1947 case elfcpp::R_X86_64_TLSDESC_CALL:
1948 if (optimized_type == tls::TLSOPT_TO_LE)
1949 {
1950 gold_assert(tls_segment != NULL);
1951 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1952 rela, r_type, value, view,
1953 view_size);
1954 break;
1955 }
1956 else
1957 {
1958 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1959 ? GOT_TYPE_TLS_OFFSET
1960 : GOT_TYPE_TLS_DESC);
1961 unsigned int got_offset;
1962 if (gsym != NULL)
1963 {
1964 gold_assert(gsym->has_got_offset(got_type));
1965 got_offset = gsym->got_offset(got_type) - target->got_size();
1966 }
1967 else
1968 {
1969 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1970 gold_assert(object->local_has_got_offset(r_sym, got_type));
1971 got_offset = (object->local_got_offset(r_sym, got_type)
1972 - target->got_size());
1973 }
1974 if (optimized_type == tls::TLSOPT_TO_IE)
1975 {
1976 gold_assert(tls_segment != NULL);
1977 value = target->got_plt_section()->address() + got_offset;
1978 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
1979 rela, r_type, value, view, address,
1980 view_size);
1981 break;
1982 }
1983 else if (optimized_type == tls::TLSOPT_NONE)
1984 {
1985 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
1986 {
1987 // Relocate the field with the offset of the pair of GOT
1988 // entries.
1989 value = target->got_plt_section()->address() + got_offset;
1990 Relocate_functions<64, false>::pcrela32(view, value, addend,
1991 address);
1992 }
1993 break;
1994 }
1995 }
1996 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1997 _("unsupported reloc %u"), r_type);
1998 break;
1999
56622147 2000 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
e041f13d
ILT
2001 if (optimized_type == tls::TLSOPT_TO_LE)
2002 {
7bf1f802 2003 gold_assert(tls_segment != NULL);
72ec2876
ILT
2004 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2005 value, view, view_size);
2006 break;
e041f13d 2007 }
7bf1f802
ILT
2008 else if (optimized_type == tls::TLSOPT_NONE)
2009 {
2010 // Relocate the field with the offset of the GOT entry for
2011 // the module index.
2012 unsigned int got_offset;
31d60480
ILT
2013 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2014 - target->got_size());
6a41d30b
ILT
2015 value = target->got_plt_section()->address() + got_offset;
2016 Relocate_functions<64, false>::pcrela32(view, value, addend,
2017 address);
7bf1f802
ILT
2018 break;
2019 }
72ec2876 2020 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 2021 _("unsupported reloc %u"), r_type);
2e30d253 2022 break;
0ffd9845
ILT
2023
2024 case elfcpp::R_X86_64_DTPOFF32:
7bf1f802 2025 gold_assert(tls_segment != NULL);
e041f13d 2026 if (optimized_type == tls::TLSOPT_TO_LE)
6a41d30b
ILT
2027 value -= tls_segment->memsz();
2028 Relocate_functions<64, false>::rela32(view, value, 0);
0ffd9845
ILT
2029 break;
2030
2031 case elfcpp::R_X86_64_DTPOFF64:
7bf1f802 2032 gold_assert(tls_segment != NULL);
e041f13d 2033 if (optimized_type == tls::TLSOPT_TO_LE)
6a41d30b
ILT
2034 value -= tls_segment->memsz();
2035 Relocate_functions<64, false>::rela64(view, value, 0);
0ffd9845 2036 break;
2e30d253 2037
56622147
ILT
2038 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2039 if (optimized_type == tls::TLSOPT_TO_LE)
2040 {
7bf1f802 2041 gold_assert(tls_segment != NULL);
56622147
ILT
2042 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2043 rela, r_type, value, view,
2044 view_size);
2045 break;
2046 }
7bf1f802
ILT
2047 else if (optimized_type == tls::TLSOPT_NONE)
2048 {
2049 // Relocate the field with the offset of the GOT entry for
2050 // the tp-relative offset of the symbol.
2051 unsigned int got_offset;
2052 if (gsym != NULL)
2053 {
0a65a3a7
CC
2054 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2055 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2056 - target->got_size());
7bf1f802
ILT
2057 }
2058 else
2059 {
2060 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
2061 gold_assert(object->local_has_got_offset(r_sym,
2062 GOT_TYPE_TLS_OFFSET));
2063 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
7bf1f802
ILT
2064 - target->got_size());
2065 }
6a41d30b
ILT
2066 value = target->got_plt_section()->address() + got_offset;
2067 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
7bf1f802
ILT
2068 break;
2069 }
56622147
ILT
2070 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2071 _("unsupported reloc type %u"),
2072 r_type);
2073 break;
0ffd9845 2074
56622147 2075 case elfcpp::R_X86_64_TPOFF32: // Local-exec
6a41d30b
ILT
2076 value -= tls_segment->memsz();
2077 Relocate_functions<64, false>::rela32(view, value, 0);
56622147 2078 break;
2e30d253 2079 }
2e30d253
ILT
2080}
2081
7bf1f802
ILT
2082// Do a relocation in which we convert a TLS General-Dynamic to an
2083// Initial-Exec.
2084
2085inline void
2086Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2087 size_t relnum,
c2b45e22 2088 Output_segment*,
7bf1f802
ILT
2089 const elfcpp::Rela<64, false>& rela,
2090 unsigned int,
2091 elfcpp::Elf_types<64>::Elf_Addr value,
2092 unsigned char* view,
c2b45e22 2093 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2094 section_size_type view_size)
7bf1f802
ILT
2095{
2096 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2097 // .word 0x6666; rex64; call __tls_get_addr
2098 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2099
2100 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2101 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2102
2103 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2104 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2105 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2106 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2107
2108 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2109
c2b45e22
CC
2110 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2111 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
7bf1f802
ILT
2112
2113 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2114 // We can skip it.
2115 this->skip_call_tls_get_addr_ = true;
2116}
2117
e041f13d 2118// Do a relocation in which we convert a TLS General-Dynamic to a
2e30d253
ILT
2119// Local-Exec.
2120
2121inline void
d61c17ea
ILT
2122Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2123 size_t relnum,
2124 Output_segment* tls_segment,
72ec2876 2125 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
2126 unsigned int,
2127 elfcpp::Elf_types<64>::Elf_Addr value,
2128 unsigned char* view,
fe8718a4 2129 section_size_type view_size)
2e30d253 2130{
0ffd9845
ILT
2131 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2132 // .word 0x6666; rex64; call __tls_get_addr
2133 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2e30d253 2134
72ec2876
ILT
2135 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2136 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2e30d253 2137
72ec2876
ILT
2138 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2139 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2140 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2141 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2e30d253 2142
0ffd9845 2143 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2e30d253 2144
6a41d30b 2145 value -= tls_segment->memsz();
0ffd9845 2146 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2e30d253
ILT
2147
2148 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2149 // We can skip it.
2150 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
2151}
2152
c2b45e22
CC
2153// Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2154
2155inline void
2156Target_x86_64::Relocate::tls_desc_gd_to_ie(
2157 const Relocate_info<64, false>* relinfo,
2158 size_t relnum,
2159 Output_segment*,
2160 const elfcpp::Rela<64, false>& rela,
2161 unsigned int r_type,
2162 elfcpp::Elf_types<64>::Elf_Addr value,
2163 unsigned char* view,
2164 elfcpp::Elf_types<64>::Elf_Addr address,
2165 section_size_type view_size)
2166{
2167 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2168 {
2169 // leaq foo@tlsdesc(%rip), %rax
2170 // ==> movq foo@gottpoff(%rip), %rax
2171 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2172 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2173 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2174 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2175 view[-2] = 0x8b;
2176 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2177 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2178 }
2179 else
2180 {
2181 // call *foo@tlscall(%rax)
2182 // ==> nop; nop
2183 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2184 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2185 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2186 view[0] == 0xff && view[1] == 0x10);
2187 view[0] = 0x66;
2188 view[1] = 0x90;
2189 }
2190}
2191
2192// Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2193
2194inline void
2195Target_x86_64::Relocate::tls_desc_gd_to_le(
2196 const Relocate_info<64, false>* relinfo,
2197 size_t relnum,
2198 Output_segment* tls_segment,
2199 const elfcpp::Rela<64, false>& rela,
2200 unsigned int r_type,
2201 elfcpp::Elf_types<64>::Elf_Addr value,
2202 unsigned char* view,
2203 section_size_type view_size)
2204{
2205 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2206 {
2207 // leaq foo@tlsdesc(%rip), %rax
2208 // ==> movq foo@tpoff, %rax
2209 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2210 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2211 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2212 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2213 view[-2] = 0xc7;
2214 view[-1] = 0xc0;
2215 value -= tls_segment->memsz();
2216 Relocate_functions<64, false>::rela32(view, value, 0);
2217 }
2218 else
2219 {
2220 // call *foo@tlscall(%rax)
2221 // ==> nop; nop
2222 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2223 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2224 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2225 view[0] == 0xff && view[1] == 0x10);
2226 view[0] = 0x66;
2227 view[1] = 0x90;
2228 }
2229}
2230
2e30d253 2231inline void
72ec2876
ILT
2232Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2233 size_t relnum,
2234 Output_segment*,
2235 const elfcpp::Rela<64, false>& rela,
2236 unsigned int,
2237 elfcpp::Elf_types<64>::Elf_Addr,
2238 unsigned char* view,
fe8718a4 2239 section_size_type view_size)
2e30d253 2240{
72ec2876
ILT
2241 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2242 // ... leq foo@dtpoff(%rax),%reg
2243 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2e30d253 2244
72ec2876
ILT
2245 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2246 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2e30d253 2247
72ec2876
ILT
2248 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2249 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2250
2251 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2252
2253 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2254
2255 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2256 // We can skip it.
2257 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
2258}
2259
56622147
ILT
2260// Do a relocation in which we convert a TLS Initial-Exec to a
2261// Local-Exec.
2262
2263inline void
2264Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2265 size_t relnum,
2266 Output_segment* tls_segment,
2267 const elfcpp::Rela<64, false>& rela,
2268 unsigned int,
2269 elfcpp::Elf_types<64>::Elf_Addr value,
2270 unsigned char* view,
fe8718a4 2271 section_size_type view_size)
56622147
ILT
2272{
2273 // We need to examine the opcodes to figure out which instruction we
2274 // are looking at.
2275
2276 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2277 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2278
2279 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2280 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2281
2282 unsigned char op1 = view[-3];
2283 unsigned char op2 = view[-2];
2284 unsigned char op3 = view[-1];
2285 unsigned char reg = op3 >> 3;
2286
2287 if (op2 == 0x8b)
2288 {
2289 // movq
2290 if (op1 == 0x4c)
2291 view[-3] = 0x49;
2292 view[-2] = 0xc7;
2293 view[-1] = 0xc0 | reg;
2294 }
2295 else if (reg == 4)
2296 {
2297 // Special handling for %rsp.
2298 if (op1 == 0x4c)
2299 view[-3] = 0x49;
2300 view[-2] = 0x81;
2301 view[-1] = 0xc0 | reg;
2302 }
2303 else
2304 {
2305 // addq
2306 if (op1 == 0x4c)
2307 view[-3] = 0x4d;
2308 view[-2] = 0x8d;
2309 view[-1] = 0x80 | reg | (reg << 3);
2310 }
2311
6a41d30b 2312 value -= tls_segment->memsz();
56622147
ILT
2313 Relocate_functions<64, false>::rela32(view, value, 0);
2314}
2315
2e30d253
ILT
2316// Relocate section data.
2317
2318void
2319Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
d61c17ea
ILT
2320 unsigned int sh_type,
2321 const unsigned char* prelocs,
2322 size_t reloc_count,
730cdc88
ILT
2323 Output_section* output_section,
2324 bool needs_special_offset_handling,
d61c17ea
ILT
2325 unsigned char* view,
2326 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2327 section_size_type view_size)
2e30d253
ILT
2328{
2329 gold_assert(sh_type == elfcpp::SHT_RELA);
2330
2331 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2332 Target_x86_64::Relocate>(
2333 relinfo,
2334 this,
2335 prelocs,
2336 reloc_count,
730cdc88
ILT
2337 output_section,
2338 needs_special_offset_handling,
2e30d253
ILT
2339 view,
2340 address,
2341 view_size);
2342}
2343
6a74a719
ILT
2344// Return the size of a relocation while scanning during a relocatable
2345// link.
2346
2347unsigned int
2348Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2349 unsigned int r_type,
2350 Relobj* object)
2351{
2352 switch (r_type)
2353 {
2354 case elfcpp::R_X86_64_NONE:
2355 case elfcpp::R_386_GNU_VTINHERIT:
2356 case elfcpp::R_386_GNU_VTENTRY:
2357 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2358 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2359 case elfcpp::R_X86_64_TLSDESC_CALL:
2360 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2361 case elfcpp::R_X86_64_DTPOFF32:
2362 case elfcpp::R_X86_64_DTPOFF64:
2363 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2364 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2365 return 0;
2366
2367 case elfcpp::R_X86_64_64:
2368 case elfcpp::R_X86_64_PC64:
2369 case elfcpp::R_X86_64_GOTOFF64:
2370 case elfcpp::R_X86_64_GOTPC64:
2371 case elfcpp::R_X86_64_PLTOFF64:
2372 case elfcpp::R_X86_64_GOT64:
2373 case elfcpp::R_X86_64_GOTPCREL64:
2374 case elfcpp::R_X86_64_GOTPCREL:
2375 case elfcpp::R_X86_64_GOTPLT64:
2376 return 8;
2377
2378 case elfcpp::R_X86_64_32:
2379 case elfcpp::R_X86_64_32S:
2380 case elfcpp::R_X86_64_PC32:
2381 case elfcpp::R_X86_64_PLT32:
2382 case elfcpp::R_X86_64_GOTPC32:
2383 case elfcpp::R_X86_64_GOT32:
2384 return 4;
2385
2386 case elfcpp::R_X86_64_16:
2387 case elfcpp::R_X86_64_PC16:
2388 return 2;
2389
2390 case elfcpp::R_X86_64_8:
2391 case elfcpp::R_X86_64_PC8:
2392 return 1;
2393
2394 case elfcpp::R_X86_64_COPY:
2395 case elfcpp::R_X86_64_GLOB_DAT:
2396 case elfcpp::R_X86_64_JUMP_SLOT:
2397 case elfcpp::R_X86_64_RELATIVE:
2398 // These are outstanding tls relocs, which are unexpected when linking
2399 case elfcpp::R_X86_64_TPOFF64:
2400 case elfcpp::R_X86_64_DTPMOD64:
2401 case elfcpp::R_X86_64_TLSDESC:
2402 object->error(_("unexpected reloc %u in object file"), r_type);
2403 return 0;
2404
2405 case elfcpp::R_X86_64_SIZE32:
2406 case elfcpp::R_X86_64_SIZE64:
2407 default:
2408 object->error(_("unsupported reloc %u against local symbol"), r_type);
2409 return 0;
2410 }
2411}
2412
2413// Scan the relocs during a relocatable link.
2414
2415void
2416Target_x86_64::scan_relocatable_relocs(const General_options& options,
2417 Symbol_table* symtab,
2418 Layout* layout,
2419 Sized_relobj<64, false>* object,
2420 unsigned int data_shndx,
2421 unsigned int sh_type,
2422 const unsigned char* prelocs,
2423 size_t reloc_count,
2424 Output_section* output_section,
2425 bool needs_special_offset_handling,
2426 size_t local_symbol_count,
2427 const unsigned char* plocal_symbols,
2428 Relocatable_relocs* rr)
2429{
2430 gold_assert(sh_type == elfcpp::SHT_RELA);
2431
2432 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2433 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2434
7019cd25 2435 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
6a74a719
ILT
2436 Scan_relocatable_relocs>(
2437 options,
2438 symtab,
2439 layout,
2440 object,
2441 data_shndx,
2442 prelocs,
2443 reloc_count,
2444 output_section,
2445 needs_special_offset_handling,
2446 local_symbol_count,
2447 plocal_symbols,
2448 rr);
2449}
2450
2451// Relocate a section during a relocatable link.
2452
2453void
2454Target_x86_64::relocate_for_relocatable(
2455 const Relocate_info<64, false>* relinfo,
2456 unsigned int sh_type,
2457 const unsigned char* prelocs,
2458 size_t reloc_count,
2459 Output_section* output_section,
2460 off_t offset_in_output_section,
2461 const Relocatable_relocs* rr,
2462 unsigned char* view,
2463 elfcpp::Elf_types<64>::Elf_Addr view_address,
2464 section_size_type view_size,
2465 unsigned char* reloc_view,
2466 section_size_type reloc_view_size)
2467{
2468 gold_assert(sh_type == elfcpp::SHT_RELA);
2469
7019cd25 2470 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
6a74a719
ILT
2471 relinfo,
2472 prelocs,
2473 reloc_count,
2474 output_section,
2475 offset_in_output_section,
2476 rr,
2477 view,
2478 view_address,
2479 view_size,
2480 reloc_view,
2481 reloc_view_size);
2482}
2483
4fb6c25d
ILT
2484// Return the value to use for a dynamic which requires special
2485// treatment. This is how we support equality comparisons of function
2486// pointers across shared library boundaries, as described in the
2487// processor specific ABI supplement.
2488
2489uint64_t
2490Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2491{
2492 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2493 return this->plt_section()->address() + gsym->plt_offset();
2494}
2495
2e30d253
ILT
2496// Return a string used to fill a code section with nops to take up
2497// the specified length.
2498
2499std::string
8851ecca 2500Target_x86_64::do_code_fill(section_size_type length) const
2e30d253
ILT
2501{
2502 if (length >= 16)
2503 {
2504 // Build a jmpq instruction to skip over the bytes.
2505 unsigned char jmp[5];
2506 jmp[0] = 0xe9;
04bf7072 2507 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2e30d253
ILT
2508 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2509 + std::string(length - 5, '\0'));
2510 }
2511
2512 // Nop sequences of various lengths.
2513 const char nop1[1] = { 0x90 }; // nop
2514 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2515 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2516 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2517 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2518 0x00 }; // leal 0(%esi,1),%esi
2519 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2520 0x00, 0x00 };
2521 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2522 0x00, 0x00, 0x00 };
2523 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2524 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2525 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2526 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2527 0x00 };
2528 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2529 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2530 0x00, 0x00 };
2531 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2532 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2533 0x00, 0x00, 0x00 };
2534 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2535 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2536 0x00, 0x00, 0x00, 0x00 };
2537 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2538 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2539 0x27, 0x00, 0x00, 0x00,
2540 0x00 };
2541 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2542 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2543 0xbc, 0x27, 0x00, 0x00,
2544 0x00, 0x00 };
2545 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2546 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2547 0x90, 0x90, 0x90, 0x90,
2548 0x90, 0x90, 0x90 };
2549
2550 const char* nops[16] = {
2551 NULL,
2552 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2553 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2554 };
2555
2556 return std::string(nops[length], length);
2557}
2558
2559// The selector for x86_64 object files.
2560
2561class Target_selector_x86_64 : public Target_selector
2562{
2563public:
2564 Target_selector_x86_64()
e96caa79 2565 : Target_selector(elfcpp::EM_X86_64, 64, false, "elf64-x86-64")
2e30d253
ILT
2566 { }
2567
2568 Target*
e96caa79
ILT
2569 do_instantiate_target()
2570 { return new Target_x86_64(); }
2e30d253
ILT
2571};
2572
2e30d253
ILT
2573Target_selector_x86_64 target_selector_x86_64;
2574
2575} // End anonymous namespace.
This page took 0.193149 seconds and 4 git commands to generate.