elfcpp/ChangeLog:
[deliverable/binutils-gdb.git] / gold / x86_64.cc
CommitLineData
2e30d253
ILT
1// x86_64.cc -- x86_64 target support for gold.
2
a8df5856 3// Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
2e30d253
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8b105e34
ILT
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
2e30d253
ILT
11// (at your option) any later version.
12
8b105e34
ILT
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
2e30d253
ILT
22
23#include "gold.h"
24
25#include <cstring>
26
27#include "elfcpp.h"
28#include "parameters.h"
29#include "reloc.h"
30#include "x86_64.h"
31#include "object.h"
32#include "symtab.h"
33#include "layout.h"
34#include "output.h"
12c0daef 35#include "copy-relocs.h"
2e30d253
ILT
36#include "target.h"
37#include "target-reloc.h"
38#include "target-select.h"
e041f13d 39#include "tls.h"
36959681 40#include "freebsd.h"
f345227a 41#include "gc.h"
21bb3914 42#include "icf.h"
2e30d253
ILT
43
44namespace
45{
46
47using namespace gold;
48
49class Output_data_plt_x86_64;
50
51// The x86_64 target class.
d61c17ea
ILT
52// See the ABI at
53// http://www.x86-64.org/documentation/abi.pdf
54// TLS info comes from
55// http://people.redhat.com/drepper/tls.pdf
0ffd9845 56// http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
2e30d253 57
36959681 58class Target_x86_64 : public Target_freebsd<64, false>
2e30d253
ILT
59{
60 public:
e822f2b1
ILT
61 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
62 // uses only Elf64_Rela relocation entries with explicit addends."
2e30d253
ILT
63 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
64
65 Target_x86_64()
36959681 66 : Target_freebsd<64, false>(&x86_64_info),
a8df5856
ILT
67 got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
68 global_offset_table_(NULL), rela_dyn_(NULL),
69 copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
e291e7b9
ILT
70 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
71 tls_base_symbol_defined_(false)
2e30d253
ILT
72 { }
73
21bb3914
ST
74 // This function should be defined in targets that can use relocation
75 // types to determine (implemented in local_reloc_may_be_function_pointer
76 // and global_reloc_may_be_function_pointer)
77 // if a function's pointer is taken. ICF uses this in safe mode to only
78 // fold those functions whose pointer is defintely not taken. For x86_64
79 // pie binaries, safe ICF cannot be done by looking at relocation types.
80 inline bool
81 can_check_for_function_pointers() const
82 { return !parameters->options().pie(); }
83
8a5e3e08
ILT
84 // Hook for a new output section.
85 void
86 do_new_output_section(Output_section*) const;
87
6d03d481
ST
88 // Scan the relocations to look for symbol adjustments.
89 void
ad0f2072 90 gc_process_relocs(Symbol_table* symtab,
6d03d481
ST
91 Layout* layout,
92 Sized_relobj<64, false>* object,
93 unsigned int data_shndx,
94 unsigned int sh_type,
95 const unsigned char* prelocs,
96 size_t reloc_count,
97 Output_section* output_section,
98 bool needs_special_offset_handling,
99 size_t local_symbol_count,
100 const unsigned char* plocal_symbols);
101
2e30d253
ILT
102 // Scan the relocations to look for symbol adjustments.
103 void
ad0f2072 104 scan_relocs(Symbol_table* symtab,
2e30d253
ILT
105 Layout* layout,
106 Sized_relobj<64, false>* object,
107 unsigned int data_shndx,
108 unsigned int sh_type,
109 const unsigned char* prelocs,
110 size_t reloc_count,
730cdc88
ILT
111 Output_section* output_section,
112 bool needs_special_offset_handling,
2e30d253 113 size_t local_symbol_count,
730cdc88 114 const unsigned char* plocal_symbols);
2e30d253
ILT
115
116 // Finalize the sections.
117 void
f59f41f3 118 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2e30d253 119
4fb6c25d
ILT
120 // Return the value to use for a dynamic which requires special
121 // treatment.
122 uint64_t
123 do_dynsym_value(const Symbol*) const;
124
2e30d253
ILT
125 // Relocate a section.
126 void
127 relocate_section(const Relocate_info<64, false>*,
128 unsigned int sh_type,
129 const unsigned char* prelocs,
130 size_t reloc_count,
730cdc88
ILT
131 Output_section* output_section,
132 bool needs_special_offset_handling,
2e30d253
ILT
133 unsigned char* view,
134 elfcpp::Elf_types<64>::Elf_Addr view_address,
364c7fa5
ILT
135 section_size_type view_size,
136 const Reloc_symbol_changes*);
2e30d253 137
6a74a719
ILT
138 // Scan the relocs during a relocatable link.
139 void
ad0f2072 140 scan_relocatable_relocs(Symbol_table* symtab,
6a74a719
ILT
141 Layout* layout,
142 Sized_relobj<64, false>* object,
143 unsigned int data_shndx,
144 unsigned int sh_type,
145 const unsigned char* prelocs,
146 size_t reloc_count,
147 Output_section* output_section,
148 bool needs_special_offset_handling,
149 size_t local_symbol_count,
150 const unsigned char* plocal_symbols,
151 Relocatable_relocs*);
152
153 // Relocate a section during a relocatable link.
154 void
155 relocate_for_relocatable(const Relocate_info<64, false>*,
156 unsigned int sh_type,
157 const unsigned char* prelocs,
158 size_t reloc_count,
159 Output_section* output_section,
160 off_t offset_in_output_section,
161 const Relocatable_relocs*,
162 unsigned char* view,
163 elfcpp::Elf_types<64>::Elf_Addr view_address,
164 section_size_type view_size,
165 unsigned char* reloc_view,
166 section_size_type reloc_view_size);
167
2e30d253
ILT
168 // Return a string used to fill a code section with nops.
169 std::string
8851ecca 170 do_code_fill(section_size_type length) const;
2e30d253 171
9a2d6984
ILT
172 // Return whether SYM is defined by the ABI.
173 bool
9c2d0ef9 174 do_is_defined_by_abi(const Symbol* sym) const
9a2d6984
ILT
175 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
176
e291e7b9
ILT
177 // Return the symbol index to use for a target specific relocation.
178 // The only target specific relocation is R_X86_64_TLSDESC for a
179 // local symbol, which is an absolute reloc.
180 unsigned int
181 do_reloc_symbol_index(void*, unsigned int r_type) const
182 {
183 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
184 return 0;
185 }
186
187 // Return the addend to use for a target specific relocation.
188 uint64_t
189 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
190
364c7fa5
ILT
191 // Adjust -fstack-split code which calls non-stack-split code.
192 void
193 do_calls_non_split(Relobj* object, unsigned int shndx,
194 section_offset_type fnoffset, section_size_type fnsize,
195 unsigned char* view, section_size_type view_size,
196 std::string* from, std::string* to) const;
197
96f2030e 198 // Return the size of the GOT section.
fe8718a4 199 section_size_type
96f2030e
ILT
200 got_size()
201 {
202 gold_assert(this->got_ != NULL);
203 return this->got_->data_size();
204 }
205
e291e7b9
ILT
206 // Add a new reloc argument, returning the index in the vector.
207 size_t
208 add_tlsdesc_info(Sized_relobj<64, false>* object, unsigned int r_sym)
209 {
210 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
211 return this->tlsdesc_reloc_info_.size() - 1;
212 }
213
2e30d253
ILT
214 private:
215 // The class which scans relocations.
a036edd8 216 class Scan
2e30d253 217 {
a036edd8
ILT
218 public:
219 Scan()
220 : issued_non_pic_error_(false)
221 { }
222
2e30d253 223 inline void
ad0f2072 224 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
2e30d253
ILT
225 Sized_relobj<64, false>* object,
226 unsigned int data_shndx,
07f397ab 227 Output_section* output_section,
2e30d253
ILT
228 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
229 const elfcpp::Sym<64, false>& lsym);
230
231 inline void
ad0f2072 232 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
2e30d253
ILT
233 Sized_relobj<64, false>* object,
234 unsigned int data_shndx,
07f397ab 235 Output_section* output_section,
2e30d253
ILT
236 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
237 Symbol* gsym);
e041f13d 238
21bb3914
ST
239 inline bool
240 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
241 Target_x86_64* target,
242 Sized_relobj<64, false>* object,
243 unsigned int data_shndx,
244 Output_section* output_section,
245 const elfcpp::Rela<64, false>& reloc,
246 unsigned int r_type,
247 const elfcpp::Sym<64, false>& lsym);
248
249 inline bool
250 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
251 Target_x86_64* target,
252 Sized_relobj<64, false>* object,
253 unsigned int data_shndx,
254 Output_section* output_section,
255 const elfcpp::Rela<64, false>& reloc,
256 unsigned int r_type,
257 Symbol* gsym);
258
a036edd8 259 private:
e041f13d
ILT
260 static void
261 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
262
263 static void
264 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
265 Symbol*);
a036edd8
ILT
266
267 void
268 check_non_pic(Relobj*, unsigned int r_type);
269
21bb3914
ST
270 inline bool
271 possible_function_pointer_reloc(unsigned int r_type);
272
a036edd8
ILT
273 // Whether we have issued an error about a non-PIC compilation.
274 bool issued_non_pic_error_;
2e30d253
ILT
275 };
276
277 // The class which implements relocation.
278 class Relocate
279 {
280 public:
281 Relocate()
497897f9 282 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
2e30d253
ILT
283 { }
284
285 ~Relocate()
286 {
287 if (this->skip_call_tls_get_addr_)
288 {
289 // FIXME: This needs to specify the location somehow.
a0c4fb0a 290 gold_error(_("missing expected TLS relocation"));
2e30d253
ILT
291 }
292 }
293
294 // Do a relocation. Return false if the caller should not issue
295 // any warnings about this relocation.
296 inline bool
031cdbed
ILT
297 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
298 size_t relnum, const elfcpp::Rela<64, false>&,
2e30d253
ILT
299 unsigned int r_type, const Sized_symbol<64>*,
300 const Symbol_value<64>*,
301 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
fe8718a4 302 section_size_type);
2e30d253
ILT
303
304 private:
305 // Do a TLS relocation.
306 inline void
7bf1f802
ILT
307 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
308 size_t relnum, const elfcpp::Rela<64, false>&,
2e30d253
ILT
309 unsigned int r_type, const Sized_symbol<64>*,
310 const Symbol_value<64>*,
fe8718a4
ILT
311 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
312 section_size_type);
2e30d253 313
c2b45e22 314 // Do a TLS General-Dynamic to Initial-Exec transition.
7bf1f802
ILT
315 inline void
316 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
317 Output_segment* tls_segment,
318 const elfcpp::Rela<64, false>&, unsigned int r_type,
319 elfcpp::Elf_types<64>::Elf_Addr value,
320 unsigned char* view,
c2b45e22 321 elfcpp::Elf_types<64>::Elf_Addr,
fe8718a4 322 section_size_type view_size);
7bf1f802 323
56622147
ILT
324 // Do a TLS General-Dynamic to Local-Exec transition.
325 inline void
326 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
2e30d253
ILT
327 Output_segment* tls_segment,
328 const elfcpp::Rela<64, false>&, unsigned int r_type,
329 elfcpp::Elf_types<64>::Elf_Addr value,
330 unsigned char* view,
fe8718a4 331 section_size_type view_size);
2e30d253 332
c2b45e22
CC
333 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
334 inline void
335 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
336 Output_segment* tls_segment,
337 const elfcpp::Rela<64, false>&, unsigned int r_type,
338 elfcpp::Elf_types<64>::Elf_Addr value,
339 unsigned char* view,
340 elfcpp::Elf_types<64>::Elf_Addr,
341 section_size_type view_size);
342
343 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
344 inline void
345 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
346 Output_segment* tls_segment,
347 const elfcpp::Rela<64, false>&, unsigned int r_type,
348 elfcpp::Elf_types<64>::Elf_Addr value,
349 unsigned char* view,
350 section_size_type view_size);
351
56622147 352 // Do a TLS Local-Dynamic to Local-Exec transition.
2e30d253 353 inline void
56622147 354 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
2e30d253
ILT
355 Output_segment* tls_segment,
356 const elfcpp::Rela<64, false>&, unsigned int r_type,
357 elfcpp::Elf_types<64>::Elf_Addr value,
358 unsigned char* view,
fe8718a4 359 section_size_type view_size);
2e30d253 360
56622147
ILT
361 // Do a TLS Initial-Exec to Local-Exec transition.
362 static inline void
363 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
72ec2876
ILT
364 Output_segment* tls_segment,
365 const elfcpp::Rela<64, false>&, unsigned int r_type,
366 elfcpp::Elf_types<64>::Elf_Addr value,
367 unsigned char* view,
fe8718a4 368 section_size_type view_size);
2e30d253
ILT
369
370 // This is set if we should skip the next reloc, which should be a
371 // PLT32 reloc against ___tls_get_addr.
372 bool skip_call_tls_get_addr_;
497897f9
ILT
373
374 // This is set if we see a relocation which could load the address
375 // of the TLS block. Whether we see such a relocation determines
376 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
377 // in debugging sections.
378 bool saw_tls_block_reloc_;
2e30d253
ILT
379 };
380
6a74a719
ILT
381 // A class which returns the size required for a relocation type,
382 // used while scanning relocs during a relocatable link.
383 class Relocatable_size_for_reloc
384 {
385 public:
386 unsigned int
387 get_size_for_reloc(unsigned int, Relobj*);
388 };
389
2e30d253
ILT
390 // Adjust TLS relocation type based on the options and whether this
391 // is a local symbol.
e041f13d 392 static tls::Tls_optimization
2e30d253
ILT
393 optimize_tls_reloc(bool is_final, int r_type);
394
395 // Get the GOT section, creating it if necessary.
396 Output_data_got<64, false>*
397 got_section(Symbol_table*, Layout*);
398
96f2030e
ILT
399 // Get the GOT PLT section.
400 Output_data_space*
401 got_plt_section() const
402 {
403 gold_assert(this->got_plt_ != NULL);
404 return this->got_plt_;
405 }
406
a8df5856
ILT
407 // Get the GOT section for TLSDESC entries.
408 Output_data_got<64, false>*
409 got_tlsdesc_section() const
410 {
411 gold_assert(this->got_tlsdesc_ != NULL);
412 return this->got_tlsdesc_;
413 }
414
c2b45e22
CC
415 // Create the PLT section.
416 void
417 make_plt_section(Symbol_table* symtab, Layout* layout);
418
2e30d253
ILT
419 // Create a PLT entry for a global symbol.
420 void
421 make_plt_entry(Symbol_table*, Layout*, Symbol*);
422
9fa33bee 423 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
edfbb029
CC
424 void
425 define_tls_base_symbol(Symbol_table*, Layout*);
426
c2b45e22
CC
427 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
428 void
429 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
430
31d60480
ILT
431 // Create a GOT entry for the TLS module index.
432 unsigned int
433 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
434 Sized_relobj<64, false>* object);
435
2e30d253
ILT
436 // Get the PLT section.
437 Output_data_plt_x86_64*
438 plt_section() const
439 {
440 gold_assert(this->plt_ != NULL);
441 return this->plt_;
442 }
443
444 // Get the dynamic reloc section, creating it if necessary.
445 Reloc_section*
0ffd9845 446 rela_dyn_section(Layout*);
2e30d253 447
e291e7b9
ILT
448 // Get the section to use for TLSDESC relocations.
449 Reloc_section*
450 rela_tlsdesc_section(Layout*) const;
451
12c0daef 452 // Add a potential copy relocation.
2e30d253 453 void
ef9beddf
ILT
454 copy_reloc(Symbol_table* symtab, Layout* layout,
455 Sized_relobj<64, false>* object,
12c0daef
ILT
456 unsigned int shndx, Output_section* output_section,
457 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
458 {
459 this->copy_relocs_.copy_reloc(symtab, layout,
460 symtab->get_sized_symbol<64>(sym),
461 object, shndx, output_section,
462 reloc, this->rela_dyn_section(layout));
463 }
2e30d253
ILT
464
465 // Information about this specific target which we pass to the
466 // general Target structure.
467 static const Target::Target_info x86_64_info;
468
0a65a3a7
CC
469 enum Got_type
470 {
471 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
472 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
473 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
474 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
475 };
476
e291e7b9
ILT
477 // This type is used as the argument to the target specific
478 // relocation routines. The only target specific reloc is
479 // R_X86_64_TLSDESC against a local symbol.
480 struct Tlsdesc_info
481 {
482 Tlsdesc_info(Sized_relobj<64, false>* a_object, unsigned int a_r_sym)
483 : object(a_object), r_sym(a_r_sym)
484 { }
485
486 // The object in which the local symbol is defined.
487 Sized_relobj<64, false>* object;
488 // The local symbol index in the object.
489 unsigned int r_sym;
490 };
491
2e30d253
ILT
492 // The GOT section.
493 Output_data_got<64, false>* got_;
494 // The PLT section.
495 Output_data_plt_x86_64* plt_;
496 // The GOT PLT section.
497 Output_data_space* got_plt_;
a8df5856
ILT
498 // The GOT section for TLSDESC relocations.
499 Output_data_got<64, false>* got_tlsdesc_;
e785ec03
ILT
500 // The _GLOBAL_OFFSET_TABLE_ symbol.
501 Symbol* global_offset_table_;
2e30d253 502 // The dynamic reloc section.
0ffd9845 503 Reloc_section* rela_dyn_;
2e30d253 504 // Relocs saved to avoid a COPY reloc.
12c0daef 505 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
2e30d253
ILT
506 // Space for variables copied with a COPY reloc.
507 Output_data_space* dynbss_;
c2b45e22 508 // Offset of the GOT entry for the TLS module index.
31d60480 509 unsigned int got_mod_index_offset_;
e291e7b9
ILT
510 // We handle R_X86_64_TLSDESC against a local symbol as a target
511 // specific relocation. Here we store the object and local symbol
512 // index for the relocation.
513 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
edfbb029
CC
514 // True if the _TLS_MODULE_BASE_ symbol has been defined.
515 bool tls_base_symbol_defined_;
2e30d253
ILT
516};
517
518const Target::Target_info Target_x86_64::x86_64_info =
519{
520 64, // size
521 false, // is_big_endian
522 elfcpp::EM_X86_64, // machine_code
523 false, // has_make_symbol
524 false, // has_resolve
525 true, // has_code_fill
35cdfc9a 526 true, // is_default_stack_executable
0864d551 527 '\0', // wrap_char
2e30d253 528 "/lib/ld64.so.1", // program interpreter
0c5e9c22 529 0x400000, // default_text_segment_address
cd72c291 530 0x1000, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
531 0x1000, // common_pagesize (overridable by -z common-page-size)
532 elfcpp::SHN_UNDEF, // small_common_shndx
533 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
534 0, // small_common_section_flags
05a352e6
DK
535 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
536 NULL, // attributes_section
537 NULL // attributes_vendor
2e30d253
ILT
538};
539
8a5e3e08
ILT
540// This is called when a new output section is created. This is where
541// we handle the SHF_X86_64_LARGE.
542
543void
544Target_x86_64::do_new_output_section(Output_section *os) const
545{
546 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
547 os->set_is_large_section();
548}
549
2e30d253
ILT
550// Get the GOT section, creating it if necessary.
551
552Output_data_got<64, false>*
553Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
554{
555 if (this->got_ == NULL)
556 {
557 gold_assert(symtab != NULL && layout != NULL);
558
559 this->got_ = new Output_data_got<64, false>();
560
82742395
ILT
561 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
562 (elfcpp::SHF_ALLOC
563 | elfcpp::SHF_WRITE),
22f0da72
ILT
564 this->got_, ORDER_RELRO_LAST,
565 true);
2e30d253 566
7d9e3d98 567 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
82742395
ILT
568 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
569 (elfcpp::SHF_ALLOC
570 | elfcpp::SHF_WRITE),
22f0da72
ILT
571 this->got_plt_, ORDER_NON_RELRO_FIRST,
572 false);
2e30d253
ILT
573
574 // The first three entries are reserved.
27bc2bce 575 this->got_plt_->set_current_data_size(3 * 8);
2e30d253 576
1a2dff53
ILT
577 // Those bytes can go into the relro segment.
578 layout->increase_relro(3 * 8);
579
2e30d253 580 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
e785ec03
ILT
581 this->global_offset_table_ =
582 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
583 Symbol_table::PREDEFINED,
584 this->got_plt_,
585 0, 0, elfcpp::STT_OBJECT,
586 elfcpp::STB_LOCAL,
587 elfcpp::STV_HIDDEN, 0,
588 false, false);
a8df5856
ILT
589
590 // If there are any TLSDESC relocations, they get GOT entries in
591 // .got.plt after the jump slot entries.
592 this->got_tlsdesc_ = new Output_data_got<64, false>();
593 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
594 (elfcpp::SHF_ALLOC
595 | elfcpp::SHF_WRITE),
22f0da72
ILT
596 this->got_tlsdesc_,
597 ORDER_NON_RELRO_FIRST, false);
2e30d253
ILT
598 }
599
600 return this->got_;
601}
602
603// Get the dynamic reloc section, creating it if necessary.
604
605Target_x86_64::Reloc_section*
0ffd9845 606Target_x86_64::rela_dyn_section(Layout* layout)
2e30d253 607{
0ffd9845 608 if (this->rela_dyn_ == NULL)
2e30d253
ILT
609 {
610 gold_assert(layout != NULL);
d98bc257 611 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2e30d253 612 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
613 elfcpp::SHF_ALLOC, this->rela_dyn_,
614 ORDER_DYNAMIC_RELOCS, false);
2e30d253 615 }
0ffd9845 616 return this->rela_dyn_;
2e30d253
ILT
617}
618
619// A class to handle the PLT data.
620
621class Output_data_plt_x86_64 : public Output_section_data
622{
623 public:
624 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
625
c2b45e22
CC
626 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
627 Output_data_space*);
2e30d253
ILT
628
629 // Add an entry to the PLT.
630 void
631 add_entry(Symbol* gsym);
632
c2b45e22
CC
633 // Add the reserved TLSDESC_PLT entry to the PLT.
634 void
635 reserve_tlsdesc_entry(unsigned int got_offset)
636 { this->tlsdesc_got_offset_ = got_offset; }
637
638 // Return true if a TLSDESC_PLT entry has been reserved.
639 bool
640 has_tlsdesc_entry() const
641 { return this->tlsdesc_got_offset_ != -1U; }
642
643 // Return the GOT offset for the reserved TLSDESC_PLT entry.
644 unsigned int
645 get_tlsdesc_got_offset() const
646 { return this->tlsdesc_got_offset_; }
647
648 // Return the offset of the reserved TLSDESC_PLT entry.
649 unsigned int
650 get_tlsdesc_plt_offset() const
651 { return (this->count_ + 1) * plt_entry_size; }
652
e291e7b9 653 // Return the .rela.plt section data.
2e30d253 654 const Reloc_section*
e291e7b9 655 rela_plt() const
2e30d253
ILT
656 { return this->rel_; }
657
e291e7b9
ILT
658 // Return where the TLSDESC relocations should go.
659 Reloc_section*
660 rela_tlsdesc(Layout*);
661
2e30d253
ILT
662 protected:
663 void
664 do_adjust_output_section(Output_section* os);
665
7d9e3d98
ILT
666 // Write to a map file.
667 void
668 do_print_to_mapfile(Mapfile* mapfile) const
669 { mapfile->print_output_data(this, _("** PLT")); }
670
2e30d253
ILT
671 private:
672 // The size of an entry in the PLT.
673 static const int plt_entry_size = 16;
674
675 // The first entry in the PLT.
676 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
677 // procedure linkage table for both programs and shared objects."
678 static unsigned char first_plt_entry[plt_entry_size];
679
680 // Other entries in the PLT for an executable.
681 static unsigned char plt_entry[plt_entry_size];
682
c2b45e22
CC
683 // The reserved TLSDESC entry in the PLT for an executable.
684 static unsigned char tlsdesc_plt_entry[plt_entry_size];
685
2e30d253
ILT
686 // Set the final size.
687 void
c2b45e22 688 set_final_data_size();
2e30d253
ILT
689
690 // Write out the PLT data.
691 void
692 do_write(Output_file*);
693
694 // The reloc section.
695 Reloc_section* rel_;
e291e7b9
ILT
696 // The TLSDESC relocs, if necessary. These must follow the regular
697 // PLT relocs.
698 Reloc_section* tlsdesc_rel_;
c2b45e22
CC
699 // The .got section.
700 Output_data_got<64, false>* got_;
2e30d253
ILT
701 // The .got.plt section.
702 Output_data_space* got_plt_;
703 // The number of PLT entries.
704 unsigned int count_;
c2b45e22
CC
705 // Offset of the reserved TLSDESC_GOT entry when needed.
706 unsigned int tlsdesc_got_offset_;
2e30d253
ILT
707};
708
709// Create the PLT section. The ordinary .got section is an argument,
710// since we need to refer to the start. We also create our own .got
711// section just for PLT entries.
712
713Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
c2b45e22 714 Output_data_got<64, false>* got,
2e30d253 715 Output_data_space* got_plt)
e291e7b9
ILT
716 : Output_section_data(8), tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
717 count_(0), tlsdesc_got_offset_(-1U)
2e30d253 718{
d98bc257 719 this->rel_ = new Reloc_section(false);
2e30d253 720 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
22f0da72
ILT
721 elfcpp::SHF_ALLOC, this->rel_,
722 ORDER_DYNAMIC_PLT_RELOCS, false);
2e30d253
ILT
723}
724
725void
726Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
727{
b0481b0b 728 os->set_entsize(plt_entry_size);
2e30d253
ILT
729}
730
731// Add an entry to the PLT.
732
733void
734Output_data_plt_x86_64::add_entry(Symbol* gsym)
735{
736 gold_assert(!gsym->has_plt_offset());
737
738 // Note that when setting the PLT offset we skip the initial
739 // reserved PLT entry.
740 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
741
742 ++this->count_;
743
fe8718a4 744 section_offset_type got_offset = this->got_plt_->current_data_size();
2e30d253
ILT
745
746 // Every PLT entry needs a GOT entry which points back to the PLT
747 // entry (this will be changed by the dynamic linker, normally
748 // lazily when the function is called).
27bc2bce 749 this->got_plt_->set_current_data_size(got_offset + 8);
2e30d253
ILT
750
751 // Every PLT entry needs a reloc.
752 gsym->set_needs_dynsym_entry();
753 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
754 got_offset, 0);
755
756 // Note that we don't need to save the symbol. The contents of the
757 // PLT are independent of which symbols are used. The symbols only
758 // appear in the relocations.
759}
760
e291e7b9
ILT
761// Return where the TLSDESC relocations should go, creating it if
762// necessary. These follow the JUMP_SLOT relocations.
763
764Output_data_plt_x86_64::Reloc_section*
765Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
766{
767 if (this->tlsdesc_rel_ == NULL)
768 {
769 this->tlsdesc_rel_ = new Reloc_section(false);
770 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
771 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
22f0da72 772 ORDER_DYNAMIC_PLT_RELOCS, false);
e291e7b9
ILT
773 gold_assert(this->tlsdesc_rel_->output_section() ==
774 this->rel_->output_section());
775 }
776 return this->tlsdesc_rel_;
777}
778
c2b45e22
CC
779// Set the final size.
780void
781Output_data_plt_x86_64::set_final_data_size()
782{
783 unsigned int count = this->count_;
784 if (this->has_tlsdesc_entry())
785 ++count;
786 this->set_data_size((count + 1) * plt_entry_size);
787}
788
2e30d253
ILT
789// The first entry in the PLT for an executable.
790
791unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
792{
793 // From AMD64 ABI Draft 0.98, page 76
794 0xff, 0x35, // pushq contents of memory address
2e30d253 795 0, 0, 0, 0, // replaced with address of .got + 8
78d911fd
ILT
796 0xff, 0x25, // jmp indirect
797 0, 0, 0, 0, // replaced with address of .got + 16
2e30d253
ILT
798 0x90, 0x90, 0x90, 0x90 // noop (x4)
799};
800
801// Subsequent entries in the PLT for an executable.
802
803unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
804{
805 // From AMD64 ABI Draft 0.98, page 76
806 0xff, 0x25, // jmpq indirect
807 0, 0, 0, 0, // replaced with address of symbol in .got
808 0x68, // pushq immediate
809 0, 0, 0, 0, // replaced with offset into relocation table
810 0xe9, // jmpq relative
811 0, 0, 0, 0 // replaced with offset to start of .plt
812};
813
c2b45e22
CC
814// The reserved TLSDESC entry in the PLT for an executable.
815
816unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
817{
818 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
819 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
820 0xff, 0x35, // pushq x(%rip)
821 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
822 0xff, 0x25, // jmpq *y(%rip)
823 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
824 0x0f, 0x1f, // nop
825 0x40, 0
826};
827
2e30d253
ILT
828// Write out the PLT. This uses the hand-coded instructions above,
829// and adjusts them as needed. This is specified by the AMD64 ABI.
830
831void
832Output_data_plt_x86_64::do_write(Output_file* of)
833{
2ea97941 834 const off_t offset = this->offset();
fe8718a4
ILT
835 const section_size_type oview_size =
836 convert_to_section_size_type(this->data_size());
2ea97941 837 unsigned char* const oview = of->get_output_view(offset, oview_size);
2e30d253
ILT
838
839 const off_t got_file_offset = this->got_plt_->offset();
fe8718a4
ILT
840 const section_size_type got_size =
841 convert_to_section_size_type(this->got_plt_->data_size());
2e30d253
ILT
842 unsigned char* const got_view = of->get_output_view(got_file_offset,
843 got_size);
844
845 unsigned char* pov = oview;
846
c2b45e22 847 // The base address of the .plt section.
a984ee1d 848 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
c2b45e22 849 // The base address of the .got section.
a984ee1d 850 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
c2b45e22
CC
851 // The base address of the PLT portion of the .got section,
852 // which is where the GOT pointer will point, and where the
853 // three reserved GOT entries are located.
a984ee1d 854 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
2e30d253
ILT
855
856 memcpy(pov, first_plt_entry, plt_entry_size);
78d911fd 857 // We do a jmp relative to the PC at the end of this instruction.
a984ee1d
ILT
858 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
859 (got_address + 8
860 - (plt_address + 6)));
861 elfcpp::Swap<32, false>::writeval(pov + 8,
862 (got_address + 16
863 - (plt_address + 12)));
2e30d253
ILT
864 pov += plt_entry_size;
865
866 unsigned char* got_pov = got_view;
867
868 memset(got_pov, 0, 24);
869 got_pov += 24;
870
871 unsigned int plt_offset = plt_entry_size;
872 unsigned int got_offset = 24;
873 const unsigned int count = this->count_;
874 for (unsigned int plt_index = 0;
875 plt_index < count;
876 ++plt_index,
877 pov += plt_entry_size,
878 got_pov += 8,
879 plt_offset += plt_entry_size,
880 got_offset += 8)
881 {
882 // Set and adjust the PLT entry itself.
883 memcpy(pov, plt_entry, plt_entry_size);
78d911fd
ILT
884 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
885 (got_address + got_offset
886 - (plt_address + plt_offset
887 + 6)));
2e30d253
ILT
888
889 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
890 elfcpp::Swap<32, false>::writeval(pov + 12,
891 - (plt_offset + plt_entry_size));
892
893 // Set the entry in the GOT.
894 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
895 }
896
c2b45e22
CC
897 if (this->has_tlsdesc_entry())
898 {
899 // Set and adjust the reserved TLSDESC PLT entry.
900 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
901 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
902 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
903 (got_address + 8
904 - (plt_address + plt_offset
905 + 6)));
906 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
907 (got_base
908 + tlsdesc_got_offset
909 - (plt_address + plt_offset
910 + 12)));
911 pov += plt_entry_size;
912 }
913
fe8718a4
ILT
914 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
915 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2e30d253 916
2ea97941 917 of->write_output_view(offset, oview_size, oview);
2e30d253
ILT
918 of->write_output_view(got_file_offset, got_size, got_view);
919}
920
c2b45e22 921// Create the PLT section.
2e30d253
ILT
922
923void
c2b45e22 924Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
2e30d253 925{
2e30d253
ILT
926 if (this->plt_ == NULL)
927 {
928 // Create the GOT sections first.
929 this->got_section(symtab, layout);
930
c2b45e22
CC
931 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
932 this->got_plt_);
2e30d253
ILT
933 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
934 (elfcpp::SHF_ALLOC
935 | elfcpp::SHF_EXECINSTR),
22f0da72 936 this->plt_, ORDER_PLT, false);
2e30d253 937 }
c2b45e22
CC
938}
939
e291e7b9
ILT
940// Return the section for TLSDESC relocations.
941
942Target_x86_64::Reloc_section*
943Target_x86_64::rela_tlsdesc_section(Layout* layout) const
944{
945 return this->plt_section()->rela_tlsdesc(layout);
946}
947
c2b45e22
CC
948// Create a PLT entry for a global symbol.
949
950void
951Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
952 Symbol* gsym)
953{
954 if (gsym->has_plt_offset())
955 return;
956
957 if (this->plt_ == NULL)
958 this->make_plt_section(symtab, layout);
2e30d253
ILT
959
960 this->plt_->add_entry(gsym);
961}
962
9fa33bee 963// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
edfbb029
CC
964
965void
966Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
967{
968 if (this->tls_base_symbol_defined_)
969 return;
970
971 Output_segment* tls_segment = layout->tls_segment();
972 if (tls_segment != NULL)
973 {
183fd0e3 974 bool is_exec = parameters->options().output_is_executable();
edfbb029 975 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
99fff23b 976 Symbol_table::PREDEFINED,
edfbb029
CC
977 tls_segment, 0, 0,
978 elfcpp::STT_TLS,
979 elfcpp::STB_LOCAL,
980 elfcpp::STV_HIDDEN, 0,
183fd0e3
AO
981 (is_exec
982 ? Symbol::SEGMENT_END
983 : Symbol::SEGMENT_START),
984 true);
edfbb029
CC
985 }
986 this->tls_base_symbol_defined_ = true;
987}
988
c2b45e22
CC
989// Create the reserved PLT and GOT entries for the TLS descriptor resolver.
990
991void
992Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
993 Layout* layout)
994{
995 if (this->plt_ == NULL)
996 this->make_plt_section(symtab, layout);
997
998 if (!this->plt_->has_tlsdesc_entry())
999 {
1000 // Allocate the TLSDESC_GOT entry.
1001 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1002 unsigned int got_offset = got->add_constant(0);
1003
1004 // Allocate the TLSDESC_PLT entry.
1005 this->plt_->reserve_tlsdesc_entry(got_offset);
1006 }
1007}
1008
31d60480
ILT
1009// Create a GOT entry for the TLS module index.
1010
1011unsigned int
1012Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1013 Sized_relobj<64, false>* object)
1014{
1015 if (this->got_mod_index_offset_ == -1U)
1016 {
1017 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1018 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1019 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1020 unsigned int got_offset = got->add_constant(0);
1021 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1022 got_offset, 0);
009a67a2 1023 got->add_constant(0);
31d60480
ILT
1024 this->got_mod_index_offset_ = got_offset;
1025 }
1026 return this->got_mod_index_offset_;
1027}
1028
2e30d253
ILT
1029// Optimize the TLS relocation type based on what we know about the
1030// symbol. IS_FINAL is true if the final address of this symbol is
1031// known at link time.
1032
e041f13d 1033tls::Tls_optimization
2e30d253
ILT
1034Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1035{
2e30d253
ILT
1036 // If we are generating a shared library, then we can't do anything
1037 // in the linker.
8851ecca 1038 if (parameters->options().shared())
e041f13d 1039 return tls::TLSOPT_NONE;
2e30d253
ILT
1040
1041 switch (r_type)
1042 {
1043 case elfcpp::R_X86_64_TLSGD:
e041f13d
ILT
1044 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1045 case elfcpp::R_X86_64_TLSDESC_CALL:
1046 // These are General-Dynamic which permits fully general TLS
2e30d253
ILT
1047 // access. Since we know that we are generating an executable,
1048 // we can convert this to Initial-Exec. If we also know that
1049 // this is a local symbol, we can further switch to Local-Exec.
1050 if (is_final)
e041f13d
ILT
1051 return tls::TLSOPT_TO_LE;
1052 return tls::TLSOPT_TO_IE;
2e30d253 1053
d61c17ea 1054 case elfcpp::R_X86_64_TLSLD:
2e30d253
ILT
1055 // This is Local-Dynamic, which refers to a local symbol in the
1056 // dynamic TLS block. Since we know that we generating an
1057 // executable, we can switch to Local-Exec.
e041f13d 1058 return tls::TLSOPT_TO_LE;
2e30d253 1059
0ffd9845 1060 case elfcpp::R_X86_64_DTPOFF32:
0ffd9845
ILT
1061 case elfcpp::R_X86_64_DTPOFF64:
1062 // Another Local-Dynamic reloc.
e041f13d 1063 return tls::TLSOPT_TO_LE;
0ffd9845 1064
d61c17ea 1065 case elfcpp::R_X86_64_GOTTPOFF:
2e30d253
ILT
1066 // These are Initial-Exec relocs which get the thread offset
1067 // from the GOT. If we know that we are linking against the
1068 // local symbol, we can switch to Local-Exec, which links the
1069 // thread offset into the instruction.
1070 if (is_final)
e041f13d
ILT
1071 return tls::TLSOPT_TO_LE;
1072 return tls::TLSOPT_NONE;
2e30d253 1073
d61c17ea 1074 case elfcpp::R_X86_64_TPOFF32:
2e30d253
ILT
1075 // When we already have Local-Exec, there is nothing further we
1076 // can do.
e041f13d 1077 return tls::TLSOPT_NONE;
2e30d253
ILT
1078
1079 default:
1080 gold_unreachable();
1081 }
2e30d253
ILT
1082}
1083
e041f13d
ILT
1084// Report an unsupported relocation against a local symbol.
1085
1086void
1087Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
1088 unsigned int r_type)
1089{
75f2446e
ILT
1090 gold_error(_("%s: unsupported reloc %u against local symbol"),
1091 object->name().c_str(), r_type);
e041f13d
ILT
1092}
1093
a036edd8
ILT
1094// We are about to emit a dynamic relocation of type R_TYPE. If the
1095// dynamic linker does not support it, issue an error. The GNU linker
1096// only issues a non-PIC error for an allocated read-only section.
1097// Here we know the section is allocated, but we don't know that it is
1098// read-only. But we check for all the relocation types which the
1099// glibc dynamic linker supports, so it seems appropriate to issue an
1100// error even if the section is not read-only.
1101
1102void
1103Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1104{
1105 switch (r_type)
1106 {
1107 // These are the relocation types supported by glibc for x86_64.
1108 case elfcpp::R_X86_64_RELATIVE:
1109 case elfcpp::R_X86_64_GLOB_DAT:
1110 case elfcpp::R_X86_64_JUMP_SLOT:
1111 case elfcpp::R_X86_64_DTPMOD64:
1112 case elfcpp::R_X86_64_DTPOFF64:
1113 case elfcpp::R_X86_64_TPOFF64:
1114 case elfcpp::R_X86_64_64:
1115 case elfcpp::R_X86_64_32:
1116 case elfcpp::R_X86_64_PC32:
1117 case elfcpp::R_X86_64_COPY:
1118 return;
1119
1120 default:
1121 // This prevents us from issuing more than one error per reloc
1122 // section. But we can still wind up issuing more than one
1123 // error per object file.
1124 if (this->issued_non_pic_error_)
1125 return;
33aea2fd 1126 gold_assert(parameters->options().output_is_position_independent());
a036edd8
ILT
1127 object->error(_("requires unsupported dynamic reloc; "
1128 "recompile with -fPIC"));
1129 this->issued_non_pic_error_ = true;
1130 return;
1131
1132 case elfcpp::R_X86_64_NONE:
1133 gold_unreachable();
1134 }
1135}
1136
2e30d253
ILT
1137// Scan a relocation for a local symbol.
1138
1139inline void
ad0f2072 1140Target_x86_64::Scan::local(Symbol_table* symtab,
d61c17ea
ILT
1141 Layout* layout,
1142 Target_x86_64* target,
1143 Sized_relobj<64, false>* object,
0ffd9845 1144 unsigned int data_shndx,
4f4c5f80 1145 Output_section* output_section,
0ffd9845 1146 const elfcpp::Rela<64, false>& reloc,
d61c17ea 1147 unsigned int r_type,
7bf1f802 1148 const elfcpp::Sym<64, false>& lsym)
2e30d253
ILT
1149{
1150 switch (r_type)
1151 {
1152 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
1153 case elfcpp::R_X86_64_GNU_VTINHERIT:
1154 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
1155 break;
1156
1157 case elfcpp::R_X86_64_64:
d61c6bd4 1158 // If building a shared library (or a position-independent
dceae3c1
ILT
1159 // executable), we need to create a dynamic relocation for this
1160 // location. The relocation applied at link time will apply the
1161 // link-time value, so we flag the location with an
1162 // R_X86_64_RELATIVE relocation so the dynamic loader can
d61c6bd4 1163 // relocate it easily.
8851ecca 1164 if (parameters->options().output_is_position_independent())
d61c6bd4 1165 {
e8c846c3 1166 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d61c6bd4 1167 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
e8c846c3
ILT
1168 rela_dyn->add_local_relative(object, r_sym,
1169 elfcpp::R_X86_64_RELATIVE,
1170 output_section, data_shndx,
1171 reloc.get_r_offset(),
1172 reloc.get_r_addend());
d61c6bd4
ILT
1173 }
1174 break;
1175
2e30d253
ILT
1176 case elfcpp::R_X86_64_32:
1177 case elfcpp::R_X86_64_32S:
1178 case elfcpp::R_X86_64_16:
1179 case elfcpp::R_X86_64_8:
96f2030e 1180 // If building a shared library (or a position-independent
dceae3c1
ILT
1181 // executable), we need to create a dynamic relocation for this
1182 // location. We can't use an R_X86_64_RELATIVE relocation
1183 // because that is always a 64-bit relocation.
8851ecca 1184 if (parameters->options().output_is_position_independent())
96f2030e 1185 {
a036edd8
ILT
1186 this->check_non_pic(object, r_type);
1187
96f2030e 1188 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
d491d34e 1189 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
dceae3c1 1190 if (lsym.get_st_type() != elfcpp::STT_SECTION)
d491d34e
ILT
1191 rela_dyn->add_local(object, r_sym, r_type, output_section,
1192 data_shndx, reloc.get_r_offset(),
1193 reloc.get_r_addend());
dceae3c1
ILT
1194 else
1195 {
1196 gold_assert(lsym.get_st_value() == 0);
d491d34e
ILT
1197 unsigned int shndx = lsym.get_st_shndx();
1198 bool is_ordinary;
1199 shndx = object->adjust_sym_shndx(r_sym, shndx,
1200 &is_ordinary);
1201 if (!is_ordinary)
1202 object->error(_("section symbol %u has bad shndx %u"),
1203 r_sym, shndx);
1204 else
1205 rela_dyn->add_local_section(object, shndx,
1206 r_type, output_section,
1207 data_shndx, reloc.get_r_offset(),
1208 reloc.get_r_addend());
dceae3c1 1209 }
96f2030e 1210 }
2e30d253
ILT
1211 break;
1212
1213 case elfcpp::R_X86_64_PC64:
1214 case elfcpp::R_X86_64_PC32:
1215 case elfcpp::R_X86_64_PC16:
1216 case elfcpp::R_X86_64_PC8:
1217 break;
1218
f389a824
ILT
1219 case elfcpp::R_X86_64_PLT32:
1220 // Since we know this is a local symbol, we can handle this as a
1221 // PC32 reloc.
1222 break;
1223
fdc2f80f 1224 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1225 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1226 case elfcpp::R_X86_64_GOTPC64:
1227 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1228 // We need a GOT section.
1229 target->got_section(symtab, layout);
ee9e9e86
ILT
1230 // For PLTOFF64, we'd normally want a PLT section, but since we
1231 // know this is a local symbol, no PLT is needed.
2e30d253
ILT
1232 break;
1233
0ffd9845
ILT
1234 case elfcpp::R_X86_64_GOT64:
1235 case elfcpp::R_X86_64_GOT32:
1236 case elfcpp::R_X86_64_GOTPCREL64:
1237 case elfcpp::R_X86_64_GOTPCREL:
ee9e9e86 1238 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
1239 {
1240 // The symbol requires a GOT entry.
1241 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1242 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7 1243 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
0ffd9845
ILT
1244 {
1245 // If we are generating a shared object, we need to add a
7bf1f802 1246 // dynamic relocation for this symbol's GOT entry.
8851ecca 1247 if (parameters->options().output_is_position_independent())
0ffd9845
ILT
1248 {
1249 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7bf1f802
ILT
1250 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1251 if (r_type != elfcpp::R_X86_64_GOT32)
0a65a3a7
CC
1252 rela_dyn->add_local_relative(
1253 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1254 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
7bf1f802 1255 else
dceae3c1 1256 {
a036edd8
ILT
1257 this->check_non_pic(object, r_type);
1258
dceae3c1 1259 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
0a65a3a7
CC
1260 rela_dyn->add_local(
1261 object, r_sym, r_type, got,
1262 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
dceae3c1 1263 }
0ffd9845
ILT
1264 }
1265 }
ee9e9e86
ILT
1266 // For GOTPLT64, we'd normally want a PLT section, but since
1267 // we know this is a local symbol, no PLT is needed.
0ffd9845
ILT
1268 }
1269 break;
1270
2e30d253
ILT
1271 case elfcpp::R_X86_64_COPY:
1272 case elfcpp::R_X86_64_GLOB_DAT:
1273 case elfcpp::R_X86_64_JUMP_SLOT:
1274 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1275 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 1276 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1277 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 1278 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1279 gold_error(_("%s: unexpected reloc %u in object file"),
1280 object->name().c_str(), r_type);
2e30d253
ILT
1281 break;
1282
d61c17ea 1283 // These are initial tls relocs, which are expected when linking
56622147
ILT
1284 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1285 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1286 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1287 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1288 case elfcpp::R_X86_64_DTPOFF32:
1289 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1290 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1291 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253 1292 {
8851ecca 1293 bool output_is_shared = parameters->options().shared();
e041f13d
ILT
1294 const tls::Tls_optimization optimized_type
1295 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
2e30d253
ILT
1296 switch (r_type)
1297 {
56622147 1298 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
1299 if (optimized_type == tls::TLSOPT_NONE)
1300 {
1301 // Create a pair of GOT entries for the module index and
1302 // dtv-relative offset.
1303 Output_data_got<64, false>* got
1304 = target->got_section(symtab, layout);
1305 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d491d34e
ILT
1306 unsigned int shndx = lsym.get_st_shndx();
1307 bool is_ordinary;
1308 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1309 if (!is_ordinary)
1310 object->error(_("local symbol %u has bad shndx %u"),
1311 r_sym, shndx);
1312 else
1313 got->add_local_pair_with_rela(object, r_sym,
1314 shndx,
1315 GOT_TYPE_TLS_PAIR,
1316 target->rela_dyn_section(layout),
1317 elfcpp::R_X86_64_DTPMOD64, 0);
7bf1f802
ILT
1318 }
1319 else if (optimized_type != tls::TLSOPT_TO_LE)
1320 unsupported_reloc_local(object, r_type);
1321 break;
1322
56622147 1323 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 1324 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
1325 if (optimized_type == tls::TLSOPT_NONE)
1326 {
1327 // Create reserved PLT and GOT entries for the resolver.
1328 target->reserve_tlsdesc_entries(symtab, layout);
1329
a8df5856
ILT
1330 // Generate a double GOT entry with an
1331 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1332 // is resolved lazily, so the GOT entry needs to be in
1333 // an area in .got.plt, not .got. Call got_section to
1334 // make sure the section has been created.
1335 target->got_section(symtab, layout);
1336 Output_data_got<64, false>* got = target->got_tlsdesc_section();
c2b45e22 1337 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
e291e7b9
ILT
1338 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1339 {
1340 unsigned int got_offset = got->add_constant(0);
1341 got->add_constant(0);
1342 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1343 got_offset);
1344 Reloc_section* rt = target->rela_tlsdesc_section(layout);
1345 // We store the arguments we need in a vector, and
1346 // use the index into the vector as the parameter
1347 // to pass to the target specific routines.
1348 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1349 void* arg = reinterpret_cast<void*>(intarg);
1350 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1351 got, got_offset, 0);
1352 }
c2b45e22
CC
1353 }
1354 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 1355 unsupported_reloc_local(object, r_type);
2e30d253
ILT
1356 break;
1357
c2b45e22
CC
1358 case elfcpp::R_X86_64_TLSDESC_CALL:
1359 break;
1360
e041f13d 1361 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
1362 if (optimized_type == tls::TLSOPT_NONE)
1363 {
1364 // Create a GOT entry for the module index.
31d60480 1365 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
1366 }
1367 else if (optimized_type != tls::TLSOPT_TO_LE)
1368 unsupported_reloc_local(object, r_type);
1369 break;
1370
0ffd9845
ILT
1371 case elfcpp::R_X86_64_DTPOFF32:
1372 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
1373 break;
1374
56622147 1375 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 1376 layout->set_has_static_tls();
7bf1f802
ILT
1377 if (optimized_type == tls::TLSOPT_NONE)
1378 {
1379 // Create a GOT entry for the tp-relative offset.
1380 Output_data_got<64, false>* got
1381 = target->got_section(symtab, layout);
1382 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7 1383 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1384 target->rela_dyn_section(layout),
1385 elfcpp::R_X86_64_TPOFF64);
1386 }
1387 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
1388 unsupported_reloc_local(object, r_type);
1389 break;
0ffd9845 1390
56622147 1391 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 1392 layout->set_has_static_tls();
7bf1f802
ILT
1393 if (output_is_shared)
1394 unsupported_reloc_local(object, r_type);
2e30d253 1395 break;
e041f13d
ILT
1396
1397 default:
1398 gold_unreachable();
2e30d253
ILT
1399 }
1400 }
1401 break;
2e30d253 1402
fdc2f80f
ILT
1403 case elfcpp::R_X86_64_SIZE32:
1404 case elfcpp::R_X86_64_SIZE64:
2e30d253 1405 default:
75f2446e
ILT
1406 gold_error(_("%s: unsupported reloc %u against local symbol"),
1407 object->name().c_str(), r_type);
2e30d253
ILT
1408 break;
1409 }
1410}
1411
1412
e041f13d
ILT
1413// Report an unsupported relocation against a global symbol.
1414
1415void
1416Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1417 unsigned int r_type,
1418 Symbol* gsym)
1419{
75f2446e 1420 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12 1421 object->name().c_str(), r_type, gsym->demangled_name().c_str());
e041f13d
ILT
1422}
1423
ce97fa81 1424// Returns true if this relocation type could be that of a function pointer.
21bb3914
ST
1425inline bool
1426Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
1427{
21bb3914
ST
1428 switch (r_type)
1429 {
1430 case elfcpp::R_X86_64_64:
1431 case elfcpp::R_X86_64_32:
1432 case elfcpp::R_X86_64_32S:
1433 case elfcpp::R_X86_64_16:
1434 case elfcpp::R_X86_64_8:
ce97fa81
ST
1435 case elfcpp::R_X86_64_GOT64:
1436 case elfcpp::R_X86_64_GOT32:
1437 case elfcpp::R_X86_64_GOTPCREL64:
1438 case elfcpp::R_X86_64_GOTPCREL:
1439 case elfcpp::R_X86_64_GOTPLT64:
21bb3914
ST
1440 {
1441 return true;
1442 }
1443 }
1444 return false;
1445}
1446
1447// For safe ICF, scan a relocation for a local symbol to check if it
1448// corresponds to a function pointer being taken. In that case mark
1449// the function whose pointer was taken as not foldable.
1450
1451inline bool
1452Target_x86_64::Scan::local_reloc_may_be_function_pointer(
1453 Symbol_table* ,
1454 Layout* ,
1455 Target_x86_64* ,
1456 Sized_relobj<64, false>* ,
1457 unsigned int ,
1458 Output_section* ,
1459 const elfcpp::Rela<64, false>& ,
1460 unsigned int r_type,
1461 const elfcpp::Sym<64, false>&)
1462{
1463 // When building a shared library, do not fold any local symbols as it is
1464 // not possible to distinguish pointer taken versus a call by looking at
1465 // the relocation types.
1466 return (parameters->options().shared()
1467 || possible_function_pointer_reloc(r_type));
1468}
1469
1470// For safe ICF, scan a relocation for a global symbol to check if it
1471// corresponds to a function pointer being taken. In that case mark
1472// the function whose pointer was taken as not foldable.
1473
1474inline bool
1475Target_x86_64::Scan::global_reloc_may_be_function_pointer(
1476 Symbol_table*,
1477 Layout* ,
1478 Target_x86_64* ,
1479 Sized_relobj<64, false>* ,
1480 unsigned int ,
1481 Output_section* ,
1482 const elfcpp::Rela<64, false>& ,
1483 unsigned int r_type,
1484 Symbol* gsym)
1485{
1486 // When building a shared library, do not fold symbols whose visibility
1487 // is hidden, internal or protected.
1488 return ((parameters->options().shared()
1489 && (gsym->visibility() == elfcpp::STV_INTERNAL
1490 || gsym->visibility() == elfcpp::STV_PROTECTED
1491 || gsym->visibility() == elfcpp::STV_HIDDEN))
1492 || possible_function_pointer_reloc(r_type));
1493}
1494
2e30d253
ILT
1495// Scan a relocation for a global symbol.
1496
1497inline void
ad0f2072 1498Target_x86_64::Scan::global(Symbol_table* symtab,
d61c17ea
ILT
1499 Layout* layout,
1500 Target_x86_64* target,
1501 Sized_relobj<64, false>* object,
1502 unsigned int data_shndx,
4f4c5f80 1503 Output_section* output_section,
d61c17ea
ILT
1504 const elfcpp::Rela<64, false>& reloc,
1505 unsigned int r_type,
1506 Symbol* gsym)
2e30d253
ILT
1507{
1508 switch (r_type)
1509 {
1510 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
1511 case elfcpp::R_X86_64_GNU_VTINHERIT:
1512 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
1513 break;
1514
1515 case elfcpp::R_X86_64_64:
2e30d253
ILT
1516 case elfcpp::R_X86_64_32:
1517 case elfcpp::R_X86_64_32S:
2e30d253 1518 case elfcpp::R_X86_64_16:
2e30d253 1519 case elfcpp::R_X86_64_8:
96f2030e 1520 {
d61c6bd4
ILT
1521 // Make a PLT entry if necessary.
1522 if (gsym->needs_plt_entry())
1523 {
1524 target->make_plt_entry(symtab, layout, gsym);
1525 // Since this is not a PC-relative relocation, we may be
1526 // taking the address of a function. In that case we need to
1527 // set the entry in the dynamic symbol table to the address of
1528 // the PLT entry.
8851ecca 1529 if (gsym->is_from_dynobj() && !parameters->options().shared())
d61c6bd4
ILT
1530 gsym->set_needs_dynsym_value();
1531 }
1532 // Make a dynamic relocation if necessary.
0700cf32 1533 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
d61c6bd4 1534 {
966d4097 1535 if (gsym->may_need_copy_reloc())
d61c6bd4 1536 {
12c0daef 1537 target->copy_reloc(symtab, layout, object,
7bf1f802 1538 data_shndx, output_section, gsym, reloc);
d61c6bd4
ILT
1539 }
1540 else if (r_type == elfcpp::R_X86_64_64
1541 && gsym->can_use_relative_reloc(false))
1542 {
1543 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
e8c846c3
ILT
1544 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1545 output_section, object,
1546 data_shndx, reloc.get_r_offset(),
1547 reloc.get_r_addend());
d61c6bd4
ILT
1548 }
1549 else
1550 {
a036edd8 1551 this->check_non_pic(object, r_type);
96f2030e 1552 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
1553 rela_dyn->add_global(gsym, r_type, output_section, object,
1554 data_shndx, reloc.get_r_offset(),
96f2030e 1555 reloc.get_r_addend());
d61c6bd4
ILT
1556 }
1557 }
1558 }
1559 break;
1560
1561 case elfcpp::R_X86_64_PC64:
1562 case elfcpp::R_X86_64_PC32:
1563 case elfcpp::R_X86_64_PC16:
1564 case elfcpp::R_X86_64_PC8:
1565 {
1566 // Make a PLT entry if necessary.
1567 if (gsym->needs_plt_entry())
1568 target->make_plt_entry(symtab, layout, gsym);
1569 // Make a dynamic relocation if necessary.
0700cf32 1570 int flags = Symbol::NON_PIC_REF;
53d7974c 1571 if (gsym->is_func())
0700cf32
ILT
1572 flags |= Symbol::FUNCTION_CALL;
1573 if (gsym->needs_dynamic_reloc(flags))
86849f1f 1574 {
966d4097 1575 if (gsym->may_need_copy_reloc())
d61c6bd4 1576 {
12c0daef 1577 target->copy_reloc(symtab, layout, object,
7bf1f802 1578 data_shndx, output_section, gsym, reloc);
d61c6bd4 1579 }
86849f1f 1580 else
d61c6bd4 1581 {
a036edd8 1582 this->check_non_pic(object, r_type);
d61c6bd4 1583 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
1584 rela_dyn->add_global(gsym, r_type, output_section, object,
1585 data_shndx, reloc.get_r_offset(),
d61c6bd4
ILT
1586 reloc.get_r_addend());
1587 }
86849f1f 1588 }
d61c6bd4 1589 }
2e30d253
ILT
1590 break;
1591
ff006520 1592 case elfcpp::R_X86_64_GOT64:
2e30d253 1593 case elfcpp::R_X86_64_GOT32:
ff006520
ILT
1594 case elfcpp::R_X86_64_GOTPCREL64:
1595 case elfcpp::R_X86_64_GOTPCREL:
1596 case elfcpp::R_X86_64_GOTPLT64:
2e30d253
ILT
1597 {
1598 // The symbol requires a GOT entry.
1599 Output_data_got<64, false>* got = target->got_section(symtab, layout);
7bf1f802 1600 if (gsym->final_value_is_known())
0a65a3a7 1601 got->add_global(gsym, GOT_TYPE_STANDARD);
7bf1f802
ILT
1602 else
1603 {
2e30d253
ILT
1604 // If this symbol is not fully resolved, we need to add a
1605 // dynamic relocation for it.
7bf1f802 1606 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8fc19601
ILT
1607 if (gsym->is_from_dynobj()
1608 || gsym->is_undefined()
1609 || gsym->is_preemptible())
0a65a3a7 1610 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
7bf1f802
ILT
1611 elfcpp::R_X86_64_GLOB_DAT);
1612 else
2e30d253 1613 {
0a65a3a7
CC
1614 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1615 rela_dyn->add_global_relative(
1616 gsym, elfcpp::R_X86_64_RELATIVE, got,
1617 gsym->got_offset(GOT_TYPE_STANDARD), 0);
2e30d253
ILT
1618 }
1619 }
ee9e9e86
ILT
1620 // For GOTPLT64, we also need a PLT entry (but only if the
1621 // symbol is not fully resolved).
1622 if (r_type == elfcpp::R_X86_64_GOTPLT64
1623 && !gsym->final_value_is_known())
1624 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1625 }
1626 break;
1627
1628 case elfcpp::R_X86_64_PLT32:
1629 // If the symbol is fully resolved, this is just a PC32 reloc.
1630 // Otherwise we need a PLT entry.
1631 if (gsym->final_value_is_known())
1632 break;
96f2030e
ILT
1633 // If building a shared library, we can also skip the PLT entry
1634 // if the symbol is defined in the output file and is protected
1635 // or hidden.
1636 if (gsym->is_defined()
1637 && !gsym->is_from_dynobj()
1638 && !gsym->is_preemptible())
1639 break;
2e30d253
ILT
1640 target->make_plt_entry(symtab, layout, gsym);
1641 break;
1642
fdc2f80f 1643 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1644 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1645 case elfcpp::R_X86_64_GOTPC64:
1646 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1647 // We need a GOT section.
1648 target->got_section(symtab, layout);
ee9e9e86
ILT
1649 // For PLTOFF64, we also need a PLT entry (but only if the
1650 // symbol is not fully resolved).
1651 if (r_type == elfcpp::R_X86_64_PLTOFF64
1652 && !gsym->final_value_is_known())
1653 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1654 break;
1655
2e30d253
ILT
1656 case elfcpp::R_X86_64_COPY:
1657 case elfcpp::R_X86_64_GLOB_DAT:
1658 case elfcpp::R_X86_64_JUMP_SLOT:
1659 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1660 // These are outstanding tls relocs, which are unexpected when linking
e822f2b1 1661 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1662 case elfcpp::R_X86_64_DTPMOD64:
e822f2b1 1663 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1664 gold_error(_("%s: unexpected reloc %u in object file"),
1665 object->name().c_str(), r_type);
2e30d253 1666 break;
2e30d253 1667
d61c17ea 1668 // These are initial tls relocs, which are expected for global()
56622147
ILT
1669 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1670 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1671 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1672 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1673 case elfcpp::R_X86_64_DTPOFF32:
1674 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1675 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1676 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253
ILT
1677 {
1678 const bool is_final = gsym->final_value_is_known();
e041f13d
ILT
1679 const tls::Tls_optimization optimized_type
1680 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
1681 switch (r_type)
1682 {
56622147 1683 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
1684 if (optimized_type == tls::TLSOPT_NONE)
1685 {
1686 // Create a pair of GOT entries for the module index and
1687 // dtv-relative offset.
1688 Output_data_got<64, false>* got
1689 = target->got_section(symtab, layout);
0a65a3a7
CC
1690 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1691 target->rela_dyn_section(layout),
1692 elfcpp::R_X86_64_DTPMOD64,
1693 elfcpp::R_X86_64_DTPOFF64);
7bf1f802
ILT
1694 }
1695 else if (optimized_type == tls::TLSOPT_TO_IE)
1696 {
1697 // Create a GOT entry for the tp-relative offset.
1698 Output_data_got<64, false>* got
1699 = target->got_section(symtab, layout);
0a65a3a7 1700 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1701 target->rela_dyn_section(layout),
1702 elfcpp::R_X86_64_TPOFF64);
1703 }
1704 else if (optimized_type != tls::TLSOPT_TO_LE)
1705 unsupported_reloc_global(object, r_type, gsym);
1706 break;
1707
56622147 1708 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 1709 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
1710 if (optimized_type == tls::TLSOPT_NONE)
1711 {
1712 // Create reserved PLT and GOT entries for the resolver.
1713 target->reserve_tlsdesc_entries(symtab, layout);
1714
a8df5856
ILT
1715 // Create a double GOT entry with an R_X86_64_TLSDESC
1716 // reloc. The R_X86_64_TLSDESC reloc is resolved
1717 // lazily, so the GOT entry needs to be in an area in
1718 // .got.plt, not .got. Call got_section to make sure
1719 // the section has been created.
1720 target->got_section(symtab, layout);
1721 Output_data_got<64, false>* got = target->got_tlsdesc_section();
e291e7b9
ILT
1722 Reloc_section *rt = target->rela_tlsdesc_section(layout);
1723 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
c2b45e22
CC
1724 elfcpp::R_X86_64_TLSDESC, 0);
1725 }
1726 else if (optimized_type == tls::TLSOPT_TO_IE)
1727 {
1728 // Create a GOT entry for the tp-relative offset.
1729 Output_data_got<64, false>* got
1730 = target->got_section(symtab, layout);
1731 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1732 target->rela_dyn_section(layout),
1733 elfcpp::R_X86_64_TPOFF64);
1734 }
1735 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 1736 unsupported_reloc_global(object, r_type, gsym);
2e30d253
ILT
1737 break;
1738
c2b45e22
CC
1739 case elfcpp::R_X86_64_TLSDESC_CALL:
1740 break;
1741
e041f13d 1742 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
1743 if (optimized_type == tls::TLSOPT_NONE)
1744 {
1745 // Create a GOT entry for the module index.
31d60480 1746 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
1747 }
1748 else if (optimized_type != tls::TLSOPT_TO_LE)
1749 unsupported_reloc_global(object, r_type, gsym);
1750 break;
1751
0ffd9845
ILT
1752 case elfcpp::R_X86_64_DTPOFF32:
1753 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
1754 break;
1755
56622147 1756 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 1757 layout->set_has_static_tls();
7bf1f802
ILT
1758 if (optimized_type == tls::TLSOPT_NONE)
1759 {
1760 // Create a GOT entry for the tp-relative offset.
1761 Output_data_got<64, false>* got
1762 = target->got_section(symtab, layout);
0a65a3a7 1763 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1764 target->rela_dyn_section(layout),
1765 elfcpp::R_X86_64_TPOFF64);
1766 }
1767 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
1768 unsupported_reloc_global(object, r_type, gsym);
1769 break;
0ffd9845 1770
56622147 1771 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 1772 layout->set_has_static_tls();
8851ecca 1773 if (parameters->options().shared())
7bf1f802 1774 unsupported_reloc_local(object, r_type);
2e30d253 1775 break;
e041f13d
ILT
1776
1777 default:
1778 gold_unreachable();
2e30d253
ILT
1779 }
1780 }
1781 break;
fdc2f80f
ILT
1782
1783 case elfcpp::R_X86_64_SIZE32:
1784 case elfcpp::R_X86_64_SIZE64:
2e30d253 1785 default:
75f2446e 1786 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12
ILT
1787 object->name().c_str(), r_type,
1788 gsym->demangled_name().c_str());
2e30d253
ILT
1789 break;
1790 }
1791}
1792
6d03d481 1793void
ad0f2072 1794Target_x86_64::gc_process_relocs(Symbol_table* symtab,
6d03d481
ST
1795 Layout* layout,
1796 Sized_relobj<64, false>* object,
1797 unsigned int data_shndx,
1798 unsigned int sh_type,
1799 const unsigned char* prelocs,
1800 size_t reloc_count,
1801 Output_section* output_section,
1802 bool needs_special_offset_handling,
1803 size_t local_symbol_count,
1804 const unsigned char* plocal_symbols)
1805{
1806
1807 if (sh_type == elfcpp::SHT_REL)
1808 {
1809 return;
1810 }
1811
1812 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
41cbeecc
ST
1813 Target_x86_64::Scan,
1814 Target_x86_64::Relocatable_size_for_reloc>(
6d03d481
ST
1815 symtab,
1816 layout,
1817 this,
1818 object,
1819 data_shndx,
1820 prelocs,
1821 reloc_count,
1822 output_section,
1823 needs_special_offset_handling,
1824 local_symbol_count,
1825 plocal_symbols);
1826
1827}
2e30d253
ILT
1828// Scan relocations for a section.
1829
1830void
ad0f2072 1831Target_x86_64::scan_relocs(Symbol_table* symtab,
d61c17ea
ILT
1832 Layout* layout,
1833 Sized_relobj<64, false>* object,
1834 unsigned int data_shndx,
1835 unsigned int sh_type,
1836 const unsigned char* prelocs,
1837 size_t reloc_count,
730cdc88
ILT
1838 Output_section* output_section,
1839 bool needs_special_offset_handling,
d61c17ea 1840 size_t local_symbol_count,
730cdc88 1841 const unsigned char* plocal_symbols)
2e30d253
ILT
1842{
1843 if (sh_type == elfcpp::SHT_REL)
1844 {
75f2446e
ILT
1845 gold_error(_("%s: unsupported REL reloc section"),
1846 object->name().c_str());
1847 return;
2e30d253
ILT
1848 }
1849
1850 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1851 Target_x86_64::Scan>(
2e30d253
ILT
1852 symtab,
1853 layout,
1854 this,
1855 object,
1856 data_shndx,
1857 prelocs,
1858 reloc_count,
730cdc88
ILT
1859 output_section,
1860 needs_special_offset_handling,
2e30d253 1861 local_symbol_count,
730cdc88 1862 plocal_symbols);
2e30d253
ILT
1863}
1864
1865// Finalize the sections.
1866
1867void
f59f41f3
DK
1868Target_x86_64::do_finalize_sections(
1869 Layout* layout,
1870 const Input_objects*,
e785ec03 1871 Symbol_table* symtab)
2e30d253 1872{
ea715a34
ILT
1873 const Reloc_section* rel_plt = (this->plt_ == NULL
1874 ? NULL
e291e7b9 1875 : this->plt_->rela_plt());
ea715a34 1876 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
612a8d3d 1877 this->rela_dyn_, true, false);
ea715a34 1878
2e30d253
ILT
1879 // Fill in some more dynamic tags.
1880 Output_data_dynamic* const odyn = layout->dynamic_data();
1881 if (odyn != NULL)
1882 {
22b127cc 1883 if (this->plt_ != NULL
ea715a34
ILT
1884 && this->plt_->output_section() != NULL
1885 && this->plt_->has_tlsdesc_entry())
2e30d253 1886 {
ea715a34
ILT
1887 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1888 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1889 this->got_->finalize_data_size();
1890 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1891 this->plt_, plt_offset);
1892 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1893 this->got_, got_offset);
2e30d253
ILT
1894 }
1895 }
1896
1897 // Emit any relocs we saved in an attempt to avoid generating COPY
1898 // relocs.
12c0daef
ILT
1899 if (this->copy_relocs_.any_saved_relocs())
1900 this->copy_relocs_.emit(this->rela_dyn_section(layout));
e785ec03
ILT
1901
1902 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1903 // the .got.plt section.
1904 Symbol* sym = this->global_offset_table_;
1905 if (sym != NULL)
1906 {
1907 uint64_t data_size = this->got_plt_->current_data_size();
1908 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
1909 }
2e30d253
ILT
1910}
1911
1912// Perform a relocation.
1913
1914inline bool
1915Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1916 Target_x86_64* target,
031cdbed 1917 Output_section*,
2e30d253 1918 size_t relnum,
0ffd9845 1919 const elfcpp::Rela<64, false>& rela,
2e30d253
ILT
1920 unsigned int r_type,
1921 const Sized_symbol<64>* gsym,
1922 const Symbol_value<64>* psymval,
1923 unsigned char* view,
1924 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 1925 section_size_type view_size)
2e30d253
ILT
1926{
1927 if (this->skip_call_tls_get_addr_)
1928 {
5efc7cd2
CC
1929 if ((r_type != elfcpp::R_X86_64_PLT32
1930 && r_type != elfcpp::R_X86_64_PC32)
2e30d253 1931 || gsym == NULL
0ffd9845 1932 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2e30d253 1933 {
75f2446e
ILT
1934 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1935 _("missing expected TLS relocation"));
1936 }
1937 else
1938 {
1939 this->skip_call_tls_get_addr_ = false;
1940 return false;
2e30d253 1941 }
2e30d253
ILT
1942 }
1943
1944 // Pick the value to use for symbols defined in shared objects.
1945 Symbol_value<64> symval;
96f2030e 1946 if (gsym != NULL
de4c45bd
ILT
1947 && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
1948 || r_type == elfcpp::R_X86_64_PC32
1949 || r_type == elfcpp::R_X86_64_PC16
1950 || r_type == elfcpp::R_X86_64_PC8))
2e30d253
ILT
1951 {
1952 symval.set_output_value(target->plt_section()->address()
1953 + gsym->plt_offset());
1954 psymval = &symval;
1955 }
1956
1957 const Sized_relobj<64, false>* object = relinfo->object;
0ffd9845
ILT
1958 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1959
1960 // Get the GOT offset if needed.
96f2030e
ILT
1961 // The GOT pointer points to the end of the GOT section.
1962 // We need to subtract the size of the GOT section to get
1963 // the actual offset to use in the relocation.
0ffd9845
ILT
1964 bool have_got_offset = false;
1965 unsigned int got_offset = 0;
1966 switch (r_type)
1967 {
1968 case elfcpp::R_X86_64_GOT32:
1969 case elfcpp::R_X86_64_GOT64:
1970 case elfcpp::R_X86_64_GOTPLT64:
1971 case elfcpp::R_X86_64_GOTPCREL:
1972 case elfcpp::R_X86_64_GOTPCREL64:
1973 if (gsym != NULL)
1974 {
0a65a3a7
CC
1975 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1976 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
0ffd9845
ILT
1977 }
1978 else
1979 {
1980 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
1981 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1982 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1983 - target->got_size());
0ffd9845
ILT
1984 }
1985 have_got_offset = true;
1986 break;
1987
1988 default:
1989 break;
1990 }
2e30d253
ILT
1991
1992 switch (r_type)
1993 {
1994 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
1995 case elfcpp::R_X86_64_GNU_VTINHERIT:
1996 case elfcpp::R_X86_64_GNU_VTENTRY:
2e30d253
ILT
1997 break;
1998
1999 case elfcpp::R_X86_64_64:
2000 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2001 break;
2002
2003 case elfcpp::R_X86_64_PC64:
2004 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2005 address);
2006 break;
2007
2008 case elfcpp::R_X86_64_32:
7bb3655e
ILT
2009 // FIXME: we need to verify that value + addend fits into 32 bits:
2010 // uint64_t x = value + addend;
2011 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2012 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2e30d253
ILT
2013 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2014 break;
2015
2016 case elfcpp::R_X86_64_32S:
7bb3655e
ILT
2017 // FIXME: we need to verify that value + addend fits into 32 bits:
2018 // int64_t x = value + addend; // note this quantity is signed!
2019 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2e30d253
ILT
2020 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2021 break;
2022
2023 case elfcpp::R_X86_64_PC32:
2024 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2025 address);
2026 break;
2027
2028 case elfcpp::R_X86_64_16:
2029 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2030 break;
2031
2032 case elfcpp::R_X86_64_PC16:
2033 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2034 address);
2035 break;
2036
2037 case elfcpp::R_X86_64_8:
2038 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2039 break;
2040
2041 case elfcpp::R_X86_64_PC8:
2042 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2043 address);
2044 break;
2045
2046 case elfcpp::R_X86_64_PLT32:
f389a824
ILT
2047 gold_assert(gsym == NULL
2048 || gsym->has_plt_offset()
99f8faca
ILT
2049 || gsym->final_value_is_known()
2050 || (gsym->is_defined()
2051 && !gsym->is_from_dynobj()
2052 && !gsym->is_preemptible()));
ee9e9e86
ILT
2053 // Note: while this code looks the same as for R_X86_64_PC32, it
2054 // behaves differently because psymval was set to point to
2055 // the PLT entry, rather than the symbol, in Scan::global().
2e30d253
ILT
2056 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2057 address);
2058 break;
2059
ee9e9e86
ILT
2060 case elfcpp::R_X86_64_PLTOFF64:
2061 {
2062 gold_assert(gsym);
2063 gold_assert(gsym->has_plt_offset()
2064 || gsym->final_value_is_known());
2065 elfcpp::Elf_types<64>::Elf_Addr got_address;
2066 got_address = target->got_section(NULL, NULL)->address();
c1866bd5
ILT
2067 Relocate_functions<64, false>::rela64(view, object, psymval,
2068 addend - got_address);
ee9e9e86
ILT
2069 }
2070
2e30d253 2071 case elfcpp::R_X86_64_GOT32:
0ffd9845
ILT
2072 gold_assert(have_got_offset);
2073 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2e30d253
ILT
2074 break;
2075
e822f2b1
ILT
2076 case elfcpp::R_X86_64_GOTPC32:
2077 {
2078 gold_assert(gsym);
2079 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2080 value = target->got_plt_section()->address();
e822f2b1
ILT
2081 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2082 }
2083 break;
2084
2085 case elfcpp::R_X86_64_GOT64:
2086 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2087 // Since we always add a PLT entry, this is equivalent.
fdc2f80f 2088 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
2089 gold_assert(have_got_offset);
2090 Relocate_functions<64, false>::rela64(view, got_offset, addend);
e822f2b1
ILT
2091 break;
2092
2093 case elfcpp::R_X86_64_GOTPC64:
2094 {
2095 gold_assert(gsym);
2096 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2097 value = target->got_plt_section()->address();
e822f2b1
ILT
2098 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2099 }
2100 break;
2101
2e30d253
ILT
2102 case elfcpp::R_X86_64_GOTOFF64:
2103 {
2104 elfcpp::Elf_types<64>::Elf_Addr value;
2105 value = (psymval->value(object, 0)
96f2030e 2106 - target->got_plt_section()->address());
2e30d253
ILT
2107 Relocate_functions<64, false>::rela64(view, value, addend);
2108 }
2109 break;
2110
2111 case elfcpp::R_X86_64_GOTPCREL:
2112 {
0ffd9845
ILT
2113 gold_assert(have_got_offset);
2114 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2115 value = target->got_plt_section()->address() + got_offset;
0ffd9845 2116 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2e30d253
ILT
2117 }
2118 break;
2119
e822f2b1
ILT
2120 case elfcpp::R_X86_64_GOTPCREL64:
2121 {
0ffd9845
ILT
2122 gold_assert(have_got_offset);
2123 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 2124 value = target->got_plt_section()->address() + got_offset;
0ffd9845 2125 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
e822f2b1
ILT
2126 }
2127 break;
2128
2e30d253
ILT
2129 case elfcpp::R_X86_64_COPY:
2130 case elfcpp::R_X86_64_GLOB_DAT:
2131 case elfcpp::R_X86_64_JUMP_SLOT:
2132 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 2133 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 2134 case elfcpp::R_X86_64_TPOFF64:
2e30d253 2135 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 2136 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
2137 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2138 _("unexpected reloc %u in object file"),
2139 r_type);
2e30d253
ILT
2140 break;
2141
d61c17ea 2142 // These are initial tls relocs, which are expected when linking
56622147
ILT
2143 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2144 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 2145 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 2146 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
2147 case elfcpp::R_X86_64_DTPOFF32:
2148 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
2149 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2150 case elfcpp::R_X86_64_TPOFF32: // Local-exec
7bf1f802
ILT
2151 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2152 view, address, view_size);
2e30d253 2153 break;
2e30d253 2154
fdc2f80f
ILT
2155 case elfcpp::R_X86_64_SIZE32:
2156 case elfcpp::R_X86_64_SIZE64:
2e30d253 2157 default:
75f2446e
ILT
2158 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2159 _("unsupported reloc %u"),
2160 r_type);
2e30d253
ILT
2161 break;
2162 }
2163
2164 return true;
2165}
2166
2167// Perform a TLS relocation.
2168
2169inline void
d61c17ea 2170Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
7bf1f802 2171 Target_x86_64* target,
d61c17ea 2172 size_t relnum,
72ec2876 2173 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
2174 unsigned int r_type,
2175 const Sized_symbol<64>* gsym,
2176 const Symbol_value<64>* psymval,
2177 unsigned char* view,
6a41d30b 2178 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2179 section_size_type view_size)
2e30d253 2180{
2e30d253 2181 Output_segment* tls_segment = relinfo->layout->tls_segment();
7bf1f802
ILT
2182
2183 const Sized_relobj<64, false>* object = relinfo->object;
6a41d30b 2184 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2e30d253
ILT
2185
2186 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2187
2188 const bool is_final = (gsym == NULL
b3705d2a 2189 ? !parameters->options().shared()
2e30d253 2190 : gsym->final_value_is_known());
e041f13d
ILT
2191 const tls::Tls_optimization optimized_type
2192 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
2193 switch (r_type)
2194 {
56622147 2195 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
497897f9 2196 this->saw_tls_block_reloc_ = true;
e041f13d 2197 if (optimized_type == tls::TLSOPT_TO_LE)
2e30d253 2198 {
7bf1f802 2199 gold_assert(tls_segment != NULL);
2e30d253 2200 this->tls_gd_to_le(relinfo, relnum, tls_segment,
72ec2876 2201 rela, r_type, value, view,
2e30d253
ILT
2202 view_size);
2203 break;
2204 }
7bf1f802
ILT
2205 else
2206 {
c2b45e22
CC
2207 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2208 ? GOT_TYPE_TLS_OFFSET
2209 : GOT_TYPE_TLS_PAIR);
7bf1f802
ILT
2210 unsigned int got_offset;
2211 if (gsym != NULL)
2212 {
c2b45e22
CC
2213 gold_assert(gsym->has_got_offset(got_type));
2214 got_offset = gsym->got_offset(got_type) - target->got_size();
7bf1f802
ILT
2215 }
2216 else
2217 {
2218 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
c2b45e22
CC
2219 gold_assert(object->local_has_got_offset(r_sym, got_type));
2220 got_offset = (object->local_got_offset(r_sym, got_type)
7bf1f802
ILT
2221 - target->got_size());
2222 }
2223 if (optimized_type == tls::TLSOPT_TO_IE)
2224 {
2225 gold_assert(tls_segment != NULL);
c2b45e22 2226 value = target->got_plt_section()->address() + got_offset;
7bf1f802 2227 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
c2b45e22 2228 value, view, address, view_size);
7bf1f802
ILT
2229 break;
2230 }
2231 else if (optimized_type == tls::TLSOPT_NONE)
2232 {
2233 // Relocate the field with the offset of the pair of GOT
2234 // entries.
6a41d30b
ILT
2235 value = target->got_plt_section()->address() + got_offset;
2236 Relocate_functions<64, false>::pcrela32(view, value, addend,
2237 address);
7bf1f802
ILT
2238 break;
2239 }
2240 }
72ec2876 2241 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 2242 _("unsupported reloc %u"), r_type);
2e30d253
ILT
2243 break;
2244
c2b45e22
CC
2245 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2246 case elfcpp::R_X86_64_TLSDESC_CALL:
497897f9 2247 this->saw_tls_block_reloc_ = true;
c2b45e22
CC
2248 if (optimized_type == tls::TLSOPT_TO_LE)
2249 {
2250 gold_assert(tls_segment != NULL);
2251 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2252 rela, r_type, value, view,
2253 view_size);
2254 break;
2255 }
2256 else
2257 {
2258 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2259 ? GOT_TYPE_TLS_OFFSET
2260 : GOT_TYPE_TLS_DESC);
a8df5856
ILT
2261 unsigned int got_offset = 0;
2262 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2263 && optimized_type == tls::TLSOPT_NONE)
2264 {
2265 // We created GOT entries in the .got.tlsdesc portion of
2266 // the .got.plt section, but the offset stored in the
2267 // symbol is the offset within .got.tlsdesc.
2268 got_offset = (target->got_size()
2269 + target->got_plt_section()->data_size());
2270 }
c2b45e22
CC
2271 if (gsym != NULL)
2272 {
2273 gold_assert(gsym->has_got_offset(got_type));
a8df5856 2274 got_offset += gsym->got_offset(got_type) - target->got_size();
c2b45e22
CC
2275 }
2276 else
2277 {
2278 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2279 gold_assert(object->local_has_got_offset(r_sym, got_type));
a8df5856
ILT
2280 got_offset += (object->local_got_offset(r_sym, got_type)
2281 - target->got_size());
c2b45e22
CC
2282 }
2283 if (optimized_type == tls::TLSOPT_TO_IE)
2284 {
2285 gold_assert(tls_segment != NULL);
2286 value = target->got_plt_section()->address() + got_offset;
2287 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2288 rela, r_type, value, view, address,
2289 view_size);
2290 break;
2291 }
2292 else if (optimized_type == tls::TLSOPT_NONE)
2293 {
2294 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2295 {
2296 // Relocate the field with the offset of the pair of GOT
2297 // entries.
2298 value = target->got_plt_section()->address() + got_offset;
2299 Relocate_functions<64, false>::pcrela32(view, value, addend,
2300 address);
2301 }
2302 break;
2303 }
2304 }
2305 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2306 _("unsupported reloc %u"), r_type);
2307 break;
2308
56622147 2309 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
497897f9 2310 this->saw_tls_block_reloc_ = true;
e041f13d
ILT
2311 if (optimized_type == tls::TLSOPT_TO_LE)
2312 {
7bf1f802 2313 gold_assert(tls_segment != NULL);
72ec2876
ILT
2314 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2315 value, view, view_size);
2316 break;
e041f13d 2317 }
7bf1f802
ILT
2318 else if (optimized_type == tls::TLSOPT_NONE)
2319 {
2320 // Relocate the field with the offset of the GOT entry for
2321 // the module index.
2322 unsigned int got_offset;
31d60480
ILT
2323 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2324 - target->got_size());
6a41d30b
ILT
2325 value = target->got_plt_section()->address() + got_offset;
2326 Relocate_functions<64, false>::pcrela32(view, value, addend,
2327 address);
7bf1f802
ILT
2328 break;
2329 }
72ec2876 2330 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 2331 _("unsupported reloc %u"), r_type);
2e30d253 2332 break;
0ffd9845
ILT
2333
2334 case elfcpp::R_X86_64_DTPOFF32:
e041f13d 2335 if (optimized_type == tls::TLSOPT_TO_LE)
497897f9
ILT
2336 {
2337 // This relocation type is used in debugging information.
2338 // In that case we need to not optimize the value. If we
2339 // haven't seen a TLSLD reloc, then we assume we should not
2340 // optimize this reloc.
2341 if (this->saw_tls_block_reloc_)
27721062
ST
2342 {
2343 gold_assert(tls_segment != NULL);
2344 value -= tls_segment->memsz();
2345 }
497897f9 2346 }
d85c80a3 2347 Relocate_functions<64, false>::rela32(view, value, addend);
0ffd9845
ILT
2348 break;
2349
2350 case elfcpp::R_X86_64_DTPOFF64:
e041f13d 2351 if (optimized_type == tls::TLSOPT_TO_LE)
497897f9
ILT
2352 {
2353 // See R_X86_64_DTPOFF32, just above, for why we test this.
2354 if (this->saw_tls_block_reloc_)
27721062
ST
2355 {
2356 gold_assert(tls_segment != NULL);
2357 value -= tls_segment->memsz();
2358 }
497897f9 2359 }
d85c80a3 2360 Relocate_functions<64, false>::rela64(view, value, addend);
0ffd9845 2361 break;
2e30d253 2362
56622147
ILT
2363 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2364 if (optimized_type == tls::TLSOPT_TO_LE)
2365 {
7bf1f802 2366 gold_assert(tls_segment != NULL);
56622147
ILT
2367 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2368 rela, r_type, value, view,
2369 view_size);
2370 break;
2371 }
7bf1f802
ILT
2372 else if (optimized_type == tls::TLSOPT_NONE)
2373 {
2374 // Relocate the field with the offset of the GOT entry for
2375 // the tp-relative offset of the symbol.
2376 unsigned int got_offset;
2377 if (gsym != NULL)
2378 {
0a65a3a7
CC
2379 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2380 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2381 - target->got_size());
7bf1f802
ILT
2382 }
2383 else
2384 {
2385 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
2386 gold_assert(object->local_has_got_offset(r_sym,
2387 GOT_TYPE_TLS_OFFSET));
2388 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
7bf1f802
ILT
2389 - target->got_size());
2390 }
6a41d30b
ILT
2391 value = target->got_plt_section()->address() + got_offset;
2392 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
7bf1f802
ILT
2393 break;
2394 }
56622147
ILT
2395 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2396 _("unsupported reloc type %u"),
2397 r_type);
2398 break;
0ffd9845 2399
56622147 2400 case elfcpp::R_X86_64_TPOFF32: // Local-exec
6a41d30b 2401 value -= tls_segment->memsz();
d85c80a3 2402 Relocate_functions<64, false>::rela32(view, value, addend);
56622147 2403 break;
2e30d253 2404 }
2e30d253
ILT
2405}
2406
7bf1f802
ILT
2407// Do a relocation in which we convert a TLS General-Dynamic to an
2408// Initial-Exec.
2409
2410inline void
2411Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2412 size_t relnum,
c2b45e22 2413 Output_segment*,
7bf1f802
ILT
2414 const elfcpp::Rela<64, false>& rela,
2415 unsigned int,
2416 elfcpp::Elf_types<64>::Elf_Addr value,
2417 unsigned char* view,
c2b45e22 2418 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2419 section_size_type view_size)
7bf1f802
ILT
2420{
2421 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2422 // .word 0x6666; rex64; call __tls_get_addr
2423 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2424
2425 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2426 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2427
2428 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2429 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2430 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2431 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2432
2433 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2434
c2b45e22
CC
2435 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2436 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
7bf1f802
ILT
2437
2438 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2439 // We can skip it.
2440 this->skip_call_tls_get_addr_ = true;
2441}
2442
e041f13d 2443// Do a relocation in which we convert a TLS General-Dynamic to a
2e30d253
ILT
2444// Local-Exec.
2445
2446inline void
d61c17ea
ILT
2447Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2448 size_t relnum,
2449 Output_segment* tls_segment,
72ec2876 2450 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
2451 unsigned int,
2452 elfcpp::Elf_types<64>::Elf_Addr value,
2453 unsigned char* view,
fe8718a4 2454 section_size_type view_size)
2e30d253 2455{
0ffd9845
ILT
2456 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2457 // .word 0x6666; rex64; call __tls_get_addr
2458 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2e30d253 2459
72ec2876
ILT
2460 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2461 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2e30d253 2462
72ec2876
ILT
2463 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2464 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2465 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2466 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2e30d253 2467
0ffd9845 2468 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2e30d253 2469
6a41d30b 2470 value -= tls_segment->memsz();
0ffd9845 2471 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2e30d253
ILT
2472
2473 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2474 // We can skip it.
2475 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
2476}
2477
c2b45e22
CC
2478// Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2479
2480inline void
2481Target_x86_64::Relocate::tls_desc_gd_to_ie(
2482 const Relocate_info<64, false>* relinfo,
2483 size_t relnum,
2484 Output_segment*,
2485 const elfcpp::Rela<64, false>& rela,
2486 unsigned int r_type,
2487 elfcpp::Elf_types<64>::Elf_Addr value,
2488 unsigned char* view,
2489 elfcpp::Elf_types<64>::Elf_Addr address,
2490 section_size_type view_size)
2491{
2492 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2493 {
2494 // leaq foo@tlsdesc(%rip), %rax
2495 // ==> movq foo@gottpoff(%rip), %rax
2496 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2497 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2498 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2499 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2500 view[-2] = 0x8b;
2501 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2502 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2503 }
2504 else
2505 {
2506 // call *foo@tlscall(%rax)
2507 // ==> nop; nop
2508 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2509 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2510 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2511 view[0] == 0xff && view[1] == 0x10);
2512 view[0] = 0x66;
2513 view[1] = 0x90;
2514 }
2515}
2516
2517// Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2518
2519inline void
2520Target_x86_64::Relocate::tls_desc_gd_to_le(
2521 const Relocate_info<64, false>* relinfo,
2522 size_t relnum,
2523 Output_segment* tls_segment,
2524 const elfcpp::Rela<64, false>& rela,
2525 unsigned int r_type,
2526 elfcpp::Elf_types<64>::Elf_Addr value,
2527 unsigned char* view,
2528 section_size_type view_size)
2529{
2530 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2531 {
2532 // leaq foo@tlsdesc(%rip), %rax
2533 // ==> movq foo@tpoff, %rax
2534 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2535 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2536 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2537 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2538 view[-2] = 0xc7;
2539 view[-1] = 0xc0;
2540 value -= tls_segment->memsz();
2541 Relocate_functions<64, false>::rela32(view, value, 0);
2542 }
2543 else
2544 {
2545 // call *foo@tlscall(%rax)
2546 // ==> nop; nop
2547 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2548 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2549 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2550 view[0] == 0xff && view[1] == 0x10);
2551 view[0] = 0x66;
2552 view[1] = 0x90;
2553 }
2554}
2555
2e30d253 2556inline void
72ec2876
ILT
2557Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2558 size_t relnum,
2559 Output_segment*,
2560 const elfcpp::Rela<64, false>& rela,
2561 unsigned int,
2562 elfcpp::Elf_types<64>::Elf_Addr,
2563 unsigned char* view,
fe8718a4 2564 section_size_type view_size)
2e30d253 2565{
72ec2876
ILT
2566 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2567 // ... leq foo@dtpoff(%rax),%reg
2568 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2e30d253 2569
72ec2876
ILT
2570 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2571 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2e30d253 2572
72ec2876
ILT
2573 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2574 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2575
2576 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2577
2578 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2579
2580 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2581 // We can skip it.
2582 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
2583}
2584
56622147
ILT
2585// Do a relocation in which we convert a TLS Initial-Exec to a
2586// Local-Exec.
2587
2588inline void
2589Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2590 size_t relnum,
2591 Output_segment* tls_segment,
2592 const elfcpp::Rela<64, false>& rela,
2593 unsigned int,
2594 elfcpp::Elf_types<64>::Elf_Addr value,
2595 unsigned char* view,
fe8718a4 2596 section_size_type view_size)
56622147
ILT
2597{
2598 // We need to examine the opcodes to figure out which instruction we
2599 // are looking at.
2600
2601 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2602 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2603
2604 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2605 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2606
2607 unsigned char op1 = view[-3];
2608 unsigned char op2 = view[-2];
2609 unsigned char op3 = view[-1];
2610 unsigned char reg = op3 >> 3;
2611
2612 if (op2 == 0x8b)
2613 {
2614 // movq
2615 if (op1 == 0x4c)
2616 view[-3] = 0x49;
2617 view[-2] = 0xc7;
2618 view[-1] = 0xc0 | reg;
2619 }
2620 else if (reg == 4)
2621 {
2622 // Special handling for %rsp.
2623 if (op1 == 0x4c)
2624 view[-3] = 0x49;
2625 view[-2] = 0x81;
2626 view[-1] = 0xc0 | reg;
2627 }
2628 else
2629 {
2630 // addq
2631 if (op1 == 0x4c)
2632 view[-3] = 0x4d;
2633 view[-2] = 0x8d;
2634 view[-1] = 0x80 | reg | (reg << 3);
2635 }
2636
6a41d30b 2637 value -= tls_segment->memsz();
56622147
ILT
2638 Relocate_functions<64, false>::rela32(view, value, 0);
2639}
2640
2e30d253
ILT
2641// Relocate section data.
2642
2643void
364c7fa5
ILT
2644Target_x86_64::relocate_section(
2645 const Relocate_info<64, false>* relinfo,
2646 unsigned int sh_type,
2647 const unsigned char* prelocs,
2648 size_t reloc_count,
2649 Output_section* output_section,
2650 bool needs_special_offset_handling,
2651 unsigned char* view,
2652 elfcpp::Elf_types<64>::Elf_Addr address,
2653 section_size_type view_size,
2654 const Reloc_symbol_changes* reloc_symbol_changes)
2e30d253
ILT
2655{
2656 gold_assert(sh_type == elfcpp::SHT_RELA);
2657
2658 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2659 Target_x86_64::Relocate>(
2660 relinfo,
2661 this,
2662 prelocs,
2663 reloc_count,
730cdc88
ILT
2664 output_section,
2665 needs_special_offset_handling,
2e30d253
ILT
2666 view,
2667 address,
364c7fa5
ILT
2668 view_size,
2669 reloc_symbol_changes);
2e30d253
ILT
2670}
2671
6a74a719
ILT
2672// Return the size of a relocation while scanning during a relocatable
2673// link.
2674
2675unsigned int
2676Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2677 unsigned int r_type,
2678 Relobj* object)
2679{
2680 switch (r_type)
2681 {
2682 case elfcpp::R_X86_64_NONE:
6e5710ce
ILT
2683 case elfcpp::R_X86_64_GNU_VTINHERIT:
2684 case elfcpp::R_X86_64_GNU_VTENTRY:
6a74a719
ILT
2685 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2686 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2687 case elfcpp::R_X86_64_TLSDESC_CALL:
2688 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2689 case elfcpp::R_X86_64_DTPOFF32:
2690 case elfcpp::R_X86_64_DTPOFF64:
2691 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2692 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2693 return 0;
2694
2695 case elfcpp::R_X86_64_64:
2696 case elfcpp::R_X86_64_PC64:
2697 case elfcpp::R_X86_64_GOTOFF64:
2698 case elfcpp::R_X86_64_GOTPC64:
2699 case elfcpp::R_X86_64_PLTOFF64:
2700 case elfcpp::R_X86_64_GOT64:
2701 case elfcpp::R_X86_64_GOTPCREL64:
2702 case elfcpp::R_X86_64_GOTPCREL:
2703 case elfcpp::R_X86_64_GOTPLT64:
2704 return 8;
2705
2706 case elfcpp::R_X86_64_32:
2707 case elfcpp::R_X86_64_32S:
2708 case elfcpp::R_X86_64_PC32:
2709 case elfcpp::R_X86_64_PLT32:
2710 case elfcpp::R_X86_64_GOTPC32:
2711 case elfcpp::R_X86_64_GOT32:
2712 return 4;
2713
2714 case elfcpp::R_X86_64_16:
2715 case elfcpp::R_X86_64_PC16:
2716 return 2;
2717
2718 case elfcpp::R_X86_64_8:
2719 case elfcpp::R_X86_64_PC8:
2720 return 1;
2721
2722 case elfcpp::R_X86_64_COPY:
2723 case elfcpp::R_X86_64_GLOB_DAT:
2724 case elfcpp::R_X86_64_JUMP_SLOT:
2725 case elfcpp::R_X86_64_RELATIVE:
2726 // These are outstanding tls relocs, which are unexpected when linking
2727 case elfcpp::R_X86_64_TPOFF64:
2728 case elfcpp::R_X86_64_DTPMOD64:
2729 case elfcpp::R_X86_64_TLSDESC:
2730 object->error(_("unexpected reloc %u in object file"), r_type);
2731 return 0;
2732
2733 case elfcpp::R_X86_64_SIZE32:
2734 case elfcpp::R_X86_64_SIZE64:
2735 default:
2736 object->error(_("unsupported reloc %u against local symbol"), r_type);
2737 return 0;
2738 }
2739}
2740
2741// Scan the relocs during a relocatable link.
2742
2743void
ad0f2072 2744Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
6a74a719
ILT
2745 Layout* layout,
2746 Sized_relobj<64, false>* object,
2747 unsigned int data_shndx,
2748 unsigned int sh_type,
2749 const unsigned char* prelocs,
2750 size_t reloc_count,
2751 Output_section* output_section,
2752 bool needs_special_offset_handling,
2753 size_t local_symbol_count,
2754 const unsigned char* plocal_symbols,
2755 Relocatable_relocs* rr)
2756{
2757 gold_assert(sh_type == elfcpp::SHT_RELA);
2758
2759 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2760 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2761
7019cd25 2762 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
6a74a719 2763 Scan_relocatable_relocs>(
6a74a719
ILT
2764 symtab,
2765 layout,
2766 object,
2767 data_shndx,
2768 prelocs,
2769 reloc_count,
2770 output_section,
2771 needs_special_offset_handling,
2772 local_symbol_count,
2773 plocal_symbols,
2774 rr);
2775}
2776
2777// Relocate a section during a relocatable link.
2778
2779void
2780Target_x86_64::relocate_for_relocatable(
2781 const Relocate_info<64, false>* relinfo,
2782 unsigned int sh_type,
2783 const unsigned char* prelocs,
2784 size_t reloc_count,
2785 Output_section* output_section,
2786 off_t offset_in_output_section,
2787 const Relocatable_relocs* rr,
2788 unsigned char* view,
2789 elfcpp::Elf_types<64>::Elf_Addr view_address,
2790 section_size_type view_size,
2791 unsigned char* reloc_view,
2792 section_size_type reloc_view_size)
2793{
2794 gold_assert(sh_type == elfcpp::SHT_RELA);
2795
7019cd25 2796 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
6a74a719
ILT
2797 relinfo,
2798 prelocs,
2799 reloc_count,
2800 output_section,
2801 offset_in_output_section,
2802 rr,
2803 view,
2804 view_address,
2805 view_size,
2806 reloc_view,
2807 reloc_view_size);
2808}
2809
4fb6c25d
ILT
2810// Return the value to use for a dynamic which requires special
2811// treatment. This is how we support equality comparisons of function
2812// pointers across shared library boundaries, as described in the
2813// processor specific ABI supplement.
2814
2815uint64_t
2816Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2817{
2818 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2819 return this->plt_section()->address() + gsym->plt_offset();
2820}
2821
2e30d253
ILT
2822// Return a string used to fill a code section with nops to take up
2823// the specified length.
2824
2825std::string
8851ecca 2826Target_x86_64::do_code_fill(section_size_type length) const
2e30d253
ILT
2827{
2828 if (length >= 16)
2829 {
2830 // Build a jmpq instruction to skip over the bytes.
2831 unsigned char jmp[5];
2832 jmp[0] = 0xe9;
04bf7072 2833 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2e30d253
ILT
2834 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2835 + std::string(length - 5, '\0'));
2836 }
2837
2838 // Nop sequences of various lengths.
2839 const char nop1[1] = { 0x90 }; // nop
2840 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1caf2c51
ILT
2841 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
2842 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
2843 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
2844 0x00 };
2845 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
2e30d253 2846 0x00, 0x00 };
1caf2c51 2847 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
2e30d253 2848 0x00, 0x00, 0x00 };
1caf2c51
ILT
2849 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
2850 0x00, 0x00, 0x00, 0x00 };
2851 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
2852 0x00, 0x00, 0x00, 0x00,
2e30d253 2853 0x00 };
1caf2c51
ILT
2854 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
2855 0x84, 0x00, 0x00, 0x00,
2e30d253 2856 0x00, 0x00 };
1caf2c51
ILT
2857 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
2858 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 2859 0x00, 0x00, 0x00 };
1caf2c51
ILT
2860 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
2861 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 2862 0x00, 0x00, 0x00, 0x00 };
1caf2c51
ILT
2863 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2864 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
2865 0x00, 0x00, 0x00, 0x00,
2e30d253 2866 0x00 };
1caf2c51
ILT
2867 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2868 0x66, 0x2e, 0x0f, 0x1f, // data16
2869 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2e30d253 2870 0x00, 0x00 };
1caf2c51
ILT
2871 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2872 0x66, 0x66, 0x2e, 0x0f, // data16; data16
2873 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2874 0x00, 0x00, 0x00 };
2e30d253
ILT
2875
2876 const char* nops[16] = {
2877 NULL,
2878 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2879 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2880 };
2881
2882 return std::string(nops[length], length);
2883}
2884
e291e7b9
ILT
2885// Return the addend to use for a target specific relocation. The
2886// only target specific relocation is R_X86_64_TLSDESC for a local
2887// symbol. We want to set the addend is the offset of the local
2888// symbol in the TLS segment.
2889
2890uint64_t
2891Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
2892 uint64_t) const
2893{
2894 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
2895 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
2896 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
2897 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
2898 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
2899 gold_assert(psymval->is_tls_symbol());
2900 // The value of a TLS symbol is the offset in the TLS segment.
2901 return psymval->value(ti.object, 0);
2902}
2903
364c7fa5
ILT
2904// FNOFFSET in section SHNDX in OBJECT is the start of a function
2905// compiled with -fstack-split. The function calls non-stack-split
2906// code. We have to change the function so that it always ensures
2907// that it has enough stack space to run some random function.
2908
2909void
2910Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
2911 section_offset_type fnoffset,
2912 section_size_type fnsize,
2913 unsigned char* view,
2914 section_size_type view_size,
2915 std::string* from,
2916 std::string* to) const
2917{
2918 // The function starts with a comparison of the stack pointer and a
2919 // field in the TCB. This is followed by a jump.
2920
2921 // cmp %fs:NN,%rsp
2922 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
2923 && fnsize > 9)
2924 {
2925 // We will call __morestack if the carry flag is set after this
2926 // comparison. We turn the comparison into an stc instruction
2927 // and some nops.
2928 view[fnoffset] = '\xf9';
2929 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
2930 }
2931 // lea NN(%rsp),%r10
cbc999b9
ILT
2932 // lea NN(%rsp),%r11
2933 else if ((this->match_view(view, view_size, fnoffset,
2934 "\x4c\x8d\x94\x24", 4)
2935 || this->match_view(view, view_size, fnoffset,
2936 "\x4c\x8d\x9c\x24", 4))
364c7fa5
ILT
2937 && fnsize > 8)
2938 {
2939 // This is loading an offset from the stack pointer for a
2940 // comparison. The offset is negative, so we decrease the
2941 // offset by the amount of space we need for the stack. This
2942 // means we will avoid calling __morestack if there happens to
2943 // be plenty of space on the stack already.
2944 unsigned char* pval = view + fnoffset + 4;
2945 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2946 val -= parameters->options().split_stack_adjust_size();
2947 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2948 }
2949 else
2950 {
2951 if (!object->has_no_split_stack())
2952 object->error(_("failed to match split-stack sequence at "
2953 "section %u offset %0zx"),
ac33a407 2954 shndx, static_cast<size_t>(fnoffset));
364c7fa5
ILT
2955 return;
2956 }
2957
2958 // We have to change the function so that it calls
2959 // __morestack_non_split instead of __morestack. The former will
2960 // allocate additional stack space.
2961 *from = "__morestack";
2962 *to = "__morestack_non_split";
2963}
2964
2e30d253
ILT
2965// The selector for x86_64 object files.
2966
36959681 2967class Target_selector_x86_64 : public Target_selector_freebsd
2e30d253
ILT
2968{
2969public:
2970 Target_selector_x86_64()
36959681
ILT
2971 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
2972 "elf64-x86-64-freebsd")
2e30d253
ILT
2973 { }
2974
2975 Target*
e96caa79
ILT
2976 do_instantiate_target()
2977 { return new Target_x86_64(); }
36959681 2978
2e30d253
ILT
2979};
2980
2e30d253
ILT
2981Target_selector_x86_64 target_selector_x86_64;
2982
2983} // End anonymous namespace.
This page took 0.310575 seconds and 4 git commands to generate.