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