Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // i386.cc -- i386 target support for gold. |
2 | ||
3 | #include "gold.h" | |
ead1e424 ILT |
4 | |
5 | #include <cstring> | |
6 | ||
14bfc3f5 | 7 | #include "elfcpp.h" |
92e059d8 | 8 | #include "reloc.h" |
61ba1cf9 ILT |
9 | #include "i386.h" |
10 | #include "object.h" | |
ead1e424 | 11 | #include "symtab.h" |
92e059d8 ILT |
12 | #include "layout.h" |
13 | #include "output.h" | |
14bfc3f5 | 14 | #include "target.h" |
61ba1cf9 | 15 | #include "target-reloc.h" |
14bfc3f5 ILT |
16 | #include "target-select.h" |
17 | ||
18 | namespace | |
19 | { | |
20 | ||
21 | using namespace gold; | |
22 | ||
a3ad94ed ILT |
23 | class Output_data_plt_i386; |
24 | ||
14bfc3f5 ILT |
25 | // The i386 target class. |
26 | ||
27 | class Target_i386 : public Sized_target<32, false> | |
28 | { | |
29 | public: | |
30 | Target_i386() | |
ead1e424 | 31 | : Sized_target<32, false>(&i386_info), |
a3ad94ed | 32 | got_(NULL), plt_(NULL), got_plt_(NULL) |
14bfc3f5 | 33 | { } |
75f65a3e | 34 | |
92e059d8 | 35 | // Scan the relocations to look for symbol adjustments. |
61ba1cf9 | 36 | void |
92e059d8 ILT |
37 | scan_relocs(const General_options& options, |
38 | Symbol_table* symtab, | |
ead1e424 | 39 | Layout* layout, |
f6ce93d6 | 40 | Sized_relobj<32, false>* object, |
a3ad94ed | 41 | unsigned int data_shndx, |
92e059d8 ILT |
42 | unsigned int sh_type, |
43 | const unsigned char* prelocs, | |
44 | size_t reloc_count, | |
45 | size_t local_symbol_count, | |
46 | const unsigned char* plocal_symbols, | |
47 | Symbol** global_symbols); | |
61ba1cf9 | 48 | |
92e059d8 ILT |
49 | // Relocate a section. |
50 | void | |
51 | relocate_section(const Relocate_info<32, false>*, | |
52 | unsigned int sh_type, | |
53 | const unsigned char* prelocs, | |
54 | size_t reloc_count, | |
55 | unsigned char* view, | |
56 | elfcpp::Elf_types<32>::Elf_Addr view_address, | |
57 | off_t view_size); | |
58 | ||
59 | private: | |
60 | // The class which scans relocations. | |
61 | struct Scan | |
61ba1cf9 ILT |
62 | { |
63 | inline void | |
ead1e424 ILT |
64 | local(const General_options& options, Symbol_table* symtab, |
65 | Layout* layout, Target_i386* target, | |
f6ce93d6 | 66 | Sized_relobj<32, false>* object, |
a3ad94ed | 67 | unsigned int data_shndx, |
92e059d8 ILT |
68 | const elfcpp::Rel<32, false>& reloc, unsigned int r_type, |
69 | const elfcpp::Sym<32, false>& lsym); | |
61ba1cf9 | 70 | |
92e059d8 | 71 | inline void |
ead1e424 ILT |
72 | global(const General_options& options, Symbol_table* symtab, |
73 | Layout* layout, Target_i386* target, | |
f6ce93d6 | 74 | Sized_relobj<32, false>* object, |
a3ad94ed | 75 | unsigned int data_shndx, |
92e059d8 ILT |
76 | const elfcpp::Rel<32, false>& reloc, unsigned int r_type, |
77 | Symbol* gsym); | |
61ba1cf9 ILT |
78 | }; |
79 | ||
92e059d8 ILT |
80 | // The class which implements relocation. |
81 | class Relocate | |
82 | { | |
83 | public: | |
ead1e424 ILT |
84 | Relocate() |
85 | : skip_call_tls_get_addr_(false) | |
86 | { } | |
87 | ||
88 | ~Relocate() | |
89 | { | |
90 | if (this->skip_call_tls_get_addr_) | |
91 | { | |
92 | // FIXME: This needs to specify the location somehow. | |
93 | fprintf(stderr, _("%s: missing expected TLS relocation\n"), | |
94 | program_name); | |
95 | gold_exit(false); | |
96 | } | |
97 | } | |
98 | ||
99 | // Do a relocation. Return false if the caller should not issue | |
100 | // any warnings about this relocation. | |
101 | inline bool | |
102 | relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum, | |
92e059d8 | 103 | const elfcpp::Rel<32, false>&, |
c06b7b0b | 104 | unsigned int r_type, const Sized_symbol<32>*, |
92e059d8 ILT |
105 | elfcpp::Elf_types<32>::Elf_Addr, |
106 | unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, | |
107 | off_t); | |
108 | ||
109 | private: | |
110 | // Do a TLS relocation. | |
ead1e424 | 111 | inline void |
92e059d8 ILT |
112 | relocate_tls(const Relocate_info<32, false>*, size_t relnum, |
113 | const elfcpp::Rel<32, false>&, | |
c06b7b0b | 114 | unsigned int r_type, const Sized_symbol<32>*, |
92e059d8 ILT |
115 | elfcpp::Elf_types<32>::Elf_Addr, |
116 | unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t); | |
117 | ||
118 | // Do a TLS Initial-Exec to Local-Exec transition. | |
119 | static inline void | |
120 | tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum, | |
121 | Output_segment* tls_segment, | |
122 | const elfcpp::Rel<32, false>&, unsigned int r_type, | |
123 | elfcpp::Elf_types<32>::Elf_Addr value, | |
124 | unsigned char* view, | |
125 | off_t view_size); | |
126 | ||
ead1e424 ILT |
127 | // Do a TLS Global-Dynamic to Local-Exec transition. |
128 | inline void | |
129 | tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum, | |
130 | Output_segment* tls_segment, | |
131 | const elfcpp::Rel<32, false>&, unsigned int r_type, | |
132 | elfcpp::Elf_types<32>::Elf_Addr value, | |
133 | unsigned char* view, | |
134 | off_t view_size); | |
135 | ||
92e059d8 ILT |
136 | // Check the range for a TLS relocation. |
137 | static inline void | |
138 | check_range(const Relocate_info<32, false>*, size_t relnum, | |
139 | const elfcpp::Rel<32, false>&, off_t, off_t); | |
140 | ||
141 | // Check the validity of a TLS relocation. This is like assert. | |
142 | static inline void | |
143 | check_tls(const Relocate_info<32, false>*, size_t relnum, | |
144 | const elfcpp::Rel<32, false>&, bool); | |
ead1e424 ILT |
145 | |
146 | // This is set if we should skip the next reloc, which should be a | |
147 | // PLT32 reloc against ___tls_get_addr. | |
148 | bool skip_call_tls_get_addr_; | |
92e059d8 ILT |
149 | }; |
150 | ||
151 | // Adjust TLS relocation type based on the options and whether this | |
152 | // is a local symbol. | |
153 | static unsigned int | |
a3ad94ed | 154 | optimize_tls_reloc(const General_options*, bool is_final, int r_type); |
92e059d8 | 155 | |
ead1e424 | 156 | // Get the GOT section, creating it if necessary. |
dbe717ef | 157 | Output_data_got<32, false>* |
a3ad94ed ILT |
158 | got_section(const General_options*, Symbol_table*, Layout*); |
159 | ||
160 | // Create a PLT entry for a global symbol. | |
161 | void | |
162 | make_plt_entry(const General_options* options, Symbol_table*, | |
163 | Layout*, Symbol*); | |
164 | ||
165 | // Get the PLT section. | |
166 | Output_data_plt_i386* | |
167 | plt_section() const | |
168 | { | |
169 | gold_assert(this->plt_ != NULL); | |
170 | return this->plt_; | |
171 | } | |
172 | ||
173 | // Copy a relocation against a global symbol. | |
174 | void | |
175 | copy_reloc(const General_options*, Sized_relobj<32, false>*, unsigned int, | |
176 | Symbol*, const elfcpp::Rel<32, false>&); | |
ead1e424 | 177 | |
92e059d8 ILT |
178 | // Information about this specific target which we pass to the |
179 | // general Target structure. | |
75f65a3e | 180 | static const Target::Target_info i386_info; |
ead1e424 ILT |
181 | |
182 | // The GOT section. | |
dbe717ef | 183 | Output_data_got<32, false>* got_; |
a3ad94ed ILT |
184 | // The PLT section. |
185 | Output_data_plt_i386* plt_; | |
186 | // The GOT PLT section. | |
187 | Output_data_space* got_plt_; | |
75f65a3e ILT |
188 | }; |
189 | ||
190 | const Target::Target_info Target_i386::i386_info = | |
191 | { | |
61ba1cf9 ILT |
192 | 32, // size |
193 | false, // is_big_endian | |
194 | elfcpp::EM_386, // machine_code | |
195 | false, // has_make_symbol | |
dbe717ef ILT |
196 | false, // has_resolve |
197 | "/usr/lib/libc.so.1", // dynamic_linker | |
198 | 0x08048000, // text_segment_address | |
61ba1cf9 ILT |
199 | 0x1000, // abi_pagesize |
200 | 0x1000 // common_pagesize | |
14bfc3f5 ILT |
201 | }; |
202 | ||
ead1e424 ILT |
203 | // Get the GOT section, creating it if necessary. |
204 | ||
dbe717ef | 205 | Output_data_got<32, false>* |
a3ad94ed ILT |
206 | Target_i386::got_section(const General_options* options, Symbol_table* symtab, |
207 | Layout* layout) | |
ead1e424 ILT |
208 | { |
209 | if (this->got_ == NULL) | |
210 | { | |
a3ad94ed ILT |
211 | gold_assert(options != NULL && symtab != NULL && layout != NULL); |
212 | ||
213 | this->got_ = new Output_data_got<32, false>(options); | |
ead1e424 | 214 | |
ead1e424 ILT |
215 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, |
216 | elfcpp::SHF_ALLOC, this->got_); | |
217 | ||
a3ad94ed ILT |
218 | // The old GNU linker creates a .got.plt section. We just |
219 | // create another set of data in the .got section. Note that we | |
220 | // always create a PLT if we create a GOT, although the PLT | |
221 | // might be empty. | |
222 | this->got_plt_ = new Output_data_space(4); | |
223 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, | |
224 | elfcpp::SHF_ALLOC, this->got_plt_); | |
225 | ||
ead1e424 | 226 | // The first three entries are reserved. |
a3ad94ed | 227 | this->got_plt_->set_space_size(3 * 4); |
ead1e424 | 228 | |
a3ad94ed ILT |
229 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. |
230 | symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", | |
231 | this->got_plt_, | |
ead1e424 ILT |
232 | 0, 0, elfcpp::STT_OBJECT, |
233 | elfcpp::STB_GLOBAL, | |
234 | elfcpp::STV_HIDDEN, 0, | |
235 | false, false); | |
236 | } | |
a3ad94ed | 237 | |
ead1e424 ILT |
238 | return this->got_; |
239 | } | |
240 | ||
a3ad94ed ILT |
241 | // A class to handle the PLT data. |
242 | ||
243 | class Output_data_plt_i386 : public Output_section_data | |
244 | { | |
245 | public: | |
246 | typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section; | |
247 | ||
248 | Output_data_plt_i386(Layout*, Output_data_space*, bool is_shared); | |
249 | ||
250 | // Add an entry to the PLT. | |
251 | void | |
252 | add_entry(Symbol* gsym); | |
253 | ||
254 | private: | |
255 | // The size of an entry in the PLT. | |
256 | static const int plt_entry_size = 16; | |
257 | ||
258 | // The first entry in the PLT for an executable. | |
259 | static unsigned char exec_first_plt_entry[plt_entry_size]; | |
260 | ||
261 | // The first entry in the PLT for a shared object. | |
262 | static unsigned char dyn_first_plt_entry[plt_entry_size]; | |
263 | ||
264 | // Other entries in the PLT for an executable. | |
265 | static unsigned char exec_plt_entry[plt_entry_size]; | |
266 | ||
267 | // Other entries in the PLT for a shared object. | |
268 | static unsigned char dyn_plt_entry[plt_entry_size]; | |
269 | ||
270 | // Set the final size. | |
271 | void | |
272 | do_set_address(uint64_t, off_t) | |
273 | { this->set_data_size((this->count_ + 1) * plt_entry_size); } | |
274 | ||
275 | // Write out the PLT data. | |
276 | void | |
277 | do_write(Output_file*); | |
278 | ||
279 | // The reloc section. | |
280 | Reloc_section* rel_; | |
281 | // The .got.plt section. | |
282 | Output_data_space* got_plt_; | |
283 | // The number of PLT entries. | |
284 | unsigned int count_; | |
285 | // Whether we are generated a shared object. | |
286 | bool is_shared_; | |
287 | }; | |
288 | ||
289 | // Create the PLT section. The ordinary .got section is an argument, | |
290 | // since we need to refer to the start. We also create our own .got | |
291 | // section just for PLT entries. | |
292 | ||
293 | Output_data_plt_i386::Output_data_plt_i386(Layout* layout, | |
294 | Output_data_space* got_plt, | |
295 | bool is_shared) | |
296 | : Output_section_data(4), got_plt_(got_plt), is_shared_(is_shared) | |
297 | { | |
298 | this->rel_ = new Reloc_section(); | |
299 | layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL, | |
300 | elfcpp::SHF_ALLOC, this->rel_); | |
301 | } | |
302 | ||
303 | // Add an entry to the PLT. | |
304 | ||
305 | void | |
306 | Output_data_plt_i386::add_entry(Symbol* gsym) | |
307 | { | |
308 | gold_assert(!gsym->has_plt_offset()); | |
309 | ||
310 | // Note that when setting the PLT offset we skip the initial | |
311 | // reserved PLT entry. | |
312 | gsym->set_plt_offset((this->count_ + 1) * plt_entry_size); | |
313 | ||
314 | ++this->count_; | |
315 | ||
316 | off_t got_offset = this->got_plt_->data_size(); | |
317 | ||
318 | // Every PLT entry needs a GOT entry which points back to the PLT | |
319 | // entry (this will be changed by the dynamic linker, normally | |
320 | // lazily when the function is called). | |
321 | this->got_plt_->set_space_size(got_offset + 4); | |
322 | ||
323 | // Every PLT entry needs a reloc. | |
324 | this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_, | |
325 | got_offset); | |
326 | ||
327 | // Note that we don't need to save the symbol. The contents of the | |
328 | // PLT are independent of which symbols are used. The symbols only | |
329 | // appear in the relocations. | |
330 | } | |
331 | ||
332 | // The first entry in the PLT for an executable. | |
333 | ||
334 | unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] = | |
335 | { | |
336 | 0xff, 0x35, // pushl contents of memory address | |
337 | 0, 0, 0, 0, // replaced with address of .got + 4 | |
338 | 0xff, 0x25, // jmp indirect | |
339 | 0, 0, 0, 0, // replaced with address of .got + 8 | |
340 | 0, 0, 0, 0 // unused | |
341 | }; | |
342 | ||
343 | // The first entry in the PLT for a shared object. | |
344 | ||
345 | unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] = | |
346 | { | |
347 | 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx) | |
348 | 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx) | |
349 | 0, 0, 0, 0 // unused | |
350 | }; | |
351 | ||
352 | // Subsequent entries in the PLT for an executable. | |
353 | ||
354 | unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] = | |
355 | { | |
356 | 0xff, 0x25, // jmp indirect | |
357 | 0, 0, 0, 0, // replaced with address of symbol in .got | |
358 | 0x68, // pushl immediate | |
359 | 0, 0, 0, 0, // replaced with offset into relocation table | |
360 | 0xe9, // jmp relative | |
361 | 0, 0, 0, 0 // replaced with offset to start of .plt | |
362 | }; | |
363 | ||
364 | // Subsequent entries in the PLT for a shared object. | |
365 | ||
366 | unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] = | |
367 | { | |
368 | 0xff, 0xa3, // jmp *offset(%ebx) | |
369 | 0, 0, 0, 0, // replaced with offset of symbol in .got | |
370 | 0x68, // pushl immediate | |
371 | 0, 0, 0, 0, // replaced with offset into relocation table | |
372 | 0xe9, // jmp relative | |
373 | 0, 0, 0, 0 // replaced with offset to start of .plt | |
374 | }; | |
375 | ||
376 | // Write out the PLT. This uses the hand-coded instructions above, | |
377 | // and adjusts them as needed. This is all specified by the i386 ELF | |
378 | // Processor Supplement. | |
379 | ||
380 | void | |
381 | Output_data_plt_i386::do_write(Output_file* of) | |
382 | { | |
383 | const off_t offset = this->offset(); | |
384 | const off_t oview_size = this->data_size(); | |
385 | unsigned char* const oview = of->get_output_view(offset, oview_size); | |
386 | ||
387 | const off_t got_file_offset = this->got_plt_->offset(); | |
388 | const off_t got_size = this->got_plt_->data_size(); | |
389 | unsigned char* const got_view = of->get_output_view(got_file_offset, | |
390 | got_size); | |
391 | ||
392 | unsigned char* pov = oview; | |
393 | ||
394 | elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address(); | |
395 | elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address(); | |
396 | ||
397 | if (this->is_shared_) | |
398 | memcpy(pov, dyn_first_plt_entry, plt_entry_size); | |
399 | else | |
400 | { | |
401 | memcpy(pov, exec_first_plt_entry, plt_entry_size); | |
402 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4); | |
403 | elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8); | |
404 | } | |
405 | pov += plt_entry_size; | |
406 | ||
407 | unsigned char* got_pov = got_view; | |
408 | ||
409 | memset(got_pov, 0, 12); | |
410 | got_pov += 12; | |
411 | ||
412 | const int rel_size = elfcpp::Elf_sizes<32>::rel_size; | |
413 | ||
414 | unsigned int plt_offset = plt_entry_size; | |
415 | unsigned int plt_rel_offset = 0; | |
416 | unsigned int got_offset = 12; | |
417 | const unsigned int count = this->count_; | |
418 | for (unsigned int i = 0; | |
419 | i < count; | |
420 | ++i, | |
421 | pov += plt_entry_size, | |
422 | got_pov += 4, | |
423 | plt_offset += plt_entry_size, | |
424 | plt_rel_offset += rel_size, | |
425 | got_offset += 4) | |
426 | { | |
427 | // Set and adjust the PLT entry itself. | |
428 | ||
429 | if (this->is_shared_) | |
430 | { | |
431 | memcpy(pov, dyn_plt_entry, plt_entry_size); | |
432 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset); | |
433 | } | |
434 | else | |
435 | { | |
436 | memcpy(pov, exec_plt_entry, plt_entry_size); | |
437 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, | |
438 | (got_address | |
439 | + got_offset)); | |
440 | } | |
441 | ||
442 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset); | |
443 | elfcpp::Swap<32, false>::writeval(pov + 12, | |
444 | - (plt_offset + plt_entry_size)); | |
445 | ||
446 | // Set the entry in the GOT. | |
447 | elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6); | |
448 | } | |
449 | ||
450 | gold_assert(pov - oview == oview_size); | |
451 | gold_assert(got_pov - got_view == got_size); | |
452 | ||
453 | of->write_output_view(offset, oview_size, oview); | |
454 | of->write_output_view(got_file_offset, got_size, got_view); | |
455 | } | |
456 | ||
457 | // Create a PLT entry for a global symbol. | |
458 | ||
459 | void | |
460 | Target_i386::make_plt_entry(const General_options* options, | |
461 | Symbol_table* symtab, Layout* layout, Symbol* gsym) | |
462 | { | |
463 | if (gsym->has_plt_offset()) | |
464 | return; | |
465 | ||
466 | if (this->plt_ == NULL) | |
467 | { | |
468 | // Create the GOT sections first. | |
469 | this->got_section(options, symtab, layout); | |
470 | ||
471 | this->plt_ = new Output_data_plt_i386(layout, this->got_plt_, | |
472 | options->is_shared()); | |
473 | } | |
474 | ||
475 | this->plt_->add_entry(gsym); | |
476 | } | |
477 | ||
478 | // Handle a relocation against a non-function symbol defined in a | |
479 | // dynamic object. The traditional way to handle this is to generate | |
480 | // a COPY relocation to copy the variable at runtime from the shared | |
481 | // object into the executable's data segment. However, this is | |
482 | // undesirable in general, as if the size of the object changes in the | |
483 | // dynamic object, the executable will no longer work correctly. If | |
484 | // this relocation is in a writable section, then we can create a | |
485 | // dynamic reloc and the dynamic linker will resolve it to the correct | |
486 | // address at runtime. However, we do not want do that if the | |
487 | // relocation is in a read-only section, as it would prevent the | |
488 | // readonly segment from being shared. And if we have to eventually | |
489 | // generate a COPY reloc, then any dynamic relocations will be | |
490 | // useless. So this means that if this is a writable section, we need | |
491 | // to save the relocation until we see whether we have to create a | |
492 | // COPY relocation for this symbol for any other relocation. | |
493 | ||
494 | void | |
495 | Target_i386::copy_reloc(const General_options* options, | |
496 | Sized_relobj<32, false>* object, | |
497 | unsigned int data_shndx, Symbol* gsym, | |
498 | const elfcpp::Rel<32, false>&) | |
499 | { | |
500 | if (!Relocate_functions<32, false>::need_copy_reloc(options, object, | |
501 | data_shndx, gsym)) | |
502 | { | |
503 | // So far we do not need a COPY reloc. Save this relocation. | |
504 | // If it turns out that we never a COPY reloc for this symbol, | |
505 | // then we emit the relocation. | |
506 | } | |
507 | ||
508 | } | |
509 | ||
92e059d8 | 510 | // Optimize the TLS relocation type based on what we know about the |
a3ad94ed ILT |
511 | // symbol. IS_FINAL is true if the final address of this symbol is |
512 | // known at link time. | |
92e059d8 ILT |
513 | |
514 | unsigned int | |
a3ad94ed ILT |
515 | Target_i386::optimize_tls_reloc(const General_options* options, |
516 | bool is_final, | |
92e059d8 ILT |
517 | int r_type) |
518 | { | |
519 | // If we are generating a shared library, then we can't do anything | |
520 | // in the linker. | |
521 | if (options->is_shared()) | |
522 | return r_type; | |
523 | ||
524 | switch (r_type) | |
525 | { | |
526 | case elfcpp::R_386_TLS_GD: | |
527 | case elfcpp::R_386_TLS_GOTDESC: | |
528 | case elfcpp::R_386_TLS_DESC_CALL: | |
529 | // These are Global-Dynamic which permits fully general TLS | |
530 | // access. Since we know that we are generating an executable, | |
531 | // we can convert this to Initial-Exec. If we also know that | |
532 | // this is a local symbol, we can further switch to Local-Exec. | |
a3ad94ed | 533 | if (is_final) |
92e059d8 ILT |
534 | return elfcpp::R_386_TLS_LE_32; |
535 | return elfcpp::R_386_TLS_IE_32; | |
536 | ||
537 | case elfcpp::R_386_TLS_LDM: | |
538 | // This is Local-Dynamic, which refers to a local symbol in the | |
539 | // dynamic TLS block. Since we know that we generating an | |
540 | // executable, we can switch to Local-Exec. | |
541 | return elfcpp::R_386_TLS_LE_32; | |
542 | ||
543 | case elfcpp::R_386_TLS_LDO_32: | |
544 | // Another type of Local-Dynamic relocation. | |
545 | return elfcpp::R_386_TLS_LE; | |
546 | ||
547 | case elfcpp::R_386_TLS_IE: | |
548 | case elfcpp::R_386_TLS_GOTIE: | |
549 | case elfcpp::R_386_TLS_IE_32: | |
550 | // These are Initial-Exec relocs which get the thread offset | |
551 | // from the GOT. If we know that we are linking against the | |
552 | // local symbol, we can switch to Local-Exec, which links the | |
553 | // thread offset into the instruction. | |
a3ad94ed | 554 | if (is_final) |
92e059d8 ILT |
555 | return elfcpp::R_386_TLS_LE_32; |
556 | return r_type; | |
557 | ||
558 | case elfcpp::R_386_TLS_LE: | |
559 | case elfcpp::R_386_TLS_LE_32: | |
560 | // When we already have Local-Exec, there is nothing further we | |
561 | // can do. | |
562 | return r_type; | |
563 | ||
564 | default: | |
a3ad94ed | 565 | gold_unreachable(); |
92e059d8 ILT |
566 | } |
567 | } | |
568 | ||
569 | // Scan a relocation for a local symbol. | |
570 | ||
571 | inline void | |
572 | Target_i386::Scan::local(const General_options& options, | |
ead1e424 ILT |
573 | Symbol_table* symtab, |
574 | Layout* layout, | |
575 | Target_i386* target, | |
f6ce93d6 | 576 | Sized_relobj<32, false>* object, |
a3ad94ed ILT |
577 | unsigned int, |
578 | const elfcpp::Rel<32, false>&, | |
579 | unsigned int r_type, | |
92e059d8 ILT |
580 | const elfcpp::Sym<32, false>&) |
581 | { | |
582 | switch (r_type) | |
583 | { | |
584 | case elfcpp::R_386_NONE: | |
585 | case elfcpp::R_386_GNU_VTINHERIT: | |
586 | case elfcpp::R_386_GNU_VTENTRY: | |
587 | break; | |
588 | ||
589 | case elfcpp::R_386_32: | |
590 | case elfcpp::R_386_16: | |
591 | case elfcpp::R_386_8: | |
592 | // FIXME: If we are generating a shared object we need to copy | |
593 | // this relocation into the object. | |
a3ad94ed | 594 | gold_assert(!options.is_shared()); |
92e059d8 ILT |
595 | break; |
596 | ||
597 | case elfcpp::R_386_PC32: | |
598 | case elfcpp::R_386_PC16: | |
599 | case elfcpp::R_386_PC8: | |
600 | break; | |
601 | ||
ead1e424 ILT |
602 | case elfcpp::R_386_GOTOFF: |
603 | case elfcpp::R_386_GOTPC: | |
604 | // We need a GOT section. | |
a3ad94ed | 605 | target->got_section(&options, symtab, layout); |
ead1e424 ILT |
606 | break; |
607 | ||
92e059d8 ILT |
608 | case elfcpp::R_386_COPY: |
609 | case elfcpp::R_386_GLOB_DAT: | |
610 | case elfcpp::R_386_JUMP_SLOT: | |
611 | case elfcpp::R_386_RELATIVE: | |
612 | case elfcpp::R_386_TLS_TPOFF: | |
613 | case elfcpp::R_386_TLS_DTPMOD32: | |
614 | case elfcpp::R_386_TLS_DTPOFF32: | |
615 | case elfcpp::R_386_TLS_TPOFF32: | |
616 | case elfcpp::R_386_TLS_DESC: | |
617 | fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"), | |
618 | program_name, object->name().c_str(), r_type); | |
619 | gold_exit(false); | |
620 | break; | |
621 | ||
622 | case elfcpp::R_386_TLS_IE: | |
623 | case elfcpp::R_386_TLS_GOTIE: | |
624 | case elfcpp::R_386_TLS_LE: | |
625 | case elfcpp::R_386_TLS_GD: | |
626 | case elfcpp::R_386_TLS_LDM: | |
627 | case elfcpp::R_386_TLS_LDO_32: | |
628 | case elfcpp::R_386_TLS_IE_32: | |
629 | case elfcpp::R_386_TLS_LE_32: | |
630 | case elfcpp::R_386_TLS_GOTDESC: | |
631 | case elfcpp::R_386_TLS_DESC_CALL: | |
a3ad94ed ILT |
632 | r_type = Target_i386::optimize_tls_reloc(&options, |
633 | !options.is_shared(), | |
634 | r_type); | |
92e059d8 ILT |
635 | switch (r_type) |
636 | { | |
637 | case elfcpp::R_386_TLS_LE: | |
638 | case elfcpp::R_386_TLS_LE_32: | |
639 | // FIXME: If generating a shared object, we need to copy | |
640 | // this relocation into the object. | |
a3ad94ed | 641 | gold_assert(!options.is_shared()); |
92e059d8 ILT |
642 | break; |
643 | ||
644 | case elfcpp::R_386_TLS_IE: | |
645 | case elfcpp::R_386_TLS_GOTIE: | |
646 | case elfcpp::R_386_TLS_GD: | |
647 | case elfcpp::R_386_TLS_LDM: | |
648 | case elfcpp::R_386_TLS_LDO_32: | |
649 | case elfcpp::R_386_TLS_IE_32: | |
650 | case elfcpp::R_386_TLS_GOTDESC: | |
651 | case elfcpp::R_386_TLS_DESC_CALL: | |
652 | fprintf(stderr, | |
653 | _("%s: %s: unsupported reloc %u against local symbol\n"), | |
654 | program_name, object->name().c_str(), r_type); | |
655 | break; | |
656 | } | |
657 | break; | |
658 | ||
659 | case elfcpp::R_386_GOT32: | |
660 | case elfcpp::R_386_PLT32: | |
92e059d8 ILT |
661 | case elfcpp::R_386_32PLT: |
662 | case elfcpp::R_386_TLS_GD_32: | |
663 | case elfcpp::R_386_TLS_GD_PUSH: | |
664 | case elfcpp::R_386_TLS_GD_CALL: | |
665 | case elfcpp::R_386_TLS_GD_POP: | |
666 | case elfcpp::R_386_TLS_LDM_32: | |
667 | case elfcpp::R_386_TLS_LDM_PUSH: | |
668 | case elfcpp::R_386_TLS_LDM_CALL: | |
669 | case elfcpp::R_386_TLS_LDM_POP: | |
670 | case elfcpp::R_386_USED_BY_INTEL_200: | |
671 | default: | |
672 | fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"), | |
673 | program_name, object->name().c_str(), r_type); | |
674 | break; | |
675 | } | |
676 | } | |
677 | ||
678 | // Scan a relocation for a global symbol. | |
679 | ||
680 | inline void | |
681 | Target_i386::Scan::global(const General_options& options, | |
ead1e424 ILT |
682 | Symbol_table* symtab, |
683 | Layout* layout, | |
684 | Target_i386* target, | |
f6ce93d6 | 685 | Sized_relobj<32, false>* object, |
a3ad94ed ILT |
686 | unsigned int data_shndx, |
687 | const elfcpp::Rel<32, false>& reloc, | |
688 | unsigned int r_type, | |
92e059d8 ILT |
689 | Symbol* gsym) |
690 | { | |
691 | switch (r_type) | |
692 | { | |
693 | case elfcpp::R_386_NONE: | |
694 | case elfcpp::R_386_GNU_VTINHERIT: | |
695 | case elfcpp::R_386_GNU_VTENTRY: | |
696 | break; | |
697 | ||
698 | case elfcpp::R_386_32: | |
699 | case elfcpp::R_386_PC32: | |
700 | case elfcpp::R_386_16: | |
701 | case elfcpp::R_386_PC16: | |
702 | case elfcpp::R_386_8: | |
703 | case elfcpp::R_386_PC8: | |
704 | // FIXME: If we are generating a shared object we may need to | |
705 | // copy this relocation into the object. If this symbol is | |
706 | // defined in a shared object, we may need to copy this | |
707 | // relocation in order to avoid a COPY relocation. | |
a3ad94ed ILT |
708 | gold_assert(!options.is_shared()); |
709 | ||
710 | if (gsym->is_defined_in_dynobj()) | |
711 | { | |
712 | // This symbol is defined in a dynamic object. If it is a | |
713 | // function, we make a PLT entry. Otherwise we need to | |
714 | // either generate a COPY reloc or copy this reloc. | |
715 | if (gsym->type() == elfcpp::STT_FUNC) | |
716 | target->make_plt_entry(&options, symtab, layout, gsym); | |
717 | else | |
718 | target->copy_reloc(&options, object, data_shndx, gsym, reloc); | |
719 | } | |
720 | ||
92e059d8 ILT |
721 | break; |
722 | ||
ead1e424 ILT |
723 | case elfcpp::R_386_GOT32: |
724 | // The symbol requires a GOT entry. | |
a3ad94ed | 725 | if (target->got_section(&options, symtab, layout)->add_global(gsym)) |
ead1e424 | 726 | { |
a3ad94ed | 727 | // If this symbol is not fully resolved, we need to add a |
ead1e424 | 728 | // dynamic relocation for it. |
a3ad94ed ILT |
729 | if (!gsym->final_value_is_known(&options)) |
730 | gold_unreachable(); | |
ead1e424 ILT |
731 | } |
732 | break; | |
733 | ||
734 | case elfcpp::R_386_PLT32: | |
a3ad94ed ILT |
735 | // If the symbol is fully resolved, this is just a PC32 reloc. |
736 | // Otherwise we need a PLT entry. | |
737 | if (gsym->final_value_is_known(&options)) | |
ead1e424 | 738 | break; |
a3ad94ed | 739 | target->make_plt_entry(&options, symtab, layout, gsym); |
ead1e424 ILT |
740 | break; |
741 | ||
742 | case elfcpp::R_386_GOTOFF: | |
743 | case elfcpp::R_386_GOTPC: | |
744 | // We need a GOT section. | |
a3ad94ed | 745 | target->got_section(&options, symtab, layout); |
ead1e424 ILT |
746 | break; |
747 | ||
92e059d8 ILT |
748 | case elfcpp::R_386_COPY: |
749 | case elfcpp::R_386_GLOB_DAT: | |
750 | case elfcpp::R_386_JUMP_SLOT: | |
751 | case elfcpp::R_386_RELATIVE: | |
752 | case elfcpp::R_386_TLS_TPOFF: | |
753 | case elfcpp::R_386_TLS_DTPMOD32: | |
754 | case elfcpp::R_386_TLS_DTPOFF32: | |
755 | case elfcpp::R_386_TLS_TPOFF32: | |
756 | case elfcpp::R_386_TLS_DESC: | |
757 | fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"), | |
758 | program_name, object->name().c_str(), r_type); | |
759 | gold_exit(false); | |
760 | break; | |
761 | ||
762 | case elfcpp::R_386_TLS_IE: | |
763 | case elfcpp::R_386_TLS_GOTIE: | |
764 | case elfcpp::R_386_TLS_LE: | |
765 | case elfcpp::R_386_TLS_GD: | |
766 | case elfcpp::R_386_TLS_LDM: | |
767 | case elfcpp::R_386_TLS_LDO_32: | |
768 | case elfcpp::R_386_TLS_IE_32: | |
769 | case elfcpp::R_386_TLS_LE_32: | |
770 | case elfcpp::R_386_TLS_GOTDESC: | |
771 | case elfcpp::R_386_TLS_DESC_CALL: | |
a3ad94ed ILT |
772 | { |
773 | const bool is_final = gsym->final_value_is_known(&options); | |
774 | r_type = Target_i386::optimize_tls_reloc(&options, is_final, r_type); | |
775 | switch (r_type) | |
776 | { | |
777 | case elfcpp::R_386_TLS_LE: | |
778 | case elfcpp::R_386_TLS_LE_32: | |
779 | // FIXME: If generating a shared object, we need to copy | |
780 | // this relocation into the object. | |
781 | gold_assert(!options.is_shared()); | |
782 | break; | |
783 | ||
784 | case elfcpp::R_386_TLS_IE: | |
785 | case elfcpp::R_386_TLS_GOTIE: | |
786 | case elfcpp::R_386_TLS_GD: | |
787 | case elfcpp::R_386_TLS_LDM: | |
788 | case elfcpp::R_386_TLS_LDO_32: | |
789 | case elfcpp::R_386_TLS_IE_32: | |
790 | case elfcpp::R_386_TLS_GOTDESC: | |
791 | case elfcpp::R_386_TLS_DESC_CALL: | |
792 | fprintf(stderr, | |
793 | _("%s: %s: unsupported reloc %u " | |
794 | "against global symbol %s\n"), | |
795 | program_name, object->name().c_str(), r_type, | |
796 | gsym->name()); | |
797 | break; | |
798 | } | |
799 | } | |
92e059d8 ILT |
800 | break; |
801 | ||
92e059d8 ILT |
802 | case elfcpp::R_386_32PLT: |
803 | case elfcpp::R_386_TLS_GD_32: | |
804 | case elfcpp::R_386_TLS_GD_PUSH: | |
805 | case elfcpp::R_386_TLS_GD_CALL: | |
806 | case elfcpp::R_386_TLS_GD_POP: | |
807 | case elfcpp::R_386_TLS_LDM_32: | |
808 | case elfcpp::R_386_TLS_LDM_PUSH: | |
809 | case elfcpp::R_386_TLS_LDM_CALL: | |
810 | case elfcpp::R_386_TLS_LDM_POP: | |
811 | case elfcpp::R_386_USED_BY_INTEL_200: | |
812 | default: | |
813 | fprintf(stderr, | |
814 | _("%s: %s: unsupported reloc %u against global symbol %s\n"), | |
815 | program_name, object->name().c_str(), r_type, gsym->name()); | |
816 | break; | |
817 | } | |
818 | } | |
819 | ||
820 | // Scan relocations for a section. | |
821 | ||
822 | void | |
823 | Target_i386::scan_relocs(const General_options& options, | |
824 | Symbol_table* symtab, | |
ead1e424 | 825 | Layout* layout, |
f6ce93d6 | 826 | Sized_relobj<32, false>* object, |
a3ad94ed | 827 | unsigned int data_shndx, |
92e059d8 ILT |
828 | unsigned int sh_type, |
829 | const unsigned char* prelocs, | |
830 | size_t reloc_count, | |
831 | size_t local_symbol_count, | |
832 | const unsigned char* plocal_symbols, | |
833 | Symbol** global_symbols) | |
834 | { | |
835 | if (sh_type == elfcpp::SHT_RELA) | |
836 | { | |
837 | fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"), | |
838 | program_name, object->name().c_str()); | |
839 | gold_exit(false); | |
840 | } | |
841 | ||
ead1e424 ILT |
842 | gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL, |
843 | Target_i386::Scan>( | |
92e059d8 ILT |
844 | options, |
845 | symtab, | |
ead1e424 ILT |
846 | layout, |
847 | this, | |
92e059d8 | 848 | object, |
a3ad94ed | 849 | data_shndx, |
92e059d8 ILT |
850 | prelocs, |
851 | reloc_count, | |
852 | local_symbol_count, | |
853 | plocal_symbols, | |
854 | global_symbols); | |
855 | } | |
856 | ||
61ba1cf9 ILT |
857 | // Perform a relocation. |
858 | ||
ead1e424 | 859 | inline bool |
92e059d8 | 860 | Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo, |
ead1e424 | 861 | Target_i386* target, |
92e059d8 ILT |
862 | size_t relnum, |
863 | const elfcpp::Rel<32, false>& rel, | |
864 | unsigned int r_type, | |
c06b7b0b | 865 | const Sized_symbol<32>* gsym, |
92e059d8 ILT |
866 | elfcpp::Elf_types<32>::Elf_Addr value, |
867 | unsigned char* view, | |
868 | elfcpp::Elf_types<32>::Elf_Addr address, | |
869 | off_t view_size) | |
61ba1cf9 | 870 | { |
ead1e424 ILT |
871 | if (this->skip_call_tls_get_addr_) |
872 | { | |
873 | if (r_type != elfcpp::R_386_PLT32 | |
874 | || gsym == NULL | |
875 | || strcmp(gsym->name(), "___tls_get_addr") != 0) | |
876 | { | |
877 | fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"), | |
878 | program_name, | |
879 | relinfo->location(relnum, rel.get_r_offset()).c_str()); | |
880 | gold_exit(false); | |
881 | } | |
882 | ||
883 | this->skip_call_tls_get_addr_ = false; | |
884 | ||
885 | return false; | |
886 | } | |
887 | ||
a3ad94ed ILT |
888 | // Pick the value to use for symbols defined in shared objects. |
889 | if (gsym != NULL && gsym->is_defined_in_dynobj()) | |
890 | { | |
891 | if (gsym->has_plt_offset()) | |
892 | address = target->plt_section()->address() + gsym->plt_offset(); | |
893 | else | |
894 | gold_unreachable(); | |
895 | } | |
896 | ||
61ba1cf9 ILT |
897 | switch (r_type) |
898 | { | |
899 | case elfcpp::R_386_NONE: | |
92e059d8 ILT |
900 | case elfcpp::R_386_GNU_VTINHERIT: |
901 | case elfcpp::R_386_GNU_VTENTRY: | |
61ba1cf9 ILT |
902 | break; |
903 | ||
904 | case elfcpp::R_386_32: | |
92e059d8 | 905 | Relocate_functions<32, false>::rel32(view, value); |
61ba1cf9 ILT |
906 | break; |
907 | ||
908 | case elfcpp::R_386_PC32: | |
92e059d8 ILT |
909 | Relocate_functions<32, false>::pcrel32(view, value, address); |
910 | break; | |
911 | ||
912 | case elfcpp::R_386_16: | |
913 | Relocate_functions<32, false>::rel16(view, value); | |
914 | break; | |
915 | ||
916 | case elfcpp::R_386_PC16: | |
917 | Relocate_functions<32, false>::pcrel16(view, value, address); | |
61ba1cf9 ILT |
918 | break; |
919 | ||
92e059d8 ILT |
920 | case elfcpp::R_386_8: |
921 | Relocate_functions<32, false>::rel8(view, value); | |
922 | break; | |
923 | ||
924 | case elfcpp::R_386_PC8: | |
925 | Relocate_functions<32, false>::pcrel8(view, value, address); | |
926 | break; | |
927 | ||
ead1e424 | 928 | case elfcpp::R_386_PLT32: |
a3ad94ed ILT |
929 | gold_assert(gsym->has_plt_offset() |
930 | || gsym->final_value_is_known(relinfo->options)); | |
931 | Relocate_functions<32, false>::pcrel32(view, value, address); | |
ead1e424 ILT |
932 | break; |
933 | ||
934 | case elfcpp::R_386_GOT32: | |
935 | // Local GOT offsets not yet supported. | |
a3ad94ed ILT |
936 | gold_assert(gsym); |
937 | gold_assert(gsym->has_got_offset()); | |
ead1e424 ILT |
938 | value = gsym->got_offset(); |
939 | Relocate_functions<32, false>::rel32(view, value); | |
940 | break; | |
941 | ||
942 | case elfcpp::R_386_GOTOFF: | |
a3ad94ed | 943 | value -= target->got_section(NULL, NULL, NULL)->address(); |
ead1e424 ILT |
944 | Relocate_functions<32, false>::rel32(view, value); |
945 | break; | |
946 | ||
947 | case elfcpp::R_386_GOTPC: | |
a3ad94ed | 948 | value = target->got_section(NULL, NULL, NULL)->address(); |
ead1e424 ILT |
949 | Relocate_functions<32, false>::pcrel32(view, value, address); |
950 | break; | |
951 | ||
92e059d8 ILT |
952 | case elfcpp::R_386_COPY: |
953 | case elfcpp::R_386_GLOB_DAT: | |
954 | case elfcpp::R_386_JUMP_SLOT: | |
955 | case elfcpp::R_386_RELATIVE: | |
956 | case elfcpp::R_386_TLS_TPOFF: | |
957 | case elfcpp::R_386_TLS_DTPMOD32: | |
958 | case elfcpp::R_386_TLS_DTPOFF32: | |
959 | case elfcpp::R_386_TLS_TPOFF32: | |
960 | case elfcpp::R_386_TLS_DESC: | |
961 | fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"), | |
962 | program_name, | |
963 | relinfo->location(relnum, rel.get_r_offset()).c_str(), | |
964 | r_type); | |
965 | gold_exit(false); | |
966 | break; | |
967 | ||
968 | case elfcpp::R_386_TLS_IE: | |
969 | case elfcpp::R_386_TLS_GOTIE: | |
970 | case elfcpp::R_386_TLS_LE: | |
971 | case elfcpp::R_386_TLS_GD: | |
972 | case elfcpp::R_386_TLS_LDM: | |
973 | case elfcpp::R_386_TLS_LDO_32: | |
974 | case elfcpp::R_386_TLS_IE_32: | |
975 | case elfcpp::R_386_TLS_LE_32: | |
976 | case elfcpp::R_386_TLS_GOTDESC: | |
977 | case elfcpp::R_386_TLS_DESC_CALL: | |
ead1e424 ILT |
978 | this->relocate_tls(relinfo, relnum, rel, r_type, gsym, value, view, |
979 | address, view_size); | |
92e059d8 ILT |
980 | break; |
981 | ||
92e059d8 ILT |
982 | case elfcpp::R_386_32PLT: |
983 | case elfcpp::R_386_TLS_GD_32: | |
984 | case elfcpp::R_386_TLS_GD_PUSH: | |
985 | case elfcpp::R_386_TLS_GD_CALL: | |
986 | case elfcpp::R_386_TLS_GD_POP: | |
987 | case elfcpp::R_386_TLS_LDM_32: | |
988 | case elfcpp::R_386_TLS_LDM_PUSH: | |
989 | case elfcpp::R_386_TLS_LDM_CALL: | |
990 | case elfcpp::R_386_TLS_LDM_POP: | |
991 | case elfcpp::R_386_USED_BY_INTEL_200: | |
61ba1cf9 ILT |
992 | default: |
993 | fprintf(stderr, _("%s: %s: unsupported reloc %u\n"), | |
92e059d8 ILT |
994 | program_name, |
995 | relinfo->location(relnum, rel.get_r_offset()).c_str(), | |
996 | r_type); | |
61ba1cf9 | 997 | // gold_exit(false); |
92e059d8 ILT |
998 | break; |
999 | } | |
ead1e424 ILT |
1000 | |
1001 | return true; | |
92e059d8 ILT |
1002 | } |
1003 | ||
1004 | // Perform a TLS relocation. | |
1005 | ||
1006 | inline void | |
1007 | Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo, | |
1008 | size_t relnum, | |
1009 | const elfcpp::Rel<32, false>& rel, | |
1010 | unsigned int r_type, | |
c06b7b0b | 1011 | const Sized_symbol<32>* gsym, |
92e059d8 ILT |
1012 | elfcpp::Elf_types<32>::Elf_Addr value, |
1013 | unsigned char* view, | |
1014 | elfcpp::Elf_types<32>::Elf_Addr, | |
1015 | off_t view_size) | |
1016 | { | |
1017 | Output_segment* tls_segment = relinfo->layout->tls_segment(); | |
1018 | if (tls_segment == NULL) | |
1019 | { | |
1020 | fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"), | |
1021 | program_name, | |
1022 | relinfo->location(relnum, rel.get_r_offset()).c_str()); | |
1023 | gold_exit(false); | |
1024 | } | |
1025 | ||
a3ad94ed ILT |
1026 | const bool is_final = (gsym == NULL |
1027 | ? !relinfo->options->is_shared() | |
1028 | : gsym->final_value_is_known(relinfo->options)); | |
92e059d8 | 1029 | const unsigned int opt_r_type = |
a3ad94ed | 1030 | Target_i386::optimize_tls_reloc(relinfo->options, is_final, r_type); |
92e059d8 ILT |
1031 | switch (r_type) |
1032 | { | |
1033 | case elfcpp::R_386_TLS_LE_32: | |
1034 | value = tls_segment->vaddr() + tls_segment->memsz() - value; | |
1035 | Relocate_functions<32, false>::rel32(view, value); | |
1036 | break; | |
1037 | ||
1038 | case elfcpp::R_386_TLS_LE: | |
1039 | value = value - (tls_segment->vaddr() + tls_segment->memsz()); | |
1040 | Relocate_functions<32, false>::rel32(view, value); | |
1041 | break; | |
1042 | ||
1043 | case elfcpp::R_386_TLS_IE: | |
1044 | case elfcpp::R_386_TLS_GOTIE: | |
1045 | case elfcpp::R_386_TLS_IE_32: | |
1046 | if (opt_r_type == elfcpp::R_386_TLS_LE_32) | |
1047 | { | |
1048 | Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment, | |
1049 | rel, r_type, value, view, | |
1050 | view_size); | |
1051 | break; | |
1052 | } | |
1053 | fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"), | |
1054 | program_name, | |
1055 | relinfo->location(relnum, rel.get_r_offset()).c_str(), | |
1056 | r_type); | |
1057 | // gold_exit(false); | |
1058 | break; | |
1059 | ||
1060 | case elfcpp::R_386_TLS_GD: | |
ead1e424 ILT |
1061 | if (opt_r_type == elfcpp::R_386_TLS_LE_32) |
1062 | { | |
1063 | this->tls_gd_to_le(relinfo, relnum, tls_segment, | |
1064 | rel, r_type, value, view, | |
1065 | view_size); | |
1066 | break; | |
1067 | } | |
1068 | fprintf(stderr, _("%s: %s: unsupported reloc %u\n"), | |
1069 | program_name, | |
1070 | relinfo->location(relnum, rel.get_r_offset()).c_str(), | |
1071 | r_type); | |
1072 | // gold_exit(false); | |
1073 | break; | |
1074 | ||
92e059d8 ILT |
1075 | case elfcpp::R_386_TLS_LDM: |
1076 | case elfcpp::R_386_TLS_LDO_32: | |
1077 | case elfcpp::R_386_TLS_GOTDESC: | |
1078 | case elfcpp::R_386_TLS_DESC_CALL: | |
1079 | fprintf(stderr, _("%s: %s: unsupported reloc %u\n"), | |
1080 | program_name, | |
1081 | relinfo->location(relnum, rel.get_r_offset()).c_str(), | |
1082 | r_type); | |
1083 | // gold_exit(false); | |
1084 | break; | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | // Do a relocation in which we convert a TLS Initial-Exec to a | |
1089 | // Local-Exec. | |
1090 | ||
1091 | inline void | |
1092 | Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo, | |
1093 | size_t relnum, | |
1094 | Output_segment* tls_segment, | |
1095 | const elfcpp::Rel<32, false>& rel, | |
1096 | unsigned int r_type, | |
1097 | elfcpp::Elf_types<32>::Elf_Addr value, | |
1098 | unsigned char* view, | |
1099 | off_t view_size) | |
1100 | { | |
1101 | // We have to actually change the instructions, which means that we | |
1102 | // need to examine the opcodes to figure out which instruction we | |
1103 | // are looking at. | |
1104 | if (r_type == elfcpp::R_386_TLS_IE) | |
1105 | { | |
1106 | // movl %gs:XX,%eax ==> movl $YY,%eax | |
1107 | // movl %gs:XX,%reg ==> movl $YY,%reg | |
1108 | // addl %gs:XX,%reg ==> addl $YY,%reg | |
1109 | Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -1); | |
1110 | Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4); | |
1111 | ||
1112 | unsigned char op1 = view[-1]; | |
1113 | if (op1 == 0xa1) | |
1114 | { | |
1115 | // movl XX,%eax ==> movl $YY,%eax | |
1116 | view[-1] = 0xb8; | |
1117 | } | |
1118 | else | |
1119 | { | |
1120 | Target_i386::Relocate::check_range(relinfo, relnum, rel, | |
1121 | view_size, -2); | |
1122 | ||
1123 | unsigned char op2 = view[-2]; | |
1124 | if (op2 == 0x8b) | |
1125 | { | |
1126 | // movl XX,%reg ==> movl $YY,%reg | |
1127 | Target_i386::Relocate::check_tls(relinfo, relnum, rel, | |
1128 | (op1 & 0xc7) == 0x05); | |
1129 | view[-2] = 0xc7; | |
1130 | view[-1] = 0xc0 | ((op1 >> 3) & 7); | |
1131 | } | |
1132 | else if (op2 == 0x03) | |
1133 | { | |
1134 | // addl XX,%reg ==> addl $YY,%reg | |
1135 | Target_i386::Relocate::check_tls(relinfo, relnum, rel, | |
1136 | (op1 & 0xc7) == 0x05); | |
1137 | view[-2] = 0x81; | |
1138 | view[-1] = 0xc0 | ((op1 >> 3) & 7); | |
1139 | } | |
1140 | else | |
1141 | Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0); | |
1142 | } | |
1143 | } | |
1144 | else | |
1145 | { | |
1146 | // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2 | |
1147 | // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2 | |
1148 | // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2 | |
1149 | Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2); | |
1150 | Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4); | |
1151 | ||
1152 | unsigned char op1 = view[-1]; | |
1153 | unsigned char op2 = view[-2]; | |
1154 | Target_i386::Relocate::check_tls(relinfo, relnum, rel, | |
1155 | (op1 & 0xc0) == 0x80 && (op1 & 7) != 4); | |
1156 | if (op2 == 0x8b) | |
1157 | { | |
1158 | // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2 | |
1159 | view[-2] = 0xc7; | |
1160 | view[-1] = 0xc0 | ((op1 >> 3) & 7); | |
1161 | } | |
1162 | else if (op2 == 0x2b) | |
1163 | { | |
1164 | // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2 | |
1165 | view[-2] = 0x81; | |
1166 | view[-1] = 0xe8 | ((op1 >> 3) & 7); | |
1167 | } | |
1168 | else if (op2 == 0x03) | |
1169 | { | |
1170 | // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2 | |
1171 | view[-2] = 0x81; | |
1172 | view[-1] = 0xc0 | ((op1 >> 3) & 7); | |
1173 | } | |
1174 | else | |
1175 | Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0); | |
1176 | } | |
1177 | ||
ead1e424 ILT |
1178 | value = tls_segment->vaddr() + tls_segment->memsz() - value; |
1179 | if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE) | |
1180 | value = - value; | |
92e059d8 ILT |
1181 | |
1182 | Relocate_functions<32, false>::rel32(view, value); | |
1183 | } | |
1184 | ||
ead1e424 ILT |
1185 | // Do a relocation in which we convert a TLS Global-Dynamic to a |
1186 | // Local-Exec. | |
1187 | ||
1188 | inline void | |
1189 | Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo, | |
1190 | size_t relnum, | |
1191 | Output_segment* tls_segment, | |
1192 | const elfcpp::Rel<32, false>& rel, | |
1193 | unsigned int, | |
1194 | elfcpp::Elf_types<32>::Elf_Addr value, | |
1195 | unsigned char* view, | |
1196 | off_t view_size) | |
1197 | { | |
1198 | // leal foo(,%reg,1),%eax; call ___tls_get_addr | |
1199 | // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax | |
1200 | // leal foo(%reg),%eax; call ___tls_get_addr | |
1201 | // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax | |
1202 | ||
1203 | Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2); | |
1204 | Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 9); | |
1205 | ||
1206 | unsigned char op1 = view[-1]; | |
1207 | unsigned char op2 = view[-2]; | |
1208 | ||
1209 | Target_i386::Relocate::check_tls(relinfo, relnum, rel, | |
1210 | op2 == 0x8d || op2 == 0x04); | |
1211 | Target_i386::Relocate::check_tls(relinfo, relnum, rel, | |
1212 | view[4] == 0xe8); | |
1213 | ||
1214 | int roff = 5; | |
1215 | ||
1216 | if (op2 == 0x04) | |
1217 | { | |
1218 | Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -3); | |
1219 | Target_i386::Relocate::check_tls(relinfo, relnum, rel, | |
1220 | view[-3] == 0x8d); | |
1221 | Target_i386::Relocate::check_tls(relinfo, relnum, rel, | |
1222 | ((op1 & 0xc7) == 0x05 | |
1223 | && op1 != (4 << 3))); | |
1224 | memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12); | |
1225 | } | |
1226 | else | |
1227 | { | |
1228 | Target_i386::Relocate::check_tls(relinfo, relnum, rel, | |
1229 | (op1 & 0xf8) == 0x80 && (op1 & 7) != 4); | |
1230 | if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90) | |
1231 | { | |
1232 | // There is a trailing nop. Use the size byte subl. | |
1233 | memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12); | |
1234 | roff = 6; | |
1235 | } | |
1236 | else | |
1237 | { | |
1238 | // Use the five byte subl. | |
1239 | memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11); | |
1240 | } | |
1241 | } | |
1242 | ||
1243 | value = tls_segment->vaddr() + tls_segment->memsz() - value; | |
1244 | Relocate_functions<32, false>::rel32(view + roff, value); | |
1245 | ||
1246 | // The next reloc should be a PLT32 reloc against __tls_get_addr. | |
1247 | // We can skip it. | |
1248 | this->skip_call_tls_get_addr_ = true; | |
1249 | } | |
1250 | ||
92e059d8 ILT |
1251 | // Check the range for a TLS relocation. |
1252 | ||
1253 | inline void | |
1254 | Target_i386::Relocate::check_range(const Relocate_info<32, false>* relinfo, | |
1255 | size_t relnum, | |
1256 | const elfcpp::Rel<32, false>& rel, | |
1257 | off_t view_size, off_t off) | |
1258 | { | |
1259 | off_t offset = rel.get_r_offset() + off; | |
1260 | if (offset < 0 || offset > view_size) | |
1261 | { | |
1262 | fprintf(stderr, _("%s: %s: TLS relocation out of range\n"), | |
1263 | program_name, | |
1264 | relinfo->location(relnum, rel.get_r_offset()).c_str()); | |
1265 | gold_exit(false); | |
1266 | } | |
1267 | } | |
1268 | ||
1269 | // Check the validity of a TLS relocation. This is like assert. | |
1270 | ||
1271 | inline void | |
1272 | Target_i386::Relocate::check_tls(const Relocate_info<32, false>* relinfo, | |
1273 | size_t relnum, | |
1274 | const elfcpp::Rel<32, false>& rel, | |
1275 | bool valid) | |
1276 | { | |
1277 | if (!valid) | |
1278 | { | |
1279 | fprintf(stderr, | |
1280 | _("%s: %s: TLS relocation against invalid instruction\n"), | |
1281 | program_name, | |
1282 | relinfo->location(relnum, rel.get_r_offset()).c_str()); | |
1283 | gold_exit(false); | |
61ba1cf9 ILT |
1284 | } |
1285 | } | |
1286 | ||
1287 | // Relocate section data. | |
1288 | ||
1289 | void | |
92e059d8 | 1290 | Target_i386::relocate_section(const Relocate_info<32, false>* relinfo, |
61ba1cf9 ILT |
1291 | unsigned int sh_type, |
1292 | const unsigned char* prelocs, | |
1293 | size_t reloc_count, | |
61ba1cf9 ILT |
1294 | unsigned char* view, |
1295 | elfcpp::Elf_types<32>::Elf_Addr address, | |
1296 | off_t view_size) | |
1297 | { | |
a3ad94ed | 1298 | gold_assert(sh_type == elfcpp::SHT_REL); |
61ba1cf9 | 1299 | |
ead1e424 ILT |
1300 | gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL, |
1301 | Target_i386::Relocate>( | |
92e059d8 | 1302 | relinfo, |
ead1e424 | 1303 | this, |
61ba1cf9 ILT |
1304 | prelocs, |
1305 | reloc_count, | |
61ba1cf9 ILT |
1306 | view, |
1307 | address, | |
1308 | view_size); | |
1309 | } | |
1310 | ||
14bfc3f5 ILT |
1311 | // The selector for i386 object files. |
1312 | ||
1313 | class Target_selector_i386 : public Target_selector | |
1314 | { | |
1315 | public: | |
1316 | Target_selector_i386() | |
1317 | : Target_selector(elfcpp::EM_386, 32, false) | |
1318 | { } | |
1319 | ||
1320 | Target* | |
ead1e424 ILT |
1321 | recognize(int machine, int osabi, int abiversion); |
1322 | ||
1323 | private: | |
1324 | Target_i386* target_; | |
14bfc3f5 ILT |
1325 | }; |
1326 | ||
1327 | // Recognize an i386 object file when we already know that the machine | |
1328 | // number is EM_386. | |
1329 | ||
1330 | Target* | |
ead1e424 | 1331 | Target_selector_i386::recognize(int, int, int) |
14bfc3f5 | 1332 | { |
ead1e424 ILT |
1333 | if (this->target_ == NULL) |
1334 | this->target_ = new Target_i386(); | |
1335 | return this->target_; | |
14bfc3f5 ILT |
1336 | } |
1337 | ||
1338 | Target_selector_i386 target_selector_i386; | |
1339 | ||
1340 | } // End anonymous namespace. |