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