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