Commit | Line | Data |
---|---|---|
2e30d253 ILT |
1 | // x86_64.cc -- x86_64 target support for gold. |
2 | ||
b3ce541e | 3 | // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
2e30d253 ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8b105e34 ILT |
8 | // This program is free software; you can redistribute it and/or modify |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
2e30d253 ILT |
11 | // (at your option) any later version. |
12 | ||
8b105e34 ILT |
13 | // This program is distributed in the hope that it will be useful, |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
2e30d253 ILT |
22 | |
23 | #include "gold.h" | |
24 | ||
25 | #include <cstring> | |
26 | ||
27 | #include "elfcpp.h" | |
07a60597 | 28 | #include "dwarf.h" |
2e30d253 ILT |
29 | #include "parameters.h" |
30 | #include "reloc.h" | |
31 | #include "x86_64.h" | |
32 | #include "object.h" | |
33 | #include "symtab.h" | |
34 | #include "layout.h" | |
35 | #include "output.h" | |
12c0daef | 36 | #include "copy-relocs.h" |
2e30d253 ILT |
37 | #include "target.h" |
38 | #include "target-reloc.h" | |
39 | #include "target-select.h" | |
e041f13d | 40 | #include "tls.h" |
36959681 | 41 | #include "freebsd.h" |
f345227a | 42 | #include "gc.h" |
21bb3914 | 43 | #include "icf.h" |
2e30d253 ILT |
44 | |
45 | namespace | |
46 | { | |
47 | ||
48 | using namespace gold; | |
49 | ||
7223e9ca ILT |
50 | // A class to handle the PLT data. |
51 | ||
52 | class Output_data_plt_x86_64 : public Output_section_data | |
53 | { | |
54 | public: | |
55 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section; | |
56 | ||
67181c72 | 57 | Output_data_plt_x86_64(Layout* layout, Output_data_got<64, false>* got, |
4829d394 | 58 | Output_data_space* got_plt, |
67181c72 | 59 | Output_data_space* got_irelative) |
7d172687 ILT |
60 | : Output_section_data(16), layout_(layout), tlsdesc_rel_(NULL), |
61 | irelative_rel_(NULL), got_(got), got_plt_(got_plt), | |
62 | got_irelative_(got_irelative), count_(0), irelative_count_(0), | |
63 | tlsdesc_got_offset_(-1U), free_list_() | |
67181c72 ILT |
64 | { this->init(layout); } |
65 | ||
66 | Output_data_plt_x86_64(Layout* layout, Output_data_got<64, false>* got, | |
67 | Output_data_space* got_plt, | |
68 | Output_data_space* got_irelative, | |
4829d394 | 69 | unsigned int plt_count) |
07a60597 | 70 | : Output_section_data((plt_count + 1) * plt_entry_size, 16, false), |
7d172687 ILT |
71 | layout_(layout), tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got), |
72 | got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count), | |
73 | irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_() | |
4829d394 | 74 | { |
67181c72 | 75 | this->init(layout); |
4829d394 CC |
76 | |
77 | // Initialize the free list and reserve the first entry. | |
78 | this->free_list_.init((plt_count + 1) * plt_entry_size, false); | |
79 | this->free_list_.remove(0, plt_entry_size); | |
80 | } | |
81 | ||
82 | // Initialize the PLT section. | |
83 | void | |
67181c72 | 84 | init(Layout* layout); |
7223e9ca ILT |
85 | |
86 | // Add an entry to the PLT. | |
87 | void | |
67181c72 | 88 | add_entry(Symbol_table*, Layout*, Symbol* gsym); |
7223e9ca ILT |
89 | |
90 | // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. | |
91 | unsigned int | |
67181c72 ILT |
92 | add_local_ifunc_entry(Symbol_table* symtab, Layout*, |
93 | Sized_relobj_file<64, false>* relobj, | |
7223e9ca ILT |
94 | unsigned int local_sym_index); |
95 | ||
4829d394 CC |
96 | // Add the relocation for a PLT entry. |
97 | void | |
67181c72 ILT |
98 | add_relocation(Symbol_table*, Layout*, Symbol* gsym, |
99 | unsigned int got_offset); | |
4829d394 | 100 | |
7223e9ca ILT |
101 | // Add the reserved TLSDESC_PLT entry to the PLT. |
102 | void | |
103 | reserve_tlsdesc_entry(unsigned int got_offset) | |
104 | { this->tlsdesc_got_offset_ = got_offset; } | |
105 | ||
106 | // Return true if a TLSDESC_PLT entry has been reserved. | |
107 | bool | |
108 | has_tlsdesc_entry() const | |
109 | { return this->tlsdesc_got_offset_ != -1U; } | |
110 | ||
111 | // Return the GOT offset for the reserved TLSDESC_PLT entry. | |
112 | unsigned int | |
113 | get_tlsdesc_got_offset() const | |
114 | { return this->tlsdesc_got_offset_; } | |
115 | ||
116 | // Return the offset of the reserved TLSDESC_PLT entry. | |
117 | unsigned int | |
118 | get_tlsdesc_plt_offset() const | |
67181c72 | 119 | { return (this->count_ + this->irelative_count_ + 1) * plt_entry_size; } |
7223e9ca ILT |
120 | |
121 | // Return the .rela.plt section data. | |
122 | Reloc_section* | |
123 | rela_plt() | |
124 | { return this->rel_; } | |
125 | ||
126 | // Return where the TLSDESC relocations should go. | |
127 | Reloc_section* | |
128 | rela_tlsdesc(Layout*); | |
129 | ||
67181c72 ILT |
130 | // Return where the IRELATIVE relocations should go in the PLT |
131 | // relocations. | |
132 | Reloc_section* | |
133 | rela_irelative(Symbol_table*, Layout*); | |
134 | ||
135 | // Return whether we created a section for IRELATIVE relocations. | |
136 | bool | |
137 | has_irelative_section() const | |
138 | { return this->irelative_rel_ != NULL; } | |
139 | ||
7223e9ca ILT |
140 | // Return the number of PLT entries. |
141 | unsigned int | |
142 | entry_count() const | |
67181c72 | 143 | { return this->count_ + this->irelative_count_; } |
7223e9ca ILT |
144 | |
145 | // Return the offset of the first non-reserved PLT entry. | |
146 | static unsigned int | |
147 | first_plt_entry_offset() | |
148 | { return plt_entry_size; } | |
149 | ||
150 | // Return the size of a PLT entry. | |
151 | static unsigned int | |
152 | get_plt_entry_size() | |
153 | { return plt_entry_size; } | |
154 | ||
4829d394 CC |
155 | // Reserve a slot in the PLT for an existing symbol in an incremental update. |
156 | void | |
157 | reserve_slot(unsigned int plt_index) | |
158 | { | |
159 | this->free_list_.remove((plt_index + 1) * plt_entry_size, | |
160 | (plt_index + 2) * plt_entry_size); | |
161 | } | |
162 | ||
67181c72 ILT |
163 | // Return the PLT address to use for a global symbol. |
164 | uint64_t | |
165 | address_for_global(const Symbol*); | |
166 | ||
167 | // Return the PLT address to use for a local symbol. | |
168 | uint64_t | |
169 | address_for_local(const Relobj*, unsigned int symndx); | |
170 | ||
7223e9ca ILT |
171 | protected: |
172 | void | |
173 | do_adjust_output_section(Output_section* os); | |
174 | ||
175 | // Write to a map file. | |
176 | void | |
177 | do_print_to_mapfile(Mapfile* mapfile) const | |
178 | { mapfile->print_output_data(this, _("** PLT")); } | |
179 | ||
180 | private: | |
181 | // The size of an entry in the PLT. | |
182 | static const int plt_entry_size = 16; | |
183 | ||
184 | // The first entry in the PLT. | |
185 | // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same | |
186 | // procedure linkage table for both programs and shared objects." | |
07a60597 | 187 | static const unsigned char first_plt_entry[plt_entry_size]; |
7223e9ca ILT |
188 | |
189 | // Other entries in the PLT for an executable. | |
07a60597 | 190 | static const unsigned char plt_entry[plt_entry_size]; |
7223e9ca ILT |
191 | |
192 | // The reserved TLSDESC entry in the PLT for an executable. | |
07a60597 ILT |
193 | static const unsigned char tlsdesc_plt_entry[plt_entry_size]; |
194 | ||
195 | // The .eh_frame unwind information for the PLT. | |
196 | static const int plt_eh_frame_cie_size = 16; | |
197 | static const int plt_eh_frame_fde_size = 32; | |
198 | static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size]; | |
199 | static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size]; | |
7223e9ca ILT |
200 | |
201 | // Set the final size. | |
202 | void | |
203 | set_final_data_size(); | |
204 | ||
205 | // Write out the PLT data. | |
206 | void | |
207 | do_write(Output_file*); | |
208 | ||
7d172687 ILT |
209 | // A pointer to the Layout class, so that we can find the .dynamic |
210 | // section when we write out the GOT PLT section. | |
211 | Layout* layout_; | |
7223e9ca ILT |
212 | // The reloc section. |
213 | Reloc_section* rel_; | |
214 | // The TLSDESC relocs, if necessary. These must follow the regular | |
215 | // PLT relocs. | |
216 | Reloc_section* tlsdesc_rel_; | |
67181c72 ILT |
217 | // The IRELATIVE relocs, if necessary. These must follow the |
218 | // regular PLT relocations and the TLSDESC relocations. | |
219 | Reloc_section* irelative_rel_; | |
7223e9ca ILT |
220 | // The .got section. |
221 | Output_data_got<64, false>* got_; | |
222 | // The .got.plt section. | |
223 | Output_data_space* got_plt_; | |
67181c72 ILT |
224 | // The part of the .got.plt section used for IRELATIVE relocs. |
225 | Output_data_space* got_irelative_; | |
7223e9ca ILT |
226 | // The number of PLT entries. |
227 | unsigned int count_; | |
67181c72 ILT |
228 | // Number of PLT entries with R_X86_64_IRELATIVE relocs. These |
229 | // follow the regular PLT entries. | |
230 | unsigned int irelative_count_; | |
7223e9ca ILT |
231 | // Offset of the reserved TLSDESC_GOT entry when needed. |
232 | unsigned int tlsdesc_got_offset_; | |
4829d394 CC |
233 | // List of available regions within the section, for incremental |
234 | // update links. | |
235 | Free_list free_list_; | |
7223e9ca | 236 | }; |
2e30d253 ILT |
237 | |
238 | // The x86_64 target class. | |
d61c17ea ILT |
239 | // See the ABI at |
240 | // http://www.x86-64.org/documentation/abi.pdf | |
241 | // TLS info comes from | |
242 | // http://people.redhat.com/drepper/tls.pdf | |
0ffd9845 | 243 | // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt |
2e30d253 | 244 | |
200b2bb9 | 245 | class Target_x86_64 : public Sized_target<64, false> |
2e30d253 ILT |
246 | { |
247 | public: | |
e822f2b1 ILT |
248 | // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures |
249 | // uses only Elf64_Rela relocation entries with explicit addends." | |
2e30d253 ILT |
250 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section; |
251 | ||
252 | Target_x86_64() | |
200b2bb9 | 253 | : Sized_target<64, false>(&x86_64_info), |
67181c72 ILT |
254 | got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL), |
255 | got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL), | |
256 | rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY), | |
257 | dynbss_(NULL), got_mod_index_offset_(-1U), tlsdesc_reloc_info_(), | |
e291e7b9 | 258 | tls_base_symbol_defined_(false) |
2e30d253 ILT |
259 | { } |
260 | ||
8a5e3e08 ILT |
261 | // Hook for a new output section. |
262 | void | |
263 | do_new_output_section(Output_section*) const; | |
264 | ||
6d03d481 ST |
265 | // Scan the relocations to look for symbol adjustments. |
266 | void | |
ad0f2072 | 267 | gc_process_relocs(Symbol_table* symtab, |
6d03d481 | 268 | Layout* layout, |
6fa2a40b | 269 | Sized_relobj_file<64, false>* object, |
6d03d481 ST |
270 | unsigned int data_shndx, |
271 | unsigned int sh_type, | |
272 | const unsigned char* prelocs, | |
273 | size_t reloc_count, | |
274 | Output_section* output_section, | |
275 | bool needs_special_offset_handling, | |
276 | size_t local_symbol_count, | |
277 | const unsigned char* plocal_symbols); | |
278 | ||
2e30d253 ILT |
279 | // Scan the relocations to look for symbol adjustments. |
280 | void | |
ad0f2072 | 281 | scan_relocs(Symbol_table* symtab, |
2e30d253 | 282 | Layout* layout, |
6fa2a40b | 283 | Sized_relobj_file<64, false>* object, |
2e30d253 ILT |
284 | unsigned int data_shndx, |
285 | unsigned int sh_type, | |
286 | const unsigned char* prelocs, | |
287 | size_t reloc_count, | |
730cdc88 ILT |
288 | Output_section* output_section, |
289 | bool needs_special_offset_handling, | |
2e30d253 | 290 | size_t local_symbol_count, |
730cdc88 | 291 | const unsigned char* plocal_symbols); |
2e30d253 ILT |
292 | |
293 | // Finalize the sections. | |
294 | void | |
f59f41f3 | 295 | do_finalize_sections(Layout*, const Input_objects*, Symbol_table*); |
2e30d253 | 296 | |
4fb6c25d ILT |
297 | // Return the value to use for a dynamic which requires special |
298 | // treatment. | |
299 | uint64_t | |
300 | do_dynsym_value(const Symbol*) const; | |
301 | ||
2e30d253 ILT |
302 | // Relocate a section. |
303 | void | |
304 | relocate_section(const Relocate_info<64, false>*, | |
305 | unsigned int sh_type, | |
306 | const unsigned char* prelocs, | |
307 | size_t reloc_count, | |
730cdc88 ILT |
308 | Output_section* output_section, |
309 | bool needs_special_offset_handling, | |
2e30d253 ILT |
310 | unsigned char* view, |
311 | elfcpp::Elf_types<64>::Elf_Addr view_address, | |
364c7fa5 ILT |
312 | section_size_type view_size, |
313 | const Reloc_symbol_changes*); | |
2e30d253 | 314 | |
6a74a719 ILT |
315 | // Scan the relocs during a relocatable link. |
316 | void | |
ad0f2072 | 317 | scan_relocatable_relocs(Symbol_table* symtab, |
6a74a719 | 318 | Layout* layout, |
6fa2a40b | 319 | Sized_relobj_file<64, false>* object, |
6a74a719 ILT |
320 | unsigned int data_shndx, |
321 | unsigned int sh_type, | |
322 | const unsigned char* prelocs, | |
323 | size_t reloc_count, | |
324 | Output_section* output_section, | |
325 | bool needs_special_offset_handling, | |
326 | size_t local_symbol_count, | |
327 | const unsigned char* plocal_symbols, | |
328 | Relocatable_relocs*); | |
329 | ||
330 | // Relocate a section during a relocatable link. | |
331 | void | |
332 | relocate_for_relocatable(const Relocate_info<64, false>*, | |
333 | unsigned int sh_type, | |
334 | const unsigned char* prelocs, | |
335 | size_t reloc_count, | |
336 | Output_section* output_section, | |
337 | off_t offset_in_output_section, | |
338 | const Relocatable_relocs*, | |
339 | unsigned char* view, | |
340 | elfcpp::Elf_types<64>::Elf_Addr view_address, | |
341 | section_size_type view_size, | |
342 | unsigned char* reloc_view, | |
343 | section_size_type reloc_view_size); | |
344 | ||
2e30d253 ILT |
345 | // Return a string used to fill a code section with nops. |
346 | std::string | |
8851ecca | 347 | do_code_fill(section_size_type length) const; |
2e30d253 | 348 | |
9a2d6984 ILT |
349 | // Return whether SYM is defined by the ABI. |
350 | bool | |
9c2d0ef9 | 351 | do_is_defined_by_abi(const Symbol* sym) const |
9a2d6984 ILT |
352 | { return strcmp(sym->name(), "__tls_get_addr") == 0; } |
353 | ||
e291e7b9 ILT |
354 | // Return the symbol index to use for a target specific relocation. |
355 | // The only target specific relocation is R_X86_64_TLSDESC for a | |
356 | // local symbol, which is an absolute reloc. | |
357 | unsigned int | |
358 | do_reloc_symbol_index(void*, unsigned int r_type) const | |
359 | { | |
360 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC); | |
361 | return 0; | |
362 | } | |
363 | ||
364 | // Return the addend to use for a target specific relocation. | |
365 | uint64_t | |
366 | do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const; | |
367 | ||
7223e9ca | 368 | // Return the PLT section. |
67181c72 ILT |
369 | uint64_t |
370 | do_plt_address_for_global(const Symbol* gsym) const | |
371 | { return this->plt_section()->address_for_global(gsym); } | |
7223e9ca | 372 | |
67181c72 ILT |
373 | uint64_t |
374 | do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const | |
375 | { return this->plt_section()->address_for_local(relobj, symndx); } | |
7223e9ca | 376 | |
b3ce541e ILT |
377 | // This function should be defined in targets that can use relocation |
378 | // types to determine (implemented in local_reloc_may_be_function_pointer | |
379 | // and global_reloc_may_be_function_pointer) | |
380 | // if a function's pointer is taken. ICF uses this in safe mode to only | |
381 | // fold those functions whose pointer is defintely not taken. For x86_64 | |
382 | // pie binaries, safe ICF cannot be done by looking at relocation types. | |
383 | bool | |
384 | do_can_check_for_function_pointers() const | |
385 | { return !parameters->options().pie(); } | |
386 | ||
02d7cd44 ILT |
387 | // Return the base for a DW_EH_PE_datarel encoding. |
388 | uint64_t | |
389 | do_ehframe_datarel_base() const; | |
390 | ||
9b547ce6 | 391 | // Adjust -fsplit-stack code which calls non-split-stack code. |
364c7fa5 ILT |
392 | void |
393 | do_calls_non_split(Relobj* object, unsigned int shndx, | |
394 | section_offset_type fnoffset, section_size_type fnsize, | |
395 | unsigned char* view, section_size_type view_size, | |
396 | std::string* from, std::string* to) const; | |
397 | ||
96f2030e | 398 | // Return the size of the GOT section. |
fe8718a4 | 399 | section_size_type |
0e70b911 | 400 | got_size() const |
96f2030e ILT |
401 | { |
402 | gold_assert(this->got_ != NULL); | |
403 | return this->got_->data_size(); | |
404 | } | |
405 | ||
0e70b911 CC |
406 | // Return the number of entries in the GOT. |
407 | unsigned int | |
408 | got_entry_count() const | |
409 | { | |
410 | if (this->got_ == NULL) | |
411 | return 0; | |
412 | return this->got_size() / 8; | |
413 | } | |
414 | ||
415 | // Return the number of entries in the PLT. | |
416 | unsigned int | |
417 | plt_entry_count() const; | |
418 | ||
419 | // Return the offset of the first non-reserved PLT entry. | |
420 | unsigned int | |
421 | first_plt_entry_offset() const; | |
422 | ||
423 | // Return the size of each PLT entry. | |
424 | unsigned int | |
425 | plt_entry_size() const; | |
426 | ||
4829d394 CC |
427 | // Create the GOT section for an incremental update. |
428 | Output_data_got<64, false>* | |
429 | init_got_plt_for_update(Symbol_table* symtab, | |
430 | Layout* layout, | |
431 | unsigned int got_count, | |
432 | unsigned int plt_count); | |
433 | ||
6fa2a40b CC |
434 | // Reserve a GOT entry for a local symbol, and regenerate any |
435 | // necessary dynamic relocations. | |
436 | void | |
437 | reserve_local_got_entry(unsigned int got_index, | |
438 | Sized_relobj<64, false>* obj, | |
439 | unsigned int r_sym, | |
440 | unsigned int got_type); | |
441 | ||
442 | // Reserve a GOT entry for a global symbol, and regenerate any | |
443 | // necessary dynamic relocations. | |
444 | void | |
445 | reserve_global_got_entry(unsigned int got_index, Symbol* gsym, | |
446 | unsigned int got_type); | |
447 | ||
4829d394 | 448 | // Register an existing PLT entry for a global symbol. |
4829d394 | 449 | void |
67181c72 ILT |
450 | register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index, |
451 | Symbol* gsym); | |
4829d394 | 452 | |
26d3c67d CC |
453 | // Force a COPY relocation for a given symbol. |
454 | void | |
455 | emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t); | |
456 | ||
94a3fc8b CC |
457 | // Apply an incremental relocation. |
458 | void | |
459 | apply_relocation(const Relocate_info<64, false>* relinfo, | |
460 | elfcpp::Elf_types<64>::Elf_Addr r_offset, | |
461 | unsigned int r_type, | |
462 | elfcpp::Elf_types<64>::Elf_Swxword r_addend, | |
463 | const Symbol* gsym, | |
464 | unsigned char* view, | |
465 | elfcpp::Elf_types<64>::Elf_Addr address, | |
466 | section_size_type view_size); | |
467 | ||
e291e7b9 ILT |
468 | // Add a new reloc argument, returning the index in the vector. |
469 | size_t | |
6fa2a40b | 470 | add_tlsdesc_info(Sized_relobj_file<64, false>* object, unsigned int r_sym) |
e291e7b9 ILT |
471 | { |
472 | this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym)); | |
473 | return this->tlsdesc_reloc_info_.size() - 1; | |
474 | } | |
475 | ||
2e30d253 ILT |
476 | private: |
477 | // The class which scans relocations. | |
a036edd8 | 478 | class Scan |
2e30d253 | 479 | { |
a036edd8 ILT |
480 | public: |
481 | Scan() | |
482 | : issued_non_pic_error_(false) | |
483 | { } | |
484 | ||
95a2c8d6 RS |
485 | static inline int |
486 | get_reference_flags(unsigned int r_type); | |
487 | ||
2e30d253 | 488 | inline void |
ad0f2072 | 489 | local(Symbol_table* symtab, Layout* layout, Target_x86_64* target, |
6fa2a40b | 490 | Sized_relobj_file<64, false>* object, |
2e30d253 | 491 | unsigned int data_shndx, |
07f397ab | 492 | Output_section* output_section, |
2e30d253 ILT |
493 | const elfcpp::Rela<64, false>& reloc, unsigned int r_type, |
494 | const elfcpp::Sym<64, false>& lsym); | |
495 | ||
496 | inline void | |
ad0f2072 | 497 | global(Symbol_table* symtab, Layout* layout, Target_x86_64* target, |
6fa2a40b | 498 | Sized_relobj_file<64, false>* object, |
2e30d253 | 499 | unsigned int data_shndx, |
07f397ab | 500 | Output_section* output_section, |
2e30d253 ILT |
501 | const elfcpp::Rela<64, false>& reloc, unsigned int r_type, |
502 | Symbol* gsym); | |
e041f13d | 503 | |
21bb3914 ST |
504 | inline bool |
505 | local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout, | |
506 | Target_x86_64* target, | |
6fa2a40b | 507 | Sized_relobj_file<64, false>* object, |
21bb3914 ST |
508 | unsigned int data_shndx, |
509 | Output_section* output_section, | |
510 | const elfcpp::Rela<64, false>& reloc, | |
511 | unsigned int r_type, | |
512 | const elfcpp::Sym<64, false>& lsym); | |
513 | ||
514 | inline bool | |
515 | global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout, | |
516 | Target_x86_64* target, | |
6fa2a40b | 517 | Sized_relobj_file<64, false>* object, |
21bb3914 ST |
518 | unsigned int data_shndx, |
519 | Output_section* output_section, | |
520 | const elfcpp::Rela<64, false>& reloc, | |
521 | unsigned int r_type, | |
522 | Symbol* gsym); | |
523 | ||
a036edd8 | 524 | private: |
e041f13d | 525 | static void |
6fa2a40b | 526 | unsupported_reloc_local(Sized_relobj_file<64, false>*, unsigned int r_type); |
e041f13d ILT |
527 | |
528 | static void | |
6fa2a40b | 529 | unsupported_reloc_global(Sized_relobj_file<64, false>*, unsigned int r_type, |
e041f13d | 530 | Symbol*); |
a036edd8 ILT |
531 | |
532 | void | |
a29b0dad | 533 | check_non_pic(Relobj*, unsigned int r_type, Symbol*); |
a036edd8 | 534 | |
21bb3914 ST |
535 | inline bool |
536 | possible_function_pointer_reloc(unsigned int r_type); | |
537 | ||
7223e9ca | 538 | bool |
6fa2a40b CC |
539 | reloc_needs_plt_for_ifunc(Sized_relobj_file<64, false>*, |
540 | unsigned int r_type); | |
7223e9ca | 541 | |
a036edd8 ILT |
542 | // Whether we have issued an error about a non-PIC compilation. |
543 | bool issued_non_pic_error_; | |
2e30d253 ILT |
544 | }; |
545 | ||
546 | // The class which implements relocation. | |
547 | class Relocate | |
548 | { | |
549 | public: | |
550 | Relocate() | |
36171d64 | 551 | : skip_call_tls_get_addr_(false) |
2e30d253 ILT |
552 | { } |
553 | ||
554 | ~Relocate() | |
555 | { | |
556 | if (this->skip_call_tls_get_addr_) | |
557 | { | |
558 | // FIXME: This needs to specify the location somehow. | |
a0c4fb0a | 559 | gold_error(_("missing expected TLS relocation")); |
2e30d253 ILT |
560 | } |
561 | } | |
562 | ||
563 | // Do a relocation. Return false if the caller should not issue | |
564 | // any warnings about this relocation. | |
565 | inline bool | |
031cdbed ILT |
566 | relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*, |
567 | size_t relnum, const elfcpp::Rela<64, false>&, | |
2e30d253 ILT |
568 | unsigned int r_type, const Sized_symbol<64>*, |
569 | const Symbol_value<64>*, | |
570 | unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, | |
fe8718a4 | 571 | section_size_type); |
2e30d253 ILT |
572 | |
573 | private: | |
574 | // Do a TLS relocation. | |
575 | inline void | |
7bf1f802 ILT |
576 | relocate_tls(const Relocate_info<64, false>*, Target_x86_64*, |
577 | size_t relnum, const elfcpp::Rela<64, false>&, | |
2e30d253 ILT |
578 | unsigned int r_type, const Sized_symbol<64>*, |
579 | const Symbol_value<64>*, | |
fe8718a4 ILT |
580 | unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, |
581 | section_size_type); | |
2e30d253 | 582 | |
c2b45e22 | 583 | // Do a TLS General-Dynamic to Initial-Exec transition. |
7bf1f802 ILT |
584 | inline void |
585 | tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum, | |
586 | Output_segment* tls_segment, | |
587 | const elfcpp::Rela<64, false>&, unsigned int r_type, | |
588 | elfcpp::Elf_types<64>::Elf_Addr value, | |
589 | unsigned char* view, | |
c2b45e22 | 590 | elfcpp::Elf_types<64>::Elf_Addr, |
fe8718a4 | 591 | section_size_type view_size); |
7bf1f802 | 592 | |
56622147 ILT |
593 | // Do a TLS General-Dynamic to Local-Exec transition. |
594 | inline void | |
595 | tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum, | |
2e30d253 ILT |
596 | Output_segment* tls_segment, |
597 | const elfcpp::Rela<64, false>&, unsigned int r_type, | |
598 | elfcpp::Elf_types<64>::Elf_Addr value, | |
599 | unsigned char* view, | |
fe8718a4 | 600 | section_size_type view_size); |
2e30d253 | 601 | |
c2b45e22 CC |
602 | // Do a TLSDESC-style General-Dynamic to Initial-Exec transition. |
603 | inline void | |
604 | tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum, | |
605 | Output_segment* tls_segment, | |
606 | const elfcpp::Rela<64, false>&, unsigned int r_type, | |
607 | elfcpp::Elf_types<64>::Elf_Addr value, | |
608 | unsigned char* view, | |
609 | elfcpp::Elf_types<64>::Elf_Addr, | |
610 | section_size_type view_size); | |
611 | ||
612 | // Do a TLSDESC-style General-Dynamic to Local-Exec transition. | |
613 | inline void | |
614 | tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum, | |
615 | Output_segment* tls_segment, | |
616 | const elfcpp::Rela<64, false>&, unsigned int r_type, | |
617 | elfcpp::Elf_types<64>::Elf_Addr value, | |
618 | unsigned char* view, | |
619 | section_size_type view_size); | |
620 | ||
56622147 | 621 | // Do a TLS Local-Dynamic to Local-Exec transition. |
2e30d253 | 622 | inline void |
56622147 | 623 | tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum, |
2e30d253 ILT |
624 | Output_segment* tls_segment, |
625 | const elfcpp::Rela<64, false>&, unsigned int r_type, | |
626 | elfcpp::Elf_types<64>::Elf_Addr value, | |
627 | unsigned char* view, | |
fe8718a4 | 628 | section_size_type view_size); |
2e30d253 | 629 | |
56622147 ILT |
630 | // Do a TLS Initial-Exec to Local-Exec transition. |
631 | static inline void | |
632 | tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum, | |
72ec2876 ILT |
633 | Output_segment* tls_segment, |
634 | const elfcpp::Rela<64, false>&, unsigned int r_type, | |
635 | elfcpp::Elf_types<64>::Elf_Addr value, | |
636 | unsigned char* view, | |
fe8718a4 | 637 | section_size_type view_size); |
2e30d253 ILT |
638 | |
639 | // This is set if we should skip the next reloc, which should be a | |
640 | // PLT32 reloc against ___tls_get_addr. | |
641 | bool skip_call_tls_get_addr_; | |
642 | }; | |
643 | ||
6a74a719 ILT |
644 | // A class which returns the size required for a relocation type, |
645 | // used while scanning relocs during a relocatable link. | |
646 | class Relocatable_size_for_reloc | |
647 | { | |
648 | public: | |
649 | unsigned int | |
650 | get_size_for_reloc(unsigned int, Relobj*); | |
651 | }; | |
652 | ||
2e30d253 ILT |
653 | // Adjust TLS relocation type based on the options and whether this |
654 | // is a local symbol. | |
e041f13d | 655 | static tls::Tls_optimization |
2e30d253 ILT |
656 | optimize_tls_reloc(bool is_final, int r_type); |
657 | ||
658 | // Get the GOT section, creating it if necessary. | |
659 | Output_data_got<64, false>* | |
660 | got_section(Symbol_table*, Layout*); | |
661 | ||
96f2030e ILT |
662 | // Get the GOT PLT section. |
663 | Output_data_space* | |
664 | got_plt_section() const | |
665 | { | |
666 | gold_assert(this->got_plt_ != NULL); | |
667 | return this->got_plt_; | |
668 | } | |
669 | ||
a8df5856 ILT |
670 | // Get the GOT section for TLSDESC entries. |
671 | Output_data_got<64, false>* | |
672 | got_tlsdesc_section() const | |
673 | { | |
674 | gold_assert(this->got_tlsdesc_ != NULL); | |
675 | return this->got_tlsdesc_; | |
676 | } | |
677 | ||
c2b45e22 CC |
678 | // Create the PLT section. |
679 | void | |
680 | make_plt_section(Symbol_table* symtab, Layout* layout); | |
681 | ||
2e30d253 ILT |
682 | // Create a PLT entry for a global symbol. |
683 | void | |
684 | make_plt_entry(Symbol_table*, Layout*, Symbol*); | |
685 | ||
7223e9ca ILT |
686 | // Create a PLT entry for a local STT_GNU_IFUNC symbol. |
687 | void | |
688 | make_local_ifunc_plt_entry(Symbol_table*, Layout*, | |
6fa2a40b | 689 | Sized_relobj_file<64, false>* relobj, |
7223e9ca ILT |
690 | unsigned int local_sym_index); |
691 | ||
9fa33bee | 692 | // Define the _TLS_MODULE_BASE_ symbol in the TLS segment. |
edfbb029 CC |
693 | void |
694 | define_tls_base_symbol(Symbol_table*, Layout*); | |
695 | ||
c2b45e22 CC |
696 | // Create the reserved PLT and GOT entries for the TLS descriptor resolver. |
697 | void | |
698 | reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout); | |
699 | ||
31d60480 ILT |
700 | // Create a GOT entry for the TLS module index. |
701 | unsigned int | |
702 | got_mod_index_entry(Symbol_table* symtab, Layout* layout, | |
6fa2a40b | 703 | Sized_relobj_file<64, false>* object); |
31d60480 | 704 | |
2e30d253 ILT |
705 | // Get the PLT section. |
706 | Output_data_plt_x86_64* | |
707 | plt_section() const | |
708 | { | |
709 | gold_assert(this->plt_ != NULL); | |
710 | return this->plt_; | |
711 | } | |
712 | ||
713 | // Get the dynamic reloc section, creating it if necessary. | |
714 | Reloc_section* | |
0ffd9845 | 715 | rela_dyn_section(Layout*); |
2e30d253 | 716 | |
e291e7b9 ILT |
717 | // Get the section to use for TLSDESC relocations. |
718 | Reloc_section* | |
719 | rela_tlsdesc_section(Layout*) const; | |
720 | ||
67181c72 ILT |
721 | // Get the section to use for IRELATIVE relocations. |
722 | Reloc_section* | |
723 | rela_irelative_section(Layout*); | |
724 | ||
12c0daef | 725 | // Add a potential copy relocation. |
2e30d253 | 726 | void |
ef9beddf | 727 | copy_reloc(Symbol_table* symtab, Layout* layout, |
6fa2a40b | 728 | Sized_relobj_file<64, false>* object, |
12c0daef ILT |
729 | unsigned int shndx, Output_section* output_section, |
730 | Symbol* sym, const elfcpp::Rela<64, false>& reloc) | |
731 | { | |
732 | this->copy_relocs_.copy_reloc(symtab, layout, | |
733 | symtab->get_sized_symbol<64>(sym), | |
734 | object, shndx, output_section, | |
735 | reloc, this->rela_dyn_section(layout)); | |
736 | } | |
2e30d253 ILT |
737 | |
738 | // Information about this specific target which we pass to the | |
739 | // general Target structure. | |
740 | static const Target::Target_info x86_64_info; | |
741 | ||
0e70b911 CC |
742 | // The types of GOT entries needed for this platform. |
743 | // These values are exposed to the ABI in an incremental link. | |
744 | // Do not renumber existing values without changing the version | |
745 | // number of the .gnu_incremental_inputs section. | |
0a65a3a7 CC |
746 | enum Got_type |
747 | { | |
748 | GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol | |
749 | GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset | |
750 | GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair | |
751 | GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair | |
752 | }; | |
753 | ||
e291e7b9 ILT |
754 | // This type is used as the argument to the target specific |
755 | // relocation routines. The only target specific reloc is | |
756 | // R_X86_64_TLSDESC against a local symbol. | |
757 | struct Tlsdesc_info | |
758 | { | |
6fa2a40b | 759 | Tlsdesc_info(Sized_relobj_file<64, false>* a_object, unsigned int a_r_sym) |
e291e7b9 ILT |
760 | : object(a_object), r_sym(a_r_sym) |
761 | { } | |
762 | ||
763 | // The object in which the local symbol is defined. | |
6fa2a40b | 764 | Sized_relobj_file<64, false>* object; |
e291e7b9 ILT |
765 | // The local symbol index in the object. |
766 | unsigned int r_sym; | |
767 | }; | |
768 | ||
2e30d253 ILT |
769 | // The GOT section. |
770 | Output_data_got<64, false>* got_; | |
771 | // The PLT section. | |
772 | Output_data_plt_x86_64* plt_; | |
773 | // The GOT PLT section. | |
774 | Output_data_space* got_plt_; | |
67181c72 ILT |
775 | // The GOT section for IRELATIVE relocations. |
776 | Output_data_space* got_irelative_; | |
a8df5856 ILT |
777 | // The GOT section for TLSDESC relocations. |
778 | Output_data_got<64, false>* got_tlsdesc_; | |
e785ec03 ILT |
779 | // The _GLOBAL_OFFSET_TABLE_ symbol. |
780 | Symbol* global_offset_table_; | |
2e30d253 | 781 | // The dynamic reloc section. |
0ffd9845 | 782 | Reloc_section* rela_dyn_; |
67181c72 ILT |
783 | // The section to use for IRELATIVE relocs. |
784 | Reloc_section* rela_irelative_; | |
2e30d253 | 785 | // Relocs saved to avoid a COPY reloc. |
12c0daef | 786 | Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_; |
2e30d253 ILT |
787 | // Space for variables copied with a COPY reloc. |
788 | Output_data_space* dynbss_; | |
c2b45e22 | 789 | // Offset of the GOT entry for the TLS module index. |
31d60480 | 790 | unsigned int got_mod_index_offset_; |
e291e7b9 ILT |
791 | // We handle R_X86_64_TLSDESC against a local symbol as a target |
792 | // specific relocation. Here we store the object and local symbol | |
793 | // index for the relocation. | |
794 | std::vector<Tlsdesc_info> tlsdesc_reloc_info_; | |
edfbb029 CC |
795 | // True if the _TLS_MODULE_BASE_ symbol has been defined. |
796 | bool tls_base_symbol_defined_; | |
2e30d253 ILT |
797 | }; |
798 | ||
799 | const Target::Target_info Target_x86_64::x86_64_info = | |
800 | { | |
801 | 64, // size | |
802 | false, // is_big_endian | |
803 | elfcpp::EM_X86_64, // machine_code | |
804 | false, // has_make_symbol | |
805 | false, // has_resolve | |
806 | true, // has_code_fill | |
35cdfc9a | 807 | true, // is_default_stack_executable |
b3ce541e | 808 | true, // can_icf_inline_merge_sections |
0864d551 | 809 | '\0', // wrap_char |
2e30d253 | 810 | "/lib/ld64.so.1", // program interpreter |
0c5e9c22 | 811 | 0x400000, // default_text_segment_address |
cd72c291 | 812 | 0x1000, // abi_pagesize (overridable by -z max-page-size) |
8a5e3e08 ILT |
813 | 0x1000, // common_pagesize (overridable by -z common-page-size) |
814 | elfcpp::SHN_UNDEF, // small_common_shndx | |
815 | elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx | |
816 | 0, // small_common_section_flags | |
05a352e6 DK |
817 | elfcpp::SHF_X86_64_LARGE, // large_common_section_flags |
818 | NULL, // attributes_section | |
819 | NULL // attributes_vendor | |
2e30d253 ILT |
820 | }; |
821 | ||
8a5e3e08 ILT |
822 | // This is called when a new output section is created. This is where |
823 | // we handle the SHF_X86_64_LARGE. | |
824 | ||
825 | void | |
ca09d69a | 826 | Target_x86_64::do_new_output_section(Output_section* os) const |
8a5e3e08 ILT |
827 | { |
828 | if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0) | |
829 | os->set_is_large_section(); | |
830 | } | |
831 | ||
2e30d253 ILT |
832 | // Get the GOT section, creating it if necessary. |
833 | ||
834 | Output_data_got<64, false>* | |
835 | Target_x86_64::got_section(Symbol_table* symtab, Layout* layout) | |
836 | { | |
837 | if (this->got_ == NULL) | |
838 | { | |
839 | gold_assert(symtab != NULL && layout != NULL); | |
840 | ||
9446efde ILT |
841 | // When using -z now, we can treat .got.plt as a relro section. |
842 | // Without -z now, it is modified after program startup by lazy | |
843 | // PLT relocations. | |
844 | bool is_got_plt_relro = parameters->options().now(); | |
845 | Output_section_order got_order = (is_got_plt_relro | |
846 | ? ORDER_RELRO | |
847 | : ORDER_RELRO_LAST); | |
848 | Output_section_order got_plt_order = (is_got_plt_relro | |
849 | ? ORDER_RELRO | |
850 | : ORDER_NON_RELRO_FIRST); | |
851 | ||
2e30d253 ILT |
852 | this->got_ = new Output_data_got<64, false>(); |
853 | ||
82742395 ILT |
854 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, |
855 | (elfcpp::SHF_ALLOC | |
856 | | elfcpp::SHF_WRITE), | |
9446efde | 857 | this->got_, got_order, true); |
2e30d253 | 858 | |
7d9e3d98 | 859 | this->got_plt_ = new Output_data_space(8, "** GOT PLT"); |
82742395 ILT |
860 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, |
861 | (elfcpp::SHF_ALLOC | |
862 | | elfcpp::SHF_WRITE), | |
9446efde ILT |
863 | this->got_plt_, got_plt_order, |
864 | is_got_plt_relro); | |
2e30d253 ILT |
865 | |
866 | // The first three entries are reserved. | |
27bc2bce | 867 | this->got_plt_->set_current_data_size(3 * 8); |
2e30d253 | 868 | |
9446efde ILT |
869 | if (!is_got_plt_relro) |
870 | { | |
871 | // Those bytes can go into the relro segment. | |
872 | layout->increase_relro(3 * 8); | |
873 | } | |
1a2dff53 | 874 | |
2e30d253 | 875 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. |
e785ec03 ILT |
876 | this->global_offset_table_ = |
877 | symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, | |
878 | Symbol_table::PREDEFINED, | |
879 | this->got_plt_, | |
880 | 0, 0, elfcpp::STT_OBJECT, | |
881 | elfcpp::STB_LOCAL, | |
882 | elfcpp::STV_HIDDEN, 0, | |
883 | false, false); | |
a8df5856 | 884 | |
67181c72 ILT |
885 | // If there are any IRELATIVE relocations, they get GOT entries |
886 | // in .got.plt after the jump slot entries. | |
887 | this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT"); | |
888 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, | |
889 | (elfcpp::SHF_ALLOC | |
890 | | elfcpp::SHF_WRITE), | |
891 | this->got_irelative_, | |
9446efde | 892 | got_plt_order, is_got_plt_relro); |
67181c72 | 893 | |
a8df5856 | 894 | // If there are any TLSDESC relocations, they get GOT entries in |
67181c72 | 895 | // .got.plt after the jump slot and IRELATIVE entries. |
a8df5856 ILT |
896 | this->got_tlsdesc_ = new Output_data_got<64, false>(); |
897 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, | |
898 | (elfcpp::SHF_ALLOC | |
899 | | elfcpp::SHF_WRITE), | |
22f0da72 | 900 | this->got_tlsdesc_, |
9446efde | 901 | got_plt_order, is_got_plt_relro); |
2e30d253 ILT |
902 | } |
903 | ||
904 | return this->got_; | |
905 | } | |
906 | ||
907 | // Get the dynamic reloc section, creating it if necessary. | |
908 | ||
909 | Target_x86_64::Reloc_section* | |
0ffd9845 | 910 | Target_x86_64::rela_dyn_section(Layout* layout) |
2e30d253 | 911 | { |
0ffd9845 | 912 | if (this->rela_dyn_ == NULL) |
2e30d253 ILT |
913 | { |
914 | gold_assert(layout != NULL); | |
d98bc257 | 915 | this->rela_dyn_ = new Reloc_section(parameters->options().combreloc()); |
2e30d253 | 916 | layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, |
22f0da72 ILT |
917 | elfcpp::SHF_ALLOC, this->rela_dyn_, |
918 | ORDER_DYNAMIC_RELOCS, false); | |
2e30d253 | 919 | } |
0ffd9845 | 920 | return this->rela_dyn_; |
2e30d253 ILT |
921 | } |
922 | ||
67181c72 ILT |
923 | // Get the section to use for IRELATIVE relocs, creating it if |
924 | // necessary. These go in .rela.dyn, but only after all other dynamic | |
925 | // relocations. They need to follow the other dynamic relocations so | |
926 | // that they can refer to global variables initialized by those | |
927 | // relocs. | |
928 | ||
929 | Target_x86_64::Reloc_section* | |
930 | Target_x86_64::rela_irelative_section(Layout* layout) | |
931 | { | |
932 | if (this->rela_irelative_ == NULL) | |
933 | { | |
934 | // Make sure we have already created the dynamic reloc section. | |
935 | this->rela_dyn_section(layout); | |
936 | this->rela_irelative_ = new Reloc_section(false); | |
937 | layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, | |
938 | elfcpp::SHF_ALLOC, this->rela_irelative_, | |
939 | ORDER_DYNAMIC_RELOCS, false); | |
940 | gold_assert(this->rela_dyn_->output_section() | |
941 | == this->rela_irelative_->output_section()); | |
942 | } | |
943 | return this->rela_irelative_; | |
944 | } | |
945 | ||
4829d394 | 946 | // Initialize the PLT section. |
2e30d253 | 947 | |
4829d394 | 948 | void |
67181c72 | 949 | Output_data_plt_x86_64::init(Layout* layout) |
2e30d253 | 950 | { |
d98bc257 | 951 | this->rel_ = new Reloc_section(false); |
2e30d253 | 952 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, |
22f0da72 ILT |
953 | elfcpp::SHF_ALLOC, this->rel_, |
954 | ORDER_DYNAMIC_PLT_RELOCS, false); | |
7223e9ca | 955 | |
07a60597 ILT |
956 | // Add unwind information if requested. |
957 | if (parameters->options().ld_generated_unwind_info()) | |
958 | layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size, | |
959 | plt_eh_frame_fde, plt_eh_frame_fde_size); | |
2e30d253 ILT |
960 | } |
961 | ||
962 | void | |
963 | Output_data_plt_x86_64::do_adjust_output_section(Output_section* os) | |
964 | { | |
b0481b0b | 965 | os->set_entsize(plt_entry_size); |
2e30d253 ILT |
966 | } |
967 | ||
968 | // Add an entry to the PLT. | |
969 | ||
970 | void | |
67181c72 ILT |
971 | Output_data_plt_x86_64::add_entry(Symbol_table* symtab, Layout* layout, |
972 | Symbol* gsym) | |
2e30d253 ILT |
973 | { |
974 | gold_assert(!gsym->has_plt_offset()); | |
975 | ||
4829d394 CC |
976 | unsigned int plt_index; |
977 | off_t plt_offset; | |
978 | section_offset_type got_offset; | |
2e30d253 | 979 | |
67181c72 ILT |
980 | unsigned int* pcount; |
981 | unsigned int offset; | |
982 | unsigned int reserved; | |
983 | Output_data_space* got; | |
984 | if (gsym->type() == elfcpp::STT_GNU_IFUNC | |
985 | && gsym->can_use_relative_reloc(false)) | |
986 | { | |
987 | pcount = &this->irelative_count_; | |
988 | offset = 0; | |
989 | reserved = 0; | |
990 | got = this->got_irelative_; | |
991 | } | |
992 | else | |
993 | { | |
994 | pcount = &this->count_; | |
995 | offset = 1; | |
996 | reserved = 3; | |
997 | got = this->got_plt_; | |
998 | } | |
999 | ||
4829d394 CC |
1000 | if (!this->is_data_size_valid()) |
1001 | { | |
67181c72 ILT |
1002 | // Note that when setting the PLT offset for a non-IRELATIVE |
1003 | // entry we skip the initial reserved PLT entry. | |
1004 | plt_index = *pcount + offset; | |
4829d394 | 1005 | plt_offset = plt_index * plt_entry_size; |
2e30d253 | 1006 | |
67181c72 | 1007 | ++*pcount; |
2e30d253 | 1008 | |
67181c72 ILT |
1009 | got_offset = (plt_index - offset + reserved) * 8; |
1010 | gold_assert(got_offset == got->current_data_size()); | |
2e30d253 | 1011 | |
4829d394 CC |
1012 | // Every PLT entry needs a GOT entry which points back to the PLT |
1013 | // entry (this will be changed by the dynamic linker, normally | |
1014 | // lazily when the function is called). | |
67181c72 | 1015 | got->set_current_data_size(got_offset + 8); |
4829d394 | 1016 | } |
7223e9ca ILT |
1017 | else |
1018 | { | |
67181c72 ILT |
1019 | // FIXME: This is probably not correct for IRELATIVE relocs. |
1020 | ||
4829d394 CC |
1021 | // For incremental updates, find an available slot. |
1022 | plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0); | |
1023 | if (plt_offset == -1) | |
e6455dfb CC |
1024 | gold_fallback(_("out of patch space (PLT);" |
1025 | " relink with --incremental-full")); | |
4829d394 CC |
1026 | |
1027 | // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset | |
1028 | // can be calculated from the PLT index, adjusting for the three | |
1029 | // reserved entries at the beginning of the GOT. | |
1030 | plt_index = plt_offset / plt_entry_size - 1; | |
67181c72 | 1031 | got_offset = (plt_index - offset + reserved) * 8; |
7223e9ca | 1032 | } |
2e30d253 | 1033 | |
4829d394 CC |
1034 | gsym->set_plt_offset(plt_offset); |
1035 | ||
1036 | // Every PLT entry needs a reloc. | |
67181c72 | 1037 | this->add_relocation(symtab, layout, gsym, got_offset); |
4829d394 | 1038 | |
2e30d253 ILT |
1039 | // Note that we don't need to save the symbol. The contents of the |
1040 | // PLT are independent of which symbols are used. The symbols only | |
1041 | // appear in the relocations. | |
1042 | } | |
1043 | ||
7223e9ca ILT |
1044 | // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return |
1045 | // the PLT offset. | |
1046 | ||
1047 | unsigned int | |
6fa2a40b | 1048 | Output_data_plt_x86_64::add_local_ifunc_entry( |
67181c72 ILT |
1049 | Symbol_table* symtab, |
1050 | Layout* layout, | |
6fa2a40b CC |
1051 | Sized_relobj_file<64, false>* relobj, |
1052 | unsigned int local_sym_index) | |
7223e9ca | 1053 | { |
67181c72 ILT |
1054 | unsigned int plt_offset = this->irelative_count_ * plt_entry_size; |
1055 | ++this->irelative_count_; | |
7223e9ca | 1056 | |
67181c72 | 1057 | section_offset_type got_offset = this->got_irelative_->current_data_size(); |
7223e9ca ILT |
1058 | |
1059 | // Every PLT entry needs a GOT entry which points back to the PLT | |
1060 | // entry. | |
67181c72 | 1061 | this->got_irelative_->set_current_data_size(got_offset + 8); |
7223e9ca ILT |
1062 | |
1063 | // Every PLT entry needs a reloc. | |
67181c72 ILT |
1064 | Reloc_section* rela = this->rela_irelative(symtab, layout); |
1065 | rela->add_symbolless_local_addend(relobj, local_sym_index, | |
1066 | elfcpp::R_X86_64_IRELATIVE, | |
1067 | this->got_irelative_, got_offset, 0); | |
7223e9ca ILT |
1068 | |
1069 | return plt_offset; | |
1070 | } | |
1071 | ||
4829d394 CC |
1072 | // Add the relocation for a PLT entry. |
1073 | ||
1074 | void | |
67181c72 ILT |
1075 | Output_data_plt_x86_64::add_relocation(Symbol_table* symtab, Layout* layout, |
1076 | Symbol* gsym, unsigned int got_offset) | |
4829d394 CC |
1077 | { |
1078 | if (gsym->type() == elfcpp::STT_GNU_IFUNC | |
1079 | && gsym->can_use_relative_reloc(false)) | |
67181c72 ILT |
1080 | { |
1081 | Reloc_section* rela = this->rela_irelative(symtab, layout); | |
1082 | rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE, | |
1083 | this->got_irelative_, got_offset, 0); | |
1084 | } | |
4829d394 CC |
1085 | else |
1086 | { | |
1087 | gsym->set_needs_dynsym_entry(); | |
1088 | this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_, | |
1089 | got_offset, 0); | |
1090 | } | |
1091 | } | |
1092 | ||
e291e7b9 ILT |
1093 | // Return where the TLSDESC relocations should go, creating it if |
1094 | // necessary. These follow the JUMP_SLOT relocations. | |
1095 | ||
1096 | Output_data_plt_x86_64::Reloc_section* | |
1097 | Output_data_plt_x86_64::rela_tlsdesc(Layout* layout) | |
1098 | { | |
1099 | if (this->tlsdesc_rel_ == NULL) | |
1100 | { | |
1101 | this->tlsdesc_rel_ = new Reloc_section(false); | |
1102 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, | |
1103 | elfcpp::SHF_ALLOC, this->tlsdesc_rel_, | |
22f0da72 | 1104 | ORDER_DYNAMIC_PLT_RELOCS, false); |
67181c72 ILT |
1105 | gold_assert(this->tlsdesc_rel_->output_section() |
1106 | == this->rel_->output_section()); | |
e291e7b9 ILT |
1107 | } |
1108 | return this->tlsdesc_rel_; | |
1109 | } | |
1110 | ||
67181c72 ILT |
1111 | // Return where the IRELATIVE relocations should go in the PLT. These |
1112 | // follow the JUMP_SLOT and the TLSDESC relocations. | |
1113 | ||
1114 | Output_data_plt_x86_64::Reloc_section* | |
1115 | Output_data_plt_x86_64::rela_irelative(Symbol_table* symtab, Layout* layout) | |
1116 | { | |
1117 | if (this->irelative_rel_ == NULL) | |
1118 | { | |
1119 | // Make sure we have a place for the TLSDESC relocations, in | |
1120 | // case we see any later on. | |
1121 | this->rela_tlsdesc(layout); | |
1122 | this->irelative_rel_ = new Reloc_section(false); | |
1123 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, | |
1124 | elfcpp::SHF_ALLOC, this->irelative_rel_, | |
1125 | ORDER_DYNAMIC_PLT_RELOCS, false); | |
1126 | gold_assert(this->irelative_rel_->output_section() | |
1127 | == this->rel_->output_section()); | |
1128 | ||
1129 | if (parameters->doing_static_link()) | |
1130 | { | |
1131 | // A statically linked executable will only have a .rela.plt | |
1132 | // section to hold R_X86_64_IRELATIVE relocs for | |
1133 | // STT_GNU_IFUNC symbols. The library will use these | |
1134 | // symbols to locate the IRELATIVE relocs at program startup | |
1135 | // time. | |
1136 | symtab->define_in_output_data("__rela_iplt_start", NULL, | |
1137 | Symbol_table::PREDEFINED, | |
1138 | this->irelative_rel_, 0, 0, | |
1139 | elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, | |
1140 | elfcpp::STV_HIDDEN, 0, false, true); | |
1141 | symtab->define_in_output_data("__rela_iplt_end", NULL, | |
1142 | Symbol_table::PREDEFINED, | |
1143 | this->irelative_rel_, 0, 0, | |
1144 | elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, | |
1145 | elfcpp::STV_HIDDEN, 0, true, true); | |
1146 | } | |
1147 | } | |
1148 | return this->irelative_rel_; | |
1149 | } | |
1150 | ||
1151 | // Return the PLT address to use for a global symbol. | |
1152 | ||
1153 | uint64_t | |
1154 | Output_data_plt_x86_64::address_for_global(const Symbol* gsym) | |
1155 | { | |
1156 | uint64_t offset = 0; | |
1157 | if (gsym->type() == elfcpp::STT_GNU_IFUNC | |
1158 | && gsym->can_use_relative_reloc(false)) | |
1159 | offset = (this->count_ + 1) * plt_entry_size; | |
1160 | return this->address() + offset; | |
1161 | } | |
1162 | ||
1163 | // Return the PLT address to use for a local symbol. These are always | |
1164 | // IRELATIVE relocs. | |
1165 | ||
1166 | uint64_t | |
1167 | Output_data_plt_x86_64::address_for_local(const Relobj*, unsigned int) | |
1168 | { | |
1169 | return this->address() + (this->count_ + 1) * plt_entry_size; | |
1170 | } | |
1171 | ||
c2b45e22 CC |
1172 | // Set the final size. |
1173 | void | |
1174 | Output_data_plt_x86_64::set_final_data_size() | |
1175 | { | |
67181c72 | 1176 | unsigned int count = this->count_ + this->irelative_count_; |
c2b45e22 CC |
1177 | if (this->has_tlsdesc_entry()) |
1178 | ++count; | |
1179 | this->set_data_size((count + 1) * plt_entry_size); | |
1180 | } | |
1181 | ||
2e30d253 ILT |
1182 | // The first entry in the PLT for an executable. |
1183 | ||
07a60597 | 1184 | const unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] = |
2e30d253 ILT |
1185 | { |
1186 | // From AMD64 ABI Draft 0.98, page 76 | |
1187 | 0xff, 0x35, // pushq contents of memory address | |
2e30d253 | 1188 | 0, 0, 0, 0, // replaced with address of .got + 8 |
78d911fd ILT |
1189 | 0xff, 0x25, // jmp indirect |
1190 | 0, 0, 0, 0, // replaced with address of .got + 16 | |
2e30d253 ILT |
1191 | 0x90, 0x90, 0x90, 0x90 // noop (x4) |
1192 | }; | |
1193 | ||
1194 | // Subsequent entries in the PLT for an executable. | |
1195 | ||
07a60597 | 1196 | const unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] = |
2e30d253 ILT |
1197 | { |
1198 | // From AMD64 ABI Draft 0.98, page 76 | |
1199 | 0xff, 0x25, // jmpq indirect | |
1200 | 0, 0, 0, 0, // replaced with address of symbol in .got | |
1201 | 0x68, // pushq immediate | |
1202 | 0, 0, 0, 0, // replaced with offset into relocation table | |
1203 | 0xe9, // jmpq relative | |
1204 | 0, 0, 0, 0 // replaced with offset to start of .plt | |
1205 | }; | |
1206 | ||
c2b45e22 CC |
1207 | // The reserved TLSDESC entry in the PLT for an executable. |
1208 | ||
07a60597 | 1209 | const unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] = |
c2b45e22 CC |
1210 | { |
1211 | // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32 | |
1212 | // and AMD64/EM64T", Version 0.9.4 (2005-10-10). | |
1213 | 0xff, 0x35, // pushq x(%rip) | |
1214 | 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8) | |
1215 | 0xff, 0x25, // jmpq *y(%rip) | |
1216 | 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry | |
1217 | 0x0f, 0x1f, // nop | |
1218 | 0x40, 0 | |
1219 | }; | |
1220 | ||
07a60597 ILT |
1221 | // The .eh_frame unwind information for the PLT. |
1222 | ||
1223 | const unsigned char | |
1224 | Output_data_plt_x86_64::plt_eh_frame_cie[plt_eh_frame_cie_size] = | |
1225 | { | |
1226 | 1, // CIE version. | |
1227 | 'z', // Augmentation: augmentation size included. | |
1228 | 'R', // Augmentation: FDE encoding included. | |
1229 | '\0', // End of augmentation string. | |
1230 | 1, // Code alignment factor. | |
1231 | 0x78, // Data alignment factor. | |
1232 | 16, // Return address column. | |
1233 | 1, // Augmentation size. | |
1234 | (elfcpp::DW_EH_PE_pcrel // FDE encoding. | |
1235 | | elfcpp::DW_EH_PE_sdata4), | |
1236 | elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8. | |
1237 | elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8. | |
1238 | elfcpp::DW_CFA_nop, // Align to 16 bytes. | |
1239 | elfcpp::DW_CFA_nop | |
1240 | }; | |
1241 | ||
1242 | const unsigned char | |
1243 | Output_data_plt_x86_64::plt_eh_frame_fde[plt_eh_frame_fde_size] = | |
1244 | { | |
1245 | 0, 0, 0, 0, // Replaced with offset to .plt. | |
1246 | 0, 0, 0, 0, // Replaced with size of .plt. | |
1247 | 0, // Augmentation size. | |
1248 | elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16. | |
1249 | elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6. | |
1250 | elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24. | |
1251 | elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16. | |
1252 | elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression. | |
1253 | 11, // Block length. | |
1254 | elfcpp::DW_OP_breg7, 8, // Push %rsp + 8. | |
1255 | elfcpp::DW_OP_breg16, 0, // Push %rip. | |
1256 | elfcpp::DW_OP_lit15, // Push 0xf. | |
1257 | elfcpp::DW_OP_and, // & (%rip & 0xf). | |
1258 | elfcpp::DW_OP_lit11, // Push 0xb. | |
1259 | elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb) | |
1260 | elfcpp::DW_OP_lit3, // Push 3. | |
1261 | elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3) | |
1262 | elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8 | |
1263 | elfcpp::DW_CFA_nop, // Align to 32 bytes. | |
1264 | elfcpp::DW_CFA_nop, | |
1265 | elfcpp::DW_CFA_nop, | |
1266 | elfcpp::DW_CFA_nop | |
1267 | }; | |
1268 | ||
2e30d253 ILT |
1269 | // Write out the PLT. This uses the hand-coded instructions above, |
1270 | // and adjusts them as needed. This is specified by the AMD64 ABI. | |
1271 | ||
1272 | void | |
1273 | Output_data_plt_x86_64::do_write(Output_file* of) | |
1274 | { | |
2ea97941 | 1275 | const off_t offset = this->offset(); |
fe8718a4 ILT |
1276 | const section_size_type oview_size = |
1277 | convert_to_section_size_type(this->data_size()); | |
2ea97941 | 1278 | unsigned char* const oview = of->get_output_view(offset, oview_size); |
2e30d253 ILT |
1279 | |
1280 | const off_t got_file_offset = this->got_plt_->offset(); | |
67181c72 ILT |
1281 | gold_assert(parameters->incremental_update() |
1282 | || (got_file_offset + this->got_plt_->data_size() | |
1283 | == this->got_irelative_->offset())); | |
fe8718a4 | 1284 | const section_size_type got_size = |
67181c72 ILT |
1285 | convert_to_section_size_type(this->got_plt_->data_size() |
1286 | + this->got_irelative_->data_size()); | |
2e30d253 ILT |
1287 | unsigned char* const got_view = of->get_output_view(got_file_offset, |
1288 | got_size); | |
1289 | ||
1290 | unsigned char* pov = oview; | |
1291 | ||
c2b45e22 | 1292 | // The base address of the .plt section. |
a984ee1d | 1293 | elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address(); |
c2b45e22 | 1294 | // The base address of the .got section. |
a984ee1d | 1295 | elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address(); |
c2b45e22 CC |
1296 | // The base address of the PLT portion of the .got section, |
1297 | // which is where the GOT pointer will point, and where the | |
1298 | // three reserved GOT entries are located. | |
a984ee1d | 1299 | elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address(); |
2e30d253 ILT |
1300 | |
1301 | memcpy(pov, first_plt_entry, plt_entry_size); | |
78d911fd | 1302 | // We do a jmp relative to the PC at the end of this instruction. |
a984ee1d ILT |
1303 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, |
1304 | (got_address + 8 | |
1305 | - (plt_address + 6))); | |
1306 | elfcpp::Swap<32, false>::writeval(pov + 8, | |
1307 | (got_address + 16 | |
1308 | - (plt_address + 12))); | |
2e30d253 ILT |
1309 | pov += plt_entry_size; |
1310 | ||
1311 | unsigned char* got_pov = got_view; | |
1312 | ||
7d172687 ILT |
1313 | // The first entry in the GOT is the address of the .dynamic section |
1314 | // aka the PT_DYNAMIC segment. The next two entries are reserved. | |
1315 | // We saved space for them when we created the section in | |
1316 | // Target_x86_64::got_section. | |
1317 | Output_section* dynamic = this->layout_->dynamic_section(); | |
1318 | uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address(); | |
1319 | elfcpp::Swap<64, false>::writeval(got_pov, dynamic_addr); | |
1320 | got_pov += 8; | |
1321 | memset(got_pov, 0, 16); | |
1322 | got_pov += 16; | |
2e30d253 ILT |
1323 | |
1324 | unsigned int plt_offset = plt_entry_size; | |
1325 | unsigned int got_offset = 24; | |
67181c72 | 1326 | const unsigned int count = this->count_ + this->irelative_count_; |
2e30d253 ILT |
1327 | for (unsigned int plt_index = 0; |
1328 | plt_index < count; | |
1329 | ++plt_index, | |
1330 | pov += plt_entry_size, | |
1331 | got_pov += 8, | |
1332 | plt_offset += plt_entry_size, | |
1333 | got_offset += 8) | |
1334 | { | |
1335 | // Set and adjust the PLT entry itself. | |
1336 | memcpy(pov, plt_entry, plt_entry_size); | |
78d911fd ILT |
1337 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, |
1338 | (got_address + got_offset | |
1339 | - (plt_address + plt_offset | |
1340 | + 6))); | |
2e30d253 ILT |
1341 | |
1342 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index); | |
1343 | elfcpp::Swap<32, false>::writeval(pov + 12, | |
1344 | - (plt_offset + plt_entry_size)); | |
1345 | ||
1346 | // Set the entry in the GOT. | |
1347 | elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6); | |
1348 | } | |
1349 | ||
c2b45e22 CC |
1350 | if (this->has_tlsdesc_entry()) |
1351 | { | |
1352 | // Set and adjust the reserved TLSDESC PLT entry. | |
1353 | unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset(); | |
1354 | memcpy(pov, tlsdesc_plt_entry, plt_entry_size); | |
1355 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, | |
1356 | (got_address + 8 | |
1357 | - (plt_address + plt_offset | |
1358 | + 6))); | |
1359 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 8, | |
1360 | (got_base | |
1361 | + tlsdesc_got_offset | |
1362 | - (plt_address + plt_offset | |
1363 | + 12))); | |
1364 | pov += plt_entry_size; | |
1365 | } | |
1366 | ||
fe8718a4 ILT |
1367 | gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); |
1368 | gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size); | |
2e30d253 | 1369 | |
2ea97941 | 1370 | of->write_output_view(offset, oview_size, oview); |
2e30d253 ILT |
1371 | of->write_output_view(got_file_offset, got_size, got_view); |
1372 | } | |
1373 | ||
c2b45e22 | 1374 | // Create the PLT section. |
2e30d253 ILT |
1375 | |
1376 | void | |
c2b45e22 | 1377 | Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout) |
2e30d253 | 1378 | { |
2e30d253 ILT |
1379 | if (this->plt_ == NULL) |
1380 | { | |
1381 | // Create the GOT sections first. | |
1382 | this->got_section(symtab, layout); | |
1383 | ||
67181c72 ILT |
1384 | this->plt_ = new Output_data_plt_x86_64(layout, this->got_, |
1385 | this->got_plt_, | |
1386 | this->got_irelative_); | |
2e30d253 ILT |
1387 | layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, |
1388 | (elfcpp::SHF_ALLOC | |
1389 | | elfcpp::SHF_EXECINSTR), | |
22f0da72 | 1390 | this->plt_, ORDER_PLT, false); |
7223e9ca ILT |
1391 | |
1392 | // Make the sh_info field of .rela.plt point to .plt. | |
1393 | Output_section* rela_plt_os = this->plt_->rela_plt()->output_section(); | |
1394 | rela_plt_os->set_info_section(this->plt_->output_section()); | |
2e30d253 | 1395 | } |
c2b45e22 CC |
1396 | } |
1397 | ||
e291e7b9 ILT |
1398 | // Return the section for TLSDESC relocations. |
1399 | ||
1400 | Target_x86_64::Reloc_section* | |
1401 | Target_x86_64::rela_tlsdesc_section(Layout* layout) const | |
1402 | { | |
1403 | return this->plt_section()->rela_tlsdesc(layout); | |
1404 | } | |
1405 | ||
c2b45e22 CC |
1406 | // Create a PLT entry for a global symbol. |
1407 | ||
1408 | void | |
1409 | Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout, | |
1410 | Symbol* gsym) | |
1411 | { | |
1412 | if (gsym->has_plt_offset()) | |
1413 | return; | |
1414 | ||
1415 | if (this->plt_ == NULL) | |
1416 | this->make_plt_section(symtab, layout); | |
2e30d253 | 1417 | |
67181c72 | 1418 | this->plt_->add_entry(symtab, layout, gsym); |
2e30d253 ILT |
1419 | } |
1420 | ||
7223e9ca ILT |
1421 | // Make a PLT entry for a local STT_GNU_IFUNC symbol. |
1422 | ||
1423 | void | |
1424 | Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout, | |
6fa2a40b | 1425 | Sized_relobj_file<64, false>* relobj, |
7223e9ca ILT |
1426 | unsigned int local_sym_index) |
1427 | { | |
1428 | if (relobj->local_has_plt_offset(local_sym_index)) | |
1429 | return; | |
1430 | if (this->plt_ == NULL) | |
1431 | this->make_plt_section(symtab, layout); | |
67181c72 ILT |
1432 | unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout, |
1433 | relobj, | |
7223e9ca ILT |
1434 | local_sym_index); |
1435 | relobj->set_local_plt_offset(local_sym_index, plt_offset); | |
1436 | } | |
1437 | ||
0e70b911 CC |
1438 | // Return the number of entries in the PLT. |
1439 | ||
1440 | unsigned int | |
1441 | Target_x86_64::plt_entry_count() const | |
1442 | { | |
1443 | if (this->plt_ == NULL) | |
1444 | return 0; | |
1445 | return this->plt_->entry_count(); | |
1446 | } | |
1447 | ||
1448 | // Return the offset of the first non-reserved PLT entry. | |
1449 | ||
1450 | unsigned int | |
1451 | Target_x86_64::first_plt_entry_offset() const | |
1452 | { | |
1453 | return Output_data_plt_x86_64::first_plt_entry_offset(); | |
1454 | } | |
1455 | ||
1456 | // Return the size of each PLT entry. | |
1457 | ||
1458 | unsigned int | |
1459 | Target_x86_64::plt_entry_size() const | |
1460 | { | |
1461 | return Output_data_plt_x86_64::get_plt_entry_size(); | |
1462 | } | |
1463 | ||
4829d394 CC |
1464 | // Create the GOT and PLT sections for an incremental update. |
1465 | ||
1466 | Output_data_got<64, false>* | |
1467 | Target_x86_64::init_got_plt_for_update(Symbol_table* symtab, | |
1468 | Layout* layout, | |
1469 | unsigned int got_count, | |
1470 | unsigned int plt_count) | |
1471 | { | |
1472 | gold_assert(this->got_ == NULL); | |
1473 | ||
1474 | this->got_ = new Output_data_got<64, false>(got_count * 8); | |
1475 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, | |
1476 | (elfcpp::SHF_ALLOC | |
1477 | | elfcpp::SHF_WRITE), | |
1478 | this->got_, ORDER_RELRO_LAST, | |
1479 | true); | |
1480 | ||
1481 | // Add the three reserved entries. | |
1482 | this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT"); | |
1483 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, | |
1484 | (elfcpp::SHF_ALLOC | |
1485 | | elfcpp::SHF_WRITE), | |
1486 | this->got_plt_, ORDER_NON_RELRO_FIRST, | |
1487 | false); | |
1488 | ||
1489 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. | |
1490 | this->global_offset_table_ = | |
1491 | symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, | |
1492 | Symbol_table::PREDEFINED, | |
1493 | this->got_plt_, | |
1494 | 0, 0, elfcpp::STT_OBJECT, | |
1495 | elfcpp::STB_LOCAL, | |
1496 | elfcpp::STV_HIDDEN, 0, | |
1497 | false, false); | |
1498 | ||
1499 | // If there are any TLSDESC relocations, they get GOT entries in | |
1500 | // .got.plt after the jump slot entries. | |
1501 | // FIXME: Get the count for TLSDESC entries. | |
1502 | this->got_tlsdesc_ = new Output_data_got<64, false>(0); | |
1503 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, | |
1504 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, | |
1505 | this->got_tlsdesc_, | |
1506 | ORDER_NON_RELRO_FIRST, false); | |
1507 | ||
67181c72 ILT |
1508 | // If there are any IRELATIVE relocations, they get GOT entries in |
1509 | // .got.plt after the jump slot and TLSDESC entries. | |
1510 | this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT"); | |
1511 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, | |
1512 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, | |
1513 | this->got_irelative_, | |
1514 | ORDER_NON_RELRO_FIRST, false); | |
1515 | ||
4829d394 | 1516 | // Create the PLT section. |
67181c72 ILT |
1517 | this->plt_ = new Output_data_plt_x86_64(layout, this->got_, this->got_plt_, |
1518 | this->got_irelative_, plt_count); | |
4829d394 CC |
1519 | layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, |
1520 | elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR, | |
1521 | this->plt_, ORDER_PLT, false); | |
1522 | ||
1523 | // Make the sh_info field of .rela.plt point to .plt. | |
1524 | Output_section* rela_plt_os = this->plt_->rela_plt()->output_section(); | |
1525 | rela_plt_os->set_info_section(this->plt_->output_section()); | |
1526 | ||
6fa2a40b CC |
1527 | // Create the rela_dyn section. |
1528 | this->rela_dyn_section(layout); | |
1529 | ||
4829d394 CC |
1530 | return this->got_; |
1531 | } | |
1532 | ||
6fa2a40b CC |
1533 | // Reserve a GOT entry for a local symbol, and regenerate any |
1534 | // necessary dynamic relocations. | |
1535 | ||
1536 | void | |
1537 | Target_x86_64::reserve_local_got_entry( | |
1538 | unsigned int got_index, | |
1539 | Sized_relobj<64, false>* obj, | |
1540 | unsigned int r_sym, | |
1541 | unsigned int got_type) | |
1542 | { | |
1543 | unsigned int got_offset = got_index * 8; | |
1544 | Reloc_section* rela_dyn = this->rela_dyn_section(NULL); | |
1545 | ||
1546 | this->got_->reserve_local(got_index, obj, r_sym, got_type); | |
1547 | switch (got_type) | |
1548 | { | |
1549 | case GOT_TYPE_STANDARD: | |
1550 | if (parameters->options().output_is_position_independent()) | |
1551 | rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE, | |
1552 | this->got_, got_offset, 0); | |
1553 | break; | |
1554 | case GOT_TYPE_TLS_OFFSET: | |
1555 | rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64, | |
1556 | this->got_, got_offset, 0); | |
1557 | break; | |
1558 | case GOT_TYPE_TLS_PAIR: | |
1559 | this->got_->reserve_slot(got_index + 1); | |
1560 | rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64, | |
1561 | this->got_, got_offset, 0); | |
1562 | break; | |
1563 | case GOT_TYPE_TLS_DESC: | |
1564 | gold_fatal(_("TLS_DESC not yet supported for incremental linking")); | |
1565 | // this->got_->reserve_slot(got_index + 1); | |
1566 | // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg, | |
1567 | // this->got_, got_offset, 0); | |
1568 | break; | |
1569 | default: | |
1570 | gold_unreachable(); | |
1571 | } | |
1572 | } | |
1573 | ||
1574 | // Reserve a GOT entry for a global symbol, and regenerate any | |
1575 | // necessary dynamic relocations. | |
1576 | ||
1577 | void | |
1578 | Target_x86_64::reserve_global_got_entry(unsigned int got_index, Symbol* gsym, | |
1579 | unsigned int got_type) | |
1580 | { | |
1581 | unsigned int got_offset = got_index * 8; | |
1582 | Reloc_section* rela_dyn = this->rela_dyn_section(NULL); | |
1583 | ||
1584 | this->got_->reserve_global(got_index, gsym, got_type); | |
1585 | switch (got_type) | |
1586 | { | |
1587 | case GOT_TYPE_STANDARD: | |
1588 | if (!gsym->final_value_is_known()) | |
1589 | { | |
1590 | if (gsym->is_from_dynobj() | |
1591 | || gsym->is_undefined() | |
1592 | || gsym->is_preemptible() | |
1593 | || gsym->type() == elfcpp::STT_GNU_IFUNC) | |
1594 | rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, | |
1595 | this->got_, got_offset, 0); | |
1596 | else | |
1597 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE, | |
1598 | this->got_, got_offset, 0); | |
1599 | } | |
1600 | break; | |
1601 | case GOT_TYPE_TLS_OFFSET: | |
1602 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64, | |
1603 | this->got_, got_offset, 0); | |
1604 | break; | |
1605 | case GOT_TYPE_TLS_PAIR: | |
1606 | this->got_->reserve_slot(got_index + 1); | |
1607 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64, | |
1608 | this->got_, got_offset, 0); | |
1609 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64, | |
1610 | this->got_, got_offset + 8, 0); | |
1611 | break; | |
1612 | case GOT_TYPE_TLS_DESC: | |
1613 | this->got_->reserve_slot(got_index + 1); | |
1614 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC, | |
1615 | this->got_, got_offset, 0); | |
1616 | break; | |
1617 | default: | |
1618 | gold_unreachable(); | |
1619 | } | |
1620 | } | |
1621 | ||
4829d394 CC |
1622 | // Register an existing PLT entry for a global symbol. |
1623 | ||
1624 | void | |
67181c72 ILT |
1625 | Target_x86_64::register_global_plt_entry(Symbol_table* symtab, |
1626 | Layout* layout, | |
1627 | unsigned int plt_index, | |
4829d394 CC |
1628 | Symbol* gsym) |
1629 | { | |
1630 | gold_assert(this->plt_ != NULL); | |
1631 | gold_assert(!gsym->has_plt_offset()); | |
1632 | ||
1633 | this->plt_->reserve_slot(plt_index); | |
1634 | ||
1635 | gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size()); | |
1636 | ||
1637 | unsigned int got_offset = (plt_index + 3) * 8; | |
67181c72 | 1638 | this->plt_->add_relocation(symtab, layout, gsym, got_offset); |
4829d394 CC |
1639 | } |
1640 | ||
26d3c67d CC |
1641 | // Force a COPY relocation for a given symbol. |
1642 | ||
1643 | void | |
1644 | Target_x86_64::emit_copy_reloc( | |
1645 | Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset) | |
1646 | { | |
1647 | this->copy_relocs_.emit_copy_reloc(symtab, | |
1648 | symtab->get_sized_symbol<64>(sym), | |
1649 | os, | |
1650 | offset, | |
1651 | this->rela_dyn_section(NULL)); | |
1652 | } | |
1653 | ||
9fa33bee | 1654 | // Define the _TLS_MODULE_BASE_ symbol in the TLS segment. |
edfbb029 CC |
1655 | |
1656 | void | |
1657 | Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout) | |
1658 | { | |
1659 | if (this->tls_base_symbol_defined_) | |
1660 | return; | |
1661 | ||
1662 | Output_segment* tls_segment = layout->tls_segment(); | |
1663 | if (tls_segment != NULL) | |
1664 | { | |
183fd0e3 | 1665 | bool is_exec = parameters->options().output_is_executable(); |
edfbb029 | 1666 | symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL, |
99fff23b | 1667 | Symbol_table::PREDEFINED, |
edfbb029 CC |
1668 | tls_segment, 0, 0, |
1669 | elfcpp::STT_TLS, | |
1670 | elfcpp::STB_LOCAL, | |
1671 | elfcpp::STV_HIDDEN, 0, | |
183fd0e3 AO |
1672 | (is_exec |
1673 | ? Symbol::SEGMENT_END | |
1674 | : Symbol::SEGMENT_START), | |
1675 | true); | |
edfbb029 CC |
1676 | } |
1677 | this->tls_base_symbol_defined_ = true; | |
1678 | } | |
1679 | ||
c2b45e22 CC |
1680 | // Create the reserved PLT and GOT entries for the TLS descriptor resolver. |
1681 | ||
1682 | void | |
1683 | Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab, | |
1684 | Layout* layout) | |
1685 | { | |
1686 | if (this->plt_ == NULL) | |
1687 | this->make_plt_section(symtab, layout); | |
1688 | ||
1689 | if (!this->plt_->has_tlsdesc_entry()) | |
1690 | { | |
1691 | // Allocate the TLSDESC_GOT entry. | |
1692 | Output_data_got<64, false>* got = this->got_section(symtab, layout); | |
1693 | unsigned int got_offset = got->add_constant(0); | |
1694 | ||
1695 | // Allocate the TLSDESC_PLT entry. | |
1696 | this->plt_->reserve_tlsdesc_entry(got_offset); | |
1697 | } | |
1698 | } | |
1699 | ||
31d60480 ILT |
1700 | // Create a GOT entry for the TLS module index. |
1701 | ||
1702 | unsigned int | |
1703 | Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout, | |
6fa2a40b | 1704 | Sized_relobj_file<64, false>* object) |
31d60480 ILT |
1705 | { |
1706 | if (this->got_mod_index_offset_ == -1U) | |
1707 | { | |
1708 | gold_assert(symtab != NULL && layout != NULL && object != NULL); | |
1709 | Reloc_section* rela_dyn = this->rela_dyn_section(layout); | |
1710 | Output_data_got<64, false>* got = this->got_section(symtab, layout); | |
1711 | unsigned int got_offset = got->add_constant(0); | |
1712 | rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got, | |
1713 | got_offset, 0); | |
009a67a2 | 1714 | got->add_constant(0); |
31d60480 ILT |
1715 | this->got_mod_index_offset_ = got_offset; |
1716 | } | |
1717 | return this->got_mod_index_offset_; | |
1718 | } | |
1719 | ||
2e30d253 ILT |
1720 | // Optimize the TLS relocation type based on what we know about the |
1721 | // symbol. IS_FINAL is true if the final address of this symbol is | |
1722 | // known at link time. | |
1723 | ||
e041f13d | 1724 | tls::Tls_optimization |
2e30d253 ILT |
1725 | Target_x86_64::optimize_tls_reloc(bool is_final, int r_type) |
1726 | { | |
2e30d253 ILT |
1727 | // If we are generating a shared library, then we can't do anything |
1728 | // in the linker. | |
8851ecca | 1729 | if (parameters->options().shared()) |
e041f13d | 1730 | return tls::TLSOPT_NONE; |
2e30d253 ILT |
1731 | |
1732 | switch (r_type) | |
1733 | { | |
1734 | case elfcpp::R_X86_64_TLSGD: | |
e041f13d ILT |
1735 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
1736 | case elfcpp::R_X86_64_TLSDESC_CALL: | |
1737 | // These are General-Dynamic which permits fully general TLS | |
2e30d253 ILT |
1738 | // access. Since we know that we are generating an executable, |
1739 | // we can convert this to Initial-Exec. If we also know that | |
1740 | // this is a local symbol, we can further switch to Local-Exec. | |
1741 | if (is_final) | |
e041f13d ILT |
1742 | return tls::TLSOPT_TO_LE; |
1743 | return tls::TLSOPT_TO_IE; | |
2e30d253 | 1744 | |
d61c17ea | 1745 | case elfcpp::R_X86_64_TLSLD: |
2e30d253 ILT |
1746 | // This is Local-Dynamic, which refers to a local symbol in the |
1747 | // dynamic TLS block. Since we know that we generating an | |
1748 | // executable, we can switch to Local-Exec. | |
e041f13d | 1749 | return tls::TLSOPT_TO_LE; |
2e30d253 | 1750 | |
0ffd9845 | 1751 | case elfcpp::R_X86_64_DTPOFF32: |
0ffd9845 ILT |
1752 | case elfcpp::R_X86_64_DTPOFF64: |
1753 | // Another Local-Dynamic reloc. | |
e041f13d | 1754 | return tls::TLSOPT_TO_LE; |
0ffd9845 | 1755 | |
d61c17ea | 1756 | case elfcpp::R_X86_64_GOTTPOFF: |
2e30d253 ILT |
1757 | // These are Initial-Exec relocs which get the thread offset |
1758 | // from the GOT. If we know that we are linking against the | |
1759 | // local symbol, we can switch to Local-Exec, which links the | |
1760 | // thread offset into the instruction. | |
1761 | if (is_final) | |
e041f13d ILT |
1762 | return tls::TLSOPT_TO_LE; |
1763 | return tls::TLSOPT_NONE; | |
2e30d253 | 1764 | |
d61c17ea | 1765 | case elfcpp::R_X86_64_TPOFF32: |
2e30d253 ILT |
1766 | // When we already have Local-Exec, there is nothing further we |
1767 | // can do. | |
e041f13d | 1768 | return tls::TLSOPT_NONE; |
2e30d253 ILT |
1769 | |
1770 | default: | |
1771 | gold_unreachable(); | |
1772 | } | |
2e30d253 ILT |
1773 | } |
1774 | ||
95a2c8d6 RS |
1775 | // Get the Reference_flags for a particular relocation. |
1776 | ||
1777 | int | |
1778 | Target_x86_64::Scan::get_reference_flags(unsigned int r_type) | |
1779 | { | |
1780 | switch (r_type) | |
1781 | { | |
1782 | case elfcpp::R_X86_64_NONE: | |
1783 | case elfcpp::R_X86_64_GNU_VTINHERIT: | |
1784 | case elfcpp::R_X86_64_GNU_VTENTRY: | |
1785 | case elfcpp::R_X86_64_GOTPC32: | |
1786 | case elfcpp::R_X86_64_GOTPC64: | |
1787 | // No symbol reference. | |
1788 | return 0; | |
1789 | ||
1790 | case elfcpp::R_X86_64_64: | |
1791 | case elfcpp::R_X86_64_32: | |
1792 | case elfcpp::R_X86_64_32S: | |
1793 | case elfcpp::R_X86_64_16: | |
1794 | case elfcpp::R_X86_64_8: | |
1795 | return Symbol::ABSOLUTE_REF; | |
1796 | ||
1797 | case elfcpp::R_X86_64_PC64: | |
1798 | case elfcpp::R_X86_64_PC32: | |
1799 | case elfcpp::R_X86_64_PC16: | |
1800 | case elfcpp::R_X86_64_PC8: | |
1801 | case elfcpp::R_X86_64_GOTOFF64: | |
1802 | return Symbol::RELATIVE_REF; | |
1803 | ||
1804 | case elfcpp::R_X86_64_PLT32: | |
1805 | case elfcpp::R_X86_64_PLTOFF64: | |
1806 | return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF; | |
1807 | ||
1808 | case elfcpp::R_X86_64_GOT64: | |
1809 | case elfcpp::R_X86_64_GOT32: | |
1810 | case elfcpp::R_X86_64_GOTPCREL64: | |
1811 | case elfcpp::R_X86_64_GOTPCREL: | |
1812 | case elfcpp::R_X86_64_GOTPLT64: | |
1813 | // Absolute in GOT. | |
1814 | return Symbol::ABSOLUTE_REF; | |
1815 | ||
1816 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic | |
1817 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) | |
1818 | case elfcpp::R_X86_64_TLSDESC_CALL: | |
1819 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic | |
1820 | case elfcpp::R_X86_64_DTPOFF32: | |
1821 | case elfcpp::R_X86_64_DTPOFF64: | |
1822 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec | |
1823 | case elfcpp::R_X86_64_TPOFF32: // Local-exec | |
1824 | return Symbol::TLS_REF; | |
1825 | ||
1826 | case elfcpp::R_X86_64_COPY: | |
1827 | case elfcpp::R_X86_64_GLOB_DAT: | |
1828 | case elfcpp::R_X86_64_JUMP_SLOT: | |
1829 | case elfcpp::R_X86_64_RELATIVE: | |
1830 | case elfcpp::R_X86_64_IRELATIVE: | |
1831 | case elfcpp::R_X86_64_TPOFF64: | |
1832 | case elfcpp::R_X86_64_DTPMOD64: | |
1833 | case elfcpp::R_X86_64_TLSDESC: | |
1834 | case elfcpp::R_X86_64_SIZE32: | |
1835 | case elfcpp::R_X86_64_SIZE64: | |
1836 | default: | |
1837 | // Not expected. We will give an error later. | |
1838 | return 0; | |
1839 | } | |
1840 | } | |
1841 | ||
e041f13d ILT |
1842 | // Report an unsupported relocation against a local symbol. |
1843 | ||
1844 | void | |
6fa2a40b CC |
1845 | Target_x86_64::Scan::unsupported_reloc_local( |
1846 | Sized_relobj_file<64, false>* object, | |
1847 | unsigned int r_type) | |
e041f13d | 1848 | { |
75f2446e ILT |
1849 | gold_error(_("%s: unsupported reloc %u against local symbol"), |
1850 | object->name().c_str(), r_type); | |
e041f13d ILT |
1851 | } |
1852 | ||
a036edd8 ILT |
1853 | // We are about to emit a dynamic relocation of type R_TYPE. If the |
1854 | // dynamic linker does not support it, issue an error. The GNU linker | |
1855 | // only issues a non-PIC error for an allocated read-only section. | |
1856 | // Here we know the section is allocated, but we don't know that it is | |
1857 | // read-only. But we check for all the relocation types which the | |
1858 | // glibc dynamic linker supports, so it seems appropriate to issue an | |
a29b0dad ILT |
1859 | // error even if the section is not read-only. If GSYM is not NULL, |
1860 | // it is the symbol the relocation is against; if it is NULL, the | |
1861 | // relocation is against a local symbol. | |
a036edd8 ILT |
1862 | |
1863 | void | |
a29b0dad ILT |
1864 | Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type, |
1865 | Symbol* gsym) | |
a036edd8 ILT |
1866 | { |
1867 | switch (r_type) | |
1868 | { | |
2fbb4320 ILT |
1869 | // These are the relocation types supported by glibc for x86_64 |
1870 | // which should always work. | |
a036edd8 | 1871 | case elfcpp::R_X86_64_RELATIVE: |
7223e9ca | 1872 | case elfcpp::R_X86_64_IRELATIVE: |
a036edd8 ILT |
1873 | case elfcpp::R_X86_64_GLOB_DAT: |
1874 | case elfcpp::R_X86_64_JUMP_SLOT: | |
1875 | case elfcpp::R_X86_64_DTPMOD64: | |
1876 | case elfcpp::R_X86_64_DTPOFF64: | |
1877 | case elfcpp::R_X86_64_TPOFF64: | |
1878 | case elfcpp::R_X86_64_64: | |
2fbb4320 ILT |
1879 | case elfcpp::R_X86_64_COPY: |
1880 | return; | |
1881 | ||
1882 | // glibc supports these reloc types, but they can overflow. | |
a036edd8 | 1883 | case elfcpp::R_X86_64_PC32: |
a29b0dad ILT |
1884 | // A PC relative reference is OK against a local symbol or if |
1885 | // the symbol is defined locally. | |
1886 | if (gsym == NULL | |
1887 | || (!gsym->is_from_dynobj() | |
1888 | && !gsym->is_undefined() | |
1889 | && !gsym->is_preemptible())) | |
1890 | return; | |
1891 | /* Fall through. */ | |
1892 | case elfcpp::R_X86_64_32: | |
2fbb4320 ILT |
1893 | if (this->issued_non_pic_error_) |
1894 | return; | |
1895 | gold_assert(parameters->options().output_is_position_independent()); | |
a29b0dad ILT |
1896 | if (gsym == NULL) |
1897 | object->error(_("requires dynamic R_X86_64_32 reloc which may " | |
1898 | "overflow at runtime; recompile with -fPIC")); | |
1899 | else | |
1900 | object->error(_("requires dynamic %s reloc against '%s' which may " | |
1901 | "overflow at runtime; recompile with -fPIC"), | |
1902 | (r_type == elfcpp::R_X86_64_32 | |
1903 | ? "R_X86_64_32" | |
1904 | : "R_X86_64_PC32"), | |
1905 | gsym->name()); | |
2fbb4320 | 1906 | this->issued_non_pic_error_ = true; |
a036edd8 ILT |
1907 | return; |
1908 | ||
1909 | default: | |
1910 | // This prevents us from issuing more than one error per reloc | |
1911 | // section. But we can still wind up issuing more than one | |
1912 | // error per object file. | |
1913 | if (this->issued_non_pic_error_) | |
1914 | return; | |
33aea2fd | 1915 | gold_assert(parameters->options().output_is_position_independent()); |
a29b0dad ILT |
1916 | object->error(_("requires unsupported dynamic reloc %u; " |
1917 | "recompile with -fPIC"), | |
1918 | r_type); | |
a036edd8 ILT |
1919 | this->issued_non_pic_error_ = true; |
1920 | return; | |
1921 | ||
1922 | case elfcpp::R_X86_64_NONE: | |
1923 | gold_unreachable(); | |
1924 | } | |
1925 | } | |
1926 | ||
7223e9ca ILT |
1927 | // Return whether we need to make a PLT entry for a relocation of the |
1928 | // given type against a STT_GNU_IFUNC symbol. | |
1929 | ||
1930 | bool | |
6fa2a40b CC |
1931 | Target_x86_64::Scan::reloc_needs_plt_for_ifunc( |
1932 | Sized_relobj_file<64, false>* object, | |
1933 | unsigned int r_type) | |
7223e9ca | 1934 | { |
95a2c8d6 RS |
1935 | int flags = Scan::get_reference_flags(r_type); |
1936 | if (flags & Symbol::TLS_REF) | |
1937 | gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"), | |
1938 | object->name().c_str(), r_type); | |
1939 | return flags != 0; | |
7223e9ca ILT |
1940 | } |
1941 | ||
2e30d253 ILT |
1942 | // Scan a relocation for a local symbol. |
1943 | ||
1944 | inline void | |
ad0f2072 | 1945 | Target_x86_64::Scan::local(Symbol_table* symtab, |
d61c17ea ILT |
1946 | Layout* layout, |
1947 | Target_x86_64* target, | |
6fa2a40b | 1948 | Sized_relobj_file<64, false>* object, |
0ffd9845 | 1949 | unsigned int data_shndx, |
4f4c5f80 | 1950 | Output_section* output_section, |
0ffd9845 | 1951 | const elfcpp::Rela<64, false>& reloc, |
d61c17ea | 1952 | unsigned int r_type, |
7bf1f802 | 1953 | const elfcpp::Sym<64, false>& lsym) |
2e30d253 | 1954 | { |
7223e9ca ILT |
1955 | // A local STT_GNU_IFUNC symbol may require a PLT entry. |
1956 | if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC | |
1957 | && this->reloc_needs_plt_for_ifunc(object, r_type)) | |
1958 | { | |
1959 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); | |
1960 | target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym); | |
1961 | } | |
1962 | ||
2e30d253 ILT |
1963 | switch (r_type) |
1964 | { | |
1965 | case elfcpp::R_X86_64_NONE: | |
6e5710ce ILT |
1966 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
1967 | case elfcpp::R_X86_64_GNU_VTENTRY: | |
2e30d253 ILT |
1968 | break; |
1969 | ||
1970 | case elfcpp::R_X86_64_64: | |
d61c6bd4 | 1971 | // If building a shared library (or a position-independent |
dceae3c1 ILT |
1972 | // executable), we need to create a dynamic relocation for this |
1973 | // location. The relocation applied at link time will apply the | |
1974 | // link-time value, so we flag the location with an | |
1975 | // R_X86_64_RELATIVE relocation so the dynamic loader can | |
d61c6bd4 | 1976 | // relocate it easily. |
8851ecca | 1977 | if (parameters->options().output_is_position_independent()) |
d61c6bd4 | 1978 | { |
e8c846c3 | 1979 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
d61c6bd4 | 1980 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
7223e9ca ILT |
1981 | rela_dyn->add_local_relative(object, r_sym, |
1982 | elfcpp::R_X86_64_RELATIVE, | |
1983 | output_section, data_shndx, | |
1984 | reloc.get_r_offset(), | |
1985 | reloc.get_r_addend()); | |
d61c6bd4 ILT |
1986 | } |
1987 | break; | |
1988 | ||
2e30d253 ILT |
1989 | case elfcpp::R_X86_64_32: |
1990 | case elfcpp::R_X86_64_32S: | |
1991 | case elfcpp::R_X86_64_16: | |
1992 | case elfcpp::R_X86_64_8: | |
96f2030e | 1993 | // If building a shared library (or a position-independent |
dceae3c1 ILT |
1994 | // executable), we need to create a dynamic relocation for this |
1995 | // location. We can't use an R_X86_64_RELATIVE relocation | |
1996 | // because that is always a 64-bit relocation. | |
8851ecca | 1997 | if (parameters->options().output_is_position_independent()) |
96f2030e | 1998 | { |
a29b0dad | 1999 | this->check_non_pic(object, r_type, NULL); |
a036edd8 | 2000 | |
96f2030e | 2001 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
d491d34e | 2002 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
dceae3c1 | 2003 | if (lsym.get_st_type() != elfcpp::STT_SECTION) |
d491d34e ILT |
2004 | rela_dyn->add_local(object, r_sym, r_type, output_section, |
2005 | data_shndx, reloc.get_r_offset(), | |
2006 | reloc.get_r_addend()); | |
dceae3c1 ILT |
2007 | else |
2008 | { | |
2009 | gold_assert(lsym.get_st_value() == 0); | |
d491d34e ILT |
2010 | unsigned int shndx = lsym.get_st_shndx(); |
2011 | bool is_ordinary; | |
2012 | shndx = object->adjust_sym_shndx(r_sym, shndx, | |
2013 | &is_ordinary); | |
2014 | if (!is_ordinary) | |
2015 | object->error(_("section symbol %u has bad shndx %u"), | |
2016 | r_sym, shndx); | |
2017 | else | |
2018 | rela_dyn->add_local_section(object, shndx, | |
2019 | r_type, output_section, | |
2020 | data_shndx, reloc.get_r_offset(), | |
2021 | reloc.get_r_addend()); | |
dceae3c1 | 2022 | } |
96f2030e | 2023 | } |
2e30d253 ILT |
2024 | break; |
2025 | ||
2026 | case elfcpp::R_X86_64_PC64: | |
2027 | case elfcpp::R_X86_64_PC32: | |
2028 | case elfcpp::R_X86_64_PC16: | |
2029 | case elfcpp::R_X86_64_PC8: | |
2030 | break; | |
2031 | ||
f389a824 ILT |
2032 | case elfcpp::R_X86_64_PLT32: |
2033 | // Since we know this is a local symbol, we can handle this as a | |
2034 | // PC32 reloc. | |
2035 | break; | |
2036 | ||
fdc2f80f | 2037 | case elfcpp::R_X86_64_GOTPC32: |
e822f2b1 | 2038 | case elfcpp::R_X86_64_GOTOFF64: |
fdc2f80f ILT |
2039 | case elfcpp::R_X86_64_GOTPC64: |
2040 | case elfcpp::R_X86_64_PLTOFF64: | |
2e30d253 ILT |
2041 | // We need a GOT section. |
2042 | target->got_section(symtab, layout); | |
ee9e9e86 ILT |
2043 | // For PLTOFF64, we'd normally want a PLT section, but since we |
2044 | // know this is a local symbol, no PLT is needed. | |
2e30d253 ILT |
2045 | break; |
2046 | ||
0ffd9845 ILT |
2047 | case elfcpp::R_X86_64_GOT64: |
2048 | case elfcpp::R_X86_64_GOT32: | |
2049 | case elfcpp::R_X86_64_GOTPCREL64: | |
2050 | case elfcpp::R_X86_64_GOTPCREL: | |
ee9e9e86 | 2051 | case elfcpp::R_X86_64_GOTPLT64: |
0ffd9845 ILT |
2052 | { |
2053 | // The symbol requires a GOT entry. | |
2054 | Output_data_got<64, false>* got = target->got_section(symtab, layout); | |
2055 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); | |
7223e9ca ILT |
2056 | |
2057 | // For a STT_GNU_IFUNC symbol we want the PLT offset. That | |
2058 | // lets function pointers compare correctly with shared | |
2059 | // libraries. Otherwise we would need an IRELATIVE reloc. | |
2060 | bool is_new; | |
2061 | if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC) | |
2062 | is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD); | |
2063 | else | |
2064 | is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD); | |
2065 | if (is_new) | |
0ffd9845 ILT |
2066 | { |
2067 | // If we are generating a shared object, we need to add a | |
7bf1f802 | 2068 | // dynamic relocation for this symbol's GOT entry. |
8851ecca | 2069 | if (parameters->options().output_is_position_independent()) |
0ffd9845 ILT |
2070 | { |
2071 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
7bf1f802 ILT |
2072 | // R_X86_64_RELATIVE assumes a 64-bit relocation. |
2073 | if (r_type != elfcpp::R_X86_64_GOT32) | |
7223e9ca ILT |
2074 | { |
2075 | unsigned int got_offset = | |
2076 | object->local_got_offset(r_sym, GOT_TYPE_STANDARD); | |
2077 | rela_dyn->add_local_relative(object, r_sym, | |
2078 | elfcpp::R_X86_64_RELATIVE, | |
2079 | got, got_offset, 0); | |
2080 | } | |
7bf1f802 | 2081 | else |
dceae3c1 | 2082 | { |
a29b0dad | 2083 | this->check_non_pic(object, r_type, NULL); |
a036edd8 | 2084 | |
dceae3c1 | 2085 | gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION); |
0a65a3a7 CC |
2086 | rela_dyn->add_local( |
2087 | object, r_sym, r_type, got, | |
2088 | object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0); | |
dceae3c1 | 2089 | } |
0ffd9845 ILT |
2090 | } |
2091 | } | |
ee9e9e86 ILT |
2092 | // For GOTPLT64, we'd normally want a PLT section, but since |
2093 | // we know this is a local symbol, no PLT is needed. | |
0ffd9845 ILT |
2094 | } |
2095 | break; | |
2096 | ||
2e30d253 ILT |
2097 | case elfcpp::R_X86_64_COPY: |
2098 | case elfcpp::R_X86_64_GLOB_DAT: | |
2099 | case elfcpp::R_X86_64_JUMP_SLOT: | |
2100 | case elfcpp::R_X86_64_RELATIVE: | |
7223e9ca | 2101 | case elfcpp::R_X86_64_IRELATIVE: |
d61c17ea | 2102 | // These are outstanding tls relocs, which are unexpected when linking |
2e30d253 | 2103 | case elfcpp::R_X86_64_TPOFF64: |
2e30d253 | 2104 | case elfcpp::R_X86_64_DTPMOD64: |
2e30d253 | 2105 | case elfcpp::R_X86_64_TLSDESC: |
75f2446e ILT |
2106 | gold_error(_("%s: unexpected reloc %u in object file"), |
2107 | object->name().c_str(), r_type); | |
2e30d253 ILT |
2108 | break; |
2109 | ||
d61c17ea | 2110 | // These are initial tls relocs, which are expected when linking |
56622147 ILT |
2111 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
2112 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) | |
e041f13d | 2113 | case elfcpp::R_X86_64_TLSDESC_CALL: |
56622147 | 2114 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
0ffd9845 ILT |
2115 | case elfcpp::R_X86_64_DTPOFF32: |
2116 | case elfcpp::R_X86_64_DTPOFF64: | |
56622147 ILT |
2117 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
2118 | case elfcpp::R_X86_64_TPOFF32: // Local-exec | |
2e30d253 | 2119 | { |
8851ecca | 2120 | bool output_is_shared = parameters->options().shared(); |
e041f13d ILT |
2121 | const tls::Tls_optimization optimized_type |
2122 | = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type); | |
2e30d253 ILT |
2123 | switch (r_type) |
2124 | { | |
56622147 | 2125 | case elfcpp::R_X86_64_TLSGD: // General-dynamic |
7bf1f802 ILT |
2126 | if (optimized_type == tls::TLSOPT_NONE) |
2127 | { | |
2128 | // Create a pair of GOT entries for the module index and | |
2129 | // dtv-relative offset. | |
2130 | Output_data_got<64, false>* got | |
2131 | = target->got_section(symtab, layout); | |
2132 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); | |
d491d34e ILT |
2133 | unsigned int shndx = lsym.get_st_shndx(); |
2134 | bool is_ordinary; | |
2135 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); | |
2136 | if (!is_ordinary) | |
2137 | object->error(_("local symbol %u has bad shndx %u"), | |
2138 | r_sym, shndx); | |
2139 | else | |
2140 | got->add_local_pair_with_rela(object, r_sym, | |
2141 | shndx, | |
2142 | GOT_TYPE_TLS_PAIR, | |
2143 | target->rela_dyn_section(layout), | |
2144 | elfcpp::R_X86_64_DTPMOD64, 0); | |
7bf1f802 ILT |
2145 | } |
2146 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
2147 | unsupported_reloc_local(object, r_type); | |
2148 | break; | |
2149 | ||
56622147 | 2150 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
edfbb029 | 2151 | target->define_tls_base_symbol(symtab, layout); |
c2b45e22 CC |
2152 | if (optimized_type == tls::TLSOPT_NONE) |
2153 | { | |
2154 | // Create reserved PLT and GOT entries for the resolver. | |
2155 | target->reserve_tlsdesc_entries(symtab, layout); | |
2156 | ||
a8df5856 ILT |
2157 | // Generate a double GOT entry with an |
2158 | // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc | |
2159 | // is resolved lazily, so the GOT entry needs to be in | |
2160 | // an area in .got.plt, not .got. Call got_section to | |
2161 | // make sure the section has been created. | |
2162 | target->got_section(symtab, layout); | |
2163 | Output_data_got<64, false>* got = target->got_tlsdesc_section(); | |
c2b45e22 | 2164 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
e291e7b9 ILT |
2165 | if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC)) |
2166 | { | |
2167 | unsigned int got_offset = got->add_constant(0); | |
2168 | got->add_constant(0); | |
2169 | object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC, | |
2170 | got_offset); | |
2171 | Reloc_section* rt = target->rela_tlsdesc_section(layout); | |
2172 | // We store the arguments we need in a vector, and | |
2173 | // use the index into the vector as the parameter | |
2174 | // to pass to the target specific routines. | |
2175 | uintptr_t intarg = target->add_tlsdesc_info(object, r_sym); | |
2176 | void* arg = reinterpret_cast<void*>(intarg); | |
2177 | rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg, | |
2178 | got, got_offset, 0); | |
2179 | } | |
c2b45e22 CC |
2180 | } |
2181 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
56622147 | 2182 | unsupported_reloc_local(object, r_type); |
2e30d253 ILT |
2183 | break; |
2184 | ||
c2b45e22 CC |
2185 | case elfcpp::R_X86_64_TLSDESC_CALL: |
2186 | break; | |
2187 | ||
e041f13d | 2188 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
7bf1f802 ILT |
2189 | if (optimized_type == tls::TLSOPT_NONE) |
2190 | { | |
2191 | // Create a GOT entry for the module index. | |
31d60480 | 2192 | target->got_mod_index_entry(symtab, layout, object); |
7bf1f802 ILT |
2193 | } |
2194 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
2195 | unsupported_reloc_local(object, r_type); | |
2196 | break; | |
2197 | ||
0ffd9845 ILT |
2198 | case elfcpp::R_X86_64_DTPOFF32: |
2199 | case elfcpp::R_X86_64_DTPOFF64: | |
e041f13d ILT |
2200 | break; |
2201 | ||
56622147 | 2202 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
535890bb | 2203 | layout->set_has_static_tls(); |
7bf1f802 ILT |
2204 | if (optimized_type == tls::TLSOPT_NONE) |
2205 | { | |
2206 | // Create a GOT entry for the tp-relative offset. | |
2207 | Output_data_got<64, false>* got | |
2208 | = target->got_section(symtab, layout); | |
2209 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); | |
0a65a3a7 | 2210 | got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET, |
7bf1f802 ILT |
2211 | target->rela_dyn_section(layout), |
2212 | elfcpp::R_X86_64_TPOFF64); | |
2213 | } | |
2214 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
56622147 ILT |
2215 | unsupported_reloc_local(object, r_type); |
2216 | break; | |
0ffd9845 | 2217 | |
56622147 | 2218 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
535890bb | 2219 | layout->set_has_static_tls(); |
7bf1f802 ILT |
2220 | if (output_is_shared) |
2221 | unsupported_reloc_local(object, r_type); | |
2e30d253 | 2222 | break; |
e041f13d ILT |
2223 | |
2224 | default: | |
2225 | gold_unreachable(); | |
2e30d253 ILT |
2226 | } |
2227 | } | |
2228 | break; | |
2e30d253 | 2229 | |
fdc2f80f ILT |
2230 | case elfcpp::R_X86_64_SIZE32: |
2231 | case elfcpp::R_X86_64_SIZE64: | |
2e30d253 | 2232 | default: |
75f2446e ILT |
2233 | gold_error(_("%s: unsupported reloc %u against local symbol"), |
2234 | object->name().c_str(), r_type); | |
2e30d253 ILT |
2235 | break; |
2236 | } | |
2237 | } | |
2238 | ||
2239 | ||
e041f13d ILT |
2240 | // Report an unsupported relocation against a global symbol. |
2241 | ||
2242 | void | |
6fa2a40b CC |
2243 | Target_x86_64::Scan::unsupported_reloc_global( |
2244 | Sized_relobj_file<64, false>* object, | |
2245 | unsigned int r_type, | |
2246 | Symbol* gsym) | |
e041f13d | 2247 | { |
75f2446e | 2248 | gold_error(_("%s: unsupported reloc %u against global symbol %s"), |
a2b1aa12 | 2249 | object->name().c_str(), r_type, gsym->demangled_name().c_str()); |
e041f13d ILT |
2250 | } |
2251 | ||
ce97fa81 | 2252 | // Returns true if this relocation type could be that of a function pointer. |
21bb3914 ST |
2253 | inline bool |
2254 | Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type) | |
2255 | { | |
21bb3914 ST |
2256 | switch (r_type) |
2257 | { | |
2258 | case elfcpp::R_X86_64_64: | |
2259 | case elfcpp::R_X86_64_32: | |
2260 | case elfcpp::R_X86_64_32S: | |
2261 | case elfcpp::R_X86_64_16: | |
2262 | case elfcpp::R_X86_64_8: | |
ce97fa81 ST |
2263 | case elfcpp::R_X86_64_GOT64: |
2264 | case elfcpp::R_X86_64_GOT32: | |
2265 | case elfcpp::R_X86_64_GOTPCREL64: | |
2266 | case elfcpp::R_X86_64_GOTPCREL: | |
2267 | case elfcpp::R_X86_64_GOTPLT64: | |
21bb3914 ST |
2268 | { |
2269 | return true; | |
2270 | } | |
2271 | } | |
2272 | return false; | |
2273 | } | |
2274 | ||
2275 | // For safe ICF, scan a relocation for a local symbol to check if it | |
2276 | // corresponds to a function pointer being taken. In that case mark | |
2277 | // the function whose pointer was taken as not foldable. | |
2278 | ||
2279 | inline bool | |
2280 | Target_x86_64::Scan::local_reloc_may_be_function_pointer( | |
2281 | Symbol_table* , | |
2282 | Layout* , | |
2283 | Target_x86_64* , | |
6fa2a40b | 2284 | Sized_relobj_file<64, false>* , |
21bb3914 ST |
2285 | unsigned int , |
2286 | Output_section* , | |
2287 | const elfcpp::Rela<64, false>& , | |
2288 | unsigned int r_type, | |
2289 | const elfcpp::Sym<64, false>&) | |
2290 | { | |
2291 | // When building a shared library, do not fold any local symbols as it is | |
2292 | // not possible to distinguish pointer taken versus a call by looking at | |
2293 | // the relocation types. | |
2294 | return (parameters->options().shared() | |
2295 | || possible_function_pointer_reloc(r_type)); | |
2296 | } | |
2297 | ||
2298 | // For safe ICF, scan a relocation for a global symbol to check if it | |
2299 | // corresponds to a function pointer being taken. In that case mark | |
2300 | // the function whose pointer was taken as not foldable. | |
2301 | ||
2302 | inline bool | |
2303 | Target_x86_64::Scan::global_reloc_may_be_function_pointer( | |
2304 | Symbol_table*, | |
2305 | Layout* , | |
2306 | Target_x86_64* , | |
6fa2a40b | 2307 | Sized_relobj_file<64, false>* , |
21bb3914 ST |
2308 | unsigned int , |
2309 | Output_section* , | |
2310 | const elfcpp::Rela<64, false>& , | |
2311 | unsigned int r_type, | |
2312 | Symbol* gsym) | |
2313 | { | |
2314 | // When building a shared library, do not fold symbols whose visibility | |
2315 | // is hidden, internal or protected. | |
2316 | return ((parameters->options().shared() | |
2317 | && (gsym->visibility() == elfcpp::STV_INTERNAL | |
2318 | || gsym->visibility() == elfcpp::STV_PROTECTED | |
2319 | || gsym->visibility() == elfcpp::STV_HIDDEN)) | |
2320 | || possible_function_pointer_reloc(r_type)); | |
2321 | } | |
2322 | ||
2e30d253 ILT |
2323 | // Scan a relocation for a global symbol. |
2324 | ||
2325 | inline void | |
ad0f2072 | 2326 | Target_x86_64::Scan::global(Symbol_table* symtab, |
d61c17ea ILT |
2327 | Layout* layout, |
2328 | Target_x86_64* target, | |
6fa2a40b | 2329 | Sized_relobj_file<64, false>* object, |
d61c17ea | 2330 | unsigned int data_shndx, |
4f4c5f80 | 2331 | Output_section* output_section, |
d61c17ea ILT |
2332 | const elfcpp::Rela<64, false>& reloc, |
2333 | unsigned int r_type, | |
2334 | Symbol* gsym) | |
2e30d253 | 2335 | { |
7223e9ca ILT |
2336 | // A STT_GNU_IFUNC symbol may require a PLT entry. |
2337 | if (gsym->type() == elfcpp::STT_GNU_IFUNC | |
2338 | && this->reloc_needs_plt_for_ifunc(object, r_type)) | |
2339 | target->make_plt_entry(symtab, layout, gsym); | |
2340 | ||
2e30d253 ILT |
2341 | switch (r_type) |
2342 | { | |
2343 | case elfcpp::R_X86_64_NONE: | |
6e5710ce ILT |
2344 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
2345 | case elfcpp::R_X86_64_GNU_VTENTRY: | |
2e30d253 ILT |
2346 | break; |
2347 | ||
2348 | case elfcpp::R_X86_64_64: | |
2e30d253 ILT |
2349 | case elfcpp::R_X86_64_32: |
2350 | case elfcpp::R_X86_64_32S: | |
2e30d253 | 2351 | case elfcpp::R_X86_64_16: |
2e30d253 | 2352 | case elfcpp::R_X86_64_8: |
96f2030e | 2353 | { |
d61c6bd4 ILT |
2354 | // Make a PLT entry if necessary. |
2355 | if (gsym->needs_plt_entry()) | |
2356 | { | |
2357 | target->make_plt_entry(symtab, layout, gsym); | |
2358 | // Since this is not a PC-relative relocation, we may be | |
2359 | // taking the address of a function. In that case we need to | |
2360 | // set the entry in the dynamic symbol table to the address of | |
2361 | // the PLT entry. | |
8851ecca | 2362 | if (gsym->is_from_dynobj() && !parameters->options().shared()) |
d61c6bd4 ILT |
2363 | gsym->set_needs_dynsym_value(); |
2364 | } | |
2365 | // Make a dynamic relocation if necessary. | |
95a2c8d6 | 2366 | if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) |
d61c6bd4 | 2367 | { |
966d4097 | 2368 | if (gsym->may_need_copy_reloc()) |
d61c6bd4 | 2369 | { |
12c0daef | 2370 | target->copy_reloc(symtab, layout, object, |
7bf1f802 | 2371 | data_shndx, output_section, gsym, reloc); |
d61c6bd4 | 2372 | } |
7223e9ca ILT |
2373 | else if (r_type == elfcpp::R_X86_64_64 |
2374 | && gsym->type() == elfcpp::STT_GNU_IFUNC | |
2375 | && gsym->can_use_relative_reloc(false) | |
2376 | && !gsym->is_from_dynobj() | |
2377 | && !gsym->is_undefined() | |
2378 | && !gsym->is_preemptible()) | |
2379 | { | |
2380 | // Use an IRELATIVE reloc for a locally defined | |
2381 | // STT_GNU_IFUNC symbol. This makes a function | |
2382 | // address in a PIE executable match the address in a | |
2383 | // shared library that it links against. | |
67181c72 ILT |
2384 | Reloc_section* rela_dyn = |
2385 | target->rela_irelative_section(layout); | |
7223e9ca ILT |
2386 | unsigned int r_type = elfcpp::R_X86_64_IRELATIVE; |
2387 | rela_dyn->add_symbolless_global_addend(gsym, r_type, | |
2388 | output_section, object, | |
2389 | data_shndx, | |
2390 | reloc.get_r_offset(), | |
2391 | reloc.get_r_addend()); | |
2392 | } | |
d61c6bd4 ILT |
2393 | else if (r_type == elfcpp::R_X86_64_64 |
2394 | && gsym->can_use_relative_reloc(false)) | |
2395 | { | |
2396 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
7223e9ca ILT |
2397 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE, |
2398 | output_section, object, | |
2399 | data_shndx, | |
2400 | reloc.get_r_offset(), | |
2401 | reloc.get_r_addend()); | |
d61c6bd4 ILT |
2402 | } |
2403 | else | |
2404 | { | |
a29b0dad | 2405 | this->check_non_pic(object, r_type, gsym); |
96f2030e | 2406 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
4f4c5f80 ILT |
2407 | rela_dyn->add_global(gsym, r_type, output_section, object, |
2408 | data_shndx, reloc.get_r_offset(), | |
96f2030e | 2409 | reloc.get_r_addend()); |
d61c6bd4 ILT |
2410 | } |
2411 | } | |
2412 | } | |
2413 | break; | |
2414 | ||
2415 | case elfcpp::R_X86_64_PC64: | |
2416 | case elfcpp::R_X86_64_PC32: | |
2417 | case elfcpp::R_X86_64_PC16: | |
2418 | case elfcpp::R_X86_64_PC8: | |
2419 | { | |
2420 | // Make a PLT entry if necessary. | |
2421 | if (gsym->needs_plt_entry()) | |
2422 | target->make_plt_entry(symtab, layout, gsym); | |
2423 | // Make a dynamic relocation if necessary. | |
95a2c8d6 | 2424 | if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) |
86849f1f | 2425 | { |
966d4097 | 2426 | if (gsym->may_need_copy_reloc()) |
d61c6bd4 | 2427 | { |
12c0daef | 2428 | target->copy_reloc(symtab, layout, object, |
7bf1f802 | 2429 | data_shndx, output_section, gsym, reloc); |
d61c6bd4 | 2430 | } |
86849f1f | 2431 | else |
d61c6bd4 | 2432 | { |
a29b0dad | 2433 | this->check_non_pic(object, r_type, gsym); |
d61c6bd4 | 2434 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
4f4c5f80 ILT |
2435 | rela_dyn->add_global(gsym, r_type, output_section, object, |
2436 | data_shndx, reloc.get_r_offset(), | |
d61c6bd4 ILT |
2437 | reloc.get_r_addend()); |
2438 | } | |
86849f1f | 2439 | } |
d61c6bd4 | 2440 | } |
2e30d253 ILT |
2441 | break; |
2442 | ||
ff006520 | 2443 | case elfcpp::R_X86_64_GOT64: |
2e30d253 | 2444 | case elfcpp::R_X86_64_GOT32: |
ff006520 ILT |
2445 | case elfcpp::R_X86_64_GOTPCREL64: |
2446 | case elfcpp::R_X86_64_GOTPCREL: | |
2447 | case elfcpp::R_X86_64_GOTPLT64: | |
2e30d253 ILT |
2448 | { |
2449 | // The symbol requires a GOT entry. | |
2450 | Output_data_got<64, false>* got = target->got_section(symtab, layout); | |
7bf1f802 | 2451 | if (gsym->final_value_is_known()) |
7223e9ca ILT |
2452 | { |
2453 | // For a STT_GNU_IFUNC symbol we want the PLT address. | |
2454 | if (gsym->type() == elfcpp::STT_GNU_IFUNC) | |
2455 | got->add_global_plt(gsym, GOT_TYPE_STANDARD); | |
2456 | else | |
2457 | got->add_global(gsym, GOT_TYPE_STANDARD); | |
2458 | } | |
7bf1f802 ILT |
2459 | else |
2460 | { | |
2e30d253 ILT |
2461 | // If this symbol is not fully resolved, we need to add a |
2462 | // dynamic relocation for it. | |
7bf1f802 | 2463 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
07aa62f2 ILT |
2464 | |
2465 | // Use a GLOB_DAT rather than a RELATIVE reloc if: | |
2466 | // | |
2467 | // 1) The symbol may be defined in some other module. | |
2468 | // | |
2469 | // 2) We are building a shared library and this is a | |
2470 | // protected symbol; using GLOB_DAT means that the dynamic | |
2471 | // linker can use the address of the PLT in the main | |
2472 | // executable when appropriate so that function address | |
2473 | // comparisons work. | |
2474 | // | |
2475 | // 3) This is a STT_GNU_IFUNC symbol in position dependent | |
2476 | // code, again so that function address comparisons work. | |
7223e9ca ILT |
2477 | if (gsym->is_from_dynobj() |
2478 | || gsym->is_undefined() | |
2479 | || gsym->is_preemptible() | |
07aa62f2 ILT |
2480 | || (gsym->visibility() == elfcpp::STV_PROTECTED |
2481 | && parameters->options().shared()) | |
7223e9ca ILT |
2482 | || (gsym->type() == elfcpp::STT_GNU_IFUNC |
2483 | && parameters->options().output_is_position_independent())) | |
0a65a3a7 | 2484 | got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn, |
7bf1f802 ILT |
2485 | elfcpp::R_X86_64_GLOB_DAT); |
2486 | else | |
2e30d253 | 2487 | { |
7223e9ca ILT |
2488 | // For a STT_GNU_IFUNC symbol we want to write the PLT |
2489 | // offset into the GOT, so that function pointer | |
2490 | // comparisons work correctly. | |
2491 | bool is_new; | |
2492 | if (gsym->type() != elfcpp::STT_GNU_IFUNC) | |
2493 | is_new = got->add_global(gsym, GOT_TYPE_STANDARD); | |
2494 | else | |
2495 | { | |
2496 | is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD); | |
2497 | // Tell the dynamic linker to use the PLT address | |
2498 | // when resolving relocations. | |
2499 | if (gsym->is_from_dynobj() | |
2500 | && !parameters->options().shared()) | |
2501 | gsym->set_needs_dynsym_value(); | |
2502 | } | |
2503 | if (is_new) | |
2504 | { | |
2505 | unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD); | |
2506 | rela_dyn->add_global_relative(gsym, | |
2507 | elfcpp::R_X86_64_RELATIVE, | |
2508 | got, got_off, 0); | |
2509 | } | |
2e30d253 ILT |
2510 | } |
2511 | } | |
ee9e9e86 ILT |
2512 | // For GOTPLT64, we also need a PLT entry (but only if the |
2513 | // symbol is not fully resolved). | |
2514 | if (r_type == elfcpp::R_X86_64_GOTPLT64 | |
2515 | && !gsym->final_value_is_known()) | |
2516 | target->make_plt_entry(symtab, layout, gsym); | |
2e30d253 ILT |
2517 | } |
2518 | break; | |
2519 | ||
2520 | case elfcpp::R_X86_64_PLT32: | |
2521 | // If the symbol is fully resolved, this is just a PC32 reloc. | |
2522 | // Otherwise we need a PLT entry. | |
2523 | if (gsym->final_value_is_known()) | |
2524 | break; | |
96f2030e ILT |
2525 | // If building a shared library, we can also skip the PLT entry |
2526 | // if the symbol is defined in the output file and is protected | |
2527 | // or hidden. | |
2528 | if (gsym->is_defined() | |
2529 | && !gsym->is_from_dynobj() | |
2530 | && !gsym->is_preemptible()) | |
2531 | break; | |
2e30d253 ILT |
2532 | target->make_plt_entry(symtab, layout, gsym); |
2533 | break; | |
2534 | ||
fdc2f80f | 2535 | case elfcpp::R_X86_64_GOTPC32: |
e822f2b1 | 2536 | case elfcpp::R_X86_64_GOTOFF64: |
fdc2f80f ILT |
2537 | case elfcpp::R_X86_64_GOTPC64: |
2538 | case elfcpp::R_X86_64_PLTOFF64: | |
2e30d253 ILT |
2539 | // We need a GOT section. |
2540 | target->got_section(symtab, layout); | |
ee9e9e86 ILT |
2541 | // For PLTOFF64, we also need a PLT entry (but only if the |
2542 | // symbol is not fully resolved). | |
2543 | if (r_type == elfcpp::R_X86_64_PLTOFF64 | |
2544 | && !gsym->final_value_is_known()) | |
2545 | target->make_plt_entry(symtab, layout, gsym); | |
2e30d253 ILT |
2546 | break; |
2547 | ||
2e30d253 ILT |
2548 | case elfcpp::R_X86_64_COPY: |
2549 | case elfcpp::R_X86_64_GLOB_DAT: | |
2550 | case elfcpp::R_X86_64_JUMP_SLOT: | |
2551 | case elfcpp::R_X86_64_RELATIVE: | |
7223e9ca | 2552 | case elfcpp::R_X86_64_IRELATIVE: |
d61c17ea | 2553 | // These are outstanding tls relocs, which are unexpected when linking |
e822f2b1 | 2554 | case elfcpp::R_X86_64_TPOFF64: |
2e30d253 | 2555 | case elfcpp::R_X86_64_DTPMOD64: |
e822f2b1 | 2556 | case elfcpp::R_X86_64_TLSDESC: |
75f2446e ILT |
2557 | gold_error(_("%s: unexpected reloc %u in object file"), |
2558 | object->name().c_str(), r_type); | |
2e30d253 | 2559 | break; |
2e30d253 | 2560 | |
d61c17ea | 2561 | // These are initial tls relocs, which are expected for global() |
56622147 ILT |
2562 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
2563 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) | |
e041f13d | 2564 | case elfcpp::R_X86_64_TLSDESC_CALL: |
56622147 | 2565 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
0ffd9845 ILT |
2566 | case elfcpp::R_X86_64_DTPOFF32: |
2567 | case elfcpp::R_X86_64_DTPOFF64: | |
56622147 ILT |
2568 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
2569 | case elfcpp::R_X86_64_TPOFF32: // Local-exec | |
2e30d253 ILT |
2570 | { |
2571 | const bool is_final = gsym->final_value_is_known(); | |
e041f13d ILT |
2572 | const tls::Tls_optimization optimized_type |
2573 | = Target_x86_64::optimize_tls_reloc(is_final, r_type); | |
2e30d253 ILT |
2574 | switch (r_type) |
2575 | { | |
56622147 | 2576 | case elfcpp::R_X86_64_TLSGD: // General-dynamic |
7bf1f802 ILT |
2577 | if (optimized_type == tls::TLSOPT_NONE) |
2578 | { | |
2579 | // Create a pair of GOT entries for the module index and | |
2580 | // dtv-relative offset. | |
2581 | Output_data_got<64, false>* got | |
2582 | = target->got_section(symtab, layout); | |
0a65a3a7 CC |
2583 | got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR, |
2584 | target->rela_dyn_section(layout), | |
2585 | elfcpp::R_X86_64_DTPMOD64, | |
2586 | elfcpp::R_X86_64_DTPOFF64); | |
7bf1f802 ILT |
2587 | } |
2588 | else if (optimized_type == tls::TLSOPT_TO_IE) | |
2589 | { | |
2590 | // Create a GOT entry for the tp-relative offset. | |
2591 | Output_data_got<64, false>* got | |
2592 | = target->got_section(symtab, layout); | |
0a65a3a7 | 2593 | got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET, |
7bf1f802 ILT |
2594 | target->rela_dyn_section(layout), |
2595 | elfcpp::R_X86_64_TPOFF64); | |
2596 | } | |
2597 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
2598 | unsupported_reloc_global(object, r_type, gsym); | |
2599 | break; | |
2600 | ||
56622147 | 2601 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
edfbb029 | 2602 | target->define_tls_base_symbol(symtab, layout); |
c2b45e22 CC |
2603 | if (optimized_type == tls::TLSOPT_NONE) |
2604 | { | |
2605 | // Create reserved PLT and GOT entries for the resolver. | |
2606 | target->reserve_tlsdesc_entries(symtab, layout); | |
2607 | ||
a8df5856 ILT |
2608 | // Create a double GOT entry with an R_X86_64_TLSDESC |
2609 | // reloc. The R_X86_64_TLSDESC reloc is resolved | |
2610 | // lazily, so the GOT entry needs to be in an area in | |
2611 | // .got.plt, not .got. Call got_section to make sure | |
2612 | // the section has been created. | |
2613 | target->got_section(symtab, layout); | |
2614 | Output_data_got<64, false>* got = target->got_tlsdesc_section(); | |
ca09d69a | 2615 | Reloc_section* rt = target->rela_tlsdesc_section(layout); |
e291e7b9 | 2616 | got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt, |
c2b45e22 CC |
2617 | elfcpp::R_X86_64_TLSDESC, 0); |
2618 | } | |
2619 | else if (optimized_type == tls::TLSOPT_TO_IE) | |
2620 | { | |
2621 | // Create a GOT entry for the tp-relative offset. | |
2622 | Output_data_got<64, false>* got | |
2623 | = target->got_section(symtab, layout); | |
2624 | got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET, | |
2625 | target->rela_dyn_section(layout), | |
2626 | elfcpp::R_X86_64_TPOFF64); | |
2627 | } | |
2628 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
56622147 | 2629 | unsupported_reloc_global(object, r_type, gsym); |
2e30d253 ILT |
2630 | break; |
2631 | ||
c2b45e22 CC |
2632 | case elfcpp::R_X86_64_TLSDESC_CALL: |
2633 | break; | |
2634 | ||
e041f13d | 2635 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
7bf1f802 ILT |
2636 | if (optimized_type == tls::TLSOPT_NONE) |
2637 | { | |
2638 | // Create a GOT entry for the module index. | |
31d60480 | 2639 | target->got_mod_index_entry(symtab, layout, object); |
7bf1f802 ILT |
2640 | } |
2641 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
2642 | unsupported_reloc_global(object, r_type, gsym); | |
2643 | break; | |
2644 | ||
0ffd9845 ILT |
2645 | case elfcpp::R_X86_64_DTPOFF32: |
2646 | case elfcpp::R_X86_64_DTPOFF64: | |
e041f13d ILT |
2647 | break; |
2648 | ||
56622147 | 2649 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
535890bb | 2650 | layout->set_has_static_tls(); |
7bf1f802 ILT |
2651 | if (optimized_type == tls::TLSOPT_NONE) |
2652 | { | |
2653 | // Create a GOT entry for the tp-relative offset. | |
2654 | Output_data_got<64, false>* got | |
2655 | = target->got_section(symtab, layout); | |
0a65a3a7 | 2656 | got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET, |
7bf1f802 ILT |
2657 | target->rela_dyn_section(layout), |
2658 | elfcpp::R_X86_64_TPOFF64); | |
2659 | } | |
2660 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
56622147 ILT |
2661 | unsupported_reloc_global(object, r_type, gsym); |
2662 | break; | |
0ffd9845 | 2663 | |
56622147 | 2664 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
535890bb | 2665 | layout->set_has_static_tls(); |
8851ecca | 2666 | if (parameters->options().shared()) |
7bf1f802 | 2667 | unsupported_reloc_local(object, r_type); |
2e30d253 | 2668 | break; |
e041f13d ILT |
2669 | |
2670 | default: | |
2671 | gold_unreachable(); | |
2e30d253 ILT |
2672 | } |
2673 | } | |
2674 | break; | |
fdc2f80f ILT |
2675 | |
2676 | case elfcpp::R_X86_64_SIZE32: | |
2677 | case elfcpp::R_X86_64_SIZE64: | |
2e30d253 | 2678 | default: |
75f2446e | 2679 | gold_error(_("%s: unsupported reloc %u against global symbol %s"), |
a2b1aa12 ILT |
2680 | object->name().c_str(), r_type, |
2681 | gsym->demangled_name().c_str()); | |
2e30d253 ILT |
2682 | break; |
2683 | } | |
2684 | } | |
2685 | ||
6d03d481 | 2686 | void |
ad0f2072 | 2687 | Target_x86_64::gc_process_relocs(Symbol_table* symtab, |
6d03d481 | 2688 | Layout* layout, |
6fa2a40b | 2689 | Sized_relobj_file<64, false>* object, |
6d03d481 ST |
2690 | unsigned int data_shndx, |
2691 | unsigned int sh_type, | |
2692 | const unsigned char* prelocs, | |
2693 | size_t reloc_count, | |
2694 | Output_section* output_section, | |
2695 | bool needs_special_offset_handling, | |
2696 | size_t local_symbol_count, | |
2697 | const unsigned char* plocal_symbols) | |
2698 | { | |
2699 | ||
2700 | if (sh_type == elfcpp::SHT_REL) | |
2701 | { | |
2702 | return; | |
2703 | } | |
2704 | ||
2705 | gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA, | |
41cbeecc ST |
2706 | Target_x86_64::Scan, |
2707 | Target_x86_64::Relocatable_size_for_reloc>( | |
6d03d481 ST |
2708 | symtab, |
2709 | layout, | |
2710 | this, | |
2711 | object, | |
2712 | data_shndx, | |
2713 | prelocs, | |
2714 | reloc_count, | |
2715 | output_section, | |
2716 | needs_special_offset_handling, | |
2717 | local_symbol_count, | |
2718 | plocal_symbols); | |
2719 | ||
2720 | } | |
2e30d253 ILT |
2721 | // Scan relocations for a section. |
2722 | ||
2723 | void | |
ad0f2072 | 2724 | Target_x86_64::scan_relocs(Symbol_table* symtab, |
d61c17ea | 2725 | Layout* layout, |
6fa2a40b | 2726 | Sized_relobj_file<64, false>* object, |
d61c17ea ILT |
2727 | unsigned int data_shndx, |
2728 | unsigned int sh_type, | |
2729 | const unsigned char* prelocs, | |
2730 | size_t reloc_count, | |
730cdc88 ILT |
2731 | Output_section* output_section, |
2732 | bool needs_special_offset_handling, | |
d61c17ea | 2733 | size_t local_symbol_count, |
730cdc88 | 2734 | const unsigned char* plocal_symbols) |
2e30d253 ILT |
2735 | { |
2736 | if (sh_type == elfcpp::SHT_REL) | |
2737 | { | |
75f2446e ILT |
2738 | gold_error(_("%s: unsupported REL reloc section"), |
2739 | object->name().c_str()); | |
2740 | return; | |
2e30d253 ILT |
2741 | } |
2742 | ||
2743 | gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA, | |
2744 | Target_x86_64::Scan>( | |
2e30d253 ILT |
2745 | symtab, |
2746 | layout, | |
2747 | this, | |
2748 | object, | |
2749 | data_shndx, | |
2750 | prelocs, | |
2751 | reloc_count, | |
730cdc88 ILT |
2752 | output_section, |
2753 | needs_special_offset_handling, | |
2e30d253 | 2754 | local_symbol_count, |
730cdc88 | 2755 | plocal_symbols); |
2e30d253 ILT |
2756 | } |
2757 | ||
2758 | // Finalize the sections. | |
2759 | ||
2760 | void | |
f59f41f3 DK |
2761 | Target_x86_64::do_finalize_sections( |
2762 | Layout* layout, | |
2763 | const Input_objects*, | |
e785ec03 | 2764 | Symbol_table* symtab) |
2e30d253 | 2765 | { |
ea715a34 ILT |
2766 | const Reloc_section* rel_plt = (this->plt_ == NULL |
2767 | ? NULL | |
e291e7b9 | 2768 | : this->plt_->rela_plt()); |
ea715a34 | 2769 | layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt, |
612a8d3d | 2770 | this->rela_dyn_, true, false); |
ea715a34 | 2771 | |
2e30d253 ILT |
2772 | // Fill in some more dynamic tags. |
2773 | Output_data_dynamic* const odyn = layout->dynamic_data(); | |
2774 | if (odyn != NULL) | |
2775 | { | |
22b127cc | 2776 | if (this->plt_ != NULL |
ea715a34 ILT |
2777 | && this->plt_->output_section() != NULL |
2778 | && this->plt_->has_tlsdesc_entry()) | |
2e30d253 | 2779 | { |
ea715a34 ILT |
2780 | unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset(); |
2781 | unsigned int got_offset = this->plt_->get_tlsdesc_got_offset(); | |
2782 | this->got_->finalize_data_size(); | |
2783 | odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT, | |
2784 | this->plt_, plt_offset); | |
2785 | odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT, | |
2786 | this->got_, got_offset); | |
2e30d253 ILT |
2787 | } |
2788 | } | |
2789 | ||
2790 | // Emit any relocs we saved in an attempt to avoid generating COPY | |
2791 | // relocs. | |
12c0daef ILT |
2792 | if (this->copy_relocs_.any_saved_relocs()) |
2793 | this->copy_relocs_.emit(this->rela_dyn_section(layout)); | |
e785ec03 ILT |
2794 | |
2795 | // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of | |
2796 | // the .got.plt section. | |
2797 | Symbol* sym = this->global_offset_table_; | |
2798 | if (sym != NULL) | |
2799 | { | |
2800 | uint64_t data_size = this->got_plt_->current_data_size(); | |
2801 | symtab->get_sized_symbol<64>(sym)->set_symsize(data_size); | |
2802 | } | |
28a13fec | 2803 | |
67181c72 ILT |
2804 | if (parameters->doing_static_link() |
2805 | && (this->plt_ == NULL || !this->plt_->has_irelative_section())) | |
28a13fec ILT |
2806 | { |
2807 | // If linking statically, make sure that the __rela_iplt symbols | |
2808 | // were defined if necessary, even if we didn't create a PLT. | |
2809 | static const Define_symbol_in_segment syms[] = | |
2810 | { | |
2811 | { | |
2812 | "__rela_iplt_start", // name | |
2813 | elfcpp::PT_LOAD, // segment_type | |
2814 | elfcpp::PF_W, // segment_flags_set | |
2815 | elfcpp::PF(0), // segment_flags_clear | |
2816 | 0, // value | |
2817 | 0, // size | |
2818 | elfcpp::STT_NOTYPE, // type | |
2819 | elfcpp::STB_GLOBAL, // binding | |
2820 | elfcpp::STV_HIDDEN, // visibility | |
2821 | 0, // nonvis | |
2822 | Symbol::SEGMENT_START, // offset_from_base | |
2823 | true // only_if_ref | |
2824 | }, | |
2825 | { | |
2826 | "__rela_iplt_end", // name | |
2827 | elfcpp::PT_LOAD, // segment_type | |
2828 | elfcpp::PF_W, // segment_flags_set | |
2829 | elfcpp::PF(0), // segment_flags_clear | |
2830 | 0, // value | |
2831 | 0, // size | |
2832 | elfcpp::STT_NOTYPE, // type | |
2833 | elfcpp::STB_GLOBAL, // binding | |
2834 | elfcpp::STV_HIDDEN, // visibility | |
2835 | 0, // nonvis | |
2836 | Symbol::SEGMENT_START, // offset_from_base | |
2837 | true // only_if_ref | |
2838 | } | |
2839 | }; | |
2840 | ||
2841 | symtab->define_symbols(layout, 2, syms, | |
2842 | layout->script_options()->saw_sections_clause()); | |
2843 | } | |
2e30d253 ILT |
2844 | } |
2845 | ||
2846 | // Perform a relocation. | |
2847 | ||
2848 | inline bool | |
2849 | Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo, | |
2850 | Target_x86_64* target, | |
031cdbed | 2851 | Output_section*, |
2e30d253 | 2852 | size_t relnum, |
0ffd9845 | 2853 | const elfcpp::Rela<64, false>& rela, |
2e30d253 ILT |
2854 | unsigned int r_type, |
2855 | const Sized_symbol<64>* gsym, | |
2856 | const Symbol_value<64>* psymval, | |
2857 | unsigned char* view, | |
2858 | elfcpp::Elf_types<64>::Elf_Addr address, | |
fe8718a4 | 2859 | section_size_type view_size) |
2e30d253 ILT |
2860 | { |
2861 | if (this->skip_call_tls_get_addr_) | |
2862 | { | |
5efc7cd2 CC |
2863 | if ((r_type != elfcpp::R_X86_64_PLT32 |
2864 | && r_type != elfcpp::R_X86_64_PC32) | |
2e30d253 | 2865 | || gsym == NULL |
0ffd9845 | 2866 | || strcmp(gsym->name(), "__tls_get_addr") != 0) |
2e30d253 | 2867 | { |
75f2446e ILT |
2868 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
2869 | _("missing expected TLS relocation")); | |
2870 | } | |
2871 | else | |
2872 | { | |
2873 | this->skip_call_tls_get_addr_ = false; | |
2874 | return false; | |
2e30d253 | 2875 | } |
2e30d253 ILT |
2876 | } |
2877 | ||
6fa2a40b | 2878 | const Sized_relobj_file<64, false>* object = relinfo->object; |
7223e9ca ILT |
2879 | |
2880 | // Pick the value to use for symbols defined in the PLT. | |
2e30d253 | 2881 | Symbol_value<64> symval; |
96f2030e | 2882 | if (gsym != NULL |
95a2c8d6 | 2883 | && gsym->use_plt_offset(Scan::get_reference_flags(r_type))) |
2e30d253 | 2884 | { |
67181c72 | 2885 | symval.set_output_value(target->plt_address_for_global(gsym) |
2e30d253 ILT |
2886 | + gsym->plt_offset()); |
2887 | psymval = &symval; | |
2888 | } | |
7223e9ca ILT |
2889 | else if (gsym == NULL && psymval->is_ifunc_symbol()) |
2890 | { | |
2891 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); | |
2892 | if (object->local_has_plt_offset(r_sym)) | |
2893 | { | |
67181c72 | 2894 | symval.set_output_value(target->plt_address_for_local(object, r_sym) |
7223e9ca ILT |
2895 | + object->local_plt_offset(r_sym)); |
2896 | psymval = &symval; | |
2897 | } | |
2898 | } | |
2e30d253 | 2899 | |
0ffd9845 ILT |
2900 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
2901 | ||
2902 | // Get the GOT offset if needed. | |
96f2030e ILT |
2903 | // The GOT pointer points to the end of the GOT section. |
2904 | // We need to subtract the size of the GOT section to get | |
2905 | // the actual offset to use in the relocation. | |
0ffd9845 ILT |
2906 | bool have_got_offset = false; |
2907 | unsigned int got_offset = 0; | |
2908 | switch (r_type) | |
2909 | { | |
2910 | case elfcpp::R_X86_64_GOT32: | |
2911 | case elfcpp::R_X86_64_GOT64: | |
2912 | case elfcpp::R_X86_64_GOTPLT64: | |
2913 | case elfcpp::R_X86_64_GOTPCREL: | |
2914 | case elfcpp::R_X86_64_GOTPCREL64: | |
2915 | if (gsym != NULL) | |
2916 | { | |
0a65a3a7 CC |
2917 | gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); |
2918 | got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size(); | |
0ffd9845 ILT |
2919 | } |
2920 | else | |
2921 | { | |
2922 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); | |
0a65a3a7 CC |
2923 | gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)); |
2924 | got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD) | |
2925 | - target->got_size()); | |
0ffd9845 ILT |
2926 | } |
2927 | have_got_offset = true; | |
2928 | break; | |
2929 | ||
2930 | default: | |
2931 | break; | |
2932 | } | |
2e30d253 ILT |
2933 | |
2934 | switch (r_type) | |
2935 | { | |
2936 | case elfcpp::R_X86_64_NONE: | |
6e5710ce ILT |
2937 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
2938 | case elfcpp::R_X86_64_GNU_VTENTRY: | |
2e30d253 ILT |
2939 | break; |
2940 | ||
2941 | case elfcpp::R_X86_64_64: | |
2942 | Relocate_functions<64, false>::rela64(view, object, psymval, addend); | |
2943 | break; | |
2944 | ||
2945 | case elfcpp::R_X86_64_PC64: | |
2946 | Relocate_functions<64, false>::pcrela64(view, object, psymval, addend, | |
2947 | address); | |
2948 | break; | |
2949 | ||
2950 | case elfcpp::R_X86_64_32: | |
7bb3655e ILT |
2951 | // FIXME: we need to verify that value + addend fits into 32 bits: |
2952 | // uint64_t x = value + addend; | |
2953 | // x == static_cast<uint64_t>(static_cast<uint32_t>(x)) | |
2954 | // Likewise for other <=32-bit relocations (but see R_X86_64_32S). | |
2e30d253 ILT |
2955 | Relocate_functions<64, false>::rela32(view, object, psymval, addend); |
2956 | break; | |
2957 | ||
2958 | case elfcpp::R_X86_64_32S: | |
7bb3655e ILT |
2959 | // FIXME: we need to verify that value + addend fits into 32 bits: |
2960 | // int64_t x = value + addend; // note this quantity is signed! | |
2961 | // x == static_cast<int64_t>(static_cast<int32_t>(x)) | |
2e30d253 ILT |
2962 | Relocate_functions<64, false>::rela32(view, object, psymval, addend); |
2963 | break; | |
2964 | ||
2965 | case elfcpp::R_X86_64_PC32: | |
2966 | Relocate_functions<64, false>::pcrela32(view, object, psymval, addend, | |
2967 | address); | |
2968 | break; | |
2969 | ||
2970 | case elfcpp::R_X86_64_16: | |
2971 | Relocate_functions<64, false>::rela16(view, object, psymval, addend); | |
2972 | break; | |
2973 | ||
2974 | case elfcpp::R_X86_64_PC16: | |
2975 | Relocate_functions<64, false>::pcrela16(view, object, psymval, addend, | |
2976 | address); | |
2977 | break; | |
2978 | ||
2979 | case elfcpp::R_X86_64_8: | |
2980 | Relocate_functions<64, false>::rela8(view, object, psymval, addend); | |
2981 | break; | |
2982 | ||
2983 | case elfcpp::R_X86_64_PC8: | |
2984 | Relocate_functions<64, false>::pcrela8(view, object, psymval, addend, | |
2985 | address); | |
2986 | break; | |
2987 | ||
2988 | case elfcpp::R_X86_64_PLT32: | |
f389a824 ILT |
2989 | gold_assert(gsym == NULL |
2990 | || gsym->has_plt_offset() | |
99f8faca ILT |
2991 | || gsym->final_value_is_known() |
2992 | || (gsym->is_defined() | |
2993 | && !gsym->is_from_dynobj() | |
2994 | && !gsym->is_preemptible())); | |
ee9e9e86 ILT |
2995 | // Note: while this code looks the same as for R_X86_64_PC32, it |
2996 | // behaves differently because psymval was set to point to | |
2997 | // the PLT entry, rather than the symbol, in Scan::global(). | |
2e30d253 ILT |
2998 | Relocate_functions<64, false>::pcrela32(view, object, psymval, addend, |
2999 | address); | |
3000 | break; | |
3001 | ||
ee9e9e86 ILT |
3002 | case elfcpp::R_X86_64_PLTOFF64: |
3003 | { | |
3004 | gold_assert(gsym); | |
3005 | gold_assert(gsym->has_plt_offset() | |
3006 | || gsym->final_value_is_known()); | |
3007 | elfcpp::Elf_types<64>::Elf_Addr got_address; | |
3008 | got_address = target->got_section(NULL, NULL)->address(); | |
c1866bd5 ILT |
3009 | Relocate_functions<64, false>::rela64(view, object, psymval, |
3010 | addend - got_address); | |
ee9e9e86 ILT |
3011 | } |
3012 | ||
2e30d253 | 3013 | case elfcpp::R_X86_64_GOT32: |
0ffd9845 ILT |
3014 | gold_assert(have_got_offset); |
3015 | Relocate_functions<64, false>::rela32(view, got_offset, addend); | |
2e30d253 ILT |
3016 | break; |
3017 | ||
e822f2b1 ILT |
3018 | case elfcpp::R_X86_64_GOTPC32: |
3019 | { | |
3020 | gold_assert(gsym); | |
3021 | elfcpp::Elf_types<64>::Elf_Addr value; | |
96f2030e | 3022 | value = target->got_plt_section()->address(); |
e822f2b1 ILT |
3023 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); |
3024 | } | |
3025 | break; | |
3026 | ||
3027 | case elfcpp::R_X86_64_GOT64: | |
3028 | // The ABI doc says "Like GOT64, but indicates a PLT entry is needed." | |
3029 | // Since we always add a PLT entry, this is equivalent. | |
fdc2f80f | 3030 | case elfcpp::R_X86_64_GOTPLT64: |
0ffd9845 ILT |
3031 | gold_assert(have_got_offset); |
3032 | Relocate_functions<64, false>::rela64(view, got_offset, addend); | |
e822f2b1 ILT |
3033 | break; |
3034 | ||
3035 | case elfcpp::R_X86_64_GOTPC64: | |
3036 | { | |
3037 | gold_assert(gsym); | |
3038 | elfcpp::Elf_types<64>::Elf_Addr value; | |
96f2030e | 3039 | value = target->got_plt_section()->address(); |
e822f2b1 ILT |
3040 | Relocate_functions<64, false>::pcrela64(view, value, addend, address); |
3041 | } | |
3042 | break; | |
3043 | ||
2e30d253 ILT |
3044 | case elfcpp::R_X86_64_GOTOFF64: |
3045 | { | |
3046 | elfcpp::Elf_types<64>::Elf_Addr value; | |
3047 | value = (psymval->value(object, 0) | |
96f2030e | 3048 | - target->got_plt_section()->address()); |
2e30d253 ILT |
3049 | Relocate_functions<64, false>::rela64(view, value, addend); |
3050 | } | |
3051 | break; | |
3052 | ||
3053 | case elfcpp::R_X86_64_GOTPCREL: | |
3054 | { | |
0ffd9845 ILT |
3055 | gold_assert(have_got_offset); |
3056 | elfcpp::Elf_types<64>::Elf_Addr value; | |
96f2030e | 3057 | value = target->got_plt_section()->address() + got_offset; |
0ffd9845 | 3058 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); |
2e30d253 ILT |
3059 | } |
3060 | break; | |
3061 | ||
e822f2b1 ILT |
3062 | case elfcpp::R_X86_64_GOTPCREL64: |
3063 | { | |
0ffd9845 ILT |
3064 | gold_assert(have_got_offset); |
3065 | elfcpp::Elf_types<64>::Elf_Addr value; | |
96f2030e | 3066 | value = target->got_plt_section()->address() + got_offset; |
0ffd9845 | 3067 | Relocate_functions<64, false>::pcrela64(view, value, addend, address); |
e822f2b1 ILT |
3068 | } |
3069 | break; | |
3070 | ||
2e30d253 ILT |
3071 | case elfcpp::R_X86_64_COPY: |
3072 | case elfcpp::R_X86_64_GLOB_DAT: | |
3073 | case elfcpp::R_X86_64_JUMP_SLOT: | |
3074 | case elfcpp::R_X86_64_RELATIVE: | |
7223e9ca | 3075 | case elfcpp::R_X86_64_IRELATIVE: |
d61c17ea | 3076 | // These are outstanding tls relocs, which are unexpected when linking |
2e30d253 | 3077 | case elfcpp::R_X86_64_TPOFF64: |
2e30d253 | 3078 | case elfcpp::R_X86_64_DTPMOD64: |
2e30d253 | 3079 | case elfcpp::R_X86_64_TLSDESC: |
75f2446e ILT |
3080 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
3081 | _("unexpected reloc %u in object file"), | |
3082 | r_type); | |
2e30d253 ILT |
3083 | break; |
3084 | ||
d61c17ea | 3085 | // These are initial tls relocs, which are expected when linking |
56622147 ILT |
3086 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
3087 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) | |
e041f13d | 3088 | case elfcpp::R_X86_64_TLSDESC_CALL: |
56622147 | 3089 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
0ffd9845 ILT |
3090 | case elfcpp::R_X86_64_DTPOFF32: |
3091 | case elfcpp::R_X86_64_DTPOFF64: | |
56622147 ILT |
3092 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
3093 | case elfcpp::R_X86_64_TPOFF32: // Local-exec | |
7bf1f802 ILT |
3094 | this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval, |
3095 | view, address, view_size); | |
2e30d253 | 3096 | break; |
2e30d253 | 3097 | |
fdc2f80f ILT |
3098 | case elfcpp::R_X86_64_SIZE32: |
3099 | case elfcpp::R_X86_64_SIZE64: | |
2e30d253 | 3100 | default: |
75f2446e ILT |
3101 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
3102 | _("unsupported reloc %u"), | |
3103 | r_type); | |
2e30d253 ILT |
3104 | break; |
3105 | } | |
3106 | ||
3107 | return true; | |
3108 | } | |
3109 | ||
3110 | // Perform a TLS relocation. | |
3111 | ||
3112 | inline void | |
d61c17ea | 3113 | Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo, |
7bf1f802 | 3114 | Target_x86_64* target, |
d61c17ea | 3115 | size_t relnum, |
72ec2876 | 3116 | const elfcpp::Rela<64, false>& rela, |
d61c17ea ILT |
3117 | unsigned int r_type, |
3118 | const Sized_symbol<64>* gsym, | |
3119 | const Symbol_value<64>* psymval, | |
3120 | unsigned char* view, | |
6a41d30b | 3121 | elfcpp::Elf_types<64>::Elf_Addr address, |
fe8718a4 | 3122 | section_size_type view_size) |
2e30d253 | 3123 | { |
2e30d253 | 3124 | Output_segment* tls_segment = relinfo->layout->tls_segment(); |
7bf1f802 | 3125 | |
6fa2a40b | 3126 | const Sized_relobj_file<64, false>* object = relinfo->object; |
6a41d30b | 3127 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
36171d64 CC |
3128 | elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr); |
3129 | bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0; | |
2e30d253 ILT |
3130 | |
3131 | elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0); | |
3132 | ||
3133 | const bool is_final = (gsym == NULL | |
b3705d2a | 3134 | ? !parameters->options().shared() |
2e30d253 | 3135 | : gsym->final_value_is_known()); |
36171d64 | 3136 | tls::Tls_optimization optimized_type |
e041f13d | 3137 | = Target_x86_64::optimize_tls_reloc(is_final, r_type); |
2e30d253 ILT |
3138 | switch (r_type) |
3139 | { | |
56622147 | 3140 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
36171d64 CC |
3141 | if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) |
3142 | { | |
3143 | // If this code sequence is used in a non-executable section, | |
3144 | // we will not optimize the R_X86_64_DTPOFF32/64 relocation, | |
3145 | // on the assumption that it's being used by itself in a debug | |
3146 | // section. Therefore, in the unlikely event that the code | |
3147 | // sequence appears in a non-executable section, we simply | |
3148 | // leave it unoptimized. | |
3149 | optimized_type = tls::TLSOPT_NONE; | |
3150 | } | |
e041f13d | 3151 | if (optimized_type == tls::TLSOPT_TO_LE) |
2e30d253 | 3152 | { |
62855347 ILT |
3153 | if (tls_segment == NULL) |
3154 | { | |
191f1a2d ILT |
3155 | gold_assert(parameters->errors()->error_count() > 0 |
3156 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
3157 | return; |
3158 | } | |
2e30d253 | 3159 | this->tls_gd_to_le(relinfo, relnum, tls_segment, |
72ec2876 | 3160 | rela, r_type, value, view, |
2e30d253 ILT |
3161 | view_size); |
3162 | break; | |
3163 | } | |
7bf1f802 ILT |
3164 | else |
3165 | { | |
c2b45e22 CC |
3166 | unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE |
3167 | ? GOT_TYPE_TLS_OFFSET | |
3168 | : GOT_TYPE_TLS_PAIR); | |
7bf1f802 ILT |
3169 | unsigned int got_offset; |
3170 | if (gsym != NULL) | |
3171 | { | |
c2b45e22 CC |
3172 | gold_assert(gsym->has_got_offset(got_type)); |
3173 | got_offset = gsym->got_offset(got_type) - target->got_size(); | |
7bf1f802 ILT |
3174 | } |
3175 | else | |
3176 | { | |
3177 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); | |
c2b45e22 CC |
3178 | gold_assert(object->local_has_got_offset(r_sym, got_type)); |
3179 | got_offset = (object->local_got_offset(r_sym, got_type) | |
7bf1f802 ILT |
3180 | - target->got_size()); |
3181 | } | |
3182 | if (optimized_type == tls::TLSOPT_TO_IE) | |
3183 | { | |
62855347 ILT |
3184 | if (tls_segment == NULL) |
3185 | { | |
191f1a2d ILT |
3186 | gold_assert(parameters->errors()->error_count() > 0 |
3187 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
3188 | return; |
3189 | } | |
c2b45e22 | 3190 | value = target->got_plt_section()->address() + got_offset; |
7bf1f802 | 3191 | this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type, |
c2b45e22 | 3192 | value, view, address, view_size); |
7bf1f802 ILT |
3193 | break; |
3194 | } | |
3195 | else if (optimized_type == tls::TLSOPT_NONE) | |
3196 | { | |
3197 | // Relocate the field with the offset of the pair of GOT | |
3198 | // entries. | |
6a41d30b ILT |
3199 | value = target->got_plt_section()->address() + got_offset; |
3200 | Relocate_functions<64, false>::pcrela32(view, value, addend, | |
3201 | address); | |
7bf1f802 ILT |
3202 | break; |
3203 | } | |
3204 | } | |
72ec2876 | 3205 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
75f2446e | 3206 | _("unsupported reloc %u"), r_type); |
2e30d253 ILT |
3207 | break; |
3208 | ||
c2b45e22 CC |
3209 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
3210 | case elfcpp::R_X86_64_TLSDESC_CALL: | |
36171d64 CC |
3211 | if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) |
3212 | { | |
3213 | // See above comment for R_X86_64_TLSGD. | |
3214 | optimized_type = tls::TLSOPT_NONE; | |
3215 | } | |
c2b45e22 CC |
3216 | if (optimized_type == tls::TLSOPT_TO_LE) |
3217 | { | |
62855347 ILT |
3218 | if (tls_segment == NULL) |
3219 | { | |
191f1a2d ILT |
3220 | gold_assert(parameters->errors()->error_count() > 0 |
3221 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
3222 | return; |
3223 | } | |
c2b45e22 CC |
3224 | this->tls_desc_gd_to_le(relinfo, relnum, tls_segment, |
3225 | rela, r_type, value, view, | |
3226 | view_size); | |
3227 | break; | |
3228 | } | |
3229 | else | |
3230 | { | |
3231 | unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE | |
3232 | ? GOT_TYPE_TLS_OFFSET | |
3233 | : GOT_TYPE_TLS_DESC); | |
a8df5856 ILT |
3234 | unsigned int got_offset = 0; |
3235 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC | |
3236 | && optimized_type == tls::TLSOPT_NONE) | |
3237 | { | |
3238 | // We created GOT entries in the .got.tlsdesc portion of | |
3239 | // the .got.plt section, but the offset stored in the | |
3240 | // symbol is the offset within .got.tlsdesc. | |
3241 | got_offset = (target->got_size() | |
3242 | + target->got_plt_section()->data_size()); | |
3243 | } | |
c2b45e22 CC |
3244 | if (gsym != NULL) |
3245 | { | |
3246 | gold_assert(gsym->has_got_offset(got_type)); | |
a8df5856 | 3247 | got_offset += gsym->got_offset(got_type) - target->got_size(); |
c2b45e22 CC |
3248 | } |
3249 | else | |
3250 | { | |
3251 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); | |
3252 | gold_assert(object->local_has_got_offset(r_sym, got_type)); | |
a8df5856 ILT |
3253 | got_offset += (object->local_got_offset(r_sym, got_type) |
3254 | - target->got_size()); | |
c2b45e22 CC |
3255 | } |
3256 | if (optimized_type == tls::TLSOPT_TO_IE) | |
3257 | { | |
62855347 ILT |
3258 | if (tls_segment == NULL) |
3259 | { | |
191f1a2d ILT |
3260 | gold_assert(parameters->errors()->error_count() > 0 |
3261 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
3262 | return; |
3263 | } | |
c2b45e22 CC |
3264 | value = target->got_plt_section()->address() + got_offset; |
3265 | this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, | |
3266 | rela, r_type, value, view, address, | |
3267 | view_size); | |
3268 | break; | |
3269 | } | |
3270 | else if (optimized_type == tls::TLSOPT_NONE) | |
3271 | { | |
3272 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) | |
3273 | { | |
3274 | // Relocate the field with the offset of the pair of GOT | |
3275 | // entries. | |
3276 | value = target->got_plt_section()->address() + got_offset; | |
3277 | Relocate_functions<64, false>::pcrela32(view, value, addend, | |
3278 | address); | |
3279 | } | |
3280 | break; | |
3281 | } | |
3282 | } | |
3283 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), | |
3284 | _("unsupported reloc %u"), r_type); | |
3285 | break; | |
3286 | ||
56622147 | 3287 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
36171d64 CC |
3288 | if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) |
3289 | { | |
3290 | // See above comment for R_X86_64_TLSGD. | |
3291 | optimized_type = tls::TLSOPT_NONE; | |
3292 | } | |
e041f13d ILT |
3293 | if (optimized_type == tls::TLSOPT_TO_LE) |
3294 | { | |
62855347 ILT |
3295 | if (tls_segment == NULL) |
3296 | { | |
191f1a2d ILT |
3297 | gold_assert(parameters->errors()->error_count() > 0 |
3298 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
3299 | return; |
3300 | } | |
72ec2876 ILT |
3301 | this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type, |
3302 | value, view, view_size); | |
3303 | break; | |
e041f13d | 3304 | } |
7bf1f802 ILT |
3305 | else if (optimized_type == tls::TLSOPT_NONE) |
3306 | { | |
3307 | // Relocate the field with the offset of the GOT entry for | |
3308 | // the module index. | |
3309 | unsigned int got_offset; | |
31d60480 ILT |
3310 | got_offset = (target->got_mod_index_entry(NULL, NULL, NULL) |
3311 | - target->got_size()); | |
6a41d30b ILT |
3312 | value = target->got_plt_section()->address() + got_offset; |
3313 | Relocate_functions<64, false>::pcrela32(view, value, addend, | |
3314 | address); | |
7bf1f802 ILT |
3315 | break; |
3316 | } | |
72ec2876 | 3317 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
75f2446e | 3318 | _("unsupported reloc %u"), r_type); |
2e30d253 | 3319 | break; |
0ffd9845 ILT |
3320 | |
3321 | case elfcpp::R_X86_64_DTPOFF32: | |
36171d64 CC |
3322 | // This relocation type is used in debugging information. |
3323 | // In that case we need to not optimize the value. If the | |
3324 | // section is not executable, then we assume we should not | |
3325 | // optimize this reloc. See comments above for R_X86_64_TLSGD, | |
3326 | // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and | |
3327 | // R_X86_64_TLSLD. | |
3328 | if (optimized_type == tls::TLSOPT_TO_LE && is_executable) | |
3329 | { | |
62855347 ILT |
3330 | if (tls_segment == NULL) |
3331 | { | |
191f1a2d ILT |
3332 | gold_assert(parameters->errors()->error_count() > 0 |
3333 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
3334 | return; |
3335 | } | |
36171d64 CC |
3336 | value -= tls_segment->memsz(); |
3337 | } | |
d85c80a3 | 3338 | Relocate_functions<64, false>::rela32(view, value, addend); |
0ffd9845 ILT |
3339 | break; |
3340 | ||
3341 | case elfcpp::R_X86_64_DTPOFF64: | |
36171d64 CC |
3342 | // See R_X86_64_DTPOFF32, just above, for why we check for is_executable. |
3343 | if (optimized_type == tls::TLSOPT_TO_LE && is_executable) | |
3344 | { | |
62855347 ILT |
3345 | if (tls_segment == NULL) |
3346 | { | |
191f1a2d ILT |
3347 | gold_assert(parameters->errors()->error_count() > 0 |
3348 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
3349 | return; |
3350 | } | |
36171d64 CC |
3351 | value -= tls_segment->memsz(); |
3352 | } | |
d85c80a3 | 3353 | Relocate_functions<64, false>::rela64(view, value, addend); |
0ffd9845 | 3354 | break; |
2e30d253 | 3355 | |
56622147 ILT |
3356 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
3357 | if (optimized_type == tls::TLSOPT_TO_LE) | |
3358 | { | |
62855347 ILT |
3359 | if (tls_segment == NULL) |
3360 | { | |
191f1a2d ILT |
3361 | gold_assert(parameters->errors()->error_count() > 0 |
3362 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
3363 | return; |
3364 | } | |
56622147 ILT |
3365 | Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment, |
3366 | rela, r_type, value, view, | |
3367 | view_size); | |
3368 | break; | |
3369 | } | |
7bf1f802 ILT |
3370 | else if (optimized_type == tls::TLSOPT_NONE) |
3371 | { | |
3372 | // Relocate the field with the offset of the GOT entry for | |
3373 | // the tp-relative offset of the symbol. | |
3374 | unsigned int got_offset; | |
3375 | if (gsym != NULL) | |
3376 | { | |
0a65a3a7 CC |
3377 | gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET)); |
3378 | got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET) | |
3379 | - target->got_size()); | |
7bf1f802 ILT |
3380 | } |
3381 | else | |
3382 | { | |
3383 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); | |
0a65a3a7 CC |
3384 | gold_assert(object->local_has_got_offset(r_sym, |
3385 | GOT_TYPE_TLS_OFFSET)); | |
3386 | got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET) | |
7bf1f802 ILT |
3387 | - target->got_size()); |
3388 | } | |
6a41d30b ILT |
3389 | value = target->got_plt_section()->address() + got_offset; |
3390 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); | |
7bf1f802 ILT |
3391 | break; |
3392 | } | |
56622147 ILT |
3393 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
3394 | _("unsupported reloc type %u"), | |
3395 | r_type); | |
3396 | break; | |
0ffd9845 | 3397 | |
56622147 | 3398 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
62855347 ILT |
3399 | if (tls_segment == NULL) |
3400 | { | |
191f1a2d ILT |
3401 | gold_assert(parameters->errors()->error_count() > 0 |
3402 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
3403 | return; |
3404 | } | |
6a41d30b | 3405 | value -= tls_segment->memsz(); |
d85c80a3 | 3406 | Relocate_functions<64, false>::rela32(view, value, addend); |
56622147 | 3407 | break; |
2e30d253 | 3408 | } |
2e30d253 ILT |
3409 | } |
3410 | ||
7bf1f802 ILT |
3411 | // Do a relocation in which we convert a TLS General-Dynamic to an |
3412 | // Initial-Exec. | |
3413 | ||
3414 | inline void | |
3415 | Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo, | |
3416 | size_t relnum, | |
c2b45e22 | 3417 | Output_segment*, |
7bf1f802 ILT |
3418 | const elfcpp::Rela<64, false>& rela, |
3419 | unsigned int, | |
3420 | elfcpp::Elf_types<64>::Elf_Addr value, | |
3421 | unsigned char* view, | |
c2b45e22 | 3422 | elfcpp::Elf_types<64>::Elf_Addr address, |
fe8718a4 | 3423 | section_size_type view_size) |
7bf1f802 ILT |
3424 | { |
3425 | // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; | |
3426 | // .word 0x6666; rex64; call __tls_get_addr | |
3427 | // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax | |
3428 | ||
3429 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4); | |
3430 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12); | |
3431 | ||
3432 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
3433 | (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0)); | |
3434 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
3435 | (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0)); | |
3436 | ||
3437 | memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16); | |
3438 | ||
c2b45e22 CC |
3439 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
3440 | Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address); | |
7bf1f802 ILT |
3441 | |
3442 | // The next reloc should be a PLT32 reloc against __tls_get_addr. | |
3443 | // We can skip it. | |
3444 | this->skip_call_tls_get_addr_ = true; | |
3445 | } | |
3446 | ||
e041f13d | 3447 | // Do a relocation in which we convert a TLS General-Dynamic to a |
2e30d253 ILT |
3448 | // Local-Exec. |
3449 | ||
3450 | inline void | |
d61c17ea ILT |
3451 | Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo, |
3452 | size_t relnum, | |
3453 | Output_segment* tls_segment, | |
72ec2876 | 3454 | const elfcpp::Rela<64, false>& rela, |
d61c17ea ILT |
3455 | unsigned int, |
3456 | elfcpp::Elf_types<64>::Elf_Addr value, | |
3457 | unsigned char* view, | |
fe8718a4 | 3458 | section_size_type view_size) |
2e30d253 | 3459 | { |
0ffd9845 ILT |
3460 | // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; |
3461 | // .word 0x6666; rex64; call __tls_get_addr | |
3462 | // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax | |
2e30d253 | 3463 | |
72ec2876 ILT |
3464 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4); |
3465 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12); | |
2e30d253 | 3466 | |
72ec2876 ILT |
3467 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
3468 | (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0)); | |
3469 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
3470 | (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0)); | |
2e30d253 | 3471 | |
0ffd9845 | 3472 | memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16); |
2e30d253 | 3473 | |
6a41d30b | 3474 | value -= tls_segment->memsz(); |
0ffd9845 | 3475 | Relocate_functions<64, false>::rela32(view + 8, value, 0); |
2e30d253 ILT |
3476 | |
3477 | // The next reloc should be a PLT32 reloc against __tls_get_addr. | |
3478 | // We can skip it. | |
3479 | this->skip_call_tls_get_addr_ = true; | |
2e30d253 ILT |
3480 | } |
3481 | ||
c2b45e22 CC |
3482 | // Do a TLSDESC-style General-Dynamic to Initial-Exec transition. |
3483 | ||
3484 | inline void | |
3485 | Target_x86_64::Relocate::tls_desc_gd_to_ie( | |
3486 | const Relocate_info<64, false>* relinfo, | |
3487 | size_t relnum, | |
3488 | Output_segment*, | |
3489 | const elfcpp::Rela<64, false>& rela, | |
3490 | unsigned int r_type, | |
3491 | elfcpp::Elf_types<64>::Elf_Addr value, | |
3492 | unsigned char* view, | |
3493 | elfcpp::Elf_types<64>::Elf_Addr address, | |
3494 | section_size_type view_size) | |
3495 | { | |
3496 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) | |
3497 | { | |
3498 | // leaq foo@tlsdesc(%rip), %rax | |
3499 | // ==> movq foo@gottpoff(%rip), %rax | |
3500 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); | |
3501 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); | |
3502 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
3503 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05); | |
3504 | view[-2] = 0x8b; | |
3505 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); | |
3506 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); | |
3507 | } | |
3508 | else | |
3509 | { | |
3510 | // call *foo@tlscall(%rax) | |
3511 | // ==> nop; nop | |
3512 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL); | |
3513 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2); | |
3514 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
3515 | view[0] == 0xff && view[1] == 0x10); | |
3516 | view[0] = 0x66; | |
3517 | view[1] = 0x90; | |
3518 | } | |
3519 | } | |
3520 | ||
3521 | // Do a TLSDESC-style General-Dynamic to Local-Exec transition. | |
3522 | ||
3523 | inline void | |
3524 | Target_x86_64::Relocate::tls_desc_gd_to_le( | |
3525 | const Relocate_info<64, false>* relinfo, | |
3526 | size_t relnum, | |
3527 | Output_segment* tls_segment, | |
3528 | const elfcpp::Rela<64, false>& rela, | |
3529 | unsigned int r_type, | |
3530 | elfcpp::Elf_types<64>::Elf_Addr value, | |
3531 | unsigned char* view, | |
3532 | section_size_type view_size) | |
3533 | { | |
3534 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) | |
3535 | { | |
3536 | // leaq foo@tlsdesc(%rip), %rax | |
3537 | // ==> movq foo@tpoff, %rax | |
3538 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); | |
3539 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); | |
3540 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
3541 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05); | |
3542 | view[-2] = 0xc7; | |
3543 | view[-1] = 0xc0; | |
3544 | value -= tls_segment->memsz(); | |
3545 | Relocate_functions<64, false>::rela32(view, value, 0); | |
3546 | } | |
3547 | else | |
3548 | { | |
3549 | // call *foo@tlscall(%rax) | |
3550 | // ==> nop; nop | |
3551 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL); | |
3552 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2); | |
3553 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
3554 | view[0] == 0xff && view[1] == 0x10); | |
3555 | view[0] = 0x66; | |
3556 | view[1] = 0x90; | |
3557 | } | |
3558 | } | |
3559 | ||
2e30d253 | 3560 | inline void |
72ec2876 ILT |
3561 | Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo, |
3562 | size_t relnum, | |
3563 | Output_segment*, | |
3564 | const elfcpp::Rela<64, false>& rela, | |
3565 | unsigned int, | |
3566 | elfcpp::Elf_types<64>::Elf_Addr, | |
3567 | unsigned char* view, | |
fe8718a4 | 3568 | section_size_type view_size) |
2e30d253 | 3569 | { |
72ec2876 ILT |
3570 | // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt; |
3571 | // ... leq foo@dtpoff(%rax),%reg | |
3572 | // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx | |
2e30d253 | 3573 | |
72ec2876 ILT |
3574 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); |
3575 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9); | |
2e30d253 | 3576 | |
72ec2876 ILT |
3577 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
3578 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d); | |
3579 | ||
3580 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8); | |
3581 | ||
3582 | memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12); | |
3583 | ||
3584 | // The next reloc should be a PLT32 reloc against __tls_get_addr. | |
3585 | // We can skip it. | |
3586 | this->skip_call_tls_get_addr_ = true; | |
2e30d253 ILT |
3587 | } |
3588 | ||
56622147 ILT |
3589 | // Do a relocation in which we convert a TLS Initial-Exec to a |
3590 | // Local-Exec. | |
3591 | ||
3592 | inline void | |
3593 | Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo, | |
3594 | size_t relnum, | |
3595 | Output_segment* tls_segment, | |
3596 | const elfcpp::Rela<64, false>& rela, | |
3597 | unsigned int, | |
3598 | elfcpp::Elf_types<64>::Elf_Addr value, | |
3599 | unsigned char* view, | |
fe8718a4 | 3600 | section_size_type view_size) |
56622147 ILT |
3601 | { |
3602 | // We need to examine the opcodes to figure out which instruction we | |
3603 | // are looking at. | |
3604 | ||
3605 | // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg | |
3606 | // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg | |
3607 | ||
3608 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); | |
3609 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); | |
3610 | ||
3611 | unsigned char op1 = view[-3]; | |
3612 | unsigned char op2 = view[-2]; | |
3613 | unsigned char op3 = view[-1]; | |
3614 | unsigned char reg = op3 >> 3; | |
3615 | ||
3616 | if (op2 == 0x8b) | |
3617 | { | |
3618 | // movq | |
3619 | if (op1 == 0x4c) | |
3620 | view[-3] = 0x49; | |
3621 | view[-2] = 0xc7; | |
3622 | view[-1] = 0xc0 | reg; | |
3623 | } | |
3624 | else if (reg == 4) | |
3625 | { | |
3626 | // Special handling for %rsp. | |
3627 | if (op1 == 0x4c) | |
3628 | view[-3] = 0x49; | |
3629 | view[-2] = 0x81; | |
3630 | view[-1] = 0xc0 | reg; | |
3631 | } | |
3632 | else | |
3633 | { | |
3634 | // addq | |
3635 | if (op1 == 0x4c) | |
3636 | view[-3] = 0x4d; | |
3637 | view[-2] = 0x8d; | |
3638 | view[-1] = 0x80 | reg | (reg << 3); | |
3639 | } | |
3640 | ||
6a41d30b | 3641 | value -= tls_segment->memsz(); |
56622147 ILT |
3642 | Relocate_functions<64, false>::rela32(view, value, 0); |
3643 | } | |
3644 | ||
2e30d253 ILT |
3645 | // Relocate section data. |
3646 | ||
3647 | void | |
364c7fa5 ILT |
3648 | Target_x86_64::relocate_section( |
3649 | const Relocate_info<64, false>* relinfo, | |
3650 | unsigned int sh_type, | |
3651 | const unsigned char* prelocs, | |
3652 | size_t reloc_count, | |
3653 | Output_section* output_section, | |
3654 | bool needs_special_offset_handling, | |
3655 | unsigned char* view, | |
3656 | elfcpp::Elf_types<64>::Elf_Addr address, | |
3657 | section_size_type view_size, | |
3658 | const Reloc_symbol_changes* reloc_symbol_changes) | |
2e30d253 ILT |
3659 | { |
3660 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
3661 | ||
3662 | gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA, | |
3663 | Target_x86_64::Relocate>( | |
3664 | relinfo, | |
3665 | this, | |
3666 | prelocs, | |
3667 | reloc_count, | |
730cdc88 ILT |
3668 | output_section, |
3669 | needs_special_offset_handling, | |
2e30d253 ILT |
3670 | view, |
3671 | address, | |
364c7fa5 ILT |
3672 | view_size, |
3673 | reloc_symbol_changes); | |
2e30d253 ILT |
3674 | } |
3675 | ||
94a3fc8b CC |
3676 | // Apply an incremental relocation. Incremental relocations always refer |
3677 | // to global symbols. | |
3678 | ||
3679 | void | |
3680 | Target_x86_64::apply_relocation( | |
3681 | const Relocate_info<64, false>* relinfo, | |
3682 | elfcpp::Elf_types<64>::Elf_Addr r_offset, | |
3683 | unsigned int r_type, | |
3684 | elfcpp::Elf_types<64>::Elf_Swxword r_addend, | |
3685 | const Symbol* gsym, | |
3686 | unsigned char* view, | |
3687 | elfcpp::Elf_types<64>::Elf_Addr address, | |
3688 | section_size_type view_size) | |
3689 | { | |
3690 | gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>( | |
3691 | relinfo, | |
3692 | this, | |
3693 | r_offset, | |
3694 | r_type, | |
3695 | r_addend, | |
3696 | gsym, | |
3697 | view, | |
3698 | address, | |
3699 | view_size); | |
3700 | } | |
3701 | ||
6a74a719 ILT |
3702 | // Return the size of a relocation while scanning during a relocatable |
3703 | // link. | |
3704 | ||
3705 | unsigned int | |
3706 | Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc( | |
3707 | unsigned int r_type, | |
3708 | Relobj* object) | |
3709 | { | |
3710 | switch (r_type) | |
3711 | { | |
3712 | case elfcpp::R_X86_64_NONE: | |
6e5710ce ILT |
3713 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
3714 | case elfcpp::R_X86_64_GNU_VTENTRY: | |
6a74a719 ILT |
3715 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
3716 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) | |
3717 | case elfcpp::R_X86_64_TLSDESC_CALL: | |
3718 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic | |
3719 | case elfcpp::R_X86_64_DTPOFF32: | |
3720 | case elfcpp::R_X86_64_DTPOFF64: | |
3721 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec | |
3722 | case elfcpp::R_X86_64_TPOFF32: // Local-exec | |
3723 | return 0; | |
3724 | ||
3725 | case elfcpp::R_X86_64_64: | |
3726 | case elfcpp::R_X86_64_PC64: | |
3727 | case elfcpp::R_X86_64_GOTOFF64: | |
3728 | case elfcpp::R_X86_64_GOTPC64: | |
3729 | case elfcpp::R_X86_64_PLTOFF64: | |
3730 | case elfcpp::R_X86_64_GOT64: | |
3731 | case elfcpp::R_X86_64_GOTPCREL64: | |
3732 | case elfcpp::R_X86_64_GOTPCREL: | |
3733 | case elfcpp::R_X86_64_GOTPLT64: | |
3734 | return 8; | |
3735 | ||
3736 | case elfcpp::R_X86_64_32: | |
3737 | case elfcpp::R_X86_64_32S: | |
3738 | case elfcpp::R_X86_64_PC32: | |
3739 | case elfcpp::R_X86_64_PLT32: | |
3740 | case elfcpp::R_X86_64_GOTPC32: | |
3741 | case elfcpp::R_X86_64_GOT32: | |
3742 | return 4; | |
3743 | ||
3744 | case elfcpp::R_X86_64_16: | |
3745 | case elfcpp::R_X86_64_PC16: | |
3746 | return 2; | |
3747 | ||
3748 | case elfcpp::R_X86_64_8: | |
3749 | case elfcpp::R_X86_64_PC8: | |
3750 | return 1; | |
3751 | ||
3752 | case elfcpp::R_X86_64_COPY: | |
3753 | case elfcpp::R_X86_64_GLOB_DAT: | |
3754 | case elfcpp::R_X86_64_JUMP_SLOT: | |
3755 | case elfcpp::R_X86_64_RELATIVE: | |
7223e9ca | 3756 | case elfcpp::R_X86_64_IRELATIVE: |
6a74a719 ILT |
3757 | // These are outstanding tls relocs, which are unexpected when linking |
3758 | case elfcpp::R_X86_64_TPOFF64: | |
3759 | case elfcpp::R_X86_64_DTPMOD64: | |
3760 | case elfcpp::R_X86_64_TLSDESC: | |
3761 | object->error(_("unexpected reloc %u in object file"), r_type); | |
3762 | return 0; | |
3763 | ||
3764 | case elfcpp::R_X86_64_SIZE32: | |
3765 | case elfcpp::R_X86_64_SIZE64: | |
3766 | default: | |
3767 | object->error(_("unsupported reloc %u against local symbol"), r_type); | |
3768 | return 0; | |
3769 | } | |
3770 | } | |
3771 | ||
3772 | // Scan the relocs during a relocatable link. | |
3773 | ||
3774 | void | |
ad0f2072 | 3775 | Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab, |
6a74a719 | 3776 | Layout* layout, |
6fa2a40b | 3777 | Sized_relobj_file<64, false>* object, |
6a74a719 ILT |
3778 | unsigned int data_shndx, |
3779 | unsigned int sh_type, | |
3780 | const unsigned char* prelocs, | |
3781 | size_t reloc_count, | |
3782 | Output_section* output_section, | |
3783 | bool needs_special_offset_handling, | |
3784 | size_t local_symbol_count, | |
3785 | const unsigned char* plocal_symbols, | |
3786 | Relocatable_relocs* rr) | |
3787 | { | |
3788 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
3789 | ||
3790 | typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA, | |
3791 | Relocatable_size_for_reloc> Scan_relocatable_relocs; | |
3792 | ||
7019cd25 | 3793 | gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA, |
6a74a719 | 3794 | Scan_relocatable_relocs>( |
6a74a719 ILT |
3795 | symtab, |
3796 | layout, | |
3797 | object, | |
3798 | data_shndx, | |
3799 | prelocs, | |
3800 | reloc_count, | |
3801 | output_section, | |
3802 | needs_special_offset_handling, | |
3803 | local_symbol_count, | |
3804 | plocal_symbols, | |
3805 | rr); | |
3806 | } | |
3807 | ||
3808 | // Relocate a section during a relocatable link. | |
3809 | ||
3810 | void | |
3811 | Target_x86_64::relocate_for_relocatable( | |
3812 | const Relocate_info<64, false>* relinfo, | |
3813 | unsigned int sh_type, | |
3814 | const unsigned char* prelocs, | |
3815 | size_t reloc_count, | |
3816 | Output_section* output_section, | |
3817 | off_t offset_in_output_section, | |
3818 | const Relocatable_relocs* rr, | |
3819 | unsigned char* view, | |
3820 | elfcpp::Elf_types<64>::Elf_Addr view_address, | |
3821 | section_size_type view_size, | |
3822 | unsigned char* reloc_view, | |
3823 | section_size_type reloc_view_size) | |
3824 | { | |
3825 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
3826 | ||
7019cd25 | 3827 | gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>( |
6a74a719 ILT |
3828 | relinfo, |
3829 | prelocs, | |
3830 | reloc_count, | |
3831 | output_section, | |
3832 | offset_in_output_section, | |
3833 | rr, | |
3834 | view, | |
3835 | view_address, | |
3836 | view_size, | |
3837 | reloc_view, | |
3838 | reloc_view_size); | |
3839 | } | |
3840 | ||
4fb6c25d ILT |
3841 | // Return the value to use for a dynamic which requires special |
3842 | // treatment. This is how we support equality comparisons of function | |
3843 | // pointers across shared library boundaries, as described in the | |
3844 | // processor specific ABI supplement. | |
3845 | ||
3846 | uint64_t | |
3847 | Target_x86_64::do_dynsym_value(const Symbol* gsym) const | |
3848 | { | |
3849 | gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); | |
67181c72 | 3850 | return this->plt_address_for_global(gsym) + gsym->plt_offset(); |
4fb6c25d ILT |
3851 | } |
3852 | ||
2e30d253 ILT |
3853 | // Return a string used to fill a code section with nops to take up |
3854 | // the specified length. | |
3855 | ||
3856 | std::string | |
8851ecca | 3857 | Target_x86_64::do_code_fill(section_size_type length) const |
2e30d253 ILT |
3858 | { |
3859 | if (length >= 16) | |
3860 | { | |
3861 | // Build a jmpq instruction to skip over the bytes. | |
3862 | unsigned char jmp[5]; | |
3863 | jmp[0] = 0xe9; | |
04bf7072 | 3864 | elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5); |
2e30d253 ILT |
3865 | return (std::string(reinterpret_cast<char*>(&jmp[0]), 5) |
3866 | + std::string(length - 5, '\0')); | |
3867 | } | |
3868 | ||
3869 | // Nop sequences of various lengths. | |
3870 | const char nop1[1] = { 0x90 }; // nop | |
3871 | const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax | |
1caf2c51 ILT |
3872 | const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax) |
3873 | const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax) | |
3874 | const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1) | |
3875 | 0x00 }; | |
3876 | const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1) | |
2e30d253 | 3877 | 0x00, 0x00 }; |
1caf2c51 | 3878 | const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax) |
2e30d253 | 3879 | 0x00, 0x00, 0x00 }; |
1caf2c51 ILT |
3880 | const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1) |
3881 | 0x00, 0x00, 0x00, 0x00 }; | |
3882 | const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1) | |
3883 | 0x00, 0x00, 0x00, 0x00, | |
2e30d253 | 3884 | 0x00 }; |
1caf2c51 ILT |
3885 | const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1) |
3886 | 0x84, 0x00, 0x00, 0x00, | |
2e30d253 | 3887 | 0x00, 0x00 }; |
1caf2c51 ILT |
3888 | const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16 |
3889 | 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1) | |
2e30d253 | 3890 | 0x00, 0x00, 0x00 }; |
1caf2c51 ILT |
3891 | const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16 |
3892 | 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1) | |
2e30d253 | 3893 | 0x00, 0x00, 0x00, 0x00 }; |
1caf2c51 ILT |
3894 | const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16 |
3895 | 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1) | |
3896 | 0x00, 0x00, 0x00, 0x00, | |
2e30d253 | 3897 | 0x00 }; |
1caf2c51 ILT |
3898 | const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16 |
3899 | 0x66, 0x2e, 0x0f, 0x1f, // data16 | |
3900 | 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1) | |
2e30d253 | 3901 | 0x00, 0x00 }; |
1caf2c51 ILT |
3902 | const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16 |
3903 | 0x66, 0x66, 0x2e, 0x0f, // data16; data16 | |
3904 | 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1) | |
3905 | 0x00, 0x00, 0x00 }; | |
2e30d253 ILT |
3906 | |
3907 | const char* nops[16] = { | |
3908 | NULL, | |
3909 | nop1, nop2, nop3, nop4, nop5, nop6, nop7, | |
3910 | nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15 | |
3911 | }; | |
3912 | ||
3913 | return std::string(nops[length], length); | |
3914 | } | |
3915 | ||
e291e7b9 ILT |
3916 | // Return the addend to use for a target specific relocation. The |
3917 | // only target specific relocation is R_X86_64_TLSDESC for a local | |
3918 | // symbol. We want to set the addend is the offset of the local | |
3919 | // symbol in the TLS segment. | |
3920 | ||
3921 | uint64_t | |
3922 | Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type, | |
3923 | uint64_t) const | |
3924 | { | |
3925 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC); | |
3926 | uintptr_t intarg = reinterpret_cast<uintptr_t>(arg); | |
3927 | gold_assert(intarg < this->tlsdesc_reloc_info_.size()); | |
3928 | const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]); | |
3929 | const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym); | |
3930 | gold_assert(psymval->is_tls_symbol()); | |
3931 | // The value of a TLS symbol is the offset in the TLS segment. | |
3932 | return psymval->value(ti.object, 0); | |
3933 | } | |
3934 | ||
02d7cd44 ILT |
3935 | // Return the value to use for the base of a DW_EH_PE_datarel offset |
3936 | // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their | |
3937 | // assembler can not write out the difference between two labels in | |
3938 | // different sections, so instead of using a pc-relative value they | |
3939 | // use an offset from the GOT. | |
3940 | ||
3941 | uint64_t | |
3942 | Target_x86_64::do_ehframe_datarel_base() const | |
3943 | { | |
3944 | gold_assert(this->global_offset_table_ != NULL); | |
3945 | Symbol* sym = this->global_offset_table_; | |
3946 | Sized_symbol<64>* ssym = static_cast<Sized_symbol<64>*>(sym); | |
3947 | return ssym->value(); | |
3948 | } | |
3949 | ||
364c7fa5 | 3950 | // FNOFFSET in section SHNDX in OBJECT is the start of a function |
9b547ce6 | 3951 | // compiled with -fsplit-stack. The function calls non-split-stack |
364c7fa5 ILT |
3952 | // code. We have to change the function so that it always ensures |
3953 | // that it has enough stack space to run some random function. | |
3954 | ||
3955 | void | |
3956 | Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx, | |
3957 | section_offset_type fnoffset, | |
3958 | section_size_type fnsize, | |
3959 | unsigned char* view, | |
3960 | section_size_type view_size, | |
3961 | std::string* from, | |
3962 | std::string* to) const | |
3963 | { | |
3964 | // The function starts with a comparison of the stack pointer and a | |
3965 | // field in the TCB. This is followed by a jump. | |
3966 | ||
3967 | // cmp %fs:NN,%rsp | |
3968 | if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5) | |
3969 | && fnsize > 9) | |
3970 | { | |
3971 | // We will call __morestack if the carry flag is set after this | |
3972 | // comparison. We turn the comparison into an stc instruction | |
3973 | // and some nops. | |
3974 | view[fnoffset] = '\xf9'; | |
3975 | this->set_view_to_nop(view, view_size, fnoffset + 1, 8); | |
3976 | } | |
3977 | // lea NN(%rsp),%r10 | |
cbc999b9 ILT |
3978 | // lea NN(%rsp),%r11 |
3979 | else if ((this->match_view(view, view_size, fnoffset, | |
3980 | "\x4c\x8d\x94\x24", 4) | |
3981 | || this->match_view(view, view_size, fnoffset, | |
3982 | "\x4c\x8d\x9c\x24", 4)) | |
364c7fa5 ILT |
3983 | && fnsize > 8) |
3984 | { | |
3985 | // This is loading an offset from the stack pointer for a | |
3986 | // comparison. The offset is negative, so we decrease the | |
3987 | // offset by the amount of space we need for the stack. This | |
3988 | // means we will avoid calling __morestack if there happens to | |
3989 | // be plenty of space on the stack already. | |
3990 | unsigned char* pval = view + fnoffset + 4; | |
3991 | uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval); | |
3992 | val -= parameters->options().split_stack_adjust_size(); | |
3993 | elfcpp::Swap_unaligned<32, false>::writeval(pval, val); | |
3994 | } | |
3995 | else | |
3996 | { | |
3997 | if (!object->has_no_split_stack()) | |
3998 | object->error(_("failed to match split-stack sequence at " | |
3999 | "section %u offset %0zx"), | |
ac33a407 | 4000 | shndx, static_cast<size_t>(fnoffset)); |
364c7fa5 ILT |
4001 | return; |
4002 | } | |
4003 | ||
4004 | // We have to change the function so that it calls | |
4005 | // __morestack_non_split instead of __morestack. The former will | |
4006 | // allocate additional stack space. | |
4007 | *from = "__morestack"; | |
4008 | *to = "__morestack_non_split"; | |
4009 | } | |
4010 | ||
2e30d253 ILT |
4011 | // The selector for x86_64 object files. |
4012 | ||
36959681 | 4013 | class Target_selector_x86_64 : public Target_selector_freebsd |
2e30d253 ILT |
4014 | { |
4015 | public: | |
4016 | Target_selector_x86_64() | |
36959681 | 4017 | : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64", |
03ef7571 | 4018 | "elf64-x86-64-freebsd", "elf_x86_64") |
2e30d253 ILT |
4019 | { } |
4020 | ||
4021 | Target* | |
e96caa79 ILT |
4022 | do_instantiate_target() |
4023 | { return new Target_x86_64(); } | |
36959681 | 4024 | |
2e30d253 ILT |
4025 | }; |
4026 | ||
2e30d253 ILT |
4027 | Target_selector_x86_64 target_selector_x86_64; |
4028 | ||
4029 | } // End anonymous namespace. |