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>
31 : Sized_target<32, false>(&i386_info),
32 got_(NULL), plt_(NULL), got_plt_(NULL)
35 // Scan the relocations to look for symbol adjustments.
37 scan_relocs(const General_options& options,
40 Sized_relobj<32, false>* object,
41 unsigned int data_shndx,
43 const unsigned char* prelocs,
45 size_t local_symbol_count,
46 const unsigned char* plocal_symbols,
47 Symbol** global_symbols);
49 // Relocate a section.
51 relocate_section(const Relocate_info<32, false>*,
53 const unsigned char* prelocs,
56 elfcpp::Elf_types<32>::Elf_Addr view_address,
60 // The class which scans relocations.
64 local(const General_options& options, Symbol_table* symtab,
65 Layout* layout, Target_i386* target,
66 Sized_relobj<32, false>* object,
67 unsigned int data_shndx,
68 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
69 const elfcpp::Sym<32, false>& lsym);
72 global(const General_options& options, Symbol_table* symtab,
73 Layout* layout, Target_i386* target,
74 Sized_relobj<32, false>* object,
75 unsigned int data_shndx,
76 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
80 // The class which implements relocation.
85 : skip_call_tls_get_addr_(false)
90 if (this->skip_call_tls_get_addr_)
92 // FIXME: This needs to specify the location somehow.
93 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
99 // Do a relocation. Return false if the caller should not issue
100 // any warnings about this relocation.
102 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
103 const elfcpp::Rel<32, false>&,
104 unsigned int r_type, const Sized_symbol<32>*,
105 elfcpp::Elf_types<32>::Elf_Addr,
106 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
110 // Do a TLS relocation.
112 relocate_tls(const Relocate_info<32, false>*, size_t relnum,
113 const elfcpp::Rel<32, false>&,
114 unsigned int r_type, const Sized_symbol<32>*,
115 elfcpp::Elf_types<32>::Elf_Addr,
116 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
118 // Do a TLS Initial-Exec to Local-Exec transition.
120 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
121 Output_segment* tls_segment,
122 const elfcpp::Rel<32, false>&, unsigned int r_type,
123 elfcpp::Elf_types<32>::Elf_Addr value,
127 // Do a TLS Global-Dynamic to Local-Exec transition.
129 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
130 Output_segment* tls_segment,
131 const elfcpp::Rel<32, false>&, unsigned int r_type,
132 elfcpp::Elf_types<32>::Elf_Addr value,
136 // Check the range for a TLS relocation.
138 check_range(const Relocate_info<32, false>*, size_t relnum,
139 const elfcpp::Rel<32, false>&, off_t, off_t);
141 // Check the validity of a TLS relocation. This is like assert.
143 check_tls(const Relocate_info<32, false>*, size_t relnum,
144 const elfcpp::Rel<32, false>&, bool);
146 // This is set if we should skip the next reloc, which should be a
147 // PLT32 reloc against ___tls_get_addr.
148 bool skip_call_tls_get_addr_;
151 // Adjust TLS relocation type based on the options and whether this
152 // is a local symbol.
154 optimize_tls_reloc(const General_options*, bool is_final, int r_type);
156 // Get the GOT section, creating it if necessary.
157 Output_data_got<32, false>*
158 got_section(const General_options*, Symbol_table*, Layout*);
160 // Create a PLT entry for a global symbol.
162 make_plt_entry(const General_options* options, Symbol_table*,
165 // Get the PLT section.
166 Output_data_plt_i386*
169 gold_assert(this->plt_ != NULL);
173 // Copy a relocation against a global symbol.
175 copy_reloc(const General_options*, Sized_relobj<32, false>*, unsigned int,
176 Symbol*, const elfcpp::Rel<32, false>&);
178 // Information about this specific target which we pass to the
179 // general Target structure.
180 static const Target::Target_info i386_info;
183 Output_data_got<32, false>* got_;
185 Output_data_plt_i386* plt_;
186 // The GOT PLT section.
187 Output_data_space* got_plt_;
190 const Target::Target_info Target_i386::i386_info =
193 false, // is_big_endian
194 elfcpp::EM_386, // machine_code
195 false, // has_make_symbol
196 false, // has_resolve
197 "/usr/lib/libc.so.1", // dynamic_linker
198 0x08048000, // text_segment_address
199 0x1000, // abi_pagesize
200 0x1000 // common_pagesize
203 // Get the GOT section, creating it if necessary.
205 Output_data_got<32, false>*
206 Target_i386::got_section(const General_options* options, Symbol_table* symtab,
209 if (this->got_ == NULL)
211 gold_assert(options != NULL && symtab != NULL && layout != NULL);
213 this->got_ = new Output_data_got<32, false>(options);
215 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
216 elfcpp::SHF_ALLOC, this->got_);
218 // The old GNU linker creates a .got.plt section. We just
219 // create another set of data in the .got section. Note that we
220 // always create a PLT if we create a GOT, although the PLT
222 this->got_plt_ = new Output_data_space(4);
223 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
224 elfcpp::SHF_ALLOC, this->got_plt_);
226 // The first three entries are reserved.
227 this->got_plt_->set_space_size(3 * 4);
229 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
230 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_",
232 0, 0, elfcpp::STT_OBJECT,
234 elfcpp::STV_HIDDEN, 0,
241 // A class to handle the PLT data.
243 class Output_data_plt_i386 : public Output_section_data
246 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
248 Output_data_plt_i386(Layout*, Output_data_space*, bool is_shared);
250 // Add an entry to the PLT.
252 add_entry(Symbol* gsym);
255 // The size of an entry in the PLT.
256 static const int plt_entry_size = 16;
258 // The first entry in the PLT for an executable.
259 static unsigned char exec_first_plt_entry[plt_entry_size];
261 // The first entry in the PLT for a shared object.
262 static unsigned char dyn_first_plt_entry[plt_entry_size];
264 // Other entries in the PLT for an executable.
265 static unsigned char exec_plt_entry[plt_entry_size];
267 // Other entries in the PLT for a shared object.
268 static unsigned char dyn_plt_entry[plt_entry_size];
270 // Set the final size.
272 do_set_address(uint64_t, off_t)
273 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
275 // Write out the PLT data.
277 do_write(Output_file*);
279 // The reloc section.
281 // The .got.plt section.
282 Output_data_space* got_plt_;
283 // The number of PLT entries.
285 // Whether we are generated a shared object.
289 // Create the PLT section. The ordinary .got section is an argument,
290 // since we need to refer to the start. We also create our own .got
291 // section just for PLT entries.
293 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
294 Output_data_space* got_plt,
296 : Output_section_data(4), got_plt_(got_plt), is_shared_(is_shared)
298 this->rel_ = new Reloc_section();
299 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
300 elfcpp::SHF_ALLOC, this->rel_);
303 // Add an entry to the PLT.
306 Output_data_plt_i386::add_entry(Symbol* gsym)
308 gold_assert(!gsym->has_plt_offset());
310 // Note that when setting the PLT offset we skip the initial
311 // reserved PLT entry.
312 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
316 off_t got_offset = this->got_plt_->data_size();
318 // Every PLT entry needs a GOT entry which points back to the PLT
319 // entry (this will be changed by the dynamic linker, normally
320 // lazily when the function is called).
321 this->got_plt_->set_space_size(got_offset + 4);
323 // Every PLT entry needs a reloc.
324 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
327 // Note that we don't need to save the symbol. The contents of the
328 // PLT are independent of which symbols are used. The symbols only
329 // appear in the relocations.
332 // The first entry in the PLT for an executable.
334 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
336 0xff, 0x35, // pushl contents of memory address
337 0, 0, 0, 0, // replaced with address of .got + 4
338 0xff, 0x25, // jmp indirect
339 0, 0, 0, 0, // replaced with address of .got + 8
343 // The first entry in the PLT for a shared object.
345 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
347 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
348 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
352 // Subsequent entries in the PLT for an executable.
354 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
356 0xff, 0x25, // jmp indirect
357 0, 0, 0, 0, // replaced with address of symbol in .got
358 0x68, // pushl immediate
359 0, 0, 0, 0, // replaced with offset into relocation table
360 0xe9, // jmp relative
361 0, 0, 0, 0 // replaced with offset to start of .plt
364 // Subsequent entries in the PLT for a shared object.
366 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
368 0xff, 0xa3, // jmp *offset(%ebx)
369 0, 0, 0, 0, // replaced with offset of symbol in .got
370 0x68, // pushl immediate
371 0, 0, 0, 0, // replaced with offset into relocation table
372 0xe9, // jmp relative
373 0, 0, 0, 0 // replaced with offset to start of .plt
376 // Write out the PLT. This uses the hand-coded instructions above,
377 // and adjusts them as needed. This is all specified by the i386 ELF
378 // Processor Supplement.
381 Output_data_plt_i386::do_write(Output_file* of)
383 const off_t offset = this->offset();
384 const off_t oview_size = this->data_size();
385 unsigned char* const oview = of->get_output_view(offset, oview_size);
387 const off_t got_file_offset = this->got_plt_->offset();
388 const off_t got_size = this->got_plt_->data_size();
389 unsigned char* const got_view = of->get_output_view(got_file_offset,
392 unsigned char* pov = oview;
394 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
395 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
397 if (this->is_shared_)
398 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
401 memcpy(pov, exec_first_plt_entry, plt_entry_size);
402 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
403 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
405 pov += plt_entry_size;
407 unsigned char* got_pov = got_view;
409 memset(got_pov, 0, 12);
412 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
414 unsigned int plt_offset = plt_entry_size;
415 unsigned int plt_rel_offset = 0;
416 unsigned int got_offset = 12;
417 const unsigned int count = this->count_;
418 for (unsigned int i = 0;
421 pov += plt_entry_size,
423 plt_offset += plt_entry_size,
424 plt_rel_offset += rel_size,
427 // Set and adjust the PLT entry itself.
429 if (this->is_shared_)
431 memcpy(pov, dyn_plt_entry, plt_entry_size);
432 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
436 memcpy(pov, exec_plt_entry, plt_entry_size);
437 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
442 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
443 elfcpp::Swap<32, false>::writeval(pov + 12,
444 - (plt_offset + plt_entry_size));
446 // Set the entry in the GOT.
447 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
450 gold_assert(pov - oview == oview_size);
451 gold_assert(got_pov - got_view == got_size);
453 of->write_output_view(offset, oview_size, oview);
454 of->write_output_view(got_file_offset, got_size, got_view);
457 // Create a PLT entry for a global symbol.
460 Target_i386::make_plt_entry(const General_options* options,
461 Symbol_table* symtab, Layout* layout, Symbol* gsym)
463 if (gsym->has_plt_offset())
466 if (this->plt_ == NULL)
468 // Create the GOT sections first.
469 this->got_section(options, symtab, layout);
471 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_,
472 options->is_shared());
475 this->plt_->add_entry(gsym);
478 // Handle a relocation against a non-function symbol defined in a
479 // dynamic object. The traditional way to handle this is to generate
480 // a COPY relocation to copy the variable at runtime from the shared
481 // object into the executable's data segment. However, this is
482 // undesirable in general, as if the size of the object changes in the
483 // dynamic object, the executable will no longer work correctly. If
484 // this relocation is in a writable section, then we can create a
485 // dynamic reloc and the dynamic linker will resolve it to the correct
486 // address at runtime. However, we do not want do that if the
487 // relocation is in a read-only section, as it would prevent the
488 // readonly segment from being shared. And if we have to eventually
489 // generate a COPY reloc, then any dynamic relocations will be
490 // useless. So this means that if this is a writable section, we need
491 // to save the relocation until we see whether we have to create a
492 // COPY relocation for this symbol for any other relocation.
495 Target_i386::copy_reloc(const General_options* options,
496 Sized_relobj<32, false>* object,
497 unsigned int data_shndx, Symbol* gsym,
498 const elfcpp::Rel<32, false>&)
500 if (!Relocate_functions<32, false>::need_copy_reloc(options, object,
503 // So far we do not need a COPY reloc. Save this relocation.
504 // If it turns out that we never a COPY reloc for this symbol,
505 // then we emit the relocation.
510 // Optimize the TLS relocation type based on what we know about the
511 // symbol. IS_FINAL is true if the final address of this symbol is
512 // known at link time.
515 Target_i386::optimize_tls_reloc(const General_options* options,
519 // If we are generating a shared library, then we can't do anything
521 if (options->is_shared())
526 case elfcpp::R_386_TLS_GD:
527 case elfcpp::R_386_TLS_GOTDESC:
528 case elfcpp::R_386_TLS_DESC_CALL:
529 // These are Global-Dynamic which permits fully general TLS
530 // access. Since we know that we are generating an executable,
531 // we can convert this to Initial-Exec. If we also know that
532 // this is a local symbol, we can further switch to Local-Exec.
534 return elfcpp::R_386_TLS_LE_32;
535 return elfcpp::R_386_TLS_IE_32;
537 case elfcpp::R_386_TLS_LDM:
538 // This is Local-Dynamic, which refers to a local symbol in the
539 // dynamic TLS block. Since we know that we generating an
540 // executable, we can switch to Local-Exec.
541 return elfcpp::R_386_TLS_LE_32;
543 case elfcpp::R_386_TLS_LDO_32:
544 // Another type of Local-Dynamic relocation.
545 return elfcpp::R_386_TLS_LE;
547 case elfcpp::R_386_TLS_IE:
548 case elfcpp::R_386_TLS_GOTIE:
549 case elfcpp::R_386_TLS_IE_32:
550 // These are Initial-Exec relocs which get the thread offset
551 // from the GOT. If we know that we are linking against the
552 // local symbol, we can switch to Local-Exec, which links the
553 // thread offset into the instruction.
555 return elfcpp::R_386_TLS_LE_32;
558 case elfcpp::R_386_TLS_LE:
559 case elfcpp::R_386_TLS_LE_32:
560 // When we already have Local-Exec, there is nothing further we
569 // Scan a relocation for a local symbol.
572 Target_i386::Scan::local(const General_options& options,
573 Symbol_table* symtab,
576 Sized_relobj<32, false>* object,
578 const elfcpp::Rel<32, false>&,
580 const elfcpp::Sym<32, false>&)
584 case elfcpp::R_386_NONE:
585 case elfcpp::R_386_GNU_VTINHERIT:
586 case elfcpp::R_386_GNU_VTENTRY:
589 case elfcpp::R_386_32:
590 case elfcpp::R_386_16:
591 case elfcpp::R_386_8:
592 // FIXME: If we are generating a shared object we need to copy
593 // this relocation into the object.
594 gold_assert(!options.is_shared());
597 case elfcpp::R_386_PC32:
598 case elfcpp::R_386_PC16:
599 case elfcpp::R_386_PC8:
602 case elfcpp::R_386_GOTOFF:
603 case elfcpp::R_386_GOTPC:
604 // We need a GOT section.
605 target->got_section(&options, symtab, layout);
608 case elfcpp::R_386_COPY:
609 case elfcpp::R_386_GLOB_DAT:
610 case elfcpp::R_386_JUMP_SLOT:
611 case elfcpp::R_386_RELATIVE:
612 case elfcpp::R_386_TLS_TPOFF:
613 case elfcpp::R_386_TLS_DTPMOD32:
614 case elfcpp::R_386_TLS_DTPOFF32:
615 case elfcpp::R_386_TLS_TPOFF32:
616 case elfcpp::R_386_TLS_DESC:
617 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
618 program_name, object->name().c_str(), r_type);
622 case elfcpp::R_386_TLS_IE:
623 case elfcpp::R_386_TLS_GOTIE:
624 case elfcpp::R_386_TLS_LE:
625 case elfcpp::R_386_TLS_GD:
626 case elfcpp::R_386_TLS_LDM:
627 case elfcpp::R_386_TLS_LDO_32:
628 case elfcpp::R_386_TLS_IE_32:
629 case elfcpp::R_386_TLS_LE_32:
630 case elfcpp::R_386_TLS_GOTDESC:
631 case elfcpp::R_386_TLS_DESC_CALL:
632 r_type = Target_i386::optimize_tls_reloc(&options,
633 !options.is_shared(),
637 case elfcpp::R_386_TLS_LE:
638 case elfcpp::R_386_TLS_LE_32:
639 // FIXME: If generating a shared object, we need to copy
640 // this relocation into the object.
641 gold_assert(!options.is_shared());
644 case elfcpp::R_386_TLS_IE:
645 case elfcpp::R_386_TLS_GOTIE:
646 case elfcpp::R_386_TLS_GD:
647 case elfcpp::R_386_TLS_LDM:
648 case elfcpp::R_386_TLS_LDO_32:
649 case elfcpp::R_386_TLS_IE_32:
650 case elfcpp::R_386_TLS_GOTDESC:
651 case elfcpp::R_386_TLS_DESC_CALL:
653 _("%s: %s: unsupported reloc %u against local symbol\n"),
654 program_name, object->name().c_str(), r_type);
659 case elfcpp::R_386_GOT32:
660 case elfcpp::R_386_PLT32:
661 case elfcpp::R_386_32PLT:
662 case elfcpp::R_386_TLS_GD_32:
663 case elfcpp::R_386_TLS_GD_PUSH:
664 case elfcpp::R_386_TLS_GD_CALL:
665 case elfcpp::R_386_TLS_GD_POP:
666 case elfcpp::R_386_TLS_LDM_32:
667 case elfcpp::R_386_TLS_LDM_PUSH:
668 case elfcpp::R_386_TLS_LDM_CALL:
669 case elfcpp::R_386_TLS_LDM_POP:
670 case elfcpp::R_386_USED_BY_INTEL_200:
672 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
673 program_name, object->name().c_str(), r_type);
678 // Scan a relocation for a global symbol.
681 Target_i386::Scan::global(const General_options& options,
682 Symbol_table* symtab,
685 Sized_relobj<32, false>* object,
686 unsigned int data_shndx,
687 const elfcpp::Rel<32, false>& reloc,
693 case elfcpp::R_386_NONE:
694 case elfcpp::R_386_GNU_VTINHERIT:
695 case elfcpp::R_386_GNU_VTENTRY:
698 case elfcpp::R_386_32:
699 case elfcpp::R_386_PC32:
700 case elfcpp::R_386_16:
701 case elfcpp::R_386_PC16:
702 case elfcpp::R_386_8:
703 case elfcpp::R_386_PC8:
704 // FIXME: If we are generating a shared object we may need to
705 // copy this relocation into the object. If this symbol is
706 // defined in a shared object, we may need to copy this
707 // relocation in order to avoid a COPY relocation.
708 gold_assert(!options.is_shared());
710 if (gsym->is_defined_in_dynobj())
712 // This symbol is defined in a dynamic object. If it is a
713 // function, we make a PLT entry. Otherwise we need to
714 // either generate a COPY reloc or copy this reloc.
715 if (gsym->type() == elfcpp::STT_FUNC)
716 target->make_plt_entry(&options, symtab, layout, gsym);
718 target->copy_reloc(&options, object, data_shndx, gsym, reloc);
723 case elfcpp::R_386_GOT32:
724 // The symbol requires a GOT entry.
725 if (target->got_section(&options, symtab, layout)->add_global(gsym))
727 // If this symbol is not fully resolved, we need to add a
728 // dynamic relocation for it.
729 if (!gsym->final_value_is_known(&options))
734 case elfcpp::R_386_PLT32:
735 // If the symbol is fully resolved, this is just a PC32 reloc.
736 // Otherwise we need a PLT entry.
737 if (gsym->final_value_is_known(&options))
739 target->make_plt_entry(&options, symtab, layout, gsym);
742 case elfcpp::R_386_GOTOFF:
743 case elfcpp::R_386_GOTPC:
744 // We need a GOT section.
745 target->got_section(&options, symtab, layout);
748 case elfcpp::R_386_COPY:
749 case elfcpp::R_386_GLOB_DAT:
750 case elfcpp::R_386_JUMP_SLOT:
751 case elfcpp::R_386_RELATIVE:
752 case elfcpp::R_386_TLS_TPOFF:
753 case elfcpp::R_386_TLS_DTPMOD32:
754 case elfcpp::R_386_TLS_DTPOFF32:
755 case elfcpp::R_386_TLS_TPOFF32:
756 case elfcpp::R_386_TLS_DESC:
757 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
758 program_name, object->name().c_str(), r_type);
762 case elfcpp::R_386_TLS_IE:
763 case elfcpp::R_386_TLS_GOTIE:
764 case elfcpp::R_386_TLS_LE:
765 case elfcpp::R_386_TLS_GD:
766 case elfcpp::R_386_TLS_LDM:
767 case elfcpp::R_386_TLS_LDO_32:
768 case elfcpp::R_386_TLS_IE_32:
769 case elfcpp::R_386_TLS_LE_32:
770 case elfcpp::R_386_TLS_GOTDESC:
771 case elfcpp::R_386_TLS_DESC_CALL:
773 const bool is_final = gsym->final_value_is_known(&options);
774 r_type = Target_i386::optimize_tls_reloc(&options, is_final, r_type);
777 case elfcpp::R_386_TLS_LE:
778 case elfcpp::R_386_TLS_LE_32:
779 // FIXME: If generating a shared object, we need to copy
780 // this relocation into the object.
781 gold_assert(!options.is_shared());
784 case elfcpp::R_386_TLS_IE:
785 case elfcpp::R_386_TLS_GOTIE:
786 case elfcpp::R_386_TLS_GD:
787 case elfcpp::R_386_TLS_LDM:
788 case elfcpp::R_386_TLS_LDO_32:
789 case elfcpp::R_386_TLS_IE_32:
790 case elfcpp::R_386_TLS_GOTDESC:
791 case elfcpp::R_386_TLS_DESC_CALL:
793 _("%s: %s: unsupported reloc %u "
794 "against global symbol %s\n"),
795 program_name, object->name().c_str(), r_type,
802 case elfcpp::R_386_32PLT:
803 case elfcpp::R_386_TLS_GD_32:
804 case elfcpp::R_386_TLS_GD_PUSH:
805 case elfcpp::R_386_TLS_GD_CALL:
806 case elfcpp::R_386_TLS_GD_POP:
807 case elfcpp::R_386_TLS_LDM_32:
808 case elfcpp::R_386_TLS_LDM_PUSH:
809 case elfcpp::R_386_TLS_LDM_CALL:
810 case elfcpp::R_386_TLS_LDM_POP:
811 case elfcpp::R_386_USED_BY_INTEL_200:
814 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
815 program_name, object->name().c_str(), r_type, gsym->name());
820 // Scan relocations for a section.
823 Target_i386::scan_relocs(const General_options& options,
824 Symbol_table* symtab,
826 Sized_relobj<32, false>* object,
827 unsigned int data_shndx,
828 unsigned int sh_type,
829 const unsigned char* prelocs,
831 size_t local_symbol_count,
832 const unsigned char* plocal_symbols,
833 Symbol** global_symbols)
835 if (sh_type == elfcpp::SHT_RELA)
837 fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"),
838 program_name, object->name().c_str());
842 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
857 // Perform a relocation.
860 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
863 const elfcpp::Rel<32, false>& rel,
865 const Sized_symbol<32>* gsym,
866 elfcpp::Elf_types<32>::Elf_Addr value,
868 elfcpp::Elf_types<32>::Elf_Addr address,
871 if (this->skip_call_tls_get_addr_)
873 if (r_type != elfcpp::R_386_PLT32
875 || strcmp(gsym->name(), "___tls_get_addr") != 0)
877 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
879 relinfo->location(relnum, rel.get_r_offset()).c_str());
883 this->skip_call_tls_get_addr_ = false;
888 // Pick the value to use for symbols defined in shared objects.
889 if (gsym != NULL && gsym->is_defined_in_dynobj())
891 if (gsym->has_plt_offset())
892 address = target->plt_section()->address() + gsym->plt_offset();
899 case elfcpp::R_386_NONE:
900 case elfcpp::R_386_GNU_VTINHERIT:
901 case elfcpp::R_386_GNU_VTENTRY:
904 case elfcpp::R_386_32:
905 Relocate_functions<32, false>::rel32(view, value);
908 case elfcpp::R_386_PC32:
909 Relocate_functions<32, false>::pcrel32(view, value, address);
912 case elfcpp::R_386_16:
913 Relocate_functions<32, false>::rel16(view, value);
916 case elfcpp::R_386_PC16:
917 Relocate_functions<32, false>::pcrel16(view, value, address);
920 case elfcpp::R_386_8:
921 Relocate_functions<32, false>::rel8(view, value);
924 case elfcpp::R_386_PC8:
925 Relocate_functions<32, false>::pcrel8(view, value, address);
928 case elfcpp::R_386_PLT32:
929 gold_assert(gsym->has_plt_offset()
930 || gsym->final_value_is_known(relinfo->options));
931 Relocate_functions<32, false>::pcrel32(view, value, address);
934 case elfcpp::R_386_GOT32:
935 // Local GOT offsets not yet supported.
937 gold_assert(gsym->has_got_offset());
938 value = gsym->got_offset();
939 Relocate_functions<32, false>::rel32(view, value);
942 case elfcpp::R_386_GOTOFF:
943 value -= target->got_section(NULL, NULL, NULL)->address();
944 Relocate_functions<32, false>::rel32(view, value);
947 case elfcpp::R_386_GOTPC:
948 value = target->got_section(NULL, NULL, NULL)->address();
949 Relocate_functions<32, false>::pcrel32(view, value, address);
952 case elfcpp::R_386_COPY:
953 case elfcpp::R_386_GLOB_DAT:
954 case elfcpp::R_386_JUMP_SLOT:
955 case elfcpp::R_386_RELATIVE:
956 case elfcpp::R_386_TLS_TPOFF:
957 case elfcpp::R_386_TLS_DTPMOD32:
958 case elfcpp::R_386_TLS_DTPOFF32:
959 case elfcpp::R_386_TLS_TPOFF32:
960 case elfcpp::R_386_TLS_DESC:
961 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
963 relinfo->location(relnum, rel.get_r_offset()).c_str(),
968 case elfcpp::R_386_TLS_IE:
969 case elfcpp::R_386_TLS_GOTIE:
970 case elfcpp::R_386_TLS_LE:
971 case elfcpp::R_386_TLS_GD:
972 case elfcpp::R_386_TLS_LDM:
973 case elfcpp::R_386_TLS_LDO_32:
974 case elfcpp::R_386_TLS_IE_32:
975 case elfcpp::R_386_TLS_LE_32:
976 case elfcpp::R_386_TLS_GOTDESC:
977 case elfcpp::R_386_TLS_DESC_CALL:
978 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, value, view,
982 case elfcpp::R_386_32PLT:
983 case elfcpp::R_386_TLS_GD_32:
984 case elfcpp::R_386_TLS_GD_PUSH:
985 case elfcpp::R_386_TLS_GD_CALL:
986 case elfcpp::R_386_TLS_GD_POP:
987 case elfcpp::R_386_TLS_LDM_32:
988 case elfcpp::R_386_TLS_LDM_PUSH:
989 case elfcpp::R_386_TLS_LDM_CALL:
990 case elfcpp::R_386_TLS_LDM_POP:
991 case elfcpp::R_386_USED_BY_INTEL_200:
993 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
995 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1004 // Perform a TLS relocation.
1007 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1009 const elfcpp::Rel<32, false>& rel,
1010 unsigned int r_type,
1011 const Sized_symbol<32>* gsym,
1012 elfcpp::Elf_types<32>::Elf_Addr value,
1013 unsigned char* view,
1014 elfcpp::Elf_types<32>::Elf_Addr,
1017 Output_segment* tls_segment = relinfo->layout->tls_segment();
1018 if (tls_segment == NULL)
1020 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1022 relinfo->location(relnum, rel.get_r_offset()).c_str());
1026 const bool is_final = (gsym == NULL
1027 ? !relinfo->options->is_shared()
1028 : gsym->final_value_is_known(relinfo->options));
1029 const unsigned int opt_r_type =
1030 Target_i386::optimize_tls_reloc(relinfo->options, is_final, r_type);
1033 case elfcpp::R_386_TLS_LE_32:
1034 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1035 Relocate_functions<32, false>::rel32(view, value);
1038 case elfcpp::R_386_TLS_LE:
1039 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1040 Relocate_functions<32, false>::rel32(view, value);
1043 case elfcpp::R_386_TLS_IE:
1044 case elfcpp::R_386_TLS_GOTIE:
1045 case elfcpp::R_386_TLS_IE_32:
1046 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1048 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1049 rel, r_type, value, view,
1053 fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
1055 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1057 // gold_exit(false);
1060 case elfcpp::R_386_TLS_GD:
1061 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1063 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1064 rel, r_type, value, view,
1068 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1070 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1072 // gold_exit(false);
1075 case elfcpp::R_386_TLS_LDM:
1076 case elfcpp::R_386_TLS_LDO_32:
1077 case elfcpp::R_386_TLS_GOTDESC:
1078 case elfcpp::R_386_TLS_DESC_CALL:
1079 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1081 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1083 // gold_exit(false);
1088 // Do a relocation in which we convert a TLS Initial-Exec to a
1092 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1094 Output_segment* tls_segment,
1095 const elfcpp::Rel<32, false>& rel,
1096 unsigned int r_type,
1097 elfcpp::Elf_types<32>::Elf_Addr value,
1098 unsigned char* view,
1101 // We have to actually change the instructions, which means that we
1102 // need to examine the opcodes to figure out which instruction we
1104 if (r_type == elfcpp::R_386_TLS_IE)
1106 // movl %gs:XX,%eax ==> movl $YY,%eax
1107 // movl %gs:XX,%reg ==> movl $YY,%reg
1108 // addl %gs:XX,%reg ==> addl $YY,%reg
1109 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1110 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1112 unsigned char op1 = view[-1];
1115 // movl XX,%eax ==> movl $YY,%eax
1120 Target_i386::Relocate::check_range(relinfo, relnum, rel,
1123 unsigned char op2 = view[-2];
1126 // movl XX,%reg ==> movl $YY,%reg
1127 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1128 (op1 & 0xc7) == 0x05);
1130 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1132 else if (op2 == 0x03)
1134 // addl XX,%reg ==> addl $YY,%reg
1135 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1136 (op1 & 0xc7) == 0x05);
1138 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1141 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1146 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1147 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1148 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1149 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1150 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1152 unsigned char op1 = view[-1];
1153 unsigned char op2 = view[-2];
1154 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1155 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1158 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1160 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1162 else if (op2 == 0x2b)
1164 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1166 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1168 else if (op2 == 0x03)
1170 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1172 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1175 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1178 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1179 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1182 Relocate_functions<32, false>::rel32(view, value);
1185 // Do a relocation in which we convert a TLS Global-Dynamic to a
1189 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1191 Output_segment* tls_segment,
1192 const elfcpp::Rel<32, false>& rel,
1194 elfcpp::Elf_types<32>::Elf_Addr value,
1195 unsigned char* view,
1198 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1199 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1200 // leal foo(%reg),%eax; call ___tls_get_addr
1201 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1203 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1204 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1206 unsigned char op1 = view[-1];
1207 unsigned char op2 = view[-2];
1209 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1210 op2 == 0x8d || op2 == 0x04);
1211 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1218 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1219 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1221 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1222 ((op1 & 0xc7) == 0x05
1223 && op1 != (4 << 3)));
1224 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1228 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1229 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1230 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1232 // There is a trailing nop. Use the size byte subl.
1233 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1238 // Use the five byte subl.
1239 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1243 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1244 Relocate_functions<32, false>::rel32(view + roff, value);
1246 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1248 this->skip_call_tls_get_addr_ = true;
1251 // Check the range for a TLS relocation.
1254 Target_i386::Relocate::check_range(const Relocate_info<32, false>* relinfo,
1256 const elfcpp::Rel<32, false>& rel,
1257 off_t view_size, off_t off)
1259 off_t offset = rel.get_r_offset() + off;
1260 if (offset < 0 || offset > view_size)
1262 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1264 relinfo->location(relnum, rel.get_r_offset()).c_str());
1269 // Check the validity of a TLS relocation. This is like assert.
1272 Target_i386::Relocate::check_tls(const Relocate_info<32, false>* relinfo,
1274 const elfcpp::Rel<32, false>& rel,
1280 _("%s: %s: TLS relocation against invalid instruction\n"),
1282 relinfo->location(relnum, rel.get_r_offset()).c_str());
1287 // Relocate section data.
1290 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
1291 unsigned int sh_type,
1292 const unsigned char* prelocs,
1294 unsigned char* view,
1295 elfcpp::Elf_types<32>::Elf_Addr address,
1298 gold_assert(sh_type == elfcpp::SHT_REL);
1300 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1301 Target_i386::Relocate>(
1311 // The selector for i386 object files.
1313 class Target_selector_i386 : public Target_selector
1316 Target_selector_i386()
1317 : Target_selector(elfcpp::EM_386, 32, false)
1321 recognize(int machine, int osabi, int abiversion);
1324 Target_i386* target_;
1327 // Recognize an i386 object file when we already know that the machine
1328 // number is EM_386.
1331 Target_selector_i386::recognize(int, int, int)
1333 if (this->target_ == NULL)
1334 this->target_ = new Target_i386();
1335 return this->target_;
1338 Target_selector_i386 target_selector_i386;
1340 } // End anonymous namespace.