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.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
48 // A class to handle the PLT data.
50 class Output_data_plt_i386 : public Output_section_data
53 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
55 Output_data_plt_i386(Symbol_table*, Layout*, Output_data_space*);
57 // Add an entry to the PLT.
59 add_entry(Symbol* gsym);
61 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
63 add_local_ifunc_entry(Sized_relobj_file<32, false>* relobj,
64 unsigned int local_sym_index);
66 // Return the .rel.plt section data.
69 { return this->rel_; }
71 // Return where the TLS_DESC relocations should go.
73 rel_tls_desc(Layout*);
75 // Return the number of PLT entries.
78 { return this->count_; }
80 // Return the offset of the first non-reserved PLT entry.
82 first_plt_entry_offset()
83 { return plt_entry_size; }
85 // Return the size of a PLT entry.
88 { return plt_entry_size; }
92 do_adjust_output_section(Output_section* os);
94 // Write to a map file.
96 do_print_to_mapfile(Mapfile* mapfile) const
97 { mapfile->print_output_data(this, _("** PLT")); }
100 // The size of an entry in the PLT.
101 static const int plt_entry_size = 16;
103 // The first entry in the PLT for an executable.
104 static unsigned char exec_first_plt_entry[plt_entry_size];
106 // The first entry in the PLT for a shared object.
107 static unsigned char dyn_first_plt_entry[plt_entry_size];
109 // Other entries in the PLT for an executable.
110 static unsigned char exec_plt_entry[plt_entry_size];
112 // Other entries in the PLT for a shared object.
113 static unsigned char dyn_plt_entry[plt_entry_size];
115 // Set the final size.
117 set_final_data_size()
118 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
120 // Write out the PLT data.
122 do_write(Output_file*);
124 // We keep a list of global STT_GNU_IFUNC symbols, each with its
125 // offset in the GOT.
129 unsigned int got_offset;
132 // We keep a list of local STT_GNU_IFUNC symbols, each with its
133 // offset in the GOT.
136 Sized_relobj_file<32, false>* object;
137 unsigned int local_sym_index;
138 unsigned int got_offset;
141 // The reloc section.
143 // The TLS_DESC relocations, if necessary. These must follow the
144 // regular PLT relocs.
145 Reloc_section* tls_desc_rel_;
146 // The .got.plt section.
147 Output_data_space* got_plt_;
148 // The number of PLT entries.
150 // Global STT_GNU_IFUNC symbols.
151 std::vector<Global_ifunc> global_ifuncs_;
152 // Local STT_GNU_IFUNC symbols.
153 std::vector<Local_ifunc> local_ifuncs_;
156 // The i386 target class.
157 // TLS info comes from
158 // http://people.redhat.com/drepper/tls.pdf
159 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
161 class Target_i386 : public Sized_target<32, false>
164 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
167 : Sized_target<32, false>(&i386_info),
168 got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
169 global_offset_table_(NULL), rel_dyn_(NULL),
170 copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
171 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
174 // Process the relocations to determine unreferenced sections for
175 // garbage collection.
177 gc_process_relocs(Symbol_table* symtab,
179 Sized_relobj_file<32, false>* object,
180 unsigned int data_shndx,
181 unsigned int sh_type,
182 const unsigned char* prelocs,
184 Output_section* output_section,
185 bool needs_special_offset_handling,
186 size_t local_symbol_count,
187 const unsigned char* plocal_symbols);
189 // Scan the relocations to look for symbol adjustments.
191 scan_relocs(Symbol_table* symtab,
193 Sized_relobj_file<32, false>* object,
194 unsigned int data_shndx,
195 unsigned int sh_type,
196 const unsigned char* prelocs,
198 Output_section* output_section,
199 bool needs_special_offset_handling,
200 size_t local_symbol_count,
201 const unsigned char* plocal_symbols);
203 // Finalize the sections.
205 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
207 // Return the value to use for a dynamic which requires special
210 do_dynsym_value(const Symbol*) const;
212 // Relocate a section.
214 relocate_section(const Relocate_info<32, false>*,
215 unsigned int sh_type,
216 const unsigned char* prelocs,
218 Output_section* output_section,
219 bool needs_special_offset_handling,
221 elfcpp::Elf_types<32>::Elf_Addr view_address,
222 section_size_type view_size,
223 const Reloc_symbol_changes*);
225 // Scan the relocs during a relocatable link.
227 scan_relocatable_relocs(Symbol_table* symtab,
229 Sized_relobj_file<32, false>* object,
230 unsigned int data_shndx,
231 unsigned int sh_type,
232 const unsigned char* prelocs,
234 Output_section* output_section,
235 bool needs_special_offset_handling,
236 size_t local_symbol_count,
237 const unsigned char* plocal_symbols,
238 Relocatable_relocs*);
240 // Relocate a section during a relocatable link.
242 relocate_for_relocatable(const Relocate_info<32, false>*,
243 unsigned int sh_type,
244 const unsigned char* prelocs,
246 Output_section* output_section,
247 off_t offset_in_output_section,
248 const Relocatable_relocs*,
250 elfcpp::Elf_types<32>::Elf_Addr view_address,
251 section_size_type view_size,
252 unsigned char* reloc_view,
253 section_size_type reloc_view_size);
255 // Return a string used to fill a code section with nops.
257 do_code_fill(section_size_type length) const;
259 // Return whether SYM is defined by the ABI.
261 do_is_defined_by_abi(const Symbol* sym) const
262 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
264 // Return whether a symbol name implies a local label. The UnixWare
265 // 2.1 cc generates temporary symbols that start with .X, so we
266 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
267 // If so, we should move the .X recognition into
268 // Target::do_is_local_label_name.
270 do_is_local_label_name(const char* name) const
272 if (name[0] == '.' && name[1] == 'X')
274 return Target::do_is_local_label_name(name);
277 // Return the PLT section.
279 do_plt_section_for_global(const Symbol*) const
280 { return this->plt_section(); }
283 do_plt_section_for_local(const Relobj*, unsigned int) const
284 { return this->plt_section(); }
286 // We can tell whether we take the address of a function.
288 do_can_check_for_function_pointers() const
291 // Return whether SYM is call to a non-split function.
293 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
295 // Adjust -fsplit-stack code which calls non-split-stack code.
297 do_calls_non_split(Relobj* object, unsigned int shndx,
298 section_offset_type fnoffset, section_size_type fnsize,
299 unsigned char* view, section_size_type view_size,
300 std::string* from, std::string* to) const;
302 // Return the size of the GOT section.
306 gold_assert(this->got_ != NULL);
307 return this->got_->data_size();
310 // Return the number of entries in the GOT.
312 got_entry_count() const
314 if (this->got_ == NULL)
316 return this->got_size() / 4;
319 // Return the number of entries in the PLT.
321 plt_entry_count() const;
323 // Return the offset of the first non-reserved PLT entry.
325 first_plt_entry_offset() const;
327 // Return the size of each PLT entry.
329 plt_entry_size() const;
332 // The class which scans relocations.
337 get_reference_flags(unsigned int r_type);
340 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
341 Sized_relobj_file<32, false>* object,
342 unsigned int data_shndx,
343 Output_section* output_section,
344 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
345 const elfcpp::Sym<32, false>& lsym);
348 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
349 Sized_relobj_file<32, false>* object,
350 unsigned int data_shndx,
351 Output_section* output_section,
352 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
356 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
358 Sized_relobj_file<32, false>* object,
359 unsigned int data_shndx,
360 Output_section* output_section,
361 const elfcpp::Rel<32, false>& reloc,
363 const elfcpp::Sym<32, false>& lsym);
366 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
368 Sized_relobj_file<32, false>* object,
369 unsigned int data_shndx,
370 Output_section* output_section,
371 const elfcpp::Rel<32, false>& reloc,
376 possible_function_pointer_reloc(unsigned int r_type);
379 reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
380 unsigned int r_type);
383 unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
386 unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
390 // The class which implements relocation.
395 : skip_call_tls_get_addr_(false),
396 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
401 if (this->skip_call_tls_get_addr_)
403 // FIXME: This needs to specify the location somehow.
404 gold_error(_("missing expected TLS relocation"));
408 // Return whether the static relocation needs to be applied.
410 should_apply_static_reloc(const Sized_symbol<32>* gsym,
413 Output_section* output_section);
415 // Do a relocation. Return false if the caller should not issue
416 // any warnings about this relocation.
418 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
419 size_t relnum, const elfcpp::Rel<32, false>&,
420 unsigned int r_type, const Sized_symbol<32>*,
421 const Symbol_value<32>*,
422 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
426 // Do a TLS relocation.
428 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
429 size_t relnum, const elfcpp::Rel<32, false>&,
430 unsigned int r_type, const Sized_symbol<32>*,
431 const Symbol_value<32>*,
432 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
435 // Do a TLS General-Dynamic to Initial-Exec transition.
437 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
438 Output_segment* tls_segment,
439 const elfcpp::Rel<32, false>&, unsigned int r_type,
440 elfcpp::Elf_types<32>::Elf_Addr value,
442 section_size_type view_size);
444 // Do a TLS General-Dynamic to Local-Exec transition.
446 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
447 Output_segment* tls_segment,
448 const elfcpp::Rel<32, false>&, unsigned int r_type,
449 elfcpp::Elf_types<32>::Elf_Addr value,
451 section_size_type view_size);
453 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
456 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
457 Output_segment* tls_segment,
458 const elfcpp::Rel<32, false>&, unsigned int r_type,
459 elfcpp::Elf_types<32>::Elf_Addr value,
461 section_size_type view_size);
463 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
466 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
467 Output_segment* tls_segment,
468 const elfcpp::Rel<32, false>&, unsigned int r_type,
469 elfcpp::Elf_types<32>::Elf_Addr value,
471 section_size_type view_size);
473 // Do a TLS Local-Dynamic to Local-Exec transition.
475 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
476 Output_segment* tls_segment,
477 const elfcpp::Rel<32, false>&, unsigned int r_type,
478 elfcpp::Elf_types<32>::Elf_Addr value,
480 section_size_type view_size);
482 // Do a TLS Initial-Exec to Local-Exec transition.
484 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
485 Output_segment* tls_segment,
486 const elfcpp::Rel<32, false>&, unsigned int r_type,
487 elfcpp::Elf_types<32>::Elf_Addr value,
489 section_size_type view_size);
491 // We need to keep track of which type of local dynamic relocation
492 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
493 enum Local_dynamic_type
500 // This is set if we should skip the next reloc, which should be a
501 // PLT32 reloc against ___tls_get_addr.
502 bool skip_call_tls_get_addr_;
503 // The type of local dynamic relocation we have seen in the section
504 // being relocated, if any.
505 Local_dynamic_type local_dynamic_type_;
508 // A class which returns the size required for a relocation type,
509 // used while scanning relocs during a relocatable link.
510 class Relocatable_size_for_reloc
514 get_size_for_reloc(unsigned int, Relobj*);
517 // Adjust TLS relocation type based on the options and whether this
518 // is a local symbol.
519 static tls::Tls_optimization
520 optimize_tls_reloc(bool is_final, int r_type);
522 // Get the GOT section, creating it if necessary.
523 Output_data_got<32, false>*
524 got_section(Symbol_table*, Layout*);
526 // Get the GOT PLT section.
528 got_plt_section() const
530 gold_assert(this->got_plt_ != NULL);
531 return this->got_plt_;
534 // Get the GOT section for TLSDESC entries.
535 Output_data_got<32, false>*
536 got_tlsdesc_section() const
538 gold_assert(this->got_tlsdesc_ != NULL);
539 return this->got_tlsdesc_;
542 // Create the PLT section.
544 make_plt_section(Symbol_table* symtab, Layout* layout);
546 // Create a PLT entry for a global symbol.
548 make_plt_entry(Symbol_table*, Layout*, Symbol*);
550 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
552 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
553 Sized_relobj_file<32, false>* relobj,
554 unsigned int local_sym_index);
556 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
558 define_tls_base_symbol(Symbol_table*, Layout*);
560 // Create a GOT entry for the TLS module index.
562 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
563 Sized_relobj_file<32, false>* object);
565 // Get the PLT section.
566 Output_data_plt_i386*
569 gold_assert(this->plt_ != NULL);
573 // Get the dynamic reloc section, creating it if necessary.
575 rel_dyn_section(Layout*);
577 // Get the section to use for TLS_DESC relocations.
579 rel_tls_desc_section(Layout*) const;
581 // Add a potential copy relocation.
583 copy_reloc(Symbol_table* symtab, Layout* layout,
584 Sized_relobj_file<32, false>* object,
585 unsigned int shndx, Output_section* output_section,
586 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
588 this->copy_relocs_.copy_reloc(symtab, layout,
589 symtab->get_sized_symbol<32>(sym),
590 object, shndx, output_section, reloc,
591 this->rel_dyn_section(layout));
594 // Information about this specific target which we pass to the
595 // general Target structure.
596 static const Target::Target_info i386_info;
598 // The types of GOT entries needed for this platform.
599 // These values are exposed to the ABI in an incremental link.
600 // Do not renumber existing values without changing the version
601 // number of the .gnu_incremental_inputs section.
604 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
605 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
606 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
607 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
608 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
612 Output_data_got<32, false>* got_;
614 Output_data_plt_i386* plt_;
615 // The GOT PLT section.
616 Output_data_space* got_plt_;
617 // The GOT section for TLSDESC relocations.
618 Output_data_got<32, false>* got_tlsdesc_;
619 // The _GLOBAL_OFFSET_TABLE_ symbol.
620 Symbol* global_offset_table_;
621 // The dynamic reloc section.
622 Reloc_section* rel_dyn_;
623 // Relocs saved to avoid a COPY reloc.
624 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
625 // Space for variables copied with a COPY reloc.
626 Output_data_space* dynbss_;
627 // Offset of the GOT entry for the TLS module index.
628 unsigned int got_mod_index_offset_;
629 // True if the _TLS_MODULE_BASE_ symbol has been defined.
630 bool tls_base_symbol_defined_;
633 const Target::Target_info Target_i386::i386_info =
636 false, // is_big_endian
637 elfcpp::EM_386, // machine_code
638 false, // has_make_symbol
639 false, // has_resolve
640 true, // has_code_fill
641 true, // is_default_stack_executable
642 true, // can_icf_inline_merge_sections
644 "/usr/lib/libc.so.1", // dynamic_linker
645 0x08048000, // default_text_segment_address
646 0x1000, // abi_pagesize (overridable by -z max-page-size)
647 0x1000, // common_pagesize (overridable by -z common-page-size)
648 elfcpp::SHN_UNDEF, // small_common_shndx
649 elfcpp::SHN_UNDEF, // large_common_shndx
650 0, // small_common_section_flags
651 0, // large_common_section_flags
652 NULL, // attributes_section
653 NULL // attributes_vendor
656 // Get the GOT section, creating it if necessary.
658 Output_data_got<32, false>*
659 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
661 if (this->got_ == NULL)
663 gold_assert(symtab != NULL && layout != NULL);
665 this->got_ = new Output_data_got<32, false>();
667 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
669 | elfcpp::SHF_WRITE),
670 this->got_, ORDER_RELRO_LAST, true);
672 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
673 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
675 | elfcpp::SHF_WRITE),
676 this->got_plt_, ORDER_NON_RELRO_FIRST,
679 // The first three entries are reserved.
680 this->got_plt_->set_current_data_size(3 * 4);
682 // Those bytes can go into the relro segment.
683 layout->increase_relro(3 * 4);
685 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
686 this->global_offset_table_ =
687 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
688 Symbol_table::PREDEFINED,
690 0, 0, elfcpp::STT_OBJECT,
692 elfcpp::STV_HIDDEN, 0,
695 // If there are any TLSDESC relocations, they get GOT entries in
696 // .got.plt after the jump slot entries.
697 this->got_tlsdesc_ = new Output_data_got<32, false>();
698 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
700 | elfcpp::SHF_WRITE),
702 ORDER_NON_RELRO_FIRST, false);
708 // Get the dynamic reloc section, creating it if necessary.
710 Target_i386::Reloc_section*
711 Target_i386::rel_dyn_section(Layout* layout)
713 if (this->rel_dyn_ == NULL)
715 gold_assert(layout != NULL);
716 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
717 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
718 elfcpp::SHF_ALLOC, this->rel_dyn_,
719 ORDER_DYNAMIC_RELOCS, false);
721 return this->rel_dyn_;
724 // Create the PLT section. The ordinary .got section is an argument,
725 // since we need to refer to the start. We also create our own .got
726 // section just for PLT entries.
728 Output_data_plt_i386::Output_data_plt_i386(Symbol_table* symtab,
730 Output_data_space* got_plt)
731 : Output_section_data(4), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0),
732 global_ifuncs_(), local_ifuncs_()
734 this->rel_ = new Reloc_section(false);
735 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
736 elfcpp::SHF_ALLOC, this->rel_,
737 ORDER_DYNAMIC_PLT_RELOCS, false);
739 if (parameters->doing_static_link())
741 // A statically linked executable will only have a .rel.plt
742 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
743 // symbols. The library will use these symbols to locate the
744 // IRELATIVE relocs at program startup time.
745 symtab->define_in_output_data("__rel_iplt_start", NULL,
746 Symbol_table::PREDEFINED,
747 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
748 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
750 symtab->define_in_output_data("__rel_iplt_end", NULL,
751 Symbol_table::PREDEFINED,
752 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
753 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
759 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
761 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
762 // linker, and so do we.
766 // Add an entry to the PLT.
769 Output_data_plt_i386::add_entry(Symbol* gsym)
771 gold_assert(!gsym->has_plt_offset());
773 // Note that when setting the PLT offset we skip the initial
774 // reserved PLT entry.
775 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
779 section_offset_type got_offset = this->got_plt_->current_data_size();
781 // Every PLT entry needs a GOT entry which points back to the PLT
782 // entry (this will be changed by the dynamic linker, normally
783 // lazily when the function is called).
784 this->got_plt_->set_current_data_size(got_offset + 4);
786 // Every PLT entry needs a reloc.
787 if (gsym->type() == elfcpp::STT_GNU_IFUNC
788 && gsym->can_use_relative_reloc(false))
790 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
791 this->got_plt_, got_offset);
792 struct Global_ifunc gi;
794 gi.got_offset = got_offset;
795 this->global_ifuncs_.push_back(gi);
799 gsym->set_needs_dynsym_entry();
800 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
804 // Note that we don't need to save the symbol. The contents of the
805 // PLT are independent of which symbols are used. The symbols only
806 // appear in the relocations.
809 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
813 Output_data_plt_i386::add_local_ifunc_entry(
814 Sized_relobj_file<32, false>* relobj,
815 unsigned int local_sym_index)
817 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
820 section_offset_type got_offset = this->got_plt_->current_data_size();
822 // Every PLT entry needs a GOT entry which points back to the PLT
824 this->got_plt_->set_current_data_size(got_offset + 4);
826 // Every PLT entry needs a reloc.
827 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
828 elfcpp::R_386_IRELATIVE,
829 this->got_plt_, got_offset);
831 struct Local_ifunc li;
833 li.local_sym_index = local_sym_index;
834 li.got_offset = got_offset;
835 this->local_ifuncs_.push_back(li);
840 // Return where the TLS_DESC relocations should go, creating it if
841 // necessary. These follow the JUMP_SLOT relocations.
843 Output_data_plt_i386::Reloc_section*
844 Output_data_plt_i386::rel_tls_desc(Layout* layout)
846 if (this->tls_desc_rel_ == NULL)
848 this->tls_desc_rel_ = new Reloc_section(false);
849 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
850 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
851 ORDER_DYNAMIC_PLT_RELOCS, false);
852 gold_assert(this->tls_desc_rel_->output_section() ==
853 this->rel_->output_section());
855 return this->tls_desc_rel_;
858 // The first entry in the PLT for an executable.
860 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
862 0xff, 0x35, // pushl contents of memory address
863 0, 0, 0, 0, // replaced with address of .got + 4
864 0xff, 0x25, // jmp indirect
865 0, 0, 0, 0, // replaced with address of .got + 8
869 // The first entry in the PLT for a shared object.
871 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
873 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
874 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
878 // Subsequent entries in the PLT for an executable.
880 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
882 0xff, 0x25, // jmp indirect
883 0, 0, 0, 0, // replaced with address of symbol in .got
884 0x68, // pushl immediate
885 0, 0, 0, 0, // replaced with offset into relocation table
886 0xe9, // jmp relative
887 0, 0, 0, 0 // replaced with offset to start of .plt
890 // Subsequent entries in the PLT for a shared object.
892 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
894 0xff, 0xa3, // jmp *offset(%ebx)
895 0, 0, 0, 0, // replaced with offset of symbol in .got
896 0x68, // pushl immediate
897 0, 0, 0, 0, // replaced with offset into relocation table
898 0xe9, // jmp relative
899 0, 0, 0, 0 // replaced with offset to start of .plt
902 // Write out the PLT. This uses the hand-coded instructions above,
903 // and adjusts them as needed. This is all specified by the i386 ELF
904 // Processor Supplement.
907 Output_data_plt_i386::do_write(Output_file* of)
909 const off_t offset = this->offset();
910 const section_size_type oview_size =
911 convert_to_section_size_type(this->data_size());
912 unsigned char* const oview = of->get_output_view(offset, oview_size);
914 const off_t got_file_offset = this->got_plt_->offset();
915 const section_size_type got_size =
916 convert_to_section_size_type(this->got_plt_->data_size());
917 unsigned char* const got_view = of->get_output_view(got_file_offset,
920 unsigned char* pov = oview;
922 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
923 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
925 if (parameters->options().output_is_position_independent())
926 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
929 memcpy(pov, exec_first_plt_entry, plt_entry_size);
930 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
931 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
933 pov += plt_entry_size;
935 unsigned char* got_pov = got_view;
937 memset(got_pov, 0, 12);
940 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
942 unsigned int plt_offset = plt_entry_size;
943 unsigned int plt_rel_offset = 0;
944 unsigned int got_offset = 12;
945 const unsigned int count = this->count_;
946 for (unsigned int i = 0;
949 pov += plt_entry_size,
951 plt_offset += plt_entry_size,
952 plt_rel_offset += rel_size,
955 // Set and adjust the PLT entry itself.
957 if (parameters->options().output_is_position_independent())
959 memcpy(pov, dyn_plt_entry, plt_entry_size);
960 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
964 memcpy(pov, exec_plt_entry, plt_entry_size);
965 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
970 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
971 elfcpp::Swap<32, false>::writeval(pov + 12,
972 - (plt_offset + plt_entry_size));
974 // Set the entry in the GOT.
975 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
978 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
979 // the GOT to point to the actual symbol value, rather than point to
980 // the PLT entry. That will let the dynamic linker call the right
981 // function when resolving IRELATIVE relocations.
982 for (std::vector<Global_ifunc>::const_iterator p =
983 this->global_ifuncs_.begin();
984 p != this->global_ifuncs_.end();
987 const Sized_symbol<32>* ssym =
988 static_cast<const Sized_symbol<32>*>(p->sym);
989 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
993 for (std::vector<Local_ifunc>::const_iterator p =
994 this->local_ifuncs_.begin();
995 p != this->local_ifuncs_.end();
998 const Symbol_value<32>* psymval =
999 p->object->local_symbol(p->local_sym_index);
1000 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
1001 psymval->value(p->object, 0));
1004 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1005 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1007 of->write_output_view(offset, oview_size, oview);
1008 of->write_output_view(got_file_offset, got_size, got_view);
1011 // Create the PLT section.
1014 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1016 if (this->plt_ == NULL)
1018 // Create the GOT sections first.
1019 this->got_section(symtab, layout);
1021 this->plt_ = new Output_data_plt_i386(symtab, layout, this->got_plt_);
1022 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1024 | elfcpp::SHF_EXECINSTR),
1025 this->plt_, ORDER_PLT, false);
1027 // Make the sh_info field of .rel.plt point to .plt.
1028 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1029 rel_plt_os->set_info_section(this->plt_->output_section());
1033 // Create a PLT entry for a global symbol.
1036 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1038 if (gsym->has_plt_offset())
1040 if (this->plt_ == NULL)
1041 this->make_plt_section(symtab, layout);
1042 this->plt_->add_entry(gsym);
1045 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1048 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1049 Sized_relobj_file<32, false>* relobj,
1050 unsigned int local_sym_index)
1052 if (relobj->local_has_plt_offset(local_sym_index))
1054 if (this->plt_ == NULL)
1055 this->make_plt_section(symtab, layout);
1056 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1058 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1061 // Return the number of entries in the PLT.
1064 Target_i386::plt_entry_count() const
1066 if (this->plt_ == NULL)
1068 return this->plt_->entry_count();
1071 // Return the offset of the first non-reserved PLT entry.
1074 Target_i386::first_plt_entry_offset() const
1076 return Output_data_plt_i386::first_plt_entry_offset();
1079 // Return the size of each PLT entry.
1082 Target_i386::plt_entry_size() const
1084 return Output_data_plt_i386::get_plt_entry_size();
1087 // Get the section to use for TLS_DESC relocations.
1089 Target_i386::Reloc_section*
1090 Target_i386::rel_tls_desc_section(Layout* layout) const
1092 return this->plt_section()->rel_tls_desc(layout);
1095 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1098 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1100 if (this->tls_base_symbol_defined_)
1103 Output_segment* tls_segment = layout->tls_segment();
1104 if (tls_segment != NULL)
1106 bool is_exec = parameters->options().output_is_executable();
1107 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1108 Symbol_table::PREDEFINED,
1112 elfcpp::STV_HIDDEN, 0,
1114 ? Symbol::SEGMENT_END
1115 : Symbol::SEGMENT_START),
1118 this->tls_base_symbol_defined_ = true;
1121 // Create a GOT entry for the TLS module index.
1124 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1125 Sized_relobj_file<32, false>* object)
1127 if (this->got_mod_index_offset_ == -1U)
1129 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1130 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1131 Output_data_got<32, false>* got = this->got_section(symtab, layout);
1132 unsigned int got_offset = got->add_constant(0);
1133 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1135 got->add_constant(0);
1136 this->got_mod_index_offset_ = got_offset;
1138 return this->got_mod_index_offset_;
1141 // Optimize the TLS relocation type based on what we know about the
1142 // symbol. IS_FINAL is true if the final address of this symbol is
1143 // known at link time.
1145 tls::Tls_optimization
1146 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1148 // If we are generating a shared library, then we can't do anything
1150 if (parameters->options().shared())
1151 return tls::TLSOPT_NONE;
1155 case elfcpp::R_386_TLS_GD:
1156 case elfcpp::R_386_TLS_GOTDESC:
1157 case elfcpp::R_386_TLS_DESC_CALL:
1158 // These are General-Dynamic which permits fully general TLS
1159 // access. Since we know that we are generating an executable,
1160 // we can convert this to Initial-Exec. If we also know that
1161 // this is a local symbol, we can further switch to Local-Exec.
1163 return tls::TLSOPT_TO_LE;
1164 return tls::TLSOPT_TO_IE;
1166 case elfcpp::R_386_TLS_LDM:
1167 // This is Local-Dynamic, which refers to a local symbol in the
1168 // dynamic TLS block. Since we know that we generating an
1169 // executable, we can switch to Local-Exec.
1170 return tls::TLSOPT_TO_LE;
1172 case elfcpp::R_386_TLS_LDO_32:
1173 // Another type of Local-Dynamic relocation.
1174 return tls::TLSOPT_TO_LE;
1176 case elfcpp::R_386_TLS_IE:
1177 case elfcpp::R_386_TLS_GOTIE:
1178 case elfcpp::R_386_TLS_IE_32:
1179 // These are Initial-Exec relocs which get the thread offset
1180 // from the GOT. If we know that we are linking against the
1181 // local symbol, we can switch to Local-Exec, which links the
1182 // thread offset into the instruction.
1184 return tls::TLSOPT_TO_LE;
1185 return tls::TLSOPT_NONE;
1187 case elfcpp::R_386_TLS_LE:
1188 case elfcpp::R_386_TLS_LE_32:
1189 // When we already have Local-Exec, there is nothing further we
1191 return tls::TLSOPT_NONE;
1198 // Get the Reference_flags for a particular relocation.
1201 Target_i386::Scan::get_reference_flags(unsigned int r_type)
1205 case elfcpp::R_386_NONE:
1206 case elfcpp::R_386_GNU_VTINHERIT:
1207 case elfcpp::R_386_GNU_VTENTRY:
1208 case elfcpp::R_386_GOTPC:
1209 // No symbol reference.
1212 case elfcpp::R_386_32:
1213 case elfcpp::R_386_16:
1214 case elfcpp::R_386_8:
1215 return Symbol::ABSOLUTE_REF;
1217 case elfcpp::R_386_PC32:
1218 case elfcpp::R_386_PC16:
1219 case elfcpp::R_386_PC8:
1220 case elfcpp::R_386_GOTOFF:
1221 return Symbol::RELATIVE_REF;
1223 case elfcpp::R_386_PLT32:
1224 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1226 case elfcpp::R_386_GOT32:
1228 return Symbol::ABSOLUTE_REF;
1230 case elfcpp::R_386_TLS_GD: // Global-dynamic
1231 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1232 case elfcpp::R_386_TLS_DESC_CALL:
1233 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1234 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1235 case elfcpp::R_386_TLS_IE: // Initial-exec
1236 case elfcpp::R_386_TLS_IE_32:
1237 case elfcpp::R_386_TLS_GOTIE:
1238 case elfcpp::R_386_TLS_LE: // Local-exec
1239 case elfcpp::R_386_TLS_LE_32:
1240 return Symbol::TLS_REF;
1242 case elfcpp::R_386_COPY:
1243 case elfcpp::R_386_GLOB_DAT:
1244 case elfcpp::R_386_JUMP_SLOT:
1245 case elfcpp::R_386_RELATIVE:
1246 case elfcpp::R_386_IRELATIVE:
1247 case elfcpp::R_386_TLS_TPOFF:
1248 case elfcpp::R_386_TLS_DTPMOD32:
1249 case elfcpp::R_386_TLS_DTPOFF32:
1250 case elfcpp::R_386_TLS_TPOFF32:
1251 case elfcpp::R_386_TLS_DESC:
1252 case elfcpp::R_386_32PLT:
1253 case elfcpp::R_386_TLS_GD_32:
1254 case elfcpp::R_386_TLS_GD_PUSH:
1255 case elfcpp::R_386_TLS_GD_CALL:
1256 case elfcpp::R_386_TLS_GD_POP:
1257 case elfcpp::R_386_TLS_LDM_32:
1258 case elfcpp::R_386_TLS_LDM_PUSH:
1259 case elfcpp::R_386_TLS_LDM_CALL:
1260 case elfcpp::R_386_TLS_LDM_POP:
1261 case elfcpp::R_386_USED_BY_INTEL_200:
1263 // Not expected. We will give an error later.
1268 // Report an unsupported relocation against a local symbol.
1271 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
1272 unsigned int r_type)
1274 gold_error(_("%s: unsupported reloc %u against local symbol"),
1275 object->name().c_str(), r_type);
1278 // Return whether we need to make a PLT entry for a relocation of a
1279 // given type against a STT_GNU_IFUNC symbol.
1282 Target_i386::Scan::reloc_needs_plt_for_ifunc(
1283 Sized_relobj_file<32, false>* object,
1284 unsigned int r_type)
1286 int flags = Scan::get_reference_flags(r_type);
1287 if (flags & Symbol::TLS_REF)
1288 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1289 object->name().c_str(), r_type);
1293 // Scan a relocation for a local symbol.
1296 Target_i386::Scan::local(Symbol_table* symtab,
1298 Target_i386* target,
1299 Sized_relobj_file<32, false>* object,
1300 unsigned int data_shndx,
1301 Output_section* output_section,
1302 const elfcpp::Rel<32, false>& reloc,
1303 unsigned int r_type,
1304 const elfcpp::Sym<32, false>& lsym)
1306 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1307 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1308 && this->reloc_needs_plt_for_ifunc(object, r_type))
1310 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1311 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1316 case elfcpp::R_386_NONE:
1317 case elfcpp::R_386_GNU_VTINHERIT:
1318 case elfcpp::R_386_GNU_VTENTRY:
1321 case elfcpp::R_386_32:
1322 // If building a shared library (or a position-independent
1323 // executable), we need to create a dynamic relocation for
1324 // this location. The relocation applied at link time will
1325 // apply the link-time value, so we flag the location with
1326 // an R_386_RELATIVE relocation so the dynamic loader can
1327 // relocate it easily.
1328 if (parameters->options().output_is_position_independent())
1330 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1331 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1332 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1333 output_section, data_shndx,
1334 reloc.get_r_offset());
1338 case elfcpp::R_386_16:
1339 case elfcpp::R_386_8:
1340 // If building a shared library (or a position-independent
1341 // executable), we need to create a dynamic relocation for
1342 // this location. Because the addend needs to remain in the
1343 // data section, we need to be careful not to apply this
1344 // relocation statically.
1345 if (parameters->options().output_is_position_independent())
1347 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1348 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1349 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1350 rel_dyn->add_local(object, r_sym, r_type, output_section,
1351 data_shndx, reloc.get_r_offset());
1354 gold_assert(lsym.get_st_value() == 0);
1355 unsigned int shndx = lsym.get_st_shndx();
1357 shndx = object->adjust_sym_shndx(r_sym, shndx,
1360 object->error(_("section symbol %u has bad shndx %u"),
1363 rel_dyn->add_local_section(object, shndx,
1364 r_type, output_section,
1365 data_shndx, reloc.get_r_offset());
1370 case elfcpp::R_386_PC32:
1371 case elfcpp::R_386_PC16:
1372 case elfcpp::R_386_PC8:
1375 case elfcpp::R_386_PLT32:
1376 // Since we know this is a local symbol, we can handle this as a
1380 case elfcpp::R_386_GOTOFF:
1381 case elfcpp::R_386_GOTPC:
1382 // We need a GOT section.
1383 target->got_section(symtab, layout);
1386 case elfcpp::R_386_GOT32:
1388 // The symbol requires a GOT entry.
1389 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1390 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1392 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1393 // lets function pointers compare correctly with shared
1394 // libraries. Otherwise we would need an IRELATIVE reloc.
1396 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1397 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1399 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1402 // If we are generating a shared object, we need to add a
1403 // dynamic RELATIVE relocation for this symbol's GOT entry.
1404 if (parameters->options().output_is_position_independent())
1406 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1407 unsigned int got_offset =
1408 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1409 rel_dyn->add_local_relative(object, r_sym,
1410 elfcpp::R_386_RELATIVE,
1417 // These are relocations which should only be seen by the
1418 // dynamic linker, and should never be seen here.
1419 case elfcpp::R_386_COPY:
1420 case elfcpp::R_386_GLOB_DAT:
1421 case elfcpp::R_386_JUMP_SLOT:
1422 case elfcpp::R_386_RELATIVE:
1423 case elfcpp::R_386_IRELATIVE:
1424 case elfcpp::R_386_TLS_TPOFF:
1425 case elfcpp::R_386_TLS_DTPMOD32:
1426 case elfcpp::R_386_TLS_DTPOFF32:
1427 case elfcpp::R_386_TLS_TPOFF32:
1428 case elfcpp::R_386_TLS_DESC:
1429 gold_error(_("%s: unexpected reloc %u in object file"),
1430 object->name().c_str(), r_type);
1433 // These are initial TLS relocs, which are expected when
1435 case elfcpp::R_386_TLS_GD: // Global-dynamic
1436 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1437 case elfcpp::R_386_TLS_DESC_CALL:
1438 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1439 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1440 case elfcpp::R_386_TLS_IE: // Initial-exec
1441 case elfcpp::R_386_TLS_IE_32:
1442 case elfcpp::R_386_TLS_GOTIE:
1443 case elfcpp::R_386_TLS_LE: // Local-exec
1444 case elfcpp::R_386_TLS_LE_32:
1446 bool output_is_shared = parameters->options().shared();
1447 const tls::Tls_optimization optimized_type
1448 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1451 case elfcpp::R_386_TLS_GD: // Global-dynamic
1452 if (optimized_type == tls::TLSOPT_NONE)
1454 // Create a pair of GOT entries for the module index and
1455 // dtv-relative offset.
1456 Output_data_got<32, false>* got
1457 = target->got_section(symtab, layout);
1458 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1459 unsigned int shndx = lsym.get_st_shndx();
1461 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1463 object->error(_("local symbol %u has bad shndx %u"),
1466 got->add_local_pair_with_rel(object, r_sym, shndx,
1468 target->rel_dyn_section(layout),
1469 elfcpp::R_386_TLS_DTPMOD32, 0);
1471 else if (optimized_type != tls::TLSOPT_TO_LE)
1472 unsupported_reloc_local(object, r_type);
1475 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1476 target->define_tls_base_symbol(symtab, layout);
1477 if (optimized_type == tls::TLSOPT_NONE)
1479 // Create a double GOT entry with an R_386_TLS_DESC
1480 // reloc. The R_386_TLS_DESC reloc is resolved
1481 // lazily, so the GOT entry needs to be in an area in
1482 // .got.plt, not .got. Call got_section to make sure
1483 // the section has been created.
1484 target->got_section(symtab, layout);
1485 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1486 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1487 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1489 unsigned int got_offset = got->add_constant(0);
1490 // The local symbol value is stored in the second
1492 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1493 // That set the GOT offset of the local symbol to
1494 // point to the second entry, but we want it to
1495 // point to the first.
1496 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1498 Reloc_section* rt = target->rel_tls_desc_section(layout);
1499 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1502 else if (optimized_type != tls::TLSOPT_TO_LE)
1503 unsupported_reloc_local(object, r_type);
1506 case elfcpp::R_386_TLS_DESC_CALL:
1509 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1510 if (optimized_type == tls::TLSOPT_NONE)
1512 // Create a GOT entry for the module index.
1513 target->got_mod_index_entry(symtab, layout, object);
1515 else if (optimized_type != tls::TLSOPT_TO_LE)
1516 unsupported_reloc_local(object, r_type);
1519 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1522 case elfcpp::R_386_TLS_IE: // Initial-exec
1523 case elfcpp::R_386_TLS_IE_32:
1524 case elfcpp::R_386_TLS_GOTIE:
1525 layout->set_has_static_tls();
1526 if (optimized_type == tls::TLSOPT_NONE)
1528 // For the R_386_TLS_IE relocation, we need to create a
1529 // dynamic relocation when building a shared library.
1530 if (r_type == elfcpp::R_386_TLS_IE
1531 && parameters->options().shared())
1533 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1535 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1536 rel_dyn->add_local_relative(object, r_sym,
1537 elfcpp::R_386_RELATIVE,
1538 output_section, data_shndx,
1539 reloc.get_r_offset());
1541 // Create a GOT entry for the tp-relative offset.
1542 Output_data_got<32, false>* got
1543 = target->got_section(symtab, layout);
1544 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1545 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1546 ? elfcpp::R_386_TLS_TPOFF32
1547 : elfcpp::R_386_TLS_TPOFF);
1548 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1549 ? GOT_TYPE_TLS_OFFSET
1550 : GOT_TYPE_TLS_NOFFSET);
1551 got->add_local_with_rel(object, r_sym, got_type,
1552 target->rel_dyn_section(layout),
1555 else if (optimized_type != tls::TLSOPT_TO_LE)
1556 unsupported_reloc_local(object, r_type);
1559 case elfcpp::R_386_TLS_LE: // Local-exec
1560 case elfcpp::R_386_TLS_LE_32:
1561 layout->set_has_static_tls();
1562 if (output_is_shared)
1564 // We need to create a dynamic relocation.
1565 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1566 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1567 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1568 ? elfcpp::R_386_TLS_TPOFF32
1569 : elfcpp::R_386_TLS_TPOFF);
1570 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1571 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1572 data_shndx, reloc.get_r_offset());
1582 case elfcpp::R_386_32PLT:
1583 case elfcpp::R_386_TLS_GD_32:
1584 case elfcpp::R_386_TLS_GD_PUSH:
1585 case elfcpp::R_386_TLS_GD_CALL:
1586 case elfcpp::R_386_TLS_GD_POP:
1587 case elfcpp::R_386_TLS_LDM_32:
1588 case elfcpp::R_386_TLS_LDM_PUSH:
1589 case elfcpp::R_386_TLS_LDM_CALL:
1590 case elfcpp::R_386_TLS_LDM_POP:
1591 case elfcpp::R_386_USED_BY_INTEL_200:
1593 unsupported_reloc_local(object, r_type);
1598 // Report an unsupported relocation against a global symbol.
1601 Target_i386::Scan::unsupported_reloc_global(
1602 Sized_relobj_file<32, false>* object,
1603 unsigned int r_type,
1606 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1607 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1611 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
1615 case elfcpp::R_386_32:
1616 case elfcpp::R_386_16:
1617 case elfcpp::R_386_8:
1618 case elfcpp::R_386_GOTOFF:
1619 case elfcpp::R_386_GOT32:
1630 Target_i386::Scan::local_reloc_may_be_function_pointer(
1634 Sized_relobj_file<32, false>* ,
1637 const elfcpp::Rel<32, false>& ,
1638 unsigned int r_type,
1639 const elfcpp::Sym<32, false>&)
1641 return possible_function_pointer_reloc(r_type);
1645 Target_i386::Scan::global_reloc_may_be_function_pointer(
1649 Sized_relobj_file<32, false>* ,
1652 const elfcpp::Rel<32, false>& ,
1653 unsigned int r_type,
1656 return possible_function_pointer_reloc(r_type);
1659 // Scan a relocation for a global symbol.
1662 Target_i386::Scan::global(Symbol_table* symtab,
1664 Target_i386* target,
1665 Sized_relobj_file<32, false>* object,
1666 unsigned int data_shndx,
1667 Output_section* output_section,
1668 const elfcpp::Rel<32, false>& reloc,
1669 unsigned int r_type,
1672 // A STT_GNU_IFUNC symbol may require a PLT entry.
1673 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1674 && this->reloc_needs_plt_for_ifunc(object, r_type))
1675 target->make_plt_entry(symtab, layout, gsym);
1679 case elfcpp::R_386_NONE:
1680 case elfcpp::R_386_GNU_VTINHERIT:
1681 case elfcpp::R_386_GNU_VTENTRY:
1684 case elfcpp::R_386_32:
1685 case elfcpp::R_386_16:
1686 case elfcpp::R_386_8:
1688 // Make a PLT entry if necessary.
1689 if (gsym->needs_plt_entry())
1691 target->make_plt_entry(symtab, layout, gsym);
1692 // Since this is not a PC-relative relocation, we may be
1693 // taking the address of a function. In that case we need to
1694 // set the entry in the dynamic symbol table to the address of
1696 if (gsym->is_from_dynobj() && !parameters->options().shared())
1697 gsym->set_needs_dynsym_value();
1699 // Make a dynamic relocation if necessary.
1700 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1702 if (gsym->may_need_copy_reloc())
1704 target->copy_reloc(symtab, layout, object,
1705 data_shndx, output_section, gsym, reloc);
1707 else if (r_type == elfcpp::R_386_32
1708 && gsym->type() == elfcpp::STT_GNU_IFUNC
1709 && gsym->can_use_relative_reloc(false)
1710 && !gsym->is_from_dynobj()
1711 && !gsym->is_undefined()
1712 && !gsym->is_preemptible())
1714 // Use an IRELATIVE reloc for a locally defined
1715 // STT_GNU_IFUNC symbol. This makes a function
1716 // address in a PIE executable match the address in a
1717 // shared library that it links against.
1718 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1719 rel_dyn->add_symbolless_global_addend(gsym,
1720 elfcpp::R_386_IRELATIVE,
1723 reloc.get_r_offset());
1725 else if (r_type == elfcpp::R_386_32
1726 && gsym->can_use_relative_reloc(false))
1728 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1729 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1730 output_section, object,
1731 data_shndx, reloc.get_r_offset());
1735 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1736 rel_dyn->add_global(gsym, r_type, output_section, object,
1737 data_shndx, reloc.get_r_offset());
1743 case elfcpp::R_386_PC32:
1744 case elfcpp::R_386_PC16:
1745 case elfcpp::R_386_PC8:
1747 // Make a PLT entry if necessary.
1748 if (gsym->needs_plt_entry())
1750 // These relocations are used for function calls only in
1751 // non-PIC code. For a 32-bit relocation in a shared library,
1752 // we'll need a text relocation anyway, so we can skip the
1753 // PLT entry and let the dynamic linker bind the call directly
1754 // to the target. For smaller relocations, we should use a
1755 // PLT entry to ensure that the call can reach.
1756 if (!parameters->options().shared()
1757 || r_type != elfcpp::R_386_PC32)
1758 target->make_plt_entry(symtab, layout, gsym);
1760 // Make a dynamic relocation if necessary.
1761 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1763 if (gsym->may_need_copy_reloc())
1765 target->copy_reloc(symtab, layout, object,
1766 data_shndx, output_section, gsym, reloc);
1770 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1771 rel_dyn->add_global(gsym, r_type, output_section, object,
1772 data_shndx, reloc.get_r_offset());
1778 case elfcpp::R_386_GOT32:
1780 // The symbol requires a GOT entry.
1781 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1782 if (gsym->final_value_is_known())
1784 // For a STT_GNU_IFUNC symbol we want the PLT address.
1785 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1786 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1788 got->add_global(gsym, GOT_TYPE_STANDARD);
1792 // If this symbol is not fully resolved, we need to add a
1793 // GOT entry with a dynamic relocation.
1794 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1795 if (gsym->is_from_dynobj()
1796 || gsym->is_undefined()
1797 || gsym->is_preemptible()
1798 || (gsym->type() == elfcpp::STT_GNU_IFUNC
1799 && parameters->options().output_is_position_independent()))
1800 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1801 rel_dyn, elfcpp::R_386_GLOB_DAT);
1804 // For a STT_GNU_IFUNC symbol we want to write the PLT
1805 // offset into the GOT, so that function pointer
1806 // comparisons work correctly.
1808 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
1809 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
1812 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1813 // Tell the dynamic linker to use the PLT address
1814 // when resolving relocations.
1815 if (gsym->is_from_dynobj()
1816 && !parameters->options().shared())
1817 gsym->set_needs_dynsym_value();
1821 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
1822 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1830 case elfcpp::R_386_PLT32:
1831 // If the symbol is fully resolved, this is just a PC32 reloc.
1832 // Otherwise we need a PLT entry.
1833 if (gsym->final_value_is_known())
1835 // If building a shared library, we can also skip the PLT entry
1836 // if the symbol is defined in the output file and is protected
1838 if (gsym->is_defined()
1839 && !gsym->is_from_dynobj()
1840 && !gsym->is_preemptible())
1842 target->make_plt_entry(symtab, layout, gsym);
1845 case elfcpp::R_386_GOTOFF:
1846 case elfcpp::R_386_GOTPC:
1847 // We need a GOT section.
1848 target->got_section(symtab, layout);
1851 // These are relocations which should only be seen by the
1852 // dynamic linker, and should never be seen here.
1853 case elfcpp::R_386_COPY:
1854 case elfcpp::R_386_GLOB_DAT:
1855 case elfcpp::R_386_JUMP_SLOT:
1856 case elfcpp::R_386_RELATIVE:
1857 case elfcpp::R_386_IRELATIVE:
1858 case elfcpp::R_386_TLS_TPOFF:
1859 case elfcpp::R_386_TLS_DTPMOD32:
1860 case elfcpp::R_386_TLS_DTPOFF32:
1861 case elfcpp::R_386_TLS_TPOFF32:
1862 case elfcpp::R_386_TLS_DESC:
1863 gold_error(_("%s: unexpected reloc %u in object file"),
1864 object->name().c_str(), r_type);
1867 // These are initial tls relocs, which are expected when
1869 case elfcpp::R_386_TLS_GD: // Global-dynamic
1870 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1871 case elfcpp::R_386_TLS_DESC_CALL:
1872 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1873 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1874 case elfcpp::R_386_TLS_IE: // Initial-exec
1875 case elfcpp::R_386_TLS_IE_32:
1876 case elfcpp::R_386_TLS_GOTIE:
1877 case elfcpp::R_386_TLS_LE: // Local-exec
1878 case elfcpp::R_386_TLS_LE_32:
1880 const bool is_final = gsym->final_value_is_known();
1881 const tls::Tls_optimization optimized_type
1882 = Target_i386::optimize_tls_reloc(is_final, r_type);
1885 case elfcpp::R_386_TLS_GD: // Global-dynamic
1886 if (optimized_type == tls::TLSOPT_NONE)
1888 // Create a pair of GOT entries for the module index and
1889 // dtv-relative offset.
1890 Output_data_got<32, false>* got
1891 = target->got_section(symtab, layout);
1892 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1893 target->rel_dyn_section(layout),
1894 elfcpp::R_386_TLS_DTPMOD32,
1895 elfcpp::R_386_TLS_DTPOFF32);
1897 else if (optimized_type == tls::TLSOPT_TO_IE)
1899 // Create a GOT entry for the tp-relative offset.
1900 Output_data_got<32, false>* got
1901 = target->got_section(symtab, layout);
1902 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1903 target->rel_dyn_section(layout),
1904 elfcpp::R_386_TLS_TPOFF);
1906 else if (optimized_type != tls::TLSOPT_TO_LE)
1907 unsupported_reloc_global(object, r_type, gsym);
1910 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1911 target->define_tls_base_symbol(symtab, layout);
1912 if (optimized_type == tls::TLSOPT_NONE)
1914 // Create a double GOT entry with an R_386_TLS_DESC
1915 // reloc. The R_386_TLS_DESC reloc is resolved
1916 // lazily, so the GOT entry needs to be in an area in
1917 // .got.plt, not .got. Call got_section to make sure
1918 // the section has been created.
1919 target->got_section(symtab, layout);
1920 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1921 Reloc_section* rt = target->rel_tls_desc_section(layout);
1922 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
1923 elfcpp::R_386_TLS_DESC, 0);
1925 else if (optimized_type == tls::TLSOPT_TO_IE)
1927 // Create a GOT entry for the tp-relative offset.
1928 Output_data_got<32, false>* got
1929 = target->got_section(symtab, layout);
1930 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1931 target->rel_dyn_section(layout),
1932 elfcpp::R_386_TLS_TPOFF);
1934 else if (optimized_type != tls::TLSOPT_TO_LE)
1935 unsupported_reloc_global(object, r_type, gsym);
1938 case elfcpp::R_386_TLS_DESC_CALL:
1941 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1942 if (optimized_type == tls::TLSOPT_NONE)
1944 // Create a GOT entry for the module index.
1945 target->got_mod_index_entry(symtab, layout, object);
1947 else if (optimized_type != tls::TLSOPT_TO_LE)
1948 unsupported_reloc_global(object, r_type, gsym);
1951 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1954 case elfcpp::R_386_TLS_IE: // Initial-exec
1955 case elfcpp::R_386_TLS_IE_32:
1956 case elfcpp::R_386_TLS_GOTIE:
1957 layout->set_has_static_tls();
1958 if (optimized_type == tls::TLSOPT_NONE)
1960 // For the R_386_TLS_IE relocation, we need to create a
1961 // dynamic relocation when building a shared library.
1962 if (r_type == elfcpp::R_386_TLS_IE
1963 && parameters->options().shared())
1965 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1966 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1967 output_section, object,
1969 reloc.get_r_offset());
1971 // Create a GOT entry for the tp-relative offset.
1972 Output_data_got<32, false>* got
1973 = target->got_section(symtab, layout);
1974 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1975 ? elfcpp::R_386_TLS_TPOFF32
1976 : elfcpp::R_386_TLS_TPOFF);
1977 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1978 ? GOT_TYPE_TLS_OFFSET
1979 : GOT_TYPE_TLS_NOFFSET);
1980 got->add_global_with_rel(gsym, got_type,
1981 target->rel_dyn_section(layout),
1984 else if (optimized_type != tls::TLSOPT_TO_LE)
1985 unsupported_reloc_global(object, r_type, gsym);
1988 case elfcpp::R_386_TLS_LE: // Local-exec
1989 case elfcpp::R_386_TLS_LE_32:
1990 layout->set_has_static_tls();
1991 if (parameters->options().shared())
1993 // We need to create a dynamic relocation.
1994 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1995 ? elfcpp::R_386_TLS_TPOFF32
1996 : elfcpp::R_386_TLS_TPOFF);
1997 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1998 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1999 data_shndx, reloc.get_r_offset());
2009 case elfcpp::R_386_32PLT:
2010 case elfcpp::R_386_TLS_GD_32:
2011 case elfcpp::R_386_TLS_GD_PUSH:
2012 case elfcpp::R_386_TLS_GD_CALL:
2013 case elfcpp::R_386_TLS_GD_POP:
2014 case elfcpp::R_386_TLS_LDM_32:
2015 case elfcpp::R_386_TLS_LDM_PUSH:
2016 case elfcpp::R_386_TLS_LDM_CALL:
2017 case elfcpp::R_386_TLS_LDM_POP:
2018 case elfcpp::R_386_USED_BY_INTEL_200:
2020 unsupported_reloc_global(object, r_type, gsym);
2025 // Process relocations for gc.
2028 Target_i386::gc_process_relocs(Symbol_table* symtab,
2030 Sized_relobj_file<32, false>* object,
2031 unsigned int data_shndx,
2033 const unsigned char* prelocs,
2035 Output_section* output_section,
2036 bool needs_special_offset_handling,
2037 size_t local_symbol_count,
2038 const unsigned char* plocal_symbols)
2040 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2042 Target_i386::Relocatable_size_for_reloc>(
2051 needs_special_offset_handling,
2056 // Scan relocations for a section.
2059 Target_i386::scan_relocs(Symbol_table* symtab,
2061 Sized_relobj_file<32, false>* object,
2062 unsigned int data_shndx,
2063 unsigned int sh_type,
2064 const unsigned char* prelocs,
2066 Output_section* output_section,
2067 bool needs_special_offset_handling,
2068 size_t local_symbol_count,
2069 const unsigned char* plocal_symbols)
2071 if (sh_type == elfcpp::SHT_RELA)
2073 gold_error(_("%s: unsupported RELA reloc section"),
2074 object->name().c_str());
2078 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2088 needs_special_offset_handling,
2093 // Finalize the sections.
2096 Target_i386::do_finalize_sections(
2098 const Input_objects*,
2099 Symbol_table* symtab)
2101 const Reloc_section* rel_plt = (this->plt_ == NULL
2103 : this->plt_->rel_plt());
2104 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2105 this->rel_dyn_, true, false);
2107 // Emit any relocs we saved in an attempt to avoid generating COPY
2109 if (this->copy_relocs_.any_saved_relocs())
2110 this->copy_relocs_.emit(this->rel_dyn_section(layout));
2112 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2113 // the .got.plt section.
2114 Symbol* sym = this->global_offset_table_;
2117 uint32_t data_size = this->got_plt_->current_data_size();
2118 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2122 // Return whether a direct absolute static relocation needs to be applied.
2123 // In cases where Scan::local() or Scan::global() has created
2124 // a dynamic relocation other than R_386_RELATIVE, the addend
2125 // of the relocation is carried in the data, and we must not
2126 // apply the static relocation.
2129 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2130 unsigned int r_type,
2132 Output_section* output_section)
2134 // If the output section is not allocated, then we didn't call
2135 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2137 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2140 int ref_flags = Scan::get_reference_flags(r_type);
2142 // For local symbols, we will have created a non-RELATIVE dynamic
2143 // relocation only if (a) the output is position independent,
2144 // (b) the relocation is absolute (not pc- or segment-relative), and
2145 // (c) the relocation is not 32 bits wide.
2147 return !(parameters->options().output_is_position_independent()
2148 && (ref_flags & Symbol::ABSOLUTE_REF)
2151 // For global symbols, we use the same helper routines used in the
2152 // scan pass. If we did not create a dynamic relocation, or if we
2153 // created a RELATIVE dynamic relocation, we should apply the static
2155 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2156 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2157 && gsym->can_use_relative_reloc(ref_flags
2158 & Symbol::FUNCTION_CALL);
2159 return !has_dyn || is_rel;
2162 // Perform a relocation.
2165 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2166 Target_i386* target,
2167 Output_section* output_section,
2169 const elfcpp::Rel<32, false>& rel,
2170 unsigned int r_type,
2171 const Sized_symbol<32>* gsym,
2172 const Symbol_value<32>* psymval,
2173 unsigned char* view,
2174 elfcpp::Elf_types<32>::Elf_Addr address,
2175 section_size_type view_size)
2177 if (this->skip_call_tls_get_addr_)
2179 if ((r_type != elfcpp::R_386_PLT32
2180 && r_type != elfcpp::R_386_PC32)
2182 || strcmp(gsym->name(), "___tls_get_addr") != 0)
2183 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2184 _("missing expected TLS relocation"));
2187 this->skip_call_tls_get_addr_ = false;
2192 const Sized_relobj_file<32, false>* object = relinfo->object;
2194 // Pick the value to use for symbols defined in shared objects.
2195 Symbol_value<32> symval;
2197 && gsym->type() == elfcpp::STT_GNU_IFUNC
2198 && r_type == elfcpp::R_386_32
2199 && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
2200 && gsym->can_use_relative_reloc(false)
2201 && !gsym->is_from_dynobj()
2202 && !gsym->is_undefined()
2203 && !gsym->is_preemptible())
2205 // In this case we are generating a R_386_IRELATIVE reloc. We
2206 // want to use the real value of the symbol, not the PLT offset.
2208 else if (gsym != NULL
2209 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2211 symval.set_output_value(target->plt_section()->address()
2212 + gsym->plt_offset());
2215 else if (gsym == NULL && psymval->is_ifunc_symbol())
2217 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2218 if (object->local_has_plt_offset(r_sym))
2220 symval.set_output_value(target->plt_section()->address()
2221 + object->local_plt_offset(r_sym));
2226 // Get the GOT offset if needed.
2227 // The GOT pointer points to the end of the GOT section.
2228 // We need to subtract the size of the GOT section to get
2229 // the actual offset to use in the relocation.
2230 bool have_got_offset = false;
2231 unsigned int got_offset = 0;
2234 case elfcpp::R_386_GOT32:
2237 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2238 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2239 - target->got_size());
2243 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2244 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2245 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2246 - target->got_size());
2248 have_got_offset = true;
2257 case elfcpp::R_386_NONE:
2258 case elfcpp::R_386_GNU_VTINHERIT:
2259 case elfcpp::R_386_GNU_VTENTRY:
2262 case elfcpp::R_386_32:
2263 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2264 Relocate_functions<32, false>::rel32(view, object, psymval);
2267 case elfcpp::R_386_PC32:
2268 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2269 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2272 case elfcpp::R_386_16:
2273 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2274 Relocate_functions<32, false>::rel16(view, object, psymval);
2277 case elfcpp::R_386_PC16:
2278 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2279 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2282 case elfcpp::R_386_8:
2283 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2284 Relocate_functions<32, false>::rel8(view, object, psymval);
2287 case elfcpp::R_386_PC8:
2288 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2289 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2292 case elfcpp::R_386_PLT32:
2293 gold_assert(gsym == NULL
2294 || gsym->has_plt_offset()
2295 || gsym->final_value_is_known()
2296 || (gsym->is_defined()
2297 && !gsym->is_from_dynobj()
2298 && !gsym->is_preemptible()));
2299 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2302 case elfcpp::R_386_GOT32:
2303 gold_assert(have_got_offset);
2304 Relocate_functions<32, false>::rel32(view, got_offset);
2307 case elfcpp::R_386_GOTOFF:
2309 elfcpp::Elf_types<32>::Elf_Addr value;
2310 value = (psymval->value(object, 0)
2311 - target->got_plt_section()->address());
2312 Relocate_functions<32, false>::rel32(view, value);
2316 case elfcpp::R_386_GOTPC:
2318 elfcpp::Elf_types<32>::Elf_Addr value;
2319 value = target->got_plt_section()->address();
2320 Relocate_functions<32, false>::pcrel32(view, value, address);
2324 case elfcpp::R_386_COPY:
2325 case elfcpp::R_386_GLOB_DAT:
2326 case elfcpp::R_386_JUMP_SLOT:
2327 case elfcpp::R_386_RELATIVE:
2328 case elfcpp::R_386_IRELATIVE:
2329 // These are outstanding tls relocs, which are unexpected when
2331 case elfcpp::R_386_TLS_TPOFF:
2332 case elfcpp::R_386_TLS_DTPMOD32:
2333 case elfcpp::R_386_TLS_DTPOFF32:
2334 case elfcpp::R_386_TLS_TPOFF32:
2335 case elfcpp::R_386_TLS_DESC:
2336 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2337 _("unexpected reloc %u in object file"),
2341 // These are initial tls relocs, which are expected when
2343 case elfcpp::R_386_TLS_GD: // Global-dynamic
2344 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2345 case elfcpp::R_386_TLS_DESC_CALL:
2346 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2347 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2348 case elfcpp::R_386_TLS_IE: // Initial-exec
2349 case elfcpp::R_386_TLS_IE_32:
2350 case elfcpp::R_386_TLS_GOTIE:
2351 case elfcpp::R_386_TLS_LE: // Local-exec
2352 case elfcpp::R_386_TLS_LE_32:
2353 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2354 view, address, view_size);
2357 case elfcpp::R_386_32PLT:
2358 case elfcpp::R_386_TLS_GD_32:
2359 case elfcpp::R_386_TLS_GD_PUSH:
2360 case elfcpp::R_386_TLS_GD_CALL:
2361 case elfcpp::R_386_TLS_GD_POP:
2362 case elfcpp::R_386_TLS_LDM_32:
2363 case elfcpp::R_386_TLS_LDM_PUSH:
2364 case elfcpp::R_386_TLS_LDM_CALL:
2365 case elfcpp::R_386_TLS_LDM_POP:
2366 case elfcpp::R_386_USED_BY_INTEL_200:
2368 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2369 _("unsupported reloc %u"),
2377 // Perform a TLS relocation.
2380 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
2381 Target_i386* target,
2383 const elfcpp::Rel<32, false>& rel,
2384 unsigned int r_type,
2385 const Sized_symbol<32>* gsym,
2386 const Symbol_value<32>* psymval,
2387 unsigned char* view,
2388 elfcpp::Elf_types<32>::Elf_Addr,
2389 section_size_type view_size)
2391 Output_segment* tls_segment = relinfo->layout->tls_segment();
2393 const Sized_relobj_file<32, false>* object = relinfo->object;
2395 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
2397 const bool is_final = (gsym == NULL
2398 ? !parameters->options().shared()
2399 : gsym->final_value_is_known());
2400 const tls::Tls_optimization optimized_type
2401 = Target_i386::optimize_tls_reloc(is_final, r_type);
2404 case elfcpp::R_386_TLS_GD: // Global-dynamic
2405 if (optimized_type == tls::TLSOPT_TO_LE)
2407 gold_assert(tls_segment != NULL);
2408 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2409 rel, r_type, value, view,
2415 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2416 ? GOT_TYPE_TLS_NOFFSET
2417 : GOT_TYPE_TLS_PAIR);
2418 unsigned int got_offset;
2421 gold_assert(gsym->has_got_offset(got_type));
2422 got_offset = gsym->got_offset(got_type) - target->got_size();
2426 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2427 gold_assert(object->local_has_got_offset(r_sym, got_type));
2428 got_offset = (object->local_got_offset(r_sym, got_type)
2429 - target->got_size());
2431 if (optimized_type == tls::TLSOPT_TO_IE)
2433 gold_assert(tls_segment != NULL);
2434 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2435 got_offset, view, view_size);
2438 else if (optimized_type == tls::TLSOPT_NONE)
2440 // Relocate the field with the offset of the pair of GOT
2442 Relocate_functions<32, false>::rel32(view, got_offset);
2446 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2447 _("unsupported reloc %u"),
2451 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2452 case elfcpp::R_386_TLS_DESC_CALL:
2453 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2454 if (optimized_type == tls::TLSOPT_TO_LE)
2456 gold_assert(tls_segment != NULL);
2457 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2458 rel, r_type, value, view,
2464 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2465 ? GOT_TYPE_TLS_NOFFSET
2466 : GOT_TYPE_TLS_DESC);
2467 unsigned int got_offset = 0;
2468 if (r_type == elfcpp::R_386_TLS_GOTDESC
2469 && optimized_type == tls::TLSOPT_NONE)
2471 // We created GOT entries in the .got.tlsdesc portion of
2472 // the .got.plt section, but the offset stored in the
2473 // symbol is the offset within .got.tlsdesc.
2474 got_offset = (target->got_size()
2475 + target->got_plt_section()->data_size());
2479 gold_assert(gsym->has_got_offset(got_type));
2480 got_offset += gsym->got_offset(got_type) - target->got_size();
2484 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2485 gold_assert(object->local_has_got_offset(r_sym, got_type));
2486 got_offset += (object->local_got_offset(r_sym, got_type)
2487 - target->got_size());
2489 if (optimized_type == tls::TLSOPT_TO_IE)
2491 gold_assert(tls_segment != NULL);
2492 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2493 got_offset, view, view_size);
2496 else if (optimized_type == tls::TLSOPT_NONE)
2498 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2500 // Relocate the field with the offset of the pair of GOT
2502 Relocate_functions<32, false>::rel32(view, got_offset);
2507 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2508 _("unsupported reloc %u"),
2512 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2513 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2515 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2516 _("both SUN and GNU model "
2517 "TLS relocations"));
2520 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2521 if (optimized_type == tls::TLSOPT_TO_LE)
2523 gold_assert(tls_segment != NULL);
2524 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2525 value, view, view_size);
2528 else if (optimized_type == tls::TLSOPT_NONE)
2530 // Relocate the field with the offset of the GOT entry for
2531 // the module index.
2532 unsigned int got_offset;
2533 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2534 - target->got_size());
2535 Relocate_functions<32, false>::rel32(view, got_offset);
2538 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2539 _("unsupported reloc %u"),
2543 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2544 if (optimized_type == tls::TLSOPT_TO_LE)
2546 // This reloc can appear in debugging sections, in which
2547 // case we must not convert to local-exec. We decide what
2548 // to do based on whether the section is marked as
2549 // containing executable code. That is what the GNU linker
2551 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2552 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2554 gold_assert(tls_segment != NULL);
2555 value -= tls_segment->memsz();
2558 Relocate_functions<32, false>::rel32(view, value);
2561 case elfcpp::R_386_TLS_IE: // Initial-exec
2562 case elfcpp::R_386_TLS_GOTIE:
2563 case elfcpp::R_386_TLS_IE_32:
2564 if (optimized_type == tls::TLSOPT_TO_LE)
2566 gold_assert(tls_segment != NULL);
2567 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2568 rel, r_type, value, view,
2572 else if (optimized_type == tls::TLSOPT_NONE)
2574 // Relocate the field with the offset of the GOT entry for
2575 // the tp-relative offset of the symbol.
2576 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2577 ? GOT_TYPE_TLS_OFFSET
2578 : GOT_TYPE_TLS_NOFFSET);
2579 unsigned int got_offset;
2582 gold_assert(gsym->has_got_offset(got_type));
2583 got_offset = gsym->got_offset(got_type);
2587 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2588 gold_assert(object->local_has_got_offset(r_sym, got_type));
2589 got_offset = object->local_got_offset(r_sym, got_type);
2591 // For the R_386_TLS_IE relocation, we need to apply the
2592 // absolute address of the GOT entry.
2593 if (r_type == elfcpp::R_386_TLS_IE)
2594 got_offset += target->got_plt_section()->address();
2595 // All GOT offsets are relative to the end of the GOT.
2596 got_offset -= target->got_size();
2597 Relocate_functions<32, false>::rel32(view, got_offset);
2600 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2601 _("unsupported reloc %u"),
2605 case elfcpp::R_386_TLS_LE: // Local-exec
2606 // If we're creating a shared library, a dynamic relocation will
2607 // have been created for this location, so do not apply it now.
2608 if (!parameters->options().shared())
2610 gold_assert(tls_segment != NULL);
2611 value -= tls_segment->memsz();
2612 Relocate_functions<32, false>::rel32(view, value);
2616 case elfcpp::R_386_TLS_LE_32:
2617 // If we're creating a shared library, a dynamic relocation will
2618 // have been created for this location, so do not apply it now.
2619 if (!parameters->options().shared())
2621 gold_assert(tls_segment != NULL);
2622 value = tls_segment->memsz() - value;
2623 Relocate_functions<32, false>::rel32(view, value);
2629 // Do a relocation in which we convert a TLS General-Dynamic to a
2633 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2635 Output_segment* tls_segment,
2636 const elfcpp::Rel<32, false>& rel,
2638 elfcpp::Elf_types<32>::Elf_Addr value,
2639 unsigned char* view,
2640 section_size_type view_size)
2642 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2643 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2644 // leal foo(%reg),%eax; call ___tls_get_addr
2645 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2647 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2648 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2650 unsigned char op1 = view[-1];
2651 unsigned char op2 = view[-2];
2653 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2654 op2 == 0x8d || op2 == 0x04);
2655 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2661 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2662 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2663 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2664 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2665 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2669 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2670 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2671 if (rel.get_r_offset() + 9 < view_size
2674 // There is a trailing nop. Use the size byte subl.
2675 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2680 // Use the five byte subl.
2681 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2685 value = tls_segment->memsz() - value;
2686 Relocate_functions<32, false>::rel32(view + roff, value);
2688 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2690 this->skip_call_tls_get_addr_ = true;
2693 // Do a relocation in which we convert a TLS General-Dynamic to an
2697 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
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(,%ebx,1),%eax; call ___tls_get_addr
2707 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2709 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2710 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2712 unsigned char op1 = view[-1];
2713 unsigned char op2 = view[-2];
2715 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2716 op2 == 0x8d || op2 == 0x04);
2717 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2721 // FIXME: For now, support only the first (SIB) form.
2722 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2726 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2727 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2728 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2729 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2730 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2734 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2735 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2736 if (rel.get_r_offset() + 9 < view_size
2739 // FIXME: This is not the right instruction sequence.
2740 // There is a trailing nop. Use the size byte subl.
2741 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2746 // FIXME: This is not the right instruction sequence.
2747 // Use the five byte subl.
2748 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2752 Relocate_functions<32, false>::rel32(view + roff, value);
2754 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2756 this->skip_call_tls_get_addr_ = true;
2759 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2760 // General-Dynamic to a Local-Exec.
2763 Target_i386::Relocate::tls_desc_gd_to_le(
2764 const Relocate_info<32, false>* relinfo,
2766 Output_segment* tls_segment,
2767 const elfcpp::Rel<32, false>& rel,
2768 unsigned int r_type,
2769 elfcpp::Elf_types<32>::Elf_Addr value,
2770 unsigned char* view,
2771 section_size_type view_size)
2773 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2775 // leal foo@TLSDESC(%ebx), %eax
2776 // ==> leal foo@NTPOFF, %eax
2777 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2778 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2779 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2780 view[-2] == 0x8d && view[-1] == 0x83);
2782 value -= tls_segment->memsz();
2783 Relocate_functions<32, false>::rel32(view, value);
2787 // call *foo@TLSCALL(%eax)
2789 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2790 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2791 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2792 view[0] == 0xff && view[1] == 0x10);
2798 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2799 // General-Dynamic to an Initial-Exec.
2802 Target_i386::Relocate::tls_desc_gd_to_ie(
2803 const Relocate_info<32, false>* relinfo,
2806 const elfcpp::Rel<32, false>& rel,
2807 unsigned int r_type,
2808 elfcpp::Elf_types<32>::Elf_Addr value,
2809 unsigned char* view,
2810 section_size_type view_size)
2812 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2814 // leal foo@TLSDESC(%ebx), %eax
2815 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2816 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2817 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2818 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2819 view[-2] == 0x8d && view[-1] == 0x83);
2821 Relocate_functions<32, false>::rel32(view, value);
2825 // call *foo@TLSCALL(%eax)
2827 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2828 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2829 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2830 view[0] == 0xff && view[1] == 0x10);
2836 // Do a relocation in which we convert a TLS Local-Dynamic to a
2840 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2843 const elfcpp::Rel<32, false>& rel,
2845 elfcpp::Elf_types<32>::Elf_Addr,
2846 unsigned char* view,
2847 section_size_type view_size)
2849 // leal foo(%reg), %eax; call ___tls_get_addr
2850 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2852 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2853 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2855 // FIXME: Does this test really always pass?
2856 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2857 view[-2] == 0x8d && view[-1] == 0x83);
2859 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2861 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2863 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2865 this->skip_call_tls_get_addr_ = true;
2868 // Do a relocation in which we convert a TLS Initial-Exec to a
2872 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2874 Output_segment* tls_segment,
2875 const elfcpp::Rel<32, false>& rel,
2876 unsigned int r_type,
2877 elfcpp::Elf_types<32>::Elf_Addr value,
2878 unsigned char* view,
2879 section_size_type view_size)
2881 // We have to actually change the instructions, which means that we
2882 // need to examine the opcodes to figure out which instruction we
2884 if (r_type == elfcpp::R_386_TLS_IE)
2886 // movl %gs:XX,%eax ==> movl $YY,%eax
2887 // movl %gs:XX,%reg ==> movl $YY,%reg
2888 // addl %gs:XX,%reg ==> addl $YY,%reg
2889 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2890 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2892 unsigned char op1 = view[-1];
2895 // movl XX,%eax ==> movl $YY,%eax
2900 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2902 unsigned char op2 = view[-2];
2905 // movl XX,%reg ==> movl $YY,%reg
2906 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2907 (op1 & 0xc7) == 0x05);
2909 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2911 else if (op2 == 0x03)
2913 // addl XX,%reg ==> addl $YY,%reg
2914 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2915 (op1 & 0xc7) == 0x05);
2917 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2920 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2925 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2926 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2927 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2928 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2929 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2931 unsigned char op1 = view[-1];
2932 unsigned char op2 = view[-2];
2933 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2934 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2937 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2939 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2941 else if (op2 == 0x2b)
2943 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2945 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2947 else if (op2 == 0x03)
2949 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2951 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2954 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2957 value = tls_segment->memsz() - value;
2958 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2961 Relocate_functions<32, false>::rel32(view, value);
2964 // Relocate section data.
2967 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2968 unsigned int sh_type,
2969 const unsigned char* prelocs,
2971 Output_section* output_section,
2972 bool needs_special_offset_handling,
2973 unsigned char* view,
2974 elfcpp::Elf_types<32>::Elf_Addr address,
2975 section_size_type view_size,
2976 const Reloc_symbol_changes* reloc_symbol_changes)
2978 gold_assert(sh_type == elfcpp::SHT_REL);
2980 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2981 Target_i386::Relocate>(
2987 needs_special_offset_handling,
2991 reloc_symbol_changes);
2994 // Return the size of a relocation while scanning during a relocatable
2998 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2999 unsigned int r_type,
3004 case elfcpp::R_386_NONE:
3005 case elfcpp::R_386_GNU_VTINHERIT:
3006 case elfcpp::R_386_GNU_VTENTRY:
3007 case elfcpp::R_386_TLS_GD: // Global-dynamic
3008 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
3009 case elfcpp::R_386_TLS_DESC_CALL:
3010 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3011 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3012 case elfcpp::R_386_TLS_IE: // Initial-exec
3013 case elfcpp::R_386_TLS_IE_32:
3014 case elfcpp::R_386_TLS_GOTIE:
3015 case elfcpp::R_386_TLS_LE: // Local-exec
3016 case elfcpp::R_386_TLS_LE_32:
3019 case elfcpp::R_386_32:
3020 case elfcpp::R_386_PC32:
3021 case elfcpp::R_386_GOT32:
3022 case elfcpp::R_386_PLT32:
3023 case elfcpp::R_386_GOTOFF:
3024 case elfcpp::R_386_GOTPC:
3027 case elfcpp::R_386_16:
3028 case elfcpp::R_386_PC16:
3031 case elfcpp::R_386_8:
3032 case elfcpp::R_386_PC8:
3035 // These are relocations which should only be seen by the
3036 // dynamic linker, and should never be seen here.
3037 case elfcpp::R_386_COPY:
3038 case elfcpp::R_386_GLOB_DAT:
3039 case elfcpp::R_386_JUMP_SLOT:
3040 case elfcpp::R_386_RELATIVE:
3041 case elfcpp::R_386_IRELATIVE:
3042 case elfcpp::R_386_TLS_TPOFF:
3043 case elfcpp::R_386_TLS_DTPMOD32:
3044 case elfcpp::R_386_TLS_DTPOFF32:
3045 case elfcpp::R_386_TLS_TPOFF32:
3046 case elfcpp::R_386_TLS_DESC:
3047 object->error(_("unexpected reloc %u in object file"), r_type);
3050 case elfcpp::R_386_32PLT:
3051 case elfcpp::R_386_TLS_GD_32:
3052 case elfcpp::R_386_TLS_GD_PUSH:
3053 case elfcpp::R_386_TLS_GD_CALL:
3054 case elfcpp::R_386_TLS_GD_POP:
3055 case elfcpp::R_386_TLS_LDM_32:
3056 case elfcpp::R_386_TLS_LDM_PUSH:
3057 case elfcpp::R_386_TLS_LDM_CALL:
3058 case elfcpp::R_386_TLS_LDM_POP:
3059 case elfcpp::R_386_USED_BY_INTEL_200:
3061 object->error(_("unsupported reloc %u in object file"), r_type);
3066 // Scan the relocs during a relocatable link.
3069 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3071 Sized_relobj_file<32, false>* object,
3072 unsigned int data_shndx,
3073 unsigned int sh_type,
3074 const unsigned char* prelocs,
3076 Output_section* output_section,
3077 bool needs_special_offset_handling,
3078 size_t local_symbol_count,
3079 const unsigned char* plocal_symbols,
3080 Relocatable_relocs* rr)
3082 gold_assert(sh_type == elfcpp::SHT_REL);
3084 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3085 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3087 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
3088 Scan_relocatable_relocs>(
3096 needs_special_offset_handling,
3102 // Relocate a section during a relocatable link.
3105 Target_i386::relocate_for_relocatable(
3106 const Relocate_info<32, false>* relinfo,
3107 unsigned int sh_type,
3108 const unsigned char* prelocs,
3110 Output_section* output_section,
3111 off_t offset_in_output_section,
3112 const Relocatable_relocs* rr,
3113 unsigned char* view,
3114 elfcpp::Elf_types<32>::Elf_Addr view_address,
3115 section_size_type view_size,
3116 unsigned char* reloc_view,
3117 section_size_type reloc_view_size)
3119 gold_assert(sh_type == elfcpp::SHT_REL);
3121 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
3126 offset_in_output_section,
3135 // Return the value to use for a dynamic which requires special
3136 // treatment. This is how we support equality comparisons of function
3137 // pointers across shared library boundaries, as described in the
3138 // processor specific ABI supplement.
3141 Target_i386::do_dynsym_value(const Symbol* gsym) const
3143 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3144 return this->plt_section()->address() + gsym->plt_offset();
3147 // Return a string used to fill a code section with nops to take up
3148 // the specified length.
3151 Target_i386::do_code_fill(section_size_type length) const
3155 // Build a jmp instruction to skip over the bytes.
3156 unsigned char jmp[5];
3158 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3159 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3160 + std::string(length - 5, '\0'));
3163 // Nop sequences of various lengths.
3164 const char nop1[1] = { 0x90 }; // nop
3165 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3166 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3167 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3168 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3169 0x00 }; // leal 0(%esi,1),%esi
3170 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3172 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3174 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3175 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3176 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3177 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3179 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3180 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3182 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3183 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3185 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3186 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3187 0x00, 0x00, 0x00, 0x00 };
3188 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3189 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3190 0x27, 0x00, 0x00, 0x00,
3192 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3193 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3194 0xbc, 0x27, 0x00, 0x00,
3196 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3197 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3198 0x90, 0x90, 0x90, 0x90,
3201 const char* nops[16] = {
3203 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3204 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3207 return std::string(nops[length], length);
3210 // Return whether SYM should be treated as a call to a non-split
3211 // function. We don't want that to be true of a call to a
3212 // get_pc_thunk function.
3215 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
3217 return (sym->type() == elfcpp::STT_FUNC
3218 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3221 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3222 // compiled with -fsplit-stack. The function calls non-split-stack
3223 // code. We have to change the function so that it always ensures
3224 // that it has enough stack space to run some random function.
3227 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3228 section_offset_type fnoffset,
3229 section_size_type fnsize,
3230 unsigned char* view,
3231 section_size_type view_size,
3233 std::string* to) const
3235 // The function starts with a comparison of the stack pointer and a
3236 // field in the TCB. This is followed by a jump.
3239 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3242 // We will call __morestack if the carry flag is set after this
3243 // comparison. We turn the comparison into an stc instruction
3245 view[fnoffset] = '\xf9';
3246 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3248 // lea NN(%esp),%ecx
3249 // lea NN(%esp),%edx
3250 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3251 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
3254 // This is loading an offset from the stack pointer for a
3255 // comparison. The offset is negative, so we decrease the
3256 // offset by the amount of space we need for the stack. This
3257 // means we will avoid calling __morestack if there happens to
3258 // be plenty of space on the stack already.
3259 unsigned char* pval = view + fnoffset + 3;
3260 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3261 val -= parameters->options().split_stack_adjust_size();
3262 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3266 if (!object->has_no_split_stack())
3267 object->error(_("failed to match split-stack sequence at "
3268 "section %u offset %0zx"),
3269 shndx, static_cast<size_t>(fnoffset));
3273 // We have to change the function so that it calls
3274 // __morestack_non_split instead of __morestack. The former will
3275 // allocate additional stack space.
3276 *from = "__morestack";
3277 *to = "__morestack_non_split";
3280 // The selector for i386 object files.
3282 class Target_selector_i386 : public Target_selector_freebsd
3285 Target_selector_i386()
3286 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
3287 "elf32-i386", "elf32-i386-freebsd")
3291 do_instantiate_target()
3292 { return new Target_i386(); }
3295 Target_selector_i386 target_selector_i386;
3297 } // End anonymous namespace.