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