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