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