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