+// Get the dynamic reloc section, creating it if necessary.
+
+Target_i386::Reloc_section*
+Target_i386::rel_dyn_section(Layout* layout)
+{
+ if (this->rel_dyn_ == NULL)
+ {
+ gold_assert(layout != NULL);
+ this->rel_dyn_ = new Reloc_section();
+ layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
+ elfcpp::SHF_ALLOC, this->rel_dyn_);
+ }
+ return this->rel_dyn_;
+}
+
+// A class to handle the PLT data.
+
+class Output_data_plt_i386 : public Output_section_data
+{
+ public:
+ typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
+
+ Output_data_plt_i386(Layout*, Output_data_space*);
+
+ // Add an entry to the PLT.
+ void
+ add_entry(Symbol* gsym);
+
+ // Return the .rel.plt section data.
+ const Reloc_section*
+ rel_plt() const
+ { return this->rel_; }
+
+ protected:
+ void
+ do_adjust_output_section(Output_section* os);
+
+ private:
+ // The size of an entry in the PLT.
+ static const int plt_entry_size = 16;
+
+ // The first entry in the PLT for an executable.
+ static unsigned char exec_first_plt_entry[plt_entry_size];
+
+ // The first entry in the PLT for a shared object.
+ static unsigned char dyn_first_plt_entry[plt_entry_size];
+
+ // Other entries in the PLT for an executable.
+ static unsigned char exec_plt_entry[plt_entry_size];
+
+ // Other entries in the PLT for a shared object.
+ static unsigned char dyn_plt_entry[plt_entry_size];
+
+ // Set the final size.
+ void
+ do_set_address(uint64_t, off_t)
+ { this->set_data_size((this->count_ + 1) * plt_entry_size); }
+
+ // Write out the PLT data.
+ void
+ do_write(Output_file*);
+
+ // The reloc section.
+ Reloc_section* rel_;
+ // The .got.plt section.
+ Output_data_space* got_plt_;
+ // The number of PLT entries.
+ unsigned int count_;
+};
+
+// Create the PLT section. The ordinary .got section is an argument,
+// since we need to refer to the start. We also create our own .got
+// section just for PLT entries.
+
+Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
+ Output_data_space* got_plt)
+ : Output_section_data(4), got_plt_(got_plt), count_(0)
+{
+ this->rel_ = new Reloc_section();
+ layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
+ elfcpp::SHF_ALLOC, this->rel_);
+}
+
+void
+Output_data_plt_i386::do_adjust_output_section(Output_section* os)
+{
+ // UnixWare sets the entsize of .plt to 4, and so does the old GNU
+ // linker, and so do we.
+ os->set_entsize(4);
+}
+
+// Add an entry to the PLT.
+
+void
+Output_data_plt_i386::add_entry(Symbol* gsym)
+{
+ gold_assert(!gsym->has_plt_offset());
+
+ // Note that when setting the PLT offset we skip the initial
+ // reserved PLT entry.
+ gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
+
+ ++this->count_;
+
+ off_t got_offset = this->got_plt_->data_size();
+
+ // Every PLT entry needs a GOT entry which points back to the PLT
+ // entry (this will be changed by the dynamic linker, normally
+ // lazily when the function is called).
+ this->got_plt_->set_space_size(got_offset + 4);
+
+ // Every PLT entry needs a reloc.
+ gsym->set_needs_dynsym_entry();
+ this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
+ got_offset);
+
+ // Note that we don't need to save the symbol. The contents of the
+ // PLT are independent of which symbols are used. The symbols only
+ // appear in the relocations.
+}
+
+// The first entry in the PLT for an executable.
+
+unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
+{
+ 0xff, 0x35, // pushl contents of memory address
+ 0, 0, 0, 0, // replaced with address of .got + 4
+ 0xff, 0x25, // jmp indirect
+ 0, 0, 0, 0, // replaced with address of .got + 8
+ 0, 0, 0, 0 // unused
+};
+
+// The first entry in the PLT for a shared object.
+
+unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
+{
+ 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
+ 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
+ 0, 0, 0, 0 // unused
+};
+
+// Subsequent entries in the PLT for an executable.
+
+unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
+{
+ 0xff, 0x25, // jmp indirect
+ 0, 0, 0, 0, // replaced with address of symbol in .got
+ 0x68, // pushl immediate
+ 0, 0, 0, 0, // replaced with offset into relocation table
+ 0xe9, // jmp relative
+ 0, 0, 0, 0 // replaced with offset to start of .plt
+};
+
+// Subsequent entries in the PLT for a shared object.
+
+unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
+{
+ 0xff, 0xa3, // jmp *offset(%ebx)
+ 0, 0, 0, 0, // replaced with offset of symbol in .got
+ 0x68, // pushl immediate
+ 0, 0, 0, 0, // replaced with offset into relocation table
+ 0xe9, // jmp relative
+ 0, 0, 0, 0 // replaced with offset to start of .plt
+};
+
+// Write out the PLT. This uses the hand-coded instructions above,
+// and adjusts them as needed. This is all specified by the i386 ELF
+// Processor Supplement.
+
+void
+Output_data_plt_i386::do_write(Output_file* of)
+{
+ const off_t offset = this->offset();
+ const off_t oview_size = this->data_size();
+ unsigned char* const oview = of->get_output_view(offset, oview_size);
+
+ const off_t got_file_offset = this->got_plt_->offset();
+ const off_t got_size = this->got_plt_->data_size();
+ unsigned char* const got_view = of->get_output_view(got_file_offset,
+ got_size);
+
+ unsigned char* pov = oview;
+
+ elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
+ elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
+
+ if (parameters->output_is_shared())
+ memcpy(pov, dyn_first_plt_entry, plt_entry_size);
+ else
+ {
+ memcpy(pov, exec_first_plt_entry, plt_entry_size);
+ elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
+ elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
+ }
+ pov += plt_entry_size;
+
+ unsigned char* got_pov = got_view;
+
+ memset(got_pov, 0, 12);
+ got_pov += 12;
+
+ const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
+
+ unsigned int plt_offset = plt_entry_size;
+ unsigned int plt_rel_offset = 0;
+ unsigned int got_offset = 12;
+ const unsigned int count = this->count_;
+ for (unsigned int i = 0;
+ i < count;
+ ++i,
+ pov += plt_entry_size,
+ got_pov += 4,
+ plt_offset += plt_entry_size,
+ plt_rel_offset += rel_size,
+ got_offset += 4)
+ {
+ // Set and adjust the PLT entry itself.
+
+ if (parameters->output_is_shared())
+ {
+ memcpy(pov, dyn_plt_entry, plt_entry_size);
+ elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
+ }
+ else
+ {
+ memcpy(pov, exec_plt_entry, plt_entry_size);
+ elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
+ (got_address
+ + got_offset));
+ }
+
+ elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
+ elfcpp::Swap<32, false>::writeval(pov + 12,
+ - (plt_offset + plt_entry_size));
+
+ // Set the entry in the GOT.
+ elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
+ }
+
+ gold_assert(pov - oview == oview_size);
+ gold_assert(got_pov - got_view == got_size);
+
+ of->write_output_view(offset, oview_size, oview);
+ of->write_output_view(got_file_offset, got_size, got_view);
+}
+
+// Create a PLT entry for a global symbol.
+
+void
+Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
+{
+ if (gsym->has_plt_offset())
+ return;
+
+ if (this->plt_ == NULL)
+ {
+ // Create the GOT sections first.
+ this->got_section(symtab, layout);
+
+ this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
+ layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
+ (elfcpp::SHF_ALLOC
+ | elfcpp::SHF_EXECINSTR),
+ this->plt_);
+ }
+
+ this->plt_->add_entry(gsym);
+}
+
+// Handle a relocation against a non-function symbol defined in a
+// dynamic object. The traditional way to handle this is to generate
+// a COPY relocation to copy the variable at runtime from the shared
+// object into the executable's data segment. However, this is
+// undesirable in general, as if the size of the object changes in the
+// dynamic object, the executable will no longer work correctly. If
+// this relocation is in a writable section, then we can create a
+// dynamic reloc and the dynamic linker will resolve it to the correct
+// address at runtime. However, we do not want do that if the
+// relocation is in a read-only section, as it would prevent the
+// readonly segment from being shared. And if we have to eventually
+// generate a COPY reloc, then any dynamic relocations will be
+// useless. So this means that if this is a writable section, we need
+// to save the relocation until we see whether we have to create a
+// COPY relocation for this symbol for any other relocation.
+
+void
+Target_i386::copy_reloc(const General_options* options,
+ Symbol_table* symtab,
+ Layout* layout,
+ Sized_relobj<32, false>* object,
+ unsigned int data_shndx, Symbol* gsym,
+ const elfcpp::Rel<32, false>& rel)
+{
+ Sized_symbol<32>* ssym;
+ ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
+ SELECT_SIZE(32));
+
+ if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
+ data_shndx, ssym))
+ {
+ // So far we do not need a COPY reloc. Save this relocation.
+ // If it turns out that we never need a COPY reloc for this
+ // symbol, then we will emit the relocation.
+ if (this->copy_relocs_ == NULL)
+ this->copy_relocs_ = new Copy_relocs<32, false>();
+ this->copy_relocs_->save(ssym, object, data_shndx, rel);
+ }
+ else
+ {
+ // Allocate space for this symbol in the .bss section.
+
+ elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
+
+ // There is no defined way to determine the required alignment
+ // of the symbol. We pick the alignment based on the size. We
+ // set an arbitrary maximum of 256.
+ unsigned int align;
+ for (align = 1; align < 512; align <<= 1)
+ if ((symsize & align) != 0)
+ break;
+
+ if (this->dynbss_ == NULL)
+ {
+ this->dynbss_ = new Output_data_space(align);
+ layout->add_output_section_data(".bss",
+ elfcpp::SHT_NOBITS,
+ (elfcpp::SHF_ALLOC
+ | elfcpp::SHF_WRITE),
+ this->dynbss_);
+ }
+
+ Output_data_space* dynbss = this->dynbss_;
+
+ if (align > dynbss->addralign())
+ dynbss->set_space_alignment(align);
+
+ off_t dynbss_size = dynbss->data_size();
+ dynbss_size = align_address(dynbss_size, align);
+ off_t offset = dynbss_size;
+ dynbss->set_space_size(dynbss_size + symsize);
+
+ symtab->define_with_copy_reloc(this, ssym, dynbss, offset);
+
+ // Add the COPY reloc.
+ Reloc_section* rel_dyn = this->rel_dyn_section(layout);
+ rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
+ }
+}
+