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.
48 // TLS info comes from
49 // http://people.redhat.com/drepper/tls.pdf
50 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
52 class Target_i386 : public Sized_target<32, false>
55 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
58 : Sized_target<32, false>(&i386_info),
59 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
60 copy_relocs_(NULL), dynbss_(NULL)
63 // Scan the relocations to look for symbol adjustments.
65 scan_relocs(const General_options& options,
68 Sized_relobj<32, false>* object,
69 unsigned int data_shndx,
71 const unsigned char* prelocs,
73 Output_section* output_section,
74 bool needs_special_offset_handling,
75 size_t local_symbol_count,
76 const unsigned char* plocal_symbols);
78 // Finalize the sections.
80 do_finalize_sections(Layout*);
82 // Return the value to use for a dynamic which requires special
85 do_dynsym_value(const Symbol*) const;
87 // Relocate a section.
89 relocate_section(const Relocate_info<32, false>*,
91 const unsigned char* prelocs,
93 Output_section* output_section,
94 bool needs_special_offset_handling,
96 elfcpp::Elf_types<32>::Elf_Addr view_address,
99 // Return a string used to fill a code section with nops.
101 do_code_fill(off_t length);
103 // Return the size of the GOT section.
107 gold_assert(this->got_ != NULL);
108 return this->got_->data_size();
112 // The class which scans relocations.
116 local(const General_options& options, Symbol_table* symtab,
117 Layout* layout, Target_i386* target,
118 Sized_relobj<32, false>* object,
119 unsigned int data_shndx,
120 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
121 const elfcpp::Sym<32, false>& lsym);
124 global(const General_options& options, Symbol_table* symtab,
125 Layout* layout, Target_i386* target,
126 Sized_relobj<32, false>* object,
127 unsigned int data_shndx,
128 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
132 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
135 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
139 // The class which implements relocation.
144 : skip_call_tls_get_addr_(false),
145 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
150 if (this->skip_call_tls_get_addr_)
152 // FIXME: This needs to specify the location somehow.
153 gold_error(_("missing expected TLS relocation"));
157 // Return whether the static relocation needs to be applied.
159 should_apply_static_reloc(const Sized_symbol<32>* gsym,
160 bool is_absolute_ref,
161 bool is_function_call,
164 // Do a relocation. Return false if the caller should not issue
165 // any warnings about this relocation.
167 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
168 const elfcpp::Rel<32, false>&,
169 unsigned int r_type, const Sized_symbol<32>*,
170 const Symbol_value<32>*,
171 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
175 // Do a TLS relocation.
177 relocate_tls(const Relocate_info<32, false>*, size_t relnum,
178 const elfcpp::Rel<32, false>&,
179 unsigned int r_type, const Sized_symbol<32>*,
180 const Symbol_value<32>*,
181 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
183 // Do a TLS General-Dynamic to Local-Exec transition.
185 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
186 Output_segment* tls_segment,
187 const elfcpp::Rel<32, false>&, unsigned int r_type,
188 elfcpp::Elf_types<32>::Elf_Addr value,
192 // Do a TLS Local-Dynamic to Local-Exec transition.
194 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
195 Output_segment* tls_segment,
196 const elfcpp::Rel<32, false>&, unsigned int r_type,
197 elfcpp::Elf_types<32>::Elf_Addr value,
201 // Do a TLS Initial-Exec to Local-Exec transition.
203 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
204 Output_segment* tls_segment,
205 const elfcpp::Rel<32, false>&, unsigned int r_type,
206 elfcpp::Elf_types<32>::Elf_Addr value,
210 // We need to keep track of which type of local dynamic relocation
211 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
212 enum Local_dynamic_type
219 // This is set if we should skip the next reloc, which should be a
220 // PLT32 reloc against ___tls_get_addr.
221 bool skip_call_tls_get_addr_;
222 // The type of local dynamic relocation we have seen in the section
223 // being relocated, if any.
224 Local_dynamic_type local_dynamic_type_;
227 // Adjust TLS relocation type based on the options and whether this
228 // is a local symbol.
229 static tls::Tls_optimization
230 optimize_tls_reloc(bool is_final, int r_type);
232 // Get the GOT section, creating it if necessary.
233 Output_data_got<32, false>*
234 got_section(Symbol_table*, Layout*);
236 // Get the GOT PLT section.
238 got_plt_section() const
240 gold_assert(this->got_plt_ != NULL);
241 return this->got_plt_;
244 // Create a PLT entry for a global symbol.
246 make_plt_entry(Symbol_table*, Layout*, Symbol*);
248 // Get the PLT section.
249 const Output_data_plt_i386*
252 gold_assert(this->plt_ != NULL);
256 // Get the dynamic reloc section, creating it if necessary.
258 rel_dyn_section(Layout*);
260 // Return true if the symbol may need a COPY relocation.
261 // References from an executable object to non-function symbols
262 // defined in a dynamic object may need a COPY relocation.
264 may_need_copy_reloc(Symbol* gsym)
266 return (!parameters->output_is_shared()
267 && gsym->is_from_dynobj()
268 && gsym->type() != elfcpp::STT_FUNC);
271 // Copy a relocation against a global symbol.
273 copy_reloc(const General_options*, Symbol_table*, Layout*,
274 Sized_relobj<32, false>*, unsigned int,
275 Symbol*, const elfcpp::Rel<32, false>&);
277 // Information about this specific target which we pass to the
278 // general Target structure.
279 static const Target::Target_info i386_info;
282 Output_data_got<32, false>* got_;
284 Output_data_plt_i386* plt_;
285 // The GOT PLT section.
286 Output_data_space* got_plt_;
287 // The dynamic reloc section.
288 Reloc_section* rel_dyn_;
289 // Relocs saved to avoid a COPY reloc.
290 Copy_relocs<32, false>* copy_relocs_;
291 // Space for variables copied with a COPY reloc.
292 Output_data_space* dynbss_;
295 const Target::Target_info Target_i386::i386_info =
298 false, // is_big_endian
299 elfcpp::EM_386, // machine_code
300 false, // has_make_symbol
301 false, // has_resolve
302 true, // has_code_fill
303 true, // is_default_stack_executable
304 "/usr/lib/libc.so.1", // dynamic_linker
305 0x08048000, // default_text_segment_address
306 0x1000, // abi_pagesize
307 0x1000 // common_pagesize
310 // Get the GOT section, creating it if necessary.
312 Output_data_got<32, false>*
313 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
315 if (this->got_ == NULL)
317 gold_assert(symtab != NULL && layout != NULL);
319 this->got_ = new Output_data_got<32, false>();
321 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
322 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
325 // The old GNU linker creates a .got.plt section. We just
326 // create another set of data in the .got section. Note that we
327 // always create a PLT if we create a GOT, although the PLT
329 this->got_plt_ = new Output_data_space(4);
330 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
331 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
334 // The first three entries are reserved.
335 this->got_plt_->set_space_size(3 * 4);
337 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
338 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
340 0, 0, elfcpp::STT_OBJECT,
342 elfcpp::STV_HIDDEN, 0,
349 // Get the dynamic reloc section, creating it if necessary.
351 Target_i386::Reloc_section*
352 Target_i386::rel_dyn_section(Layout* layout)
354 if (this->rel_dyn_ == NULL)
356 gold_assert(layout != NULL);
357 this->rel_dyn_ = new Reloc_section();
358 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
359 elfcpp::SHF_ALLOC, this->rel_dyn_);
361 return this->rel_dyn_;
364 // A class to handle the PLT data.
366 class Output_data_plt_i386 : public Output_section_data
369 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
371 Output_data_plt_i386(Layout*, Output_data_space*);
373 // Add an entry to the PLT.
375 add_entry(Symbol* gsym);
377 // Return the .rel.plt section data.
380 { return this->rel_; }
384 do_adjust_output_section(Output_section* os);
387 // The size of an entry in the PLT.
388 static const int plt_entry_size = 16;
390 // The first entry in the PLT for an executable.
391 static unsigned char exec_first_plt_entry[plt_entry_size];
393 // The first entry in the PLT for a shared object.
394 static unsigned char dyn_first_plt_entry[plt_entry_size];
396 // Other entries in the PLT for an executable.
397 static unsigned char exec_plt_entry[plt_entry_size];
399 // Other entries in the PLT for a shared object.
400 static unsigned char dyn_plt_entry[plt_entry_size];
402 // Set the final size.
404 do_set_address(uint64_t, off_t)
405 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
407 // Write out the PLT data.
409 do_write(Output_file*);
411 // The reloc section.
413 // The .got.plt section.
414 Output_data_space* got_plt_;
415 // The number of PLT entries.
419 // Create the PLT section. The ordinary .got section is an argument,
420 // since we need to refer to the start. We also create our own .got
421 // section just for PLT entries.
423 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
424 Output_data_space* got_plt)
425 : Output_section_data(4), got_plt_(got_plt), count_(0)
427 this->rel_ = new Reloc_section();
428 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
429 elfcpp::SHF_ALLOC, this->rel_);
433 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
435 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
436 // linker, and so do we.
440 // Add an entry to the PLT.
443 Output_data_plt_i386::add_entry(Symbol* gsym)
445 gold_assert(!gsym->has_plt_offset());
447 // Note that when setting the PLT offset we skip the initial
448 // reserved PLT entry.
449 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
453 off_t got_offset = this->got_plt_->data_size();
455 // Every PLT entry needs a GOT entry which points back to the PLT
456 // entry (this will be changed by the dynamic linker, normally
457 // lazily when the function is called).
458 this->got_plt_->set_space_size(got_offset + 4);
460 // Every PLT entry needs a reloc.
461 gsym->set_needs_dynsym_entry();
462 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
465 // Note that we don't need to save the symbol. The contents of the
466 // PLT are independent of which symbols are used. The symbols only
467 // appear in the relocations.
470 // The first entry in the PLT for an executable.
472 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
474 0xff, 0x35, // pushl contents of memory address
475 0, 0, 0, 0, // replaced with address of .got + 4
476 0xff, 0x25, // jmp indirect
477 0, 0, 0, 0, // replaced with address of .got + 8
481 // The first entry in the PLT for a shared object.
483 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
485 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
486 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
490 // Subsequent entries in the PLT for an executable.
492 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
494 0xff, 0x25, // jmp indirect
495 0, 0, 0, 0, // replaced with address of symbol in .got
496 0x68, // pushl immediate
497 0, 0, 0, 0, // replaced with offset into relocation table
498 0xe9, // jmp relative
499 0, 0, 0, 0 // replaced with offset to start of .plt
502 // Subsequent entries in the PLT for a shared object.
504 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
506 0xff, 0xa3, // jmp *offset(%ebx)
507 0, 0, 0, 0, // replaced with offset of symbol in .got
508 0x68, // pushl immediate
509 0, 0, 0, 0, // replaced with offset into relocation table
510 0xe9, // jmp relative
511 0, 0, 0, 0 // replaced with offset to start of .plt
514 // Write out the PLT. This uses the hand-coded instructions above,
515 // and adjusts them as needed. This is all specified by the i386 ELF
516 // Processor Supplement.
519 Output_data_plt_i386::do_write(Output_file* of)
521 const off_t offset = this->offset();
522 const off_t oview_size = this->data_size();
523 unsigned char* const oview = of->get_output_view(offset, oview_size);
525 const off_t got_file_offset = this->got_plt_->offset();
526 const off_t got_size = this->got_plt_->data_size();
527 unsigned char* const got_view = of->get_output_view(got_file_offset,
530 unsigned char* pov = oview;
532 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
533 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
535 if (parameters->output_is_shared())
536 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
539 memcpy(pov, exec_first_plt_entry, plt_entry_size);
540 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
541 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
543 pov += plt_entry_size;
545 unsigned char* got_pov = got_view;
547 memset(got_pov, 0, 12);
550 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
552 unsigned int plt_offset = plt_entry_size;
553 unsigned int plt_rel_offset = 0;
554 unsigned int got_offset = 12;
555 const unsigned int count = this->count_;
556 for (unsigned int i = 0;
559 pov += plt_entry_size,
561 plt_offset += plt_entry_size,
562 plt_rel_offset += rel_size,
565 // Set and adjust the PLT entry itself.
567 if (parameters->output_is_shared())
569 memcpy(pov, dyn_plt_entry, plt_entry_size);
570 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
574 memcpy(pov, exec_plt_entry, plt_entry_size);
575 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
580 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
581 elfcpp::Swap<32, false>::writeval(pov + 12,
582 - (plt_offset + plt_entry_size));
584 // Set the entry in the GOT.
585 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
588 gold_assert(pov - oview == oview_size);
589 gold_assert(got_pov - got_view == got_size);
591 of->write_output_view(offset, oview_size, oview);
592 of->write_output_view(got_file_offset, got_size, got_view);
595 // Create a PLT entry for a global symbol.
598 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
600 if (gsym->has_plt_offset())
603 if (this->plt_ == NULL)
605 // Create the GOT sections first.
606 this->got_section(symtab, layout);
608 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
609 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
611 | elfcpp::SHF_EXECINSTR),
615 this->plt_->add_entry(gsym);
618 // Handle a relocation against a non-function symbol defined in a
619 // dynamic object. The traditional way to handle this is to generate
620 // a COPY relocation to copy the variable at runtime from the shared
621 // object into the executable's data segment. However, this is
622 // undesirable in general, as if the size of the object changes in the
623 // dynamic object, the executable will no longer work correctly. If
624 // this relocation is in a writable section, then we can create a
625 // dynamic reloc and the dynamic linker will resolve it to the correct
626 // address at runtime. However, we do not want do that if the
627 // relocation is in a read-only section, as it would prevent the
628 // readonly segment from being shared. And if we have to eventually
629 // generate a COPY reloc, then any dynamic relocations will be
630 // useless. So this means that if this is a writable section, we need
631 // to save the relocation until we see whether we have to create a
632 // COPY relocation for this symbol for any other relocation.
635 Target_i386::copy_reloc(const General_options* options,
636 Symbol_table* symtab,
638 Sized_relobj<32, false>* object,
639 unsigned int data_shndx, Symbol* gsym,
640 const elfcpp::Rel<32, false>& rel)
642 Sized_symbol<32>* ssym;
643 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
646 if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
649 // So far we do not need a COPY reloc. Save this relocation.
650 // If it turns out that we never need a COPY reloc for this
651 // symbol, then we will emit the relocation.
652 if (this->copy_relocs_ == NULL)
653 this->copy_relocs_ = new Copy_relocs<32, false>();
654 this->copy_relocs_->save(ssym, object, data_shndx, rel);
658 // Allocate space for this symbol in the .bss section.
660 elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
662 // There is no defined way to determine the required alignment
663 // of the symbol. We pick the alignment based on the size. We
664 // set an arbitrary maximum of 256.
666 for (align = 1; align < 512; align <<= 1)
667 if ((symsize & align) != 0)
670 if (this->dynbss_ == NULL)
672 this->dynbss_ = new Output_data_space(align);
673 layout->add_output_section_data(".bss",
676 | elfcpp::SHF_WRITE),
680 Output_data_space* dynbss = this->dynbss_;
682 if (align > dynbss->addralign())
683 dynbss->set_space_alignment(align);
685 off_t dynbss_size = dynbss->data_size();
686 dynbss_size = align_address(dynbss_size, align);
687 off_t offset = dynbss_size;
688 dynbss->set_space_size(dynbss_size + symsize);
690 symtab->define_with_copy_reloc(this, ssym, dynbss, offset);
692 // Add the COPY reloc.
693 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
694 rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
698 // Optimize the TLS relocation type based on what we know about the
699 // symbol. IS_FINAL is true if the final address of this symbol is
700 // known at link time.
702 tls::Tls_optimization
703 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
705 // If we are generating a shared library, then we can't do anything
707 if (parameters->output_is_shared())
708 return tls::TLSOPT_NONE;
712 case elfcpp::R_386_TLS_GD:
713 case elfcpp::R_386_TLS_GOTDESC:
714 case elfcpp::R_386_TLS_DESC_CALL:
715 // These are General-Dynamic which permits fully general TLS
716 // access. Since we know that we are generating an executable,
717 // we can convert this to Initial-Exec. If we also know that
718 // this is a local symbol, we can further switch to Local-Exec.
720 return tls::TLSOPT_TO_LE;
721 return tls::TLSOPT_TO_IE;
723 case elfcpp::R_386_TLS_LDM:
724 // This is Local-Dynamic, which refers to a local symbol in the
725 // dynamic TLS block. Since we know that we generating an
726 // executable, we can switch to Local-Exec.
727 return tls::TLSOPT_TO_LE;
729 case elfcpp::R_386_TLS_LDO_32:
730 // Another type of Local-Dynamic relocation.
731 return tls::TLSOPT_TO_LE;
733 case elfcpp::R_386_TLS_IE:
734 case elfcpp::R_386_TLS_GOTIE:
735 case elfcpp::R_386_TLS_IE_32:
736 // These are Initial-Exec relocs which get the thread offset
737 // from the GOT. If we know that we are linking against the
738 // local symbol, we can switch to Local-Exec, which links the
739 // thread offset into the instruction.
741 return tls::TLSOPT_TO_LE;
742 return tls::TLSOPT_NONE;
744 case elfcpp::R_386_TLS_LE:
745 case elfcpp::R_386_TLS_LE_32:
746 // When we already have Local-Exec, there is nothing further we
748 return tls::TLSOPT_NONE;
755 // Report an unsupported relocation against a local symbol.
758 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
761 gold_error(_("%s: unsupported reloc %u against local symbol"),
762 object->name().c_str(), r_type);
765 // Scan a relocation for a local symbol.
768 Target_i386::Scan::local(const General_options&,
769 Symbol_table* symtab,
772 Sized_relobj<32, false>* object,
773 unsigned int data_shndx,
774 const elfcpp::Rel<32, false>& reloc,
776 const elfcpp::Sym<32, false>&)
780 case elfcpp::R_386_NONE:
781 case elfcpp::R_386_GNU_VTINHERIT:
782 case elfcpp::R_386_GNU_VTENTRY:
785 case elfcpp::R_386_32:
786 // If building a shared library (or a position-independent
787 // executable), we need to create a dynamic relocation for
788 // this location. The relocation applied at link time will
789 // apply the link-time value, so we flag the location with
790 // an R_386_RELATIVE relocation so the dynamic loader can
791 // relocate it easily.
792 if (parameters->output_is_position_independent())
794 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
795 rel_dyn->add_local(object, 0, elfcpp::R_386_RELATIVE, data_shndx,
796 reloc.get_r_offset());
800 case elfcpp::R_386_16:
801 case elfcpp::R_386_8:
802 // If building a shared library (or a position-independent
803 // executable), we need to create a dynamic relocation for
804 // this location. Because the addend needs to remain in the
805 // data section, we need to be careful not to apply this
806 // relocation statically.
807 if (parameters->output_is_position_independent())
809 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
810 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
811 rel_dyn->add_local(object, r_sym, r_type, data_shndx,
812 reloc.get_r_offset());
816 case elfcpp::R_386_PC32:
817 case elfcpp::R_386_PC16:
818 case elfcpp::R_386_PC8:
821 case elfcpp::R_386_PLT32:
822 // Since we know this is a local symbol, we can handle this as a
826 case elfcpp::R_386_GOTOFF:
827 case elfcpp::R_386_GOTPC:
828 // We need a GOT section.
829 target->got_section(symtab, layout);
832 case elfcpp::R_386_GOT32:
834 // The symbol requires a GOT entry.
835 Output_data_got<32, false>* got = target->got_section(symtab, layout);
836 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
837 if (got->add_local(object, r_sym))
839 // If we are generating a shared object, we need to add a
840 // dynamic RELATIVE relocation for this symbol.
841 if (parameters->output_is_position_independent())
843 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
844 rel_dyn->add_local(object, 0, elfcpp::R_386_RELATIVE,
845 data_shndx, reloc.get_r_offset());
851 // These are relocations which should only be seen by the
852 // dynamic linker, and should never be seen here.
853 case elfcpp::R_386_COPY:
854 case elfcpp::R_386_GLOB_DAT:
855 case elfcpp::R_386_JUMP_SLOT:
856 case elfcpp::R_386_RELATIVE:
857 case elfcpp::R_386_TLS_TPOFF:
858 case elfcpp::R_386_TLS_DTPMOD32:
859 case elfcpp::R_386_TLS_DTPOFF32:
860 case elfcpp::R_386_TLS_TPOFF32:
861 case elfcpp::R_386_TLS_DESC:
862 gold_error(_("%s: unexpected reloc %u in object file"),
863 object->name().c_str(), r_type);
866 // These are initial TLS relocs, which are expected when
868 case elfcpp::R_386_TLS_GD: // Global-dynamic
869 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
870 case elfcpp::R_386_TLS_DESC_CALL:
871 case elfcpp::R_386_TLS_LDM: // Local-dynamic
872 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
873 case elfcpp::R_386_TLS_IE: // Initial-exec
874 case elfcpp::R_386_TLS_IE_32:
875 case elfcpp::R_386_TLS_GOTIE:
876 case elfcpp::R_386_TLS_LE: // Local-exec
877 case elfcpp::R_386_TLS_LE_32:
879 bool output_is_shared = parameters->output_is_shared();
880 const tls::Tls_optimization optimized_type
881 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
884 case elfcpp::R_386_TLS_GD: // Global-dynamic
885 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
886 case elfcpp::R_386_TLS_DESC_CALL:
887 // FIXME: If not relaxing to LE, we need to generate
888 // DTPMOD32 and DTPOFF32 relocs.
889 if (optimized_type != tls::TLSOPT_TO_LE)
890 unsupported_reloc_local(object, r_type);
893 case elfcpp::R_386_TLS_LDM: // Local-dynamic
894 // FIXME: If not relaxing to LE, we need to generate a
896 if (optimized_type != tls::TLSOPT_TO_LE)
897 unsupported_reloc_local(object, r_type);
900 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
903 case elfcpp::R_386_TLS_IE: // Initial-exec
904 case elfcpp::R_386_TLS_IE_32:
905 case elfcpp::R_386_TLS_GOTIE:
906 // FIXME: If not relaxing to LE, we need to generate a
907 // TPOFF or TPOFF32 reloc.
908 if (optimized_type != tls::TLSOPT_TO_LE)
909 unsupported_reloc_local(object, r_type);
912 case elfcpp::R_386_TLS_LE: // Local-exec
913 case elfcpp::R_386_TLS_LE_32:
914 // FIXME: If generating a shared object, we need to copy
915 // this relocation into the object.
916 gold_assert(!output_is_shared);
925 case elfcpp::R_386_32PLT:
926 case elfcpp::R_386_TLS_GD_32:
927 case elfcpp::R_386_TLS_GD_PUSH:
928 case elfcpp::R_386_TLS_GD_CALL:
929 case elfcpp::R_386_TLS_GD_POP:
930 case elfcpp::R_386_TLS_LDM_32:
931 case elfcpp::R_386_TLS_LDM_PUSH:
932 case elfcpp::R_386_TLS_LDM_CALL:
933 case elfcpp::R_386_TLS_LDM_POP:
934 case elfcpp::R_386_USED_BY_INTEL_200:
936 unsupported_reloc_local(object, r_type);
941 // Report an unsupported relocation against a global symbol.
944 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
948 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
949 object->name().c_str(), r_type, gsym->name());
952 // Scan a relocation for a global symbol.
955 Target_i386::Scan::global(const General_options& options,
956 Symbol_table* symtab,
959 Sized_relobj<32, false>* object,
960 unsigned int data_shndx,
961 const elfcpp::Rel<32, false>& reloc,
967 case elfcpp::R_386_NONE:
968 case elfcpp::R_386_GNU_VTINHERIT:
969 case elfcpp::R_386_GNU_VTENTRY:
972 case elfcpp::R_386_32:
973 case elfcpp::R_386_16:
974 case elfcpp::R_386_8:
976 // Make a PLT entry if necessary.
977 if (gsym->needs_plt_entry())
979 target->make_plt_entry(symtab, layout, gsym);
980 // Since this is not a PC-relative relocation, we may be
981 // taking the address of a function. In that case we need to
982 // set the entry in the dynamic symbol table to the address of
984 if (gsym->is_from_dynobj())
985 gsym->set_needs_dynsym_value();
987 // Make a dynamic relocation if necessary.
988 if (gsym->needs_dynamic_reloc(true, false))
990 if (target->may_need_copy_reloc(gsym))
992 target->copy_reloc(&options, symtab, layout, object, data_shndx,
995 else if (r_type == elfcpp::R_386_32
996 && gsym->can_use_relative_reloc(false))
998 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
999 rel_dyn->add_local(object, 0, elfcpp::R_386_RELATIVE, data_shndx,
1000 reloc.get_r_offset());
1004 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1005 rel_dyn->add_global(gsym, r_type, object, data_shndx,
1006 reloc.get_r_offset());
1012 case elfcpp::R_386_PC32:
1013 case elfcpp::R_386_PC16:
1014 case elfcpp::R_386_PC8:
1016 // Make a PLT entry if necessary.
1017 if (gsym->needs_plt_entry())
1018 target->make_plt_entry(symtab, layout, gsym);
1019 // Make a dynamic relocation if necessary.
1020 bool is_function_call = (gsym->type() == elfcpp::STT_FUNC);
1021 if (gsym->needs_dynamic_reloc(false, is_function_call))
1023 if (target->may_need_copy_reloc(gsym))
1025 target->copy_reloc(&options, symtab, layout, object, data_shndx,
1030 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1031 rel_dyn->add_global(gsym, r_type, object, data_shndx,
1032 reloc.get_r_offset());
1038 case elfcpp::R_386_GOT32:
1040 // The symbol requires a GOT entry.
1041 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1042 if (got->add_global(gsym))
1044 // If this symbol is not fully resolved, we need to add a
1045 // dynamic relocation for it.
1046 if (!gsym->final_value_is_known())
1048 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1049 if (gsym->is_preemptible())
1050 rel_dyn->add_global(gsym, elfcpp::R_386_GLOB_DAT, got,
1051 gsym->got_offset());
1054 rel_dyn->add_local(object, 0, elfcpp::R_386_RELATIVE,
1055 got, gsym->got_offset());
1056 // Make sure we write the link-time value to the GOT.
1057 gsym->set_needs_value_in_got();
1064 case elfcpp::R_386_PLT32:
1065 // If the symbol is fully resolved, this is just a PC32 reloc.
1066 // Otherwise we need a PLT entry.
1067 if (gsym->final_value_is_known())
1069 // If building a shared library, we can also skip the PLT entry
1070 // if the symbol is defined in the output file and is protected
1072 if (gsym->is_defined()
1073 && !gsym->is_from_dynobj()
1074 && !gsym->is_preemptible())
1076 target->make_plt_entry(symtab, layout, gsym);
1079 case elfcpp::R_386_GOTOFF:
1080 case elfcpp::R_386_GOTPC:
1081 // We need a GOT section.
1082 target->got_section(symtab, layout);
1085 // These are relocations which should only be seen by the
1086 // dynamic linker, and should never be seen here.
1087 case elfcpp::R_386_COPY:
1088 case elfcpp::R_386_GLOB_DAT:
1089 case elfcpp::R_386_JUMP_SLOT:
1090 case elfcpp::R_386_RELATIVE:
1091 case elfcpp::R_386_TLS_TPOFF:
1092 case elfcpp::R_386_TLS_DTPMOD32:
1093 case elfcpp::R_386_TLS_DTPOFF32:
1094 case elfcpp::R_386_TLS_TPOFF32:
1095 case elfcpp::R_386_TLS_DESC:
1096 gold_error(_("%s: unexpected reloc %u in object file"),
1097 object->name().c_str(), r_type);
1100 // These are initial tls relocs, which are expected when
1102 case elfcpp::R_386_TLS_GD: // Global-dynamic
1103 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1104 case elfcpp::R_386_TLS_DESC_CALL:
1105 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1106 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1107 case elfcpp::R_386_TLS_IE: // Initial-exec
1108 case elfcpp::R_386_TLS_IE_32:
1109 case elfcpp::R_386_TLS_GOTIE:
1110 case elfcpp::R_386_TLS_LE: // Local-exec
1111 case elfcpp::R_386_TLS_LE_32:
1113 const bool is_final = gsym->final_value_is_known();
1114 const tls::Tls_optimization optimized_type
1115 = Target_i386::optimize_tls_reloc(is_final, r_type);
1118 case elfcpp::R_386_TLS_GD: // Global-dynamic
1119 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1120 case elfcpp::R_386_TLS_DESC_CALL:
1121 // FIXME: If not relaxing to LE, we need to generate
1122 // DTPMOD32 and DTPOFF32 relocs.
1123 if (optimized_type != tls::TLSOPT_TO_LE)
1124 unsupported_reloc_global(object, r_type, gsym);
1127 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1128 // FIXME: If not relaxing to LE, we need to generate a
1130 if (optimized_type != tls::TLSOPT_TO_LE)
1131 unsupported_reloc_global(object, r_type, gsym);
1134 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1137 case elfcpp::R_386_TLS_IE: // Initial-exec
1138 case elfcpp::R_386_TLS_IE_32:
1139 case elfcpp::R_386_TLS_GOTIE:
1140 // FIXME: If not relaxing to LE, we need to generate a
1141 // TPOFF or TPOFF32 reloc.
1142 if (optimized_type != tls::TLSOPT_TO_LE)
1143 unsupported_reloc_global(object, r_type, gsym);
1146 case elfcpp::R_386_TLS_LE: // Local-exec
1147 case elfcpp::R_386_TLS_LE_32:
1148 // FIXME: If generating a shared object, we need to copy
1149 // this relocation into the object.
1150 gold_assert(!parameters->output_is_shared());
1159 case elfcpp::R_386_32PLT:
1160 case elfcpp::R_386_TLS_GD_32:
1161 case elfcpp::R_386_TLS_GD_PUSH:
1162 case elfcpp::R_386_TLS_GD_CALL:
1163 case elfcpp::R_386_TLS_GD_POP:
1164 case elfcpp::R_386_TLS_LDM_32:
1165 case elfcpp::R_386_TLS_LDM_PUSH:
1166 case elfcpp::R_386_TLS_LDM_CALL:
1167 case elfcpp::R_386_TLS_LDM_POP:
1168 case elfcpp::R_386_USED_BY_INTEL_200:
1170 unsupported_reloc_global(object, r_type, gsym);
1175 // Scan relocations for a section.
1178 Target_i386::scan_relocs(const General_options& options,
1179 Symbol_table* symtab,
1181 Sized_relobj<32, false>* object,
1182 unsigned int data_shndx,
1183 unsigned int sh_type,
1184 const unsigned char* prelocs,
1186 Output_section* output_section,
1187 bool needs_special_offset_handling,
1188 size_t local_symbol_count,
1189 const unsigned char* plocal_symbols)
1191 if (sh_type == elfcpp::SHT_RELA)
1193 gold_error(_("%s: unsupported RELA reloc section"),
1194 object->name().c_str());
1198 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1209 needs_special_offset_handling,
1214 // Finalize the sections.
1217 Target_i386::do_finalize_sections(Layout* layout)
1219 // Fill in some more dynamic tags.
1220 Output_data_dynamic* const odyn = layout->dynamic_data();
1223 if (this->got_plt_ != NULL)
1224 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1226 if (this->plt_ != NULL)
1228 const Output_data* od = this->plt_->rel_plt();
1229 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1230 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1231 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1234 if (this->rel_dyn_ != NULL)
1236 const Output_data* od = this->rel_dyn_;
1237 odyn->add_section_address(elfcpp::DT_REL, od);
1238 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1239 odyn->add_constant(elfcpp::DT_RELENT,
1240 elfcpp::Elf_sizes<32>::rel_size);
1243 if (!parameters->output_is_shared())
1245 // The value of the DT_DEBUG tag is filled in by the dynamic
1246 // linker at run time, and used by the debugger.
1247 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1251 // Emit any relocs we saved in an attempt to avoid generating COPY
1253 if (this->copy_relocs_ == NULL)
1255 if (this->copy_relocs_->any_to_emit())
1257 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1258 this->copy_relocs_->emit(rel_dyn);
1260 delete this->copy_relocs_;
1261 this->copy_relocs_ = NULL;
1264 // Return whether a direct absolute static relocation needs to be applied.
1265 // In cases where Scan::local() or Scan::global() has created
1266 // a dynamic relocation other than R_386_RELATIVE, the addend
1267 // of the relocation is carried in the data, and we must not
1268 // apply the static relocation.
1271 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1272 bool is_absolute_ref,
1273 bool is_function_call,
1276 // For local symbols, we will have created a non-RELATIVE dynamic
1277 // relocation only if (a) the output is position independent,
1278 // (b) the relocation is absolute (not pc- or segment-relative), and
1279 // (c) the relocation is not 32 bits wide.
1281 return !(parameters->output_is_position_independent()
1285 // For global symbols, we use the same helper routines used in the scan pass.
1286 return !(gsym->needs_dynamic_reloc(is_absolute_ref, is_function_call)
1287 && !gsym->can_use_relative_reloc(is_function_call));
1290 // Perform a relocation.
1293 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1294 Target_i386* target,
1296 const elfcpp::Rel<32, false>& rel,
1297 unsigned int r_type,
1298 const Sized_symbol<32>* gsym,
1299 const Symbol_value<32>* psymval,
1300 unsigned char* view,
1301 elfcpp::Elf_types<32>::Elf_Addr address,
1304 if (this->skip_call_tls_get_addr_)
1306 if (r_type != elfcpp::R_386_PLT32
1308 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1309 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1310 _("missing expected TLS relocation"));
1313 this->skip_call_tls_get_addr_ = false;
1318 // Pick the value to use for symbols defined in shared objects.
1319 Symbol_value<32> symval;
1321 && (gsym->is_from_dynobj()
1322 || (parameters->output_is_shared()
1323 && gsym->is_preemptible()))
1324 && gsym->has_plt_offset())
1326 symval.set_output_value(target->plt_section()->address()
1327 + gsym->plt_offset());
1331 const Sized_relobj<32, false>* object = relinfo->object;
1333 // Get the GOT offset if needed.
1334 // The GOT pointer points to the end of the GOT section.
1335 // We need to subtract the size of the GOT section to get
1336 // the actual offset to use in the relocation.
1337 bool have_got_offset = false;
1338 unsigned int got_offset = 0;
1341 case elfcpp::R_386_GOT32:
1344 gold_assert(gsym->has_got_offset());
1345 got_offset = gsym->got_offset() - target->got_size();
1349 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1350 got_offset = object->local_got_offset(r_sym) - target->got_size();
1352 have_got_offset = true;
1361 case elfcpp::R_386_NONE:
1362 case elfcpp::R_386_GNU_VTINHERIT:
1363 case elfcpp::R_386_GNU_VTENTRY:
1366 case elfcpp::R_386_32:
1367 if (should_apply_static_reloc(gsym, true, false, true))
1368 Relocate_functions<32, false>::rel32(view, object, psymval);
1371 case elfcpp::R_386_PC32:
1373 bool is_function_call = (gsym != NULL
1374 && gsym->type() == elfcpp::STT_FUNC);
1375 if (should_apply_static_reloc(gsym, false, is_function_call, true))
1376 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1380 case elfcpp::R_386_16:
1381 if (should_apply_static_reloc(gsym, true, false, false))
1382 Relocate_functions<32, false>::rel16(view, object, psymval);
1385 case elfcpp::R_386_PC16:
1387 bool is_function_call = (gsym != NULL
1388 && gsym->type() == elfcpp::STT_FUNC);
1389 if (should_apply_static_reloc(gsym, false, is_function_call, false))
1390 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1394 case elfcpp::R_386_8:
1395 if (should_apply_static_reloc(gsym, true, false, false))
1396 Relocate_functions<32, false>::rel8(view, object, psymval);
1399 case elfcpp::R_386_PC8:
1401 bool is_function_call = (gsym != NULL
1402 && gsym->type() == elfcpp::STT_FUNC);
1403 if (should_apply_static_reloc(gsym, false, is_function_call, false))
1404 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1408 case elfcpp::R_386_PLT32:
1409 gold_assert(gsym == NULL
1410 || gsym->has_plt_offset()
1411 || gsym->final_value_is_known());
1412 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1415 case elfcpp::R_386_GOT32:
1416 gold_assert(have_got_offset);
1417 Relocate_functions<32, false>::rel32(view, got_offset);
1420 case elfcpp::R_386_GOTOFF:
1422 elfcpp::Elf_types<32>::Elf_Addr value;
1423 value = (psymval->value(object, 0)
1424 - target->got_plt_section()->address());
1425 Relocate_functions<32, false>::rel32(view, value);
1429 case elfcpp::R_386_GOTPC:
1431 elfcpp::Elf_types<32>::Elf_Addr value;
1432 value = target->got_plt_section()->address();
1433 Relocate_functions<32, false>::pcrel32(view, value, address);
1437 case elfcpp::R_386_COPY:
1438 case elfcpp::R_386_GLOB_DAT:
1439 case elfcpp::R_386_JUMP_SLOT:
1440 case elfcpp::R_386_RELATIVE:
1441 // These are outstanding tls relocs, which are unexpected when
1443 case elfcpp::R_386_TLS_TPOFF:
1444 case elfcpp::R_386_TLS_DTPMOD32:
1445 case elfcpp::R_386_TLS_DTPOFF32:
1446 case elfcpp::R_386_TLS_TPOFF32:
1447 case elfcpp::R_386_TLS_DESC:
1448 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1449 _("unexpected reloc %u in object file"),
1453 // These are initial tls relocs, which are expected when
1455 case elfcpp::R_386_TLS_GD: // Global-dynamic
1456 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1457 case elfcpp::R_386_TLS_DESC_CALL:
1458 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1459 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1460 case elfcpp::R_386_TLS_IE: // Initial-exec
1461 case elfcpp::R_386_TLS_IE_32:
1462 case elfcpp::R_386_TLS_GOTIE:
1463 case elfcpp::R_386_TLS_LE: // Local-exec
1464 case elfcpp::R_386_TLS_LE_32:
1465 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
1466 address, view_size);
1469 case elfcpp::R_386_32PLT:
1470 case elfcpp::R_386_TLS_GD_32:
1471 case elfcpp::R_386_TLS_GD_PUSH:
1472 case elfcpp::R_386_TLS_GD_CALL:
1473 case elfcpp::R_386_TLS_GD_POP:
1474 case elfcpp::R_386_TLS_LDM_32:
1475 case elfcpp::R_386_TLS_LDM_PUSH:
1476 case elfcpp::R_386_TLS_LDM_CALL:
1477 case elfcpp::R_386_TLS_LDM_POP:
1478 case elfcpp::R_386_USED_BY_INTEL_200:
1480 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1481 _("unsupported reloc %u"),
1489 // Perform a TLS relocation.
1492 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1494 const elfcpp::Rel<32, false>& rel,
1495 unsigned int r_type,
1496 const Sized_symbol<32>* gsym,
1497 const Symbol_value<32>* psymval,
1498 unsigned char* view,
1499 elfcpp::Elf_types<32>::Elf_Addr,
1502 Output_segment* tls_segment = relinfo->layout->tls_segment();
1503 if (tls_segment == NULL)
1505 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1506 _("TLS reloc but no TLS segment"));
1510 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(relinfo->object, 0);
1512 const bool is_final = (gsym == NULL
1513 ? !parameters->output_is_position_independent()
1514 : gsym->final_value_is_known());
1515 const tls::Tls_optimization optimized_type
1516 = Target_i386::optimize_tls_reloc(is_final, r_type);
1519 case elfcpp::R_386_TLS_GD: // Global-dynamic
1520 if (optimized_type == tls::TLSOPT_TO_LE)
1522 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1523 rel, r_type, value, view,
1527 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1528 _("unsupported reloc %u"),
1532 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1533 case elfcpp::R_386_TLS_DESC_CALL:
1534 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1535 _("unsupported reloc %u"),
1539 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1540 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1542 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1543 _("both SUN and GNU model "
1544 "TLS relocations"));
1547 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1548 if (optimized_type == tls::TLSOPT_TO_LE)
1550 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1551 value, view, view_size);
1554 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1555 _("unsupported reloc %u"),
1559 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1560 // This reloc can appear in debugging sections, in which case we
1561 // won't see the TLS_LDM reloc. The local_dynamic_type field
1563 if (optimized_type != tls::TLSOPT_TO_LE
1564 || this->local_dynamic_type_ == LOCAL_DYNAMIC_NONE)
1565 value = value - tls_segment->vaddr();
1566 else if (this->local_dynamic_type_ == LOCAL_DYNAMIC_GNU)
1567 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1569 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1570 Relocate_functions<32, false>::rel32(view, value);
1573 case elfcpp::R_386_TLS_IE: // Initial-exec
1574 case elfcpp::R_386_TLS_GOTIE:
1575 case elfcpp::R_386_TLS_IE_32:
1576 if (optimized_type == tls::TLSOPT_TO_LE)
1578 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1579 rel, r_type, value, view,
1583 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1584 _("unsupported reloc %u"),
1588 case elfcpp::R_386_TLS_LE: // Local-exec
1589 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1590 Relocate_functions<32, false>::rel32(view, value);
1593 case elfcpp::R_386_TLS_LE_32:
1594 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1595 Relocate_functions<32, false>::rel32(view, value);
1600 // Do a relocation in which we convert a TLS General-Dynamic to a
1604 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1606 Output_segment* tls_segment,
1607 const elfcpp::Rel<32, false>& rel,
1609 elfcpp::Elf_types<32>::Elf_Addr value,
1610 unsigned char* view,
1613 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1614 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1615 // leal foo(%reg),%eax; call ___tls_get_addr
1616 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1618 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1619 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1621 unsigned char op1 = view[-1];
1622 unsigned char op2 = view[-2];
1624 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1625 op2 == 0x8d || op2 == 0x04);
1626 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1632 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
1633 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
1634 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1635 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
1636 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1640 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1641 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1642 if (static_cast<off_t>(rel.get_r_offset() + 9) < view_size
1645 // There is a trailing nop. Use the size byte subl.
1646 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1651 // Use the five byte subl.
1652 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1656 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1657 Relocate_functions<32, false>::rel32(view + roff, value);
1659 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1661 this->skip_call_tls_get_addr_ = true;
1664 // Do a relocation in which we convert a TLS Local-Dynamic to a
1668 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
1671 const elfcpp::Rel<32, false>& rel,
1673 elfcpp::Elf_types<32>::Elf_Addr,
1674 unsigned char* view,
1677 // leal foo(%reg), %eax; call ___tls_get_addr
1678 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1680 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1681 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1683 // FIXME: Does this test really always pass?
1684 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1685 view[-2] == 0x8d && view[-1] == 0x83);
1687 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1689 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1691 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1693 this->skip_call_tls_get_addr_ = true;
1696 // Do a relocation in which we convert a TLS Initial-Exec to a
1700 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1702 Output_segment* tls_segment,
1703 const elfcpp::Rel<32, false>& rel,
1704 unsigned int r_type,
1705 elfcpp::Elf_types<32>::Elf_Addr value,
1706 unsigned char* view,
1709 // We have to actually change the instructions, which means that we
1710 // need to examine the opcodes to figure out which instruction we
1712 if (r_type == elfcpp::R_386_TLS_IE)
1714 // movl %gs:XX,%eax ==> movl $YY,%eax
1715 // movl %gs:XX,%reg ==> movl $YY,%reg
1716 // addl %gs:XX,%reg ==> addl $YY,%reg
1717 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
1718 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
1720 unsigned char op1 = view[-1];
1723 // movl XX,%eax ==> movl $YY,%eax
1728 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1730 unsigned char op2 = view[-2];
1733 // movl XX,%reg ==> movl $YY,%reg
1734 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1735 (op1 & 0xc7) == 0x05);
1737 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1739 else if (op2 == 0x03)
1741 // addl XX,%reg ==> addl $YY,%reg
1742 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1743 (op1 & 0xc7) == 0x05);
1745 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1748 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
1753 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1754 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1755 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1756 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1757 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
1759 unsigned char op1 = view[-1];
1760 unsigned char op2 = view[-2];
1761 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1762 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1765 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1767 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1769 else if (op2 == 0x2b)
1771 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1773 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1775 else if (op2 == 0x03)
1777 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1779 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1782 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
1785 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1786 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1789 Relocate_functions<32, false>::rel32(view, value);
1792 // Relocate section data.
1795 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
1796 unsigned int sh_type,
1797 const unsigned char* prelocs,
1799 Output_section* output_section,
1800 bool needs_special_offset_handling,
1801 unsigned char* view,
1802 elfcpp::Elf_types<32>::Elf_Addr address,
1805 gold_assert(sh_type == elfcpp::SHT_REL);
1807 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1808 Target_i386::Relocate>(
1814 needs_special_offset_handling,
1820 // Return the value to use for a dynamic which requires special
1821 // treatment. This is how we support equality comparisons of function
1822 // pointers across shared library boundaries, as described in the
1823 // processor specific ABI supplement.
1826 Target_i386::do_dynsym_value(const Symbol* gsym) const
1828 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1829 return this->plt_section()->address() + gsym->plt_offset();
1832 // Return a string used to fill a code section with nops to take up
1833 // the specified length.
1836 Target_i386::do_code_fill(off_t length)
1840 // Build a jmp instruction to skip over the bytes.
1841 unsigned char jmp[5];
1843 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
1844 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1845 + std::string(length - 5, '\0'));
1848 // Nop sequences of various lengths.
1849 const char nop1[1] = { 0x90 }; // nop
1850 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1851 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1852 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1853 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1854 0x00 }; // leal 0(%esi,1),%esi
1855 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1857 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1859 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1860 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1861 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1862 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1864 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1865 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1867 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1868 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1870 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1871 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1872 0x00, 0x00, 0x00, 0x00 };
1873 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1874 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1875 0x27, 0x00, 0x00, 0x00,
1877 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1878 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1879 0xbc, 0x27, 0x00, 0x00,
1881 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1882 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1883 0x90, 0x90, 0x90, 0x90,
1886 const char* nops[16] = {
1888 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1889 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1892 return std::string(nops[length], length);
1895 // The selector for i386 object files.
1897 class Target_selector_i386 : public Target_selector
1900 Target_selector_i386()
1901 : Target_selector(elfcpp::EM_386, 32, false)
1905 recognize(int machine, int osabi, int abiversion);
1908 Target_i386* target_;
1911 // Recognize an i386 object file when we already know that the machine
1912 // number is EM_386.
1915 Target_selector_i386::recognize(int, int, int)
1917 if (this->target_ == NULL)
1918 this->target_ = new Target_i386();
1919 return this->target_;
1922 Target_selector_i386 target_selector_i386;
1924 } // End anonymous namespace.