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