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