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