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