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