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