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" | |
47 | ||
48 | namespace | |
49 | { | |
50 | ||
51 | using namespace gold; | |
52 | ||
53 | class Output_data_plt_x86_64; | |
54 | ||
55 | // The x86_64 target class. | |
d61c17ea ILT |
56 | // See the ABI at |
57 | // http://www.x86-64.org/documentation/abi.pdf | |
58 | // TLS info comes from | |
59 | // http://people.redhat.com/drepper/tls.pdf | |
60 | // http://ia32-abi.googlegroups.com/web/RFC-TLSDESC-x86.txt?gda=kWQJPEQAAACEfYQFX0dubPQ2NuO4whhjkR4HAp8tBMb_I0iuUeQslmG1qiJ7UbTIup-M2XPURDRiZJyPR4BqKR2agJ-5jfT5Ley2_-oiOJ4zLNAGCw24Bg | |
2e30d253 ILT |
61 | |
62 | class Target_x86_64 : public Sized_target<64, false> | |
63 | { | |
64 | public: | |
e822f2b1 ILT |
65 | // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures |
66 | // uses only Elf64_Rela relocation entries with explicit addends." | |
2e30d253 ILT |
67 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section; |
68 | ||
69 | Target_x86_64() | |
70 | : Sized_target<64, false>(&x86_64_info), | |
71 | got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL), | |
72 | copy_relocs_(NULL), dynbss_(NULL) | |
73 | { } | |
74 | ||
75 | // Scan the relocations to look for symbol adjustments. | |
76 | void | |
77 | scan_relocs(const General_options& options, | |
78 | Symbol_table* symtab, | |
79 | Layout* layout, | |
80 | Sized_relobj<64, false>* object, | |
81 | unsigned int data_shndx, | |
82 | unsigned int sh_type, | |
83 | const unsigned char* prelocs, | |
84 | size_t reloc_count, | |
85 | size_t local_symbol_count, | |
86 | const unsigned char* plocal_symbols, | |
87 | Symbol** global_symbols); | |
88 | ||
89 | // Finalize the sections. | |
90 | void | |
91 | do_finalize_sections(Layout*); | |
92 | ||
4fb6c25d ILT |
93 | // Return the value to use for a dynamic which requires special |
94 | // treatment. | |
95 | uint64_t | |
96 | do_dynsym_value(const Symbol*) const; | |
97 | ||
2e30d253 ILT |
98 | // Relocate a section. |
99 | void | |
100 | relocate_section(const Relocate_info<64, false>*, | |
101 | unsigned int sh_type, | |
102 | const unsigned char* prelocs, | |
103 | size_t reloc_count, | |
104 | unsigned char* view, | |
105 | elfcpp::Elf_types<64>::Elf_Addr view_address, | |
106 | off_t view_size); | |
107 | ||
108 | // Return a string used to fill a code section with nops. | |
109 | std::string | |
110 | do_code_fill(off_t length); | |
111 | ||
112 | private: | |
113 | // The class which scans relocations. | |
114 | struct Scan | |
115 | { | |
116 | inline void | |
117 | local(const General_options& options, Symbol_table* symtab, | |
118 | Layout* layout, Target_x86_64* target, | |
119 | Sized_relobj<64, false>* object, | |
120 | unsigned int data_shndx, | |
121 | const elfcpp::Rela<64, false>& reloc, unsigned int r_type, | |
122 | const elfcpp::Sym<64, false>& lsym); | |
123 | ||
124 | inline void | |
125 | global(const General_options& options, Symbol_table* symtab, | |
126 | Layout* layout, Target_x86_64* target, | |
127 | Sized_relobj<64, false>* object, | |
128 | unsigned int data_shndx, | |
129 | const elfcpp::Rela<64, false>& reloc, unsigned int r_type, | |
130 | Symbol* gsym); | |
131 | }; | |
132 | ||
133 | // The class which implements relocation. | |
134 | class Relocate | |
135 | { | |
136 | public: | |
137 | Relocate() | |
138 | : skip_call_tls_get_addr_(false) | |
139 | { } | |
140 | ||
141 | ~Relocate() | |
142 | { | |
143 | if (this->skip_call_tls_get_addr_) | |
144 | { | |
145 | // FIXME: This needs to specify the location somehow. | |
146 | fprintf(stderr, _("%s: missing expected TLS relocation\n"), | |
147 | program_name); | |
148 | gold_exit(false); | |
149 | } | |
150 | } | |
151 | ||
152 | // Do a relocation. Return false if the caller should not issue | |
153 | // any warnings about this relocation. | |
154 | inline bool | |
155 | relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum, | |
156 | const elfcpp::Rela<64, false>&, | |
157 | unsigned int r_type, const Sized_symbol<64>*, | |
158 | const Symbol_value<64>*, | |
159 | unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, | |
160 | off_t); | |
161 | ||
162 | private: | |
163 | // Do a TLS relocation. | |
164 | inline void | |
165 | relocate_tls(const Relocate_info<64, false>*, size_t relnum, | |
166 | const elfcpp::Rela<64, false>&, | |
167 | unsigned int r_type, const Sized_symbol<64>*, | |
168 | const Symbol_value<64>*, | |
169 | unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, off_t); | |
170 | ||
171 | // Do a TLS Initial-Exec to Local-Exec transition. | |
172 | static inline void | |
173 | tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum, | |
174 | Output_segment* tls_segment, | |
175 | const elfcpp::Rela<64, false>&, unsigned int r_type, | |
176 | elfcpp::Elf_types<64>::Elf_Addr value, | |
177 | unsigned char* view, | |
178 | off_t view_size); | |
179 | ||
180 | // Do a TLS Global-Dynamic to Local-Exec transition. | |
181 | inline void | |
182 | tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum, | |
183 | Output_segment* tls_segment, | |
184 | const elfcpp::Rela<64, false>&, unsigned int r_type, | |
185 | elfcpp::Elf_types<64>::Elf_Addr value, | |
186 | unsigned char* view, | |
187 | off_t view_size); | |
188 | ||
189 | // Check the range for a TLS relocation. | |
190 | static inline void | |
191 | check_range(const Relocate_info<64, false>*, size_t relnum, | |
192 | const elfcpp::Rela<64, false>&, off_t, off_t); | |
193 | ||
194 | // Check the validity of a TLS relocation. This is like assert. | |
195 | static inline void | |
196 | check_tls(const Relocate_info<64, false>*, size_t relnum, | |
197 | const elfcpp::Rela<64, false>&, bool); | |
198 | ||
199 | // This is set if we should skip the next reloc, which should be a | |
200 | // PLT32 reloc against ___tls_get_addr. | |
201 | bool skip_call_tls_get_addr_; | |
202 | }; | |
203 | ||
204 | // Adjust TLS relocation type based on the options and whether this | |
205 | // is a local symbol. | |
206 | static unsigned int | |
207 | optimize_tls_reloc(bool is_final, int r_type); | |
208 | ||
209 | // Get the GOT section, creating it if necessary. | |
210 | Output_data_got<64, false>* | |
211 | got_section(Symbol_table*, Layout*); | |
212 | ||
213 | // Create a PLT entry for a global symbol. | |
214 | void | |
215 | make_plt_entry(Symbol_table*, Layout*, Symbol*); | |
216 | ||
217 | // Get the PLT section. | |
218 | Output_data_plt_x86_64* | |
219 | plt_section() const | |
220 | { | |
221 | gold_assert(this->plt_ != NULL); | |
222 | return this->plt_; | |
223 | } | |
224 | ||
225 | // Get the dynamic reloc section, creating it if necessary. | |
226 | Reloc_section* | |
227 | rel_dyn_section(Layout*); | |
228 | ||
229 | // Copy a relocation against a global symbol. | |
230 | void | |
231 | copy_reloc(const General_options*, Symbol_table*, Layout*, | |
232 | Sized_relobj<64, false>*, unsigned int, | |
233 | Symbol*, const elfcpp::Rela<64, false>&); | |
234 | ||
235 | // Information about this specific target which we pass to the | |
236 | // general Target structure. | |
237 | static const Target::Target_info x86_64_info; | |
238 | ||
239 | // The GOT section. | |
240 | Output_data_got<64, false>* got_; | |
241 | // The PLT section. | |
242 | Output_data_plt_x86_64* plt_; | |
243 | // The GOT PLT section. | |
244 | Output_data_space* got_plt_; | |
245 | // The dynamic reloc section. | |
246 | Reloc_section* rel_dyn_; | |
247 | // Relocs saved to avoid a COPY reloc. | |
248 | Copy_relocs<64, false>* copy_relocs_; | |
249 | // Space for variables copied with a COPY reloc. | |
250 | Output_data_space* dynbss_; | |
251 | }; | |
252 | ||
253 | const Target::Target_info Target_x86_64::x86_64_info = | |
254 | { | |
255 | 64, // size | |
256 | false, // is_big_endian | |
257 | elfcpp::EM_X86_64, // machine_code | |
258 | false, // has_make_symbol | |
259 | false, // has_resolve | |
260 | true, // has_code_fill | |
261 | "/lib/ld64.so.1", // program interpreter | |
262 | 0x400000, // text_segment_address | |
263 | 0x1000, // abi_pagesize | |
264 | 0x1000 // common_pagesize | |
265 | }; | |
266 | ||
267 | // Get the GOT section, creating it if necessary. | |
268 | ||
269 | Output_data_got<64, false>* | |
270 | Target_x86_64::got_section(Symbol_table* symtab, Layout* layout) | |
271 | { | |
272 | if (this->got_ == NULL) | |
273 | { | |
274 | gold_assert(symtab != NULL && layout != NULL); | |
275 | ||
276 | this->got_ = new Output_data_got<64, false>(); | |
277 | ||
278 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, | |
279 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, | |
280 | this->got_); | |
281 | ||
282 | // The old GNU linker creates a .got.plt section. We just | |
283 | // create another set of data in the .got section. Note that we | |
284 | // always create a PLT if we create a GOT, although the PLT | |
285 | // might be empty. | |
286 | // TODO(csilvers): do we really need an alignment of 8? | |
287 | this->got_plt_ = new Output_data_space(8); | |
288 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, | |
289 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, | |
290 | this->got_plt_); | |
291 | ||
292 | // The first three entries are reserved. | |
293 | this->got_plt_->set_space_size(3 * 8); | |
294 | ||
295 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. | |
296 | symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL, | |
297 | this->got_plt_, | |
298 | 0, 0, elfcpp::STT_OBJECT, | |
299 | elfcpp::STB_LOCAL, | |
300 | elfcpp::STV_HIDDEN, 0, | |
301 | false, false); | |
302 | } | |
303 | ||
304 | return this->got_; | |
305 | } | |
306 | ||
307 | // Get the dynamic reloc section, creating it if necessary. | |
308 | ||
309 | Target_x86_64::Reloc_section* | |
310 | Target_x86_64::rel_dyn_section(Layout* layout) | |
311 | { | |
312 | if (this->rel_dyn_ == NULL) | |
313 | { | |
314 | gold_assert(layout != NULL); | |
315 | this->rel_dyn_ = new Reloc_section(); | |
316 | layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, | |
317 | elfcpp::SHF_ALLOC, this->rel_dyn_); | |
318 | } | |
319 | return this->rel_dyn_; | |
320 | } | |
321 | ||
322 | // A class to handle the PLT data. | |
323 | ||
324 | class Output_data_plt_x86_64 : public Output_section_data | |
325 | { | |
326 | public: | |
327 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section; | |
328 | ||
329 | Output_data_plt_x86_64(Layout*, Output_data_space*); | |
330 | ||
331 | // Add an entry to the PLT. | |
332 | void | |
333 | add_entry(Symbol* gsym); | |
334 | ||
335 | // Return the .rel.plt section data. | |
336 | const Reloc_section* | |
337 | rel_plt() const | |
338 | { return this->rel_; } | |
339 | ||
340 | protected: | |
341 | void | |
342 | do_adjust_output_section(Output_section* os); | |
343 | ||
344 | private: | |
345 | // The size of an entry in the PLT. | |
346 | static const int plt_entry_size = 16; | |
347 | ||
348 | // The first entry in the PLT. | |
349 | // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same | |
350 | // procedure linkage table for both programs and shared objects." | |
351 | static unsigned char first_plt_entry[plt_entry_size]; | |
352 | ||
353 | // Other entries in the PLT for an executable. | |
354 | static unsigned char plt_entry[plt_entry_size]; | |
355 | ||
356 | // Set the final size. | |
357 | void | |
358 | do_set_address(uint64_t, off_t) | |
359 | { this->set_data_size((this->count_ + 1) * plt_entry_size); } | |
360 | ||
361 | // Write out the PLT data. | |
362 | void | |
363 | do_write(Output_file*); | |
364 | ||
365 | // The reloc section. | |
366 | Reloc_section* rel_; | |
367 | // The .got.plt section. | |
368 | Output_data_space* got_plt_; | |
369 | // The number of PLT entries. | |
370 | unsigned int count_; | |
371 | }; | |
372 | ||
373 | // Create the PLT section. The ordinary .got section is an argument, | |
374 | // since we need to refer to the start. We also create our own .got | |
375 | // section just for PLT entries. | |
376 | ||
377 | Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout, | |
378 | Output_data_space* got_plt) | |
379 | // TODO(csilvers): do we really need an alignment of 8? | |
380 | : Output_section_data(8), got_plt_(got_plt), count_(0) | |
381 | { | |
382 | this->rel_ = new Reloc_section(); | |
383 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, | |
384 | elfcpp::SHF_ALLOC, this->rel_); | |
385 | } | |
386 | ||
387 | void | |
388 | Output_data_plt_x86_64::do_adjust_output_section(Output_section* os) | |
389 | { | |
390 | // UnixWare sets the entsize of .plt to 4, and so does the old GNU | |
391 | // linker, and so do we. | |
392 | os->set_entsize(4); | |
393 | } | |
394 | ||
395 | // Add an entry to the PLT. | |
396 | ||
397 | void | |
398 | Output_data_plt_x86_64::add_entry(Symbol* gsym) | |
399 | { | |
400 | gold_assert(!gsym->has_plt_offset()); | |
401 | ||
402 | // Note that when setting the PLT offset we skip the initial | |
403 | // reserved PLT entry. | |
404 | gsym->set_plt_offset((this->count_ + 1) * plt_entry_size); | |
405 | ||
406 | ++this->count_; | |
407 | ||
408 | off_t got_offset = this->got_plt_->data_size(); | |
409 | ||
410 | // Every PLT entry needs a GOT entry which points back to the PLT | |
411 | // entry (this will be changed by the dynamic linker, normally | |
412 | // lazily when the function is called). | |
413 | this->got_plt_->set_space_size(got_offset + 8); | |
414 | ||
415 | // Every PLT entry needs a reloc. | |
416 | gsym->set_needs_dynsym_entry(); | |
417 | this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_, | |
418 | got_offset, 0); | |
419 | ||
420 | // Note that we don't need to save the symbol. The contents of the | |
421 | // PLT are independent of which symbols are used. The symbols only | |
422 | // appear in the relocations. | |
423 | } | |
424 | ||
425 | // The first entry in the PLT for an executable. | |
426 | ||
427 | unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] = | |
428 | { | |
429 | // From AMD64 ABI Draft 0.98, page 76 | |
430 | 0xff, 0x35, // pushq contents of memory address | |
431 | 0, 0, 0, 0, // replaced with address of .got + 4 | |
432 | 0xff, 0x25, // jmp indirect | |
433 | 0, 0, 0, 0, // replaced with address of .got + 8 | |
434 | 0x90, 0x90, 0x90, 0x90 // noop (x4) | |
435 | }; | |
436 | ||
437 | // Subsequent entries in the PLT for an executable. | |
438 | ||
439 | unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] = | |
440 | { | |
441 | // From AMD64 ABI Draft 0.98, page 76 | |
442 | 0xff, 0x25, // jmpq indirect | |
443 | 0, 0, 0, 0, // replaced with address of symbol in .got | |
444 | 0x68, // pushq immediate | |
445 | 0, 0, 0, 0, // replaced with offset into relocation table | |
446 | 0xe9, // jmpq relative | |
447 | 0, 0, 0, 0 // replaced with offset to start of .plt | |
448 | }; | |
449 | ||
450 | // Write out the PLT. This uses the hand-coded instructions above, | |
451 | // and adjusts them as needed. This is specified by the AMD64 ABI. | |
452 | ||
453 | void | |
454 | Output_data_plt_x86_64::do_write(Output_file* of) | |
455 | { | |
456 | const off_t offset = this->offset(); | |
457 | const off_t oview_size = this->data_size(); | |
458 | unsigned char* const oview = of->get_output_view(offset, oview_size); | |
459 | ||
460 | const off_t got_file_offset = this->got_plt_->offset(); | |
461 | const off_t got_size = this->got_plt_->data_size(); | |
462 | unsigned char* const got_view = of->get_output_view(got_file_offset, | |
463 | got_size); | |
464 | ||
465 | unsigned char* pov = oview; | |
466 | ||
467 | elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address(); | |
468 | elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address(); | |
469 | ||
470 | memcpy(pov, first_plt_entry, plt_entry_size); | |
471 | if (!parameters->output_is_shared()) | |
472 | { | |
473 | // We do a jmp relative to the PC at the end of this instruction. | |
474 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8 | |
475 | - (plt_address + 6)); | |
476 | elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16 | |
477 | - (plt_address + 12)); | |
478 | } | |
479 | pov += plt_entry_size; | |
480 | ||
481 | unsigned char* got_pov = got_view; | |
482 | ||
483 | memset(got_pov, 0, 24); | |
484 | got_pov += 24; | |
485 | ||
486 | unsigned int plt_offset = plt_entry_size; | |
487 | unsigned int got_offset = 24; | |
488 | const unsigned int count = this->count_; | |
489 | for (unsigned int plt_index = 0; | |
490 | plt_index < count; | |
491 | ++plt_index, | |
492 | pov += plt_entry_size, | |
493 | got_pov += 8, | |
494 | plt_offset += plt_entry_size, | |
495 | got_offset += 8) | |
496 | { | |
497 | // Set and adjust the PLT entry itself. | |
498 | memcpy(pov, plt_entry, plt_entry_size); | |
499 | if (parameters->output_is_shared()) | |
500 | // FIXME(csilvers): what's the right thing to write here? | |
501 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset); | |
502 | else | |
503 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, | |
504 | (got_address + got_offset | |
505 | - (plt_address + plt_offset | |
506 | + 6))); | |
507 | ||
508 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index); | |
509 | elfcpp::Swap<32, false>::writeval(pov + 12, | |
510 | - (plt_offset + plt_entry_size)); | |
511 | ||
512 | // Set the entry in the GOT. | |
513 | elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6); | |
514 | } | |
515 | ||
516 | gold_assert(pov - oview == oview_size); | |
517 | gold_assert(got_pov - got_view == got_size); | |
518 | ||
519 | of->write_output_view(offset, oview_size, oview); | |
520 | of->write_output_view(got_file_offset, got_size, got_view); | |
521 | } | |
522 | ||
523 | // Create a PLT entry for a global symbol. | |
524 | ||
525 | void | |
526 | Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout, | |
527 | Symbol* gsym) | |
528 | { | |
529 | if (gsym->has_plt_offset()) | |
530 | return; | |
531 | ||
532 | if (this->plt_ == NULL) | |
533 | { | |
534 | // Create the GOT sections first. | |
535 | this->got_section(symtab, layout); | |
536 | ||
537 | this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_); | |
538 | layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, | |
539 | (elfcpp::SHF_ALLOC | |
540 | | elfcpp::SHF_EXECINSTR), | |
541 | this->plt_); | |
542 | } | |
543 | ||
544 | this->plt_->add_entry(gsym); | |
545 | } | |
546 | ||
547 | // Handle a relocation against a non-function symbol defined in a | |
548 | // dynamic object. The traditional way to handle this is to generate | |
549 | // a COPY relocation to copy the variable at runtime from the shared | |
550 | // object into the executable's data segment. However, this is | |
551 | // undesirable in general, as if the size of the object changes in the | |
552 | // dynamic object, the executable will no longer work correctly. If | |
553 | // this relocation is in a writable section, then we can create a | |
554 | // dynamic reloc and the dynamic linker will resolve it to the correct | |
555 | // address at runtime. However, we do not want do that if the | |
556 | // relocation is in a read-only section, as it would prevent the | |
557 | // readonly segment from being shared. And if we have to eventually | |
558 | // generate a COPY reloc, then any dynamic relocations will be | |
559 | // useless. So this means that if this is a writable section, we need | |
560 | // to save the relocation until we see whether we have to create a | |
561 | // COPY relocation for this symbol for any other relocation. | |
562 | ||
563 | void | |
564 | Target_x86_64::copy_reloc(const General_options* options, | |
d61c17ea ILT |
565 | Symbol_table* symtab, |
566 | Layout* layout, | |
567 | Sized_relobj<64, false>* object, | |
568 | unsigned int data_shndx, Symbol* gsym, | |
569 | const elfcpp::Rela<64, false>& rel) | |
2e30d253 ILT |
570 | { |
571 | Sized_symbol<64>* ssym; | |
572 | ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym | |
573 | SELECT_SIZE(64)); | |
574 | ||
575 | if (!Copy_relocs<64, false>::need_copy_reloc(options, object, | |
576 | data_shndx, ssym)) | |
577 | { | |
578 | // So far we do not need a COPY reloc. Save this relocation. | |
579 | // If it turns out that we never need a COPY reloc for this | |
580 | // symbol, then we will emit the relocation. | |
581 | if (this->copy_relocs_ == NULL) | |
582 | this->copy_relocs_ = new Copy_relocs<64, false>(); | |
583 | this->copy_relocs_->save(ssym, object, data_shndx, rel); | |
584 | } | |
585 | else | |
586 | { | |
587 | // Allocate space for this symbol in the .bss section. | |
588 | ||
589 | elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize(); | |
590 | ||
591 | // There is no defined way to determine the required alignment | |
592 | // of the symbol. We pick the alignment based on the size. We | |
593 | // set an arbitrary maximum of 256. | |
594 | unsigned int align; | |
595 | for (align = 1; align < 512; align <<= 1) | |
596 | if ((symsize & align) != 0) | |
597 | break; | |
598 | ||
599 | if (this->dynbss_ == NULL) | |
600 | { | |
601 | this->dynbss_ = new Output_data_space(align); | |
602 | layout->add_output_section_data(".bss", | |
603 | elfcpp::SHT_NOBITS, | |
604 | (elfcpp::SHF_ALLOC | |
605 | | elfcpp::SHF_WRITE), | |
606 | this->dynbss_); | |
607 | } | |
608 | ||
609 | Output_data_space* dynbss = this->dynbss_; | |
610 | ||
611 | if (align > dynbss->addralign()) | |
612 | dynbss->set_space_alignment(align); | |
613 | ||
614 | off_t dynbss_size = dynbss->data_size(); | |
615 | dynbss_size = align_address(dynbss_size, align); | |
616 | off_t offset = dynbss_size; | |
617 | dynbss->set_space_size(dynbss_size + symsize); | |
618 | ||
619 | // Define the symbol in the .dynbss section. | |
620 | symtab->define_in_output_data(this, ssym->name(), ssym->version(), | |
621 | dynbss, offset, symsize, ssym->type(), | |
622 | ssym->binding(), ssym->visibility(), | |
623 | ssym->nonvis(), false, false); | |
624 | ||
625 | // Add the COPY reloc. | |
626 | ssym->set_needs_dynsym_entry(); | |
627 | Reloc_section* rel_dyn = this->rel_dyn_section(layout); | |
ff006520 | 628 | rel_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0); |
2e30d253 ILT |
629 | } |
630 | } | |
631 | ||
632 | ||
633 | // Optimize the TLS relocation type based on what we know about the | |
634 | // symbol. IS_FINAL is true if the final address of this symbol is | |
635 | // known at link time. | |
636 | ||
637 | unsigned int | |
638 | Target_x86_64::optimize_tls_reloc(bool is_final, int r_type) | |
639 | { | |
2e30d253 ILT |
640 | // If we are generating a shared library, then we can't do anything |
641 | // in the linker. | |
642 | if (parameters->output_is_shared()) | |
643 | return r_type; | |
644 | ||
645 | switch (r_type) | |
646 | { | |
647 | case elfcpp::R_X86_64_TLSGD: | |
d61c17ea ILT |
648 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct? |
649 | case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct? | |
2e30d253 ILT |
650 | // These are Global-Dynamic which permits fully general TLS |
651 | // access. Since we know that we are generating an executable, | |
652 | // we can convert this to Initial-Exec. If we also know that | |
653 | // this is a local symbol, we can further switch to Local-Exec. | |
654 | if (is_final) | |
d61c17ea ILT |
655 | return elfcpp::R_X86_64_TPOFF32; |
656 | return elfcpp::R_X86_64_GOTTPOFF; // used for Initial-exec | |
2e30d253 | 657 | |
d61c17ea | 658 | case elfcpp::R_X86_64_TLSLD: |
2e30d253 ILT |
659 | // This is Local-Dynamic, which refers to a local symbol in the |
660 | // dynamic TLS block. Since we know that we generating an | |
661 | // executable, we can switch to Local-Exec. | |
d61c17ea | 662 | return elfcpp::R_X86_64_TPOFF32; |
2e30d253 | 663 | |
d61c17ea | 664 | case elfcpp::R_X86_64_GOTTPOFF: |
2e30d253 ILT |
665 | // These are Initial-Exec relocs which get the thread offset |
666 | // from the GOT. If we know that we are linking against the | |
667 | // local symbol, we can switch to Local-Exec, which links the | |
668 | // thread offset into the instruction. | |
669 | if (is_final) | |
d61c17ea | 670 | return elfcpp::R_X86_64_TPOFF32; |
2e30d253 ILT |
671 | return r_type; |
672 | ||
d61c17ea | 673 | case elfcpp::R_X86_64_TPOFF32: |
2e30d253 ILT |
674 | // When we already have Local-Exec, there is nothing further we |
675 | // can do. | |
676 | return r_type; | |
677 | ||
678 | default: | |
679 | gold_unreachable(); | |
680 | } | |
2e30d253 ILT |
681 | } |
682 | ||
683 | // Scan a relocation for a local symbol. | |
684 | ||
685 | inline void | |
686 | Target_x86_64::Scan::local(const General_options&, | |
d61c17ea ILT |
687 | Symbol_table* symtab, |
688 | Layout* layout, | |
689 | Target_x86_64* target, | |
690 | Sized_relobj<64, false>* object, | |
691 | unsigned int, | |
692 | const elfcpp::Rela<64, false>&, | |
693 | unsigned int r_type, | |
694 | const elfcpp::Sym<64, false>&) | |
2e30d253 ILT |
695 | { |
696 | switch (r_type) | |
697 | { | |
698 | case elfcpp::R_X86_64_NONE: | |
e822f2b1 ILT |
699 | case elfcpp::R_386_GNU_VTINHERIT: |
700 | case elfcpp::R_386_GNU_VTENTRY: | |
2e30d253 ILT |
701 | break; |
702 | ||
703 | case elfcpp::R_X86_64_64: | |
704 | case elfcpp::R_X86_64_32: | |
705 | case elfcpp::R_X86_64_32S: | |
706 | case elfcpp::R_X86_64_16: | |
707 | case elfcpp::R_X86_64_8: | |
708 | // FIXME: If we are generating a shared object we need to copy | |
709 | // this relocation into the object. | |
710 | gold_assert(!parameters->output_is_shared()); | |
711 | break; | |
712 | ||
713 | case elfcpp::R_X86_64_PC64: | |
714 | case elfcpp::R_X86_64_PC32: | |
715 | case elfcpp::R_X86_64_PC16: | |
716 | case elfcpp::R_X86_64_PC8: | |
717 | break; | |
718 | ||
e822f2b1 | 719 | case elfcpp::R_X86_64_GOTPC32: // TODO(csilvers): correct? |
e822f2b1 ILT |
720 | case elfcpp::R_X86_64_GOTOFF64: |
721 | case elfcpp::R_X86_64_GOTPC64: // TODO(csilvers): correct? | |
e822f2b1 | 722 | case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): correct? |
2e30d253 ILT |
723 | // We need a GOT section. |
724 | target->got_section(symtab, layout); | |
725 | break; | |
726 | ||
727 | case elfcpp::R_X86_64_COPY: | |
728 | case elfcpp::R_X86_64_GLOB_DAT: | |
729 | case elfcpp::R_X86_64_JUMP_SLOT: | |
730 | case elfcpp::R_X86_64_RELATIVE: | |
d61c17ea | 731 | // These are outstanding tls relocs, which are unexpected when linking |
2e30d253 | 732 | case elfcpp::R_X86_64_TPOFF64: |
2e30d253 ILT |
733 | case elfcpp::R_X86_64_DTPMOD64: |
734 | case elfcpp::R_X86_64_DTPOFF64: | |
735 | case elfcpp::R_X86_64_DTPOFF32: | |
736 | case elfcpp::R_X86_64_TLSDESC: | |
737 | fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"), | |
738 | program_name, object->name().c_str(), r_type); | |
739 | gold_exit(false); | |
740 | break; | |
741 | ||
d61c17ea | 742 | // These are initial tls relocs, which are expected when linking |
e822f2b1 ILT |
743 | case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct? |
744 | case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct? | |
d61c17ea ILT |
745 | case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct? |
746 | case elfcpp::R_X86_64_TPOFF32: // TODO(csilvers): correct? | |
747 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct? | |
748 | case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct? | |
2e30d253 ILT |
749 | { |
750 | bool output_is_shared = parameters->output_is_shared(); | |
d61c17ea | 751 | r_type = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type); |
2e30d253 ILT |
752 | switch (r_type) |
753 | { | |
d61c17ea | 754 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
2e30d253 ILT |
755 | // FIXME: If generating a shared object, we need to copy |
756 | // this relocation into the object. | |
757 | gold_assert(!output_is_shared); | |
758 | break; | |
759 | ||
d61c17ea ILT |
760 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
761 | case elfcpp::R_X86_64_TLSGD: // General Dynamic | |
762 | case elfcpp::R_X86_64_TLSLD: // Local Dynamic | |
763 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: | |
764 | case elfcpp::R_X86_64_TLSDESC_CALL: | |
2e30d253 ILT |
765 | fprintf(stderr, |
766 | _("%s: %s: unsupported reloc %u against local symbol\n"), | |
767 | program_name, object->name().c_str(), r_type); | |
768 | break; | |
769 | } | |
770 | } | |
771 | break; | |
2e30d253 | 772 | |
ff006520 | 773 | case elfcpp::R_X86_64_GOT64: // TODO(csilvers): correct? |
2e30d253 | 774 | case elfcpp::R_X86_64_GOT32: |
ff006520 ILT |
775 | case elfcpp::R_X86_64_GOTPCREL64: // TODO(csilvers): correct? |
776 | case elfcpp::R_X86_64_GOTPCREL: | |
777 | case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct? | |
2e30d253 | 778 | case elfcpp::R_X86_64_PLT32: |
e822f2b1 ILT |
779 | case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct? |
780 | case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct? | |
2e30d253 ILT |
781 | default: |
782 | fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"), | |
783 | program_name, object->name().c_str(), r_type); | |
784 | break; | |
785 | } | |
786 | } | |
787 | ||
788 | ||
789 | // Scan a relocation for a global symbol. | |
790 | ||
791 | inline void | |
792 | Target_x86_64::Scan::global(const General_options& options, | |
d61c17ea ILT |
793 | Symbol_table* symtab, |
794 | Layout* layout, | |
795 | Target_x86_64* target, | |
796 | Sized_relobj<64, false>* object, | |
797 | unsigned int data_shndx, | |
798 | const elfcpp::Rela<64, false>& reloc, | |
799 | unsigned int r_type, | |
800 | Symbol* gsym) | |
2e30d253 ILT |
801 | { |
802 | switch (r_type) | |
803 | { | |
804 | case elfcpp::R_X86_64_NONE: | |
e822f2b1 ILT |
805 | case elfcpp::R_386_GNU_VTINHERIT: |
806 | case elfcpp::R_386_GNU_VTENTRY: | |
2e30d253 ILT |
807 | break; |
808 | ||
809 | case elfcpp::R_X86_64_64: | |
810 | case elfcpp::R_X86_64_PC64: | |
811 | case elfcpp::R_X86_64_32: | |
812 | case elfcpp::R_X86_64_32S: | |
813 | case elfcpp::R_X86_64_PC32: | |
814 | case elfcpp::R_X86_64_16: | |
815 | case elfcpp::R_X86_64_PC16: | |
816 | case elfcpp::R_X86_64_8: | |
817 | case elfcpp::R_X86_64_PC8: | |
818 | // FIXME: If we are generating a shared object we may need to | |
819 | // copy this relocation into the object. If this symbol is | |
820 | // defined in a shared object, we may need to copy this | |
821 | // relocation in order to avoid a COPY relocation. | |
822 | gold_assert(!parameters->output_is_shared()); | |
823 | ||
824 | if (gsym->is_from_dynobj()) | |
825 | { | |
826 | // This symbol is defined in a dynamic object. If it is a | |
827 | // function, we make a PLT entry. Otherwise we need to | |
828 | // either generate a COPY reloc or copy this reloc. | |
829 | if (gsym->type() == elfcpp::STT_FUNC) | |
4fb6c25d ILT |
830 | { |
831 | target->make_plt_entry(symtab, layout, gsym); | |
832 | ||
833 | // If this is not a PC relative reference, then we may | |
834 | // be taking the address of the function. In that case | |
835 | // we need to set the entry in the dynamic symbol table | |
836 | // to the address of the PLT entry. | |
837 | if (r_type != elfcpp::R_X86_64_PC64 | |
838 | && r_type != elfcpp::R_X86_64_PC32 | |
839 | && r_type != elfcpp::R_X86_64_PC16 | |
840 | && r_type != elfcpp::R_X86_64_PC8) | |
841 | gsym->set_needs_dynsym_value(); | |
842 | } | |
2e30d253 ILT |
843 | else |
844 | target->copy_reloc(&options, symtab, layout, object, data_shndx, | |
845 | gsym, reloc); | |
846 | } | |
847 | ||
848 | break; | |
849 | ||
ff006520 | 850 | case elfcpp::R_X86_64_GOT64: |
2e30d253 | 851 | case elfcpp::R_X86_64_GOT32: |
ff006520 ILT |
852 | case elfcpp::R_X86_64_GOTPCREL64: |
853 | case elfcpp::R_X86_64_GOTPCREL: | |
854 | case elfcpp::R_X86_64_GOTPLT64: | |
2e30d253 ILT |
855 | { |
856 | // The symbol requires a GOT entry. | |
857 | Output_data_got<64, false>* got = target->got_section(symtab, layout); | |
858 | if (got->add_global(gsym)) | |
859 | { | |
860 | // If this symbol is not fully resolved, we need to add a | |
861 | // dynamic relocation for it. | |
862 | if (!gsym->final_value_is_known()) | |
863 | { | |
864 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
865 | rel_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, got, | |
ff006520 | 866 | gsym->got_offset(), 0); |
2e30d253 ILT |
867 | } |
868 | } | |
869 | } | |
870 | break; | |
871 | ||
872 | case elfcpp::R_X86_64_PLT32: | |
873 | // If the symbol is fully resolved, this is just a PC32 reloc. | |
874 | // Otherwise we need a PLT entry. | |
875 | if (gsym->final_value_is_known()) | |
876 | break; | |
877 | target->make_plt_entry(symtab, layout, gsym); | |
878 | break; | |
879 | ||
e822f2b1 | 880 | case elfcpp::R_X86_64_GOTPC32: // TODO(csilvers): correct? |
e822f2b1 ILT |
881 | case elfcpp::R_X86_64_GOTOFF64: |
882 | case elfcpp::R_X86_64_GOTPC64: // TODO(csilvers): correct? | |
e822f2b1 | 883 | case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): correct? |
2e30d253 ILT |
884 | // We need a GOT section. |
885 | target->got_section(symtab, layout); | |
886 | break; | |
887 | ||
2e30d253 ILT |
888 | case elfcpp::R_X86_64_COPY: |
889 | case elfcpp::R_X86_64_GLOB_DAT: | |
890 | case elfcpp::R_X86_64_JUMP_SLOT: | |
891 | case elfcpp::R_X86_64_RELATIVE: | |
d61c17ea | 892 | // These are outstanding tls relocs, which are unexpected when linking |
e822f2b1 | 893 | case elfcpp::R_X86_64_TPOFF64: |
2e30d253 ILT |
894 | case elfcpp::R_X86_64_DTPMOD64: |
895 | case elfcpp::R_X86_64_DTPOFF64: | |
896 | case elfcpp::R_X86_64_DTPOFF32: | |
e822f2b1 | 897 | case elfcpp::R_X86_64_TLSDESC: |
2e30d253 ILT |
898 | fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"), |
899 | program_name, object->name().c_str(), r_type); | |
900 | gold_exit(false); | |
901 | break; | |
2e30d253 | 902 | |
d61c17ea | 903 | // These are initial tls relocs, which are expected for global() |
e822f2b1 ILT |
904 | case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct? |
905 | case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct? | |
d61c17ea ILT |
906 | case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct? |
907 | case elfcpp::R_X86_64_TPOFF32: // TODO(csilvers): correct? | |
908 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct? | |
909 | case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct? | |
2e30d253 ILT |
910 | { |
911 | const bool is_final = gsym->final_value_is_known(); | |
912 | r_type = Target_x86_64::optimize_tls_reloc(is_final, r_type); | |
913 | switch (r_type) | |
914 | { | |
d61c17ea | 915 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
2e30d253 ILT |
916 | // FIXME: If generating a shared object, we need to copy |
917 | // this relocation into the object. | |
918 | gold_assert(!parameters->output_is_shared()); | |
919 | break; | |
920 | ||
d61c17ea ILT |
921 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
922 | case elfcpp::R_X86_64_TLSGD: // General Dynamic | |
923 | case elfcpp::R_X86_64_TLSLD: // Local Dynamic | |
924 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: | |
925 | case elfcpp::R_X86_64_TLSDESC_CALL: | |
2e30d253 ILT |
926 | fprintf(stderr, |
927 | _("%s: %s: unsupported reloc %u " | |
928 | "against global symbol %s\n"), | |
929 | program_name, object->name().c_str(), r_type, | |
930 | gsym->name()); | |
931 | break; | |
932 | } | |
933 | } | |
934 | break; | |
2e30d253 | 935 | |
e822f2b1 ILT |
936 | case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct? |
937 | case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct? | |
2e30d253 ILT |
938 | default: |
939 | fprintf(stderr, | |
940 | _("%s: %s: unsupported reloc %u against global symbol %s\n"), | |
941 | program_name, object->name().c_str(), r_type, gsym->name()); | |
942 | break; | |
943 | } | |
944 | } | |
945 | ||
946 | // Scan relocations for a section. | |
947 | ||
948 | void | |
949 | Target_x86_64::scan_relocs(const General_options& options, | |
d61c17ea ILT |
950 | Symbol_table* symtab, |
951 | Layout* layout, | |
952 | Sized_relobj<64, false>* object, | |
953 | unsigned int data_shndx, | |
954 | unsigned int sh_type, | |
955 | const unsigned char* prelocs, | |
956 | size_t reloc_count, | |
957 | size_t local_symbol_count, | |
958 | const unsigned char* plocal_symbols, | |
959 | Symbol** global_symbols) | |
2e30d253 ILT |
960 | { |
961 | if (sh_type == elfcpp::SHT_REL) | |
962 | { | |
963 | fprintf(stderr, _("%s: %s: unsupported REL reloc section\n"), | |
964 | program_name, object->name().c_str()); | |
965 | gold_exit(false); | |
966 | } | |
967 | ||
968 | gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA, | |
969 | Target_x86_64::Scan>( | |
970 | options, | |
971 | symtab, | |
972 | layout, | |
973 | this, | |
974 | object, | |
975 | data_shndx, | |
976 | prelocs, | |
977 | reloc_count, | |
978 | local_symbol_count, | |
979 | plocal_symbols, | |
980 | global_symbols); | |
981 | } | |
982 | ||
983 | // Finalize the sections. | |
984 | ||
985 | void | |
986 | Target_x86_64::do_finalize_sections(Layout* layout) | |
987 | { | |
988 | // Fill in some more dynamic tags. | |
989 | Output_data_dynamic* const odyn = layout->dynamic_data(); | |
990 | if (odyn != NULL) | |
991 | { | |
992 | if (this->got_plt_ != NULL) | |
993 | odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_); | |
994 | ||
995 | if (this->plt_ != NULL) | |
996 | { | |
997 | const Output_data* od = this->plt_->rel_plt(); | |
998 | odyn->add_section_size(elfcpp::DT_PLTRELSZ, od); | |
999 | odyn->add_section_address(elfcpp::DT_JMPREL, od); | |
1000 | odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA); | |
1001 | } | |
1002 | ||
1003 | if (this->rel_dyn_ != NULL) | |
1004 | { | |
1005 | const Output_data* od = this->rel_dyn_; | |
1006 | odyn->add_section_address(elfcpp::DT_RELA, od); | |
e84992bb | 1007 | odyn->add_section_size(elfcpp::DT_RELASZ, od); |
2e30d253 | 1008 | odyn->add_constant(elfcpp::DT_RELAENT, |
e84992bb | 1009 | elfcpp::Elf_sizes<64>::rela_size); |
2e30d253 ILT |
1010 | } |
1011 | ||
1012 | if (!parameters->output_is_shared()) | |
1013 | { | |
1014 | // The value of the DT_DEBUG tag is filled in by the dynamic | |
1015 | // linker at run time, and used by the debugger. | |
1016 | odyn->add_constant(elfcpp::DT_DEBUG, 0); | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | // Emit any relocs we saved in an attempt to avoid generating COPY | |
1021 | // relocs. | |
1022 | if (this->copy_relocs_ == NULL) | |
1023 | return; | |
1024 | if (this->copy_relocs_->any_to_emit()) | |
1025 | { | |
1026 | Reloc_section* rel_dyn = this->rel_dyn_section(layout); | |
1027 | this->copy_relocs_->emit(rel_dyn); | |
1028 | } | |
1029 | delete this->copy_relocs_; | |
1030 | this->copy_relocs_ = NULL; | |
1031 | } | |
1032 | ||
1033 | // Perform a relocation. | |
1034 | ||
1035 | inline bool | |
1036 | Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo, | |
1037 | Target_x86_64* target, | |
1038 | size_t relnum, | |
1039 | const elfcpp::Rela<64, false>& rel, | |
1040 | unsigned int r_type, | |
1041 | const Sized_symbol<64>* gsym, | |
1042 | const Symbol_value<64>* psymval, | |
1043 | unsigned char* view, | |
1044 | elfcpp::Elf_types<64>::Elf_Addr address, | |
1045 | off_t view_size) | |
1046 | { | |
1047 | if (this->skip_call_tls_get_addr_) | |
1048 | { | |
1049 | if (r_type != elfcpp::R_X86_64_PLT32 | |
1050 | || gsym == NULL | |
1051 | || strcmp(gsym->name(), "___tls_get_addr") != 0) | |
1052 | { | |
1053 | fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"), | |
1054 | program_name, | |
1055 | relinfo->location(relnum, rel.get_r_offset()).c_str()); | |
1056 | gold_exit(false); | |
1057 | } | |
1058 | ||
1059 | this->skip_call_tls_get_addr_ = false; | |
1060 | ||
1061 | return false; | |
1062 | } | |
1063 | ||
1064 | // Pick the value to use for symbols defined in shared objects. | |
1065 | Symbol_value<64> symval; | |
1066 | if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset()) | |
1067 | { | |
1068 | symval.set_output_value(target->plt_section()->address() | |
1069 | + gsym->plt_offset()); | |
1070 | psymval = &symval; | |
1071 | } | |
1072 | ||
1073 | const Sized_relobj<64, false>* object = relinfo->object; | |
1074 | const elfcpp::Elf_Xword addend = rel.get_r_addend(); | |
1075 | ||
1076 | switch (r_type) | |
1077 | { | |
1078 | case elfcpp::R_X86_64_NONE: | |
e822f2b1 ILT |
1079 | case elfcpp::R_386_GNU_VTINHERIT: |
1080 | case elfcpp::R_386_GNU_VTENTRY: | |
2e30d253 ILT |
1081 | break; |
1082 | ||
1083 | case elfcpp::R_X86_64_64: | |
1084 | Relocate_functions<64, false>::rela64(view, object, psymval, addend); | |
1085 | break; | |
1086 | ||
1087 | case elfcpp::R_X86_64_PC64: | |
1088 | Relocate_functions<64, false>::pcrela64(view, object, psymval, addend, | |
1089 | address); | |
1090 | break; | |
1091 | ||
1092 | case elfcpp::R_X86_64_32: | |
7bb3655e ILT |
1093 | // FIXME: we need to verify that value + addend fits into 32 bits: |
1094 | // uint64_t x = value + addend; | |
1095 | // x == static_cast<uint64_t>(static_cast<uint32_t>(x)) | |
1096 | // Likewise for other <=32-bit relocations (but see R_X86_64_32S). | |
2e30d253 ILT |
1097 | Relocate_functions<64, false>::rela32(view, object, psymval, addend); |
1098 | break; | |
1099 | ||
1100 | case elfcpp::R_X86_64_32S: | |
7bb3655e ILT |
1101 | // FIXME: we need to verify that value + addend fits into 32 bits: |
1102 | // int64_t x = value + addend; // note this quantity is signed! | |
1103 | // x == static_cast<int64_t>(static_cast<int32_t>(x)) | |
2e30d253 ILT |
1104 | Relocate_functions<64, false>::rela32(view, object, psymval, addend); |
1105 | break; | |
1106 | ||
1107 | case elfcpp::R_X86_64_PC32: | |
1108 | Relocate_functions<64, false>::pcrela32(view, object, psymval, addend, | |
1109 | address); | |
1110 | break; | |
1111 | ||
1112 | case elfcpp::R_X86_64_16: | |
1113 | Relocate_functions<64, false>::rela16(view, object, psymval, addend); | |
1114 | break; | |
1115 | ||
1116 | case elfcpp::R_X86_64_PC16: | |
1117 | Relocate_functions<64, false>::pcrela16(view, object, psymval, addend, | |
1118 | address); | |
1119 | break; | |
1120 | ||
1121 | case elfcpp::R_X86_64_8: | |
1122 | Relocate_functions<64, false>::rela8(view, object, psymval, addend); | |
1123 | break; | |
1124 | ||
1125 | case elfcpp::R_X86_64_PC8: | |
1126 | Relocate_functions<64, false>::pcrela8(view, object, psymval, addend, | |
1127 | address); | |
1128 | break; | |
1129 | ||
1130 | case elfcpp::R_X86_64_PLT32: | |
1131 | gold_assert(gsym->has_plt_offset() | |
1132 | || gsym->final_value_is_known()); | |
1133 | Relocate_functions<64, false>::pcrela32(view, object, psymval, addend, | |
1134 | address); | |
1135 | break; | |
1136 | ||
1137 | case elfcpp::R_X86_64_GOT32: | |
1138 | // Local GOT offsets not yet supported. | |
1139 | gold_assert(gsym); | |
1140 | gold_assert(gsym->has_got_offset()); | |
1141 | Relocate_functions<64, false>::rela32(view, gsym->got_offset(), addend); | |
1142 | break; | |
1143 | ||
e822f2b1 ILT |
1144 | case elfcpp::R_X86_64_GOTPC32: |
1145 | { | |
1146 | gold_assert(gsym); | |
1147 | elfcpp::Elf_types<64>::Elf_Addr value; | |
1148 | value = target->got_section(NULL, NULL)->address(); | |
1149 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); | |
1150 | } | |
1151 | break; | |
1152 | ||
1153 | case elfcpp::R_X86_64_GOT64: | |
1154 | // The ABI doc says "Like GOT64, but indicates a PLT entry is needed." | |
1155 | // Since we always add a PLT entry, this is equivalent. | |
1156 | case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct? | |
1157 | // Local GOT offsets not yet supported. | |
1158 | gold_assert(gsym); | |
1159 | gold_assert(gsym->has_got_offset()); | |
1160 | Relocate_functions<64, false>::rela64(view, gsym->got_offset(), addend); | |
1161 | break; | |
1162 | ||
1163 | case elfcpp::R_X86_64_GOTPC64: | |
1164 | { | |
1165 | gold_assert(gsym); | |
1166 | elfcpp::Elf_types<64>::Elf_Addr value; | |
1167 | value = target->got_section(NULL, NULL)->address(); | |
1168 | Relocate_functions<64, false>::pcrela64(view, value, addend, address); | |
1169 | } | |
1170 | break; | |
1171 | ||
2e30d253 ILT |
1172 | case elfcpp::R_X86_64_GOTOFF64: |
1173 | { | |
1174 | elfcpp::Elf_types<64>::Elf_Addr value; | |
1175 | value = (psymval->value(object, 0) | |
1176 | - target->got_section(NULL, NULL)->address()); | |
1177 | Relocate_functions<64, false>::rela64(view, value, addend); | |
1178 | } | |
1179 | break; | |
1180 | ||
1181 | case elfcpp::R_X86_64_GOTPCREL: | |
1182 | { | |
ff006520 | 1183 | // Local GOT offsets not yet supported. |
2e30d253 | 1184 | gold_assert(gsym); |
ff006520 | 1185 | gold_assert(gsym->has_got_offset()); |
2e30d253 | 1186 | elfcpp::Elf_types<64>::Elf_Addr value; |
2e30d253 | 1187 | value = (target->got_section(NULL, NULL)->address() |
ff006520 | 1188 | + gsym->got_offset()); |
2e30d253 ILT |
1189 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); |
1190 | } | |
1191 | break; | |
1192 | ||
e822f2b1 ILT |
1193 | case elfcpp::R_X86_64_GOTPCREL64: |
1194 | { | |
ff006520 | 1195 | // Local GOT offsets not yet supported. |
e822f2b1 | 1196 | gold_assert(gsym); |
ff006520 | 1197 | gold_assert(gsym->has_got_offset()); |
e822f2b1 | 1198 | elfcpp::Elf_types<64>::Elf_Addr value; |
e822f2b1 | 1199 | value = (target->got_section(NULL, NULL)->address() |
ff006520 | 1200 | + gsym->got_offset()); |
e822f2b1 ILT |
1201 | Relocate_functions<64, false>::pcrela64(view, value, addend, address); |
1202 | } | |
1203 | break; | |
1204 | ||
2e30d253 ILT |
1205 | case elfcpp::R_X86_64_COPY: |
1206 | case elfcpp::R_X86_64_GLOB_DAT: | |
1207 | case elfcpp::R_X86_64_JUMP_SLOT: | |
1208 | case elfcpp::R_X86_64_RELATIVE: | |
d61c17ea | 1209 | // These are outstanding tls relocs, which are unexpected when linking |
2e30d253 | 1210 | case elfcpp::R_X86_64_TPOFF64: |
2e30d253 ILT |
1211 | case elfcpp::R_X86_64_DTPMOD64: |
1212 | case elfcpp::R_X86_64_DTPOFF64: | |
1213 | case elfcpp::R_X86_64_DTPOFF32: | |
1214 | case elfcpp::R_X86_64_TLSDESC: | |
1215 | fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"), | |
1216 | program_name, | |
1217 | relinfo->location(relnum, rel.get_r_offset()).c_str(), | |
1218 | r_type); | |
1219 | gold_exit(false); | |
1220 | break; | |
1221 | ||
d61c17ea | 1222 | // These are initial tls relocs, which are expected when linking |
e822f2b1 ILT |
1223 | case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct? |
1224 | case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct? | |
d61c17ea ILT |
1225 | case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct? |
1226 | case elfcpp::R_X86_64_TPOFF32: // TODO(csilvers): correct? | |
1227 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct? | |
1228 | case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct? | |
2e30d253 ILT |
1229 | this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view, |
1230 | address, view_size); | |
1231 | break; | |
2e30d253 | 1232 | |
e822f2b1 ILT |
1233 | case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct? |
1234 | case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct? | |
1235 | case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): implement me! | |
2e30d253 ILT |
1236 | default: |
1237 | fprintf(stderr, _("%s: %s: unsupported reloc %u\n"), | |
1238 | program_name, | |
1239 | relinfo->location(relnum, rel.get_r_offset()).c_str(), | |
1240 | r_type); | |
7d00dcbd | 1241 | gold_exit(false); |
2e30d253 ILT |
1242 | break; |
1243 | } | |
1244 | ||
1245 | return true; | |
1246 | } | |
1247 | ||
1248 | // Perform a TLS relocation. | |
1249 | ||
1250 | inline void | |
d61c17ea ILT |
1251 | Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo, |
1252 | size_t relnum, | |
1253 | const elfcpp::Rela<64, false>& rel, | |
1254 | unsigned int r_type, | |
1255 | const Sized_symbol<64>* gsym, | |
1256 | const Symbol_value<64>* psymval, | |
1257 | unsigned char* view, | |
2e30d253 | 1258 | elfcpp::Elf_types<64>::Elf_Addr, |
d61c17ea | 1259 | off_t view_size) |
2e30d253 | 1260 | { |
2e30d253 ILT |
1261 | Output_segment* tls_segment = relinfo->layout->tls_segment(); |
1262 | if (tls_segment == NULL) | |
1263 | { | |
1264 | fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"), | |
1265 | program_name, | |
1266 | relinfo->location(relnum, rel.get_r_offset()).c_str()); | |
1267 | gold_exit(false); | |
1268 | } | |
1269 | ||
1270 | elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0); | |
1271 | ||
1272 | const bool is_final = (gsym == NULL | |
1273 | ? !parameters->output_is_shared() | |
1274 | : gsym->final_value_is_known()); | |
1275 | const unsigned int opt_r_type = | |
1276 | Target_x86_64::optimize_tls_reloc(is_final, r_type); | |
1277 | switch (r_type) | |
1278 | { | |
d61c17ea | 1279 | case elfcpp::R_X86_64_TPOFF32: // Local-exec reloc |
2e30d253 | 1280 | value = value - (tls_segment->vaddr() + tls_segment->memsz()); |
d61c17ea | 1281 | Relocate_functions<64, false>::rel32(view, value); |
2e30d253 ILT |
1282 | break; |
1283 | ||
d61c17ea ILT |
1284 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec reloc |
1285 | if (opt_r_type == elfcpp::R_X86_64_TPOFF32) | |
2e30d253 ILT |
1286 | { |
1287 | Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment, | |
d61c17ea ILT |
1288 | rel, r_type, value, view, |
1289 | view_size); | |
2e30d253 ILT |
1290 | break; |
1291 | } | |
1292 | fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"), | |
d61c17ea ILT |
1293 | program_name, |
1294 | relinfo->location(relnum, rel.get_r_offset()).c_str(), | |
1295 | r_type); | |
7d00dcbd | 1296 | gold_exit(false); |
2e30d253 ILT |
1297 | break; |
1298 | ||
d61c17ea ILT |
1299 | case elfcpp::R_X86_64_TLSGD: |
1300 | if (opt_r_type == elfcpp::R_X86_64_TPOFF32) | |
2e30d253 ILT |
1301 | { |
1302 | this->tls_gd_to_le(relinfo, relnum, tls_segment, | |
1303 | rel, r_type, value, view, | |
1304 | view_size); | |
1305 | break; | |
1306 | } | |
1307 | fprintf(stderr, _("%s: %s: unsupported reloc %u\n"), | |
1308 | program_name, | |
1309 | relinfo->location(relnum, rel.get_r_offset()).c_str(), | |
1310 | r_type); | |
7d00dcbd | 1311 | gold_exit(false); |
2e30d253 ILT |
1312 | break; |
1313 | ||
d61c17ea | 1314 | case elfcpp::R_X86_64_TLSLD: |
2e30d253 ILT |
1315 | fprintf(stderr, _("%s: %s: unsupported reloc %u\n"), |
1316 | program_name, | |
1317 | relinfo->location(relnum, rel.get_r_offset()).c_str(), | |
1318 | r_type); | |
7d00dcbd | 1319 | gold_exit(false); |
2e30d253 ILT |
1320 | break; |
1321 | } | |
2e30d253 ILT |
1322 | } |
1323 | ||
1324 | // Do a relocation in which we convert a TLS Initial-Exec to a | |
1325 | // Local-Exec. | |
d61c17ea | 1326 | // TODO(csilvers): verify this is right. |
2e30d253 ILT |
1327 | |
1328 | inline void | |
d61c17ea ILT |
1329 | Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo, |
1330 | size_t relnum, | |
1331 | Output_segment* tls_segment, | |
1332 | const elfcpp::Rela<64, false>& rel, | |
1333 | unsigned int, | |
1334 | elfcpp::Elf_types<64>::Elf_Addr value, | |
1335 | unsigned char* view, | |
1336 | off_t view_size) | |
2e30d253 | 1337 | { |
2e30d253 ILT |
1338 | // We have to actually change the instructions, which means that we |
1339 | // need to examine the opcodes to figure out which instruction we | |
1340 | // are looking at. | |
2e30d253 | 1341 | |
d61c17ea ILT |
1342 | // movl %gs:XX,%eax ==> movl $YY,%eax |
1343 | // movl %gs:XX,%reg ==> movl $YY,%reg | |
1344 | // addl %gs:XX,%reg ==> addl $YY,%reg | |
1345 | Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -1); | |
1346 | Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 4); | |
1347 | ||
1348 | unsigned char op1 = view[-1]; | |
1349 | if (op1 == 0xa1) | |
1350 | { | |
1351 | // movl XX,%eax ==> movl $YY,%eax | |
1352 | view[-1] = 0xb8; | |
2e30d253 ILT |
1353 | } |
1354 | else | |
1355 | { | |
d61c17ea ILT |
1356 | Target_x86_64::Relocate::check_range(relinfo, relnum, rel, |
1357 | view_size, -2); | |
2e30d253 | 1358 | |
2e30d253 | 1359 | unsigned char op2 = view[-2]; |
2e30d253 | 1360 | if (op2 == 0x8b) |
d61c17ea ILT |
1361 | { |
1362 | // movl XX,%reg ==> movl $YY,%reg | |
1363 | Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, | |
1364 | (op1 & 0xc7) == 0x05); | |
1365 | view[-2] = 0xc7; | |
1366 | view[-1] = 0xc0 | ((op1 >> 3) & 7); | |
1367 | } | |
2e30d253 | 1368 | else if (op2 == 0x03) |
d61c17ea ILT |
1369 | { |
1370 | // addl XX,%reg ==> addl $YY,%reg | |
1371 | Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, | |
1372 | (op1 & 0xc7) == 0x05); | |
1373 | view[-2] = 0x81; | |
1374 | view[-1] = 0xc0 | ((op1 >> 3) & 7); | |
1375 | } | |
2e30d253 | 1376 | else |
d61c17ea | 1377 | Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, 0); |
2e30d253 ILT |
1378 | } |
1379 | ||
d61c17ea ILT |
1380 | value = value - (tls_segment->vaddr() + tls_segment->memsz()); |
1381 | Relocate_functions<64, false>::rel32(view, value); | |
2e30d253 ILT |
1382 | } |
1383 | ||
1384 | // Do a relocation in which we convert a TLS Global-Dynamic to a | |
1385 | // Local-Exec. | |
d61c17ea | 1386 | // TODO(csilvers): verify this is right. |
2e30d253 ILT |
1387 | |
1388 | inline void | |
d61c17ea ILT |
1389 | Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo, |
1390 | size_t relnum, | |
1391 | Output_segment* tls_segment, | |
1392 | const elfcpp::Rela<64, false>& rel, | |
1393 | unsigned int, | |
1394 | elfcpp::Elf_types<64>::Elf_Addr value, | |
1395 | unsigned char* view, | |
1396 | off_t view_size) | |
2e30d253 | 1397 | { |
2e30d253 ILT |
1398 | // leal foo(,%reg,1),%eax; call ___tls_get_addr |
1399 | // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax | |
1400 | // leal foo(%reg),%eax; call ___tls_get_addr | |
1401 | // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax | |
1402 | ||
1403 | Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -2); | |
1404 | Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 9); | |
1405 | ||
1406 | unsigned char op1 = view[-1]; | |
1407 | unsigned char op2 = view[-2]; | |
1408 | ||
1409 | Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, | |
d61c17ea | 1410 | op2 == 0x8d || op2 == 0x04); |
2e30d253 | 1411 | Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, |
d61c17ea | 1412 | view[4] == 0xe8); |
2e30d253 ILT |
1413 | |
1414 | int roff = 5; | |
1415 | ||
1416 | if (op2 == 0x04) | |
1417 | { | |
1418 | Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -3); | |
1419 | Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, | |
d61c17ea | 1420 | view[-3] == 0x8d); |
2e30d253 | 1421 | Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, |
d61c17ea ILT |
1422 | ((op1 & 0xc7) == 0x05 |
1423 | && op1 != (4 << 3))); | |
2e30d253 ILT |
1424 | memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12); |
1425 | } | |
1426 | else | |
1427 | { | |
1428 | Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, | |
d61c17ea ILT |
1429 | (op1 & 0xf8) == 0x80 && (op1 & 7) != 4); |
1430 | if (static_cast<off_t>(rel.get_r_offset() + 9) < view_size | |
1431 | && view[9] == 0x90) | |
2e30d253 ILT |
1432 | { |
1433 | // There is a trailing nop. Use the size byte subl. | |
1434 | memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12); | |
1435 | roff = 6; | |
1436 | } | |
1437 | else | |
1438 | { | |
1439 | // Use the five byte subl. | |
1440 | memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11); | |
1441 | } | |
1442 | } | |
1443 | ||
1444 | value = tls_segment->vaddr() + tls_segment->memsz() - value; | |
d61c17ea | 1445 | Relocate_functions<64, false>::rel32(view + roff, value); |
2e30d253 ILT |
1446 | |
1447 | // The next reloc should be a PLT32 reloc against __tls_get_addr. | |
1448 | // We can skip it. | |
1449 | this->skip_call_tls_get_addr_ = true; | |
2e30d253 ILT |
1450 | } |
1451 | ||
1452 | // Check the range for a TLS relocation. | |
1453 | ||
1454 | inline void | |
1455 | Target_x86_64::Relocate::check_range(const Relocate_info<64, false>* relinfo, | |
d61c17ea ILT |
1456 | size_t relnum, |
1457 | const elfcpp::Rela<64, false>& rel, | |
1458 | off_t view_size, off_t off) | |
2e30d253 ILT |
1459 | { |
1460 | off_t offset = rel.get_r_offset() + off; | |
1461 | if (offset < 0 || offset > view_size) | |
1462 | { | |
1463 | fprintf(stderr, _("%s: %s: TLS relocation out of range\n"), | |
1464 | program_name, | |
1465 | relinfo->location(relnum, rel.get_r_offset()).c_str()); | |
1466 | gold_exit(false); | |
1467 | } | |
1468 | } | |
1469 | ||
1470 | // Check the validity of a TLS relocation. This is like assert. | |
1471 | ||
1472 | inline void | |
1473 | Target_x86_64::Relocate::check_tls(const Relocate_info<64, false>* relinfo, | |
d61c17ea ILT |
1474 | size_t relnum, |
1475 | const elfcpp::Rela<64, false>& rel, | |
1476 | bool valid) | |
2e30d253 ILT |
1477 | { |
1478 | if (!valid) | |
1479 | { | |
1480 | fprintf(stderr, | |
1481 | _("%s: %s: TLS relocation against invalid instruction\n"), | |
1482 | program_name, | |
1483 | relinfo->location(relnum, rel.get_r_offset()).c_str()); | |
1484 | gold_exit(false); | |
1485 | } | |
1486 | } | |
1487 | ||
1488 | // Relocate section data. | |
1489 | ||
1490 | void | |
1491 | Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo, | |
d61c17ea ILT |
1492 | unsigned int sh_type, |
1493 | const unsigned char* prelocs, | |
1494 | size_t reloc_count, | |
1495 | unsigned char* view, | |
1496 | elfcpp::Elf_types<64>::Elf_Addr address, | |
1497 | off_t view_size) | |
2e30d253 ILT |
1498 | { |
1499 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
1500 | ||
1501 | gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA, | |
1502 | Target_x86_64::Relocate>( | |
1503 | relinfo, | |
1504 | this, | |
1505 | prelocs, | |
1506 | reloc_count, | |
1507 | view, | |
1508 | address, | |
1509 | view_size); | |
1510 | } | |
1511 | ||
4fb6c25d ILT |
1512 | // Return the value to use for a dynamic which requires special |
1513 | // treatment. This is how we support equality comparisons of function | |
1514 | // pointers across shared library boundaries, as described in the | |
1515 | // processor specific ABI supplement. | |
1516 | ||
1517 | uint64_t | |
1518 | Target_x86_64::do_dynsym_value(const Symbol* gsym) const | |
1519 | { | |
1520 | gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); | |
1521 | return this->plt_section()->address() + gsym->plt_offset(); | |
1522 | } | |
1523 | ||
2e30d253 ILT |
1524 | // Return a string used to fill a code section with nops to take up |
1525 | // the specified length. | |
1526 | ||
1527 | std::string | |
1528 | Target_x86_64::do_code_fill(off_t length) | |
1529 | { | |
1530 | if (length >= 16) | |
1531 | { | |
1532 | // Build a jmpq instruction to skip over the bytes. | |
1533 | unsigned char jmp[5]; | |
1534 | jmp[0] = 0xe9; | |
1535 | elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5); | |
1536 | return (std::string(reinterpret_cast<char*>(&jmp[0]), 5) | |
1537 | + std::string(length - 5, '\0')); | |
1538 | } | |
1539 | ||
1540 | // Nop sequences of various lengths. | |
1541 | const char nop1[1] = { 0x90 }; // nop | |
1542 | const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax | |
1543 | const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi | |
1544 | const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi | |
1545 | const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop | |
1546 | 0x00 }; // leal 0(%esi,1),%esi | |
1547 | const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi | |
1548 | 0x00, 0x00 }; | |
1549 | const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi | |
1550 | 0x00, 0x00, 0x00 }; | |
1551 | const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop | |
1552 | 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi | |
1553 | const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi | |
1554 | 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi | |
1555 | 0x00 }; | |
1556 | const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi | |
1557 | 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi | |
1558 | 0x00, 0x00 }; | |
1559 | const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi | |
1560 | 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi | |
1561 | 0x00, 0x00, 0x00 }; | |
1562 | const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi | |
1563 | 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi | |
1564 | 0x00, 0x00, 0x00, 0x00 }; | |
1565 | const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi | |
1566 | 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi | |
1567 | 0x27, 0x00, 0x00, 0x00, | |
1568 | 0x00 }; | |
1569 | const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi | |
1570 | 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi | |
1571 | 0xbc, 0x27, 0x00, 0x00, | |
1572 | 0x00, 0x00 }; | |
1573 | const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15 | |
1574 | 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,... | |
1575 | 0x90, 0x90, 0x90, 0x90, | |
1576 | 0x90, 0x90, 0x90 }; | |
1577 | ||
1578 | const char* nops[16] = { | |
1579 | NULL, | |
1580 | nop1, nop2, nop3, nop4, nop5, nop6, nop7, | |
1581 | nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15 | |
1582 | }; | |
1583 | ||
1584 | return std::string(nops[length], length); | |
1585 | } | |
1586 | ||
1587 | // The selector for x86_64 object files. | |
1588 | ||
1589 | class Target_selector_x86_64 : public Target_selector | |
1590 | { | |
1591 | public: | |
1592 | Target_selector_x86_64() | |
1593 | : Target_selector(elfcpp::EM_X86_64, 64, false) | |
1594 | { } | |
1595 | ||
1596 | Target* | |
1597 | recognize(int machine, int osabi, int abiversion); | |
1598 | ||
1599 | private: | |
1600 | Target_x86_64* target_; | |
1601 | }; | |
1602 | ||
1603 | // Recognize an x86_64 object file when we already know that the machine | |
1604 | // number is EM_X86_64. | |
1605 | ||
1606 | Target* | |
1607 | Target_selector_x86_64::recognize(int, int, int) | |
1608 | { | |
1609 | if (this->target_ == NULL) | |
1610 | this->target_ = new Target_x86_64(); | |
1611 | return this->target_; | |
1612 | } | |
1613 | ||
1614 | Target_selector_x86_64 target_selector_x86_64; | |
1615 | ||
1616 | } // End anonymous namespace. |