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