1 // i386.cc -- i386 target support for gold.
15 #include "target-reloc.h"
16 #include "target-select.h"
23 class Output_data_plt_i386;
25 // The i386 target class.
27 class Target_i386 : public Sized_target<32, false>
30 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
33 : Sized_target<32, false>(&i386_info),
34 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
35 copy_relocs_(NULL), dynbss_(NULL)
38 // Scan the relocations to look for symbol adjustments.
40 scan_relocs(const General_options& options,
43 Sized_relobj<32, false>* object,
44 unsigned int data_shndx,
46 const unsigned char* prelocs,
48 size_t local_symbol_count,
49 const unsigned char* plocal_symbols,
50 Symbol** global_symbols);
52 // Finalize the sections.
54 do_finalize_sections(Layout*);
56 // Relocate a section.
58 relocate_section(const Relocate_info<32, false>*,
60 const unsigned char* prelocs,
63 elfcpp::Elf_types<32>::Elf_Addr view_address,
67 // The class which scans relocations.
71 local(const General_options& options, Symbol_table* symtab,
72 Layout* layout, Target_i386* target,
73 Sized_relobj<32, false>* object,
74 unsigned int data_shndx,
75 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
76 const elfcpp::Sym<32, false>& lsym);
79 global(const General_options& options, Symbol_table* symtab,
80 Layout* layout, Target_i386* target,
81 Sized_relobj<32, false>* object,
82 unsigned int data_shndx,
83 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
87 // The class which implements relocation.
92 : skip_call_tls_get_addr_(false)
97 if (this->skip_call_tls_get_addr_)
99 // FIXME: This needs to specify the location somehow.
100 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
106 // Do a relocation. Return false if the caller should not issue
107 // any warnings about this relocation.
109 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
110 const elfcpp::Rel<32, false>&,
111 unsigned int r_type, const Sized_symbol<32>*,
112 elfcpp::Elf_types<32>::Elf_Addr,
113 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
117 // Do a TLS relocation.
119 relocate_tls(const Relocate_info<32, false>*, size_t relnum,
120 const elfcpp::Rel<32, false>&,
121 unsigned int r_type, const Sized_symbol<32>*,
122 elfcpp::Elf_types<32>::Elf_Addr,
123 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
125 // Do a TLS Initial-Exec to Local-Exec transition.
127 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
128 Output_segment* tls_segment,
129 const elfcpp::Rel<32, false>&, unsigned int r_type,
130 elfcpp::Elf_types<32>::Elf_Addr value,
134 // Do a TLS Global-Dynamic to Local-Exec transition.
136 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
137 Output_segment* tls_segment,
138 const elfcpp::Rel<32, false>&, unsigned int r_type,
139 elfcpp::Elf_types<32>::Elf_Addr value,
143 // Check the range for a TLS relocation.
145 check_range(const Relocate_info<32, false>*, size_t relnum,
146 const elfcpp::Rel<32, false>&, off_t, off_t);
148 // Check the validity of a TLS relocation. This is like assert.
150 check_tls(const Relocate_info<32, false>*, size_t relnum,
151 const elfcpp::Rel<32, false>&, bool);
153 // This is set if we should skip the next reloc, which should be a
154 // PLT32 reloc against ___tls_get_addr.
155 bool skip_call_tls_get_addr_;
158 // Adjust TLS relocation type based on the options and whether this
159 // is a local symbol.
161 optimize_tls_reloc(const General_options*, bool is_final, int r_type);
163 // Get the GOT section, creating it if necessary.
164 Output_data_got<32, false>*
165 got_section(const General_options*, Symbol_table*, Layout*);
167 // Create a PLT entry for a global symbol.
169 make_plt_entry(const General_options* options, Symbol_table*,
172 // Get the PLT section.
173 Output_data_plt_i386*
176 gold_assert(this->plt_ != NULL);
180 // Get the dynamic reloc section, creating it if necessary.
182 rel_dyn_section(Layout*);
184 // Copy a relocation against a global symbol.
186 copy_reloc(const General_options*, Symbol_table*, Layout*,
187 Sized_relobj<32, false>*, unsigned int,
188 Symbol*, const elfcpp::Rel<32, false>&);
190 // Information about this specific target which we pass to the
191 // general Target structure.
192 static const Target::Target_info i386_info;
195 Output_data_got<32, false>* got_;
197 Output_data_plt_i386* plt_;
198 // The GOT PLT section.
199 Output_data_space* got_plt_;
200 // The dynamic reloc section.
201 Reloc_section* rel_dyn_;
202 // Relocs saved to avoid a COPY reloc.
203 Copy_relocs<32, false>* copy_relocs_;
204 // Space for variables copied with a COPY reloc.
205 Output_data_space* dynbss_;
208 const Target::Target_info Target_i386::i386_info =
211 false, // is_big_endian
212 elfcpp::EM_386, // machine_code
213 false, // has_make_symbol
214 false, // has_resolve
215 "/usr/lib/libc.so.1", // dynamic_linker
216 0x08048000, // text_segment_address
217 0x1000, // abi_pagesize
218 0x1000 // common_pagesize
221 // Get the GOT section, creating it if necessary.
223 Output_data_got<32, false>*
224 Target_i386::got_section(const General_options* options, Symbol_table* symtab,
227 if (this->got_ == NULL)
229 gold_assert(options != NULL && symtab != NULL && layout != NULL);
231 this->got_ = new Output_data_got<32, false>(options);
233 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
234 elfcpp::SHF_ALLOC, this->got_);
236 // The old GNU linker creates a .got.plt section. We just
237 // create another set of data in the .got section. Note that we
238 // always create a PLT if we create a GOT, although the PLT
240 this->got_plt_ = new Output_data_space(4);
241 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
242 elfcpp::SHF_ALLOC, this->got_plt_);
244 // The first three entries are reserved.
245 this->got_plt_->set_space_size(3 * 4);
247 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
248 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_",
250 0, 0, elfcpp::STT_OBJECT,
252 elfcpp::STV_HIDDEN, 0,
259 // Get the dynamic reloc section, creating it if necessary.
261 Target_i386::Reloc_section*
262 Target_i386::rel_dyn_section(Layout* layout)
264 if (this->rel_dyn_ == NULL)
266 gold_assert(layout != NULL);
267 this->rel_dyn_ = new Reloc_section();
268 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
269 elfcpp::SHF_ALLOC, this->rel_dyn_);
271 return this->rel_dyn_;
274 // A class to handle the PLT data.
276 class Output_data_plt_i386 : public Output_section_data
279 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
281 Output_data_plt_i386(Layout*, Output_data_space*, bool is_shared);
283 // Add an entry to the PLT.
285 add_entry(Symbol* gsym);
288 // The size of an entry in the PLT.
289 static const int plt_entry_size = 16;
291 // The first entry in the PLT for an executable.
292 static unsigned char exec_first_plt_entry[plt_entry_size];
294 // The first entry in the PLT for a shared object.
295 static unsigned char dyn_first_plt_entry[plt_entry_size];
297 // Other entries in the PLT for an executable.
298 static unsigned char exec_plt_entry[plt_entry_size];
300 // Other entries in the PLT for a shared object.
301 static unsigned char dyn_plt_entry[plt_entry_size];
303 // Set the final size.
305 do_set_address(uint64_t, off_t)
306 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
308 // Write out the PLT data.
310 do_write(Output_file*);
312 // The reloc section.
314 // The .got.plt section.
315 Output_data_space* got_plt_;
316 // The number of PLT entries.
318 // Whether we are generated a shared object.
322 // Create the PLT section. The ordinary .got section is an argument,
323 // since we need to refer to the start. We also create our own .got
324 // section just for PLT entries.
326 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
327 Output_data_space* got_plt,
329 : Output_section_data(4), got_plt_(got_plt), is_shared_(is_shared)
331 this->rel_ = new Reloc_section();
332 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
333 elfcpp::SHF_ALLOC, this->rel_);
336 // Add an entry to the PLT.
339 Output_data_plt_i386::add_entry(Symbol* gsym)
341 gold_assert(!gsym->has_plt_offset());
343 // Note that when setting the PLT offset we skip the initial
344 // reserved PLT entry.
345 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
349 off_t got_offset = this->got_plt_->data_size();
351 // Every PLT entry needs a GOT entry which points back to the PLT
352 // entry (this will be changed by the dynamic linker, normally
353 // lazily when the function is called).
354 this->got_plt_->set_space_size(got_offset + 4);
356 // Every PLT entry needs a reloc.
357 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
360 // Note that we don't need to save the symbol. The contents of the
361 // PLT are independent of which symbols are used. The symbols only
362 // appear in the relocations.
365 // The first entry in the PLT for an executable.
367 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
369 0xff, 0x35, // pushl contents of memory address
370 0, 0, 0, 0, // replaced with address of .got + 4
371 0xff, 0x25, // jmp indirect
372 0, 0, 0, 0, // replaced with address of .got + 8
376 // The first entry in the PLT for a shared object.
378 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
380 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
381 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
385 // Subsequent entries in the PLT for an executable.
387 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
389 0xff, 0x25, // jmp indirect
390 0, 0, 0, 0, // replaced with address of symbol in .got
391 0x68, // pushl immediate
392 0, 0, 0, 0, // replaced with offset into relocation table
393 0xe9, // jmp relative
394 0, 0, 0, 0 // replaced with offset to start of .plt
397 // Subsequent entries in the PLT for a shared object.
399 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
401 0xff, 0xa3, // jmp *offset(%ebx)
402 0, 0, 0, 0, // replaced with offset of symbol in .got
403 0x68, // pushl immediate
404 0, 0, 0, 0, // replaced with offset into relocation table
405 0xe9, // jmp relative
406 0, 0, 0, 0 // replaced with offset to start of .plt
409 // Write out the PLT. This uses the hand-coded instructions above,
410 // and adjusts them as needed. This is all specified by the i386 ELF
411 // Processor Supplement.
414 Output_data_plt_i386::do_write(Output_file* of)
416 const off_t offset = this->offset();
417 const off_t oview_size = this->data_size();
418 unsigned char* const oview = of->get_output_view(offset, oview_size);
420 const off_t got_file_offset = this->got_plt_->offset();
421 const off_t got_size = this->got_plt_->data_size();
422 unsigned char* const got_view = of->get_output_view(got_file_offset,
425 unsigned char* pov = oview;
427 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
428 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
430 if (this->is_shared_)
431 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
434 memcpy(pov, exec_first_plt_entry, plt_entry_size);
435 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
436 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
438 pov += plt_entry_size;
440 unsigned char* got_pov = got_view;
442 memset(got_pov, 0, 12);
445 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
447 unsigned int plt_offset = plt_entry_size;
448 unsigned int plt_rel_offset = 0;
449 unsigned int got_offset = 12;
450 const unsigned int count = this->count_;
451 for (unsigned int i = 0;
454 pov += plt_entry_size,
456 plt_offset += plt_entry_size,
457 plt_rel_offset += rel_size,
460 // Set and adjust the PLT entry itself.
462 if (this->is_shared_)
464 memcpy(pov, dyn_plt_entry, plt_entry_size);
465 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
469 memcpy(pov, exec_plt_entry, plt_entry_size);
470 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
475 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
476 elfcpp::Swap<32, false>::writeval(pov + 12,
477 - (plt_offset + plt_entry_size));
479 // Set the entry in the GOT.
480 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
483 gold_assert(pov - oview == oview_size);
484 gold_assert(got_pov - got_view == got_size);
486 of->write_output_view(offset, oview_size, oview);
487 of->write_output_view(got_file_offset, got_size, got_view);
490 // Create a PLT entry for a global symbol.
493 Target_i386::make_plt_entry(const General_options* options,
494 Symbol_table* symtab, Layout* layout, Symbol* gsym)
496 if (gsym->has_plt_offset())
499 if (this->plt_ == NULL)
501 // Create the GOT sections first.
502 this->got_section(options, symtab, layout);
504 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_,
505 options->is_shared());
508 this->plt_->add_entry(gsym);
511 // Handle a relocation against a non-function symbol defined in a
512 // dynamic object. The traditional way to handle this is to generate
513 // a COPY relocation to copy the variable at runtime from the shared
514 // object into the executable's data segment. However, this is
515 // undesirable in general, as if the size of the object changes in the
516 // dynamic object, the executable will no longer work correctly. If
517 // this relocation is in a writable section, then we can create a
518 // dynamic reloc and the dynamic linker will resolve it to the correct
519 // address at runtime. However, we do not want do that if the
520 // relocation is in a read-only section, as it would prevent the
521 // readonly segment from being shared. And if we have to eventually
522 // generate a COPY reloc, then any dynamic relocations will be
523 // useless. So this means that if this is a writable section, we need
524 // to save the relocation until we see whether we have to create a
525 // COPY relocation for this symbol for any other relocation.
528 Target_i386::copy_reloc(const General_options* options,
529 Symbol_table* symtab,
531 Sized_relobj<32, false>* object,
532 unsigned int data_shndx, Symbol* gsym,
533 const elfcpp::Rel<32, false>& rel)
535 Sized_symbol<32>* ssym;
536 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
539 if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
542 // So far we do not need a COPY reloc. Save this relocation.
543 // If it turns out that we never need a COPY reloc for this
544 // symbol, then we will emit the relocation.
545 if (this->copy_relocs_ == NULL)
546 this->copy_relocs_ = new Copy_relocs<32, false>();
547 this->copy_relocs_->save(ssym, object, data_shndx, rel);
551 // Allocate space for this symbol in the .bss section.
553 elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
555 // There is no defined way to determine the required alignment
556 // of the symbol. We pick the alignment based on the size. We
557 // set an arbitrary maximum of 256.
559 for (align = 1; align < 512; align <<= 1)
560 if ((symsize & align) != 0)
563 if (this->dynbss_ == NULL)
565 this->dynbss_ = new Output_data_space(align);
566 layout->add_output_section_data(".bss",
569 | elfcpp::SHF_WRITE),
573 Output_data_space* dynbss = this->dynbss_;
575 if (align > dynbss->addralign())
576 dynbss->set_space_alignment(align);
578 off_t dynbss_size = dynbss->data_size();
579 dynbss_size = align_address(dynbss_size, align);
580 off_t offset = dynbss_size;
581 dynbss->set_space_size(dynbss_size + symsize);
583 // Define the symbol in the .dynbss section.
584 symtab->define_in_output_data(this, ssym->name(), dynbss, offset,
585 symsize, ssym->type(), ssym->binding(),
586 ssym->visibility(), ssym->nonvis(),
589 // Add the COPY reloc.
590 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
591 rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
595 // Optimize the TLS relocation type based on what we know about the
596 // symbol. IS_FINAL is true if the final address of this symbol is
597 // known at link time.
600 Target_i386::optimize_tls_reloc(const General_options* options,
604 // If we are generating a shared library, then we can't do anything
606 if (options->is_shared())
611 case elfcpp::R_386_TLS_GD:
612 case elfcpp::R_386_TLS_GOTDESC:
613 case elfcpp::R_386_TLS_DESC_CALL:
614 // These are Global-Dynamic which permits fully general TLS
615 // access. Since we know that we are generating an executable,
616 // we can convert this to Initial-Exec. If we also know that
617 // this is a local symbol, we can further switch to Local-Exec.
619 return elfcpp::R_386_TLS_LE_32;
620 return elfcpp::R_386_TLS_IE_32;
622 case elfcpp::R_386_TLS_LDM:
623 // This is Local-Dynamic, which refers to a local symbol in the
624 // dynamic TLS block. Since we know that we generating an
625 // executable, we can switch to Local-Exec.
626 return elfcpp::R_386_TLS_LE_32;
628 case elfcpp::R_386_TLS_LDO_32:
629 // Another type of Local-Dynamic relocation.
630 return elfcpp::R_386_TLS_LE;
632 case elfcpp::R_386_TLS_IE:
633 case elfcpp::R_386_TLS_GOTIE:
634 case elfcpp::R_386_TLS_IE_32:
635 // These are Initial-Exec relocs which get the thread offset
636 // from the GOT. If we know that we are linking against the
637 // local symbol, we can switch to Local-Exec, which links the
638 // thread offset into the instruction.
640 return elfcpp::R_386_TLS_LE_32;
643 case elfcpp::R_386_TLS_LE:
644 case elfcpp::R_386_TLS_LE_32:
645 // When we already have Local-Exec, there is nothing further we
654 // Scan a relocation for a local symbol.
657 Target_i386::Scan::local(const General_options& options,
658 Symbol_table* symtab,
661 Sized_relobj<32, false>* object,
663 const elfcpp::Rel<32, false>&,
665 const elfcpp::Sym<32, false>&)
669 case elfcpp::R_386_NONE:
670 case elfcpp::R_386_GNU_VTINHERIT:
671 case elfcpp::R_386_GNU_VTENTRY:
674 case elfcpp::R_386_32:
675 case elfcpp::R_386_16:
676 case elfcpp::R_386_8:
677 // FIXME: If we are generating a shared object we need to copy
678 // this relocation into the object.
679 gold_assert(!options.is_shared());
682 case elfcpp::R_386_PC32:
683 case elfcpp::R_386_PC16:
684 case elfcpp::R_386_PC8:
687 case elfcpp::R_386_GOTOFF:
688 case elfcpp::R_386_GOTPC:
689 // We need a GOT section.
690 target->got_section(&options, symtab, layout);
693 case elfcpp::R_386_COPY:
694 case elfcpp::R_386_GLOB_DAT:
695 case elfcpp::R_386_JUMP_SLOT:
696 case elfcpp::R_386_RELATIVE:
697 case elfcpp::R_386_TLS_TPOFF:
698 case elfcpp::R_386_TLS_DTPMOD32:
699 case elfcpp::R_386_TLS_DTPOFF32:
700 case elfcpp::R_386_TLS_TPOFF32:
701 case elfcpp::R_386_TLS_DESC:
702 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
703 program_name, object->name().c_str(), r_type);
707 case elfcpp::R_386_TLS_IE:
708 case elfcpp::R_386_TLS_GOTIE:
709 case elfcpp::R_386_TLS_LE:
710 case elfcpp::R_386_TLS_GD:
711 case elfcpp::R_386_TLS_LDM:
712 case elfcpp::R_386_TLS_LDO_32:
713 case elfcpp::R_386_TLS_IE_32:
714 case elfcpp::R_386_TLS_LE_32:
715 case elfcpp::R_386_TLS_GOTDESC:
716 case elfcpp::R_386_TLS_DESC_CALL:
717 r_type = Target_i386::optimize_tls_reloc(&options,
718 !options.is_shared(),
722 case elfcpp::R_386_TLS_LE:
723 case elfcpp::R_386_TLS_LE_32:
724 // FIXME: If generating a shared object, we need to copy
725 // this relocation into the object.
726 gold_assert(!options.is_shared());
729 case elfcpp::R_386_TLS_IE:
730 case elfcpp::R_386_TLS_GOTIE:
731 case elfcpp::R_386_TLS_GD:
732 case elfcpp::R_386_TLS_LDM:
733 case elfcpp::R_386_TLS_LDO_32:
734 case elfcpp::R_386_TLS_IE_32:
735 case elfcpp::R_386_TLS_GOTDESC:
736 case elfcpp::R_386_TLS_DESC_CALL:
738 _("%s: %s: unsupported reloc %u against local symbol\n"),
739 program_name, object->name().c_str(), r_type);
744 case elfcpp::R_386_GOT32:
745 case elfcpp::R_386_PLT32:
746 case elfcpp::R_386_32PLT:
747 case elfcpp::R_386_TLS_GD_32:
748 case elfcpp::R_386_TLS_GD_PUSH:
749 case elfcpp::R_386_TLS_GD_CALL:
750 case elfcpp::R_386_TLS_GD_POP:
751 case elfcpp::R_386_TLS_LDM_32:
752 case elfcpp::R_386_TLS_LDM_PUSH:
753 case elfcpp::R_386_TLS_LDM_CALL:
754 case elfcpp::R_386_TLS_LDM_POP:
755 case elfcpp::R_386_USED_BY_INTEL_200:
757 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
758 program_name, object->name().c_str(), r_type);
763 // Scan a relocation for a global symbol.
766 Target_i386::Scan::global(const General_options& options,
767 Symbol_table* symtab,
770 Sized_relobj<32, false>* object,
771 unsigned int data_shndx,
772 const elfcpp::Rel<32, false>& reloc,
778 case elfcpp::R_386_NONE:
779 case elfcpp::R_386_GNU_VTINHERIT:
780 case elfcpp::R_386_GNU_VTENTRY:
783 case elfcpp::R_386_32:
784 case elfcpp::R_386_PC32:
785 case elfcpp::R_386_16:
786 case elfcpp::R_386_PC16:
787 case elfcpp::R_386_8:
788 case elfcpp::R_386_PC8:
789 // FIXME: If we are generating a shared object we may need to
790 // copy this relocation into the object. If this symbol is
791 // defined in a shared object, we may need to copy this
792 // relocation in order to avoid a COPY relocation.
793 gold_assert(!options.is_shared());
795 if (gsym->is_defined_in_dynobj())
797 // This symbol is defined in a dynamic object. If it is a
798 // function, we make a PLT entry. Otherwise we need to
799 // either generate a COPY reloc or copy this reloc.
800 if (gsym->type() == elfcpp::STT_FUNC)
801 target->make_plt_entry(&options, symtab, layout, gsym);
803 target->copy_reloc(&options, symtab, layout, object, data_shndx,
809 case elfcpp::R_386_GOT32:
810 // The symbol requires a GOT entry.
811 if (target->got_section(&options, symtab, layout)->add_global(gsym))
813 // If this symbol is not fully resolved, we need to add a
814 // dynamic relocation for it.
815 if (!gsym->final_value_is_known(&options))
820 case elfcpp::R_386_PLT32:
821 // If the symbol is fully resolved, this is just a PC32 reloc.
822 // Otherwise we need a PLT entry.
823 if (gsym->final_value_is_known(&options))
825 target->make_plt_entry(&options, symtab, layout, gsym);
828 case elfcpp::R_386_GOTOFF:
829 case elfcpp::R_386_GOTPC:
830 // We need a GOT section.
831 target->got_section(&options, symtab, layout);
834 case elfcpp::R_386_COPY:
835 case elfcpp::R_386_GLOB_DAT:
836 case elfcpp::R_386_JUMP_SLOT:
837 case elfcpp::R_386_RELATIVE:
838 case elfcpp::R_386_TLS_TPOFF:
839 case elfcpp::R_386_TLS_DTPMOD32:
840 case elfcpp::R_386_TLS_DTPOFF32:
841 case elfcpp::R_386_TLS_TPOFF32:
842 case elfcpp::R_386_TLS_DESC:
843 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
844 program_name, object->name().c_str(), r_type);
848 case elfcpp::R_386_TLS_IE:
849 case elfcpp::R_386_TLS_GOTIE:
850 case elfcpp::R_386_TLS_LE:
851 case elfcpp::R_386_TLS_GD:
852 case elfcpp::R_386_TLS_LDM:
853 case elfcpp::R_386_TLS_LDO_32:
854 case elfcpp::R_386_TLS_IE_32:
855 case elfcpp::R_386_TLS_LE_32:
856 case elfcpp::R_386_TLS_GOTDESC:
857 case elfcpp::R_386_TLS_DESC_CALL:
859 const bool is_final = gsym->final_value_is_known(&options);
860 r_type = Target_i386::optimize_tls_reloc(&options, is_final, r_type);
863 case elfcpp::R_386_TLS_LE:
864 case elfcpp::R_386_TLS_LE_32:
865 // FIXME: If generating a shared object, we need to copy
866 // this relocation into the object.
867 gold_assert(!options.is_shared());
870 case elfcpp::R_386_TLS_IE:
871 case elfcpp::R_386_TLS_GOTIE:
872 case elfcpp::R_386_TLS_GD:
873 case elfcpp::R_386_TLS_LDM:
874 case elfcpp::R_386_TLS_LDO_32:
875 case elfcpp::R_386_TLS_IE_32:
876 case elfcpp::R_386_TLS_GOTDESC:
877 case elfcpp::R_386_TLS_DESC_CALL:
879 _("%s: %s: unsupported reloc %u "
880 "against global symbol %s\n"),
881 program_name, object->name().c_str(), r_type,
888 case elfcpp::R_386_32PLT:
889 case elfcpp::R_386_TLS_GD_32:
890 case elfcpp::R_386_TLS_GD_PUSH:
891 case elfcpp::R_386_TLS_GD_CALL:
892 case elfcpp::R_386_TLS_GD_POP:
893 case elfcpp::R_386_TLS_LDM_32:
894 case elfcpp::R_386_TLS_LDM_PUSH:
895 case elfcpp::R_386_TLS_LDM_CALL:
896 case elfcpp::R_386_TLS_LDM_POP:
897 case elfcpp::R_386_USED_BY_INTEL_200:
900 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
901 program_name, object->name().c_str(), r_type, gsym->name());
906 // Scan relocations for a section.
909 Target_i386::scan_relocs(const General_options& options,
910 Symbol_table* symtab,
912 Sized_relobj<32, false>* object,
913 unsigned int data_shndx,
914 unsigned int sh_type,
915 const unsigned char* prelocs,
917 size_t local_symbol_count,
918 const unsigned char* plocal_symbols,
919 Symbol** global_symbols)
921 if (sh_type == elfcpp::SHT_RELA)
923 fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"),
924 program_name, object->name().c_str());
928 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
943 // Finalize the sections. This is where we emit any relocs we saved
944 // in an attempt to avoid generating extra COPY relocs.
947 Target_i386::do_finalize_sections(Layout* layout)
949 if (this->copy_relocs_ == NULL)
951 if (this->copy_relocs_->any_to_emit())
953 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
954 this->copy_relocs_->emit(rel_dyn);
956 delete this->copy_relocs_;
957 this->copy_relocs_ = NULL;
960 // Perform a relocation.
963 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
966 const elfcpp::Rel<32, false>& rel,
968 const Sized_symbol<32>* gsym,
969 elfcpp::Elf_types<32>::Elf_Addr value,
971 elfcpp::Elf_types<32>::Elf_Addr address,
974 if (this->skip_call_tls_get_addr_)
976 if (r_type != elfcpp::R_386_PLT32
978 || strcmp(gsym->name(), "___tls_get_addr") != 0)
980 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
982 relinfo->location(relnum, rel.get_r_offset()).c_str());
986 this->skip_call_tls_get_addr_ = false;
991 // Pick the value to use for symbols defined in shared objects.
992 if (gsym != NULL && gsym->is_defined_in_dynobj())
994 if (gsym->has_plt_offset())
995 address = target->plt_section()->address() + gsym->plt_offset();
1002 case elfcpp::R_386_NONE:
1003 case elfcpp::R_386_GNU_VTINHERIT:
1004 case elfcpp::R_386_GNU_VTENTRY:
1007 case elfcpp::R_386_32:
1008 Relocate_functions<32, false>::rel32(view, value);
1011 case elfcpp::R_386_PC32:
1012 Relocate_functions<32, false>::pcrel32(view, value, address);
1015 case elfcpp::R_386_16:
1016 Relocate_functions<32, false>::rel16(view, value);
1019 case elfcpp::R_386_PC16:
1020 Relocate_functions<32, false>::pcrel16(view, value, address);
1023 case elfcpp::R_386_8:
1024 Relocate_functions<32, false>::rel8(view, value);
1027 case elfcpp::R_386_PC8:
1028 Relocate_functions<32, false>::pcrel8(view, value, address);
1031 case elfcpp::R_386_PLT32:
1032 gold_assert(gsym->has_plt_offset()
1033 || gsym->final_value_is_known(relinfo->options));
1034 Relocate_functions<32, false>::pcrel32(view, value, address);
1037 case elfcpp::R_386_GOT32:
1038 // Local GOT offsets not yet supported.
1040 gold_assert(gsym->has_got_offset());
1041 value = gsym->got_offset();
1042 Relocate_functions<32, false>::rel32(view, value);
1045 case elfcpp::R_386_GOTOFF:
1046 value -= target->got_section(NULL, NULL, NULL)->address();
1047 Relocate_functions<32, false>::rel32(view, value);
1050 case elfcpp::R_386_GOTPC:
1051 value = target->got_section(NULL, NULL, NULL)->address();
1052 Relocate_functions<32, false>::pcrel32(view, value, address);
1055 case elfcpp::R_386_COPY:
1056 case elfcpp::R_386_GLOB_DAT:
1057 case elfcpp::R_386_JUMP_SLOT:
1058 case elfcpp::R_386_RELATIVE:
1059 case elfcpp::R_386_TLS_TPOFF:
1060 case elfcpp::R_386_TLS_DTPMOD32:
1061 case elfcpp::R_386_TLS_DTPOFF32:
1062 case elfcpp::R_386_TLS_TPOFF32:
1063 case elfcpp::R_386_TLS_DESC:
1064 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1066 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1071 case elfcpp::R_386_TLS_IE:
1072 case elfcpp::R_386_TLS_GOTIE:
1073 case elfcpp::R_386_TLS_LE:
1074 case elfcpp::R_386_TLS_GD:
1075 case elfcpp::R_386_TLS_LDM:
1076 case elfcpp::R_386_TLS_LDO_32:
1077 case elfcpp::R_386_TLS_IE_32:
1078 case elfcpp::R_386_TLS_LE_32:
1079 case elfcpp::R_386_TLS_GOTDESC:
1080 case elfcpp::R_386_TLS_DESC_CALL:
1081 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, value, view,
1082 address, view_size);
1085 case elfcpp::R_386_32PLT:
1086 case elfcpp::R_386_TLS_GD_32:
1087 case elfcpp::R_386_TLS_GD_PUSH:
1088 case elfcpp::R_386_TLS_GD_CALL:
1089 case elfcpp::R_386_TLS_GD_POP:
1090 case elfcpp::R_386_TLS_LDM_32:
1091 case elfcpp::R_386_TLS_LDM_PUSH:
1092 case elfcpp::R_386_TLS_LDM_CALL:
1093 case elfcpp::R_386_TLS_LDM_POP:
1094 case elfcpp::R_386_USED_BY_INTEL_200:
1096 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1098 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1100 // gold_exit(false);
1107 // Perform a TLS relocation.
1110 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1112 const elfcpp::Rel<32, false>& rel,
1113 unsigned int r_type,
1114 const Sized_symbol<32>* gsym,
1115 elfcpp::Elf_types<32>::Elf_Addr value,
1116 unsigned char* view,
1117 elfcpp::Elf_types<32>::Elf_Addr,
1120 Output_segment* tls_segment = relinfo->layout->tls_segment();
1121 if (tls_segment == NULL)
1123 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1125 relinfo->location(relnum, rel.get_r_offset()).c_str());
1129 const bool is_final = (gsym == NULL
1130 ? !relinfo->options->is_shared()
1131 : gsym->final_value_is_known(relinfo->options));
1132 const unsigned int opt_r_type =
1133 Target_i386::optimize_tls_reloc(relinfo->options, is_final, r_type);
1136 case elfcpp::R_386_TLS_LE_32:
1137 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1138 Relocate_functions<32, false>::rel32(view, value);
1141 case elfcpp::R_386_TLS_LE:
1142 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1143 Relocate_functions<32, false>::rel32(view, value);
1146 case elfcpp::R_386_TLS_IE:
1147 case elfcpp::R_386_TLS_GOTIE:
1148 case elfcpp::R_386_TLS_IE_32:
1149 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1151 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1152 rel, r_type, value, view,
1156 fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
1158 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1160 // gold_exit(false);
1163 case elfcpp::R_386_TLS_GD:
1164 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1166 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1167 rel, r_type, value, view,
1171 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1173 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1175 // gold_exit(false);
1178 case elfcpp::R_386_TLS_LDM:
1179 case elfcpp::R_386_TLS_LDO_32:
1180 case elfcpp::R_386_TLS_GOTDESC:
1181 case elfcpp::R_386_TLS_DESC_CALL:
1182 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1184 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1186 // gold_exit(false);
1191 // Do a relocation in which we convert a TLS Initial-Exec to a
1195 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1197 Output_segment* tls_segment,
1198 const elfcpp::Rel<32, false>& rel,
1199 unsigned int r_type,
1200 elfcpp::Elf_types<32>::Elf_Addr value,
1201 unsigned char* view,
1204 // We have to actually change the instructions, which means that we
1205 // need to examine the opcodes to figure out which instruction we
1207 if (r_type == elfcpp::R_386_TLS_IE)
1209 // movl %gs:XX,%eax ==> movl $YY,%eax
1210 // movl %gs:XX,%reg ==> movl $YY,%reg
1211 // addl %gs:XX,%reg ==> addl $YY,%reg
1212 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1213 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1215 unsigned char op1 = view[-1];
1218 // movl XX,%eax ==> movl $YY,%eax
1223 Target_i386::Relocate::check_range(relinfo, relnum, rel,
1226 unsigned char op2 = view[-2];
1229 // movl XX,%reg ==> movl $YY,%reg
1230 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1231 (op1 & 0xc7) == 0x05);
1233 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1235 else if (op2 == 0x03)
1237 // addl XX,%reg ==> addl $YY,%reg
1238 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1239 (op1 & 0xc7) == 0x05);
1241 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1244 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1249 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1250 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1251 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1252 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1253 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1255 unsigned char op1 = view[-1];
1256 unsigned char op2 = view[-2];
1257 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1258 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1261 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1263 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1265 else if (op2 == 0x2b)
1267 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1269 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1271 else if (op2 == 0x03)
1273 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1275 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1278 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1281 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1282 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1285 Relocate_functions<32, false>::rel32(view, value);
1288 // Do a relocation in which we convert a TLS Global-Dynamic to a
1292 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1294 Output_segment* tls_segment,
1295 const elfcpp::Rel<32, false>& rel,
1297 elfcpp::Elf_types<32>::Elf_Addr value,
1298 unsigned char* view,
1301 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1302 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1303 // leal foo(%reg),%eax; call ___tls_get_addr
1304 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1306 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1307 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1309 unsigned char op1 = view[-1];
1310 unsigned char op2 = view[-2];
1312 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1313 op2 == 0x8d || op2 == 0x04);
1314 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1321 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1322 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1324 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1325 ((op1 & 0xc7) == 0x05
1326 && op1 != (4 << 3)));
1327 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1331 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1332 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1333 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1335 // There is a trailing nop. Use the size byte subl.
1336 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1341 // Use the five byte subl.
1342 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1346 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1347 Relocate_functions<32, false>::rel32(view + roff, value);
1349 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1351 this->skip_call_tls_get_addr_ = true;
1354 // Check the range for a TLS relocation.
1357 Target_i386::Relocate::check_range(const Relocate_info<32, false>* relinfo,
1359 const elfcpp::Rel<32, false>& rel,
1360 off_t view_size, off_t off)
1362 off_t offset = rel.get_r_offset() + off;
1363 if (offset < 0 || offset > view_size)
1365 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1367 relinfo->location(relnum, rel.get_r_offset()).c_str());
1372 // Check the validity of a TLS relocation. This is like assert.
1375 Target_i386::Relocate::check_tls(const Relocate_info<32, false>* relinfo,
1377 const elfcpp::Rel<32, false>& rel,
1383 _("%s: %s: TLS relocation against invalid instruction\n"),
1385 relinfo->location(relnum, rel.get_r_offset()).c_str());
1390 // Relocate section data.
1393 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
1394 unsigned int sh_type,
1395 const unsigned char* prelocs,
1397 unsigned char* view,
1398 elfcpp::Elf_types<32>::Elf_Addr address,
1401 gold_assert(sh_type == elfcpp::SHT_REL);
1403 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1404 Target_i386::Relocate>(
1414 // The selector for i386 object files.
1416 class Target_selector_i386 : public Target_selector
1419 Target_selector_i386()
1420 : Target_selector(elfcpp::EM_386, 32, false)
1424 recognize(int machine, int osabi, int abiversion);
1427 Target_i386* target_;
1430 // Recognize an i386 object file when we already know that the machine
1431 // number is EM_386.
1434 Target_selector_i386::recognize(int, int, int)
1436 if (this->target_ == NULL)
1437 this->target_ = new Target_i386();
1438 return this->target_;
1441 Target_selector_i386 target_selector_i386;
1443 } // End anonymous namespace.