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