1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
47 class Output_data_plt_i386;
49 // The i386 target class.
50 // TLS info comes from
51 // http://people.redhat.com/drepper/tls.pdf
52 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
54 class Target_i386 : public Target_freebsd<32, false>
57 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
60 : Target_freebsd<32, false>(&i386_info),
61 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
62 copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
63 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
66 // Process the relocations to determine unreferenced sections for
67 // garbage collection.
69 gc_process_relocs(const General_options& options,
72 Sized_relobj<32, false>* object,
73 unsigned int data_shndx,
75 const unsigned char* prelocs,
77 Output_section* output_section,
78 bool needs_special_offset_handling,
79 size_t local_symbol_count,
80 const unsigned char* plocal_symbols);
82 // Scan the relocations to look for symbol adjustments.
84 scan_relocs(const General_options& options,
87 Sized_relobj<32, false>* object,
88 unsigned int data_shndx,
90 const unsigned char* prelocs,
92 Output_section* output_section,
93 bool needs_special_offset_handling,
94 size_t local_symbol_count,
95 const unsigned char* plocal_symbols);
97 // Finalize the sections.
99 do_finalize_sections(Layout*);
101 // Return the value to use for a dynamic which requires special
104 do_dynsym_value(const Symbol*) const;
106 // Relocate a section.
108 relocate_section(const Relocate_info<32, false>*,
109 unsigned int sh_type,
110 const unsigned char* prelocs,
112 Output_section* output_section,
113 bool needs_special_offset_handling,
115 elfcpp::Elf_types<32>::Elf_Addr view_address,
116 section_size_type view_size);
118 // Scan the relocs during a relocatable link.
120 scan_relocatable_relocs(const General_options& options,
121 Symbol_table* symtab,
123 Sized_relobj<32, false>* object,
124 unsigned int data_shndx,
125 unsigned int sh_type,
126 const unsigned char* prelocs,
128 Output_section* output_section,
129 bool needs_special_offset_handling,
130 size_t local_symbol_count,
131 const unsigned char* plocal_symbols,
132 Relocatable_relocs*);
134 // Relocate a section during a relocatable link.
136 relocate_for_relocatable(const Relocate_info<32, false>*,
137 unsigned int sh_type,
138 const unsigned char* prelocs,
140 Output_section* output_section,
141 off_t offset_in_output_section,
142 const Relocatable_relocs*,
144 elfcpp::Elf_types<32>::Elf_Addr view_address,
145 section_size_type view_size,
146 unsigned char* reloc_view,
147 section_size_type reloc_view_size);
149 // Return a string used to fill a code section with nops.
151 do_code_fill(section_size_type length) const;
153 // Return whether SYM is defined by the ABI.
155 do_is_defined_by_abi(const Symbol* sym) const
156 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
158 // Return whether a symbol name implies a local label. The UnixWare
159 // 2.1 cc generates temporary symbols that start with .X, so we
160 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
161 // If so, we should move the .X recognition into
162 // Target::do_is_local_label_name.
164 do_is_local_label_name(const char* name) const
166 if (name[0] == '.' && name[1] == 'X')
168 return Target::do_is_local_label_name(name);
171 // Return the size of the GOT section.
175 gold_assert(this->got_ != NULL);
176 return this->got_->data_size();
180 // The class which scans relocations.
184 local(const General_options& options, Symbol_table* symtab,
185 Layout* layout, Target_i386* target,
186 Sized_relobj<32, false>* object,
187 unsigned int data_shndx,
188 Output_section* output_section,
189 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
190 const elfcpp::Sym<32, false>& lsym);
193 global(const General_options& options, Symbol_table* symtab,
194 Layout* layout, Target_i386* target,
195 Sized_relobj<32, false>* object,
196 unsigned int data_shndx,
197 Output_section* output_section,
198 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
202 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
205 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
209 // The class which implements relocation.
214 : skip_call_tls_get_addr_(false),
215 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
220 if (this->skip_call_tls_get_addr_)
222 // FIXME: This needs to specify the location somehow.
223 gold_error(_("missing expected TLS relocation"));
227 // Return whether the static relocation needs to be applied.
229 should_apply_static_reloc(const Sized_symbol<32>* gsym,
232 Output_section* output_section);
234 // Do a relocation. Return false if the caller should not issue
235 // any warnings about this relocation.
237 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
238 size_t relnum, const elfcpp::Rel<32, false>&,
239 unsigned int r_type, const Sized_symbol<32>*,
240 const Symbol_value<32>*,
241 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
245 // Do a TLS relocation.
247 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
248 size_t relnum, const elfcpp::Rel<32, false>&,
249 unsigned int r_type, const Sized_symbol<32>*,
250 const Symbol_value<32>*,
251 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
254 // Do a TLS General-Dynamic to Initial-Exec transition.
256 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
257 Output_segment* tls_segment,
258 const elfcpp::Rel<32, false>&, unsigned int r_type,
259 elfcpp::Elf_types<32>::Elf_Addr value,
261 section_size_type view_size);
263 // Do a TLS General-Dynamic to Local-Exec transition.
265 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
266 Output_segment* tls_segment,
267 const elfcpp::Rel<32, false>&, unsigned int r_type,
268 elfcpp::Elf_types<32>::Elf_Addr value,
270 section_size_type view_size);
272 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
275 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
276 Output_segment* tls_segment,
277 const elfcpp::Rel<32, false>&, unsigned int r_type,
278 elfcpp::Elf_types<32>::Elf_Addr value,
280 section_size_type view_size);
282 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
285 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
286 Output_segment* tls_segment,
287 const elfcpp::Rel<32, false>&, unsigned int r_type,
288 elfcpp::Elf_types<32>::Elf_Addr value,
290 section_size_type view_size);
292 // Do a TLS Local-Dynamic to Local-Exec transition.
294 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
295 Output_segment* tls_segment,
296 const elfcpp::Rel<32, false>&, unsigned int r_type,
297 elfcpp::Elf_types<32>::Elf_Addr value,
299 section_size_type view_size);
301 // Do a TLS Initial-Exec to Local-Exec transition.
303 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
304 Output_segment* tls_segment,
305 const elfcpp::Rel<32, false>&, unsigned int r_type,
306 elfcpp::Elf_types<32>::Elf_Addr value,
308 section_size_type view_size);
310 // We need to keep track of which type of local dynamic relocation
311 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
312 enum Local_dynamic_type
319 // This is set if we should skip the next reloc, which should be a
320 // PLT32 reloc against ___tls_get_addr.
321 bool skip_call_tls_get_addr_;
322 // The type of local dynamic relocation we have seen in the section
323 // being relocated, if any.
324 Local_dynamic_type local_dynamic_type_;
327 // A class which returns the size required for a relocation type,
328 // used while scanning relocs during a relocatable link.
329 class Relocatable_size_for_reloc
333 get_size_for_reloc(unsigned int, Relobj*);
336 // Adjust TLS relocation type based on the options and whether this
337 // is a local symbol.
338 static tls::Tls_optimization
339 optimize_tls_reloc(bool is_final, int r_type);
341 // Get the GOT section, creating it if necessary.
342 Output_data_got<32, false>*
343 got_section(Symbol_table*, Layout*);
345 // Get the GOT PLT section.
347 got_plt_section() const
349 gold_assert(this->got_plt_ != NULL);
350 return this->got_plt_;
353 // Create a PLT entry for a global symbol.
355 make_plt_entry(Symbol_table*, Layout*, Symbol*);
357 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
359 define_tls_base_symbol(Symbol_table*, Layout*);
361 // Create a GOT entry for the TLS module index.
363 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
364 Sized_relobj<32, false>* object);
366 // Get the PLT section.
367 const Output_data_plt_i386*
370 gold_assert(this->plt_ != NULL);
374 // Get the dynamic reloc section, creating it if necessary.
376 rel_dyn_section(Layout*);
378 // Return true if the symbol may need a COPY relocation.
379 // References from an executable object to non-function symbols
380 // defined in a dynamic object may need a COPY relocation.
382 may_need_copy_reloc(Symbol* gsym)
384 return (!parameters->options().shared()
385 && gsym->is_from_dynobj()
386 && gsym->type() != elfcpp::STT_FUNC);
389 // Add a potential copy relocation.
391 copy_reloc(Symbol_table* symtab, Layout* layout,
392 Sized_relobj<32, false>* object,
393 unsigned int shndx, Output_section* output_section,
394 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
396 this->copy_relocs_.copy_reloc(symtab, layout,
397 symtab->get_sized_symbol<32>(sym),
398 object, shndx, output_section, reloc,
399 this->rel_dyn_section(layout));
402 // Information about this specific target which we pass to the
403 // general Target structure.
404 static const Target::Target_info i386_info;
406 // The types of GOT entries needed for this platform.
409 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
410 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
411 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
412 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
413 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
417 Output_data_got<32, false>* got_;
419 Output_data_plt_i386* plt_;
420 // The GOT PLT section.
421 Output_data_space* got_plt_;
422 // The dynamic reloc section.
423 Reloc_section* rel_dyn_;
424 // Relocs saved to avoid a COPY reloc.
425 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
426 // Space for variables copied with a COPY reloc.
427 Output_data_space* dynbss_;
428 // Offset of the GOT entry for the TLS module index.
429 unsigned int got_mod_index_offset_;
430 // True if the _TLS_MODULE_BASE_ symbol has been defined.
431 bool tls_base_symbol_defined_;
434 const Target::Target_info Target_i386::i386_info =
437 false, // is_big_endian
438 elfcpp::EM_386, // machine_code
439 false, // has_make_symbol
440 false, // has_resolve
441 true, // has_code_fill
442 true, // is_default_stack_executable
444 "/usr/lib/libc.so.1", // dynamic_linker
445 0x08048000, // default_text_segment_address
446 0x1000, // abi_pagesize (overridable by -z max-page-size)
447 0x1000 // common_pagesize (overridable by -z common-page-size)
450 // Get the GOT section, creating it if necessary.
452 Output_data_got<32, false>*
453 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
455 if (this->got_ == NULL)
457 gold_assert(symtab != NULL && layout != NULL);
459 this->got_ = new Output_data_got<32, false>();
462 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
464 | elfcpp::SHF_WRITE),
468 // The old GNU linker creates a .got.plt section. We just
469 // create another set of data in the .got section. Note that we
470 // always create a PLT if we create a GOT, although the PLT
472 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
473 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
475 | elfcpp::SHF_WRITE),
479 // The first three entries are reserved.
480 this->got_plt_->set_current_data_size(3 * 4);
482 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
483 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
485 0, 0, elfcpp::STT_OBJECT,
487 elfcpp::STV_HIDDEN, 0,
494 // Get the dynamic reloc section, creating it if necessary.
496 Target_i386::Reloc_section*
497 Target_i386::rel_dyn_section(Layout* layout)
499 if (this->rel_dyn_ == NULL)
501 gold_assert(layout != NULL);
502 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
503 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
504 elfcpp::SHF_ALLOC, this->rel_dyn_);
506 return this->rel_dyn_;
509 // A class to handle the PLT data.
511 class Output_data_plt_i386 : public Output_section_data
514 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
516 Output_data_plt_i386(Layout*, Output_data_space*);
518 // Add an entry to the PLT.
520 add_entry(Symbol* gsym);
522 // Return the .rel.plt section data.
525 { return this->rel_; }
529 do_adjust_output_section(Output_section* os);
531 // Write to a map file.
533 do_print_to_mapfile(Mapfile* mapfile) const
534 { mapfile->print_output_data(this, _("** PLT")); }
537 // The size of an entry in the PLT.
538 static const int plt_entry_size = 16;
540 // The first entry in the PLT for an executable.
541 static unsigned char exec_first_plt_entry[plt_entry_size];
543 // The first entry in the PLT for a shared object.
544 static unsigned char dyn_first_plt_entry[plt_entry_size];
546 // Other entries in the PLT for an executable.
547 static unsigned char exec_plt_entry[plt_entry_size];
549 // Other entries in the PLT for a shared object.
550 static unsigned char dyn_plt_entry[plt_entry_size];
552 // Set the final size.
554 set_final_data_size()
555 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
557 // Write out the PLT data.
559 do_write(Output_file*);
561 // The reloc section.
563 // The .got.plt section.
564 Output_data_space* got_plt_;
565 // The number of PLT entries.
569 // Create the PLT section. The ordinary .got section is an argument,
570 // since we need to refer to the start. We also create our own .got
571 // section just for PLT entries.
573 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
574 Output_data_space* got_plt)
575 : Output_section_data(4), got_plt_(got_plt), count_(0)
577 this->rel_ = new Reloc_section(false);
578 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
579 elfcpp::SHF_ALLOC, this->rel_);
583 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
585 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
586 // linker, and so do we.
590 // Add an entry to the PLT.
593 Output_data_plt_i386::add_entry(Symbol* gsym)
595 gold_assert(!gsym->has_plt_offset());
597 // Note that when setting the PLT offset we skip the initial
598 // reserved PLT entry.
599 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
603 section_offset_type got_offset = this->got_plt_->current_data_size();
605 // Every PLT entry needs a GOT entry which points back to the PLT
606 // entry (this will be changed by the dynamic linker, normally
607 // lazily when the function is called).
608 this->got_plt_->set_current_data_size(got_offset + 4);
610 // Every PLT entry needs a reloc.
611 gsym->set_needs_dynsym_entry();
612 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
615 // Note that we don't need to save the symbol. The contents of the
616 // PLT are independent of which symbols are used. The symbols only
617 // appear in the relocations.
620 // The first entry in the PLT for an executable.
622 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
624 0xff, 0x35, // pushl contents of memory address
625 0, 0, 0, 0, // replaced with address of .got + 4
626 0xff, 0x25, // jmp indirect
627 0, 0, 0, 0, // replaced with address of .got + 8
631 // The first entry in the PLT for a shared object.
633 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
635 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
636 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
640 // Subsequent entries in the PLT for an executable.
642 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
644 0xff, 0x25, // jmp indirect
645 0, 0, 0, 0, // replaced with address of symbol in .got
646 0x68, // pushl immediate
647 0, 0, 0, 0, // replaced with offset into relocation table
648 0xe9, // jmp relative
649 0, 0, 0, 0 // replaced with offset to start of .plt
652 // Subsequent entries in the PLT for a shared object.
654 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
656 0xff, 0xa3, // jmp *offset(%ebx)
657 0, 0, 0, 0, // replaced with offset of symbol in .got
658 0x68, // pushl immediate
659 0, 0, 0, 0, // replaced with offset into relocation table
660 0xe9, // jmp relative
661 0, 0, 0, 0 // replaced with offset to start of .plt
664 // Write out the PLT. This uses the hand-coded instructions above,
665 // and adjusts them as needed. This is all specified by the i386 ELF
666 // Processor Supplement.
669 Output_data_plt_i386::do_write(Output_file* of)
671 const off_t offset = this->offset();
672 const section_size_type oview_size =
673 convert_to_section_size_type(this->data_size());
674 unsigned char* const oview = of->get_output_view(offset, oview_size);
676 const off_t got_file_offset = this->got_plt_->offset();
677 const section_size_type got_size =
678 convert_to_section_size_type(this->got_plt_->data_size());
679 unsigned char* const got_view = of->get_output_view(got_file_offset,
682 unsigned char* pov = oview;
684 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
685 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
687 if (parameters->options().shared())
688 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
691 memcpy(pov, exec_first_plt_entry, plt_entry_size);
692 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
693 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
695 pov += plt_entry_size;
697 unsigned char* got_pov = got_view;
699 memset(got_pov, 0, 12);
702 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
704 unsigned int plt_offset = plt_entry_size;
705 unsigned int plt_rel_offset = 0;
706 unsigned int got_offset = 12;
707 const unsigned int count = this->count_;
708 for (unsigned int i = 0;
711 pov += plt_entry_size,
713 plt_offset += plt_entry_size,
714 plt_rel_offset += rel_size,
717 // Set and adjust the PLT entry itself.
719 if (parameters->options().shared())
721 memcpy(pov, dyn_plt_entry, plt_entry_size);
722 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
726 memcpy(pov, exec_plt_entry, plt_entry_size);
727 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
732 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
733 elfcpp::Swap<32, false>::writeval(pov + 12,
734 - (plt_offset + plt_entry_size));
736 // Set the entry in the GOT.
737 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
740 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
741 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
743 of->write_output_view(offset, oview_size, oview);
744 of->write_output_view(got_file_offset, got_size, got_view);
747 // Create a PLT entry for a global symbol.
750 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
752 if (gsym->has_plt_offset())
755 if (this->plt_ == NULL)
757 // Create the GOT sections first.
758 this->got_section(symtab, layout);
760 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
761 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
763 | elfcpp::SHF_EXECINSTR),
767 this->plt_->add_entry(gsym);
770 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
773 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
775 if (this->tls_base_symbol_defined_)
778 Output_segment* tls_segment = layout->tls_segment();
779 if (tls_segment != NULL)
781 bool is_exec = parameters->options().output_is_executable();
782 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
786 elfcpp::STV_HIDDEN, 0,
788 ? Symbol::SEGMENT_END
789 : Symbol::SEGMENT_START),
792 this->tls_base_symbol_defined_ = true;
795 // Create a GOT entry for the TLS module index.
798 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
799 Sized_relobj<32, false>* object)
801 if (this->got_mod_index_offset_ == -1U)
803 gold_assert(symtab != NULL && layout != NULL && object != NULL);
804 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
805 Output_data_got<32, false>* got = this->got_section(symtab, layout);
806 unsigned int got_offset = got->add_constant(0);
807 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
809 got->add_constant(0);
810 this->got_mod_index_offset_ = got_offset;
812 return this->got_mod_index_offset_;
815 // Optimize the TLS relocation type based on what we know about the
816 // symbol. IS_FINAL is true if the final address of this symbol is
817 // known at link time.
819 tls::Tls_optimization
820 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
822 // If we are generating a shared library, then we can't do anything
824 if (parameters->options().shared())
825 return tls::TLSOPT_NONE;
829 case elfcpp::R_386_TLS_GD:
830 case elfcpp::R_386_TLS_GOTDESC:
831 case elfcpp::R_386_TLS_DESC_CALL:
832 // These are General-Dynamic which permits fully general TLS
833 // access. Since we know that we are generating an executable,
834 // we can convert this to Initial-Exec. If we also know that
835 // this is a local symbol, we can further switch to Local-Exec.
837 return tls::TLSOPT_TO_LE;
838 return tls::TLSOPT_TO_IE;
840 case elfcpp::R_386_TLS_LDM:
841 // This is Local-Dynamic, which refers to a local symbol in the
842 // dynamic TLS block. Since we know that we generating an
843 // executable, we can switch to Local-Exec.
844 return tls::TLSOPT_TO_LE;
846 case elfcpp::R_386_TLS_LDO_32:
847 // Another type of Local-Dynamic relocation.
848 return tls::TLSOPT_TO_LE;
850 case elfcpp::R_386_TLS_IE:
851 case elfcpp::R_386_TLS_GOTIE:
852 case elfcpp::R_386_TLS_IE_32:
853 // These are Initial-Exec relocs which get the thread offset
854 // from the GOT. If we know that we are linking against the
855 // local symbol, we can switch to Local-Exec, which links the
856 // thread offset into the instruction.
858 return tls::TLSOPT_TO_LE;
859 return tls::TLSOPT_NONE;
861 case elfcpp::R_386_TLS_LE:
862 case elfcpp::R_386_TLS_LE_32:
863 // When we already have Local-Exec, there is nothing further we
865 return tls::TLSOPT_NONE;
872 // Report an unsupported relocation against a local symbol.
875 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
878 gold_error(_("%s: unsupported reloc %u against local symbol"),
879 object->name().c_str(), r_type);
882 // Scan a relocation for a local symbol.
885 Target_i386::Scan::local(const General_options&,
886 Symbol_table* symtab,
889 Sized_relobj<32, false>* object,
890 unsigned int data_shndx,
891 Output_section* output_section,
892 const elfcpp::Rel<32, false>& reloc,
894 const elfcpp::Sym<32, false>& lsym)
898 case elfcpp::R_386_NONE:
899 case elfcpp::R_386_GNU_VTINHERIT:
900 case elfcpp::R_386_GNU_VTENTRY:
903 case elfcpp::R_386_32:
904 // If building a shared library (or a position-independent
905 // executable), we need to create a dynamic relocation for
906 // this location. The relocation applied at link time will
907 // apply the link-time value, so we flag the location with
908 // an R_386_RELATIVE relocation so the dynamic loader can
909 // relocate it easily.
910 if (parameters->options().output_is_position_independent())
912 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
913 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
914 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
915 output_section, data_shndx,
916 reloc.get_r_offset());
920 case elfcpp::R_386_16:
921 case elfcpp::R_386_8:
922 // If building a shared library (or a position-independent
923 // executable), we need to create a dynamic relocation for
924 // this location. Because the addend needs to remain in the
925 // data section, we need to be careful not to apply this
926 // relocation statically.
927 if (parameters->options().output_is_position_independent())
929 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
930 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
931 if (lsym.get_st_type() != elfcpp::STT_SECTION)
932 rel_dyn->add_local(object, r_sym, r_type, output_section,
933 data_shndx, reloc.get_r_offset());
936 gold_assert(lsym.get_st_value() == 0);
937 unsigned int shndx = lsym.get_st_shndx();
939 shndx = object->adjust_sym_shndx(r_sym, shndx,
942 object->error(_("section symbol %u has bad shndx %u"),
945 rel_dyn->add_local_section(object, shndx,
946 r_type, output_section,
947 data_shndx, reloc.get_r_offset());
952 case elfcpp::R_386_PC32:
953 case elfcpp::R_386_PC16:
954 case elfcpp::R_386_PC8:
957 case elfcpp::R_386_PLT32:
958 // Since we know this is a local symbol, we can handle this as a
962 case elfcpp::R_386_GOTOFF:
963 case elfcpp::R_386_GOTPC:
964 // We need a GOT section.
965 target->got_section(symtab, layout);
968 case elfcpp::R_386_GOT32:
970 // The symbol requires a GOT entry.
971 Output_data_got<32, false>* got = target->got_section(symtab, layout);
972 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
973 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
975 // If we are generating a shared object, we need to add a
976 // dynamic RELATIVE relocation for this symbol's GOT entry.
977 if (parameters->options().output_is_position_independent())
979 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
980 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
981 rel_dyn->add_local_relative(
982 object, r_sym, elfcpp::R_386_RELATIVE, got,
983 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
989 // These are relocations which should only be seen by the
990 // dynamic linker, and should never be seen here.
991 case elfcpp::R_386_COPY:
992 case elfcpp::R_386_GLOB_DAT:
993 case elfcpp::R_386_JUMP_SLOT:
994 case elfcpp::R_386_RELATIVE:
995 case elfcpp::R_386_TLS_TPOFF:
996 case elfcpp::R_386_TLS_DTPMOD32:
997 case elfcpp::R_386_TLS_DTPOFF32:
998 case elfcpp::R_386_TLS_TPOFF32:
999 case elfcpp::R_386_TLS_DESC:
1000 gold_error(_("%s: unexpected reloc %u in object file"),
1001 object->name().c_str(), r_type);
1004 // These are initial TLS relocs, which are expected when
1006 case elfcpp::R_386_TLS_GD: // Global-dynamic
1007 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1008 case elfcpp::R_386_TLS_DESC_CALL:
1009 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1010 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1011 case elfcpp::R_386_TLS_IE: // Initial-exec
1012 case elfcpp::R_386_TLS_IE_32:
1013 case elfcpp::R_386_TLS_GOTIE:
1014 case elfcpp::R_386_TLS_LE: // Local-exec
1015 case elfcpp::R_386_TLS_LE_32:
1017 bool output_is_shared = parameters->options().shared();
1018 const tls::Tls_optimization optimized_type
1019 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1022 case elfcpp::R_386_TLS_GD: // Global-dynamic
1023 if (optimized_type == tls::TLSOPT_NONE)
1025 // Create a pair of GOT entries for the module index and
1026 // dtv-relative offset.
1027 Output_data_got<32, false>* got
1028 = target->got_section(symtab, layout);
1029 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1030 unsigned int shndx = lsym.get_st_shndx();
1032 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1034 object->error(_("local symbol %u has bad shndx %u"),
1037 got->add_local_pair_with_rel(object, r_sym, shndx,
1039 target->rel_dyn_section(layout),
1040 elfcpp::R_386_TLS_DTPMOD32, 0);
1042 else if (optimized_type != tls::TLSOPT_TO_LE)
1043 unsupported_reloc_local(object, r_type);
1046 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1047 target->define_tls_base_symbol(symtab, layout);
1048 if (optimized_type == tls::TLSOPT_NONE)
1050 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1051 Output_data_got<32, false>* got
1052 = target->got_section(symtab, layout);
1053 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1054 unsigned int shndx = lsym.get_st_shndx();
1056 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1058 object->error(_("local symbol %u has bad shndx %u"),
1061 got->add_local_pair_with_rel(object, r_sym, shndx,
1063 target->rel_dyn_section(layout),
1064 elfcpp::R_386_TLS_DESC, 0);
1066 else if (optimized_type != tls::TLSOPT_TO_LE)
1067 unsupported_reloc_local(object, r_type);
1070 case elfcpp::R_386_TLS_DESC_CALL:
1073 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1074 if (optimized_type == tls::TLSOPT_NONE)
1076 // Create a GOT entry for the module index.
1077 target->got_mod_index_entry(symtab, layout, object);
1079 else if (optimized_type != tls::TLSOPT_TO_LE)
1080 unsupported_reloc_local(object, r_type);
1083 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1086 case elfcpp::R_386_TLS_IE: // Initial-exec
1087 case elfcpp::R_386_TLS_IE_32:
1088 case elfcpp::R_386_TLS_GOTIE:
1089 layout->set_has_static_tls();
1090 if (optimized_type == tls::TLSOPT_NONE)
1092 // For the R_386_TLS_IE relocation, we need to create a
1093 // dynamic relocation when building a shared library.
1094 if (r_type == elfcpp::R_386_TLS_IE
1095 && parameters->options().shared())
1097 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1099 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1100 rel_dyn->add_local_relative(object, r_sym,
1101 elfcpp::R_386_RELATIVE,
1102 output_section, data_shndx,
1103 reloc.get_r_offset());
1105 // Create a GOT entry for the tp-relative offset.
1106 Output_data_got<32, false>* got
1107 = target->got_section(symtab, layout);
1108 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1109 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1110 ? elfcpp::R_386_TLS_TPOFF32
1111 : elfcpp::R_386_TLS_TPOFF);
1112 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1113 ? GOT_TYPE_TLS_OFFSET
1114 : GOT_TYPE_TLS_NOFFSET);
1115 got->add_local_with_rel(object, r_sym, got_type,
1116 target->rel_dyn_section(layout),
1119 else if (optimized_type != tls::TLSOPT_TO_LE)
1120 unsupported_reloc_local(object, r_type);
1123 case elfcpp::R_386_TLS_LE: // Local-exec
1124 case elfcpp::R_386_TLS_LE_32:
1125 layout->set_has_static_tls();
1126 if (output_is_shared)
1128 // We need to create a dynamic relocation.
1129 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1130 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1131 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1132 ? elfcpp::R_386_TLS_TPOFF32
1133 : elfcpp::R_386_TLS_TPOFF);
1134 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1135 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1136 data_shndx, reloc.get_r_offset());
1146 case elfcpp::R_386_32PLT:
1147 case elfcpp::R_386_TLS_GD_32:
1148 case elfcpp::R_386_TLS_GD_PUSH:
1149 case elfcpp::R_386_TLS_GD_CALL:
1150 case elfcpp::R_386_TLS_GD_POP:
1151 case elfcpp::R_386_TLS_LDM_32:
1152 case elfcpp::R_386_TLS_LDM_PUSH:
1153 case elfcpp::R_386_TLS_LDM_CALL:
1154 case elfcpp::R_386_TLS_LDM_POP:
1155 case elfcpp::R_386_USED_BY_INTEL_200:
1157 unsupported_reloc_local(object, r_type);
1162 // Report an unsupported relocation against a global symbol.
1165 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1166 unsigned int r_type,
1169 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1170 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1173 // Scan a relocation for a global symbol.
1176 Target_i386::Scan::global(const General_options&,
1177 Symbol_table* symtab,
1179 Target_i386* target,
1180 Sized_relobj<32, false>* object,
1181 unsigned int data_shndx,
1182 Output_section* output_section,
1183 const elfcpp::Rel<32, false>& reloc,
1184 unsigned int r_type,
1189 case elfcpp::R_386_NONE:
1190 case elfcpp::R_386_GNU_VTINHERIT:
1191 case elfcpp::R_386_GNU_VTENTRY:
1194 case elfcpp::R_386_32:
1195 case elfcpp::R_386_16:
1196 case elfcpp::R_386_8:
1198 // Make a PLT entry if necessary.
1199 if (gsym->needs_plt_entry())
1201 target->make_plt_entry(symtab, layout, gsym);
1202 // Since this is not a PC-relative relocation, we may be
1203 // taking the address of a function. In that case we need to
1204 // set the entry in the dynamic symbol table to the address of
1206 if (gsym->is_from_dynobj() && !parameters->options().shared())
1207 gsym->set_needs_dynsym_value();
1209 // Make a dynamic relocation if necessary.
1210 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1212 if (target->may_need_copy_reloc(gsym))
1214 target->copy_reloc(symtab, layout, object,
1215 data_shndx, output_section, gsym, reloc);
1217 else if (r_type == elfcpp::R_386_32
1218 && gsym->can_use_relative_reloc(false))
1220 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1221 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1222 output_section, object,
1223 data_shndx, reloc.get_r_offset());
1227 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1228 rel_dyn->add_global(gsym, r_type, output_section, object,
1229 data_shndx, reloc.get_r_offset());
1235 case elfcpp::R_386_PC32:
1236 case elfcpp::R_386_PC16:
1237 case elfcpp::R_386_PC8:
1239 // Make a PLT entry if necessary.
1240 if (gsym->needs_plt_entry())
1242 // These relocations are used for function calls only in
1243 // non-PIC code. For a 32-bit relocation in a shared library,
1244 // we'll need a text relocation anyway, so we can skip the
1245 // PLT entry and let the dynamic linker bind the call directly
1246 // to the target. For smaller relocations, we should use a
1247 // PLT entry to ensure that the call can reach.
1248 if (!parameters->options().shared()
1249 || r_type != elfcpp::R_386_PC32)
1250 target->make_plt_entry(symtab, layout, gsym);
1252 // Make a dynamic relocation if necessary.
1253 int flags = Symbol::NON_PIC_REF;
1254 if (gsym->type() == elfcpp::STT_FUNC)
1255 flags |= Symbol::FUNCTION_CALL;
1256 if (gsym->needs_dynamic_reloc(flags))
1258 if (target->may_need_copy_reloc(gsym))
1260 target->copy_reloc(symtab, layout, object,
1261 data_shndx, output_section, gsym, reloc);
1265 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1266 rel_dyn->add_global(gsym, r_type, output_section, object,
1267 data_shndx, reloc.get_r_offset());
1273 case elfcpp::R_386_GOT32:
1275 // The symbol requires a GOT entry.
1276 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1277 if (gsym->final_value_is_known())
1278 got->add_global(gsym, GOT_TYPE_STANDARD);
1281 // If this symbol is not fully resolved, we need to add a
1282 // GOT entry with a dynamic relocation.
1283 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1284 if (gsym->is_from_dynobj()
1285 || gsym->is_undefined()
1286 || gsym->is_preemptible())
1287 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1288 rel_dyn, elfcpp::R_386_GLOB_DAT);
1291 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1292 rel_dyn->add_global_relative(
1293 gsym, elfcpp::R_386_RELATIVE, got,
1294 gsym->got_offset(GOT_TYPE_STANDARD));
1300 case elfcpp::R_386_PLT32:
1301 // If the symbol is fully resolved, this is just a PC32 reloc.
1302 // Otherwise we need a PLT entry.
1303 if (gsym->final_value_is_known())
1305 // If building a shared library, we can also skip the PLT entry
1306 // if the symbol is defined in the output file and is protected
1308 if (gsym->is_defined()
1309 && !gsym->is_from_dynobj()
1310 && !gsym->is_preemptible())
1312 target->make_plt_entry(symtab, layout, gsym);
1315 case elfcpp::R_386_GOTOFF:
1316 case elfcpp::R_386_GOTPC:
1317 // We need a GOT section.
1318 target->got_section(symtab, layout);
1321 // These are relocations which should only be seen by the
1322 // dynamic linker, and should never be seen here.
1323 case elfcpp::R_386_COPY:
1324 case elfcpp::R_386_GLOB_DAT:
1325 case elfcpp::R_386_JUMP_SLOT:
1326 case elfcpp::R_386_RELATIVE:
1327 case elfcpp::R_386_TLS_TPOFF:
1328 case elfcpp::R_386_TLS_DTPMOD32:
1329 case elfcpp::R_386_TLS_DTPOFF32:
1330 case elfcpp::R_386_TLS_TPOFF32:
1331 case elfcpp::R_386_TLS_DESC:
1332 gold_error(_("%s: unexpected reloc %u in object file"),
1333 object->name().c_str(), r_type);
1336 // These are initial tls relocs, which are expected when
1338 case elfcpp::R_386_TLS_GD: // Global-dynamic
1339 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1340 case elfcpp::R_386_TLS_DESC_CALL:
1341 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1342 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1343 case elfcpp::R_386_TLS_IE: // Initial-exec
1344 case elfcpp::R_386_TLS_IE_32:
1345 case elfcpp::R_386_TLS_GOTIE:
1346 case elfcpp::R_386_TLS_LE: // Local-exec
1347 case elfcpp::R_386_TLS_LE_32:
1349 const bool is_final = gsym->final_value_is_known();
1350 const tls::Tls_optimization optimized_type
1351 = Target_i386::optimize_tls_reloc(is_final, r_type);
1354 case elfcpp::R_386_TLS_GD: // Global-dynamic
1355 if (optimized_type == tls::TLSOPT_NONE)
1357 // Create a pair of GOT entries for the module index and
1358 // dtv-relative offset.
1359 Output_data_got<32, false>* got
1360 = target->got_section(symtab, layout);
1361 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1362 target->rel_dyn_section(layout),
1363 elfcpp::R_386_TLS_DTPMOD32,
1364 elfcpp::R_386_TLS_DTPOFF32);
1366 else if (optimized_type == tls::TLSOPT_TO_IE)
1368 // Create a GOT entry for the tp-relative offset.
1369 Output_data_got<32, false>* got
1370 = target->got_section(symtab, layout);
1371 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1372 target->rel_dyn_section(layout),
1373 elfcpp::R_386_TLS_TPOFF);
1375 else if (optimized_type != tls::TLSOPT_TO_LE)
1376 unsupported_reloc_global(object, r_type, gsym);
1379 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1380 target->define_tls_base_symbol(symtab, layout);
1381 if (optimized_type == tls::TLSOPT_NONE)
1383 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1384 Output_data_got<32, false>* got
1385 = target->got_section(symtab, layout);
1386 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC,
1387 target->rel_dyn_section(layout),
1388 elfcpp::R_386_TLS_DESC, 0);
1390 else if (optimized_type == tls::TLSOPT_TO_IE)
1392 // Create a GOT entry for the tp-relative offset.
1393 Output_data_got<32, false>* got
1394 = target->got_section(symtab, layout);
1395 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1396 target->rel_dyn_section(layout),
1397 elfcpp::R_386_TLS_TPOFF);
1399 else if (optimized_type != tls::TLSOPT_TO_LE)
1400 unsupported_reloc_global(object, r_type, gsym);
1403 case elfcpp::R_386_TLS_DESC_CALL:
1406 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1407 if (optimized_type == tls::TLSOPT_NONE)
1409 // Create a GOT entry for the module index.
1410 target->got_mod_index_entry(symtab, layout, object);
1412 else if (optimized_type != tls::TLSOPT_TO_LE)
1413 unsupported_reloc_global(object, r_type, gsym);
1416 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1419 case elfcpp::R_386_TLS_IE: // Initial-exec
1420 case elfcpp::R_386_TLS_IE_32:
1421 case elfcpp::R_386_TLS_GOTIE:
1422 layout->set_has_static_tls();
1423 if (optimized_type == tls::TLSOPT_NONE)
1425 // For the R_386_TLS_IE relocation, we need to create a
1426 // dynamic relocation when building a shared library.
1427 if (r_type == elfcpp::R_386_TLS_IE
1428 && parameters->options().shared())
1430 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1431 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1432 output_section, object,
1434 reloc.get_r_offset());
1436 // Create a GOT entry for the tp-relative offset.
1437 Output_data_got<32, false>* got
1438 = target->got_section(symtab, layout);
1439 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1440 ? elfcpp::R_386_TLS_TPOFF32
1441 : elfcpp::R_386_TLS_TPOFF);
1442 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1443 ? GOT_TYPE_TLS_OFFSET
1444 : GOT_TYPE_TLS_NOFFSET);
1445 got->add_global_with_rel(gsym, got_type,
1446 target->rel_dyn_section(layout),
1449 else if (optimized_type != tls::TLSOPT_TO_LE)
1450 unsupported_reloc_global(object, r_type, gsym);
1453 case elfcpp::R_386_TLS_LE: // Local-exec
1454 case elfcpp::R_386_TLS_LE_32:
1455 layout->set_has_static_tls();
1456 if (parameters->options().shared())
1458 // We need to create a dynamic relocation.
1459 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1460 ? elfcpp::R_386_TLS_TPOFF32
1461 : elfcpp::R_386_TLS_TPOFF);
1462 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1463 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1464 data_shndx, reloc.get_r_offset());
1474 case elfcpp::R_386_32PLT:
1475 case elfcpp::R_386_TLS_GD_32:
1476 case elfcpp::R_386_TLS_GD_PUSH:
1477 case elfcpp::R_386_TLS_GD_CALL:
1478 case elfcpp::R_386_TLS_GD_POP:
1479 case elfcpp::R_386_TLS_LDM_32:
1480 case elfcpp::R_386_TLS_LDM_PUSH:
1481 case elfcpp::R_386_TLS_LDM_CALL:
1482 case elfcpp::R_386_TLS_LDM_POP:
1483 case elfcpp::R_386_USED_BY_INTEL_200:
1485 unsupported_reloc_global(object, r_type, gsym);
1490 // Process relocations for gc.
1493 Target_i386::gc_process_relocs(const General_options& options,
1494 Symbol_table* symtab,
1496 Sized_relobj<32, false>* object,
1497 unsigned int data_shndx,
1499 const unsigned char* prelocs,
1501 Output_section* output_section,
1502 bool needs_special_offset_handling,
1503 size_t local_symbol_count,
1504 const unsigned char* plocal_symbols)
1506 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1517 needs_special_offset_handling,
1522 // Scan relocations for a section.
1525 Target_i386::scan_relocs(const General_options& options,
1526 Symbol_table* symtab,
1528 Sized_relobj<32, false>* object,
1529 unsigned int data_shndx,
1530 unsigned int sh_type,
1531 const unsigned char* prelocs,
1533 Output_section* output_section,
1534 bool needs_special_offset_handling,
1535 size_t local_symbol_count,
1536 const unsigned char* plocal_symbols)
1538 if (sh_type == elfcpp::SHT_RELA)
1540 gold_error(_("%s: unsupported RELA reloc section"),
1541 object->name().c_str());
1545 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1556 needs_special_offset_handling,
1561 // Finalize the sections.
1564 Target_i386::do_finalize_sections(Layout* layout)
1566 // Fill in some more dynamic tags.
1567 Output_data_dynamic* const odyn = layout->dynamic_data();
1570 if (this->got_plt_ != NULL)
1571 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1573 if (this->plt_ != NULL)
1575 const Output_data* od = this->plt_->rel_plt();
1576 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1577 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1578 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1581 if (this->rel_dyn_ != NULL)
1583 const Output_data* od = this->rel_dyn_;
1584 odyn->add_section_address(elfcpp::DT_REL, od);
1585 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1586 odyn->add_constant(elfcpp::DT_RELENT,
1587 elfcpp::Elf_sizes<32>::rel_size);
1590 if (!parameters->options().shared())
1592 // The value of the DT_DEBUG tag is filled in by the dynamic
1593 // linker at run time, and used by the debugger.
1594 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1598 // Emit any relocs we saved in an attempt to avoid generating COPY
1600 if (this->copy_relocs_.any_saved_relocs())
1601 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1604 // Return whether a direct absolute static relocation needs to be applied.
1605 // In cases where Scan::local() or Scan::global() has created
1606 // a dynamic relocation other than R_386_RELATIVE, the addend
1607 // of the relocation is carried in the data, and we must not
1608 // apply the static relocation.
1611 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1614 Output_section* output_section)
1616 // If the output section is not allocated, then we didn't call
1617 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1619 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1622 // For local symbols, we will have created a non-RELATIVE dynamic
1623 // relocation only if (a) the output is position independent,
1624 // (b) the relocation is absolute (not pc- or segment-relative), and
1625 // (c) the relocation is not 32 bits wide.
1627 return !(parameters->options().output_is_position_independent()
1628 && (ref_flags & Symbol::ABSOLUTE_REF)
1631 // For global symbols, we use the same helper routines used in the
1632 // scan pass. If we did not create a dynamic relocation, or if we
1633 // created a RELATIVE dynamic relocation, we should apply the static
1635 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1636 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1637 && gsym->can_use_relative_reloc(ref_flags
1638 & Symbol::FUNCTION_CALL);
1639 return !has_dyn || is_rel;
1642 // Perform a relocation.
1645 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1646 Target_i386* target,
1647 Output_section *output_section,
1649 const elfcpp::Rel<32, false>& rel,
1650 unsigned int r_type,
1651 const Sized_symbol<32>* gsym,
1652 const Symbol_value<32>* psymval,
1653 unsigned char* view,
1654 elfcpp::Elf_types<32>::Elf_Addr address,
1655 section_size_type view_size)
1657 if (this->skip_call_tls_get_addr_)
1659 if ((r_type != elfcpp::R_386_PLT32
1660 && r_type != elfcpp::R_386_PC32)
1662 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1663 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1664 _("missing expected TLS relocation"));
1667 this->skip_call_tls_get_addr_ = false;
1672 // Pick the value to use for symbols defined in shared objects.
1673 Symbol_value<32> symval;
1675 && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
1676 || r_type == elfcpp::R_386_PC16
1677 || r_type == elfcpp::R_386_PC32))
1679 symval.set_output_value(target->plt_section()->address()
1680 + gsym->plt_offset());
1684 const Sized_relobj<32, false>* object = relinfo->object;
1686 // Get the GOT offset if needed.
1687 // The GOT pointer points to the end of the GOT section.
1688 // We need to subtract the size of the GOT section to get
1689 // the actual offset to use in the relocation.
1690 bool have_got_offset = false;
1691 unsigned int got_offset = 0;
1694 case elfcpp::R_386_GOT32:
1697 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1698 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1699 - target->got_size());
1703 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1704 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1705 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1706 - target->got_size());
1708 have_got_offset = true;
1717 case elfcpp::R_386_NONE:
1718 case elfcpp::R_386_GNU_VTINHERIT:
1719 case elfcpp::R_386_GNU_VTENTRY:
1722 case elfcpp::R_386_32:
1723 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1725 Relocate_functions<32, false>::rel32(view, object, psymval);
1728 case elfcpp::R_386_PC32:
1730 int ref_flags = Symbol::NON_PIC_REF;
1731 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1732 ref_flags |= Symbol::FUNCTION_CALL;
1733 if (should_apply_static_reloc(gsym, ref_flags, true, output_section))
1734 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1738 case elfcpp::R_386_16:
1739 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1741 Relocate_functions<32, false>::rel16(view, object, psymval);
1744 case elfcpp::R_386_PC16:
1746 int ref_flags = Symbol::NON_PIC_REF;
1747 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1748 ref_flags |= Symbol::FUNCTION_CALL;
1749 if (should_apply_static_reloc(gsym, ref_flags, false, output_section))
1750 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1754 case elfcpp::R_386_8:
1755 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1757 Relocate_functions<32, false>::rel8(view, object, psymval);
1760 case elfcpp::R_386_PC8:
1762 int ref_flags = Symbol::NON_PIC_REF;
1763 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1764 ref_flags |= Symbol::FUNCTION_CALL;
1765 if (should_apply_static_reloc(gsym, ref_flags, false,
1767 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1771 case elfcpp::R_386_PLT32:
1772 gold_assert(gsym == NULL
1773 || gsym->has_plt_offset()
1774 || gsym->final_value_is_known()
1775 || (gsym->is_defined()
1776 && !gsym->is_from_dynobj()
1777 && !gsym->is_preemptible()));
1778 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1781 case elfcpp::R_386_GOT32:
1782 gold_assert(have_got_offset);
1783 Relocate_functions<32, false>::rel32(view, got_offset);
1786 case elfcpp::R_386_GOTOFF:
1788 elfcpp::Elf_types<32>::Elf_Addr value;
1789 value = (psymval->value(object, 0)
1790 - target->got_plt_section()->address());
1791 Relocate_functions<32, false>::rel32(view, value);
1795 case elfcpp::R_386_GOTPC:
1797 elfcpp::Elf_types<32>::Elf_Addr value;
1798 value = target->got_plt_section()->address();
1799 Relocate_functions<32, false>::pcrel32(view, value, address);
1803 case elfcpp::R_386_COPY:
1804 case elfcpp::R_386_GLOB_DAT:
1805 case elfcpp::R_386_JUMP_SLOT:
1806 case elfcpp::R_386_RELATIVE:
1807 // These are outstanding tls relocs, which are unexpected when
1809 case elfcpp::R_386_TLS_TPOFF:
1810 case elfcpp::R_386_TLS_DTPMOD32:
1811 case elfcpp::R_386_TLS_DTPOFF32:
1812 case elfcpp::R_386_TLS_TPOFF32:
1813 case elfcpp::R_386_TLS_DESC:
1814 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1815 _("unexpected reloc %u in object file"),
1819 // These are initial tls relocs, which are expected when
1821 case elfcpp::R_386_TLS_GD: // Global-dynamic
1822 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1823 case elfcpp::R_386_TLS_DESC_CALL:
1824 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1825 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1826 case elfcpp::R_386_TLS_IE: // Initial-exec
1827 case elfcpp::R_386_TLS_IE_32:
1828 case elfcpp::R_386_TLS_GOTIE:
1829 case elfcpp::R_386_TLS_LE: // Local-exec
1830 case elfcpp::R_386_TLS_LE_32:
1831 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1832 view, address, view_size);
1835 case elfcpp::R_386_32PLT:
1836 case elfcpp::R_386_TLS_GD_32:
1837 case elfcpp::R_386_TLS_GD_PUSH:
1838 case elfcpp::R_386_TLS_GD_CALL:
1839 case elfcpp::R_386_TLS_GD_POP:
1840 case elfcpp::R_386_TLS_LDM_32:
1841 case elfcpp::R_386_TLS_LDM_PUSH:
1842 case elfcpp::R_386_TLS_LDM_CALL:
1843 case elfcpp::R_386_TLS_LDM_POP:
1844 case elfcpp::R_386_USED_BY_INTEL_200:
1846 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1847 _("unsupported reloc %u"),
1855 // Perform a TLS relocation.
1858 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1859 Target_i386* target,
1861 const elfcpp::Rel<32, false>& rel,
1862 unsigned int r_type,
1863 const Sized_symbol<32>* gsym,
1864 const Symbol_value<32>* psymval,
1865 unsigned char* view,
1866 elfcpp::Elf_types<32>::Elf_Addr,
1867 section_size_type view_size)
1869 Output_segment* tls_segment = relinfo->layout->tls_segment();
1871 const Sized_relobj<32, false>* object = relinfo->object;
1873 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1875 const bool is_final =
1877 ? !parameters->options().output_is_position_independent()
1878 : gsym->final_value_is_known());
1879 const tls::Tls_optimization optimized_type
1880 = Target_i386::optimize_tls_reloc(is_final, r_type);
1883 case elfcpp::R_386_TLS_GD: // Global-dynamic
1884 if (optimized_type == tls::TLSOPT_TO_LE)
1886 gold_assert(tls_segment != NULL);
1887 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1888 rel, r_type, value, view,
1894 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1895 ? GOT_TYPE_TLS_NOFFSET
1896 : GOT_TYPE_TLS_PAIR);
1897 unsigned int got_offset;
1900 gold_assert(gsym->has_got_offset(got_type));
1901 got_offset = gsym->got_offset(got_type) - target->got_size();
1905 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1906 gold_assert(object->local_has_got_offset(r_sym, got_type));
1907 got_offset = (object->local_got_offset(r_sym, got_type)
1908 - target->got_size());
1910 if (optimized_type == tls::TLSOPT_TO_IE)
1912 gold_assert(tls_segment != NULL);
1913 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1914 got_offset, view, view_size);
1917 else if (optimized_type == tls::TLSOPT_NONE)
1919 // Relocate the field with the offset of the pair of GOT
1921 Relocate_functions<32, false>::rel32(view, got_offset);
1925 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1926 _("unsupported reloc %u"),
1930 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1931 case elfcpp::R_386_TLS_DESC_CALL:
1932 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1933 if (optimized_type == tls::TLSOPT_TO_LE)
1935 gold_assert(tls_segment != NULL);
1936 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1937 rel, r_type, value, view,
1943 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1944 ? GOT_TYPE_TLS_NOFFSET
1945 : GOT_TYPE_TLS_DESC);
1946 unsigned int got_offset;
1949 gold_assert(gsym->has_got_offset(got_type));
1950 got_offset = gsym->got_offset(got_type) - target->got_size();
1954 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1955 gold_assert(object->local_has_got_offset(r_sym, got_type));
1956 got_offset = (object->local_got_offset(r_sym, got_type)
1957 - target->got_size());
1959 if (optimized_type == tls::TLSOPT_TO_IE)
1961 gold_assert(tls_segment != NULL);
1962 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1963 got_offset, view, view_size);
1966 else if (optimized_type == tls::TLSOPT_NONE)
1968 if (r_type == elfcpp::R_386_TLS_GOTDESC)
1970 // Relocate the field with the offset of the pair of GOT
1972 Relocate_functions<32, false>::rel32(view, got_offset);
1977 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1978 _("unsupported reloc %u"),
1982 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1983 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1985 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1986 _("both SUN and GNU model "
1987 "TLS relocations"));
1990 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1991 if (optimized_type == tls::TLSOPT_TO_LE)
1993 gold_assert(tls_segment != NULL);
1994 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1995 value, view, view_size);
1998 else if (optimized_type == tls::TLSOPT_NONE)
2000 // Relocate the field with the offset of the GOT entry for
2001 // the module index.
2002 unsigned int got_offset;
2003 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2004 - target->got_size());
2005 Relocate_functions<32, false>::rel32(view, got_offset);
2008 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2009 _("unsupported reloc %u"),
2013 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2014 // This reloc can appear in debugging sections, in which case we
2015 // won't see the TLS_LDM reloc. The local_dynamic_type field
2017 if (optimized_type == tls::TLSOPT_TO_LE
2018 && this->local_dynamic_type_ != LOCAL_DYNAMIC_NONE)
2020 gold_assert(tls_segment != NULL);
2021 value -= tls_segment->memsz();
2023 Relocate_functions<32, false>::rel32(view, value);
2026 case elfcpp::R_386_TLS_IE: // Initial-exec
2027 case elfcpp::R_386_TLS_GOTIE:
2028 case elfcpp::R_386_TLS_IE_32:
2029 if (optimized_type == tls::TLSOPT_TO_LE)
2031 gold_assert(tls_segment != NULL);
2032 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2033 rel, r_type, value, view,
2037 else if (optimized_type == tls::TLSOPT_NONE)
2039 // Relocate the field with the offset of the GOT entry for
2040 // the tp-relative offset of the symbol.
2041 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2042 ? GOT_TYPE_TLS_OFFSET
2043 : GOT_TYPE_TLS_NOFFSET);
2044 unsigned int got_offset;
2047 gold_assert(gsym->has_got_offset(got_type));
2048 got_offset = gsym->got_offset(got_type);
2052 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2053 gold_assert(object->local_has_got_offset(r_sym, got_type));
2054 got_offset = object->local_got_offset(r_sym, got_type);
2056 // For the R_386_TLS_IE relocation, we need to apply the
2057 // absolute address of the GOT entry.
2058 if (r_type == elfcpp::R_386_TLS_IE)
2059 got_offset += target->got_plt_section()->address();
2060 // All GOT offsets are relative to the end of the GOT.
2061 got_offset -= target->got_size();
2062 Relocate_functions<32, false>::rel32(view, got_offset);
2065 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2066 _("unsupported reloc %u"),
2070 case elfcpp::R_386_TLS_LE: // Local-exec
2071 // If we're creating a shared library, a dynamic relocation will
2072 // have been created for this location, so do not apply it now.
2073 if (!parameters->options().shared())
2075 gold_assert(tls_segment != NULL);
2076 value -= tls_segment->memsz();
2077 Relocate_functions<32, false>::rel32(view, value);
2081 case elfcpp::R_386_TLS_LE_32:
2082 // If we're creating a shared library, a dynamic relocation will
2083 // have been created for this location, so do not apply it now.
2084 if (!parameters->options().shared())
2086 gold_assert(tls_segment != NULL);
2087 value = tls_segment->memsz() - value;
2088 Relocate_functions<32, false>::rel32(view, value);
2094 // Do a relocation in which we convert a TLS General-Dynamic to a
2098 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2100 Output_segment* tls_segment,
2101 const elfcpp::Rel<32, false>& rel,
2103 elfcpp::Elf_types<32>::Elf_Addr value,
2104 unsigned char* view,
2105 section_size_type view_size)
2107 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2108 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2109 // leal foo(%reg),%eax; call ___tls_get_addr
2110 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2112 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2113 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2115 unsigned char op1 = view[-1];
2116 unsigned char op2 = view[-2];
2118 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2119 op2 == 0x8d || op2 == 0x04);
2120 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2126 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2127 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2128 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2129 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2130 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2134 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2135 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2136 if (rel.get_r_offset() + 9 < view_size
2139 // There is a trailing nop. Use the size byte subl.
2140 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2145 // Use the five byte subl.
2146 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2150 value = tls_segment->memsz() - value;
2151 Relocate_functions<32, false>::rel32(view + roff, value);
2153 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2155 this->skip_call_tls_get_addr_ = true;
2158 // Do a relocation in which we convert a TLS General-Dynamic to an
2162 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2165 const elfcpp::Rel<32, false>& rel,
2167 elfcpp::Elf_types<32>::Elf_Addr value,
2168 unsigned char* view,
2169 section_size_type view_size)
2171 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2172 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2174 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2175 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2177 unsigned char op1 = view[-1];
2178 unsigned char op2 = view[-2];
2180 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2181 op2 == 0x8d || op2 == 0x04);
2182 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2186 // FIXME: For now, support only the first (SIB) form.
2187 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2191 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2192 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2193 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2194 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2195 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2199 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2200 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2201 if (rel.get_r_offset() + 9 < view_size
2204 // FIXME: This is not the right instruction sequence.
2205 // There is a trailing nop. Use the size byte subl.
2206 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2211 // FIXME: This is not the right instruction sequence.
2212 // Use the five byte subl.
2213 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2217 Relocate_functions<32, false>::rel32(view + roff, value);
2219 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2221 this->skip_call_tls_get_addr_ = true;
2224 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2225 // General-Dynamic to a Local-Exec.
2228 Target_i386::Relocate::tls_desc_gd_to_le(
2229 const Relocate_info<32, false>* relinfo,
2231 Output_segment* tls_segment,
2232 const elfcpp::Rel<32, false>& rel,
2233 unsigned int r_type,
2234 elfcpp::Elf_types<32>::Elf_Addr value,
2235 unsigned char* view,
2236 section_size_type view_size)
2238 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2240 // leal foo@TLSDESC(%ebx), %eax
2241 // ==> leal foo@NTPOFF, %eax
2242 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2243 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2244 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2245 view[-2] == 0x8d && view[-1] == 0x83);
2247 value -= tls_segment->memsz();
2248 Relocate_functions<32, false>::rel32(view, value);
2252 // call *foo@TLSCALL(%eax)
2254 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2255 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2256 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2257 view[0] == 0xff && view[1] == 0x10);
2263 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2264 // General-Dynamic to an Initial-Exec.
2267 Target_i386::Relocate::tls_desc_gd_to_ie(
2268 const Relocate_info<32, false>* relinfo,
2271 const elfcpp::Rel<32, false>& rel,
2272 unsigned int r_type,
2273 elfcpp::Elf_types<32>::Elf_Addr value,
2274 unsigned char* view,
2275 section_size_type view_size)
2277 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2279 // leal foo@TLSDESC(%ebx), %eax
2280 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2281 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2282 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2283 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2284 view[-2] == 0x8d && view[-1] == 0x83);
2286 Relocate_functions<32, false>::rel32(view, value);
2290 // call *foo@TLSCALL(%eax)
2292 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2293 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2294 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2295 view[0] == 0xff && view[1] == 0x10);
2301 // Do a relocation in which we convert a TLS Local-Dynamic to a
2305 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2308 const elfcpp::Rel<32, false>& rel,
2310 elfcpp::Elf_types<32>::Elf_Addr,
2311 unsigned char* view,
2312 section_size_type view_size)
2314 // leal foo(%reg), %eax; call ___tls_get_addr
2315 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2317 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2318 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2320 // FIXME: Does this test really always pass?
2321 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2322 view[-2] == 0x8d && view[-1] == 0x83);
2324 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2326 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2328 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2330 this->skip_call_tls_get_addr_ = true;
2333 // Do a relocation in which we convert a TLS Initial-Exec to a
2337 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2339 Output_segment* tls_segment,
2340 const elfcpp::Rel<32, false>& rel,
2341 unsigned int r_type,
2342 elfcpp::Elf_types<32>::Elf_Addr value,
2343 unsigned char* view,
2344 section_size_type view_size)
2346 // We have to actually change the instructions, which means that we
2347 // need to examine the opcodes to figure out which instruction we
2349 if (r_type == elfcpp::R_386_TLS_IE)
2351 // movl %gs:XX,%eax ==> movl $YY,%eax
2352 // movl %gs:XX,%reg ==> movl $YY,%reg
2353 // addl %gs:XX,%reg ==> addl $YY,%reg
2354 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2355 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2357 unsigned char op1 = view[-1];
2360 // movl XX,%eax ==> movl $YY,%eax
2365 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2367 unsigned char op2 = view[-2];
2370 // movl XX,%reg ==> movl $YY,%reg
2371 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2372 (op1 & 0xc7) == 0x05);
2374 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2376 else if (op2 == 0x03)
2378 // addl XX,%reg ==> addl $YY,%reg
2379 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2380 (op1 & 0xc7) == 0x05);
2382 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2385 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2390 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2391 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2392 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2393 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2394 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2396 unsigned char op1 = view[-1];
2397 unsigned char op2 = view[-2];
2398 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2399 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2402 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2404 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2406 else if (op2 == 0x2b)
2408 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2410 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2412 else if (op2 == 0x03)
2414 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2416 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2419 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2422 value = tls_segment->memsz() - value;
2423 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2426 Relocate_functions<32, false>::rel32(view, value);
2429 // Relocate section data.
2432 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2433 unsigned int sh_type,
2434 const unsigned char* prelocs,
2436 Output_section* output_section,
2437 bool needs_special_offset_handling,
2438 unsigned char* view,
2439 elfcpp::Elf_types<32>::Elf_Addr address,
2440 section_size_type view_size)
2442 gold_assert(sh_type == elfcpp::SHT_REL);
2444 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2445 Target_i386::Relocate>(
2451 needs_special_offset_handling,
2457 // Return the size of a relocation while scanning during a relocatable
2461 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2462 unsigned int r_type,
2467 case elfcpp::R_386_NONE:
2468 case elfcpp::R_386_GNU_VTINHERIT:
2469 case elfcpp::R_386_GNU_VTENTRY:
2470 case elfcpp::R_386_TLS_GD: // Global-dynamic
2471 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2472 case elfcpp::R_386_TLS_DESC_CALL:
2473 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2474 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2475 case elfcpp::R_386_TLS_IE: // Initial-exec
2476 case elfcpp::R_386_TLS_IE_32:
2477 case elfcpp::R_386_TLS_GOTIE:
2478 case elfcpp::R_386_TLS_LE: // Local-exec
2479 case elfcpp::R_386_TLS_LE_32:
2482 case elfcpp::R_386_32:
2483 case elfcpp::R_386_PC32:
2484 case elfcpp::R_386_GOT32:
2485 case elfcpp::R_386_PLT32:
2486 case elfcpp::R_386_GOTOFF:
2487 case elfcpp::R_386_GOTPC:
2490 case elfcpp::R_386_16:
2491 case elfcpp::R_386_PC16:
2494 case elfcpp::R_386_8:
2495 case elfcpp::R_386_PC8:
2498 // These are relocations which should only be seen by the
2499 // dynamic linker, and should never be seen here.
2500 case elfcpp::R_386_COPY:
2501 case elfcpp::R_386_GLOB_DAT:
2502 case elfcpp::R_386_JUMP_SLOT:
2503 case elfcpp::R_386_RELATIVE:
2504 case elfcpp::R_386_TLS_TPOFF:
2505 case elfcpp::R_386_TLS_DTPMOD32:
2506 case elfcpp::R_386_TLS_DTPOFF32:
2507 case elfcpp::R_386_TLS_TPOFF32:
2508 case elfcpp::R_386_TLS_DESC:
2509 object->error(_("unexpected reloc %u in object file"), r_type);
2512 case elfcpp::R_386_32PLT:
2513 case elfcpp::R_386_TLS_GD_32:
2514 case elfcpp::R_386_TLS_GD_PUSH:
2515 case elfcpp::R_386_TLS_GD_CALL:
2516 case elfcpp::R_386_TLS_GD_POP:
2517 case elfcpp::R_386_TLS_LDM_32:
2518 case elfcpp::R_386_TLS_LDM_PUSH:
2519 case elfcpp::R_386_TLS_LDM_CALL:
2520 case elfcpp::R_386_TLS_LDM_POP:
2521 case elfcpp::R_386_USED_BY_INTEL_200:
2523 object->error(_("unsupported reloc %u in object file"), r_type);
2528 // Scan the relocs during a relocatable link.
2531 Target_i386::scan_relocatable_relocs(const General_options& options,
2532 Symbol_table* symtab,
2534 Sized_relobj<32, false>* object,
2535 unsigned int data_shndx,
2536 unsigned int sh_type,
2537 const unsigned char* prelocs,
2539 Output_section* output_section,
2540 bool needs_special_offset_handling,
2541 size_t local_symbol_count,
2542 const unsigned char* plocal_symbols,
2543 Relocatable_relocs* rr)
2545 gold_assert(sh_type == elfcpp::SHT_REL);
2547 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2548 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2550 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
2551 Scan_relocatable_relocs>(
2560 needs_special_offset_handling,
2566 // Relocate a section during a relocatable link.
2569 Target_i386::relocate_for_relocatable(
2570 const Relocate_info<32, false>* relinfo,
2571 unsigned int sh_type,
2572 const unsigned char* prelocs,
2574 Output_section* output_section,
2575 off_t offset_in_output_section,
2576 const Relocatable_relocs* rr,
2577 unsigned char* view,
2578 elfcpp::Elf_types<32>::Elf_Addr view_address,
2579 section_size_type view_size,
2580 unsigned char* reloc_view,
2581 section_size_type reloc_view_size)
2583 gold_assert(sh_type == elfcpp::SHT_REL);
2585 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
2590 offset_in_output_section,
2599 // Return the value to use for a dynamic which requires special
2600 // treatment. This is how we support equality comparisons of function
2601 // pointers across shared library boundaries, as described in the
2602 // processor specific ABI supplement.
2605 Target_i386::do_dynsym_value(const Symbol* gsym) const
2607 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2608 return this->plt_section()->address() + gsym->plt_offset();
2611 // Return a string used to fill a code section with nops to take up
2612 // the specified length.
2615 Target_i386::do_code_fill(section_size_type length) const
2619 // Build a jmp instruction to skip over the bytes.
2620 unsigned char jmp[5];
2622 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2623 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2624 + std::string(length - 5, '\0'));
2627 // Nop sequences of various lengths.
2628 const char nop1[1] = { 0x90 }; // nop
2629 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2630 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2631 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2632 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2633 0x00 }; // leal 0(%esi,1),%esi
2634 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2636 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2638 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2639 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2640 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2641 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2643 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2644 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2646 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2647 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2649 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2650 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2651 0x00, 0x00, 0x00, 0x00 };
2652 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2653 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2654 0x27, 0x00, 0x00, 0x00,
2656 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2657 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2658 0xbc, 0x27, 0x00, 0x00,
2660 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2661 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2662 0x90, 0x90, 0x90, 0x90,
2665 const char* nops[16] = {
2667 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2668 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2671 return std::string(nops[length], length);
2674 // The selector for i386 object files.
2676 class Target_selector_i386 : public Target_selector_freebsd
2679 Target_selector_i386()
2680 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
2681 "elf32-i386", "elf32-i386-freebsd")
2685 do_instantiate_target()
2686 { return new Target_i386(); }
2689 Target_selector_i386 target_selector_i386;
2691 } // End anonymous namespace.