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