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