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