1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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 elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
418 const Relocatable_relocs*,
420 elfcpp::Elf_types<32>::Elf_Addr view_address,
421 section_size_type view_size,
422 unsigned char* reloc_view,
423 section_size_type reloc_view_size);
425 // Return a string used to fill a code section with nops.
427 do_code_fill(section_size_type length) const;
429 // Return whether SYM is defined by the ABI.
431 do_is_defined_by_abi(const Symbol* sym) const
432 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
434 // Return whether a symbol name implies a local label. The UnixWare
435 // 2.1 cc generates temporary symbols that start with .X, so we
436 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
437 // If so, we should move the .X recognition into
438 // Target::do_is_local_label_name.
440 do_is_local_label_name(const char* name) const
442 if (name[0] == '.' && name[1] == 'X')
444 return Target::do_is_local_label_name(name);
447 // Return the PLT address to use for a global symbol.
449 do_plt_address_for_global(const Symbol* gsym) const
450 { return this->plt_section()->address_for_global(gsym); }
453 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
454 { return this->plt_section()->address_for_local(relobj, symndx); }
456 // We can tell whether we take the address of a function.
458 do_can_check_for_function_pointers() const
461 // Return the base for a DW_EH_PE_datarel encoding.
463 do_ehframe_datarel_base() const;
465 // Return whether SYM is call to a non-split function.
467 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
469 // Adjust -fsplit-stack code which calls non-split-stack code.
471 do_calls_non_split(Relobj* object, unsigned int shndx,
472 section_offset_type fnoffset, section_size_type fnsize,
473 unsigned char* view, section_size_type view_size,
474 std::string* from, std::string* to) const;
476 // Return the size of the GOT section.
480 gold_assert(this->got_ != NULL);
481 return this->got_->data_size();
484 // Return the number of entries in the GOT.
486 got_entry_count() const
488 if (this->got_ == NULL)
490 return this->got_size() / 4;
493 // Return the number of entries in the PLT.
495 plt_entry_count() const;
497 // Return the offset of the first non-reserved PLT entry.
499 first_plt_entry_offset() const;
501 // Return the size of each PLT entry.
503 plt_entry_size() const;
506 // Instantiate the plt_ member.
507 // This chooses the right PLT flavor for an executable or a shared object.
508 Output_data_plt_i386*
509 make_data_plt(Layout* layout,
510 Output_data_space* got_plt,
511 Output_data_space* got_irelative,
513 { return this->do_make_data_plt(layout, got_plt, got_irelative, dyn); }
515 virtual Output_data_plt_i386*
516 do_make_data_plt(Layout* layout,
517 Output_data_space* got_plt,
518 Output_data_space* got_irelative,
522 return new Output_data_plt_i386_dyn(layout, got_plt, got_irelative);
524 return new Output_data_plt_i386_exec(layout, got_plt, got_irelative);
528 // The class which scans relocations.
533 get_reference_flags(unsigned int r_type);
536 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
537 Sized_relobj_file<32, false>* object,
538 unsigned int data_shndx,
539 Output_section* output_section,
540 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
541 const elfcpp::Sym<32, false>& lsym,
545 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
546 Sized_relobj_file<32, false>* object,
547 unsigned int data_shndx,
548 Output_section* output_section,
549 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
553 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
555 Sized_relobj_file<32, false>* object,
556 unsigned int data_shndx,
557 Output_section* output_section,
558 const elfcpp::Rel<32, false>& reloc,
560 const elfcpp::Sym<32, false>& lsym);
563 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
565 Sized_relobj_file<32, false>* object,
566 unsigned int data_shndx,
567 Output_section* output_section,
568 const elfcpp::Rel<32, false>& reloc,
573 possible_function_pointer_reloc(unsigned int r_type);
576 reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
577 unsigned int r_type);
580 unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
583 unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
587 // The class which implements relocation.
592 : skip_call_tls_get_addr_(false),
593 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
598 if (this->skip_call_tls_get_addr_)
600 // FIXME: This needs to specify the location somehow.
601 gold_error(_("missing expected TLS relocation"));
605 // Return whether the static relocation needs to be applied.
607 should_apply_static_reloc(const Sized_symbol<32>* gsym,
610 Output_section* output_section);
612 // Do a relocation. Return false if the caller should not issue
613 // any warnings about this relocation.
615 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
616 size_t relnum, const elfcpp::Rel<32, false>&,
617 unsigned int r_type, const Sized_symbol<32>*,
618 const Symbol_value<32>*,
619 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
623 // Do a TLS relocation.
625 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
626 size_t relnum, const elfcpp::Rel<32, false>&,
627 unsigned int r_type, const Sized_symbol<32>*,
628 const Symbol_value<32>*,
629 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
632 // Do a TLS General-Dynamic to Initial-Exec transition.
634 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
635 Output_segment* tls_segment,
636 const elfcpp::Rel<32, false>&, unsigned int r_type,
637 elfcpp::Elf_types<32>::Elf_Addr value,
639 section_size_type view_size);
641 // Do a TLS General-Dynamic to Local-Exec transition.
643 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
644 Output_segment* tls_segment,
645 const elfcpp::Rel<32, false>&, unsigned int r_type,
646 elfcpp::Elf_types<32>::Elf_Addr value,
648 section_size_type view_size);
650 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
653 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
654 Output_segment* tls_segment,
655 const elfcpp::Rel<32, false>&, unsigned int r_type,
656 elfcpp::Elf_types<32>::Elf_Addr value,
658 section_size_type view_size);
660 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
663 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
664 Output_segment* tls_segment,
665 const elfcpp::Rel<32, false>&, unsigned int r_type,
666 elfcpp::Elf_types<32>::Elf_Addr value,
668 section_size_type view_size);
670 // Do a TLS Local-Dynamic to Local-Exec transition.
672 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
673 Output_segment* tls_segment,
674 const elfcpp::Rel<32, false>&, unsigned int r_type,
675 elfcpp::Elf_types<32>::Elf_Addr value,
677 section_size_type view_size);
679 // Do a TLS Initial-Exec to Local-Exec transition.
681 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
682 Output_segment* tls_segment,
683 const elfcpp::Rel<32, false>&, unsigned int r_type,
684 elfcpp::Elf_types<32>::Elf_Addr value,
686 section_size_type view_size);
688 // We need to keep track of which type of local dynamic relocation
689 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
690 enum Local_dynamic_type
697 // This is set if we should skip the next reloc, which should be a
698 // PLT32 reloc against ___tls_get_addr.
699 bool skip_call_tls_get_addr_;
700 // The type of local dynamic relocation we have seen in the section
701 // being relocated, if any.
702 Local_dynamic_type local_dynamic_type_;
705 // A class which returns the size required for a relocation type,
706 // used while scanning relocs during a relocatable link.
707 class Relocatable_size_for_reloc
711 get_size_for_reloc(unsigned int, Relobj*);
714 // Adjust TLS relocation type based on the options and whether this
715 // is a local symbol.
716 static tls::Tls_optimization
717 optimize_tls_reloc(bool is_final, int r_type);
719 // Get the GOT section, creating it if necessary.
720 Output_data_got<32, false>*
721 got_section(Symbol_table*, Layout*);
723 // Get the GOT PLT section.
725 got_plt_section() const
727 gold_assert(this->got_plt_ != NULL);
728 return this->got_plt_;
731 // Get the GOT section for TLSDESC entries.
732 Output_data_got<32, false>*
733 got_tlsdesc_section() const
735 gold_assert(this->got_tlsdesc_ != NULL);
736 return this->got_tlsdesc_;
739 // Create the PLT section.
741 make_plt_section(Symbol_table* symtab, Layout* layout);
743 // Create a PLT entry for a global symbol.
745 make_plt_entry(Symbol_table*, Layout*, Symbol*);
747 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
749 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
750 Sized_relobj_file<32, false>* relobj,
751 unsigned int local_sym_index);
753 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
755 define_tls_base_symbol(Symbol_table*, Layout*);
757 // Create a GOT entry for the TLS module index.
759 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
760 Sized_relobj_file<32, false>* object);
762 // Get the PLT section.
763 Output_data_plt_i386*
766 gold_assert(this->plt_ != NULL);
770 // Get the dynamic reloc section, creating it if necessary.
772 rel_dyn_section(Layout*);
774 // Get the section to use for TLS_DESC relocations.
776 rel_tls_desc_section(Layout*) const;
778 // Get the section to use for IRELATIVE relocations.
780 rel_irelative_section(Layout*);
782 // Add a potential copy relocation.
784 copy_reloc(Symbol_table* symtab, Layout* layout,
785 Sized_relobj_file<32, false>* object,
786 unsigned int shndx, Output_section* output_section,
787 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
789 this->copy_relocs_.copy_reloc(symtab, layout,
790 symtab->get_sized_symbol<32>(sym),
791 object, shndx, output_section, reloc,
792 this->rel_dyn_section(layout));
795 // Information about this specific target which we pass to the
796 // general Target structure.
797 static const Target::Target_info i386_info;
799 // The types of GOT entries needed for this platform.
800 // These values are exposed to the ABI in an incremental link.
801 // Do not renumber existing values without changing the version
802 // number of the .gnu_incremental_inputs section.
805 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
806 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
807 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
808 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
809 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
813 Output_data_got<32, false>* got_;
815 Output_data_plt_i386* plt_;
816 // The GOT PLT section.
817 Output_data_space* got_plt_;
818 // The GOT section for IRELATIVE relocations.
819 Output_data_space* got_irelative_;
820 // The GOT section for TLSDESC relocations.
821 Output_data_got<32, false>* got_tlsdesc_;
822 // The _GLOBAL_OFFSET_TABLE_ symbol.
823 Symbol* global_offset_table_;
824 // The dynamic reloc section.
825 Reloc_section* rel_dyn_;
826 // The section to use for IRELATIVE relocs.
827 Reloc_section* rel_irelative_;
828 // Relocs saved to avoid a COPY reloc.
829 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
830 // Space for variables copied with a COPY reloc.
831 Output_data_space* dynbss_;
832 // Offset of the GOT entry for the TLS module index.
833 unsigned int got_mod_index_offset_;
834 // True if the _TLS_MODULE_BASE_ symbol has been defined.
835 bool tls_base_symbol_defined_;
838 const Target::Target_info Target_i386::i386_info =
841 false, // is_big_endian
842 elfcpp::EM_386, // machine_code
843 false, // has_make_symbol
844 false, // has_resolve
845 true, // has_code_fill
846 true, // is_default_stack_executable
847 true, // can_icf_inline_merge_sections
849 "/usr/lib/libc.so.1", // dynamic_linker
850 0x08048000, // default_text_segment_address
851 0x1000, // abi_pagesize (overridable by -z max-page-size)
852 0x1000, // common_pagesize (overridable by -z common-page-size)
853 false, // isolate_execinstr
855 elfcpp::SHN_UNDEF, // small_common_shndx
856 elfcpp::SHN_UNDEF, // large_common_shndx
857 0, // small_common_section_flags
858 0, // large_common_section_flags
859 NULL, // attributes_section
860 NULL // attributes_vendor
863 // Get the GOT section, creating it if necessary.
865 Output_data_got<32, false>*
866 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
868 if (this->got_ == NULL)
870 gold_assert(symtab != NULL && layout != NULL);
872 this->got_ = new Output_data_got<32, false>();
874 // When using -z now, we can treat .got.plt as a relro section.
875 // Without -z now, it is modified after program startup by lazy
877 bool is_got_plt_relro = parameters->options().now();
878 Output_section_order got_order = (is_got_plt_relro
881 Output_section_order got_plt_order = (is_got_plt_relro
883 : ORDER_NON_RELRO_FIRST);
885 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
887 | elfcpp::SHF_WRITE),
888 this->got_, got_order, true);
890 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
891 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
893 | elfcpp::SHF_WRITE),
894 this->got_plt_, got_plt_order,
897 // The first three entries are reserved.
898 this->got_plt_->set_current_data_size(3 * 4);
900 if (!is_got_plt_relro)
902 // Those bytes can go into the relro segment.
903 layout->increase_relro(3 * 4);
906 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
907 this->global_offset_table_ =
908 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
909 Symbol_table::PREDEFINED,
911 0, 0, elfcpp::STT_OBJECT,
913 elfcpp::STV_HIDDEN, 0,
916 // If there are any IRELATIVE relocations, they get GOT entries
917 // in .got.plt after the jump slot relocations.
918 this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT");
919 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
921 | elfcpp::SHF_WRITE),
922 this->got_irelative_,
923 got_plt_order, is_got_plt_relro);
925 // If there are any TLSDESC relocations, they get GOT entries in
926 // .got.plt after the jump slot entries.
927 this->got_tlsdesc_ = new Output_data_got<32, false>();
928 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
930 | elfcpp::SHF_WRITE),
932 got_plt_order, is_got_plt_relro);
938 // Get the dynamic reloc section, creating it if necessary.
940 Target_i386::Reloc_section*
941 Target_i386::rel_dyn_section(Layout* layout)
943 if (this->rel_dyn_ == NULL)
945 gold_assert(layout != NULL);
946 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
947 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
948 elfcpp::SHF_ALLOC, this->rel_dyn_,
949 ORDER_DYNAMIC_RELOCS, false);
951 return this->rel_dyn_;
954 // Get the section to use for IRELATIVE relocs, creating it if
955 // necessary. These go in .rel.dyn, but only after all other dynamic
956 // relocations. They need to follow the other dynamic relocations so
957 // that they can refer to global variables initialized by those
960 Target_i386::Reloc_section*
961 Target_i386::rel_irelative_section(Layout* layout)
963 if (this->rel_irelative_ == NULL)
965 // Make sure we have already create the dynamic reloc section.
966 this->rel_dyn_section(layout);
967 this->rel_irelative_ = new Reloc_section(false);
968 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
969 elfcpp::SHF_ALLOC, this->rel_irelative_,
970 ORDER_DYNAMIC_RELOCS, false);
971 gold_assert(this->rel_dyn_->output_section()
972 == this->rel_irelative_->output_section());
974 return this->rel_irelative_;
977 // Create the PLT section. The ordinary .got section is an argument,
978 // since we need to refer to the start. We also create our own .got
979 // section just for PLT entries.
981 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
983 Output_data_space* got_plt,
984 Output_data_space* got_irelative)
985 : Output_section_data(addralign),
986 layout_(layout), tls_desc_rel_(NULL),
987 irelative_rel_(NULL), got_plt_(got_plt), got_irelative_(got_irelative),
988 count_(0), irelative_count_(0), global_ifuncs_(), local_ifuncs_()
990 this->rel_ = new Reloc_section(false);
991 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
992 elfcpp::SHF_ALLOC, this->rel_,
993 ORDER_DYNAMIC_PLT_RELOCS, false);
997 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
999 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
1000 // linker, and so do we.
1004 // Add an entry to the PLT.
1007 Output_data_plt_i386::add_entry(Symbol_table* symtab, Layout* layout,
1010 gold_assert(!gsym->has_plt_offset());
1012 // Every PLT entry needs a reloc.
1013 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1014 && gsym->can_use_relative_reloc(false))
1016 gsym->set_plt_offset(this->irelative_count_ * this->get_plt_entry_size());
1017 ++this->irelative_count_;
1018 section_offset_type got_offset =
1019 this->got_irelative_->current_data_size();
1020 this->got_irelative_->set_current_data_size(got_offset + 4);
1021 Reloc_section* rel = this->rel_irelative(symtab, layout);
1022 rel->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
1023 this->got_irelative_, got_offset);
1024 struct Global_ifunc gi;
1026 gi.got_offset = got_offset;
1027 this->global_ifuncs_.push_back(gi);
1031 // When setting the PLT offset we skip the initial reserved PLT
1033 gsym->set_plt_offset((this->count_ + 1) * this->get_plt_entry_size());
1037 section_offset_type got_offset = this->got_plt_->current_data_size();
1039 // Every PLT entry needs a GOT entry which points back to the
1040 // PLT entry (this will be changed by the dynamic linker,
1041 // normally lazily when the function is called).
1042 this->got_plt_->set_current_data_size(got_offset + 4);
1044 gsym->set_needs_dynsym_entry();
1045 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
1049 // Note that we don't need to save the symbol. The contents of the
1050 // PLT are independent of which symbols are used. The symbols only
1051 // appear in the relocations.
1054 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1058 Output_data_plt_i386::add_local_ifunc_entry(
1059 Symbol_table* symtab,
1061 Sized_relobj_file<32, false>* relobj,
1062 unsigned int local_sym_index)
1064 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1065 ++this->irelative_count_;
1067 section_offset_type got_offset = this->got_irelative_->current_data_size();
1069 // Every PLT entry needs a GOT entry which points back to the PLT
1071 this->got_irelative_->set_current_data_size(got_offset + 4);
1073 // Every PLT entry needs a reloc.
1074 Reloc_section* rel = this->rel_irelative(symtab, layout);
1075 rel->add_symbolless_local_addend(relobj, local_sym_index,
1076 elfcpp::R_386_IRELATIVE,
1077 this->got_irelative_, got_offset);
1079 struct Local_ifunc li;
1081 li.local_sym_index = local_sym_index;
1082 li.got_offset = got_offset;
1083 this->local_ifuncs_.push_back(li);
1088 // Return where the TLS_DESC relocations should go, creating it if
1089 // necessary. These follow the JUMP_SLOT relocations.
1091 Output_data_plt_i386::Reloc_section*
1092 Output_data_plt_i386::rel_tls_desc(Layout* layout)
1094 if (this->tls_desc_rel_ == NULL)
1096 this->tls_desc_rel_ = new Reloc_section(false);
1097 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1098 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
1099 ORDER_DYNAMIC_PLT_RELOCS, false);
1100 gold_assert(this->tls_desc_rel_->output_section()
1101 == this->rel_->output_section());
1103 return this->tls_desc_rel_;
1106 // Return where the IRELATIVE relocations should go in the PLT. These
1107 // follow the JUMP_SLOT and TLS_DESC relocations.
1109 Output_data_plt_i386::Reloc_section*
1110 Output_data_plt_i386::rel_irelative(Symbol_table* symtab, Layout* layout)
1112 if (this->irelative_rel_ == NULL)
1114 // Make sure we have a place for the TLS_DESC relocations, in
1115 // case we see any later on.
1116 this->rel_tls_desc(layout);
1117 this->irelative_rel_ = new Reloc_section(false);
1118 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1119 elfcpp::SHF_ALLOC, this->irelative_rel_,
1120 ORDER_DYNAMIC_PLT_RELOCS, false);
1121 gold_assert(this->irelative_rel_->output_section()
1122 == this->rel_->output_section());
1124 if (parameters->doing_static_link())
1126 // A statically linked executable will only have a .rel.plt
1127 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
1128 // symbols. The library will use these symbols to locate
1129 // the IRELATIVE relocs at program startup time.
1130 symtab->define_in_output_data("__rel_iplt_start", NULL,
1131 Symbol_table::PREDEFINED,
1132 this->irelative_rel_, 0, 0,
1133 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1134 elfcpp::STV_HIDDEN, 0, false, true);
1135 symtab->define_in_output_data("__rel_iplt_end", NULL,
1136 Symbol_table::PREDEFINED,
1137 this->irelative_rel_, 0, 0,
1138 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1139 elfcpp::STV_HIDDEN, 0, true, true);
1142 return this->irelative_rel_;
1145 // Return the PLT address to use for a global symbol.
1148 Output_data_plt_i386::address_for_global(const Symbol* gsym)
1150 uint64_t offset = 0;
1151 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1152 && gsym->can_use_relative_reloc(false))
1153 offset = (this->count_ + 1) * this->get_plt_entry_size();
1154 return this->address() + offset + gsym->plt_offset();
1157 // Return the PLT address to use for a local symbol. These are always
1158 // IRELATIVE relocs.
1161 Output_data_plt_i386::address_for_local(const Relobj* object,
1164 return (this->address()
1165 + (this->count_ + 1) * this->get_plt_entry_size()
1166 + object->local_plt_offset(r_sym));
1169 // The first entry in the PLT for an executable.
1171 const unsigned char Output_data_plt_i386_exec::first_plt_entry[plt_entry_size] =
1173 0xff, 0x35, // pushl contents of memory address
1174 0, 0, 0, 0, // replaced with address of .got + 4
1175 0xff, 0x25, // jmp indirect
1176 0, 0, 0, 0, // replaced with address of .got + 8
1177 0, 0, 0, 0 // unused
1181 Output_data_plt_i386_exec::do_fill_first_plt_entry(
1183 elfcpp::Elf_types<32>::Elf_Addr got_address)
1185 memcpy(pov, first_plt_entry, plt_entry_size);
1186 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
1187 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
1190 // The first entry in the PLT for a shared object.
1192 const unsigned char Output_data_plt_i386_dyn::first_plt_entry[plt_entry_size] =
1194 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
1195 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
1196 0, 0, 0, 0 // unused
1200 Output_data_plt_i386_dyn::do_fill_first_plt_entry(
1202 elfcpp::Elf_types<32>::Elf_Addr)
1204 memcpy(pov, first_plt_entry, plt_entry_size);
1207 // Subsequent entries in the PLT for an executable.
1209 const unsigned char Output_data_plt_i386_exec::plt_entry[plt_entry_size] =
1211 0xff, 0x25, // jmp indirect
1212 0, 0, 0, 0, // replaced with address of symbol in .got
1213 0x68, // pushl immediate
1214 0, 0, 0, 0, // replaced with offset into relocation table
1215 0xe9, // jmp relative
1216 0, 0, 0, 0 // replaced with offset to start of .plt
1220 Output_data_plt_i386_exec::do_fill_plt_entry(
1222 elfcpp::Elf_types<32>::Elf_Addr got_address,
1223 unsigned int got_offset,
1224 unsigned int plt_offset,
1225 unsigned int plt_rel_offset)
1227 memcpy(pov, plt_entry, plt_entry_size);
1228 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1229 got_address + got_offset);
1230 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1231 elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
1235 // Subsequent entries in the PLT for a shared object.
1237 const unsigned char Output_data_plt_i386_dyn::plt_entry[plt_entry_size] =
1239 0xff, 0xa3, // jmp *offset(%ebx)
1240 0, 0, 0, 0, // replaced with offset of symbol in .got
1241 0x68, // pushl immediate
1242 0, 0, 0, 0, // replaced with offset into relocation table
1243 0xe9, // jmp relative
1244 0, 0, 0, 0 // replaced with offset to start of .plt
1248 Output_data_plt_i386_dyn::do_fill_plt_entry(unsigned char* pov,
1249 elfcpp::Elf_types<32>::Elf_Addr,
1250 unsigned int got_offset,
1251 unsigned int plt_offset,
1252 unsigned int plt_rel_offset)
1254 memcpy(pov, plt_entry, plt_entry_size);
1255 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
1256 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1257 elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
1261 // The .eh_frame unwind information for the PLT.
1264 Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1267 'z', // Augmentation: augmentation size included.
1268 'R', // Augmentation: FDE encoding included.
1269 '\0', // End of augmentation string.
1270 1, // Code alignment factor.
1271 0x7c, // Data alignment factor.
1272 8, // Return address column.
1273 1, // Augmentation size.
1274 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1275 | elfcpp::DW_EH_PE_sdata4),
1276 elfcpp::DW_CFA_def_cfa, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
1277 elfcpp::DW_CFA_offset + 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
1278 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1283 Output_data_plt_i386_standard::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1285 0, 0, 0, 0, // Replaced with offset to .plt.
1286 0, 0, 0, 0, // Replaced with size of .plt.
1287 0, // Augmentation size.
1288 elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
1289 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1290 elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
1291 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1292 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1293 11, // Block length.
1294 elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
1295 elfcpp::DW_OP_breg8, 0, // Push %eip.
1296 elfcpp::DW_OP_lit15, // Push 0xf.
1297 elfcpp::DW_OP_and, // & (%eip & 0xf).
1298 elfcpp::DW_OP_lit11, // Push 0xb.
1299 elfcpp::DW_OP_ge, // >= ((%eip & 0xf) >= 0xb)
1300 elfcpp::DW_OP_lit2, // Push 2.
1301 elfcpp::DW_OP_shl, // << (((%eip & 0xf) >= 0xb) << 2)
1302 elfcpp::DW_OP_plus, // + ((((%eip&0xf)>=0xb)<<2)+%esp+4
1303 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1309 // Write out the PLT. This uses the hand-coded instructions above,
1310 // and adjusts them as needed. This is all specified by the i386 ELF
1311 // Processor Supplement.
1314 Output_data_plt_i386::do_write(Output_file* of)
1316 const off_t offset = this->offset();
1317 const section_size_type oview_size =
1318 convert_to_section_size_type(this->data_size());
1319 unsigned char* const oview = of->get_output_view(offset, oview_size);
1321 const off_t got_file_offset = this->got_plt_->offset();
1322 gold_assert(parameters->incremental_update()
1323 || (got_file_offset + this->got_plt_->data_size()
1324 == this->got_irelative_->offset()));
1325 const section_size_type got_size =
1326 convert_to_section_size_type(this->got_plt_->data_size()
1327 + this->got_irelative_->data_size());
1328 unsigned char* const got_view = of->get_output_view(got_file_offset,
1331 unsigned char* pov = oview;
1333 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
1334 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
1336 this->fill_first_plt_entry(pov, got_address);
1337 pov += this->get_plt_entry_size();
1339 unsigned char* got_pov = got_view;
1341 // The first entry in the GOT is the address of the .dynamic section
1342 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1343 // We saved space for them when we created the section in
1344 // Target_i386::got_section.
1345 Output_section* dynamic = this->layout_->dynamic_section();
1346 uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1347 elfcpp::Swap<32, false>::writeval(got_pov, dynamic_addr);
1349 memset(got_pov, 0, 8);
1352 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1354 unsigned int plt_offset = this->get_plt_entry_size();
1355 unsigned int plt_rel_offset = 0;
1356 unsigned int got_offset = 12;
1357 const unsigned int count = this->count_ + this->irelative_count_;
1358 for (unsigned int i = 0;
1361 pov += this->get_plt_entry_size(),
1363 plt_offset += this->get_plt_entry_size(),
1364 plt_rel_offset += rel_size,
1367 // Set and adjust the PLT entry itself.
1368 unsigned int lazy_offset = this->fill_plt_entry(pov,
1374 // Set the entry in the GOT.
1375 elfcpp::Swap<32, false>::writeval(got_pov,
1376 plt_address + plt_offset + lazy_offset);
1379 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
1380 // the GOT to point to the actual symbol value, rather than point to
1381 // the PLT entry. That will let the dynamic linker call the right
1382 // function when resolving IRELATIVE relocations.
1383 unsigned char* got_irelative_view = got_view + this->got_plt_->data_size();
1384 for (std::vector<Global_ifunc>::const_iterator p =
1385 this->global_ifuncs_.begin();
1386 p != this->global_ifuncs_.end();
1389 const Sized_symbol<32>* ssym =
1390 static_cast<const Sized_symbol<32>*>(p->sym);
1391 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1395 for (std::vector<Local_ifunc>::const_iterator p =
1396 this->local_ifuncs_.begin();
1397 p != this->local_ifuncs_.end();
1400 const Symbol_value<32>* psymval =
1401 p->object->local_symbol(p->local_sym_index);
1402 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1403 psymval->value(p->object, 0));
1406 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1407 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1409 of->write_output_view(offset, oview_size, oview);
1410 of->write_output_view(got_file_offset, got_size, got_view);
1413 // Create the PLT section.
1416 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1418 if (this->plt_ == NULL)
1420 // Create the GOT sections first.
1421 this->got_section(symtab, layout);
1423 const bool dyn = parameters->options().output_is_position_independent();
1424 this->plt_ = this->make_data_plt(layout,
1426 this->got_irelative_,
1429 // Add unwind information if requested.
1430 if (parameters->options().ld_generated_unwind_info())
1431 this->plt_->add_eh_frame(layout);
1433 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1435 | elfcpp::SHF_EXECINSTR),
1436 this->plt_, ORDER_PLT, false);
1438 // Make the sh_info field of .rel.plt point to .plt.
1439 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1440 rel_plt_os->set_info_section(this->plt_->output_section());
1444 // Create a PLT entry for a global symbol.
1447 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1449 if (gsym->has_plt_offset())
1451 if (this->plt_ == NULL)
1452 this->make_plt_section(symtab, layout);
1453 this->plt_->add_entry(symtab, layout, gsym);
1456 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1459 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1460 Sized_relobj_file<32, false>* relobj,
1461 unsigned int local_sym_index)
1463 if (relobj->local_has_plt_offset(local_sym_index))
1465 if (this->plt_ == NULL)
1466 this->make_plt_section(symtab, layout);
1467 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1470 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1473 // Return the number of entries in the PLT.
1476 Target_i386::plt_entry_count() const
1478 if (this->plt_ == NULL)
1480 return this->plt_->entry_count();
1483 // Return the offset of the first non-reserved PLT entry.
1486 Target_i386::first_plt_entry_offset() const
1488 return this->plt_->first_plt_entry_offset();
1491 // Return the size of each PLT entry.
1494 Target_i386::plt_entry_size() const
1496 return this->plt_->get_plt_entry_size();
1499 // Get the section to use for TLS_DESC relocations.
1501 Target_i386::Reloc_section*
1502 Target_i386::rel_tls_desc_section(Layout* layout) const
1504 return this->plt_section()->rel_tls_desc(layout);
1507 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1510 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1512 if (this->tls_base_symbol_defined_)
1515 Output_segment* tls_segment = layout->tls_segment();
1516 if (tls_segment != NULL)
1518 bool is_exec = parameters->options().output_is_executable();
1519 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1520 Symbol_table::PREDEFINED,
1524 elfcpp::STV_HIDDEN, 0,
1526 ? Symbol::SEGMENT_END
1527 : Symbol::SEGMENT_START),
1530 this->tls_base_symbol_defined_ = true;
1533 // Create a GOT entry for the TLS module index.
1536 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1537 Sized_relobj_file<32, false>* object)
1539 if (this->got_mod_index_offset_ == -1U)
1541 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1542 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1543 Output_data_got<32, false>* got = this->got_section(symtab, layout);
1544 unsigned int got_offset = got->add_constant(0);
1545 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1547 got->add_constant(0);
1548 this->got_mod_index_offset_ = got_offset;
1550 return this->got_mod_index_offset_;
1553 // Optimize the TLS relocation type based on what we know about the
1554 // symbol. IS_FINAL is true if the final address of this symbol is
1555 // known at link time.
1557 tls::Tls_optimization
1558 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1560 // If we are generating a shared library, then we can't do anything
1562 if (parameters->options().shared())
1563 return tls::TLSOPT_NONE;
1567 case elfcpp::R_386_TLS_GD:
1568 case elfcpp::R_386_TLS_GOTDESC:
1569 case elfcpp::R_386_TLS_DESC_CALL:
1570 // These are General-Dynamic which permits fully general TLS
1571 // access. Since we know that we are generating an executable,
1572 // we can convert this to Initial-Exec. If we also know that
1573 // this is a local symbol, we can further switch to Local-Exec.
1575 return tls::TLSOPT_TO_LE;
1576 return tls::TLSOPT_TO_IE;
1578 case elfcpp::R_386_TLS_LDM:
1579 // This is Local-Dynamic, which refers to a local symbol in the
1580 // dynamic TLS block. Since we know that we generating an
1581 // executable, we can switch to Local-Exec.
1582 return tls::TLSOPT_TO_LE;
1584 case elfcpp::R_386_TLS_LDO_32:
1585 // Another type of Local-Dynamic relocation.
1586 return tls::TLSOPT_TO_LE;
1588 case elfcpp::R_386_TLS_IE:
1589 case elfcpp::R_386_TLS_GOTIE:
1590 case elfcpp::R_386_TLS_IE_32:
1591 // These are Initial-Exec relocs which get the thread offset
1592 // from the GOT. If we know that we are linking against the
1593 // local symbol, we can switch to Local-Exec, which links the
1594 // thread offset into the instruction.
1596 return tls::TLSOPT_TO_LE;
1597 return tls::TLSOPT_NONE;
1599 case elfcpp::R_386_TLS_LE:
1600 case elfcpp::R_386_TLS_LE_32:
1601 // When we already have Local-Exec, there is nothing further we
1603 return tls::TLSOPT_NONE;
1610 // Get the Reference_flags for a particular relocation.
1613 Target_i386::Scan::get_reference_flags(unsigned int r_type)
1617 case elfcpp::R_386_NONE:
1618 case elfcpp::R_386_GNU_VTINHERIT:
1619 case elfcpp::R_386_GNU_VTENTRY:
1620 case elfcpp::R_386_GOTPC:
1621 // No symbol reference.
1624 case elfcpp::R_386_32:
1625 case elfcpp::R_386_16:
1626 case elfcpp::R_386_8:
1627 return Symbol::ABSOLUTE_REF;
1629 case elfcpp::R_386_PC32:
1630 case elfcpp::R_386_PC16:
1631 case elfcpp::R_386_PC8:
1632 case elfcpp::R_386_GOTOFF:
1633 return Symbol::RELATIVE_REF;
1635 case elfcpp::R_386_PLT32:
1636 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1638 case elfcpp::R_386_GOT32:
1640 return Symbol::ABSOLUTE_REF;
1642 case elfcpp::R_386_TLS_GD: // Global-dynamic
1643 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1644 case elfcpp::R_386_TLS_DESC_CALL:
1645 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1646 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1647 case elfcpp::R_386_TLS_IE: // Initial-exec
1648 case elfcpp::R_386_TLS_IE_32:
1649 case elfcpp::R_386_TLS_GOTIE:
1650 case elfcpp::R_386_TLS_LE: // Local-exec
1651 case elfcpp::R_386_TLS_LE_32:
1652 return Symbol::TLS_REF;
1654 case elfcpp::R_386_COPY:
1655 case elfcpp::R_386_GLOB_DAT:
1656 case elfcpp::R_386_JUMP_SLOT:
1657 case elfcpp::R_386_RELATIVE:
1658 case elfcpp::R_386_IRELATIVE:
1659 case elfcpp::R_386_TLS_TPOFF:
1660 case elfcpp::R_386_TLS_DTPMOD32:
1661 case elfcpp::R_386_TLS_DTPOFF32:
1662 case elfcpp::R_386_TLS_TPOFF32:
1663 case elfcpp::R_386_TLS_DESC:
1664 case elfcpp::R_386_32PLT:
1665 case elfcpp::R_386_TLS_GD_32:
1666 case elfcpp::R_386_TLS_GD_PUSH:
1667 case elfcpp::R_386_TLS_GD_CALL:
1668 case elfcpp::R_386_TLS_GD_POP:
1669 case elfcpp::R_386_TLS_LDM_32:
1670 case elfcpp::R_386_TLS_LDM_PUSH:
1671 case elfcpp::R_386_TLS_LDM_CALL:
1672 case elfcpp::R_386_TLS_LDM_POP:
1673 case elfcpp::R_386_USED_BY_INTEL_200:
1675 // Not expected. We will give an error later.
1680 // Report an unsupported relocation against a local symbol.
1683 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
1684 unsigned int r_type)
1686 gold_error(_("%s: unsupported reloc %u against local symbol"),
1687 object->name().c_str(), r_type);
1690 // Return whether we need to make a PLT entry for a relocation of a
1691 // given type against a STT_GNU_IFUNC symbol.
1694 Target_i386::Scan::reloc_needs_plt_for_ifunc(
1695 Sized_relobj_file<32, false>* object,
1696 unsigned int r_type)
1698 int flags = Scan::get_reference_flags(r_type);
1699 if (flags & Symbol::TLS_REF)
1700 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1701 object->name().c_str(), r_type);
1705 // Scan a relocation for a local symbol.
1708 Target_i386::Scan::local(Symbol_table* symtab,
1710 Target_i386* target,
1711 Sized_relobj_file<32, false>* object,
1712 unsigned int data_shndx,
1713 Output_section* output_section,
1714 const elfcpp::Rel<32, false>& reloc,
1715 unsigned int r_type,
1716 const elfcpp::Sym<32, false>& lsym,
1722 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1723 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1724 && this->reloc_needs_plt_for_ifunc(object, r_type))
1726 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1727 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1732 case elfcpp::R_386_NONE:
1733 case elfcpp::R_386_GNU_VTINHERIT:
1734 case elfcpp::R_386_GNU_VTENTRY:
1737 case elfcpp::R_386_32:
1738 // If building a shared library (or a position-independent
1739 // executable), we need to create a dynamic relocation for
1740 // this location. The relocation applied at link time will
1741 // apply the link-time value, so we flag the location with
1742 // an R_386_RELATIVE relocation so the dynamic loader can
1743 // relocate it easily.
1744 if (parameters->options().output_is_position_independent())
1746 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1747 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1748 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1749 output_section, data_shndx,
1750 reloc.get_r_offset());
1754 case elfcpp::R_386_16:
1755 case elfcpp::R_386_8:
1756 // If building a shared library (or a position-independent
1757 // executable), we need to create a dynamic relocation for
1758 // this location. Because the addend needs to remain in the
1759 // data section, we need to be careful not to apply this
1760 // relocation statically.
1761 if (parameters->options().output_is_position_independent())
1763 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1764 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1765 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1766 rel_dyn->add_local(object, r_sym, r_type, output_section,
1767 data_shndx, reloc.get_r_offset());
1770 gold_assert(lsym.get_st_value() == 0);
1771 unsigned int shndx = lsym.get_st_shndx();
1773 shndx = object->adjust_sym_shndx(r_sym, shndx,
1776 object->error(_("section symbol %u has bad shndx %u"),
1779 rel_dyn->add_local_section(object, shndx,
1780 r_type, output_section,
1781 data_shndx, reloc.get_r_offset());
1786 case elfcpp::R_386_PC32:
1787 case elfcpp::R_386_PC16:
1788 case elfcpp::R_386_PC8:
1791 case elfcpp::R_386_PLT32:
1792 // Since we know this is a local symbol, we can handle this as a
1796 case elfcpp::R_386_GOTOFF:
1797 case elfcpp::R_386_GOTPC:
1798 // We need a GOT section.
1799 target->got_section(symtab, layout);
1802 case elfcpp::R_386_GOT32:
1804 // The symbol requires a GOT entry.
1805 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1806 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1808 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1809 // lets function pointers compare correctly with shared
1810 // libraries. Otherwise we would need an IRELATIVE reloc.
1812 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1813 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1815 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1818 // If we are generating a shared object, we need to add a
1819 // dynamic RELATIVE relocation for this symbol's GOT entry.
1820 if (parameters->options().output_is_position_independent())
1822 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1823 unsigned int got_offset =
1824 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1825 rel_dyn->add_local_relative(object, r_sym,
1826 elfcpp::R_386_RELATIVE,
1833 // These are relocations which should only be seen by the
1834 // dynamic linker, and should never be seen here.
1835 case elfcpp::R_386_COPY:
1836 case elfcpp::R_386_GLOB_DAT:
1837 case elfcpp::R_386_JUMP_SLOT:
1838 case elfcpp::R_386_RELATIVE:
1839 case elfcpp::R_386_IRELATIVE:
1840 case elfcpp::R_386_TLS_TPOFF:
1841 case elfcpp::R_386_TLS_DTPMOD32:
1842 case elfcpp::R_386_TLS_DTPOFF32:
1843 case elfcpp::R_386_TLS_TPOFF32:
1844 case elfcpp::R_386_TLS_DESC:
1845 gold_error(_("%s: unexpected reloc %u in object file"),
1846 object->name().c_str(), r_type);
1849 // These are initial TLS relocs, which are expected when
1851 case elfcpp::R_386_TLS_GD: // Global-dynamic
1852 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1853 case elfcpp::R_386_TLS_DESC_CALL:
1854 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1855 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1856 case elfcpp::R_386_TLS_IE: // Initial-exec
1857 case elfcpp::R_386_TLS_IE_32:
1858 case elfcpp::R_386_TLS_GOTIE:
1859 case elfcpp::R_386_TLS_LE: // Local-exec
1860 case elfcpp::R_386_TLS_LE_32:
1862 bool output_is_shared = parameters->options().shared();
1863 const tls::Tls_optimization optimized_type
1864 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1867 case elfcpp::R_386_TLS_GD: // Global-dynamic
1868 if (optimized_type == tls::TLSOPT_NONE)
1870 // Create a pair of GOT entries for the module index and
1871 // dtv-relative offset.
1872 Output_data_got<32, false>* got
1873 = target->got_section(symtab, layout);
1874 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1875 unsigned int shndx = lsym.get_st_shndx();
1877 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1879 object->error(_("local symbol %u has bad shndx %u"),
1882 got->add_local_pair_with_rel(object, r_sym, shndx,
1884 target->rel_dyn_section(layout),
1885 elfcpp::R_386_TLS_DTPMOD32);
1887 else if (optimized_type != tls::TLSOPT_TO_LE)
1888 unsupported_reloc_local(object, r_type);
1891 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1892 target->define_tls_base_symbol(symtab, layout);
1893 if (optimized_type == tls::TLSOPT_NONE)
1895 // Create a double GOT entry with an R_386_TLS_DESC
1896 // reloc. The R_386_TLS_DESC reloc is resolved
1897 // lazily, so the GOT entry needs to be in an area in
1898 // .got.plt, not .got. Call got_section to make sure
1899 // the section has been created.
1900 target->got_section(symtab, layout);
1901 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1902 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1903 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1905 unsigned int got_offset = got->add_constant(0);
1906 // The local symbol value is stored in the second
1908 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1909 // That set the GOT offset of the local symbol to
1910 // point to the second entry, but we want it to
1911 // point to the first.
1912 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1914 Reloc_section* rt = target->rel_tls_desc_section(layout);
1915 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1918 else if (optimized_type != tls::TLSOPT_TO_LE)
1919 unsupported_reloc_local(object, r_type);
1922 case elfcpp::R_386_TLS_DESC_CALL:
1925 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1926 if (optimized_type == tls::TLSOPT_NONE)
1928 // Create a GOT entry for the module index.
1929 target->got_mod_index_entry(symtab, layout, object);
1931 else if (optimized_type != tls::TLSOPT_TO_LE)
1932 unsupported_reloc_local(object, r_type);
1935 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1938 case elfcpp::R_386_TLS_IE: // Initial-exec
1939 case elfcpp::R_386_TLS_IE_32:
1940 case elfcpp::R_386_TLS_GOTIE:
1941 layout->set_has_static_tls();
1942 if (optimized_type == tls::TLSOPT_NONE)
1944 // For the R_386_TLS_IE relocation, we need to create a
1945 // dynamic relocation when building a shared library.
1946 if (r_type == elfcpp::R_386_TLS_IE
1947 && parameters->options().shared())
1949 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1951 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1952 rel_dyn->add_local_relative(object, r_sym,
1953 elfcpp::R_386_RELATIVE,
1954 output_section, data_shndx,
1955 reloc.get_r_offset());
1957 // Create a GOT entry for the tp-relative offset.
1958 Output_data_got<32, false>* got
1959 = target->got_section(symtab, layout);
1960 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1961 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1962 ? elfcpp::R_386_TLS_TPOFF32
1963 : elfcpp::R_386_TLS_TPOFF);
1964 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1965 ? GOT_TYPE_TLS_OFFSET
1966 : GOT_TYPE_TLS_NOFFSET);
1967 got->add_local_with_rel(object, r_sym, got_type,
1968 target->rel_dyn_section(layout),
1971 else if (optimized_type != tls::TLSOPT_TO_LE)
1972 unsupported_reloc_local(object, r_type);
1975 case elfcpp::R_386_TLS_LE: // Local-exec
1976 case elfcpp::R_386_TLS_LE_32:
1977 layout->set_has_static_tls();
1978 if (output_is_shared)
1980 // We need to create a dynamic relocation.
1981 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1982 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1983 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1984 ? elfcpp::R_386_TLS_TPOFF32
1985 : elfcpp::R_386_TLS_TPOFF);
1986 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1987 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1988 data_shndx, reloc.get_r_offset());
1998 case elfcpp::R_386_32PLT:
1999 case elfcpp::R_386_TLS_GD_32:
2000 case elfcpp::R_386_TLS_GD_PUSH:
2001 case elfcpp::R_386_TLS_GD_CALL:
2002 case elfcpp::R_386_TLS_GD_POP:
2003 case elfcpp::R_386_TLS_LDM_32:
2004 case elfcpp::R_386_TLS_LDM_PUSH:
2005 case elfcpp::R_386_TLS_LDM_CALL:
2006 case elfcpp::R_386_TLS_LDM_POP:
2007 case elfcpp::R_386_USED_BY_INTEL_200:
2009 unsupported_reloc_local(object, r_type);
2014 // Report an unsupported relocation against a global symbol.
2017 Target_i386::Scan::unsupported_reloc_global(
2018 Sized_relobj_file<32, false>* object,
2019 unsigned int r_type,
2022 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2023 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2027 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
2031 case elfcpp::R_386_32:
2032 case elfcpp::R_386_16:
2033 case elfcpp::R_386_8:
2034 case elfcpp::R_386_GOTOFF:
2035 case elfcpp::R_386_GOT32:
2046 Target_i386::Scan::local_reloc_may_be_function_pointer(
2050 Sized_relobj_file<32, false>* ,
2053 const elfcpp::Rel<32, false>& ,
2054 unsigned int r_type,
2055 const elfcpp::Sym<32, false>&)
2057 return possible_function_pointer_reloc(r_type);
2061 Target_i386::Scan::global_reloc_may_be_function_pointer(
2065 Sized_relobj_file<32, false>* ,
2068 const elfcpp::Rel<32, false>& ,
2069 unsigned int r_type,
2072 return possible_function_pointer_reloc(r_type);
2075 // Scan a relocation for a global symbol.
2078 Target_i386::Scan::global(Symbol_table* symtab,
2080 Target_i386* target,
2081 Sized_relobj_file<32, false>* object,
2082 unsigned int data_shndx,
2083 Output_section* output_section,
2084 const elfcpp::Rel<32, false>& reloc,
2085 unsigned int r_type,
2088 // A STT_GNU_IFUNC symbol may require a PLT entry.
2089 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2090 && this->reloc_needs_plt_for_ifunc(object, r_type))
2091 target->make_plt_entry(symtab, layout, gsym);
2095 case elfcpp::R_386_NONE:
2096 case elfcpp::R_386_GNU_VTINHERIT:
2097 case elfcpp::R_386_GNU_VTENTRY:
2100 case elfcpp::R_386_32:
2101 case elfcpp::R_386_16:
2102 case elfcpp::R_386_8:
2104 // Make a PLT entry if necessary.
2105 if (gsym->needs_plt_entry())
2107 target->make_plt_entry(symtab, layout, gsym);
2108 // Since this is not a PC-relative relocation, we may be
2109 // taking the address of a function. In that case we need to
2110 // set the entry in the dynamic symbol table to the address of
2112 if (gsym->is_from_dynobj() && !parameters->options().shared())
2113 gsym->set_needs_dynsym_value();
2115 // Make a dynamic relocation if necessary.
2116 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2118 if (gsym->may_need_copy_reloc())
2120 target->copy_reloc(symtab, layout, object,
2121 data_shndx, output_section, gsym, reloc);
2123 else if (r_type == elfcpp::R_386_32
2124 && gsym->type() == elfcpp::STT_GNU_IFUNC
2125 && gsym->can_use_relative_reloc(false)
2126 && !gsym->is_from_dynobj()
2127 && !gsym->is_undefined()
2128 && !gsym->is_preemptible())
2130 // Use an IRELATIVE reloc for a locally defined
2131 // STT_GNU_IFUNC symbol. This makes a function
2132 // address in a PIE executable match the address in a
2133 // shared library that it links against.
2134 Reloc_section* rel_dyn = target->rel_irelative_section(layout);
2135 rel_dyn->add_symbolless_global_addend(gsym,
2136 elfcpp::R_386_IRELATIVE,
2139 reloc.get_r_offset());
2141 else if (r_type == elfcpp::R_386_32
2142 && gsym->can_use_relative_reloc(false))
2144 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2145 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2146 output_section, object,
2147 data_shndx, reloc.get_r_offset());
2151 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2152 rel_dyn->add_global(gsym, r_type, output_section, object,
2153 data_shndx, reloc.get_r_offset());
2159 case elfcpp::R_386_PC32:
2160 case elfcpp::R_386_PC16:
2161 case elfcpp::R_386_PC8:
2163 // Make a PLT entry if necessary.
2164 if (gsym->needs_plt_entry())
2166 // These relocations are used for function calls only in
2167 // non-PIC code. For a 32-bit relocation in a shared library,
2168 // we'll need a text relocation anyway, so we can skip the
2169 // PLT entry and let the dynamic linker bind the call directly
2170 // to the target. For smaller relocations, we should use a
2171 // PLT entry to ensure that the call can reach.
2172 if (!parameters->options().shared()
2173 || r_type != elfcpp::R_386_PC32)
2174 target->make_plt_entry(symtab, layout, gsym);
2176 // Make a dynamic relocation if necessary.
2177 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2179 if (gsym->may_need_copy_reloc())
2181 target->copy_reloc(symtab, layout, object,
2182 data_shndx, output_section, gsym, reloc);
2186 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2187 rel_dyn->add_global(gsym, r_type, output_section, object,
2188 data_shndx, reloc.get_r_offset());
2194 case elfcpp::R_386_GOT32:
2196 // The symbol requires a GOT entry.
2197 Output_data_got<32, false>* got = target->got_section(symtab, layout);
2198 if (gsym->final_value_is_known())
2200 // For a STT_GNU_IFUNC symbol we want the PLT address.
2201 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2202 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2204 got->add_global(gsym, GOT_TYPE_STANDARD);
2208 // If this symbol is not fully resolved, we need to add a
2209 // GOT entry with a dynamic relocation.
2210 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2212 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2214 // 1) The symbol may be defined in some other module.
2216 // 2) We are building a shared library and this is a
2217 // protected symbol; using GLOB_DAT means that the dynamic
2218 // linker can use the address of the PLT in the main
2219 // executable when appropriate so that function address
2220 // comparisons work.
2222 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2223 // code, again so that function address comparisons work.
2224 if (gsym->is_from_dynobj()
2225 || gsym->is_undefined()
2226 || gsym->is_preemptible()
2227 || (gsym->visibility() == elfcpp::STV_PROTECTED
2228 && parameters->options().shared())
2229 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2230 && parameters->options().output_is_position_independent()))
2231 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
2232 rel_dyn, elfcpp::R_386_GLOB_DAT);
2235 // For a STT_GNU_IFUNC symbol we want to write the PLT
2236 // offset into the GOT, so that function pointer
2237 // comparisons work correctly.
2239 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2240 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2243 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2244 // Tell the dynamic linker to use the PLT address
2245 // when resolving relocations.
2246 if (gsym->is_from_dynobj()
2247 && !parameters->options().shared())
2248 gsym->set_needs_dynsym_value();
2252 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2253 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2261 case elfcpp::R_386_PLT32:
2262 // If the symbol is fully resolved, this is just a PC32 reloc.
2263 // Otherwise we need a PLT entry.
2264 if (gsym->final_value_is_known())
2266 // If building a shared library, we can also skip the PLT entry
2267 // if the symbol is defined in the output file and is protected
2269 if (gsym->is_defined()
2270 && !gsym->is_from_dynobj()
2271 && !gsym->is_preemptible())
2273 target->make_plt_entry(symtab, layout, gsym);
2276 case elfcpp::R_386_GOTOFF:
2277 case elfcpp::R_386_GOTPC:
2278 // We need a GOT section.
2279 target->got_section(symtab, layout);
2282 // These are relocations which should only be seen by the
2283 // dynamic linker, and should never be seen here.
2284 case elfcpp::R_386_COPY:
2285 case elfcpp::R_386_GLOB_DAT:
2286 case elfcpp::R_386_JUMP_SLOT:
2287 case elfcpp::R_386_RELATIVE:
2288 case elfcpp::R_386_IRELATIVE:
2289 case elfcpp::R_386_TLS_TPOFF:
2290 case elfcpp::R_386_TLS_DTPMOD32:
2291 case elfcpp::R_386_TLS_DTPOFF32:
2292 case elfcpp::R_386_TLS_TPOFF32:
2293 case elfcpp::R_386_TLS_DESC:
2294 gold_error(_("%s: unexpected reloc %u in object file"),
2295 object->name().c_str(), r_type);
2298 // These are initial tls relocs, which are expected when
2300 case elfcpp::R_386_TLS_GD: // Global-dynamic
2301 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2302 case elfcpp::R_386_TLS_DESC_CALL:
2303 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2304 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2305 case elfcpp::R_386_TLS_IE: // Initial-exec
2306 case elfcpp::R_386_TLS_IE_32:
2307 case elfcpp::R_386_TLS_GOTIE:
2308 case elfcpp::R_386_TLS_LE: // Local-exec
2309 case elfcpp::R_386_TLS_LE_32:
2311 const bool is_final = gsym->final_value_is_known();
2312 const tls::Tls_optimization optimized_type
2313 = Target_i386::optimize_tls_reloc(is_final, r_type);
2316 case elfcpp::R_386_TLS_GD: // Global-dynamic
2317 if (optimized_type == tls::TLSOPT_NONE)
2319 // Create a pair of GOT entries for the module index and
2320 // dtv-relative offset.
2321 Output_data_got<32, false>* got
2322 = target->got_section(symtab, layout);
2323 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2324 target->rel_dyn_section(layout),
2325 elfcpp::R_386_TLS_DTPMOD32,
2326 elfcpp::R_386_TLS_DTPOFF32);
2328 else if (optimized_type == tls::TLSOPT_TO_IE)
2330 // Create a GOT entry for the tp-relative offset.
2331 Output_data_got<32, false>* got
2332 = target->got_section(symtab, layout);
2333 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2334 target->rel_dyn_section(layout),
2335 elfcpp::R_386_TLS_TPOFF);
2337 else if (optimized_type != tls::TLSOPT_TO_LE)
2338 unsupported_reloc_global(object, r_type, gsym);
2341 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
2342 target->define_tls_base_symbol(symtab, layout);
2343 if (optimized_type == tls::TLSOPT_NONE)
2345 // Create a double GOT entry with an R_386_TLS_DESC
2346 // reloc. The R_386_TLS_DESC reloc is resolved
2347 // lazily, so the GOT entry needs to be in an area in
2348 // .got.plt, not .got. Call got_section to make sure
2349 // the section has been created.
2350 target->got_section(symtab, layout);
2351 Output_data_got<32, false>* got = target->got_tlsdesc_section();
2352 Reloc_section* rt = target->rel_tls_desc_section(layout);
2353 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
2354 elfcpp::R_386_TLS_DESC, 0);
2356 else if (optimized_type == tls::TLSOPT_TO_IE)
2358 // Create a GOT entry for the tp-relative offset.
2359 Output_data_got<32, false>* got
2360 = target->got_section(symtab, layout);
2361 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2362 target->rel_dyn_section(layout),
2363 elfcpp::R_386_TLS_TPOFF);
2365 else if (optimized_type != tls::TLSOPT_TO_LE)
2366 unsupported_reloc_global(object, r_type, gsym);
2369 case elfcpp::R_386_TLS_DESC_CALL:
2372 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2373 if (optimized_type == tls::TLSOPT_NONE)
2375 // Create a GOT entry for the module index.
2376 target->got_mod_index_entry(symtab, layout, object);
2378 else if (optimized_type != tls::TLSOPT_TO_LE)
2379 unsupported_reloc_global(object, r_type, gsym);
2382 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2385 case elfcpp::R_386_TLS_IE: // Initial-exec
2386 case elfcpp::R_386_TLS_IE_32:
2387 case elfcpp::R_386_TLS_GOTIE:
2388 layout->set_has_static_tls();
2389 if (optimized_type == tls::TLSOPT_NONE)
2391 // For the R_386_TLS_IE relocation, we need to create a
2392 // dynamic relocation when building a shared library.
2393 if (r_type == elfcpp::R_386_TLS_IE
2394 && parameters->options().shared())
2396 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2397 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2398 output_section, object,
2400 reloc.get_r_offset());
2402 // Create a GOT entry for the tp-relative offset.
2403 Output_data_got<32, false>* got
2404 = target->got_section(symtab, layout);
2405 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
2406 ? elfcpp::R_386_TLS_TPOFF32
2407 : elfcpp::R_386_TLS_TPOFF);
2408 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2409 ? GOT_TYPE_TLS_OFFSET
2410 : GOT_TYPE_TLS_NOFFSET);
2411 got->add_global_with_rel(gsym, got_type,
2412 target->rel_dyn_section(layout),
2415 else if (optimized_type != tls::TLSOPT_TO_LE)
2416 unsupported_reloc_global(object, r_type, gsym);
2419 case elfcpp::R_386_TLS_LE: // Local-exec
2420 case elfcpp::R_386_TLS_LE_32:
2421 layout->set_has_static_tls();
2422 if (parameters->options().shared())
2424 // We need to create a dynamic relocation.
2425 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
2426 ? elfcpp::R_386_TLS_TPOFF32
2427 : elfcpp::R_386_TLS_TPOFF);
2428 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2429 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
2430 data_shndx, reloc.get_r_offset());
2440 case elfcpp::R_386_32PLT:
2441 case elfcpp::R_386_TLS_GD_32:
2442 case elfcpp::R_386_TLS_GD_PUSH:
2443 case elfcpp::R_386_TLS_GD_CALL:
2444 case elfcpp::R_386_TLS_GD_POP:
2445 case elfcpp::R_386_TLS_LDM_32:
2446 case elfcpp::R_386_TLS_LDM_PUSH:
2447 case elfcpp::R_386_TLS_LDM_CALL:
2448 case elfcpp::R_386_TLS_LDM_POP:
2449 case elfcpp::R_386_USED_BY_INTEL_200:
2451 unsupported_reloc_global(object, r_type, gsym);
2456 // Process relocations for gc.
2459 Target_i386::gc_process_relocs(Symbol_table* symtab,
2461 Sized_relobj_file<32, false>* object,
2462 unsigned int data_shndx,
2464 const unsigned char* prelocs,
2466 Output_section* output_section,
2467 bool needs_special_offset_handling,
2468 size_t local_symbol_count,
2469 const unsigned char* plocal_symbols)
2471 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2473 Target_i386::Relocatable_size_for_reloc>(
2482 needs_special_offset_handling,
2487 // Scan relocations for a section.
2490 Target_i386::scan_relocs(Symbol_table* symtab,
2492 Sized_relobj_file<32, false>* object,
2493 unsigned int data_shndx,
2494 unsigned int sh_type,
2495 const unsigned char* prelocs,
2497 Output_section* output_section,
2498 bool needs_special_offset_handling,
2499 size_t local_symbol_count,
2500 const unsigned char* plocal_symbols)
2502 if (sh_type == elfcpp::SHT_RELA)
2504 gold_error(_("%s: unsupported RELA reloc section"),
2505 object->name().c_str());
2509 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2519 needs_special_offset_handling,
2524 // Finalize the sections.
2527 Target_i386::do_finalize_sections(
2529 const Input_objects*,
2530 Symbol_table* symtab)
2532 const Reloc_section* rel_plt = (this->plt_ == NULL
2534 : this->plt_->rel_plt());
2535 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2536 this->rel_dyn_, true, false);
2538 // Emit any relocs we saved in an attempt to avoid generating COPY
2540 if (this->copy_relocs_.any_saved_relocs())
2541 this->copy_relocs_.emit(this->rel_dyn_section(layout));
2543 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2544 // the .got.plt section.
2545 Symbol* sym = this->global_offset_table_;
2548 uint32_t data_size = this->got_plt_->current_data_size();
2549 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2552 if (parameters->doing_static_link()
2553 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
2555 // If linking statically, make sure that the __rel_iplt symbols
2556 // were defined if necessary, even if we didn't create a PLT.
2557 static const Define_symbol_in_segment syms[] =
2560 "__rel_iplt_start", // name
2561 elfcpp::PT_LOAD, // segment_type
2562 elfcpp::PF_W, // segment_flags_set
2563 elfcpp::PF(0), // segment_flags_clear
2566 elfcpp::STT_NOTYPE, // type
2567 elfcpp::STB_GLOBAL, // binding
2568 elfcpp::STV_HIDDEN, // visibility
2570 Symbol::SEGMENT_START, // offset_from_base
2574 "__rel_iplt_end", // name
2575 elfcpp::PT_LOAD, // segment_type
2576 elfcpp::PF_W, // segment_flags_set
2577 elfcpp::PF(0), // segment_flags_clear
2580 elfcpp::STT_NOTYPE, // type
2581 elfcpp::STB_GLOBAL, // binding
2582 elfcpp::STV_HIDDEN, // visibility
2584 Symbol::SEGMENT_START, // offset_from_base
2589 symtab->define_symbols(layout, 2, syms,
2590 layout->script_options()->saw_sections_clause());
2594 // Return whether a direct absolute static relocation needs to be applied.
2595 // In cases where Scan::local() or Scan::global() has created
2596 // a dynamic relocation other than R_386_RELATIVE, the addend
2597 // of the relocation is carried in the data, and we must not
2598 // apply the static relocation.
2601 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2602 unsigned int r_type,
2604 Output_section* output_section)
2606 // If the output section is not allocated, then we didn't call
2607 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2609 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2612 int ref_flags = Scan::get_reference_flags(r_type);
2614 // For local symbols, we will have created a non-RELATIVE dynamic
2615 // relocation only if (a) the output is position independent,
2616 // (b) the relocation is absolute (not pc- or segment-relative), and
2617 // (c) the relocation is not 32 bits wide.
2619 return !(parameters->options().output_is_position_independent()
2620 && (ref_flags & Symbol::ABSOLUTE_REF)
2623 // For global symbols, we use the same helper routines used in the
2624 // scan pass. If we did not create a dynamic relocation, or if we
2625 // created a RELATIVE dynamic relocation, we should apply the static
2627 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2628 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2629 && gsym->can_use_relative_reloc(ref_flags
2630 & Symbol::FUNCTION_CALL);
2631 return !has_dyn || is_rel;
2634 // Perform a relocation.
2637 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2638 Target_i386* target,
2639 Output_section* output_section,
2641 const elfcpp::Rel<32, false>& rel,
2642 unsigned int r_type,
2643 const Sized_symbol<32>* gsym,
2644 const Symbol_value<32>* psymval,
2645 unsigned char* view,
2646 elfcpp::Elf_types<32>::Elf_Addr address,
2647 section_size_type view_size)
2649 if (this->skip_call_tls_get_addr_)
2651 if ((r_type != elfcpp::R_386_PLT32
2652 && r_type != elfcpp::R_386_PC32)
2654 || strcmp(gsym->name(), "___tls_get_addr") != 0)
2655 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2656 _("missing expected TLS relocation"));
2659 this->skip_call_tls_get_addr_ = false;
2667 const Sized_relobj_file<32, false>* object = relinfo->object;
2669 // Pick the value to use for symbols defined in shared objects.
2670 Symbol_value<32> symval;
2672 && gsym->type() == elfcpp::STT_GNU_IFUNC
2673 && r_type == elfcpp::R_386_32
2674 && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
2675 && gsym->can_use_relative_reloc(false)
2676 && !gsym->is_from_dynobj()
2677 && !gsym->is_undefined()
2678 && !gsym->is_preemptible())
2680 // In this case we are generating a R_386_IRELATIVE reloc. We
2681 // want to use the real value of the symbol, not the PLT offset.
2683 else if (gsym != NULL
2684 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2686 symval.set_output_value(target->plt_address_for_global(gsym));
2689 else if (gsym == NULL && psymval->is_ifunc_symbol())
2691 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2692 if (object->local_has_plt_offset(r_sym))
2694 symval.set_output_value(target->plt_address_for_local(object, r_sym));
2699 // Get the GOT offset if needed.
2700 // The GOT pointer points to the end of the GOT section.
2701 // We need to subtract the size of the GOT section to get
2702 // the actual offset to use in the relocation.
2703 bool have_got_offset = false;
2704 unsigned int got_offset = 0;
2707 case elfcpp::R_386_GOT32:
2710 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2711 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2712 - target->got_size());
2716 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2717 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2718 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2719 - target->got_size());
2721 have_got_offset = true;
2730 case elfcpp::R_386_NONE:
2731 case elfcpp::R_386_GNU_VTINHERIT:
2732 case elfcpp::R_386_GNU_VTENTRY:
2735 case elfcpp::R_386_32:
2736 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2737 Relocate_functions<32, false>::rel32(view, object, psymval);
2740 case elfcpp::R_386_PC32:
2741 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2742 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2745 case elfcpp::R_386_16:
2746 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2747 Relocate_functions<32, false>::rel16(view, object, psymval);
2750 case elfcpp::R_386_PC16:
2751 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2752 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2755 case elfcpp::R_386_8:
2756 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2757 Relocate_functions<32, false>::rel8(view, object, psymval);
2760 case elfcpp::R_386_PC8:
2761 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2762 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2765 case elfcpp::R_386_PLT32:
2766 gold_assert(gsym == NULL
2767 || gsym->has_plt_offset()
2768 || gsym->final_value_is_known()
2769 || (gsym->is_defined()
2770 && !gsym->is_from_dynobj()
2771 && !gsym->is_preemptible()));
2772 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2775 case elfcpp::R_386_GOT32:
2776 gold_assert(have_got_offset);
2777 Relocate_functions<32, false>::rel32(view, got_offset);
2780 case elfcpp::R_386_GOTOFF:
2782 elfcpp::Elf_types<32>::Elf_Addr value;
2783 value = (psymval->value(object, 0)
2784 - target->got_plt_section()->address());
2785 Relocate_functions<32, false>::rel32(view, value);
2789 case elfcpp::R_386_GOTPC:
2791 elfcpp::Elf_types<32>::Elf_Addr value;
2792 value = target->got_plt_section()->address();
2793 Relocate_functions<32, false>::pcrel32(view, value, address);
2797 case elfcpp::R_386_COPY:
2798 case elfcpp::R_386_GLOB_DAT:
2799 case elfcpp::R_386_JUMP_SLOT:
2800 case elfcpp::R_386_RELATIVE:
2801 case elfcpp::R_386_IRELATIVE:
2802 // These are outstanding tls relocs, which are unexpected when
2804 case elfcpp::R_386_TLS_TPOFF:
2805 case elfcpp::R_386_TLS_DTPMOD32:
2806 case elfcpp::R_386_TLS_DTPOFF32:
2807 case elfcpp::R_386_TLS_TPOFF32:
2808 case elfcpp::R_386_TLS_DESC:
2809 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2810 _("unexpected reloc %u in object file"),
2814 // These are initial tls relocs, which are expected when
2816 case elfcpp::R_386_TLS_GD: // Global-dynamic
2817 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2818 case elfcpp::R_386_TLS_DESC_CALL:
2819 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2820 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2821 case elfcpp::R_386_TLS_IE: // Initial-exec
2822 case elfcpp::R_386_TLS_IE_32:
2823 case elfcpp::R_386_TLS_GOTIE:
2824 case elfcpp::R_386_TLS_LE: // Local-exec
2825 case elfcpp::R_386_TLS_LE_32:
2826 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2827 view, address, view_size);
2830 case elfcpp::R_386_32PLT:
2831 case elfcpp::R_386_TLS_GD_32:
2832 case elfcpp::R_386_TLS_GD_PUSH:
2833 case elfcpp::R_386_TLS_GD_CALL:
2834 case elfcpp::R_386_TLS_GD_POP:
2835 case elfcpp::R_386_TLS_LDM_32:
2836 case elfcpp::R_386_TLS_LDM_PUSH:
2837 case elfcpp::R_386_TLS_LDM_CALL:
2838 case elfcpp::R_386_TLS_LDM_POP:
2839 case elfcpp::R_386_USED_BY_INTEL_200:
2841 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2842 _("unsupported reloc %u"),
2850 // Perform a TLS relocation.
2853 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
2854 Target_i386* target,
2856 const elfcpp::Rel<32, false>& rel,
2857 unsigned int r_type,
2858 const Sized_symbol<32>* gsym,
2859 const Symbol_value<32>* psymval,
2860 unsigned char* view,
2861 elfcpp::Elf_types<32>::Elf_Addr,
2862 section_size_type view_size)
2864 Output_segment* tls_segment = relinfo->layout->tls_segment();
2866 const Sized_relobj_file<32, false>* object = relinfo->object;
2868 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
2870 const bool is_final = (gsym == NULL
2871 ? !parameters->options().shared()
2872 : gsym->final_value_is_known());
2873 const tls::Tls_optimization optimized_type
2874 = Target_i386::optimize_tls_reloc(is_final, r_type);
2877 case elfcpp::R_386_TLS_GD: // Global-dynamic
2878 if (optimized_type == tls::TLSOPT_TO_LE)
2880 if (tls_segment == NULL)
2882 gold_assert(parameters->errors()->error_count() > 0
2883 || issue_undefined_symbol_error(gsym));
2886 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2887 rel, r_type, value, view,
2893 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2894 ? GOT_TYPE_TLS_NOFFSET
2895 : GOT_TYPE_TLS_PAIR);
2896 unsigned int got_offset;
2899 gold_assert(gsym->has_got_offset(got_type));
2900 got_offset = gsym->got_offset(got_type) - target->got_size();
2904 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2905 gold_assert(object->local_has_got_offset(r_sym, got_type));
2906 got_offset = (object->local_got_offset(r_sym, got_type)
2907 - target->got_size());
2909 if (optimized_type == tls::TLSOPT_TO_IE)
2911 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2912 got_offset, view, view_size);
2915 else if (optimized_type == tls::TLSOPT_NONE)
2917 // Relocate the field with the offset of the pair of GOT
2919 Relocate_functions<32, false>::rel32(view, got_offset);
2923 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2924 _("unsupported reloc %u"),
2928 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2929 case elfcpp::R_386_TLS_DESC_CALL:
2930 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2931 if (optimized_type == tls::TLSOPT_TO_LE)
2933 if (tls_segment == NULL)
2935 gold_assert(parameters->errors()->error_count() > 0
2936 || issue_undefined_symbol_error(gsym));
2939 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2940 rel, r_type, value, view,
2946 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2947 ? GOT_TYPE_TLS_NOFFSET
2948 : GOT_TYPE_TLS_DESC);
2949 unsigned int got_offset = 0;
2950 if (r_type == elfcpp::R_386_TLS_GOTDESC
2951 && optimized_type == tls::TLSOPT_NONE)
2953 // We created GOT entries in the .got.tlsdesc portion of
2954 // the .got.plt section, but the offset stored in the
2955 // symbol is the offset within .got.tlsdesc.
2956 got_offset = (target->got_size()
2957 + target->got_plt_section()->data_size());
2961 gold_assert(gsym->has_got_offset(got_type));
2962 got_offset += gsym->got_offset(got_type) - target->got_size();
2966 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2967 gold_assert(object->local_has_got_offset(r_sym, got_type));
2968 got_offset += (object->local_got_offset(r_sym, got_type)
2969 - target->got_size());
2971 if (optimized_type == tls::TLSOPT_TO_IE)
2973 if (tls_segment == NULL)
2975 gold_assert(parameters->errors()->error_count() > 0
2976 || issue_undefined_symbol_error(gsym));
2979 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2980 got_offset, view, view_size);
2983 else if (optimized_type == tls::TLSOPT_NONE)
2985 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2987 // Relocate the field with the offset of the pair of GOT
2989 Relocate_functions<32, false>::rel32(view, got_offset);
2994 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2995 _("unsupported reloc %u"),
2999 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3000 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
3002 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3003 _("both SUN and GNU model "
3004 "TLS relocations"));
3007 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
3008 if (optimized_type == tls::TLSOPT_TO_LE)
3010 if (tls_segment == NULL)
3012 gold_assert(parameters->errors()->error_count() > 0
3013 || issue_undefined_symbol_error(gsym));
3016 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
3017 value, view, view_size);
3020 else if (optimized_type == tls::TLSOPT_NONE)
3022 // Relocate the field with the offset of the GOT entry for
3023 // the module index.
3024 unsigned int got_offset;
3025 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3026 - target->got_size());
3027 Relocate_functions<32, false>::rel32(view, got_offset);
3030 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3031 _("unsupported reloc %u"),
3035 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3036 if (optimized_type == tls::TLSOPT_TO_LE)
3038 // This reloc can appear in debugging sections, in which
3039 // case we must not convert to local-exec. We decide what
3040 // to do based on whether the section is marked as
3041 // containing executable code. That is what the GNU linker
3043 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
3044 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
3046 if (tls_segment == NULL)
3048 gold_assert(parameters->errors()->error_count() > 0
3049 || issue_undefined_symbol_error(gsym));
3052 value -= tls_segment->memsz();
3055 Relocate_functions<32, false>::rel32(view, value);
3058 case elfcpp::R_386_TLS_IE: // Initial-exec
3059 case elfcpp::R_386_TLS_GOTIE:
3060 case elfcpp::R_386_TLS_IE_32:
3061 if (optimized_type == tls::TLSOPT_TO_LE)
3063 if (tls_segment == NULL)
3065 gold_assert(parameters->errors()->error_count() > 0
3066 || issue_undefined_symbol_error(gsym));
3069 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3070 rel, r_type, value, view,
3074 else if (optimized_type == tls::TLSOPT_NONE)
3076 // Relocate the field with the offset of the GOT entry for
3077 // the tp-relative offset of the symbol.
3078 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
3079 ? GOT_TYPE_TLS_OFFSET
3080 : GOT_TYPE_TLS_NOFFSET);
3081 unsigned int got_offset;
3084 gold_assert(gsym->has_got_offset(got_type));
3085 got_offset = gsym->got_offset(got_type);
3089 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3090 gold_assert(object->local_has_got_offset(r_sym, got_type));
3091 got_offset = object->local_got_offset(r_sym, got_type);
3093 // For the R_386_TLS_IE relocation, we need to apply the
3094 // absolute address of the GOT entry.
3095 if (r_type == elfcpp::R_386_TLS_IE)
3096 got_offset += target->got_plt_section()->address();
3097 // All GOT offsets are relative to the end of the GOT.
3098 got_offset -= target->got_size();
3099 Relocate_functions<32, false>::rel32(view, got_offset);
3102 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3103 _("unsupported reloc %u"),
3107 case elfcpp::R_386_TLS_LE: // Local-exec
3108 // If we're creating a shared library, a dynamic relocation will
3109 // have been created for this location, so do not apply it now.
3110 if (!parameters->options().shared())
3112 if (tls_segment == NULL)
3114 gold_assert(parameters->errors()->error_count() > 0
3115 || issue_undefined_symbol_error(gsym));
3118 value -= tls_segment->memsz();
3119 Relocate_functions<32, false>::rel32(view, value);
3123 case elfcpp::R_386_TLS_LE_32:
3124 // If we're creating a shared library, a dynamic relocation will
3125 // have been created for this location, so do not apply it now.
3126 if (!parameters->options().shared())
3128 if (tls_segment == NULL)
3130 gold_assert(parameters->errors()->error_count() > 0
3131 || issue_undefined_symbol_error(gsym));
3134 value = tls_segment->memsz() - value;
3135 Relocate_functions<32, false>::rel32(view, value);
3141 // Do a relocation in which we convert a TLS General-Dynamic to a
3145 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
3147 Output_segment* tls_segment,
3148 const elfcpp::Rel<32, false>& rel,
3150 elfcpp::Elf_types<32>::Elf_Addr value,
3151 unsigned char* view,
3152 section_size_type view_size)
3154 // leal foo(,%reg,1),%eax; call ___tls_get_addr
3155 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
3156 // leal foo(%reg),%eax; call ___tls_get_addr
3157 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
3159 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3160 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3162 unsigned char op1 = view[-1];
3163 unsigned char op2 = view[-2];
3165 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3166 op2 == 0x8d || op2 == 0x04);
3167 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3173 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
3174 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
3175 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3176 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
3177 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3181 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3182 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
3183 if (rel.get_r_offset() + 9 < view_size
3186 // There is a trailing nop. Use the size byte subl.
3187 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3192 // Use the five byte subl.
3193 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
3197 value = tls_segment->memsz() - value;
3198 Relocate_functions<32, false>::rel32(view + roff, value);
3200 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3202 this->skip_call_tls_get_addr_ = true;
3205 // Do a relocation in which we convert a TLS General-Dynamic to an
3209 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
3212 const elfcpp::Rel<32, false>& rel,
3214 elfcpp::Elf_types<32>::Elf_Addr value,
3215 unsigned char* view,
3216 section_size_type view_size)
3218 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
3219 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
3221 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3222 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3224 unsigned char op1 = view[-1];
3225 unsigned char op2 = view[-2];
3227 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3228 op2 == 0x8d || op2 == 0x04);
3229 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3233 // FIXME: For now, support only the first (SIB) form.
3234 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
3238 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
3239 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
3240 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3241 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
3242 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
3246 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3247 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
3248 if (rel.get_r_offset() + 9 < view_size
3251 // FIXME: This is not the right instruction sequence.
3252 // There is a trailing nop. Use the size byte subl.
3253 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3258 // FIXME: This is not the right instruction sequence.
3259 // Use the five byte subl.
3260 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
3264 Relocate_functions<32, false>::rel32(view + roff, value);
3266 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3268 this->skip_call_tls_get_addr_ = true;
3271 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3272 // General-Dynamic to a Local-Exec.
3275 Target_i386::Relocate::tls_desc_gd_to_le(
3276 const Relocate_info<32, false>* relinfo,
3278 Output_segment* tls_segment,
3279 const elfcpp::Rel<32, false>& rel,
3280 unsigned int r_type,
3281 elfcpp::Elf_types<32>::Elf_Addr value,
3282 unsigned char* view,
3283 section_size_type view_size)
3285 if (r_type == elfcpp::R_386_TLS_GOTDESC)
3287 // leal foo@TLSDESC(%ebx), %eax
3288 // ==> leal foo@NTPOFF, %eax
3289 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3290 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3291 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3292 view[-2] == 0x8d && view[-1] == 0x83);
3294 value -= tls_segment->memsz();
3295 Relocate_functions<32, false>::rel32(view, value);
3299 // call *foo@TLSCALL(%eax)
3301 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3302 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3303 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3304 view[0] == 0xff && view[1] == 0x10);
3310 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3311 // General-Dynamic to an Initial-Exec.
3314 Target_i386::Relocate::tls_desc_gd_to_ie(
3315 const Relocate_info<32, false>* relinfo,
3318 const elfcpp::Rel<32, false>& rel,
3319 unsigned int r_type,
3320 elfcpp::Elf_types<32>::Elf_Addr value,
3321 unsigned char* view,
3322 section_size_type view_size)
3324 if (r_type == elfcpp::R_386_TLS_GOTDESC)
3326 // leal foo@TLSDESC(%ebx), %eax
3327 // ==> movl foo@GOTNTPOFF(%ebx), %eax
3328 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3329 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3330 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3331 view[-2] == 0x8d && view[-1] == 0x83);
3333 Relocate_functions<32, false>::rel32(view, value);
3337 // call *foo@TLSCALL(%eax)
3339 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3340 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3341 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3342 view[0] == 0xff && view[1] == 0x10);
3348 // Do a relocation in which we convert a TLS Local-Dynamic to a
3352 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
3355 const elfcpp::Rel<32, false>& rel,
3357 elfcpp::Elf_types<32>::Elf_Addr,
3358 unsigned char* view,
3359 section_size_type view_size)
3361 // leal foo(%reg), %eax; call ___tls_get_addr
3362 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
3364 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3365 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3367 // FIXME: Does this test really always pass?
3368 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3369 view[-2] == 0x8d && view[-1] == 0x83);
3371 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3373 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
3375 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3377 this->skip_call_tls_get_addr_ = true;
3380 // Do a relocation in which we convert a TLS Initial-Exec to a
3384 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
3386 Output_segment* tls_segment,
3387 const elfcpp::Rel<32, false>& rel,
3388 unsigned int r_type,
3389 elfcpp::Elf_types<32>::Elf_Addr value,
3390 unsigned char* view,
3391 section_size_type view_size)
3393 // We have to actually change the instructions, which means that we
3394 // need to examine the opcodes to figure out which instruction we
3396 if (r_type == elfcpp::R_386_TLS_IE)
3398 // movl %gs:XX,%eax ==> movl $YY,%eax
3399 // movl %gs:XX,%reg ==> movl $YY,%reg
3400 // addl %gs:XX,%reg ==> addl $YY,%reg
3401 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
3402 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3404 unsigned char op1 = view[-1];
3407 // movl XX,%eax ==> movl $YY,%eax
3412 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3414 unsigned char op2 = view[-2];
3417 // movl XX,%reg ==> movl $YY,%reg
3418 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3419 (op1 & 0xc7) == 0x05);
3421 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3423 else if (op2 == 0x03)
3425 // addl XX,%reg ==> addl $YY,%reg
3426 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3427 (op1 & 0xc7) == 0x05);
3429 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3432 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3437 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3438 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3439 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3440 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3441 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3443 unsigned char op1 = view[-1];
3444 unsigned char op2 = view[-2];
3445 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3446 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
3449 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3451 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3453 else if (op2 == 0x2b)
3455 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3457 view[-1] = 0xe8 | ((op1 >> 3) & 7);
3459 else if (op2 == 0x03)
3461 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3463 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3466 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3469 value = tls_segment->memsz() - value;
3470 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
3473 Relocate_functions<32, false>::rel32(view, value);
3476 // Relocate section data.
3479 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
3480 unsigned int sh_type,
3481 const unsigned char* prelocs,
3483 Output_section* output_section,
3484 bool needs_special_offset_handling,
3485 unsigned char* view,
3486 elfcpp::Elf_types<32>::Elf_Addr address,
3487 section_size_type view_size,
3488 const Reloc_symbol_changes* reloc_symbol_changes)
3490 gold_assert(sh_type == elfcpp::SHT_REL);
3492 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
3493 Target_i386::Relocate, gold::Default_comdat_behavior>(
3499 needs_special_offset_handling,
3503 reloc_symbol_changes);
3506 // Return the size of a relocation while scanning during a relocatable
3510 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3511 unsigned int r_type,
3516 case elfcpp::R_386_NONE:
3517 case elfcpp::R_386_GNU_VTINHERIT:
3518 case elfcpp::R_386_GNU_VTENTRY:
3519 case elfcpp::R_386_TLS_GD: // Global-dynamic
3520 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
3521 case elfcpp::R_386_TLS_DESC_CALL:
3522 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3523 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3524 case elfcpp::R_386_TLS_IE: // Initial-exec
3525 case elfcpp::R_386_TLS_IE_32:
3526 case elfcpp::R_386_TLS_GOTIE:
3527 case elfcpp::R_386_TLS_LE: // Local-exec
3528 case elfcpp::R_386_TLS_LE_32:
3531 case elfcpp::R_386_32:
3532 case elfcpp::R_386_PC32:
3533 case elfcpp::R_386_GOT32:
3534 case elfcpp::R_386_PLT32:
3535 case elfcpp::R_386_GOTOFF:
3536 case elfcpp::R_386_GOTPC:
3539 case elfcpp::R_386_16:
3540 case elfcpp::R_386_PC16:
3543 case elfcpp::R_386_8:
3544 case elfcpp::R_386_PC8:
3547 // These are relocations which should only be seen by the
3548 // dynamic linker, and should never be seen here.
3549 case elfcpp::R_386_COPY:
3550 case elfcpp::R_386_GLOB_DAT:
3551 case elfcpp::R_386_JUMP_SLOT:
3552 case elfcpp::R_386_RELATIVE:
3553 case elfcpp::R_386_IRELATIVE:
3554 case elfcpp::R_386_TLS_TPOFF:
3555 case elfcpp::R_386_TLS_DTPMOD32:
3556 case elfcpp::R_386_TLS_DTPOFF32:
3557 case elfcpp::R_386_TLS_TPOFF32:
3558 case elfcpp::R_386_TLS_DESC:
3559 object->error(_("unexpected reloc %u in object file"), r_type);
3562 case elfcpp::R_386_32PLT:
3563 case elfcpp::R_386_TLS_GD_32:
3564 case elfcpp::R_386_TLS_GD_PUSH:
3565 case elfcpp::R_386_TLS_GD_CALL:
3566 case elfcpp::R_386_TLS_GD_POP:
3567 case elfcpp::R_386_TLS_LDM_32:
3568 case elfcpp::R_386_TLS_LDM_PUSH:
3569 case elfcpp::R_386_TLS_LDM_CALL:
3570 case elfcpp::R_386_TLS_LDM_POP:
3571 case elfcpp::R_386_USED_BY_INTEL_200:
3573 object->error(_("unsupported reloc %u in object file"), r_type);
3578 // Scan the relocs during a relocatable link.
3581 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3583 Sized_relobj_file<32, false>* object,
3584 unsigned int data_shndx,
3585 unsigned int sh_type,
3586 const unsigned char* prelocs,
3588 Output_section* output_section,
3589 bool needs_special_offset_handling,
3590 size_t local_symbol_count,
3591 const unsigned char* plocal_symbols,
3592 Relocatable_relocs* rr)
3594 gold_assert(sh_type == elfcpp::SHT_REL);
3596 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3597 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3599 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
3600 Scan_relocatable_relocs>(
3608 needs_special_offset_handling,
3614 // Emit relocations for a section.
3617 Target_i386::relocate_relocs(
3618 const Relocate_info<32, false>* relinfo,
3619 unsigned int sh_type,
3620 const unsigned char* prelocs,
3622 Output_section* output_section,
3623 elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
3624 const Relocatable_relocs* rr,
3625 unsigned char* view,
3626 elfcpp::Elf_types<32>::Elf_Addr view_address,
3627 section_size_type view_size,
3628 unsigned char* reloc_view,
3629 section_size_type reloc_view_size)
3631 gold_assert(sh_type == elfcpp::SHT_REL);
3633 gold::relocate_relocs<32, false, elfcpp::SHT_REL>(
3638 offset_in_output_section,
3647 // Return the value to use for a dynamic which requires special
3648 // treatment. This is how we support equality comparisons of function
3649 // pointers across shared library boundaries, as described in the
3650 // processor specific ABI supplement.
3653 Target_i386::do_dynsym_value(const Symbol* gsym) const
3655 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3656 return this->plt_address_for_global(gsym);
3659 // Return a string used to fill a code section with nops to take up
3660 // the specified length.
3663 Target_i386::do_code_fill(section_size_type length) const
3667 // Build a jmp instruction to skip over the bytes.
3668 unsigned char jmp[5];
3670 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3671 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3672 + std::string(length - 5, static_cast<char>(0x90)));
3675 // Nop sequences of various lengths.
3676 const char nop1[1] = { '\x90' }; // nop
3677 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
3678 const char nop3[3] = { '\x8d', '\x76', '\x00' }; // leal 0(%esi),%esi
3679 const char nop4[4] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi
3681 const char nop5[5] = { '\x90', '\x8d', '\x74', // nop
3682 '\x26', '\x00' }; // leal 0(%esi,1),%esi
3683 const char nop6[6] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
3684 '\x00', '\x00', '\x00' };
3685 const char nop7[7] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi
3686 '\x00', '\x00', '\x00',
3688 const char nop8[8] = { '\x90', '\x8d', '\xb4', // nop
3689 '\x26', '\x00', '\x00', // leal 0L(%esi,1),%esi
3691 const char nop9[9] = { '\x89', '\xf6', '\x8d', // movl %esi,%esi
3692 '\xbc', '\x27', '\x00', // leal 0L(%edi,1),%edi
3693 '\x00', '\x00', '\x00' };
3694 const char nop10[10] = { '\x8d', '\x76', '\x00', // leal 0(%esi),%esi
3695 '\x8d', '\xbc', '\x27', // leal 0L(%edi,1),%edi
3696 '\x00', '\x00', '\x00',
3698 const char nop11[11] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi
3699 '\x00', '\x8d', '\xbc', // leal 0L(%edi,1),%edi
3700 '\x27', '\x00', '\x00',
3702 const char nop12[12] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
3703 '\x00', '\x00', '\x00', // leal 0L(%edi),%edi
3704 '\x8d', '\xbf', '\x00',
3705 '\x00', '\x00', '\x00' };
3706 const char nop13[13] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
3707 '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi
3708 '\x8d', '\xbc', '\x27',
3709 '\x00', '\x00', '\x00',
3711 const char nop14[14] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi
3712 '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi
3713 '\x00', '\x8d', '\xbc',
3714 '\x27', '\x00', '\x00',
3716 const char nop15[15] = { '\xeb', '\x0d', '\x90', // jmp .+15
3717 '\x90', '\x90', '\x90', // nop,nop,nop,...
3718 '\x90', '\x90', '\x90',
3719 '\x90', '\x90', '\x90',
3720 '\x90', '\x90', '\x90' };
3722 const char* nops[16] = {
3724 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3725 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3728 return std::string(nops[length], length);
3731 // Return the value to use for the base of a DW_EH_PE_datarel offset
3732 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3733 // assembler can not write out the difference between two labels in
3734 // different sections, so instead of using a pc-relative value they
3735 // use an offset from the GOT.
3738 Target_i386::do_ehframe_datarel_base() const
3740 gold_assert(this->global_offset_table_ != NULL);
3741 Symbol* sym = this->global_offset_table_;
3742 Sized_symbol<32>* ssym = static_cast<Sized_symbol<32>*>(sym);
3743 return ssym->value();
3746 // Return whether SYM should be treated as a call to a non-split
3747 // function. We don't want that to be true of a call to a
3748 // get_pc_thunk function.
3751 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
3753 return (sym->type() == elfcpp::STT_FUNC
3754 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3757 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3758 // compiled with -fsplit-stack. The function calls non-split-stack
3759 // code. We have to change the function so that it always ensures
3760 // that it has enough stack space to run some random function.
3763 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3764 section_offset_type fnoffset,
3765 section_size_type fnsize,
3766 unsigned char* view,
3767 section_size_type view_size,
3769 std::string* to) const
3771 // The function starts with a comparison of the stack pointer and a
3772 // field in the TCB. This is followed by a jump.
3775 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3778 // We will call __morestack if the carry flag is set after this
3779 // comparison. We turn the comparison into an stc instruction
3781 view[fnoffset] = '\xf9';
3782 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3784 // lea NN(%esp),%ecx
3785 // lea NN(%esp),%edx
3786 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3787 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
3790 // This is loading an offset from the stack pointer for a
3791 // comparison. The offset is negative, so we decrease the
3792 // offset by the amount of space we need for the stack. This
3793 // means we will avoid calling __morestack if there happens to
3794 // be plenty of space on the stack already.
3795 unsigned char* pval = view + fnoffset + 3;
3796 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3797 val -= parameters->options().split_stack_adjust_size();
3798 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3802 if (!object->has_no_split_stack())
3803 object->error(_("failed to match split-stack sequence at "
3804 "section %u offset %0zx"),
3805 shndx, static_cast<size_t>(fnoffset));
3809 // We have to change the function so that it calls
3810 // __morestack_non_split instead of __morestack. The former will
3811 // allocate additional stack space.
3812 *from = "__morestack";
3813 *to = "__morestack_non_split";
3816 // The selector for i386 object files. Note this is never instantiated
3817 // directly. It's only used in Target_selector_i386_nacl, below.
3819 class Target_selector_i386 : public Target_selector_freebsd
3822 Target_selector_i386()
3823 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
3824 "elf32-i386", "elf32-i386-freebsd",
3829 do_instantiate_target()
3830 { return new Target_i386(); }
3833 // NaCl variant. It uses different PLT contents.
3835 class Output_data_plt_i386_nacl : public Output_data_plt_i386
3838 Output_data_plt_i386_nacl(Layout* layout,
3839 Output_data_space* got_plt,
3840 Output_data_space* got_irelative)
3841 : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative)
3845 virtual unsigned int
3846 do_get_plt_entry_size() const
3847 { return plt_entry_size; }
3850 do_add_eh_frame(Layout* layout)
3852 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
3853 plt_eh_frame_fde, plt_eh_frame_fde_size);
3856 // The size of an entry in the PLT.
3857 static const int plt_entry_size = 64;
3859 // The .eh_frame unwind information for the PLT.
3860 static const int plt_eh_frame_fde_size = 32;
3861 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
3864 class Output_data_plt_i386_nacl_exec : public Output_data_plt_i386_nacl
3867 Output_data_plt_i386_nacl_exec(Layout* layout,
3868 Output_data_space* got_plt,
3869 Output_data_space* got_irelative)
3870 : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
3875 do_fill_first_plt_entry(unsigned char* pov,
3876 elfcpp::Elf_types<32>::Elf_Addr got_address);
3878 virtual unsigned int
3879 do_fill_plt_entry(unsigned char* pov,
3880 elfcpp::Elf_types<32>::Elf_Addr got_address,
3881 unsigned int got_offset,
3882 unsigned int plt_offset,
3883 unsigned int plt_rel_offset);
3886 // The first entry in the PLT for an executable.
3887 static const unsigned char first_plt_entry[plt_entry_size];
3889 // Other entries in the PLT for an executable.
3890 static const unsigned char plt_entry[plt_entry_size];
3893 class Output_data_plt_i386_nacl_dyn : public Output_data_plt_i386_nacl
3896 Output_data_plt_i386_nacl_dyn(Layout* layout,
3897 Output_data_space* got_plt,
3898 Output_data_space* got_irelative)
3899 : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
3904 do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr);
3906 virtual unsigned int
3907 do_fill_plt_entry(unsigned char* pov,
3908 elfcpp::Elf_types<32>::Elf_Addr,
3909 unsigned int got_offset,
3910 unsigned int plt_offset,
3911 unsigned int plt_rel_offset);
3914 // The first entry in the PLT for a shared object.
3915 static const unsigned char first_plt_entry[plt_entry_size];
3917 // Other entries in the PLT for a shared object.
3918 static const unsigned char plt_entry[plt_entry_size];
3921 class Target_i386_nacl : public Target_i386
3925 : Target_i386(&i386_nacl_info)
3929 virtual Output_data_plt_i386*
3930 do_make_data_plt(Layout* layout,
3931 Output_data_space* got_plt,
3932 Output_data_space* got_irelative,
3936 return new Output_data_plt_i386_nacl_dyn(layout, got_plt, got_irelative);
3938 return new Output_data_plt_i386_nacl_exec(layout, got_plt, got_irelative);
3942 static const Target::Target_info i386_nacl_info;
3945 const Target::Target_info Target_i386_nacl::i386_nacl_info =
3948 false, // is_big_endian
3949 elfcpp::EM_386, // machine_code
3950 false, // has_make_symbol
3951 false, // has_resolve
3952 true, // has_code_fill
3953 true, // is_default_stack_executable
3954 true, // can_icf_inline_merge_sections
3956 "/lib/ld-nacl-x86-32.so.1", // dynamic_linker
3957 0x20000, // default_text_segment_address
3958 0x10000, // abi_pagesize (overridable by -z max-page-size)
3959 0x10000, // common_pagesize (overridable by -z common-page-size)
3960 true, // isolate_execinstr
3961 0x10000000, // rosegment_gap
3962 elfcpp::SHN_UNDEF, // small_common_shndx
3963 elfcpp::SHN_UNDEF, // large_common_shndx
3964 0, // small_common_section_flags
3965 0, // large_common_section_flags
3966 NULL, // attributes_section
3967 NULL // attributes_vendor
3970 #define NACLMASK 0xe0 // 32-byte alignment mask
3973 Output_data_plt_i386_nacl_exec::first_plt_entry[plt_entry_size] =
3975 0xff, 0x35, // pushl contents of memory address
3976 0, 0, 0, 0, // replaced with address of .got + 4
3977 0x8b, 0x0d, // movl contents of address, %ecx
3978 0, 0, 0, 0, // replaced with address of .got + 8
3979 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
3980 0xff, 0xe1, // jmp *%ecx
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, 0x90, // nops
3987 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
3988 0x90, 0x90, 0x90, 0x90, 0x90
3992 Output_data_plt_i386_nacl_exec::do_fill_first_plt_entry(
3994 elfcpp::Elf_types<32>::Elf_Addr got_address)
3996 memcpy(pov, first_plt_entry, plt_entry_size);
3997 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
3998 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
4001 // The first entry in the PLT for a shared object.
4004 Output_data_plt_i386_nacl_dyn::first_plt_entry[plt_entry_size] =
4006 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
4007 0x8b, 0x4b, 0x08, // mov 0x8(%ebx), %ecx
4008 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
4009 0xff, 0xe1, // jmp *%ecx
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
4018 0x90, 0x90, 0x90, 0x90, 0x90, // nops
4019 0x90, 0x90, 0x90, 0x90, 0x90 // nops
4023 Output_data_plt_i386_nacl_dyn::do_fill_first_plt_entry(
4025 elfcpp::Elf_types<32>::Elf_Addr)
4027 memcpy(pov, first_plt_entry, plt_entry_size);
4030 // Subsequent entries in the PLT for an executable.
4033 Output_data_plt_i386_nacl_exec::plt_entry[plt_entry_size] =
4035 0x8b, 0x0d, // movl contents of address, %ecx */
4036 0, 0, 0, 0, // replaced with address of symbol in .got
4037 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
4038 0xff, 0xe1, // jmp *%ecx
4040 // Pad to the next 32-byte boundary with nop instructions.
4042 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4043 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4045 // Lazy GOT entries point here (32-byte aligned).
4046 0x68, // pushl immediate
4047 0, 0, 0, 0, // replaced with offset into relocation table
4048 0xe9, // jmp relative
4049 0, 0, 0, 0, // replaced with offset to start of .plt
4051 // Pad to the next 32-byte boundary with nop instructions.
4052 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4053 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4058 Output_data_plt_i386_nacl_exec::do_fill_plt_entry(
4060 elfcpp::Elf_types<32>::Elf_Addr got_address,
4061 unsigned int got_offset,
4062 unsigned int plt_offset,
4063 unsigned int plt_rel_offset)
4065 memcpy(pov, plt_entry, plt_entry_size);
4066 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4067 got_address + got_offset);
4068 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
4069 elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
4073 // Subsequent entries in the PLT for a shared object.
4076 Output_data_plt_i386_nacl_dyn::plt_entry[plt_entry_size] =
4078 0x8b, 0x8b, // movl offset(%ebx), %ecx
4079 0, 0, 0, 0, // replaced with offset of symbol in .got
4080 0x83, 0xe1, 0xe0, // andl $NACLMASK, %ecx
4081 0xff, 0xe1, // jmp *%ecx
4083 // Pad to the next 32-byte boundary with nop instructions.
4085 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4086 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4088 // Lazy GOT entries point here (32-byte aligned).
4089 0x68, // pushl immediate
4090 0, 0, 0, 0, // replaced with offset into relocation table.
4091 0xe9, // jmp relative
4092 0, 0, 0, 0, // replaced with offset to start of .plt.
4094 // Pad to the next 32-byte boundary with nop instructions.
4095 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4096 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4101 Output_data_plt_i386_nacl_dyn::do_fill_plt_entry(
4103 elfcpp::Elf_types<32>::Elf_Addr,
4104 unsigned int got_offset,
4105 unsigned int plt_offset,
4106 unsigned int plt_rel_offset)
4108 memcpy(pov, plt_entry, plt_entry_size);
4109 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
4110 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
4111 elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
4116 Output_data_plt_i386_nacl::plt_eh_frame_fde[plt_eh_frame_fde_size] =
4118 0, 0, 0, 0, // Replaced with offset to .plt.
4119 0, 0, 0, 0, // Replaced with size of .plt.
4120 0, // Augmentation size.
4121 elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
4122 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
4123 elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
4124 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
4125 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
4126 13, // Block length.
4127 elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
4128 elfcpp::DW_OP_breg8, 0, // Push %eip.
4129 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
4130 elfcpp::DW_OP_and, // & (%eip & 0x3f).
4131 elfcpp::DW_OP_const1u, 37, // Push 0x25.
4132 elfcpp::DW_OP_ge, // >= ((%eip & 0x3f) >= 0x25)
4133 elfcpp::DW_OP_lit2, // Push 2.
4134 elfcpp::DW_OP_shl, // << (((%eip & 0x3f) >= 0x25) << 2)
4135 elfcpp::DW_OP_plus, // + ((((%eip&0x3f)>=0x25)<<2)+%esp+4
4136 elfcpp::DW_CFA_nop, // Align to 32 bytes.
4140 // The selector for i386-nacl object files.
4142 class Target_selector_i386_nacl
4143 : public Target_selector_nacl<Target_selector_i386, Target_i386_nacl>
4146 Target_selector_i386_nacl()
4147 : Target_selector_nacl<Target_selector_i386,
4148 Target_i386_nacl>("x86-32",
4154 Target_selector_i386_nacl target_selector_i386;
4156 } // End anonymous namespace.