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