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