1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
51 // A class to handle the PLT data.
52 // This is an abstract base class that handles most of the linker details
53 // but does not know the actual contents of PLT entries. The derived
54 // classes below fill in those details.
56 class Output_data_plt_i386 : public Output_section_data
59 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
61 Output_data_plt_i386(Layout*, uint64_t addralign,
62 Output_data_space*, Output_data_space*);
64 // Add an entry to the PLT.
66 add_entry(Symbol_table*, Layout*, Symbol* gsym);
68 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
70 add_local_ifunc_entry(Symbol_table*, Layout*,
71 Sized_relobj_file<32, false>* relobj,
72 unsigned int local_sym_index);
74 // Return the .rel.plt section data.
77 { return this->rel_; }
79 // Return where the TLS_DESC relocations should go.
81 rel_tls_desc(Layout*);
83 // Return where the IRELATIVE relocations should go.
85 rel_irelative(Symbol_table*, Layout*);
87 // Return whether we created a section for IRELATIVE relocations.
89 has_irelative_section() const
90 { return this->irelative_rel_ != NULL; }
92 // Return the number of PLT entries.
95 { return this->count_ + this->irelative_count_; }
97 // Return the offset of the first non-reserved PLT entry.
99 first_plt_entry_offset()
100 { return this->get_plt_entry_size(); }
102 // Return the size of a PLT entry.
104 get_plt_entry_size() const
105 { return this->do_get_plt_entry_size(); }
107 // Return the PLT address to use for a global symbol.
109 address_for_global(const Symbol*);
111 // Return the PLT address to use for a local symbol.
113 address_for_local(const Relobj*, unsigned int symndx);
115 // Add .eh_frame information for the PLT.
117 add_eh_frame(Layout* layout)
118 { this->do_add_eh_frame(layout); }
121 // Fill the first PLT entry, given the pointer to the PLT section data
122 // and the runtime address of the GOT.
124 fill_first_plt_entry(unsigned char* pov,
125 elfcpp::Elf_types<32>::Elf_Addr got_address)
126 { this->do_fill_first_plt_entry(pov, got_address); }
128 // Fill a normal PLT entry, given the pointer to the entry's data in the
129 // section, the runtime address of the GOT, the offset into the GOT of
130 // the corresponding slot, the offset into the relocation section of the
131 // corresponding reloc, and the offset of this entry within the whole
132 // PLT. Return the offset from this PLT entry's runtime address that
133 // should be used to compute the initial value of the GOT slot.
135 fill_plt_entry(unsigned char* pov,
136 elfcpp::Elf_types<32>::Elf_Addr got_address,
137 unsigned int got_offset,
138 unsigned int plt_offset,
139 unsigned int plt_rel_offset)
141 return this->do_fill_plt_entry(pov, got_address, got_offset,
142 plt_offset, plt_rel_offset);
146 do_get_plt_entry_size() const = 0;
149 do_fill_first_plt_entry(unsigned char* pov,
150 elfcpp::Elf_types<32>::Elf_Addr got_address) = 0;
153 do_fill_plt_entry(unsigned char* pov,
154 elfcpp::Elf_types<32>::Elf_Addr got_address,
155 unsigned int got_offset,
156 unsigned int plt_offset,
157 unsigned int plt_rel_offset) = 0;
160 do_add_eh_frame(Layout*) = 0;
163 do_adjust_output_section(Output_section* os);
165 // Write to a map file.
167 do_print_to_mapfile(Mapfile* mapfile) const
168 { mapfile->print_output_data(this, _("** PLT")); }
170 // The .eh_frame unwind information for the PLT.
171 // The CIE is common across variants of the PLT format.
172 static const int plt_eh_frame_cie_size = 16;
173 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
176 // Set the final size.
178 set_final_data_size()
180 this->set_data_size((this->count_ + this->irelative_count_ + 1)
181 * this->get_plt_entry_size());
184 // Write out the PLT data.
186 do_write(Output_file*);
188 // We keep a list of global STT_GNU_IFUNC symbols, each with its
189 // offset in the GOT.
193 unsigned int got_offset;
196 // We keep a list of local STT_GNU_IFUNC symbols, each with its
197 // offset in the GOT.
200 Sized_relobj_file<32, false>* object;
201 unsigned int local_sym_index;
202 unsigned int got_offset;
205 // A pointer to the Layout class, so that we can find the .dynamic
206 // section when we write out the GOT PLT section.
208 // The reloc section.
210 // The TLS_DESC relocations, if necessary. These must follow the
211 // regular PLT relocs.
212 Reloc_section* tls_desc_rel_;
213 // The IRELATIVE relocations, if necessary. These must follow the
214 // regular relocatoins and the TLS_DESC relocations.
215 Reloc_section* irelative_rel_;
216 // The .got.plt section.
217 Output_data_space* got_plt_;
218 // The part of the .got.plt section used for IRELATIVE relocs.
219 Output_data_space* got_irelative_;
220 // The number of PLT entries.
222 // Number of PLT entries with R_386_IRELATIVE relocs. These follow
223 // the regular PLT entries.
224 unsigned int irelative_count_;
225 // Global STT_GNU_IFUNC symbols.
226 std::vector<Global_ifunc> global_ifuncs_;
227 // Local STT_GNU_IFUNC symbols.
228 std::vector<Local_ifunc> local_ifuncs_;
231 // This is an abstract class for the standard PLT layout.
232 // The derived classes below handle the actual PLT contents
233 // for the executable (non-PIC) and shared-library (PIC) cases.
234 // The unwind information is uniform across those two, so it's here.
236 class Output_data_plt_i386_standard : public Output_data_plt_i386
239 Output_data_plt_i386_standard(Layout* layout,
240 Output_data_space* got_plt,
241 Output_data_space* got_irelative)
242 : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative)
247 do_get_plt_entry_size() const
248 { return plt_entry_size; }
251 do_add_eh_frame(Layout* layout)
253 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
254 plt_eh_frame_fde, plt_eh_frame_fde_size);
257 // The size of an entry in the PLT.
258 static const int plt_entry_size = 16;
260 // The .eh_frame unwind information for the PLT.
261 static const int plt_eh_frame_fde_size = 32;
262 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
265 // Actually fill the PLT contents for an executable (non-PIC).
267 class Output_data_plt_i386_exec : public Output_data_plt_i386_standard
270 Output_data_plt_i386_exec(Layout* layout,
271 Output_data_space* got_plt,
272 Output_data_space* got_irelative)
273 : Output_data_plt_i386_standard(layout, got_plt, got_irelative)
278 do_fill_first_plt_entry(unsigned char* pov,
279 elfcpp::Elf_types<32>::Elf_Addr got_address);
282 do_fill_plt_entry(unsigned char* pov,
283 elfcpp::Elf_types<32>::Elf_Addr got_address,
284 unsigned int got_offset,
285 unsigned int plt_offset,
286 unsigned int plt_rel_offset);
289 // The first entry in the PLT for an executable.
290 static const unsigned char first_plt_entry[plt_entry_size];
292 // Other entries in the PLT for an executable.
293 static const unsigned char plt_entry[plt_entry_size];
296 // Actually fill the PLT contents for a shared library (PIC).
298 class Output_data_plt_i386_dyn : public Output_data_plt_i386_standard
301 Output_data_plt_i386_dyn(Layout* layout,
302 Output_data_space* got_plt,
303 Output_data_space* got_irelative)
304 : Output_data_plt_i386_standard(layout, got_plt, got_irelative)
309 do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr);
312 do_fill_plt_entry(unsigned char* pov,
313 elfcpp::Elf_types<32>::Elf_Addr,
314 unsigned int got_offset,
315 unsigned int plt_offset,
316 unsigned int plt_rel_offset);
319 // The first entry in the PLT for a shared object.
320 static const unsigned char first_plt_entry[plt_entry_size];
322 // Other entries in the PLT for a shared object.
323 static const unsigned char plt_entry[plt_entry_size];
326 // The i386 target class.
327 // TLS info comes from
328 // http://people.redhat.com/drepper/tls.pdf
329 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
331 class Target_i386 : public Sized_target<32, false>
334 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
336 Target_i386(const Target::Target_info* info = &i386_info)
337 : Sized_target<32, false>(info),
338 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
339 got_tlsdesc_(NULL), global_offset_table_(NULL), rel_dyn_(NULL),
340 rel_irelative_(NULL), copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
341 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
344 // Process the relocations to determine unreferenced sections for
345 // garbage collection.
347 gc_process_relocs(Symbol_table* symtab,
349 Sized_relobj_file<32, false>* object,
350 unsigned int data_shndx,
351 unsigned int sh_type,
352 const unsigned char* prelocs,
354 Output_section* output_section,
355 bool needs_special_offset_handling,
356 size_t local_symbol_count,
357 const unsigned char* plocal_symbols);
359 // Scan the relocations to look for symbol adjustments.
361 scan_relocs(Symbol_table* symtab,
363 Sized_relobj_file<32, false>* object,
364 unsigned int data_shndx,
365 unsigned int sh_type,
366 const unsigned char* prelocs,
368 Output_section* output_section,
369 bool needs_special_offset_handling,
370 size_t local_symbol_count,
371 const unsigned char* plocal_symbols);
373 // Finalize the sections.
375 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
377 // Return the value to use for a dynamic which requires special
380 do_dynsym_value(const Symbol*) const;
382 // Relocate a section.
384 relocate_section(const Relocate_info<32, false>*,
385 unsigned int sh_type,
386 const unsigned char* prelocs,
388 Output_section* output_section,
389 bool needs_special_offset_handling,
391 elfcpp::Elf_types<32>::Elf_Addr view_address,
392 section_size_type view_size,
393 const Reloc_symbol_changes*);
395 // Scan the relocs during a relocatable link.
397 scan_relocatable_relocs(Symbol_table* symtab,
399 Sized_relobj_file<32, false>* object,
400 unsigned int data_shndx,
401 unsigned int sh_type,
402 const unsigned char* prelocs,
404 Output_section* output_section,
405 bool needs_special_offset_handling,
406 size_t local_symbol_count,
407 const unsigned char* plocal_symbols,
408 Relocatable_relocs*);
410 // Emit relocations for a section.
412 relocate_relocs(const Relocate_info<32, false>*,
413 unsigned int sh_type,
414 const unsigned char* prelocs,
416 Output_section* output_section,
417 typename elfcpp::Elf_types<32>::Elf_Off
418 offset_in_output_section,
419 const Relocatable_relocs*,
421 elfcpp::Elf_types<32>::Elf_Addr view_address,
422 section_size_type view_size,
423 unsigned char* reloc_view,
424 section_size_type reloc_view_size);
426 // Return a string used to fill a code section with nops.
428 do_code_fill(section_size_type length) const;
430 // Return whether SYM is defined by the ABI.
432 do_is_defined_by_abi(const Symbol* sym) const
433 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
435 // Return whether a symbol name implies a local label. The UnixWare
436 // 2.1 cc generates temporary symbols that start with .X, so we
437 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
438 // If so, we should move the .X recognition into
439 // Target::do_is_local_label_name.
441 do_is_local_label_name(const char* name) const
443 if (name[0] == '.' && name[1] == 'X')
445 return Target::do_is_local_label_name(name);
448 // Return the PLT address to use for a global symbol.
450 do_plt_address_for_global(const Symbol* gsym) const
451 { return this->plt_section()->address_for_global(gsym); }
454 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
455 { return this->plt_section()->address_for_local(relobj, symndx); }
457 // We can tell whether we take the address of a function.
459 do_can_check_for_function_pointers() const
462 // Return the base for a DW_EH_PE_datarel encoding.
464 do_ehframe_datarel_base() const;
466 // Return whether SYM is call to a non-split function.
468 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
470 // Adjust -fsplit-stack code which calls non-split-stack code.
472 do_calls_non_split(Relobj* object, unsigned int shndx,
473 section_offset_type fnoffset, section_size_type fnsize,
474 unsigned char* view, section_size_type view_size,
475 std::string* from, std::string* to) const;
477 // Return the size of the GOT section.
481 gold_assert(this->got_ != NULL);
482 return this->got_->data_size();
485 // Return the number of entries in the GOT.
487 got_entry_count() const
489 if (this->got_ == NULL)
491 return this->got_size() / 4;
494 // Return the number of entries in the PLT.
496 plt_entry_count() const;
498 // Return the offset of the first non-reserved PLT entry.
500 first_plt_entry_offset() const;
502 // Return the size of each PLT entry.
504 plt_entry_size() const;
507 // Instantiate the plt_ member.
508 // This chooses the right PLT flavor for an executable or a shared object.
509 Output_data_plt_i386*
510 make_data_plt(Layout* layout,
511 Output_data_space* got_plt,
512 Output_data_space* got_irelative,
514 { return this->do_make_data_plt(layout, got_plt, got_irelative, dyn); }
516 virtual Output_data_plt_i386*
517 do_make_data_plt(Layout* layout,
518 Output_data_space* got_plt,
519 Output_data_space* got_irelative,
523 return new Output_data_plt_i386_dyn(layout, got_plt, got_irelative);
525 return new Output_data_plt_i386_exec(layout, got_plt, got_irelative);
529 // The class which scans relocations.
534 get_reference_flags(unsigned int r_type);
537 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
538 Sized_relobj_file<32, false>* object,
539 unsigned int data_shndx,
540 Output_section* output_section,
541 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
542 const elfcpp::Sym<32, false>& lsym,
546 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
547 Sized_relobj_file<32, false>* object,
548 unsigned int data_shndx,
549 Output_section* output_section,
550 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
554 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
556 Sized_relobj_file<32, false>* object,
557 unsigned int data_shndx,
558 Output_section* output_section,
559 const elfcpp::Rel<32, false>& reloc,
561 const elfcpp::Sym<32, false>& lsym);
564 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
566 Sized_relobj_file<32, false>* object,
567 unsigned int data_shndx,
568 Output_section* output_section,
569 const elfcpp::Rel<32, false>& reloc,
574 possible_function_pointer_reloc(unsigned int r_type);
577 reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
578 unsigned int r_type);
581 unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
584 unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
588 // The class which implements relocation.
593 : skip_call_tls_get_addr_(false),
594 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
599 if (this->skip_call_tls_get_addr_)
601 // FIXME: This needs to specify the location somehow.
602 gold_error(_("missing expected TLS relocation"));
606 // Return whether the static relocation needs to be applied.
608 should_apply_static_reloc(const Sized_symbol<32>* gsym,
611 Output_section* output_section);
613 // Do a relocation. Return false if the caller should not issue
614 // any warnings about this relocation.
616 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
617 size_t relnum, const elfcpp::Rel<32, false>&,
618 unsigned int r_type, const Sized_symbol<32>*,
619 const Symbol_value<32>*,
620 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
624 // Do a TLS relocation.
626 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
627 size_t relnum, const elfcpp::Rel<32, false>&,
628 unsigned int r_type, const Sized_symbol<32>*,
629 const Symbol_value<32>*,
630 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
633 // Do a TLS General-Dynamic to Initial-Exec transition.
635 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
636 Output_segment* tls_segment,
637 const elfcpp::Rel<32, false>&, unsigned int r_type,
638 elfcpp::Elf_types<32>::Elf_Addr value,
640 section_size_type view_size);
642 // Do a TLS General-Dynamic to Local-Exec transition.
644 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
645 Output_segment* tls_segment,
646 const elfcpp::Rel<32, false>&, unsigned int r_type,
647 elfcpp::Elf_types<32>::Elf_Addr value,
649 section_size_type view_size);
651 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
654 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
655 Output_segment* tls_segment,
656 const elfcpp::Rel<32, false>&, unsigned int r_type,
657 elfcpp::Elf_types<32>::Elf_Addr value,
659 section_size_type view_size);
661 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
664 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
665 Output_segment* tls_segment,
666 const elfcpp::Rel<32, false>&, unsigned int r_type,
667 elfcpp::Elf_types<32>::Elf_Addr value,
669 section_size_type view_size);
671 // Do a TLS Local-Dynamic to Local-Exec transition.
673 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
674 Output_segment* tls_segment,
675 const elfcpp::Rel<32, false>&, unsigned int r_type,
676 elfcpp::Elf_types<32>::Elf_Addr value,
678 section_size_type view_size);
680 // Do a TLS Initial-Exec to Local-Exec transition.
682 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
683 Output_segment* tls_segment,
684 const elfcpp::Rel<32, false>&, unsigned int r_type,
685 elfcpp::Elf_types<32>::Elf_Addr value,
687 section_size_type view_size);
689 // We need to keep track of which type of local dynamic relocation
690 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
691 enum Local_dynamic_type
698 // This is set if we should skip the next reloc, which should be a
699 // PLT32 reloc against ___tls_get_addr.
700 bool skip_call_tls_get_addr_;
701 // The type of local dynamic relocation we have seen in the section
702 // being relocated, if any.
703 Local_dynamic_type local_dynamic_type_;
706 // A class which returns the size required for a relocation type,
707 // used while scanning relocs during a relocatable link.
708 class Relocatable_size_for_reloc
712 get_size_for_reloc(unsigned int, Relobj*);
715 // Adjust TLS relocation type based on the options and whether this
716 // is a local symbol.
717 static tls::Tls_optimization
718 optimize_tls_reloc(bool is_final, int r_type);
720 // Get the GOT section, creating it if necessary.
721 Output_data_got<32, false>*
722 got_section(Symbol_table*, Layout*);
724 // Get the GOT PLT section.
726 got_plt_section() const
728 gold_assert(this->got_plt_ != NULL);
729 return this->got_plt_;
732 // Get the GOT section for TLSDESC entries.
733 Output_data_got<32, false>*
734 got_tlsdesc_section() const
736 gold_assert(this->got_tlsdesc_ != NULL);
737 return this->got_tlsdesc_;
740 // Create the PLT section.
742 make_plt_section(Symbol_table* symtab, Layout* layout);
744 // Create a PLT entry for a global symbol.
746 make_plt_entry(Symbol_table*, Layout*, Symbol*);
748 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
750 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
751 Sized_relobj_file<32, false>* relobj,
752 unsigned int local_sym_index);
754 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
756 define_tls_base_symbol(Symbol_table*, Layout*);
758 // Create a GOT entry for the TLS module index.
760 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
761 Sized_relobj_file<32, false>* object);
763 // Get the PLT section.
764 Output_data_plt_i386*
767 gold_assert(this->plt_ != NULL);
771 // Get the dynamic reloc section, creating it if necessary.
773 rel_dyn_section(Layout*);
775 // Get the section to use for TLS_DESC relocations.
777 rel_tls_desc_section(Layout*) const;
779 // Get the section to use for IRELATIVE relocations.
781 rel_irelative_section(Layout*);
783 // Add a potential copy relocation.
785 copy_reloc(Symbol_table* symtab, Layout* layout,
786 Sized_relobj_file<32, false>* object,
787 unsigned int shndx, Output_section* output_section,
788 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
790 this->copy_relocs_.copy_reloc(symtab, layout,
791 symtab->get_sized_symbol<32>(sym),
792 object, shndx, output_section, reloc,
793 this->rel_dyn_section(layout));
796 // Information about this specific target which we pass to the
797 // general Target structure.
798 static const Target::Target_info i386_info;
800 // The types of GOT entries needed for this platform.
801 // These values are exposed to the ABI in an incremental link.
802 // Do not renumber existing values without changing the version
803 // number of the .gnu_incremental_inputs section.
806 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
807 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
808 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
809 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
810 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
814 Output_data_got<32, false>* got_;
816 Output_data_plt_i386* plt_;
817 // The GOT PLT section.
818 Output_data_space* got_plt_;
819 // The GOT section for IRELATIVE relocations.
820 Output_data_space* got_irelative_;
821 // The GOT section for TLSDESC relocations.
822 Output_data_got<32, false>* got_tlsdesc_;
823 // The _GLOBAL_OFFSET_TABLE_ symbol.
824 Symbol* global_offset_table_;
825 // The dynamic reloc section.
826 Reloc_section* rel_dyn_;
827 // The section to use for IRELATIVE relocs.
828 Reloc_section* rel_irelative_;
829 // Relocs saved to avoid a COPY reloc.
830 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
831 // Space for variables copied with a COPY reloc.
832 Output_data_space* dynbss_;
833 // Offset of the GOT entry for the TLS module index.
834 unsigned int got_mod_index_offset_;
835 // True if the _TLS_MODULE_BASE_ symbol has been defined.
836 bool tls_base_symbol_defined_;
839 const Target::Target_info Target_i386::i386_info =
842 false, // is_big_endian
843 elfcpp::EM_386, // machine_code
844 false, // has_make_symbol
845 false, // has_resolve
846 true, // has_code_fill
847 true, // is_default_stack_executable
848 true, // can_icf_inline_merge_sections
850 "/usr/lib/libc.so.1", // dynamic_linker
851 0x08048000, // default_text_segment_address
852 0x1000, // abi_pagesize (overridable by -z max-page-size)
853 0x1000, // common_pagesize (overridable by -z common-page-size)
854 false, // isolate_execinstr
856 elfcpp::SHN_UNDEF, // small_common_shndx
857 elfcpp::SHN_UNDEF, // large_common_shndx
858 0, // small_common_section_flags
859 0, // large_common_section_flags
860 NULL, // attributes_section
861 NULL // attributes_vendor
864 // Get the GOT section, creating it if necessary.
866 Output_data_got<32, false>*
867 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
869 if (this->got_ == NULL)
871 gold_assert(symtab != NULL && layout != NULL);
873 this->got_ = new Output_data_got<32, false>();
875 // When using -z now, we can treat .got.plt as a relro section.
876 // Without -z now, it is modified after program startup by lazy
878 bool is_got_plt_relro = parameters->options().now();
879 Output_section_order got_order = (is_got_plt_relro
882 Output_section_order got_plt_order = (is_got_plt_relro
884 : ORDER_NON_RELRO_FIRST);
886 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
888 | elfcpp::SHF_WRITE),
889 this->got_, got_order, true);
891 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
892 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
894 | elfcpp::SHF_WRITE),
895 this->got_plt_, got_plt_order,
898 // The first three entries are reserved.
899 this->got_plt_->set_current_data_size(3 * 4);
901 if (!is_got_plt_relro)
903 // Those bytes can go into the relro segment.
904 layout->increase_relro(3 * 4);
907 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
908 this->global_offset_table_ =
909 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
910 Symbol_table::PREDEFINED,
912 0, 0, elfcpp::STT_OBJECT,
914 elfcpp::STV_HIDDEN, 0,
917 // If there are any IRELATIVE relocations, they get GOT entries
918 // in .got.plt after the jump slot relocations.
919 this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT");
920 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
922 | elfcpp::SHF_WRITE),
923 this->got_irelative_,
924 got_plt_order, is_got_plt_relro);
926 // If there are any TLSDESC relocations, they get GOT entries in
927 // .got.plt after the jump slot entries.
928 this->got_tlsdesc_ = new Output_data_got<32, false>();
929 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
931 | elfcpp::SHF_WRITE),
933 got_plt_order, is_got_plt_relro);
939 // Get the dynamic reloc section, creating it if necessary.
941 Target_i386::Reloc_section*
942 Target_i386::rel_dyn_section(Layout* layout)
944 if (this->rel_dyn_ == NULL)
946 gold_assert(layout != NULL);
947 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
948 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
949 elfcpp::SHF_ALLOC, this->rel_dyn_,
950 ORDER_DYNAMIC_RELOCS, false);
952 return this->rel_dyn_;
955 // Get the section to use for IRELATIVE relocs, creating it if
956 // necessary. These go in .rel.dyn, but only after all other dynamic
957 // relocations. They need to follow the other dynamic relocations so
958 // that they can refer to global variables initialized by those
961 Target_i386::Reloc_section*
962 Target_i386::rel_irelative_section(Layout* layout)
964 if (this->rel_irelative_ == NULL)
966 // Make sure we have already create the dynamic reloc section.
967 this->rel_dyn_section(layout);
968 this->rel_irelative_ = new Reloc_section(false);
969 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
970 elfcpp::SHF_ALLOC, this->rel_irelative_,
971 ORDER_DYNAMIC_RELOCS, false);
972 gold_assert(this->rel_dyn_->output_section()
973 == this->rel_irelative_->output_section());
975 return this->rel_irelative_;
978 // Create the PLT section. The ordinary .got section is an argument,
979 // since we need to refer to the start. We also create our own .got
980 // section just for PLT entries.
982 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
984 Output_data_space* got_plt,
985 Output_data_space* got_irelative)
986 : Output_section_data(addralign),
987 layout_(layout), tls_desc_rel_(NULL),
988 irelative_rel_(NULL), got_plt_(got_plt), got_irelative_(got_irelative),
989 count_(0), irelative_count_(0), global_ifuncs_(), local_ifuncs_()
991 this->rel_ = new Reloc_section(false);
992 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
993 elfcpp::SHF_ALLOC, this->rel_,
994 ORDER_DYNAMIC_PLT_RELOCS, false);
998 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
1000 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
1001 // linker, and so do we.
1005 // Add an entry to the PLT.
1008 Output_data_plt_i386::add_entry(Symbol_table* symtab, Layout* layout,
1011 gold_assert(!gsym->has_plt_offset());
1013 // Every PLT entry needs a reloc.
1014 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1015 && gsym->can_use_relative_reloc(false))
1017 gsym->set_plt_offset(this->irelative_count_ * this->get_plt_entry_size());
1018 ++this->irelative_count_;
1019 section_offset_type got_offset =
1020 this->got_irelative_->current_data_size();
1021 this->got_irelative_->set_current_data_size(got_offset + 4);
1022 Reloc_section* rel = this->rel_irelative(symtab, layout);
1023 rel->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
1024 this->got_irelative_, got_offset);
1025 struct Global_ifunc gi;
1027 gi.got_offset = got_offset;
1028 this->global_ifuncs_.push_back(gi);
1032 // When setting the PLT offset we skip the initial reserved PLT
1034 gsym->set_plt_offset((this->count_ + 1) * this->get_plt_entry_size());
1038 section_offset_type got_offset = this->got_plt_->current_data_size();
1040 // Every PLT entry needs a GOT entry which points back to the
1041 // PLT entry (this will be changed by the dynamic linker,
1042 // normally lazily when the function is called).
1043 this->got_plt_->set_current_data_size(got_offset + 4);
1045 gsym->set_needs_dynsym_entry();
1046 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
1050 // Note that we don't need to save the symbol. The contents of the
1051 // PLT are independent of which symbols are used. The symbols only
1052 // appear in the relocations.
1055 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1059 Output_data_plt_i386::add_local_ifunc_entry(
1060 Symbol_table* symtab,
1062 Sized_relobj_file<32, false>* relobj,
1063 unsigned int local_sym_index)
1065 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1066 ++this->irelative_count_;
1068 section_offset_type got_offset = this->got_irelative_->current_data_size();
1070 // Every PLT entry needs a GOT entry which points back to the PLT
1072 this->got_irelative_->set_current_data_size(got_offset + 4);
1074 // Every PLT entry needs a reloc.
1075 Reloc_section* rel = this->rel_irelative(symtab, layout);
1076 rel->add_symbolless_local_addend(relobj, local_sym_index,
1077 elfcpp::R_386_IRELATIVE,
1078 this->got_irelative_, got_offset);
1080 struct Local_ifunc li;
1082 li.local_sym_index = local_sym_index;
1083 li.got_offset = got_offset;
1084 this->local_ifuncs_.push_back(li);
1089 // Return where the TLS_DESC relocations should go, creating it if
1090 // necessary. These follow the JUMP_SLOT relocations.
1092 Output_data_plt_i386::Reloc_section*
1093 Output_data_plt_i386::rel_tls_desc(Layout* layout)
1095 if (this->tls_desc_rel_ == NULL)
1097 this->tls_desc_rel_ = new Reloc_section(false);
1098 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1099 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
1100 ORDER_DYNAMIC_PLT_RELOCS, false);
1101 gold_assert(this->tls_desc_rel_->output_section()
1102 == this->rel_->output_section());
1104 return this->tls_desc_rel_;
1107 // Return where the IRELATIVE relocations should go in the PLT. These
1108 // follow the JUMP_SLOT and TLS_DESC relocations.
1110 Output_data_plt_i386::Reloc_section*
1111 Output_data_plt_i386::rel_irelative(Symbol_table* symtab, Layout* layout)
1113 if (this->irelative_rel_ == NULL)
1115 // Make sure we have a place for the TLS_DESC relocations, in
1116 // case we see any later on.
1117 this->rel_tls_desc(layout);
1118 this->irelative_rel_ = new Reloc_section(false);
1119 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1120 elfcpp::SHF_ALLOC, this->irelative_rel_,
1121 ORDER_DYNAMIC_PLT_RELOCS, false);
1122 gold_assert(this->irelative_rel_->output_section()
1123 == this->rel_->output_section());
1125 if (parameters->doing_static_link())
1127 // A statically linked executable will only have a .rel.plt
1128 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
1129 // symbols. The library will use these symbols to locate
1130 // the IRELATIVE relocs at program startup time.
1131 symtab->define_in_output_data("__rel_iplt_start", NULL,
1132 Symbol_table::PREDEFINED,
1133 this->irelative_rel_, 0, 0,
1134 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1135 elfcpp::STV_HIDDEN, 0, false, true);
1136 symtab->define_in_output_data("__rel_iplt_end", NULL,
1137 Symbol_table::PREDEFINED,
1138 this->irelative_rel_, 0, 0,
1139 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1140 elfcpp::STV_HIDDEN, 0, true, true);
1143 return this->irelative_rel_;
1146 // Return the PLT address to use for a global symbol.
1149 Output_data_plt_i386::address_for_global(const Symbol* gsym)
1151 uint64_t offset = 0;
1152 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1153 && gsym->can_use_relative_reloc(false))
1154 offset = (this->count_ + 1) * this->get_plt_entry_size();
1155 return this->address() + offset + gsym->plt_offset();
1158 // Return the PLT address to use for a local symbol. These are always
1159 // IRELATIVE relocs.
1162 Output_data_plt_i386::address_for_local(const Relobj* object,
1165 return (this->address()
1166 + (this->count_ + 1) * this->get_plt_entry_size()
1167 + object->local_plt_offset(r_sym));
1170 // The first entry in the PLT for an executable.
1172 const unsigned char Output_data_plt_i386_exec::first_plt_entry[plt_entry_size] =
1174 0xff, 0x35, // pushl contents of memory address
1175 0, 0, 0, 0, // replaced with address of .got + 4
1176 0xff, 0x25, // jmp indirect
1177 0, 0, 0, 0, // replaced with address of .got + 8
1178 0, 0, 0, 0 // unused
1182 Output_data_plt_i386_exec::do_fill_first_plt_entry(
1184 elfcpp::Elf_types<32>::Elf_Addr got_address)
1186 memcpy(pov, first_plt_entry, plt_entry_size);
1187 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
1188 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
1191 // The first entry in the PLT for a shared object.
1193 const unsigned char Output_data_plt_i386_dyn::first_plt_entry[plt_entry_size] =
1195 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
1196 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
1197 0, 0, 0, 0 // unused
1201 Output_data_plt_i386_dyn::do_fill_first_plt_entry(
1203 elfcpp::Elf_types<32>::Elf_Addr)
1205 memcpy(pov, first_plt_entry, plt_entry_size);
1208 // Subsequent entries in the PLT for an executable.
1210 const unsigned char Output_data_plt_i386_exec::plt_entry[plt_entry_size] =
1212 0xff, 0x25, // jmp indirect
1213 0, 0, 0, 0, // replaced with address of symbol in .got
1214 0x68, // pushl immediate
1215 0, 0, 0, 0, // replaced with offset into relocation table
1216 0xe9, // jmp relative
1217 0, 0, 0, 0 // replaced with offset to start of .plt
1221 Output_data_plt_i386_exec::do_fill_plt_entry(
1223 elfcpp::Elf_types<32>::Elf_Addr got_address,
1224 unsigned int got_offset,
1225 unsigned int plt_offset,
1226 unsigned int plt_rel_offset)
1228 memcpy(pov, plt_entry, plt_entry_size);
1229 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1230 got_address + got_offset);
1231 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1232 elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
1236 // Subsequent entries in the PLT for a shared object.
1238 const unsigned char Output_data_plt_i386_dyn::plt_entry[plt_entry_size] =
1240 0xff, 0xa3, // jmp *offset(%ebx)
1241 0, 0, 0, 0, // replaced with offset of symbol in .got
1242 0x68, // pushl immediate
1243 0, 0, 0, 0, // replaced with offset into relocation table
1244 0xe9, // jmp relative
1245 0, 0, 0, 0 // replaced with offset to start of .plt
1249 Output_data_plt_i386_dyn::do_fill_plt_entry(unsigned char* pov,
1250 elfcpp::Elf_types<32>::Elf_Addr,
1251 unsigned int got_offset,
1252 unsigned int plt_offset,
1253 unsigned int plt_rel_offset)
1255 memcpy(pov, plt_entry, plt_entry_size);
1256 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
1257 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1258 elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
1262 // The .eh_frame unwind information for the PLT.
1265 Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1268 'z', // Augmentation: augmentation size included.
1269 'R', // Augmentation: FDE encoding included.
1270 '\0', // End of augmentation string.
1271 1, // Code alignment factor.
1272 0x7c, // Data alignment factor.
1273 8, // Return address column.
1274 1, // Augmentation size.
1275 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1276 | elfcpp::DW_EH_PE_sdata4),
1277 elfcpp::DW_CFA_def_cfa, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
1278 elfcpp::DW_CFA_offset + 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
1279 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1284 Output_data_plt_i386_standard::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1286 0, 0, 0, 0, // Replaced with offset to .plt.
1287 0, 0, 0, 0, // Replaced with size of .plt.
1288 0, // Augmentation size.
1289 elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
1290 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1291 elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
1292 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1293 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1294 11, // Block length.
1295 elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
1296 elfcpp::DW_OP_breg8, 0, // Push %eip.
1297 elfcpp::DW_OP_lit15, // Push 0xf.
1298 elfcpp::DW_OP_and, // & (%eip & 0xf).
1299 elfcpp::DW_OP_lit11, // Push 0xb.
1300 elfcpp::DW_OP_ge, // >= ((%eip & 0xf) >= 0xb)
1301 elfcpp::DW_OP_lit2, // Push 2.
1302 elfcpp::DW_OP_shl, // << (((%eip & 0xf) >= 0xb) << 2)
1303 elfcpp::DW_OP_plus, // + ((((%eip&0xf)>=0xb)<<2)+%esp+4
1304 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1310 // Write out the PLT. This uses the hand-coded instructions above,
1311 // and adjusts them as needed. This is all specified by the i386 ELF
1312 // Processor Supplement.
1315 Output_data_plt_i386::do_write(Output_file* of)
1317 const off_t offset = this->offset();
1318 const section_size_type oview_size =
1319 convert_to_section_size_type(this->data_size());
1320 unsigned char* const oview = of->get_output_view(offset, oview_size);
1322 const off_t got_file_offset = this->got_plt_->offset();
1323 gold_assert(parameters->incremental_update()
1324 || (got_file_offset + this->got_plt_->data_size()
1325 == this->got_irelative_->offset()));
1326 const section_size_type got_size =
1327 convert_to_section_size_type(this->got_plt_->data_size()
1328 + this->got_irelative_->data_size());
1329 unsigned char* const got_view = of->get_output_view(got_file_offset,
1332 unsigned char* pov = oview;
1334 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
1335 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
1337 this->fill_first_plt_entry(pov, got_address);
1338 pov += this->get_plt_entry_size();
1340 unsigned char* got_pov = got_view;
1342 // The first entry in the GOT is the address of the .dynamic section
1343 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1344 // We saved space for them when we created the section in
1345 // Target_i386::got_section.
1346 Output_section* dynamic = this->layout_->dynamic_section();
1347 uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1348 elfcpp::Swap<32, false>::writeval(got_pov, dynamic_addr);
1350 memset(got_pov, 0, 8);
1353 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1355 unsigned int plt_offset = this->get_plt_entry_size();
1356 unsigned int plt_rel_offset = 0;
1357 unsigned int got_offset = 12;
1358 const unsigned int count = this->count_ + this->irelative_count_;
1359 for (unsigned int i = 0;
1362 pov += this->get_plt_entry_size(),
1364 plt_offset += this->get_plt_entry_size(),
1365 plt_rel_offset += rel_size,
1368 // Set and adjust the PLT entry itself.
1369 unsigned int lazy_offset = this->fill_plt_entry(pov,
1375 // Set the entry in the GOT.
1376 elfcpp::Swap<32, false>::writeval(got_pov,
1377 plt_address + plt_offset + lazy_offset);
1380 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
1381 // the GOT to point to the actual symbol value, rather than point to
1382 // the PLT entry. That will let the dynamic linker call the right
1383 // function when resolving IRELATIVE relocations.
1384 unsigned char* got_irelative_view = got_view + this->got_plt_->data_size();
1385 for (std::vector<Global_ifunc>::const_iterator p =
1386 this->global_ifuncs_.begin();
1387 p != this->global_ifuncs_.end();
1390 const Sized_symbol<32>* ssym =
1391 static_cast<const Sized_symbol<32>*>(p->sym);
1392 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1396 for (std::vector<Local_ifunc>::const_iterator p =
1397 this->local_ifuncs_.begin();
1398 p != this->local_ifuncs_.end();
1401 const Symbol_value<32>* psymval =
1402 p->object->local_symbol(p->local_sym_index);
1403 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1404 psymval->value(p->object, 0));
1407 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1408 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1410 of->write_output_view(offset, oview_size, oview);
1411 of->write_output_view(got_file_offset, got_size, got_view);
1414 // Create the PLT section.
1417 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1419 if (this->plt_ == NULL)
1421 // Create the GOT sections first.
1422 this->got_section(symtab, layout);
1424 const bool dyn = parameters->options().output_is_position_independent();
1425 this->plt_ = this->make_data_plt(layout,
1427 this->got_irelative_,
1430 // Add unwind information if requested.
1431 if (parameters->options().ld_generated_unwind_info())
1432 this->plt_->add_eh_frame(layout);
1434 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1436 | elfcpp::SHF_EXECINSTR),
1437 this->plt_, ORDER_PLT, false);
1439 // Make the sh_info field of .rel.plt point to .plt.
1440 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1441 rel_plt_os->set_info_section(this->plt_->output_section());
1445 // Create a PLT entry for a global symbol.
1448 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1450 if (gsym->has_plt_offset())
1452 if (this->plt_ == NULL)
1453 this->make_plt_section(symtab, layout);
1454 this->plt_->add_entry(symtab, layout, gsym);
1457 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1460 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1461 Sized_relobj_file<32, false>* relobj,
1462 unsigned int local_sym_index)
1464 if (relobj->local_has_plt_offset(local_sym_index))
1466 if (this->plt_ == NULL)
1467 this->make_plt_section(symtab, layout);
1468 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1471 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1474 // Return the number of entries in the PLT.
1477 Target_i386::plt_entry_count() const
1479 if (this->plt_ == NULL)
1481 return this->plt_->entry_count();
1484 // Return the offset of the first non-reserved PLT entry.
1487 Target_i386::first_plt_entry_offset() const
1489 return this->plt_->first_plt_entry_offset();
1492 // Return the size of each PLT entry.
1495 Target_i386::plt_entry_size() const
1497 return this->plt_->get_plt_entry_size();
1500 // Get the section to use for TLS_DESC relocations.
1502 Target_i386::Reloc_section*
1503 Target_i386::rel_tls_desc_section(Layout* layout) const
1505 return this->plt_section()->rel_tls_desc(layout);
1508 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1511 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1513 if (this->tls_base_symbol_defined_)
1516 Output_segment* tls_segment = layout->tls_segment();
1517 if (tls_segment != NULL)
1519 bool is_exec = parameters->options().output_is_executable();
1520 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1521 Symbol_table::PREDEFINED,
1525 elfcpp::STV_HIDDEN, 0,
1527 ? Symbol::SEGMENT_END
1528 : Symbol::SEGMENT_START),
1531 this->tls_base_symbol_defined_ = true;
1534 // Create a GOT entry for the TLS module index.
1537 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1538 Sized_relobj_file<32, false>* object)
1540 if (this->got_mod_index_offset_ == -1U)
1542 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1543 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1544 Output_data_got<32, false>* got = this->got_section(symtab, layout);
1545 unsigned int got_offset = got->add_constant(0);
1546 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1548 got->add_constant(0);
1549 this->got_mod_index_offset_ = got_offset;
1551 return this->got_mod_index_offset_;
1554 // Optimize the TLS relocation type based on what we know about the
1555 // symbol. IS_FINAL is true if the final address of this symbol is
1556 // known at link time.
1558 tls::Tls_optimization
1559 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1561 // If we are generating a shared library, then we can't do anything
1563 if (parameters->options().shared())
1564 return tls::TLSOPT_NONE;
1568 case elfcpp::R_386_TLS_GD:
1569 case elfcpp::R_386_TLS_GOTDESC:
1570 case elfcpp::R_386_TLS_DESC_CALL:
1571 // These are General-Dynamic which permits fully general TLS
1572 // access. Since we know that we are generating an executable,
1573 // we can convert this to Initial-Exec. If we also know that
1574 // this is a local symbol, we can further switch to Local-Exec.
1576 return tls::TLSOPT_TO_LE;
1577 return tls::TLSOPT_TO_IE;
1579 case elfcpp::R_386_TLS_LDM:
1580 // This is Local-Dynamic, which refers to a local symbol in the
1581 // dynamic TLS block. Since we know that we generating an
1582 // executable, we can switch to Local-Exec.
1583 return tls::TLSOPT_TO_LE;
1585 case elfcpp::R_386_TLS_LDO_32:
1586 // Another type of Local-Dynamic relocation.
1587 return tls::TLSOPT_TO_LE;
1589 case elfcpp::R_386_TLS_IE:
1590 case elfcpp::R_386_TLS_GOTIE:
1591 case elfcpp::R_386_TLS_IE_32:
1592 // These are Initial-Exec relocs which get the thread offset
1593 // from the GOT. If we know that we are linking against the
1594 // local symbol, we can switch to Local-Exec, which links the
1595 // thread offset into the instruction.
1597 return tls::TLSOPT_TO_LE;
1598 return tls::TLSOPT_NONE;
1600 case elfcpp::R_386_TLS_LE:
1601 case elfcpp::R_386_TLS_LE_32:
1602 // When we already have Local-Exec, there is nothing further we
1604 return tls::TLSOPT_NONE;
1611 // Get the Reference_flags for a particular relocation.
1614 Target_i386::Scan::get_reference_flags(unsigned int r_type)
1618 case elfcpp::R_386_NONE:
1619 case elfcpp::R_386_GNU_VTINHERIT:
1620 case elfcpp::R_386_GNU_VTENTRY:
1621 case elfcpp::R_386_GOTPC:
1622 // No symbol reference.
1625 case elfcpp::R_386_32:
1626 case elfcpp::R_386_16:
1627 case elfcpp::R_386_8:
1628 return Symbol::ABSOLUTE_REF;
1630 case elfcpp::R_386_PC32:
1631 case elfcpp::R_386_PC16:
1632 case elfcpp::R_386_PC8:
1633 case elfcpp::R_386_GOTOFF:
1634 return Symbol::RELATIVE_REF;
1636 case elfcpp::R_386_PLT32:
1637 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1639 case elfcpp::R_386_GOT32:
1641 return Symbol::ABSOLUTE_REF;
1643 case elfcpp::R_386_TLS_GD: // Global-dynamic
1644 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1645 case elfcpp::R_386_TLS_DESC_CALL:
1646 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1647 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1648 case elfcpp::R_386_TLS_IE: // Initial-exec
1649 case elfcpp::R_386_TLS_IE_32:
1650 case elfcpp::R_386_TLS_GOTIE:
1651 case elfcpp::R_386_TLS_LE: // Local-exec
1652 case elfcpp::R_386_TLS_LE_32:
1653 return Symbol::TLS_REF;
1655 case elfcpp::R_386_COPY:
1656 case elfcpp::R_386_GLOB_DAT:
1657 case elfcpp::R_386_JUMP_SLOT:
1658 case elfcpp::R_386_RELATIVE:
1659 case elfcpp::R_386_IRELATIVE:
1660 case elfcpp::R_386_TLS_TPOFF:
1661 case elfcpp::R_386_TLS_DTPMOD32:
1662 case elfcpp::R_386_TLS_DTPOFF32:
1663 case elfcpp::R_386_TLS_TPOFF32:
1664 case elfcpp::R_386_TLS_DESC:
1665 case elfcpp::R_386_32PLT:
1666 case elfcpp::R_386_TLS_GD_32:
1667 case elfcpp::R_386_TLS_GD_PUSH:
1668 case elfcpp::R_386_TLS_GD_CALL:
1669 case elfcpp::R_386_TLS_GD_POP:
1670 case elfcpp::R_386_TLS_LDM_32:
1671 case elfcpp::R_386_TLS_LDM_PUSH:
1672 case elfcpp::R_386_TLS_LDM_CALL:
1673 case elfcpp::R_386_TLS_LDM_POP:
1674 case elfcpp::R_386_USED_BY_INTEL_200:
1676 // Not expected. We will give an error later.
1681 // Report an unsupported relocation against a local symbol.
1684 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
1685 unsigned int r_type)
1687 gold_error(_("%s: unsupported reloc %u against local symbol"),
1688 object->name().c_str(), r_type);
1691 // Return whether we need to make a PLT entry for a relocation of a
1692 // given type against a STT_GNU_IFUNC symbol.
1695 Target_i386::Scan::reloc_needs_plt_for_ifunc(
1696 Sized_relobj_file<32, false>* object,
1697 unsigned int r_type)
1699 int flags = Scan::get_reference_flags(r_type);
1700 if (flags & Symbol::TLS_REF)
1701 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1702 object->name().c_str(), r_type);
1706 // Scan a relocation for a local symbol.
1709 Target_i386::Scan::local(Symbol_table* symtab,
1711 Target_i386* target,
1712 Sized_relobj_file<32, false>* object,
1713 unsigned int data_shndx,
1714 Output_section* output_section,
1715 const elfcpp::Rel<32, false>& reloc,
1716 unsigned int r_type,
1717 const elfcpp::Sym<32, false>& lsym,
1723 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1724 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1725 && this->reloc_needs_plt_for_ifunc(object, r_type))
1727 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1728 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1733 case elfcpp::R_386_NONE:
1734 case elfcpp::R_386_GNU_VTINHERIT:
1735 case elfcpp::R_386_GNU_VTENTRY:
1738 case elfcpp::R_386_32:
1739 // If building a shared library (or a position-independent
1740 // executable), we need to create a dynamic relocation for
1741 // this location. The relocation applied at link time will
1742 // apply the link-time value, so we flag the location with
1743 // an R_386_RELATIVE relocation so the dynamic loader can
1744 // relocate it easily.
1745 if (parameters->options().output_is_position_independent())
1747 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1748 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1749 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1750 output_section, data_shndx,
1751 reloc.get_r_offset());
1755 case elfcpp::R_386_16:
1756 case elfcpp::R_386_8:
1757 // If building a shared library (or a position-independent
1758 // executable), we need to create a dynamic relocation for
1759 // this location. Because the addend needs to remain in the
1760 // data section, we need to be careful not to apply this
1761 // relocation statically.
1762 if (parameters->options().output_is_position_independent())
1764 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1765 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1766 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1767 rel_dyn->add_local(object, r_sym, r_type, output_section,
1768 data_shndx, reloc.get_r_offset());
1771 gold_assert(lsym.get_st_value() == 0);
1772 unsigned int shndx = lsym.get_st_shndx();
1774 shndx = object->adjust_sym_shndx(r_sym, shndx,
1777 object->error(_("section symbol %u has bad shndx %u"),
1780 rel_dyn->add_local_section(object, shndx,
1781 r_type, output_section,
1782 data_shndx, reloc.get_r_offset());
1787 case elfcpp::R_386_PC32:
1788 case elfcpp::R_386_PC16:
1789 case elfcpp::R_386_PC8:
1792 case elfcpp::R_386_PLT32:
1793 // Since we know this is a local symbol, we can handle this as a
1797 case elfcpp::R_386_GOTOFF:
1798 case elfcpp::R_386_GOTPC:
1799 // We need a GOT section.
1800 target->got_section(symtab, layout);
1803 case elfcpp::R_386_GOT32:
1805 // The symbol requires a GOT entry.
1806 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1807 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1809 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1810 // lets function pointers compare correctly with shared
1811 // libraries. Otherwise we would need an IRELATIVE reloc.
1813 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1814 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1816 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1819 // If we are generating a shared object, we need to add a
1820 // dynamic RELATIVE relocation for this symbol's GOT entry.
1821 if (parameters->options().output_is_position_independent())
1823 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1824 unsigned int got_offset =
1825 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1826 rel_dyn->add_local_relative(object, r_sym,
1827 elfcpp::R_386_RELATIVE,
1834 // These are relocations which should only be seen by the
1835 // dynamic linker, and should never be seen here.
1836 case elfcpp::R_386_COPY:
1837 case elfcpp::R_386_GLOB_DAT:
1838 case elfcpp::R_386_JUMP_SLOT:
1839 case elfcpp::R_386_RELATIVE:
1840 case elfcpp::R_386_IRELATIVE:
1841 case elfcpp::R_386_TLS_TPOFF:
1842 case elfcpp::R_386_TLS_DTPMOD32:
1843 case elfcpp::R_386_TLS_DTPOFF32:
1844 case elfcpp::R_386_TLS_TPOFF32:
1845 case elfcpp::R_386_TLS_DESC:
1846 gold_error(_("%s: unexpected reloc %u in object file"),
1847 object->name().c_str(), r_type);
1850 // These are initial TLS relocs, which are expected when
1852 case elfcpp::R_386_TLS_GD: // Global-dynamic
1853 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1854 case elfcpp::R_386_TLS_DESC_CALL:
1855 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1856 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1857 case elfcpp::R_386_TLS_IE: // Initial-exec
1858 case elfcpp::R_386_TLS_IE_32:
1859 case elfcpp::R_386_TLS_GOTIE:
1860 case elfcpp::R_386_TLS_LE: // Local-exec
1861 case elfcpp::R_386_TLS_LE_32:
1863 bool output_is_shared = parameters->options().shared();
1864 const tls::Tls_optimization optimized_type
1865 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1868 case elfcpp::R_386_TLS_GD: // Global-dynamic
1869 if (optimized_type == tls::TLSOPT_NONE)
1871 // Create a pair of GOT entries for the module index and
1872 // dtv-relative offset.
1873 Output_data_got<32, false>* got
1874 = target->got_section(symtab, layout);
1875 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1876 unsigned int shndx = lsym.get_st_shndx();
1878 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1880 object->error(_("local symbol %u has bad shndx %u"),
1883 got->add_local_pair_with_rel(object, r_sym, shndx,
1885 target->rel_dyn_section(layout),
1886 elfcpp::R_386_TLS_DTPMOD32);
1888 else if (optimized_type != tls::TLSOPT_TO_LE)
1889 unsupported_reloc_local(object, r_type);
1892 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1893 target->define_tls_base_symbol(symtab, layout);
1894 if (optimized_type == tls::TLSOPT_NONE)
1896 // Create a double GOT entry with an R_386_TLS_DESC
1897 // reloc. The R_386_TLS_DESC reloc is resolved
1898 // lazily, so the GOT entry needs to be in an area in
1899 // .got.plt, not .got. Call got_section to make sure
1900 // the section has been created.
1901 target->got_section(symtab, layout);
1902 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1903 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1904 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1906 unsigned int got_offset = got->add_constant(0);
1907 // The local symbol value is stored in the second
1909 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1910 // That set the GOT offset of the local symbol to
1911 // point to the second entry, but we want it to
1912 // point to the first.
1913 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1915 Reloc_section* rt = target->rel_tls_desc_section(layout);
1916 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1919 else if (optimized_type != tls::TLSOPT_TO_LE)
1920 unsupported_reloc_local(object, r_type);
1923 case elfcpp::R_386_TLS_DESC_CALL:
1926 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1927 if (optimized_type == tls::TLSOPT_NONE)
1929 // Create a GOT entry for the module index.
1930 target->got_mod_index_entry(symtab, layout, object);
1932 else if (optimized_type != tls::TLSOPT_TO_LE)
1933 unsupported_reloc_local(object, r_type);
1936 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1939 case elfcpp::R_386_TLS_IE: // Initial-exec
1940 case elfcpp::R_386_TLS_IE_32:
1941 case elfcpp::R_386_TLS_GOTIE:
1942 layout->set_has_static_tls();
1943 if (optimized_type == tls::TLSOPT_NONE)
1945 // For the R_386_TLS_IE relocation, we need to create a
1946 // dynamic relocation when building a shared library.
1947 if (r_type == elfcpp::R_386_TLS_IE
1948 && parameters->options().shared())
1950 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1952 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1953 rel_dyn->add_local_relative(object, r_sym,
1954 elfcpp::R_386_RELATIVE,
1955 output_section, data_shndx,
1956 reloc.get_r_offset());
1958 // Create a GOT entry for the tp-relative offset.
1959 Output_data_got<32, false>* got
1960 = target->got_section(symtab, layout);
1961 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1962 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1963 ? elfcpp::R_386_TLS_TPOFF32
1964 : elfcpp::R_386_TLS_TPOFF);
1965 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1966 ? GOT_TYPE_TLS_OFFSET
1967 : GOT_TYPE_TLS_NOFFSET);
1968 got->add_local_with_rel(object, r_sym, got_type,
1969 target->rel_dyn_section(layout),
1972 else if (optimized_type != tls::TLSOPT_TO_LE)
1973 unsupported_reloc_local(object, r_type);
1976 case elfcpp::R_386_TLS_LE: // Local-exec
1977 case elfcpp::R_386_TLS_LE_32:
1978 layout->set_has_static_tls();
1979 if (output_is_shared)
1981 // We need to create a dynamic relocation.
1982 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1983 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1984 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1985 ? elfcpp::R_386_TLS_TPOFF32
1986 : elfcpp::R_386_TLS_TPOFF);
1987 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1988 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1989 data_shndx, reloc.get_r_offset());
1999 case elfcpp::R_386_32PLT:
2000 case elfcpp::R_386_TLS_GD_32:
2001 case elfcpp::R_386_TLS_GD_PUSH:
2002 case elfcpp::R_386_TLS_GD_CALL:
2003 case elfcpp::R_386_TLS_GD_POP:
2004 case elfcpp::R_386_TLS_LDM_32:
2005 case elfcpp::R_386_TLS_LDM_PUSH:
2006 case elfcpp::R_386_TLS_LDM_CALL:
2007 case elfcpp::R_386_TLS_LDM_POP:
2008 case elfcpp::R_386_USED_BY_INTEL_200:
2010 unsupported_reloc_local(object, r_type);
2015 // Report an unsupported relocation against a global symbol.
2018 Target_i386::Scan::unsupported_reloc_global(
2019 Sized_relobj_file<32, false>* object,
2020 unsigned int r_type,
2023 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2024 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2028 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
2032 case elfcpp::R_386_32:
2033 case elfcpp::R_386_16:
2034 case elfcpp::R_386_8:
2035 case elfcpp::R_386_GOTOFF:
2036 case elfcpp::R_386_GOT32:
2047 Target_i386::Scan::local_reloc_may_be_function_pointer(
2051 Sized_relobj_file<32, false>* ,
2054 const elfcpp::Rel<32, false>& ,
2055 unsigned int r_type,
2056 const elfcpp::Sym<32, false>&)
2058 return possible_function_pointer_reloc(r_type);
2062 Target_i386::Scan::global_reloc_may_be_function_pointer(
2066 Sized_relobj_file<32, false>* ,
2069 const elfcpp::Rel<32, false>& ,
2070 unsigned int r_type,
2073 return possible_function_pointer_reloc(r_type);
2076 // Scan a relocation for a global symbol.
2079 Target_i386::Scan::global(Symbol_table* symtab,
2081 Target_i386* target,
2082 Sized_relobj_file<32, false>* object,
2083 unsigned int data_shndx,
2084 Output_section* output_section,
2085 const elfcpp::Rel<32, false>& reloc,
2086 unsigned int r_type,
2089 // A STT_GNU_IFUNC symbol may require a PLT entry.
2090 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2091 && this->reloc_needs_plt_for_ifunc(object, r_type))
2092 target->make_plt_entry(symtab, layout, gsym);
2096 case elfcpp::R_386_NONE:
2097 case elfcpp::R_386_GNU_VTINHERIT:
2098 case elfcpp::R_386_GNU_VTENTRY:
2101 case elfcpp::R_386_32:
2102 case elfcpp::R_386_16:
2103 case elfcpp::R_386_8:
2105 // Make a PLT entry if necessary.
2106 if (gsym->needs_plt_entry())
2108 target->make_plt_entry(symtab, layout, gsym);
2109 // Since this is not a PC-relative relocation, we may be
2110 // taking the address of a function. In that case we need to
2111 // set the entry in the dynamic symbol table to the address of
2113 if (gsym->is_from_dynobj() && !parameters->options().shared())
2114 gsym->set_needs_dynsym_value();
2116 // Make a dynamic relocation if necessary.
2117 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2119 if (gsym->may_need_copy_reloc())
2121 target->copy_reloc(symtab, layout, object,
2122 data_shndx, output_section, gsym, reloc);
2124 else if (r_type == elfcpp::R_386_32
2125 && gsym->type() == elfcpp::STT_GNU_IFUNC
2126 && gsym->can_use_relative_reloc(false)
2127 && !gsym->is_from_dynobj()
2128 && !gsym->is_undefined()
2129 && !gsym->is_preemptible())
2131 // Use an IRELATIVE reloc for a locally defined
2132 // STT_GNU_IFUNC symbol. This makes a function
2133 // address in a PIE executable match the address in a
2134 // shared library that it links against.
2135 Reloc_section* rel_dyn = target->rel_irelative_section(layout);
2136 rel_dyn->add_symbolless_global_addend(gsym,
2137 elfcpp::R_386_IRELATIVE,
2140 reloc.get_r_offset());
2142 else if (r_type == elfcpp::R_386_32
2143 && gsym->can_use_relative_reloc(false))
2145 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2146 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2147 output_section, object,
2148 data_shndx, reloc.get_r_offset());
2152 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2153 rel_dyn->add_global(gsym, r_type, output_section, object,
2154 data_shndx, reloc.get_r_offset());
2160 case elfcpp::R_386_PC32:
2161 case elfcpp::R_386_PC16:
2162 case elfcpp::R_386_PC8:
2164 // Make a PLT entry if necessary.
2165 if (gsym->needs_plt_entry())
2167 // These relocations are used for function calls only in
2168 // non-PIC code. For a 32-bit relocation in a shared library,
2169 // we'll need a text relocation anyway, so we can skip the
2170 // PLT entry and let the dynamic linker bind the call directly
2171 // to the target. For smaller relocations, we should use a
2172 // PLT entry to ensure that the call can reach.
2173 if (!parameters->options().shared()
2174 || r_type != elfcpp::R_386_PC32)
2175 target->make_plt_entry(symtab, layout, gsym);
2177 // Make a dynamic relocation if necessary.
2178 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2180 if (gsym->may_need_copy_reloc())
2182 target->copy_reloc(symtab, layout, object,
2183 data_shndx, output_section, gsym, reloc);
2187 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2188 rel_dyn->add_global(gsym, r_type, output_section, object,
2189 data_shndx, reloc.get_r_offset());
2195 case elfcpp::R_386_GOT32:
2197 // The symbol requires a GOT entry.
2198 Output_data_got<32, false>* got = target->got_section(symtab, layout);
2199 if (gsym->final_value_is_known())
2201 // For a STT_GNU_IFUNC symbol we want the PLT address.
2202 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2203 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2205 got->add_global(gsym, GOT_TYPE_STANDARD);
2209 // If this symbol is not fully resolved, we need to add a
2210 // GOT entry with a dynamic relocation.
2211 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2213 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2215 // 1) The symbol may be defined in some other module.
2217 // 2) We are building a shared library and this is a
2218 // protected symbol; using GLOB_DAT means that the dynamic
2219 // linker can use the address of the PLT in the main
2220 // executable when appropriate so that function address
2221 // comparisons work.
2223 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2224 // code, again so that function address comparisons work.
2225 if (gsym->is_from_dynobj()
2226 || gsym->is_undefined()
2227 || gsym->is_preemptible()
2228 || (gsym->visibility() == elfcpp::STV_PROTECTED
2229 && parameters->options().shared())
2230 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2231 && parameters->options().output_is_position_independent()))
2232 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
2233 rel_dyn, elfcpp::R_386_GLOB_DAT);
2236 // For a STT_GNU_IFUNC symbol we want to write the PLT
2237 // offset into the GOT, so that function pointer
2238 // comparisons work correctly.
2240 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2241 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2244 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2245 // Tell the dynamic linker to use the PLT address
2246 // when resolving relocations.
2247 if (gsym->is_from_dynobj()
2248 && !parameters->options().shared())
2249 gsym->set_needs_dynsym_value();
2253 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2254 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2262 case elfcpp::R_386_PLT32:
2263 // If the symbol is fully resolved, this is just a PC32 reloc.
2264 // Otherwise we need a PLT entry.
2265 if (gsym->final_value_is_known())
2267 // If building a shared library, we can also skip the PLT entry
2268 // if the symbol is defined in the output file and is protected
2270 if (gsym->is_defined()
2271 && !gsym->is_from_dynobj()
2272 && !gsym->is_preemptible())
2274 target->make_plt_entry(symtab, layout, gsym);
2277 case elfcpp::R_386_GOTOFF:
2278 case elfcpp::R_386_GOTPC:
2279 // We need a GOT section.
2280 target->got_section(symtab, layout);
2283 // These are relocations which should only be seen by the
2284 // dynamic linker, and should never be seen here.
2285 case elfcpp::R_386_COPY:
2286 case elfcpp::R_386_GLOB_DAT:
2287 case elfcpp::R_386_JUMP_SLOT:
2288 case elfcpp::R_386_RELATIVE:
2289 case elfcpp::R_386_IRELATIVE:
2290 case elfcpp::R_386_TLS_TPOFF:
2291 case elfcpp::R_386_TLS_DTPMOD32:
2292 case elfcpp::R_386_TLS_DTPOFF32:
2293 case elfcpp::R_386_TLS_TPOFF32:
2294 case elfcpp::R_386_TLS_DESC:
2295 gold_error(_("%s: unexpected reloc %u in object file"),
2296 object->name().c_str(), r_type);
2299 // These are initial tls relocs, which are expected when
2301 case elfcpp::R_386_TLS_GD: // Global-dynamic
2302 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2303 case elfcpp::R_386_TLS_DESC_CALL:
2304 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2305 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2306 case elfcpp::R_386_TLS_IE: // Initial-exec
2307 case elfcpp::R_386_TLS_IE_32:
2308 case elfcpp::R_386_TLS_GOTIE:
2309 case elfcpp::R_386_TLS_LE: // Local-exec
2310 case elfcpp::R_386_TLS_LE_32:
2312 const bool is_final = gsym->final_value_is_known();
2313 const tls::Tls_optimization optimized_type
2314 = Target_i386::optimize_tls_reloc(is_final, r_type);
2317 case elfcpp::R_386_TLS_GD: // Global-dynamic
2318 if (optimized_type == tls::TLSOPT_NONE)
2320 // Create a pair of GOT entries for the module index and
2321 // dtv-relative offset.
2322 Output_data_got<32, false>* got
2323 = target->got_section(symtab, layout);
2324 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2325 target->rel_dyn_section(layout),
2326 elfcpp::R_386_TLS_DTPMOD32,
2327 elfcpp::R_386_TLS_DTPOFF32);
2329 else if (optimized_type == tls::TLSOPT_TO_IE)
2331 // Create a GOT entry for the tp-relative offset.
2332 Output_data_got<32, false>* got
2333 = target->got_section(symtab, layout);
2334 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2335 target->rel_dyn_section(layout),
2336 elfcpp::R_386_TLS_TPOFF);
2338 else if (optimized_type != tls::TLSOPT_TO_LE)
2339 unsupported_reloc_global(object, r_type, gsym);
2342 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
2343 target->define_tls_base_symbol(symtab, layout);
2344 if (optimized_type == tls::TLSOPT_NONE)
2346 // Create a double GOT entry with an R_386_TLS_DESC
2347 // reloc. The R_386_TLS_DESC reloc is resolved
2348 // lazily, so the GOT entry needs to be in an area in
2349 // .got.plt, not .got. Call got_section to make sure
2350 // the section has been created.
2351 target->got_section(symtab, layout);
2352 Output_data_got<32, false>* got = target->got_tlsdesc_section();
2353 Reloc_section* rt = target->rel_tls_desc_section(layout);
2354 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
2355 elfcpp::R_386_TLS_DESC, 0);
2357 else if (optimized_type == tls::TLSOPT_TO_IE)
2359 // Create a GOT entry for the tp-relative offset.
2360 Output_data_got<32, false>* got
2361 = target->got_section(symtab, layout);
2362 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2363 target->rel_dyn_section(layout),
2364 elfcpp::R_386_TLS_TPOFF);
2366 else if (optimized_type != tls::TLSOPT_TO_LE)
2367 unsupported_reloc_global(object, r_type, gsym);
2370 case elfcpp::R_386_TLS_DESC_CALL:
2373 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2374 if (optimized_type == tls::TLSOPT_NONE)
2376 // Create a GOT entry for the module index.
2377 target->got_mod_index_entry(symtab, layout, object);
2379 else if (optimized_type != tls::TLSOPT_TO_LE)
2380 unsupported_reloc_global(object, r_type, gsym);
2383 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2386 case elfcpp::R_386_TLS_IE: // Initial-exec
2387 case elfcpp::R_386_TLS_IE_32:
2388 case elfcpp::R_386_TLS_GOTIE:
2389 layout->set_has_static_tls();
2390 if (optimized_type == tls::TLSOPT_NONE)
2392 // For the R_386_TLS_IE relocation, we need to create a
2393 // dynamic relocation when building a shared library.
2394 if (r_type == elfcpp::R_386_TLS_IE
2395 && parameters->options().shared())
2397 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2398 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2399 output_section, object,
2401 reloc.get_r_offset());
2403 // Create a GOT entry for the tp-relative offset.
2404 Output_data_got<32, false>* got
2405 = target->got_section(symtab, layout);
2406 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
2407 ? elfcpp::R_386_TLS_TPOFF32
2408 : elfcpp::R_386_TLS_TPOFF);
2409 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2410 ? GOT_TYPE_TLS_OFFSET
2411 : GOT_TYPE_TLS_NOFFSET);
2412 got->add_global_with_rel(gsym, got_type,
2413 target->rel_dyn_section(layout),
2416 else if (optimized_type != tls::TLSOPT_TO_LE)
2417 unsupported_reloc_global(object, r_type, gsym);
2420 case elfcpp::R_386_TLS_LE: // Local-exec
2421 case elfcpp::R_386_TLS_LE_32:
2422 layout->set_has_static_tls();
2423 if (parameters->options().shared())
2425 // We need to create a dynamic relocation.
2426 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
2427 ? elfcpp::R_386_TLS_TPOFF32
2428 : elfcpp::R_386_TLS_TPOFF);
2429 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2430 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
2431 data_shndx, reloc.get_r_offset());
2441 case elfcpp::R_386_32PLT:
2442 case elfcpp::R_386_TLS_GD_32:
2443 case elfcpp::R_386_TLS_GD_PUSH:
2444 case elfcpp::R_386_TLS_GD_CALL:
2445 case elfcpp::R_386_TLS_GD_POP:
2446 case elfcpp::R_386_TLS_LDM_32:
2447 case elfcpp::R_386_TLS_LDM_PUSH:
2448 case elfcpp::R_386_TLS_LDM_CALL:
2449 case elfcpp::R_386_TLS_LDM_POP:
2450 case elfcpp::R_386_USED_BY_INTEL_200:
2452 unsupported_reloc_global(object, r_type, gsym);
2457 // Process relocations for gc.
2460 Target_i386::gc_process_relocs(Symbol_table* symtab,
2462 Sized_relobj_file<32, false>* object,
2463 unsigned int data_shndx,
2465 const unsigned char* prelocs,
2467 Output_section* output_section,
2468 bool needs_special_offset_handling,
2469 size_t local_symbol_count,
2470 const unsigned char* plocal_symbols)
2472 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2474 Target_i386::Relocatable_size_for_reloc>(
2483 needs_special_offset_handling,
2488 // Scan relocations for a section.
2491 Target_i386::scan_relocs(Symbol_table* symtab,
2493 Sized_relobj_file<32, false>* object,
2494 unsigned int data_shndx,
2495 unsigned int sh_type,
2496 const unsigned char* prelocs,
2498 Output_section* output_section,
2499 bool needs_special_offset_handling,
2500 size_t local_symbol_count,
2501 const unsigned char* plocal_symbols)
2503 if (sh_type == elfcpp::SHT_RELA)
2505 gold_error(_("%s: unsupported RELA reloc section"),
2506 object->name().c_str());
2510 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2520 needs_special_offset_handling,
2525 // Finalize the sections.
2528 Target_i386::do_finalize_sections(
2530 const Input_objects*,
2531 Symbol_table* symtab)
2533 const Reloc_section* rel_plt = (this->plt_ == NULL
2535 : this->plt_->rel_plt());
2536 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2537 this->rel_dyn_, true, false);
2539 // Emit any relocs we saved in an attempt to avoid generating COPY
2541 if (this->copy_relocs_.any_saved_relocs())
2542 this->copy_relocs_.emit(this->rel_dyn_section(layout));
2544 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2545 // the .got.plt section.
2546 Symbol* sym = this->global_offset_table_;
2549 uint32_t data_size = this->got_plt_->current_data_size();
2550 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2553 if (parameters->doing_static_link()
2554 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
2556 // If linking statically, make sure that the __rel_iplt symbols
2557 // were defined if necessary, even if we didn't create a PLT.
2558 static const Define_symbol_in_segment syms[] =
2561 "__rel_iplt_start", // name
2562 elfcpp::PT_LOAD, // segment_type
2563 elfcpp::PF_W, // segment_flags_set
2564 elfcpp::PF(0), // segment_flags_clear
2567 elfcpp::STT_NOTYPE, // type
2568 elfcpp::STB_GLOBAL, // binding
2569 elfcpp::STV_HIDDEN, // visibility
2571 Symbol::SEGMENT_START, // offset_from_base
2575 "__rel_iplt_end", // name
2576 elfcpp::PT_LOAD, // segment_type
2577 elfcpp::PF_W, // segment_flags_set
2578 elfcpp::PF(0), // segment_flags_clear
2581 elfcpp::STT_NOTYPE, // type
2582 elfcpp::STB_GLOBAL, // binding
2583 elfcpp::STV_HIDDEN, // visibility
2585 Symbol::SEGMENT_START, // offset_from_base
2590 symtab->define_symbols(layout, 2, syms,
2591 layout->script_options()->saw_sections_clause());
2595 // Return whether a direct absolute static relocation needs to be applied.
2596 // In cases where Scan::local() or Scan::global() has created
2597 // a dynamic relocation other than R_386_RELATIVE, the addend
2598 // of the relocation is carried in the data, and we must not
2599 // apply the static relocation.
2602 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2603 unsigned int r_type,
2605 Output_section* output_section)
2607 // If the output section is not allocated, then we didn't call
2608 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2610 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2613 int ref_flags = Scan::get_reference_flags(r_type);
2615 // For local symbols, we will have created a non-RELATIVE dynamic
2616 // relocation only if (a) the output is position independent,
2617 // (b) the relocation is absolute (not pc- or segment-relative), and
2618 // (c) the relocation is not 32 bits wide.
2620 return !(parameters->options().output_is_position_independent()
2621 && (ref_flags & Symbol::ABSOLUTE_REF)
2624 // For global symbols, we use the same helper routines used in the
2625 // scan pass. If we did not create a dynamic relocation, or if we
2626 // created a RELATIVE dynamic relocation, we should apply the static
2628 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2629 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2630 && gsym->can_use_relative_reloc(ref_flags
2631 & Symbol::FUNCTION_CALL);
2632 return !has_dyn || is_rel;
2635 // Perform a relocation.
2638 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2639 Target_i386* target,
2640 Output_section* output_section,
2642 const elfcpp::Rel<32, false>& rel,
2643 unsigned int r_type,
2644 const Sized_symbol<32>* gsym,
2645 const Symbol_value<32>* psymval,
2646 unsigned char* view,
2647 elfcpp::Elf_types<32>::Elf_Addr address,
2648 section_size_type view_size)
2650 if (this->skip_call_tls_get_addr_)
2652 if ((r_type != elfcpp::R_386_PLT32
2653 && r_type != elfcpp::R_386_PC32)
2655 || strcmp(gsym->name(), "___tls_get_addr") != 0)
2656 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2657 _("missing expected TLS relocation"));
2660 this->skip_call_tls_get_addr_ = false;
2665 const Sized_relobj_file<32, false>* object = relinfo->object;
2667 // Pick the value to use for symbols defined in shared objects.
2668 Symbol_value<32> symval;
2670 && gsym->type() == elfcpp::STT_GNU_IFUNC
2671 && r_type == elfcpp::R_386_32
2672 && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
2673 && gsym->can_use_relative_reloc(false)
2674 && !gsym->is_from_dynobj()
2675 && !gsym->is_undefined()
2676 && !gsym->is_preemptible())
2678 // In this case we are generating a R_386_IRELATIVE reloc. We
2679 // want to use the real value of the symbol, not the PLT offset.
2681 else if (gsym != NULL
2682 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2684 symval.set_output_value(target->plt_address_for_global(gsym));
2687 else if (gsym == NULL && psymval->is_ifunc_symbol())
2689 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2690 if (object->local_has_plt_offset(r_sym))
2692 symval.set_output_value(target->plt_address_for_local(object, r_sym));
2697 // Get the GOT offset if needed.
2698 // The GOT pointer points to the end of the GOT section.
2699 // We need to subtract the size of the GOT section to get
2700 // the actual offset to use in the relocation.
2701 bool have_got_offset = false;
2702 unsigned int got_offset = 0;
2705 case elfcpp::R_386_GOT32:
2708 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2709 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2710 - target->got_size());
2714 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2715 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2716 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2717 - target->got_size());
2719 have_got_offset = true;
2728 case elfcpp::R_386_NONE:
2729 case elfcpp::R_386_GNU_VTINHERIT:
2730 case elfcpp::R_386_GNU_VTENTRY:
2733 case elfcpp::R_386_32:
2734 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2735 Relocate_functions<32, false>::rel32(view, object, psymval);
2738 case elfcpp::R_386_PC32:
2739 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2740 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2743 case elfcpp::R_386_16:
2744 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2745 Relocate_functions<32, false>::rel16(view, object, psymval);
2748 case elfcpp::R_386_PC16:
2749 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2750 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2753 case elfcpp::R_386_8:
2754 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2755 Relocate_functions<32, false>::rel8(view, object, psymval);
2758 case elfcpp::R_386_PC8:
2759 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2760 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2763 case elfcpp::R_386_PLT32:
2764 gold_assert(gsym == NULL
2765 || gsym->has_plt_offset()
2766 || gsym->final_value_is_known()
2767 || (gsym->is_defined()
2768 && !gsym->is_from_dynobj()
2769 && !gsym->is_preemptible()));
2770 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2773 case elfcpp::R_386_GOT32:
2774 gold_assert(have_got_offset);
2775 Relocate_functions<32, false>::rel32(view, got_offset);
2778 case elfcpp::R_386_GOTOFF:
2780 elfcpp::Elf_types<32>::Elf_Addr value;
2781 value = (psymval->value(object, 0)
2782 - target->got_plt_section()->address());
2783 Relocate_functions<32, false>::rel32(view, value);
2787 case elfcpp::R_386_GOTPC:
2789 elfcpp::Elf_types<32>::Elf_Addr value;
2790 value = target->got_plt_section()->address();
2791 Relocate_functions<32, false>::pcrel32(view, value, address);
2795 case elfcpp::R_386_COPY:
2796 case elfcpp::R_386_GLOB_DAT:
2797 case elfcpp::R_386_JUMP_SLOT:
2798 case elfcpp::R_386_RELATIVE:
2799 case elfcpp::R_386_IRELATIVE:
2800 // These are outstanding tls relocs, which are unexpected when
2802 case elfcpp::R_386_TLS_TPOFF:
2803 case elfcpp::R_386_TLS_DTPMOD32:
2804 case elfcpp::R_386_TLS_DTPOFF32:
2805 case elfcpp::R_386_TLS_TPOFF32:
2806 case elfcpp::R_386_TLS_DESC:
2807 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2808 _("unexpected reloc %u in object file"),
2812 // These are initial tls relocs, which are expected when
2814 case elfcpp::R_386_TLS_GD: // Global-dynamic
2815 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2816 case elfcpp::R_386_TLS_DESC_CALL:
2817 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2818 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2819 case elfcpp::R_386_TLS_IE: // Initial-exec
2820 case elfcpp::R_386_TLS_IE_32:
2821 case elfcpp::R_386_TLS_GOTIE:
2822 case elfcpp::R_386_TLS_LE: // Local-exec
2823 case elfcpp::R_386_TLS_LE_32:
2824 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2825 view, address, view_size);
2828 case elfcpp::R_386_32PLT:
2829 case elfcpp::R_386_TLS_GD_32:
2830 case elfcpp::R_386_TLS_GD_PUSH:
2831 case elfcpp::R_386_TLS_GD_CALL:
2832 case elfcpp::R_386_TLS_GD_POP:
2833 case elfcpp::R_386_TLS_LDM_32:
2834 case elfcpp::R_386_TLS_LDM_PUSH:
2835 case elfcpp::R_386_TLS_LDM_CALL:
2836 case elfcpp::R_386_TLS_LDM_POP:
2837 case elfcpp::R_386_USED_BY_INTEL_200:
2839 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2840 _("unsupported reloc %u"),
2848 // Perform a TLS relocation.
2851 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
2852 Target_i386* target,
2854 const elfcpp::Rel<32, false>& rel,
2855 unsigned int r_type,
2856 const Sized_symbol<32>* gsym,
2857 const Symbol_value<32>* psymval,
2858 unsigned char* view,
2859 elfcpp::Elf_types<32>::Elf_Addr,
2860 section_size_type view_size)
2862 Output_segment* tls_segment = relinfo->layout->tls_segment();
2864 const Sized_relobj_file<32, false>* object = relinfo->object;
2866 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
2868 const bool is_final = (gsym == NULL
2869 ? !parameters->options().shared()
2870 : gsym->final_value_is_known());
2871 const tls::Tls_optimization optimized_type
2872 = Target_i386::optimize_tls_reloc(is_final, r_type);
2875 case elfcpp::R_386_TLS_GD: // Global-dynamic
2876 if (optimized_type == tls::TLSOPT_TO_LE)
2878 if (tls_segment == NULL)
2880 gold_assert(parameters->errors()->error_count() > 0
2881 || issue_undefined_symbol_error(gsym));
2884 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2885 rel, r_type, value, view,
2891 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2892 ? GOT_TYPE_TLS_NOFFSET
2893 : GOT_TYPE_TLS_PAIR);
2894 unsigned int got_offset;
2897 gold_assert(gsym->has_got_offset(got_type));
2898 got_offset = gsym->got_offset(got_type) - target->got_size();
2902 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2903 gold_assert(object->local_has_got_offset(r_sym, got_type));
2904 got_offset = (object->local_got_offset(r_sym, got_type)
2905 - target->got_size());
2907 if (optimized_type == tls::TLSOPT_TO_IE)
2909 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2910 got_offset, view, view_size);
2913 else if (optimized_type == tls::TLSOPT_NONE)
2915 // Relocate the field with the offset of the pair of GOT
2917 Relocate_functions<32, false>::rel32(view, got_offset);
2921 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2922 _("unsupported reloc %u"),
2926 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2927 case elfcpp::R_386_TLS_DESC_CALL:
2928 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2929 if (optimized_type == tls::TLSOPT_TO_LE)
2931 if (tls_segment == NULL)
2933 gold_assert(parameters->errors()->error_count() > 0
2934 || issue_undefined_symbol_error(gsym));
2937 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2938 rel, r_type, value, view,
2944 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2945 ? GOT_TYPE_TLS_NOFFSET
2946 : GOT_TYPE_TLS_DESC);
2947 unsigned int got_offset = 0;
2948 if (r_type == elfcpp::R_386_TLS_GOTDESC
2949 && optimized_type == tls::TLSOPT_NONE)
2951 // We created GOT entries in the .got.tlsdesc portion of
2952 // the .got.plt section, but the offset stored in the
2953 // symbol is the offset within .got.tlsdesc.
2954 got_offset = (target->got_size()
2955 + target->got_plt_section()->data_size());
2959 gold_assert(gsym->has_got_offset(got_type));
2960 got_offset += gsym->got_offset(got_type) - target->got_size();
2964 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2965 gold_assert(object->local_has_got_offset(r_sym, got_type));
2966 got_offset += (object->local_got_offset(r_sym, got_type)
2967 - target->got_size());
2969 if (optimized_type == tls::TLSOPT_TO_IE)
2971 if (tls_segment == NULL)
2973 gold_assert(parameters->errors()->error_count() > 0
2974 || issue_undefined_symbol_error(gsym));
2977 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2978 got_offset, view, view_size);
2981 else if (optimized_type == tls::TLSOPT_NONE)
2983 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2985 // Relocate the field with the offset of the pair of GOT
2987 Relocate_functions<32, false>::rel32(view, got_offset);
2992 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2993 _("unsupported reloc %u"),
2997 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2998 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
3000 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3001 _("both SUN and GNU model "
3002 "TLS relocations"));
3005 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
3006 if (optimized_type == tls::TLSOPT_TO_LE)
3008 if (tls_segment == NULL)
3010 gold_assert(parameters->errors()->error_count() > 0
3011 || issue_undefined_symbol_error(gsym));
3014 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
3015 value, view, view_size);
3018 else if (optimized_type == tls::TLSOPT_NONE)
3020 // Relocate the field with the offset of the GOT entry for
3021 // the module index.
3022 unsigned int got_offset;
3023 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3024 - target->got_size());
3025 Relocate_functions<32, false>::rel32(view, got_offset);
3028 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3029 _("unsupported reloc %u"),
3033 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3034 if (optimized_type == tls::TLSOPT_TO_LE)
3036 // This reloc can appear in debugging sections, in which
3037 // case we must not convert to local-exec. We decide what
3038 // to do based on whether the section is marked as
3039 // containing executable code. That is what the GNU linker
3041 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
3042 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
3044 if (tls_segment == NULL)
3046 gold_assert(parameters->errors()->error_count() > 0
3047 || issue_undefined_symbol_error(gsym));
3050 value -= tls_segment->memsz();
3053 Relocate_functions<32, false>::rel32(view, value);
3056 case elfcpp::R_386_TLS_IE: // Initial-exec
3057 case elfcpp::R_386_TLS_GOTIE:
3058 case elfcpp::R_386_TLS_IE_32:
3059 if (optimized_type == tls::TLSOPT_TO_LE)
3061 if (tls_segment == NULL)
3063 gold_assert(parameters->errors()->error_count() > 0
3064 || issue_undefined_symbol_error(gsym));
3067 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3068 rel, r_type, value, view,
3072 else if (optimized_type == tls::TLSOPT_NONE)
3074 // Relocate the field with the offset of the GOT entry for
3075 // the tp-relative offset of the symbol.
3076 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
3077 ? GOT_TYPE_TLS_OFFSET
3078 : GOT_TYPE_TLS_NOFFSET);
3079 unsigned int got_offset;
3082 gold_assert(gsym->has_got_offset(got_type));
3083 got_offset = gsym->got_offset(got_type);
3087 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3088 gold_assert(object->local_has_got_offset(r_sym, got_type));
3089 got_offset = object->local_got_offset(r_sym, got_type);
3091 // For the R_386_TLS_IE relocation, we need to apply the
3092 // absolute address of the GOT entry.
3093 if (r_type == elfcpp::R_386_TLS_IE)
3094 got_offset += target->got_plt_section()->address();
3095 // All GOT offsets are relative to the end of the GOT.
3096 got_offset -= target->got_size();
3097 Relocate_functions<32, false>::rel32(view, got_offset);
3100 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3101 _("unsupported reloc %u"),
3105 case elfcpp::R_386_TLS_LE: // Local-exec
3106 // If we're creating a shared library, a dynamic relocation will
3107 // have been created for this location, so do not apply it now.
3108 if (!parameters->options().shared())
3110 if (tls_segment == NULL)
3112 gold_assert(parameters->errors()->error_count() > 0
3113 || issue_undefined_symbol_error(gsym));
3116 value -= tls_segment->memsz();
3117 Relocate_functions<32, false>::rel32(view, value);
3121 case elfcpp::R_386_TLS_LE_32:
3122 // If we're creating a shared library, a dynamic relocation will
3123 // have been created for this location, so do not apply it now.
3124 if (!parameters->options().shared())
3126 if (tls_segment == NULL)
3128 gold_assert(parameters->errors()->error_count() > 0
3129 || issue_undefined_symbol_error(gsym));
3132 value = tls_segment->memsz() - value;
3133 Relocate_functions<32, false>::rel32(view, value);
3139 // Do a relocation in which we convert a TLS General-Dynamic to a
3143 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
3145 Output_segment* tls_segment,
3146 const elfcpp::Rel<32, false>& rel,
3148 elfcpp::Elf_types<32>::Elf_Addr value,
3149 unsigned char* view,
3150 section_size_type view_size)
3152 // leal foo(,%reg,1),%eax; call ___tls_get_addr
3153 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
3154 // leal foo(%reg),%eax; call ___tls_get_addr
3155 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
3157 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3158 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3160 unsigned char op1 = view[-1];
3161 unsigned char op2 = view[-2];
3163 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3164 op2 == 0x8d || op2 == 0x04);
3165 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3171 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
3172 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
3173 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3174 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
3175 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3179 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3180 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
3181 if (rel.get_r_offset() + 9 < view_size
3184 // There is a trailing nop. Use the size byte subl.
3185 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3190 // Use the five byte subl.
3191 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
3195 value = tls_segment->memsz() - value;
3196 Relocate_functions<32, false>::rel32(view + roff, value);
3198 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3200 this->skip_call_tls_get_addr_ = true;
3203 // Do a relocation in which we convert a TLS General-Dynamic to an
3207 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
3210 const elfcpp::Rel<32, false>& rel,
3212 elfcpp::Elf_types<32>::Elf_Addr value,
3213 unsigned char* view,
3214 section_size_type view_size)
3216 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
3217 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
3219 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3220 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3222 unsigned char op1 = view[-1];
3223 unsigned char op2 = view[-2];
3225 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3226 op2 == 0x8d || op2 == 0x04);
3227 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3231 // FIXME: For now, support only the first (SIB) form.
3232 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
3236 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
3237 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
3238 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3239 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
3240 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
3244 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3245 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
3246 if (rel.get_r_offset() + 9 < view_size
3249 // FIXME: This is not the right instruction sequence.
3250 // There is a trailing nop. Use the size byte subl.
3251 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3256 // FIXME: This is not the right instruction sequence.
3257 // Use the five byte subl.
3258 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
3262 Relocate_functions<32, false>::rel32(view + roff, value);
3264 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3266 this->skip_call_tls_get_addr_ = true;
3269 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3270 // General-Dynamic to a Local-Exec.
3273 Target_i386::Relocate::tls_desc_gd_to_le(
3274 const Relocate_info<32, false>* relinfo,
3276 Output_segment* tls_segment,
3277 const elfcpp::Rel<32, false>& rel,
3278 unsigned int r_type,
3279 elfcpp::Elf_types<32>::Elf_Addr value,
3280 unsigned char* view,
3281 section_size_type view_size)
3283 if (r_type == elfcpp::R_386_TLS_GOTDESC)
3285 // leal foo@TLSDESC(%ebx), %eax
3286 // ==> leal foo@NTPOFF, %eax
3287 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3288 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3289 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3290 view[-2] == 0x8d && view[-1] == 0x83);
3292 value -= tls_segment->memsz();
3293 Relocate_functions<32, false>::rel32(view, value);
3297 // call *foo@TLSCALL(%eax)
3299 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3300 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3301 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3302 view[0] == 0xff && view[1] == 0x10);
3308 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3309 // General-Dynamic to an Initial-Exec.
3312 Target_i386::Relocate::tls_desc_gd_to_ie(
3313 const Relocate_info<32, false>* relinfo,
3316 const elfcpp::Rel<32, false>& rel,
3317 unsigned int r_type,
3318 elfcpp::Elf_types<32>::Elf_Addr value,
3319 unsigned char* view,
3320 section_size_type view_size)
3322 if (r_type == elfcpp::R_386_TLS_GOTDESC)
3324 // leal foo@TLSDESC(%ebx), %eax
3325 // ==> movl foo@GOTNTPOFF(%ebx), %eax
3326 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3327 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3328 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3329 view[-2] == 0x8d && view[-1] == 0x83);
3331 Relocate_functions<32, false>::rel32(view, value);
3335 // call *foo@TLSCALL(%eax)
3337 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3338 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3339 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3340 view[0] == 0xff && view[1] == 0x10);
3346 // Do a relocation in which we convert a TLS Local-Dynamic to a
3350 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
3353 const elfcpp::Rel<32, false>& rel,
3355 elfcpp::Elf_types<32>::Elf_Addr,
3356 unsigned char* view,
3357 section_size_type view_size)
3359 // leal foo(%reg), %eax; call ___tls_get_addr
3360 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
3362 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3363 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3365 // FIXME: Does this test really always pass?
3366 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3367 view[-2] == 0x8d && view[-1] == 0x83);
3369 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3371 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
3373 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3375 this->skip_call_tls_get_addr_ = true;
3378 // Do a relocation in which we convert a TLS Initial-Exec to a
3382 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
3384 Output_segment* tls_segment,
3385 const elfcpp::Rel<32, false>& rel,
3386 unsigned int r_type,
3387 elfcpp::Elf_types<32>::Elf_Addr value,
3388 unsigned char* view,
3389 section_size_type view_size)
3391 // We have to actually change the instructions, which means that we
3392 // need to examine the opcodes to figure out which instruction we
3394 if (r_type == elfcpp::R_386_TLS_IE)
3396 // movl %gs:XX,%eax ==> movl $YY,%eax
3397 // movl %gs:XX,%reg ==> movl $YY,%reg
3398 // addl %gs:XX,%reg ==> addl $YY,%reg
3399 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
3400 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3402 unsigned char op1 = view[-1];
3405 // movl XX,%eax ==> movl $YY,%eax
3410 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3412 unsigned char op2 = view[-2];
3415 // movl XX,%reg ==> movl $YY,%reg
3416 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3417 (op1 & 0xc7) == 0x05);
3419 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3421 else if (op2 == 0x03)
3423 // addl XX,%reg ==> addl $YY,%reg
3424 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3425 (op1 & 0xc7) == 0x05);
3427 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3430 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3435 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3436 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3437 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3438 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3439 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3441 unsigned char op1 = view[-1];
3442 unsigned char op2 = view[-2];
3443 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3444 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
3447 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3449 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3451 else if (op2 == 0x2b)
3453 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3455 view[-1] = 0xe8 | ((op1 >> 3) & 7);
3457 else if (op2 == 0x03)
3459 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3461 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3464 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3467 value = tls_segment->memsz() - value;
3468 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
3471 Relocate_functions<32, false>::rel32(view, value);
3474 // Relocate section data.
3477 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
3478 unsigned int sh_type,
3479 const unsigned char* prelocs,
3481 Output_section* output_section,
3482 bool needs_special_offset_handling,
3483 unsigned char* view,
3484 elfcpp::Elf_types<32>::Elf_Addr address,
3485 section_size_type view_size,
3486 const Reloc_symbol_changes* reloc_symbol_changes)
3488 gold_assert(sh_type == elfcpp::SHT_REL);
3490 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
3491 Target_i386::Relocate, gold::Default_comdat_behavior>(
3497 needs_special_offset_handling,
3501 reloc_symbol_changes);
3504 // Return the size of a relocation while scanning during a relocatable
3508 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3509 unsigned int r_type,
3514 case elfcpp::R_386_NONE:
3515 case elfcpp::R_386_GNU_VTINHERIT:
3516 case elfcpp::R_386_GNU_VTENTRY:
3517 case elfcpp::R_386_TLS_GD: // Global-dynamic
3518 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
3519 case elfcpp::R_386_TLS_DESC_CALL:
3520 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3521 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3522 case elfcpp::R_386_TLS_IE: // Initial-exec
3523 case elfcpp::R_386_TLS_IE_32:
3524 case elfcpp::R_386_TLS_GOTIE:
3525 case elfcpp::R_386_TLS_LE: // Local-exec
3526 case elfcpp::R_386_TLS_LE_32:
3529 case elfcpp::R_386_32:
3530 case elfcpp::R_386_PC32:
3531 case elfcpp::R_386_GOT32:
3532 case elfcpp::R_386_PLT32:
3533 case elfcpp::R_386_GOTOFF:
3534 case elfcpp::R_386_GOTPC:
3537 case elfcpp::R_386_16:
3538 case elfcpp::R_386_PC16:
3541 case elfcpp::R_386_8:
3542 case elfcpp::R_386_PC8:
3545 // These are relocations which should only be seen by the
3546 // dynamic linker, and should never be seen here.
3547 case elfcpp::R_386_COPY:
3548 case elfcpp::R_386_GLOB_DAT:
3549 case elfcpp::R_386_JUMP_SLOT:
3550 case elfcpp::R_386_RELATIVE:
3551 case elfcpp::R_386_IRELATIVE:
3552 case elfcpp::R_386_TLS_TPOFF:
3553 case elfcpp::R_386_TLS_DTPMOD32:
3554 case elfcpp::R_386_TLS_DTPOFF32:
3555 case elfcpp::R_386_TLS_TPOFF32:
3556 case elfcpp::R_386_TLS_DESC:
3557 object->error(_("unexpected reloc %u in object file"), r_type);
3560 case elfcpp::R_386_32PLT:
3561 case elfcpp::R_386_TLS_GD_32:
3562 case elfcpp::R_386_TLS_GD_PUSH:
3563 case elfcpp::R_386_TLS_GD_CALL:
3564 case elfcpp::R_386_TLS_GD_POP:
3565 case elfcpp::R_386_TLS_LDM_32:
3566 case elfcpp::R_386_TLS_LDM_PUSH:
3567 case elfcpp::R_386_TLS_LDM_CALL:
3568 case elfcpp::R_386_TLS_LDM_POP:
3569 case elfcpp::R_386_USED_BY_INTEL_200:
3571 object->error(_("unsupported reloc %u in object file"), r_type);
3576 // Scan the relocs during a relocatable link.
3579 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3581 Sized_relobj_file<32, false>* object,
3582 unsigned int data_shndx,
3583 unsigned int sh_type,
3584 const unsigned char* prelocs,
3586 Output_section* output_section,
3587 bool needs_special_offset_handling,
3588 size_t local_symbol_count,
3589 const unsigned char* plocal_symbols,
3590 Relocatable_relocs* rr)
3592 gold_assert(sh_type == elfcpp::SHT_REL);
3594 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3595 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3597 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
3598 Scan_relocatable_relocs>(
3606 needs_special_offset_handling,
3612 // Emit relocations for a section.
3615 Target_i386::relocate_relocs(
3616 const Relocate_info<32, false>* relinfo,
3617 unsigned int sh_type,
3618 const unsigned char* prelocs,
3620 Output_section* output_section,
3621 typename elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
3622 const Relocatable_relocs* rr,
3623 unsigned char* view,
3624 elfcpp::Elf_types<32>::Elf_Addr view_address,
3625 section_size_type view_size,
3626 unsigned char* reloc_view,
3627 section_size_type reloc_view_size)
3629 gold_assert(sh_type == elfcpp::SHT_REL);
3631 gold::relocate_relocs<32, false, elfcpp::SHT_REL>(
3636 offset_in_output_section,
3645 // Return the value to use for a dynamic which requires special
3646 // treatment. This is how we support equality comparisons of function
3647 // pointers across shared library boundaries, as described in the
3648 // processor specific ABI supplement.
3651 Target_i386::do_dynsym_value(const Symbol* gsym) const
3653 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3654 return this->plt_address_for_global(gsym);
3657 // Return a string used to fill a code section with nops to take up
3658 // the specified length.
3661 Target_i386::do_code_fill(section_size_type length) const
3665 // Build a jmp instruction to skip over the bytes.
3666 unsigned char jmp[5];
3668 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3669 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3670 + std::string(length - 5, static_cast<char>(0x90)));
3673 // Nop sequences of various lengths.
3674 const char nop1[1] = { '\x90' }; // nop
3675 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
3676 const char nop3[3] = { '\x8d', '\x76', '\x00' }; // leal 0(%esi),%esi
3677 const char nop4[4] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi
3679 const char nop5[5] = { '\x90', '\x8d', '\x74', // nop
3680 '\x26', '\x00' }; // leal 0(%esi,1),%esi
3681 const char nop6[6] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
3682 '\x00', '\x00', '\x00' };
3683 const char nop7[7] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi
3684 '\x00', '\x00', '\x00',
3686 const char nop8[8] = { '\x90', '\x8d', '\xb4', // nop
3687 '\x26', '\x00', '\x00', // leal 0L(%esi,1),%esi
3689 const char nop9[9] = { '\x89', '\xf6', '\x8d', // movl %esi,%esi
3690 '\xbc', '\x27', '\x00', // leal 0L(%edi,1),%edi
3691 '\x00', '\x00', '\x00' };
3692 const char nop10[10] = { '\x8d', '\x76', '\x00', // leal 0(%esi),%esi
3693 '\x8d', '\xbc', '\x27', // leal 0L(%edi,1),%edi
3694 '\x00', '\x00', '\x00',
3696 const char nop11[11] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi
3697 '\x00', '\x8d', '\xbc', // leal 0L(%edi,1),%edi
3698 '\x27', '\x00', '\x00',
3700 const char nop12[12] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
3701 '\x00', '\x00', '\x00', // leal 0L(%edi),%edi
3702 '\x8d', '\xbf', '\x00',
3703 '\x00', '\x00', '\x00' };
3704 const char nop13[13] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
3705 '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi
3706 '\x8d', '\xbc', '\x27',
3707 '\x00', '\x00', '\x00',
3709 const char nop14[14] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi
3710 '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi
3711 '\x00', '\x8d', '\xbc',
3712 '\x27', '\x00', '\x00',
3714 const char nop15[15] = { '\xeb', '\x0d', '\x90', // jmp .+15
3715 '\x90', '\x90', '\x90', // nop,nop,nop,...
3716 '\x90', '\x90', '\x90',
3717 '\x90', '\x90', '\x90',
3718 '\x90', '\x90', '\x90' };
3720 const char* nops[16] = {
3722 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3723 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3726 return std::string(nops[length], length);
3729 // Return the value to use for the base of a DW_EH_PE_datarel offset
3730 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3731 // assembler can not write out the difference between two labels in
3732 // different sections, so instead of using a pc-relative value they
3733 // use an offset from the GOT.
3736 Target_i386::do_ehframe_datarel_base() const
3738 gold_assert(this->global_offset_table_ != NULL);
3739 Symbol* sym = this->global_offset_table_;
3740 Sized_symbol<32>* ssym = static_cast<Sized_symbol<32>*>(sym);
3741 return ssym->value();
3744 // Return whether SYM should be treated as a call to a non-split
3745 // function. We don't want that to be true of a call to a
3746 // get_pc_thunk function.
3749 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
3751 return (sym->type() == elfcpp::STT_FUNC
3752 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3755 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3756 // compiled with -fsplit-stack. The function calls non-split-stack
3757 // code. We have to change the function so that it always ensures
3758 // that it has enough stack space to run some random function.
3761 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3762 section_offset_type fnoffset,
3763 section_size_type fnsize,
3764 unsigned char* view,
3765 section_size_type view_size,
3767 std::string* to) const
3769 // The function starts with a comparison of the stack pointer and a
3770 // field in the TCB. This is followed by a jump.
3773 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3776 // We will call __morestack if the carry flag is set after this
3777 // comparison. We turn the comparison into an stc instruction
3779 view[fnoffset] = '\xf9';
3780 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3782 // lea NN(%esp),%ecx
3783 // lea NN(%esp),%edx
3784 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3785 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
3788 // This is loading an offset from the stack pointer for a
3789 // comparison. The offset is negative, so we decrease the
3790 // offset by the amount of space we need for the stack. This
3791 // means we will avoid calling __morestack if there happens to
3792 // be plenty of space on the stack already.
3793 unsigned char* pval = view + fnoffset + 3;
3794 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3795 val -= parameters->options().split_stack_adjust_size();
3796 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3800 if (!object->has_no_split_stack())
3801 object->error(_("failed to match split-stack sequence at "
3802 "section %u offset %0zx"),
3803 shndx, static_cast<size_t>(fnoffset));
3807 // We have to change the function so that it calls
3808 // __morestack_non_split instead of __morestack. The former will
3809 // allocate additional stack space.
3810 *from = "__morestack";
3811 *to = "__morestack_non_split";
3814 // The selector for i386 object files. Note this is never instantiated
3815 // directly. It's only used in Target_selector_i386_nacl, below.
3817 class Target_selector_i386 : public Target_selector_freebsd
3820 Target_selector_i386()
3821 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
3822 "elf32-i386", "elf32-i386-freebsd",
3827 do_instantiate_target()
3828 { return new Target_i386(); }
3831 // NaCl variant. It uses different PLT contents.
3833 class Output_data_plt_i386_nacl : public Output_data_plt_i386
3836 Output_data_plt_i386_nacl(Layout* layout,
3837 Output_data_space* got_plt,
3838 Output_data_space* got_irelative)
3839 : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative)
3843 virtual unsigned int
3844 do_get_plt_entry_size() const
3845 { return plt_entry_size; }
3848 do_add_eh_frame(Layout* layout)
3850 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
3851 plt_eh_frame_fde, plt_eh_frame_fde_size);
3854 // The size of an entry in the PLT.
3855 static const int plt_entry_size = 64;
3857 // The .eh_frame unwind information for the PLT.
3858 static const int plt_eh_frame_fde_size = 32;
3859 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
3862 class Output_data_plt_i386_nacl_exec : public Output_data_plt_i386_nacl
3865 Output_data_plt_i386_nacl_exec(Layout* layout,
3866 Output_data_space* got_plt,
3867 Output_data_space* got_irelative)
3868 : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
3873 do_fill_first_plt_entry(unsigned char* pov,
3874 elfcpp::Elf_types<32>::Elf_Addr got_address);
3876 virtual unsigned int
3877 do_fill_plt_entry(unsigned char* pov,
3878 elfcpp::Elf_types<32>::Elf_Addr got_address,
3879 unsigned int got_offset,
3880 unsigned int plt_offset,
3881 unsigned int plt_rel_offset);
3884 // The first entry in the PLT for an executable.
3885 static const unsigned char first_plt_entry[plt_entry_size];
3887 // Other entries in the PLT for an executable.
3888 static const unsigned char plt_entry[plt_entry_size];
3891 class Output_data_plt_i386_nacl_dyn : public Output_data_plt_i386_nacl
3894 Output_data_plt_i386_nacl_dyn(Layout* layout,
3895 Output_data_space* got_plt,
3896 Output_data_space* got_irelative)
3897 : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
3902 do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr);
3904 virtual unsigned int
3905 do_fill_plt_entry(unsigned char* pov,
3906 elfcpp::Elf_types<32>::Elf_Addr,
3907 unsigned int got_offset,
3908 unsigned int plt_offset,
3909 unsigned int plt_rel_offset);
3912 // The first entry in the PLT for a shared object.
3913 static const unsigned char first_plt_entry[plt_entry_size];
3915 // Other entries in the PLT for a shared object.
3916 static const unsigned char plt_entry[plt_entry_size];
3919 class Target_i386_nacl : public Target_i386
3923 : Target_i386(&i386_nacl_info)
3927 virtual Output_data_plt_i386*
3928 do_make_data_plt(Layout* layout,
3929 Output_data_space* got_plt,
3930 Output_data_space* got_irelative,
3934 return new Output_data_plt_i386_nacl_dyn(layout, got_plt, got_irelative);
3936 return new Output_data_plt_i386_nacl_exec(layout, got_plt, got_irelative);
3940 static const Target::Target_info i386_nacl_info;
3943 const Target::Target_info Target_i386_nacl::i386_nacl_info =
3946 false, // is_big_endian
3947 elfcpp::EM_386, // machine_code
3948 false, // has_make_symbol
3949 false, // has_resolve
3950 true, // has_code_fill
3951 true, // is_default_stack_executable
3952 true, // can_icf_inline_merge_sections
3954 "/lib/ld-nacl-x86-32.so.1", // dynamic_linker
3955 0x20000, // default_text_segment_address
3956 0x10000, // abi_pagesize (overridable by -z max-page-size)
3957 0x10000, // common_pagesize (overridable by -z common-page-size)
3958 true, // isolate_execinstr
3959 0x10000000, // rosegment_gap
3960 elfcpp::SHN_UNDEF, // small_common_shndx
3961 elfcpp::SHN_UNDEF, // large_common_shndx
3962 0, // small_common_section_flags
3963 0, // large_common_section_flags
3964 NULL, // attributes_section
3965 NULL // attributes_vendor
3968 #define NACLMASK 0xe0 // 32-byte alignment mask
3971 Output_data_plt_i386_nacl_exec::first_plt_entry[plt_entry_size] =
3973 0xff, 0x35, // pushl contents of memory address
3974 0, 0, 0, 0, // replaced with address of .got + 4
3975 0x8b, 0x0d, // movl contents of address, %ecx
3976 0, 0, 0, 0, // replaced with address of .got + 8
3977 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
3978 0xff, 0xe1, // jmp *%ecx
3979 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
3980 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
3981 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
3982 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
3983 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
3984 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
3985 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
3986 0x90, 0x90, 0x90, 0x90, 0x90
3990 Output_data_plt_i386_nacl_exec::do_fill_first_plt_entry(
3992 elfcpp::Elf_types<32>::Elf_Addr got_address)
3994 memcpy(pov, first_plt_entry, plt_entry_size);
3995 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
3996 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
3999 // The first entry in the PLT for a shared object.
4002 Output_data_plt_i386_nacl_dyn::first_plt_entry[plt_entry_size] =
4004 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
4005 0x8b, 0x4b, 0x08, // mov 0x8(%ebx), %ecx
4006 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
4007 0xff, 0xe1, // jmp *%ecx
4008 0x90, 0x90, 0x90, 0x90, 0x90, // nops
4009 0x90, 0x90, 0x90, 0x90, 0x90, // nops
4010 0x90, 0x90, 0x90, 0x90, 0x90, // nops
4011 0x90, 0x90, 0x90, 0x90, 0x90, // nops
4012 0x90, 0x90, 0x90, 0x90, 0x90, // nops
4013 0x90, 0x90, 0x90, 0x90, 0x90, // nops
4014 0x90, 0x90, 0x90, 0x90, 0x90, // nops
4015 0x90, 0x90, 0x90, 0x90, 0x90, // nops
4016 0x90, 0x90, 0x90, 0x90, 0x90, // nops
4017 0x90, 0x90, 0x90, 0x90, 0x90 // nops
4021 Output_data_plt_i386_nacl_dyn::do_fill_first_plt_entry(
4023 elfcpp::Elf_types<32>::Elf_Addr)
4025 memcpy(pov, first_plt_entry, plt_entry_size);
4028 // Subsequent entries in the PLT for an executable.
4031 Output_data_plt_i386_nacl_exec::plt_entry[plt_entry_size] =
4033 0x8b, 0x0d, // movl contents of address, %ecx */
4034 0, 0, 0, 0, // replaced with address of symbol in .got
4035 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
4036 0xff, 0xe1, // jmp *%ecx
4038 // Pad to the next 32-byte boundary with nop instructions.
4040 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4041 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4043 // Lazy GOT entries point here (32-byte aligned).
4044 0x68, // pushl immediate
4045 0, 0, 0, 0, // replaced with offset into relocation table
4046 0xe9, // jmp relative
4047 0, 0, 0, 0, // replaced with offset to start of .plt
4049 // Pad to the next 32-byte boundary with nop instructions.
4050 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4051 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4056 Output_data_plt_i386_nacl_exec::do_fill_plt_entry(
4058 elfcpp::Elf_types<32>::Elf_Addr got_address,
4059 unsigned int got_offset,
4060 unsigned int plt_offset,
4061 unsigned int plt_rel_offset)
4063 memcpy(pov, plt_entry, plt_entry_size);
4064 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4065 got_address + got_offset);
4066 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
4067 elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
4071 // Subsequent entries in the PLT for a shared object.
4074 Output_data_plt_i386_nacl_dyn::plt_entry[plt_entry_size] =
4076 0x8b, 0x8b, // movl offset(%ebx), %ecx
4077 0, 0, 0, 0, // replaced with offset of symbol in .got
4078 0x83, 0xe1, 0xe0, // andl $NACLMASK, %ecx
4079 0xff, 0xe1, // jmp *%ecx
4081 // Pad to the next 32-byte boundary with nop instructions.
4083 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4084 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4086 // Lazy GOT entries point here (32-byte aligned).
4087 0x68, // pushl immediate
4088 0, 0, 0, 0, // replaced with offset into relocation table.
4089 0xe9, // jmp relative
4090 0, 0, 0, 0, // replaced with offset to start of .plt.
4092 // Pad to the next 32-byte boundary with nop instructions.
4093 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4094 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4099 Output_data_plt_i386_nacl_dyn::do_fill_plt_entry(
4101 elfcpp::Elf_types<32>::Elf_Addr,
4102 unsigned int got_offset,
4103 unsigned int plt_offset,
4104 unsigned int plt_rel_offset)
4106 memcpy(pov, plt_entry, plt_entry_size);
4107 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
4108 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
4109 elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
4114 Output_data_plt_i386_nacl::plt_eh_frame_fde[plt_eh_frame_fde_size] =
4116 0, 0, 0, 0, // Replaced with offset to .plt.
4117 0, 0, 0, 0, // Replaced with size of .plt.
4118 0, // Augmentation size.
4119 elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
4120 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
4121 elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
4122 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
4123 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
4124 13, // Block length.
4125 elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
4126 elfcpp::DW_OP_breg8, 0, // Push %eip.
4127 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
4128 elfcpp::DW_OP_and, // & (%eip & 0x3f).
4129 elfcpp::DW_OP_const1u, 37, // Push 0x25.
4130 elfcpp::DW_OP_ge, // >= ((%eip & 0x3f) >= 0x25)
4131 elfcpp::DW_OP_lit2, // Push 2.
4132 elfcpp::DW_OP_shl, // << (((%eip & 0x3f) >= 0x25) << 2)
4133 elfcpp::DW_OP_plus, // + ((((%eip&0x3f)>=0x25)<<2)+%esp+4
4134 elfcpp::DW_CFA_nop, // Align to 32 bytes.
4138 // The selector for i386-nacl object files.
4140 class Target_selector_i386_nacl
4141 : public Target_selector_nacl<Target_selector_i386, Target_i386_nacl>
4144 Target_selector_i386_nacl()
4145 : Target_selector_nacl<Target_selector_i386,
4146 Target_i386_nacl>("x86-32",
4152 Target_selector_i386_nacl target_selector_i386;
4154 } // End anonymous namespace.