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 the base for a DW_EH_PE_datarel encoding.
300 do_ehframe_datarel_base() const;
302 // Return whether SYM is call to a non-split function.
304 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
306 // Adjust -fsplit-stack code which calls non-split-stack code.
308 do_calls_non_split(Relobj* object, unsigned int shndx,
309 section_offset_type fnoffset, section_size_type fnsize,
310 unsigned char* view, section_size_type view_size,
311 std::string* from, std::string* to) const;
313 // Return the size of the GOT section.
317 gold_assert(this->got_ != NULL);
318 return this->got_->data_size();
321 // Return the number of entries in the GOT.
323 got_entry_count() const
325 if (this->got_ == NULL)
327 return this->got_size() / 4;
330 // Return the number of entries in the PLT.
332 plt_entry_count() const;
334 // Return the offset of the first non-reserved PLT entry.
336 first_plt_entry_offset() const;
338 // Return the size of each PLT entry.
340 plt_entry_size() const;
343 // The class which scans relocations.
348 get_reference_flags(unsigned int r_type);
351 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
352 Sized_relobj_file<32, false>* object,
353 unsigned int data_shndx,
354 Output_section* output_section,
355 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
356 const elfcpp::Sym<32, false>& lsym);
359 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
360 Sized_relobj_file<32, false>* object,
361 unsigned int data_shndx,
362 Output_section* output_section,
363 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
367 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
369 Sized_relobj_file<32, false>* object,
370 unsigned int data_shndx,
371 Output_section* output_section,
372 const elfcpp::Rel<32, false>& reloc,
374 const elfcpp::Sym<32, false>& lsym);
377 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
379 Sized_relobj_file<32, false>* object,
380 unsigned int data_shndx,
381 Output_section* output_section,
382 const elfcpp::Rel<32, false>& reloc,
387 possible_function_pointer_reloc(unsigned int r_type);
390 reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
391 unsigned int r_type);
394 unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
397 unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
401 // The class which implements relocation.
406 : skip_call_tls_get_addr_(false),
407 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
412 if (this->skip_call_tls_get_addr_)
414 // FIXME: This needs to specify the location somehow.
415 gold_error(_("missing expected TLS relocation"));
419 // Return whether the static relocation needs to be applied.
421 should_apply_static_reloc(const Sized_symbol<32>* gsym,
424 Output_section* output_section);
426 // Do a relocation. Return false if the caller should not issue
427 // any warnings about this relocation.
429 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
430 size_t relnum, const elfcpp::Rel<32, false>&,
431 unsigned int r_type, const Sized_symbol<32>*,
432 const Symbol_value<32>*,
433 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
437 // Do a TLS relocation.
439 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
440 size_t relnum, const elfcpp::Rel<32, false>&,
441 unsigned int r_type, const Sized_symbol<32>*,
442 const Symbol_value<32>*,
443 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
446 // Do a TLS General-Dynamic to Initial-Exec transition.
448 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
449 Output_segment* tls_segment,
450 const elfcpp::Rel<32, false>&, unsigned int r_type,
451 elfcpp::Elf_types<32>::Elf_Addr value,
453 section_size_type view_size);
455 // Do a TLS General-Dynamic to Local-Exec transition.
457 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
458 Output_segment* tls_segment,
459 const elfcpp::Rel<32, false>&, unsigned int r_type,
460 elfcpp::Elf_types<32>::Elf_Addr value,
462 section_size_type view_size);
464 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
467 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
468 Output_segment* tls_segment,
469 const elfcpp::Rel<32, false>&, unsigned int r_type,
470 elfcpp::Elf_types<32>::Elf_Addr value,
472 section_size_type view_size);
474 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
477 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
478 Output_segment* tls_segment,
479 const elfcpp::Rel<32, false>&, unsigned int r_type,
480 elfcpp::Elf_types<32>::Elf_Addr value,
482 section_size_type view_size);
484 // Do a TLS Local-Dynamic to Local-Exec transition.
486 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
487 Output_segment* tls_segment,
488 const elfcpp::Rel<32, false>&, unsigned int r_type,
489 elfcpp::Elf_types<32>::Elf_Addr value,
491 section_size_type view_size);
493 // Do a TLS Initial-Exec to Local-Exec transition.
495 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
496 Output_segment* tls_segment,
497 const elfcpp::Rel<32, false>&, unsigned int r_type,
498 elfcpp::Elf_types<32>::Elf_Addr value,
500 section_size_type view_size);
502 // We need to keep track of which type of local dynamic relocation
503 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
504 enum Local_dynamic_type
511 // This is set if we should skip the next reloc, which should be a
512 // PLT32 reloc against ___tls_get_addr.
513 bool skip_call_tls_get_addr_;
514 // The type of local dynamic relocation we have seen in the section
515 // being relocated, if any.
516 Local_dynamic_type local_dynamic_type_;
519 // A class which returns the size required for a relocation type,
520 // used while scanning relocs during a relocatable link.
521 class Relocatable_size_for_reloc
525 get_size_for_reloc(unsigned int, Relobj*);
528 // Adjust TLS relocation type based on the options and whether this
529 // is a local symbol.
530 static tls::Tls_optimization
531 optimize_tls_reloc(bool is_final, int r_type);
533 // Get the GOT section, creating it if necessary.
534 Output_data_got<32, false>*
535 got_section(Symbol_table*, Layout*);
537 // Get the GOT PLT section.
539 got_plt_section() const
541 gold_assert(this->got_plt_ != NULL);
542 return this->got_plt_;
545 // Get the GOT section for TLSDESC entries.
546 Output_data_got<32, false>*
547 got_tlsdesc_section() const
549 gold_assert(this->got_tlsdesc_ != NULL);
550 return this->got_tlsdesc_;
553 // Create the PLT section.
555 make_plt_section(Symbol_table* symtab, Layout* layout);
557 // Create a PLT entry for a global symbol.
559 make_plt_entry(Symbol_table*, Layout*, Symbol*);
561 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
563 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
564 Sized_relobj_file<32, false>* relobj,
565 unsigned int local_sym_index);
567 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
569 define_tls_base_symbol(Symbol_table*, Layout*);
571 // Create a GOT entry for the TLS module index.
573 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
574 Sized_relobj_file<32, false>* object);
576 // Get the PLT section.
577 Output_data_plt_i386*
580 gold_assert(this->plt_ != NULL);
584 // Get the dynamic reloc section, creating it if necessary.
586 rel_dyn_section(Layout*);
588 // Get the section to use for TLS_DESC relocations.
590 rel_tls_desc_section(Layout*) const;
592 // Add a potential copy relocation.
594 copy_reloc(Symbol_table* symtab, Layout* layout,
595 Sized_relobj_file<32, false>* object,
596 unsigned int shndx, Output_section* output_section,
597 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
599 this->copy_relocs_.copy_reloc(symtab, layout,
600 symtab->get_sized_symbol<32>(sym),
601 object, shndx, output_section, reloc,
602 this->rel_dyn_section(layout));
605 // Information about this specific target which we pass to the
606 // general Target structure.
607 static const Target::Target_info i386_info;
609 // The types of GOT entries needed for this platform.
610 // These values are exposed to the ABI in an incremental link.
611 // Do not renumber existing values without changing the version
612 // number of the .gnu_incremental_inputs section.
615 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
616 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
617 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
618 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
619 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
623 Output_data_got<32, false>* got_;
625 Output_data_plt_i386* plt_;
626 // The GOT PLT section.
627 Output_data_space* got_plt_;
628 // The GOT section for TLSDESC relocations.
629 Output_data_got<32, false>* got_tlsdesc_;
630 // The _GLOBAL_OFFSET_TABLE_ symbol.
631 Symbol* global_offset_table_;
632 // The dynamic reloc section.
633 Reloc_section* rel_dyn_;
634 // Relocs saved to avoid a COPY reloc.
635 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
636 // Space for variables copied with a COPY reloc.
637 Output_data_space* dynbss_;
638 // Offset of the GOT entry for the TLS module index.
639 unsigned int got_mod_index_offset_;
640 // True if the _TLS_MODULE_BASE_ symbol has been defined.
641 bool tls_base_symbol_defined_;
644 const Target::Target_info Target_i386::i386_info =
647 false, // is_big_endian
648 elfcpp::EM_386, // machine_code
649 false, // has_make_symbol
650 false, // has_resolve
651 true, // has_code_fill
652 true, // is_default_stack_executable
653 true, // can_icf_inline_merge_sections
655 "/usr/lib/libc.so.1", // dynamic_linker
656 0x08048000, // default_text_segment_address
657 0x1000, // abi_pagesize (overridable by -z max-page-size)
658 0x1000, // common_pagesize (overridable by -z common-page-size)
659 elfcpp::SHN_UNDEF, // small_common_shndx
660 elfcpp::SHN_UNDEF, // large_common_shndx
661 0, // small_common_section_flags
662 0, // large_common_section_flags
663 NULL, // attributes_section
664 NULL // attributes_vendor
667 // Get the GOT section, creating it if necessary.
669 Output_data_got<32, false>*
670 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
672 if (this->got_ == NULL)
674 gold_assert(symtab != NULL && layout != NULL);
676 this->got_ = new Output_data_got<32, false>();
678 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
680 | elfcpp::SHF_WRITE),
681 this->got_, ORDER_RELRO_LAST, true);
683 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
684 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
686 | elfcpp::SHF_WRITE),
687 this->got_plt_, ORDER_NON_RELRO_FIRST,
690 // The first three entries are reserved.
691 this->got_plt_->set_current_data_size(3 * 4);
693 // Those bytes can go into the relro segment.
694 layout->increase_relro(3 * 4);
696 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
697 this->global_offset_table_ =
698 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
699 Symbol_table::PREDEFINED,
701 0, 0, elfcpp::STT_OBJECT,
703 elfcpp::STV_HIDDEN, 0,
706 // If there are any TLSDESC relocations, they get GOT entries in
707 // .got.plt after the jump slot entries.
708 this->got_tlsdesc_ = new Output_data_got<32, false>();
709 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
711 | elfcpp::SHF_WRITE),
713 ORDER_NON_RELRO_FIRST, false);
719 // Get the dynamic reloc section, creating it if necessary.
721 Target_i386::Reloc_section*
722 Target_i386::rel_dyn_section(Layout* layout)
724 if (this->rel_dyn_ == NULL)
726 gold_assert(layout != NULL);
727 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
728 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
729 elfcpp::SHF_ALLOC, this->rel_dyn_,
730 ORDER_DYNAMIC_RELOCS, false);
732 return this->rel_dyn_;
735 // Create the PLT section. The ordinary .got section is an argument,
736 // since we need to refer to the start. We also create our own .got
737 // section just for PLT entries.
739 Output_data_plt_i386::Output_data_plt_i386(Symbol_table* symtab,
741 Output_data_space* got_plt)
742 : Output_section_data(16), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0),
743 global_ifuncs_(), local_ifuncs_()
745 this->rel_ = new Reloc_section(false);
746 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
747 elfcpp::SHF_ALLOC, this->rel_,
748 ORDER_DYNAMIC_PLT_RELOCS, false);
750 if (parameters->doing_static_link())
752 // A statically linked executable will only have a .rel.plt
753 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
754 // symbols. The library will use these symbols to locate the
755 // IRELATIVE relocs at program startup time.
756 symtab->define_in_output_data("__rel_iplt_start", NULL,
757 Symbol_table::PREDEFINED,
758 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
759 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
761 symtab->define_in_output_data("__rel_iplt_end", NULL,
762 Symbol_table::PREDEFINED,
763 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
764 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
768 // Add unwind information if requested.
769 if (parameters->options().ld_generated_unwind_info())
770 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
771 plt_eh_frame_fde, plt_eh_frame_fde_size);
775 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
777 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
778 // linker, and so do we.
782 // Add an entry to the PLT.
785 Output_data_plt_i386::add_entry(Symbol* gsym)
787 gold_assert(!gsym->has_plt_offset());
789 // Note that when setting the PLT offset we skip the initial
790 // reserved PLT entry.
791 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
795 section_offset_type got_offset = this->got_plt_->current_data_size();
797 // Every PLT entry needs a GOT entry which points back to the PLT
798 // entry (this will be changed by the dynamic linker, normally
799 // lazily when the function is called).
800 this->got_plt_->set_current_data_size(got_offset + 4);
802 // Every PLT entry needs a reloc.
803 if (gsym->type() == elfcpp::STT_GNU_IFUNC
804 && gsym->can_use_relative_reloc(false))
806 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
807 this->got_plt_, got_offset);
808 struct Global_ifunc gi;
810 gi.got_offset = got_offset;
811 this->global_ifuncs_.push_back(gi);
815 gsym->set_needs_dynsym_entry();
816 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
820 // Note that we don't need to save the symbol. The contents of the
821 // PLT are independent of which symbols are used. The symbols only
822 // appear in the relocations.
825 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
829 Output_data_plt_i386::add_local_ifunc_entry(
830 Sized_relobj_file<32, false>* relobj,
831 unsigned int local_sym_index)
833 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
836 section_offset_type got_offset = this->got_plt_->current_data_size();
838 // Every PLT entry needs a GOT entry which points back to the PLT
840 this->got_plt_->set_current_data_size(got_offset + 4);
842 // Every PLT entry needs a reloc.
843 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
844 elfcpp::R_386_IRELATIVE,
845 this->got_plt_, got_offset);
847 struct Local_ifunc li;
849 li.local_sym_index = local_sym_index;
850 li.got_offset = got_offset;
851 this->local_ifuncs_.push_back(li);
856 // Return where the TLS_DESC relocations should go, creating it if
857 // necessary. These follow the JUMP_SLOT relocations.
859 Output_data_plt_i386::Reloc_section*
860 Output_data_plt_i386::rel_tls_desc(Layout* layout)
862 if (this->tls_desc_rel_ == NULL)
864 this->tls_desc_rel_ = new Reloc_section(false);
865 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
866 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
867 ORDER_DYNAMIC_PLT_RELOCS, false);
868 gold_assert(this->tls_desc_rel_->output_section() ==
869 this->rel_->output_section());
871 return this->tls_desc_rel_;
874 // The first entry in the PLT for an executable.
876 const unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
878 0xff, 0x35, // pushl contents of memory address
879 0, 0, 0, 0, // replaced with address of .got + 4
880 0xff, 0x25, // jmp indirect
881 0, 0, 0, 0, // replaced with address of .got + 8
885 // The first entry in the PLT for a shared object.
887 const unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
889 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
890 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
894 // Subsequent entries in the PLT for an executable.
896 const unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
898 0xff, 0x25, // jmp indirect
899 0, 0, 0, 0, // replaced with address of symbol in .got
900 0x68, // pushl immediate
901 0, 0, 0, 0, // replaced with offset into relocation table
902 0xe9, // jmp relative
903 0, 0, 0, 0 // replaced with offset to start of .plt
906 // Subsequent entries in the PLT for a shared object.
908 const unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
910 0xff, 0xa3, // jmp *offset(%ebx)
911 0, 0, 0, 0, // replaced with offset of symbol in .got
912 0x68, // pushl immediate
913 0, 0, 0, 0, // replaced with offset into relocation table
914 0xe9, // jmp relative
915 0, 0, 0, 0 // replaced with offset to start of .plt
918 // The .eh_frame unwind information for the PLT.
921 Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] =
924 'z', // Augmentation: augmentation size included.
925 'R', // Augmentation: FDE encoding included.
926 '\0', // End of augmentation string.
927 1, // Code alignment factor.
928 0x7c, // Data alignment factor.
929 8, // Return address column.
930 1, // Augmentation size.
931 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
932 | elfcpp::DW_EH_PE_sdata4),
933 elfcpp::DW_CFA_def_cfa, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
934 elfcpp::DW_CFA_offset + 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
935 elfcpp::DW_CFA_nop, // Align to 16 bytes.
940 Output_data_plt_i386::plt_eh_frame_fde[plt_eh_frame_fde_size] =
942 0, 0, 0, 0, // Replaced with offset to .plt.
943 0, 0, 0, 0, // Replaced with size of .plt.
944 0, // Augmentation size.
945 elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
946 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
947 elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
948 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
949 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
951 elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
952 elfcpp::DW_OP_breg8, 0, // Push %eip.
953 elfcpp::DW_OP_lit15, // Push 0xf.
954 elfcpp::DW_OP_and, // & (%eip & 0xf).
955 elfcpp::DW_OP_lit11, // Push 0xb.
956 elfcpp::DW_OP_ge, // >= ((%eip & 0xf) >= 0xb)
957 elfcpp::DW_OP_lit2, // Push 2.
958 elfcpp::DW_OP_shl, // << (((%eip & 0xf) >= 0xb) << 2)
959 elfcpp::DW_OP_plus, // + ((((%eip&0xf)>=0xb)<<2)+%esp+4
960 elfcpp::DW_CFA_nop, // Align to 32 bytes.
966 // Write out the PLT. This uses the hand-coded instructions above,
967 // and adjusts them as needed. This is all specified by the i386 ELF
968 // Processor Supplement.
971 Output_data_plt_i386::do_write(Output_file* of)
973 const off_t offset = this->offset();
974 const section_size_type oview_size =
975 convert_to_section_size_type(this->data_size());
976 unsigned char* const oview = of->get_output_view(offset, oview_size);
978 const off_t got_file_offset = this->got_plt_->offset();
979 const section_size_type got_size =
980 convert_to_section_size_type(this->got_plt_->data_size());
981 unsigned char* const got_view = of->get_output_view(got_file_offset,
984 unsigned char* pov = oview;
986 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
987 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
989 if (parameters->options().output_is_position_independent())
990 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
993 memcpy(pov, exec_first_plt_entry, plt_entry_size);
994 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
995 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
997 pov += plt_entry_size;
999 unsigned char* got_pov = got_view;
1001 memset(got_pov, 0, 12);
1004 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1006 unsigned int plt_offset = plt_entry_size;
1007 unsigned int plt_rel_offset = 0;
1008 unsigned int got_offset = 12;
1009 const unsigned int count = this->count_;
1010 for (unsigned int i = 0;
1013 pov += plt_entry_size,
1015 plt_offset += plt_entry_size,
1016 plt_rel_offset += rel_size,
1019 // Set and adjust the PLT entry itself.
1021 if (parameters->options().output_is_position_independent())
1023 memcpy(pov, dyn_plt_entry, plt_entry_size);
1024 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
1028 memcpy(pov, exec_plt_entry, plt_entry_size);
1029 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1034 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1035 elfcpp::Swap<32, false>::writeval(pov + 12,
1036 - (plt_offset + plt_entry_size));
1038 // Set the entry in the GOT.
1039 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
1042 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
1043 // the GOT to point to the actual symbol value, rather than point to
1044 // the PLT entry. That will let the dynamic linker call the right
1045 // function when resolving IRELATIVE relocations.
1046 for (std::vector<Global_ifunc>::const_iterator p =
1047 this->global_ifuncs_.begin();
1048 p != this->global_ifuncs_.end();
1051 const Sized_symbol<32>* ssym =
1052 static_cast<const Sized_symbol<32>*>(p->sym);
1053 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
1057 for (std::vector<Local_ifunc>::const_iterator p =
1058 this->local_ifuncs_.begin();
1059 p != this->local_ifuncs_.end();
1062 const Symbol_value<32>* psymval =
1063 p->object->local_symbol(p->local_sym_index);
1064 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
1065 psymval->value(p->object, 0));
1068 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1069 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1071 of->write_output_view(offset, oview_size, oview);
1072 of->write_output_view(got_file_offset, got_size, got_view);
1075 // Create the PLT section.
1078 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1080 if (this->plt_ == NULL)
1082 // Create the GOT sections first.
1083 this->got_section(symtab, layout);
1085 this->plt_ = new Output_data_plt_i386(symtab, layout, this->got_plt_);
1086 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1088 | elfcpp::SHF_EXECINSTR),
1089 this->plt_, ORDER_PLT, false);
1091 // Make the sh_info field of .rel.plt point to .plt.
1092 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1093 rel_plt_os->set_info_section(this->plt_->output_section());
1097 // Create a PLT entry for a global symbol.
1100 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1102 if (gsym->has_plt_offset())
1104 if (this->plt_ == NULL)
1105 this->make_plt_section(symtab, layout);
1106 this->plt_->add_entry(gsym);
1109 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1112 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1113 Sized_relobj_file<32, false>* relobj,
1114 unsigned int local_sym_index)
1116 if (relobj->local_has_plt_offset(local_sym_index))
1118 if (this->plt_ == NULL)
1119 this->make_plt_section(symtab, layout);
1120 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1122 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1125 // Return the number of entries in the PLT.
1128 Target_i386::plt_entry_count() const
1130 if (this->plt_ == NULL)
1132 return this->plt_->entry_count();
1135 // Return the offset of the first non-reserved PLT entry.
1138 Target_i386::first_plt_entry_offset() const
1140 return Output_data_plt_i386::first_plt_entry_offset();
1143 // Return the size of each PLT entry.
1146 Target_i386::plt_entry_size() const
1148 return Output_data_plt_i386::get_plt_entry_size();
1151 // Get the section to use for TLS_DESC relocations.
1153 Target_i386::Reloc_section*
1154 Target_i386::rel_tls_desc_section(Layout* layout) const
1156 return this->plt_section()->rel_tls_desc(layout);
1159 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1162 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1164 if (this->tls_base_symbol_defined_)
1167 Output_segment* tls_segment = layout->tls_segment();
1168 if (tls_segment != NULL)
1170 bool is_exec = parameters->options().output_is_executable();
1171 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1172 Symbol_table::PREDEFINED,
1176 elfcpp::STV_HIDDEN, 0,
1178 ? Symbol::SEGMENT_END
1179 : Symbol::SEGMENT_START),
1182 this->tls_base_symbol_defined_ = true;
1185 // Create a GOT entry for the TLS module index.
1188 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1189 Sized_relobj_file<32, false>* object)
1191 if (this->got_mod_index_offset_ == -1U)
1193 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1194 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1195 Output_data_got<32, false>* got = this->got_section(symtab, layout);
1196 unsigned int got_offset = got->add_constant(0);
1197 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1199 got->add_constant(0);
1200 this->got_mod_index_offset_ = got_offset;
1202 return this->got_mod_index_offset_;
1205 // Optimize the TLS relocation type based on what we know about the
1206 // symbol. IS_FINAL is true if the final address of this symbol is
1207 // known at link time.
1209 tls::Tls_optimization
1210 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1212 // If we are generating a shared library, then we can't do anything
1214 if (parameters->options().shared())
1215 return tls::TLSOPT_NONE;
1219 case elfcpp::R_386_TLS_GD:
1220 case elfcpp::R_386_TLS_GOTDESC:
1221 case elfcpp::R_386_TLS_DESC_CALL:
1222 // These are General-Dynamic which permits fully general TLS
1223 // access. Since we know that we are generating an executable,
1224 // we can convert this to Initial-Exec. If we also know that
1225 // this is a local symbol, we can further switch to Local-Exec.
1227 return tls::TLSOPT_TO_LE;
1228 return tls::TLSOPT_TO_IE;
1230 case elfcpp::R_386_TLS_LDM:
1231 // This is Local-Dynamic, which refers to a local symbol in the
1232 // dynamic TLS block. Since we know that we generating an
1233 // executable, we can switch to Local-Exec.
1234 return tls::TLSOPT_TO_LE;
1236 case elfcpp::R_386_TLS_LDO_32:
1237 // Another type of Local-Dynamic relocation.
1238 return tls::TLSOPT_TO_LE;
1240 case elfcpp::R_386_TLS_IE:
1241 case elfcpp::R_386_TLS_GOTIE:
1242 case elfcpp::R_386_TLS_IE_32:
1243 // These are Initial-Exec relocs which get the thread offset
1244 // from the GOT. If we know that we are linking against the
1245 // local symbol, we can switch to Local-Exec, which links the
1246 // thread offset into the instruction.
1248 return tls::TLSOPT_TO_LE;
1249 return tls::TLSOPT_NONE;
1251 case elfcpp::R_386_TLS_LE:
1252 case elfcpp::R_386_TLS_LE_32:
1253 // When we already have Local-Exec, there is nothing further we
1255 return tls::TLSOPT_NONE;
1262 // Get the Reference_flags for a particular relocation.
1265 Target_i386::Scan::get_reference_flags(unsigned int r_type)
1269 case elfcpp::R_386_NONE:
1270 case elfcpp::R_386_GNU_VTINHERIT:
1271 case elfcpp::R_386_GNU_VTENTRY:
1272 case elfcpp::R_386_GOTPC:
1273 // No symbol reference.
1276 case elfcpp::R_386_32:
1277 case elfcpp::R_386_16:
1278 case elfcpp::R_386_8:
1279 return Symbol::ABSOLUTE_REF;
1281 case elfcpp::R_386_PC32:
1282 case elfcpp::R_386_PC16:
1283 case elfcpp::R_386_PC8:
1284 case elfcpp::R_386_GOTOFF:
1285 return Symbol::RELATIVE_REF;
1287 case elfcpp::R_386_PLT32:
1288 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1290 case elfcpp::R_386_GOT32:
1292 return Symbol::ABSOLUTE_REF;
1294 case elfcpp::R_386_TLS_GD: // Global-dynamic
1295 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1296 case elfcpp::R_386_TLS_DESC_CALL:
1297 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1298 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1299 case elfcpp::R_386_TLS_IE: // Initial-exec
1300 case elfcpp::R_386_TLS_IE_32:
1301 case elfcpp::R_386_TLS_GOTIE:
1302 case elfcpp::R_386_TLS_LE: // Local-exec
1303 case elfcpp::R_386_TLS_LE_32:
1304 return Symbol::TLS_REF;
1306 case elfcpp::R_386_COPY:
1307 case elfcpp::R_386_GLOB_DAT:
1308 case elfcpp::R_386_JUMP_SLOT:
1309 case elfcpp::R_386_RELATIVE:
1310 case elfcpp::R_386_IRELATIVE:
1311 case elfcpp::R_386_TLS_TPOFF:
1312 case elfcpp::R_386_TLS_DTPMOD32:
1313 case elfcpp::R_386_TLS_DTPOFF32:
1314 case elfcpp::R_386_TLS_TPOFF32:
1315 case elfcpp::R_386_TLS_DESC:
1316 case elfcpp::R_386_32PLT:
1317 case elfcpp::R_386_TLS_GD_32:
1318 case elfcpp::R_386_TLS_GD_PUSH:
1319 case elfcpp::R_386_TLS_GD_CALL:
1320 case elfcpp::R_386_TLS_GD_POP:
1321 case elfcpp::R_386_TLS_LDM_32:
1322 case elfcpp::R_386_TLS_LDM_PUSH:
1323 case elfcpp::R_386_TLS_LDM_CALL:
1324 case elfcpp::R_386_TLS_LDM_POP:
1325 case elfcpp::R_386_USED_BY_INTEL_200:
1327 // Not expected. We will give an error later.
1332 // Report an unsupported relocation against a local symbol.
1335 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
1336 unsigned int r_type)
1338 gold_error(_("%s: unsupported reloc %u against local symbol"),
1339 object->name().c_str(), r_type);
1342 // Return whether we need to make a PLT entry for a relocation of a
1343 // given type against a STT_GNU_IFUNC symbol.
1346 Target_i386::Scan::reloc_needs_plt_for_ifunc(
1347 Sized_relobj_file<32, false>* object,
1348 unsigned int r_type)
1350 int flags = Scan::get_reference_flags(r_type);
1351 if (flags & Symbol::TLS_REF)
1352 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1353 object->name().c_str(), r_type);
1357 // Scan a relocation for a local symbol.
1360 Target_i386::Scan::local(Symbol_table* symtab,
1362 Target_i386* target,
1363 Sized_relobj_file<32, false>* object,
1364 unsigned int data_shndx,
1365 Output_section* output_section,
1366 const elfcpp::Rel<32, false>& reloc,
1367 unsigned int r_type,
1368 const elfcpp::Sym<32, false>& lsym)
1370 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1371 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1372 && this->reloc_needs_plt_for_ifunc(object, r_type))
1374 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1375 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1380 case elfcpp::R_386_NONE:
1381 case elfcpp::R_386_GNU_VTINHERIT:
1382 case elfcpp::R_386_GNU_VTENTRY:
1385 case elfcpp::R_386_32:
1386 // If building a shared library (or a position-independent
1387 // executable), we need to create a dynamic relocation for
1388 // this location. The relocation applied at link time will
1389 // apply the link-time value, so we flag the location with
1390 // an R_386_RELATIVE relocation so the dynamic loader can
1391 // relocate it easily.
1392 if (parameters->options().output_is_position_independent())
1394 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1395 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1396 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1397 output_section, data_shndx,
1398 reloc.get_r_offset());
1402 case elfcpp::R_386_16:
1403 case elfcpp::R_386_8:
1404 // If building a shared library (or a position-independent
1405 // executable), we need to create a dynamic relocation for
1406 // this location. Because the addend needs to remain in the
1407 // data section, we need to be careful not to apply this
1408 // relocation statically.
1409 if (parameters->options().output_is_position_independent())
1411 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1412 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1413 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1414 rel_dyn->add_local(object, r_sym, r_type, output_section,
1415 data_shndx, reloc.get_r_offset());
1418 gold_assert(lsym.get_st_value() == 0);
1419 unsigned int shndx = lsym.get_st_shndx();
1421 shndx = object->adjust_sym_shndx(r_sym, shndx,
1424 object->error(_("section symbol %u has bad shndx %u"),
1427 rel_dyn->add_local_section(object, shndx,
1428 r_type, output_section,
1429 data_shndx, reloc.get_r_offset());
1434 case elfcpp::R_386_PC32:
1435 case elfcpp::R_386_PC16:
1436 case elfcpp::R_386_PC8:
1439 case elfcpp::R_386_PLT32:
1440 // Since we know this is a local symbol, we can handle this as a
1444 case elfcpp::R_386_GOTOFF:
1445 case elfcpp::R_386_GOTPC:
1446 // We need a GOT section.
1447 target->got_section(symtab, layout);
1450 case elfcpp::R_386_GOT32:
1452 // The symbol requires a GOT entry.
1453 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1454 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1456 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1457 // lets function pointers compare correctly with shared
1458 // libraries. Otherwise we would need an IRELATIVE reloc.
1460 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1461 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1463 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1466 // If we are generating a shared object, we need to add a
1467 // dynamic RELATIVE relocation for this symbol's GOT entry.
1468 if (parameters->options().output_is_position_independent())
1470 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1471 unsigned int got_offset =
1472 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1473 rel_dyn->add_local_relative(object, r_sym,
1474 elfcpp::R_386_RELATIVE,
1481 // These are relocations which should only be seen by the
1482 // dynamic linker, and should never be seen here.
1483 case elfcpp::R_386_COPY:
1484 case elfcpp::R_386_GLOB_DAT:
1485 case elfcpp::R_386_JUMP_SLOT:
1486 case elfcpp::R_386_RELATIVE:
1487 case elfcpp::R_386_IRELATIVE:
1488 case elfcpp::R_386_TLS_TPOFF:
1489 case elfcpp::R_386_TLS_DTPMOD32:
1490 case elfcpp::R_386_TLS_DTPOFF32:
1491 case elfcpp::R_386_TLS_TPOFF32:
1492 case elfcpp::R_386_TLS_DESC:
1493 gold_error(_("%s: unexpected reloc %u in object file"),
1494 object->name().c_str(), r_type);
1497 // These are initial TLS relocs, which are expected when
1499 case elfcpp::R_386_TLS_GD: // Global-dynamic
1500 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1501 case elfcpp::R_386_TLS_DESC_CALL:
1502 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1503 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1504 case elfcpp::R_386_TLS_IE: // Initial-exec
1505 case elfcpp::R_386_TLS_IE_32:
1506 case elfcpp::R_386_TLS_GOTIE:
1507 case elfcpp::R_386_TLS_LE: // Local-exec
1508 case elfcpp::R_386_TLS_LE_32:
1510 bool output_is_shared = parameters->options().shared();
1511 const tls::Tls_optimization optimized_type
1512 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1515 case elfcpp::R_386_TLS_GD: // Global-dynamic
1516 if (optimized_type == tls::TLSOPT_NONE)
1518 // Create a pair of GOT entries for the module index and
1519 // dtv-relative offset.
1520 Output_data_got<32, false>* got
1521 = target->got_section(symtab, layout);
1522 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1523 unsigned int shndx = lsym.get_st_shndx();
1525 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1527 object->error(_("local symbol %u has bad shndx %u"),
1530 got->add_local_pair_with_rel(object, r_sym, shndx,
1532 target->rel_dyn_section(layout),
1533 elfcpp::R_386_TLS_DTPMOD32, 0);
1535 else if (optimized_type != tls::TLSOPT_TO_LE)
1536 unsupported_reloc_local(object, r_type);
1539 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1540 target->define_tls_base_symbol(symtab, layout);
1541 if (optimized_type == tls::TLSOPT_NONE)
1543 // Create a double GOT entry with an R_386_TLS_DESC
1544 // reloc. The R_386_TLS_DESC reloc is resolved
1545 // lazily, so the GOT entry needs to be in an area in
1546 // .got.plt, not .got. Call got_section to make sure
1547 // the section has been created.
1548 target->got_section(symtab, layout);
1549 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1550 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1551 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1553 unsigned int got_offset = got->add_constant(0);
1554 // The local symbol value is stored in the second
1556 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1557 // That set the GOT offset of the local symbol to
1558 // point to the second entry, but we want it to
1559 // point to the first.
1560 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1562 Reloc_section* rt = target->rel_tls_desc_section(layout);
1563 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1566 else if (optimized_type != tls::TLSOPT_TO_LE)
1567 unsupported_reloc_local(object, r_type);
1570 case elfcpp::R_386_TLS_DESC_CALL:
1573 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1574 if (optimized_type == tls::TLSOPT_NONE)
1576 // Create a GOT entry for the module index.
1577 target->got_mod_index_entry(symtab, layout, object);
1579 else if (optimized_type != tls::TLSOPT_TO_LE)
1580 unsupported_reloc_local(object, r_type);
1583 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1586 case elfcpp::R_386_TLS_IE: // Initial-exec
1587 case elfcpp::R_386_TLS_IE_32:
1588 case elfcpp::R_386_TLS_GOTIE:
1589 layout->set_has_static_tls();
1590 if (optimized_type == tls::TLSOPT_NONE)
1592 // For the R_386_TLS_IE relocation, we need to create a
1593 // dynamic relocation when building a shared library.
1594 if (r_type == elfcpp::R_386_TLS_IE
1595 && parameters->options().shared())
1597 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1599 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1600 rel_dyn->add_local_relative(object, r_sym,
1601 elfcpp::R_386_RELATIVE,
1602 output_section, data_shndx,
1603 reloc.get_r_offset());
1605 // Create a GOT entry for the tp-relative offset.
1606 Output_data_got<32, false>* got
1607 = target->got_section(symtab, layout);
1608 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1609 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1610 ? elfcpp::R_386_TLS_TPOFF32
1611 : elfcpp::R_386_TLS_TPOFF);
1612 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1613 ? GOT_TYPE_TLS_OFFSET
1614 : GOT_TYPE_TLS_NOFFSET);
1615 got->add_local_with_rel(object, r_sym, got_type,
1616 target->rel_dyn_section(layout),
1619 else if (optimized_type != tls::TLSOPT_TO_LE)
1620 unsupported_reloc_local(object, r_type);
1623 case elfcpp::R_386_TLS_LE: // Local-exec
1624 case elfcpp::R_386_TLS_LE_32:
1625 layout->set_has_static_tls();
1626 if (output_is_shared)
1628 // We need to create a dynamic relocation.
1629 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1630 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1631 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1632 ? elfcpp::R_386_TLS_TPOFF32
1633 : elfcpp::R_386_TLS_TPOFF);
1634 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1635 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1636 data_shndx, reloc.get_r_offset());
1646 case elfcpp::R_386_32PLT:
1647 case elfcpp::R_386_TLS_GD_32:
1648 case elfcpp::R_386_TLS_GD_PUSH:
1649 case elfcpp::R_386_TLS_GD_CALL:
1650 case elfcpp::R_386_TLS_GD_POP:
1651 case elfcpp::R_386_TLS_LDM_32:
1652 case elfcpp::R_386_TLS_LDM_PUSH:
1653 case elfcpp::R_386_TLS_LDM_CALL:
1654 case elfcpp::R_386_TLS_LDM_POP:
1655 case elfcpp::R_386_USED_BY_INTEL_200:
1657 unsupported_reloc_local(object, r_type);
1662 // Report an unsupported relocation against a global symbol.
1665 Target_i386::Scan::unsupported_reloc_global(
1666 Sized_relobj_file<32, false>* object,
1667 unsigned int r_type,
1670 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1671 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1675 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
1679 case elfcpp::R_386_32:
1680 case elfcpp::R_386_16:
1681 case elfcpp::R_386_8:
1682 case elfcpp::R_386_GOTOFF:
1683 case elfcpp::R_386_GOT32:
1694 Target_i386::Scan::local_reloc_may_be_function_pointer(
1698 Sized_relobj_file<32, false>* ,
1701 const elfcpp::Rel<32, false>& ,
1702 unsigned int r_type,
1703 const elfcpp::Sym<32, false>&)
1705 return possible_function_pointer_reloc(r_type);
1709 Target_i386::Scan::global_reloc_may_be_function_pointer(
1713 Sized_relobj_file<32, false>* ,
1716 const elfcpp::Rel<32, false>& ,
1717 unsigned int r_type,
1720 return possible_function_pointer_reloc(r_type);
1723 // Scan a relocation for a global symbol.
1726 Target_i386::Scan::global(Symbol_table* symtab,
1728 Target_i386* target,
1729 Sized_relobj_file<32, false>* object,
1730 unsigned int data_shndx,
1731 Output_section* output_section,
1732 const elfcpp::Rel<32, false>& reloc,
1733 unsigned int r_type,
1736 // A STT_GNU_IFUNC symbol may require a PLT entry.
1737 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1738 && this->reloc_needs_plt_for_ifunc(object, r_type))
1739 target->make_plt_entry(symtab, layout, gsym);
1743 case elfcpp::R_386_NONE:
1744 case elfcpp::R_386_GNU_VTINHERIT:
1745 case elfcpp::R_386_GNU_VTENTRY:
1748 case elfcpp::R_386_32:
1749 case elfcpp::R_386_16:
1750 case elfcpp::R_386_8:
1752 // Make a PLT entry if necessary.
1753 if (gsym->needs_plt_entry())
1755 target->make_plt_entry(symtab, layout, gsym);
1756 // Since this is not a PC-relative relocation, we may be
1757 // taking the address of a function. In that case we need to
1758 // set the entry in the dynamic symbol table to the address of
1760 if (gsym->is_from_dynobj() && !parameters->options().shared())
1761 gsym->set_needs_dynsym_value();
1763 // Make a dynamic relocation if necessary.
1764 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1766 if (gsym->may_need_copy_reloc())
1768 target->copy_reloc(symtab, layout, object,
1769 data_shndx, output_section, gsym, reloc);
1771 else if (r_type == elfcpp::R_386_32
1772 && gsym->type() == elfcpp::STT_GNU_IFUNC
1773 && gsym->can_use_relative_reloc(false)
1774 && !gsym->is_from_dynobj()
1775 && !gsym->is_undefined()
1776 && !gsym->is_preemptible())
1778 // Use an IRELATIVE reloc for a locally defined
1779 // STT_GNU_IFUNC symbol. This makes a function
1780 // address in a PIE executable match the address in a
1781 // shared library that it links against.
1782 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1783 rel_dyn->add_symbolless_global_addend(gsym,
1784 elfcpp::R_386_IRELATIVE,
1787 reloc.get_r_offset());
1789 else if (r_type == elfcpp::R_386_32
1790 && gsym->can_use_relative_reloc(false))
1792 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1793 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1794 output_section, object,
1795 data_shndx, reloc.get_r_offset());
1799 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1800 rel_dyn->add_global(gsym, r_type, output_section, object,
1801 data_shndx, reloc.get_r_offset());
1807 case elfcpp::R_386_PC32:
1808 case elfcpp::R_386_PC16:
1809 case elfcpp::R_386_PC8:
1811 // Make a PLT entry if necessary.
1812 if (gsym->needs_plt_entry())
1814 // These relocations are used for function calls only in
1815 // non-PIC code. For a 32-bit relocation in a shared library,
1816 // we'll need a text relocation anyway, so we can skip the
1817 // PLT entry and let the dynamic linker bind the call directly
1818 // to the target. For smaller relocations, we should use a
1819 // PLT entry to ensure that the call can reach.
1820 if (!parameters->options().shared()
1821 || r_type != elfcpp::R_386_PC32)
1822 target->make_plt_entry(symtab, layout, gsym);
1824 // Make a dynamic relocation if necessary.
1825 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1827 if (gsym->may_need_copy_reloc())
1829 target->copy_reloc(symtab, layout, object,
1830 data_shndx, output_section, gsym, reloc);
1834 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1835 rel_dyn->add_global(gsym, r_type, output_section, object,
1836 data_shndx, reloc.get_r_offset());
1842 case elfcpp::R_386_GOT32:
1844 // The symbol requires a GOT entry.
1845 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1846 if (gsym->final_value_is_known())
1848 // For a STT_GNU_IFUNC symbol we want the PLT address.
1849 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1850 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1852 got->add_global(gsym, GOT_TYPE_STANDARD);
1856 // If this symbol is not fully resolved, we need to add a
1857 // GOT entry with a dynamic relocation.
1858 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1859 if (gsym->is_from_dynobj()
1860 || gsym->is_undefined()
1861 || gsym->is_preemptible()
1862 || (gsym->type() == elfcpp::STT_GNU_IFUNC
1863 && parameters->options().output_is_position_independent()))
1864 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1865 rel_dyn, elfcpp::R_386_GLOB_DAT);
1868 // For a STT_GNU_IFUNC symbol we want to write the PLT
1869 // offset into the GOT, so that function pointer
1870 // comparisons work correctly.
1872 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
1873 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
1876 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1877 // Tell the dynamic linker to use the PLT address
1878 // when resolving relocations.
1879 if (gsym->is_from_dynobj()
1880 && !parameters->options().shared())
1881 gsym->set_needs_dynsym_value();
1885 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
1886 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1894 case elfcpp::R_386_PLT32:
1895 // If the symbol is fully resolved, this is just a PC32 reloc.
1896 // Otherwise we need a PLT entry.
1897 if (gsym->final_value_is_known())
1899 // If building a shared library, we can also skip the PLT entry
1900 // if the symbol is defined in the output file and is protected
1902 if (gsym->is_defined()
1903 && !gsym->is_from_dynobj()
1904 && !gsym->is_preemptible())
1906 target->make_plt_entry(symtab, layout, gsym);
1909 case elfcpp::R_386_GOTOFF:
1910 case elfcpp::R_386_GOTPC:
1911 // We need a GOT section.
1912 target->got_section(symtab, layout);
1915 // These are relocations which should only be seen by the
1916 // dynamic linker, and should never be seen here.
1917 case elfcpp::R_386_COPY:
1918 case elfcpp::R_386_GLOB_DAT:
1919 case elfcpp::R_386_JUMP_SLOT:
1920 case elfcpp::R_386_RELATIVE:
1921 case elfcpp::R_386_IRELATIVE:
1922 case elfcpp::R_386_TLS_TPOFF:
1923 case elfcpp::R_386_TLS_DTPMOD32:
1924 case elfcpp::R_386_TLS_DTPOFF32:
1925 case elfcpp::R_386_TLS_TPOFF32:
1926 case elfcpp::R_386_TLS_DESC:
1927 gold_error(_("%s: unexpected reloc %u in object file"),
1928 object->name().c_str(), r_type);
1931 // These are initial tls relocs, which are expected when
1933 case elfcpp::R_386_TLS_GD: // Global-dynamic
1934 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1935 case elfcpp::R_386_TLS_DESC_CALL:
1936 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1937 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1938 case elfcpp::R_386_TLS_IE: // Initial-exec
1939 case elfcpp::R_386_TLS_IE_32:
1940 case elfcpp::R_386_TLS_GOTIE:
1941 case elfcpp::R_386_TLS_LE: // Local-exec
1942 case elfcpp::R_386_TLS_LE_32:
1944 const bool is_final = gsym->final_value_is_known();
1945 const tls::Tls_optimization optimized_type
1946 = Target_i386::optimize_tls_reloc(is_final, r_type);
1949 case elfcpp::R_386_TLS_GD: // Global-dynamic
1950 if (optimized_type == tls::TLSOPT_NONE)
1952 // Create a pair of GOT entries for the module index and
1953 // dtv-relative offset.
1954 Output_data_got<32, false>* got
1955 = target->got_section(symtab, layout);
1956 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1957 target->rel_dyn_section(layout),
1958 elfcpp::R_386_TLS_DTPMOD32,
1959 elfcpp::R_386_TLS_DTPOFF32);
1961 else if (optimized_type == tls::TLSOPT_TO_IE)
1963 // Create a GOT entry for the tp-relative offset.
1964 Output_data_got<32, false>* got
1965 = target->got_section(symtab, layout);
1966 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1967 target->rel_dyn_section(layout),
1968 elfcpp::R_386_TLS_TPOFF);
1970 else if (optimized_type != tls::TLSOPT_TO_LE)
1971 unsupported_reloc_global(object, r_type, gsym);
1974 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1975 target->define_tls_base_symbol(symtab, layout);
1976 if (optimized_type == tls::TLSOPT_NONE)
1978 // Create a double GOT entry with an R_386_TLS_DESC
1979 // reloc. The R_386_TLS_DESC reloc is resolved
1980 // lazily, so the GOT entry needs to be in an area in
1981 // .got.plt, not .got. Call got_section to make sure
1982 // the section has been created.
1983 target->got_section(symtab, layout);
1984 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1985 Reloc_section* rt = target->rel_tls_desc_section(layout);
1986 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
1987 elfcpp::R_386_TLS_DESC, 0);
1989 else if (optimized_type == tls::TLSOPT_TO_IE)
1991 // Create a GOT entry for the tp-relative offset.
1992 Output_data_got<32, false>* got
1993 = target->got_section(symtab, layout);
1994 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1995 target->rel_dyn_section(layout),
1996 elfcpp::R_386_TLS_TPOFF);
1998 else if (optimized_type != tls::TLSOPT_TO_LE)
1999 unsupported_reloc_global(object, r_type, gsym);
2002 case elfcpp::R_386_TLS_DESC_CALL:
2005 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2006 if (optimized_type == tls::TLSOPT_NONE)
2008 // Create a GOT entry for the module index.
2009 target->got_mod_index_entry(symtab, layout, object);
2011 else if (optimized_type != tls::TLSOPT_TO_LE)
2012 unsupported_reloc_global(object, r_type, gsym);
2015 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2018 case elfcpp::R_386_TLS_IE: // Initial-exec
2019 case elfcpp::R_386_TLS_IE_32:
2020 case elfcpp::R_386_TLS_GOTIE:
2021 layout->set_has_static_tls();
2022 if (optimized_type == tls::TLSOPT_NONE)
2024 // For the R_386_TLS_IE relocation, we need to create a
2025 // dynamic relocation when building a shared library.
2026 if (r_type == elfcpp::R_386_TLS_IE
2027 && parameters->options().shared())
2029 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2030 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2031 output_section, object,
2033 reloc.get_r_offset());
2035 // Create a GOT entry for the tp-relative offset.
2036 Output_data_got<32, false>* got
2037 = target->got_section(symtab, layout);
2038 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
2039 ? elfcpp::R_386_TLS_TPOFF32
2040 : elfcpp::R_386_TLS_TPOFF);
2041 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2042 ? GOT_TYPE_TLS_OFFSET
2043 : GOT_TYPE_TLS_NOFFSET);
2044 got->add_global_with_rel(gsym, got_type,
2045 target->rel_dyn_section(layout),
2048 else if (optimized_type != tls::TLSOPT_TO_LE)
2049 unsupported_reloc_global(object, r_type, gsym);
2052 case elfcpp::R_386_TLS_LE: // Local-exec
2053 case elfcpp::R_386_TLS_LE_32:
2054 layout->set_has_static_tls();
2055 if (parameters->options().shared())
2057 // We need to create a dynamic relocation.
2058 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
2059 ? elfcpp::R_386_TLS_TPOFF32
2060 : elfcpp::R_386_TLS_TPOFF);
2061 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2062 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
2063 data_shndx, reloc.get_r_offset());
2073 case elfcpp::R_386_32PLT:
2074 case elfcpp::R_386_TLS_GD_32:
2075 case elfcpp::R_386_TLS_GD_PUSH:
2076 case elfcpp::R_386_TLS_GD_CALL:
2077 case elfcpp::R_386_TLS_GD_POP:
2078 case elfcpp::R_386_TLS_LDM_32:
2079 case elfcpp::R_386_TLS_LDM_PUSH:
2080 case elfcpp::R_386_TLS_LDM_CALL:
2081 case elfcpp::R_386_TLS_LDM_POP:
2082 case elfcpp::R_386_USED_BY_INTEL_200:
2084 unsupported_reloc_global(object, r_type, gsym);
2089 // Process relocations for gc.
2092 Target_i386::gc_process_relocs(Symbol_table* symtab,
2094 Sized_relobj_file<32, false>* object,
2095 unsigned int data_shndx,
2097 const unsigned char* prelocs,
2099 Output_section* output_section,
2100 bool needs_special_offset_handling,
2101 size_t local_symbol_count,
2102 const unsigned char* plocal_symbols)
2104 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2106 Target_i386::Relocatable_size_for_reloc>(
2115 needs_special_offset_handling,
2120 // Scan relocations for a section.
2123 Target_i386::scan_relocs(Symbol_table* symtab,
2125 Sized_relobj_file<32, false>* object,
2126 unsigned int data_shndx,
2127 unsigned int sh_type,
2128 const unsigned char* prelocs,
2130 Output_section* output_section,
2131 bool needs_special_offset_handling,
2132 size_t local_symbol_count,
2133 const unsigned char* plocal_symbols)
2135 if (sh_type == elfcpp::SHT_RELA)
2137 gold_error(_("%s: unsupported RELA reloc section"),
2138 object->name().c_str());
2142 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2152 needs_special_offset_handling,
2157 // Finalize the sections.
2160 Target_i386::do_finalize_sections(
2162 const Input_objects*,
2163 Symbol_table* symtab)
2165 const Reloc_section* rel_plt = (this->plt_ == NULL
2167 : this->plt_->rel_plt());
2168 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2169 this->rel_dyn_, true, false);
2171 // Emit any relocs we saved in an attempt to avoid generating COPY
2173 if (this->copy_relocs_.any_saved_relocs())
2174 this->copy_relocs_.emit(this->rel_dyn_section(layout));
2176 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2177 // the .got.plt section.
2178 Symbol* sym = this->global_offset_table_;
2181 uint32_t data_size = this->got_plt_->current_data_size();
2182 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2186 // Return whether a direct absolute static relocation needs to be applied.
2187 // In cases where Scan::local() or Scan::global() has created
2188 // a dynamic relocation other than R_386_RELATIVE, the addend
2189 // of the relocation is carried in the data, and we must not
2190 // apply the static relocation.
2193 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2194 unsigned int r_type,
2196 Output_section* output_section)
2198 // If the output section is not allocated, then we didn't call
2199 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2201 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2204 int ref_flags = Scan::get_reference_flags(r_type);
2206 // For local symbols, we will have created a non-RELATIVE dynamic
2207 // relocation only if (a) the output is position independent,
2208 // (b) the relocation is absolute (not pc- or segment-relative), and
2209 // (c) the relocation is not 32 bits wide.
2211 return !(parameters->options().output_is_position_independent()
2212 && (ref_flags & Symbol::ABSOLUTE_REF)
2215 // For global symbols, we use the same helper routines used in the
2216 // scan pass. If we did not create a dynamic relocation, or if we
2217 // created a RELATIVE dynamic relocation, we should apply the static
2219 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2220 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2221 && gsym->can_use_relative_reloc(ref_flags
2222 & Symbol::FUNCTION_CALL);
2223 return !has_dyn || is_rel;
2226 // Perform a relocation.
2229 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2230 Target_i386* target,
2231 Output_section* output_section,
2233 const elfcpp::Rel<32, false>& rel,
2234 unsigned int r_type,
2235 const Sized_symbol<32>* gsym,
2236 const Symbol_value<32>* psymval,
2237 unsigned char* view,
2238 elfcpp::Elf_types<32>::Elf_Addr address,
2239 section_size_type view_size)
2241 if (this->skip_call_tls_get_addr_)
2243 if ((r_type != elfcpp::R_386_PLT32
2244 && r_type != elfcpp::R_386_PC32)
2246 || strcmp(gsym->name(), "___tls_get_addr") != 0)
2247 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2248 _("missing expected TLS relocation"));
2251 this->skip_call_tls_get_addr_ = false;
2256 const Sized_relobj_file<32, false>* object = relinfo->object;
2258 // Pick the value to use for symbols defined in shared objects.
2259 Symbol_value<32> symval;
2261 && gsym->type() == elfcpp::STT_GNU_IFUNC
2262 && r_type == elfcpp::R_386_32
2263 && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
2264 && gsym->can_use_relative_reloc(false)
2265 && !gsym->is_from_dynobj()
2266 && !gsym->is_undefined()
2267 && !gsym->is_preemptible())
2269 // In this case we are generating a R_386_IRELATIVE reloc. We
2270 // want to use the real value of the symbol, not the PLT offset.
2272 else if (gsym != NULL
2273 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2275 symval.set_output_value(target->plt_section()->address()
2276 + gsym->plt_offset());
2279 else if (gsym == NULL && psymval->is_ifunc_symbol())
2281 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2282 if (object->local_has_plt_offset(r_sym))
2284 symval.set_output_value(target->plt_section()->address()
2285 + object->local_plt_offset(r_sym));
2290 // Get the GOT offset if needed.
2291 // The GOT pointer points to the end of the GOT section.
2292 // We need to subtract the size of the GOT section to get
2293 // the actual offset to use in the relocation.
2294 bool have_got_offset = false;
2295 unsigned int got_offset = 0;
2298 case elfcpp::R_386_GOT32:
2301 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2302 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2303 - target->got_size());
2307 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2308 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2309 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2310 - target->got_size());
2312 have_got_offset = true;
2321 case elfcpp::R_386_NONE:
2322 case elfcpp::R_386_GNU_VTINHERIT:
2323 case elfcpp::R_386_GNU_VTENTRY:
2326 case elfcpp::R_386_32:
2327 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2328 Relocate_functions<32, false>::rel32(view, object, psymval);
2331 case elfcpp::R_386_PC32:
2332 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2333 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2336 case elfcpp::R_386_16:
2337 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2338 Relocate_functions<32, false>::rel16(view, object, psymval);
2341 case elfcpp::R_386_PC16:
2342 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2343 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2346 case elfcpp::R_386_8:
2347 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2348 Relocate_functions<32, false>::rel8(view, object, psymval);
2351 case elfcpp::R_386_PC8:
2352 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2353 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2356 case elfcpp::R_386_PLT32:
2357 gold_assert(gsym == NULL
2358 || gsym->has_plt_offset()
2359 || gsym->final_value_is_known()
2360 || (gsym->is_defined()
2361 && !gsym->is_from_dynobj()
2362 && !gsym->is_preemptible()));
2363 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2366 case elfcpp::R_386_GOT32:
2367 gold_assert(have_got_offset);
2368 Relocate_functions<32, false>::rel32(view, got_offset);
2371 case elfcpp::R_386_GOTOFF:
2373 elfcpp::Elf_types<32>::Elf_Addr value;
2374 value = (psymval->value(object, 0)
2375 - target->got_plt_section()->address());
2376 Relocate_functions<32, false>::rel32(view, value);
2380 case elfcpp::R_386_GOTPC:
2382 elfcpp::Elf_types<32>::Elf_Addr value;
2383 value = target->got_plt_section()->address();
2384 Relocate_functions<32, false>::pcrel32(view, value, address);
2388 case elfcpp::R_386_COPY:
2389 case elfcpp::R_386_GLOB_DAT:
2390 case elfcpp::R_386_JUMP_SLOT:
2391 case elfcpp::R_386_RELATIVE:
2392 case elfcpp::R_386_IRELATIVE:
2393 // These are outstanding tls relocs, which are unexpected when
2395 case elfcpp::R_386_TLS_TPOFF:
2396 case elfcpp::R_386_TLS_DTPMOD32:
2397 case elfcpp::R_386_TLS_DTPOFF32:
2398 case elfcpp::R_386_TLS_TPOFF32:
2399 case elfcpp::R_386_TLS_DESC:
2400 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2401 _("unexpected reloc %u in object file"),
2405 // These are initial tls relocs, which are expected when
2407 case elfcpp::R_386_TLS_GD: // Global-dynamic
2408 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2409 case elfcpp::R_386_TLS_DESC_CALL:
2410 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2411 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2412 case elfcpp::R_386_TLS_IE: // Initial-exec
2413 case elfcpp::R_386_TLS_IE_32:
2414 case elfcpp::R_386_TLS_GOTIE:
2415 case elfcpp::R_386_TLS_LE: // Local-exec
2416 case elfcpp::R_386_TLS_LE_32:
2417 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2418 view, address, view_size);
2421 case elfcpp::R_386_32PLT:
2422 case elfcpp::R_386_TLS_GD_32:
2423 case elfcpp::R_386_TLS_GD_PUSH:
2424 case elfcpp::R_386_TLS_GD_CALL:
2425 case elfcpp::R_386_TLS_GD_POP:
2426 case elfcpp::R_386_TLS_LDM_32:
2427 case elfcpp::R_386_TLS_LDM_PUSH:
2428 case elfcpp::R_386_TLS_LDM_CALL:
2429 case elfcpp::R_386_TLS_LDM_POP:
2430 case elfcpp::R_386_USED_BY_INTEL_200:
2432 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2433 _("unsupported reloc %u"),
2441 // Perform a TLS relocation.
2444 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
2445 Target_i386* target,
2447 const elfcpp::Rel<32, false>& rel,
2448 unsigned int r_type,
2449 const Sized_symbol<32>* gsym,
2450 const Symbol_value<32>* psymval,
2451 unsigned char* view,
2452 elfcpp::Elf_types<32>::Elf_Addr,
2453 section_size_type view_size)
2455 Output_segment* tls_segment = relinfo->layout->tls_segment();
2457 const Sized_relobj_file<32, false>* object = relinfo->object;
2459 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
2461 const bool is_final = (gsym == NULL
2462 ? !parameters->options().shared()
2463 : gsym->final_value_is_known());
2464 const tls::Tls_optimization optimized_type
2465 = Target_i386::optimize_tls_reloc(is_final, r_type);
2468 case elfcpp::R_386_TLS_GD: // Global-dynamic
2469 if (optimized_type == tls::TLSOPT_TO_LE)
2471 gold_assert(tls_segment != NULL);
2472 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2473 rel, r_type, value, view,
2479 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2480 ? GOT_TYPE_TLS_NOFFSET
2481 : GOT_TYPE_TLS_PAIR);
2482 unsigned int got_offset;
2485 gold_assert(gsym->has_got_offset(got_type));
2486 got_offset = gsym->got_offset(got_type) - target->got_size();
2490 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2491 gold_assert(object->local_has_got_offset(r_sym, got_type));
2492 got_offset = (object->local_got_offset(r_sym, got_type)
2493 - target->got_size());
2495 if (optimized_type == tls::TLSOPT_TO_IE)
2497 gold_assert(tls_segment != NULL);
2498 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2499 got_offset, view, view_size);
2502 else if (optimized_type == tls::TLSOPT_NONE)
2504 // Relocate the field with the offset of the pair of GOT
2506 Relocate_functions<32, false>::rel32(view, got_offset);
2510 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2511 _("unsupported reloc %u"),
2515 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2516 case elfcpp::R_386_TLS_DESC_CALL:
2517 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2518 if (optimized_type == tls::TLSOPT_TO_LE)
2520 gold_assert(tls_segment != NULL);
2521 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2522 rel, r_type, value, view,
2528 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2529 ? GOT_TYPE_TLS_NOFFSET
2530 : GOT_TYPE_TLS_DESC);
2531 unsigned int got_offset = 0;
2532 if (r_type == elfcpp::R_386_TLS_GOTDESC
2533 && optimized_type == tls::TLSOPT_NONE)
2535 // We created GOT entries in the .got.tlsdesc portion of
2536 // the .got.plt section, but the offset stored in the
2537 // symbol is the offset within .got.tlsdesc.
2538 got_offset = (target->got_size()
2539 + target->got_plt_section()->data_size());
2543 gold_assert(gsym->has_got_offset(got_type));
2544 got_offset += gsym->got_offset(got_type) - target->got_size();
2548 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2549 gold_assert(object->local_has_got_offset(r_sym, got_type));
2550 got_offset += (object->local_got_offset(r_sym, got_type)
2551 - target->got_size());
2553 if (optimized_type == tls::TLSOPT_TO_IE)
2555 gold_assert(tls_segment != NULL);
2556 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2557 got_offset, view, view_size);
2560 else if (optimized_type == tls::TLSOPT_NONE)
2562 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2564 // Relocate the field with the offset of the pair of GOT
2566 Relocate_functions<32, false>::rel32(view, got_offset);
2571 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2572 _("unsupported reloc %u"),
2576 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2577 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2579 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2580 _("both SUN and GNU model "
2581 "TLS relocations"));
2584 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2585 if (optimized_type == tls::TLSOPT_TO_LE)
2587 gold_assert(tls_segment != NULL);
2588 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2589 value, view, view_size);
2592 else if (optimized_type == tls::TLSOPT_NONE)
2594 // Relocate the field with the offset of the GOT entry for
2595 // the module index.
2596 unsigned int got_offset;
2597 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2598 - target->got_size());
2599 Relocate_functions<32, false>::rel32(view, got_offset);
2602 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2603 _("unsupported reloc %u"),
2607 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2608 if (optimized_type == tls::TLSOPT_TO_LE)
2610 // This reloc can appear in debugging sections, in which
2611 // case we must not convert to local-exec. We decide what
2612 // to do based on whether the section is marked as
2613 // containing executable code. That is what the GNU linker
2615 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2616 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2618 gold_assert(tls_segment != NULL);
2619 value -= tls_segment->memsz();
2622 Relocate_functions<32, false>::rel32(view, value);
2625 case elfcpp::R_386_TLS_IE: // Initial-exec
2626 case elfcpp::R_386_TLS_GOTIE:
2627 case elfcpp::R_386_TLS_IE_32:
2628 if (optimized_type == tls::TLSOPT_TO_LE)
2630 gold_assert(tls_segment != NULL);
2631 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2632 rel, r_type, value, view,
2636 else if (optimized_type == tls::TLSOPT_NONE)
2638 // Relocate the field with the offset of the GOT entry for
2639 // the tp-relative offset of the symbol.
2640 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2641 ? GOT_TYPE_TLS_OFFSET
2642 : GOT_TYPE_TLS_NOFFSET);
2643 unsigned int got_offset;
2646 gold_assert(gsym->has_got_offset(got_type));
2647 got_offset = gsym->got_offset(got_type);
2651 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2652 gold_assert(object->local_has_got_offset(r_sym, got_type));
2653 got_offset = object->local_got_offset(r_sym, got_type);
2655 // For the R_386_TLS_IE relocation, we need to apply the
2656 // absolute address of the GOT entry.
2657 if (r_type == elfcpp::R_386_TLS_IE)
2658 got_offset += target->got_plt_section()->address();
2659 // All GOT offsets are relative to the end of the GOT.
2660 got_offset -= target->got_size();
2661 Relocate_functions<32, false>::rel32(view, got_offset);
2664 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2665 _("unsupported reloc %u"),
2669 case elfcpp::R_386_TLS_LE: // Local-exec
2670 // If we're creating a shared library, a dynamic relocation will
2671 // have been created for this location, so do not apply it now.
2672 if (!parameters->options().shared())
2674 gold_assert(tls_segment != NULL);
2675 value -= tls_segment->memsz();
2676 Relocate_functions<32, false>::rel32(view, value);
2680 case elfcpp::R_386_TLS_LE_32:
2681 // If we're creating a shared library, a dynamic relocation will
2682 // have been created for this location, so do not apply it now.
2683 if (!parameters->options().shared())
2685 gold_assert(tls_segment != NULL);
2686 value = tls_segment->memsz() - value;
2687 Relocate_functions<32, false>::rel32(view, value);
2693 // Do a relocation in which we convert a TLS General-Dynamic to a
2697 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2699 Output_segment* tls_segment,
2700 const elfcpp::Rel<32, false>& rel,
2702 elfcpp::Elf_types<32>::Elf_Addr value,
2703 unsigned char* view,
2704 section_size_type view_size)
2706 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2707 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2708 // leal foo(%reg),%eax; call ___tls_get_addr
2709 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2711 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2712 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2714 unsigned char op1 = view[-1];
2715 unsigned char op2 = view[-2];
2717 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2718 op2 == 0x8d || op2 == 0x04);
2719 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2725 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2726 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2727 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2728 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2729 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2733 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2734 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2735 if (rel.get_r_offset() + 9 < view_size
2738 // There is a trailing nop. Use the size byte subl.
2739 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2744 // Use the five byte subl.
2745 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2749 value = tls_segment->memsz() - value;
2750 Relocate_functions<32, false>::rel32(view + roff, value);
2752 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2754 this->skip_call_tls_get_addr_ = true;
2757 // Do a relocation in which we convert a TLS General-Dynamic to an
2761 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2764 const elfcpp::Rel<32, false>& rel,
2766 elfcpp::Elf_types<32>::Elf_Addr value,
2767 unsigned char* view,
2768 section_size_type view_size)
2770 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2771 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2773 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2774 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2776 unsigned char op1 = view[-1];
2777 unsigned char op2 = view[-2];
2779 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2780 op2 == 0x8d || op2 == 0x04);
2781 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2785 // FIXME: For now, support only the first (SIB) form.
2786 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2790 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2791 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2792 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2793 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2794 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2798 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2799 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2800 if (rel.get_r_offset() + 9 < view_size
2803 // FIXME: This is not the right instruction sequence.
2804 // There is a trailing nop. Use the size byte subl.
2805 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2810 // FIXME: This is not the right instruction sequence.
2811 // Use the five byte subl.
2812 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2816 Relocate_functions<32, false>::rel32(view + roff, value);
2818 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2820 this->skip_call_tls_get_addr_ = true;
2823 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2824 // General-Dynamic to a Local-Exec.
2827 Target_i386::Relocate::tls_desc_gd_to_le(
2828 const Relocate_info<32, false>* relinfo,
2830 Output_segment* tls_segment,
2831 const elfcpp::Rel<32, false>& rel,
2832 unsigned int r_type,
2833 elfcpp::Elf_types<32>::Elf_Addr value,
2834 unsigned char* view,
2835 section_size_type view_size)
2837 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2839 // leal foo@TLSDESC(%ebx), %eax
2840 // ==> leal foo@NTPOFF, %eax
2841 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2842 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2843 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2844 view[-2] == 0x8d && view[-1] == 0x83);
2846 value -= tls_segment->memsz();
2847 Relocate_functions<32, false>::rel32(view, value);
2851 // call *foo@TLSCALL(%eax)
2853 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2854 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2855 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2856 view[0] == 0xff && view[1] == 0x10);
2862 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2863 // General-Dynamic to an Initial-Exec.
2866 Target_i386::Relocate::tls_desc_gd_to_ie(
2867 const Relocate_info<32, false>* relinfo,
2870 const elfcpp::Rel<32, false>& rel,
2871 unsigned int r_type,
2872 elfcpp::Elf_types<32>::Elf_Addr value,
2873 unsigned char* view,
2874 section_size_type view_size)
2876 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2878 // leal foo@TLSDESC(%ebx), %eax
2879 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2880 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2881 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2882 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2883 view[-2] == 0x8d && view[-1] == 0x83);
2885 Relocate_functions<32, false>::rel32(view, value);
2889 // call *foo@TLSCALL(%eax)
2891 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2892 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2893 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2894 view[0] == 0xff && view[1] == 0x10);
2900 // Do a relocation in which we convert a TLS Local-Dynamic to a
2904 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2907 const elfcpp::Rel<32, false>& rel,
2909 elfcpp::Elf_types<32>::Elf_Addr,
2910 unsigned char* view,
2911 section_size_type view_size)
2913 // leal foo(%reg), %eax; call ___tls_get_addr
2914 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2916 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2917 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2919 // FIXME: Does this test really always pass?
2920 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2921 view[-2] == 0x8d && view[-1] == 0x83);
2923 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2925 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2927 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2929 this->skip_call_tls_get_addr_ = true;
2932 // Do a relocation in which we convert a TLS Initial-Exec to a
2936 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2938 Output_segment* tls_segment,
2939 const elfcpp::Rel<32, false>& rel,
2940 unsigned int r_type,
2941 elfcpp::Elf_types<32>::Elf_Addr value,
2942 unsigned char* view,
2943 section_size_type view_size)
2945 // We have to actually change the instructions, which means that we
2946 // need to examine the opcodes to figure out which instruction we
2948 if (r_type == elfcpp::R_386_TLS_IE)
2950 // movl %gs:XX,%eax ==> movl $YY,%eax
2951 // movl %gs:XX,%reg ==> movl $YY,%reg
2952 // addl %gs:XX,%reg ==> addl $YY,%reg
2953 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2954 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2956 unsigned char op1 = view[-1];
2959 // movl XX,%eax ==> movl $YY,%eax
2964 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2966 unsigned char op2 = view[-2];
2969 // movl XX,%reg ==> movl $YY,%reg
2970 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2971 (op1 & 0xc7) == 0x05);
2973 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2975 else if (op2 == 0x03)
2977 // addl XX,%reg ==> addl $YY,%reg
2978 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2979 (op1 & 0xc7) == 0x05);
2981 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2984 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2989 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2990 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2991 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2992 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2993 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2995 unsigned char op1 = view[-1];
2996 unsigned char op2 = view[-2];
2997 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2998 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
3001 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3003 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3005 else if (op2 == 0x2b)
3007 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3009 view[-1] = 0xe8 | ((op1 >> 3) & 7);
3011 else if (op2 == 0x03)
3013 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3015 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3018 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3021 value = tls_segment->memsz() - value;
3022 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
3025 Relocate_functions<32, false>::rel32(view, value);
3028 // Relocate section data.
3031 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
3032 unsigned int sh_type,
3033 const unsigned char* prelocs,
3035 Output_section* output_section,
3036 bool needs_special_offset_handling,
3037 unsigned char* view,
3038 elfcpp::Elf_types<32>::Elf_Addr address,
3039 section_size_type view_size,
3040 const Reloc_symbol_changes* reloc_symbol_changes)
3042 gold_assert(sh_type == elfcpp::SHT_REL);
3044 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
3045 Target_i386::Relocate>(
3051 needs_special_offset_handling,
3055 reloc_symbol_changes);
3058 // Return the size of a relocation while scanning during a relocatable
3062 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3063 unsigned int r_type,
3068 case elfcpp::R_386_NONE:
3069 case elfcpp::R_386_GNU_VTINHERIT:
3070 case elfcpp::R_386_GNU_VTENTRY:
3071 case elfcpp::R_386_TLS_GD: // Global-dynamic
3072 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
3073 case elfcpp::R_386_TLS_DESC_CALL:
3074 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3075 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3076 case elfcpp::R_386_TLS_IE: // Initial-exec
3077 case elfcpp::R_386_TLS_IE_32:
3078 case elfcpp::R_386_TLS_GOTIE:
3079 case elfcpp::R_386_TLS_LE: // Local-exec
3080 case elfcpp::R_386_TLS_LE_32:
3083 case elfcpp::R_386_32:
3084 case elfcpp::R_386_PC32:
3085 case elfcpp::R_386_GOT32:
3086 case elfcpp::R_386_PLT32:
3087 case elfcpp::R_386_GOTOFF:
3088 case elfcpp::R_386_GOTPC:
3091 case elfcpp::R_386_16:
3092 case elfcpp::R_386_PC16:
3095 case elfcpp::R_386_8:
3096 case elfcpp::R_386_PC8:
3099 // These are relocations which should only be seen by the
3100 // dynamic linker, and should never be seen here.
3101 case elfcpp::R_386_COPY:
3102 case elfcpp::R_386_GLOB_DAT:
3103 case elfcpp::R_386_JUMP_SLOT:
3104 case elfcpp::R_386_RELATIVE:
3105 case elfcpp::R_386_IRELATIVE:
3106 case elfcpp::R_386_TLS_TPOFF:
3107 case elfcpp::R_386_TLS_DTPMOD32:
3108 case elfcpp::R_386_TLS_DTPOFF32:
3109 case elfcpp::R_386_TLS_TPOFF32:
3110 case elfcpp::R_386_TLS_DESC:
3111 object->error(_("unexpected reloc %u in object file"), r_type);
3114 case elfcpp::R_386_32PLT:
3115 case elfcpp::R_386_TLS_GD_32:
3116 case elfcpp::R_386_TLS_GD_PUSH:
3117 case elfcpp::R_386_TLS_GD_CALL:
3118 case elfcpp::R_386_TLS_GD_POP:
3119 case elfcpp::R_386_TLS_LDM_32:
3120 case elfcpp::R_386_TLS_LDM_PUSH:
3121 case elfcpp::R_386_TLS_LDM_CALL:
3122 case elfcpp::R_386_TLS_LDM_POP:
3123 case elfcpp::R_386_USED_BY_INTEL_200:
3125 object->error(_("unsupported reloc %u in object file"), r_type);
3130 // Scan the relocs during a relocatable link.
3133 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3135 Sized_relobj_file<32, false>* object,
3136 unsigned int data_shndx,
3137 unsigned int sh_type,
3138 const unsigned char* prelocs,
3140 Output_section* output_section,
3141 bool needs_special_offset_handling,
3142 size_t local_symbol_count,
3143 const unsigned char* plocal_symbols,
3144 Relocatable_relocs* rr)
3146 gold_assert(sh_type == elfcpp::SHT_REL);
3148 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3149 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3151 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
3152 Scan_relocatable_relocs>(
3160 needs_special_offset_handling,
3166 // Relocate a section during a relocatable link.
3169 Target_i386::relocate_for_relocatable(
3170 const Relocate_info<32, false>* relinfo,
3171 unsigned int sh_type,
3172 const unsigned char* prelocs,
3174 Output_section* output_section,
3175 off_t offset_in_output_section,
3176 const Relocatable_relocs* rr,
3177 unsigned char* view,
3178 elfcpp::Elf_types<32>::Elf_Addr view_address,
3179 section_size_type view_size,
3180 unsigned char* reloc_view,
3181 section_size_type reloc_view_size)
3183 gold_assert(sh_type == elfcpp::SHT_REL);
3185 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
3190 offset_in_output_section,
3199 // Return the value to use for a dynamic which requires special
3200 // treatment. This is how we support equality comparisons of function
3201 // pointers across shared library boundaries, as described in the
3202 // processor specific ABI supplement.
3205 Target_i386::do_dynsym_value(const Symbol* gsym) const
3207 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3208 return this->plt_section()->address() + gsym->plt_offset();
3211 // Return a string used to fill a code section with nops to take up
3212 // the specified length.
3215 Target_i386::do_code_fill(section_size_type length) const
3219 // Build a jmp instruction to skip over the bytes.
3220 unsigned char jmp[5];
3222 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3223 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3224 + std::string(length - 5, '\0'));
3227 // Nop sequences of various lengths.
3228 const char nop1[1] = { 0x90 }; // nop
3229 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3230 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3231 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3232 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3233 0x00 }; // leal 0(%esi,1),%esi
3234 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3236 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3238 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3239 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3240 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3241 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3243 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3244 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3246 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3247 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3249 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3250 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3251 0x00, 0x00, 0x00, 0x00 };
3252 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3253 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3254 0x27, 0x00, 0x00, 0x00,
3256 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3257 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3258 0xbc, 0x27, 0x00, 0x00,
3260 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3261 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3262 0x90, 0x90, 0x90, 0x90,
3265 const char* nops[16] = {
3267 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3268 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3271 return std::string(nops[length], length);
3274 // Return the value to use for the base of a DW_EH_PE_datarel offset
3275 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3276 // assembler can not write out the difference between two labels in
3277 // different sections, so instead of using a pc-relative value they
3278 // use an offset from the GOT.
3281 Target_i386::do_ehframe_datarel_base() const
3283 gold_assert(this->global_offset_table_ != NULL);
3284 Symbol* sym = this->global_offset_table_;
3285 Sized_symbol<32>* ssym = static_cast<Sized_symbol<32>*>(sym);
3286 return ssym->value();
3289 // Return whether SYM should be treated as a call to a non-split
3290 // function. We don't want that to be true of a call to a
3291 // get_pc_thunk function.
3294 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
3296 return (sym->type() == elfcpp::STT_FUNC
3297 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3300 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3301 // compiled with -fsplit-stack. The function calls non-split-stack
3302 // code. We have to change the function so that it always ensures
3303 // that it has enough stack space to run some random function.
3306 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3307 section_offset_type fnoffset,
3308 section_size_type fnsize,
3309 unsigned char* view,
3310 section_size_type view_size,
3312 std::string* to) const
3314 // The function starts with a comparison of the stack pointer and a
3315 // field in the TCB. This is followed by a jump.
3318 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3321 // We will call __morestack if the carry flag is set after this
3322 // comparison. We turn the comparison into an stc instruction
3324 view[fnoffset] = '\xf9';
3325 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3327 // lea NN(%esp),%ecx
3328 // lea NN(%esp),%edx
3329 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3330 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
3333 // This is loading an offset from the stack pointer for a
3334 // comparison. The offset is negative, so we decrease the
3335 // offset by the amount of space we need for the stack. This
3336 // means we will avoid calling __morestack if there happens to
3337 // be plenty of space on the stack already.
3338 unsigned char* pval = view + fnoffset + 3;
3339 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3340 val -= parameters->options().split_stack_adjust_size();
3341 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3345 if (!object->has_no_split_stack())
3346 object->error(_("failed to match split-stack sequence at "
3347 "section %u offset %0zx"),
3348 shndx, static_cast<size_t>(fnoffset));
3352 // We have to change the function so that it calls
3353 // __morestack_non_split instead of __morestack. The former will
3354 // allocate additional stack space.
3355 *from = "__morestack";
3356 *to = "__morestack_non_split";
3359 // The selector for i386 object files.
3361 class Target_selector_i386 : public Target_selector_freebsd
3364 Target_selector_i386()
3365 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
3366 "elf32-i386", "elf32-i386-freebsd",
3371 do_instantiate_target()
3372 { return new Target_i386(); }
3375 Target_selector_i386 target_selector_i386;
3377 } // End anonymous namespace.