1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "parameters.h"
36 #include "copy-relocs.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
49 // A class to handle the PLT data.
51 class Output_data_plt_i386 : public Output_section_data
54 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
56 Output_data_plt_i386(Symbol_table*, Layout*, Output_data_space*);
58 // Add an entry to the PLT.
60 add_entry(Symbol* gsym);
62 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
64 add_local_ifunc_entry(Sized_relobj_file<32, false>* relobj,
65 unsigned int local_sym_index);
67 // Return the .rel.plt section data.
70 { return this->rel_; }
72 // Return where the TLS_DESC relocations should go.
74 rel_tls_desc(Layout*);
76 // Return the number of PLT entries.
79 { return this->count_; }
81 // Return the offset of the first non-reserved PLT entry.
83 first_plt_entry_offset()
84 { return plt_entry_size; }
86 // Return the size of a PLT entry.
89 { return plt_entry_size; }
93 do_adjust_output_section(Output_section* os);
95 // Write to a map file.
97 do_print_to_mapfile(Mapfile* mapfile) const
98 { mapfile->print_output_data(this, _("** PLT")); }
101 // The size of an entry in the PLT.
102 static const int plt_entry_size = 16;
104 // The first entry in the PLT for an executable.
105 static const unsigned char exec_first_plt_entry[plt_entry_size];
107 // The first entry in the PLT for a shared object.
108 static const unsigned char dyn_first_plt_entry[plt_entry_size];
110 // Other entries in the PLT for an executable.
111 static const unsigned char exec_plt_entry[plt_entry_size];
113 // Other entries in the PLT for a shared object.
114 static const unsigned char dyn_plt_entry[plt_entry_size];
116 // The .eh_frame unwind information for the PLT.
117 static const int plt_eh_frame_cie_size = 16;
118 static const int plt_eh_frame_fde_size = 32;
119 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
120 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
122 // Set the final size.
124 set_final_data_size()
125 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
127 // Write out the PLT data.
129 do_write(Output_file*);
131 // We keep a list of global STT_GNU_IFUNC symbols, each with its
132 // offset in the GOT.
136 unsigned int got_offset;
139 // We keep a list of local STT_GNU_IFUNC symbols, each with its
140 // offset in the GOT.
143 Sized_relobj_file<32, false>* object;
144 unsigned int local_sym_index;
145 unsigned int got_offset;
148 // The reloc section.
150 // The TLS_DESC relocations, if necessary. These must follow the
151 // regular PLT relocs.
152 Reloc_section* tls_desc_rel_;
153 // The .got.plt section.
154 Output_data_space* got_plt_;
155 // The number of PLT entries.
157 // Global STT_GNU_IFUNC symbols.
158 std::vector<Global_ifunc> global_ifuncs_;
159 // Local STT_GNU_IFUNC symbols.
160 std::vector<Local_ifunc> local_ifuncs_;
163 // The i386 target class.
164 // TLS info comes from
165 // http://people.redhat.com/drepper/tls.pdf
166 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
168 class Target_i386 : public Sized_target<32, false>
171 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
174 : Sized_target<32, false>(&i386_info),
175 got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
176 global_offset_table_(NULL), rel_dyn_(NULL),
177 copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
178 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
181 // Process the relocations to determine unreferenced sections for
182 // garbage collection.
184 gc_process_relocs(Symbol_table* symtab,
186 Sized_relobj_file<32, false>* object,
187 unsigned int data_shndx,
188 unsigned int sh_type,
189 const unsigned char* prelocs,
191 Output_section* output_section,
192 bool needs_special_offset_handling,
193 size_t local_symbol_count,
194 const unsigned char* plocal_symbols);
196 // Scan the relocations to look for symbol adjustments.
198 scan_relocs(Symbol_table* symtab,
200 Sized_relobj_file<32, false>* object,
201 unsigned int data_shndx,
202 unsigned int sh_type,
203 const unsigned char* prelocs,
205 Output_section* output_section,
206 bool needs_special_offset_handling,
207 size_t local_symbol_count,
208 const unsigned char* plocal_symbols);
210 // Finalize the sections.
212 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
214 // Return the value to use for a dynamic which requires special
217 do_dynsym_value(const Symbol*) const;
219 // Relocate a section.
221 relocate_section(const Relocate_info<32, false>*,
222 unsigned int sh_type,
223 const unsigned char* prelocs,
225 Output_section* output_section,
226 bool needs_special_offset_handling,
228 elfcpp::Elf_types<32>::Elf_Addr view_address,
229 section_size_type view_size,
230 const Reloc_symbol_changes*);
232 // Scan the relocs during a relocatable link.
234 scan_relocatable_relocs(Symbol_table* symtab,
236 Sized_relobj_file<32, false>* object,
237 unsigned int data_shndx,
238 unsigned int sh_type,
239 const unsigned char* prelocs,
241 Output_section* output_section,
242 bool needs_special_offset_handling,
243 size_t local_symbol_count,
244 const unsigned char* plocal_symbols,
245 Relocatable_relocs*);
247 // Relocate a section during a relocatable link.
249 relocate_for_relocatable(const Relocate_info<32, false>*,
250 unsigned int sh_type,
251 const unsigned char* prelocs,
253 Output_section* output_section,
254 off_t offset_in_output_section,
255 const Relocatable_relocs*,
257 elfcpp::Elf_types<32>::Elf_Addr view_address,
258 section_size_type view_size,
259 unsigned char* reloc_view,
260 section_size_type reloc_view_size);
262 // Return a string used to fill a code section with nops.
264 do_code_fill(section_size_type length) const;
266 // Return whether SYM is defined by the ABI.
268 do_is_defined_by_abi(const Symbol* sym) const
269 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
271 // Return whether a symbol name implies a local label. The UnixWare
272 // 2.1 cc generates temporary symbols that start with .X, so we
273 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
274 // If so, we should move the .X recognition into
275 // Target::do_is_local_label_name.
277 do_is_local_label_name(const char* name) const
279 if (name[0] == '.' && name[1] == 'X')
281 return Target::do_is_local_label_name(name);
284 // Return the PLT section.
286 do_plt_section_for_global(const Symbol*) const
287 { return this->plt_section(); }
290 do_plt_section_for_local(const Relobj*, unsigned int) const
291 { return this->plt_section(); }
293 // We can tell whether we take the address of a function.
295 do_can_check_for_function_pointers() const
298 // Return whether SYM is call to a non-split function.
300 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
302 // Adjust -fsplit-stack code which calls non-split-stack code.
304 do_calls_non_split(Relobj* object, unsigned int shndx,
305 section_offset_type fnoffset, section_size_type fnsize,
306 unsigned char* view, section_size_type view_size,
307 std::string* from, std::string* to) const;
309 // Return the size of the GOT section.
313 gold_assert(this->got_ != NULL);
314 return this->got_->data_size();
317 // Return the number of entries in the GOT.
319 got_entry_count() const
321 if (this->got_ == NULL)
323 return this->got_size() / 4;
326 // Return the number of entries in the PLT.
328 plt_entry_count() const;
330 // Return the offset of the first non-reserved PLT entry.
332 first_plt_entry_offset() const;
334 // Return the size of each PLT entry.
336 plt_entry_size() const;
339 // The class which scans relocations.
344 get_reference_flags(unsigned int r_type);
347 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
348 Sized_relobj_file<32, false>* object,
349 unsigned int data_shndx,
350 Output_section* output_section,
351 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
352 const elfcpp::Sym<32, false>& lsym);
355 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
356 Sized_relobj_file<32, false>* object,
357 unsigned int data_shndx,
358 Output_section* output_section,
359 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
363 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
365 Sized_relobj_file<32, false>* object,
366 unsigned int data_shndx,
367 Output_section* output_section,
368 const elfcpp::Rel<32, false>& reloc,
370 const elfcpp::Sym<32, false>& lsym);
373 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
375 Sized_relobj_file<32, false>* object,
376 unsigned int data_shndx,
377 Output_section* output_section,
378 const elfcpp::Rel<32, false>& reloc,
383 possible_function_pointer_reloc(unsigned int r_type);
386 reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
387 unsigned int r_type);
390 unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
393 unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
397 // The class which implements relocation.
402 : skip_call_tls_get_addr_(false),
403 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
408 if (this->skip_call_tls_get_addr_)
410 // FIXME: This needs to specify the location somehow.
411 gold_error(_("missing expected TLS relocation"));
415 // Return whether the static relocation needs to be applied.
417 should_apply_static_reloc(const Sized_symbol<32>* gsym,
420 Output_section* output_section);
422 // Do a relocation. Return false if the caller should not issue
423 // any warnings about this relocation.
425 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
426 size_t relnum, const elfcpp::Rel<32, false>&,
427 unsigned int r_type, const Sized_symbol<32>*,
428 const Symbol_value<32>*,
429 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
433 // Do a TLS relocation.
435 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
436 size_t relnum, const elfcpp::Rel<32, false>&,
437 unsigned int r_type, const Sized_symbol<32>*,
438 const Symbol_value<32>*,
439 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
442 // Do a TLS General-Dynamic to Initial-Exec transition.
444 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
445 Output_segment* tls_segment,
446 const elfcpp::Rel<32, false>&, unsigned int r_type,
447 elfcpp::Elf_types<32>::Elf_Addr value,
449 section_size_type view_size);
451 // Do a TLS General-Dynamic to Local-Exec transition.
453 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
454 Output_segment* tls_segment,
455 const elfcpp::Rel<32, false>&, unsigned int r_type,
456 elfcpp::Elf_types<32>::Elf_Addr value,
458 section_size_type view_size);
460 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
463 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
464 Output_segment* tls_segment,
465 const elfcpp::Rel<32, false>&, unsigned int r_type,
466 elfcpp::Elf_types<32>::Elf_Addr value,
468 section_size_type view_size);
470 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
473 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
474 Output_segment* tls_segment,
475 const elfcpp::Rel<32, false>&, unsigned int r_type,
476 elfcpp::Elf_types<32>::Elf_Addr value,
478 section_size_type view_size);
480 // Do a TLS Local-Dynamic to Local-Exec transition.
482 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
483 Output_segment* tls_segment,
484 const elfcpp::Rel<32, false>&, unsigned int r_type,
485 elfcpp::Elf_types<32>::Elf_Addr value,
487 section_size_type view_size);
489 // Do a TLS Initial-Exec to Local-Exec transition.
491 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
492 Output_segment* tls_segment,
493 const elfcpp::Rel<32, false>&, unsigned int r_type,
494 elfcpp::Elf_types<32>::Elf_Addr value,
496 section_size_type view_size);
498 // We need to keep track of which type of local dynamic relocation
499 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
500 enum Local_dynamic_type
507 // This is set if we should skip the next reloc, which should be a
508 // PLT32 reloc against ___tls_get_addr.
509 bool skip_call_tls_get_addr_;
510 // The type of local dynamic relocation we have seen in the section
511 // being relocated, if any.
512 Local_dynamic_type local_dynamic_type_;
515 // A class which returns the size required for a relocation type,
516 // used while scanning relocs during a relocatable link.
517 class Relocatable_size_for_reloc
521 get_size_for_reloc(unsigned int, Relobj*);
524 // Adjust TLS relocation type based on the options and whether this
525 // is a local symbol.
526 static tls::Tls_optimization
527 optimize_tls_reloc(bool is_final, int r_type);
529 // Get the GOT section, creating it if necessary.
530 Output_data_got<32, false>*
531 got_section(Symbol_table*, Layout*);
533 // Get the GOT PLT section.
535 got_plt_section() const
537 gold_assert(this->got_plt_ != NULL);
538 return this->got_plt_;
541 // Get the GOT section for TLSDESC entries.
542 Output_data_got<32, false>*
543 got_tlsdesc_section() const
545 gold_assert(this->got_tlsdesc_ != NULL);
546 return this->got_tlsdesc_;
549 // Create the PLT section.
551 make_plt_section(Symbol_table* symtab, Layout* layout);
553 // Create a PLT entry for a global symbol.
555 make_plt_entry(Symbol_table*, Layout*, Symbol*);
557 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
559 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
560 Sized_relobj_file<32, false>* relobj,
561 unsigned int local_sym_index);
563 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
565 define_tls_base_symbol(Symbol_table*, Layout*);
567 // Create a GOT entry for the TLS module index.
569 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
570 Sized_relobj_file<32, false>* object);
572 // Get the PLT section.
573 Output_data_plt_i386*
576 gold_assert(this->plt_ != NULL);
580 // Get the dynamic reloc section, creating it if necessary.
582 rel_dyn_section(Layout*);
584 // Get the section to use for TLS_DESC relocations.
586 rel_tls_desc_section(Layout*) const;
588 // Add a potential copy relocation.
590 copy_reloc(Symbol_table* symtab, Layout* layout,
591 Sized_relobj_file<32, false>* object,
592 unsigned int shndx, Output_section* output_section,
593 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
595 this->copy_relocs_.copy_reloc(symtab, layout,
596 symtab->get_sized_symbol<32>(sym),
597 object, shndx, output_section, reloc,
598 this->rel_dyn_section(layout));
601 // Information about this specific target which we pass to the
602 // general Target structure.
603 static const Target::Target_info i386_info;
605 // The types of GOT entries needed for this platform.
606 // These values are exposed to the ABI in an incremental link.
607 // Do not renumber existing values without changing the version
608 // number of the .gnu_incremental_inputs section.
611 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
612 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
613 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
614 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
615 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
619 Output_data_got<32, false>* got_;
621 Output_data_plt_i386* plt_;
622 // The GOT PLT section.
623 Output_data_space* got_plt_;
624 // The GOT section for TLSDESC relocations.
625 Output_data_got<32, false>* got_tlsdesc_;
626 // The _GLOBAL_OFFSET_TABLE_ symbol.
627 Symbol* global_offset_table_;
628 // The dynamic reloc section.
629 Reloc_section* rel_dyn_;
630 // Relocs saved to avoid a COPY reloc.
631 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
632 // Space for variables copied with a COPY reloc.
633 Output_data_space* dynbss_;
634 // Offset of the GOT entry for the TLS module index.
635 unsigned int got_mod_index_offset_;
636 // True if the _TLS_MODULE_BASE_ symbol has been defined.
637 bool tls_base_symbol_defined_;
640 const Target::Target_info Target_i386::i386_info =
643 false, // is_big_endian
644 elfcpp::EM_386, // machine_code
645 false, // has_make_symbol
646 false, // has_resolve
647 true, // has_code_fill
648 true, // is_default_stack_executable
649 true, // can_icf_inline_merge_sections
651 "/usr/lib/libc.so.1", // dynamic_linker
652 0x08048000, // default_text_segment_address
653 0x1000, // abi_pagesize (overridable by -z max-page-size)
654 0x1000, // common_pagesize (overridable by -z common-page-size)
655 elfcpp::SHN_UNDEF, // small_common_shndx
656 elfcpp::SHN_UNDEF, // large_common_shndx
657 0, // small_common_section_flags
658 0, // large_common_section_flags
659 NULL, // attributes_section
660 NULL // attributes_vendor
663 // Get the GOT section, creating it if necessary.
665 Output_data_got<32, false>*
666 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
668 if (this->got_ == NULL)
670 gold_assert(symtab != NULL && layout != NULL);
672 this->got_ = new Output_data_got<32, false>();
674 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
676 | elfcpp::SHF_WRITE),
677 this->got_, ORDER_RELRO_LAST, true);
679 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
680 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
682 | elfcpp::SHF_WRITE),
683 this->got_plt_, ORDER_NON_RELRO_FIRST,
686 // The first three entries are reserved.
687 this->got_plt_->set_current_data_size(3 * 4);
689 // Those bytes can go into the relro segment.
690 layout->increase_relro(3 * 4);
692 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
693 this->global_offset_table_ =
694 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
695 Symbol_table::PREDEFINED,
697 0, 0, elfcpp::STT_OBJECT,
699 elfcpp::STV_HIDDEN, 0,
702 // If there are any TLSDESC relocations, they get GOT entries in
703 // .got.plt after the jump slot entries.
704 this->got_tlsdesc_ = new Output_data_got<32, false>();
705 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
707 | elfcpp::SHF_WRITE),
709 ORDER_NON_RELRO_FIRST, false);
715 // Get the dynamic reloc section, creating it if necessary.
717 Target_i386::Reloc_section*
718 Target_i386::rel_dyn_section(Layout* layout)
720 if (this->rel_dyn_ == NULL)
722 gold_assert(layout != NULL);
723 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
724 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
725 elfcpp::SHF_ALLOC, this->rel_dyn_,
726 ORDER_DYNAMIC_RELOCS, false);
728 return this->rel_dyn_;
731 // Create the PLT section. The ordinary .got section is an argument,
732 // since we need to refer to the start. We also create our own .got
733 // section just for PLT entries.
735 Output_data_plt_i386::Output_data_plt_i386(Symbol_table* symtab,
737 Output_data_space* got_plt)
738 : Output_section_data(16), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0),
739 global_ifuncs_(), local_ifuncs_()
741 this->rel_ = new Reloc_section(false);
742 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
743 elfcpp::SHF_ALLOC, this->rel_,
744 ORDER_DYNAMIC_PLT_RELOCS, false);
746 if (parameters->doing_static_link())
748 // A statically linked executable will only have a .rel.plt
749 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
750 // symbols. The library will use these symbols to locate the
751 // IRELATIVE relocs at program startup time.
752 symtab->define_in_output_data("__rel_iplt_start", NULL,
753 Symbol_table::PREDEFINED,
754 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
755 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
757 symtab->define_in_output_data("__rel_iplt_end", NULL,
758 Symbol_table::PREDEFINED,
759 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
760 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
764 // Add unwind information if requested.
765 if (parameters->options().ld_generated_unwind_info())
766 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
767 plt_eh_frame_fde, plt_eh_frame_fde_size);
771 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
773 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
774 // linker, and so do we.
778 // Add an entry to the PLT.
781 Output_data_plt_i386::add_entry(Symbol* gsym)
783 gold_assert(!gsym->has_plt_offset());
785 // Note that when setting the PLT offset we skip the initial
786 // reserved PLT entry.
787 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
791 section_offset_type got_offset = this->got_plt_->current_data_size();
793 // Every PLT entry needs a GOT entry which points back to the PLT
794 // entry (this will be changed by the dynamic linker, normally
795 // lazily when the function is called).
796 this->got_plt_->set_current_data_size(got_offset + 4);
798 // Every PLT entry needs a reloc.
799 if (gsym->type() == elfcpp::STT_GNU_IFUNC
800 && gsym->can_use_relative_reloc(false))
802 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
803 this->got_plt_, got_offset);
804 struct Global_ifunc gi;
806 gi.got_offset = got_offset;
807 this->global_ifuncs_.push_back(gi);
811 gsym->set_needs_dynsym_entry();
812 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
816 // Note that we don't need to save the symbol. The contents of the
817 // PLT are independent of which symbols are used. The symbols only
818 // appear in the relocations.
821 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
825 Output_data_plt_i386::add_local_ifunc_entry(
826 Sized_relobj_file<32, false>* relobj,
827 unsigned int local_sym_index)
829 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
832 section_offset_type got_offset = this->got_plt_->current_data_size();
834 // Every PLT entry needs a GOT entry which points back to the PLT
836 this->got_plt_->set_current_data_size(got_offset + 4);
838 // Every PLT entry needs a reloc.
839 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
840 elfcpp::R_386_IRELATIVE,
841 this->got_plt_, got_offset);
843 struct Local_ifunc li;
845 li.local_sym_index = local_sym_index;
846 li.got_offset = got_offset;
847 this->local_ifuncs_.push_back(li);
852 // Return where the TLS_DESC relocations should go, creating it if
853 // necessary. These follow the JUMP_SLOT relocations.
855 Output_data_plt_i386::Reloc_section*
856 Output_data_plt_i386::rel_tls_desc(Layout* layout)
858 if (this->tls_desc_rel_ == NULL)
860 this->tls_desc_rel_ = new Reloc_section(false);
861 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
862 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
863 ORDER_DYNAMIC_PLT_RELOCS, false);
864 gold_assert(this->tls_desc_rel_->output_section() ==
865 this->rel_->output_section());
867 return this->tls_desc_rel_;
870 // The first entry in the PLT for an executable.
872 const unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
874 0xff, 0x35, // pushl contents of memory address
875 0, 0, 0, 0, // replaced with address of .got + 4
876 0xff, 0x25, // jmp indirect
877 0, 0, 0, 0, // replaced with address of .got + 8
881 // The first entry in the PLT for a shared object.
883 const unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
885 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
886 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
890 // Subsequent entries in the PLT for an executable.
892 const unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
894 0xff, 0x25, // jmp indirect
895 0, 0, 0, 0, // replaced with address of symbol in .got
896 0x68, // pushl immediate
897 0, 0, 0, 0, // replaced with offset into relocation table
898 0xe9, // jmp relative
899 0, 0, 0, 0 // replaced with offset to start of .plt
902 // Subsequent entries in the PLT for a shared object.
904 const unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
906 0xff, 0xa3, // jmp *offset(%ebx)
907 0, 0, 0, 0, // replaced with offset of symbol in .got
908 0x68, // pushl immediate
909 0, 0, 0, 0, // replaced with offset into relocation table
910 0xe9, // jmp relative
911 0, 0, 0, 0 // replaced with offset to start of .plt
914 // The .eh_frame unwind information for the PLT.
917 Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] =
920 'z', // Augmentation: augmentation size included.
921 'R', // Augmentation: FDE encoding included.
922 '\0', // End of augmentation string.
923 1, // Code alignment factor.
924 0x7c, // Data alignment factor.
925 8, // Return address column.
926 1, // Augmentation size.
927 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
928 | elfcpp::DW_EH_PE_sdata4),
929 elfcpp::DW_CFA_def_cfa, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
930 elfcpp::DW_CFA_offset + 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
931 elfcpp::DW_CFA_nop, // Align to 16 bytes.
936 Output_data_plt_i386::plt_eh_frame_fde[plt_eh_frame_fde_size] =
938 0, 0, 0, 0, // Replaced with offset to .plt.
939 0, 0, 0, 0, // Replaced with size of .plt.
940 0, // Augmentation size.
941 elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
942 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
943 elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
944 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
945 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
947 elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
948 elfcpp::DW_OP_breg8, 0, // Push %eip.
949 elfcpp::DW_OP_lit15, // Push 0xf.
950 elfcpp::DW_OP_and, // & (%eip & 0xf).
951 elfcpp::DW_OP_lit11, // Push 0xb.
952 elfcpp::DW_OP_ge, // >= ((%eip & 0xf) >= 0xb)
953 elfcpp::DW_OP_lit2, // Push 2.
954 elfcpp::DW_OP_shl, // << (((%eip & 0xf) >= 0xb) << 2)
955 elfcpp::DW_OP_plus, // + ((((%eip&0xf)>=0xb)<<2)+%esp+8
956 elfcpp::DW_CFA_nop, // Align to 32 bytes.
962 // Write out the PLT. This uses the hand-coded instructions above,
963 // and adjusts them as needed. This is all specified by the i386 ELF
964 // Processor Supplement.
967 Output_data_plt_i386::do_write(Output_file* of)
969 const off_t offset = this->offset();
970 const section_size_type oview_size =
971 convert_to_section_size_type(this->data_size());
972 unsigned char* const oview = of->get_output_view(offset, oview_size);
974 const off_t got_file_offset = this->got_plt_->offset();
975 const section_size_type got_size =
976 convert_to_section_size_type(this->got_plt_->data_size());
977 unsigned char* const got_view = of->get_output_view(got_file_offset,
980 unsigned char* pov = oview;
982 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
983 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
985 if (parameters->options().output_is_position_independent())
986 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
989 memcpy(pov, exec_first_plt_entry, plt_entry_size);
990 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
991 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
993 pov += plt_entry_size;
995 unsigned char* got_pov = got_view;
997 memset(got_pov, 0, 12);
1000 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1002 unsigned int plt_offset = plt_entry_size;
1003 unsigned int plt_rel_offset = 0;
1004 unsigned int got_offset = 12;
1005 const unsigned int count = this->count_;
1006 for (unsigned int i = 0;
1009 pov += plt_entry_size,
1011 plt_offset += plt_entry_size,
1012 plt_rel_offset += rel_size,
1015 // Set and adjust the PLT entry itself.
1017 if (parameters->options().output_is_position_independent())
1019 memcpy(pov, dyn_plt_entry, plt_entry_size);
1020 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
1024 memcpy(pov, exec_plt_entry, plt_entry_size);
1025 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1030 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1031 elfcpp::Swap<32, false>::writeval(pov + 12,
1032 - (plt_offset + plt_entry_size));
1034 // Set the entry in the GOT.
1035 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
1038 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
1039 // the GOT to point to the actual symbol value, rather than point to
1040 // the PLT entry. That will let the dynamic linker call the right
1041 // function when resolving IRELATIVE relocations.
1042 for (std::vector<Global_ifunc>::const_iterator p =
1043 this->global_ifuncs_.begin();
1044 p != this->global_ifuncs_.end();
1047 const Sized_symbol<32>* ssym =
1048 static_cast<const Sized_symbol<32>*>(p->sym);
1049 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
1053 for (std::vector<Local_ifunc>::const_iterator p =
1054 this->local_ifuncs_.begin();
1055 p != this->local_ifuncs_.end();
1058 const Symbol_value<32>* psymval =
1059 p->object->local_symbol(p->local_sym_index);
1060 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
1061 psymval->value(p->object, 0));
1064 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1065 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1067 of->write_output_view(offset, oview_size, oview);
1068 of->write_output_view(got_file_offset, got_size, got_view);
1071 // Create the PLT section.
1074 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1076 if (this->plt_ == NULL)
1078 // Create the GOT sections first.
1079 this->got_section(symtab, layout);
1081 this->plt_ = new Output_data_plt_i386(symtab, layout, this->got_plt_);
1082 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1084 | elfcpp::SHF_EXECINSTR),
1085 this->plt_, ORDER_PLT, false);
1087 // Make the sh_info field of .rel.plt point to .plt.
1088 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1089 rel_plt_os->set_info_section(this->plt_->output_section());
1093 // Create a PLT entry for a global symbol.
1096 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1098 if (gsym->has_plt_offset())
1100 if (this->plt_ == NULL)
1101 this->make_plt_section(symtab, layout);
1102 this->plt_->add_entry(gsym);
1105 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1108 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1109 Sized_relobj_file<32, false>* relobj,
1110 unsigned int local_sym_index)
1112 if (relobj->local_has_plt_offset(local_sym_index))
1114 if (this->plt_ == NULL)
1115 this->make_plt_section(symtab, layout);
1116 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1118 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1121 // Return the number of entries in the PLT.
1124 Target_i386::plt_entry_count() const
1126 if (this->plt_ == NULL)
1128 return this->plt_->entry_count();
1131 // Return the offset of the first non-reserved PLT entry.
1134 Target_i386::first_plt_entry_offset() const
1136 return Output_data_plt_i386::first_plt_entry_offset();
1139 // Return the size of each PLT entry.
1142 Target_i386::plt_entry_size() const
1144 return Output_data_plt_i386::get_plt_entry_size();
1147 // Get the section to use for TLS_DESC relocations.
1149 Target_i386::Reloc_section*
1150 Target_i386::rel_tls_desc_section(Layout* layout) const
1152 return this->plt_section()->rel_tls_desc(layout);
1155 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1158 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1160 if (this->tls_base_symbol_defined_)
1163 Output_segment* tls_segment = layout->tls_segment();
1164 if (tls_segment != NULL)
1166 bool is_exec = parameters->options().output_is_executable();
1167 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1168 Symbol_table::PREDEFINED,
1172 elfcpp::STV_HIDDEN, 0,
1174 ? Symbol::SEGMENT_END
1175 : Symbol::SEGMENT_START),
1178 this->tls_base_symbol_defined_ = true;
1181 // Create a GOT entry for the TLS module index.
1184 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1185 Sized_relobj_file<32, false>* object)
1187 if (this->got_mod_index_offset_ == -1U)
1189 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1190 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1191 Output_data_got<32, false>* got = this->got_section(symtab, layout);
1192 unsigned int got_offset = got->add_constant(0);
1193 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1195 got->add_constant(0);
1196 this->got_mod_index_offset_ = got_offset;
1198 return this->got_mod_index_offset_;
1201 // Optimize the TLS relocation type based on what we know about the
1202 // symbol. IS_FINAL is true if the final address of this symbol is
1203 // known at link time.
1205 tls::Tls_optimization
1206 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1208 // If we are generating a shared library, then we can't do anything
1210 if (parameters->options().shared())
1211 return tls::TLSOPT_NONE;
1215 case elfcpp::R_386_TLS_GD:
1216 case elfcpp::R_386_TLS_GOTDESC:
1217 case elfcpp::R_386_TLS_DESC_CALL:
1218 // These are General-Dynamic which permits fully general TLS
1219 // access. Since we know that we are generating an executable,
1220 // we can convert this to Initial-Exec. If we also know that
1221 // this is a local symbol, we can further switch to Local-Exec.
1223 return tls::TLSOPT_TO_LE;
1224 return tls::TLSOPT_TO_IE;
1226 case elfcpp::R_386_TLS_LDM:
1227 // This is Local-Dynamic, which refers to a local symbol in the
1228 // dynamic TLS block. Since we know that we generating an
1229 // executable, we can switch to Local-Exec.
1230 return tls::TLSOPT_TO_LE;
1232 case elfcpp::R_386_TLS_LDO_32:
1233 // Another type of Local-Dynamic relocation.
1234 return tls::TLSOPT_TO_LE;
1236 case elfcpp::R_386_TLS_IE:
1237 case elfcpp::R_386_TLS_GOTIE:
1238 case elfcpp::R_386_TLS_IE_32:
1239 // These are Initial-Exec relocs which get the thread offset
1240 // from the GOT. If we know that we are linking against the
1241 // local symbol, we can switch to Local-Exec, which links the
1242 // thread offset into the instruction.
1244 return tls::TLSOPT_TO_LE;
1245 return tls::TLSOPT_NONE;
1247 case elfcpp::R_386_TLS_LE:
1248 case elfcpp::R_386_TLS_LE_32:
1249 // When we already have Local-Exec, there is nothing further we
1251 return tls::TLSOPT_NONE;
1258 // Get the Reference_flags for a particular relocation.
1261 Target_i386::Scan::get_reference_flags(unsigned int r_type)
1265 case elfcpp::R_386_NONE:
1266 case elfcpp::R_386_GNU_VTINHERIT:
1267 case elfcpp::R_386_GNU_VTENTRY:
1268 case elfcpp::R_386_GOTPC:
1269 // No symbol reference.
1272 case elfcpp::R_386_32:
1273 case elfcpp::R_386_16:
1274 case elfcpp::R_386_8:
1275 return Symbol::ABSOLUTE_REF;
1277 case elfcpp::R_386_PC32:
1278 case elfcpp::R_386_PC16:
1279 case elfcpp::R_386_PC8:
1280 case elfcpp::R_386_GOTOFF:
1281 return Symbol::RELATIVE_REF;
1283 case elfcpp::R_386_PLT32:
1284 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1286 case elfcpp::R_386_GOT32:
1288 return Symbol::ABSOLUTE_REF;
1290 case elfcpp::R_386_TLS_GD: // Global-dynamic
1291 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1292 case elfcpp::R_386_TLS_DESC_CALL:
1293 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1294 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1295 case elfcpp::R_386_TLS_IE: // Initial-exec
1296 case elfcpp::R_386_TLS_IE_32:
1297 case elfcpp::R_386_TLS_GOTIE:
1298 case elfcpp::R_386_TLS_LE: // Local-exec
1299 case elfcpp::R_386_TLS_LE_32:
1300 return Symbol::TLS_REF;
1302 case elfcpp::R_386_COPY:
1303 case elfcpp::R_386_GLOB_DAT:
1304 case elfcpp::R_386_JUMP_SLOT:
1305 case elfcpp::R_386_RELATIVE:
1306 case elfcpp::R_386_IRELATIVE:
1307 case elfcpp::R_386_TLS_TPOFF:
1308 case elfcpp::R_386_TLS_DTPMOD32:
1309 case elfcpp::R_386_TLS_DTPOFF32:
1310 case elfcpp::R_386_TLS_TPOFF32:
1311 case elfcpp::R_386_TLS_DESC:
1312 case elfcpp::R_386_32PLT:
1313 case elfcpp::R_386_TLS_GD_32:
1314 case elfcpp::R_386_TLS_GD_PUSH:
1315 case elfcpp::R_386_TLS_GD_CALL:
1316 case elfcpp::R_386_TLS_GD_POP:
1317 case elfcpp::R_386_TLS_LDM_32:
1318 case elfcpp::R_386_TLS_LDM_PUSH:
1319 case elfcpp::R_386_TLS_LDM_CALL:
1320 case elfcpp::R_386_TLS_LDM_POP:
1321 case elfcpp::R_386_USED_BY_INTEL_200:
1323 // Not expected. We will give an error later.
1328 // Report an unsupported relocation against a local symbol.
1331 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
1332 unsigned int r_type)
1334 gold_error(_("%s: unsupported reloc %u against local symbol"),
1335 object->name().c_str(), r_type);
1338 // Return whether we need to make a PLT entry for a relocation of a
1339 // given type against a STT_GNU_IFUNC symbol.
1342 Target_i386::Scan::reloc_needs_plt_for_ifunc(
1343 Sized_relobj_file<32, false>* object,
1344 unsigned int r_type)
1346 int flags = Scan::get_reference_flags(r_type);
1347 if (flags & Symbol::TLS_REF)
1348 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1349 object->name().c_str(), r_type);
1353 // Scan a relocation for a local symbol.
1356 Target_i386::Scan::local(Symbol_table* symtab,
1358 Target_i386* target,
1359 Sized_relobj_file<32, false>* object,
1360 unsigned int data_shndx,
1361 Output_section* output_section,
1362 const elfcpp::Rel<32, false>& reloc,
1363 unsigned int r_type,
1364 const elfcpp::Sym<32, false>& lsym)
1366 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1367 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1368 && this->reloc_needs_plt_for_ifunc(object, r_type))
1370 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1371 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1376 case elfcpp::R_386_NONE:
1377 case elfcpp::R_386_GNU_VTINHERIT:
1378 case elfcpp::R_386_GNU_VTENTRY:
1381 case elfcpp::R_386_32:
1382 // If building a shared library (or a position-independent
1383 // executable), we need to create a dynamic relocation for
1384 // this location. The relocation applied at link time will
1385 // apply the link-time value, so we flag the location with
1386 // an R_386_RELATIVE relocation so the dynamic loader can
1387 // relocate it easily.
1388 if (parameters->options().output_is_position_independent())
1390 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1391 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1392 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1393 output_section, data_shndx,
1394 reloc.get_r_offset());
1398 case elfcpp::R_386_16:
1399 case elfcpp::R_386_8:
1400 // If building a shared library (or a position-independent
1401 // executable), we need to create a dynamic relocation for
1402 // this location. Because the addend needs to remain in the
1403 // data section, we need to be careful not to apply this
1404 // relocation statically.
1405 if (parameters->options().output_is_position_independent())
1407 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1408 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1409 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1410 rel_dyn->add_local(object, r_sym, r_type, output_section,
1411 data_shndx, reloc.get_r_offset());
1414 gold_assert(lsym.get_st_value() == 0);
1415 unsigned int shndx = lsym.get_st_shndx();
1417 shndx = object->adjust_sym_shndx(r_sym, shndx,
1420 object->error(_("section symbol %u has bad shndx %u"),
1423 rel_dyn->add_local_section(object, shndx,
1424 r_type, output_section,
1425 data_shndx, reloc.get_r_offset());
1430 case elfcpp::R_386_PC32:
1431 case elfcpp::R_386_PC16:
1432 case elfcpp::R_386_PC8:
1435 case elfcpp::R_386_PLT32:
1436 // Since we know this is a local symbol, we can handle this as a
1440 case elfcpp::R_386_GOTOFF:
1441 case elfcpp::R_386_GOTPC:
1442 // We need a GOT section.
1443 target->got_section(symtab, layout);
1446 case elfcpp::R_386_GOT32:
1448 // The symbol requires a GOT entry.
1449 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1450 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1452 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1453 // lets function pointers compare correctly with shared
1454 // libraries. Otherwise we would need an IRELATIVE reloc.
1456 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1457 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1459 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1462 // If we are generating a shared object, we need to add a
1463 // dynamic RELATIVE relocation for this symbol's GOT entry.
1464 if (parameters->options().output_is_position_independent())
1466 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1467 unsigned int got_offset =
1468 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1469 rel_dyn->add_local_relative(object, r_sym,
1470 elfcpp::R_386_RELATIVE,
1477 // These are relocations which should only be seen by the
1478 // dynamic linker, and should never be seen here.
1479 case elfcpp::R_386_COPY:
1480 case elfcpp::R_386_GLOB_DAT:
1481 case elfcpp::R_386_JUMP_SLOT:
1482 case elfcpp::R_386_RELATIVE:
1483 case elfcpp::R_386_IRELATIVE:
1484 case elfcpp::R_386_TLS_TPOFF:
1485 case elfcpp::R_386_TLS_DTPMOD32:
1486 case elfcpp::R_386_TLS_DTPOFF32:
1487 case elfcpp::R_386_TLS_TPOFF32:
1488 case elfcpp::R_386_TLS_DESC:
1489 gold_error(_("%s: unexpected reloc %u in object file"),
1490 object->name().c_str(), r_type);
1493 // These are initial TLS relocs, which are expected when
1495 case elfcpp::R_386_TLS_GD: // Global-dynamic
1496 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1497 case elfcpp::R_386_TLS_DESC_CALL:
1498 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1499 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1500 case elfcpp::R_386_TLS_IE: // Initial-exec
1501 case elfcpp::R_386_TLS_IE_32:
1502 case elfcpp::R_386_TLS_GOTIE:
1503 case elfcpp::R_386_TLS_LE: // Local-exec
1504 case elfcpp::R_386_TLS_LE_32:
1506 bool output_is_shared = parameters->options().shared();
1507 const tls::Tls_optimization optimized_type
1508 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1511 case elfcpp::R_386_TLS_GD: // Global-dynamic
1512 if (optimized_type == tls::TLSOPT_NONE)
1514 // Create a pair of GOT entries for the module index and
1515 // dtv-relative offset.
1516 Output_data_got<32, false>* got
1517 = target->got_section(symtab, layout);
1518 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1519 unsigned int shndx = lsym.get_st_shndx();
1521 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1523 object->error(_("local symbol %u has bad shndx %u"),
1526 got->add_local_pair_with_rel(object, r_sym, shndx,
1528 target->rel_dyn_section(layout),
1529 elfcpp::R_386_TLS_DTPMOD32, 0);
1531 else if (optimized_type != tls::TLSOPT_TO_LE)
1532 unsupported_reloc_local(object, r_type);
1535 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1536 target->define_tls_base_symbol(symtab, layout);
1537 if (optimized_type == tls::TLSOPT_NONE)
1539 // Create a double GOT entry with an R_386_TLS_DESC
1540 // reloc. The R_386_TLS_DESC reloc is resolved
1541 // lazily, so the GOT entry needs to be in an area in
1542 // .got.plt, not .got. Call got_section to make sure
1543 // the section has been created.
1544 target->got_section(symtab, layout);
1545 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1546 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1547 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1549 unsigned int got_offset = got->add_constant(0);
1550 // The local symbol value is stored in the second
1552 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1553 // That set the GOT offset of the local symbol to
1554 // point to the second entry, but we want it to
1555 // point to the first.
1556 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1558 Reloc_section* rt = target->rel_tls_desc_section(layout);
1559 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1562 else if (optimized_type != tls::TLSOPT_TO_LE)
1563 unsupported_reloc_local(object, r_type);
1566 case elfcpp::R_386_TLS_DESC_CALL:
1569 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1570 if (optimized_type == tls::TLSOPT_NONE)
1572 // Create a GOT entry for the module index.
1573 target->got_mod_index_entry(symtab, layout, object);
1575 else if (optimized_type != tls::TLSOPT_TO_LE)
1576 unsupported_reloc_local(object, r_type);
1579 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1582 case elfcpp::R_386_TLS_IE: // Initial-exec
1583 case elfcpp::R_386_TLS_IE_32:
1584 case elfcpp::R_386_TLS_GOTIE:
1585 layout->set_has_static_tls();
1586 if (optimized_type == tls::TLSOPT_NONE)
1588 // For the R_386_TLS_IE relocation, we need to create a
1589 // dynamic relocation when building a shared library.
1590 if (r_type == elfcpp::R_386_TLS_IE
1591 && parameters->options().shared())
1593 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1595 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1596 rel_dyn->add_local_relative(object, r_sym,
1597 elfcpp::R_386_RELATIVE,
1598 output_section, data_shndx,
1599 reloc.get_r_offset());
1601 // Create a GOT entry for the tp-relative offset.
1602 Output_data_got<32, false>* got
1603 = target->got_section(symtab, layout);
1604 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1605 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1606 ? elfcpp::R_386_TLS_TPOFF32
1607 : elfcpp::R_386_TLS_TPOFF);
1608 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1609 ? GOT_TYPE_TLS_OFFSET
1610 : GOT_TYPE_TLS_NOFFSET);
1611 got->add_local_with_rel(object, r_sym, got_type,
1612 target->rel_dyn_section(layout),
1615 else if (optimized_type != tls::TLSOPT_TO_LE)
1616 unsupported_reloc_local(object, r_type);
1619 case elfcpp::R_386_TLS_LE: // Local-exec
1620 case elfcpp::R_386_TLS_LE_32:
1621 layout->set_has_static_tls();
1622 if (output_is_shared)
1624 // We need to create a dynamic relocation.
1625 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1626 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1627 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1628 ? elfcpp::R_386_TLS_TPOFF32
1629 : elfcpp::R_386_TLS_TPOFF);
1630 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1631 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1632 data_shndx, reloc.get_r_offset());
1642 case elfcpp::R_386_32PLT:
1643 case elfcpp::R_386_TLS_GD_32:
1644 case elfcpp::R_386_TLS_GD_PUSH:
1645 case elfcpp::R_386_TLS_GD_CALL:
1646 case elfcpp::R_386_TLS_GD_POP:
1647 case elfcpp::R_386_TLS_LDM_32:
1648 case elfcpp::R_386_TLS_LDM_PUSH:
1649 case elfcpp::R_386_TLS_LDM_CALL:
1650 case elfcpp::R_386_TLS_LDM_POP:
1651 case elfcpp::R_386_USED_BY_INTEL_200:
1653 unsupported_reloc_local(object, r_type);
1658 // Report an unsupported relocation against a global symbol.
1661 Target_i386::Scan::unsupported_reloc_global(
1662 Sized_relobj_file<32, false>* object,
1663 unsigned int r_type,
1666 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1667 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1671 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
1675 case elfcpp::R_386_32:
1676 case elfcpp::R_386_16:
1677 case elfcpp::R_386_8:
1678 case elfcpp::R_386_GOTOFF:
1679 case elfcpp::R_386_GOT32:
1690 Target_i386::Scan::local_reloc_may_be_function_pointer(
1694 Sized_relobj_file<32, false>* ,
1697 const elfcpp::Rel<32, false>& ,
1698 unsigned int r_type,
1699 const elfcpp::Sym<32, false>&)
1701 return possible_function_pointer_reloc(r_type);
1705 Target_i386::Scan::global_reloc_may_be_function_pointer(
1709 Sized_relobj_file<32, false>* ,
1712 const elfcpp::Rel<32, false>& ,
1713 unsigned int r_type,
1716 return possible_function_pointer_reloc(r_type);
1719 // Scan a relocation for a global symbol.
1722 Target_i386::Scan::global(Symbol_table* symtab,
1724 Target_i386* target,
1725 Sized_relobj_file<32, false>* object,
1726 unsigned int data_shndx,
1727 Output_section* output_section,
1728 const elfcpp::Rel<32, false>& reloc,
1729 unsigned int r_type,
1732 // A STT_GNU_IFUNC symbol may require a PLT entry.
1733 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1734 && this->reloc_needs_plt_for_ifunc(object, r_type))
1735 target->make_plt_entry(symtab, layout, gsym);
1739 case elfcpp::R_386_NONE:
1740 case elfcpp::R_386_GNU_VTINHERIT:
1741 case elfcpp::R_386_GNU_VTENTRY:
1744 case elfcpp::R_386_32:
1745 case elfcpp::R_386_16:
1746 case elfcpp::R_386_8:
1748 // Make a PLT entry if necessary.
1749 if (gsym->needs_plt_entry())
1751 target->make_plt_entry(symtab, layout, gsym);
1752 // Since this is not a PC-relative relocation, we may be
1753 // taking the address of a function. In that case we need to
1754 // set the entry in the dynamic symbol table to the address of
1756 if (gsym->is_from_dynobj() && !parameters->options().shared())
1757 gsym->set_needs_dynsym_value();
1759 // Make a dynamic relocation if necessary.
1760 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1762 if (gsym->may_need_copy_reloc())
1764 target->copy_reloc(symtab, layout, object,
1765 data_shndx, output_section, gsym, reloc);
1767 else if (r_type == elfcpp::R_386_32
1768 && gsym->type() == elfcpp::STT_GNU_IFUNC
1769 && gsym->can_use_relative_reloc(false)
1770 && !gsym->is_from_dynobj()
1771 && !gsym->is_undefined()
1772 && !gsym->is_preemptible())
1774 // Use an IRELATIVE reloc for a locally defined
1775 // STT_GNU_IFUNC symbol. This makes a function
1776 // address in a PIE executable match the address in a
1777 // shared library that it links against.
1778 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1779 rel_dyn->add_symbolless_global_addend(gsym,
1780 elfcpp::R_386_IRELATIVE,
1783 reloc.get_r_offset());
1785 else if (r_type == elfcpp::R_386_32
1786 && gsym->can_use_relative_reloc(false))
1788 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1789 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1790 output_section, object,
1791 data_shndx, reloc.get_r_offset());
1795 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1796 rel_dyn->add_global(gsym, r_type, output_section, object,
1797 data_shndx, reloc.get_r_offset());
1803 case elfcpp::R_386_PC32:
1804 case elfcpp::R_386_PC16:
1805 case elfcpp::R_386_PC8:
1807 // Make a PLT entry if necessary.
1808 if (gsym->needs_plt_entry())
1810 // These relocations are used for function calls only in
1811 // non-PIC code. For a 32-bit relocation in a shared library,
1812 // we'll need a text relocation anyway, so we can skip the
1813 // PLT entry and let the dynamic linker bind the call directly
1814 // to the target. For smaller relocations, we should use a
1815 // PLT entry to ensure that the call can reach.
1816 if (!parameters->options().shared()
1817 || r_type != elfcpp::R_386_PC32)
1818 target->make_plt_entry(symtab, layout, gsym);
1820 // Make a dynamic relocation if necessary.
1821 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1823 if (gsym->may_need_copy_reloc())
1825 target->copy_reloc(symtab, layout, object,
1826 data_shndx, output_section, gsym, reloc);
1830 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1831 rel_dyn->add_global(gsym, r_type, output_section, object,
1832 data_shndx, reloc.get_r_offset());
1838 case elfcpp::R_386_GOT32:
1840 // The symbol requires a GOT entry.
1841 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1842 if (gsym->final_value_is_known())
1844 // For a STT_GNU_IFUNC symbol we want the PLT address.
1845 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1846 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1848 got->add_global(gsym, GOT_TYPE_STANDARD);
1852 // If this symbol is not fully resolved, we need to add a
1853 // GOT entry with a dynamic relocation.
1854 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1855 if (gsym->is_from_dynobj()
1856 || gsym->is_undefined()
1857 || gsym->is_preemptible()
1858 || (gsym->type() == elfcpp::STT_GNU_IFUNC
1859 && parameters->options().output_is_position_independent()))
1860 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1861 rel_dyn, elfcpp::R_386_GLOB_DAT);
1864 // For a STT_GNU_IFUNC symbol we want to write the PLT
1865 // offset into the GOT, so that function pointer
1866 // comparisons work correctly.
1868 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
1869 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
1872 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1873 // Tell the dynamic linker to use the PLT address
1874 // when resolving relocations.
1875 if (gsym->is_from_dynobj()
1876 && !parameters->options().shared())
1877 gsym->set_needs_dynsym_value();
1881 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
1882 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1890 case elfcpp::R_386_PLT32:
1891 // If the symbol is fully resolved, this is just a PC32 reloc.
1892 // Otherwise we need a PLT entry.
1893 if (gsym->final_value_is_known())
1895 // If building a shared library, we can also skip the PLT entry
1896 // if the symbol is defined in the output file and is protected
1898 if (gsym->is_defined()
1899 && !gsym->is_from_dynobj()
1900 && !gsym->is_preemptible())
1902 target->make_plt_entry(symtab, layout, gsym);
1905 case elfcpp::R_386_GOTOFF:
1906 case elfcpp::R_386_GOTPC:
1907 // We need a GOT section.
1908 target->got_section(symtab, layout);
1911 // These are relocations which should only be seen by the
1912 // dynamic linker, and should never be seen here.
1913 case elfcpp::R_386_COPY:
1914 case elfcpp::R_386_GLOB_DAT:
1915 case elfcpp::R_386_JUMP_SLOT:
1916 case elfcpp::R_386_RELATIVE:
1917 case elfcpp::R_386_IRELATIVE:
1918 case elfcpp::R_386_TLS_TPOFF:
1919 case elfcpp::R_386_TLS_DTPMOD32:
1920 case elfcpp::R_386_TLS_DTPOFF32:
1921 case elfcpp::R_386_TLS_TPOFF32:
1922 case elfcpp::R_386_TLS_DESC:
1923 gold_error(_("%s: unexpected reloc %u in object file"),
1924 object->name().c_str(), r_type);
1927 // These are initial tls relocs, which are expected when
1929 case elfcpp::R_386_TLS_GD: // Global-dynamic
1930 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1931 case elfcpp::R_386_TLS_DESC_CALL:
1932 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1933 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1934 case elfcpp::R_386_TLS_IE: // Initial-exec
1935 case elfcpp::R_386_TLS_IE_32:
1936 case elfcpp::R_386_TLS_GOTIE:
1937 case elfcpp::R_386_TLS_LE: // Local-exec
1938 case elfcpp::R_386_TLS_LE_32:
1940 const bool is_final = gsym->final_value_is_known();
1941 const tls::Tls_optimization optimized_type
1942 = Target_i386::optimize_tls_reloc(is_final, r_type);
1945 case elfcpp::R_386_TLS_GD: // Global-dynamic
1946 if (optimized_type == tls::TLSOPT_NONE)
1948 // Create a pair of GOT entries for the module index and
1949 // dtv-relative offset.
1950 Output_data_got<32, false>* got
1951 = target->got_section(symtab, layout);
1952 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1953 target->rel_dyn_section(layout),
1954 elfcpp::R_386_TLS_DTPMOD32,
1955 elfcpp::R_386_TLS_DTPOFF32);
1957 else if (optimized_type == tls::TLSOPT_TO_IE)
1959 // Create a GOT entry for the tp-relative offset.
1960 Output_data_got<32, false>* got
1961 = target->got_section(symtab, layout);
1962 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1963 target->rel_dyn_section(layout),
1964 elfcpp::R_386_TLS_TPOFF);
1966 else if (optimized_type != tls::TLSOPT_TO_LE)
1967 unsupported_reloc_global(object, r_type, gsym);
1970 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1971 target->define_tls_base_symbol(symtab, layout);
1972 if (optimized_type == tls::TLSOPT_NONE)
1974 // Create a double GOT entry with an R_386_TLS_DESC
1975 // reloc. The R_386_TLS_DESC reloc is resolved
1976 // lazily, so the GOT entry needs to be in an area in
1977 // .got.plt, not .got. Call got_section to make sure
1978 // the section has been created.
1979 target->got_section(symtab, layout);
1980 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1981 Reloc_section* rt = target->rel_tls_desc_section(layout);
1982 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
1983 elfcpp::R_386_TLS_DESC, 0);
1985 else if (optimized_type == tls::TLSOPT_TO_IE)
1987 // Create a GOT entry for the tp-relative offset.
1988 Output_data_got<32, false>* got
1989 = target->got_section(symtab, layout);
1990 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1991 target->rel_dyn_section(layout),
1992 elfcpp::R_386_TLS_TPOFF);
1994 else if (optimized_type != tls::TLSOPT_TO_LE)
1995 unsupported_reloc_global(object, r_type, gsym);
1998 case elfcpp::R_386_TLS_DESC_CALL:
2001 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2002 if (optimized_type == tls::TLSOPT_NONE)
2004 // Create a GOT entry for the module index.
2005 target->got_mod_index_entry(symtab, layout, object);
2007 else if (optimized_type != tls::TLSOPT_TO_LE)
2008 unsupported_reloc_global(object, r_type, gsym);
2011 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2014 case elfcpp::R_386_TLS_IE: // Initial-exec
2015 case elfcpp::R_386_TLS_IE_32:
2016 case elfcpp::R_386_TLS_GOTIE:
2017 layout->set_has_static_tls();
2018 if (optimized_type == tls::TLSOPT_NONE)
2020 // For the R_386_TLS_IE relocation, we need to create a
2021 // dynamic relocation when building a shared library.
2022 if (r_type == elfcpp::R_386_TLS_IE
2023 && parameters->options().shared())
2025 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2026 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2027 output_section, object,
2029 reloc.get_r_offset());
2031 // Create a GOT entry for the tp-relative offset.
2032 Output_data_got<32, false>* got
2033 = target->got_section(symtab, layout);
2034 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
2035 ? elfcpp::R_386_TLS_TPOFF32
2036 : elfcpp::R_386_TLS_TPOFF);
2037 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2038 ? GOT_TYPE_TLS_OFFSET
2039 : GOT_TYPE_TLS_NOFFSET);
2040 got->add_global_with_rel(gsym, got_type,
2041 target->rel_dyn_section(layout),
2044 else if (optimized_type != tls::TLSOPT_TO_LE)
2045 unsupported_reloc_global(object, r_type, gsym);
2048 case elfcpp::R_386_TLS_LE: // Local-exec
2049 case elfcpp::R_386_TLS_LE_32:
2050 layout->set_has_static_tls();
2051 if (parameters->options().shared())
2053 // We need to create a dynamic relocation.
2054 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
2055 ? elfcpp::R_386_TLS_TPOFF32
2056 : elfcpp::R_386_TLS_TPOFF);
2057 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2058 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
2059 data_shndx, reloc.get_r_offset());
2069 case elfcpp::R_386_32PLT:
2070 case elfcpp::R_386_TLS_GD_32:
2071 case elfcpp::R_386_TLS_GD_PUSH:
2072 case elfcpp::R_386_TLS_GD_CALL:
2073 case elfcpp::R_386_TLS_GD_POP:
2074 case elfcpp::R_386_TLS_LDM_32:
2075 case elfcpp::R_386_TLS_LDM_PUSH:
2076 case elfcpp::R_386_TLS_LDM_CALL:
2077 case elfcpp::R_386_TLS_LDM_POP:
2078 case elfcpp::R_386_USED_BY_INTEL_200:
2080 unsupported_reloc_global(object, r_type, gsym);
2085 // Process relocations for gc.
2088 Target_i386::gc_process_relocs(Symbol_table* symtab,
2090 Sized_relobj_file<32, false>* object,
2091 unsigned int data_shndx,
2093 const unsigned char* prelocs,
2095 Output_section* output_section,
2096 bool needs_special_offset_handling,
2097 size_t local_symbol_count,
2098 const unsigned char* plocal_symbols)
2100 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2102 Target_i386::Relocatable_size_for_reloc>(
2111 needs_special_offset_handling,
2116 // Scan relocations for a section.
2119 Target_i386::scan_relocs(Symbol_table* symtab,
2121 Sized_relobj_file<32, false>* object,
2122 unsigned int data_shndx,
2123 unsigned int sh_type,
2124 const unsigned char* prelocs,
2126 Output_section* output_section,
2127 bool needs_special_offset_handling,
2128 size_t local_symbol_count,
2129 const unsigned char* plocal_symbols)
2131 if (sh_type == elfcpp::SHT_RELA)
2133 gold_error(_("%s: unsupported RELA reloc section"),
2134 object->name().c_str());
2138 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2148 needs_special_offset_handling,
2153 // Finalize the sections.
2156 Target_i386::do_finalize_sections(
2158 const Input_objects*,
2159 Symbol_table* symtab)
2161 const Reloc_section* rel_plt = (this->plt_ == NULL
2163 : this->plt_->rel_plt());
2164 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2165 this->rel_dyn_, true, false);
2167 // Emit any relocs we saved in an attempt to avoid generating COPY
2169 if (this->copy_relocs_.any_saved_relocs())
2170 this->copy_relocs_.emit(this->rel_dyn_section(layout));
2172 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2173 // the .got.plt section.
2174 Symbol* sym = this->global_offset_table_;
2177 uint32_t data_size = this->got_plt_->current_data_size();
2178 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2182 // Return whether a direct absolute static relocation needs to be applied.
2183 // In cases where Scan::local() or Scan::global() has created
2184 // a dynamic relocation other than R_386_RELATIVE, the addend
2185 // of the relocation is carried in the data, and we must not
2186 // apply the static relocation.
2189 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2190 unsigned int r_type,
2192 Output_section* output_section)
2194 // If the output section is not allocated, then we didn't call
2195 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2197 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2200 int ref_flags = Scan::get_reference_flags(r_type);
2202 // For local symbols, we will have created a non-RELATIVE dynamic
2203 // relocation only if (a) the output is position independent,
2204 // (b) the relocation is absolute (not pc- or segment-relative), and
2205 // (c) the relocation is not 32 bits wide.
2207 return !(parameters->options().output_is_position_independent()
2208 && (ref_flags & Symbol::ABSOLUTE_REF)
2211 // For global symbols, we use the same helper routines used in the
2212 // scan pass. If we did not create a dynamic relocation, or if we
2213 // created a RELATIVE dynamic relocation, we should apply the static
2215 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2216 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2217 && gsym->can_use_relative_reloc(ref_flags
2218 & Symbol::FUNCTION_CALL);
2219 return !has_dyn || is_rel;
2222 // Perform a relocation.
2225 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2226 Target_i386* target,
2227 Output_section* output_section,
2229 const elfcpp::Rel<32, false>& rel,
2230 unsigned int r_type,
2231 const Sized_symbol<32>* gsym,
2232 const Symbol_value<32>* psymval,
2233 unsigned char* view,
2234 elfcpp::Elf_types<32>::Elf_Addr address,
2235 section_size_type view_size)
2237 if (this->skip_call_tls_get_addr_)
2239 if ((r_type != elfcpp::R_386_PLT32
2240 && r_type != elfcpp::R_386_PC32)
2242 || strcmp(gsym->name(), "___tls_get_addr") != 0)
2243 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2244 _("missing expected TLS relocation"));
2247 this->skip_call_tls_get_addr_ = false;
2252 const Sized_relobj_file<32, false>* object = relinfo->object;
2254 // Pick the value to use for symbols defined in shared objects.
2255 Symbol_value<32> symval;
2257 && gsym->type() == elfcpp::STT_GNU_IFUNC
2258 && r_type == elfcpp::R_386_32
2259 && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
2260 && gsym->can_use_relative_reloc(false)
2261 && !gsym->is_from_dynobj()
2262 && !gsym->is_undefined()
2263 && !gsym->is_preemptible())
2265 // In this case we are generating a R_386_IRELATIVE reloc. We
2266 // want to use the real value of the symbol, not the PLT offset.
2268 else if (gsym != NULL
2269 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2271 symval.set_output_value(target->plt_section()->address()
2272 + gsym->plt_offset());
2275 else if (gsym == NULL && psymval->is_ifunc_symbol())
2277 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2278 if (object->local_has_plt_offset(r_sym))
2280 symval.set_output_value(target->plt_section()->address()
2281 + object->local_plt_offset(r_sym));
2286 // Get the GOT offset if needed.
2287 // The GOT pointer points to the end of the GOT section.
2288 // We need to subtract the size of the GOT section to get
2289 // the actual offset to use in the relocation.
2290 bool have_got_offset = false;
2291 unsigned int got_offset = 0;
2294 case elfcpp::R_386_GOT32:
2297 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2298 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2299 - target->got_size());
2303 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2304 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2305 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2306 - target->got_size());
2308 have_got_offset = true;
2317 case elfcpp::R_386_NONE:
2318 case elfcpp::R_386_GNU_VTINHERIT:
2319 case elfcpp::R_386_GNU_VTENTRY:
2322 case elfcpp::R_386_32:
2323 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2324 Relocate_functions<32, false>::rel32(view, object, psymval);
2327 case elfcpp::R_386_PC32:
2328 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2329 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2332 case elfcpp::R_386_16:
2333 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2334 Relocate_functions<32, false>::rel16(view, object, psymval);
2337 case elfcpp::R_386_PC16:
2338 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2339 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2342 case elfcpp::R_386_8:
2343 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2344 Relocate_functions<32, false>::rel8(view, object, psymval);
2347 case elfcpp::R_386_PC8:
2348 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2349 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2352 case elfcpp::R_386_PLT32:
2353 gold_assert(gsym == NULL
2354 || gsym->has_plt_offset()
2355 || gsym->final_value_is_known()
2356 || (gsym->is_defined()
2357 && !gsym->is_from_dynobj()
2358 && !gsym->is_preemptible()));
2359 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2362 case elfcpp::R_386_GOT32:
2363 gold_assert(have_got_offset);
2364 Relocate_functions<32, false>::rel32(view, got_offset);
2367 case elfcpp::R_386_GOTOFF:
2369 elfcpp::Elf_types<32>::Elf_Addr value;
2370 value = (psymval->value(object, 0)
2371 - target->got_plt_section()->address());
2372 Relocate_functions<32, false>::rel32(view, value);
2376 case elfcpp::R_386_GOTPC:
2378 elfcpp::Elf_types<32>::Elf_Addr value;
2379 value = target->got_plt_section()->address();
2380 Relocate_functions<32, false>::pcrel32(view, value, address);
2384 case elfcpp::R_386_COPY:
2385 case elfcpp::R_386_GLOB_DAT:
2386 case elfcpp::R_386_JUMP_SLOT:
2387 case elfcpp::R_386_RELATIVE:
2388 case elfcpp::R_386_IRELATIVE:
2389 // These are outstanding tls relocs, which are unexpected when
2391 case elfcpp::R_386_TLS_TPOFF:
2392 case elfcpp::R_386_TLS_DTPMOD32:
2393 case elfcpp::R_386_TLS_DTPOFF32:
2394 case elfcpp::R_386_TLS_TPOFF32:
2395 case elfcpp::R_386_TLS_DESC:
2396 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2397 _("unexpected reloc %u in object file"),
2401 // These are initial tls relocs, which are expected when
2403 case elfcpp::R_386_TLS_GD: // Global-dynamic
2404 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2405 case elfcpp::R_386_TLS_DESC_CALL:
2406 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2407 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2408 case elfcpp::R_386_TLS_IE: // Initial-exec
2409 case elfcpp::R_386_TLS_IE_32:
2410 case elfcpp::R_386_TLS_GOTIE:
2411 case elfcpp::R_386_TLS_LE: // Local-exec
2412 case elfcpp::R_386_TLS_LE_32:
2413 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2414 view, address, view_size);
2417 case elfcpp::R_386_32PLT:
2418 case elfcpp::R_386_TLS_GD_32:
2419 case elfcpp::R_386_TLS_GD_PUSH:
2420 case elfcpp::R_386_TLS_GD_CALL:
2421 case elfcpp::R_386_TLS_GD_POP:
2422 case elfcpp::R_386_TLS_LDM_32:
2423 case elfcpp::R_386_TLS_LDM_PUSH:
2424 case elfcpp::R_386_TLS_LDM_CALL:
2425 case elfcpp::R_386_TLS_LDM_POP:
2426 case elfcpp::R_386_USED_BY_INTEL_200:
2428 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2429 _("unsupported reloc %u"),
2437 // Perform a TLS relocation.
2440 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
2441 Target_i386* target,
2443 const elfcpp::Rel<32, false>& rel,
2444 unsigned int r_type,
2445 const Sized_symbol<32>* gsym,
2446 const Symbol_value<32>* psymval,
2447 unsigned char* view,
2448 elfcpp::Elf_types<32>::Elf_Addr,
2449 section_size_type view_size)
2451 Output_segment* tls_segment = relinfo->layout->tls_segment();
2453 const Sized_relobj_file<32, false>* object = relinfo->object;
2455 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
2457 const bool is_final = (gsym == NULL
2458 ? !parameters->options().shared()
2459 : gsym->final_value_is_known());
2460 const tls::Tls_optimization optimized_type
2461 = Target_i386::optimize_tls_reloc(is_final, r_type);
2464 case elfcpp::R_386_TLS_GD: // Global-dynamic
2465 if (optimized_type == tls::TLSOPT_TO_LE)
2467 gold_assert(tls_segment != NULL);
2468 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2469 rel, r_type, value, view,
2475 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2476 ? GOT_TYPE_TLS_NOFFSET
2477 : GOT_TYPE_TLS_PAIR);
2478 unsigned int got_offset;
2481 gold_assert(gsym->has_got_offset(got_type));
2482 got_offset = gsym->got_offset(got_type) - target->got_size();
2486 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2487 gold_assert(object->local_has_got_offset(r_sym, got_type));
2488 got_offset = (object->local_got_offset(r_sym, got_type)
2489 - target->got_size());
2491 if (optimized_type == tls::TLSOPT_TO_IE)
2493 gold_assert(tls_segment != NULL);
2494 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2495 got_offset, view, view_size);
2498 else if (optimized_type == tls::TLSOPT_NONE)
2500 // Relocate the field with the offset of the pair of GOT
2502 Relocate_functions<32, false>::rel32(view, got_offset);
2506 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2507 _("unsupported reloc %u"),
2511 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2512 case elfcpp::R_386_TLS_DESC_CALL:
2513 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2514 if (optimized_type == tls::TLSOPT_TO_LE)
2516 gold_assert(tls_segment != NULL);
2517 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2518 rel, r_type, value, view,
2524 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2525 ? GOT_TYPE_TLS_NOFFSET
2526 : GOT_TYPE_TLS_DESC);
2527 unsigned int got_offset = 0;
2528 if (r_type == elfcpp::R_386_TLS_GOTDESC
2529 && optimized_type == tls::TLSOPT_NONE)
2531 // We created GOT entries in the .got.tlsdesc portion of
2532 // the .got.plt section, but the offset stored in the
2533 // symbol is the offset within .got.tlsdesc.
2534 got_offset = (target->got_size()
2535 + target->got_plt_section()->data_size());
2539 gold_assert(gsym->has_got_offset(got_type));
2540 got_offset += gsym->got_offset(got_type) - target->got_size();
2544 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2545 gold_assert(object->local_has_got_offset(r_sym, got_type));
2546 got_offset += (object->local_got_offset(r_sym, got_type)
2547 - target->got_size());
2549 if (optimized_type == tls::TLSOPT_TO_IE)
2551 gold_assert(tls_segment != NULL);
2552 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2553 got_offset, view, view_size);
2556 else if (optimized_type == tls::TLSOPT_NONE)
2558 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2560 // Relocate the field with the offset of the pair of GOT
2562 Relocate_functions<32, false>::rel32(view, got_offset);
2567 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2568 _("unsupported reloc %u"),
2572 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2573 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2575 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2576 _("both SUN and GNU model "
2577 "TLS relocations"));
2580 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2581 if (optimized_type == tls::TLSOPT_TO_LE)
2583 gold_assert(tls_segment != NULL);
2584 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2585 value, view, view_size);
2588 else if (optimized_type == tls::TLSOPT_NONE)
2590 // Relocate the field with the offset of the GOT entry for
2591 // the module index.
2592 unsigned int got_offset;
2593 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2594 - target->got_size());
2595 Relocate_functions<32, false>::rel32(view, got_offset);
2598 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2599 _("unsupported reloc %u"),
2603 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2604 if (optimized_type == tls::TLSOPT_TO_LE)
2606 // This reloc can appear in debugging sections, in which
2607 // case we must not convert to local-exec. We decide what
2608 // to do based on whether the section is marked as
2609 // containing executable code. That is what the GNU linker
2611 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2612 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2614 gold_assert(tls_segment != NULL);
2615 value -= tls_segment->memsz();
2618 Relocate_functions<32, false>::rel32(view, value);
2621 case elfcpp::R_386_TLS_IE: // Initial-exec
2622 case elfcpp::R_386_TLS_GOTIE:
2623 case elfcpp::R_386_TLS_IE_32:
2624 if (optimized_type == tls::TLSOPT_TO_LE)
2626 gold_assert(tls_segment != NULL);
2627 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2628 rel, r_type, value, view,
2632 else if (optimized_type == tls::TLSOPT_NONE)
2634 // Relocate the field with the offset of the GOT entry for
2635 // the tp-relative offset of the symbol.
2636 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2637 ? GOT_TYPE_TLS_OFFSET
2638 : GOT_TYPE_TLS_NOFFSET);
2639 unsigned int got_offset;
2642 gold_assert(gsym->has_got_offset(got_type));
2643 got_offset = gsym->got_offset(got_type);
2647 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2648 gold_assert(object->local_has_got_offset(r_sym, got_type));
2649 got_offset = object->local_got_offset(r_sym, got_type);
2651 // For the R_386_TLS_IE relocation, we need to apply the
2652 // absolute address of the GOT entry.
2653 if (r_type == elfcpp::R_386_TLS_IE)
2654 got_offset += target->got_plt_section()->address();
2655 // All GOT offsets are relative to the end of the GOT.
2656 got_offset -= target->got_size();
2657 Relocate_functions<32, false>::rel32(view, got_offset);
2660 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2661 _("unsupported reloc %u"),
2665 case elfcpp::R_386_TLS_LE: // Local-exec
2666 // If we're creating a shared library, a dynamic relocation will
2667 // have been created for this location, so do not apply it now.
2668 if (!parameters->options().shared())
2670 gold_assert(tls_segment != NULL);
2671 value -= tls_segment->memsz();
2672 Relocate_functions<32, false>::rel32(view, value);
2676 case elfcpp::R_386_TLS_LE_32:
2677 // If we're creating a shared library, a dynamic relocation will
2678 // have been created for this location, so do not apply it now.
2679 if (!parameters->options().shared())
2681 gold_assert(tls_segment != NULL);
2682 value = tls_segment->memsz() - value;
2683 Relocate_functions<32, false>::rel32(view, value);
2689 // Do a relocation in which we convert a TLS General-Dynamic to a
2693 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2695 Output_segment* tls_segment,
2696 const elfcpp::Rel<32, false>& rel,
2698 elfcpp::Elf_types<32>::Elf_Addr value,
2699 unsigned char* view,
2700 section_size_type view_size)
2702 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2703 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2704 // leal foo(%reg),%eax; call ___tls_get_addr
2705 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2707 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2708 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2710 unsigned char op1 = view[-1];
2711 unsigned char op2 = view[-2];
2713 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2714 op2 == 0x8d || op2 == 0x04);
2715 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2721 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2722 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2723 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2724 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2725 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2729 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2730 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2731 if (rel.get_r_offset() + 9 < view_size
2734 // There is a trailing nop. Use the size byte subl.
2735 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2740 // Use the five byte subl.
2741 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2745 value = tls_segment->memsz() - value;
2746 Relocate_functions<32, false>::rel32(view + roff, value);
2748 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2750 this->skip_call_tls_get_addr_ = true;
2753 // Do a relocation in which we convert a TLS General-Dynamic to an
2757 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2760 const elfcpp::Rel<32, false>& rel,
2762 elfcpp::Elf_types<32>::Elf_Addr value,
2763 unsigned char* view,
2764 section_size_type view_size)
2766 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2767 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2769 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2770 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2772 unsigned char op1 = view[-1];
2773 unsigned char op2 = view[-2];
2775 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2776 op2 == 0x8d || op2 == 0x04);
2777 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2781 // FIXME: For now, support only the first (SIB) form.
2782 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2786 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2787 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2788 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2789 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2790 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2794 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2795 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2796 if (rel.get_r_offset() + 9 < view_size
2799 // FIXME: This is not the right instruction sequence.
2800 // There is a trailing nop. Use the size byte subl.
2801 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2806 // FIXME: This is not the right instruction sequence.
2807 // Use the five byte subl.
2808 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2812 Relocate_functions<32, false>::rel32(view + roff, value);
2814 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2816 this->skip_call_tls_get_addr_ = true;
2819 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2820 // General-Dynamic to a Local-Exec.
2823 Target_i386::Relocate::tls_desc_gd_to_le(
2824 const Relocate_info<32, false>* relinfo,
2826 Output_segment* tls_segment,
2827 const elfcpp::Rel<32, false>& rel,
2828 unsigned int r_type,
2829 elfcpp::Elf_types<32>::Elf_Addr value,
2830 unsigned char* view,
2831 section_size_type view_size)
2833 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2835 // leal foo@TLSDESC(%ebx), %eax
2836 // ==> leal foo@NTPOFF, %eax
2837 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2838 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2839 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2840 view[-2] == 0x8d && view[-1] == 0x83);
2842 value -= tls_segment->memsz();
2843 Relocate_functions<32, false>::rel32(view, value);
2847 // call *foo@TLSCALL(%eax)
2849 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2850 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2851 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2852 view[0] == 0xff && view[1] == 0x10);
2858 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2859 // General-Dynamic to an Initial-Exec.
2862 Target_i386::Relocate::tls_desc_gd_to_ie(
2863 const Relocate_info<32, false>* relinfo,
2866 const elfcpp::Rel<32, false>& rel,
2867 unsigned int r_type,
2868 elfcpp::Elf_types<32>::Elf_Addr value,
2869 unsigned char* view,
2870 section_size_type view_size)
2872 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2874 // leal foo@TLSDESC(%ebx), %eax
2875 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2876 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2877 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2878 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2879 view[-2] == 0x8d && view[-1] == 0x83);
2881 Relocate_functions<32, false>::rel32(view, value);
2885 // call *foo@TLSCALL(%eax)
2887 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2888 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2889 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2890 view[0] == 0xff && view[1] == 0x10);
2896 // Do a relocation in which we convert a TLS Local-Dynamic to a
2900 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2903 const elfcpp::Rel<32, false>& rel,
2905 elfcpp::Elf_types<32>::Elf_Addr,
2906 unsigned char* view,
2907 section_size_type view_size)
2909 // leal foo(%reg), %eax; call ___tls_get_addr
2910 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2912 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2913 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2915 // FIXME: Does this test really always pass?
2916 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2917 view[-2] == 0x8d && view[-1] == 0x83);
2919 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2921 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2923 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2925 this->skip_call_tls_get_addr_ = true;
2928 // Do a relocation in which we convert a TLS Initial-Exec to a
2932 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2934 Output_segment* tls_segment,
2935 const elfcpp::Rel<32, false>& rel,
2936 unsigned int r_type,
2937 elfcpp::Elf_types<32>::Elf_Addr value,
2938 unsigned char* view,
2939 section_size_type view_size)
2941 // We have to actually change the instructions, which means that we
2942 // need to examine the opcodes to figure out which instruction we
2944 if (r_type == elfcpp::R_386_TLS_IE)
2946 // movl %gs:XX,%eax ==> movl $YY,%eax
2947 // movl %gs:XX,%reg ==> movl $YY,%reg
2948 // addl %gs:XX,%reg ==> addl $YY,%reg
2949 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2950 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2952 unsigned char op1 = view[-1];
2955 // movl XX,%eax ==> movl $YY,%eax
2960 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2962 unsigned char op2 = view[-2];
2965 // movl XX,%reg ==> movl $YY,%reg
2966 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2967 (op1 & 0xc7) == 0x05);
2969 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2971 else if (op2 == 0x03)
2973 // addl XX,%reg ==> addl $YY,%reg
2974 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2975 (op1 & 0xc7) == 0x05);
2977 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2980 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2985 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2986 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2987 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2988 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2989 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2991 unsigned char op1 = view[-1];
2992 unsigned char op2 = view[-2];
2993 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2994 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2997 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2999 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3001 else if (op2 == 0x2b)
3003 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3005 view[-1] = 0xe8 | ((op1 >> 3) & 7);
3007 else if (op2 == 0x03)
3009 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3011 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3014 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3017 value = tls_segment->memsz() - value;
3018 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
3021 Relocate_functions<32, false>::rel32(view, value);
3024 // Relocate section data.
3027 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
3028 unsigned int sh_type,
3029 const unsigned char* prelocs,
3031 Output_section* output_section,
3032 bool needs_special_offset_handling,
3033 unsigned char* view,
3034 elfcpp::Elf_types<32>::Elf_Addr address,
3035 section_size_type view_size,
3036 const Reloc_symbol_changes* reloc_symbol_changes)
3038 gold_assert(sh_type == elfcpp::SHT_REL);
3040 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
3041 Target_i386::Relocate>(
3047 needs_special_offset_handling,
3051 reloc_symbol_changes);
3054 // Return the size of a relocation while scanning during a relocatable
3058 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3059 unsigned int r_type,
3064 case elfcpp::R_386_NONE:
3065 case elfcpp::R_386_GNU_VTINHERIT:
3066 case elfcpp::R_386_GNU_VTENTRY:
3067 case elfcpp::R_386_TLS_GD: // Global-dynamic
3068 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
3069 case elfcpp::R_386_TLS_DESC_CALL:
3070 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3071 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3072 case elfcpp::R_386_TLS_IE: // Initial-exec
3073 case elfcpp::R_386_TLS_IE_32:
3074 case elfcpp::R_386_TLS_GOTIE:
3075 case elfcpp::R_386_TLS_LE: // Local-exec
3076 case elfcpp::R_386_TLS_LE_32:
3079 case elfcpp::R_386_32:
3080 case elfcpp::R_386_PC32:
3081 case elfcpp::R_386_GOT32:
3082 case elfcpp::R_386_PLT32:
3083 case elfcpp::R_386_GOTOFF:
3084 case elfcpp::R_386_GOTPC:
3087 case elfcpp::R_386_16:
3088 case elfcpp::R_386_PC16:
3091 case elfcpp::R_386_8:
3092 case elfcpp::R_386_PC8:
3095 // These are relocations which should only be seen by the
3096 // dynamic linker, and should never be seen here.
3097 case elfcpp::R_386_COPY:
3098 case elfcpp::R_386_GLOB_DAT:
3099 case elfcpp::R_386_JUMP_SLOT:
3100 case elfcpp::R_386_RELATIVE:
3101 case elfcpp::R_386_IRELATIVE:
3102 case elfcpp::R_386_TLS_TPOFF:
3103 case elfcpp::R_386_TLS_DTPMOD32:
3104 case elfcpp::R_386_TLS_DTPOFF32:
3105 case elfcpp::R_386_TLS_TPOFF32:
3106 case elfcpp::R_386_TLS_DESC:
3107 object->error(_("unexpected reloc %u in object file"), r_type);
3110 case elfcpp::R_386_32PLT:
3111 case elfcpp::R_386_TLS_GD_32:
3112 case elfcpp::R_386_TLS_GD_PUSH:
3113 case elfcpp::R_386_TLS_GD_CALL:
3114 case elfcpp::R_386_TLS_GD_POP:
3115 case elfcpp::R_386_TLS_LDM_32:
3116 case elfcpp::R_386_TLS_LDM_PUSH:
3117 case elfcpp::R_386_TLS_LDM_CALL:
3118 case elfcpp::R_386_TLS_LDM_POP:
3119 case elfcpp::R_386_USED_BY_INTEL_200:
3121 object->error(_("unsupported reloc %u in object file"), r_type);
3126 // Scan the relocs during a relocatable link.
3129 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3131 Sized_relobj_file<32, false>* object,
3132 unsigned int data_shndx,
3133 unsigned int sh_type,
3134 const unsigned char* prelocs,
3136 Output_section* output_section,
3137 bool needs_special_offset_handling,
3138 size_t local_symbol_count,
3139 const unsigned char* plocal_symbols,
3140 Relocatable_relocs* rr)
3142 gold_assert(sh_type == elfcpp::SHT_REL);
3144 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3145 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3147 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
3148 Scan_relocatable_relocs>(
3156 needs_special_offset_handling,
3162 // Relocate a section during a relocatable link.
3165 Target_i386::relocate_for_relocatable(
3166 const Relocate_info<32, false>* relinfo,
3167 unsigned int sh_type,
3168 const unsigned char* prelocs,
3170 Output_section* output_section,
3171 off_t offset_in_output_section,
3172 const Relocatable_relocs* rr,
3173 unsigned char* view,
3174 elfcpp::Elf_types<32>::Elf_Addr view_address,
3175 section_size_type view_size,
3176 unsigned char* reloc_view,
3177 section_size_type reloc_view_size)
3179 gold_assert(sh_type == elfcpp::SHT_REL);
3181 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
3186 offset_in_output_section,
3195 // Return the value to use for a dynamic which requires special
3196 // treatment. This is how we support equality comparisons of function
3197 // pointers across shared library boundaries, as described in the
3198 // processor specific ABI supplement.
3201 Target_i386::do_dynsym_value(const Symbol* gsym) const
3203 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3204 return this->plt_section()->address() + gsym->plt_offset();
3207 // Return a string used to fill a code section with nops to take up
3208 // the specified length.
3211 Target_i386::do_code_fill(section_size_type length) const
3215 // Build a jmp instruction to skip over the bytes.
3216 unsigned char jmp[5];
3218 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3219 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3220 + std::string(length - 5, '\0'));
3223 // Nop sequences of various lengths.
3224 const char nop1[1] = { 0x90 }; // nop
3225 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3226 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3227 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3228 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3229 0x00 }; // leal 0(%esi,1),%esi
3230 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3232 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3234 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3235 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3236 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3237 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3239 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3240 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3242 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3243 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3245 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3246 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3247 0x00, 0x00, 0x00, 0x00 };
3248 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3249 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3250 0x27, 0x00, 0x00, 0x00,
3252 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3253 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3254 0xbc, 0x27, 0x00, 0x00,
3256 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3257 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3258 0x90, 0x90, 0x90, 0x90,
3261 const char* nops[16] = {
3263 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3264 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3267 return std::string(nops[length], length);
3270 // Return whether SYM should be treated as a call to a non-split
3271 // function. We don't want that to be true of a call to a
3272 // get_pc_thunk function.
3275 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
3277 return (sym->type() == elfcpp::STT_FUNC
3278 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3281 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3282 // compiled with -fsplit-stack. The function calls non-split-stack
3283 // code. We have to change the function so that it always ensures
3284 // that it has enough stack space to run some random function.
3287 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3288 section_offset_type fnoffset,
3289 section_size_type fnsize,
3290 unsigned char* view,
3291 section_size_type view_size,
3293 std::string* to) const
3295 // The function starts with a comparison of the stack pointer and a
3296 // field in the TCB. This is followed by a jump.
3299 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3302 // We will call __morestack if the carry flag is set after this
3303 // comparison. We turn the comparison into an stc instruction
3305 view[fnoffset] = '\xf9';
3306 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3308 // lea NN(%esp),%ecx
3309 // lea NN(%esp),%edx
3310 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3311 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
3314 // This is loading an offset from the stack pointer for a
3315 // comparison. The offset is negative, so we decrease the
3316 // offset by the amount of space we need for the stack. This
3317 // means we will avoid calling __morestack if there happens to
3318 // be plenty of space on the stack already.
3319 unsigned char* pval = view + fnoffset + 3;
3320 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3321 val -= parameters->options().split_stack_adjust_size();
3322 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3326 if (!object->has_no_split_stack())
3327 object->error(_("failed to match split-stack sequence at "
3328 "section %u offset %0zx"),
3329 shndx, static_cast<size_t>(fnoffset));
3333 // We have to change the function so that it calls
3334 // __morestack_non_split instead of __morestack. The former will
3335 // allocate additional stack space.
3336 *from = "__morestack";
3337 *to = "__morestack_non_split";
3340 // The selector for i386 object files.
3342 class Target_selector_i386 : public Target_selector_freebsd
3345 Target_selector_i386()
3346 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
3347 "elf32-i386", "elf32-i386-freebsd",
3352 do_instantiate_target()
3353 { return new Target_i386(); }
3356 Target_selector_i386 target_selector_i386;
3358 } // End anonymous namespace.