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