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