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