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