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