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