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