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