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