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