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