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