From Craig Silverstein: x86_64 support.
[deliverable/binutils-gdb.git] / gold / x86_64.cc
CommitLineData
2e30d253
ILT
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
48namespace
49{
50
51using namespace gold;
52
53class Output_data_plt_x86_64;
54
55// The x86_64 target class.
56// See the ABI at http://www.x86-64.org/documentation/abi.pdf
57
58class Target_x86_64 : public Sized_target<64, false>
59{
60 public:
61 // In the x86_64 ABI, it says "The AMD64 ABI architectures uses only
62 // Elf64_Rela relocation entries with explicit addends."
63 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
64
65 Target_x86_64()
66 : Sized_target<64, false>(&x86_64_info),
67 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
68 copy_relocs_(NULL), dynbss_(NULL)
69 { }
70
71 // Scan the relocations to look for symbol adjustments.
72 void
73 scan_relocs(const General_options& options,
74 Symbol_table* symtab,
75 Layout* layout,
76 Sized_relobj<64, false>* object,
77 unsigned int data_shndx,
78 unsigned int sh_type,
79 const unsigned char* prelocs,
80 size_t reloc_count,
81 size_t local_symbol_count,
82 const unsigned char* plocal_symbols,
83 Symbol** global_symbols);
84
85 // Finalize the sections.
86 void
87 do_finalize_sections(Layout*);
88
89 // Relocate a section.
90 void
91 relocate_section(const Relocate_info<64, false>*,
92 unsigned int sh_type,
93 const unsigned char* prelocs,
94 size_t reloc_count,
95 unsigned char* view,
96 elfcpp::Elf_types<64>::Elf_Addr view_address,
97 off_t view_size);
98
99 // Return a string used to fill a code section with nops.
100 std::string
101 do_code_fill(off_t length);
102
103 private:
104 // The class which scans relocations.
105 struct Scan
106 {
107 inline void
108 local(const General_options& options, Symbol_table* symtab,
109 Layout* layout, Target_x86_64* target,
110 Sized_relobj<64, false>* object,
111 unsigned int data_shndx,
112 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
113 const elfcpp::Sym<64, false>& lsym);
114
115 inline void
116 global(const General_options& options, Symbol_table* symtab,
117 Layout* layout, Target_x86_64* target,
118 Sized_relobj<64, false>* object,
119 unsigned int data_shndx,
120 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
121 Symbol* gsym);
122 };
123
124 // The class which implements relocation.
125 class Relocate
126 {
127 public:
128 Relocate()
129 : skip_call_tls_get_addr_(false)
130 { }
131
132 ~Relocate()
133 {
134 if (this->skip_call_tls_get_addr_)
135 {
136 // FIXME: This needs to specify the location somehow.
137 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
138 program_name);
139 gold_exit(false);
140 }
141 }
142
143 // Do a relocation. Return false if the caller should not issue
144 // any warnings about this relocation.
145 inline bool
146 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
147 const elfcpp::Rela<64, false>&,
148 unsigned int r_type, const Sized_symbol<64>*,
149 const Symbol_value<64>*,
150 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
151 off_t);
152
153 private:
154 // Do a TLS relocation.
155 inline void
156 relocate_tls(const Relocate_info<64, false>*, size_t relnum,
157 const elfcpp::Rela<64, false>&,
158 unsigned int r_type, const Sized_symbol<64>*,
159 const Symbol_value<64>*,
160 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, off_t);
161
162 // Do a TLS Initial-Exec to Local-Exec transition.
163 static inline void
164 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
165 Output_segment* tls_segment,
166 const elfcpp::Rela<64, false>&, unsigned int r_type,
167 elfcpp::Elf_types<64>::Elf_Addr value,
168 unsigned char* view,
169 off_t view_size);
170
171 // Do a TLS Global-Dynamic to Local-Exec transition.
172 inline void
173 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
174 Output_segment* tls_segment,
175 const elfcpp::Rela<64, false>&, unsigned int r_type,
176 elfcpp::Elf_types<64>::Elf_Addr value,
177 unsigned char* view,
178 off_t view_size);
179
180 // Check the range for a TLS relocation.
181 static inline void
182 check_range(const Relocate_info<64, false>*, size_t relnum,
183 const elfcpp::Rela<64, false>&, off_t, off_t);
184
185 // Check the validity of a TLS relocation. This is like assert.
186 static inline void
187 check_tls(const Relocate_info<64, false>*, size_t relnum,
188 const elfcpp::Rela<64, false>&, bool);
189
190 // This is set if we should skip the next reloc, which should be a
191 // PLT32 reloc against ___tls_get_addr.
192 bool skip_call_tls_get_addr_;
193 };
194
195 // Adjust TLS relocation type based on the options and whether this
196 // is a local symbol.
197 static unsigned int
198 optimize_tls_reloc(bool is_final, int r_type);
199
200 // Get the GOT section, creating it if necessary.
201 Output_data_got<64, false>*
202 got_section(Symbol_table*, Layout*);
203
204 // Create a PLT entry for a global symbol.
205 void
206 make_plt_entry(Symbol_table*, Layout*, Symbol*);
207
208 // Get the PLT section.
209 Output_data_plt_x86_64*
210 plt_section() const
211 {
212 gold_assert(this->plt_ != NULL);
213 return this->plt_;
214 }
215
216 // Get the dynamic reloc section, creating it if necessary.
217 Reloc_section*
218 rel_dyn_section(Layout*);
219
220 // Copy a relocation against a global symbol.
221 void
222 copy_reloc(const General_options*, Symbol_table*, Layout*,
223 Sized_relobj<64, false>*, unsigned int,
224 Symbol*, const elfcpp::Rela<64, false>&);
225
226 // Information about this specific target which we pass to the
227 // general Target structure.
228 static const Target::Target_info x86_64_info;
229
230 // The GOT section.
231 Output_data_got<64, false>* got_;
232 // The PLT section.
233 Output_data_plt_x86_64* plt_;
234 // The GOT PLT section.
235 Output_data_space* got_plt_;
236 // The dynamic reloc section.
237 Reloc_section* rel_dyn_;
238 // Relocs saved to avoid a COPY reloc.
239 Copy_relocs<64, false>* copy_relocs_;
240 // Space for variables copied with a COPY reloc.
241 Output_data_space* dynbss_;
242};
243
244const Target::Target_info Target_x86_64::x86_64_info =
245{
246 64, // size
247 false, // is_big_endian
248 elfcpp::EM_X86_64, // machine_code
249 false, // has_make_symbol
250 false, // has_resolve
251 true, // has_code_fill
252 "/lib/ld64.so.1", // program interpreter
253 0x400000, // text_segment_address
254 0x1000, // abi_pagesize
255 0x1000 // common_pagesize
256};
257
258// Get the GOT section, creating it if necessary.
259
260Output_data_got<64, false>*
261Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
262{
263 if (this->got_ == NULL)
264 {
265 gold_assert(symtab != NULL && layout != NULL);
266
267 this->got_ = new Output_data_got<64, false>();
268
269 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
270 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
271 this->got_);
272
273 // The old GNU linker creates a .got.plt section. We just
274 // create another set of data in the .got section. Note that we
275 // always create a PLT if we create a GOT, although the PLT
276 // might be empty.
277 // TODO(csilvers): do we really need an alignment of 8?
278 this->got_plt_ = new Output_data_space(8);
279 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
280 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
281 this->got_plt_);
282
283 // The first three entries are reserved.
284 this->got_plt_->set_space_size(3 * 8);
285
286 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
287 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
288 this->got_plt_,
289 0, 0, elfcpp::STT_OBJECT,
290 elfcpp::STB_LOCAL,
291 elfcpp::STV_HIDDEN, 0,
292 false, false);
293 }
294
295 return this->got_;
296}
297
298// Get the dynamic reloc section, creating it if necessary.
299
300Target_x86_64::Reloc_section*
301Target_x86_64::rel_dyn_section(Layout* layout)
302{
303 if (this->rel_dyn_ == NULL)
304 {
305 gold_assert(layout != NULL);
306 this->rel_dyn_ = new Reloc_section();
307 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
308 elfcpp::SHF_ALLOC, this->rel_dyn_);
309 }
310 return this->rel_dyn_;
311}
312
313// A class to handle the PLT data.
314
315class Output_data_plt_x86_64 : public Output_section_data
316{
317 public:
318 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
319
320 Output_data_plt_x86_64(Layout*, Output_data_space*);
321
322 // Add an entry to the PLT.
323 void
324 add_entry(Symbol* gsym);
325
326 // Return the .rel.plt section data.
327 const Reloc_section*
328 rel_plt() const
329 { return this->rel_; }
330
331 protected:
332 void
333 do_adjust_output_section(Output_section* os);
334
335 private:
336 // The size of an entry in the PLT.
337 static const int plt_entry_size = 16;
338
339 // The first entry in the PLT.
340 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
341 // procedure linkage table for both programs and shared objects."
342 static unsigned char first_plt_entry[plt_entry_size];
343
344 // Other entries in the PLT for an executable.
345 static unsigned char plt_entry[plt_entry_size];
346
347 // Set the final size.
348 void
349 do_set_address(uint64_t, off_t)
350 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
351
352 // Write out the PLT data.
353 void
354 do_write(Output_file*);
355
356 // The reloc section.
357 Reloc_section* rel_;
358 // The .got.plt section.
359 Output_data_space* got_plt_;
360 // The number of PLT entries.
361 unsigned int count_;
362};
363
364// Create the PLT section. The ordinary .got section is an argument,
365// since we need to refer to the start. We also create our own .got
366// section just for PLT entries.
367
368Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
369 Output_data_space* got_plt)
370 // TODO(csilvers): do we really need an alignment of 8?
371 : Output_section_data(8), got_plt_(got_plt), count_(0)
372{
373 this->rel_ = new Reloc_section();
374 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
375 elfcpp::SHF_ALLOC, this->rel_);
376}
377
378void
379Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
380{
381 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
382 // linker, and so do we.
383 os->set_entsize(4);
384}
385
386// Add an entry to the PLT.
387
388void
389Output_data_plt_x86_64::add_entry(Symbol* gsym)
390{
391 gold_assert(!gsym->has_plt_offset());
392
393 // Note that when setting the PLT offset we skip the initial
394 // reserved PLT entry.
395 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
396
397 ++this->count_;
398
399 off_t got_offset = this->got_plt_->data_size();
400
401 // Every PLT entry needs a GOT entry which points back to the PLT
402 // entry (this will be changed by the dynamic linker, normally
403 // lazily when the function is called).
404 this->got_plt_->set_space_size(got_offset + 8);
405
406 // Every PLT entry needs a reloc.
407 gsym->set_needs_dynsym_entry();
408 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
409 got_offset, 0);
410
411 // Note that we don't need to save the symbol. The contents of the
412 // PLT are independent of which symbols are used. The symbols only
413 // appear in the relocations.
414}
415
416// The first entry in the PLT for an executable.
417
418unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
419{
420 // From AMD64 ABI Draft 0.98, page 76
421 0xff, 0x35, // pushq contents of memory address
422 0, 0, 0, 0, // replaced with address of .got + 4
423 0xff, 0x25, // jmp indirect
424 0, 0, 0, 0, // replaced with address of .got + 8
425 0x90, 0x90, 0x90, 0x90 // noop (x4)
426};
427
428// Subsequent entries in the PLT for an executable.
429
430unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
431{
432 // From AMD64 ABI Draft 0.98, page 76
433 0xff, 0x25, // jmpq indirect
434 0, 0, 0, 0, // replaced with address of symbol in .got
435 0x68, // pushq immediate
436 0, 0, 0, 0, // replaced with offset into relocation table
437 0xe9, // jmpq relative
438 0, 0, 0, 0 // replaced with offset to start of .plt
439};
440
441// Write out the PLT. This uses the hand-coded instructions above,
442// and adjusts them as needed. This is specified by the AMD64 ABI.
443
444void
445Output_data_plt_x86_64::do_write(Output_file* of)
446{
447 const off_t offset = this->offset();
448 const off_t oview_size = this->data_size();
449 unsigned char* const oview = of->get_output_view(offset, oview_size);
450
451 const off_t got_file_offset = this->got_plt_->offset();
452 const off_t got_size = this->got_plt_->data_size();
453 unsigned char* const got_view = of->get_output_view(got_file_offset,
454 got_size);
455
456 unsigned char* pov = oview;
457
458 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
459 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
460
461 memcpy(pov, first_plt_entry, plt_entry_size);
462 if (!parameters->output_is_shared())
463 {
464 // We do a jmp relative to the PC at the end of this instruction.
465 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
466 - (plt_address + 6));
467 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
468 - (plt_address + 12));
469 }
470 pov += plt_entry_size;
471
472 unsigned char* got_pov = got_view;
473
474 memset(got_pov, 0, 24);
475 got_pov += 24;
476
477 unsigned int plt_offset = plt_entry_size;
478 unsigned int got_offset = 24;
479 const unsigned int count = this->count_;
480 for (unsigned int plt_index = 0;
481 plt_index < count;
482 ++plt_index,
483 pov += plt_entry_size,
484 got_pov += 8,
485 plt_offset += plt_entry_size,
486 got_offset += 8)
487 {
488 // Set and adjust the PLT entry itself.
489 memcpy(pov, plt_entry, plt_entry_size);
490 if (parameters->output_is_shared())
491 // FIXME(csilvers): what's the right thing to write here?
492 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
493 else
494 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
495 (got_address + got_offset
496 - (plt_address + plt_offset
497 + 6)));
498
499 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
500 elfcpp::Swap<32, false>::writeval(pov + 12,
501 - (plt_offset + plt_entry_size));
502
503 // Set the entry in the GOT.
504 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
505 }
506
507 gold_assert(pov - oview == oview_size);
508 gold_assert(got_pov - got_view == got_size);
509
510 of->write_output_view(offset, oview_size, oview);
511 of->write_output_view(got_file_offset, got_size, got_view);
512}
513
514// Create a PLT entry for a global symbol.
515
516void
517Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
518 Symbol* gsym)
519{
520 if (gsym->has_plt_offset())
521 return;
522
523 if (this->plt_ == NULL)
524 {
525 // Create the GOT sections first.
526 this->got_section(symtab, layout);
527
528 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
529 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
530 (elfcpp::SHF_ALLOC
531 | elfcpp::SHF_EXECINSTR),
532 this->plt_);
533 }
534
535 this->plt_->add_entry(gsym);
536}
537
538// Handle a relocation against a non-function symbol defined in a
539// dynamic object. The traditional way to handle this is to generate
540// a COPY relocation to copy the variable at runtime from the shared
541// object into the executable's data segment. However, this is
542// undesirable in general, as if the size of the object changes in the
543// dynamic object, the executable will no longer work correctly. If
544// this relocation is in a writable section, then we can create a
545// dynamic reloc and the dynamic linker will resolve it to the correct
546// address at runtime. However, we do not want do that if the
547// relocation is in a read-only section, as it would prevent the
548// readonly segment from being shared. And if we have to eventually
549// generate a COPY reloc, then any dynamic relocations will be
550// useless. So this means that if this is a writable section, we need
551// to save the relocation until we see whether we have to create a
552// COPY relocation for this symbol for any other relocation.
553
554void
555Target_x86_64::copy_reloc(const General_options* options,
556 Symbol_table* symtab,
557 Layout* layout,
558 Sized_relobj<64, false>* object,
559 unsigned int data_shndx, Symbol* gsym,
560 const elfcpp::Rela<64, false>& rel)
561{
562 Sized_symbol<64>* ssym;
563 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
564 SELECT_SIZE(64));
565
566 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
567 data_shndx, ssym))
568 {
569 // So far we do not need a COPY reloc. Save this relocation.
570 // If it turns out that we never need a COPY reloc for this
571 // symbol, then we will emit the relocation.
572 if (this->copy_relocs_ == NULL)
573 this->copy_relocs_ = new Copy_relocs<64, false>();
574 this->copy_relocs_->save(ssym, object, data_shndx, rel);
575 }
576 else
577 {
578 // Allocate space for this symbol in the .bss section.
579
580 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
581
582 // There is no defined way to determine the required alignment
583 // of the symbol. We pick the alignment based on the size. We
584 // set an arbitrary maximum of 256.
585 unsigned int align;
586 for (align = 1; align < 512; align <<= 1)
587 if ((symsize & align) != 0)
588 break;
589
590 if (this->dynbss_ == NULL)
591 {
592 this->dynbss_ = new Output_data_space(align);
593 layout->add_output_section_data(".bss",
594 elfcpp::SHT_NOBITS,
595 (elfcpp::SHF_ALLOC
596 | elfcpp::SHF_WRITE),
597 this->dynbss_);
598 }
599
600 Output_data_space* dynbss = this->dynbss_;
601
602 if (align > dynbss->addralign())
603 dynbss->set_space_alignment(align);
604
605 off_t dynbss_size = dynbss->data_size();
606 dynbss_size = align_address(dynbss_size, align);
607 off_t offset = dynbss_size;
608 dynbss->set_space_size(dynbss_size + symsize);
609
610 // Define the symbol in the .dynbss section.
611 symtab->define_in_output_data(this, ssym->name(), ssym->version(),
612 dynbss, offset, symsize, ssym->type(),
613 ssym->binding(), ssym->visibility(),
614 ssym->nonvis(), false, false);
615
616 // Add the COPY reloc.
617 ssym->set_needs_dynsym_entry();
618 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
619 rel_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset,
620 rel.get_r_addend());
621 }
622}
623
624
625// Optimize the TLS relocation type based on what we know about the
626// symbol. IS_FINAL is true if the final address of this symbol is
627// known at link time.
628
629unsigned int
630Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
631{
632 return is_final ? r_type : 0;
633#if 0
634 // If we are generating a shared library, then we can't do anything
635 // in the linker.
636 if (parameters->output_is_shared())
637 return r_type;
638
639 switch (r_type)
640 {
641 case elfcpp::R_X86_64_TLSGD:
642 // These are Global-Dynamic which permits fully general TLS
643 // access. Since we know that we are generating an executable,
644 // we can convert this to Initial-Exec. If we also know that
645 // this is a local symbol, we can further switch to Local-Exec.
646 if (is_final)
647 return elfcpp::R_X86_64_TLS_LE_64;
648 return elfcpp::R_X86_64_TLS_IE_64;
649
650 case elfcpp::R_X86_64_TLS_LDM:
651 // This is Local-Dynamic, which refers to a local symbol in the
652 // dynamic TLS block. Since we know that we generating an
653 // executable, we can switch to Local-Exec.
654 return elfcpp::R_X86_64_TLS_LE_64;
655
656 case elfcpp::R_X86_64_TLS_LDO_64:
657 // Another type of Local-Dynamic relocation.
658 return elfcpp::R_X86_64_TLS_LE;
659
660 case elfcpp::R_X86_64_TLS_IE:
661 case elfcpp::R_X86_64_TLS_GOTIE:
662 case elfcpp::R_X86_64_TLS_IE_64:
663 // These are Initial-Exec relocs which get the thread offset
664 // from the GOT. If we know that we are linking against the
665 // local symbol, we can switch to Local-Exec, which links the
666 // thread offset into the instruction.
667 if (is_final)
668 return elfcpp::R_X86_64_TLS_LE_64;
669 return r_type;
670
671 case elfcpp::R_X86_64_TLS_LE:
672 case elfcpp::R_X86_64_TLS_LE_64:
673 // When we already have Local-Exec, there is nothing further we
674 // can do.
675 return r_type;
676
677 default:
678 gold_unreachable();
679 }
680#endif
681}
682
683// Scan a relocation for a local symbol.
684
685inline void
686Target_x86_64::Scan::local(const General_options&,
687 Symbol_table* symtab,
688 Layout* layout,
689 Target_x86_64* target,
690 Sized_relobj<64, false>* object,
691 unsigned int,
692 const elfcpp::Rela<64, false>&,
693 unsigned int r_type,
694 const elfcpp::Sym<64, false>&)
695{
696 switch (r_type)
697 {
698 case elfcpp::R_X86_64_NONE:
699 break;
700
701 case elfcpp::R_X86_64_64:
702 case elfcpp::R_X86_64_32:
703 case elfcpp::R_X86_64_32S:
704 case elfcpp::R_X86_64_16:
705 case elfcpp::R_X86_64_8:
706 // FIXME: If we are generating a shared object we need to copy
707 // this relocation into the object.
708 gold_assert(!parameters->output_is_shared());
709 break;
710
711 case elfcpp::R_X86_64_PC64:
712 case elfcpp::R_X86_64_PC32:
713 case elfcpp::R_X86_64_PC16:
714 case elfcpp::R_X86_64_PC8:
715 break;
716
717 case elfcpp::R_X86_64_GOTOFF64:
718 case elfcpp::R_X86_64_GOTPCREL:
719 // We need a GOT section.
720 target->got_section(symtab, layout);
721 break;
722
723 case elfcpp::R_X86_64_COPY:
724 case elfcpp::R_X86_64_GLOB_DAT:
725 case elfcpp::R_X86_64_JUMP_SLOT:
726 case elfcpp::R_X86_64_RELATIVE:
727 case elfcpp::R_X86_64_TPOFF64:
728 case elfcpp::R_X86_64_TPOFF32:
729 case elfcpp::R_X86_64_DTPMOD64:
730 case elfcpp::R_X86_64_DTPOFF64:
731 case elfcpp::R_X86_64_DTPOFF32:
732 case elfcpp::R_X86_64_TLSDESC:
733 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
734 program_name, object->name().c_str(), r_type);
735 gold_exit(false);
736 break;
737
738#if 0
739 case elfcpp::R_X86_64_TLS_IE:
740 case elfcpp::R_X86_64_TLS_GOTIE:
741 case elfcpp::R_X86_64_TLS_LE:
742 case elfcpp::R_X86_64_TLS_GD:
743 case elfcpp::R_X86_64_TLS_LDM:
744 case elfcpp::R_X86_64_TLS_LDO_64:
745 case elfcpp::R_X86_64_TLS_IE_64:
746 case elfcpp::R_X86_64_TLS_LE_64:
747 {
748 bool output_is_shared = parameters->output_is_shared();
749 r_type = Target_x86_64::optimize_tls_reloc(!output_is_shared,
750 r_type);
751 switch (r_type)
752 {
753 case elfcpp::R_X86_64_TLS_LE:
754 case elfcpp::R_X86_64_TLS_LE_64:
755 // FIXME: If generating a shared object, we need to copy
756 // this relocation into the object.
757 gold_assert(!output_is_shared);
758 break;
759
760 case elfcpp::R_X86_64_TLS_IE:
761 case elfcpp::R_X86_64_TLS_GOTIE:
762 case elfcpp::R_X86_64_TLS_GD:
763 case elfcpp::R_X86_64_TLS_LDM:
764 case elfcpp::R_X86_64_TLS_LDO_64:
765 case elfcpp::R_X86_64_TLS_IE_64:
766 fprintf(stderr,
767 _("%s: %s: unsupported reloc %u against local symbol\n"),
768 program_name, object->name().c_str(), r_type);
769 break;
770 }
771 }
772 break;
773#endif
774
775#if 0
776 case elfcpp::R_X86_64_GOT32:
777 case elfcpp::R_X86_64_PLT32:
778 case elfcpp::R_X86_64_TLS_GD_64:
779 case elfcpp::R_X86_64_TLS_GD_PUSH:
780 case elfcpp::R_X86_64_TLS_GD_CALL:
781 case elfcpp::R_X86_64_TLS_GD_POP:
782 case elfcpp::R_X86_64_TLS_LDM_64:
783 case elfcpp::R_X86_64_TLS_LDM_PUSH:
784 case elfcpp::R_X86_64_TLS_LDM_CALL:
785 case elfcpp::R_X86_64_TLS_LDM_POP:
786#endif
787 default:
788 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
789 program_name, object->name().c_str(), r_type);
790 break;
791 }
792}
793
794
795// Scan a relocation for a global symbol.
796
797inline void
798Target_x86_64::Scan::global(const General_options& options,
799 Symbol_table* symtab,
800 Layout* layout,
801 Target_x86_64* target,
802 Sized_relobj<64, false>* object,
803 unsigned int data_shndx,
804 const elfcpp::Rela<64, false>& reloc,
805 unsigned int r_type,
806 Symbol* gsym)
807{
808 switch (r_type)
809 {
810 case elfcpp::R_X86_64_NONE:
811 break;
812
813 case elfcpp::R_X86_64_64:
814 case elfcpp::R_X86_64_PC64:
815 case elfcpp::R_X86_64_32:
816 case elfcpp::R_X86_64_32S:
817 case elfcpp::R_X86_64_PC32:
818 case elfcpp::R_X86_64_16:
819 case elfcpp::R_X86_64_PC16:
820 case elfcpp::R_X86_64_8:
821 case elfcpp::R_X86_64_PC8:
822 // FIXME: If we are generating a shared object we may need to
823 // copy this relocation into the object. If this symbol is
824 // defined in a shared object, we may need to copy this
825 // relocation in order to avoid a COPY relocation.
826 gold_assert(!parameters->output_is_shared());
827
828 if (gsym->is_from_dynobj())
829 {
830 // This symbol is defined in a dynamic object. If it is a
831 // function, we make a PLT entry. Otherwise we need to
832 // either generate a COPY reloc or copy this reloc.
833 if (gsym->type() == elfcpp::STT_FUNC)
834 target->make_plt_entry(symtab, layout, gsym);
835 else
836 target->copy_reloc(&options, symtab, layout, object, data_shndx,
837 gsym, reloc);
838 }
839
840 break;
841
842 case elfcpp::R_X86_64_GOT32:
843 {
844 // The symbol requires a GOT entry.
845 Output_data_got<64, false>* got = target->got_section(symtab, layout);
846 if (got->add_global(gsym))
847 {
848 // If this symbol is not fully resolved, we need to add a
849 // dynamic relocation for it.
850 if (!gsym->final_value_is_known())
851 {
852 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
853 rel_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, got,
854 gsym->got_offset(), reloc.get_r_addend());
855 }
856 }
857 }
858 break;
859
860 case elfcpp::R_X86_64_PLT32:
861 // If the symbol is fully resolved, this is just a PC32 reloc.
862 // Otherwise we need a PLT entry.
863 if (gsym->final_value_is_known())
864 break;
865 target->make_plt_entry(symtab, layout, gsym);
866 break;
867
868 case elfcpp::R_X86_64_GOTOFF64:
869 case elfcpp::R_X86_64_GOTPC32:
870 case elfcpp::R_X86_64_GOTPCREL:
871 // We need a GOT section.
872 target->got_section(symtab, layout);
873 break;
874
875#if 0
876 case elfcpp::R_X86_64_COPY:
877 case elfcpp::R_X86_64_GLOB_DAT:
878 case elfcpp::R_X86_64_JUMP_SLOT:
879 case elfcpp::R_X86_64_RELATIVE:
880 case elfcpp::R_X86_64_TLS_TPOFF:
881 case elfcpp::R_X86_64_DTPMOD64:
882 case elfcpp::R_X86_64_DTPOFF64:
883 case elfcpp::R_X86_64_DTPOFF32:
884 case elfcpp::R_X86_64_TLS_TPOFF64:
885 case elfcpp::R_X86_64_TLS_DESC:
886 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
887 program_name, object->name().c_str(), r_type);
888 gold_exit(false);
889 break;
890#endif
891
892#if 0
893 case elfcpp::R_X86_64_TLS_IE:
894 case elfcpp::R_X86_64_TLS_GOTIE:
895 case elfcpp::R_X86_64_TLS_LE:
896 case elfcpp::R_X86_64_TLS_GD:
897 case elfcpp::R_X86_64_TLS_LDM:
898 case elfcpp::R_X86_64_TLS_LDO_64:
899 case elfcpp::R_X86_64_TLS_IE_64:
900 case elfcpp::R_X86_64_TLS_LE_64:
901 {
902 const bool is_final = gsym->final_value_is_known();
903 r_type = Target_x86_64::optimize_tls_reloc(is_final, r_type);
904 switch (r_type)
905 {
906 case elfcpp::R_X86_64_TLS_LE:
907 case elfcpp::R_X86_64_TLS_LE_64:
908 // FIXME: If generating a shared object, we need to copy
909 // this relocation into the object.
910 gold_assert(!parameters->output_is_shared());
911 break;
912
913 case elfcpp::R_X86_64_TLS_IE:
914 case elfcpp::R_X86_64_TLS_GOTIE:
915 case elfcpp::R_X86_64_TLS_GD:
916 case elfcpp::R_X86_64_TLS_LDM:
917 case elfcpp::R_X86_64_TLS_LDO_64:
918 case elfcpp::R_X86_64_TLS_IE_64:
919 fprintf(stderr,
920 _("%s: %s: unsupported reloc %u "
921 "against global symbol %s\n"),
922 program_name, object->name().c_str(), r_type,
923 gsym->name());
924 break;
925 }
926 }
927 break;
928#endif
929
930#if 0
931 case elfcpp::R_X86_64_TLS_GD_64:
932 case elfcpp::R_X86_64_TLS_GD_PUSH:
933 case elfcpp::R_X86_64_TLS_GD_CALL:
934 case elfcpp::R_X86_64_TLS_GD_POP:
935 case elfcpp::R_X86_64_TLS_LDM_64:
936 case elfcpp::R_X86_64_TLS_LDM_PUSH:
937 case elfcpp::R_X86_64_TLS_LDM_CALL:
938 case elfcpp::R_X86_64_TLS_LDM_POP:
939#endif
940 default:
941 fprintf(stderr,
942 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
943 program_name, object->name().c_str(), r_type, gsym->name());
944 break;
945 }
946}
947
948// Scan relocations for a section.
949
950void
951Target_x86_64::scan_relocs(const General_options& options,
952 Symbol_table* symtab,
953 Layout* layout,
954 Sized_relobj<64, false>* object,
955 unsigned int data_shndx,
956 unsigned int sh_type,
957 const unsigned char* prelocs,
958 size_t reloc_count,
959 size_t local_symbol_count,
960 const unsigned char* plocal_symbols,
961 Symbol** global_symbols)
962{
963 if (sh_type == elfcpp::SHT_REL)
964 {
965 fprintf(stderr, _("%s: %s: unsupported REL reloc section\n"),
966 program_name, object->name().c_str());
967 gold_exit(false);
968 }
969
970 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
971 Target_x86_64::Scan>(
972 options,
973 symtab,
974 layout,
975 this,
976 object,
977 data_shndx,
978 prelocs,
979 reloc_count,
980 local_symbol_count,
981 plocal_symbols,
982 global_symbols);
983}
984
985// Finalize the sections.
986
987void
988Target_x86_64::do_finalize_sections(Layout* layout)
989{
990 // Fill in some more dynamic tags.
991 Output_data_dynamic* const odyn = layout->dynamic_data();
992 if (odyn != NULL)
993 {
994 if (this->got_plt_ != NULL)
995 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
996
997 if (this->plt_ != NULL)
998 {
999 const Output_data* od = this->plt_->rel_plt();
1000 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1001 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1002 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1003 }
1004
1005 if (this->rel_dyn_ != NULL)
1006 {
1007 const Output_data* od = this->rel_dyn_;
1008 odyn->add_section_address(elfcpp::DT_RELA, od);
1009 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1010 odyn->add_constant(elfcpp::DT_RELAENT,
1011 elfcpp::Elf_sizes<64>::rel_size);
1012 }
1013
1014 if (!parameters->output_is_shared())
1015 {
1016 // The value of the DT_DEBUG tag is filled in by the dynamic
1017 // linker at run time, and used by the debugger.
1018 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1019 }
1020 }
1021
1022 // Emit any relocs we saved in an attempt to avoid generating COPY
1023 // relocs.
1024 if (this->copy_relocs_ == NULL)
1025 return;
1026 if (this->copy_relocs_->any_to_emit())
1027 {
1028 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1029 this->copy_relocs_->emit(rel_dyn);
1030 }
1031 delete this->copy_relocs_;
1032 this->copy_relocs_ = NULL;
1033}
1034
1035// Perform a relocation.
1036
1037inline bool
1038Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1039 Target_x86_64* target,
1040 size_t relnum,
1041 const elfcpp::Rela<64, false>& rel,
1042 unsigned int r_type,
1043 const Sized_symbol<64>* gsym,
1044 const Symbol_value<64>* psymval,
1045 unsigned char* view,
1046 elfcpp::Elf_types<64>::Elf_Addr address,
1047 off_t view_size)
1048{
1049 if (this->skip_call_tls_get_addr_)
1050 {
1051 if (r_type != elfcpp::R_X86_64_PLT32
1052 || gsym == NULL
1053 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1054 {
1055 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
1056 program_name,
1057 relinfo->location(relnum, rel.get_r_offset()).c_str());
1058 gold_exit(false);
1059 }
1060
1061 this->skip_call_tls_get_addr_ = false;
1062
1063 return false;
1064 }
1065
1066 // Pick the value to use for symbols defined in shared objects.
1067 Symbol_value<64> symval;
1068 if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
1069 {
1070 symval.set_output_value(target->plt_section()->address()
1071 + gsym->plt_offset());
1072 psymval = &symval;
1073 }
1074
1075 const Sized_relobj<64, false>* object = relinfo->object;
1076 const elfcpp::Elf_Xword addend = rel.get_r_addend();
1077
1078 switch (r_type)
1079 {
1080 case elfcpp::R_X86_64_NONE:
1081 break;
1082
1083 case elfcpp::R_X86_64_64:
1084 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1085 break;
1086
1087 case elfcpp::R_X86_64_PC64:
1088 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1089 address);
1090 break;
1091
1092 case elfcpp::R_X86_64_32:
1093 // FIXME: Needs error checking.
1094 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1095 break;
1096
1097 case elfcpp::R_X86_64_32S:
1098 // FIXME: Needs error checking.
1099 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1100 break;
1101
1102 case elfcpp::R_X86_64_PC32:
1103 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1104 address);
1105 break;
1106
1107 case elfcpp::R_X86_64_16:
1108 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1109 break;
1110
1111 case elfcpp::R_X86_64_PC16:
1112 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1113 address);
1114 break;
1115
1116 case elfcpp::R_X86_64_8:
1117 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1118 break;
1119
1120 case elfcpp::R_X86_64_PC8:
1121 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1122 address);
1123 break;
1124
1125 case elfcpp::R_X86_64_PLT32:
1126 gold_assert(gsym->has_plt_offset()
1127 || gsym->final_value_is_known());
1128 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1129 address);
1130 break;
1131
1132 case elfcpp::R_X86_64_GOT32:
1133 // Local GOT offsets not yet supported.
1134 gold_assert(gsym);
1135 gold_assert(gsym->has_got_offset());
1136 Relocate_functions<64, false>::rela32(view, gsym->got_offset(), addend);
1137 break;
1138
1139 case elfcpp::R_X86_64_GOTOFF64:
1140 {
1141 elfcpp::Elf_types<64>::Elf_Addr value;
1142 value = (psymval->value(object, 0)
1143 - target->got_section(NULL, NULL)->address());
1144 Relocate_functions<64, false>::rela64(view, value, addend);
1145 }
1146 break;
1147
1148 case elfcpp::R_X86_64_GOTPCREL:
1149 {
1150 gold_assert(gsym);
1151 elfcpp::Elf_types<64>::Elf_Addr value;
1152 // FIXME(csilvers): this is probably totally wrong for G + GOT
1153 value = (target->got_section(NULL, NULL)->address()
1154 + (gsym->has_got_offset() ? gsym->got_offset() : 0));
1155 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1156 }
1157 break;
1158
1159 case elfcpp::R_X86_64_COPY:
1160 case elfcpp::R_X86_64_GLOB_DAT:
1161 case elfcpp::R_X86_64_JUMP_SLOT:
1162 case elfcpp::R_X86_64_RELATIVE:
1163 case elfcpp::R_X86_64_TPOFF64:
1164 case elfcpp::R_X86_64_TPOFF32:
1165 case elfcpp::R_X86_64_DTPMOD64:
1166 case elfcpp::R_X86_64_DTPOFF64:
1167 case elfcpp::R_X86_64_DTPOFF32:
1168 case elfcpp::R_X86_64_TLSDESC:
1169 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1170 program_name,
1171 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1172 r_type);
1173 gold_exit(false);
1174 break;
1175
1176#if 0
1177 case elfcpp::R_X86_64_TLS_IE:
1178 case elfcpp::R_X86_64_TLS_GOTIE:
1179 case elfcpp::R_X86_64_TLS_LE:
1180 case elfcpp::R_X86_64_TLS_GD:
1181 case elfcpp::R_X86_64_TLS_LDM:
1182 case elfcpp::R_X86_64_TLS_LDO_64:
1183 case elfcpp::R_X86_64_TLS_IE_64:
1184 case elfcpp::R_X86_64_TLS_LE_64:
1185 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
1186 address, view_size);
1187 break;
1188#else
1189 view_size++; // this is to make view_size used
1190#endif
1191
1192#if 0
1193 case elfcpp::R_X86_64_TLS_GD_64:
1194 case elfcpp::R_X86_64_TLS_GD_PUSH:
1195 case elfcpp::R_X86_64_TLS_GD_CALL:
1196 case elfcpp::R_X86_64_TLS_GD_POP:
1197 case elfcpp::R_X86_64_TLS_LDM_64:
1198 case elfcpp::R_X86_64_TLS_LDM_PUSH:
1199 case elfcpp::R_X86_64_TLS_LDM_CALL:
1200 case elfcpp::R_X86_64_TLS_LDM_POP:
1201#endif
1202 default:
1203 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1204 program_name,
1205 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1206 r_type);
1207 // gold_exit(false);
1208 break;
1209 }
1210
1211 return true;
1212}
1213
1214// Perform a TLS relocation.
1215
1216inline void
1217Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>*, // relinfo,
1218 size_t , // relnum,
1219 const elfcpp::Rela<64, false>& , // rel,
1220 unsigned int , // r_type,
1221 const Sized_symbol<64>* , // gsym,
1222 const Symbol_value<64>* , // psymval,
1223 unsigned char* , // view,
1224 elfcpp::Elf_types<64>::Elf_Addr,
1225 off_t)// view_size)
1226{
1227#if 0
1228 Output_segment* tls_segment = relinfo->layout->tls_segment();
1229 if (tls_segment == NULL)
1230 {
1231 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1232 program_name,
1233 relinfo->location(relnum, rel.get_r_offset()).c_str());
1234 gold_exit(false);
1235 }
1236
1237 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1238
1239 const bool is_final = (gsym == NULL
1240 ? !parameters->output_is_shared()
1241 : gsym->final_value_is_known());
1242 const unsigned int opt_r_type =
1243 Target_x86_64::optimize_tls_reloc(is_final, r_type);
1244 switch (r_type)
1245 {
1246 case elfcpp::R_X86_64_TLS_LE_64:
1247 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1248 Relocate_functions<64, false>::rel64(view, value);
1249 break;
1250
1251 case elfcpp::R_X86_64_TLS_LE:
1252 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1253 Relocate_functions<64, false>::rel64(view, value);
1254 break;
1255
1256 case elfcpp::R_X86_64_TLS_IE:
1257 case elfcpp::R_X86_64_TLS_GOTIE:
1258 case elfcpp::R_X86_64_TLS_IE_64:
1259 if (opt_r_type == elfcpp::R_X86_64_TLS_LE_64)
1260 {
1261 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1262 rel, r_type, value, view,
1263 view_size);
1264 break;
1265 }
1266 fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
1267 program_name,
1268 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1269 r_type);
1270 // gold_exit(false);
1271 break;
1272
1273 case elfcpp::R_X86_64_TLS_GD:
1274 if (opt_r_type == elfcpp::R_X86_64_TLS_LE_64)
1275 {
1276 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1277 rel, r_type, value, view,
1278 view_size);
1279 break;
1280 }
1281 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1282 program_name,
1283 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1284 r_type);
1285 // gold_exit(false);
1286 break;
1287
1288 case elfcpp::R_X86_64_TLS_LDM:
1289 case elfcpp::R_X86_64_TLS_LDO_64:
1290 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1291 program_name,
1292 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1293 r_type);
1294 // gold_exit(false);
1295 break;
1296 }
1297#endif
1298}
1299
1300// Do a relocation in which we convert a TLS Initial-Exec to a
1301// Local-Exec.
1302
1303inline void
1304Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* , // relinfo,
1305 size_t , // relnum,
1306 Output_segment* , // tls_segment,
1307 const elfcpp::Rela<64, false>& , // rel,
1308 unsigned int , // r_type,
1309 elfcpp::Elf_types<64>::Elf_Addr , // value,
1310 unsigned char* , // view,
1311 off_t) // view_size)
1312{
1313#if 0
1314 // We have to actually change the instructions, which means that we
1315 // need to examine the opcodes to figure out which instruction we
1316 // are looking at.
1317 if (r_type == elfcpp::R_X86_64_TLS_IE)
1318 {
1319 // movl %gs:XX,%eax ==> movl $YY,%eax
1320 // movl %gs:XX,%reg ==> movl $YY,%reg
1321 // addl %gs:XX,%reg ==> addl $YY,%reg
1322 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1323 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1324
1325 unsigned char op1 = view[-1];
1326 if (op1 == 0xa1)
1327 {
1328 // movl XX,%eax ==> movl $YY,%eax
1329 view[-1] = 0xb8;
1330 }
1331 else
1332 {
1333 Target_x86_64::Relocate::check_range(relinfo, relnum, rel,
1334 view_size, -2);
1335
1336 unsigned char op2 = view[-2];
1337 if (op2 == 0x8b)
1338 {
1339 // movl XX,%reg ==> movl $YY,%reg
1340 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1341 (op1 & 0xc7) == 0x05);
1342 view[-2] = 0xc7;
1343 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1344 }
1345 else if (op2 == 0x03)
1346 {
1347 // addl XX,%reg ==> addl $YY,%reg
1348 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1349 (op1 & 0xc7) == 0x05);
1350 view[-2] = 0x81;
1351 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1352 }
1353 else
1354 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, 0);
1355 }
1356 }
1357 else
1358 {
1359 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1360 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1361 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1362 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1363 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1364
1365 unsigned char op1 = view[-1];
1366 unsigned char op2 = view[-2];
1367 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1368 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1369 if (op2 == 0x8b)
1370 {
1371 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1372 view[-2] = 0xc7;
1373 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1374 }
1375 else if (op2 == 0x2b)
1376 {
1377 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1378 view[-2] = 0x81;
1379 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1380 }
1381 else if (op2 == 0x03)
1382 {
1383 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1384 view[-2] = 0x81;
1385 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1386 }
1387 else
1388 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, 0);
1389 }
1390
1391 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1392 if (r_type == elfcpp::R_X86_64_TLS_IE || r_type == elfcpp::R_X86_64_TLS_GOTIE)
1393 value = - value;
1394
1395 Relocate_functions<64, false>::rel64(view, value);
1396#endif
1397}
1398
1399// Do a relocation in which we convert a TLS Global-Dynamic to a
1400// Local-Exec.
1401
1402inline void
1403Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* , // relinfo,
1404 size_t , // relnum,
1405 Output_segment* , // tls_segment,
1406 const elfcpp::Rela<64, false>& , // rel,
1407 unsigned int,
1408 elfcpp::Elf_types<64>::Elf_Addr , // value,
1409 unsigned char* , // view,
1410 off_t)// view_size)
1411{
1412#if 0
1413 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1414 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1415 // leal foo(%reg),%eax; call ___tls_get_addr
1416 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1417
1418 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1419 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1420
1421 unsigned char op1 = view[-1];
1422 unsigned char op2 = view[-2];
1423
1424 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1425 op2 == 0x8d || op2 == 0x04);
1426 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1427 view[4] == 0xe8);
1428
1429 int roff = 5;
1430
1431 if (op2 == 0x04)
1432 {
1433 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1434 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1435 view[-3] == 0x8d);
1436 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1437 ((op1 & 0xc7) == 0x05
1438 && op1 != (4 << 3)));
1439 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1440 }
1441 else
1442 {
1443 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1444 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1445 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1446 {
1447 // There is a trailing nop. Use the size byte subl.
1448 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1449 roff = 6;
1450 }
1451 else
1452 {
1453 // Use the five byte subl.
1454 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1455 }
1456 }
1457
1458 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1459 Relocate_functions<64, false>::rel64(view + roff, value);
1460
1461 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1462 // We can skip it.
1463 this->skip_call_tls_get_addr_ = true;
1464#endif
1465}
1466
1467// Check the range for a TLS relocation.
1468
1469inline void
1470Target_x86_64::Relocate::check_range(const Relocate_info<64, false>* relinfo,
1471 size_t relnum,
1472 const elfcpp::Rela<64, false>& rel,
1473 off_t view_size, off_t off)
1474{
1475 off_t offset = rel.get_r_offset() + off;
1476 if (offset < 0 || offset > view_size)
1477 {
1478 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1479 program_name,
1480 relinfo->location(relnum, rel.get_r_offset()).c_str());
1481 gold_exit(false);
1482 }
1483}
1484
1485// Check the validity of a TLS relocation. This is like assert.
1486
1487inline void
1488Target_x86_64::Relocate::check_tls(const Relocate_info<64, false>* relinfo,
1489 size_t relnum,
1490 const elfcpp::Rela<64, false>& rel,
1491 bool valid)
1492{
1493 if (!valid)
1494 {
1495 fprintf(stderr,
1496 _("%s: %s: TLS relocation against invalid instruction\n"),
1497 program_name,
1498 relinfo->location(relnum, rel.get_r_offset()).c_str());
1499 gold_exit(false);
1500 }
1501}
1502
1503// Relocate section data.
1504
1505void
1506Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1507 unsigned int sh_type,
1508 const unsigned char* prelocs,
1509 size_t reloc_count,
1510 unsigned char* view,
1511 elfcpp::Elf_types<64>::Elf_Addr address,
1512 off_t view_size)
1513{
1514 gold_assert(sh_type == elfcpp::SHT_RELA);
1515
1516 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1517 Target_x86_64::Relocate>(
1518 relinfo,
1519 this,
1520 prelocs,
1521 reloc_count,
1522 view,
1523 address,
1524 view_size);
1525}
1526
1527// Return a string used to fill a code section with nops to take up
1528// the specified length.
1529
1530std::string
1531Target_x86_64::do_code_fill(off_t length)
1532{
1533 if (length >= 16)
1534 {
1535 // Build a jmpq instruction to skip over the bytes.
1536 unsigned char jmp[5];
1537 jmp[0] = 0xe9;
1538 elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1539 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1540 + std::string(length - 5, '\0'));
1541 }
1542
1543 // Nop sequences of various lengths.
1544 const char nop1[1] = { 0x90 }; // nop
1545 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1546 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1547 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1548 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1549 0x00 }; // leal 0(%esi,1),%esi
1550 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1551 0x00, 0x00 };
1552 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1553 0x00, 0x00, 0x00 };
1554 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1555 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1556 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1557 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1558 0x00 };
1559 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1560 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1561 0x00, 0x00 };
1562 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1563 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1564 0x00, 0x00, 0x00 };
1565 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1566 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1567 0x00, 0x00, 0x00, 0x00 };
1568 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1569 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1570 0x27, 0x00, 0x00, 0x00,
1571 0x00 };
1572 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1573 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1574 0xbc, 0x27, 0x00, 0x00,
1575 0x00, 0x00 };
1576 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1577 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1578 0x90, 0x90, 0x90, 0x90,
1579 0x90, 0x90, 0x90 };
1580
1581 const char* nops[16] = {
1582 NULL,
1583 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1584 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1585 };
1586
1587 return std::string(nops[length], length);
1588}
1589
1590// The selector for x86_64 object files.
1591
1592class Target_selector_x86_64 : public Target_selector
1593{
1594public:
1595 Target_selector_x86_64()
1596 : Target_selector(elfcpp::EM_X86_64, 64, false)
1597 { }
1598
1599 Target*
1600 recognize(int machine, int osabi, int abiversion);
1601
1602 private:
1603 Target_x86_64* target_;
1604};
1605
1606// Recognize an x86_64 object file when we already know that the machine
1607// number is EM_X86_64.
1608
1609Target*
1610Target_selector_x86_64::recognize(int, int, int)
1611{
1612 if (this->target_ == NULL)
1613 this->target_ = new Target_x86_64();
1614 return this->target_;
1615}
1616
1617Target_selector_x86_64 target_selector_x86_64;
1618
1619} // End anonymous namespace.
This page took 0.081348 seconds and 4 git commands to generate.