1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010 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<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<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 Target_freebsd<32, false>
164 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
167 : Target_freebsd<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)
175 can_check_for_function_pointers() const
178 // Process the relocations to determine unreferenced sections for
179 // garbage collection.
181 gc_process_relocs(Symbol_table* symtab,
183 Sized_relobj<32, false>* object,
184 unsigned int data_shndx,
185 unsigned int sh_type,
186 const unsigned char* prelocs,
188 Output_section* output_section,
189 bool needs_special_offset_handling,
190 size_t local_symbol_count,
191 const unsigned char* plocal_symbols);
193 // Scan the relocations to look for symbol adjustments.
195 scan_relocs(Symbol_table* symtab,
197 Sized_relobj<32, false>* object,
198 unsigned int data_shndx,
199 unsigned int sh_type,
200 const unsigned char* prelocs,
202 Output_section* output_section,
203 bool needs_special_offset_handling,
204 size_t local_symbol_count,
205 const unsigned char* plocal_symbols);
207 // Finalize the sections.
209 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
211 // Return the value to use for a dynamic which requires special
214 do_dynsym_value(const Symbol*) const;
216 // Relocate a section.
218 relocate_section(const Relocate_info<32, false>*,
219 unsigned int sh_type,
220 const unsigned char* prelocs,
222 Output_section* output_section,
223 bool needs_special_offset_handling,
225 elfcpp::Elf_types<32>::Elf_Addr view_address,
226 section_size_type view_size,
227 const Reloc_symbol_changes*);
229 // Scan the relocs during a relocatable link.
231 scan_relocatable_relocs(Symbol_table* symtab,
233 Sized_relobj<32, false>* object,
234 unsigned int data_shndx,
235 unsigned int sh_type,
236 const unsigned char* prelocs,
238 Output_section* output_section,
239 bool needs_special_offset_handling,
240 size_t local_symbol_count,
241 const unsigned char* plocal_symbols,
242 Relocatable_relocs*);
244 // Relocate a section during a relocatable link.
246 relocate_for_relocatable(const Relocate_info<32, false>*,
247 unsigned int sh_type,
248 const unsigned char* prelocs,
250 Output_section* output_section,
251 off_t offset_in_output_section,
252 const Relocatable_relocs*,
254 elfcpp::Elf_types<32>::Elf_Addr view_address,
255 section_size_type view_size,
256 unsigned char* reloc_view,
257 section_size_type reloc_view_size);
259 // Return a string used to fill a code section with nops.
261 do_code_fill(section_size_type length) const;
263 // Return whether SYM is defined by the ABI.
265 do_is_defined_by_abi(const Symbol* sym) const
266 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
268 // Return whether a symbol name implies a local label. The UnixWare
269 // 2.1 cc generates temporary symbols that start with .X, so we
270 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
271 // If so, we should move the .X recognition into
272 // Target::do_is_local_label_name.
274 do_is_local_label_name(const char* name) const
276 if (name[0] == '.' && name[1] == 'X')
278 return Target::do_is_local_label_name(name);
281 // Return the PLT section.
283 do_plt_section_for_global(const Symbol*) const
284 { return this->plt_section(); }
287 do_plt_section_for_local(const Relobj*, unsigned int) const
288 { return this->plt_section(); }
290 // Return whether SYM is call to a non-split function.
292 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
294 // Adjust -fstack-split code which calls non-stack-split code.
296 do_calls_non_split(Relobj* object, unsigned int shndx,
297 section_offset_type fnoffset, section_size_type fnsize,
298 unsigned char* view, section_size_type view_size,
299 std::string* from, std::string* to) const;
301 // Return the size of the GOT section.
305 gold_assert(this->got_ != NULL);
306 return this->got_->data_size();
309 // Return the number of entries in the GOT.
311 got_entry_count() const
313 if (this->got_ == NULL)
315 return this->got_size() / 4;
318 // Return the number of entries in the PLT.
320 plt_entry_count() const;
322 // Return the offset of the first non-reserved PLT entry.
324 first_plt_entry_offset() const;
326 // Return the size of each PLT entry.
328 plt_entry_size() const;
331 // The class which scans relocations.
335 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
336 Sized_relobj<32, false>* object,
337 unsigned int data_shndx,
338 Output_section* output_section,
339 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
340 const elfcpp::Sym<32, false>& lsym);
343 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
344 Sized_relobj<32, false>* object,
345 unsigned int data_shndx,
346 Output_section* output_section,
347 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
351 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
353 Sized_relobj<32, false>* object,
354 unsigned int data_shndx,
355 Output_section* output_section,
356 const elfcpp::Rel<32, false>& reloc,
358 const elfcpp::Sym<32, false>& lsym);
361 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
363 Sized_relobj<32, false>* object,
364 unsigned int data_shndx,
365 Output_section* output_section,
366 const elfcpp::Rel<32, false>& reloc,
371 possible_function_pointer_reloc(unsigned int r_type);
374 reloc_needs_plt_for_ifunc(Sized_relobj<32, false>*, unsigned int r_type);
377 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
380 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
384 // The class which implements relocation.
389 : skip_call_tls_get_addr_(false),
390 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
395 if (this->skip_call_tls_get_addr_)
397 // FIXME: This needs to specify the location somehow.
398 gold_error(_("missing expected TLS relocation"));
402 // Return whether the static relocation needs to be applied.
404 should_apply_static_reloc(const Sized_symbol<32>* gsym,
407 Output_section* output_section);
409 // Do a relocation. Return false if the caller should not issue
410 // any warnings about this relocation.
412 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
413 size_t relnum, const elfcpp::Rel<32, false>&,
414 unsigned int r_type, const Sized_symbol<32>*,
415 const Symbol_value<32>*,
416 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
420 // Do a TLS relocation.
422 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
423 size_t relnum, const elfcpp::Rel<32, false>&,
424 unsigned int r_type, const Sized_symbol<32>*,
425 const Symbol_value<32>*,
426 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
429 // Do a TLS General-Dynamic to Initial-Exec transition.
431 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
432 Output_segment* tls_segment,
433 const elfcpp::Rel<32, false>&, unsigned int r_type,
434 elfcpp::Elf_types<32>::Elf_Addr value,
436 section_size_type view_size);
438 // Do a TLS General-Dynamic to Local-Exec transition.
440 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
441 Output_segment* tls_segment,
442 const elfcpp::Rel<32, false>&, unsigned int r_type,
443 elfcpp::Elf_types<32>::Elf_Addr value,
445 section_size_type view_size);
447 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
450 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
451 Output_segment* tls_segment,
452 const elfcpp::Rel<32, false>&, unsigned int r_type,
453 elfcpp::Elf_types<32>::Elf_Addr value,
455 section_size_type view_size);
457 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
460 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
461 Output_segment* tls_segment,
462 const elfcpp::Rel<32, false>&, unsigned int r_type,
463 elfcpp::Elf_types<32>::Elf_Addr value,
465 section_size_type view_size);
467 // Do a TLS Local-Dynamic to Local-Exec transition.
469 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
470 Output_segment* tls_segment,
471 const elfcpp::Rel<32, false>&, unsigned int r_type,
472 elfcpp::Elf_types<32>::Elf_Addr value,
474 section_size_type view_size);
476 // Do a TLS Initial-Exec to Local-Exec transition.
478 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
479 Output_segment* tls_segment,
480 const elfcpp::Rel<32, false>&, unsigned int r_type,
481 elfcpp::Elf_types<32>::Elf_Addr value,
483 section_size_type view_size);
485 // We need to keep track of which type of local dynamic relocation
486 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
487 enum Local_dynamic_type
494 // This is set if we should skip the next reloc, which should be a
495 // PLT32 reloc against ___tls_get_addr.
496 bool skip_call_tls_get_addr_;
497 // The type of local dynamic relocation we have seen in the section
498 // being relocated, if any.
499 Local_dynamic_type local_dynamic_type_;
502 // A class which returns the size required for a relocation type,
503 // used while scanning relocs during a relocatable link.
504 class Relocatable_size_for_reloc
508 get_size_for_reloc(unsigned int, Relobj*);
511 // Adjust TLS relocation type based on the options and whether this
512 // is a local symbol.
513 static tls::Tls_optimization
514 optimize_tls_reloc(bool is_final, int r_type);
516 // Get the GOT section, creating it if necessary.
517 Output_data_got<32, false>*
518 got_section(Symbol_table*, Layout*);
520 // Get the GOT PLT section.
522 got_plt_section() const
524 gold_assert(this->got_plt_ != NULL);
525 return this->got_plt_;
528 // Get the GOT section for TLSDESC entries.
529 Output_data_got<32, false>*
530 got_tlsdesc_section() const
532 gold_assert(this->got_tlsdesc_ != NULL);
533 return this->got_tlsdesc_;
536 // Create the PLT section.
538 make_plt_section(Symbol_table* symtab, Layout* layout);
540 // Create a PLT entry for a global symbol.
542 make_plt_entry(Symbol_table*, Layout*, Symbol*);
544 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
546 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
547 Sized_relobj<32, false>* relobj,
548 unsigned int local_sym_index);
550 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
552 define_tls_base_symbol(Symbol_table*, Layout*);
554 // Create a GOT entry for the TLS module index.
556 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
557 Sized_relobj<32, false>* object);
559 // Get the PLT section.
560 Output_data_plt_i386*
563 gold_assert(this->plt_ != NULL);
567 // Get the dynamic reloc section, creating it if necessary.
569 rel_dyn_section(Layout*);
571 // Get the section to use for TLS_DESC relocations.
573 rel_tls_desc_section(Layout*) const;
575 // Add a potential copy relocation.
577 copy_reloc(Symbol_table* symtab, Layout* layout,
578 Sized_relobj<32, false>* object,
579 unsigned int shndx, Output_section* output_section,
580 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
582 this->copy_relocs_.copy_reloc(symtab, layout,
583 symtab->get_sized_symbol<32>(sym),
584 object, shndx, output_section, reloc,
585 this->rel_dyn_section(layout));
588 // Information about this specific target which we pass to the
589 // general Target structure.
590 static const Target::Target_info i386_info;
592 // The types of GOT entries needed for this platform.
593 // These values are exposed to the ABI in an incremental link.
594 // Do not renumber existing values without changing the version
595 // number of the .gnu_incremental_inputs section.
598 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
599 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
600 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
601 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
602 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
606 Output_data_got<32, false>* got_;
608 Output_data_plt_i386* plt_;
609 // The GOT PLT section.
610 Output_data_space* got_plt_;
611 // The GOT section for TLSDESC relocations.
612 Output_data_got<32, false>* got_tlsdesc_;
613 // The _GLOBAL_OFFSET_TABLE_ symbol.
614 Symbol* global_offset_table_;
615 // The dynamic reloc section.
616 Reloc_section* rel_dyn_;
617 // Relocs saved to avoid a COPY reloc.
618 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
619 // Space for variables copied with a COPY reloc.
620 Output_data_space* dynbss_;
621 // Offset of the GOT entry for the TLS module index.
622 unsigned int got_mod_index_offset_;
623 // True if the _TLS_MODULE_BASE_ symbol has been defined.
624 bool tls_base_symbol_defined_;
627 const Target::Target_info Target_i386::i386_info =
630 false, // is_big_endian
631 elfcpp::EM_386, // machine_code
632 false, // has_make_symbol
633 false, // has_resolve
634 true, // has_code_fill
635 true, // is_default_stack_executable
637 "/usr/lib/libc.so.1", // dynamic_linker
638 0x08048000, // default_text_segment_address
639 0x1000, // abi_pagesize (overridable by -z max-page-size)
640 0x1000, // common_pagesize (overridable by -z common-page-size)
641 elfcpp::SHN_UNDEF, // small_common_shndx
642 elfcpp::SHN_UNDEF, // large_common_shndx
643 0, // small_common_section_flags
644 0, // large_common_section_flags
645 NULL, // attributes_section
646 NULL // attributes_vendor
649 // Get the GOT section, creating it if necessary.
651 Output_data_got<32, false>*
652 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
654 if (this->got_ == NULL)
656 gold_assert(symtab != NULL && layout != NULL);
658 this->got_ = new Output_data_got<32, false>();
660 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
662 | elfcpp::SHF_WRITE),
663 this->got_, ORDER_RELRO_LAST, true);
665 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
666 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
668 | elfcpp::SHF_WRITE),
669 this->got_plt_, ORDER_NON_RELRO_FIRST,
672 // The first three entries are reserved.
673 this->got_plt_->set_current_data_size(3 * 4);
675 // Those bytes can go into the relro segment.
676 layout->increase_relro(3 * 4);
678 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
679 this->global_offset_table_ =
680 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
681 Symbol_table::PREDEFINED,
683 0, 0, elfcpp::STT_OBJECT,
685 elfcpp::STV_HIDDEN, 0,
688 // If there are any TLSDESC relocations, they get GOT entries in
689 // .got.plt after the jump slot entries.
690 this->got_tlsdesc_ = new Output_data_got<32, false>();
691 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
693 | elfcpp::SHF_WRITE),
695 ORDER_NON_RELRO_FIRST, false);
701 // Get the dynamic reloc section, creating it if necessary.
703 Target_i386::Reloc_section*
704 Target_i386::rel_dyn_section(Layout* layout)
706 if (this->rel_dyn_ == NULL)
708 gold_assert(layout != NULL);
709 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
710 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
711 elfcpp::SHF_ALLOC, this->rel_dyn_,
712 ORDER_DYNAMIC_RELOCS, false);
714 return this->rel_dyn_;
717 // Create the PLT section. The ordinary .got section is an argument,
718 // since we need to refer to the start. We also create our own .got
719 // section just for PLT entries.
721 Output_data_plt_i386::Output_data_plt_i386(Symbol_table* symtab,
723 Output_data_space* got_plt)
724 : Output_section_data(4), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0),
725 global_ifuncs_(), local_ifuncs_()
727 this->rel_ = new Reloc_section(false);
728 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
729 elfcpp::SHF_ALLOC, this->rel_,
730 ORDER_DYNAMIC_PLT_RELOCS, false);
732 if (parameters->doing_static_link())
734 // A statically linked executable will only have a .rel.plt
735 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
736 // symbols. The library will use these symbols to locate the
737 // IRELATIVE relocs at program startup time.
738 symtab->define_in_output_data("__rel_iplt_start", NULL,
739 Symbol_table::PREDEFINED,
740 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
741 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
743 symtab->define_in_output_data("__rel_iplt_end", NULL,
744 Symbol_table::PREDEFINED,
745 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
746 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
752 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
754 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
755 // linker, and so do we.
759 // Add an entry to the PLT.
762 Output_data_plt_i386::add_entry(Symbol* gsym)
764 gold_assert(!gsym->has_plt_offset());
766 // Note that when setting the PLT offset we skip the initial
767 // reserved PLT entry.
768 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
772 section_offset_type got_offset = this->got_plt_->current_data_size();
774 // Every PLT entry needs a GOT entry which points back to the PLT
775 // entry (this will be changed by the dynamic linker, normally
776 // lazily when the function is called).
777 this->got_plt_->set_current_data_size(got_offset + 4);
779 // Every PLT entry needs a reloc.
780 if (gsym->type() == elfcpp::STT_GNU_IFUNC
781 && gsym->can_use_relative_reloc(false))
783 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
784 this->got_plt_, got_offset);
785 struct Global_ifunc gi;
787 gi.got_offset = got_offset;
788 this->global_ifuncs_.push_back(gi);
792 gsym->set_needs_dynsym_entry();
793 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
797 // Note that we don't need to save the symbol. The contents of the
798 // PLT are independent of which symbols are used. The symbols only
799 // appear in the relocations.
802 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
806 Output_data_plt_i386::add_local_ifunc_entry(Sized_relobj<32, false>* relobj,
807 unsigned int local_sym_index)
809 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
812 section_offset_type got_offset = this->got_plt_->current_data_size();
814 // Every PLT entry needs a GOT entry which points back to the PLT
816 this->got_plt_->set_current_data_size(got_offset + 4);
818 // Every PLT entry needs a reloc.
819 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
820 elfcpp::R_386_IRELATIVE,
821 this->got_plt_, got_offset);
823 struct Local_ifunc li;
825 li.local_sym_index = local_sym_index;
826 li.got_offset = got_offset;
827 this->local_ifuncs_.push_back(li);
832 // Return where the TLS_DESC relocations should go, creating it if
833 // necessary. These follow the JUMP_SLOT relocations.
835 Output_data_plt_i386::Reloc_section*
836 Output_data_plt_i386::rel_tls_desc(Layout* layout)
838 if (this->tls_desc_rel_ == NULL)
840 this->tls_desc_rel_ = new Reloc_section(false);
841 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
842 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
843 ORDER_DYNAMIC_PLT_RELOCS, false);
844 gold_assert(this->tls_desc_rel_->output_section() ==
845 this->rel_->output_section());
847 return this->tls_desc_rel_;
850 // The first entry in the PLT for an executable.
852 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
854 0xff, 0x35, // pushl contents of memory address
855 0, 0, 0, 0, // replaced with address of .got + 4
856 0xff, 0x25, // jmp indirect
857 0, 0, 0, 0, // replaced with address of .got + 8
861 // The first entry in the PLT for a shared object.
863 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
865 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
866 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
870 // Subsequent entries in the PLT for an executable.
872 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
874 0xff, 0x25, // jmp indirect
875 0, 0, 0, 0, // replaced with address of symbol in .got
876 0x68, // pushl immediate
877 0, 0, 0, 0, // replaced with offset into relocation table
878 0xe9, // jmp relative
879 0, 0, 0, 0 // replaced with offset to start of .plt
882 // Subsequent entries in the PLT for a shared object.
884 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
886 0xff, 0xa3, // jmp *offset(%ebx)
887 0, 0, 0, 0, // replaced with offset of symbol in .got
888 0x68, // pushl immediate
889 0, 0, 0, 0, // replaced with offset into relocation table
890 0xe9, // jmp relative
891 0, 0, 0, 0 // replaced with offset to start of .plt
894 // Write out the PLT. This uses the hand-coded instructions above,
895 // and adjusts them as needed. This is all specified by the i386 ELF
896 // Processor Supplement.
899 Output_data_plt_i386::do_write(Output_file* of)
901 const off_t offset = this->offset();
902 const section_size_type oview_size =
903 convert_to_section_size_type(this->data_size());
904 unsigned char* const oview = of->get_output_view(offset, oview_size);
906 const off_t got_file_offset = this->got_plt_->offset();
907 const section_size_type got_size =
908 convert_to_section_size_type(this->got_plt_->data_size());
909 unsigned char* const got_view = of->get_output_view(got_file_offset,
912 unsigned char* pov = oview;
914 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
915 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
917 if (parameters->options().output_is_position_independent())
918 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
921 memcpy(pov, exec_first_plt_entry, plt_entry_size);
922 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
923 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
925 pov += plt_entry_size;
927 unsigned char* got_pov = got_view;
929 memset(got_pov, 0, 12);
932 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
934 unsigned int plt_offset = plt_entry_size;
935 unsigned int plt_rel_offset = 0;
936 unsigned int got_offset = 12;
937 const unsigned int count = this->count_;
938 for (unsigned int i = 0;
941 pov += plt_entry_size,
943 plt_offset += plt_entry_size,
944 plt_rel_offset += rel_size,
947 // Set and adjust the PLT entry itself.
949 if (parameters->options().output_is_position_independent())
951 memcpy(pov, dyn_plt_entry, plt_entry_size);
952 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
956 memcpy(pov, exec_plt_entry, plt_entry_size);
957 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
962 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
963 elfcpp::Swap<32, false>::writeval(pov + 12,
964 - (plt_offset + plt_entry_size));
966 // Set the entry in the GOT.
967 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
970 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
971 // the GOT to point to the actual symbol value, rather than point to
972 // the PLT entry. That will let the dynamic linker call the right
973 // function when resolving IRELATIVE relocations.
974 for (std::vector<Global_ifunc>::const_iterator p =
975 this->global_ifuncs_.begin();
976 p != this->global_ifuncs_.end();
979 const Sized_symbol<32>* ssym =
980 static_cast<const Sized_symbol<32>*>(p->sym);
981 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
985 for (std::vector<Local_ifunc>::const_iterator p =
986 this->local_ifuncs_.begin();
987 p != this->local_ifuncs_.end();
990 const Symbol_value<32>* psymval =
991 p->object->local_symbol(p->local_sym_index);
992 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
993 psymval->value(p->object, 0));
996 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
997 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
999 of->write_output_view(offset, oview_size, oview);
1000 of->write_output_view(got_file_offset, got_size, got_view);
1003 // Create the PLT section.
1006 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1008 if (this->plt_ == NULL)
1010 // Create the GOT sections first.
1011 this->got_section(symtab, layout);
1013 this->plt_ = new Output_data_plt_i386(symtab, layout, this->got_plt_);
1014 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1016 | elfcpp::SHF_EXECINSTR),
1017 this->plt_, ORDER_PLT, false);
1019 // Make the sh_info field of .rel.plt point to .plt.
1020 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1021 rel_plt_os->set_info_section(this->plt_->output_section());
1025 // Create a PLT entry for a global symbol.
1028 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1030 if (gsym->has_plt_offset())
1032 if (this->plt_ == NULL)
1033 this->make_plt_section(symtab, layout);
1034 this->plt_->add_entry(gsym);
1037 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1040 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1041 Sized_relobj<32, false>* relobj,
1042 unsigned int local_sym_index)
1044 if (relobj->local_has_plt_offset(local_sym_index))
1046 if (this->plt_ == NULL)
1047 this->make_plt_section(symtab, layout);
1048 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1050 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1053 // Return the number of entries in the PLT.
1056 Target_i386::plt_entry_count() const
1058 if (this->plt_ == NULL)
1060 return this->plt_->entry_count();
1063 // Return the offset of the first non-reserved PLT entry.
1066 Target_i386::first_plt_entry_offset() const
1068 return Output_data_plt_i386::first_plt_entry_offset();
1071 // Return the size of each PLT entry.
1074 Target_i386::plt_entry_size() const
1076 return Output_data_plt_i386::get_plt_entry_size();
1079 // Get the section to use for TLS_DESC relocations.
1081 Target_i386::Reloc_section*
1082 Target_i386::rel_tls_desc_section(Layout* layout) const
1084 return this->plt_section()->rel_tls_desc(layout);
1087 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1090 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1092 if (this->tls_base_symbol_defined_)
1095 Output_segment* tls_segment = layout->tls_segment();
1096 if (tls_segment != NULL)
1098 bool is_exec = parameters->options().output_is_executable();
1099 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1100 Symbol_table::PREDEFINED,
1104 elfcpp::STV_HIDDEN, 0,
1106 ? Symbol::SEGMENT_END
1107 : Symbol::SEGMENT_START),
1110 this->tls_base_symbol_defined_ = true;
1113 // Create a GOT entry for the TLS module index.
1116 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1117 Sized_relobj<32, false>* object)
1119 if (this->got_mod_index_offset_ == -1U)
1121 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1122 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1123 Output_data_got<32, false>* got = this->got_section(symtab, layout);
1124 unsigned int got_offset = got->add_constant(0);
1125 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1127 got->add_constant(0);
1128 this->got_mod_index_offset_ = got_offset;
1130 return this->got_mod_index_offset_;
1133 // Optimize the TLS relocation type based on what we know about the
1134 // symbol. IS_FINAL is true if the final address of this symbol is
1135 // known at link time.
1137 tls::Tls_optimization
1138 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1140 // If we are generating a shared library, then we can't do anything
1142 if (parameters->options().shared())
1143 return tls::TLSOPT_NONE;
1147 case elfcpp::R_386_TLS_GD:
1148 case elfcpp::R_386_TLS_GOTDESC:
1149 case elfcpp::R_386_TLS_DESC_CALL:
1150 // These are General-Dynamic which permits fully general TLS
1151 // access. Since we know that we are generating an executable,
1152 // we can convert this to Initial-Exec. If we also know that
1153 // this is a local symbol, we can further switch to Local-Exec.
1155 return tls::TLSOPT_TO_LE;
1156 return tls::TLSOPT_TO_IE;
1158 case elfcpp::R_386_TLS_LDM:
1159 // This is Local-Dynamic, which refers to a local symbol in the
1160 // dynamic TLS block. Since we know that we generating an
1161 // executable, we can switch to Local-Exec.
1162 return tls::TLSOPT_TO_LE;
1164 case elfcpp::R_386_TLS_LDO_32:
1165 // Another type of Local-Dynamic relocation.
1166 return tls::TLSOPT_TO_LE;
1168 case elfcpp::R_386_TLS_IE:
1169 case elfcpp::R_386_TLS_GOTIE:
1170 case elfcpp::R_386_TLS_IE_32:
1171 // These are Initial-Exec relocs which get the thread offset
1172 // from the GOT. If we know that we are linking against the
1173 // local symbol, we can switch to Local-Exec, which links the
1174 // thread offset into the instruction.
1176 return tls::TLSOPT_TO_LE;
1177 return tls::TLSOPT_NONE;
1179 case elfcpp::R_386_TLS_LE:
1180 case elfcpp::R_386_TLS_LE_32:
1181 // When we already have Local-Exec, there is nothing further we
1183 return tls::TLSOPT_NONE;
1190 // Report an unsupported relocation against a local symbol.
1193 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
1194 unsigned int r_type)
1196 gold_error(_("%s: unsupported reloc %u against local symbol"),
1197 object->name().c_str(), r_type);
1200 // Return whether we need to make a PLT entry for a relocation of a
1201 // given type against a STT_GNU_IFUNC symbol.
1204 Target_i386::Scan::reloc_needs_plt_for_ifunc(Sized_relobj<32, false>* object,
1205 unsigned int r_type)
1209 case elfcpp::R_386_NONE:
1210 case elfcpp::R_386_GNU_VTINHERIT:
1211 case elfcpp::R_386_GNU_VTENTRY:
1214 case elfcpp::R_386_32:
1215 case elfcpp::R_386_16:
1216 case elfcpp::R_386_8:
1217 case elfcpp::R_386_PC32:
1218 case elfcpp::R_386_PC16:
1219 case elfcpp::R_386_PC8:
1220 case elfcpp::R_386_PLT32:
1221 case elfcpp::R_386_GOTOFF:
1222 case elfcpp::R_386_GOTPC:
1223 case elfcpp::R_386_GOT32:
1226 case elfcpp::R_386_COPY:
1227 case elfcpp::R_386_GLOB_DAT:
1228 case elfcpp::R_386_JUMP_SLOT:
1229 case elfcpp::R_386_RELATIVE:
1230 case elfcpp::R_386_IRELATIVE:
1231 case elfcpp::R_386_TLS_TPOFF:
1232 case elfcpp::R_386_TLS_DTPMOD32:
1233 case elfcpp::R_386_TLS_DTPOFF32:
1234 case elfcpp::R_386_TLS_TPOFF32:
1235 case elfcpp::R_386_TLS_DESC:
1236 // We will give an error later.
1239 case elfcpp::R_386_TLS_GD:
1240 case elfcpp::R_386_TLS_GOTDESC:
1241 case elfcpp::R_386_TLS_DESC_CALL:
1242 case elfcpp::R_386_TLS_LDM:
1243 case elfcpp::R_386_TLS_LDO_32:
1244 case elfcpp::R_386_TLS_IE:
1245 case elfcpp::R_386_TLS_IE_32:
1246 case elfcpp::R_386_TLS_GOTIE:
1247 case elfcpp::R_386_TLS_LE:
1248 case elfcpp::R_386_TLS_LE_32:
1249 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1250 object->name().c_str(), r_type);
1253 case elfcpp::R_386_32PLT:
1254 case elfcpp::R_386_TLS_GD_32:
1255 case elfcpp::R_386_TLS_GD_PUSH:
1256 case elfcpp::R_386_TLS_GD_CALL:
1257 case elfcpp::R_386_TLS_GD_POP:
1258 case elfcpp::R_386_TLS_LDM_32:
1259 case elfcpp::R_386_TLS_LDM_PUSH:
1260 case elfcpp::R_386_TLS_LDM_CALL:
1261 case elfcpp::R_386_TLS_LDM_POP:
1262 case elfcpp::R_386_USED_BY_INTEL_200:
1264 // We will give an error later.
1269 // Scan a relocation for a local symbol.
1272 Target_i386::Scan::local(Symbol_table* symtab,
1274 Target_i386* target,
1275 Sized_relobj<32, false>* object,
1276 unsigned int data_shndx,
1277 Output_section* output_section,
1278 const elfcpp::Rel<32, false>& reloc,
1279 unsigned int r_type,
1280 const elfcpp::Sym<32, false>& lsym)
1282 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1283 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1284 && this->reloc_needs_plt_for_ifunc(object, r_type))
1286 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1287 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1292 case elfcpp::R_386_NONE:
1293 case elfcpp::R_386_GNU_VTINHERIT:
1294 case elfcpp::R_386_GNU_VTENTRY:
1297 case elfcpp::R_386_32:
1298 // If building a shared library (or a position-independent
1299 // executable), we need to create a dynamic relocation for
1300 // this location. The relocation applied at link time will
1301 // apply the link-time value, so we flag the location with
1302 // an R_386_RELATIVE relocation so the dynamic loader can
1303 // relocate it easily.
1304 if (parameters->options().output_is_position_independent())
1306 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1307 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1308 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1309 output_section, data_shndx,
1310 reloc.get_r_offset());
1314 case elfcpp::R_386_16:
1315 case elfcpp::R_386_8:
1316 // If building a shared library (or a position-independent
1317 // executable), we need to create a dynamic relocation for
1318 // this location. Because the addend needs to remain in the
1319 // data section, we need to be careful not to apply this
1320 // relocation statically.
1321 if (parameters->options().output_is_position_independent())
1323 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1324 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1325 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1326 rel_dyn->add_local(object, r_sym, r_type, output_section,
1327 data_shndx, reloc.get_r_offset());
1330 gold_assert(lsym.get_st_value() == 0);
1331 unsigned int shndx = lsym.get_st_shndx();
1333 shndx = object->adjust_sym_shndx(r_sym, shndx,
1336 object->error(_("section symbol %u has bad shndx %u"),
1339 rel_dyn->add_local_section(object, shndx,
1340 r_type, output_section,
1341 data_shndx, reloc.get_r_offset());
1346 case elfcpp::R_386_PC32:
1347 case elfcpp::R_386_PC16:
1348 case elfcpp::R_386_PC8:
1351 case elfcpp::R_386_PLT32:
1352 // Since we know this is a local symbol, we can handle this as a
1356 case elfcpp::R_386_GOTOFF:
1357 case elfcpp::R_386_GOTPC:
1358 // We need a GOT section.
1359 target->got_section(symtab, layout);
1362 case elfcpp::R_386_GOT32:
1364 // The symbol requires a GOT entry.
1365 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1366 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1368 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1369 // lets function pointers compare correctly with shared
1370 // libraries. Otherwise we would need an IRELATIVE reloc.
1372 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1373 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1375 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1378 // If we are generating a shared object, we need to add a
1379 // dynamic RELATIVE relocation for this symbol's GOT entry.
1380 if (parameters->options().output_is_position_independent())
1382 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1383 unsigned int got_offset =
1384 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1385 rel_dyn->add_local_relative(object, r_sym,
1386 elfcpp::R_386_RELATIVE,
1393 // These are relocations which should only be seen by the
1394 // dynamic linker, and should never be seen here.
1395 case elfcpp::R_386_COPY:
1396 case elfcpp::R_386_GLOB_DAT:
1397 case elfcpp::R_386_JUMP_SLOT:
1398 case elfcpp::R_386_RELATIVE:
1399 case elfcpp::R_386_IRELATIVE:
1400 case elfcpp::R_386_TLS_TPOFF:
1401 case elfcpp::R_386_TLS_DTPMOD32:
1402 case elfcpp::R_386_TLS_DTPOFF32:
1403 case elfcpp::R_386_TLS_TPOFF32:
1404 case elfcpp::R_386_TLS_DESC:
1405 gold_error(_("%s: unexpected reloc %u in object file"),
1406 object->name().c_str(), r_type);
1409 // These are initial TLS relocs, which are expected when
1411 case elfcpp::R_386_TLS_GD: // Global-dynamic
1412 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1413 case elfcpp::R_386_TLS_DESC_CALL:
1414 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1415 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1416 case elfcpp::R_386_TLS_IE: // Initial-exec
1417 case elfcpp::R_386_TLS_IE_32:
1418 case elfcpp::R_386_TLS_GOTIE:
1419 case elfcpp::R_386_TLS_LE: // Local-exec
1420 case elfcpp::R_386_TLS_LE_32:
1422 bool output_is_shared = parameters->options().shared();
1423 const tls::Tls_optimization optimized_type
1424 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1427 case elfcpp::R_386_TLS_GD: // Global-dynamic
1428 if (optimized_type == tls::TLSOPT_NONE)
1430 // Create a pair of GOT entries for the module index and
1431 // dtv-relative offset.
1432 Output_data_got<32, false>* got
1433 = target->got_section(symtab, layout);
1434 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1435 unsigned int shndx = lsym.get_st_shndx();
1437 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1439 object->error(_("local symbol %u has bad shndx %u"),
1442 got->add_local_pair_with_rel(object, r_sym, shndx,
1444 target->rel_dyn_section(layout),
1445 elfcpp::R_386_TLS_DTPMOD32, 0);
1447 else if (optimized_type != tls::TLSOPT_TO_LE)
1448 unsupported_reloc_local(object, r_type);
1451 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1452 target->define_tls_base_symbol(symtab, layout);
1453 if (optimized_type == tls::TLSOPT_NONE)
1455 // Create a double GOT entry with an R_386_TLS_DESC
1456 // reloc. The R_386_TLS_DESC reloc is resolved
1457 // lazily, so the GOT entry needs to be in an area in
1458 // .got.plt, not .got. Call got_section to make sure
1459 // the section has been created.
1460 target->got_section(symtab, layout);
1461 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1462 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1463 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1465 unsigned int got_offset = got->add_constant(0);
1466 // The local symbol value is stored in the second
1468 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1469 // That set the GOT offset of the local symbol to
1470 // point to the second entry, but we want it to
1471 // point to the first.
1472 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1474 Reloc_section* rt = target->rel_tls_desc_section(layout);
1475 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1478 else if (optimized_type != tls::TLSOPT_TO_LE)
1479 unsupported_reloc_local(object, r_type);
1482 case elfcpp::R_386_TLS_DESC_CALL:
1485 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1486 if (optimized_type == tls::TLSOPT_NONE)
1488 // Create a GOT entry for the module index.
1489 target->got_mod_index_entry(symtab, layout, object);
1491 else if (optimized_type != tls::TLSOPT_TO_LE)
1492 unsupported_reloc_local(object, r_type);
1495 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1498 case elfcpp::R_386_TLS_IE: // Initial-exec
1499 case elfcpp::R_386_TLS_IE_32:
1500 case elfcpp::R_386_TLS_GOTIE:
1501 layout->set_has_static_tls();
1502 if (optimized_type == tls::TLSOPT_NONE)
1504 // For the R_386_TLS_IE relocation, we need to create a
1505 // dynamic relocation when building a shared library.
1506 if (r_type == elfcpp::R_386_TLS_IE
1507 && parameters->options().shared())
1509 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1511 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1512 rel_dyn->add_local_relative(object, r_sym,
1513 elfcpp::R_386_RELATIVE,
1514 output_section, data_shndx,
1515 reloc.get_r_offset());
1517 // Create a GOT entry for the tp-relative offset.
1518 Output_data_got<32, false>* got
1519 = target->got_section(symtab, layout);
1520 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1521 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1522 ? elfcpp::R_386_TLS_TPOFF32
1523 : elfcpp::R_386_TLS_TPOFF);
1524 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1525 ? GOT_TYPE_TLS_OFFSET
1526 : GOT_TYPE_TLS_NOFFSET);
1527 got->add_local_with_rel(object, r_sym, got_type,
1528 target->rel_dyn_section(layout),
1531 else if (optimized_type != tls::TLSOPT_TO_LE)
1532 unsupported_reloc_local(object, r_type);
1535 case elfcpp::R_386_TLS_LE: // Local-exec
1536 case elfcpp::R_386_TLS_LE_32:
1537 layout->set_has_static_tls();
1538 if (output_is_shared)
1540 // We need to create a dynamic relocation.
1541 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1542 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1543 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1544 ? elfcpp::R_386_TLS_TPOFF32
1545 : elfcpp::R_386_TLS_TPOFF);
1546 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1547 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1548 data_shndx, reloc.get_r_offset());
1558 case elfcpp::R_386_32PLT:
1559 case elfcpp::R_386_TLS_GD_32:
1560 case elfcpp::R_386_TLS_GD_PUSH:
1561 case elfcpp::R_386_TLS_GD_CALL:
1562 case elfcpp::R_386_TLS_GD_POP:
1563 case elfcpp::R_386_TLS_LDM_32:
1564 case elfcpp::R_386_TLS_LDM_PUSH:
1565 case elfcpp::R_386_TLS_LDM_CALL:
1566 case elfcpp::R_386_TLS_LDM_POP:
1567 case elfcpp::R_386_USED_BY_INTEL_200:
1569 unsupported_reloc_local(object, r_type);
1574 // Report an unsupported relocation against a global symbol.
1577 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1578 unsigned int r_type,
1581 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1582 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1586 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
1590 case elfcpp::R_386_32:
1591 case elfcpp::R_386_16:
1592 case elfcpp::R_386_8:
1593 case elfcpp::R_386_GOTOFF:
1594 case elfcpp::R_386_GOT32:
1605 Target_i386::Scan::local_reloc_may_be_function_pointer(
1609 Sized_relobj<32, false>* ,
1612 const elfcpp::Rel<32, false>& ,
1613 unsigned int r_type,
1614 const elfcpp::Sym<32, false>&)
1616 return possible_function_pointer_reloc(r_type);
1620 Target_i386::Scan::global_reloc_may_be_function_pointer(
1624 Sized_relobj<32, false>* ,
1627 const elfcpp::Rel<32, false>& ,
1628 unsigned int r_type,
1631 return possible_function_pointer_reloc(r_type);
1634 // Scan a relocation for a global symbol.
1637 Target_i386::Scan::global(Symbol_table* symtab,
1639 Target_i386* target,
1640 Sized_relobj<32, false>* object,
1641 unsigned int data_shndx,
1642 Output_section* output_section,
1643 const elfcpp::Rel<32, false>& reloc,
1644 unsigned int r_type,
1647 // A STT_GNU_IFUNC symbol may require a PLT entry.
1648 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1649 && this->reloc_needs_plt_for_ifunc(object, r_type))
1650 target->make_plt_entry(symtab, layout, gsym);
1654 case elfcpp::R_386_NONE:
1655 case elfcpp::R_386_GNU_VTINHERIT:
1656 case elfcpp::R_386_GNU_VTENTRY:
1659 case elfcpp::R_386_32:
1660 case elfcpp::R_386_16:
1661 case elfcpp::R_386_8:
1663 // Make a PLT entry if necessary.
1664 if (gsym->needs_plt_entry())
1666 target->make_plt_entry(symtab, layout, gsym);
1667 // Since this is not a PC-relative relocation, we may be
1668 // taking the address of a function. In that case we need to
1669 // set the entry in the dynamic symbol table to the address of
1671 if (gsym->is_from_dynobj() && !parameters->options().shared())
1672 gsym->set_needs_dynsym_value();
1674 // Make a dynamic relocation if necessary.
1675 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1677 if (gsym->may_need_copy_reloc())
1679 target->copy_reloc(symtab, layout, object,
1680 data_shndx, output_section, gsym, reloc);
1682 else if (r_type == elfcpp::R_386_32
1683 && gsym->type() == elfcpp::STT_GNU_IFUNC
1684 && gsym->can_use_relative_reloc(false)
1685 && !gsym->is_from_dynobj()
1686 && !gsym->is_undefined()
1687 && !gsym->is_preemptible())
1689 // Use an IRELATIVE reloc for a locally defined
1690 // STT_GNU_IFUNC symbol. This makes a function
1691 // address in a PIE executable match the address in a
1692 // shared library that it links against.
1693 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1694 rel_dyn->add_symbolless_global_addend(gsym,
1695 elfcpp::R_386_IRELATIVE,
1698 reloc.get_r_offset());
1700 else if (r_type == elfcpp::R_386_32
1701 && gsym->can_use_relative_reloc(false))
1703 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1704 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1705 output_section, object,
1706 data_shndx, reloc.get_r_offset());
1710 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1711 rel_dyn->add_global(gsym, r_type, output_section, object,
1712 data_shndx, reloc.get_r_offset());
1718 case elfcpp::R_386_PC32:
1719 case elfcpp::R_386_PC16:
1720 case elfcpp::R_386_PC8:
1722 // Make a PLT entry if necessary.
1723 if (gsym->needs_plt_entry())
1725 // These relocations are used for function calls only in
1726 // non-PIC code. For a 32-bit relocation in a shared library,
1727 // we'll need a text relocation anyway, so we can skip the
1728 // PLT entry and let the dynamic linker bind the call directly
1729 // to the target. For smaller relocations, we should use a
1730 // PLT entry to ensure that the call can reach.
1731 if (!parameters->options().shared()
1732 || r_type != elfcpp::R_386_PC32)
1733 target->make_plt_entry(symtab, layout, gsym);
1735 // Make a dynamic relocation if necessary.
1736 int flags = Symbol::NON_PIC_REF;
1737 if (gsym->is_func())
1738 flags |= Symbol::FUNCTION_CALL;
1739 if (gsym->needs_dynamic_reloc(flags))
1741 if (gsym->may_need_copy_reloc())
1743 target->copy_reloc(symtab, layout, object,
1744 data_shndx, output_section, gsym, reloc);
1748 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1749 rel_dyn->add_global(gsym, r_type, output_section, object,
1750 data_shndx, reloc.get_r_offset());
1756 case elfcpp::R_386_GOT32:
1758 // The symbol requires a GOT entry.
1759 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1760 if (gsym->final_value_is_known())
1762 // For a STT_GNU_IFUNC symbol we want the PLT address.
1763 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1764 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1766 got->add_global(gsym, GOT_TYPE_STANDARD);
1770 // If this symbol is not fully resolved, we need to add a
1771 // GOT entry with a dynamic relocation.
1772 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1773 if (gsym->is_from_dynobj()
1774 || gsym->is_undefined()
1775 || gsym->is_preemptible()
1776 || (gsym->type() == elfcpp::STT_GNU_IFUNC
1777 && parameters->options().output_is_position_independent()))
1778 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1779 rel_dyn, elfcpp::R_386_GLOB_DAT);
1782 // For a STT_GNU_IFUNC symbol we want to write the PLT
1783 // offset into the GOT, so that function pointer
1784 // comparisons work correctly.
1786 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
1787 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
1790 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1791 // Tell the dynamic linker to use the PLT address
1792 // when resolving relocations.
1793 if (gsym->is_from_dynobj()
1794 && !parameters->options().shared())
1795 gsym->set_needs_dynsym_value();
1799 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
1800 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1808 case elfcpp::R_386_PLT32:
1809 // If the symbol is fully resolved, this is just a PC32 reloc.
1810 // Otherwise we need a PLT entry.
1811 if (gsym->final_value_is_known())
1813 // If building a shared library, we can also skip the PLT entry
1814 // if the symbol is defined in the output file and is protected
1816 if (gsym->is_defined()
1817 && !gsym->is_from_dynobj()
1818 && !gsym->is_preemptible())
1820 target->make_plt_entry(symtab, layout, gsym);
1823 case elfcpp::R_386_GOTOFF:
1824 case elfcpp::R_386_GOTPC:
1825 // We need a GOT section.
1826 target->got_section(symtab, layout);
1829 // These are relocations which should only be seen by the
1830 // dynamic linker, and should never be seen here.
1831 case elfcpp::R_386_COPY:
1832 case elfcpp::R_386_GLOB_DAT:
1833 case elfcpp::R_386_JUMP_SLOT:
1834 case elfcpp::R_386_RELATIVE:
1835 case elfcpp::R_386_IRELATIVE:
1836 case elfcpp::R_386_TLS_TPOFF:
1837 case elfcpp::R_386_TLS_DTPMOD32:
1838 case elfcpp::R_386_TLS_DTPOFF32:
1839 case elfcpp::R_386_TLS_TPOFF32:
1840 case elfcpp::R_386_TLS_DESC:
1841 gold_error(_("%s: unexpected reloc %u in object file"),
1842 object->name().c_str(), r_type);
1845 // These are initial tls relocs, which are expected when
1847 case elfcpp::R_386_TLS_GD: // Global-dynamic
1848 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1849 case elfcpp::R_386_TLS_DESC_CALL:
1850 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1851 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1852 case elfcpp::R_386_TLS_IE: // Initial-exec
1853 case elfcpp::R_386_TLS_IE_32:
1854 case elfcpp::R_386_TLS_GOTIE:
1855 case elfcpp::R_386_TLS_LE: // Local-exec
1856 case elfcpp::R_386_TLS_LE_32:
1858 const bool is_final = gsym->final_value_is_known();
1859 const tls::Tls_optimization optimized_type
1860 = Target_i386::optimize_tls_reloc(is_final, r_type);
1863 case elfcpp::R_386_TLS_GD: // Global-dynamic
1864 if (optimized_type == tls::TLSOPT_NONE)
1866 // Create a pair of GOT entries for the module index and
1867 // dtv-relative offset.
1868 Output_data_got<32, false>* got
1869 = target->got_section(symtab, layout);
1870 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1871 target->rel_dyn_section(layout),
1872 elfcpp::R_386_TLS_DTPMOD32,
1873 elfcpp::R_386_TLS_DTPOFF32);
1875 else if (optimized_type == tls::TLSOPT_TO_IE)
1877 // Create a GOT entry for the tp-relative offset.
1878 Output_data_got<32, false>* got
1879 = target->got_section(symtab, layout);
1880 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1881 target->rel_dyn_section(layout),
1882 elfcpp::R_386_TLS_TPOFF);
1884 else if (optimized_type != tls::TLSOPT_TO_LE)
1885 unsupported_reloc_global(object, r_type, gsym);
1888 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1889 target->define_tls_base_symbol(symtab, layout);
1890 if (optimized_type == tls::TLSOPT_NONE)
1892 // Create a double GOT entry with an R_386_TLS_DESC
1893 // reloc. The R_386_TLS_DESC reloc is resolved
1894 // lazily, so the GOT entry needs to be in an area in
1895 // .got.plt, not .got. Call got_section to make sure
1896 // the section has been created.
1897 target->got_section(symtab, layout);
1898 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1899 Reloc_section* rt = target->rel_tls_desc_section(layout);
1900 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
1901 elfcpp::R_386_TLS_DESC, 0);
1903 else if (optimized_type == tls::TLSOPT_TO_IE)
1905 // Create a GOT entry for the tp-relative offset.
1906 Output_data_got<32, false>* got
1907 = target->got_section(symtab, layout);
1908 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1909 target->rel_dyn_section(layout),
1910 elfcpp::R_386_TLS_TPOFF);
1912 else if (optimized_type != tls::TLSOPT_TO_LE)
1913 unsupported_reloc_global(object, r_type, gsym);
1916 case elfcpp::R_386_TLS_DESC_CALL:
1919 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1920 if (optimized_type == tls::TLSOPT_NONE)
1922 // Create a GOT entry for the module index.
1923 target->got_mod_index_entry(symtab, layout, object);
1925 else if (optimized_type != tls::TLSOPT_TO_LE)
1926 unsupported_reloc_global(object, r_type, gsym);
1929 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1932 case elfcpp::R_386_TLS_IE: // Initial-exec
1933 case elfcpp::R_386_TLS_IE_32:
1934 case elfcpp::R_386_TLS_GOTIE:
1935 layout->set_has_static_tls();
1936 if (optimized_type == tls::TLSOPT_NONE)
1938 // For the R_386_TLS_IE relocation, we need to create a
1939 // dynamic relocation when building a shared library.
1940 if (r_type == elfcpp::R_386_TLS_IE
1941 && parameters->options().shared())
1943 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1944 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1945 output_section, object,
1947 reloc.get_r_offset());
1949 // Create a GOT entry for the tp-relative offset.
1950 Output_data_got<32, false>* got
1951 = target->got_section(symtab, layout);
1952 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1953 ? elfcpp::R_386_TLS_TPOFF32
1954 : elfcpp::R_386_TLS_TPOFF);
1955 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1956 ? GOT_TYPE_TLS_OFFSET
1957 : GOT_TYPE_TLS_NOFFSET);
1958 got->add_global_with_rel(gsym, got_type,
1959 target->rel_dyn_section(layout),
1962 else if (optimized_type != tls::TLSOPT_TO_LE)
1963 unsupported_reloc_global(object, r_type, gsym);
1966 case elfcpp::R_386_TLS_LE: // Local-exec
1967 case elfcpp::R_386_TLS_LE_32:
1968 layout->set_has_static_tls();
1969 if (parameters->options().shared())
1971 // We need to create a dynamic relocation.
1972 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1973 ? elfcpp::R_386_TLS_TPOFF32
1974 : elfcpp::R_386_TLS_TPOFF);
1975 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1976 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1977 data_shndx, reloc.get_r_offset());
1987 case elfcpp::R_386_32PLT:
1988 case elfcpp::R_386_TLS_GD_32:
1989 case elfcpp::R_386_TLS_GD_PUSH:
1990 case elfcpp::R_386_TLS_GD_CALL:
1991 case elfcpp::R_386_TLS_GD_POP:
1992 case elfcpp::R_386_TLS_LDM_32:
1993 case elfcpp::R_386_TLS_LDM_PUSH:
1994 case elfcpp::R_386_TLS_LDM_CALL:
1995 case elfcpp::R_386_TLS_LDM_POP:
1996 case elfcpp::R_386_USED_BY_INTEL_200:
1998 unsupported_reloc_global(object, r_type, gsym);
2003 // Process relocations for gc.
2006 Target_i386::gc_process_relocs(Symbol_table* symtab,
2008 Sized_relobj<32, false>* object,
2009 unsigned int data_shndx,
2011 const unsigned char* prelocs,
2013 Output_section* output_section,
2014 bool needs_special_offset_handling,
2015 size_t local_symbol_count,
2016 const unsigned char* plocal_symbols)
2018 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2020 Target_i386::Relocatable_size_for_reloc>(
2029 needs_special_offset_handling,
2034 // Scan relocations for a section.
2037 Target_i386::scan_relocs(Symbol_table* symtab,
2039 Sized_relobj<32, false>* object,
2040 unsigned int data_shndx,
2041 unsigned int sh_type,
2042 const unsigned char* prelocs,
2044 Output_section* output_section,
2045 bool needs_special_offset_handling,
2046 size_t local_symbol_count,
2047 const unsigned char* plocal_symbols)
2049 if (sh_type == elfcpp::SHT_RELA)
2051 gold_error(_("%s: unsupported RELA reloc section"),
2052 object->name().c_str());
2056 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2066 needs_special_offset_handling,
2071 // Finalize the sections.
2074 Target_i386::do_finalize_sections(
2076 const Input_objects*,
2077 Symbol_table* symtab)
2079 const Reloc_section* rel_plt = (this->plt_ == NULL
2081 : this->plt_->rel_plt());
2082 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2083 this->rel_dyn_, true, false);
2085 // Emit any relocs we saved in an attempt to avoid generating COPY
2087 if (this->copy_relocs_.any_saved_relocs())
2088 this->copy_relocs_.emit(this->rel_dyn_section(layout));
2090 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2091 // the .got.plt section.
2092 Symbol* sym = this->global_offset_table_;
2095 uint32_t data_size = this->got_plt_->current_data_size();
2096 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2100 // Return whether a direct absolute static relocation needs to be applied.
2101 // In cases where Scan::local() or Scan::global() has created
2102 // a dynamic relocation other than R_386_RELATIVE, the addend
2103 // of the relocation is carried in the data, and we must not
2104 // apply the static relocation.
2107 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2110 Output_section* output_section)
2112 // If the output section is not allocated, then we didn't call
2113 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2115 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2118 // For local symbols, we will have created a non-RELATIVE dynamic
2119 // relocation only if (a) the output is position independent,
2120 // (b) the relocation is absolute (not pc- or segment-relative), and
2121 // (c) the relocation is not 32 bits wide.
2123 return !(parameters->options().output_is_position_independent()
2124 && (ref_flags & Symbol::ABSOLUTE_REF)
2127 // For global symbols, we use the same helper routines used in the
2128 // scan pass. If we did not create a dynamic relocation, or if we
2129 // created a RELATIVE dynamic relocation, we should apply the static
2131 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2132 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2133 && gsym->can_use_relative_reloc(ref_flags
2134 & Symbol::FUNCTION_CALL);
2135 return !has_dyn || is_rel;
2138 // Perform a relocation.
2141 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2142 Target_i386* target,
2143 Output_section* output_section,
2145 const elfcpp::Rel<32, false>& rel,
2146 unsigned int r_type,
2147 const Sized_symbol<32>* gsym,
2148 const Symbol_value<32>* psymval,
2149 unsigned char* view,
2150 elfcpp::Elf_types<32>::Elf_Addr address,
2151 section_size_type view_size)
2153 if (this->skip_call_tls_get_addr_)
2155 if ((r_type != elfcpp::R_386_PLT32
2156 && r_type != elfcpp::R_386_PC32)
2158 || strcmp(gsym->name(), "___tls_get_addr") != 0)
2159 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2160 _("missing expected TLS relocation"));
2163 this->skip_call_tls_get_addr_ = false;
2168 const Sized_relobj<32, false>* object = relinfo->object;
2170 // Pick the value to use for symbols defined in shared objects.
2171 Symbol_value<32> symval;
2173 && gsym->type() == elfcpp::STT_GNU_IFUNC
2174 && r_type == elfcpp::R_386_32
2175 && gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF)
2176 && gsym->can_use_relative_reloc(false)
2177 && !gsym->is_from_dynobj()
2178 && !gsym->is_undefined()
2179 && !gsym->is_preemptible())
2181 // In this case we are generating a R_386_IRELATIVE reloc. We
2182 // want to use the real value of the symbol, not the PLT offset.
2184 else if (gsym != NULL
2185 && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
2186 || r_type == elfcpp::R_386_PC16
2187 || r_type == elfcpp::R_386_PC32))
2189 symval.set_output_value(target->plt_section()->address()
2190 + gsym->plt_offset());
2193 else if (gsym == NULL && psymval->is_ifunc_symbol())
2195 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2196 if (object->local_has_plt_offset(r_sym))
2198 symval.set_output_value(target->plt_section()->address()
2199 + object->local_plt_offset(r_sym));
2204 // Get the GOT offset if needed.
2205 // The GOT pointer points to the end of the GOT section.
2206 // We need to subtract the size of the GOT section to get
2207 // the actual offset to use in the relocation.
2208 bool have_got_offset = false;
2209 unsigned int got_offset = 0;
2212 case elfcpp::R_386_GOT32:
2215 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2216 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2217 - target->got_size());
2221 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2222 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2223 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2224 - target->got_size());
2226 have_got_offset = true;
2235 case elfcpp::R_386_NONE:
2236 case elfcpp::R_386_GNU_VTINHERIT:
2237 case elfcpp::R_386_GNU_VTENTRY:
2240 case elfcpp::R_386_32:
2241 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2243 Relocate_functions<32, false>::rel32(view, object, psymval);
2246 case elfcpp::R_386_PC32:
2248 int ref_flags = Symbol::NON_PIC_REF;
2249 if (gsym != NULL && gsym->is_func())
2250 ref_flags |= Symbol::FUNCTION_CALL;
2251 if (should_apply_static_reloc(gsym, ref_flags, true, output_section))
2252 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2256 case elfcpp::R_386_16:
2257 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2259 Relocate_functions<32, false>::rel16(view, object, psymval);
2262 case elfcpp::R_386_PC16:
2264 int ref_flags = Symbol::NON_PIC_REF;
2265 if (gsym != NULL && gsym->is_func())
2266 ref_flags |= Symbol::FUNCTION_CALL;
2267 if (should_apply_static_reloc(gsym, ref_flags, false, output_section))
2268 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2272 case elfcpp::R_386_8:
2273 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2275 Relocate_functions<32, false>::rel8(view, object, psymval);
2278 case elfcpp::R_386_PC8:
2280 int ref_flags = Symbol::NON_PIC_REF;
2281 if (gsym != NULL && gsym->is_func())
2282 ref_flags |= Symbol::FUNCTION_CALL;
2283 if (should_apply_static_reloc(gsym, ref_flags, false,
2285 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2289 case elfcpp::R_386_PLT32:
2290 gold_assert(gsym == NULL
2291 || gsym->has_plt_offset()
2292 || gsym->final_value_is_known()
2293 || (gsym->is_defined()
2294 && !gsym->is_from_dynobj()
2295 && !gsym->is_preemptible()));
2296 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2299 case elfcpp::R_386_GOT32:
2300 gold_assert(have_got_offset);
2301 Relocate_functions<32, false>::rel32(view, got_offset);
2304 case elfcpp::R_386_GOTOFF:
2306 elfcpp::Elf_types<32>::Elf_Addr value;
2307 value = (psymval->value(object, 0)
2308 - target->got_plt_section()->address());
2309 Relocate_functions<32, false>::rel32(view, value);
2313 case elfcpp::R_386_GOTPC:
2315 elfcpp::Elf_types<32>::Elf_Addr value;
2316 value = target->got_plt_section()->address();
2317 Relocate_functions<32, false>::pcrel32(view, value, address);
2321 case elfcpp::R_386_COPY:
2322 case elfcpp::R_386_GLOB_DAT:
2323 case elfcpp::R_386_JUMP_SLOT:
2324 case elfcpp::R_386_RELATIVE:
2325 case elfcpp::R_386_IRELATIVE:
2326 // These are outstanding tls relocs, which are unexpected when
2328 case elfcpp::R_386_TLS_TPOFF:
2329 case elfcpp::R_386_TLS_DTPMOD32:
2330 case elfcpp::R_386_TLS_DTPOFF32:
2331 case elfcpp::R_386_TLS_TPOFF32:
2332 case elfcpp::R_386_TLS_DESC:
2333 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2334 _("unexpected reloc %u in object file"),
2338 // These are initial tls relocs, which are expected when
2340 case elfcpp::R_386_TLS_GD: // Global-dynamic
2341 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2342 case elfcpp::R_386_TLS_DESC_CALL:
2343 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2344 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2345 case elfcpp::R_386_TLS_IE: // Initial-exec
2346 case elfcpp::R_386_TLS_IE_32:
2347 case elfcpp::R_386_TLS_GOTIE:
2348 case elfcpp::R_386_TLS_LE: // Local-exec
2349 case elfcpp::R_386_TLS_LE_32:
2350 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2351 view, address, view_size);
2354 case elfcpp::R_386_32PLT:
2355 case elfcpp::R_386_TLS_GD_32:
2356 case elfcpp::R_386_TLS_GD_PUSH:
2357 case elfcpp::R_386_TLS_GD_CALL:
2358 case elfcpp::R_386_TLS_GD_POP:
2359 case elfcpp::R_386_TLS_LDM_32:
2360 case elfcpp::R_386_TLS_LDM_PUSH:
2361 case elfcpp::R_386_TLS_LDM_CALL:
2362 case elfcpp::R_386_TLS_LDM_POP:
2363 case elfcpp::R_386_USED_BY_INTEL_200:
2365 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2366 _("unsupported reloc %u"),
2374 // Perform a TLS relocation.
2377 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
2378 Target_i386* target,
2380 const elfcpp::Rel<32, false>& rel,
2381 unsigned int r_type,
2382 const Sized_symbol<32>* gsym,
2383 const Symbol_value<32>* psymval,
2384 unsigned char* view,
2385 elfcpp::Elf_types<32>::Elf_Addr,
2386 section_size_type view_size)
2388 Output_segment* tls_segment = relinfo->layout->tls_segment();
2390 const Sized_relobj<32, false>* object = relinfo->object;
2392 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
2394 const bool is_final = (gsym == NULL
2395 ? !parameters->options().shared()
2396 : gsym->final_value_is_known());
2397 const tls::Tls_optimization optimized_type
2398 = Target_i386::optimize_tls_reloc(is_final, r_type);
2401 case elfcpp::R_386_TLS_GD: // Global-dynamic
2402 if (optimized_type == tls::TLSOPT_TO_LE)
2404 gold_assert(tls_segment != NULL);
2405 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2406 rel, r_type, value, view,
2412 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2413 ? GOT_TYPE_TLS_NOFFSET
2414 : GOT_TYPE_TLS_PAIR);
2415 unsigned int got_offset;
2418 gold_assert(gsym->has_got_offset(got_type));
2419 got_offset = gsym->got_offset(got_type) - target->got_size();
2423 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2424 gold_assert(object->local_has_got_offset(r_sym, got_type));
2425 got_offset = (object->local_got_offset(r_sym, got_type)
2426 - target->got_size());
2428 if (optimized_type == tls::TLSOPT_TO_IE)
2430 gold_assert(tls_segment != NULL);
2431 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2432 got_offset, view, view_size);
2435 else if (optimized_type == tls::TLSOPT_NONE)
2437 // Relocate the field with the offset of the pair of GOT
2439 Relocate_functions<32, false>::rel32(view, got_offset);
2443 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2444 _("unsupported reloc %u"),
2448 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2449 case elfcpp::R_386_TLS_DESC_CALL:
2450 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2451 if (optimized_type == tls::TLSOPT_TO_LE)
2453 gold_assert(tls_segment != NULL);
2454 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2455 rel, r_type, value, view,
2461 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2462 ? GOT_TYPE_TLS_NOFFSET
2463 : GOT_TYPE_TLS_DESC);
2464 unsigned int got_offset = 0;
2465 if (r_type == elfcpp::R_386_TLS_GOTDESC
2466 && optimized_type == tls::TLSOPT_NONE)
2468 // We created GOT entries in the .got.tlsdesc portion of
2469 // the .got.plt section, but the offset stored in the
2470 // symbol is the offset within .got.tlsdesc.
2471 got_offset = (target->got_size()
2472 + target->got_plt_section()->data_size());
2476 gold_assert(gsym->has_got_offset(got_type));
2477 got_offset += gsym->got_offset(got_type) - target->got_size();
2481 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2482 gold_assert(object->local_has_got_offset(r_sym, got_type));
2483 got_offset += (object->local_got_offset(r_sym, got_type)
2484 - target->got_size());
2486 if (optimized_type == tls::TLSOPT_TO_IE)
2488 gold_assert(tls_segment != NULL);
2489 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2490 got_offset, view, view_size);
2493 else if (optimized_type == tls::TLSOPT_NONE)
2495 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2497 // Relocate the field with the offset of the pair of GOT
2499 Relocate_functions<32, false>::rel32(view, got_offset);
2504 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2505 _("unsupported reloc %u"),
2509 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2510 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2512 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2513 _("both SUN and GNU model "
2514 "TLS relocations"));
2517 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2518 if (optimized_type == tls::TLSOPT_TO_LE)
2520 gold_assert(tls_segment != NULL);
2521 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2522 value, view, view_size);
2525 else if (optimized_type == tls::TLSOPT_NONE)
2527 // Relocate the field with the offset of the GOT entry for
2528 // the module index.
2529 unsigned int got_offset;
2530 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2531 - target->got_size());
2532 Relocate_functions<32, false>::rel32(view, got_offset);
2535 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2536 _("unsupported reloc %u"),
2540 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2541 if (optimized_type == tls::TLSOPT_TO_LE)
2543 // This reloc can appear in debugging sections, in which
2544 // case we must not convert to local-exec. We decide what
2545 // to do based on whether the section is marked as
2546 // containing executable code. That is what the GNU linker
2548 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2549 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2551 gold_assert(tls_segment != NULL);
2552 value -= tls_segment->memsz();
2555 Relocate_functions<32, false>::rel32(view, value);
2558 case elfcpp::R_386_TLS_IE: // Initial-exec
2559 case elfcpp::R_386_TLS_GOTIE:
2560 case elfcpp::R_386_TLS_IE_32:
2561 if (optimized_type == tls::TLSOPT_TO_LE)
2563 gold_assert(tls_segment != NULL);
2564 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2565 rel, r_type, value, view,
2569 else if (optimized_type == tls::TLSOPT_NONE)
2571 // Relocate the field with the offset of the GOT entry for
2572 // the tp-relative offset of the symbol.
2573 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2574 ? GOT_TYPE_TLS_OFFSET
2575 : GOT_TYPE_TLS_NOFFSET);
2576 unsigned int got_offset;
2579 gold_assert(gsym->has_got_offset(got_type));
2580 got_offset = gsym->got_offset(got_type);
2584 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2585 gold_assert(object->local_has_got_offset(r_sym, got_type));
2586 got_offset = object->local_got_offset(r_sym, got_type);
2588 // For the R_386_TLS_IE relocation, we need to apply the
2589 // absolute address of the GOT entry.
2590 if (r_type == elfcpp::R_386_TLS_IE)
2591 got_offset += target->got_plt_section()->address();
2592 // All GOT offsets are relative to the end of the GOT.
2593 got_offset -= target->got_size();
2594 Relocate_functions<32, false>::rel32(view, got_offset);
2597 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2598 _("unsupported reloc %u"),
2602 case elfcpp::R_386_TLS_LE: // Local-exec
2603 // If we're creating a shared library, a dynamic relocation will
2604 // have been created for this location, so do not apply it now.
2605 if (!parameters->options().shared())
2607 gold_assert(tls_segment != NULL);
2608 value -= tls_segment->memsz();
2609 Relocate_functions<32, false>::rel32(view, value);
2613 case elfcpp::R_386_TLS_LE_32:
2614 // If we're creating a shared library, a dynamic relocation will
2615 // have been created for this location, so do not apply it now.
2616 if (!parameters->options().shared())
2618 gold_assert(tls_segment != NULL);
2619 value = tls_segment->memsz() - value;
2620 Relocate_functions<32, false>::rel32(view, value);
2626 // Do a relocation in which we convert a TLS General-Dynamic to a
2630 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2632 Output_segment* tls_segment,
2633 const elfcpp::Rel<32, false>& rel,
2635 elfcpp::Elf_types<32>::Elf_Addr value,
2636 unsigned char* view,
2637 section_size_type view_size)
2639 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2640 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2641 // leal foo(%reg),%eax; call ___tls_get_addr
2642 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2644 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2645 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2647 unsigned char op1 = view[-1];
2648 unsigned char op2 = view[-2];
2650 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2651 op2 == 0x8d || op2 == 0x04);
2652 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2658 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2659 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2660 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2661 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2662 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2666 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2667 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2668 if (rel.get_r_offset() + 9 < view_size
2671 // There is a trailing nop. Use the size byte subl.
2672 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2677 // Use the five byte subl.
2678 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2682 value = tls_segment->memsz() - value;
2683 Relocate_functions<32, false>::rel32(view + roff, value);
2685 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2687 this->skip_call_tls_get_addr_ = true;
2690 // Do a relocation in which we convert a TLS General-Dynamic to an
2694 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2697 const elfcpp::Rel<32, false>& rel,
2699 elfcpp::Elf_types<32>::Elf_Addr value,
2700 unsigned char* view,
2701 section_size_type view_size)
2703 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2704 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2706 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2707 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2709 unsigned char op1 = view[-1];
2710 unsigned char op2 = view[-2];
2712 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2713 op2 == 0x8d || op2 == 0x04);
2714 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2718 // FIXME: For now, support only the first (SIB) form.
2719 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2723 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2724 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2725 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2726 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2727 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2731 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2732 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2733 if (rel.get_r_offset() + 9 < view_size
2736 // FIXME: This is not the right instruction sequence.
2737 // There is a trailing nop. Use the size byte subl.
2738 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2743 // FIXME: This is not the right instruction sequence.
2744 // Use the five byte subl.
2745 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2749 Relocate_functions<32, false>::rel32(view + roff, value);
2751 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2753 this->skip_call_tls_get_addr_ = true;
2756 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2757 // General-Dynamic to a Local-Exec.
2760 Target_i386::Relocate::tls_desc_gd_to_le(
2761 const Relocate_info<32, false>* relinfo,
2763 Output_segment* tls_segment,
2764 const elfcpp::Rel<32, false>& rel,
2765 unsigned int r_type,
2766 elfcpp::Elf_types<32>::Elf_Addr value,
2767 unsigned char* view,
2768 section_size_type view_size)
2770 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2772 // leal foo@TLSDESC(%ebx), %eax
2773 // ==> leal foo@NTPOFF, %eax
2774 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2775 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2776 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2777 view[-2] == 0x8d && view[-1] == 0x83);
2779 value -= tls_segment->memsz();
2780 Relocate_functions<32, false>::rel32(view, value);
2784 // call *foo@TLSCALL(%eax)
2786 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2787 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2788 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2789 view[0] == 0xff && view[1] == 0x10);
2795 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2796 // General-Dynamic to an Initial-Exec.
2799 Target_i386::Relocate::tls_desc_gd_to_ie(
2800 const Relocate_info<32, false>* relinfo,
2803 const elfcpp::Rel<32, false>& rel,
2804 unsigned int r_type,
2805 elfcpp::Elf_types<32>::Elf_Addr value,
2806 unsigned char* view,
2807 section_size_type view_size)
2809 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2811 // leal foo@TLSDESC(%ebx), %eax
2812 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2813 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2814 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2815 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2816 view[-2] == 0x8d && view[-1] == 0x83);
2818 Relocate_functions<32, false>::rel32(view, value);
2822 // call *foo@TLSCALL(%eax)
2824 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2825 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2826 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2827 view[0] == 0xff && view[1] == 0x10);
2833 // Do a relocation in which we convert a TLS Local-Dynamic to a
2837 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2840 const elfcpp::Rel<32, false>& rel,
2842 elfcpp::Elf_types<32>::Elf_Addr,
2843 unsigned char* view,
2844 section_size_type view_size)
2846 // leal foo(%reg), %eax; call ___tls_get_addr
2847 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2849 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2850 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2852 // FIXME: Does this test really always pass?
2853 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2854 view[-2] == 0x8d && view[-1] == 0x83);
2856 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2858 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2860 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2862 this->skip_call_tls_get_addr_ = true;
2865 // Do a relocation in which we convert a TLS Initial-Exec to a
2869 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2871 Output_segment* tls_segment,
2872 const elfcpp::Rel<32, false>& rel,
2873 unsigned int r_type,
2874 elfcpp::Elf_types<32>::Elf_Addr value,
2875 unsigned char* view,
2876 section_size_type view_size)
2878 // We have to actually change the instructions, which means that we
2879 // need to examine the opcodes to figure out which instruction we
2881 if (r_type == elfcpp::R_386_TLS_IE)
2883 // movl %gs:XX,%eax ==> movl $YY,%eax
2884 // movl %gs:XX,%reg ==> movl $YY,%reg
2885 // addl %gs:XX,%reg ==> addl $YY,%reg
2886 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2887 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2889 unsigned char op1 = view[-1];
2892 // movl XX,%eax ==> movl $YY,%eax
2897 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2899 unsigned char op2 = view[-2];
2902 // movl XX,%reg ==> movl $YY,%reg
2903 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2904 (op1 & 0xc7) == 0x05);
2906 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2908 else if (op2 == 0x03)
2910 // addl XX,%reg ==> addl $YY,%reg
2911 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2912 (op1 & 0xc7) == 0x05);
2914 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2917 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2922 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2923 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2924 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2925 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2926 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2928 unsigned char op1 = view[-1];
2929 unsigned char op2 = view[-2];
2930 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2931 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2934 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2936 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2938 else if (op2 == 0x2b)
2940 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2942 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2944 else if (op2 == 0x03)
2946 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2948 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2951 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2954 value = tls_segment->memsz() - value;
2955 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2958 Relocate_functions<32, false>::rel32(view, value);
2961 // Relocate section data.
2964 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2965 unsigned int sh_type,
2966 const unsigned char* prelocs,
2968 Output_section* output_section,
2969 bool needs_special_offset_handling,
2970 unsigned char* view,
2971 elfcpp::Elf_types<32>::Elf_Addr address,
2972 section_size_type view_size,
2973 const Reloc_symbol_changes* reloc_symbol_changes)
2975 gold_assert(sh_type == elfcpp::SHT_REL);
2977 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2978 Target_i386::Relocate>(
2984 needs_special_offset_handling,
2988 reloc_symbol_changes);
2991 // Return the size of a relocation while scanning during a relocatable
2995 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2996 unsigned int r_type,
3001 case elfcpp::R_386_NONE:
3002 case elfcpp::R_386_GNU_VTINHERIT:
3003 case elfcpp::R_386_GNU_VTENTRY:
3004 case elfcpp::R_386_TLS_GD: // Global-dynamic
3005 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
3006 case elfcpp::R_386_TLS_DESC_CALL:
3007 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3008 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3009 case elfcpp::R_386_TLS_IE: // Initial-exec
3010 case elfcpp::R_386_TLS_IE_32:
3011 case elfcpp::R_386_TLS_GOTIE:
3012 case elfcpp::R_386_TLS_LE: // Local-exec
3013 case elfcpp::R_386_TLS_LE_32:
3016 case elfcpp::R_386_32:
3017 case elfcpp::R_386_PC32:
3018 case elfcpp::R_386_GOT32:
3019 case elfcpp::R_386_PLT32:
3020 case elfcpp::R_386_GOTOFF:
3021 case elfcpp::R_386_GOTPC:
3024 case elfcpp::R_386_16:
3025 case elfcpp::R_386_PC16:
3028 case elfcpp::R_386_8:
3029 case elfcpp::R_386_PC8:
3032 // These are relocations which should only be seen by the
3033 // dynamic linker, and should never be seen here.
3034 case elfcpp::R_386_COPY:
3035 case elfcpp::R_386_GLOB_DAT:
3036 case elfcpp::R_386_JUMP_SLOT:
3037 case elfcpp::R_386_RELATIVE:
3038 case elfcpp::R_386_IRELATIVE:
3039 case elfcpp::R_386_TLS_TPOFF:
3040 case elfcpp::R_386_TLS_DTPMOD32:
3041 case elfcpp::R_386_TLS_DTPOFF32:
3042 case elfcpp::R_386_TLS_TPOFF32:
3043 case elfcpp::R_386_TLS_DESC:
3044 object->error(_("unexpected reloc %u in object file"), r_type);
3047 case elfcpp::R_386_32PLT:
3048 case elfcpp::R_386_TLS_GD_32:
3049 case elfcpp::R_386_TLS_GD_PUSH:
3050 case elfcpp::R_386_TLS_GD_CALL:
3051 case elfcpp::R_386_TLS_GD_POP:
3052 case elfcpp::R_386_TLS_LDM_32:
3053 case elfcpp::R_386_TLS_LDM_PUSH:
3054 case elfcpp::R_386_TLS_LDM_CALL:
3055 case elfcpp::R_386_TLS_LDM_POP:
3056 case elfcpp::R_386_USED_BY_INTEL_200:
3058 object->error(_("unsupported reloc %u in object file"), r_type);
3063 // Scan the relocs during a relocatable link.
3066 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3068 Sized_relobj<32, false>* object,
3069 unsigned int data_shndx,
3070 unsigned int sh_type,
3071 const unsigned char* prelocs,
3073 Output_section* output_section,
3074 bool needs_special_offset_handling,
3075 size_t local_symbol_count,
3076 const unsigned char* plocal_symbols,
3077 Relocatable_relocs* rr)
3079 gold_assert(sh_type == elfcpp::SHT_REL);
3081 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3082 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3084 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
3085 Scan_relocatable_relocs>(
3093 needs_special_offset_handling,
3099 // Relocate a section during a relocatable link.
3102 Target_i386::relocate_for_relocatable(
3103 const Relocate_info<32, false>* relinfo,
3104 unsigned int sh_type,
3105 const unsigned char* prelocs,
3107 Output_section* output_section,
3108 off_t offset_in_output_section,
3109 const Relocatable_relocs* rr,
3110 unsigned char* view,
3111 elfcpp::Elf_types<32>::Elf_Addr view_address,
3112 section_size_type view_size,
3113 unsigned char* reloc_view,
3114 section_size_type reloc_view_size)
3116 gold_assert(sh_type == elfcpp::SHT_REL);
3118 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
3123 offset_in_output_section,
3132 // Return the value to use for a dynamic which requires special
3133 // treatment. This is how we support equality comparisons of function
3134 // pointers across shared library boundaries, as described in the
3135 // processor specific ABI supplement.
3138 Target_i386::do_dynsym_value(const Symbol* gsym) const
3140 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3141 return this->plt_section()->address() + gsym->plt_offset();
3144 // Return a string used to fill a code section with nops to take up
3145 // the specified length.
3148 Target_i386::do_code_fill(section_size_type length) const
3152 // Build a jmp instruction to skip over the bytes.
3153 unsigned char jmp[5];
3155 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3156 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3157 + std::string(length - 5, '\0'));
3160 // Nop sequences of various lengths.
3161 const char nop1[1] = { 0x90 }; // nop
3162 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3163 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3164 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3165 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3166 0x00 }; // leal 0(%esi,1),%esi
3167 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3169 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3171 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3172 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3173 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3174 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3176 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3177 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3179 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3180 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3182 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3183 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3184 0x00, 0x00, 0x00, 0x00 };
3185 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3186 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3187 0x27, 0x00, 0x00, 0x00,
3189 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3190 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3191 0xbc, 0x27, 0x00, 0x00,
3193 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3194 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3195 0x90, 0x90, 0x90, 0x90,
3198 const char* nops[16] = {
3200 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3201 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3204 return std::string(nops[length], length);
3207 // Return whether SYM should be treated as a call to a non-split
3208 // function. We don't want that to be true of a call to a
3209 // get_pc_thunk function.
3212 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
3214 return (sym->type() == elfcpp::STT_FUNC
3215 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3218 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3219 // compiled with -fstack-split. The function calls non-stack-split
3220 // code. We have to change the function so that it always ensures
3221 // that it has enough stack space to run some random function.
3224 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3225 section_offset_type fnoffset,
3226 section_size_type fnsize,
3227 unsigned char* view,
3228 section_size_type view_size,
3230 std::string* to) const
3232 // The function starts with a comparison of the stack pointer and a
3233 // field in the TCB. This is followed by a jump.
3236 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3239 // We will call __morestack if the carry flag is set after this
3240 // comparison. We turn the comparison into an stc instruction
3242 view[fnoffset] = '\xf9';
3243 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3245 // lea NN(%esp),%ecx
3246 // lea NN(%esp),%edx
3247 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3248 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
3251 // This is loading an offset from the stack pointer for a
3252 // comparison. The offset is negative, so we decrease the
3253 // offset by the amount of space we need for the stack. This
3254 // means we will avoid calling __morestack if there happens to
3255 // be plenty of space on the stack already.
3256 unsigned char* pval = view + fnoffset + 3;
3257 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3258 val -= parameters->options().split_stack_adjust_size();
3259 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3263 if (!object->has_no_split_stack())
3264 object->error(_("failed to match split-stack sequence at "
3265 "section %u offset %0zx"),
3266 shndx, static_cast<size_t>(fnoffset));
3270 // We have to change the function so that it calls
3271 // __morestack_non_split instead of __morestack. The former will
3272 // allocate additional stack space.
3273 *from = "__morestack";
3274 *to = "__morestack_non_split";
3277 // The selector for i386 object files.
3279 class Target_selector_i386 : public Target_selector_freebsd
3282 Target_selector_i386()
3283 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
3284 "elf32-i386", "elf32-i386-freebsd")
3288 do_instantiate_target()
3289 { return new Target_i386(); }
3292 Target_selector_i386 target_selector_i386;
3294 } // End anonymous namespace.