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