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