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