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