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