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