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