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