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