+ fprintf (f,"%s Export Ordinal Table\n", ASM_C);
+ fprintf (f, "anords:\n");
+ for (i = 0; (exp = d_exports_lexically[i]); i++)
+ {
+ if (!exp->noname || show_allnames)
+ fprintf (f, "\t%s %d\n", ASM_SHORT, exp->ordinal - d_low_ord);
+ }
+
+ fprintf(f,"%s Export Name Table\n", ASM_C);
+ for (i = 0; (exp = d_exports_lexically[i]); i++)
+ {
+ if (!exp->noname || show_allnames)
+ fprintf (f, "n%d: %s \"%s\"\n",
+ exp->ordinal, ASM_TEXT,
+ (exp->its_name ? exp->its_name : xlate (exp->name)));
+ if (exp->forward != 0)
+ fprintf (f, "f%d: %s \"%s\"\n",
+ exp->forward, ASM_TEXT, exp->internal_name);
+ }
+
+ if (a_list)
+ {
+ fprintf (f, "\t.section %s\n", DRECTVE_SECTION_NAME);
+ for (dl = a_list; dl; dl = dl->next)
+ {
+ fprintf (f, "\t%s\t\"%s\"\n", ASM_TEXT, dl->text);
+ }
+ }
+
+ if (d_list)
+ {
+ fprintf (f, "\t.section .rdata\n");
+ for (dl = d_list; dl; dl = dl->next)
+ {
+ char *p;
+ int l;
+
+ /* We don't output as ascii because there can
+ be quote characters in the string. */
+ l = 0;
+ for (p = dl->text; *p; p++)
+ {
+ if (l == 0)
+ fprintf (f, "\t%s\t", ASM_BYTE);
+ else
+ fprintf (f, ",");
+ fprintf (f, "%d", *p);
+ if (p[1] == 0)
+ {
+ fprintf (f, ",0\n");
+ break;
+ }
+ if (++l == 10)
+ {
+ fprintf (f, "\n");
+ l = 0;
+ }
+ }
+ }
+ }
+ }
+
+ /* Add to the output file a way of getting to the exported names
+ without using the import library. */
+ if (add_indirect)
+ {
+ fprintf (f, "\t.section\t.rdata\n");
+ for (i = 0, exp = d_exports; exp; i++, exp = exp->next)
+ if (!exp->noname || show_allnames)
+ {
+ /* We use a single underscore for MS compatibility, and a
+ double underscore for backward compatibility with old
+ cygwin releases. */
+ if (create_compat_implib)
+ fprintf (f, "\t%s\t__imp_%s\n", ASM_GLOBAL, exp->name);
+ fprintf (f, "\t%s\t_imp_%s%s\n", ASM_GLOBAL,
+ (!leading_underscore ? "" : "_"), exp->name);
+ if (create_compat_implib)
+ fprintf (f, "__imp_%s:\n", exp->name);
+ fprintf (f, "_imp_%s%s:\n", (!leading_underscore ? "" : "_"), exp->name);
+ fprintf (f, "\t%s\t%s\n", ASM_LONG, exp->name);
+ }
+ }
+
+ /* Dump the reloc section if a base file is provided. */
+ if (base_file)
+ {
+ bfd_vma addr;
+ bfd_vma need[COFF_PAGE_SIZE];
+ bfd_vma page_addr;
+ bfd_size_type numbytes;
+ int num_entries;
+ bfd_vma *copy;
+ int j;
+ int on_page;
+ fprintf (f, "\t.section\t.init\n");
+ fprintf (f, "lab:\n");
+
+ fseek (base_file, 0, SEEK_END);
+ numbytes = ftell (base_file);
+ fseek (base_file, 0, SEEK_SET);
+ copy = xmalloc (numbytes);
+ if (fread (copy, 1, numbytes, base_file) < numbytes)
+ fatal (_("failed to read the number of entries from base file"));
+ num_entries = numbytes / sizeof (bfd_vma);
+
+
+ fprintf (f, "\t.section\t.reloc\n");
+ if (num_entries)
+ {
+ int src;
+ int dst = 0;
+ bfd_vma last = (bfd_vma) -1;
+ qsort (copy, num_entries, sizeof (bfd_vma), sfunc);
+ /* Delete duplicates */
+ for (src = 0; src < num_entries; src++)
+ {
+ if (last != copy[src])
+ last = copy[dst++] = copy[src];
+ }
+ num_entries = dst;
+ addr = copy[0];
+ page_addr = addr & PAGE_MASK; /* work out the page addr */
+ on_page = 0;
+ for (j = 0; j < num_entries; j++)
+ {
+ addr = copy[j];
+ if ((addr & PAGE_MASK) != page_addr)
+ {
+ flush_page (f, need, page_addr, on_page);
+ on_page = 0;
+ page_addr = addr & PAGE_MASK;
+ }
+ need[on_page++] = addr;
+ }
+ flush_page (f, need, page_addr, on_page);
+
+/* fprintf (f, "\t%s\t0,0\t%s End\n", ASM_LONG, ASM_C);*/
+ }
+ }
+
+ generate_idata_ofile (f);
+
+ fclose (f);
+
+ /* Assemble the file. */
+ assemble_file (TMP_ASM, exp_name);
+
+ if (dontdeltemps == 0)
+ {
+ temp_file_to_remove[TEMP_EXPORT_FILE] = NULL;
+ unlink (TMP_ASM);
+ }
+
+ inform (_("Generated exports file"));
+}
+
+static const char *
+xlate (const char *name)
+{
+ int lead_at = (*name == '@');
+ int is_stdcall = (!lead_at && strchr (name, '@') != NULL);
+
+ if (!lead_at && (add_underscore
+ || (add_stdcall_underscore && is_stdcall)))
+ {
+ char *copy = xmalloc (strlen (name) + 2);
+
+ copy[0] = '_';
+ strcpy (copy + 1, name);
+ name = copy;
+ }
+
+ if (killat)
+ {
+ char *p;
+
+ name += lead_at;
+ /* PR 9766: Look for the last @ sign in the name. */
+ p = strrchr (name, '@');
+ if (p && ISDIGIT (p[1]))
+ *p = 0;
+ }
+ return name;
+}
+
+typedef struct
+{
+ int id;
+ const char *name;
+ int flags;
+ int align;
+ asection *sec;
+ asymbol *sym;
+ asymbol **sympp;
+ int size;
+ unsigned char *data;
+} sinfo;
+
+#define INIT_SEC_DATA(id, name, flags, align) \
+ { id, name, flags, align, NULL, NULL, NULL, 0, NULL }
+
+#ifndef DLLTOOL_PPC
+
+#define TEXT 0
+#define DATA 1
+#define BSS 2
+#define IDATA7 3
+#define IDATA5 4
+#define IDATA4 5
+#define IDATA6 6
+
+#define NSECS 7
+
+#define TEXT_SEC_FLAGS \
+ (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS)
+#define DATA_SEC_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_DATA)
+#define BSS_SEC_FLAGS SEC_ALLOC
+
+static sinfo secdata[NSECS] =
+{
+ INIT_SEC_DATA (TEXT, ".text", TEXT_SEC_FLAGS, 2),
+ INIT_SEC_DATA (DATA, ".data", DATA_SEC_FLAGS, 2),
+ INIT_SEC_DATA (BSS, ".bss", BSS_SEC_FLAGS, 2),
+ INIT_SEC_DATA (IDATA7, ".idata$7", SEC_HAS_CONTENTS, 2),
+ INIT_SEC_DATA (IDATA5, ".idata$5", SEC_HAS_CONTENTS, 2),
+ INIT_SEC_DATA (IDATA4, ".idata$4", SEC_HAS_CONTENTS, 2),
+ INIT_SEC_DATA (IDATA6, ".idata$6", SEC_HAS_CONTENTS, 1)
+};
+
+#else
+
+/* Sections numbered to make the order the same as other PowerPC NT
+ compilers. This also keeps funny alignment thingies from happening. */
+#define TEXT 0
+#define PDATA 1
+#define RDATA 2
+#define IDATA5 3
+#define IDATA4 4
+#define IDATA6 5
+#define IDATA7 6
+#define DATA 7
+#define BSS 8
+
+#define NSECS 9
+
+static sinfo secdata[NSECS] =
+{
+ INIT_SEC_DATA (TEXT, ".text", SEC_CODE | SEC_HAS_CONTENTS, 3),
+ INIT_SEC_DATA (PDATA, ".pdata", SEC_HAS_CONTENTS, 2),
+ INIT_SEC_DATA (RDATA, ".reldata", SEC_HAS_CONTENTS, 2),
+ INIT_SEC_DATA (IDATA5, ".idata$5", SEC_HAS_CONTENTS, 2),
+ INIT_SEC_DATA (IDATA4, ".idata$4", SEC_HAS_CONTENTS, 2),
+ INIT_SEC_DATA (IDATA6, ".idata$6", SEC_HAS_CONTENTS, 1),
+ INIT_SEC_DATA (IDATA7, ".idata$7", SEC_HAS_CONTENTS, 2),
+ INIT_SEC_DATA (DATA, ".data", SEC_DATA, 2),
+ INIT_SEC_DATA (BSS, ".bss", 0, 2)
+};
+
+#endif
+
+/* This is what we're trying to make. We generate the imp symbols with
+ both single and double underscores, for compatibility.
+
+ .text
+ .global _GetFileVersionInfoSizeW@8
+ .global __imp_GetFileVersionInfoSizeW@8
+_GetFileVersionInfoSizeW@8:
+ jmp * __imp_GetFileVersionInfoSizeW@8
+ .section .idata$7 # To force loading of head
+ .long __version_a_head
+# Import Address Table
+ .section .idata$5
+__imp_GetFileVersionInfoSizeW@8:
+ .rva ID2
+
+# Import Lookup Table
+ .section .idata$4
+ .rva ID2
+# Hint/Name table
+ .section .idata$6
+ID2: .short 2
+ .asciz "GetFileVersionInfoSizeW"
+
+
+ For the PowerPC, here's the variation on the above scheme:
+
+# Rather than a simple "jmp *", the code to get to the dll function
+# looks like:
+ .text
+ lwz r11,[tocv]__imp_function_name(r2)
+# RELOC: 00000000 TOCREL16,TOCDEFN __imp_function_name
+ lwz r12,0(r11)
+ stw r2,4(r1)
+ mtctr r12
+ lwz r2,4(r11)
+ bctr */
+
+static char *
+make_label (const char *prefix, const char *name)
+{
+ int len = strlen (ASM_PREFIX (name)) + strlen (prefix) + strlen (name);
+ char *copy = xmalloc (len + 1);
+
+ strcpy (copy, ASM_PREFIX (name));
+ strcat (copy, prefix);
+ strcat (copy, name);
+ return copy;
+}
+
+static char *
+make_imp_label (const char *prefix, const char *name)
+{
+ int len;
+ char *copy;
+
+ if (name[0] == '@')
+ {
+ len = strlen (prefix) + strlen (name);
+ copy = xmalloc (len + 1);
+ strcpy (copy, prefix);
+ strcat (copy, name);
+ }
+ else
+ {
+ len = strlen (ASM_PREFIX (name)) + strlen (prefix) + strlen (name);
+ copy = xmalloc (len + 1);
+ strcpy (copy, prefix);
+ strcat (copy, ASM_PREFIX (name));
+ strcat (copy, name);
+ }
+ return copy;
+}
+
+static bfd *
+make_one_lib_file (export_type *exp, int i, int delay)
+{
+ bfd * abfd;
+ asymbol * exp_label;
+ asymbol * iname = 0;
+ asymbol * iname2;
+ asymbol * iname_lab;
+ asymbol ** iname_lab_pp;
+ asymbol ** iname_pp;
+#ifdef DLLTOOL_PPC
+ asymbol ** fn_pp;
+ asymbol ** toc_pp;
+#define EXTRA 2
+#endif
+#ifndef EXTRA
+#define EXTRA 0
+#endif
+ asymbol * ptrs[NSECS + 4 + EXTRA + 1];
+ flagword applicable;
+ char * outname = xmalloc (strlen (TMP_STUB) + 10);
+ int oidx = 0;
+
+
+ sprintf (outname, "%s%05d.o", TMP_STUB, i);
+
+ abfd = bfd_openw (outname, HOW_BFD_WRITE_TARGET);
+
+ if (!abfd)
+ /* xgettext:c-format */
+ fatal (_("bfd_open failed open stub file: %s: %s"),
+ outname, bfd_get_errmsg ());
+
+ /* xgettext:c-format */
+ inform (_("Creating stub file: %s"), outname);
+
+ bfd_set_format (abfd, bfd_object);
+ bfd_set_arch_mach (abfd, HOW_BFD_ARCH, 0);
+
+#ifdef DLLTOOL_ARM
+ if (machine == MARM_INTERWORK || machine == MTHUMB)
+ bfd_set_private_flags (abfd, F_INTERWORK);
+#endif
+
+ applicable = bfd_applicable_section_flags (abfd);
+
+ /* First make symbols for the sections. */
+ for (i = 0; i < NSECS; i++)
+ {
+ sinfo *si = secdata + i;
+
+ if (si->id != i)
+ abort ();
+ si->sec = bfd_make_section_old_way (abfd, si->name);
+ bfd_set_section_flags (abfd,
+ si->sec,
+ si->flags & applicable);
+
+ bfd_set_section_alignment(abfd, si->sec, si->align);
+ si->sec->output_section = si->sec;
+ si->sym = bfd_make_empty_symbol(abfd);
+ si->sym->name = si->sec->name;
+ si->sym->section = si->sec;
+ si->sym->flags = BSF_LOCAL;
+ si->sym->value = 0;
+ ptrs[oidx] = si->sym;
+ si->sympp = ptrs + oidx;
+ si->size = 0;
+ si->data = NULL;
+
+ oidx++;
+ }
+
+ if (! exp->data)
+ {
+ exp_label = bfd_make_empty_symbol (abfd);
+ exp_label->name = make_imp_label ("", exp->name);
+
+ /* On PowerPC, the function name points to a descriptor in
+ the rdata section, the first element of which is a
+ pointer to the code (..function_name), and the second
+ points to the .toc. */
+#ifdef DLLTOOL_PPC
+ if (machine == MPPC)
+ exp_label->section = secdata[RDATA].sec;
+ else
+#endif
+ exp_label->section = secdata[TEXT].sec;
+
+ exp_label->flags = BSF_GLOBAL;
+ exp_label->value = 0;
+
+#ifdef DLLTOOL_ARM
+ if (machine == MTHUMB)
+ bfd_coff_set_symbol_class (abfd, exp_label, C_THUMBEXTFUNC);
+#endif
+ ptrs[oidx++] = exp_label;
+ }
+
+ /* Generate imp symbols with one underscore for Microsoft
+ compatibility, and with two underscores for backward
+ compatibility with old versions of cygwin. */
+ if (create_compat_implib)
+ {
+ iname = bfd_make_empty_symbol (abfd);
+ iname->name = make_imp_label ("___imp", exp->name);
+ iname->section = secdata[IDATA5].sec;
+ iname->flags = BSF_GLOBAL;
+ iname->value = 0;
+ }
+
+ iname2 = bfd_make_empty_symbol (abfd);
+ iname2->name = make_imp_label ("__imp_", exp->name);
+ iname2->section = secdata[IDATA5].sec;
+ iname2->flags = BSF_GLOBAL;
+ iname2->value = 0;
+
+ iname_lab = bfd_make_empty_symbol (abfd);
+
+ iname_lab->name = head_label;
+ iname_lab->section = bfd_und_section_ptr;
+ iname_lab->flags = 0;
+ iname_lab->value = 0;
+
+ iname_pp = ptrs + oidx;
+ if (create_compat_implib)
+ ptrs[oidx++] = iname;
+ ptrs[oidx++] = iname2;
+
+ iname_lab_pp = ptrs + oidx;
+ ptrs[oidx++] = iname_lab;
+
+#ifdef DLLTOOL_PPC
+ /* The symbol referring to the code (.text). */
+ {
+ asymbol *function_name;
+
+ function_name = bfd_make_empty_symbol(abfd);
+ function_name->name = make_label ("..", exp->name);
+ function_name->section = secdata[TEXT].sec;
+ function_name->flags = BSF_GLOBAL;
+ function_name->value = 0;
+
+ fn_pp = ptrs + oidx;
+ ptrs[oidx++] = function_name;
+ }
+
+ /* The .toc symbol. */
+ {
+ asymbol *toc_symbol;
+
+ toc_symbol = bfd_make_empty_symbol (abfd);
+ toc_symbol->name = make_label (".", "toc");
+ toc_symbol->section = bfd_und_section_ptr;
+ toc_symbol->flags = BSF_GLOBAL;
+ toc_symbol->value = 0;
+
+ toc_pp = ptrs + oidx;
+ ptrs[oidx++] = toc_symbol;
+ }
+#endif
+
+ ptrs[oidx] = 0;
+
+ for (i = 0; i < NSECS; i++)
+ {
+ sinfo *si = secdata + i;
+ asection *sec = si->sec;
+ arelent *rel, *rel2 = 0, *rel3 = 0;
+ arelent **rpp;
+
+ switch (i)
+ {
+ case TEXT:
+ if (! exp->data)
+ {
+ si->size = HOW_JTAB_SIZE;
+ si->data = xmalloc (HOW_JTAB_SIZE);
+ memcpy (si->data, HOW_JTAB, HOW_JTAB_SIZE);
+
+ /* Add the reloc into idata$5. */
+ rel = xmalloc (sizeof (arelent));
+
+ rpp = xmalloc (sizeof (arelent *) * (delay ? 4 : 2));
+ rpp[0] = rel;
+ rpp[1] = 0;
+
+ rel->address = HOW_JTAB_ROFF;
+ rel->addend = 0;
+
+ if (delay)
+ {
+ rel2 = xmalloc (sizeof (arelent));
+ rpp[1] = rel2;
+ rel2->address = HOW_JTAB_ROFF2;
+ rel2->addend = 0;
+ rel3 = xmalloc (sizeof (arelent));
+ rpp[2] = rel3;
+ rel3->address = HOW_JTAB_ROFF3;
+ rel3->addend = 0;
+ rpp[3] = 0;
+ }
+
+ if (machine == MPPC)
+ {
+ rel->howto = bfd_reloc_type_lookup (abfd,
+ BFD_RELOC_16_GOTOFF);
+ rel->sym_ptr_ptr = iname_pp;
+ }
+ else if (machine == MX86)
+ {
+ rel->howto = bfd_reloc_type_lookup (abfd,
+ BFD_RELOC_32_PCREL);
+ rel->sym_ptr_ptr = iname_pp;
+ }
+ else
+ {
+ rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
+ rel->sym_ptr_ptr = secdata[IDATA5].sympp;
+ }
+
+ if (delay)
+ {
+ if (machine == MX86)
+ rel2->howto = bfd_reloc_type_lookup (abfd,
+ BFD_RELOC_32_PCREL);
+ else
+ rel2->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
+ rel2->sym_ptr_ptr = rel->sym_ptr_ptr;
+ rel3->howto = bfd_reloc_type_lookup (abfd,
+ BFD_RELOC_32_PCREL);
+ rel3->sym_ptr_ptr = iname_lab_pp;
+ }
+
+ sec->orelocation = rpp;
+ sec->reloc_count = delay ? 3 : 1;
+ }
+ break;
+
+ case IDATA5:
+ if (delay)
+ {
+ si->size = create_for_pep ? 8 : 4;
+ si->data = xmalloc (si->size);
+ sec->reloc_count = 1;
+ memset (si->data, 0, si->size);
+ /* Point after jmp [__imp_...] instruction. */
+ si->data[0] = 6;
+ rel = xmalloc (sizeof (arelent));
+ rpp = xmalloc (sizeof (arelent *) * 2);
+ rpp[0] = rel;
+ rpp[1] = 0;
+ rel->address = 0;
+ rel->addend = 0;
+ if (create_for_pep)
+ rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_64);
+ else
+ rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
+ rel->sym_ptr_ptr = secdata[TEXT].sympp;
+ sec->orelocation = rpp;
+ break;
+ }
+ /* else fall through */
+ case IDATA4:
+ /* An idata$4 or idata$5 is one word long, and has an
+ rva to idata$6. */
+
+ if (create_for_pep)
+ {
+ si->data = xmalloc (8);
+ si->size = 8;
+ if (exp->noname)
+ {
+ si->data[0] = exp->ordinal ;
+ si->data[1] = exp->ordinal >> 8;
+ si->data[2] = exp->ordinal >> 16;
+ si->data[3] = exp->ordinal >> 24;
+ si->data[4] = 0;
+ si->data[5] = 0;
+ si->data[6] = 0;
+ si->data[7] = 0x80;
+ }
+ else
+ {
+ sec->reloc_count = 1;
+ memset (si->data, 0, si->size);
+ rel = xmalloc (sizeof (arelent));
+ rpp = xmalloc (sizeof (arelent *) * 2);
+ rpp[0] = rel;
+ rpp[1] = 0;
+ rel->address = 0;
+ rel->addend = 0;
+ rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA);
+ rel->sym_ptr_ptr = secdata[IDATA6].sympp;
+ sec->orelocation = rpp;
+ }
+ }
+ else
+ {
+ si->data = xmalloc (4);
+ si->size = 4;
+
+ if (exp->noname)
+ {
+ si->data[0] = exp->ordinal ;
+ si->data[1] = exp->ordinal >> 8;
+ si->data[2] = exp->ordinal >> 16;
+ si->data[3] = 0x80;
+ }
+ else
+ {
+ sec->reloc_count = 1;
+ memset (si->data, 0, si->size);
+ rel = xmalloc (sizeof (arelent));
+ rpp = xmalloc (sizeof (arelent *) * 2);
+ rpp[0] = rel;
+ rpp[1] = 0;
+ rel->address = 0;
+ rel->addend = 0;
+ rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA);
+ rel->sym_ptr_ptr = secdata[IDATA6].sympp;
+ sec->orelocation = rpp;
+ }
+ }
+ break;
+
+ case IDATA6:
+ if (!exp->noname)
+ {
+ /* This used to add 1 to exp->hint. I don't know
+ why it did that, and it does not match what I see
+ in programs compiled with the MS tools. */
+ int idx = exp->hint;
+ if (exp->its_name)
+ si->size = strlen (exp->its_name) + 3;
+ else
+ si->size = strlen (xlate (exp->import_name)) + 3;
+ si->data = xmalloc (si->size);
+ memset (si->data, 0, si->size);
+ si->data[0] = idx & 0xff;
+ si->data[1] = idx >> 8;
+ if (exp->its_name)
+ strcpy ((char *) si->data + 2, exp->its_name);
+ else
+ strcpy ((char *) si->data + 2, xlate (exp->import_name));
+ }
+ break;
+ case IDATA7:
+ if (delay)
+ break;
+ si->size = 4;
+ si->data = xmalloc (4);
+ memset (si->data, 0, si->size);
+ rel = xmalloc (sizeof (arelent));
+ rpp = xmalloc (sizeof (arelent *) * 2);
+ rpp[0] = rel;
+ rel->address = 0;
+ rel->addend = 0;
+ rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA);
+ rel->sym_ptr_ptr = iname_lab_pp;
+ sec->orelocation = rpp;
+ sec->reloc_count = 1;
+ break;
+
+#ifdef DLLTOOL_PPC
+ case PDATA:
+ {
+ /* The .pdata section is 5 words long.
+ Think of it as:
+ struct
+ {
+ bfd_vma BeginAddress, [0x00]
+ EndAddress, [0x04]
+ ExceptionHandler, [0x08]
+ HandlerData, [0x0c]
+ PrologEndAddress; [0x10]
+ }; */
+
+ /* So this pdata section setups up this as a glue linkage to
+ a dll routine. There are a number of house keeping things
+ we need to do:
+
+ 1. In the name of glue trickery, the ADDR32 relocs for 0,
+ 4, and 0x10 are set to point to the same place:
+ "..function_name".
+ 2. There is one more reloc needed in the pdata section.
+ The actual glue instruction to restore the toc on
+ return is saved as the offset in an IMGLUE reloc.
+ So we need a total of four relocs for this section.
+
+ 3. Lastly, the HandlerData field is set to 0x03, to indicate
+ that this is a glue routine. */
+ arelent *imglue, *ba_rel, *ea_rel, *pea_rel;
+
+ /* Alignment must be set to 2**2 or you get extra stuff. */
+ bfd_set_section_alignment(abfd, sec, 2);
+
+ si->size = 4 * 5;
+ si->data = xmalloc (si->size);
+ memset (si->data, 0, si->size);
+ rpp = xmalloc (sizeof (arelent *) * 5);
+ rpp[0] = imglue = xmalloc (sizeof (arelent));
+ rpp[1] = ba_rel = xmalloc (sizeof (arelent));
+ rpp[2] = ea_rel = xmalloc (sizeof (arelent));
+ rpp[3] = pea_rel = xmalloc (sizeof (arelent));
+ rpp[4] = 0;
+
+ /* Stick the toc reload instruction in the glue reloc. */
+ bfd_put_32(abfd, ppc_glue_insn, (char *) &imglue->address);
+
+ imglue->addend = 0;
+ imglue->howto = bfd_reloc_type_lookup (abfd,
+ BFD_RELOC_32_GOTOFF);
+ imglue->sym_ptr_ptr = fn_pp;
+
+ ba_rel->address = 0;
+ ba_rel->addend = 0;
+ ba_rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
+ ba_rel->sym_ptr_ptr = fn_pp;
+
+ bfd_put_32 (abfd, 0x18, si->data + 0x04);
+ ea_rel->address = 4;
+ ea_rel->addend = 0;
+ ea_rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
+ ea_rel->sym_ptr_ptr = fn_pp;
+
+ /* Mark it as glue. */
+ bfd_put_32 (abfd, 0x03, si->data + 0x0c);
+
+ /* Mark the prolog end address. */
+ bfd_put_32 (abfd, 0x0D, si->data + 0x10);
+ pea_rel->address = 0x10;
+ pea_rel->addend = 0;
+ pea_rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
+ pea_rel->sym_ptr_ptr = fn_pp;
+
+ sec->orelocation = rpp;
+ sec->reloc_count = 4;
+ break;
+ }
+ case RDATA:
+ /* Each external function in a PowerPC PE file has a two word
+ descriptor consisting of:
+ 1. The address of the code.
+ 2. The address of the appropriate .toc
+ We use relocs to build this. */
+ si->size = 8;
+ si->data = xmalloc (8);
+ memset (si->data, 0, si->size);
+
+ rpp = xmalloc (sizeof (arelent *) * 3);
+ rpp[0] = rel = xmalloc (sizeof (arelent));
+ rpp[1] = xmalloc (sizeof (arelent));
+ rpp[2] = 0;
+
+ rel->address = 0;
+ rel->addend = 0;
+ rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
+ rel->sym_ptr_ptr = fn_pp;
+
+ rel = rpp[1];
+
+ rel->address = 4;
+ rel->addend = 0;
+ rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
+ rel->sym_ptr_ptr = toc_pp;
+
+ sec->orelocation = rpp;
+ sec->reloc_count = 2;
+ break;
+#endif /* DLLTOOL_PPC */
+ }
+ }
+
+ {
+ bfd_vma vma = 0;
+ /* Size up all the sections. */
+ for (i = 0; i < NSECS; i++)
+ {
+ sinfo *si = secdata + i;
+
+ bfd_set_section_size (abfd, si->sec, si->size);
+ bfd_set_section_vma (abfd, si->sec, vma);
+ }
+ }
+ /* Write them out. */
+ for (i = 0; i < NSECS; i++)
+ {
+ sinfo *si = secdata + i;
+
+ if (i == IDATA5 && no_idata5)
+ continue;
+
+ if (i == IDATA4 && no_idata4)
+ continue;
+
+ bfd_set_section_contents (abfd, si->sec,
+ si->data, 0,
+ si->size);
+ }
+
+ bfd_set_symtab (abfd, ptrs, oidx);
+ bfd_close (abfd);
+ abfd = bfd_openr (outname, HOW_BFD_READ_TARGET);
+ if (!abfd)
+ /* xgettext:c-format */
+ fatal (_("bfd_open failed reopen stub file: %s: %s"),
+ outname, bfd_get_errmsg ());
+
+ return abfd;
+}
+
+static bfd *
+make_head (void)
+{
+ FILE *f = fopen (TMP_HEAD_S, FOPEN_WT);
+ bfd *abfd;
+
+ if (f == NULL)
+ {
+ fatal (_("failed to open temporary head file: %s"), TMP_HEAD_S);
+ return NULL;
+ }
+
+ temp_file_to_remove[TEMP_HEAD_FILE] = TMP_HEAD_S;
+
+ fprintf (f, "%s IMAGE_IMPORT_DESCRIPTOR\n", ASM_C);
+ fprintf (f, "\t.section\t.idata$2\n");
+
+ fprintf (f,"\t%s\t%s\n", ASM_GLOBAL, head_label);
+
+ fprintf (f, "%s:\n", head_label);
+
+ fprintf (f, "\t%shname%s\t%sPtr to image import by name list\n",
+ ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C);
+
+ fprintf (f, "\t%sthis should be the timestamp, but NT sometimes\n", ASM_C);
+ fprintf (f, "\t%sdoesn't load DLLs when this is set.\n", ASM_C);
+ fprintf (f, "\t%s\t0\t%s loaded time\n", ASM_LONG, ASM_C);
+ fprintf (f, "\t%s\t0\t%s Forwarder chain\n", ASM_LONG, ASM_C);
+ fprintf (f, "\t%s__%s_iname%s\t%s imported dll's name\n",
+ ASM_RVA_BEFORE,
+ imp_name_lab,
+ ASM_RVA_AFTER,
+ ASM_C);
+ fprintf (f, "\t%sfthunk%s\t%s pointer to firstthunk\n",
+ ASM_RVA_BEFORE,
+ ASM_RVA_AFTER, ASM_C);
+
+ fprintf (f, "%sStuff for compatibility\n", ASM_C);
+
+ if (!no_idata5)
+ {
+ fprintf (f, "\t.section\t.idata$5\n");
+ if (use_nul_prefixed_import_tables)
+ {
+ if (create_for_pep)
+ fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
+ else
+ fprintf (f,"\t%s\t0\n", ASM_LONG);
+ }
+ fprintf (f, "fthunk:\n");
+ }
+
+ if (!no_idata4)
+ {
+ fprintf (f, "\t.section\t.idata$4\n");
+ if (use_nul_prefixed_import_tables)
+ {
+ if (create_for_pep)
+ fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
+ else
+ fprintf (f,"\t%s\t0\n", ASM_LONG);
+ }
+ fprintf (f, "hname:\n");
+ }
+
+ fclose (f);
+
+ assemble_file (TMP_HEAD_S, TMP_HEAD_O);
+
+ abfd = bfd_openr (TMP_HEAD_O, HOW_BFD_READ_TARGET);
+ if (abfd == NULL)
+ /* xgettext:c-format */
+ fatal (_("failed to open temporary head file: %s: %s"),
+ TMP_HEAD_O, bfd_get_errmsg ());
+
+ temp_file_to_remove[TEMP_HEAD_O_FILE] = TMP_HEAD_O;
+ return abfd;
+}
+
+bfd *
+make_delay_head (void)
+{
+ FILE *f = fopen (TMP_HEAD_S, FOPEN_WT);
+ bfd *abfd;
+
+ if (f == NULL)
+ {
+ fatal (_("failed to open temporary head file: %s"), TMP_HEAD_S);
+ return NULL;
+ }
+
+ temp_file_to_remove[TEMP_HEAD_FILE] = TMP_HEAD_S;
+
+ /* Output the __tailMerge__xxx function */
+ fprintf (f, "%s Import trampoline\n", ASM_C);
+ fprintf (f, "\t.section\t.text\n");
+ fprintf(f,"\t%s\t%s\n", ASM_GLOBAL, head_label);
+ fprintf (f, "%s:\n", head_label);
+ fprintf (f, mtable[machine].trampoline, imp_name_lab);
+
+ /* Output the delay import descriptor */
+ fprintf (f, "\n%s DELAY_IMPORT_DESCRIPTOR\n", ASM_C);
+ fprintf (f, ".section\t.text$2\n");
+ fprintf (f,"%s __DELAY_IMPORT_DESCRIPTOR_%s\n", ASM_GLOBAL,imp_name_lab);
+ fprintf (f, "__DELAY_IMPORT_DESCRIPTOR_%s:\n", imp_name_lab);
+ fprintf (f, "\t%s 1\t%s grAttrs\n", ASM_LONG, ASM_C);
+ fprintf (f, "\t%s__%s_iname%s\t%s rvaDLLName\n",
+ ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C);
+ fprintf (f, "\t%s__DLL_HANDLE_%s%s\t%s rvaHmod\n",
+ ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C);
+ fprintf (f, "\t%s__IAT_%s%s\t%s rvaIAT\n",
+ ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C);
+ fprintf (f, "\t%s__INT_%s%s\t%s rvaINT\n",
+ ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C);
+ fprintf (f, "\t%s\t0\t%s rvaBoundIAT\n", ASM_LONG, ASM_C);
+ fprintf (f, "\t%s\t0\t%s rvaUnloadIAT\n", ASM_LONG, ASM_C);
+ fprintf (f, "\t%s\t0\t%s dwTimeStamp\n", ASM_LONG, ASM_C);
+
+ /* Output the dll_handle */
+ fprintf (f, "\n.section .data\n");
+ fprintf (f, "__DLL_HANDLE_%s:\n", imp_name_lab);
+ fprintf (f, "\t%s\t0\t%s Handle\n", ASM_LONG, ASM_C);
+ if (create_for_pep)
+ fprintf (f, "\t%s\t0\n", ASM_LONG);
+ fprintf (f, "\n");
+
+ fprintf (f, "%sStuff for compatibility\n", ASM_C);
+
+ if (!no_idata5)
+ {
+ fprintf (f, "\t.section\t.idata$5\n");
+ /* NULL terminating list. */
+ if (create_for_pep)
+ fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
+ else
+ fprintf (f,"\t%s\t0\n", ASM_LONG);
+ fprintf (f, "__IAT_%s:\n", imp_name_lab);
+ }
+
+ if (!no_idata4)
+ {
+ fprintf (f, "\t.section\t.idata$4\n");
+ fprintf (f, "\t%s\t0\n", ASM_LONG);
+ if (create_for_pep)
+ fprintf (f, "\t%s\t0\n", ASM_LONG);
+ fprintf (f, "\t.section\t.idata$4\n");
+ fprintf (f, "__INT_%s:\n", imp_name_lab);
+ }
+
+ fprintf (f, "\t.section\t.idata$2\n");
+
+ fclose (f);
+
+ assemble_file (TMP_HEAD_S, TMP_HEAD_O);
+
+ abfd = bfd_openr (TMP_HEAD_O, HOW_BFD_READ_TARGET);
+ if (abfd == NULL)
+ /* xgettext:c-format */
+ fatal (_("failed to open temporary head file: %s: %s"),
+ TMP_HEAD_O, bfd_get_errmsg ());
+
+ temp_file_to_remove[TEMP_HEAD_O_FILE] = TMP_HEAD_O;
+ return abfd;
+}
+
+static bfd *
+make_tail (void)
+{
+ FILE *f = fopen (TMP_TAIL_S, FOPEN_WT);
+ bfd *abfd;
+
+ if (f == NULL)
+ {
+ fatal (_("failed to open temporary tail file: %s"), TMP_TAIL_S);
+ return NULL;
+ }
+
+ temp_file_to_remove[TEMP_TAIL_FILE] = TMP_TAIL_S;
+
+ if (!no_idata4)
+ {
+ fprintf (f, "\t.section\t.idata$4\n");
+ if (create_for_pep)
+ fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
+ else
+ fprintf (f,"\t%s\t0\n", ASM_LONG); /* NULL terminating list. */
+ }
+
+ if (!no_idata5)
+ {
+ fprintf (f, "\t.section\t.idata$5\n");
+ if (create_for_pep)
+ fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
+ else
+ fprintf (f,"\t%s\t0\n", ASM_LONG); /* NULL terminating list. */
+ }
+
+#ifdef DLLTOOL_PPC
+ /* Normally, we need to see a null descriptor built in idata$3 to
+ act as the terminator for the list. The ideal way, I suppose,
+ would be to mark this section as a comdat type 2 section, so
+ only one would appear in the final .exe (if our linker supported
+ comdat, that is) or cause it to be inserted by something else (say
+ crt0). */
+
+ fprintf (f, "\t.section\t.idata$3\n");
+ fprintf (f, "\t%s\t0\n", ASM_LONG);
+ fprintf (f, "\t%s\t0\n", ASM_LONG);
+ fprintf (f, "\t%s\t0\n", ASM_LONG);
+ fprintf (f, "\t%s\t0\n", ASM_LONG);
+ fprintf (f, "\t%s\t0\n", ASM_LONG);
+#endif
+
+#ifdef DLLTOOL_PPC
+ /* Other PowerPC NT compilers use idata$6 for the dllname, so I
+ do too. Original, huh? */
+ fprintf (f, "\t.section\t.idata$6\n");
+#else
+ fprintf (f, "\t.section\t.idata$7\n");
+#endif
+
+ fprintf (f, "\t%s\t__%s_iname\n", ASM_GLOBAL, imp_name_lab);
+ fprintf (f, "__%s_iname:\t%s\t\"%s\"\n",
+ imp_name_lab, ASM_TEXT, dll_name);
+
+ fclose (f);
+
+ assemble_file (TMP_TAIL_S, TMP_TAIL_O);
+
+ abfd = bfd_openr (TMP_TAIL_O, HOW_BFD_READ_TARGET);
+ if (abfd == NULL)
+ /* xgettext:c-format */
+ fatal (_("failed to open temporary tail file: %s: %s"),
+ TMP_TAIL_O, bfd_get_errmsg ());
+
+ temp_file_to_remove[TEMP_TAIL_O_FILE] = TMP_TAIL_O;
+ return abfd;
+}
+
+static void
+gen_lib_file (int delay)
+{
+ int i;
+ export_type *exp;
+ bfd *ar_head;
+ bfd *ar_tail;
+ bfd *outarch;
+ bfd * head = 0;
+
+ unlink (imp_name);
+
+ outarch = bfd_openw (imp_name, HOW_BFD_WRITE_TARGET);
+
+ if (!outarch)
+ /* xgettext:c-format */
+ fatal (_("Can't create .lib file: %s: %s"),
+ imp_name, bfd_get_errmsg ());
+
+ /* xgettext:c-format */
+ inform (_("Creating library file: %s"), imp_name);
+
+ xatexit (unlink_temp_files);
+
+ bfd_set_format (outarch, bfd_archive);
+ outarch->has_armap = 1;
+ outarch->is_thin_archive = 0;
+
+ /* Work out a reasonable size of things to put onto one line. */
+ if (delay)
+ {
+ ar_head = make_delay_head ();
+ }
+ else
+ {
+ ar_head = make_head ();
+ }
+ ar_tail = make_tail();
+
+ if (ar_head == NULL || ar_tail == NULL)
+ return;
+
+ for (i = 0; (exp = d_exports_lexically[i]); i++)
+ {
+ bfd *n;
+ /* Don't add PRIVATE entries to import lib. */
+ if (exp->private)
+ continue;
+ n = make_one_lib_file (exp, i, delay);
+ n->archive_next = head;
+ head = n;
+ if (ext_prefix_alias)
+ {
+ export_type alias_exp;
+
+ assert (i < PREFIX_ALIAS_BASE);
+ alias_exp.name = make_imp_label (ext_prefix_alias, exp->name);
+ alias_exp.internal_name = exp->internal_name;
+ alias_exp.its_name = exp->its_name;
+ alias_exp.import_name = exp->name;
+ alias_exp.ordinal = exp->ordinal;
+ alias_exp.constant = exp->constant;
+ alias_exp.noname = exp->noname;
+ alias_exp.private = exp->private;
+ alias_exp.data = exp->data;
+ alias_exp.hint = exp->hint;
+ alias_exp.forward = exp->forward;
+ alias_exp.next = exp->next;
+ n = make_one_lib_file (&alias_exp, i + PREFIX_ALIAS_BASE, delay);
+ n->archive_next = head;
+ head = n;
+ }
+ }
+
+ /* 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))
+ bfd_fatal ("bfd_set_archive_head");
+
+ if (! bfd_close (outarch))
+ bfd_fatal (imp_name);
+
+ while (head != NULL)
+ {
+ bfd *n = head->archive_next;
+ bfd_close (head);
+ head = n;
+ }
+
+ /* Delete all the temp files. */
+ unlink_temp_files ();
+
+ if (dontdeltemps < 2)
+ {
+ char *name;
+
+ name = xmalloc (strlen (TMP_STUB) + 10);
+ for (i = 0; (exp = d_exports_lexically[i]); i++)
+ {
+ /* Don't delete non-existent stubs for PRIVATE entries. */
+ if (exp->private)
+ continue;
+ sprintf (name, "%s%05d.o", TMP_STUB, i);
+ if (unlink (name) < 0)
+ /* xgettext:c-format */
+ non_fatal (_("cannot delete %s: %s"), name, strerror (errno));
+ if (ext_prefix_alias)
+ {
+ sprintf (name, "%s%05d.o", TMP_STUB, i + PREFIX_ALIAS_BASE);
+ if (unlink (name) < 0)
+ /* xgettext:c-format */
+ non_fatal (_("cannot delete %s: %s"), name, strerror (errno));
+ }
+ }
+ free (name);
+ }
+
+ inform (_("Created lib file"));
+}
+
+/* Append a copy of data (cast to char *) to list. */
+
+static void
+dll_name_list_append (dll_name_list_type * list, bfd_byte * data)
+{
+ dll_name_list_node_type * entry;
+
+ /* Error checking. */
+ if (! list || ! list->tail)
+ return;
+
+ /* Allocate new node. */
+ entry = ((dll_name_list_node_type *)
+ xmalloc (sizeof (dll_name_list_node_type)));
+
+ /* Initialize its values. */
+ entry->dllname = xstrdup ((char *) data);
+ entry->next = NULL;
+
+ /* Add to tail, and move tail. */
+ list->tail->next = entry;
+ list->tail = entry;
+}
+
+/* Count the number of entries in list. */
+
+static int
+dll_name_list_count (dll_name_list_type * list)
+{
+ dll_name_list_node_type * p;
+ int count = 0;
+
+ /* Error checking. */
+ if (! list || ! list->head)
+ return 0;
+
+ p = list->head;
+
+ while (p && p->next)
+ {
+ count++;
+ p = p->next;
+ }
+ return count;
+}
+
+/* Print each entry in list to stdout. */
+
+static void
+dll_name_list_print (dll_name_list_type * list)
+{
+ dll_name_list_node_type * p;
+
+ /* Error checking. */
+ if (! list || ! list->head)
+ return;
+
+ p = list->head;
+
+ while (p && p->next && p->next->dllname && *(p->next->dllname))
+ {
+ printf ("%s\n", p->next->dllname);
+ p = p->next;
+ }
+}
+
+/* Free all entries in list, and list itself. */
+
+static void
+dll_name_list_free (dll_name_list_type * list)
+{
+ if (list)
+ {
+ dll_name_list_free_contents (list->head);
+ list->head = NULL;
+ list->tail = NULL;
+ free (list);
+ }
+}
+
+/* Recursive function to free all nodes entry->next->next...
+ as well as entry itself. */
+
+static void
+dll_name_list_free_contents (dll_name_list_node_type * entry)
+{
+ if (entry)
+ {
+ if (entry->next)
+ {
+ dll_name_list_free_contents (entry->next);
+ entry->next = NULL;
+ }
+ if (entry->dllname)
+ {
+ free (entry->dllname);
+ entry->dllname = NULL;
+ }
+ free (entry);
+ }
+}
+
+/* Allocate and initialize a dll_name_list_type object,
+ including its sentinel node. Caller is responsible
+ for calling dll_name_list_free when finished with
+ the list. */
+
+static dll_name_list_type *
+dll_name_list_create (void)
+{
+ /* Allocate list. */
+ dll_name_list_type * list = xmalloc (sizeof (dll_name_list_type));
+
+ /* Allocate and initialize sentinel node. */
+ list->head = xmalloc (sizeof (dll_name_list_node_type));
+ list->head->dllname = NULL;
+ list->head->next = NULL;
+
+ /* Bookkeeping for empty list. */
+ list->tail = list->head;
+
+ return list;
+}
+
+/* Search the symbol table of the suppled BFD for a symbol whose name matches
+ OBJ (where obj is cast to const char *). If found, set global variable
+ identify_member_contains_symname_result TRUE. It is the caller's
+ responsibility to set the result variable FALSE before iterating with
+ this function. */
+
+static void
+identify_member_contains_symname (bfd * abfd,
+ bfd * archive_bfd ATTRIBUTE_UNUSED,
+ void * obj)
+{
+ long storage_needed;
+ asymbol ** symbol_table;
+ long number_of_symbols;
+ long i;
+ symname_search_data_type * search_data = (symname_search_data_type *) obj;
+
+ /* If we already found the symbol in a different member,
+ short circuit. */
+ if (search_data->found)
+ return;
+
+ storage_needed = bfd_get_symtab_upper_bound (abfd);
+ if (storage_needed <= 0)
+ return;
+
+ symbol_table = xmalloc (storage_needed);
+ number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
+ if (number_of_symbols < 0)
+ {
+ free (symbol_table);
+ return;
+ }
+
+ for (i = 0; i < number_of_symbols; i++)
+ {
+ if (strncmp (symbol_table[i]->name,
+ search_data->symname,
+ strlen (search_data->symname)) == 0)
+ {
+ search_data->found = TRUE;
+ break;
+ }
+ }
+ free (symbol_table);
+}
+
+/* This is the main implementation for the --identify option.
+ Given the name of an import library in identify_imp_name, first determine
+ if the import library is a GNU binutils-style one (where the DLL name is
+ stored in an .idata$7 (.idata$6 on PPC) section, or if it is a MS-style
+ one (where the DLL name, along with much other data, is stored in the
+ .idata$6 section). We determine the style of import library by searching
+ for the DLL-structure symbol inserted by MS tools:
+ __NULL_IMPORT_DESCRIPTOR.
+
+ Once we know which section to search, evaluate each section for the
+ appropriate properties that indicate it may contain the name of the
+ associated DLL (this differs depending on the style). Add the contents
+ of all sections which meet the criteria to a linked list of dll names.
+
+ Finally, print them all to stdout. (If --identify-strict, an error is
+ reported if more than one match was found). */
+
+static void
+identify_dll_for_implib (void)
+{
+ bfd * abfd = NULL;
+ int count = 0;
+ identify_data_type identify_data;
+ symname_search_data_type search_data;
+
+ /* Initialize identify_data. */
+ identify_data.list = dll_name_list_create ();
+ identify_data.ms_style_implib = FALSE;
+
+ /* Initialize search_data. */
+ search_data.symname = "__NULL_IMPORT_DESCRIPTOR";
+ search_data.found = FALSE;
+
+ bfd_init ();
+
+ abfd = bfd_openr (identify_imp_name, 0);
+ if (abfd == NULL)
+ /* xgettext:c-format */
+ fatal (_("Can't open .lib file: %s: %s"),
+ identify_imp_name, bfd_get_errmsg ());
+
+ if (! bfd_check_format (abfd, bfd_archive))
+ {
+ if (! bfd_close (abfd))
+ bfd_fatal (identify_imp_name);
+
+ fatal (_("%s is not a library"), identify_imp_name);
+ }
+
+ /* Detect if this a Microsoft import library. */
+ identify_search_archive (abfd,
+ identify_member_contains_symname,
+ (void *)(& search_data));
+ if (search_data.found)
+ identify_data.ms_style_implib = TRUE;
+
+ /* Rewind the bfd. */
+ if (! bfd_close (abfd))
+ bfd_fatal (identify_imp_name);
+ abfd = bfd_openr (identify_imp_name, 0);
+ if (abfd == NULL)
+ bfd_fatal (identify_imp_name);
+
+ if (!bfd_check_format (abfd, bfd_archive))
+ {
+ if (!bfd_close (abfd))
+ bfd_fatal (identify_imp_name);
+
+ fatal (_("%s is not a library"), identify_imp_name);
+ }
+
+ /* Now search for the dll name. */
+ identify_search_archive (abfd,
+ identify_search_member,
+ (void *)(& identify_data));
+
+ if (! bfd_close (abfd))
+ bfd_fatal (identify_imp_name);
+
+ count = dll_name_list_count (identify_data.list);
+ if (count > 0)
+ {
+ if (identify_strict && count > 1)
+ {
+ dll_name_list_free (identify_data.list);
+ identify_data.list = NULL;
+ fatal (_("Import library `%s' specifies two or more dlls"),
+ identify_imp_name);
+ }
+ dll_name_list_print (identify_data.list);
+ dll_name_list_free (identify_data.list);
+ identify_data.list = NULL;
+ }
+ else
+ {
+ dll_name_list_free (identify_data.list);
+ identify_data.list = NULL;
+ fatal (_("Unable to determine dll name for `%s' (not an import library?)"),
+ identify_imp_name);
+ }
+}
+
+/* Loop over all members of the archive, applying the supplied function to
+ each member that is a bfd_object. The function will be called as if:
+ func (member_bfd, abfd, user_storage) */
+
+static void
+identify_search_archive (bfd * abfd,
+ void (* operation) (bfd *, bfd *, void *),
+ void * user_storage)
+{
+ bfd * arfile = NULL;
+ bfd * last_arfile = NULL;
+ char ** matching;
+
+ while (1)
+ {
+ arfile = bfd_openr_next_archived_file (abfd, arfile);
+
+ if (arfile == NULL)
+ {
+ if (bfd_get_error () != bfd_error_no_more_archived_files)
+ bfd_fatal (bfd_get_filename (abfd));
+ break;
+ }
+
+ if (bfd_check_format_matches (arfile, bfd_object, &matching))
+ (*operation) (arfile, abfd, user_storage);
+ else
+ {
+ bfd_nonfatal (bfd_get_filename (arfile));
+ free (matching);
+ }
+
+ if (last_arfile != NULL)
+ {
+ bfd_close (last_arfile);
+ /* PR 17512: file: 8b2168d4. */
+ if (last_arfile == arfile)
+ {
+ last_arfile = NULL;
+ break;
+ }
+ }
+
+ last_arfile = arfile;
+ }
+
+ if (last_arfile != NULL)
+ {
+ bfd_close (last_arfile);
+ }
+}
+
+/* Call the identify_search_section() function for each section of this
+ archive member. */
+
+static void
+identify_search_member (bfd *abfd,
+ bfd *archive_bfd ATTRIBUTE_UNUSED,
+ void *obj)
+{
+ bfd_map_over_sections (abfd, identify_search_section, obj);
+}