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