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