1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007 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"
36 #include "target-reloc.h"
37 #include "target-select.h"
45 class Output_data_plt_i386;
47 // The i386 target class.
49 class Target_i386 : public Sized_target<32, false>
52 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
55 : Sized_target<32, false>(&i386_info),
56 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
57 copy_relocs_(NULL), dynbss_(NULL)
60 // Scan the relocations to look for symbol adjustments.
62 scan_relocs(const General_options& options,
65 Sized_relobj<32, false>* object,
66 unsigned int data_shndx,
68 const unsigned char* prelocs,
70 size_t local_symbol_count,
71 const unsigned char* plocal_symbols,
72 Symbol** global_symbols);
74 // Finalize the sections.
76 do_finalize_sections(Layout*);
78 // Return the value to use for a dynamic which requires special
81 do_dynsym_value(const Symbol*) const;
83 // Relocate a section.
85 relocate_section(const Relocate_info<32, false>*,
87 const unsigned char* prelocs,
90 elfcpp::Elf_types<32>::Elf_Addr view_address,
93 // Return a string used to fill a code section with nops.
95 do_code_fill(off_t length);
98 // The class which scans relocations.
102 local(const General_options& options, Symbol_table* symtab,
103 Layout* layout, Target_i386* target,
104 Sized_relobj<32, false>* object,
105 unsigned int data_shndx,
106 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
107 const elfcpp::Sym<32, false>& lsym);
110 global(const General_options& options, Symbol_table* symtab,
111 Layout* layout, Target_i386* target,
112 Sized_relobj<32, false>* object,
113 unsigned int data_shndx,
114 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
118 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
121 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
125 // The class which implements relocation.
130 : skip_call_tls_get_addr_(false),
131 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
136 if (this->skip_call_tls_get_addr_)
138 // FIXME: This needs to specify the location somehow.
139 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
145 // Do a relocation. Return false if the caller should not issue
146 // any warnings about this relocation.
148 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
149 const elfcpp::Rel<32, false>&,
150 unsigned int r_type, const Sized_symbol<32>*,
151 const Symbol_value<32>*,
152 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
156 // Do a TLS relocation.
158 relocate_tls(const Relocate_info<32, false>*, size_t relnum,
159 const elfcpp::Rel<32, false>&,
160 unsigned int r_type, const Sized_symbol<32>*,
161 const Symbol_value<32>*,
162 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
164 // Do a TLS Initial-Exec to Local-Exec transition.
166 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
167 Output_segment* tls_segment,
168 const elfcpp::Rel<32, false>&, unsigned int r_type,
169 elfcpp::Elf_types<32>::Elf_Addr value,
173 // Do a TLS Global-Dynamic to Local-Exec transition.
175 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
176 Output_segment* tls_segment,
177 const elfcpp::Rel<32, false>&, unsigned int r_type,
178 elfcpp::Elf_types<32>::Elf_Addr value,
182 // Do a TLS Local-Dynamic to Local-Exec transition.
184 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
185 Output_segment* tls_segment,
186 const elfcpp::Rel<32, false>&, unsigned int r_type,
187 elfcpp::Elf_types<32>::Elf_Addr value,
191 // We need to keep track of which type of local dynamic relocation
192 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
193 enum Local_dynamic_type
200 // This is set if we should skip the next reloc, which should be a
201 // PLT32 reloc against ___tls_get_addr.
202 bool skip_call_tls_get_addr_;
203 // The type of local dynamic relocation we have seen in the section
204 // being relocated, if any.
205 Local_dynamic_type local_dynamic_type_;
208 // Adjust TLS relocation type based on the options and whether this
209 // is a local symbol.
210 static tls::Tls_optimization
211 optimize_tls_reloc(bool is_final, int r_type);
213 // Get the GOT section, creating it if necessary.
214 Output_data_got<32, false>*
215 got_section(Symbol_table*, Layout*);
217 // Create a PLT entry for a global symbol.
219 make_plt_entry(Symbol_table*, Layout*, Symbol*);
221 // Get the PLT section.
222 const Output_data_plt_i386*
225 gold_assert(this->plt_ != NULL);
229 // Get the dynamic reloc section, creating it if necessary.
231 rel_dyn_section(Layout*);
233 // Copy a relocation against a global symbol.
235 copy_reloc(const General_options*, Symbol_table*, Layout*,
236 Sized_relobj<32, false>*, unsigned int,
237 Symbol*, const elfcpp::Rel<32, false>&);
239 // Information about this specific target which we pass to the
240 // general Target structure.
241 static const Target::Target_info i386_info;
244 Output_data_got<32, false>* got_;
246 Output_data_plt_i386* plt_;
247 // The GOT PLT section.
248 Output_data_space* got_plt_;
249 // The dynamic reloc section.
250 Reloc_section* rel_dyn_;
251 // Relocs saved to avoid a COPY reloc.
252 Copy_relocs<32, false>* copy_relocs_;
253 // Space for variables copied with a COPY reloc.
254 Output_data_space* dynbss_;
257 const Target::Target_info Target_i386::i386_info =
260 false, // is_big_endian
261 elfcpp::EM_386, // machine_code
262 false, // has_make_symbol
263 false, // has_resolve
264 true, // has_code_fill
265 "/usr/lib/libc.so.1", // dynamic_linker
266 0x08048000, // text_segment_address
267 0x1000, // abi_pagesize
268 0x1000 // common_pagesize
271 // Get the GOT section, creating it if necessary.
273 Output_data_got<32, false>*
274 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
276 if (this->got_ == NULL)
278 gold_assert(symtab != NULL && layout != NULL);
280 this->got_ = new Output_data_got<32, false>();
282 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
283 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
286 // The old GNU linker creates a .got.plt section. We just
287 // create another set of data in the .got section. Note that we
288 // always create a PLT if we create a GOT, although the PLT
290 this->got_plt_ = new Output_data_space(4);
291 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
292 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
295 // The first three entries are reserved.
296 this->got_plt_->set_space_size(3 * 4);
298 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
299 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
301 0, 0, elfcpp::STT_OBJECT,
303 elfcpp::STV_HIDDEN, 0,
310 // Get the dynamic reloc section, creating it if necessary.
312 Target_i386::Reloc_section*
313 Target_i386::rel_dyn_section(Layout* layout)
315 if (this->rel_dyn_ == NULL)
317 gold_assert(layout != NULL);
318 this->rel_dyn_ = new Reloc_section();
319 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
320 elfcpp::SHF_ALLOC, this->rel_dyn_);
322 return this->rel_dyn_;
325 // A class to handle the PLT data.
327 class Output_data_plt_i386 : public Output_section_data
330 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
332 Output_data_plt_i386(Layout*, Output_data_space*);
334 // Add an entry to the PLT.
336 add_entry(Symbol* gsym);
338 // Return the .rel.plt section data.
341 { return this->rel_; }
345 do_adjust_output_section(Output_section* os);
348 // The size of an entry in the PLT.
349 static const int plt_entry_size = 16;
351 // The first entry in the PLT for an executable.
352 static unsigned char exec_first_plt_entry[plt_entry_size];
354 // The first entry in the PLT for a shared object.
355 static unsigned char dyn_first_plt_entry[plt_entry_size];
357 // Other entries in the PLT for an executable.
358 static unsigned char exec_plt_entry[plt_entry_size];
360 // Other entries in the PLT for a shared object.
361 static unsigned char dyn_plt_entry[plt_entry_size];
363 // Set the final size.
365 do_set_address(uint64_t, off_t)
366 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
368 // Write out the PLT data.
370 do_write(Output_file*);
372 // The reloc section.
374 // The .got.plt section.
375 Output_data_space* got_plt_;
376 // The number of PLT entries.
380 // Create the PLT section. The ordinary .got section is an argument,
381 // since we need to refer to the start. We also create our own .got
382 // section just for PLT entries.
384 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
385 Output_data_space* got_plt)
386 : Output_section_data(4), got_plt_(got_plt), count_(0)
388 this->rel_ = new Reloc_section();
389 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
390 elfcpp::SHF_ALLOC, this->rel_);
394 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
396 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
397 // linker, and so do we.
401 // Add an entry to the PLT.
404 Output_data_plt_i386::add_entry(Symbol* gsym)
406 gold_assert(!gsym->has_plt_offset());
408 // Note that when setting the PLT offset we skip the initial
409 // reserved PLT entry.
410 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
414 off_t got_offset = this->got_plt_->data_size();
416 // Every PLT entry needs a GOT entry which points back to the PLT
417 // entry (this will be changed by the dynamic linker, normally
418 // lazily when the function is called).
419 this->got_plt_->set_space_size(got_offset + 4);
421 // Every PLT entry needs a reloc.
422 gsym->set_needs_dynsym_entry();
423 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
426 // Note that we don't need to save the symbol. The contents of the
427 // PLT are independent of which symbols are used. The symbols only
428 // appear in the relocations.
431 // The first entry in the PLT for an executable.
433 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
435 0xff, 0x35, // pushl contents of memory address
436 0, 0, 0, 0, // replaced with address of .got + 4
437 0xff, 0x25, // jmp indirect
438 0, 0, 0, 0, // replaced with address of .got + 8
442 // The first entry in the PLT for a shared object.
444 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
446 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
447 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
451 // Subsequent entries in the PLT for an executable.
453 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
455 0xff, 0x25, // jmp indirect
456 0, 0, 0, 0, // replaced with address of symbol in .got
457 0x68, // pushl immediate
458 0, 0, 0, 0, // replaced with offset into relocation table
459 0xe9, // jmp relative
460 0, 0, 0, 0 // replaced with offset to start of .plt
463 // Subsequent entries in the PLT for a shared object.
465 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
467 0xff, 0xa3, // jmp *offset(%ebx)
468 0, 0, 0, 0, // replaced with offset of symbol in .got
469 0x68, // pushl immediate
470 0, 0, 0, 0, // replaced with offset into relocation table
471 0xe9, // jmp relative
472 0, 0, 0, 0 // replaced with offset to start of .plt
475 // Write out the PLT. This uses the hand-coded instructions above,
476 // and adjusts them as needed. This is all specified by the i386 ELF
477 // Processor Supplement.
480 Output_data_plt_i386::do_write(Output_file* of)
482 const off_t offset = this->offset();
483 const off_t oview_size = this->data_size();
484 unsigned char* const oview = of->get_output_view(offset, oview_size);
486 const off_t got_file_offset = this->got_plt_->offset();
487 const off_t got_size = this->got_plt_->data_size();
488 unsigned char* const got_view = of->get_output_view(got_file_offset,
491 unsigned char* pov = oview;
493 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
494 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
496 if (parameters->output_is_shared())
497 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
500 memcpy(pov, exec_first_plt_entry, plt_entry_size);
501 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
502 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
504 pov += plt_entry_size;
506 unsigned char* got_pov = got_view;
508 memset(got_pov, 0, 12);
511 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
513 unsigned int plt_offset = plt_entry_size;
514 unsigned int plt_rel_offset = 0;
515 unsigned int got_offset = 12;
516 const unsigned int count = this->count_;
517 for (unsigned int i = 0;
520 pov += plt_entry_size,
522 plt_offset += plt_entry_size,
523 plt_rel_offset += rel_size,
526 // Set and adjust the PLT entry itself.
528 if (parameters->output_is_shared())
530 memcpy(pov, dyn_plt_entry, plt_entry_size);
531 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
535 memcpy(pov, exec_plt_entry, plt_entry_size);
536 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
541 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
542 elfcpp::Swap<32, false>::writeval(pov + 12,
543 - (plt_offset + plt_entry_size));
545 // Set the entry in the GOT.
546 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
549 gold_assert(pov - oview == oview_size);
550 gold_assert(got_pov - got_view == got_size);
552 of->write_output_view(offset, oview_size, oview);
553 of->write_output_view(got_file_offset, got_size, got_view);
556 // Create a PLT entry for a global symbol.
559 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
561 if (gsym->has_plt_offset())
564 if (this->plt_ == NULL)
566 // Create the GOT sections first.
567 this->got_section(symtab, layout);
569 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
570 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
572 | elfcpp::SHF_EXECINSTR),
576 this->plt_->add_entry(gsym);
579 // Handle a relocation against a non-function symbol defined in a
580 // dynamic object. The traditional way to handle this is to generate
581 // a COPY relocation to copy the variable at runtime from the shared
582 // object into the executable's data segment. However, this is
583 // undesirable in general, as if the size of the object changes in the
584 // dynamic object, the executable will no longer work correctly. If
585 // this relocation is in a writable section, then we can create a
586 // dynamic reloc and the dynamic linker will resolve it to the correct
587 // address at runtime. However, we do not want do that if the
588 // relocation is in a read-only section, as it would prevent the
589 // readonly segment from being shared. And if we have to eventually
590 // generate a COPY reloc, then any dynamic relocations will be
591 // useless. So this means that if this is a writable section, we need
592 // to save the relocation until we see whether we have to create a
593 // COPY relocation for this symbol for any other relocation.
596 Target_i386::copy_reloc(const General_options* options,
597 Symbol_table* symtab,
599 Sized_relobj<32, false>* object,
600 unsigned int data_shndx, Symbol* gsym,
601 const elfcpp::Rel<32, false>& rel)
603 Sized_symbol<32>* ssym;
604 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
607 if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
610 // So far we do not need a COPY reloc. Save this relocation.
611 // If it turns out that we never need a COPY reloc for this
612 // symbol, then we will emit the relocation.
613 if (this->copy_relocs_ == NULL)
614 this->copy_relocs_ = new Copy_relocs<32, false>();
615 this->copy_relocs_->save(ssym, object, data_shndx, rel);
619 // Allocate space for this symbol in the .bss section.
621 elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
623 // There is no defined way to determine the required alignment
624 // of the symbol. We pick the alignment based on the size. We
625 // set an arbitrary maximum of 256.
627 for (align = 1; align < 512; align <<= 1)
628 if ((symsize & align) != 0)
631 if (this->dynbss_ == NULL)
633 this->dynbss_ = new Output_data_space(align);
634 layout->add_output_section_data(".bss",
637 | elfcpp::SHF_WRITE),
641 Output_data_space* dynbss = this->dynbss_;
643 if (align > dynbss->addralign())
644 dynbss->set_space_alignment(align);
646 off_t dynbss_size = dynbss->data_size();
647 dynbss_size = align_address(dynbss_size, align);
648 off_t offset = dynbss_size;
649 dynbss->set_space_size(dynbss_size + symsize);
651 // Define the symbol in the .dynbss section.
652 symtab->define_in_output_data(this, ssym->name(), ssym->version(),
653 dynbss, offset, symsize, ssym->type(),
654 ssym->binding(), ssym->visibility(),
655 ssym->nonvis(), false, false);
657 // Add the COPY reloc.
658 ssym->set_needs_dynsym_entry();
659 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
660 rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
664 // Optimize the TLS relocation type based on what we know about the
665 // symbol. IS_FINAL is true if the final address of this symbol is
666 // known at link time.
668 tls::Tls_optimization
669 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
671 // If we are generating a shared library, then we can't do anything
673 if (parameters->output_is_shared())
674 return tls::TLSOPT_NONE;
678 case elfcpp::R_386_TLS_GD:
679 case elfcpp::R_386_TLS_GOTDESC:
680 case elfcpp::R_386_TLS_DESC_CALL:
681 // These are Global-Dynamic which permits fully general TLS
682 // access. Since we know that we are generating an executable,
683 // we can convert this to Initial-Exec. If we also know that
684 // this is a local symbol, we can further switch to Local-Exec.
686 return tls::TLSOPT_TO_LE;
687 return tls::TLSOPT_TO_IE;
689 case elfcpp::R_386_TLS_LDM:
690 // This is Local-Dynamic, which refers to a local symbol in the
691 // dynamic TLS block. Since we know that we generating an
692 // executable, we can switch to Local-Exec.
693 return tls::TLSOPT_TO_LE;
695 case elfcpp::R_386_TLS_LDO_32:
696 // Another type of Local-Dynamic relocation.
697 return tls::TLSOPT_TO_LE;
699 case elfcpp::R_386_TLS_IE:
700 case elfcpp::R_386_TLS_GOTIE:
701 case elfcpp::R_386_TLS_IE_32:
702 // These are Initial-Exec relocs which get the thread offset
703 // from the GOT. If we know that we are linking against the
704 // local symbol, we can switch to Local-Exec, which links the
705 // thread offset into the instruction.
707 return tls::TLSOPT_TO_LE;
708 return tls::TLSOPT_NONE;
710 case elfcpp::R_386_TLS_LE:
711 case elfcpp::R_386_TLS_LE_32:
712 // When we already have Local-Exec, there is nothing further we
714 return tls::TLSOPT_NONE;
721 // Report an unsupported relocation against a local symbol.
724 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
727 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
728 program_name, object->name().c_str(), r_type);
731 // Scan a relocation for a local symbol.
734 Target_i386::Scan::local(const General_options&,
735 Symbol_table* symtab,
738 Sized_relobj<32, false>* object,
740 const elfcpp::Rel<32, false>&,
742 const elfcpp::Sym<32, false>&)
746 case elfcpp::R_386_NONE:
747 case elfcpp::R_386_GNU_VTINHERIT:
748 case elfcpp::R_386_GNU_VTENTRY:
751 case elfcpp::R_386_32:
752 case elfcpp::R_386_16:
753 case elfcpp::R_386_8:
754 // FIXME: If we are generating a shared object we need to copy
755 // this relocation into the object.
756 gold_assert(!parameters->output_is_shared());
759 case elfcpp::R_386_PC32:
760 case elfcpp::R_386_PC16:
761 case elfcpp::R_386_PC8:
764 case elfcpp::R_386_GOTOFF:
765 case elfcpp::R_386_GOTPC:
766 // We need a GOT section.
767 target->got_section(symtab, layout);
770 // These are relocations which should only be seen by the
771 // dynamic linker, and should never be seen here.
772 case elfcpp::R_386_COPY:
773 case elfcpp::R_386_GLOB_DAT:
774 case elfcpp::R_386_JUMP_SLOT:
775 case elfcpp::R_386_RELATIVE:
776 case elfcpp::R_386_TLS_TPOFF:
777 case elfcpp::R_386_TLS_DTPMOD32:
778 case elfcpp::R_386_TLS_DTPOFF32:
779 case elfcpp::R_386_TLS_TPOFF32:
780 case elfcpp::R_386_TLS_DESC:
781 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
782 program_name, object->name().c_str(), r_type);
786 // These are initial TLS relocs, which are expected when
788 case elfcpp::R_386_TLS_IE:
789 case elfcpp::R_386_TLS_GOTIE:
790 case elfcpp::R_386_TLS_LE:
791 case elfcpp::R_386_TLS_GD:
792 case elfcpp::R_386_TLS_LDM:
793 case elfcpp::R_386_TLS_LDO_32:
794 case elfcpp::R_386_TLS_IE_32:
795 case elfcpp::R_386_TLS_LE_32:
796 case elfcpp::R_386_TLS_GOTDESC:
797 case elfcpp::R_386_TLS_DESC_CALL:
799 bool output_is_shared = parameters->output_is_shared();
800 const tls::Tls_optimization optimized_type
801 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
804 case elfcpp::R_386_TLS_LE:
805 case elfcpp::R_386_TLS_LE_32:
806 // FIXME: If generating a shared object, we need to copy
807 // this relocation into the object.
808 gold_assert(!output_is_shared);
811 case elfcpp::R_386_TLS_IE:
812 case elfcpp::R_386_TLS_IE_32:
813 case elfcpp::R_386_TLS_GOTIE:
814 // FIXME: If not relaxing to LE, we need to generate a
815 // TPOFF or TPOFF32 reloc.
816 if (optimized_type != tls::TLSOPT_TO_LE)
817 unsupported_reloc_local(object, r_type);
820 case elfcpp::R_386_TLS_LDM:
821 // FIXME: If not relaxing to LE, we need to generate a
823 if (optimized_type != tls::TLSOPT_TO_LE)
824 unsupported_reloc_local(object, r_type);
827 case elfcpp::R_386_TLS_LDO_32:
830 case elfcpp::R_386_TLS_GD:
831 case elfcpp::R_386_TLS_GOTDESC:
832 case elfcpp::R_386_TLS_DESC_CALL:
833 // FIXME: If not relaxing to LE, we need to generate
834 // DTPMOD32 and DTPOFF32 relocs.
835 if (optimized_type != tls::TLSOPT_TO_LE)
836 unsupported_reloc_local(object, r_type);
845 case elfcpp::R_386_GOT32:
846 case elfcpp::R_386_PLT32:
847 case elfcpp::R_386_32PLT:
848 case elfcpp::R_386_TLS_GD_32:
849 case elfcpp::R_386_TLS_GD_PUSH:
850 case elfcpp::R_386_TLS_GD_CALL:
851 case elfcpp::R_386_TLS_GD_POP:
852 case elfcpp::R_386_TLS_LDM_32:
853 case elfcpp::R_386_TLS_LDM_PUSH:
854 case elfcpp::R_386_TLS_LDM_CALL:
855 case elfcpp::R_386_TLS_LDM_POP:
856 case elfcpp::R_386_USED_BY_INTEL_200:
858 unsupported_reloc_local(object, r_type);
863 // Report an unsupported relocation against a global symbol.
866 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
871 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
872 program_name, object->name().c_str(), r_type, gsym->name());
875 // Scan a relocation for a global symbol.
878 Target_i386::Scan::global(const General_options& options,
879 Symbol_table* symtab,
882 Sized_relobj<32, false>* object,
883 unsigned int data_shndx,
884 const elfcpp::Rel<32, false>& reloc,
890 case elfcpp::R_386_NONE:
891 case elfcpp::R_386_GNU_VTINHERIT:
892 case elfcpp::R_386_GNU_VTENTRY:
895 case elfcpp::R_386_32:
896 case elfcpp::R_386_PC32:
897 case elfcpp::R_386_16:
898 case elfcpp::R_386_PC16:
899 case elfcpp::R_386_8:
900 case elfcpp::R_386_PC8:
901 // FIXME: If we are generating a shared object we may need to
902 // copy this relocation into the object. If this symbol is
903 // defined in a shared object, we may need to copy this
904 // relocation in order to avoid a COPY relocation.
905 gold_assert(!parameters->output_is_shared());
907 if (gsym->is_from_dynobj())
909 // This symbol is defined in a dynamic object. If it is a
910 // function, we make a PLT entry. Otherwise we need to
911 // either generate a COPY reloc or copy this reloc.
912 if (gsym->type() == elfcpp::STT_FUNC)
914 target->make_plt_entry(symtab, layout, gsym);
916 // If this is not a PC relative reference, then we may
917 // be taking the address of the function. In that case
918 // we need to set the entry in the dynamic symbol table
919 // to the address of the PLT entry.
920 if (r_type != elfcpp::R_386_PC32
921 && r_type != elfcpp::R_386_PC16
922 && r_type != elfcpp::R_386_PC8)
923 gsym->set_needs_dynsym_value();
926 target->copy_reloc(&options, symtab, layout, object, data_shndx,
932 case elfcpp::R_386_GOT32:
934 // The symbol requires a GOT entry.
935 Output_data_got<32, false>* got = target->got_section(symtab, layout);
936 if (got->add_global(gsym))
938 // If this symbol is not fully resolved, we need to add a
939 // dynamic relocation for it.
940 if (!gsym->final_value_is_known())
942 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
943 rel_dyn->add_global(gsym, elfcpp::R_386_GLOB_DAT, got,
950 case elfcpp::R_386_PLT32:
951 // If the symbol is fully resolved, this is just a PC32 reloc.
952 // Otherwise we need a PLT entry.
953 if (gsym->final_value_is_known())
955 target->make_plt_entry(symtab, layout, gsym);
958 case elfcpp::R_386_GOTOFF:
959 case elfcpp::R_386_GOTPC:
960 // We need a GOT section.
961 target->got_section(symtab, layout);
964 // These are relocations which should only be seen by the
965 // dynamic linker, and should never be seen here.
966 case elfcpp::R_386_COPY:
967 case elfcpp::R_386_GLOB_DAT:
968 case elfcpp::R_386_JUMP_SLOT:
969 case elfcpp::R_386_RELATIVE:
970 case elfcpp::R_386_TLS_TPOFF:
971 case elfcpp::R_386_TLS_DTPMOD32:
972 case elfcpp::R_386_TLS_DTPOFF32:
973 case elfcpp::R_386_TLS_TPOFF32:
974 case elfcpp::R_386_TLS_DESC:
975 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
976 program_name, object->name().c_str(), r_type);
980 // These are initial tls relocs, which are expected when
982 case elfcpp::R_386_TLS_IE:
983 case elfcpp::R_386_TLS_GOTIE:
984 case elfcpp::R_386_TLS_LE:
985 case elfcpp::R_386_TLS_GD:
986 case elfcpp::R_386_TLS_LDM:
987 case elfcpp::R_386_TLS_LDO_32:
988 case elfcpp::R_386_TLS_IE_32:
989 case elfcpp::R_386_TLS_LE_32:
990 case elfcpp::R_386_TLS_GOTDESC:
991 case elfcpp::R_386_TLS_DESC_CALL:
993 const bool is_final = gsym->final_value_is_known();
994 const tls::Tls_optimization optimized_type
995 = Target_i386::optimize_tls_reloc(is_final, r_type);
998 case elfcpp::R_386_TLS_LE:
999 case elfcpp::R_386_TLS_LE_32:
1000 // FIXME: If generating a shared object, we need to copy
1001 // this relocation into the object.
1002 gold_assert(!parameters->output_is_shared());
1005 case elfcpp::R_386_TLS_IE:
1006 case elfcpp::R_386_TLS_IE_32:
1007 case elfcpp::R_386_TLS_GOTIE:
1008 // FIXME: If not relaxing to LE, we need to generate a
1009 // TPOFF or TPOFF32 reloc.
1010 if (optimized_type != tls::TLSOPT_TO_LE)
1011 unsupported_reloc_global(object, r_type, gsym);
1014 case elfcpp::R_386_TLS_LDM:
1015 // FIXME: If not relaxing to LE, we need to generate a
1017 if (optimized_type != tls::TLSOPT_TO_LE)
1018 unsupported_reloc_global(object, r_type, gsym);
1021 case elfcpp::R_386_TLS_LDO_32:
1024 case elfcpp::R_386_TLS_GD:
1025 case elfcpp::R_386_TLS_GOTDESC:
1026 case elfcpp::R_386_TLS_DESC_CALL:
1027 // FIXME: If not relaxing to LE, we need to generate
1028 // DTPMOD32 and DTPOFF32 relocs.
1029 if (optimized_type != tls::TLSOPT_TO_LE)
1030 unsupported_reloc_global(object, r_type, gsym);
1039 case elfcpp::R_386_32PLT:
1040 case elfcpp::R_386_TLS_GD_32:
1041 case elfcpp::R_386_TLS_GD_PUSH:
1042 case elfcpp::R_386_TLS_GD_CALL:
1043 case elfcpp::R_386_TLS_GD_POP:
1044 case elfcpp::R_386_TLS_LDM_32:
1045 case elfcpp::R_386_TLS_LDM_PUSH:
1046 case elfcpp::R_386_TLS_LDM_CALL:
1047 case elfcpp::R_386_TLS_LDM_POP:
1048 case elfcpp::R_386_USED_BY_INTEL_200:
1050 unsupported_reloc_global(object, r_type, gsym);
1055 // Scan relocations for a section.
1058 Target_i386::scan_relocs(const General_options& options,
1059 Symbol_table* symtab,
1061 Sized_relobj<32, false>* object,
1062 unsigned int data_shndx,
1063 unsigned int sh_type,
1064 const unsigned char* prelocs,
1066 size_t local_symbol_count,
1067 const unsigned char* plocal_symbols,
1068 Symbol** global_symbols)
1070 if (sh_type == elfcpp::SHT_RELA)
1072 fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"),
1073 program_name, object->name().c_str());
1077 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1092 // Finalize the sections.
1095 Target_i386::do_finalize_sections(Layout* layout)
1097 // Fill in some more dynamic tags.
1098 Output_data_dynamic* const odyn = layout->dynamic_data();
1101 if (this->got_plt_ != NULL)
1102 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1104 if (this->plt_ != NULL)
1106 const Output_data* od = this->plt_->rel_plt();
1107 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1108 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1109 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1112 if (this->rel_dyn_ != NULL)
1114 const Output_data* od = this->rel_dyn_;
1115 odyn->add_section_address(elfcpp::DT_REL, od);
1116 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1117 odyn->add_constant(elfcpp::DT_RELENT,
1118 elfcpp::Elf_sizes<32>::rel_size);
1121 if (!parameters->output_is_shared())
1123 // The value of the DT_DEBUG tag is filled in by the dynamic
1124 // linker at run time, and used by the debugger.
1125 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1129 // Emit any relocs we saved in an attempt to avoid generating COPY
1131 if (this->copy_relocs_ == NULL)
1133 if (this->copy_relocs_->any_to_emit())
1135 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1136 this->copy_relocs_->emit(rel_dyn);
1138 delete this->copy_relocs_;
1139 this->copy_relocs_ = NULL;
1142 // Perform a relocation.
1145 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1146 Target_i386* target,
1148 const elfcpp::Rel<32, false>& rel,
1149 unsigned int r_type,
1150 const Sized_symbol<32>* gsym,
1151 const Symbol_value<32>* psymval,
1152 unsigned char* view,
1153 elfcpp::Elf_types<32>::Elf_Addr address,
1156 if (this->skip_call_tls_get_addr_)
1158 if (r_type != elfcpp::R_386_PLT32
1160 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1162 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
1164 relinfo->location(relnum, rel.get_r_offset()).c_str());
1168 this->skip_call_tls_get_addr_ = false;
1173 // Pick the value to use for symbols defined in shared objects.
1174 Symbol_value<32> symval;
1175 if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
1177 symval.set_output_value(target->plt_section()->address()
1178 + gsym->plt_offset());
1182 const Sized_relobj<32, false>* object = relinfo->object;
1186 case elfcpp::R_386_NONE:
1187 case elfcpp::R_386_GNU_VTINHERIT:
1188 case elfcpp::R_386_GNU_VTENTRY:
1191 case elfcpp::R_386_32:
1192 Relocate_functions<32, false>::rel32(view, object, psymval);
1195 case elfcpp::R_386_PC32:
1196 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1199 case elfcpp::R_386_16:
1200 Relocate_functions<32, false>::rel16(view, object, psymval);
1203 case elfcpp::R_386_PC16:
1204 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1207 case elfcpp::R_386_8:
1208 Relocate_functions<32, false>::rel8(view, object, psymval);
1211 case elfcpp::R_386_PC8:
1212 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1215 case elfcpp::R_386_PLT32:
1216 gold_assert(gsym->has_plt_offset()
1217 || gsym->final_value_is_known());
1218 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1221 case elfcpp::R_386_GOT32:
1222 // Local GOT offsets not yet supported.
1224 gold_assert(gsym->has_got_offset());
1225 Relocate_functions<32, false>::rel32(view, gsym->got_offset());
1228 case elfcpp::R_386_GOTOFF:
1230 elfcpp::Elf_types<32>::Elf_Addr value;
1231 value = (psymval->value(object, 0)
1232 - target->got_section(NULL, NULL)->address());
1233 Relocate_functions<32, false>::rel32(view, value);
1237 case elfcpp::R_386_GOTPC:
1239 elfcpp::Elf_types<32>::Elf_Addr value;
1240 value = target->got_section(NULL, NULL)->address();
1241 Relocate_functions<32, false>::pcrel32(view, value, address);
1245 case elfcpp::R_386_COPY:
1246 case elfcpp::R_386_GLOB_DAT:
1247 case elfcpp::R_386_JUMP_SLOT:
1248 case elfcpp::R_386_RELATIVE:
1249 // These are outstanding tls relocs, which are unexpected when
1251 case elfcpp::R_386_TLS_TPOFF:
1252 case elfcpp::R_386_TLS_DTPMOD32:
1253 case elfcpp::R_386_TLS_DTPOFF32:
1254 case elfcpp::R_386_TLS_TPOFF32:
1255 case elfcpp::R_386_TLS_DESC:
1256 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1258 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1263 // These are initial tls relocs, which are expected when
1265 case elfcpp::R_386_TLS_IE:
1266 case elfcpp::R_386_TLS_GOTIE:
1267 case elfcpp::R_386_TLS_LE:
1268 case elfcpp::R_386_TLS_GD:
1269 case elfcpp::R_386_TLS_LDM:
1270 case elfcpp::R_386_TLS_LDO_32:
1271 case elfcpp::R_386_TLS_IE_32:
1272 case elfcpp::R_386_TLS_LE_32:
1273 case elfcpp::R_386_TLS_GOTDESC:
1274 case elfcpp::R_386_TLS_DESC_CALL:
1275 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
1276 address, view_size);
1279 case elfcpp::R_386_32PLT:
1280 case elfcpp::R_386_TLS_GD_32:
1281 case elfcpp::R_386_TLS_GD_PUSH:
1282 case elfcpp::R_386_TLS_GD_CALL:
1283 case elfcpp::R_386_TLS_GD_POP:
1284 case elfcpp::R_386_TLS_LDM_32:
1285 case elfcpp::R_386_TLS_LDM_PUSH:
1286 case elfcpp::R_386_TLS_LDM_CALL:
1287 case elfcpp::R_386_TLS_LDM_POP:
1288 case elfcpp::R_386_USED_BY_INTEL_200:
1290 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1292 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1301 // Perform a TLS relocation.
1304 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1306 const elfcpp::Rel<32, false>& rel,
1307 unsigned int r_type,
1308 const Sized_symbol<32>* gsym,
1309 const Symbol_value<32>* psymval,
1310 unsigned char* view,
1311 elfcpp::Elf_types<32>::Elf_Addr,
1314 Output_segment* tls_segment = relinfo->layout->tls_segment();
1315 if (tls_segment == NULL)
1317 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1319 relinfo->location(relnum, rel.get_r_offset()).c_str());
1323 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(relinfo->object, 0);
1325 const bool is_final = (gsym == NULL
1326 ? !parameters->output_is_shared()
1327 : gsym->final_value_is_known());
1328 const tls::Tls_optimization optimized_type
1329 = Target_i386::optimize_tls_reloc(is_final, r_type);
1332 case elfcpp::R_386_TLS_LE_32:
1333 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1334 Relocate_functions<32, false>::rel32(view, value);
1337 case elfcpp::R_386_TLS_LE:
1338 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1339 Relocate_functions<32, false>::rel32(view, value);
1342 case elfcpp::R_386_TLS_IE:
1343 case elfcpp::R_386_TLS_GOTIE:
1344 case elfcpp::R_386_TLS_IE_32:
1345 if (optimized_type == tls::TLSOPT_TO_LE)
1347 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1348 rel, r_type, value, view,
1352 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1354 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1359 case elfcpp::R_386_TLS_GD:
1360 if (optimized_type == tls::TLSOPT_TO_LE)
1362 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1363 rel, r_type, value, view,
1367 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1369 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1374 case elfcpp::R_386_TLS_LDM:
1375 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1378 _("%s: %s: both SUN and GNU model TLS relocations\n"),
1380 relinfo->location(relnum, rel.get_r_offset()).c_str());
1383 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1384 if (optimized_type == tls::TLSOPT_TO_LE)
1386 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1387 value, view, view_size);
1390 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1392 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1397 case elfcpp::R_386_TLS_LDO_32:
1398 // This reloc can appear in debugging sections, in which case we
1399 // won't see the TLS_LDM reloc. The local_dynamic_type field
1401 if (optimized_type != tls::TLSOPT_TO_LE
1402 || this->local_dynamic_type_ == LOCAL_DYNAMIC_NONE)
1403 value = value - tls_segment->vaddr();
1404 else if (this->local_dynamic_type_ == LOCAL_DYNAMIC_GNU)
1405 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1407 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1408 Relocate_functions<32, false>::rel32(view, value);
1411 case elfcpp::R_386_TLS_GOTDESC:
1412 case elfcpp::R_386_TLS_DESC_CALL:
1413 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1415 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1422 // Do a relocation in which we convert a TLS Initial-Exec to a
1426 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1428 Output_segment* tls_segment,
1429 const elfcpp::Rel<32, false>& rel,
1430 unsigned int r_type,
1431 elfcpp::Elf_types<32>::Elf_Addr value,
1432 unsigned char* view,
1435 // We have to actually change the instructions, which means that we
1436 // need to examine the opcodes to figure out which instruction we
1438 if (r_type == elfcpp::R_386_TLS_IE)
1440 // movl %gs:XX,%eax ==> movl $YY,%eax
1441 // movl %gs:XX,%reg ==> movl $YY,%reg
1442 // addl %gs:XX,%reg ==> addl $YY,%reg
1443 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
1444 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
1446 unsigned char op1 = view[-1];
1449 // movl XX,%eax ==> movl $YY,%eax
1454 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1456 unsigned char op2 = view[-2];
1459 // movl XX,%reg ==> movl $YY,%reg
1460 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1461 (op1 & 0xc7) == 0x05);
1463 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1465 else if (op2 == 0x03)
1467 // addl XX,%reg ==> addl $YY,%reg
1468 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1469 (op1 & 0xc7) == 0x05);
1471 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1474 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
1479 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1480 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1481 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1482 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1483 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
1485 unsigned char op1 = view[-1];
1486 unsigned char op2 = view[-2];
1487 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1488 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1491 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1493 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1495 else if (op2 == 0x2b)
1497 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1499 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1501 else if (op2 == 0x03)
1503 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1505 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1508 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
1511 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1512 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1515 Relocate_functions<32, false>::rel32(view, value);
1518 // Do a relocation in which we convert a TLS Global-Dynamic to a
1522 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1524 Output_segment* tls_segment,
1525 const elfcpp::Rel<32, false>& rel,
1527 elfcpp::Elf_types<32>::Elf_Addr value,
1528 unsigned char* view,
1531 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1532 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1533 // leal foo(%reg),%eax; call ___tls_get_addr
1534 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1536 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1537 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1539 unsigned char op1 = view[-1];
1540 unsigned char op2 = view[-2];
1542 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1543 op2 == 0x8d || op2 == 0x04);
1544 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1550 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
1551 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
1552 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1553 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
1554 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1558 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1559 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1560 if (static_cast<off_t>(rel.get_r_offset() + 9) < view_size
1563 // There is a trailing nop. Use the size byte subl.
1564 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1569 // Use the five byte subl.
1570 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1574 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1575 Relocate_functions<32, false>::rel32(view + roff, value);
1577 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1579 this->skip_call_tls_get_addr_ = true;
1582 // Do a relocation in which we convert a TLS Local-Dynamic to a
1586 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
1589 const elfcpp::Rel<32, false>& rel,
1591 elfcpp::Elf_types<32>::Elf_Addr,
1592 unsigned char* view,
1595 // leal foo(%reg), %eax; call ___tls_get_addr
1596 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1598 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1599 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1601 // FIXME: Does this test really always pass?
1602 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1603 view[-2] == 0x8d && view[-1] == 0x83);
1605 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1607 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1609 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1611 this->skip_call_tls_get_addr_ = true;
1614 // Relocate section data.
1617 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
1618 unsigned int sh_type,
1619 const unsigned char* prelocs,
1621 unsigned char* view,
1622 elfcpp::Elf_types<32>::Elf_Addr address,
1625 gold_assert(sh_type == elfcpp::SHT_REL);
1627 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1628 Target_i386::Relocate>(
1638 // Return the value to use for a dynamic which requires special
1639 // treatment. This is how we support equality comparisons of function
1640 // pointers across shared library boundaries, as described in the
1641 // processor specific ABI supplement.
1644 Target_i386::do_dynsym_value(const Symbol* gsym) const
1646 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1647 return this->plt_section()->address() + gsym->plt_offset();
1650 // Return a string used to fill a code section with nops to take up
1651 // the specified length.
1654 Target_i386::do_code_fill(off_t length)
1658 // Build a jmp instruction to skip over the bytes.
1659 unsigned char jmp[5];
1661 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
1662 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1663 + std::string(length - 5, '\0'));
1666 // Nop sequences of various lengths.
1667 const char nop1[1] = { 0x90 }; // nop
1668 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1669 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1670 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1671 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1672 0x00 }; // leal 0(%esi,1),%esi
1673 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1675 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1677 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1678 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1679 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1680 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1682 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1683 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1685 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1686 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1688 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1689 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1690 0x00, 0x00, 0x00, 0x00 };
1691 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1692 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1693 0x27, 0x00, 0x00, 0x00,
1695 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1696 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1697 0xbc, 0x27, 0x00, 0x00,
1699 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1700 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1701 0x90, 0x90, 0x90, 0x90,
1704 const char* nops[16] = {
1706 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1707 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1710 return std::string(nops[length], length);
1713 // The selector for i386 object files.
1715 class Target_selector_i386 : public Target_selector
1718 Target_selector_i386()
1719 : Target_selector(elfcpp::EM_386, 32, false)
1723 recognize(int machine, int osabi, int abiversion);
1726 Target_i386* target_;
1729 // Recognize an i386 object file when we already know that the machine
1730 // number is EM_386.
1733 Target_selector_i386::recognize(int, int, int)
1735 if (this->target_ == NULL)
1736 this->target_ = new Target_i386();
1737 return this->target_;
1740 Target_selector_i386 target_selector_i386;
1742 } // End anonymous namespace.