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