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