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