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