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