+ bfd_set_section_size (id5, PE_IDATA5_SIZE);
+ d5 = xmalloc (PE_IDATA5_SIZE);
+ id5->contents = d5;
+ memset (d5, 0, PE_IDATA5_SIZE);
+
+ if (exp->flag_noname)
+ {
+ d5[0] = exp->ordinal;
+ d5[1] = exp->ordinal >> 8;
+ d5[PE_IDATA5_SIZE - 1] = 0x80;
+ }
+ else
+ {
+ quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
+ save_relocs (id5);
+ }
+
+ bfd_set_section_size (id4, PE_IDATA4_SIZE);
+ d4 = xmalloc (PE_IDATA4_SIZE);
+ id4->contents = d4;
+ memset (d4, 0, PE_IDATA4_SIZE);
+
+ if (exp->flag_noname)
+ {
+ d4[0] = exp->ordinal;
+ d4[1] = exp->ordinal >> 8;
+ d4[PE_IDATA4_SIZE - 1] = 0x80;
+ }
+ else
+ {
+ quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
+ save_relocs (id4);
+ }
+
+ if (exp->flag_noname)
+ {
+ len = 0;
+ bfd_set_section_size (id6, 0);
+ }
+ else
+ {
+ int ord;
+
+ /* { short, asciz } */
+ if (exp->its_name)
+ len = 2 + strlen (exp->its_name) + 1;
+ else
+ len = 2 + strlen (exp->name) + 1;
+ if (len & 1)
+ len++;
+ bfd_set_section_size (id6, len);
+ d6 = xmalloc (len);
+ id6->contents = d6;
+ memset (d6, 0, len);
+
+ /* PR 20880: Use exp->hint as a backup, just in case exp->ordinal
+ contains an invalid value (-1). */
+ ord = (exp->ordinal >= 0) ? exp->ordinal : exp->hint;
+ d6[0] = ord;
+ d6[1] = ord >> 8;
+
+ if (exp->its_name)
+ strcpy ((char*) d6 + 2, exp->its_name);
+ else
+ strcpy ((char *) d6 + 2, exp->name);
+ }
+
+ bfd_set_symtab (abfd, symtab, symptr);
+
+ if (include_jmp_stub)
+ bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
+ bfd_set_section_contents (abfd, id7, d7, 0, 4);
+ bfd_set_section_contents (abfd, id5, d5, 0, PE_IDATA5_SIZE);
+ bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE);
+ if (!exp->flag_noname)
+ bfd_set_section_contents (abfd, id6, d6, 0, len);
+
+ bfd_make_readable (abfd);
+ return abfd;
+}
+
+static bfd *
+make_singleton_name_thunk (const char *import, bfd *parent)
+{
+ /* Name thunks go to idata$4. */
+ asection *id4;
+ unsigned char *d4;
+ char *oname;
+ bfd *abfd;
+
+ oname = 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 = xmalloc (3 * sizeof (asymbol *));
+ id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
+ quick_symbol (abfd, "__nm_thnk_", import, "", id4, BSF_GLOBAL, 0);
+ quick_symbol (abfd, "__nm_", import, "", UNDSEC, BSF_GLOBAL, 0);
+
+ /* We need space for the real thunk and for the null terminator. */
+ bfd_set_section_size (id4, PE_IDATA4_SIZE * 2);
+ d4 = xmalloc (PE_IDATA4_SIZE * 2);
+ id4->contents = d4;
+ memset (d4, 0, PE_IDATA4_SIZE * 2);
+ 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, PE_IDATA4_SIZE * 2);
+
+ bfd_make_readable (abfd);
+ return abfd;
+}
+
+static const char *
+make_import_fixup_mark (arelent *rel, char *name)
+{
+ /* We convert reloc to symbol, for later reference. */
+ static unsigned int counter;
+ struct bfd_symbol *sym = *rel->sym_ptr_ptr;
+ bfd *abfd = bfd_asymbol_bfd (sym);
+ struct bfd_link_hash_entry *bh;
+ char *fixup_name, buf[26];
+ size_t prefix_len;
+
+ /* "name" buffer has space before the symbol name for prefixes. */
+ sprintf (buf, "__fu%d_", counter++);
+ prefix_len = strlen (buf);
+ fixup_name = name - prefix_len;
+ memcpy (fixup_name, buf, prefix_len);
+
+ 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);
+
+ return bh->root.string;
+}
+
+/* .section .idata$2
+ .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 (const char *name,
+ const char *fixup_name,
+ const char *symname,
+ bfd *parent)
+{
+ asection *id2;
+ unsigned char *d2;
+ char *oname;
+ bfd *abfd;
+
+ oname = 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 = xmalloc (6 * sizeof (asymbol *));
+ id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
+
+ quick_symbol (abfd, "__nm_thnk_", name, "", UNDSEC, BSF_GLOBAL, 0);
+ quick_symbol (abfd, U (""), symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
+ quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
+
+ bfd_set_section_size (id2, 20);
+ d2 = xmalloc (20);
+ id2->contents = d2;
+ memset (d2, 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 (id2);
+
+ bfd_set_symtab (abfd, symtab, symptr);
+
+ bfd_set_section_contents (abfd, id2, d2, 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 (const char *name ATTRIBUTE_UNUSED,
+ const char *fixup_name,
+ bfd_vma addend ATTRIBUTE_UNUSED,
+ bfd_vma bitsize,
+ bfd *parent)
+{
+ asection *rt_rel;
+ unsigned char *rt_rel_d;
+ char *oname;
+ bfd *abfd;
+ bfd_size_type size;
+
+ oname = 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);
+
+ if (link_info.pei386_runtime_pseudo_reloc == 2)
+ {
+ if (runtime_pseudp_reloc_v2_init)
+ size = 3 * sizeof (asymbol *);
+ else
+ size = 6 * sizeof (asymbol *);
+ }
+ else
+ size = 2 * sizeof (asymbol *);
+
+ symptr = 0;
+ symtab = xmalloc (size);
+
+ rt_rel
+ = quick_section (abfd, ".rdata_runtime_pseudo_reloc", SEC_HAS_CONTENTS, 2);
+
+ quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
+
+ if (link_info.pei386_runtime_pseudo_reloc == 2)
+ {
+ size = 12;
+ if (!runtime_pseudp_reloc_v2_init)
+ {
+ size += 12;
+ runtime_pseudp_reloc_v2_init = TRUE;
+ }
+
+ quick_symbol (abfd, "__imp_", name, "", UNDSEC, BSF_GLOBAL, 0);
+
+ bfd_set_section_size (rt_rel, size);
+ rt_rel_d = xmalloc (size);
+ rt_rel->contents = rt_rel_d;
+ memset (rt_rel_d, 0, size);
+ quick_reloc (abfd, size - 8, BFD_RELOC_RVA, 1);
+ quick_reloc (abfd, size - 12, BFD_RELOC_RVA, 2);
+ bfd_put_32 (abfd, bitsize, rt_rel_d + (size - 4));
+ if (size != 12)
+ bfd_put_32 (abfd, 1, rt_rel_d + 8);
+ save_relocs (rt_rel);
+
+ bfd_set_symtab (abfd, symtab, symptr);
+
+ bfd_set_section_contents (abfd, rt_rel, rt_rel_d, 0, size);
+ }
+ else
+ {
+ bfd_set_section_size (rt_rel, 8);
+ rt_rel_d = 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 (bfd *parent)
+{
+ asection *extern_rt_rel;
+ unsigned char *extern_rt_rel_d;
+ char *oname;
+ bfd *abfd;
+
+ oname = 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 = xmalloc (2 * sizeof (asymbol *));
+ extern_rt_rel = quick_section (abfd, ".rdata", SEC_HAS_CONTENTS, 2);
+
+ quick_symbol (abfd, "", U ("_pei386_runtime_relocator"), "", UNDSEC,
+ BSF_NO_FLAGS, 0);
+
+ bfd_set_section_size (extern_rt_rel, PE_IDATA5_SIZE);
+ extern_rt_rel_d = xcalloc (1, PE_IDATA5_SIZE);
+ 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, PE_IDATA5_SIZE);
+
+ bfd_make_readable (abfd);
+ return abfd;
+}
+
+void
+pe_create_import_fixup (arelent *rel, asection *s, bfd_vma addend, char *name,
+ const char *symname)
+{
+ const char *fixup_name = make_import_fixup_mark (rel, name);
+ bfd *b;
+
+ /* This is the original implementation of the auto-import feature, which
+ primarily relied on the OS loader to patch things up with some help
+ from the pseudo-relocator to overcome the main limitation. See the
+ comment at the beginning of the file for an overview of the feature. */
+ if (link_info.pei386_runtime_pseudo_reloc != 2)
+ {
+ struct bfd_link_hash_entry *name_thunk_sym;
+ /* name buffer is allocated with space at beginning for prefixes. */
+ char *thname = name - (sizeof "__nm_thnk_" - 1);
+ memcpy (thname, "__nm_thnk_", sizeof "__nm_thnk_" - 1);
+ name_thunk_sym = bfd_link_hash_lookup (link_info.hash, thname, 0, 0, 1);
+
+ if (!(name_thunk_sym && name_thunk_sym->type == bfd_link_hash_defined))
+ {
+ b = make_singleton_name_thunk (name, link_info.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;
+ link_info.output_bfd->flags &= ~WP_TEXT;
+ }
+
+ if (addend == 0 || link_info.pei386_runtime_pseudo_reloc == 1)
+ {
+ b = make_import_fixup_entry (name, fixup_name, symname,
+ link_info.output_bfd);
+ add_bfd_to_link (b, b->filename, &link_info);
+ }
+ }
+
+ /* In the original implementation, the pseudo-relocator was only used when
+ the addend was not null. In the new implementation, the OS loader is
+ completely bypassed and the pseudo-relocator does the entire work. */
+ if ((addend != 0 && link_info.pei386_runtime_pseudo_reloc == 1)
+ || link_info.pei386_runtime_pseudo_reloc == 2)
+ {
+ if (pe_dll_extra_pe_debug)
+ printf ("creating runtime pseudo-reloc entry for %s (addend=%d)\n",
+ fixup_name, (int) addend);
+
+ b = make_runtime_pseudo_reloc (name, fixup_name, addend, rel->howto->bitsize,
+ link_info.output_bfd);
+ add_bfd_to_link (b, b->filename, &link_info);
+
+ if (runtime_pseudo_relocs_created++ == 0)
+ {
+ b = pe_create_runtime_relocator_reference (link_info.output_bfd);
+ add_bfd_to_link (b, b->filename, &link_info);
+ }
+ }
+
+ else if (addend != 0)
+ einfo (_("%X%P: %C: variable '%pT' can't be auto-imported; please read the documentation for ld's --enable-auto-import for details\n"),
+ s->owner, s, rel->address, (*rel->sym_ptr_ptr)->name);
+}
+
+void
+pe_dll_generate_implib (def_file *def, const char *impfilename, struct bfd_link_info *info)
+{
+ int i;
+ bfd *ar_head;
+ bfd *ar_tail;
+ bfd *outarch;
+ bfd *ibfd;
+ bfd *head = 0;
+
+ dll_filename = (def->name) ? def->name : dll_name;
+ dll_symname = xstrdup (dll_filename);
+ for (i = 0; dll_symname[i]; i++)
+ if (!ISALNUM (dll_symname[i]))
+ dll_symname[i] = '_';
+
+ unlink_if_ordinary (impfilename);
+
+ outarch = bfd_openw (impfilename, 0);
+
+ if (!outarch)
+ {
+ /* xgettext:c-format */
+ einfo (_("%X%P: can't open .lib file: %s\n"), impfilename);
+ return;
+ }
+
+ if (verbose)
+ /* xgettext:c-format */
+ info_msg (_("Creating library file: %s\n"), impfilename);
+
+ bfd_set_format (outarch, bfd_archive);
+ outarch->has_armap = 1;
+
+ /* Work out a reasonable size of things to put onto one line. */
+ ar_head = make_head (outarch);
+
+ /* Iterate the input BFDs, looking for exclude-modules-for-implib. */
+ for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
+ {
+ /* Iterate the exclude list. */
+ struct exclude_list_struct *ex;
+ char found;
+ for (ex = excludes, found = 0; ex && !found; ex = ex->next)
+ {
+ if (ex->type != EXCLUDEFORIMPLIB)
+ continue;
+ found = (filename_cmp (ex->string, ibfd->filename) == 0);
+ }
+ /* If it matched, we must open a fresh BFD for it (the original
+ input BFD is still needed for the DLL's final link) and add
+ it into the archive member chain. */
+ if (found)
+ {
+ bfd *newbfd = bfd_openr (ibfd->my_archive
+ ? ibfd->my_archive->filename : ibfd->filename, NULL);
+ if (!newbfd)
+ {
+ einfo (_("%X%P: bfd_openr %s: %E\n"), ibfd->filename);
+ return;
+ }
+ if (ibfd->my_archive)
+ {
+ /* Must now iterate through archive until we find the
+ required member. A minor shame that we'll open the
+ archive once per member that we require from it, and
+ leak those archive bfds rather than reuse them. */
+ bfd *arbfd = newbfd;
+ if (!bfd_check_format_matches (arbfd, bfd_archive, NULL))
+ {
+ einfo (_("%X%P: %s(%s): can't find member in non-archive file"),
+ ibfd->my_archive->filename, ibfd->filename);
+ return;
+ }
+ newbfd = NULL;
+ while ((newbfd = bfd_openr_next_archived_file (arbfd, newbfd)) != 0)
+ {
+ if (filename_cmp (newbfd->filename, ibfd->filename) == 0)
+ break;
+ }
+ if (!newbfd)
+ {
+ einfo (_("%X%P: %s(%s): can't find member in archive"),
+ ibfd->my_archive->filename, ibfd->filename);
+ return;
+ }
+ }
+ newbfd->archive_next = head;
+ head = newbfd;
+ }
+ }
+
+ for (i = 0; i < def->num_exports; i++)
+ {
+ /* The import library doesn't know about the internal name. */
+ char *internal = def->exports[i].internal_name;
+ bfd *n;
+
+ /* Don't add PRIVATE entries to import lib. */
+ if (pe_def_file->exports[i].flag_private)
+ continue;
+
+ def->exports[i].internal_name = def->exports[i].name;
+
+ /* PR 19803: If a symbol has been discard due to garbage
+ collection then do not create any exports for it. */
+ {
+ struct coff_link_hash_entry *h;
+
+ h = coff_link_hash_lookup (coff_hash_table (info), internal,
+ FALSE, FALSE, FALSE);
+ if (h != NULL
+ /* If the symbol is hidden and undefined then it
+ has been swept up by garbage collection. */
+ && h->symbol_class == C_HIDDEN
+ && h->root.u.def.section == bfd_und_section_ptr)
+ continue;
+
+ /* If necessary, check with an underscore prefix as well. */
+ if (pe_details->underscored && internal[0] != '@')
+ {
+ char *name;
+
+ name = xmalloc (strlen (internal) + 2);
+ sprintf (name, "_%s", internal);
+
+ h = coff_link_hash_lookup (coff_hash_table (info), name,
+ FALSE, FALSE, FALSE);
+ free (name);
+
+ if (h != NULL
+ /* If the symbol is hidden and undefined then it
+ has been swept up by garbage collection. */
+ && h->symbol_class == C_HIDDEN
+ && h->root.u.def.section == bfd_und_section_ptr)
+ continue;
+ }
+ }
+
+ n = make_one (def->exports + i, outarch,
+ ! (def->exports + i)->flag_data);
+ n->archive_next = head;
+ head = n;
+ def->exports[i].internal_name = internal;
+ }
+
+ ar_tail = make_tail (outarch);
+
+ if (ar_head == NULL || ar_tail == NULL)
+ return;
+
+ /* Now stick them all into the archive. */
+ ar_head->archive_next = head;
+ ar_tail->archive_next = ar_head;
+ head = ar_tail;
+
+ if (! bfd_set_archive_head (outarch, head))
+ einfo ("%X%P: bfd_set_archive_head: %E\n");
+
+ if (! bfd_close (outarch))
+ einfo ("%X%P: bfd_close %s: %E\n", impfilename);
+
+ while (head != NULL)
+ {
+ bfd *n = head->archive_next;
+ bfd_close (head);
+ head = n;
+ }
+}
+
+static int undef_count = 0;
+
+struct key_value
+{
+ char *key;
+ const char *oname;
+};
+
+static struct key_value *udef_table;
+
+static int undef_sort_cmp (const void *l1, const void *r1)
+{
+ const struct key_value *l = l1;
+ const struct key_value *r = r1;
+
+ return strcmp (l->key, r->key);
+}
+
+static struct bfd_link_hash_entry *
+pe_find_cdecl_alias_match (struct bfd_link_info *linfo, char *name)
+{
+ struct bfd_link_hash_entry *h = NULL;
+ struct key_value *kv;
+ struct key_value key;
+ char *at, *lname = xmalloc (strlen (name) + 3);
+
+ strcpy (lname, name);
+
+ at = strchr (lname + (lname[0] == '@'), '@');
+ if (at)
+ at[1] = 0;
+
+ key.key = lname;
+ kv = bsearch (&key, udef_table, undef_count, sizeof (struct key_value),
+ undef_sort_cmp);
+
+ if (kv)
+ {
+ h = bfd_link_hash_lookup (linfo->hash, kv->oname, FALSE, FALSE, FALSE);
+ if (h->type == bfd_link_hash_undefined)
+ goto return_h;
+ }
+
+ if (lname[0] == '?')
+ goto return_NULL;
+
+ if (at || lname[0] == '@')
+ {
+ if (lname[0] == '@')
+ {
+ if (pe_details->underscored)
+ lname[0] = '_';
+ else
+ strcpy (lname, lname + 1);
+ key.key = lname;
+ kv = bsearch (&key, udef_table, undef_count,
+ sizeof (struct key_value), undef_sort_cmp);
+ if (kv)
+ {
+ h = bfd_link_hash_lookup (linfo->hash, kv->oname, FALSE, FALSE, FALSE);
+ if (h->type == bfd_link_hash_undefined)
+ goto return_h;
+ }
+ }
+ if (at)
+ *strchr (lname, '@') = 0;
+ key.key = lname;
+ kv = bsearch (&key, udef_table, undef_count,
+ sizeof (struct key_value), undef_sort_cmp);
+ if (kv)
+ {
+ h = bfd_link_hash_lookup (linfo->hash, kv->oname, FALSE, FALSE, FALSE);
+ if (h->type == bfd_link_hash_undefined)
+ goto return_h;
+ }
+ goto return_NULL;
+ }
+
+ strcat (lname, "@");
+ key.key = lname;
+ kv = bsearch (&key, udef_table, undef_count,
+ sizeof (struct key_value), undef_sort_cmp);
+
+ if (kv)
+ {
+ h = bfd_link_hash_lookup (linfo->hash, kv->oname, FALSE, FALSE, FALSE);
+ if (h->type == bfd_link_hash_undefined)
+ goto return_h;
+ }
+
+ if (lname[0] == '_' && pe_details->underscored)
+ lname[0] = '@';
+ else
+ {
+ memmove (lname + 1, lname, strlen (lname) + 1);
+ lname[0] = '@';
+ }
+ key.key = lname;
+
+ kv = bsearch (&key, udef_table, undef_count,
+ sizeof (struct key_value), undef_sort_cmp);
+
+ if (kv)
+ {
+ h = bfd_link_hash_lookup (linfo->hash, kv->oname, FALSE, FALSE, FALSE);
+ if (h->type == bfd_link_hash_undefined)
+ goto return_h;
+ }
+
+ return_NULL:
+ h = NULL;
+ return_h:
+ free (lname);
+ return h;
+}
+
+static bfd_boolean
+pe_undef_count (struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
+ void *inf ATTRIBUTE_UNUSED)
+{
+ if (h->type == bfd_link_hash_undefined)
+ undef_count++;
+ return TRUE;
+}
+
+static bfd_boolean
+pe_undef_fill (struct bfd_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
+{
+ if (h->type == bfd_link_hash_undefined)
+ {
+ char *at;
+
+ udef_table[undef_count].key = xstrdup (h->root.string);
+ at = strchr (udef_table[undef_count].key
+ + (udef_table[undef_count].key[0] == '@'), '@');
+ if (at)
+ at[1] = 0;
+ udef_table[undef_count].oname = h->root.string;
+ undef_count++;
+ }
+ return TRUE;
+}
+
+static void
+pe_create_undef_table (void)
+{
+ undef_count = 0;
+
+ /* count undefined symbols */
+
+ bfd_link_hash_traverse (link_info.hash, pe_undef_count, "");
+
+ /* create and fill the corresponding table */
+ udef_table = xmalloc (undef_count * sizeof (struct key_value));
+
+ undef_count = 0;
+ bfd_link_hash_traverse (link_info.hash, pe_undef_fill, "");
+
+ /* sort items */
+ qsort (udef_table, undef_count, sizeof (struct key_value), undef_sort_cmp);
+}
+
+static void
+add_bfd_to_link (bfd *abfd, const char *name, struct bfd_link_info *linfo)
+{
+ lang_input_statement_type *fake_file;
+
+ fake_file = lang_add_input_file (name,
+ lang_input_file_is_fake_enum,
+ NULL);
+ fake_file->the_bfd = abfd;
+ ldlang_add_file (fake_file);
+
+ if (!bfd_link_add_symbols (abfd, linfo))
+ einfo (_("%X%P: add symbols %s: %E\n"), name);
+}
+
+void
+pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo)
+{
+ int i, j;
+ def_file_module *module;
+ def_file_import *imp;
+
+ pe_dll_id_target (bfd_get_target (output_bfd));
+
+ if (!pe_def_file)
+ return;
+
+ imp = pe_def_file->imports;
+
+ pe_create_undef_table ();
+
+ for (module = pe_def_file->modules; module; module = module->next)
+ {
+ int do_this_dll = 0;
+
+ for (i = 0; i < pe_def_file->num_imports && imp[i].module != module; i++)
+ ;
+ if (i >= pe_def_file->num_imports)
+ continue;
+
+ dll_filename = module->name;
+ dll_symname = xstrdup (module->name);
+ for (j = 0; dll_symname[j]; j++)
+ if (!ISALNUM (dll_symname[j]))
+ dll_symname[j] = '_';
+
+ for (; i < pe_def_file->num_imports && imp[i].module == module; i++)
+ {
+ def_file_export exp;
+ struct bfd_link_hash_entry *blhe;
+ int lead_at = (*imp[i].internal_name == '@');
+ /* See if we need this import. */
+ size_t len = strlen (imp[i].internal_name);
+ char *name = xmalloc (len + 2 + 6);
+ bfd_boolean include_jmp_stub = FALSE;
+ bfd_boolean is_cdecl = FALSE;
+ bfd_boolean is_undef = FALSE;
+
+ if (!lead_at && strchr (imp[i].internal_name, '@') == NULL)
+ is_cdecl = TRUE;
+
+ if (lead_at)
+ sprintf (name, "%s", imp[i].internal_name);
+ else
+ sprintf (name, "%s%s",U (""), imp[i].internal_name);
+
+ blhe = bfd_link_hash_lookup (linfo->hash, name,
+ FALSE, FALSE, FALSE);
+
+ /* Include the jump stub for <sym> only if the <sym>
+ is undefined. */
+ if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
+ {
+ if (lead_at)
+ sprintf (name, "%s%s", "__imp_", imp[i].internal_name);
+ else
+ sprintf (name, "%s%s%s", "__imp_", U (""),
+ imp[i].internal_name);
+
+ blhe = bfd_link_hash_lookup (linfo->hash, name,
+ FALSE, FALSE, FALSE);
+ if (blhe)
+ is_undef = (blhe->type == bfd_link_hash_undefined);
+ }
+ else
+ {
+ include_jmp_stub = TRUE;
+ is_undef = (blhe->type == bfd_link_hash_undefined);
+ }
+
+ if (is_cdecl && (!blhe || (blhe && blhe->type != bfd_link_hash_undefined)))
+ {
+ sprintf (name, "%s%s",U (""), imp[i].internal_name);
+ blhe = pe_find_cdecl_alias_match (linfo, name);
+ include_jmp_stub = TRUE;
+ if (blhe)
+ is_undef = (blhe->type == bfd_link_hash_undefined);
+ }
+
+ free (name);
+
+ if (is_undef)
+ {
+ bfd *one;
+ /* We do. */
+ if (!do_this_dll)
+ {
+ bfd *ar_head = make_head (output_bfd);
+ add_bfd_to_link (ar_head, ar_head->filename, linfo);
+ do_this_dll = 1;
+ }
+ exp.internal_name = imp[i].internal_name;
+ exp.name = imp[i].name;
+ exp.its_name = imp[i].its_name;
+ exp.ordinal = imp[i].ordinal;
+ exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0;
+ exp.flag_private = 0;
+ exp.flag_constant = 0;
+ exp.flag_data = imp[i].data;
+ exp.flag_noname = exp.name ? 0 : 1;
+ one = make_one (&exp, output_bfd, (! exp.flag_data) && include_jmp_stub);
+ add_bfd_to_link (one, one->filename, linfo);
+ }
+ }
+ if (do_this_dll)
+ {
+ bfd *ar_tail = make_tail (output_bfd);
+ add_bfd_to_link (ar_tail, ar_tail->filename, linfo);
+ }
+
+ free (dll_symname);
+ }
+
+ while (undef_count)
+ {
+ --undef_count;
+ free (udef_table[undef_count].key);
+ }
+ free (udef_table);
+}
+
+/* We were handed a *.DLL file. Parse it and turn it into a set of
+ IMPORTS directives in the def file. Return TRUE if the file was
+ handled, FALSE if not. */
+
+static unsigned int
+pe_get16 (bfd *abfd, int where)
+{
+ unsigned char b[2];
+
+ bfd_seek (abfd, (file_ptr) where, SEEK_SET);
+ bfd_bread (b, (bfd_size_type) 2, abfd);
+ return b[0] + (b[1] << 8);
+}
+
+static unsigned int
+pe_get32 (bfd *abfd, int where)
+{
+ unsigned char b[4];
+
+ bfd_seek (abfd, (file_ptr) where, SEEK_SET);
+ bfd_bread (b, (bfd_size_type) 4, abfd);
+ return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
+}
+
+static unsigned int
+pe_as32 (void *ptr)
+{
+ unsigned char *b = ptr;
+
+ return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
+}
+
+bfd_boolean
+pe_implied_import_dll (const char *filename)
+{
+ bfd *dll;
+ bfd_vma pe_header_offset, opthdr_ofs, num_entries, i;
+ bfd_vma export_rva, export_size, nsections, secptr, expptr;
+ bfd_vma exp_funcbase;
+ unsigned char *expdata;
+ char *erva;
+ bfd_vma name_rvas, nexp;
+ const char *dllname;
+ /* Initialization with start > end guarantees that is_data
+ will not be set by mistake, and avoids compiler warning. */
+ bfd_vma data_start = 1;
+ bfd_vma data_end = 0;
+ bfd_vma rdata_start = 1;
+ bfd_vma rdata_end = 0;
+ bfd_vma bss_start = 1;
+ bfd_vma bss_end = 0;
+ int from;
+
+ /* No, I can't use bfd here. kernel32.dll puts its export table in
+ the middle of the .rdata section. */
+ dll = bfd_openr (filename, pe_details->target_name);
+ if (!dll)
+ {
+ einfo (_("%X%P: open %s: %E\n"), filename);
+ return FALSE;
+ }
+
+ /* PEI dlls seem to be bfd_objects. */
+ if (!bfd_check_format (dll, bfd_object))
+ {
+ einfo (_("%X%P: %s: this doesn't appear to be a DLL\n"), filename);
+ return FALSE;
+ }
+
+ /* Get pe_header, optional header and numbers of directory entries. */
+ pe_header_offset = pe_get32 (dll, 0x3c);
+ opthdr_ofs = pe_header_offset + 4 + 20;
+#ifdef pe_use_x86_64
+ num_entries = pe_get32 (dll, opthdr_ofs + 92 + 4 * 4); /* & NumberOfRvaAndSizes. */
+#else
+ num_entries = pe_get32 (dll, opthdr_ofs + 92);
+#endif
+
+ /* No import or export directory entry. */
+ if (num_entries < 1)
+ return FALSE;
+
+#ifdef pe_use_x86_64
+ export_rva = pe_get32 (dll, opthdr_ofs + 96 + 4 * 4);
+ export_size = pe_get32 (dll, opthdr_ofs + 100 + 4 * 4);
+#else
+ export_rva = pe_get32 (dll, opthdr_ofs + 96);
+ export_size = pe_get32 (dll, opthdr_ofs + 100);
+#endif
+
+ /* No export table - nothing to export. */
+ if (export_size == 0)
+ return FALSE;
+
+ nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
+ secptr = (pe_header_offset + 4 + 20 +
+ pe_get16 (dll, pe_header_offset + 4 + 16));
+ expptr = 0;
+
+ /* Get the rva and size of the export section. */
+ for (i = 0; i < nsections; i++)
+ {
+ char sname[8];
+ bfd_vma secptr1 = secptr + 40 * i;
+ bfd_vma vaddr = pe_get32 (dll, secptr1 + 12);
+ bfd_vma vsize = pe_get32 (dll, secptr1 + 16);
+ bfd_vma fptr = pe_get32 (dll, secptr1 + 20);
+
+ bfd_seek (dll, (file_ptr) secptr1, SEEK_SET);
+ bfd_bread (sname, (bfd_size_type) 8, dll);
+
+ if (vaddr <= export_rva && vaddr + vsize > export_rva)
+ {
+ expptr = fptr + (export_rva - vaddr);
+ if (export_rva + export_size > vaddr + vsize)
+ export_size = vsize - (export_rva - vaddr);
+ break;
+ }
+ }
+
+ /* Scan sections and store the base and size of the
+ data and bss segments in data/base_start/end. */
+ for (i = 0; i < nsections; i++)
+ {
+ bfd_vma secptr1 = secptr + 40 * i;
+ bfd_vma vsize = pe_get32 (dll, secptr1 + 8);
+ bfd_vma vaddr = pe_get32 (dll, secptr1 + 12);
+ bfd_vma flags = pe_get32 (dll, secptr1 + 36);
+ char sec_name[9];
+
+ sec_name[8] = '\0';
+ bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET);
+ bfd_bread (sec_name, (bfd_size_type) 8, dll);
+
+ if (strcmp(sec_name,".data") == 0)
+ {
+ data_start = vaddr;
+ data_end = vaddr + vsize;
+
+ if (pe_dll_extra_pe_debug)
+ printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
+ __FUNCTION__, sec_name, (unsigned long) vaddr,
+ (unsigned long) (vaddr + vsize), (unsigned long) flags);
+ }
+ else if (strcmp(sec_name,".rdata") == 0)
+ {
+ rdata_start = vaddr;
+ rdata_end = vaddr + vsize;
+
+ if (pe_dll_extra_pe_debug)
+ printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
+ __FUNCTION__, sec_name, (unsigned long) vaddr,
+ (unsigned long) (vaddr + vsize), (unsigned long) flags);
+ }
+ else if (strcmp (sec_name,".bss") == 0)
+ {
+ bss_start = vaddr;
+ bss_end = vaddr + vsize;
+
+ if (pe_dll_extra_pe_debug)
+ printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
+ __FUNCTION__, sec_name, (unsigned long) vaddr,
+ (unsigned long) (vaddr + vsize), (unsigned long) flags);
+ }
+ }
+
+ expdata = xmalloc (export_size);
+ bfd_seek (dll, (file_ptr) expptr, SEEK_SET);
+ bfd_bread (expdata, (bfd_size_type) export_size, dll);
+ erva = (char *) expdata - export_rva;
+
+ if (pe_def_file == 0)
+ pe_def_file = def_file_empty ();
+
+ nexp = pe_as32 (expdata + 24);
+ name_rvas = pe_as32 (expdata + 32);
+ exp_funcbase = pe_as32 (expdata + 28);
+
+ /* Use internal dll name instead of filename
+ to enable symbolic dll linking. */
+ dllname = erva + pe_as32 (expdata + 12);
+
+ /* Check to see if the dll has already been added to
+ the definition list and if so return without error.
+ This avoids multiple symbol definitions. */
+ if (def_get_module (pe_def_file, dllname))
+ {
+ if (pe_dll_extra_pe_debug)
+ printf ("%s is already loaded\n", dllname);
+ return TRUE;
+ }
+
+ /* This is an optimized version of the insertion loop, which avoids lots of
+ calls to realloc and memmove from def_file_add_import. */
+ if ((from = def_file_add_import_from (pe_def_file, nexp,
+ erva + pe_as32 (erva + name_rvas),
+ dllname, 0, NULL, NULL)) >= 0)
+ {
+ for (i = 0; i < nexp; i++)
+ {
+ /* Pointer to the names vector. */
+ bfd_vma name_rva = pe_as32 (erva + name_rvas + i * 4);
+ def_file_import *imp;
+ /* Pointer to the function address vector. */
+ bfd_vma func_rva = pe_as32 (erva + exp_funcbase + i * 4);
+ /* is_data is true if the address is in the data, rdata or bss
+ segment. */
+ const int is_data =
+ (func_rva >= data_start && func_rva < data_end)
+ || (func_rva >= rdata_start && func_rva < rdata_end)
+ || (func_rva >= bss_start && func_rva < bss_end);
+
+ imp = def_file_add_import_at (pe_def_file, from + i, erva + name_rva,
+ dllname, i, NULL, NULL);
+ /* Mark symbol type. */
+ imp->data = is_data;
+
+ if (pe_dll_extra_pe_debug)
+ printf ("%s dll-name: %s sym: %s addr: 0x%lx %s\n",
+ __FUNCTION__, dllname, erva + name_rva,
+ (unsigned long) func_rva, is_data ? "(data)" : "");
+ }
+
+ return TRUE;
+ }
+
+ /* Iterate through the list of symbols. */
+ for (i = 0; i < nexp; i++)
+ {
+ /* Pointer to the names vector. */
+ bfd_vma name_rva = pe_as32 (erva + name_rvas + i * 4);
+ def_file_import *imp;
+ /* Pointer to the function address vector. */
+ bfd_vma func_rva = pe_as32 (erva + exp_funcbase + i * 4);
+ int is_data = 0;
+
+ /* Skip unwanted symbols, which are
+ exported in buggy auto-import releases. */
+ if (! CONST_STRNEQ (erva + name_rva, "__nm_"))
+ {
+ int is_dup = 0;
+ /* is_data is true if the address is in the data, rdata or bss
+ segment. */
+ is_data =
+ (func_rva >= data_start && func_rva < data_end)
+ || (func_rva >= rdata_start && func_rva < rdata_end)
+ || (func_rva >= bss_start && func_rva < bss_end);
+
+ imp = def_file_add_import (pe_def_file, erva + name_rva,
+ dllname, i, NULL, NULL, &is_dup);
+ /* Mark symbol type. */
+ if (!is_dup)
+ imp->data = is_data;
+
+ if (pe_dll_extra_pe_debug)
+ printf ("%s dll-name: %s sym: %s addr: 0x%lx %s\n",
+ __FUNCTION__, dllname, erva + name_rva,
+ (unsigned long) func_rva, is_data ? "(data)" : "");
+ }
+ }
+
+ return TRUE;
+}
+
+void
+pe_output_file_set_long_section_names (bfd *abfd)
+{
+ if (pe_use_coff_long_section_names < 0)
+ return;
+ if (!bfd_coff_set_long_section_names (abfd, pe_use_coff_long_section_names))
+ einfo (_("%X%P: error: can't use long section names on this arch\n"));
+}
+
+/* These are the main functions, called from the emulation. The first
+ is called after the bfds are read, so we can guess at how much space
+ we need. The second is called after everything is placed, so we
+ can put the right values in place. */
+
+void
+pe_dll_build_sections (bfd *abfd, struct bfd_link_info *info)
+{
+ pe_dll_id_target (bfd_get_target (abfd));
+ pe_output_file_set_long_section_names (abfd);
+ process_def_file_and_drectve (abfd, info);
+
+ if (pe_def_file->num_exports == 0 && !bfd_link_pic (info))
+ return;
+
+ generate_edata (abfd, info);
+ build_filler_bfd (1);
+ pe_output_file_set_long_section_names (filler_bfd);
+}
+
+void
+pe_exe_build_sections (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
+{
+ pe_dll_id_target (bfd_get_target (abfd));
+ pe_output_file_set_long_section_names (abfd);
+ build_filler_bfd (0);
+ pe_output_file_set_long_section_names (filler_bfd);
+}
+
+void
+pe_dll_fill_sections (bfd *abfd, struct bfd_link_info *info)
+{
+ pe_dll_id_target (bfd_get_target (abfd));
+ pe_output_file_set_long_section_names (abfd);
+ image_base = pe_data (abfd)->pe_opthdr.ImageBase;
+
+ generate_reloc (abfd, info);
+ if (reloc_sz > 0)
+ {
+ bfd_set_section_size (reloc_s, reloc_sz);
+
+ /* Resize the sections. */
+ lang_reset_memory_regions ();
+ lang_size_sections (NULL, TRUE);
+
+ /* Redo special stuff. */
+ ldemul_after_allocation ();
+
+ /* Do the assignments again. */
+ lang_do_assignments (lang_final_phase_enum);
+ }
+
+ fill_edata (abfd, info);
+
+ if (bfd_link_dll (info))
+ pe_data (abfd)->dll = 1;
+
+ edata_s->contents = edata_d;
+ reloc_s->contents = reloc_d;
+}
+
+void
+pe_exe_fill_sections (bfd *abfd, struct bfd_link_info *info)
+{
+ pe_dll_id_target (bfd_get_target (abfd));
+ pe_output_file_set_long_section_names (abfd);
+ image_base = pe_data (abfd)->pe_opthdr.ImageBase;
+
+ generate_reloc (abfd, info);
+ if (reloc_sz > 0)
+ {
+ bfd_set_section_size (reloc_s, reloc_sz);
+
+ /* Resize the sections. */
+ lang_reset_memory_regions ();
+ lang_size_sections (NULL, TRUE);
+
+ /* Redo special stuff. */
+ ldemul_after_allocation ();
+
+ /* Do the assignments again. */
+ lang_do_assignments (lang_final_phase_enum);
+ }
+ reloc_s->contents = reloc_d;
+}
+
+bfd_boolean
+pe_bfd_is_dll (bfd *abfd)
+{
+ return (bfd_get_format (abfd) == bfd_object
+ && obj_pe (abfd)
+ && pe_data (abfd)->dll);