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