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