+static bfd *
+make_singleton_name_thunk (import, parent)
+ const char *import;
+ bfd *parent;
+{
+ /* Name thunks go to idata$4. */
+ asection *id4;
+ unsigned char *d4;
+ char *oname;
+ bfd *abfd;
+
+ oname = (char *) xmalloc (20);
+ sprintf (oname, "nmth%06d.o", tmp_seq);
+ tmp_seq++;
+
+ abfd = bfd_create (oname, parent);
+ bfd_find_target (pe_details->object_target, abfd);
+ bfd_make_writable (abfd);
+
+ bfd_set_format (abfd, bfd_object);
+ bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
+
+ symptr = 0;
+ symtab = (asymbol **) xmalloc (3 * sizeof (asymbol *));
+ id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
+ quick_symbol (abfd, U ("_nm_thnk_"), import, "", id4, BSF_GLOBAL, 0);
+ quick_symbol (abfd, U ("_nm_"), import, "", UNDSEC, BSF_GLOBAL, 0);
+
+ bfd_set_section_size (abfd, id4, 8);
+ d4 = (unsigned char *) xmalloc (4);
+ id4->contents = d4;
+ memset (d4, 0, 8);
+ quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
+ save_relocs (id4);
+
+ bfd_set_symtab (abfd, symtab, symptr);
+
+ bfd_set_section_contents (abfd, id4, d4, 0, 8);
+
+ bfd_make_readable (abfd);
+ return abfd;
+}
+
+static char *
+make_import_fixup_mark (rel)
+ arelent *rel;
+{
+ /* We convert reloc to symbol, for later reference. */
+ static int counter;
+ static char *fixup_name = NULL;
+ static size_t buffer_len = 0;
+
+ struct symbol_cache_entry *sym = *rel->sym_ptr_ptr;
+
+ bfd *abfd = bfd_asymbol_bfd (sym);
+ struct bfd_link_hash_entry *bh;
+
+ if (!fixup_name)
+ {
+ fixup_name = (char *) xmalloc (384);
+ buffer_len = 384;
+ }
+
+ if (strlen (sym->name) + 25 > buffer_len)
+ /* Assume 25 chars for "__fu" + counter + "_". If counter is
+ bigger than 20 digits long, we've got worse problems than
+ overflowing this buffer... */
+ {
+ free (fixup_name);
+ /* New buffer size is length of symbol, plus 25, but then
+ rounded up to the nearest multiple of 128. */
+ buffer_len = ((strlen (sym->name) + 25) + 127) & ~127;
+ fixup_name = (char *) xmalloc (buffer_len);
+ }
+
+ sprintf (fixup_name, "__fu%d_%s", counter++, sym->name);
+
+ bh = NULL;
+ bfd_coff_link_add_one_symbol (&link_info, abfd, fixup_name, BSF_GLOBAL,
+ current_sec, /* sym->section, */
+ rel->address, NULL, TRUE, FALSE, &bh);
+
+ if (0)
+ {
+ struct coff_link_hash_entry *myh;
+
+ myh = (struct coff_link_hash_entry *) bh;
+ printf ("type:%d\n", myh->type);
+ printf ("%s\n", myh->root.u.def.section->name);
+ }
+
+ return fixup_name;
+}
+
+/* .section .idata$3
+ .rva __nm_thnk_SYM (singleton thunk with name of func)
+ .long 0
+ .long 0
+ .rva __my_dll_iname (name of dll)
+ .rva __fuNN_SYM (pointer to reference (address) in text) */
+
+static bfd *
+make_import_fixup_entry (name, fixup_name, dll_symname, parent)
+ const char *name;
+ const char *fixup_name;
+ const char *dll_symname;
+ bfd *parent;
+{
+ asection *id3;
+ unsigned char *d3;
+ char *oname;
+ bfd *abfd;
+
+ oname = (char *) xmalloc (20);
+ sprintf (oname, "fu%06d.o", tmp_seq);
+ tmp_seq++;
+
+ abfd = bfd_create (oname, parent);
+ bfd_find_target (pe_details->object_target, abfd);
+ bfd_make_writable (abfd);
+
+ bfd_set_format (abfd, bfd_object);
+ bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
+
+ symptr = 0;
+ symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *));
+ id3 = quick_section (abfd, ".idata$3", SEC_HAS_CONTENTS, 2);
+
+#if 0
+ quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
+#endif
+ quick_symbol (abfd, U ("_nm_thnk_"), name, "", UNDSEC, BSF_GLOBAL, 0);
+ quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
+ quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
+
+ bfd_set_section_size (abfd, id3, 20);
+ d3 = (unsigned char *) xmalloc (20);
+ id3->contents = d3;
+ memset (d3, 0, 20);
+
+ quick_reloc (abfd, 0, BFD_RELOC_RVA, 1);
+ quick_reloc (abfd, 12, BFD_RELOC_RVA, 2);
+ quick_reloc (abfd, 16, BFD_RELOC_RVA, 3);
+ save_relocs (id3);
+
+ bfd_set_symtab (abfd, symtab, symptr);
+
+ bfd_set_section_contents (abfd, id3, d3, 0, 20);
+
+ bfd_make_readable (abfd);
+ return abfd;
+}
+
+/* .section .rdata_runtime_pseudo_reloc
+ .long addend
+ .rva __fuNN_SYM (pointer to reference (address) in text) */
+
+static bfd *
+make_runtime_pseudo_reloc (name, fixup_name, addend, parent)
+ const char *name ATTRIBUTE_UNUSED;
+ const char *fixup_name;
+ int addend;
+ bfd *parent;
+{
+ asection *rt_rel;
+ unsigned char *rt_rel_d;
+ char *oname;
+ bfd *abfd;
+
+ oname = (char *) xmalloc (20);
+ sprintf (oname, "rtr%06d.o", tmp_seq);
+ tmp_seq++;
+
+ abfd = bfd_create (oname, parent);
+ bfd_find_target (pe_details->object_target, abfd);
+ bfd_make_writable (abfd);
+
+ bfd_set_format (abfd, bfd_object);
+ bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
+
+ symptr = 0;
+ symtab = (asymbol **) xmalloc (2 * sizeof (asymbol *));
+ rt_rel = quick_section (abfd, ".rdata_runtime_pseudo_reloc", SEC_HAS_CONTENTS, 2);
+
+ quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
+
+ bfd_set_section_size (abfd, rt_rel, 8);
+ rt_rel_d = (unsigned char *) xmalloc (8);
+ rt_rel->contents = rt_rel_d;
+ memset (rt_rel_d, 0, 8);
+ bfd_put_32 (abfd, addend, rt_rel_d);
+
+ quick_reloc (abfd, 4, BFD_RELOC_RVA, 1);
+ save_relocs (rt_rel);
+
+ bfd_set_symtab (abfd, symtab, symptr);
+
+ bfd_set_section_contents (abfd, rt_rel, rt_rel_d, 0, 8);
+
+ bfd_make_readable (abfd);
+ return abfd;
+}
+
+/* .section .rdata
+ .rva __pei386_runtime_relocator */
+
+static bfd *
+pe_create_runtime_relocator_reference (parent)
+ bfd *parent;
+{
+ asection *extern_rt_rel;
+ unsigned char *extern_rt_rel_d;
+ char *oname;
+ bfd *abfd;
+
+ oname = (char *) xmalloc (20);
+ sprintf (oname, "ertr%06d.o", tmp_seq);
+ tmp_seq++;
+
+ abfd = bfd_create (oname, parent);
+ bfd_find_target (pe_details->object_target, abfd);
+ bfd_make_writable (abfd);
+
+ bfd_set_format (abfd, bfd_object);
+ bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
+
+ symptr = 0;
+ symtab = (asymbol **) xmalloc (2 * sizeof (asymbol *));
+ extern_rt_rel = quick_section (abfd, ".rdata", SEC_HAS_CONTENTS, 2);
+
+ quick_symbol (abfd, "", "__pei386_runtime_relocator", "", UNDSEC, BSF_NO_FLAGS, 0);
+
+ bfd_set_section_size (abfd, extern_rt_rel, 4);
+ extern_rt_rel_d = (unsigned char *) xmalloc (4);
+ extern_rt_rel->contents = extern_rt_rel_d;
+
+ quick_reloc (abfd, 0, BFD_RELOC_RVA, 1);
+ save_relocs (extern_rt_rel);
+
+ bfd_set_symtab (abfd, symtab, symptr);
+
+ bfd_set_section_contents (abfd, extern_rt_rel, extern_rt_rel_d, 0, 4);
+
+ bfd_make_readable (abfd);
+ return abfd;
+}
+
+void
+pe_create_import_fixup (rel, s, addend)
+ arelent *rel;
+ asection *s;
+ int addend;
+{
+ char buf[300];
+ struct symbol_cache_entry *sym = *rel->sym_ptr_ptr;
+ struct bfd_link_hash_entry *name_thunk_sym;
+ const char *name = sym->name;
+ char *fixup_name = make_import_fixup_mark (rel);
+ bfd *b;
+
+ sprintf (buf, U ("_nm_thnk_%s"), name);
+
+ name_thunk_sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
+
+ if (!name_thunk_sym || name_thunk_sym->type != bfd_link_hash_defined)
+ {
+ bfd *b = make_singleton_name_thunk (name, output_bfd);
+ add_bfd_to_link (b, b->filename, &link_info);
+
+ /* If we ever use autoimport, we have to cast text section writable. */
+ config.text_read_only = FALSE;
+ }
+
+ if (addend == 0 || link_info.pei386_runtime_pseudo_reloc)
+ {
+ extern char * pe_data_import_dll;
+ char * dll_symname = pe_data_import_dll ? pe_data_import_dll : "unknown";
+
+ b = make_import_fixup_entry (name, fixup_name, dll_symname, output_bfd);
+ add_bfd_to_link (b, b->filename, &link_info);
+ }
+
+ if (addend != 0)
+ {
+ if (link_info.pei386_runtime_pseudo_reloc)
+ {
+ if (pe_dll_extra_pe_debug)
+ printf ("creating runtime pseudo-reloc entry for %s (addend=%d)\n",
+ fixup_name, addend);
+ b = make_runtime_pseudo_reloc (name, fixup_name, addend, output_bfd);
+ add_bfd_to_link (b, b->filename, &link_info);
+
+ if (runtime_pseudo_relocs_created == 0)
+ {
+ b = pe_create_runtime_relocator_reference (output_bfd);
+ add_bfd_to_link (b, b->filename, &link_info);
+ }
+ runtime_pseudo_relocs_created++;
+ }
+ else
+ {
+ einfo (_("%C: variable '%T' can't be auto-imported. Please read the documentation for ld's --enable-auto-import for details.\n"),
+ s->owner, s, rel->address, sym->name);
+ einfo ("%X");
+ }
+ }
+}
+
+