Commit | Line | Data |
---|---|---|
2e30d253 ILT |
1 | // x86_64.cc -- x86_64 target support for gold. |
2 | ||
6f2750fe | 3 | // Copyright (C) 2006-2016 Free Software Foundation, Inc. |
2e30d253 ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8b105e34 ILT |
8 | // This program is free software; you can redistribute it and/or modify |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
2e30d253 ILT |
11 | // (at your option) any later version. |
12 | ||
8b105e34 ILT |
13 | // This program is distributed in the hope that it will be useful, |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
2e30d253 ILT |
22 | |
23 | #include "gold.h" | |
24 | ||
25 | #include <cstring> | |
26 | ||
27 | #include "elfcpp.h" | |
07a60597 | 28 | #include "dwarf.h" |
2e30d253 ILT |
29 | #include "parameters.h" |
30 | #include "reloc.h" | |
31 | #include "x86_64.h" | |
32 | #include "object.h" | |
33 | #include "symtab.h" | |
34 | #include "layout.h" | |
35 | #include "output.h" | |
12c0daef | 36 | #include "copy-relocs.h" |
2e30d253 ILT |
37 | #include "target.h" |
38 | #include "target-reloc.h" | |
39 | #include "target-select.h" | |
e041f13d | 40 | #include "tls.h" |
36959681 | 41 | #include "freebsd.h" |
2e702c99 | 42 | #include "nacl.h" |
f345227a | 43 | #include "gc.h" |
21bb3914 | 44 | #include "icf.h" |
2e30d253 ILT |
45 | |
46 | namespace | |
47 | { | |
48 | ||
49 | using namespace gold; | |
50 | ||
57b2284c CC |
51 | // A class to handle the .got.plt section. |
52 | ||
53 | class Output_data_got_plt_x86_64 : public Output_section_data_build | |
54 | { | |
55 | public: | |
56 | Output_data_got_plt_x86_64(Layout* layout) | |
57 | : Output_section_data_build(8), | |
58 | layout_(layout) | |
59 | { } | |
60 | ||
61 | Output_data_got_plt_x86_64(Layout* layout, off_t data_size) | |
62 | : Output_section_data_build(data_size, 8), | |
63 | layout_(layout) | |
64 | { } | |
65 | ||
66 | protected: | |
67 | // Write out the PLT data. | |
68 | void | |
69 | do_write(Output_file*); | |
70 | ||
71 | // Write to a map file. | |
72 | void | |
73 | do_print_to_mapfile(Mapfile* mapfile) const | |
74 | { mapfile->print_output_data(this, "** GOT PLT"); } | |
75 | ||
76 | private: | |
77 | // A pointer to the Layout class, so that we can find the .dynamic | |
78 | // section when we write out the GOT PLT section. | |
79 | Layout* layout_; | |
80 | }; | |
81 | ||
7223e9ca | 82 | // A class to handle the PLT data. |
2e702c99 RM |
83 | // This is an abstract base class that handles most of the linker details |
84 | // but does not know the actual contents of PLT entries. The derived | |
85 | // classes below fill in those details. | |
7223e9ca | 86 | |
fc51264f | 87 | template<int size> |
7223e9ca ILT |
88 | class Output_data_plt_x86_64 : public Output_section_data |
89 | { | |
90 | public: | |
fc51264f | 91 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section; |
7223e9ca | 92 | |
2e702c99 RM |
93 | Output_data_plt_x86_64(Layout* layout, uint64_t addralign, |
94 | Output_data_got<64, false>* got, | |
57b2284c | 95 | Output_data_got_plt_x86_64* got_plt, |
67181c72 | 96 | Output_data_space* got_irelative) |
57b2284c | 97 | : Output_section_data(addralign), tlsdesc_rel_(NULL), |
7d172687 ILT |
98 | irelative_rel_(NULL), got_(got), got_plt_(got_plt), |
99 | got_irelative_(got_irelative), count_(0), irelative_count_(0), | |
100 | tlsdesc_got_offset_(-1U), free_list_() | |
67181c72 ILT |
101 | { this->init(layout); } |
102 | ||
2e702c99 RM |
103 | Output_data_plt_x86_64(Layout* layout, uint64_t plt_entry_size, |
104 | Output_data_got<64, false>* got, | |
57b2284c | 105 | Output_data_got_plt_x86_64* got_plt, |
67181c72 | 106 | Output_data_space* got_irelative, |
4829d394 | 107 | unsigned int plt_count) |
2e702c99 RM |
108 | : Output_section_data((plt_count + 1) * plt_entry_size, |
109 | plt_entry_size, false), | |
57b2284c | 110 | tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got), |
7d172687 ILT |
111 | got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count), |
112 | irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_() | |
4829d394 | 113 | { |
67181c72 | 114 | this->init(layout); |
4829d394 CC |
115 | |
116 | // Initialize the free list and reserve the first entry. | |
117 | this->free_list_.init((plt_count + 1) * plt_entry_size, false); | |
118 | this->free_list_.remove(0, plt_entry_size); | |
119 | } | |
120 | ||
121 | // Initialize the PLT section. | |
122 | void | |
67181c72 | 123 | init(Layout* layout); |
7223e9ca ILT |
124 | |
125 | // Add an entry to the PLT. | |
126 | void | |
67181c72 | 127 | add_entry(Symbol_table*, Layout*, Symbol* gsym); |
7223e9ca ILT |
128 | |
129 | // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. | |
130 | unsigned int | |
67181c72 | 131 | add_local_ifunc_entry(Symbol_table* symtab, Layout*, |
fc51264f | 132 | Sized_relobj_file<size, false>* relobj, |
7223e9ca ILT |
133 | unsigned int local_sym_index); |
134 | ||
4829d394 CC |
135 | // Add the relocation for a PLT entry. |
136 | void | |
67181c72 ILT |
137 | add_relocation(Symbol_table*, Layout*, Symbol* gsym, |
138 | unsigned int got_offset); | |
4829d394 | 139 | |
7223e9ca ILT |
140 | // Add the reserved TLSDESC_PLT entry to the PLT. |
141 | void | |
142 | reserve_tlsdesc_entry(unsigned int got_offset) | |
143 | { this->tlsdesc_got_offset_ = got_offset; } | |
144 | ||
145 | // Return true if a TLSDESC_PLT entry has been reserved. | |
146 | bool | |
147 | has_tlsdesc_entry() const | |
148 | { return this->tlsdesc_got_offset_ != -1U; } | |
149 | ||
150 | // Return the GOT offset for the reserved TLSDESC_PLT entry. | |
151 | unsigned int | |
152 | get_tlsdesc_got_offset() const | |
153 | { return this->tlsdesc_got_offset_; } | |
154 | ||
155 | // Return the offset of the reserved TLSDESC_PLT entry. | |
156 | unsigned int | |
157 | get_tlsdesc_plt_offset() const | |
2e702c99 RM |
158 | { |
159 | return ((this->count_ + this->irelative_count_ + 1) | |
160 | * this->get_plt_entry_size()); | |
161 | } | |
7223e9ca ILT |
162 | |
163 | // Return the .rela.plt section data. | |
164 | Reloc_section* | |
165 | rela_plt() | |
166 | { return this->rel_; } | |
167 | ||
168 | // Return where the TLSDESC relocations should go. | |
169 | Reloc_section* | |
170 | rela_tlsdesc(Layout*); | |
171 | ||
67181c72 ILT |
172 | // Return where the IRELATIVE relocations should go in the PLT |
173 | // relocations. | |
174 | Reloc_section* | |
175 | rela_irelative(Symbol_table*, Layout*); | |
176 | ||
177 | // Return whether we created a section for IRELATIVE relocations. | |
178 | bool | |
179 | has_irelative_section() const | |
180 | { return this->irelative_rel_ != NULL; } | |
181 | ||
7223e9ca ILT |
182 | // Return the number of PLT entries. |
183 | unsigned int | |
184 | entry_count() const | |
67181c72 | 185 | { return this->count_ + this->irelative_count_; } |
7223e9ca ILT |
186 | |
187 | // Return the offset of the first non-reserved PLT entry. | |
2e702c99 | 188 | unsigned int |
7223e9ca | 189 | first_plt_entry_offset() |
2e702c99 | 190 | { return this->get_plt_entry_size(); } |
7223e9ca ILT |
191 | |
192 | // Return the size of a PLT entry. | |
2e702c99 RM |
193 | unsigned int |
194 | get_plt_entry_size() const | |
195 | { return this->do_get_plt_entry_size(); } | |
7223e9ca | 196 | |
4829d394 CC |
197 | // Reserve a slot in the PLT for an existing symbol in an incremental update. |
198 | void | |
199 | reserve_slot(unsigned int plt_index) | |
200 | { | |
2e702c99 RM |
201 | this->free_list_.remove((plt_index + 1) * this->get_plt_entry_size(), |
202 | (plt_index + 2) * this->get_plt_entry_size()); | |
4829d394 CC |
203 | } |
204 | ||
67181c72 ILT |
205 | // Return the PLT address to use for a global symbol. |
206 | uint64_t | |
207 | address_for_global(const Symbol*); | |
208 | ||
209 | // Return the PLT address to use for a local symbol. | |
210 | uint64_t | |
211 | address_for_local(const Relobj*, unsigned int symndx); | |
212 | ||
2e702c99 RM |
213 | // Add .eh_frame information for the PLT. |
214 | void | |
215 | add_eh_frame(Layout* layout) | |
216 | { this->do_add_eh_frame(layout); } | |
217 | ||
7223e9ca | 218 | protected: |
2e702c99 RM |
219 | // Fill in the first PLT entry. |
220 | void | |
221 | fill_first_plt_entry(unsigned char* pov, | |
222 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
223 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address) | |
224 | { this->do_fill_first_plt_entry(pov, got_address, plt_address); } | |
225 | ||
226 | // Fill in a normal PLT entry. Returns the offset into the entry that | |
227 | // should be the initial GOT slot value. | |
228 | unsigned int | |
229 | fill_plt_entry(unsigned char* pov, | |
230 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
231 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
232 | unsigned int got_offset, | |
233 | unsigned int plt_offset, | |
234 | unsigned int plt_index) | |
235 | { | |
236 | return this->do_fill_plt_entry(pov, got_address, plt_address, | |
237 | got_offset, plt_offset, plt_index); | |
238 | } | |
239 | ||
240 | // Fill in the reserved TLSDESC PLT entry. | |
241 | void | |
242 | fill_tlsdesc_entry(unsigned char* pov, | |
243 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
244 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
245 | typename elfcpp::Elf_types<size>::Elf_Addr got_base, | |
246 | unsigned int tlsdesc_got_offset, | |
247 | unsigned int plt_offset) | |
248 | { | |
249 | this->do_fill_tlsdesc_entry(pov, got_address, plt_address, got_base, | |
250 | tlsdesc_got_offset, plt_offset); | |
251 | } | |
252 | ||
253 | virtual unsigned int | |
254 | do_get_plt_entry_size() const = 0; | |
255 | ||
256 | virtual void | |
257 | do_fill_first_plt_entry(unsigned char* pov, | |
258 | typename elfcpp::Elf_types<size>::Elf_Addr got_addr, | |
259 | typename elfcpp::Elf_types<size>::Elf_Addr plt_addr) | |
260 | = 0; | |
261 | ||
262 | virtual unsigned int | |
263 | do_fill_plt_entry(unsigned char* pov, | |
264 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
265 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
266 | unsigned int got_offset, | |
267 | unsigned int plt_offset, | |
268 | unsigned int plt_index) = 0; | |
269 | ||
270 | virtual void | |
271 | do_fill_tlsdesc_entry(unsigned char* pov, | |
272 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
273 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
274 | typename elfcpp::Elf_types<size>::Elf_Addr got_base, | |
275 | unsigned int tlsdesc_got_offset, | |
276 | unsigned int plt_offset) = 0; | |
277 | ||
278 | virtual void | |
279 | do_add_eh_frame(Layout* layout) = 0; | |
280 | ||
7223e9ca ILT |
281 | void |
282 | do_adjust_output_section(Output_section* os); | |
283 | ||
284 | // Write to a map file. | |
285 | void | |
286 | do_print_to_mapfile(Mapfile* mapfile) const | |
287 | { mapfile->print_output_data(this, _("** PLT")); } | |
288 | ||
2e702c99 | 289 | // The CIE of the .eh_frame unwind information for the PLT. |
07a60597 | 290 | static const int plt_eh_frame_cie_size = 16; |
07a60597 | 291 | static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size]; |
7223e9ca | 292 | |
2e702c99 | 293 | private: |
7223e9ca ILT |
294 | // Set the final size. |
295 | void | |
296 | set_final_data_size(); | |
297 | ||
298 | // Write out the PLT data. | |
299 | void | |
300 | do_write(Output_file*); | |
301 | ||
302 | // The reloc section. | |
303 | Reloc_section* rel_; | |
304 | // The TLSDESC relocs, if necessary. These must follow the regular | |
305 | // PLT relocs. | |
306 | Reloc_section* tlsdesc_rel_; | |
67181c72 ILT |
307 | // The IRELATIVE relocs, if necessary. These must follow the |
308 | // regular PLT relocations and the TLSDESC relocations. | |
309 | Reloc_section* irelative_rel_; | |
7223e9ca ILT |
310 | // The .got section. |
311 | Output_data_got<64, false>* got_; | |
312 | // The .got.plt section. | |
57b2284c | 313 | Output_data_got_plt_x86_64* got_plt_; |
67181c72 ILT |
314 | // The part of the .got.plt section used for IRELATIVE relocs. |
315 | Output_data_space* got_irelative_; | |
7223e9ca ILT |
316 | // The number of PLT entries. |
317 | unsigned int count_; | |
67181c72 ILT |
318 | // Number of PLT entries with R_X86_64_IRELATIVE relocs. These |
319 | // follow the regular PLT entries. | |
320 | unsigned int irelative_count_; | |
7223e9ca ILT |
321 | // Offset of the reserved TLSDESC_GOT entry when needed. |
322 | unsigned int tlsdesc_got_offset_; | |
4829d394 CC |
323 | // List of available regions within the section, for incremental |
324 | // update links. | |
325 | Free_list free_list_; | |
7223e9ca | 326 | }; |
2e30d253 | 327 | |
2e702c99 RM |
328 | template<int size> |
329 | class Output_data_plt_x86_64_standard : public Output_data_plt_x86_64<size> | |
330 | { | |
331 | public: | |
332 | Output_data_plt_x86_64_standard(Layout* layout, | |
333 | Output_data_got<64, false>* got, | |
57b2284c | 334 | Output_data_got_plt_x86_64* got_plt, |
2e702c99 RM |
335 | Output_data_space* got_irelative) |
336 | : Output_data_plt_x86_64<size>(layout, plt_entry_size, | |
337 | got, got_plt, got_irelative) | |
338 | { } | |
339 | ||
340 | Output_data_plt_x86_64_standard(Layout* layout, | |
341 | Output_data_got<64, false>* got, | |
57b2284c | 342 | Output_data_got_plt_x86_64* got_plt, |
2e702c99 RM |
343 | Output_data_space* got_irelative, |
344 | unsigned int plt_count) | |
345 | : Output_data_plt_x86_64<size>(layout, plt_entry_size, | |
346 | got, got_plt, got_irelative, | |
347 | plt_count) | |
348 | { } | |
349 | ||
350 | protected: | |
351 | virtual unsigned int | |
352 | do_get_plt_entry_size() const | |
353 | { return plt_entry_size; } | |
354 | ||
355 | virtual void | |
356 | do_add_eh_frame(Layout* layout) | |
357 | { | |
358 | layout->add_eh_frame_for_plt(this, | |
359 | this->plt_eh_frame_cie, | |
360 | this->plt_eh_frame_cie_size, | |
361 | plt_eh_frame_fde, | |
362 | plt_eh_frame_fde_size); | |
363 | } | |
364 | ||
365 | virtual void | |
366 | do_fill_first_plt_entry(unsigned char* pov, | |
367 | typename elfcpp::Elf_types<size>::Elf_Addr got_addr, | |
368 | typename elfcpp::Elf_types<size>::Elf_Addr plt_addr); | |
369 | ||
370 | virtual unsigned int | |
371 | do_fill_plt_entry(unsigned char* pov, | |
372 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
373 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
374 | unsigned int got_offset, | |
375 | unsigned int plt_offset, | |
376 | unsigned int plt_index); | |
377 | ||
378 | virtual void | |
379 | do_fill_tlsdesc_entry(unsigned char* pov, | |
380 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
381 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
382 | typename elfcpp::Elf_types<size>::Elf_Addr got_base, | |
383 | unsigned int tlsdesc_got_offset, | |
384 | unsigned int plt_offset); | |
385 | ||
386 | private: | |
387 | // The size of an entry in the PLT. | |
388 | static const int plt_entry_size = 16; | |
389 | ||
390 | // The first entry in the PLT. | |
391 | // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same | |
392 | // procedure linkage table for both programs and shared objects." | |
393 | static const unsigned char first_plt_entry[plt_entry_size]; | |
394 | ||
395 | // Other entries in the PLT for an executable. | |
396 | static const unsigned char plt_entry[plt_entry_size]; | |
397 | ||
398 | // The reserved TLSDESC entry in the PLT for an executable. | |
399 | static const unsigned char tlsdesc_plt_entry[plt_entry_size]; | |
400 | ||
401 | // The .eh_frame unwind information for the PLT. | |
402 | static const int plt_eh_frame_fde_size = 32; | |
403 | static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size]; | |
404 | }; | |
405 | ||
3a4f096e ST |
406 | template<int size> |
407 | class Lazy_view | |
408 | { | |
409 | public: | |
410 | Lazy_view(Sized_relobj_file<size, false>* object, unsigned int data_shndx) | |
411 | : object_(object), data_shndx_(data_shndx), view_(NULL), view_size_(0) | |
412 | { } | |
413 | ||
414 | inline unsigned char | |
415 | operator[](size_t offset) | |
416 | { | |
417 | if (this->view_ == NULL) | |
418 | this->view_ = this->object_->section_contents(this->data_shndx_, | |
419 | &this->view_size_, | |
420 | true); | |
421 | if (offset >= this->view_size_) | |
422 | return 0; | |
423 | return this->view_[offset]; | |
424 | } | |
425 | ||
426 | private: | |
427 | Sized_relobj_file<size, false>* object_; | |
428 | unsigned int data_shndx_; | |
429 | const unsigned char* view_; | |
430 | section_size_type view_size_; | |
431 | }; | |
432 | ||
2e30d253 | 433 | // The x86_64 target class. |
d61c17ea ILT |
434 | // See the ABI at |
435 | // http://www.x86-64.org/documentation/abi.pdf | |
436 | // TLS info comes from | |
437 | // http://people.redhat.com/drepper/tls.pdf | |
0ffd9845 | 438 | // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt |
2e30d253 | 439 | |
fc51264f L |
440 | template<int size> |
441 | class Target_x86_64 : public Sized_target<size, false> | |
2e30d253 ILT |
442 | { |
443 | public: | |
e822f2b1 ILT |
444 | // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures |
445 | // uses only Elf64_Rela relocation entries with explicit addends." | |
fc51264f | 446 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section; |
2e30d253 | 447 | |
2e702c99 RM |
448 | Target_x86_64(const Target::Target_info* info = &x86_64_info) |
449 | : Sized_target<size, false>(info), | |
67181c72 ILT |
450 | got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL), |
451 | got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL), | |
452 | rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY), | |
43819297 | 453 | got_mod_index_offset_(-1U), tlsdesc_reloc_info_(), |
e291e7b9 | 454 | tls_base_symbol_defined_(false) |
2e30d253 ILT |
455 | { } |
456 | ||
8a5e3e08 ILT |
457 | // Hook for a new output section. |
458 | void | |
459 | do_new_output_section(Output_section*) const; | |
460 | ||
6d03d481 ST |
461 | // Scan the relocations to look for symbol adjustments. |
462 | void | |
ad0f2072 | 463 | gc_process_relocs(Symbol_table* symtab, |
2e702c99 RM |
464 | Layout* layout, |
465 | Sized_relobj_file<size, false>* object, | |
466 | unsigned int data_shndx, | |
467 | unsigned int sh_type, | |
468 | const unsigned char* prelocs, | |
469 | size_t reloc_count, | |
470 | Output_section* output_section, | |
471 | bool needs_special_offset_handling, | |
472 | size_t local_symbol_count, | |
473 | const unsigned char* plocal_symbols); | |
6d03d481 | 474 | |
2e30d253 ILT |
475 | // Scan the relocations to look for symbol adjustments. |
476 | void | |
ad0f2072 | 477 | scan_relocs(Symbol_table* symtab, |
2e30d253 | 478 | Layout* layout, |
fc51264f | 479 | Sized_relobj_file<size, false>* object, |
2e30d253 ILT |
480 | unsigned int data_shndx, |
481 | unsigned int sh_type, | |
482 | const unsigned char* prelocs, | |
483 | size_t reloc_count, | |
730cdc88 ILT |
484 | Output_section* output_section, |
485 | bool needs_special_offset_handling, | |
2e30d253 | 486 | size_t local_symbol_count, |
730cdc88 | 487 | const unsigned char* plocal_symbols); |
2e30d253 ILT |
488 | |
489 | // Finalize the sections. | |
490 | void | |
f59f41f3 | 491 | do_finalize_sections(Layout*, const Input_objects*, Symbol_table*); |
2e30d253 | 492 | |
4fb6c25d ILT |
493 | // Return the value to use for a dynamic which requires special |
494 | // treatment. | |
495 | uint64_t | |
496 | do_dynsym_value(const Symbol*) const; | |
497 | ||
2e30d253 ILT |
498 | // Relocate a section. |
499 | void | |
fc51264f | 500 | relocate_section(const Relocate_info<size, false>*, |
2e30d253 ILT |
501 | unsigned int sh_type, |
502 | const unsigned char* prelocs, | |
503 | size_t reloc_count, | |
730cdc88 ILT |
504 | Output_section* output_section, |
505 | bool needs_special_offset_handling, | |
2e30d253 | 506 | unsigned char* view, |
fc51264f | 507 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, |
364c7fa5 ILT |
508 | section_size_type view_size, |
509 | const Reloc_symbol_changes*); | |
2e30d253 | 510 | |
6a74a719 ILT |
511 | // Scan the relocs during a relocatable link. |
512 | void | |
ad0f2072 | 513 | scan_relocatable_relocs(Symbol_table* symtab, |
6a74a719 | 514 | Layout* layout, |
fc51264f | 515 | Sized_relobj_file<size, false>* object, |
6a74a719 ILT |
516 | unsigned int data_shndx, |
517 | unsigned int sh_type, | |
518 | const unsigned char* prelocs, | |
519 | size_t reloc_count, | |
520 | Output_section* output_section, | |
521 | bool needs_special_offset_handling, | |
522 | size_t local_symbol_count, | |
523 | const unsigned char* plocal_symbols, | |
524 | Relocatable_relocs*); | |
525 | ||
4d625b70 CC |
526 | // Scan the relocs for --emit-relocs. |
527 | void | |
528 | emit_relocs_scan(Symbol_table* symtab, | |
529 | Layout* layout, | |
530 | Sized_relobj_file<size, false>* object, | |
531 | unsigned int data_shndx, | |
532 | unsigned int sh_type, | |
533 | const unsigned char* prelocs, | |
534 | size_t reloc_count, | |
535 | Output_section* output_section, | |
536 | bool needs_special_offset_handling, | |
537 | size_t local_symbol_count, | |
538 | const unsigned char* plocal_syms, | |
539 | Relocatable_relocs* rr); | |
540 | ||
7404fe1b | 541 | // Emit relocations for a section. |
6a74a719 | 542 | void |
7404fe1b | 543 | relocate_relocs( |
fc51264f L |
544 | const Relocate_info<size, false>*, |
545 | unsigned int sh_type, | |
546 | const unsigned char* prelocs, | |
547 | size_t reloc_count, | |
548 | Output_section* output_section, | |
62fe925a | 549 | typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section, |
fc51264f L |
550 | unsigned char* view, |
551 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, | |
552 | section_size_type view_size, | |
553 | unsigned char* reloc_view, | |
554 | section_size_type reloc_view_size); | |
6a74a719 | 555 | |
2e30d253 ILT |
556 | // Return a string used to fill a code section with nops. |
557 | std::string | |
8851ecca | 558 | do_code_fill(section_size_type length) const; |
2e30d253 | 559 | |
9a2d6984 ILT |
560 | // Return whether SYM is defined by the ABI. |
561 | bool | |
9c2d0ef9 | 562 | do_is_defined_by_abi(const Symbol* sym) const |
9a2d6984 ILT |
563 | { return strcmp(sym->name(), "__tls_get_addr") == 0; } |
564 | ||
e291e7b9 ILT |
565 | // Return the symbol index to use for a target specific relocation. |
566 | // The only target specific relocation is R_X86_64_TLSDESC for a | |
567 | // local symbol, which is an absolute reloc. | |
568 | unsigned int | |
569 | do_reloc_symbol_index(void*, unsigned int r_type) const | |
570 | { | |
571 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC); | |
572 | return 0; | |
573 | } | |
574 | ||
575 | // Return the addend to use for a target specific relocation. | |
576 | uint64_t | |
577 | do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const; | |
578 | ||
7223e9ca | 579 | // Return the PLT section. |
67181c72 ILT |
580 | uint64_t |
581 | do_plt_address_for_global(const Symbol* gsym) const | |
582 | { return this->plt_section()->address_for_global(gsym); } | |
7223e9ca | 583 | |
67181c72 ILT |
584 | uint64_t |
585 | do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const | |
586 | { return this->plt_section()->address_for_local(relobj, symndx); } | |
7223e9ca | 587 | |
b3ce541e ILT |
588 | // This function should be defined in targets that can use relocation |
589 | // types to determine (implemented in local_reloc_may_be_function_pointer | |
590 | // and global_reloc_may_be_function_pointer) | |
591 | // if a function's pointer is taken. ICF uses this in safe mode to only | |
592 | // fold those functions whose pointer is defintely not taken. For x86_64 | |
593 | // pie binaries, safe ICF cannot be done by looking at relocation types. | |
594 | bool | |
595 | do_can_check_for_function_pointers() const | |
596 | { return !parameters->options().pie(); } | |
597 | ||
02d7cd44 ILT |
598 | // Return the base for a DW_EH_PE_datarel encoding. |
599 | uint64_t | |
600 | do_ehframe_datarel_base() const; | |
601 | ||
9b547ce6 | 602 | // Adjust -fsplit-stack code which calls non-split-stack code. |
364c7fa5 ILT |
603 | void |
604 | do_calls_non_split(Relobj* object, unsigned int shndx, | |
605 | section_offset_type fnoffset, section_size_type fnsize, | |
6e0813d3 | 606 | const unsigned char* prelocs, size_t reloc_count, |
364c7fa5 ILT |
607 | unsigned char* view, section_size_type view_size, |
608 | std::string* from, std::string* to) const; | |
609 | ||
96f2030e | 610 | // Return the size of the GOT section. |
fe8718a4 | 611 | section_size_type |
0e70b911 | 612 | got_size() const |
96f2030e ILT |
613 | { |
614 | gold_assert(this->got_ != NULL); | |
615 | return this->got_->data_size(); | |
616 | } | |
617 | ||
0e70b911 CC |
618 | // Return the number of entries in the GOT. |
619 | unsigned int | |
620 | got_entry_count() const | |
621 | { | |
622 | if (this->got_ == NULL) | |
623 | return 0; | |
624 | return this->got_size() / 8; | |
625 | } | |
626 | ||
627 | // Return the number of entries in the PLT. | |
628 | unsigned int | |
629 | plt_entry_count() const; | |
630 | ||
631 | // Return the offset of the first non-reserved PLT entry. | |
632 | unsigned int | |
633 | first_plt_entry_offset() const; | |
634 | ||
635 | // Return the size of each PLT entry. | |
636 | unsigned int | |
637 | plt_entry_size() const; | |
638 | ||
41e83f2b L |
639 | // Return the size of each GOT entry. |
640 | unsigned int | |
641 | got_entry_size() const | |
642 | { return 8; }; | |
643 | ||
4829d394 | 644 | // Create the GOT section for an incremental update. |
dd74ae06 | 645 | Output_data_got_base* |
4829d394 CC |
646 | init_got_plt_for_update(Symbol_table* symtab, |
647 | Layout* layout, | |
648 | unsigned int got_count, | |
649 | unsigned int plt_count); | |
650 | ||
6fa2a40b CC |
651 | // Reserve a GOT entry for a local symbol, and regenerate any |
652 | // necessary dynamic relocations. | |
653 | void | |
654 | reserve_local_got_entry(unsigned int got_index, | |
2e702c99 | 655 | Sized_relobj<size, false>* obj, |
6fa2a40b CC |
656 | unsigned int r_sym, |
657 | unsigned int got_type); | |
658 | ||
659 | // Reserve a GOT entry for a global symbol, and regenerate any | |
660 | // necessary dynamic relocations. | |
661 | void | |
662 | reserve_global_got_entry(unsigned int got_index, Symbol* gsym, | |
663 | unsigned int got_type); | |
664 | ||
4829d394 | 665 | // Register an existing PLT entry for a global symbol. |
4829d394 | 666 | void |
67181c72 ILT |
667 | register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index, |
668 | Symbol* gsym); | |
4829d394 | 669 | |
26d3c67d CC |
670 | // Force a COPY relocation for a given symbol. |
671 | void | |
672 | emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t); | |
673 | ||
94a3fc8b CC |
674 | // Apply an incremental relocation. |
675 | void | |
fc51264f L |
676 | apply_relocation(const Relocate_info<size, false>* relinfo, |
677 | typename elfcpp::Elf_types<size>::Elf_Addr r_offset, | |
94a3fc8b | 678 | unsigned int r_type, |
fc51264f | 679 | typename elfcpp::Elf_types<size>::Elf_Swxword r_addend, |
94a3fc8b CC |
680 | const Symbol* gsym, |
681 | unsigned char* view, | |
fc51264f | 682 | typename elfcpp::Elf_types<size>::Elf_Addr address, |
94a3fc8b CC |
683 | section_size_type view_size); |
684 | ||
e291e7b9 ILT |
685 | // Add a new reloc argument, returning the index in the vector. |
686 | size_t | |
fc51264f | 687 | add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym) |
e291e7b9 ILT |
688 | { |
689 | this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym)); | |
690 | return this->tlsdesc_reloc_info_.size() - 1; | |
691 | } | |
692 | ||
2e702c99 RM |
693 | Output_data_plt_x86_64<size>* |
694 | make_data_plt(Layout* layout, | |
695 | Output_data_got<64, false>* got, | |
57b2284c | 696 | Output_data_got_plt_x86_64* got_plt, |
2e702c99 RM |
697 | Output_data_space* got_irelative) |
698 | { | |
699 | return this->do_make_data_plt(layout, got, got_plt, got_irelative); | |
700 | } | |
701 | ||
702 | Output_data_plt_x86_64<size>* | |
703 | make_data_plt(Layout* layout, | |
704 | Output_data_got<64, false>* got, | |
57b2284c | 705 | Output_data_got_plt_x86_64* got_plt, |
2e702c99 RM |
706 | Output_data_space* got_irelative, |
707 | unsigned int plt_count) | |
708 | { | |
709 | return this->do_make_data_plt(layout, got, got_plt, got_irelative, | |
710 | plt_count); | |
711 | } | |
712 | ||
713 | virtual Output_data_plt_x86_64<size>* | |
714 | do_make_data_plt(Layout* layout, | |
715 | Output_data_got<64, false>* got, | |
57b2284c | 716 | Output_data_got_plt_x86_64* got_plt, |
2e702c99 RM |
717 | Output_data_space* got_irelative) |
718 | { | |
719 | return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt, | |
720 | got_irelative); | |
721 | } | |
722 | ||
723 | virtual Output_data_plt_x86_64<size>* | |
724 | do_make_data_plt(Layout* layout, | |
725 | Output_data_got<64, false>* got, | |
57b2284c | 726 | Output_data_got_plt_x86_64* got_plt, |
2e702c99 RM |
727 | Output_data_space* got_irelative, |
728 | unsigned int plt_count) | |
729 | { | |
730 | return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt, | |
731 | got_irelative, | |
732 | plt_count); | |
733 | } | |
734 | ||
2e30d253 ILT |
735 | private: |
736 | // The class which scans relocations. | |
a036edd8 | 737 | class Scan |
2e30d253 | 738 | { |
a036edd8 ILT |
739 | public: |
740 | Scan() | |
741 | : issued_non_pic_error_(false) | |
742 | { } | |
743 | ||
95a2c8d6 RS |
744 | static inline int |
745 | get_reference_flags(unsigned int r_type); | |
746 | ||
2e30d253 | 747 | inline void |
ad0f2072 | 748 | local(Symbol_table* symtab, Layout* layout, Target_x86_64* target, |
fc51264f | 749 | Sized_relobj_file<size, false>* object, |
2e30d253 | 750 | unsigned int data_shndx, |
07f397ab | 751 | Output_section* output_section, |
fc51264f | 752 | const elfcpp::Rela<size, false>& reloc, unsigned int r_type, |
bfdfa4cd AM |
753 | const elfcpp::Sym<size, false>& lsym, |
754 | bool is_discarded); | |
2e30d253 ILT |
755 | |
756 | inline void | |
ad0f2072 | 757 | global(Symbol_table* symtab, Layout* layout, Target_x86_64* target, |
fc51264f | 758 | Sized_relobj_file<size, false>* object, |
2e30d253 | 759 | unsigned int data_shndx, |
07f397ab | 760 | Output_section* output_section, |
fc51264f | 761 | const elfcpp::Rela<size, false>& reloc, unsigned int r_type, |
2e30d253 | 762 | Symbol* gsym); |
e041f13d | 763 | |
21bb3914 ST |
764 | inline bool |
765 | local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout, | |
766 | Target_x86_64* target, | |
2e702c99 RM |
767 | Sized_relobj_file<size, false>* object, |
768 | unsigned int data_shndx, | |
769 | Output_section* output_section, | |
770 | const elfcpp::Rela<size, false>& reloc, | |
21bb3914 | 771 | unsigned int r_type, |
2e702c99 | 772 | const elfcpp::Sym<size, false>& lsym); |
21bb3914 ST |
773 | |
774 | inline bool | |
775 | global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout, | |
2e702c99 RM |
776 | Target_x86_64* target, |
777 | Sized_relobj_file<size, false>* object, | |
778 | unsigned int data_shndx, | |
779 | Output_section* output_section, | |
780 | const elfcpp::Rela<size, false>& reloc, | |
21bb3914 | 781 | unsigned int r_type, |
2e702c99 | 782 | Symbol* gsym); |
21bb3914 | 783 | |
a036edd8 | 784 | private: |
e041f13d | 785 | static void |
fc51264f L |
786 | unsupported_reloc_local(Sized_relobj_file<size, false>*, |
787 | unsigned int r_type); | |
e041f13d ILT |
788 | |
789 | static void | |
fc51264f L |
790 | unsupported_reloc_global(Sized_relobj_file<size, false>*, |
791 | unsigned int r_type, Symbol*); | |
a036edd8 ILT |
792 | |
793 | void | |
a29b0dad | 794 | check_non_pic(Relobj*, unsigned int r_type, Symbol*); |
a036edd8 | 795 | |
21bb3914 ST |
796 | inline bool |
797 | possible_function_pointer_reloc(unsigned int r_type); | |
798 | ||
7223e9ca | 799 | bool |
fc51264f | 800 | reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*, |
6fa2a40b | 801 | unsigned int r_type); |
7223e9ca | 802 | |
a036edd8 ILT |
803 | // Whether we have issued an error about a non-PIC compilation. |
804 | bool issued_non_pic_error_; | |
2e30d253 ILT |
805 | }; |
806 | ||
807 | // The class which implements relocation. | |
808 | class Relocate | |
809 | { | |
810 | public: | |
811 | Relocate() | |
36171d64 | 812 | : skip_call_tls_get_addr_(false) |
2e30d253 ILT |
813 | { } |
814 | ||
815 | ~Relocate() | |
816 | { | |
817 | if (this->skip_call_tls_get_addr_) | |
818 | { | |
819 | // FIXME: This needs to specify the location somehow. | |
a0c4fb0a | 820 | gold_error(_("missing expected TLS relocation")); |
2e30d253 ILT |
821 | } |
822 | } | |
823 | ||
824 | // Do a relocation. Return false if the caller should not issue | |
825 | // any warnings about this relocation. | |
826 | inline bool | |
91a65d2f AM |
827 | relocate(const Relocate_info<size, false>*, unsigned int, |
828 | Target_x86_64*, Output_section*, size_t, const unsigned char*, | |
829 | const Sized_symbol<size>*, const Symbol_value<size>*, | |
fc51264f | 830 | unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr, |
fe8718a4 | 831 | section_size_type); |
2e30d253 ILT |
832 | |
833 | private: | |
834 | // Do a TLS relocation. | |
835 | inline void | |
fc51264f | 836 | relocate_tls(const Relocate_info<size, false>*, Target_x86_64*, |
2e702c99 | 837 | size_t relnum, const elfcpp::Rela<size, false>&, |
fc51264f L |
838 | unsigned int r_type, const Sized_symbol<size>*, |
839 | const Symbol_value<size>*, | |
840 | unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr, | |
fe8718a4 | 841 | section_size_type); |
2e30d253 | 842 | |
c2b45e22 | 843 | // Do a TLS General-Dynamic to Initial-Exec transition. |
7bf1f802 | 844 | inline void |
fc51264f | 845 | tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum, |
fc51264f L |
846 | const elfcpp::Rela<size, false>&, unsigned int r_type, |
847 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
7bf1f802 | 848 | unsigned char* view, |
fc51264f | 849 | typename elfcpp::Elf_types<size>::Elf_Addr, |
fe8718a4 | 850 | section_size_type view_size); |
7bf1f802 | 851 | |
56622147 ILT |
852 | // Do a TLS General-Dynamic to Local-Exec transition. |
853 | inline void | |
fc51264f | 854 | tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum, |
2e30d253 | 855 | Output_segment* tls_segment, |
fc51264f L |
856 | const elfcpp::Rela<size, false>&, unsigned int r_type, |
857 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
2e30d253 | 858 | unsigned char* view, |
fe8718a4 | 859 | section_size_type view_size); |
2e30d253 | 860 | |
c2b45e22 CC |
861 | // Do a TLSDESC-style General-Dynamic to Initial-Exec transition. |
862 | inline void | |
fc51264f | 863 | tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum, |
fc51264f L |
864 | const elfcpp::Rela<size, false>&, unsigned int r_type, |
865 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
c2b45e22 | 866 | unsigned char* view, |
fc51264f | 867 | typename elfcpp::Elf_types<size>::Elf_Addr, |
c2b45e22 CC |
868 | section_size_type view_size); |
869 | ||
870 | // Do a TLSDESC-style General-Dynamic to Local-Exec transition. | |
871 | inline void | |
fc51264f | 872 | tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum, |
c2b45e22 | 873 | Output_segment* tls_segment, |
fc51264f L |
874 | const elfcpp::Rela<size, false>&, unsigned int r_type, |
875 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
c2b45e22 CC |
876 | unsigned char* view, |
877 | section_size_type view_size); | |
878 | ||
56622147 | 879 | // Do a TLS Local-Dynamic to Local-Exec transition. |
2e30d253 | 880 | inline void |
fc51264f | 881 | tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum, |
2e30d253 | 882 | Output_segment* tls_segment, |
fc51264f L |
883 | const elfcpp::Rela<size, false>&, unsigned int r_type, |
884 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
2e30d253 | 885 | unsigned char* view, |
fe8718a4 | 886 | section_size_type view_size); |
2e30d253 | 887 | |
56622147 ILT |
888 | // Do a TLS Initial-Exec to Local-Exec transition. |
889 | static inline void | |
fc51264f | 890 | tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum, |
72ec2876 | 891 | Output_segment* tls_segment, |
fc51264f L |
892 | const elfcpp::Rela<size, false>&, unsigned int r_type, |
893 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
72ec2876 | 894 | unsigned char* view, |
fe8718a4 | 895 | section_size_type view_size); |
2e30d253 ILT |
896 | |
897 | // This is set if we should skip the next reloc, which should be a | |
898 | // PLT32 reloc against ___tls_get_addr. | |
899 | bool skip_call_tls_get_addr_; | |
900 | }; | |
901 | ||
1fa29f10 IT |
902 | // Check if relocation against this symbol is a candidate for |
903 | // conversion from | |
904 | // mov foo@GOTPCREL(%rip), %reg | |
905 | // to lea foo(%rip), %reg. | |
3a4f096e ST |
906 | template<class View_type> |
907 | static inline bool | |
908 | can_convert_mov_to_lea(const Symbol* gsym, unsigned int r_type, | |
909 | size_t r_offset, View_type* view) | |
1fa29f10 IT |
910 | { |
911 | gold_assert(gsym != NULL); | |
3a4f096e ST |
912 | // We cannot do the conversion unless it's one of these relocations. |
913 | if (r_type != elfcpp::R_X86_64_GOTPCREL | |
914 | && r_type != elfcpp::R_X86_64_GOTPCRELX | |
915 | && r_type != elfcpp::R_X86_64_REX_GOTPCRELX) | |
916 | return false; | |
917 | // We cannot convert references to IFUNC symbols, or to symbols that | |
918 | // are not local to the current module. | |
919 | if (gsym->type() == elfcpp::STT_GNU_IFUNC | |
920 | || gsym->is_undefined () | |
921 | || gsym->is_from_dynobj() | |
922 | || gsym->is_preemptible()) | |
923 | return false; | |
924 | // If we are building a shared object and the symbol is protected, we may | |
925 | // need to go through the GOT. | |
926 | if (parameters->options().shared() | |
927 | && gsym->visibility() == elfcpp::STV_PROTECTED) | |
928 | return false; | |
929 | // We cannot convert references to the _DYNAMIC symbol. | |
930 | if (strcmp(gsym->name(), "_DYNAMIC") == 0) | |
931 | return false; | |
932 | // Check for a MOV opcode. | |
933 | return (*view)[r_offset - 2] == 0x8b; | |
934 | } | |
935 | ||
936 | // Convert | |
937 | // callq *foo@GOTPCRELX(%rip) to | |
938 | // addr32 callq foo | |
939 | // and jmpq *foo@GOTPCRELX(%rip) to | |
940 | // jmpq foo | |
941 | // nop | |
942 | template<class View_type> | |
943 | static inline bool | |
944 | can_convert_callq_to_direct(const Symbol* gsym, unsigned int r_type, | |
945 | size_t r_offset, View_type* view) | |
946 | { | |
947 | gold_assert(gsym != NULL); | |
948 | // We cannot do the conversion unless it's a GOTPCRELX relocation. | |
949 | if (r_type != elfcpp::R_X86_64_GOTPCRELX) | |
950 | return false; | |
951 | // We cannot convert references to IFUNC symbols, or to symbols that | |
952 | // are not local to the current module. | |
953 | if (gsym->type() == elfcpp::STT_GNU_IFUNC | |
954 | || gsym->is_undefined () | |
955 | || gsym->is_from_dynobj() | |
956 | || gsym->is_preemptible()) | |
957 | return false; | |
958 | // Check for a CALLQ or JMPQ opcode. | |
959 | return ((*view)[r_offset - 2] == 0xff | |
960 | && ((*view)[r_offset - 1] == 0x15 | |
961 | || (*view)[r_offset - 1] == 0x25)); | |
1fa29f10 IT |
962 | } |
963 | ||
2e30d253 ILT |
964 | // Adjust TLS relocation type based on the options and whether this |
965 | // is a local symbol. | |
e041f13d | 966 | static tls::Tls_optimization |
2e30d253 ILT |
967 | optimize_tls_reloc(bool is_final, int r_type); |
968 | ||
969 | // Get the GOT section, creating it if necessary. | |
970 | Output_data_got<64, false>* | |
971 | got_section(Symbol_table*, Layout*); | |
972 | ||
96f2030e | 973 | // Get the GOT PLT section. |
57b2284c | 974 | Output_data_got_plt_x86_64* |
96f2030e ILT |
975 | got_plt_section() const |
976 | { | |
977 | gold_assert(this->got_plt_ != NULL); | |
978 | return this->got_plt_; | |
979 | } | |
980 | ||
a8df5856 ILT |
981 | // Get the GOT section for TLSDESC entries. |
982 | Output_data_got<64, false>* | |
983 | got_tlsdesc_section() const | |
984 | { | |
985 | gold_assert(this->got_tlsdesc_ != NULL); | |
986 | return this->got_tlsdesc_; | |
987 | } | |
988 | ||
c2b45e22 CC |
989 | // Create the PLT section. |
990 | void | |
991 | make_plt_section(Symbol_table* symtab, Layout* layout); | |
992 | ||
2e30d253 ILT |
993 | // Create a PLT entry for a global symbol. |
994 | void | |
995 | make_plt_entry(Symbol_table*, Layout*, Symbol*); | |
996 | ||
7223e9ca ILT |
997 | // Create a PLT entry for a local STT_GNU_IFUNC symbol. |
998 | void | |
999 | make_local_ifunc_plt_entry(Symbol_table*, Layout*, | |
fc51264f | 1000 | Sized_relobj_file<size, false>* relobj, |
7223e9ca ILT |
1001 | unsigned int local_sym_index); |
1002 | ||
9fa33bee | 1003 | // Define the _TLS_MODULE_BASE_ symbol in the TLS segment. |
edfbb029 CC |
1004 | void |
1005 | define_tls_base_symbol(Symbol_table*, Layout*); | |
1006 | ||
c2b45e22 CC |
1007 | // Create the reserved PLT and GOT entries for the TLS descriptor resolver. |
1008 | void | |
1009 | reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout); | |
1010 | ||
31d60480 ILT |
1011 | // Create a GOT entry for the TLS module index. |
1012 | unsigned int | |
1013 | got_mod_index_entry(Symbol_table* symtab, Layout* layout, | |
fc51264f | 1014 | Sized_relobj_file<size, false>* object); |
31d60480 | 1015 | |
2e30d253 | 1016 | // Get the PLT section. |
fc51264f | 1017 | Output_data_plt_x86_64<size>* |
2e30d253 ILT |
1018 | plt_section() const |
1019 | { | |
1020 | gold_assert(this->plt_ != NULL); | |
1021 | return this->plt_; | |
1022 | } | |
1023 | ||
1024 | // Get the dynamic reloc section, creating it if necessary. | |
1025 | Reloc_section* | |
0ffd9845 | 1026 | rela_dyn_section(Layout*); |
2e30d253 | 1027 | |
e291e7b9 ILT |
1028 | // Get the section to use for TLSDESC relocations. |
1029 | Reloc_section* | |
1030 | rela_tlsdesc_section(Layout*) const; | |
1031 | ||
67181c72 ILT |
1032 | // Get the section to use for IRELATIVE relocations. |
1033 | Reloc_section* | |
1034 | rela_irelative_section(Layout*); | |
1035 | ||
12c0daef | 1036 | // Add a potential copy relocation. |
2e30d253 | 1037 | void |
ef9beddf | 1038 | copy_reloc(Symbol_table* symtab, Layout* layout, |
2e702c99 | 1039 | Sized_relobj_file<size, false>* object, |
12c0daef | 1040 | unsigned int shndx, Output_section* output_section, |
fc51264f | 1041 | Symbol* sym, const elfcpp::Rela<size, false>& reloc) |
12c0daef | 1042 | { |
859d7987 | 1043 | unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info()); |
12c0daef | 1044 | this->copy_relocs_.copy_reloc(symtab, layout, |
fc51264f | 1045 | symtab->get_sized_symbol<size>(sym), |
12c0daef | 1046 | object, shndx, output_section, |
859d7987 CC |
1047 | r_type, reloc.get_r_offset(), |
1048 | reloc.get_r_addend(), | |
1049 | this->rela_dyn_section(layout)); | |
12c0daef | 1050 | } |
2e30d253 ILT |
1051 | |
1052 | // Information about this specific target which we pass to the | |
1053 | // general Target structure. | |
1054 | static const Target::Target_info x86_64_info; | |
1055 | ||
0e70b911 CC |
1056 | // The types of GOT entries needed for this platform. |
1057 | // These values are exposed to the ABI in an incremental link. | |
1058 | // Do not renumber existing values without changing the version | |
1059 | // number of the .gnu_incremental_inputs section. | |
0a65a3a7 CC |
1060 | enum Got_type |
1061 | { | |
1062 | GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol | |
1063 | GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset | |
1064 | GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair | |
1065 | GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair | |
1066 | }; | |
1067 | ||
e291e7b9 ILT |
1068 | // This type is used as the argument to the target specific |
1069 | // relocation routines. The only target specific reloc is | |
1070 | // R_X86_64_TLSDESC against a local symbol. | |
1071 | struct Tlsdesc_info | |
1072 | { | |
fc51264f | 1073 | Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym) |
e291e7b9 ILT |
1074 | : object(a_object), r_sym(a_r_sym) |
1075 | { } | |
1076 | ||
1077 | // The object in which the local symbol is defined. | |
fc51264f | 1078 | Sized_relobj_file<size, false>* object; |
e291e7b9 ILT |
1079 | // The local symbol index in the object. |
1080 | unsigned int r_sym; | |
1081 | }; | |
1082 | ||
2e30d253 ILT |
1083 | // The GOT section. |
1084 | Output_data_got<64, false>* got_; | |
1085 | // The PLT section. | |
fc51264f | 1086 | Output_data_plt_x86_64<size>* plt_; |
2e30d253 | 1087 | // The GOT PLT section. |
57b2284c | 1088 | Output_data_got_plt_x86_64* got_plt_; |
67181c72 ILT |
1089 | // The GOT section for IRELATIVE relocations. |
1090 | Output_data_space* got_irelative_; | |
a8df5856 ILT |
1091 | // The GOT section for TLSDESC relocations. |
1092 | Output_data_got<64, false>* got_tlsdesc_; | |
e785ec03 ILT |
1093 | // The _GLOBAL_OFFSET_TABLE_ symbol. |
1094 | Symbol* global_offset_table_; | |
2e30d253 | 1095 | // The dynamic reloc section. |
0ffd9845 | 1096 | Reloc_section* rela_dyn_; |
67181c72 ILT |
1097 | // The section to use for IRELATIVE relocs. |
1098 | Reloc_section* rela_irelative_; | |
2e30d253 | 1099 | // Relocs saved to avoid a COPY reloc. |
fc51264f | 1100 | Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_; |
c2b45e22 | 1101 | // Offset of the GOT entry for the TLS module index. |
31d60480 | 1102 | unsigned int got_mod_index_offset_; |
e291e7b9 ILT |
1103 | // We handle R_X86_64_TLSDESC against a local symbol as a target |
1104 | // specific relocation. Here we store the object and local symbol | |
1105 | // index for the relocation. | |
1106 | std::vector<Tlsdesc_info> tlsdesc_reloc_info_; | |
edfbb029 CC |
1107 | // True if the _TLS_MODULE_BASE_ symbol has been defined. |
1108 | bool tls_base_symbol_defined_; | |
2e30d253 ILT |
1109 | }; |
1110 | ||
fc51264f L |
1111 | template<> |
1112 | const Target::Target_info Target_x86_64<64>::x86_64_info = | |
2e30d253 ILT |
1113 | { |
1114 | 64, // size | |
1115 | false, // is_big_endian | |
1116 | elfcpp::EM_X86_64, // machine_code | |
1117 | false, // has_make_symbol | |
1118 | false, // has_resolve | |
1119 | true, // has_code_fill | |
35cdfc9a | 1120 | true, // is_default_stack_executable |
b3ce541e | 1121 | true, // can_icf_inline_merge_sections |
0864d551 | 1122 | '\0', // wrap_char |
2e30d253 | 1123 | "/lib/ld64.so.1", // program interpreter |
0c5e9c22 | 1124 | 0x400000, // default_text_segment_address |
cd72c291 | 1125 | 0x1000, // abi_pagesize (overridable by -z max-page-size) |
8a5e3e08 | 1126 | 0x1000, // common_pagesize (overridable by -z common-page-size) |
2e702c99 RM |
1127 | false, // isolate_execinstr |
1128 | 0, // rosegment_gap | |
8a5e3e08 ILT |
1129 | elfcpp::SHN_UNDEF, // small_common_shndx |
1130 | elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx | |
1131 | 0, // small_common_section_flags | |
05a352e6 DK |
1132 | elfcpp::SHF_X86_64_LARGE, // large_common_section_flags |
1133 | NULL, // attributes_section | |
a67858e0 | 1134 | NULL, // attributes_vendor |
8d9743bd MK |
1135 | "_start", // entry_symbol_name |
1136 | 32, // hash_entry_size | |
2e30d253 ILT |
1137 | }; |
1138 | ||
fc51264f L |
1139 | template<> |
1140 | const Target::Target_info Target_x86_64<32>::x86_64_info = | |
1141 | { | |
1142 | 32, // size | |
1143 | false, // is_big_endian | |
1144 | elfcpp::EM_X86_64, // machine_code | |
1145 | false, // has_make_symbol | |
1146 | false, // has_resolve | |
1147 | true, // has_code_fill | |
1148 | true, // is_default_stack_executable | |
1149 | true, // can_icf_inline_merge_sections | |
1150 | '\0', // wrap_char | |
1151 | "/libx32/ldx32.so.1", // program interpreter | |
1152 | 0x400000, // default_text_segment_address | |
1153 | 0x1000, // abi_pagesize (overridable by -z max-page-size) | |
1154 | 0x1000, // common_pagesize (overridable by -z common-page-size) | |
2e702c99 RM |
1155 | false, // isolate_execinstr |
1156 | 0, // rosegment_gap | |
fc51264f L |
1157 | elfcpp::SHN_UNDEF, // small_common_shndx |
1158 | elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx | |
1159 | 0, // small_common_section_flags | |
1160 | elfcpp::SHF_X86_64_LARGE, // large_common_section_flags | |
1161 | NULL, // attributes_section | |
a67858e0 | 1162 | NULL, // attributes_vendor |
8d9743bd MK |
1163 | "_start", // entry_symbol_name |
1164 | 32, // hash_entry_size | |
fc51264f L |
1165 | }; |
1166 | ||
8a5e3e08 ILT |
1167 | // This is called when a new output section is created. This is where |
1168 | // we handle the SHF_X86_64_LARGE. | |
1169 | ||
fc51264f | 1170 | template<int size> |
8a5e3e08 | 1171 | void |
fc51264f | 1172 | Target_x86_64<size>::do_new_output_section(Output_section* os) const |
8a5e3e08 ILT |
1173 | { |
1174 | if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0) | |
1175 | os->set_is_large_section(); | |
1176 | } | |
1177 | ||
2e30d253 ILT |
1178 | // Get the GOT section, creating it if necessary. |
1179 | ||
fc51264f | 1180 | template<int size> |
2e30d253 | 1181 | Output_data_got<64, false>* |
fc51264f | 1182 | Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout) |
2e30d253 ILT |
1183 | { |
1184 | if (this->got_ == NULL) | |
1185 | { | |
1186 | gold_assert(symtab != NULL && layout != NULL); | |
1187 | ||
9446efde ILT |
1188 | // When using -z now, we can treat .got.plt as a relro section. |
1189 | // Without -z now, it is modified after program startup by lazy | |
1190 | // PLT relocations. | |
1191 | bool is_got_plt_relro = parameters->options().now(); | |
1192 | Output_section_order got_order = (is_got_plt_relro | |
1193 | ? ORDER_RELRO | |
1194 | : ORDER_RELRO_LAST); | |
1195 | Output_section_order got_plt_order = (is_got_plt_relro | |
1196 | ? ORDER_RELRO | |
1197 | : ORDER_NON_RELRO_FIRST); | |
1198 | ||
2e30d253 ILT |
1199 | this->got_ = new Output_data_got<64, false>(); |
1200 | ||
82742395 ILT |
1201 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, |
1202 | (elfcpp::SHF_ALLOC | |
1203 | | elfcpp::SHF_WRITE), | |
9446efde | 1204 | this->got_, got_order, true); |
2e30d253 | 1205 | |
57b2284c | 1206 | this->got_plt_ = new Output_data_got_plt_x86_64(layout); |
82742395 ILT |
1207 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, |
1208 | (elfcpp::SHF_ALLOC | |
1209 | | elfcpp::SHF_WRITE), | |
9446efde ILT |
1210 | this->got_plt_, got_plt_order, |
1211 | is_got_plt_relro); | |
2e30d253 ILT |
1212 | |
1213 | // The first three entries are reserved. | |
27bc2bce | 1214 | this->got_plt_->set_current_data_size(3 * 8); |
2e30d253 | 1215 | |
9446efde ILT |
1216 | if (!is_got_plt_relro) |
1217 | { | |
1218 | // Those bytes can go into the relro segment. | |
1219 | layout->increase_relro(3 * 8); | |
1220 | } | |
1a2dff53 | 1221 | |
2e30d253 | 1222 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. |
e785ec03 ILT |
1223 | this->global_offset_table_ = |
1224 | symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, | |
1225 | Symbol_table::PREDEFINED, | |
1226 | this->got_plt_, | |
1227 | 0, 0, elfcpp::STT_OBJECT, | |
1228 | elfcpp::STB_LOCAL, | |
1229 | elfcpp::STV_HIDDEN, 0, | |
1230 | false, false); | |
a8df5856 | 1231 | |
67181c72 ILT |
1232 | // If there are any IRELATIVE relocations, they get GOT entries |
1233 | // in .got.plt after the jump slot entries. | |
1234 | this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT"); | |
1235 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, | |
1236 | (elfcpp::SHF_ALLOC | |
1237 | | elfcpp::SHF_WRITE), | |
1238 | this->got_irelative_, | |
9446efde | 1239 | got_plt_order, is_got_plt_relro); |
67181c72 | 1240 | |
a8df5856 | 1241 | // If there are any TLSDESC relocations, they get GOT entries in |
67181c72 | 1242 | // .got.plt after the jump slot and IRELATIVE entries. |
a8df5856 ILT |
1243 | this->got_tlsdesc_ = new Output_data_got<64, false>(); |
1244 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, | |
1245 | (elfcpp::SHF_ALLOC | |
1246 | | elfcpp::SHF_WRITE), | |
22f0da72 | 1247 | this->got_tlsdesc_, |
9446efde | 1248 | got_plt_order, is_got_plt_relro); |
2e30d253 ILT |
1249 | } |
1250 | ||
1251 | return this->got_; | |
1252 | } | |
1253 | ||
1254 | // Get the dynamic reloc section, creating it if necessary. | |
1255 | ||
fc51264f L |
1256 | template<int size> |
1257 | typename Target_x86_64<size>::Reloc_section* | |
1258 | Target_x86_64<size>::rela_dyn_section(Layout* layout) | |
2e30d253 | 1259 | { |
0ffd9845 | 1260 | if (this->rela_dyn_ == NULL) |
2e30d253 ILT |
1261 | { |
1262 | gold_assert(layout != NULL); | |
d98bc257 | 1263 | this->rela_dyn_ = new Reloc_section(parameters->options().combreloc()); |
2e30d253 | 1264 | layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, |
22f0da72 ILT |
1265 | elfcpp::SHF_ALLOC, this->rela_dyn_, |
1266 | ORDER_DYNAMIC_RELOCS, false); | |
2e30d253 | 1267 | } |
0ffd9845 | 1268 | return this->rela_dyn_; |
2e30d253 ILT |
1269 | } |
1270 | ||
67181c72 ILT |
1271 | // Get the section to use for IRELATIVE relocs, creating it if |
1272 | // necessary. These go in .rela.dyn, but only after all other dynamic | |
1273 | // relocations. They need to follow the other dynamic relocations so | |
1274 | // that they can refer to global variables initialized by those | |
1275 | // relocs. | |
1276 | ||
fc51264f L |
1277 | template<int size> |
1278 | typename Target_x86_64<size>::Reloc_section* | |
1279 | Target_x86_64<size>::rela_irelative_section(Layout* layout) | |
67181c72 ILT |
1280 | { |
1281 | if (this->rela_irelative_ == NULL) | |
1282 | { | |
1283 | // Make sure we have already created the dynamic reloc section. | |
1284 | this->rela_dyn_section(layout); | |
1285 | this->rela_irelative_ = new Reloc_section(false); | |
1286 | layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, | |
1287 | elfcpp::SHF_ALLOC, this->rela_irelative_, | |
1288 | ORDER_DYNAMIC_RELOCS, false); | |
1289 | gold_assert(this->rela_dyn_->output_section() | |
1290 | == this->rela_irelative_->output_section()); | |
1291 | } | |
1292 | return this->rela_irelative_; | |
1293 | } | |
1294 | ||
57b2284c CC |
1295 | // Write the first three reserved words of the .got.plt section. |
1296 | // The remainder of the section is written while writing the PLT | |
1297 | // in Output_data_plt_i386::do_write. | |
1298 | ||
1299 | void | |
1300 | Output_data_got_plt_x86_64::do_write(Output_file* of) | |
1301 | { | |
1302 | // The first entry in the GOT is the address of the .dynamic section | |
1303 | // aka the PT_DYNAMIC segment. The next two entries are reserved. | |
1304 | // We saved space for them when we created the section in | |
1305 | // Target_x86_64::got_section. | |
1306 | const off_t got_file_offset = this->offset(); | |
1307 | gold_assert(this->data_size() >= 24); | |
1308 | unsigned char* const got_view = of->get_output_view(got_file_offset, 24); | |
1309 | Output_section* dynamic = this->layout_->dynamic_section(); | |
1310 | uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address(); | |
1311 | elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr); | |
1312 | memset(got_view + 8, 0, 16); | |
1313 | of->write_output_view(got_file_offset, 24, got_view); | |
1314 | } | |
1315 | ||
4829d394 | 1316 | // Initialize the PLT section. |
2e30d253 | 1317 | |
fc51264f | 1318 | template<int size> |
4829d394 | 1319 | void |
fc51264f | 1320 | Output_data_plt_x86_64<size>::init(Layout* layout) |
2e30d253 | 1321 | { |
d98bc257 | 1322 | this->rel_ = new Reloc_section(false); |
2e30d253 | 1323 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, |
22f0da72 ILT |
1324 | elfcpp::SHF_ALLOC, this->rel_, |
1325 | ORDER_DYNAMIC_PLT_RELOCS, false); | |
2e30d253 ILT |
1326 | } |
1327 | ||
fc51264f | 1328 | template<int size> |
2e30d253 | 1329 | void |
fc51264f | 1330 | Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os) |
2e30d253 | 1331 | { |
2e702c99 | 1332 | os->set_entsize(this->get_plt_entry_size()); |
2e30d253 ILT |
1333 | } |
1334 | ||
1335 | // Add an entry to the PLT. | |
1336 | ||
fc51264f | 1337 | template<int size> |
2e30d253 | 1338 | void |
fc51264f L |
1339 | Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout, |
1340 | Symbol* gsym) | |
2e30d253 ILT |
1341 | { |
1342 | gold_assert(!gsym->has_plt_offset()); | |
1343 | ||
4829d394 CC |
1344 | unsigned int plt_index; |
1345 | off_t plt_offset; | |
1346 | section_offset_type got_offset; | |
2e30d253 | 1347 | |
67181c72 ILT |
1348 | unsigned int* pcount; |
1349 | unsigned int offset; | |
1350 | unsigned int reserved; | |
57b2284c | 1351 | Output_section_data_build* got; |
67181c72 ILT |
1352 | if (gsym->type() == elfcpp::STT_GNU_IFUNC |
1353 | && gsym->can_use_relative_reloc(false)) | |
1354 | { | |
1355 | pcount = &this->irelative_count_; | |
1356 | offset = 0; | |
1357 | reserved = 0; | |
1358 | got = this->got_irelative_; | |
1359 | } | |
1360 | else | |
1361 | { | |
1362 | pcount = &this->count_; | |
1363 | offset = 1; | |
1364 | reserved = 3; | |
1365 | got = this->got_plt_; | |
1366 | } | |
1367 | ||
4829d394 CC |
1368 | if (!this->is_data_size_valid()) |
1369 | { | |
67181c72 ILT |
1370 | // Note that when setting the PLT offset for a non-IRELATIVE |
1371 | // entry we skip the initial reserved PLT entry. | |
1372 | plt_index = *pcount + offset; | |
2e702c99 | 1373 | plt_offset = plt_index * this->get_plt_entry_size(); |
2e30d253 | 1374 | |
67181c72 | 1375 | ++*pcount; |
2e30d253 | 1376 | |
67181c72 ILT |
1377 | got_offset = (plt_index - offset + reserved) * 8; |
1378 | gold_assert(got_offset == got->current_data_size()); | |
2e30d253 | 1379 | |
4829d394 CC |
1380 | // Every PLT entry needs a GOT entry which points back to the PLT |
1381 | // entry (this will be changed by the dynamic linker, normally | |
1382 | // lazily when the function is called). | |
67181c72 | 1383 | got->set_current_data_size(got_offset + 8); |
4829d394 | 1384 | } |
7223e9ca ILT |
1385 | else |
1386 | { | |
67181c72 ILT |
1387 | // FIXME: This is probably not correct for IRELATIVE relocs. |
1388 | ||
4829d394 | 1389 | // For incremental updates, find an available slot. |
2e702c99 RM |
1390 | plt_offset = this->free_list_.allocate(this->get_plt_entry_size(), |
1391 | this->get_plt_entry_size(), 0); | |
4829d394 | 1392 | if (plt_offset == -1) |
e6455dfb CC |
1393 | gold_fallback(_("out of patch space (PLT);" |
1394 | " relink with --incremental-full")); | |
4829d394 CC |
1395 | |
1396 | // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset | |
1397 | // can be calculated from the PLT index, adjusting for the three | |
1398 | // reserved entries at the beginning of the GOT. | |
2e702c99 | 1399 | plt_index = plt_offset / this->get_plt_entry_size() - 1; |
67181c72 | 1400 | got_offset = (plt_index - offset + reserved) * 8; |
7223e9ca | 1401 | } |
2e30d253 | 1402 | |
4829d394 CC |
1403 | gsym->set_plt_offset(plt_offset); |
1404 | ||
1405 | // Every PLT entry needs a reloc. | |
67181c72 | 1406 | this->add_relocation(symtab, layout, gsym, got_offset); |
4829d394 | 1407 | |
2e30d253 ILT |
1408 | // Note that we don't need to save the symbol. The contents of the |
1409 | // PLT are independent of which symbols are used. The symbols only | |
1410 | // appear in the relocations. | |
1411 | } | |
1412 | ||
7223e9ca ILT |
1413 | // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return |
1414 | // the PLT offset. | |
1415 | ||
fc51264f | 1416 | template<int size> |
7223e9ca | 1417 | unsigned int |
fc51264f | 1418 | Output_data_plt_x86_64<size>::add_local_ifunc_entry( |
67181c72 ILT |
1419 | Symbol_table* symtab, |
1420 | Layout* layout, | |
fc51264f | 1421 | Sized_relobj_file<size, false>* relobj, |
6fa2a40b | 1422 | unsigned int local_sym_index) |
7223e9ca | 1423 | { |
2e702c99 | 1424 | unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size(); |
67181c72 | 1425 | ++this->irelative_count_; |
7223e9ca | 1426 | |
67181c72 | 1427 | section_offset_type got_offset = this->got_irelative_->current_data_size(); |
7223e9ca ILT |
1428 | |
1429 | // Every PLT entry needs a GOT entry which points back to the PLT | |
1430 | // entry. | |
67181c72 | 1431 | this->got_irelative_->set_current_data_size(got_offset + 8); |
7223e9ca ILT |
1432 | |
1433 | // Every PLT entry needs a reloc. | |
67181c72 ILT |
1434 | Reloc_section* rela = this->rela_irelative(symtab, layout); |
1435 | rela->add_symbolless_local_addend(relobj, local_sym_index, | |
1436 | elfcpp::R_X86_64_IRELATIVE, | |
1437 | this->got_irelative_, got_offset, 0); | |
7223e9ca ILT |
1438 | |
1439 | return plt_offset; | |
1440 | } | |
1441 | ||
4829d394 CC |
1442 | // Add the relocation for a PLT entry. |
1443 | ||
fc51264f | 1444 | template<int size> |
4829d394 | 1445 | void |
fc51264f L |
1446 | Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab, |
1447 | Layout* layout, | |
1448 | Symbol* gsym, | |
1449 | unsigned int got_offset) | |
4829d394 CC |
1450 | { |
1451 | if (gsym->type() == elfcpp::STT_GNU_IFUNC | |
1452 | && gsym->can_use_relative_reloc(false)) | |
67181c72 ILT |
1453 | { |
1454 | Reloc_section* rela = this->rela_irelative(symtab, layout); | |
1455 | rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE, | |
1456 | this->got_irelative_, got_offset, 0); | |
1457 | } | |
4829d394 CC |
1458 | else |
1459 | { | |
1460 | gsym->set_needs_dynsym_entry(); | |
1461 | this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_, | |
1462 | got_offset, 0); | |
1463 | } | |
1464 | } | |
1465 | ||
e291e7b9 ILT |
1466 | // Return where the TLSDESC relocations should go, creating it if |
1467 | // necessary. These follow the JUMP_SLOT relocations. | |
1468 | ||
fc51264f L |
1469 | template<int size> |
1470 | typename Output_data_plt_x86_64<size>::Reloc_section* | |
1471 | Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout) | |
e291e7b9 ILT |
1472 | { |
1473 | if (this->tlsdesc_rel_ == NULL) | |
1474 | { | |
1475 | this->tlsdesc_rel_ = new Reloc_section(false); | |
1476 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, | |
1477 | elfcpp::SHF_ALLOC, this->tlsdesc_rel_, | |
22f0da72 | 1478 | ORDER_DYNAMIC_PLT_RELOCS, false); |
67181c72 ILT |
1479 | gold_assert(this->tlsdesc_rel_->output_section() |
1480 | == this->rel_->output_section()); | |
e291e7b9 ILT |
1481 | } |
1482 | return this->tlsdesc_rel_; | |
1483 | } | |
1484 | ||
67181c72 ILT |
1485 | // Return where the IRELATIVE relocations should go in the PLT. These |
1486 | // follow the JUMP_SLOT and the TLSDESC relocations. | |
1487 | ||
fc51264f L |
1488 | template<int size> |
1489 | typename Output_data_plt_x86_64<size>::Reloc_section* | |
1490 | Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab, | |
1491 | Layout* layout) | |
67181c72 ILT |
1492 | { |
1493 | if (this->irelative_rel_ == NULL) | |
1494 | { | |
1495 | // Make sure we have a place for the TLSDESC relocations, in | |
1496 | // case we see any later on. | |
1497 | this->rela_tlsdesc(layout); | |
1498 | this->irelative_rel_ = new Reloc_section(false); | |
1499 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, | |
1500 | elfcpp::SHF_ALLOC, this->irelative_rel_, | |
1501 | ORDER_DYNAMIC_PLT_RELOCS, false); | |
1502 | gold_assert(this->irelative_rel_->output_section() | |
1503 | == this->rel_->output_section()); | |
1504 | ||
1505 | if (parameters->doing_static_link()) | |
1506 | { | |
1507 | // A statically linked executable will only have a .rela.plt | |
1508 | // section to hold R_X86_64_IRELATIVE relocs for | |
1509 | // STT_GNU_IFUNC symbols. The library will use these | |
1510 | // symbols to locate the IRELATIVE relocs at program startup | |
1511 | // time. | |
1512 | symtab->define_in_output_data("__rela_iplt_start", NULL, | |
1513 | Symbol_table::PREDEFINED, | |
1514 | this->irelative_rel_, 0, 0, | |
1515 | elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, | |
1516 | elfcpp::STV_HIDDEN, 0, false, true); | |
1517 | symtab->define_in_output_data("__rela_iplt_end", NULL, | |
1518 | Symbol_table::PREDEFINED, | |
1519 | this->irelative_rel_, 0, 0, | |
1520 | elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, | |
1521 | elfcpp::STV_HIDDEN, 0, true, true); | |
1522 | } | |
1523 | } | |
1524 | return this->irelative_rel_; | |
1525 | } | |
1526 | ||
1527 | // Return the PLT address to use for a global symbol. | |
1528 | ||
fc51264f | 1529 | template<int size> |
67181c72 | 1530 | uint64_t |
fc51264f | 1531 | Output_data_plt_x86_64<size>::address_for_global(const Symbol* gsym) |
67181c72 ILT |
1532 | { |
1533 | uint64_t offset = 0; | |
1534 | if (gsym->type() == elfcpp::STT_GNU_IFUNC | |
1535 | && gsym->can_use_relative_reloc(false)) | |
2e702c99 | 1536 | offset = (this->count_ + 1) * this->get_plt_entry_size(); |
19fec8c1 | 1537 | return this->address() + offset + gsym->plt_offset(); |
67181c72 ILT |
1538 | } |
1539 | ||
1540 | // Return the PLT address to use for a local symbol. These are always | |
1541 | // IRELATIVE relocs. | |
1542 | ||
fc51264f | 1543 | template<int size> |
67181c72 | 1544 | uint64_t |
19fec8c1 AM |
1545 | Output_data_plt_x86_64<size>::address_for_local(const Relobj* object, |
1546 | unsigned int r_sym) | |
67181c72 | 1547 | { |
19fec8c1 AM |
1548 | return (this->address() |
1549 | + (this->count_ + 1) * this->get_plt_entry_size() | |
1550 | + object->local_plt_offset(r_sym)); | |
67181c72 ILT |
1551 | } |
1552 | ||
c2b45e22 | 1553 | // Set the final size. |
fc51264f | 1554 | template<int size> |
c2b45e22 | 1555 | void |
fc51264f | 1556 | Output_data_plt_x86_64<size>::set_final_data_size() |
c2b45e22 | 1557 | { |
67181c72 | 1558 | unsigned int count = this->count_ + this->irelative_count_; |
c2b45e22 CC |
1559 | if (this->has_tlsdesc_entry()) |
1560 | ++count; | |
2e702c99 | 1561 | this->set_data_size((count + 1) * this->get_plt_entry_size()); |
c2b45e22 CC |
1562 | } |
1563 | ||
2e30d253 ILT |
1564 | // The first entry in the PLT for an executable. |
1565 | ||
fc51264f L |
1566 | template<int size> |
1567 | const unsigned char | |
2e702c99 | 1568 | Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] = |
2e30d253 ILT |
1569 | { |
1570 | // From AMD64 ABI Draft 0.98, page 76 | |
1571 | 0xff, 0x35, // pushq contents of memory address | |
2e30d253 | 1572 | 0, 0, 0, 0, // replaced with address of .got + 8 |
78d911fd ILT |
1573 | 0xff, 0x25, // jmp indirect |
1574 | 0, 0, 0, 0, // replaced with address of .got + 16 | |
2e30d253 ILT |
1575 | 0x90, 0x90, 0x90, 0x90 // noop (x4) |
1576 | }; | |
1577 | ||
2e702c99 RM |
1578 | template<int size> |
1579 | void | |
1580 | Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry( | |
1581 | unsigned char* pov, | |
1582 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
1583 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address) | |
1584 | { | |
1585 | memcpy(pov, first_plt_entry, plt_entry_size); | |
1586 | // We do a jmp relative to the PC at the end of this instruction. | |
1587 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, | |
1588 | (got_address + 8 | |
1589 | - (plt_address + 6))); | |
1590 | elfcpp::Swap<32, false>::writeval(pov + 8, | |
1591 | (got_address + 16 | |
1592 | - (plt_address + 12))); | |
1593 | } | |
1594 | ||
2e30d253 ILT |
1595 | // Subsequent entries in the PLT for an executable. |
1596 | ||
fc51264f L |
1597 | template<int size> |
1598 | const unsigned char | |
2e702c99 | 1599 | Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] = |
2e30d253 ILT |
1600 | { |
1601 | // From AMD64 ABI Draft 0.98, page 76 | |
1602 | 0xff, 0x25, // jmpq indirect | |
1603 | 0, 0, 0, 0, // replaced with address of symbol in .got | |
1604 | 0x68, // pushq immediate | |
1605 | 0, 0, 0, 0, // replaced with offset into relocation table | |
1606 | 0xe9, // jmpq relative | |
1607 | 0, 0, 0, 0 // replaced with offset to start of .plt | |
1608 | }; | |
1609 | ||
2e702c99 RM |
1610 | template<int size> |
1611 | unsigned int | |
1612 | Output_data_plt_x86_64_standard<size>::do_fill_plt_entry( | |
1613 | unsigned char* pov, | |
1614 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
1615 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
1616 | unsigned int got_offset, | |
1617 | unsigned int plt_offset, | |
1618 | unsigned int plt_index) | |
1619 | { | |
9d585188 L |
1620 | // Check PC-relative offset overflow in PLT entry. |
1621 | uint64_t plt_got_pcrel_offset = (got_address + got_offset | |
1622 | - (plt_address + plt_offset + 6)); | |
1623 | if (Bits<32>::has_overflow(plt_got_pcrel_offset)) | |
1624 | gold_error(_("PC-relative offset overflow in PLT entry %d"), | |
1625 | plt_index + 1); | |
1626 | ||
2e702c99 RM |
1627 | memcpy(pov, plt_entry, plt_entry_size); |
1628 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, | |
9d585188 | 1629 | plt_got_pcrel_offset); |
2e702c99 RM |
1630 | |
1631 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index); | |
1632 | elfcpp::Swap<32, false>::writeval(pov + 12, | |
1633 | - (plt_offset + plt_entry_size)); | |
1634 | ||
1635 | return 6; | |
1636 | } | |
1637 | ||
c2b45e22 CC |
1638 | // The reserved TLSDESC entry in the PLT for an executable. |
1639 | ||
fc51264f L |
1640 | template<int size> |
1641 | const unsigned char | |
2e702c99 | 1642 | Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] = |
c2b45e22 CC |
1643 | { |
1644 | // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32 | |
1645 | // and AMD64/EM64T", Version 0.9.4 (2005-10-10). | |
1646 | 0xff, 0x35, // pushq x(%rip) | |
1647 | 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8) | |
1648 | 0xff, 0x25, // jmpq *y(%rip) | |
1649 | 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry | |
1650 | 0x0f, 0x1f, // nop | |
1651 | 0x40, 0 | |
1652 | }; | |
1653 | ||
2e702c99 RM |
1654 | template<int size> |
1655 | void | |
1656 | Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry( | |
1657 | unsigned char* pov, | |
1658 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
1659 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
1660 | typename elfcpp::Elf_types<size>::Elf_Addr got_base, | |
1661 | unsigned int tlsdesc_got_offset, | |
1662 | unsigned int plt_offset) | |
1663 | { | |
1664 | memcpy(pov, tlsdesc_plt_entry, plt_entry_size); | |
1665 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, | |
1666 | (got_address + 8 | |
1667 | - (plt_address + plt_offset | |
1668 | + 6))); | |
1669 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 8, | |
1670 | (got_base | |
1671 | + tlsdesc_got_offset | |
1672 | - (plt_address + plt_offset | |
1673 | + 12))); | |
1674 | } | |
1675 | ||
07a60597 ILT |
1676 | // The .eh_frame unwind information for the PLT. |
1677 | ||
fc51264f | 1678 | template<int size> |
2e702c99 | 1679 | const unsigned char |
fc51264f | 1680 | Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] = |
07a60597 ILT |
1681 | { |
1682 | 1, // CIE version. | |
1683 | 'z', // Augmentation: augmentation size included. | |
1684 | 'R', // Augmentation: FDE encoding included. | |
1685 | '\0', // End of augmentation string. | |
1686 | 1, // Code alignment factor. | |
1687 | 0x78, // Data alignment factor. | |
1688 | 16, // Return address column. | |
1689 | 1, // Augmentation size. | |
1690 | (elfcpp::DW_EH_PE_pcrel // FDE encoding. | |
1691 | | elfcpp::DW_EH_PE_sdata4), | |
1692 | elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8. | |
1693 | elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8. | |
1694 | elfcpp::DW_CFA_nop, // Align to 16 bytes. | |
1695 | elfcpp::DW_CFA_nop | |
1696 | }; | |
1697 | ||
fc51264f | 1698 | template<int size> |
07a60597 | 1699 | const unsigned char |
2e702c99 | 1700 | Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] = |
07a60597 ILT |
1701 | { |
1702 | 0, 0, 0, 0, // Replaced with offset to .plt. | |
1703 | 0, 0, 0, 0, // Replaced with size of .plt. | |
1704 | 0, // Augmentation size. | |
1705 | elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16. | |
1706 | elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6. | |
1707 | elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24. | |
1708 | elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16. | |
1709 | elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression. | |
1710 | 11, // Block length. | |
1711 | elfcpp::DW_OP_breg7, 8, // Push %rsp + 8. | |
1712 | elfcpp::DW_OP_breg16, 0, // Push %rip. | |
1713 | elfcpp::DW_OP_lit15, // Push 0xf. | |
1714 | elfcpp::DW_OP_and, // & (%rip & 0xf). | |
1715 | elfcpp::DW_OP_lit11, // Push 0xb. | |
1716 | elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb) | |
1717 | elfcpp::DW_OP_lit3, // Push 3. | |
1718 | elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3) | |
1719 | elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8 | |
1720 | elfcpp::DW_CFA_nop, // Align to 32 bytes. | |
1721 | elfcpp::DW_CFA_nop, | |
1722 | elfcpp::DW_CFA_nop, | |
1723 | elfcpp::DW_CFA_nop | |
1724 | }; | |
1725 | ||
2e30d253 ILT |
1726 | // Write out the PLT. This uses the hand-coded instructions above, |
1727 | // and adjusts them as needed. This is specified by the AMD64 ABI. | |
1728 | ||
fc51264f | 1729 | template<int size> |
2e30d253 | 1730 | void |
fc51264f | 1731 | Output_data_plt_x86_64<size>::do_write(Output_file* of) |
2e30d253 | 1732 | { |
2ea97941 | 1733 | const off_t offset = this->offset(); |
fe8718a4 ILT |
1734 | const section_size_type oview_size = |
1735 | convert_to_section_size_type(this->data_size()); | |
2ea97941 | 1736 | unsigned char* const oview = of->get_output_view(offset, oview_size); |
2e30d253 ILT |
1737 | |
1738 | const off_t got_file_offset = this->got_plt_->offset(); | |
67181c72 ILT |
1739 | gold_assert(parameters->incremental_update() |
1740 | || (got_file_offset + this->got_plt_->data_size() | |
1741 | == this->got_irelative_->offset())); | |
fe8718a4 | 1742 | const section_size_type got_size = |
67181c72 ILT |
1743 | convert_to_section_size_type(this->got_plt_->data_size() |
1744 | + this->got_irelative_->data_size()); | |
2e30d253 ILT |
1745 | unsigned char* const got_view = of->get_output_view(got_file_offset, |
1746 | got_size); | |
1747 | ||
1748 | unsigned char* pov = oview; | |
1749 | ||
c2b45e22 | 1750 | // The base address of the .plt section. |
fc51264f | 1751 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address(); |
c2b45e22 | 1752 | // The base address of the .got section. |
fc51264f | 1753 | typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address(); |
c2b45e22 CC |
1754 | // The base address of the PLT portion of the .got section, |
1755 | // which is where the GOT pointer will point, and where the | |
1756 | // three reserved GOT entries are located. | |
fc51264f L |
1757 | typename elfcpp::Elf_types<size>::Elf_Addr got_address |
1758 | = this->got_plt_->address(); | |
2e30d253 | 1759 | |
2e702c99 RM |
1760 | this->fill_first_plt_entry(pov, got_address, plt_address); |
1761 | pov += this->get_plt_entry_size(); | |
2e30d253 | 1762 | |
57b2284c CC |
1763 | // The first three entries in the GOT are reserved, and are written |
1764 | // by Output_data_got_plt_x86_64::do_write. | |
1765 | unsigned char* got_pov = got_view + 24; | |
2e30d253 | 1766 | |
2e702c99 | 1767 | unsigned int plt_offset = this->get_plt_entry_size(); |
2e30d253 | 1768 | unsigned int got_offset = 24; |
67181c72 | 1769 | const unsigned int count = this->count_ + this->irelative_count_; |
2e30d253 ILT |
1770 | for (unsigned int plt_index = 0; |
1771 | plt_index < count; | |
1772 | ++plt_index, | |
2e702c99 | 1773 | pov += this->get_plt_entry_size(), |
2e30d253 | 1774 | got_pov += 8, |
2e702c99 | 1775 | plt_offset += this->get_plt_entry_size(), |
2e30d253 ILT |
1776 | got_offset += 8) |
1777 | { | |
1778 | // Set and adjust the PLT entry itself. | |
2e702c99 RM |
1779 | unsigned int lazy_offset = this->fill_plt_entry(pov, |
1780 | got_address, plt_address, | |
1781 | got_offset, plt_offset, | |
1782 | plt_index); | |
2e30d253 ILT |
1783 | |
1784 | // Set the entry in the GOT. | |
2e702c99 RM |
1785 | elfcpp::Swap<64, false>::writeval(got_pov, |
1786 | plt_address + plt_offset + lazy_offset); | |
2e30d253 ILT |
1787 | } |
1788 | ||
c2b45e22 CC |
1789 | if (this->has_tlsdesc_entry()) |
1790 | { | |
1791 | // Set and adjust the reserved TLSDESC PLT entry. | |
1792 | unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset(); | |
2e702c99 RM |
1793 | this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base, |
1794 | tlsdesc_got_offset, plt_offset); | |
1795 | pov += this->get_plt_entry_size(); | |
c2b45e22 CC |
1796 | } |
1797 | ||
fe8718a4 ILT |
1798 | gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); |
1799 | gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size); | |
2e30d253 | 1800 | |
2ea97941 | 1801 | of->write_output_view(offset, oview_size, oview); |
2e30d253 ILT |
1802 | of->write_output_view(got_file_offset, got_size, got_view); |
1803 | } | |
1804 | ||
c2b45e22 | 1805 | // Create the PLT section. |
2e30d253 | 1806 | |
fc51264f | 1807 | template<int size> |
2e30d253 | 1808 | void |
fc51264f | 1809 | Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout) |
2e30d253 | 1810 | { |
2e30d253 ILT |
1811 | if (this->plt_ == NULL) |
1812 | { | |
1813 | // Create the GOT sections first. | |
1814 | this->got_section(symtab, layout); | |
1815 | ||
2e702c99 RM |
1816 | this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_, |
1817 | this->got_irelative_); | |
1818 | ||
1819 | // Add unwind information if requested. | |
1820 | if (parameters->options().ld_generated_unwind_info()) | |
1821 | this->plt_->add_eh_frame(layout); | |
1822 | ||
2e30d253 ILT |
1823 | layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, |
1824 | (elfcpp::SHF_ALLOC | |
1825 | | elfcpp::SHF_EXECINSTR), | |
22f0da72 | 1826 | this->plt_, ORDER_PLT, false); |
7223e9ca ILT |
1827 | |
1828 | // Make the sh_info field of .rela.plt point to .plt. | |
1829 | Output_section* rela_plt_os = this->plt_->rela_plt()->output_section(); | |
1830 | rela_plt_os->set_info_section(this->plt_->output_section()); | |
2e30d253 | 1831 | } |
c2b45e22 CC |
1832 | } |
1833 | ||
e291e7b9 ILT |
1834 | // Return the section for TLSDESC relocations. |
1835 | ||
fc51264f L |
1836 | template<int size> |
1837 | typename Target_x86_64<size>::Reloc_section* | |
1838 | Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const | |
e291e7b9 ILT |
1839 | { |
1840 | return this->plt_section()->rela_tlsdesc(layout); | |
1841 | } | |
1842 | ||
c2b45e22 CC |
1843 | // Create a PLT entry for a global symbol. |
1844 | ||
fc51264f | 1845 | template<int size> |
c2b45e22 | 1846 | void |
fc51264f L |
1847 | Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout, |
1848 | Symbol* gsym) | |
c2b45e22 CC |
1849 | { |
1850 | if (gsym->has_plt_offset()) | |
1851 | return; | |
1852 | ||
1853 | if (this->plt_ == NULL) | |
1854 | this->make_plt_section(symtab, layout); | |
2e30d253 | 1855 | |
67181c72 | 1856 | this->plt_->add_entry(symtab, layout, gsym); |
2e30d253 ILT |
1857 | } |
1858 | ||
7223e9ca ILT |
1859 | // Make a PLT entry for a local STT_GNU_IFUNC symbol. |
1860 | ||
fc51264f | 1861 | template<int size> |
7223e9ca | 1862 | void |
fc51264f L |
1863 | Target_x86_64<size>::make_local_ifunc_plt_entry( |
1864 | Symbol_table* symtab, Layout* layout, | |
1865 | Sized_relobj_file<size, false>* relobj, | |
1866 | unsigned int local_sym_index) | |
7223e9ca ILT |
1867 | { |
1868 | if (relobj->local_has_plt_offset(local_sym_index)) | |
1869 | return; | |
1870 | if (this->plt_ == NULL) | |
1871 | this->make_plt_section(symtab, layout); | |
67181c72 ILT |
1872 | unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout, |
1873 | relobj, | |
7223e9ca ILT |
1874 | local_sym_index); |
1875 | relobj->set_local_plt_offset(local_sym_index, plt_offset); | |
1876 | } | |
1877 | ||
0e70b911 CC |
1878 | // Return the number of entries in the PLT. |
1879 | ||
fc51264f | 1880 | template<int size> |
0e70b911 | 1881 | unsigned int |
fc51264f | 1882 | Target_x86_64<size>::plt_entry_count() const |
0e70b911 CC |
1883 | { |
1884 | if (this->plt_ == NULL) | |
1885 | return 0; | |
1886 | return this->plt_->entry_count(); | |
1887 | } | |
1888 | ||
1889 | // Return the offset of the first non-reserved PLT entry. | |
1890 | ||
fc51264f | 1891 | template<int size> |
0e70b911 | 1892 | unsigned int |
fc51264f | 1893 | Target_x86_64<size>::first_plt_entry_offset() const |
0e70b911 | 1894 | { |
8474a88f L |
1895 | if (this->plt_ == NULL) |
1896 | return 0; | |
2e702c99 | 1897 | return this->plt_->first_plt_entry_offset(); |
0e70b911 CC |
1898 | } |
1899 | ||
1900 | // Return the size of each PLT entry. | |
1901 | ||
fc51264f | 1902 | template<int size> |
0e70b911 | 1903 | unsigned int |
fc51264f | 1904 | Target_x86_64<size>::plt_entry_size() const |
0e70b911 | 1905 | { |
8474a88f L |
1906 | if (this->plt_ == NULL) |
1907 | return 0; | |
2e702c99 | 1908 | return this->plt_->get_plt_entry_size(); |
0e70b911 CC |
1909 | } |
1910 | ||
4829d394 CC |
1911 | // Create the GOT and PLT sections for an incremental update. |
1912 | ||
fc51264f | 1913 | template<int size> |
dd74ae06 | 1914 | Output_data_got_base* |
fc51264f | 1915 | Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab, |
4829d394 CC |
1916 | Layout* layout, |
1917 | unsigned int got_count, | |
1918 | unsigned int plt_count) | |
1919 | { | |
1920 | gold_assert(this->got_ == NULL); | |
1921 | ||
1922 | this->got_ = new Output_data_got<64, false>(got_count * 8); | |
1923 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, | |
1924 | (elfcpp::SHF_ALLOC | |
1925 | | elfcpp::SHF_WRITE), | |
1926 | this->got_, ORDER_RELRO_LAST, | |
1927 | true); | |
1928 | ||
1929 | // Add the three reserved entries. | |
57b2284c | 1930 | this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8); |
4829d394 CC |
1931 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, |
1932 | (elfcpp::SHF_ALLOC | |
1933 | | elfcpp::SHF_WRITE), | |
1934 | this->got_plt_, ORDER_NON_RELRO_FIRST, | |
1935 | false); | |
1936 | ||
1937 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. | |
1938 | this->global_offset_table_ = | |
1939 | symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, | |
1940 | Symbol_table::PREDEFINED, | |
1941 | this->got_plt_, | |
1942 | 0, 0, elfcpp::STT_OBJECT, | |
1943 | elfcpp::STB_LOCAL, | |
1944 | elfcpp::STV_HIDDEN, 0, | |
1945 | false, false); | |
1946 | ||
1947 | // If there are any TLSDESC relocations, they get GOT entries in | |
1948 | // .got.plt after the jump slot entries. | |
1949 | // FIXME: Get the count for TLSDESC entries. | |
1950 | this->got_tlsdesc_ = new Output_data_got<64, false>(0); | |
1951 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, | |
1952 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, | |
1953 | this->got_tlsdesc_, | |
1954 | ORDER_NON_RELRO_FIRST, false); | |
1955 | ||
67181c72 ILT |
1956 | // If there are any IRELATIVE relocations, they get GOT entries in |
1957 | // .got.plt after the jump slot and TLSDESC entries. | |
1958 | this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT"); | |
1959 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, | |
1960 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, | |
1961 | this->got_irelative_, | |
1962 | ORDER_NON_RELRO_FIRST, false); | |
1963 | ||
4829d394 | 1964 | // Create the PLT section. |
2e702c99 RM |
1965 | this->plt_ = this->make_data_plt(layout, this->got_, |
1966 | this->got_plt_, | |
1967 | this->got_irelative_, | |
1968 | plt_count); | |
1969 | ||
1970 | // Add unwind information if requested. | |
1971 | if (parameters->options().ld_generated_unwind_info()) | |
1972 | this->plt_->add_eh_frame(layout); | |
1973 | ||
4829d394 CC |
1974 | layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, |
1975 | elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR, | |
1976 | this->plt_, ORDER_PLT, false); | |
1977 | ||
1978 | // Make the sh_info field of .rela.plt point to .plt. | |
1979 | Output_section* rela_plt_os = this->plt_->rela_plt()->output_section(); | |
1980 | rela_plt_os->set_info_section(this->plt_->output_section()); | |
1981 | ||
6fa2a40b CC |
1982 | // Create the rela_dyn section. |
1983 | this->rela_dyn_section(layout); | |
1984 | ||
4829d394 CC |
1985 | return this->got_; |
1986 | } | |
1987 | ||
6fa2a40b CC |
1988 | // Reserve a GOT entry for a local symbol, and regenerate any |
1989 | // necessary dynamic relocations. | |
1990 | ||
fc51264f | 1991 | template<int size> |
6fa2a40b | 1992 | void |
fc51264f | 1993 | Target_x86_64<size>::reserve_local_got_entry( |
6fa2a40b | 1994 | unsigned int got_index, |
fc51264f | 1995 | Sized_relobj<size, false>* obj, |
6fa2a40b CC |
1996 | unsigned int r_sym, |
1997 | unsigned int got_type) | |
1998 | { | |
1999 | unsigned int got_offset = got_index * 8; | |
2000 | Reloc_section* rela_dyn = this->rela_dyn_section(NULL); | |
2001 | ||
2002 | this->got_->reserve_local(got_index, obj, r_sym, got_type); | |
2003 | switch (got_type) | |
2004 | { | |
2005 | case GOT_TYPE_STANDARD: | |
2006 | if (parameters->options().output_is_position_independent()) | |
2007 | rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE, | |
397b129b | 2008 | this->got_, got_offset, 0, false); |
6fa2a40b CC |
2009 | break; |
2010 | case GOT_TYPE_TLS_OFFSET: | |
2011 | rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64, | |
2012 | this->got_, got_offset, 0); | |
2013 | break; | |
2014 | case GOT_TYPE_TLS_PAIR: | |
2015 | this->got_->reserve_slot(got_index + 1); | |
2016 | rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64, | |
2017 | this->got_, got_offset, 0); | |
2018 | break; | |
2019 | case GOT_TYPE_TLS_DESC: | |
2020 | gold_fatal(_("TLS_DESC not yet supported for incremental linking")); | |
2021 | // this->got_->reserve_slot(got_index + 1); | |
2022 | // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg, | |
2023 | // this->got_, got_offset, 0); | |
2024 | break; | |
2025 | default: | |
2026 | gold_unreachable(); | |
2027 | } | |
2028 | } | |
2029 | ||
2030 | // Reserve a GOT entry for a global symbol, and regenerate any | |
2031 | // necessary dynamic relocations. | |
2032 | ||
fc51264f | 2033 | template<int size> |
6fa2a40b | 2034 | void |
fc51264f L |
2035 | Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index, |
2036 | Symbol* gsym, | |
2037 | unsigned int got_type) | |
6fa2a40b CC |
2038 | { |
2039 | unsigned int got_offset = got_index * 8; | |
2040 | Reloc_section* rela_dyn = this->rela_dyn_section(NULL); | |
2041 | ||
2042 | this->got_->reserve_global(got_index, gsym, got_type); | |
2043 | switch (got_type) | |
2044 | { | |
2045 | case GOT_TYPE_STANDARD: | |
2046 | if (!gsym->final_value_is_known()) | |
2047 | { | |
2048 | if (gsym->is_from_dynobj() | |
2049 | || gsym->is_undefined() | |
2050 | || gsym->is_preemptible() | |
2051 | || gsym->type() == elfcpp::STT_GNU_IFUNC) | |
2052 | rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, | |
2053 | this->got_, got_offset, 0); | |
2054 | else | |
2055 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE, | |
13cf9988 | 2056 | this->got_, got_offset, 0, false); |
6fa2a40b CC |
2057 | } |
2058 | break; | |
2059 | case GOT_TYPE_TLS_OFFSET: | |
2060 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64, | |
13cf9988 | 2061 | this->got_, got_offset, 0, false); |
6fa2a40b CC |
2062 | break; |
2063 | case GOT_TYPE_TLS_PAIR: | |
2064 | this->got_->reserve_slot(got_index + 1); | |
2065 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64, | |
13cf9988 | 2066 | this->got_, got_offset, 0, false); |
6fa2a40b | 2067 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64, |
13cf9988 | 2068 | this->got_, got_offset + 8, 0, false); |
6fa2a40b CC |
2069 | break; |
2070 | case GOT_TYPE_TLS_DESC: | |
2071 | this->got_->reserve_slot(got_index + 1); | |
2072 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC, | |
13cf9988 | 2073 | this->got_, got_offset, 0, false); |
6fa2a40b CC |
2074 | break; |
2075 | default: | |
2076 | gold_unreachable(); | |
2077 | } | |
2078 | } | |
2079 | ||
4829d394 CC |
2080 | // Register an existing PLT entry for a global symbol. |
2081 | ||
fc51264f | 2082 | template<int size> |
4829d394 | 2083 | void |
fc51264f L |
2084 | Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab, |
2085 | Layout* layout, | |
2086 | unsigned int plt_index, | |
2087 | Symbol* gsym) | |
4829d394 CC |
2088 | { |
2089 | gold_assert(this->plt_ != NULL); | |
2090 | gold_assert(!gsym->has_plt_offset()); | |
2091 | ||
2092 | this->plt_->reserve_slot(plt_index); | |
2093 | ||
2094 | gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size()); | |
2095 | ||
2096 | unsigned int got_offset = (plt_index + 3) * 8; | |
67181c72 | 2097 | this->plt_->add_relocation(symtab, layout, gsym, got_offset); |
4829d394 CC |
2098 | } |
2099 | ||
26d3c67d CC |
2100 | // Force a COPY relocation for a given symbol. |
2101 | ||
fc51264f | 2102 | template<int size> |
26d3c67d | 2103 | void |
fc51264f | 2104 | Target_x86_64<size>::emit_copy_reloc( |
26d3c67d CC |
2105 | Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset) |
2106 | { | |
2107 | this->copy_relocs_.emit_copy_reloc(symtab, | |
fc51264f | 2108 | symtab->get_sized_symbol<size>(sym), |
26d3c67d CC |
2109 | os, |
2110 | offset, | |
2111 | this->rela_dyn_section(NULL)); | |
2112 | } | |
2113 | ||
9fa33bee | 2114 | // Define the _TLS_MODULE_BASE_ symbol in the TLS segment. |
edfbb029 | 2115 | |
fc51264f | 2116 | template<int size> |
edfbb029 | 2117 | void |
fc51264f L |
2118 | Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab, |
2119 | Layout* layout) | |
edfbb029 CC |
2120 | { |
2121 | if (this->tls_base_symbol_defined_) | |
2122 | return; | |
2123 | ||
2124 | Output_segment* tls_segment = layout->tls_segment(); | |
2125 | if (tls_segment != NULL) | |
2126 | { | |
183fd0e3 | 2127 | bool is_exec = parameters->options().output_is_executable(); |
edfbb029 | 2128 | symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL, |
99fff23b | 2129 | Symbol_table::PREDEFINED, |
edfbb029 CC |
2130 | tls_segment, 0, 0, |
2131 | elfcpp::STT_TLS, | |
2132 | elfcpp::STB_LOCAL, | |
2133 | elfcpp::STV_HIDDEN, 0, | |
183fd0e3 AO |
2134 | (is_exec |
2135 | ? Symbol::SEGMENT_END | |
2136 | : Symbol::SEGMENT_START), | |
2137 | true); | |
edfbb029 CC |
2138 | } |
2139 | this->tls_base_symbol_defined_ = true; | |
2140 | } | |
2141 | ||
c2b45e22 CC |
2142 | // Create the reserved PLT and GOT entries for the TLS descriptor resolver. |
2143 | ||
fc51264f | 2144 | template<int size> |
c2b45e22 | 2145 | void |
fc51264f | 2146 | Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab, |
2e702c99 | 2147 | Layout* layout) |
c2b45e22 CC |
2148 | { |
2149 | if (this->plt_ == NULL) | |
2150 | this->make_plt_section(symtab, layout); | |
2151 | ||
2152 | if (!this->plt_->has_tlsdesc_entry()) | |
2153 | { | |
2154 | // Allocate the TLSDESC_GOT entry. | |
2155 | Output_data_got<64, false>* got = this->got_section(symtab, layout); | |
2156 | unsigned int got_offset = got->add_constant(0); | |
2157 | ||
2158 | // Allocate the TLSDESC_PLT entry. | |
2159 | this->plt_->reserve_tlsdesc_entry(got_offset); | |
2160 | } | |
2161 | } | |
2162 | ||
31d60480 ILT |
2163 | // Create a GOT entry for the TLS module index. |
2164 | ||
fc51264f | 2165 | template<int size> |
31d60480 | 2166 | unsigned int |
fc51264f L |
2167 | Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout, |
2168 | Sized_relobj_file<size, false>* object) | |
31d60480 ILT |
2169 | { |
2170 | if (this->got_mod_index_offset_ == -1U) | |
2171 | { | |
2172 | gold_assert(symtab != NULL && layout != NULL && object != NULL); | |
2173 | Reloc_section* rela_dyn = this->rela_dyn_section(layout); | |
2174 | Output_data_got<64, false>* got = this->got_section(symtab, layout); | |
2175 | unsigned int got_offset = got->add_constant(0); | |
2176 | rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got, | |
2e702c99 | 2177 | got_offset, 0); |
009a67a2 | 2178 | got->add_constant(0); |
31d60480 ILT |
2179 | this->got_mod_index_offset_ = got_offset; |
2180 | } | |
2181 | return this->got_mod_index_offset_; | |
2182 | } | |
2183 | ||
2e30d253 ILT |
2184 | // Optimize the TLS relocation type based on what we know about the |
2185 | // symbol. IS_FINAL is true if the final address of this symbol is | |
2186 | // known at link time. | |
2187 | ||
fc51264f | 2188 | template<int size> |
e041f13d | 2189 | tls::Tls_optimization |
fc51264f | 2190 | Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type) |
2e30d253 | 2191 | { |
2e30d253 ILT |
2192 | // If we are generating a shared library, then we can't do anything |
2193 | // in the linker. | |
8851ecca | 2194 | if (parameters->options().shared()) |
e041f13d | 2195 | return tls::TLSOPT_NONE; |
2e30d253 ILT |
2196 | |
2197 | switch (r_type) | |
2198 | { | |
2199 | case elfcpp::R_X86_64_TLSGD: | |
e041f13d ILT |
2200 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
2201 | case elfcpp::R_X86_64_TLSDESC_CALL: | |
2202 | // These are General-Dynamic which permits fully general TLS | |
2e30d253 ILT |
2203 | // access. Since we know that we are generating an executable, |
2204 | // we can convert this to Initial-Exec. If we also know that | |
2205 | // this is a local symbol, we can further switch to Local-Exec. | |
2206 | if (is_final) | |
e041f13d ILT |
2207 | return tls::TLSOPT_TO_LE; |
2208 | return tls::TLSOPT_TO_IE; | |
2e30d253 | 2209 | |
d61c17ea | 2210 | case elfcpp::R_X86_64_TLSLD: |
2e30d253 ILT |
2211 | // This is Local-Dynamic, which refers to a local symbol in the |
2212 | // dynamic TLS block. Since we know that we generating an | |
2213 | // executable, we can switch to Local-Exec. | |
e041f13d | 2214 | return tls::TLSOPT_TO_LE; |
2e30d253 | 2215 | |
0ffd9845 | 2216 | case elfcpp::R_X86_64_DTPOFF32: |
0ffd9845 ILT |
2217 | case elfcpp::R_X86_64_DTPOFF64: |
2218 | // Another Local-Dynamic reloc. | |
e041f13d | 2219 | return tls::TLSOPT_TO_LE; |
0ffd9845 | 2220 | |
d61c17ea | 2221 | case elfcpp::R_X86_64_GOTTPOFF: |
2e30d253 ILT |
2222 | // These are Initial-Exec relocs which get the thread offset |
2223 | // from the GOT. If we know that we are linking against the | |
2224 | // local symbol, we can switch to Local-Exec, which links the | |
2225 | // thread offset into the instruction. | |
2226 | if (is_final) | |
e041f13d ILT |
2227 | return tls::TLSOPT_TO_LE; |
2228 | return tls::TLSOPT_NONE; | |
2e30d253 | 2229 | |
d61c17ea | 2230 | case elfcpp::R_X86_64_TPOFF32: |
2e30d253 ILT |
2231 | // When we already have Local-Exec, there is nothing further we |
2232 | // can do. | |
e041f13d | 2233 | return tls::TLSOPT_NONE; |
2e30d253 ILT |
2234 | |
2235 | default: | |
2236 | gold_unreachable(); | |
2237 | } | |
2e30d253 ILT |
2238 | } |
2239 | ||
95a2c8d6 RS |
2240 | // Get the Reference_flags for a particular relocation. |
2241 | ||
fc51264f | 2242 | template<int size> |
95a2c8d6 | 2243 | int |
fc51264f | 2244 | Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type) |
95a2c8d6 RS |
2245 | { |
2246 | switch (r_type) | |
2247 | { | |
2248 | case elfcpp::R_X86_64_NONE: | |
2249 | case elfcpp::R_X86_64_GNU_VTINHERIT: | |
2250 | case elfcpp::R_X86_64_GNU_VTENTRY: | |
2251 | case elfcpp::R_X86_64_GOTPC32: | |
2252 | case elfcpp::R_X86_64_GOTPC64: | |
2253 | // No symbol reference. | |
2254 | return 0; | |
2255 | ||
2256 | case elfcpp::R_X86_64_64: | |
2257 | case elfcpp::R_X86_64_32: | |
2258 | case elfcpp::R_X86_64_32S: | |
2259 | case elfcpp::R_X86_64_16: | |
2260 | case elfcpp::R_X86_64_8: | |
2261 | return Symbol::ABSOLUTE_REF; | |
2262 | ||
2263 | case elfcpp::R_X86_64_PC64: | |
2264 | case elfcpp::R_X86_64_PC32: | |
f49fe902 | 2265 | case elfcpp::R_X86_64_PC32_BND: |
95a2c8d6 RS |
2266 | case elfcpp::R_X86_64_PC16: |
2267 | case elfcpp::R_X86_64_PC8: | |
2268 | case elfcpp::R_X86_64_GOTOFF64: | |
2269 | return Symbol::RELATIVE_REF; | |
2270 | ||
2271 | case elfcpp::R_X86_64_PLT32: | |
f49fe902 | 2272 | case elfcpp::R_X86_64_PLT32_BND: |
95a2c8d6 RS |
2273 | case elfcpp::R_X86_64_PLTOFF64: |
2274 | return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF; | |
2275 | ||
2276 | case elfcpp::R_X86_64_GOT64: | |
2277 | case elfcpp::R_X86_64_GOT32: | |
2278 | case elfcpp::R_X86_64_GOTPCREL64: | |
2279 | case elfcpp::R_X86_64_GOTPCREL: | |
2891b491 L |
2280 | case elfcpp::R_X86_64_GOTPCRELX: |
2281 | case elfcpp::R_X86_64_REX_GOTPCRELX: | |
95a2c8d6 RS |
2282 | case elfcpp::R_X86_64_GOTPLT64: |
2283 | // Absolute in GOT. | |
2284 | return Symbol::ABSOLUTE_REF; | |
2285 | ||
2286 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic | |
2287 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) | |
2288 | case elfcpp::R_X86_64_TLSDESC_CALL: | |
2289 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic | |
2290 | case elfcpp::R_X86_64_DTPOFF32: | |
2291 | case elfcpp::R_X86_64_DTPOFF64: | |
2292 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec | |
2293 | case elfcpp::R_X86_64_TPOFF32: // Local-exec | |
2294 | return Symbol::TLS_REF; | |
2295 | ||
2296 | case elfcpp::R_X86_64_COPY: | |
2297 | case elfcpp::R_X86_64_GLOB_DAT: | |
2298 | case elfcpp::R_X86_64_JUMP_SLOT: | |
2299 | case elfcpp::R_X86_64_RELATIVE: | |
2300 | case elfcpp::R_X86_64_IRELATIVE: | |
2301 | case elfcpp::R_X86_64_TPOFF64: | |
2302 | case elfcpp::R_X86_64_DTPMOD64: | |
2303 | case elfcpp::R_X86_64_TLSDESC: | |
2304 | case elfcpp::R_X86_64_SIZE32: | |
2305 | case elfcpp::R_X86_64_SIZE64: | |
2306 | default: | |
2307 | // Not expected. We will give an error later. | |
2308 | return 0; | |
2309 | } | |
2310 | } | |
2311 | ||
e041f13d ILT |
2312 | // Report an unsupported relocation against a local symbol. |
2313 | ||
fc51264f | 2314 | template<int size> |
e041f13d | 2315 | void |
fc51264f L |
2316 | Target_x86_64<size>::Scan::unsupported_reloc_local( |
2317 | Sized_relobj_file<size, false>* object, | |
6fa2a40b | 2318 | unsigned int r_type) |
e041f13d | 2319 | { |
75f2446e ILT |
2320 | gold_error(_("%s: unsupported reloc %u against local symbol"), |
2321 | object->name().c_str(), r_type); | |
e041f13d ILT |
2322 | } |
2323 | ||
a036edd8 ILT |
2324 | // We are about to emit a dynamic relocation of type R_TYPE. If the |
2325 | // dynamic linker does not support it, issue an error. The GNU linker | |
2326 | // only issues a non-PIC error for an allocated read-only section. | |
2327 | // Here we know the section is allocated, but we don't know that it is | |
2328 | // read-only. But we check for all the relocation types which the | |
2329 | // glibc dynamic linker supports, so it seems appropriate to issue an | |
a29b0dad ILT |
2330 | // error even if the section is not read-only. If GSYM is not NULL, |
2331 | // it is the symbol the relocation is against; if it is NULL, the | |
2332 | // relocation is against a local symbol. | |
a036edd8 | 2333 | |
fc51264f | 2334 | template<int size> |
a036edd8 | 2335 | void |
fc51264f L |
2336 | Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type, |
2337 | Symbol* gsym) | |
a036edd8 ILT |
2338 | { |
2339 | switch (r_type) | |
2340 | { | |
2fbb4320 ILT |
2341 | // These are the relocation types supported by glibc for x86_64 |
2342 | // which should always work. | |
a036edd8 | 2343 | case elfcpp::R_X86_64_RELATIVE: |
7223e9ca | 2344 | case elfcpp::R_X86_64_IRELATIVE: |
a036edd8 ILT |
2345 | case elfcpp::R_X86_64_GLOB_DAT: |
2346 | case elfcpp::R_X86_64_JUMP_SLOT: | |
2347 | case elfcpp::R_X86_64_DTPMOD64: | |
2348 | case elfcpp::R_X86_64_DTPOFF64: | |
2349 | case elfcpp::R_X86_64_TPOFF64: | |
2350 | case elfcpp::R_X86_64_64: | |
2fbb4320 ILT |
2351 | case elfcpp::R_X86_64_COPY: |
2352 | return; | |
2353 | ||
2354 | // glibc supports these reloc types, but they can overflow. | |
a036edd8 | 2355 | case elfcpp::R_X86_64_PC32: |
f49fe902 | 2356 | case elfcpp::R_X86_64_PC32_BND: |
a29b0dad ILT |
2357 | // A PC relative reference is OK against a local symbol or if |
2358 | // the symbol is defined locally. | |
2359 | if (gsym == NULL | |
2360 | || (!gsym->is_from_dynobj() | |
2361 | && !gsym->is_undefined() | |
2362 | && !gsym->is_preemptible())) | |
2363 | return; | |
2364 | /* Fall through. */ | |
2365 | case elfcpp::R_X86_64_32: | |
3660ff06 L |
2366 | // R_X86_64_32 is OK for x32. |
2367 | if (size == 32 && r_type == elfcpp::R_X86_64_32) | |
2368 | return; | |
2fbb4320 ILT |
2369 | if (this->issued_non_pic_error_) |
2370 | return; | |
2371 | gold_assert(parameters->options().output_is_position_independent()); | |
a29b0dad ILT |
2372 | if (gsym == NULL) |
2373 | object->error(_("requires dynamic R_X86_64_32 reloc which may " | |
2374 | "overflow at runtime; recompile with -fPIC")); | |
2375 | else | |
f49fe902 L |
2376 | { |
2377 | const char *r_name; | |
2378 | switch (r_type) | |
2379 | { | |
2380 | case elfcpp::R_X86_64_32: | |
2381 | r_name = "R_X86_64_32"; | |
2382 | break; | |
2383 | case elfcpp::R_X86_64_PC32: | |
2384 | r_name = "R_X86_64_PC32"; | |
2385 | break; | |
2386 | case elfcpp::R_X86_64_PC32_BND: | |
2387 | r_name = "R_X86_64_PC32_BND"; | |
2388 | break; | |
2389 | default: | |
2390 | gold_unreachable(); | |
2391 | break; | |
2392 | } | |
2393 | object->error(_("requires dynamic %s reloc against '%s' " | |
2394 | "which may overflow at runtime; recompile " | |
2395 | "with -fPIC"), | |
2396 | r_name, gsym->name()); | |
2397 | } | |
2fbb4320 | 2398 | this->issued_non_pic_error_ = true; |
a036edd8 ILT |
2399 | return; |
2400 | ||
2401 | default: | |
2402 | // This prevents us from issuing more than one error per reloc | |
2403 | // section. But we can still wind up issuing more than one | |
2404 | // error per object file. | |
2405 | if (this->issued_non_pic_error_) | |
2e702c99 | 2406 | return; |
33aea2fd | 2407 | gold_assert(parameters->options().output_is_position_independent()); |
a29b0dad | 2408 | object->error(_("requires unsupported dynamic reloc %u; " |
2e702c99 | 2409 | "recompile with -fPIC"), |
a29b0dad | 2410 | r_type); |
a036edd8 ILT |
2411 | this->issued_non_pic_error_ = true; |
2412 | return; | |
2413 | ||
2414 | case elfcpp::R_X86_64_NONE: | |
2415 | gold_unreachable(); | |
2416 | } | |
2417 | } | |
2418 | ||
7223e9ca ILT |
2419 | // Return whether we need to make a PLT entry for a relocation of the |
2420 | // given type against a STT_GNU_IFUNC symbol. | |
2421 | ||
fc51264f | 2422 | template<int size> |
7223e9ca | 2423 | bool |
fc51264f L |
2424 | Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc( |
2425 | Sized_relobj_file<size, false>* object, | |
6fa2a40b | 2426 | unsigned int r_type) |
7223e9ca | 2427 | { |
95a2c8d6 RS |
2428 | int flags = Scan::get_reference_flags(r_type); |
2429 | if (flags & Symbol::TLS_REF) | |
2430 | gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"), | |
2e702c99 | 2431 | object->name().c_str(), r_type); |
95a2c8d6 | 2432 | return flags != 0; |
7223e9ca ILT |
2433 | } |
2434 | ||
2e30d253 ILT |
2435 | // Scan a relocation for a local symbol. |
2436 | ||
fc51264f | 2437 | template<int size> |
2e30d253 | 2438 | inline void |
fc51264f L |
2439 | Target_x86_64<size>::Scan::local(Symbol_table* symtab, |
2440 | Layout* layout, | |
2441 | Target_x86_64<size>* target, | |
2442 | Sized_relobj_file<size, false>* object, | |
2443 | unsigned int data_shndx, | |
2444 | Output_section* output_section, | |
2445 | const elfcpp::Rela<size, false>& reloc, | |
2446 | unsigned int r_type, | |
bfdfa4cd AM |
2447 | const elfcpp::Sym<size, false>& lsym, |
2448 | bool is_discarded) | |
2e30d253 | 2449 | { |
bfdfa4cd AM |
2450 | if (is_discarded) |
2451 | return; | |
2452 | ||
7223e9ca | 2453 | // A local STT_GNU_IFUNC symbol may require a PLT entry. |
397b129b CC |
2454 | bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC; |
2455 | if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type)) | |
7223e9ca | 2456 | { |
fc51264f | 2457 | unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); |
7223e9ca ILT |
2458 | target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym); |
2459 | } | |
2460 | ||
2e30d253 ILT |
2461 | switch (r_type) |
2462 | { | |
2463 | case elfcpp::R_X86_64_NONE: | |
6e5710ce ILT |
2464 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
2465 | case elfcpp::R_X86_64_GNU_VTENTRY: | |
2e30d253 ILT |
2466 | break; |
2467 | ||
2468 | case elfcpp::R_X86_64_64: | |
d61c6bd4 | 2469 | // If building a shared library (or a position-independent |
dceae3c1 ILT |
2470 | // executable), we need to create a dynamic relocation for this |
2471 | // location. The relocation applied at link time will apply the | |
2472 | // link-time value, so we flag the location with an | |
2473 | // R_X86_64_RELATIVE relocation so the dynamic loader can | |
d61c6bd4 | 2474 | // relocate it easily. |
8851ecca | 2475 | if (parameters->options().output_is_position_independent()) |
2e702c99 RM |
2476 | { |
2477 | unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); | |
2478 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
7223e9ca | 2479 | rela_dyn->add_local_relative(object, r_sym, |
62fe925a | 2480 | (size == 32 |
fd885f3a L |
2481 | ? elfcpp::R_X86_64_RELATIVE64 |
2482 | : elfcpp::R_X86_64_RELATIVE), | |
7223e9ca ILT |
2483 | output_section, data_shndx, |
2484 | reloc.get_r_offset(), | |
397b129b | 2485 | reloc.get_r_addend(), is_ifunc); |
2e702c99 | 2486 | } |
d61c6bd4 ILT |
2487 | break; |
2488 | ||
2e30d253 ILT |
2489 | case elfcpp::R_X86_64_32: |
2490 | case elfcpp::R_X86_64_32S: | |
2491 | case elfcpp::R_X86_64_16: | |
2492 | case elfcpp::R_X86_64_8: | |
96f2030e | 2493 | // If building a shared library (or a position-independent |
dceae3c1 ILT |
2494 | // executable), we need to create a dynamic relocation for this |
2495 | // location. We can't use an R_X86_64_RELATIVE relocation | |
2496 | // because that is always a 64-bit relocation. | |
8851ecca | 2497 | if (parameters->options().output_is_position_independent()) |
2e702c99 | 2498 | { |
3660ff06 L |
2499 | // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32. |
2500 | if (size == 32 && r_type == elfcpp::R_X86_64_32) | |
2501 | { | |
2502 | unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); | |
2503 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
2504 | rela_dyn->add_local_relative(object, r_sym, | |
2505 | elfcpp::R_X86_64_RELATIVE, | |
2506 | output_section, data_shndx, | |
2507 | reloc.get_r_offset(), | |
2508 | reloc.get_r_addend(), is_ifunc); | |
2509 | break; | |
2510 | } | |
2511 | ||
2e702c99 | 2512 | this->check_non_pic(object, r_type, NULL); |
a036edd8 | 2513 | |
2e702c99 | 2514 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
fc51264f | 2515 | unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); |
2e702c99 | 2516 | if (lsym.get_st_type() != elfcpp::STT_SECTION) |
d491d34e ILT |
2517 | rela_dyn->add_local(object, r_sym, r_type, output_section, |
2518 | data_shndx, reloc.get_r_offset(), | |
2519 | reloc.get_r_addend()); | |
2e702c99 RM |
2520 | else |
2521 | { | |
2522 | gold_assert(lsym.get_st_value() == 0); | |
d491d34e ILT |
2523 | unsigned int shndx = lsym.get_st_shndx(); |
2524 | bool is_ordinary; | |
2525 | shndx = object->adjust_sym_shndx(r_sym, shndx, | |
2526 | &is_ordinary); | |
2527 | if (!is_ordinary) | |
2528 | object->error(_("section symbol %u has bad shndx %u"), | |
2529 | r_sym, shndx); | |
2530 | else | |
2531 | rela_dyn->add_local_section(object, shndx, | |
2532 | r_type, output_section, | |
2533 | data_shndx, reloc.get_r_offset(), | |
2534 | reloc.get_r_addend()); | |
2e702c99 RM |
2535 | } |
2536 | } | |
2e30d253 ILT |
2537 | break; |
2538 | ||
2539 | case elfcpp::R_X86_64_PC64: | |
2540 | case elfcpp::R_X86_64_PC32: | |
f49fe902 | 2541 | case elfcpp::R_X86_64_PC32_BND: |
2e30d253 ILT |
2542 | case elfcpp::R_X86_64_PC16: |
2543 | case elfcpp::R_X86_64_PC8: | |
2544 | break; | |
2545 | ||
f389a824 | 2546 | case elfcpp::R_X86_64_PLT32: |
f49fe902 | 2547 | case elfcpp::R_X86_64_PLT32_BND: |
f389a824 ILT |
2548 | // Since we know this is a local symbol, we can handle this as a |
2549 | // PC32 reloc. | |
2550 | break; | |
2551 | ||
fdc2f80f | 2552 | case elfcpp::R_X86_64_GOTPC32: |
e822f2b1 | 2553 | case elfcpp::R_X86_64_GOTOFF64: |
fdc2f80f ILT |
2554 | case elfcpp::R_X86_64_GOTPC64: |
2555 | case elfcpp::R_X86_64_PLTOFF64: | |
2e30d253 ILT |
2556 | // We need a GOT section. |
2557 | target->got_section(symtab, layout); | |
ee9e9e86 ILT |
2558 | // For PLTOFF64, we'd normally want a PLT section, but since we |
2559 | // know this is a local symbol, no PLT is needed. | |
2e30d253 ILT |
2560 | break; |
2561 | ||
0ffd9845 ILT |
2562 | case elfcpp::R_X86_64_GOT64: |
2563 | case elfcpp::R_X86_64_GOT32: | |
2564 | case elfcpp::R_X86_64_GOTPCREL64: | |
2565 | case elfcpp::R_X86_64_GOTPCREL: | |
2891b491 L |
2566 | case elfcpp::R_X86_64_GOTPCRELX: |
2567 | case elfcpp::R_X86_64_REX_GOTPCRELX: | |
ee9e9e86 | 2568 | case elfcpp::R_X86_64_GOTPLT64: |
0ffd9845 | 2569 | { |
1fa29f10 | 2570 | // The symbol requires a GOT section. |
2e702c99 | 2571 | Output_data_got<64, false>* got = target->got_section(symtab, layout); |
1fa29f10 IT |
2572 | |
2573 | // If the relocation symbol isn't IFUNC, | |
2574 | // and is local, then we will convert | |
2575 | // mov foo@GOTPCREL(%rip), %reg | |
2576 | // to lea foo(%rip), %reg. | |
2577 | // in Relocate::relocate. | |
2891b491 L |
2578 | if ((r_type == elfcpp::R_X86_64_GOTPCREL |
2579 | || r_type == elfcpp::R_X86_64_GOTPCRELX | |
2580 | || r_type == elfcpp::R_X86_64_REX_GOTPCRELX) | |
1fa29f10 IT |
2581 | && reloc.get_r_offset() >= 2 |
2582 | && !is_ifunc) | |
2583 | { | |
2584 | section_size_type stype; | |
2585 | const unsigned char* view = object->section_contents(data_shndx, | |
2586 | &stype, true); | |
2587 | if (view[reloc.get_r_offset() - 2] == 0x8b) | |
2588 | break; | |
2589 | } | |
2590 | ||
2591 | ||
2592 | // The symbol requires a GOT entry. | |
2e702c99 | 2593 | unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); |
7223e9ca ILT |
2594 | |
2595 | // For a STT_GNU_IFUNC symbol we want the PLT offset. That | |
2596 | // lets function pointers compare correctly with shared | |
2597 | // libraries. Otherwise we would need an IRELATIVE reloc. | |
2598 | bool is_new; | |
397b129b | 2599 | if (is_ifunc) |
7223e9ca ILT |
2600 | is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD); |
2601 | else | |
2602 | is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD); | |
2e702c99 RM |
2603 | if (is_new) |
2604 | { | |
2605 | // If we are generating a shared object, we need to add a | |
2606 | // dynamic relocation for this symbol's GOT entry. | |
2607 | if (parameters->options().output_is_position_independent()) | |
2608 | { | |
2609 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
7bf1f802 ILT |
2610 | // R_X86_64_RELATIVE assumes a 64-bit relocation. |
2611 | if (r_type != elfcpp::R_X86_64_GOT32) | |
7223e9ca ILT |
2612 | { |
2613 | unsigned int got_offset = | |
2614 | object->local_got_offset(r_sym, GOT_TYPE_STANDARD); | |
2615 | rela_dyn->add_local_relative(object, r_sym, | |
2616 | elfcpp::R_X86_64_RELATIVE, | |
397b129b | 2617 | got, got_offset, 0, is_ifunc); |
7223e9ca | 2618 | } |
2e702c99 RM |
2619 | else |
2620 | { | |
2621 | this->check_non_pic(object, r_type, NULL); | |
2622 | ||
2623 | gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION); | |
2624 | rela_dyn->add_local( | |
2625 | object, r_sym, r_type, got, | |
2626 | object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0); | |
2627 | } | |
2628 | } | |
2629 | } | |
2630 | // For GOTPLT64, we'd normally want a PLT section, but since | |
2631 | // we know this is a local symbol, no PLT is needed. | |
0ffd9845 ILT |
2632 | } |
2633 | break; | |
2634 | ||
2e30d253 ILT |
2635 | case elfcpp::R_X86_64_COPY: |
2636 | case elfcpp::R_X86_64_GLOB_DAT: | |
2637 | case elfcpp::R_X86_64_JUMP_SLOT: | |
2638 | case elfcpp::R_X86_64_RELATIVE: | |
7223e9ca | 2639 | case elfcpp::R_X86_64_IRELATIVE: |
d61c17ea | 2640 | // These are outstanding tls relocs, which are unexpected when linking |
2e30d253 | 2641 | case elfcpp::R_X86_64_TPOFF64: |
2e30d253 | 2642 | case elfcpp::R_X86_64_DTPMOD64: |
2e30d253 | 2643 | case elfcpp::R_X86_64_TLSDESC: |
75f2446e ILT |
2644 | gold_error(_("%s: unexpected reloc %u in object file"), |
2645 | object->name().c_str(), r_type); | |
2e30d253 ILT |
2646 | break; |
2647 | ||
d61c17ea | 2648 | // These are initial tls relocs, which are expected when linking |
56622147 ILT |
2649 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
2650 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) | |
e041f13d | 2651 | case elfcpp::R_X86_64_TLSDESC_CALL: |
56622147 | 2652 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
0ffd9845 ILT |
2653 | case elfcpp::R_X86_64_DTPOFF32: |
2654 | case elfcpp::R_X86_64_DTPOFF64: | |
56622147 ILT |
2655 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
2656 | case elfcpp::R_X86_64_TPOFF32: // Local-exec | |
2e30d253 | 2657 | { |
8851ecca | 2658 | bool output_is_shared = parameters->options().shared(); |
e041f13d | 2659 | const tls::Tls_optimization optimized_type |
2e702c99 | 2660 | = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared, |
fc51264f | 2661 | r_type); |
2e30d253 ILT |
2662 | switch (r_type) |
2663 | { | |
2e702c99 RM |
2664 | case elfcpp::R_X86_64_TLSGD: // General-dynamic |
2665 | if (optimized_type == tls::TLSOPT_NONE) | |
2666 | { | |
2667 | // Create a pair of GOT entries for the module index and | |
2668 | // dtv-relative offset. | |
2669 | Output_data_got<64, false>* got | |
2670 | = target->got_section(symtab, layout); | |
2671 | unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); | |
d491d34e ILT |
2672 | unsigned int shndx = lsym.get_st_shndx(); |
2673 | bool is_ordinary; | |
2674 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); | |
2675 | if (!is_ordinary) | |
2676 | object->error(_("local symbol %u has bad shndx %u"), | |
2677 | r_sym, shndx); | |
2e702c99 | 2678 | else |
83896202 ILT |
2679 | got->add_local_pair_with_rel(object, r_sym, |
2680 | shndx, | |
2681 | GOT_TYPE_TLS_PAIR, | |
2682 | target->rela_dyn_section(layout), | |
bd73a62d | 2683 | elfcpp::R_X86_64_DTPMOD64); |
2e702c99 RM |
2684 | } |
2685 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
7bf1f802 | 2686 | unsupported_reloc_local(object, r_type); |
2e702c99 | 2687 | break; |
7bf1f802 | 2688 | |
2e702c99 RM |
2689 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
2690 | target->define_tls_base_symbol(symtab, layout); | |
c2b45e22 CC |
2691 | if (optimized_type == tls::TLSOPT_NONE) |
2692 | { | |
2e702c99 RM |
2693 | // Create reserved PLT and GOT entries for the resolver. |
2694 | target->reserve_tlsdesc_entries(symtab, layout); | |
2695 | ||
2696 | // Generate a double GOT entry with an | |
2697 | // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc | |
2698 | // is resolved lazily, so the GOT entry needs to be in | |
2699 | // an area in .got.plt, not .got. Call got_section to | |
2700 | // make sure the section has been created. | |
a8df5856 | 2701 | target->got_section(symtab, layout); |
2e702c99 RM |
2702 | Output_data_got<64, false>* got = target->got_tlsdesc_section(); |
2703 | unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); | |
e291e7b9 ILT |
2704 | if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC)) |
2705 | { | |
2706 | unsigned int got_offset = got->add_constant(0); | |
2707 | got->add_constant(0); | |
2708 | object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC, | |
2709 | got_offset); | |
2710 | Reloc_section* rt = target->rela_tlsdesc_section(layout); | |
2711 | // We store the arguments we need in a vector, and | |
2712 | // use the index into the vector as the parameter | |
2713 | // to pass to the target specific routines. | |
2714 | uintptr_t intarg = target->add_tlsdesc_info(object, r_sym); | |
2715 | void* arg = reinterpret_cast<void*>(intarg); | |
2716 | rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg, | |
2717 | got, got_offset, 0); | |
2718 | } | |
c2b45e22 CC |
2719 | } |
2720 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
56622147 | 2721 | unsupported_reloc_local(object, r_type); |
2e30d253 ILT |
2722 | break; |
2723 | ||
2e702c99 | 2724 | case elfcpp::R_X86_64_TLSDESC_CALL: |
c2b45e22 CC |
2725 | break; |
2726 | ||
2e702c99 | 2727 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
7bf1f802 ILT |
2728 | if (optimized_type == tls::TLSOPT_NONE) |
2729 | { | |
2e702c99 RM |
2730 | // Create a GOT entry for the module index. |
2731 | target->got_mod_index_entry(symtab, layout, object); | |
7bf1f802 ILT |
2732 | } |
2733 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
2734 | unsupported_reloc_local(object, r_type); | |
2735 | break; | |
2736 | ||
2e702c99 RM |
2737 | case elfcpp::R_X86_64_DTPOFF32: |
2738 | case elfcpp::R_X86_64_DTPOFF64: | |
e041f13d ILT |
2739 | break; |
2740 | ||
2e702c99 | 2741 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
535890bb | 2742 | layout->set_has_static_tls(); |
2e702c99 RM |
2743 | if (optimized_type == tls::TLSOPT_NONE) |
2744 | { | |
2745 | // Create a GOT entry for the tp-relative offset. | |
2746 | Output_data_got<64, false>* got | |
2747 | = target->got_section(symtab, layout); | |
2748 | unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); | |
2749 | got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET, | |
83896202 ILT |
2750 | target->rela_dyn_section(layout), |
2751 | elfcpp::R_X86_64_TPOFF64); | |
2e702c99 RM |
2752 | } |
2753 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
2754 | unsupported_reloc_local(object, r_type); | |
2755 | break; | |
0ffd9845 | 2756 | |
2e702c99 | 2757 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
535890bb | 2758 | layout->set_has_static_tls(); |
2e702c99 RM |
2759 | if (output_is_shared) |
2760 | unsupported_reloc_local(object, r_type); | |
2e30d253 | 2761 | break; |
e041f13d | 2762 | |
2e702c99 RM |
2763 | default: |
2764 | gold_unreachable(); | |
2e30d253 ILT |
2765 | } |
2766 | } | |
2767 | break; | |
2e30d253 | 2768 | |
fdc2f80f ILT |
2769 | case elfcpp::R_X86_64_SIZE32: |
2770 | case elfcpp::R_X86_64_SIZE64: | |
2e30d253 | 2771 | default: |
75f2446e ILT |
2772 | gold_error(_("%s: unsupported reloc %u against local symbol"), |
2773 | object->name().c_str(), r_type); | |
2e30d253 ILT |
2774 | break; |
2775 | } | |
2776 | } | |
2777 | ||
2778 | ||
e041f13d ILT |
2779 | // Report an unsupported relocation against a global symbol. |
2780 | ||
fc51264f | 2781 | template<int size> |
e041f13d | 2782 | void |
fc51264f L |
2783 | Target_x86_64<size>::Scan::unsupported_reloc_global( |
2784 | Sized_relobj_file<size, false>* object, | |
6fa2a40b CC |
2785 | unsigned int r_type, |
2786 | Symbol* gsym) | |
e041f13d | 2787 | { |
75f2446e | 2788 | gold_error(_("%s: unsupported reloc %u against global symbol %s"), |
a2b1aa12 | 2789 | object->name().c_str(), r_type, gsym->demangled_name().c_str()); |
e041f13d ILT |
2790 | } |
2791 | ||
ce97fa81 | 2792 | // Returns true if this relocation type could be that of a function pointer. |
fc51264f | 2793 | template<int size> |
21bb3914 | 2794 | inline bool |
fc51264f | 2795 | Target_x86_64<size>::Scan::possible_function_pointer_reloc(unsigned int r_type) |
21bb3914 | 2796 | { |
21bb3914 ST |
2797 | switch (r_type) |
2798 | { | |
2799 | case elfcpp::R_X86_64_64: | |
2800 | case elfcpp::R_X86_64_32: | |
2801 | case elfcpp::R_X86_64_32S: | |
2802 | case elfcpp::R_X86_64_16: | |
2803 | case elfcpp::R_X86_64_8: | |
ce97fa81 ST |
2804 | case elfcpp::R_X86_64_GOT64: |
2805 | case elfcpp::R_X86_64_GOT32: | |
2806 | case elfcpp::R_X86_64_GOTPCREL64: | |
2807 | case elfcpp::R_X86_64_GOTPCREL: | |
2891b491 L |
2808 | case elfcpp::R_X86_64_GOTPCRELX: |
2809 | case elfcpp::R_X86_64_REX_GOTPCRELX: | |
ce97fa81 | 2810 | case elfcpp::R_X86_64_GOTPLT64: |
21bb3914 | 2811 | { |
2e702c99 | 2812 | return true; |
21bb3914 ST |
2813 | } |
2814 | } | |
2815 | return false; | |
2816 | } | |
2817 | ||
2818 | // For safe ICF, scan a relocation for a local symbol to check if it | |
2819 | // corresponds to a function pointer being taken. In that case mark | |
2820 | // the function whose pointer was taken as not foldable. | |
2821 | ||
fc51264f | 2822 | template<int size> |
21bb3914 | 2823 | inline bool |
fc51264f | 2824 | Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer( |
21bb3914 ST |
2825 | Symbol_table* , |
2826 | Layout* , | |
fc51264f L |
2827 | Target_x86_64<size>* , |
2828 | Sized_relobj_file<size, false>* , | |
21bb3914 ST |
2829 | unsigned int , |
2830 | Output_section* , | |
fc51264f | 2831 | const elfcpp::Rela<size, false>& , |
21bb3914 | 2832 | unsigned int r_type, |
fc51264f | 2833 | const elfcpp::Sym<size, false>&) |
21bb3914 ST |
2834 | { |
2835 | // When building a shared library, do not fold any local symbols as it is | |
2836 | // not possible to distinguish pointer taken versus a call by looking at | |
2837 | // the relocation types. | |
2838 | return (parameters->options().shared() | |
2e702c99 | 2839 | || possible_function_pointer_reloc(r_type)); |
21bb3914 ST |
2840 | } |
2841 | ||
2842 | // For safe ICF, scan a relocation for a global symbol to check if it | |
2843 | // corresponds to a function pointer being taken. In that case mark | |
2844 | // the function whose pointer was taken as not foldable. | |
2845 | ||
fc51264f | 2846 | template<int size> |
21bb3914 | 2847 | inline bool |
fc51264f | 2848 | Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer( |
21bb3914 ST |
2849 | Symbol_table*, |
2850 | Layout* , | |
fc51264f L |
2851 | Target_x86_64<size>* , |
2852 | Sized_relobj_file<size, false>* , | |
21bb3914 ST |
2853 | unsigned int , |
2854 | Output_section* , | |
fc51264f | 2855 | const elfcpp::Rela<size, false>& , |
21bb3914 ST |
2856 | unsigned int r_type, |
2857 | Symbol* gsym) | |
2858 | { | |
2859 | // When building a shared library, do not fold symbols whose visibility | |
2860 | // is hidden, internal or protected. | |
2861 | return ((parameters->options().shared() | |
2e702c99 | 2862 | && (gsym->visibility() == elfcpp::STV_INTERNAL |
21bb3914 ST |
2863 | || gsym->visibility() == elfcpp::STV_PROTECTED |
2864 | || gsym->visibility() == elfcpp::STV_HIDDEN)) | |
2e702c99 | 2865 | || possible_function_pointer_reloc(r_type)); |
21bb3914 ST |
2866 | } |
2867 | ||
2e30d253 ILT |
2868 | // Scan a relocation for a global symbol. |
2869 | ||
fc51264f | 2870 | template<int size> |
2e30d253 | 2871 | inline void |
fc51264f | 2872 | Target_x86_64<size>::Scan::global(Symbol_table* symtab, |
2e702c99 RM |
2873 | Layout* layout, |
2874 | Target_x86_64<size>* target, | |
2875 | Sized_relobj_file<size, false>* object, | |
2876 | unsigned int data_shndx, | |
2877 | Output_section* output_section, | |
2878 | const elfcpp::Rela<size, false>& reloc, | |
2879 | unsigned int r_type, | |
2880 | Symbol* gsym) | |
2e30d253 | 2881 | { |
7223e9ca ILT |
2882 | // A STT_GNU_IFUNC symbol may require a PLT entry. |
2883 | if (gsym->type() == elfcpp::STT_GNU_IFUNC | |
2884 | && this->reloc_needs_plt_for_ifunc(object, r_type)) | |
2885 | target->make_plt_entry(symtab, layout, gsym); | |
2886 | ||
2e30d253 ILT |
2887 | switch (r_type) |
2888 | { | |
2889 | case elfcpp::R_X86_64_NONE: | |
6e5710ce ILT |
2890 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
2891 | case elfcpp::R_X86_64_GNU_VTENTRY: | |
2e30d253 ILT |
2892 | break; |
2893 | ||
2894 | case elfcpp::R_X86_64_64: | |
2e30d253 ILT |
2895 | case elfcpp::R_X86_64_32: |
2896 | case elfcpp::R_X86_64_32S: | |
2e30d253 | 2897 | case elfcpp::R_X86_64_16: |
2e30d253 | 2898 | case elfcpp::R_X86_64_8: |
96f2030e | 2899 | { |
2e702c99 RM |
2900 | // Make a PLT entry if necessary. |
2901 | if (gsym->needs_plt_entry()) | |
2902 | { | |
2903 | target->make_plt_entry(symtab, layout, gsym); | |
2904 | // Since this is not a PC-relative relocation, we may be | |
2905 | // taking the address of a function. In that case we need to | |
2906 | // set the entry in the dynamic symbol table to the address of | |
2907 | // the PLT entry. | |
2908 | if (gsym->is_from_dynobj() && !parameters->options().shared()) | |
2909 | gsym->set_needs_dynsym_value(); | |
2910 | } | |
2911 | // Make a dynamic relocation if necessary. | |
2912 | if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) | |
2913 | { | |
a82bef93 ST |
2914 | if (!parameters->options().output_is_position_independent() |
2915 | && gsym->may_need_copy_reloc()) | |
2e702c99 RM |
2916 | { |
2917 | target->copy_reloc(symtab, layout, object, | |
2918 | data_shndx, output_section, gsym, reloc); | |
2919 | } | |
1bae613c L |
2920 | else if (((size == 64 && r_type == elfcpp::R_X86_64_64) |
2921 | || (size == 32 && r_type == elfcpp::R_X86_64_32)) | |
7223e9ca ILT |
2922 | && gsym->type() == elfcpp::STT_GNU_IFUNC |
2923 | && gsym->can_use_relative_reloc(false) | |
2924 | && !gsym->is_from_dynobj() | |
2925 | && !gsym->is_undefined() | |
2926 | && !gsym->is_preemptible()) | |
2927 | { | |
2928 | // Use an IRELATIVE reloc for a locally defined | |
2929 | // STT_GNU_IFUNC symbol. This makes a function | |
2930 | // address in a PIE executable match the address in a | |
2931 | // shared library that it links against. | |
67181c72 ILT |
2932 | Reloc_section* rela_dyn = |
2933 | target->rela_irelative_section(layout); | |
7223e9ca ILT |
2934 | unsigned int r_type = elfcpp::R_X86_64_IRELATIVE; |
2935 | rela_dyn->add_symbolless_global_addend(gsym, r_type, | |
2936 | output_section, object, | |
2937 | data_shndx, | |
2938 | reloc.get_r_offset(), | |
2939 | reloc.get_r_addend()); | |
2940 | } | |
b14016f0 L |
2941 | else if (((size == 64 && r_type == elfcpp::R_X86_64_64) |
2942 | || (size == 32 && r_type == elfcpp::R_X86_64_32)) | |
2e702c99 RM |
2943 | && gsym->can_use_relative_reloc(false)) |
2944 | { | |
2945 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
7223e9ca ILT |
2946 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE, |
2947 | output_section, object, | |
2948 | data_shndx, | |
2949 | reloc.get_r_offset(), | |
13cf9988 | 2950 | reloc.get_r_addend(), false); |
2e702c99 RM |
2951 | } |
2952 | else | |
2953 | { | |
2954 | this->check_non_pic(object, r_type, gsym); | |
2955 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
2956 | rela_dyn->add_global(gsym, r_type, output_section, object, | |
2957 | data_shndx, reloc.get_r_offset(), | |
2958 | reloc.get_r_addend()); | |
2959 | } | |
2960 | } | |
d61c6bd4 ILT |
2961 | } |
2962 | break; | |
2963 | ||
2964 | case elfcpp::R_X86_64_PC64: | |
2965 | case elfcpp::R_X86_64_PC32: | |
f49fe902 | 2966 | case elfcpp::R_X86_64_PC32_BND: |
d61c6bd4 ILT |
2967 | case elfcpp::R_X86_64_PC16: |
2968 | case elfcpp::R_X86_64_PC8: | |
2969 | { | |
2e702c99 RM |
2970 | // Make a PLT entry if necessary. |
2971 | if (gsym->needs_plt_entry()) | |
2972 | target->make_plt_entry(symtab, layout, gsym); | |
2973 | // Make a dynamic relocation if necessary. | |
2974 | if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) | |
2975 | { | |
a82bef93 ST |
2976 | if (parameters->options().output_is_executable() |
2977 | && gsym->may_need_copy_reloc()) | |
2e702c99 RM |
2978 | { |
2979 | target->copy_reloc(symtab, layout, object, | |
2980 | data_shndx, output_section, gsym, reloc); | |
2981 | } | |
2982 | else | |
2983 | { | |
2984 | this->check_non_pic(object, r_type, gsym); | |
2985 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
2986 | rela_dyn->add_global(gsym, r_type, output_section, object, | |
2987 | data_shndx, reloc.get_r_offset(), | |
2988 | reloc.get_r_addend()); | |
2989 | } | |
2990 | } | |
d61c6bd4 | 2991 | } |
2e30d253 ILT |
2992 | break; |
2993 | ||
ff006520 | 2994 | case elfcpp::R_X86_64_GOT64: |
2e30d253 | 2995 | case elfcpp::R_X86_64_GOT32: |
ff006520 ILT |
2996 | case elfcpp::R_X86_64_GOTPCREL64: |
2997 | case elfcpp::R_X86_64_GOTPCREL: | |
2891b491 L |
2998 | case elfcpp::R_X86_64_GOTPCRELX: |
2999 | case elfcpp::R_X86_64_REX_GOTPCRELX: | |
ff006520 | 3000 | case elfcpp::R_X86_64_GOTPLT64: |
2e30d253 | 3001 | { |
2e702c99 RM |
3002 | // The symbol requires a GOT entry. |
3003 | Output_data_got<64, false>* got = target->got_section(symtab, layout); | |
1fa29f10 IT |
3004 | |
3005 | // If we convert this from | |
3006 | // mov foo@GOTPCREL(%rip), %reg | |
3007 | // to lea foo(%rip), %reg. | |
3a4f096e ST |
3008 | // OR |
3009 | // if we convert | |
3010 | // (callq|jmpq) *foo@GOTPCRELX(%rip) to | |
3011 | // (callq|jmpq) foo | |
1fa29f10 | 3012 | // in Relocate::relocate, then there is nothing to do here. |
3a4f096e ST |
3013 | |
3014 | Lazy_view<size> view(object, data_shndx); | |
3015 | size_t r_offset = reloc.get_r_offset(); | |
3016 | if (r_offset >= 2 | |
3017 | && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type, | |
3018 | r_offset, &view)) | |
3019 | break; | |
3020 | ||
3021 | if (r_offset >= 2 | |
3022 | && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type, | |
3023 | r_offset, | |
3024 | &view)) | |
3025 | break; | |
1fa29f10 | 3026 | |
2e702c99 | 3027 | if (gsym->final_value_is_known()) |
7223e9ca ILT |
3028 | { |
3029 | // For a STT_GNU_IFUNC symbol we want the PLT address. | |
3030 | if (gsym->type() == elfcpp::STT_GNU_IFUNC) | |
3031 | got->add_global_plt(gsym, GOT_TYPE_STANDARD); | |
3032 | else | |
3033 | got->add_global(gsym, GOT_TYPE_STANDARD); | |
3034 | } | |
2e702c99 RM |
3035 | else |
3036 | { | |
3037 | // If this symbol is not fully resolved, we need to add a | |
3038 | // dynamic relocation for it. | |
3039 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
07aa62f2 ILT |
3040 | |
3041 | // Use a GLOB_DAT rather than a RELATIVE reloc if: | |
3042 | // | |
3043 | // 1) The symbol may be defined in some other module. | |
3044 | // | |
3045 | // 2) We are building a shared library and this is a | |
3046 | // protected symbol; using GLOB_DAT means that the dynamic | |
3047 | // linker can use the address of the PLT in the main | |
3048 | // executable when appropriate so that function address | |
3049 | // comparisons work. | |
3050 | // | |
3051 | // 3) This is a STT_GNU_IFUNC symbol in position dependent | |
3052 | // code, again so that function address comparisons work. | |
7223e9ca ILT |
3053 | if (gsym->is_from_dynobj() |
3054 | || gsym->is_undefined() | |
3055 | || gsym->is_preemptible() | |
07aa62f2 ILT |
3056 | || (gsym->visibility() == elfcpp::STV_PROTECTED |
3057 | && parameters->options().shared()) | |
7223e9ca ILT |
3058 | || (gsym->type() == elfcpp::STT_GNU_IFUNC |
3059 | && parameters->options().output_is_position_independent())) | |
2e702c99 | 3060 | got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn, |
83896202 | 3061 | elfcpp::R_X86_64_GLOB_DAT); |
2e702c99 RM |
3062 | else |
3063 | { | |
7223e9ca ILT |
3064 | // For a STT_GNU_IFUNC symbol we want to write the PLT |
3065 | // offset into the GOT, so that function pointer | |
3066 | // comparisons work correctly. | |
3067 | bool is_new; | |
3068 | if (gsym->type() != elfcpp::STT_GNU_IFUNC) | |
3069 | is_new = got->add_global(gsym, GOT_TYPE_STANDARD); | |
3070 | else | |
3071 | { | |
3072 | is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD); | |
3073 | // Tell the dynamic linker to use the PLT address | |
3074 | // when resolving relocations. | |
3075 | if (gsym->is_from_dynobj() | |
3076 | && !parameters->options().shared()) | |
3077 | gsym->set_needs_dynsym_value(); | |
3078 | } | |
2e702c99 | 3079 | if (is_new) |
7223e9ca ILT |
3080 | { |
3081 | unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD); | |
3082 | rela_dyn->add_global_relative(gsym, | |
3083 | elfcpp::R_X86_64_RELATIVE, | |
13cf9988 | 3084 | got, got_off, 0, false); |
7223e9ca | 3085 | } |
2e702c99 RM |
3086 | } |
3087 | } | |
2e30d253 ILT |
3088 | } |
3089 | break; | |
3090 | ||
3091 | case elfcpp::R_X86_64_PLT32: | |
f49fe902 | 3092 | case elfcpp::R_X86_64_PLT32_BND: |
2e30d253 ILT |
3093 | // If the symbol is fully resolved, this is just a PC32 reloc. |
3094 | // Otherwise we need a PLT entry. | |
3095 | if (gsym->final_value_is_known()) | |
3096 | break; | |
96f2030e ILT |
3097 | // If building a shared library, we can also skip the PLT entry |
3098 | // if the symbol is defined in the output file and is protected | |
3099 | // or hidden. | |
3100 | if (gsym->is_defined() | |
2e702c99 RM |
3101 | && !gsym->is_from_dynobj() |
3102 | && !gsym->is_preemptible()) | |
96f2030e | 3103 | break; |
2e30d253 ILT |
3104 | target->make_plt_entry(symtab, layout, gsym); |
3105 | break; | |
3106 | ||
fdc2f80f | 3107 | case elfcpp::R_X86_64_GOTPC32: |
e822f2b1 | 3108 | case elfcpp::R_X86_64_GOTOFF64: |
fdc2f80f ILT |
3109 | case elfcpp::R_X86_64_GOTPC64: |
3110 | case elfcpp::R_X86_64_PLTOFF64: | |
2e30d253 ILT |
3111 | // We need a GOT section. |
3112 | target->got_section(symtab, layout); | |
ee9e9e86 ILT |
3113 | // For PLTOFF64, we also need a PLT entry (but only if the |
3114 | // symbol is not fully resolved). | |
3115 | if (r_type == elfcpp::R_X86_64_PLTOFF64 | |
3116 | && !gsym->final_value_is_known()) | |
3117 | target->make_plt_entry(symtab, layout, gsym); | |
2e30d253 ILT |
3118 | break; |
3119 | ||
2e30d253 ILT |
3120 | case elfcpp::R_X86_64_COPY: |
3121 | case elfcpp::R_X86_64_GLOB_DAT: | |
3122 | case elfcpp::R_X86_64_JUMP_SLOT: | |
3123 | case elfcpp::R_X86_64_RELATIVE: | |
7223e9ca | 3124 | case elfcpp::R_X86_64_IRELATIVE: |
d61c17ea | 3125 | // These are outstanding tls relocs, which are unexpected when linking |
e822f2b1 | 3126 | case elfcpp::R_X86_64_TPOFF64: |
2e30d253 | 3127 | case elfcpp::R_X86_64_DTPMOD64: |
e822f2b1 | 3128 | case elfcpp::R_X86_64_TLSDESC: |
75f2446e ILT |
3129 | gold_error(_("%s: unexpected reloc %u in object file"), |
3130 | object->name().c_str(), r_type); | |
2e30d253 | 3131 | break; |
2e30d253 | 3132 | |
d61c17ea | 3133 | // These are initial tls relocs, which are expected for global() |
56622147 ILT |
3134 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
3135 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) | |
e041f13d | 3136 | case elfcpp::R_X86_64_TLSDESC_CALL: |
56622147 | 3137 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
0ffd9845 ILT |
3138 | case elfcpp::R_X86_64_DTPOFF32: |
3139 | case elfcpp::R_X86_64_DTPOFF64: | |
56622147 ILT |
3140 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
3141 | case elfcpp::R_X86_64_TPOFF32: // Local-exec | |
2e30d253 | 3142 | { |
65d92137 CC |
3143 | // For the Initial-Exec model, we can treat undef symbols as final |
3144 | // when building an executable. | |
3145 | const bool is_final = (gsym->final_value_is_known() || | |
3146 | (r_type == elfcpp::R_X86_64_GOTTPOFF && | |
3147 | gsym->is_undefined() && | |
3148 | parameters->options().output_is_executable())); | |
e041f13d | 3149 | const tls::Tls_optimization optimized_type |
2e702c99 | 3150 | = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type); |
2e30d253 ILT |
3151 | switch (r_type) |
3152 | { | |
2e702c99 | 3153 | case elfcpp::R_X86_64_TLSGD: // General-dynamic |
7bf1f802 ILT |
3154 | if (optimized_type == tls::TLSOPT_NONE) |
3155 | { | |
2e702c99 RM |
3156 | // Create a pair of GOT entries for the module index and |
3157 | // dtv-relative offset. | |
3158 | Output_data_got<64, false>* got | |
3159 | = target->got_section(symtab, layout); | |
3160 | got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR, | |
83896202 ILT |
3161 | target->rela_dyn_section(layout), |
3162 | elfcpp::R_X86_64_DTPMOD64, | |
3163 | elfcpp::R_X86_64_DTPOFF64); | |
7bf1f802 ILT |
3164 | } |
3165 | else if (optimized_type == tls::TLSOPT_TO_IE) | |
3166 | { | |
2e702c99 RM |
3167 | // Create a GOT entry for the tp-relative offset. |
3168 | Output_data_got<64, false>* got | |
3169 | = target->got_section(symtab, layout); | |
3170 | got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET, | |
83896202 ILT |
3171 | target->rela_dyn_section(layout), |
3172 | elfcpp::R_X86_64_TPOFF64); | |
7bf1f802 ILT |
3173 | } |
3174 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
3175 | unsupported_reloc_global(object, r_type, gsym); | |
3176 | break; | |
3177 | ||
2e702c99 RM |
3178 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
3179 | target->define_tls_base_symbol(symtab, layout); | |
c2b45e22 CC |
3180 | if (optimized_type == tls::TLSOPT_NONE) |
3181 | { | |
2e702c99 RM |
3182 | // Create reserved PLT and GOT entries for the resolver. |
3183 | target->reserve_tlsdesc_entries(symtab, layout); | |
3184 | ||
3185 | // Create a double GOT entry with an R_X86_64_TLSDESC | |
3186 | // reloc. The R_X86_64_TLSDESC reloc is resolved | |
3187 | // lazily, so the GOT entry needs to be in an area in | |
3188 | // .got.plt, not .got. Call got_section to make sure | |
3189 | // the section has been created. | |
a8df5856 | 3190 | target->got_section(symtab, layout); |
2e702c99 | 3191 | Output_data_got<64, false>* got = target->got_tlsdesc_section(); |
ca09d69a | 3192 | Reloc_section* rt = target->rela_tlsdesc_section(layout); |
2e702c99 | 3193 | got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt, |
83896202 | 3194 | elfcpp::R_X86_64_TLSDESC, 0); |
c2b45e22 CC |
3195 | } |
3196 | else if (optimized_type == tls::TLSOPT_TO_IE) | |
3197 | { | |
2e702c99 RM |
3198 | // Create a GOT entry for the tp-relative offset. |
3199 | Output_data_got<64, false>* got | |
3200 | = target->got_section(symtab, layout); | |
3201 | got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET, | |
83896202 ILT |
3202 | target->rela_dyn_section(layout), |
3203 | elfcpp::R_X86_64_TPOFF64); | |
c2b45e22 CC |
3204 | } |
3205 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
56622147 | 3206 | unsupported_reloc_global(object, r_type, gsym); |
2e30d253 ILT |
3207 | break; |
3208 | ||
2e702c99 | 3209 | case elfcpp::R_X86_64_TLSDESC_CALL: |
c2b45e22 CC |
3210 | break; |
3211 | ||
2e702c99 | 3212 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
7bf1f802 ILT |
3213 | if (optimized_type == tls::TLSOPT_NONE) |
3214 | { | |
2e702c99 RM |
3215 | // Create a GOT entry for the module index. |
3216 | target->got_mod_index_entry(symtab, layout, object); | |
7bf1f802 ILT |
3217 | } |
3218 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
3219 | unsupported_reloc_global(object, r_type, gsym); | |
3220 | break; | |
3221 | ||
2e702c99 RM |
3222 | case elfcpp::R_X86_64_DTPOFF32: |
3223 | case elfcpp::R_X86_64_DTPOFF64: | |
e041f13d ILT |
3224 | break; |
3225 | ||
2e702c99 | 3226 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
535890bb | 3227 | layout->set_has_static_tls(); |
2e702c99 RM |
3228 | if (optimized_type == tls::TLSOPT_NONE) |
3229 | { | |
3230 | // Create a GOT entry for the tp-relative offset. | |
3231 | Output_data_got<64, false>* got | |
3232 | = target->got_section(symtab, layout); | |
3233 | got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET, | |
83896202 ILT |
3234 | target->rela_dyn_section(layout), |
3235 | elfcpp::R_X86_64_TPOFF64); | |
2e702c99 RM |
3236 | } |
3237 | else if (optimized_type != tls::TLSOPT_TO_LE) | |
3238 | unsupported_reloc_global(object, r_type, gsym); | |
3239 | break; | |
0ffd9845 | 3240 | |
2e702c99 | 3241 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
535890bb | 3242 | layout->set_has_static_tls(); |
2e702c99 | 3243 | if (parameters->options().shared()) |
b1759dce | 3244 | unsupported_reloc_global(object, r_type, gsym); |
2e30d253 | 3245 | break; |
e041f13d | 3246 | |
2e702c99 RM |
3247 | default: |
3248 | gold_unreachable(); | |
2e30d253 ILT |
3249 | } |
3250 | } | |
3251 | break; | |
fdc2f80f ILT |
3252 | |
3253 | case elfcpp::R_X86_64_SIZE32: | |
3254 | case elfcpp::R_X86_64_SIZE64: | |
2e30d253 | 3255 | default: |
75f2446e | 3256 | gold_error(_("%s: unsupported reloc %u against global symbol %s"), |
a2b1aa12 | 3257 | object->name().c_str(), r_type, |
2e702c99 | 3258 | gsym->demangled_name().c_str()); |
2e30d253 ILT |
3259 | break; |
3260 | } | |
3261 | } | |
3262 | ||
fc51264f | 3263 | template<int size> |
6d03d481 | 3264 | void |
fc51264f L |
3265 | Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab, |
3266 | Layout* layout, | |
3267 | Sized_relobj_file<size, false>* object, | |
3268 | unsigned int data_shndx, | |
3269 | unsigned int sh_type, | |
3270 | const unsigned char* prelocs, | |
3271 | size_t reloc_count, | |
3272 | Output_section* output_section, | |
3273 | bool needs_special_offset_handling, | |
3274 | size_t local_symbol_count, | |
3275 | const unsigned char* plocal_symbols) | |
6d03d481 | 3276 | { |
4d625b70 CC |
3277 | typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> |
3278 | Classify_reloc; | |
6d03d481 ST |
3279 | |
3280 | if (sh_type == elfcpp::SHT_REL) | |
3281 | { | |
3282 | return; | |
3283 | } | |
3284 | ||
4d625b70 CC |
3285 | gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan, |
3286 | Classify_reloc>( | |
6d03d481 ST |
3287 | symtab, |
3288 | layout, | |
3289 | this, | |
3290 | object, | |
3291 | data_shndx, | |
3292 | prelocs, | |
3293 | reloc_count, | |
3294 | output_section, | |
3295 | needs_special_offset_handling, | |
3296 | local_symbol_count, | |
3297 | plocal_symbols); | |
2e702c99 | 3298 | |
6d03d481 | 3299 | } |
2e30d253 ILT |
3300 | // Scan relocations for a section. |
3301 | ||
fc51264f | 3302 | template<int size> |
2e30d253 | 3303 | void |
fc51264f L |
3304 | Target_x86_64<size>::scan_relocs(Symbol_table* symtab, |
3305 | Layout* layout, | |
3306 | Sized_relobj_file<size, false>* object, | |
3307 | unsigned int data_shndx, | |
3308 | unsigned int sh_type, | |
3309 | const unsigned char* prelocs, | |
3310 | size_t reloc_count, | |
3311 | Output_section* output_section, | |
3312 | bool needs_special_offset_handling, | |
3313 | size_t local_symbol_count, | |
3314 | const unsigned char* plocal_symbols) | |
2e30d253 | 3315 | { |
4d625b70 CC |
3316 | typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> |
3317 | Classify_reloc; | |
3318 | ||
2e30d253 ILT |
3319 | if (sh_type == elfcpp::SHT_REL) |
3320 | { | |
75f2446e ILT |
3321 | gold_error(_("%s: unsupported REL reloc section"), |
3322 | object->name().c_str()); | |
3323 | return; | |
2e30d253 ILT |
3324 | } |
3325 | ||
4d625b70 | 3326 | gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>( |
2e30d253 ILT |
3327 | symtab, |
3328 | layout, | |
3329 | this, | |
3330 | object, | |
3331 | data_shndx, | |
3332 | prelocs, | |
3333 | reloc_count, | |
730cdc88 ILT |
3334 | output_section, |
3335 | needs_special_offset_handling, | |
2e30d253 | 3336 | local_symbol_count, |
730cdc88 | 3337 | plocal_symbols); |
2e30d253 ILT |
3338 | } |
3339 | ||
3340 | // Finalize the sections. | |
3341 | ||
fc51264f | 3342 | template<int size> |
2e30d253 | 3343 | void |
fc51264f | 3344 | Target_x86_64<size>::do_finalize_sections( |
f59f41f3 DK |
3345 | Layout* layout, |
3346 | const Input_objects*, | |
e785ec03 | 3347 | Symbol_table* symtab) |
2e30d253 | 3348 | { |
ea715a34 ILT |
3349 | const Reloc_section* rel_plt = (this->plt_ == NULL |
3350 | ? NULL | |
e291e7b9 | 3351 | : this->plt_->rela_plt()); |
ea715a34 | 3352 | layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt, |
612a8d3d | 3353 | this->rela_dyn_, true, false); |
2e702c99 | 3354 | |
2e30d253 ILT |
3355 | // Fill in some more dynamic tags. |
3356 | Output_data_dynamic* const odyn = layout->dynamic_data(); | |
3357 | if (odyn != NULL) | |
3358 | { | |
22b127cc | 3359 | if (this->plt_ != NULL |
ea715a34 ILT |
3360 | && this->plt_->output_section() != NULL |
3361 | && this->plt_->has_tlsdesc_entry()) | |
2e30d253 | 3362 | { |
ea715a34 ILT |
3363 | unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset(); |
3364 | unsigned int got_offset = this->plt_->get_tlsdesc_got_offset(); | |
3365 | this->got_->finalize_data_size(); | |
3366 | odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT, | |
3367 | this->plt_, plt_offset); | |
3368 | odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT, | |
3369 | this->got_, got_offset); | |
2e30d253 ILT |
3370 | } |
3371 | } | |
3372 | ||
3373 | // Emit any relocs we saved in an attempt to avoid generating COPY | |
3374 | // relocs. | |
12c0daef ILT |
3375 | if (this->copy_relocs_.any_saved_relocs()) |
3376 | this->copy_relocs_.emit(this->rela_dyn_section(layout)); | |
e785ec03 ILT |
3377 | |
3378 | // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of | |
3379 | // the .got.plt section. | |
3380 | Symbol* sym = this->global_offset_table_; | |
3381 | if (sym != NULL) | |
3382 | { | |
3383 | uint64_t data_size = this->got_plt_->current_data_size(); | |
fc51264f | 3384 | symtab->get_sized_symbol<size>(sym)->set_symsize(data_size); |
e785ec03 | 3385 | } |
28a13fec | 3386 | |
67181c72 ILT |
3387 | if (parameters->doing_static_link() |
3388 | && (this->plt_ == NULL || !this->plt_->has_irelative_section())) | |
28a13fec ILT |
3389 | { |
3390 | // If linking statically, make sure that the __rela_iplt symbols | |
3391 | // were defined if necessary, even if we didn't create a PLT. | |
3392 | static const Define_symbol_in_segment syms[] = | |
3393 | { | |
3394 | { | |
3395 | "__rela_iplt_start", // name | |
3396 | elfcpp::PT_LOAD, // segment_type | |
3397 | elfcpp::PF_W, // segment_flags_set | |
3398 | elfcpp::PF(0), // segment_flags_clear | |
3399 | 0, // value | |
3400 | 0, // size | |
3401 | elfcpp::STT_NOTYPE, // type | |
3402 | elfcpp::STB_GLOBAL, // binding | |
3403 | elfcpp::STV_HIDDEN, // visibility | |
3404 | 0, // nonvis | |
3405 | Symbol::SEGMENT_START, // offset_from_base | |
3406 | true // only_if_ref | |
3407 | }, | |
3408 | { | |
3409 | "__rela_iplt_end", // name | |
3410 | elfcpp::PT_LOAD, // segment_type | |
3411 | elfcpp::PF_W, // segment_flags_set | |
3412 | elfcpp::PF(0), // segment_flags_clear | |
3413 | 0, // value | |
3414 | 0, // size | |
3415 | elfcpp::STT_NOTYPE, // type | |
3416 | elfcpp::STB_GLOBAL, // binding | |
3417 | elfcpp::STV_HIDDEN, // visibility | |
3418 | 0, // nonvis | |
3419 | Symbol::SEGMENT_START, // offset_from_base | |
3420 | true // only_if_ref | |
3421 | } | |
3422 | }; | |
3423 | ||
3424 | symtab->define_symbols(layout, 2, syms, | |
3425 | layout->script_options()->saw_sections_clause()); | |
3426 | } | |
2e30d253 ILT |
3427 | } |
3428 | ||
19ef3f4d CC |
3429 | // For x32, we need to handle PC-relative relocations using full 64-bit |
3430 | // arithmetic, so that we can detect relocation overflows properly. | |
3431 | // This class overrides the pcrela32_check methods from the defaults in | |
3432 | // Relocate_functions in reloc.h. | |
3433 | ||
3434 | template<int size> | |
3435 | class X86_64_relocate_functions : public Relocate_functions<size, false> | |
3436 | { | |
3437 | public: | |
3438 | typedef Relocate_functions<size, false> Base; | |
3439 | ||
3440 | // Do a simple PC relative relocation with the addend in the | |
3441 | // relocation. | |
3442 | static inline typename Base::Reloc_status | |
3443 | pcrela32_check(unsigned char* view, | |
3444 | typename elfcpp::Elf_types<64>::Elf_Addr value, | |
3445 | typename elfcpp::Elf_types<64>::Elf_Swxword addend, | |
3446 | typename elfcpp::Elf_types<64>::Elf_Addr address) | |
3447 | { | |
3448 | typedef typename elfcpp::Swap<32, false>::Valtype Valtype; | |
3449 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
3450 | value = value + addend - address; | |
3451 | elfcpp::Swap<32, false>::writeval(wv, value); | |
3452 | return (Bits<32>::has_overflow(value) | |
3453 | ? Base::RELOC_OVERFLOW : Base::RELOC_OK); | |
3454 | } | |
3455 | ||
3456 | // Do a simple PC relative relocation with a Symbol_value with the | |
3457 | // addend in the relocation. | |
3458 | static inline typename Base::Reloc_status | |
3459 | pcrela32_check(unsigned char* view, | |
3460 | const Sized_relobj_file<size, false>* object, | |
3461 | const Symbol_value<size>* psymval, | |
3462 | typename elfcpp::Elf_types<64>::Elf_Swxword addend, | |
3463 | typename elfcpp::Elf_types<64>::Elf_Addr address) | |
3464 | { | |
3465 | typedef typename elfcpp::Swap<32, false>::Valtype Valtype; | |
3466 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
7c8b700c CC |
3467 | typename elfcpp::Elf_types<64>::Elf_Addr value; |
3468 | if (addend >= 0) | |
3469 | value = psymval->value(object, addend); | |
3470 | else | |
3471 | { | |
3472 | // For negative addends, get the symbol value without | |
3473 | // the addend, then add the addend using 64-bit arithmetic. | |
3474 | value = psymval->value(object, 0); | |
3475 | value += addend; | |
3476 | } | |
3477 | value -= address; | |
19ef3f4d CC |
3478 | elfcpp::Swap<32, false>::writeval(wv, value); |
3479 | return (Bits<32>::has_overflow(value) | |
3480 | ? Base::RELOC_OVERFLOW : Base::RELOC_OK); | |
3481 | } | |
3482 | }; | |
3483 | ||
2e30d253 ILT |
3484 | // Perform a relocation. |
3485 | ||
fc51264f | 3486 | template<int size> |
2e30d253 | 3487 | inline bool |
fc51264f L |
3488 | Target_x86_64<size>::Relocate::relocate( |
3489 | const Relocate_info<size, false>* relinfo, | |
91a65d2f | 3490 | unsigned int, |
fc51264f L |
3491 | Target_x86_64<size>* target, |
3492 | Output_section*, | |
3493 | size_t relnum, | |
91a65d2f | 3494 | const unsigned char* preloc, |
fc51264f L |
3495 | const Sized_symbol<size>* gsym, |
3496 | const Symbol_value<size>* psymval, | |
3497 | unsigned char* view, | |
3498 | typename elfcpp::Elf_types<size>::Elf_Addr address, | |
3499 | section_size_type view_size) | |
2e30d253 | 3500 | { |
19ef3f4d | 3501 | typedef X86_64_relocate_functions<size> Reloc_funcs; |
91a65d2f AM |
3502 | const elfcpp::Rela<size, false> rela(preloc); |
3503 | unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info()); | |
3504 | ||
2e30d253 ILT |
3505 | if (this->skip_call_tls_get_addr_) |
3506 | { | |
5efc7cd2 | 3507 | if ((r_type != elfcpp::R_X86_64_PLT32 |
ad961eab | 3508 | && r_type != elfcpp::R_X86_64_GOTPCRELX |
f49fe902 L |
3509 | && r_type != elfcpp::R_X86_64_PLT32_BND |
3510 | && r_type != elfcpp::R_X86_64_PC32_BND | |
2e702c99 | 3511 | && r_type != elfcpp::R_X86_64_PC32) |
2e30d253 | 3512 | || gsym == NULL |
0ffd9845 | 3513 | || strcmp(gsym->name(), "__tls_get_addr") != 0) |
2e30d253 | 3514 | { |
75f2446e ILT |
3515 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
3516 | _("missing expected TLS relocation")); | |
3517 | } | |
3518 | else | |
3519 | { | |
3520 | this->skip_call_tls_get_addr_ = false; | |
3521 | return false; | |
2e30d253 | 3522 | } |
2e30d253 ILT |
3523 | } |
3524 | ||
0e804863 ILT |
3525 | if (view == NULL) |
3526 | return true; | |
3527 | ||
fc51264f | 3528 | const Sized_relobj_file<size, false>* object = relinfo->object; |
7223e9ca ILT |
3529 | |
3530 | // Pick the value to use for symbols defined in the PLT. | |
fc51264f | 3531 | Symbol_value<size> symval; |
96f2030e | 3532 | if (gsym != NULL |
95a2c8d6 | 3533 | && gsym->use_plt_offset(Scan::get_reference_flags(r_type))) |
2e30d253 | 3534 | { |
19fec8c1 | 3535 | symval.set_output_value(target->plt_address_for_global(gsym)); |
2e30d253 ILT |
3536 | psymval = &symval; |
3537 | } | |
7223e9ca ILT |
3538 | else if (gsym == NULL && psymval->is_ifunc_symbol()) |
3539 | { | |
fc51264f | 3540 | unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); |
7223e9ca ILT |
3541 | if (object->local_has_plt_offset(r_sym)) |
3542 | { | |
19fec8c1 | 3543 | symval.set_output_value(target->plt_address_for_local(object, r_sym)); |
7223e9ca ILT |
3544 | psymval = &symval; |
3545 | } | |
3546 | } | |
2e30d253 | 3547 | |
0ffd9845 ILT |
3548 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
3549 | ||
3550 | // Get the GOT offset if needed. | |
96f2030e ILT |
3551 | // The GOT pointer points to the end of the GOT section. |
3552 | // We need to subtract the size of the GOT section to get | |
3553 | // the actual offset to use in the relocation. | |
0ffd9845 | 3554 | bool have_got_offset = false; |
c23dd342 L |
3555 | // Since the actual offset is always negative, we use signed int to |
3556 | // support 64-bit GOT relocations. | |
3557 | int got_offset = 0; | |
0ffd9845 ILT |
3558 | switch (r_type) |
3559 | { | |
3560 | case elfcpp::R_X86_64_GOT32: | |
3561 | case elfcpp::R_X86_64_GOT64: | |
3562 | case elfcpp::R_X86_64_GOTPLT64: | |
0ffd9845 ILT |
3563 | case elfcpp::R_X86_64_GOTPCREL64: |
3564 | if (gsym != NULL) | |
2e702c99 RM |
3565 | { |
3566 | gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); | |
3567 | got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size(); | |
3568 | } | |
0ffd9845 | 3569 | else |
2e702c99 RM |
3570 | { |
3571 | unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); | |
3572 | gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)); | |
3573 | got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD) | |
3574 | - target->got_size()); | |
3575 | } | |
0ffd9845 ILT |
3576 | have_got_offset = true; |
3577 | break; | |
3578 | ||
3579 | default: | |
3580 | break; | |
3581 | } | |
2e30d253 | 3582 | |
c34c98ed CC |
3583 | typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK; |
3584 | ||
2e30d253 ILT |
3585 | switch (r_type) |
3586 | { | |
3587 | case elfcpp::R_X86_64_NONE: | |
6e5710ce ILT |
3588 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
3589 | case elfcpp::R_X86_64_GNU_VTENTRY: | |
2e30d253 ILT |
3590 | break; |
3591 | ||
3592 | case elfcpp::R_X86_64_64: | |
c34c98ed | 3593 | Reloc_funcs::rela64(view, object, psymval, addend); |
2e30d253 ILT |
3594 | break; |
3595 | ||
3596 | case elfcpp::R_X86_64_PC64: | |
c34c98ed | 3597 | Reloc_funcs::pcrela64(view, object, psymval, addend, |
2e702c99 | 3598 | address); |
2e30d253 ILT |
3599 | break; |
3600 | ||
3601 | case elfcpp::R_X86_64_32: | |
c34c98ed CC |
3602 | rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend, |
3603 | Reloc_funcs::CHECK_UNSIGNED); | |
2e30d253 ILT |
3604 | break; |
3605 | ||
3606 | case elfcpp::R_X86_64_32S: | |
c34c98ed CC |
3607 | rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend, |
3608 | Reloc_funcs::CHECK_SIGNED); | |
2e30d253 ILT |
3609 | break; |
3610 | ||
3611 | case elfcpp::R_X86_64_PC32: | |
f49fe902 | 3612 | case elfcpp::R_X86_64_PC32_BND: |
c34c98ed | 3613 | rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend, |
19ef3f4d | 3614 | address); |
2e30d253 ILT |
3615 | break; |
3616 | ||
3617 | case elfcpp::R_X86_64_16: | |
c34c98ed | 3618 | Reloc_funcs::rela16(view, object, psymval, addend); |
2e30d253 ILT |
3619 | break; |
3620 | ||
3621 | case elfcpp::R_X86_64_PC16: | |
c34c98ed | 3622 | Reloc_funcs::pcrela16(view, object, psymval, addend, address); |
2e30d253 ILT |
3623 | break; |
3624 | ||
3625 | case elfcpp::R_X86_64_8: | |
c34c98ed | 3626 | Reloc_funcs::rela8(view, object, psymval, addend); |
2e30d253 ILT |
3627 | break; |
3628 | ||
3629 | case elfcpp::R_X86_64_PC8: | |
c34c98ed | 3630 | Reloc_funcs::pcrela8(view, object, psymval, addend, address); |
2e30d253 ILT |
3631 | break; |
3632 | ||
3633 | case elfcpp::R_X86_64_PLT32: | |
f49fe902 | 3634 | case elfcpp::R_X86_64_PLT32_BND: |
f389a824 | 3635 | gold_assert(gsym == NULL |
2e702c99 | 3636 | || gsym->has_plt_offset() |
99f8faca ILT |
3637 | || gsym->final_value_is_known() |
3638 | || (gsym->is_defined() | |
3639 | && !gsym->is_from_dynobj() | |
3640 | && !gsym->is_preemptible())); | |
ee9e9e86 ILT |
3641 | // Note: while this code looks the same as for R_X86_64_PC32, it |
3642 | // behaves differently because psymval was set to point to | |
3643 | // the PLT entry, rather than the symbol, in Scan::global(). | |
c34c98ed | 3644 | rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend, |
19ef3f4d | 3645 | address); |
2e30d253 ILT |
3646 | break; |
3647 | ||
ee9e9e86 ILT |
3648 | case elfcpp::R_X86_64_PLTOFF64: |
3649 | { | |
2e702c99 RM |
3650 | gold_assert(gsym); |
3651 | gold_assert(gsym->has_plt_offset() | |
3652 | || gsym->final_value_is_known()); | |
fc51264f | 3653 | typename elfcpp::Elf_types<size>::Elf_Addr got_address; |
c23dd342 L |
3654 | // This is the address of GLOBAL_OFFSET_TABLE. |
3655 | got_address = target->got_plt_section()->address(); | |
c34c98ed | 3656 | Reloc_funcs::rela64(view, object, psymval, addend - got_address); |
ee9e9e86 | 3657 | } |
7849f6d8 | 3658 | break; |
ee9e9e86 | 3659 | |
2e30d253 | 3660 | case elfcpp::R_X86_64_GOT32: |
0ffd9845 | 3661 | gold_assert(have_got_offset); |
c34c98ed | 3662 | Reloc_funcs::rela32(view, got_offset, addend); |
2e30d253 ILT |
3663 | break; |
3664 | ||
e822f2b1 ILT |
3665 | case elfcpp::R_X86_64_GOTPC32: |
3666 | { | |
2e702c99 | 3667 | gold_assert(gsym); |
fc51264f | 3668 | typename elfcpp::Elf_types<size>::Elf_Addr value; |
96f2030e | 3669 | value = target->got_plt_section()->address(); |
19ef3f4d | 3670 | Reloc_funcs::pcrela32_check(view, value, addend, address); |
e822f2b1 ILT |
3671 | } |
3672 | break; | |
3673 | ||
3674 | case elfcpp::R_X86_64_GOT64: | |
fdc2f80f | 3675 | case elfcpp::R_X86_64_GOTPLT64: |
e88ba8d5 L |
3676 | // R_X86_64_GOTPLT64 is obsolete and treated the the same as |
3677 | // GOT64. | |
0ffd9845 | 3678 | gold_assert(have_got_offset); |
c34c98ed | 3679 | Reloc_funcs::rela64(view, got_offset, addend); |
e822f2b1 ILT |
3680 | break; |
3681 | ||
3682 | case elfcpp::R_X86_64_GOTPC64: | |
3683 | { | |
2e702c99 | 3684 | gold_assert(gsym); |
fc51264f | 3685 | typename elfcpp::Elf_types<size>::Elf_Addr value; |
96f2030e | 3686 | value = target->got_plt_section()->address(); |
c34c98ed | 3687 | Reloc_funcs::pcrela64(view, value, addend, address); |
e822f2b1 ILT |
3688 | } |
3689 | break; | |
3690 | ||
2e30d253 ILT |
3691 | case elfcpp::R_X86_64_GOTOFF64: |
3692 | { | |
fc51264f | 3693 | typename elfcpp::Elf_types<size>::Elf_Addr value; |
2e30d253 | 3694 | value = (psymval->value(object, 0) |
96f2030e | 3695 | - target->got_plt_section()->address()); |
c34c98ed | 3696 | Reloc_funcs::rela64(view, value, addend); |
2e30d253 ILT |
3697 | } |
3698 | break; | |
3699 | ||
3700 | case elfcpp::R_X86_64_GOTPCREL: | |
2891b491 L |
3701 | case elfcpp::R_X86_64_GOTPCRELX: |
3702 | case elfcpp::R_X86_64_REX_GOTPCRELX: | |
2e30d253 | 3703 | { |
1fa29f10 IT |
3704 | // Convert |
3705 | // mov foo@GOTPCREL(%rip), %reg | |
3706 | // to lea foo(%rip), %reg. | |
3707 | // if possible. | |
3a4f096e ST |
3708 | if ((gsym == NULL |
3709 | && rela.get_r_offset() >= 2 | |
3710 | && view[-2] == 0x8b | |
3711 | && !psymval->is_ifunc_symbol()) | |
3712 | || (gsym != NULL | |
3713 | && rela.get_r_offset() >= 2 | |
3714 | && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type, | |
3715 | 0, &view))) | |
1fa29f10 IT |
3716 | { |
3717 | view[-2] = 0x8d; | |
19ef3f4d | 3718 | Reloc_funcs::pcrela32(view, object, psymval, addend, address); |
1fa29f10 | 3719 | } |
3a4f096e ST |
3720 | // Convert |
3721 | // callq *foo@GOTPCRELX(%rip) to | |
3722 | // addr32 callq foo | |
3723 | // and jmpq *foo@GOTPCRELX(%rip) to | |
3724 | // jmpq foo | |
3725 | // nop | |
3726 | else if (gsym != NULL | |
3727 | && rela.get_r_offset() >= 2 | |
3728 | && Target_x86_64<size>::can_convert_callq_to_direct(gsym, | |
3729 | r_type, | |
3730 | 0, &view)) | |
3731 | { | |
3732 | if (view[-1] == 0x15) | |
3733 | { | |
3734 | // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq. | |
3735 | // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8. | |
3736 | view[-2] = 0x67; | |
3737 | view[-1] = 0xe8; | |
3738 | // Convert GOTPCRELX to 32-bit pc relative reloc. | |
3739 | Reloc_funcs::pcrela32(view, object, psymval, addend, address); | |
3740 | } | |
3741 | else | |
3742 | { | |
3743 | // Convert jmpq *foo@GOTPCRELX(%rip) to | |
3744 | // jmpq foo | |
3745 | // nop | |
3746 | // The opcode of direct jmpq is 0xe9. | |
3747 | view[-2] = 0xe9; | |
3748 | // The opcode of nop is 0x90. | |
3749 | view[3] = 0x90; | |
3750 | // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip | |
3751 | // relative and since the instruction following the jmpq is now | |
3752 | // the nop, offset the address by 1 byte. The start of the | |
3753 | // relocation also moves ahead by 1 byte. | |
3754 | Reloc_funcs::pcrela32(&view[-1], object, psymval, addend, | |
3755 | address - 1); | |
3756 | } | |
3757 | } | |
1fa29f10 IT |
3758 | else |
3759 | { | |
3760 | if (gsym != NULL) | |
3761 | { | |
3762 | gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); | |
3763 | got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size(); | |
3764 | } | |
3765 | else | |
3766 | { | |
3767 | unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); | |
3768 | gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)); | |
3769 | got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD) | |
3770 | - target->got_size()); | |
3771 | } | |
3772 | typename elfcpp::Elf_types<size>::Elf_Addr value; | |
3773 | value = target->got_plt_section()->address() + got_offset; | |
19ef3f4d | 3774 | Reloc_funcs::pcrela32_check(view, value, addend, address); |
1fa29f10 | 3775 | } |
2e30d253 ILT |
3776 | } |
3777 | break; | |
3778 | ||
e822f2b1 ILT |
3779 | case elfcpp::R_X86_64_GOTPCREL64: |
3780 | { | |
2e702c99 RM |
3781 | gold_assert(have_got_offset); |
3782 | typename elfcpp::Elf_types<size>::Elf_Addr value; | |
3783 | value = target->got_plt_section()->address() + got_offset; | |
c34c98ed | 3784 | Reloc_funcs::pcrela64(view, value, addend, address); |
e822f2b1 ILT |
3785 | } |
3786 | break; | |
3787 | ||
2e30d253 ILT |
3788 | case elfcpp::R_X86_64_COPY: |
3789 | case elfcpp::R_X86_64_GLOB_DAT: | |
3790 | case elfcpp::R_X86_64_JUMP_SLOT: | |
3791 | case elfcpp::R_X86_64_RELATIVE: | |
7223e9ca | 3792 | case elfcpp::R_X86_64_IRELATIVE: |
d61c17ea | 3793 | // These are outstanding tls relocs, which are unexpected when linking |
2e30d253 | 3794 | case elfcpp::R_X86_64_TPOFF64: |
2e30d253 | 3795 | case elfcpp::R_X86_64_DTPMOD64: |
2e30d253 | 3796 | case elfcpp::R_X86_64_TLSDESC: |
75f2446e ILT |
3797 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
3798 | _("unexpected reloc %u in object file"), | |
3799 | r_type); | |
2e30d253 ILT |
3800 | break; |
3801 | ||
d61c17ea | 3802 | // These are initial tls relocs, which are expected when linking |
56622147 ILT |
3803 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
3804 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) | |
e041f13d | 3805 | case elfcpp::R_X86_64_TLSDESC_CALL: |
56622147 | 3806 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
0ffd9845 ILT |
3807 | case elfcpp::R_X86_64_DTPOFF32: |
3808 | case elfcpp::R_X86_64_DTPOFF64: | |
56622147 ILT |
3809 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
3810 | case elfcpp::R_X86_64_TPOFF32: // Local-exec | |
7bf1f802 | 3811 | this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval, |
2e702c99 | 3812 | view, address, view_size); |
2e30d253 | 3813 | break; |
2e30d253 | 3814 | |
fdc2f80f ILT |
3815 | case elfcpp::R_X86_64_SIZE32: |
3816 | case elfcpp::R_X86_64_SIZE64: | |
2e30d253 | 3817 | default: |
75f2446e ILT |
3818 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
3819 | _("unsupported reloc %u"), | |
3820 | r_type); | |
2e30d253 ILT |
3821 | break; |
3822 | } | |
3823 | ||
c34c98ed | 3824 | if (rstatus == Reloc_funcs::RELOC_OVERFLOW) |
17ecd016 CC |
3825 | { |
3826 | if (gsym == NULL) | |
3827 | { | |
3828 | unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); | |
3829 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), | |
3830 | _("relocation overflow: " | |
3831 | "reference to local symbol %u in %s"), | |
3832 | r_sym, object->name().c_str()); | |
3833 | } | |
3834 | else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT) | |
3835 | { | |
3836 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), | |
3837 | _("relocation overflow: " | |
3838 | "reference to '%s' defined in %s"), | |
3839 | gsym->name(), | |
3840 | gsym->object()->name().c_str()); | |
3841 | } | |
3842 | else | |
3843 | { | |
3844 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), | |
3845 | _("relocation overflow: reference to '%s'"), | |
3846 | gsym->name()); | |
3847 | } | |
3848 | } | |
c34c98ed | 3849 | |
2e30d253 ILT |
3850 | return true; |
3851 | } | |
3852 | ||
3853 | // Perform a TLS relocation. | |
3854 | ||
fc51264f | 3855 | template<int size> |
2e30d253 | 3856 | inline void |
fc51264f L |
3857 | Target_x86_64<size>::Relocate::relocate_tls( |
3858 | const Relocate_info<size, false>* relinfo, | |
3859 | Target_x86_64<size>* target, | |
3860 | size_t relnum, | |
3861 | const elfcpp::Rela<size, false>& rela, | |
3862 | unsigned int r_type, | |
3863 | const Sized_symbol<size>* gsym, | |
3864 | const Symbol_value<size>* psymval, | |
3865 | unsigned char* view, | |
3866 | typename elfcpp::Elf_types<size>::Elf_Addr address, | |
3867 | section_size_type view_size) | |
2e30d253 | 3868 | { |
2e30d253 | 3869 | Output_segment* tls_segment = relinfo->layout->tls_segment(); |
7bf1f802 | 3870 | |
fc51264f | 3871 | const Sized_relobj_file<size, false>* object = relinfo->object; |
6a41d30b | 3872 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
fc51264f | 3873 | elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr); |
36171d64 | 3874 | bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0; |
2e30d253 | 3875 | |
fc51264f | 3876 | typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0); |
2e30d253 ILT |
3877 | |
3878 | const bool is_final = (gsym == NULL | |
b3705d2a | 3879 | ? !parameters->options().shared() |
2e30d253 | 3880 | : gsym->final_value_is_known()); |
36171d64 | 3881 | tls::Tls_optimization optimized_type |
fc51264f | 3882 | = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type); |
2e30d253 ILT |
3883 | switch (r_type) |
3884 | { | |
56622147 | 3885 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
36171d64 CC |
3886 | if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) |
3887 | { | |
3888 | // If this code sequence is used in a non-executable section, | |
3889 | // we will not optimize the R_X86_64_DTPOFF32/64 relocation, | |
3890 | // on the assumption that it's being used by itself in a debug | |
3891 | // section. Therefore, in the unlikely event that the code | |
3892 | // sequence appears in a non-executable section, we simply | |
3893 | // leave it unoptimized. | |
3894 | optimized_type = tls::TLSOPT_NONE; | |
3895 | } | |
e041f13d | 3896 | if (optimized_type == tls::TLSOPT_TO_LE) |
2e30d253 | 3897 | { |
62855347 ILT |
3898 | if (tls_segment == NULL) |
3899 | { | |
191f1a2d ILT |
3900 | gold_assert(parameters->errors()->error_count() > 0 |
3901 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
3902 | return; |
3903 | } | |
2e30d253 | 3904 | this->tls_gd_to_le(relinfo, relnum, tls_segment, |
72ec2876 | 3905 | rela, r_type, value, view, |
2e30d253 ILT |
3906 | view_size); |
3907 | break; | |
3908 | } | |
7bf1f802 | 3909 | else |
2e702c99 RM |
3910 | { |
3911 | unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE | |
3912 | ? GOT_TYPE_TLS_OFFSET | |
3913 | : GOT_TYPE_TLS_PAIR); | |
3914 | unsigned int got_offset; | |
3915 | if (gsym != NULL) | |
3916 | { | |
3917 | gold_assert(gsym->has_got_offset(got_type)); | |
3918 | got_offset = gsym->got_offset(got_type) - target->got_size(); | |
3919 | } | |
3920 | else | |
3921 | { | |
3922 | unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); | |
3923 | gold_assert(object->local_has_got_offset(r_sym, got_type)); | |
3924 | got_offset = (object->local_got_offset(r_sym, got_type) | |
3925 | - target->got_size()); | |
3926 | } | |
3927 | if (optimized_type == tls::TLSOPT_TO_IE) | |
3928 | { | |
3929 | value = target->got_plt_section()->address() + got_offset; | |
d21f123b | 3930 | this->tls_gd_to_ie(relinfo, relnum, rela, r_type, |
2e702c99 RM |
3931 | value, view, address, view_size); |
3932 | break; | |
3933 | } | |
3934 | else if (optimized_type == tls::TLSOPT_NONE) | |
3935 | { | |
3936 | // Relocate the field with the offset of the pair of GOT | |
3937 | // entries. | |
6a41d30b | 3938 | value = target->got_plt_section()->address() + got_offset; |
2e702c99 | 3939 | Relocate_functions<size, false>::pcrela32(view, value, addend, |
fc51264f | 3940 | address); |
2e702c99 RM |
3941 | break; |
3942 | } | |
3943 | } | |
72ec2876 | 3944 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
75f2446e | 3945 | _("unsupported reloc %u"), r_type); |
2e30d253 ILT |
3946 | break; |
3947 | ||
c2b45e22 CC |
3948 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
3949 | case elfcpp::R_X86_64_TLSDESC_CALL: | |
36171d64 CC |
3950 | if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) |
3951 | { | |
3952 | // See above comment for R_X86_64_TLSGD. | |
3953 | optimized_type = tls::TLSOPT_NONE; | |
3954 | } | |
c2b45e22 CC |
3955 | if (optimized_type == tls::TLSOPT_TO_LE) |
3956 | { | |
62855347 ILT |
3957 | if (tls_segment == NULL) |
3958 | { | |
191f1a2d ILT |
3959 | gold_assert(parameters->errors()->error_count() > 0 |
3960 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
3961 | return; |
3962 | } | |
c2b45e22 | 3963 | this->tls_desc_gd_to_le(relinfo, relnum, tls_segment, |
2e702c99 RM |
3964 | rela, r_type, value, view, |
3965 | view_size); | |
c2b45e22 CC |
3966 | break; |
3967 | } | |
3968 | else | |
2e702c99 RM |
3969 | { |
3970 | unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE | |
3971 | ? GOT_TYPE_TLS_OFFSET | |
3972 | : GOT_TYPE_TLS_DESC); | |
3973 | unsigned int got_offset = 0; | |
a8df5856 ILT |
3974 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC |
3975 | && optimized_type == tls::TLSOPT_NONE) | |
3976 | { | |
3977 | // We created GOT entries in the .got.tlsdesc portion of | |
3978 | // the .got.plt section, but the offset stored in the | |
3979 | // symbol is the offset within .got.tlsdesc. | |
3980 | got_offset = (target->got_size() | |
3981 | + target->got_plt_section()->data_size()); | |
3982 | } | |
2e702c99 RM |
3983 | if (gsym != NULL) |
3984 | { | |
3985 | gold_assert(gsym->has_got_offset(got_type)); | |
3986 | got_offset += gsym->got_offset(got_type) - target->got_size(); | |
3987 | } | |
3988 | else | |
3989 | { | |
3990 | unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); | |
3991 | gold_assert(object->local_has_got_offset(r_sym, got_type)); | |
3992 | got_offset += (object->local_got_offset(r_sym, got_type) | |
a8df5856 | 3993 | - target->got_size()); |
2e702c99 RM |
3994 | } |
3995 | if (optimized_type == tls::TLSOPT_TO_IE) | |
3996 | { | |
2e702c99 | 3997 | value = target->got_plt_section()->address() + got_offset; |
d21f123b | 3998 | this->tls_desc_gd_to_ie(relinfo, relnum, |
2e702c99 RM |
3999 | rela, r_type, value, view, address, |
4000 | view_size); | |
4001 | break; | |
4002 | } | |
4003 | else if (optimized_type == tls::TLSOPT_NONE) | |
4004 | { | |
4005 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) | |
4006 | { | |
4007 | // Relocate the field with the offset of the pair of GOT | |
4008 | // entries. | |
4009 | value = target->got_plt_section()->address() + got_offset; | |
4010 | Relocate_functions<size, false>::pcrela32(view, value, addend, | |
fc51264f | 4011 | address); |
2e702c99 RM |
4012 | } |
4013 | break; | |
4014 | } | |
4015 | } | |
c2b45e22 CC |
4016 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
4017 | _("unsupported reloc %u"), r_type); | |
4018 | break; | |
4019 | ||
56622147 | 4020 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
36171d64 CC |
4021 | if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) |
4022 | { | |
4023 | // See above comment for R_X86_64_TLSGD. | |
4024 | optimized_type = tls::TLSOPT_NONE; | |
4025 | } | |
e041f13d | 4026 | if (optimized_type == tls::TLSOPT_TO_LE) |
2e702c99 | 4027 | { |
62855347 ILT |
4028 | if (tls_segment == NULL) |
4029 | { | |
191f1a2d ILT |
4030 | gold_assert(parameters->errors()->error_count() > 0 |
4031 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
4032 | return; |
4033 | } | |
72ec2876 ILT |
4034 | this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type, |
4035 | value, view, view_size); | |
4036 | break; | |
2e702c99 | 4037 | } |
7bf1f802 | 4038 | else if (optimized_type == tls::TLSOPT_NONE) |
2e702c99 RM |
4039 | { |
4040 | // Relocate the field with the offset of the GOT entry for | |
4041 | // the module index. | |
4042 | unsigned int got_offset; | |
4043 | got_offset = (target->got_mod_index_entry(NULL, NULL, NULL) | |
31d60480 | 4044 | - target->got_size()); |
6a41d30b | 4045 | value = target->got_plt_section()->address() + got_offset; |
2e702c99 | 4046 | Relocate_functions<size, false>::pcrela32(view, value, addend, |
fc51264f | 4047 | address); |
2e702c99 RM |
4048 | break; |
4049 | } | |
72ec2876 | 4050 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
75f2446e | 4051 | _("unsupported reloc %u"), r_type); |
2e30d253 | 4052 | break; |
0ffd9845 ILT |
4053 | |
4054 | case elfcpp::R_X86_64_DTPOFF32: | |
36171d64 CC |
4055 | // This relocation type is used in debugging information. |
4056 | // In that case we need to not optimize the value. If the | |
4057 | // section is not executable, then we assume we should not | |
4058 | // optimize this reloc. See comments above for R_X86_64_TLSGD, | |
4059 | // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and | |
4060 | // R_X86_64_TLSLD. | |
4061 | if (optimized_type == tls::TLSOPT_TO_LE && is_executable) | |
4062 | { | |
62855347 ILT |
4063 | if (tls_segment == NULL) |
4064 | { | |
191f1a2d ILT |
4065 | gold_assert(parameters->errors()->error_count() > 0 |
4066 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
4067 | return; |
4068 | } | |
36171d64 CC |
4069 | value -= tls_segment->memsz(); |
4070 | } | |
fc51264f | 4071 | Relocate_functions<size, false>::rela32(view, value, addend); |
0ffd9845 ILT |
4072 | break; |
4073 | ||
4074 | case elfcpp::R_X86_64_DTPOFF64: | |
36171d64 CC |
4075 | // See R_X86_64_DTPOFF32, just above, for why we check for is_executable. |
4076 | if (optimized_type == tls::TLSOPT_TO_LE && is_executable) | |
4077 | { | |
62855347 ILT |
4078 | if (tls_segment == NULL) |
4079 | { | |
191f1a2d ILT |
4080 | gold_assert(parameters->errors()->error_count() > 0 |
4081 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
4082 | return; |
4083 | } | |
36171d64 CC |
4084 | value -= tls_segment->memsz(); |
4085 | } | |
fc51264f | 4086 | Relocate_functions<size, false>::rela64(view, value, addend); |
0ffd9845 | 4087 | break; |
2e30d253 | 4088 | |
56622147 | 4089 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
24dd5808 CC |
4090 | if (gsym != NULL |
4091 | && gsym->is_undefined() | |
4092 | && parameters->options().output_is_executable()) | |
65d92137 CC |
4093 | { |
4094 | Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum, | |
4095 | NULL, rela, | |
4096 | r_type, value, view, | |
4097 | view_size); | |
4098 | break; | |
4099 | } | |
4100 | else if (optimized_type == tls::TLSOPT_TO_LE) | |
56622147 | 4101 | { |
62855347 ILT |
4102 | if (tls_segment == NULL) |
4103 | { | |
191f1a2d ILT |
4104 | gold_assert(parameters->errors()->error_count() > 0 |
4105 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
4106 | return; |
4107 | } | |
fc51264f L |
4108 | Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum, |
4109 | tls_segment, rela, | |
4110 | r_type, value, view, | |
4111 | view_size); | |
56622147 ILT |
4112 | break; |
4113 | } | |
7bf1f802 | 4114 | else if (optimized_type == tls::TLSOPT_NONE) |
2e702c99 RM |
4115 | { |
4116 | // Relocate the field with the offset of the GOT entry for | |
4117 | // the tp-relative offset of the symbol. | |
4118 | unsigned int got_offset; | |
4119 | if (gsym != NULL) | |
4120 | { | |
4121 | gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET)); | |
4122 | got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET) | |
4123 | - target->got_size()); | |
4124 | } | |
4125 | else | |
4126 | { | |
4127 | unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); | |
4128 | gold_assert(object->local_has_got_offset(r_sym, | |
4129 | GOT_TYPE_TLS_OFFSET)); | |
4130 | got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET) | |
4131 | - target->got_size()); | |
4132 | } | |
6a41d30b | 4133 | value = target->got_plt_section()->address() + got_offset; |
2e702c99 | 4134 | Relocate_functions<size, false>::pcrela32(view, value, addend, |
fc51264f | 4135 | address); |
2e702c99 RM |
4136 | break; |
4137 | } | |
56622147 ILT |
4138 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
4139 | _("unsupported reloc type %u"), | |
4140 | r_type); | |
4141 | break; | |
0ffd9845 | 4142 | |
56622147 | 4143 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
62855347 ILT |
4144 | if (tls_segment == NULL) |
4145 | { | |
191f1a2d ILT |
4146 | gold_assert(parameters->errors()->error_count() > 0 |
4147 | || issue_undefined_symbol_error(gsym)); | |
62855347 ILT |
4148 | return; |
4149 | } | |
6a41d30b | 4150 | value -= tls_segment->memsz(); |
fc51264f | 4151 | Relocate_functions<size, false>::rela32(view, value, addend); |
56622147 | 4152 | break; |
2e30d253 | 4153 | } |
2e30d253 ILT |
4154 | } |
4155 | ||
7bf1f802 ILT |
4156 | // Do a relocation in which we convert a TLS General-Dynamic to an |
4157 | // Initial-Exec. | |
4158 | ||
fc51264f | 4159 | template<int size> |
7bf1f802 | 4160 | inline void |
fc51264f L |
4161 | Target_x86_64<size>::Relocate::tls_gd_to_ie( |
4162 | const Relocate_info<size, false>* relinfo, | |
4163 | size_t relnum, | |
fc51264f L |
4164 | const elfcpp::Rela<size, false>& rela, |
4165 | unsigned int, | |
4166 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
4167 | unsigned char* view, | |
4168 | typename elfcpp::Elf_types<size>::Elf_Addr address, | |
4169 | section_size_type view_size) | |
7bf1f802 | 4170 | { |
41194d9f L |
4171 | // For SIZE == 64: |
4172 | // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; | |
ad961eab L |
4173 | // .word 0x6666; rex64; call __tls_get_addr@PLT |
4174 | // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax | |
4175 | // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; | |
4176 | // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip) | |
41194d9f L |
4177 | // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax |
4178 | // For SIZE == 32: | |
4179 | // leaq foo@tlsgd(%rip),%rdi; | |
ad961eab L |
4180 | // .word 0x6666; rex64; call __tls_get_addr@PLT |
4181 | // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax | |
4182 | // leaq foo@tlsgd(%rip),%rdi; | |
4183 | // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip) | |
41194d9f | 4184 | // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax |
7bf1f802 | 4185 | |
7bf1f802 | 4186 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12); |
7bf1f802 | 4187 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
ad961eab L |
4188 | (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0 |
4189 | || memcmp(view + 4, "\x66\x48\xff", 3) == 0)); | |
7bf1f802 | 4190 | |
41194d9f L |
4191 | if (size == 64) |
4192 | { | |
4193 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, | |
4194 | -4); | |
4195 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
4196 | (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0)); | |
4197 | memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", | |
4198 | 16); | |
4199 | } | |
4200 | else | |
4201 | { | |
4202 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, | |
4203 | -3); | |
4204 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
4205 | (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0)); | |
4206 | memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", | |
4207 | 15); | |
4208 | } | |
7bf1f802 | 4209 | |
c2b45e22 | 4210 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
fc51264f L |
4211 | Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8, |
4212 | address); | |
7bf1f802 ILT |
4213 | |
4214 | // The next reloc should be a PLT32 reloc against __tls_get_addr. | |
4215 | // We can skip it. | |
4216 | this->skip_call_tls_get_addr_ = true; | |
4217 | } | |
4218 | ||
e041f13d | 4219 | // Do a relocation in which we convert a TLS General-Dynamic to a |
2e30d253 ILT |
4220 | // Local-Exec. |
4221 | ||
fc51264f | 4222 | template<int size> |
2e30d253 | 4223 | inline void |
fc51264f L |
4224 | Target_x86_64<size>::Relocate::tls_gd_to_le( |
4225 | const Relocate_info<size, false>* relinfo, | |
4226 | size_t relnum, | |
4227 | Output_segment* tls_segment, | |
4228 | const elfcpp::Rela<size, false>& rela, | |
4229 | unsigned int, | |
4230 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
4231 | unsigned char* view, | |
4232 | section_size_type view_size) | |
2e30d253 | 4233 | { |
41194d9f L |
4234 | // For SIZE == 64: |
4235 | // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; | |
ad961eab L |
4236 | // .word 0x6666; rex64; call __tls_get_addr@PLT |
4237 | // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax | |
4238 | // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; | |
4239 | // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip) | |
41194d9f L |
4240 | // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax |
4241 | // For SIZE == 32: | |
4242 | // leaq foo@tlsgd(%rip),%rdi; | |
ad961eab L |
4243 | // .word 0x6666; rex64; call __tls_get_addr@PLT |
4244 | // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax | |
4245 | // leaq foo@tlsgd(%rip),%rdi; | |
4246 | // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip) | |
41194d9f | 4247 | // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax |
2e30d253 | 4248 | |
72ec2876 | 4249 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12); |
72ec2876 | 4250 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
ad961eab L |
4251 | (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0 |
4252 | || memcmp(view + 4, "\x66\x48\xff", 3) == 0)); | |
41194d9f L |
4253 | |
4254 | if (size == 64) | |
4255 | { | |
4256 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, | |
4257 | -4); | |
4258 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
4259 | (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0)); | |
4260 | memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", | |
4261 | 16); | |
4262 | } | |
4263 | else | |
4264 | { | |
4265 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, | |
4266 | -3); | |
4267 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
4268 | (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0)); | |
2e30d253 | 4269 | |
41194d9f L |
4270 | memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", |
4271 | 15); | |
4272 | } | |
2e30d253 | 4273 | |
6a41d30b | 4274 | value -= tls_segment->memsz(); |
fc51264f | 4275 | Relocate_functions<size, false>::rela32(view + 8, value, 0); |
2e30d253 ILT |
4276 | |
4277 | // The next reloc should be a PLT32 reloc against __tls_get_addr. | |
4278 | // We can skip it. | |
4279 | this->skip_call_tls_get_addr_ = true; | |
2e30d253 ILT |
4280 | } |
4281 | ||
c2b45e22 CC |
4282 | // Do a TLSDESC-style General-Dynamic to Initial-Exec transition. |
4283 | ||
fc51264f | 4284 | template<int size> |
c2b45e22 | 4285 | inline void |
fc51264f L |
4286 | Target_x86_64<size>::Relocate::tls_desc_gd_to_ie( |
4287 | const Relocate_info<size, false>* relinfo, | |
c2b45e22 | 4288 | size_t relnum, |
fc51264f | 4289 | const elfcpp::Rela<size, false>& rela, |
c2b45e22 | 4290 | unsigned int r_type, |
fc51264f | 4291 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
c2b45e22 | 4292 | unsigned char* view, |
fc51264f | 4293 | typename elfcpp::Elf_types<size>::Elf_Addr address, |
c2b45e22 CC |
4294 | section_size_type view_size) |
4295 | { | |
4296 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) | |
4297 | { | |
4298 | // leaq foo@tlsdesc(%rip), %rax | |
4299 | // ==> movq foo@gottpoff(%rip), %rax | |
4300 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); | |
4301 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); | |
4302 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
2e702c99 | 4303 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05); |
c2b45e22 CC |
4304 | view[-2] = 0x8b; |
4305 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); | |
fc51264f | 4306 | Relocate_functions<size, false>::pcrela32(view, value, addend, address); |
c2b45e22 CC |
4307 | } |
4308 | else | |
4309 | { | |
4310 | // call *foo@tlscall(%rax) | |
4311 | // ==> nop; nop | |
4312 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL); | |
4313 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2); | |
4314 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
2e702c99 | 4315 | view[0] == 0xff && view[1] == 0x10); |
c2b45e22 CC |
4316 | view[0] = 0x66; |
4317 | view[1] = 0x90; | |
4318 | } | |
4319 | } | |
4320 | ||
4321 | // Do a TLSDESC-style General-Dynamic to Local-Exec transition. | |
4322 | ||
fc51264f | 4323 | template<int size> |
c2b45e22 | 4324 | inline void |
fc51264f L |
4325 | Target_x86_64<size>::Relocate::tls_desc_gd_to_le( |
4326 | const Relocate_info<size, false>* relinfo, | |
c2b45e22 CC |
4327 | size_t relnum, |
4328 | Output_segment* tls_segment, | |
fc51264f | 4329 | const elfcpp::Rela<size, false>& rela, |
c2b45e22 | 4330 | unsigned int r_type, |
fc51264f | 4331 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
c2b45e22 CC |
4332 | unsigned char* view, |
4333 | section_size_type view_size) | |
4334 | { | |
4335 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) | |
4336 | { | |
4337 | // leaq foo@tlsdesc(%rip), %rax | |
4338 | // ==> movq foo@tpoff, %rax | |
4339 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); | |
4340 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); | |
4341 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
2e702c99 | 4342 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05); |
c2b45e22 CC |
4343 | view[-2] = 0xc7; |
4344 | view[-1] = 0xc0; | |
4345 | value -= tls_segment->memsz(); | |
fc51264f | 4346 | Relocate_functions<size, false>::rela32(view, value, 0); |
c2b45e22 CC |
4347 | } |
4348 | else | |
4349 | { | |
4350 | // call *foo@tlscall(%rax) | |
4351 | // ==> nop; nop | |
4352 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL); | |
4353 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2); | |
4354 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), | |
2e702c99 | 4355 | view[0] == 0xff && view[1] == 0x10); |
c2b45e22 CC |
4356 | view[0] = 0x66; |
4357 | view[1] = 0x90; | |
4358 | } | |
4359 | } | |
4360 | ||
fc51264f | 4361 | template<int size> |
2e30d253 | 4362 | inline void |
fc51264f L |
4363 | Target_x86_64<size>::Relocate::tls_ld_to_le( |
4364 | const Relocate_info<size, false>* relinfo, | |
4365 | size_t relnum, | |
4366 | Output_segment*, | |
4367 | const elfcpp::Rela<size, false>& rela, | |
4368 | unsigned int, | |
4369 | typename elfcpp::Elf_types<size>::Elf_Addr, | |
4370 | unsigned char* view, | |
4371 | section_size_type view_size) | |
2e30d253 | 4372 | { |
72ec2876 | 4373 | // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt; |
d2cf1c6c | 4374 | // For SIZE == 64: |
72ec2876 ILT |
4375 | // ... leq foo@dtpoff(%rax),%reg |
4376 | // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx | |
d2cf1c6c L |
4377 | // For SIZE == 32: |
4378 | // ... leq foo@dtpoff(%rax),%reg | |
4379 | // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx | |
ad961eab L |
4380 | // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip) |
4381 | // For SIZE == 64: | |
4382 | // ... leq foo@dtpoff(%rax),%reg | |
4383 | // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx | |
4384 | // For SIZE == 32: | |
4385 | // ... leq foo@dtpoff(%rax),%reg | |
4386 | // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx | |
2e30d253 | 4387 | |
72ec2876 ILT |
4388 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); |
4389 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9); | |
2e30d253 | 4390 | |
72ec2876 | 4391 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
2e702c99 | 4392 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d); |
72ec2876 | 4393 | |
ad961eab L |
4394 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
4395 | view[4] == 0xe8 || view[4] == 0xff); | |
72ec2876 | 4396 | |
ad961eab L |
4397 | if (view[4] == 0xe8) |
4398 | { | |
4399 | if (size == 64) | |
4400 | memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12); | |
4401 | else | |
4402 | memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12); | |
4403 | } | |
d2cf1c6c | 4404 | else |
ad961eab L |
4405 | { |
4406 | if (size == 64) | |
4407 | memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", | |
4408 | 13); | |
4409 | else | |
4410 | memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", | |
4411 | 13); | |
4412 | } | |
72ec2876 ILT |
4413 | |
4414 | // The next reloc should be a PLT32 reloc against __tls_get_addr. | |
4415 | // We can skip it. | |
4416 | this->skip_call_tls_get_addr_ = true; | |
2e30d253 ILT |
4417 | } |
4418 | ||
56622147 ILT |
4419 | // Do a relocation in which we convert a TLS Initial-Exec to a |
4420 | // Local-Exec. | |
4421 | ||
fc51264f | 4422 | template<int size> |
56622147 | 4423 | inline void |
fc51264f L |
4424 | Target_x86_64<size>::Relocate::tls_ie_to_le( |
4425 | const Relocate_info<size, false>* relinfo, | |
4426 | size_t relnum, | |
4427 | Output_segment* tls_segment, | |
4428 | const elfcpp::Rela<size, false>& rela, | |
4429 | unsigned int, | |
4430 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
4431 | unsigned char* view, | |
4432 | section_size_type view_size) | |
56622147 ILT |
4433 | { |
4434 | // We need to examine the opcodes to figure out which instruction we | |
4435 | // are looking at. | |
4436 | ||
4437 | // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg | |
4438 | // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg | |
4439 | ||
4440 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); | |
4441 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); | |
4442 | ||
4443 | unsigned char op1 = view[-3]; | |
4444 | unsigned char op2 = view[-2]; | |
4445 | unsigned char op3 = view[-1]; | |
4446 | unsigned char reg = op3 >> 3; | |
4447 | ||
4448 | if (op2 == 0x8b) | |
4449 | { | |
4450 | // movq | |
4451 | if (op1 == 0x4c) | |
2e702c99 | 4452 | view[-3] = 0x49; |
e749cab8 L |
4453 | else if (size == 32 && op1 == 0x44) |
4454 | view[-3] = 0x41; | |
56622147 ILT |
4455 | view[-2] = 0xc7; |
4456 | view[-1] = 0xc0 | reg; | |
4457 | } | |
4458 | else if (reg == 4) | |
4459 | { | |
4460 | // Special handling for %rsp. | |
4461 | if (op1 == 0x4c) | |
2e702c99 | 4462 | view[-3] = 0x49; |
e749cab8 L |
4463 | else if (size == 32 && op1 == 0x44) |
4464 | view[-3] = 0x41; | |
56622147 ILT |
4465 | view[-2] = 0x81; |
4466 | view[-1] = 0xc0 | reg; | |
4467 | } | |
4468 | else | |
4469 | { | |
4470 | // addq | |
4471 | if (op1 == 0x4c) | |
2e702c99 | 4472 | view[-3] = 0x4d; |
e749cab8 L |
4473 | else if (size == 32 && op1 == 0x44) |
4474 | view[-3] = 0x45; | |
56622147 ILT |
4475 | view[-2] = 0x8d; |
4476 | view[-1] = 0x80 | reg | (reg << 3); | |
4477 | } | |
4478 | ||
65d92137 CC |
4479 | if (tls_segment != NULL) |
4480 | value -= tls_segment->memsz(); | |
fc51264f | 4481 | Relocate_functions<size, false>::rela32(view, value, 0); |
56622147 ILT |
4482 | } |
4483 | ||
2e30d253 ILT |
4484 | // Relocate section data. |
4485 | ||
fc51264f | 4486 | template<int size> |
2e30d253 | 4487 | void |
fc51264f L |
4488 | Target_x86_64<size>::relocate_section( |
4489 | const Relocate_info<size, false>* relinfo, | |
364c7fa5 ILT |
4490 | unsigned int sh_type, |
4491 | const unsigned char* prelocs, | |
4492 | size_t reloc_count, | |
4493 | Output_section* output_section, | |
4494 | bool needs_special_offset_handling, | |
4495 | unsigned char* view, | |
fc51264f | 4496 | typename elfcpp::Elf_types<size>::Elf_Addr address, |
364c7fa5 ILT |
4497 | section_size_type view_size, |
4498 | const Reloc_symbol_changes* reloc_symbol_changes) | |
2e30d253 | 4499 | { |
4d625b70 CC |
4500 | typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> |
4501 | Classify_reloc; | |
4502 | ||
2e30d253 ILT |
4503 | gold_assert(sh_type == elfcpp::SHT_RELA); |
4504 | ||
4d625b70 CC |
4505 | gold::relocate_section<size, false, Target_x86_64<size>, Relocate, |
4506 | gold::Default_comdat_behavior, Classify_reloc>( | |
2e30d253 ILT |
4507 | relinfo, |
4508 | this, | |
4509 | prelocs, | |
4510 | reloc_count, | |
730cdc88 ILT |
4511 | output_section, |
4512 | needs_special_offset_handling, | |
2e30d253 ILT |
4513 | view, |
4514 | address, | |
364c7fa5 ILT |
4515 | view_size, |
4516 | reloc_symbol_changes); | |
2e30d253 ILT |
4517 | } |
4518 | ||
94a3fc8b CC |
4519 | // Apply an incremental relocation. Incremental relocations always refer |
4520 | // to global symbols. | |
4521 | ||
fc51264f | 4522 | template<int size> |
94a3fc8b | 4523 | void |
fc51264f L |
4524 | Target_x86_64<size>::apply_relocation( |
4525 | const Relocate_info<size, false>* relinfo, | |
4526 | typename elfcpp::Elf_types<size>::Elf_Addr r_offset, | |
94a3fc8b | 4527 | unsigned int r_type, |
fc51264f | 4528 | typename elfcpp::Elf_types<size>::Elf_Swxword r_addend, |
94a3fc8b CC |
4529 | const Symbol* gsym, |
4530 | unsigned char* view, | |
fc51264f | 4531 | typename elfcpp::Elf_types<size>::Elf_Addr address, |
94a3fc8b CC |
4532 | section_size_type view_size) |
4533 | { | |
fc51264f | 4534 | gold::apply_relocation<size, false, Target_x86_64<size>, |
618d6666 | 4535 | typename Target_x86_64<size>::Relocate>( |
94a3fc8b CC |
4536 | relinfo, |
4537 | this, | |
4538 | r_offset, | |
4539 | r_type, | |
4540 | r_addend, | |
4541 | gsym, | |
4542 | view, | |
4543 | address, | |
4544 | view_size); | |
4545 | } | |
4546 | ||
4d625b70 | 4547 | // Scan the relocs during a relocatable link. |
6a74a719 | 4548 | |
fc51264f | 4549 | template<int size> |
4d625b70 CC |
4550 | void |
4551 | Target_x86_64<size>::scan_relocatable_relocs( | |
4552 | Symbol_table* symtab, | |
4553 | Layout* layout, | |
4554 | Sized_relobj_file<size, false>* object, | |
4555 | unsigned int data_shndx, | |
4556 | unsigned int sh_type, | |
4557 | const unsigned char* prelocs, | |
4558 | size_t reloc_count, | |
4559 | Output_section* output_section, | |
4560 | bool needs_special_offset_handling, | |
4561 | size_t local_symbol_count, | |
4562 | const unsigned char* plocal_symbols, | |
4563 | Relocatable_relocs* rr) | |
6a74a719 | 4564 | { |
4d625b70 CC |
4565 | typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> |
4566 | Classify_reloc; | |
4567 | typedef gold::Default_scan_relocatable_relocs<Classify_reloc> | |
4568 | Scan_relocatable_relocs; | |
6a74a719 | 4569 | |
4d625b70 | 4570 | gold_assert(sh_type == elfcpp::SHT_RELA); |
6a74a719 | 4571 | |
4d625b70 CC |
4572 | gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>( |
4573 | symtab, | |
4574 | layout, | |
4575 | object, | |
4576 | data_shndx, | |
4577 | prelocs, | |
4578 | reloc_count, | |
4579 | output_section, | |
4580 | needs_special_offset_handling, | |
4581 | local_symbol_count, | |
4582 | plocal_symbols, | |
4583 | rr); | |
6a74a719 ILT |
4584 | } |
4585 | ||
4d625b70 | 4586 | // Scan the relocs for --emit-relocs. |
6a74a719 | 4587 | |
fc51264f | 4588 | template<int size> |
6a74a719 | 4589 | void |
4d625b70 | 4590 | Target_x86_64<size>::emit_relocs_scan( |
fc51264f L |
4591 | Symbol_table* symtab, |
4592 | Layout* layout, | |
4593 | Sized_relobj_file<size, false>* object, | |
4594 | unsigned int data_shndx, | |
4595 | unsigned int sh_type, | |
4596 | const unsigned char* prelocs, | |
4597 | size_t reloc_count, | |
4598 | Output_section* output_section, | |
4599 | bool needs_special_offset_handling, | |
4600 | size_t local_symbol_count, | |
4d625b70 | 4601 | const unsigned char* plocal_syms, |
fc51264f | 4602 | Relocatable_relocs* rr) |
6a74a719 | 4603 | { |
4d625b70 CC |
4604 | typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> |
4605 | Classify_reloc; | |
4606 | typedef gold::Default_emit_relocs_strategy<Classify_reloc> | |
4607 | Emit_relocs_strategy; | |
6a74a719 | 4608 | |
4d625b70 | 4609 | gold_assert(sh_type == elfcpp::SHT_RELA); |
6a74a719 | 4610 | |
4d625b70 | 4611 | gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>( |
6a74a719 ILT |
4612 | symtab, |
4613 | layout, | |
4614 | object, | |
4615 | data_shndx, | |
4616 | prelocs, | |
4617 | reloc_count, | |
4618 | output_section, | |
4619 | needs_special_offset_handling, | |
4620 | local_symbol_count, | |
4d625b70 | 4621 | plocal_syms, |
6a74a719 ILT |
4622 | rr); |
4623 | } | |
4624 | ||
4625 | // Relocate a section during a relocatable link. | |
4626 | ||
fc51264f | 4627 | template<int size> |
6a74a719 | 4628 | void |
7404fe1b | 4629 | Target_x86_64<size>::relocate_relocs( |
fc51264f | 4630 | const Relocate_info<size, false>* relinfo, |
6a74a719 ILT |
4631 | unsigned int sh_type, |
4632 | const unsigned char* prelocs, | |
4633 | size_t reloc_count, | |
4634 | Output_section* output_section, | |
62fe925a | 4635 | typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section, |
6a74a719 | 4636 | unsigned char* view, |
fc51264f | 4637 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, |
6a74a719 ILT |
4638 | section_size_type view_size, |
4639 | unsigned char* reloc_view, | |
4640 | section_size_type reloc_view_size) | |
4641 | { | |
4d625b70 CC |
4642 | typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> |
4643 | Classify_reloc; | |
4644 | ||
6a74a719 ILT |
4645 | gold_assert(sh_type == elfcpp::SHT_RELA); |
4646 | ||
4d625b70 | 4647 | gold::relocate_relocs<size, false, Classify_reloc>( |
6a74a719 ILT |
4648 | relinfo, |
4649 | prelocs, | |
4650 | reloc_count, | |
4651 | output_section, | |
4652 | offset_in_output_section, | |
6a74a719 ILT |
4653 | view, |
4654 | view_address, | |
4655 | view_size, | |
4656 | reloc_view, | |
4657 | reloc_view_size); | |
4658 | } | |
4659 | ||
4fb6c25d ILT |
4660 | // Return the value to use for a dynamic which requires special |
4661 | // treatment. This is how we support equality comparisons of function | |
4662 | // pointers across shared library boundaries, as described in the | |
4663 | // processor specific ABI supplement. | |
4664 | ||
fc51264f | 4665 | template<int size> |
4fb6c25d | 4666 | uint64_t |
fc51264f | 4667 | Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const |
4fb6c25d ILT |
4668 | { |
4669 | gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); | |
19fec8c1 | 4670 | return this->plt_address_for_global(gsym); |
4fb6c25d ILT |
4671 | } |
4672 | ||
2e30d253 ILT |
4673 | // Return a string used to fill a code section with nops to take up |
4674 | // the specified length. | |
4675 | ||
fc51264f | 4676 | template<int size> |
2e30d253 | 4677 | std::string |
fc51264f | 4678 | Target_x86_64<size>::do_code_fill(section_size_type length) const |
2e30d253 ILT |
4679 | { |
4680 | if (length >= 16) | |
4681 | { | |
4682 | // Build a jmpq instruction to skip over the bytes. | |
4683 | unsigned char jmp[5]; | |
4684 | jmp[0] = 0xe9; | |
04bf7072 | 4685 | elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5); |
2e30d253 | 4686 | return (std::string(reinterpret_cast<char*>(&jmp[0]), 5) |
2e702c99 | 4687 | + std::string(length - 5, static_cast<char>(0x90))); |
2e30d253 ILT |
4688 | } |
4689 | ||
4690 | // Nop sequences of various lengths. | |
76677ad0 CC |
4691 | const char nop1[1] = { '\x90' }; // nop |
4692 | const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax | |
4693 | const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax) | |
4694 | const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax) | |
2e702c99 | 4695 | '\x00'}; |
76677ad0 CC |
4696 | const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1) |
4697 | '\x00', '\x00' }; | |
4698 | const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1) | |
2e702c99 | 4699 | '\x44', '\x00', '\x00' }; |
76677ad0 | 4700 | const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax) |
2e702c99 | 4701 | '\x00', '\x00', '\x00', |
76677ad0 CC |
4702 | '\x00' }; |
4703 | const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1) | |
2e702c99 | 4704 | '\x00', '\x00', '\x00', |
76677ad0 CC |
4705 | '\x00', '\x00' }; |
4706 | const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1) | |
2e702c99 | 4707 | '\x84', '\x00', '\x00', |
76677ad0 CC |
4708 | '\x00', '\x00', '\x00' }; |
4709 | const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1) | |
2e702c99 | 4710 | '\x1f', '\x84', '\x00', |
76677ad0 CC |
4711 | '\x00', '\x00', '\x00', |
4712 | '\x00' }; | |
4713 | const char nop11[11] = { '\x66', '\x66', '\x2e', // data16 | |
2e702c99 | 4714 | '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1) |
76677ad0 CC |
4715 | '\x00', '\x00', '\x00', |
4716 | '\x00', '\x00' }; | |
4717 | const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16 | |
2e702c99 | 4718 | '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1) |
76677ad0 CC |
4719 | '\x84', '\x00', '\x00', |
4720 | '\x00', '\x00', '\x00' }; | |
4721 | const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16 | |
2e702c99 | 4722 | '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1) |
76677ad0 CC |
4723 | '\x1f', '\x84', '\x00', |
4724 | '\x00', '\x00', '\x00', | |
2e702c99 | 4725 | '\x00' }; |
76677ad0 | 4726 | const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16 |
2e702c99 | 4727 | '\x66', '\x66', '\x2e', // data16 |
76677ad0 CC |
4728 | '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1) |
4729 | '\x00', '\x00', '\x00', | |
2e702c99 | 4730 | '\x00', '\x00' }; |
76677ad0 | 4731 | const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16 |
2e702c99 | 4732 | '\x66', '\x66', '\x66', // data16; data16 |
76677ad0 CC |
4733 | '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1) |
4734 | '\x84', '\x00', '\x00', | |
2e702c99 | 4735 | '\x00', '\x00', '\x00' }; |
2e30d253 ILT |
4736 | |
4737 | const char* nops[16] = { | |
4738 | NULL, | |
4739 | nop1, nop2, nop3, nop4, nop5, nop6, nop7, | |
4740 | nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15 | |
4741 | }; | |
4742 | ||
4743 | return std::string(nops[length], length); | |
4744 | } | |
4745 | ||
e291e7b9 ILT |
4746 | // Return the addend to use for a target specific relocation. The |
4747 | // only target specific relocation is R_X86_64_TLSDESC for a local | |
4748 | // symbol. We want to set the addend is the offset of the local | |
4749 | // symbol in the TLS segment. | |
4750 | ||
fc51264f | 4751 | template<int size> |
e291e7b9 | 4752 | uint64_t |
fc51264f L |
4753 | Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type, |
4754 | uint64_t) const | |
e291e7b9 ILT |
4755 | { |
4756 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC); | |
4757 | uintptr_t intarg = reinterpret_cast<uintptr_t>(arg); | |
4758 | gold_assert(intarg < this->tlsdesc_reloc_info_.size()); | |
4759 | const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]); | |
fc51264f | 4760 | const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym); |
e291e7b9 ILT |
4761 | gold_assert(psymval->is_tls_symbol()); |
4762 | // The value of a TLS symbol is the offset in the TLS segment. | |
4763 | return psymval->value(ti.object, 0); | |
4764 | } | |
4765 | ||
02d7cd44 ILT |
4766 | // Return the value to use for the base of a DW_EH_PE_datarel offset |
4767 | // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their | |
4768 | // assembler can not write out the difference between two labels in | |
4769 | // different sections, so instead of using a pc-relative value they | |
4770 | // use an offset from the GOT. | |
4771 | ||
fc51264f | 4772 | template<int size> |
02d7cd44 | 4773 | uint64_t |
fc51264f | 4774 | Target_x86_64<size>::do_ehframe_datarel_base() const |
02d7cd44 ILT |
4775 | { |
4776 | gold_assert(this->global_offset_table_ != NULL); | |
4777 | Symbol* sym = this->global_offset_table_; | |
fc51264f | 4778 | Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym); |
02d7cd44 ILT |
4779 | return ssym->value(); |
4780 | } | |
4781 | ||
364c7fa5 | 4782 | // FNOFFSET in section SHNDX in OBJECT is the start of a function |
9b547ce6 | 4783 | // compiled with -fsplit-stack. The function calls non-split-stack |
364c7fa5 ILT |
4784 | // code. We have to change the function so that it always ensures |
4785 | // that it has enough stack space to run some random function. | |
4786 | ||
4fc1b9d4 L |
4787 | static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 }; |
4788 | static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 }; | |
4789 | static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 }; | |
4790 | ||
4791 | static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 }; | |
4792 | static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 }; | |
4793 | static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 }; | |
4794 | ||
fc51264f | 4795 | template<int size> |
364c7fa5 | 4796 | void |
fc51264f L |
4797 | Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx, |
4798 | section_offset_type fnoffset, | |
4799 | section_size_type fnsize, | |
6e0813d3 CC |
4800 | const unsigned char*, |
4801 | size_t, | |
fc51264f L |
4802 | unsigned char* view, |
4803 | section_size_type view_size, | |
4804 | std::string* from, | |
4805 | std::string* to) const | |
364c7fa5 | 4806 | { |
4fc1b9d4 L |
4807 | const char* const cmp_insn = reinterpret_cast<const char*> |
4808 | (size == 32 ? cmp_insn_32 : cmp_insn_64); | |
4809 | const char* const lea_r10_insn = reinterpret_cast<const char*> | |
4810 | (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64); | |
4811 | const char* const lea_r11_insn = reinterpret_cast<const char*> | |
4812 | (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64); | |
4813 | ||
4814 | const size_t cmp_insn_len = | |
4815 | (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64)); | |
4816 | const size_t lea_r10_insn_len = | |
4817 | (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64)); | |
4818 | const size_t lea_r11_insn_len = | |
4819 | (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64)); | |
4820 | const size_t nop_len = (size == 32 ? 7 : 8); | |
4821 | ||
364c7fa5 ILT |
4822 | // The function starts with a comparison of the stack pointer and a |
4823 | // field in the TCB. This is followed by a jump. | |
4824 | ||
4825 | // cmp %fs:NN,%rsp | |
4fc1b9d4 L |
4826 | if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len) |
4827 | && fnsize > nop_len + 1) | |
364c7fa5 ILT |
4828 | { |
4829 | // We will call __morestack if the carry flag is set after this | |
4830 | // comparison. We turn the comparison into an stc instruction | |
4831 | // and some nops. | |
4832 | view[fnoffset] = '\xf9'; | |
4fc1b9d4 | 4833 | this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len); |
364c7fa5 ILT |
4834 | } |
4835 | // lea NN(%rsp),%r10 | |
cbc999b9 ILT |
4836 | // lea NN(%rsp),%r11 |
4837 | else if ((this->match_view(view, view_size, fnoffset, | |
4fc1b9d4 | 4838 | lea_r10_insn, lea_r10_insn_len) |
cbc999b9 | 4839 | || this->match_view(view, view_size, fnoffset, |
4fc1b9d4 | 4840 | lea_r11_insn, lea_r11_insn_len)) |
364c7fa5 ILT |
4841 | && fnsize > 8) |
4842 | { | |
4843 | // This is loading an offset from the stack pointer for a | |
4844 | // comparison. The offset is negative, so we decrease the | |
4845 | // offset by the amount of space we need for the stack. This | |
4846 | // means we will avoid calling __morestack if there happens to | |
4847 | // be plenty of space on the stack already. | |
4848 | unsigned char* pval = view + fnoffset + 4; | |
4849 | uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval); | |
4850 | val -= parameters->options().split_stack_adjust_size(); | |
4851 | elfcpp::Swap_unaligned<32, false>::writeval(pval, val); | |
4852 | } | |
4853 | else | |
4854 | { | |
4855 | if (!object->has_no_split_stack()) | |
4856 | object->error(_("failed to match split-stack sequence at " | |
4857 | "section %u offset %0zx"), | |
ac33a407 | 4858 | shndx, static_cast<size_t>(fnoffset)); |
364c7fa5 ILT |
4859 | return; |
4860 | } | |
4861 | ||
4862 | // We have to change the function so that it calls | |
4863 | // __morestack_non_split instead of __morestack. The former will | |
4864 | // allocate additional stack space. | |
4865 | *from = "__morestack"; | |
4866 | *to = "__morestack_non_split"; | |
4867 | } | |
4868 | ||
2e702c99 RM |
4869 | // The selector for x86_64 object files. Note this is never instantiated |
4870 | // directly. It's only used in Target_selector_x86_64_nacl, below. | |
2e30d253 | 4871 | |
fc51264f | 4872 | template<int size> |
36959681 | 4873 | class Target_selector_x86_64 : public Target_selector_freebsd |
2e30d253 ILT |
4874 | { |
4875 | public: | |
4876 | Target_selector_x86_64() | |
fc51264f | 4877 | : Target_selector_freebsd(elfcpp::EM_X86_64, size, false, |
2e702c99 | 4878 | (size == 64 |
fc51264f | 4879 | ? "elf64-x86-64" : "elf32-x86-64"), |
2e702c99 | 4880 | (size == 64 |
fc51264f L |
4881 | ? "elf64-x86-64-freebsd" |
4882 | : "elf32-x86-64-freebsd"), | |
4883 | (size == 64 ? "elf_x86_64" : "elf32_x86_64")) | |
2e30d253 ILT |
4884 | { } |
4885 | ||
4886 | Target* | |
e96caa79 | 4887 | do_instantiate_target() |
fc51264f | 4888 | { return new Target_x86_64<size>(); } |
36959681 | 4889 | |
2e30d253 ILT |
4890 | }; |
4891 | ||
2e702c99 RM |
4892 | // NaCl variant. It uses different PLT contents. |
4893 | ||
4894 | template<int size> | |
4895 | class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size> | |
4896 | { | |
4897 | public: | |
4898 | Output_data_plt_x86_64_nacl(Layout* layout, | |
4899 | Output_data_got<64, false>* got, | |
57b2284c | 4900 | Output_data_got_plt_x86_64* got_plt, |
2e702c99 RM |
4901 | Output_data_space* got_irelative) |
4902 | : Output_data_plt_x86_64<size>(layout, plt_entry_size, | |
4903 | got, got_plt, got_irelative) | |
4904 | { } | |
4905 | ||
4906 | Output_data_plt_x86_64_nacl(Layout* layout, | |
4907 | Output_data_got<64, false>* got, | |
57b2284c | 4908 | Output_data_got_plt_x86_64* got_plt, |
2e702c99 RM |
4909 | Output_data_space* got_irelative, |
4910 | unsigned int plt_count) | |
4911 | : Output_data_plt_x86_64<size>(layout, plt_entry_size, | |
4912 | got, got_plt, got_irelative, | |
4913 | plt_count) | |
4914 | { } | |
4915 | ||
4916 | protected: | |
4917 | virtual unsigned int | |
4918 | do_get_plt_entry_size() const | |
4919 | { return plt_entry_size; } | |
4920 | ||
4921 | virtual void | |
4922 | do_add_eh_frame(Layout* layout) | |
4923 | { | |
4924 | layout->add_eh_frame_for_plt(this, | |
4925 | this->plt_eh_frame_cie, | |
4926 | this->plt_eh_frame_cie_size, | |
4927 | plt_eh_frame_fde, | |
4928 | plt_eh_frame_fde_size); | |
4929 | } | |
4930 | ||
4931 | virtual void | |
4932 | do_fill_first_plt_entry(unsigned char* pov, | |
4933 | typename elfcpp::Elf_types<size>::Elf_Addr got_addr, | |
4934 | typename elfcpp::Elf_types<size>::Elf_Addr plt_addr); | |
4935 | ||
4936 | virtual unsigned int | |
4937 | do_fill_plt_entry(unsigned char* pov, | |
4938 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
4939 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
4940 | unsigned int got_offset, | |
4941 | unsigned int plt_offset, | |
4942 | unsigned int plt_index); | |
4943 | ||
4944 | virtual void | |
4945 | do_fill_tlsdesc_entry(unsigned char* pov, | |
4946 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
4947 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
4948 | typename elfcpp::Elf_types<size>::Elf_Addr got_base, | |
4949 | unsigned int tlsdesc_got_offset, | |
4950 | unsigned int plt_offset); | |
4951 | ||
4952 | private: | |
4953 | // The size of an entry in the PLT. | |
4954 | static const int plt_entry_size = 64; | |
4955 | ||
4956 | // The first entry in the PLT. | |
4957 | static const unsigned char first_plt_entry[plt_entry_size]; | |
4958 | ||
4959 | // Other entries in the PLT for an executable. | |
4960 | static const unsigned char plt_entry[plt_entry_size]; | |
4961 | ||
4962 | // The reserved TLSDESC entry in the PLT for an executable. | |
4963 | static const unsigned char tlsdesc_plt_entry[plt_entry_size]; | |
4964 | ||
4965 | // The .eh_frame unwind information for the PLT. | |
4966 | static const int plt_eh_frame_fde_size = 32; | |
4967 | static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size]; | |
4968 | }; | |
4969 | ||
4970 | template<int size> | |
4971 | class Target_x86_64_nacl : public Target_x86_64<size> | |
4972 | { | |
4973 | public: | |
4974 | Target_x86_64_nacl() | |
4975 | : Target_x86_64<size>(&x86_64_nacl_info) | |
4976 | { } | |
4977 | ||
4978 | virtual Output_data_plt_x86_64<size>* | |
4979 | do_make_data_plt(Layout* layout, | |
4980 | Output_data_got<64, false>* got, | |
57b2284c | 4981 | Output_data_got_plt_x86_64* got_plt, |
2e702c99 RM |
4982 | Output_data_space* got_irelative) |
4983 | { | |
4984 | return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt, | |
4985 | got_irelative); | |
4986 | } | |
4987 | ||
4988 | virtual Output_data_plt_x86_64<size>* | |
4989 | do_make_data_plt(Layout* layout, | |
4990 | Output_data_got<64, false>* got, | |
57b2284c | 4991 | Output_data_got_plt_x86_64* got_plt, |
2e702c99 RM |
4992 | Output_data_space* got_irelative, |
4993 | unsigned int plt_count) | |
4994 | { | |
4995 | return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt, | |
4996 | got_irelative, | |
4997 | plt_count); | |
4998 | } | |
4999 | ||
93f8221c RM |
5000 | virtual std::string |
5001 | do_code_fill(section_size_type length) const; | |
5002 | ||
2e702c99 RM |
5003 | private: |
5004 | static const Target::Target_info x86_64_nacl_info; | |
5005 | }; | |
5006 | ||
5007 | template<> | |
5008 | const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info = | |
5009 | { | |
5010 | 64, // size | |
5011 | false, // is_big_endian | |
5012 | elfcpp::EM_X86_64, // machine_code | |
5013 | false, // has_make_symbol | |
5014 | false, // has_resolve | |
5015 | true, // has_code_fill | |
5016 | true, // is_default_stack_executable | |
5017 | true, // can_icf_inline_merge_sections | |
5018 | '\0', // wrap_char | |
5019 | "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker | |
5020 | 0x20000, // default_text_segment_address | |
5021 | 0x10000, // abi_pagesize (overridable by -z max-page-size) | |
5022 | 0x10000, // common_pagesize (overridable by -z common-page-size) | |
5023 | true, // isolate_execinstr | |
5024 | 0x10000000, // rosegment_gap | |
5025 | elfcpp::SHN_UNDEF, // small_common_shndx | |
5026 | elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx | |
5027 | 0, // small_common_section_flags | |
5028 | elfcpp::SHF_X86_64_LARGE, // large_common_section_flags | |
5029 | NULL, // attributes_section | |
a67858e0 | 5030 | NULL, // attributes_vendor |
8d9743bd MK |
5031 | "_start", // entry_symbol_name |
5032 | 32, // hash_entry_size | |
2e702c99 RM |
5033 | }; |
5034 | ||
5035 | template<> | |
5036 | const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info = | |
5037 | { | |
5038 | 32, // size | |
5039 | false, // is_big_endian | |
5040 | elfcpp::EM_X86_64, // machine_code | |
5041 | false, // has_make_symbol | |
5042 | false, // has_resolve | |
5043 | true, // has_code_fill | |
5044 | true, // is_default_stack_executable | |
5045 | true, // can_icf_inline_merge_sections | |
5046 | '\0', // wrap_char | |
5047 | "/lib/ld-nacl-x86-64.so.1", // dynamic_linker | |
5048 | 0x20000, // default_text_segment_address | |
5049 | 0x10000, // abi_pagesize (overridable by -z max-page-size) | |
5050 | 0x10000, // common_pagesize (overridable by -z common-page-size) | |
5051 | true, // isolate_execinstr | |
5052 | 0x10000000, // rosegment_gap | |
5053 | elfcpp::SHN_UNDEF, // small_common_shndx | |
5054 | elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx | |
5055 | 0, // small_common_section_flags | |
5056 | elfcpp::SHF_X86_64_LARGE, // large_common_section_flags | |
5057 | NULL, // attributes_section | |
a67858e0 | 5058 | NULL, // attributes_vendor |
8d9743bd MK |
5059 | "_start", // entry_symbol_name |
5060 | 32, // hash_entry_size | |
2e702c99 RM |
5061 | }; |
5062 | ||
5063 | #define NACLMASK 0xe0 // 32-byte alignment mask. | |
5064 | ||
5065 | // The first entry in the PLT. | |
5066 | ||
5067 | template<int size> | |
5068 | const unsigned char | |
5069 | Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] = | |
5070 | { | |
5071 | 0xff, 0x35, // pushq contents of memory address | |
5072 | 0, 0, 0, 0, // replaced with address of .got + 8 | |
5073 | 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11 | |
5074 | 0, 0, 0, 0, // replaced with address of .got + 16 | |
5075 | 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d | |
5076 | 0x4d, 0x01, 0xfb, // add %r15, %r11 | |
5077 | 0x41, 0xff, 0xe3, // jmpq *%r11 | |
5078 | ||
5079 | // 9-byte nop sequence to pad out to the next 32-byte boundary. | |
dd0845d7 | 5080 | 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1) |
2e702c99 RM |
5081 | |
5082 | // 32 bytes of nop to pad out to the standard size | |
5083 | 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes | |
5084 | 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) | |
5085 | 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes | |
5086 | 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) | |
5087 | 0x66, // excess data32 prefix | |
5088 | 0x90 // nop | |
5089 | }; | |
5090 | ||
5091 | template<int size> | |
5092 | void | |
5093 | Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry( | |
5094 | unsigned char* pov, | |
5095 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
5096 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address) | |
5097 | { | |
5098 | memcpy(pov, first_plt_entry, plt_entry_size); | |
5099 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, | |
5100 | (got_address + 8 | |
5101 | - (plt_address + 2 + 4))); | |
5102 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 9, | |
5103 | (got_address + 16 | |
5104 | - (plt_address + 9 + 4))); | |
5105 | } | |
5106 | ||
5107 | // Subsequent entries in the PLT. | |
5108 | ||
5109 | template<int size> | |
5110 | const unsigned char | |
5111 | Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] = | |
5112 | { | |
5113 | 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11 | |
5114 | 0, 0, 0, 0, // replaced with address of symbol in .got | |
5115 | 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d | |
5116 | 0x4d, 0x01, 0xfb, // add %r15, %r11 | |
5117 | 0x41, 0xff, 0xe3, // jmpq *%r11 | |
5118 | ||
5119 | // 15-byte nop sequence to pad out to the next 32-byte boundary. | |
5120 | 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes | |
5121 | 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) | |
5122 | ||
5123 | // Lazy GOT entries point here (32-byte aligned). | |
5124 | 0x68, // pushq immediate | |
5125 | 0, 0, 0, 0, // replaced with index into relocation table | |
5126 | 0xe9, // jmp relative | |
5127 | 0, 0, 0, 0, // replaced with offset to start of .plt0 | |
5128 | ||
5129 | // 22 bytes of nop to pad out to the standard size. | |
5130 | 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes | |
5131 | 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) | |
5132 | 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax) | |
5133 | }; | |
5134 | ||
5135 | template<int size> | |
5136 | unsigned int | |
5137 | Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry( | |
5138 | unsigned char* pov, | |
5139 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
5140 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
5141 | unsigned int got_offset, | |
5142 | unsigned int plt_offset, | |
5143 | unsigned int plt_index) | |
5144 | { | |
5145 | memcpy(pov, plt_entry, plt_entry_size); | |
5146 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 3, | |
5147 | (got_address + got_offset | |
5148 | - (plt_address + plt_offset | |
5149 | + 3 + 4))); | |
5150 | ||
5151 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index); | |
5152 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 38, | |
5153 | - (plt_offset + 38 + 4)); | |
5154 | ||
5155 | return 32; | |
5156 | } | |
5157 | ||
5158 | // The reserved TLSDESC entry in the PLT. | |
5159 | ||
5160 | template<int size> | |
5161 | const unsigned char | |
5162 | Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] = | |
5163 | { | |
5164 | 0xff, 0x35, // pushq x(%rip) | |
5165 | 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8) | |
5166 | 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11 | |
5167 | 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry | |
5168 | 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d | |
5169 | 0x4d, 0x01, 0xfb, // add %r15, %r11 | |
5170 | 0x41, 0xff, 0xe3, // jmpq *%r11 | |
5171 | ||
5172 | // 41 bytes of nop to pad out to the standard size. | |
5173 | 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes | |
5174 | 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) | |
5175 | 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes | |
5176 | 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) | |
5177 | 0x66, 0x66, // excess data32 prefixes | |
5178 | 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) | |
5179 | }; | |
5180 | ||
5181 | template<int size> | |
5182 | void | |
5183 | Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry( | |
5184 | unsigned char* pov, | |
5185 | typename elfcpp::Elf_types<size>::Elf_Addr got_address, | |
5186 | typename elfcpp::Elf_types<size>::Elf_Addr plt_address, | |
5187 | typename elfcpp::Elf_types<size>::Elf_Addr got_base, | |
5188 | unsigned int tlsdesc_got_offset, | |
5189 | unsigned int plt_offset) | |
5190 | { | |
5191 | memcpy(pov, tlsdesc_plt_entry, plt_entry_size); | |
5192 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, | |
5193 | (got_address + 8 | |
5194 | - (plt_address + plt_offset | |
5195 | + 2 + 4))); | |
5196 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 9, | |
5197 | (got_base | |
5198 | + tlsdesc_got_offset | |
5199 | - (plt_address + plt_offset | |
5200 | + 9 + 4))); | |
5201 | } | |
5202 | ||
5203 | // The .eh_frame unwind information for the PLT. | |
5204 | ||
5205 | template<int size> | |
5206 | const unsigned char | |
5207 | Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] = | |
5208 | { | |
5209 | 0, 0, 0, 0, // Replaced with offset to .plt. | |
5210 | 0, 0, 0, 0, // Replaced with size of .plt. | |
5211 | 0, // Augmentation size. | |
5212 | elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16. | |
5213 | elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6. | |
5214 | elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24. | |
5215 | elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64. | |
5216 | elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression. | |
5217 | 13, // Block length. | |
5218 | elfcpp::DW_OP_breg7, 8, // Push %rsp + 8. | |
5219 | elfcpp::DW_OP_breg16, 0, // Push %rip. | |
5220 | elfcpp::DW_OP_const1u, 63, // Push 0x3f. | |
5221 | elfcpp::DW_OP_and, // & (%rip & 0x3f). | |
5222 | elfcpp::DW_OP_const1u, 37, // Push 0x25. | |
5223 | elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25) | |
5224 | elfcpp::DW_OP_lit3, // Push 3. | |
5225 | elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3) | |
5226 | elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8 | |
5227 | elfcpp::DW_CFA_nop, // Align to 32 bytes. | |
5228 | elfcpp::DW_CFA_nop | |
5229 | }; | |
5230 | ||
93f8221c RM |
5231 | // Return a string used to fill a code section with nops. |
5232 | // For NaCl, long NOPs are only valid if they do not cross | |
5233 | // bundle alignment boundaries, so keep it simple with one-byte NOPs. | |
5234 | template<int size> | |
5235 | std::string | |
5236 | Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const | |
5237 | { | |
5238 | return std::string(length, static_cast<char>(0x90)); | |
5239 | } | |
5240 | ||
2e702c99 RM |
5241 | // The selector for x86_64-nacl object files. |
5242 | ||
5243 | template<int size> | |
5244 | class Target_selector_x86_64_nacl | |
5245 | : public Target_selector_nacl<Target_selector_x86_64<size>, | |
5246 | Target_x86_64_nacl<size> > | |
5247 | { | |
5248 | public: | |
5249 | Target_selector_x86_64_nacl() | |
5250 | : Target_selector_nacl<Target_selector_x86_64<size>, | |
5251 | Target_x86_64_nacl<size> >("x86-64", | |
5252 | size == 64 | |
5253 | ? "elf64-x86-64-nacl" | |
5254 | : "elf32-x86-64-nacl", | |
5255 | size == 64 | |
5256 | ? "elf_x86_64_nacl" | |
5257 | : "elf32_x86_64_nacl") | |
5258 | { } | |
5259 | }; | |
5260 | ||
5261 | Target_selector_x86_64_nacl<64> target_selector_x86_64; | |
5262 | Target_selector_x86_64_nacl<32> target_selector_x32; | |
2e30d253 ILT |
5263 | |
5264 | } // End anonymous namespace. |