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