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