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