* Makefile.am (BFD32_BACKENDS): Remove elf32-score and
[deliverable/binutils-gdb.git] / binutils / dlltool.c
index 9d1d6f2157165f26320886c5bf7a3723b387a7a9..a24a55255ff6ec496874f8ee1fe9db5933d5da51 100644 (file)
@@ -1,12 +1,12 @@
 /* dlltool.c -- tool to generate stuff for PE style DLLs
-   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
-   Free Software Foundation, Inc.
+   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
+   2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
 
    This file is part of GNU Binutils.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
@@ -16,8 +16,8 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.  */
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+   02110-1301, USA.  */
 
 
 /* This program allows you to build the files necessary to create
@@ -57,7 +57,7 @@
 
    IMPORTS  (  (   <internal-name> =   <module-name> . <integer> )
              | ( [ <internal-name> = ] <module-name> . <external-name> )) *
-   Declares that <external-name> or the exported function whoes ordinal number
+   Declares that <external-name> or the exported function whose ordinal number
    is <integer> is to be imported from the file <module-name>.  If
    <internal-name> is specified then this is the name that the imported
    function will be refereed to in the body of the DLL.
 
 #define show_allnames 0
 
-#define PAGE_SIZE 4096
-#define PAGE_MASK (-PAGE_SIZE)
+#define PAGE_SIZE ((bfd_vma) 4096)
+#define PAGE_MASK ((bfd_vma) (-4096))
+#include "sysdep.h"
 #include "bfd.h"
 #include "libiberty.h"
-#include "bucomm.h"
 #include "getopt.h"
 #include "demangle.h"
 #include "dyn-string.h"
+#include "bucomm.h"
 #include "dlltool.h"
 #include "safe-ctype.h"
 
 #include <time.h>
 #include <sys/stat.h>
-
-#ifdef ANSI_PROTOTYPES
 #include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
+#include <assert.h>
 
 #ifdef DLLTOOL_ARM
 #include "coff/arm.h"
 #include "coff/internal.h"
 #endif
+#ifdef DLLTOOL_MX86_64
+#include "coff/x86_64.h"
+#endif
 
 /* Forward references.  */
 static char *look_for_prog (const char *, const char *, int);
 static char *deduce_name (const char *);
 
 #ifdef DLLTOOL_MCORE_ELF
-static void mcore_elf_cache_filename (char *);
+static void mcore_elf_cache_filename (const char *);
 static void mcore_elf_gen_out_file (void);
 #endif
 
@@ -352,12 +352,44 @@ static int no_idata4;
 static int no_idata5;
 static char *exp_name;
 static char *imp_name;
+static char *identify_imp_name;
+static bfd_boolean identify_strict;
+
+/* Types used to implement a linked list of dllnames associated
+   with the specified import lib. Used by the identify_* code.
+   The head entry is acts as a sentinal node and is always empty
+   (head->dllname is NULL).  */
+typedef struct dll_name_list_node_t
+{
+  char *                        dllname;
+  struct dll_name_list_node_t * next;
+} dll_name_list_node_type;
+typedef struct dll_name_list_t
+{
+  dll_name_list_node_type * head;
+  dll_name_list_node_type * tail;
+} dll_name_list_type; 
+
+/* Types used to pass data to iterator functions.  */
+typedef struct symname_search_data_t
+{
+  const char * symname;
+  bfd_boolean  found;
+} symname_search_data_type;
+typedef struct identify_data_t
+{
+   dll_name_list_type * list;
+   bfd_boolean          ms_style_implib;
+} identify_data_type; 
+
+
 static char *head_label;
 static char *imp_name_lab;
 static char *dll_name;
 
 static int add_indirect = 0;
 static int add_underscore = 0;
+static int add_stdcall_underscore = 0;
 static int dontdeltemps = 0;
 
 /* TRUE if we should export all symbols.  Otherwise, we only export
@@ -368,6 +400,8 @@ static bfd_boolean export_all_symbols;
    exporting all symbols.  */
 static bfd_boolean do_default_excludes = TRUE;
 
+static bfd_boolean use_nul_prefixed_import_tables = FALSE;
+
 /* Default symbols to exclude when exporting all the symbols.  */
 static const char *default_excludes = "DllMain@12,DllEntryPoint@0,impure_ptr";
 
@@ -375,6 +409,9 @@ static const char *default_excludes = "DllMain@12,DllEntryPoint@0,impure_ptr";
    compatibility to old Cygwin releases.  */
 static bfd_boolean create_compat_implib;
 
+/* TRUE if we have to write PE+ import libraries.  */
+static bfd_boolean create_for_pep;
+
 static char *def_file;
 
 extern char * program_name;
@@ -387,35 +424,43 @@ static int verbose;
 static FILE *output_def;
 static FILE *base_file;
 
-#ifdef DLLTOOL_ARM
-#ifdef DLLTOOL_ARM_EPOC
-static const char *mname = "arm-epoc";
-#else
+#ifdef DLLTOOL_DEFAULT_ARM
 static const char *mname = "arm";
 #endif
+
+#ifdef DLLTOOL_DEFAULT_ARM_EPOC
+static const char *mname = "arm-epoc";
 #endif
 
-#ifdef DLLTOOL_I386
+#ifdef DLLTOOL_DEFAULT_ARM_WINCE
+static const char *mname = "arm-wince";
+#endif
+
+#ifdef DLLTOOL_DEFAULT_I386
 static const char *mname = "i386";
 #endif
 
-#ifdef DLLTOOL_PPC
+#ifdef DLLTOOL_DEFAULT_MX86_64
+static const char *mname = "i386:x86-64";
+#endif
+
+#ifdef DLLTOOL_DEFAULT_PPC
 static const char *mname = "ppc";
 #endif
 
-#ifdef DLLTOOL_SH
+#ifdef DLLTOOL_DEFAULT_SH
 static const char *mname = "sh";
 #endif
 
-#ifdef DLLTOOL_MIPS
+#ifdef DLLTOOL_DEFAULT_MIPS
 static const char *mname = "mips";
 #endif
 
-#ifdef DLLTOOL_MCORE
+#ifdef DLLTOOL_DEFAULT_MCORE
 static const char * mname = "mcore-le";
 #endif
 
-#ifdef DLLTOOL_MCORE_ELF
+#ifdef DLLTOOL_DEFAULT_MCORE_ELF
 static const char * mname = "mcore-elf";
 static char * mcore_elf_out_file = NULL;
 static char * mcore_elf_linker   = NULL;
@@ -428,7 +473,11 @@ static char * mcore_elf_linker_flags = NULL;
 #define DRECTVE_SECTION_NAME ".drectve"
 #endif
 
-#define PATHMAX 250            /* What's the right name for this ?  */
+/* What's the right name for this ?  */
+#define PATHMAX 250            
+
+/* External name alias numbering starts here.  */
+#define PREFIX_ALIAS_BASE      20000
 
 char *tmp_asm_buf;
 char *tmp_head_s_buf;
@@ -628,6 +677,23 @@ mtable[] =
     arm_jtab, sizeof (arm_jtab), 8
   }
   ,
+  {
+#define MARM_WINCE 10
+    "arm-wince", ".byte", ".short", ".long", ".asciz", "@",
+    "ldr\tip,[pc]\n\tldr\tpc,[ip]\n\t.long",
+    ".global", ".space", ".align\t2",".align\t4", "-mapcs-32",
+    "pe-arm-wince-little", bfd_arch_arm,
+    arm_jtab, sizeof (arm_jtab), 8
+  }
+  ,
+  {
+#define MX86 11
+    "i386:x86-64", ".byte", ".short", ".long", ".asciz", "#",
+    "jmp *", ".global", ".space", ".align\t2",".align\t4", "",
+    "pe-x86-64",bfd_arch_i386,
+    i386_jtab, sizeof (i386_jtab), 2
+  }
+  ,
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
 };
 
@@ -642,6 +708,7 @@ typedef struct export
   {
     const char *name;
     const char *internal_name;
+    const char *import_name;
     int ordinal;
     int constant;
     int noname;                /* Don't put name in image file.  */
@@ -681,31 +748,41 @@ static void scan_open_obj_file (bfd *);
 static void scan_obj_file (const char *);
 static void dump_def_info (FILE *);
 static int sfunc (const void *, const void *);
-static void flush_page (FILE *, long *, int, int);
+static void flush_page (FILE *, bfd_vma *, bfd_vma, int);
 static void gen_def_file (void);
 static void generate_idata_ofile (FILE *);
 static void assemble_file (const char *, const char *);
 static void gen_exp_file (void);
 static const char *xlate (const char *);
-#if 0
-static void dump_iat (FILE *, export_type *);
-#endif
 static char *make_label (const char *, const char *);
 static char *make_imp_label (const char *, const char *);
 static bfd *make_one_lib_file (export_type *, int);
 static bfd *make_head (void);
 static bfd *make_tail (void);
 static void gen_lib_file (void);
+static void dll_name_list_append (dll_name_list_type *, bfd_byte *);
+static int  dll_name_list_count (dll_name_list_type *);
+static void dll_name_list_print (dll_name_list_type *);
+static void dll_name_list_free_contents (dll_name_list_node_type *);
+static void dll_name_list_free (dll_name_list_type *);
+static dll_name_list_type * dll_name_list_create (void);
+static void identify_dll_for_implib (void);
+static void identify_search_archive
+  (bfd *, void (*) (bfd *, bfd *, void *),  void *);
+static void identify_search_member (bfd *, bfd *, void *);
+static bfd_boolean identify_process_section_p (asection *, bfd_boolean);
+static void identify_search_section (bfd *, asection *, void *);
+static void identify_member_contains_symname (bfd *, bfd  *, void *);
+
 static int pfunc (const void *, const void *);
 static int nfunc (const void *, const void *);
 static void remove_null_names (export_type **);
-static void dtab (export_type **);
 static void process_duplicates (export_type **);
 static void fill_ordinals (export_type **);
-static int alphafunc (const void *, const void *);
 static void mangle_defs (void);
 static void usage (FILE *, int);
-static void inform (const char *, ...);
+static void inform (const char *, ...) ATTRIBUTE_PRINTF_1;
+static void set_dll_name_from_def (const char *);
 
 static char *
 prefix_encode (char *start, unsigned code)
@@ -754,6 +831,7 @@ rvaafter (int machine)
     {
     case MARM:
     case M386:
+    case MX86:
     case MPPC:
     case MTHUMB:
     case MARM_INTERWORK:
@@ -762,6 +840,7 @@ rvaafter (int machine)
     case MMCORE_ELF:
     case MMCORE_ELF_LE:
     case MARM_EPOC:
+    case MARM_WINCE:
       break;
     default:
       /* xgettext:c-format */
@@ -778,6 +857,7 @@ rvabefore (int machine)
     {
     case MARM:
     case M386:
+    case MX86:
     case MPPC:
     case MTHUMB:
     case MARM_INTERWORK:
@@ -786,6 +866,7 @@ rvabefore (int machine)
     case MMCORE_ELF:
     case MMCORE_ELF_LE:
     case MARM_EPOC:
+    case MARM_WINCE:
       return ".rva\t";
     default:
       /* xgettext:c-format */
@@ -809,8 +890,10 @@ asm_prefix (int machine, const char *name)
     case MMCORE_ELF:
     case MMCORE_ELF_LE:
     case MARM_EPOC:
+    case MARM_WINCE:
       break;
     case M386:
+    case MX86:
       /* Symbol names starting with ? do not have a leading underscore. */
       if (name && *name == '?')
         break;
@@ -870,7 +953,6 @@ process_def_file (const char *name)
 
 /* Communications with the parser.  */
 
-static const char *d_name;     /* Arg to NAME or LIBRARY.  */
 static int d_nfuncs;           /* Number of functions exported.  */
 static int d_named_nfuncs;     /* Number of named functions exported.  */
 static int d_low_ord;          /* Lowest ordinal index.  */
@@ -901,6 +983,7 @@ def_exports (const char *name, const char *internal_name, int ordinal,
 
   p->name = name;
   p->internal_name = internal_name ? internal_name : name;
+  p->import_name = name;
   p->ordinal = ordinal;
   p->constant = constant;
   p->noname = noname;
@@ -917,6 +1000,16 @@ def_exports (const char *name, const char *internal_name, int ordinal,
     p->forward = 0; /* no forward */
 }
 
+static void
+set_dll_name_from_def (const char * name)
+{
+  const char* image_basename = lbasename (name);
+  if (image_basename != name)
+    non_fatal (_("%s: Path components stripped from image name, '%s'."),
+             def_file, name);
+  dll_name = xstrdup (image_basename);
+}
+
 void
 def_name (const char *name, int base)
 {
@@ -926,11 +1019,10 @@ def_name (const char *name, int base)
   if (d_is_dll)
     non_fatal (_("Can't have LIBRARY and NAME"));
 
-  d_name = name;
   /* If --dllname not provided, use the one in the DEF file.
      FIXME: Is this appropriate for executables?  */
   if (! dll_name)
-    dll_name = xstrdup (name);
+    set_dll_name_from_def (name);
   d_is_exe = 1;
 }
 
@@ -943,10 +1035,9 @@ def_library (const char *name, int base)
   if (d_is_exe)
     non_fatal (_("Can't have LIBRARY and NAME"));
 
-  d_name = name;
   /* If --dllname not provided, use the one in the DEF file.  */
   if (! dll_name)
-    dll_name = xstrdup (name);
+    set_dll_name_from_def (name);
   d_is_dll = 1;
 }
 
@@ -1165,7 +1256,7 @@ run (const char *what, char *args)
 
   if (pid == -1)
     {
-      inform (strerror (errno));
+      inform ("%s", strerror (errno));
 
       fatal (errmsg_fmt, errmsg_arg);
     }
@@ -1228,7 +1319,7 @@ scan_drectve_symbols (bfd *abfd)
   while (p < e)
     {
       if (p[0] == '-'
-         && strncmp (p, "-export:", 8) == 0)
+         && CONST_STRNEQ (p, "-export:"))
        {
          char * name;
          char * c;
@@ -1246,7 +1337,7 @@ scan_drectve_symbols (bfd *abfd)
              char *tag_start = ++p;
              while (p < e && *p != ' ' && *p != '-')
                p++;
-             if (strncmp (tag_start, "data", 4) == 0)
+             if (CONST_STRNEQ (tag_start, "data"))
                flags &= ~BSF_FUNCTION;
            }
 
@@ -1505,7 +1596,7 @@ scan_obj_file (const char *filename)
 
 #ifdef DLLTOOL_MCORE_ELF
       if (mcore_elf_out_file)
-       mcore_elf_cache_filename ((char *) filename);
+       mcore_elf_cache_filename (filename);
 #endif
     }
 
@@ -1543,18 +1634,21 @@ dump_def_info (FILE *f)
 static int
 sfunc (const void *a, const void *b)
 {
-  return *(const long *) a - *(const long *) b;
+  if (*(const bfd_vma *) a == *(const bfd_vma *) b)
+    return 0;
+
+  return ((*(const bfd_vma *) a > *(const bfd_vma *) b) ? 1 : -1);
 }
 
 static void
-flush_page (FILE *f, long *need, int page_addr, int on_page)
+flush_page (FILE *f, bfd_vma *need, bfd_vma page_addr, int on_page)
 {
   int i;
 
   /* Flush this page.  */
   fprintf (f, "\t%s\t0x%08x\t%s Starting RVA for chunk\n",
           ASM_LONG,
-          page_addr,
+          (int) page_addr,
           ASM_C);
   fprintf (f, "\t%s\t0x%x\t%s Size of block\n",
           ASM_LONG,
@@ -1563,12 +1657,23 @@ flush_page (FILE *f, long *need, int page_addr, int on_page)
 
   for (i = 0; i < on_page; i++)
     {
-      long needed = need[i];
+      bfd_vma needed = need[i];
 
       if (needed)
-       needed = ((needed - page_addr) | 0x3000) & 0xffff;
+        {
+         if (!create_for_pep)
+           {
+             /* Relocation via HIGHLOW.  */
+             needed = ((needed - page_addr) | 0x3000) & 0xffff;
+           }
+         else
+           {
+             /* Relocation via DIR64.  */
+             needed = ((needed - page_addr) | 0xa000) & 0xffff;
+           }
+       }
 
-      fprintf (f, "\t%s\t0x%lx\n", ASM_SHORT, needed);
+      fprintf (f, "\t%s\t0x%lx\n", ASM_SHORT, (long) needed);
     }
 
   /* And padding */
@@ -1680,10 +1785,20 @@ generate_idata_ofile (FILE *filvar)
   for (headptr = import_list; headptr != NULL; headptr = headptr->next)
     {
       fprintf (filvar, "listone%d:\n", headindex);
-      for ( funcindex = 0; funcindex < headptr->nfuncs; funcindex++ )
-       fprintf (filvar, "\t%sfuncptr%d_%d%s\n",
-                ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER);
-      fprintf (filvar,"\t%s\t0\n", ASM_LONG); /* NULL terminating list */
+      for (funcindex = 0; funcindex < headptr->nfuncs; funcindex++)
+        {
+         if (create_for_pep)
+           fprintf (filvar, "\t%sfuncptr%d_%d%s\n%s\t0\n",
+                    ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER,
+                    ASM_LONG);
+         else
+           fprintf (filvar, "\t%sfuncptr%d_%d%s\n",
+                    ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER);
+        }
+      if (create_for_pep)
+       fprintf (filvar, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
+      else
+       fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL terminating list.  */
       headindex++;
     }
 
@@ -1692,10 +1807,20 @@ generate_idata_ofile (FILE *filvar)
   for (headptr = import_list; headptr != NULL; headptr = headptr->next)
     {
       fprintf (filvar, "listtwo%d:\n", headindex);
-      for ( funcindex = 0; funcindex < headptr->nfuncs; funcindex++ )
-       fprintf (filvar, "\t%sfuncptr%d_%d%s\n",
-                ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER);
-      fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL terminating list */
+      for (funcindex = 0; funcindex < headptr->nfuncs; funcindex++)
+        {
+         if (create_for_pep)
+           fprintf (filvar, "\t%sfuncptr%d_%d%s\n%s\t0\n",
+                    ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER,
+                    ASM_LONG);
+         else
+           fprintf (filvar, "\t%sfuncptr%d_%d%s\n",
+                    ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER);
+        }
+      if (create_for_pep)
+       fprintf (filvar, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
+      else
+       fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL terminating list.  */
       headindex++;
     }
 
@@ -1767,8 +1892,8 @@ gen_exp_file (void)
     {
       fprintf (f, "\t.section  .edata\n\n");
       fprintf (f, "\t%s        0       %s Allways 0\n", ASM_LONG, ASM_C);
-      fprintf (f, "\t%s        0x%lx   %s Time and date\n", ASM_LONG, (long) time(0),
-              ASM_C);
+      fprintf (f, "\t%s        0x%lx   %s Time and date\n", ASM_LONG,
+              (unsigned long) time(0), ASM_C);
       fprintf (f, "\t%s        0       %s Major and Minor version\n", ASM_LONG, ASM_C);
       fprintf (f, "\t%sname%s  %s Ptr to name of dll\n", ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C);
       fprintf (f, "\t%s        %d      %s Starting ordinal of exports\n", ASM_LONG, d_low_ord, ASM_C);
@@ -1799,14 +1924,6 @@ gen_exp_file (void)
        {
          if (exp->ordinal != i)
            {
-#if 0
-             fprintf (f, "\t%s\t%d\t%s %d..%d missing\n",
-                      ASM_SPACE,
-                      (exp->ordinal - i) * 4,
-                      ASM_C,
-                      i, exp->ordinal - 1);
-             i = exp->ordinal;
-#endif
              while (i < exp->ordinal)
                {
                  fprintf(f,"\t%s\t0\n", ASM_LONG);
@@ -1840,7 +1957,7 @@ gen_exp_file (void)
                     ASM_RVA_BEFORE, exp->ordinal, ASM_RVA_AFTER);
        }
 
-      fprintf (f,"%s Export Oridinal Table\n", ASM_C);
+      fprintf (f,"%s Export Ordinal Table\n", ASM_C);
       fprintf (f, "anords:\n");
       for (i = 0; (exp = d_exports_lexically[i]); i++)
        {
@@ -1926,12 +2043,12 @@ gen_exp_file (void)
   /* Dump the reloc section if a base file is provided.  */
   if (base_file)
     {
-      int addr;
-      long need[PAGE_SIZE];
-      long page_addr;
-      int numbytes;
+      bfd_vma addr;
+      bfd_vma need[PAGE_SIZE];
+      bfd_vma page_addr;
+      bfd_size_type numbytes;
       int num_entries;
-      long *copy;
+      bfd_vma *copy;
       int j;
       int on_page;
       fprintf (f, "\t.section\t.init\n");
@@ -1941,8 +2058,9 @@ gen_exp_file (void)
       numbytes = ftell (base_file);
       fseek (base_file, 0, SEEK_SET);
       copy = xmalloc (numbytes);
-      fread (copy, 1, numbytes, base_file);
-      num_entries = numbytes / sizeof (long);
+      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");
@@ -1950,8 +2068,8 @@ gen_exp_file (void)
        {
          int src;
          int dst = 0;
-         int last = -1;
-         qsort (copy, num_entries, sizeof (long), sfunc);
+         bfd_vma last = (bfd_vma) -1;
+         qsort (copy, num_entries, sizeof (bfd_vma), sfunc);
          /* Delete duplicates */
          for (src = 0; src < num_entries; src++)
            {
@@ -1997,7 +2115,9 @@ xlate (const char *name)
 {
   int lead_at = (*name == '@');
 
-  if (add_underscore &&  !lead_at)
+  if (!lead_at && (add_underscore
+                  || (add_stdcall_underscore
+                      && strchr (name, '@'))))
     {
       char *copy = xmalloc (strlen (name) + 2);
 
@@ -2011,36 +2131,14 @@ xlate (const char *name)
       char *p;
 
       name += lead_at;
-      p = strchr (name, '@');
-      if (p)
+      /* PR 9766: Look for the last @ sign in the name.  */
+      p = strrchr (name, '@');
+      if (p && ISDIGIT (p[1]))
        *p = 0;
     }
   return name;
 }
 
-/**********************************************************************/
-
-#if 0
-
-static void
-dump_iat (FILE *f, export_type *exp)
-{
-  if (exp->noname && !show_allnames )
-    {
-      fprintf (f, "\t%s\t0x%08x\n",
-              ASM_LONG,
-              exp->ordinal | 0x80000000); /* hint or orindal ?? */
-    }
-  else
-    {
-      fprintf (f, "\t%sID%d%s\n", ASM_RVA_BEFORE,
-              exp->ordinal,
-              ASM_RVA_AFTER);
-    }
-}
-
-#endif
-
 typedef struct
 {
   int id;
@@ -2191,310 +2289,262 @@ make_imp_label (const char *prefix, const char *name)
 static bfd *
 make_one_lib_file (export_type *exp, int i)
 {
-#if 0
-    {
-      char *name;
-      FILE *f;
-      const char *prefix = "d";
-      char *dest;
-
-      name = (char *) alloca (strlen (prefix) + 10);
-      sprintf (name, "%ss%05d.s", prefix, i);
-      f = fopen (name, FOPEN_WT);
-      fprintf (f, "\t.text\n");
-      fprintf (f, "\t%s\t%s%s\n", ASM_GLOBAL, ASM_PREFIX (exp->name), exp->name);
-      if (create_compat_implib)
-       fprintf (f, "\t%s\t__imp_%s\n", ASM_GLOBAL, exp->name);
-      fprintf (f, "\t%s\t_imp__%s\n", ASM_GLOBAL, exp->name);
-      if (create_compat_implib)
-       fprintf (f, "%s%s:\n\t%s\t__imp_%s\n", ASM_PREFIX (exp->name),
-                exp->name, ASM_JUMP, exp->name);
-
-      fprintf (f, "\t.section\t.idata$7\t%s To force loading of head\n", ASM_C);
-      fprintf (f, "\t%s\t%s\n", ASM_LONG, head_label);
-
-
-      fprintf (f,"%s Import Address Table\n", ASM_C);
-
-      fprintf (f, "\t.section  .idata$5\n");
-      if (create_compat_implib)
-       fprintf (f, "__imp_%s:\n", exp->name);
-      fprintf (f, "_imp__%s:\n", exp->name);
-
-      dump_iat (f, exp);
-
-      fprintf (f, "\n%s Import Lookup Table\n", ASM_C);
-      fprintf (f, "\t.section  .idata$4\n");
-
-      dump_iat (f, exp);
-
-      if(!exp->noname || show_allnames)
-       {
-         fprintf (f, "%s Hint/Name table\n", ASM_C);
-         fprintf (f, "\t.section       .idata$6\n");
-         fprintf (f, "ID%d:\t%s\t%d\n", exp->ordinal, ASM_SHORT, exp->hint);
-         fprintf (f, "\t%s\t\"%s\"\n", ASM_TEXT, xlate (exp->name));
-       }
-
-      fclose (f);
-
-      dest = (char *) alloca (strlen (prefix) + 10);
-      sprintf (dest, "%ss%05d.o", prefix, i);
-      assemble_file (name, dest);
-    }
-#else /* if 0 */
-    {
-      bfd *      abfd;
-      asymbol *  exp_label;
-      asymbol *  iname = 0;
-      asymbol *  iname2;
-      asymbol *  iname2_pre = 0;
-      asymbol *  iname_lab;
-      asymbol ** iname_lab_pp;
-      asymbol ** iname_pp;
+  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;
+  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;
+  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);
+  sprintf (outname, "%s%05d.o", TMP_STUB, i);
 
-      abfd = bfd_openw (outname, HOW_BFD_WRITE_TARGET);
+  abfd = bfd_openw (outname, HOW_BFD_WRITE_TARGET);
 
-      if (!abfd)
-       /* xgettext:c-format */
-       fatal (_("bfd_open failed open stub file: %s"), outname);
+  if (!abfd)
+    /* xgettext:c-format */
+    fatal (_("bfd_open failed open stub file: %s"), outname);
 
-      /* xgettext:c-format */
-      inform (_("Creating stub file: %s"), outname);
+  /* xgettext:c-format */
+  inform (_("Creating stub file: %s"), outname);
 
-      bfd_set_format (abfd, bfd_object);
-      bfd_set_arch_mach (abfd, HOW_BFD_ARCH, 0);
+  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);
+  if (machine == MARM_INTERWORK || machine == MTHUMB)
+    bfd_set_private_flags (abfd, F_INTERWORK);
 #endif
 
-      applicable = bfd_applicable_section_flags (abfd);
+  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++;
-       }
+  /* 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);
+  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.  */
+      /* 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
+      if (machine == MPPC)
+       exp_label->section = secdata[RDATA].sec;
+      else
 #endif
-           exp_label->section = secdata[TEXT].sec;
+       exp_label->section = secdata[TEXT].sec;
 
-         exp_label->flags = BSF_GLOBAL;
-         exp_label->value = 0;
+      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);
+      if (machine == MTHUMB)
+       bfd_coff_set_symbol_class (abfd, exp_label, C_THUMBEXTFUNC);
 #endif
-         ptrs[oidx++] = exp_label;
-
-         if (ext_prefix_alias)
-           {
-             asymbol *  exp_label_pre;
-
-             exp_label_pre = bfd_make_empty_symbol (abfd);
-             exp_label_pre->name
-               = make_imp_label (ext_prefix_alias, exp->name);
-             exp_label_pre->section = exp_label->section;
-             exp_label_pre->flags = exp_label->flags;
-             exp_label_pre->value = exp_label->value;
-#ifdef DLLTOOL_ARM
-             if (machine == MTHUMB)
-               bfd_coff_set_symbol_class (abfd, exp_label, C_THUMBEXTFUNC);
-#endif
-             ptrs[oidx++] = exp_label_pre;
-           }
-       }
-
-      /* 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;
-       }
+      ptrs[oidx++] = exp_label;
+    }
 
-      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;
+  /* 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;
+    }
 
-      if (ext_prefix_alias)
-       {
-         char *pre_name;
-
-         iname2_pre = bfd_make_empty_symbol (abfd);
-         pre_name = xmalloc (strlen (ext_prefix_alias) + 7);
-         sprintf(pre_name, "__imp_%s", ext_prefix_alias);
-         iname2_pre->name = make_imp_label (pre_name, exp->name);
-         iname2_pre->section = iname2->section;
-         iname2_pre->flags = iname2->flags;
-         iname2_pre->value = iname2->value;
-       }
+  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 = bfd_make_empty_symbol (abfd);
 
-      iname_lab->name = head_label;
-      iname_lab->section = (asection *) &bfd_und_section;
-      iname_lab->flags = 0;
-      iname_lab->value = 0;
+  iname_lab->name = head_label;
+  iname_lab->section = (asection *) &bfd_und_section;
+  iname_lab->flags = 0;
+  iname_lab->value = 0;
 
-      iname_pp = ptrs + oidx;
-      if (create_compat_implib)
-       ptrs[oidx++] = iname;
-      ptrs[oidx++] = iname2;
-      if (ext_prefix_alias)
-       ptrs[oidx++] = iname2_pre;
+  iname_pp = ptrs + oidx;
+  if (create_compat_implib)
+    ptrs[oidx++] = iname;
+  ptrs[oidx++] = iname2;
 
-      iname_lab_pp = ptrs + oidx;
-      ptrs[oidx++] = iname_lab;
+  iname_lab_pp = ptrs + oidx;
+  ptrs[oidx++] = iname_lab;
 
 #ifdef DLLTOOL_PPC
-      /* The symbol referring to the code (.text).  */
-      {
-       asymbol *function_name;
+  /* 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;
+    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;
-      }
+    fn_pp = ptrs + oidx;
+    ptrs[oidx++] = function_name;
+  }
 
-      /* The .toc symbol.  */
-      {
-       asymbol *toc_symbol;
+  /* The .toc symbol.  */
+  {
+    asymbol *toc_symbol;
 
-       toc_symbol = bfd_make_empty_symbol (abfd);
-       toc_symbol->name = make_label (".", "toc");
-       toc_symbol->section = (asection *)&bfd_und_section;
-       toc_symbol->flags = BSF_GLOBAL;
-       toc_symbol->value = 0;
+    toc_symbol = bfd_make_empty_symbol (abfd);
+    toc_symbol->name = make_label (".", "toc");
+    toc_symbol->section = (asection *)&bfd_und_section;
+    toc_symbol->flags = BSF_GLOBAL;
+    toc_symbol->value = 0;
 
-       toc_pp = ptrs + oidx;
-       ptrs[oidx++] = toc_symbol;
-      }
+    toc_pp = ptrs + oidx;
+    ptrs[oidx++] = toc_symbol;
+  }
 #endif
 
-      ptrs[oidx] = 0;
+  ptrs[oidx] = 0;
 
-      for (i = 0; i < NSECS; i++)
-       {
-         sinfo *si = secdata + i;
-         asection *sec = si->sec;
-         arelent *rel;
-         arelent **rpp;
+  for (i = 0; i < NSECS; i++)
+    {
+      sinfo *si = secdata + i;
+      asection *sec = si->sec;
+      arelent *rel;
+      arelent **rpp;
 
-         switch (i)
+      switch (i)
+       {
+       case TEXT:
+         if (! exp->data)
            {
-           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 *) * 2);
+             rpp[0] = rel;
+             rpp[1] = 0;
+
+             rel->address = HOW_JTAB_ROFF;
+             rel->addend = 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)
                {
-                 si->size = HOW_JTAB_SIZE;
-                 si->data = xmalloc (HOW_JTAB_SIZE);
-                 memcpy (si->data, HOW_JTAB, HOW_JTAB_SIZE);
+                 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;
+               }
+             sec->orelocation = rpp;
+             sec->reloc_count = 1;
+           }
+         break;
+       case IDATA4:
+       case IDATA5:
+         /* An idata$4 or idata$5 is one word long, and has an
+            rva to idata$6.  */
 
-                 /* add the reloc into idata$5 */
+         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 = HOW_JTAB_ROFF;
+                 rel->address = 0;
                  rel->addend = 0;
-
-                 if (machine == MPPC)
-                   {
-                     rel->howto = bfd_reloc_type_lookup (abfd,
-                                                         BFD_RELOC_16_GOTOFF);
-                     rel->sym_ptr_ptr = iname_pp;
-                   }
-                 else
-                   {
-                     rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
-                     rel->sym_ptr_ptr = secdata[IDATA5].sympp;
-                   }
+                 rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA);
+                 rel->sym_ptr_ptr = secdata[IDATA6].sympp;
                  sec->orelocation = rpp;
-                 sec->reloc_count = 1;
-               }
-             break;
-           case IDATA4:
-           case IDATA5:
-             /* An idata$4 or idata$5 is one word long, and has an
-                rva to idata$6.  */
-
+               }
+           }
+         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));
@@ -2506,184 +2556,180 @@ make_one_lib_file (export_type *exp, int i)
                  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;
-                 si->size = strlen (xlate (exp->name)) + 3;
-                 si->data = xmalloc (si->size);
-                 si->data[0] = idx & 0xff;
-                 si->data[1] = idx >> 8;
-                 strcpy (si->data + 2, xlate (exp->name));
-               }
-             break;
-           case IDATA7:
-             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];
+           }
+         break;
 
-             rel->address = 4;
-             rel->addend = 0;
-             rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
-             rel->sym_ptr_ptr = toc_pp;
+       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;
+             si->size = strlen (xlate (exp->import_name)) + 3;
+             si->data = xmalloc (si->size);
+             si->data[0] = idx & 0xff;
+             si->data[1] = idx >> 8;
+             strcpy ((char *) si->data + 2, xlate (exp->import_name));
+           }
+         break;
+       case IDATA7:
+         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;
 
-             sec->orelocation = rpp;
-             sec->reloc_count = 2;
-             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++)
       {
-       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);
+       sinfo *si = secdata + i;
 
-/*         vma += si->size;*/
-         }
+       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;
+  }
+  /* Write them out.  */
+  for (i = 0; i < NSECS; i++)
+    {
+      sinfo *si = secdata + i;
 
-         if (i == IDATA4 && no_idata4)
-           continue;
+      if (i == IDATA5 && no_idata5)
+       continue;
 
-         bfd_set_section_contents (abfd, si->sec,
-                                   si->data, 0,
-                                   si->size);
-       }
+      if (i == IDATA4 && no_idata4)
+       continue;
 
-      bfd_set_symtab (abfd, ptrs, oidx);
-      bfd_close (abfd);
-      abfd = bfd_openr (outname, HOW_BFD_READ_TARGET);
-      return abfd;
+      bfd_set_section_contents (abfd, si->sec,
+                               si->data, 0,
+                               si->size);
     }
-#endif
+
+  bfd_set_symtab (abfd, ptrs, oidx);
+  bfd_close (abfd);
+  abfd = bfd_openr (outname, HOW_BFD_READ_TARGET);
+  return abfd;
 }
 
 static bfd *
@@ -2725,16 +2771,26 @@ make_head (void)
   if (!no_idata5)
     {
       fprintf (f, "\t.section\t.idata$5\n");
-      fprintf (f, "\t%s\t0\n", ASM_LONG);
+      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");
-
-      fprintf (f, "\t%s\t0\n", ASM_LONG);
-      fprintf (f, "\t.section  .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");
     }
 
@@ -2759,13 +2815,19 @@ make_tail (void)
   if (!no_idata4)
     {
       fprintf (f, "\t.section  .idata$4\n");
-      fprintf (f, "\t%s\t0\n", ASM_LONG);
+      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  .idata$5\n");
-      fprintf (f, "\t%s\t0\n", ASM_LONG);
+      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
@@ -2826,6 +2888,7 @@ gen_lib_file (void)
 
   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.  */
   ar_head = make_head ();
@@ -2841,13 +2904,33 @@ gen_lib_file (void)
       if (exp->private)
        continue;
       n = make_one_lib_file (exp, i);
-      n->next = head;
+      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.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);
+         n->archive_next = head;
+         head = n;
+       }
     }
 
   /* Now stick them all into the archive.  */
-  ar_head->next = head;
-  ar_tail->next = ar_head;
+  ar_head->archive_next = head;
+  ar_tail->archive_next = ar_head;
   head = ar_tail;
 
   if (! bfd_set_archive_head (outarch, head))
@@ -2858,7 +2941,7 @@ gen_lib_file (void)
 
   while (head != NULL)
     {
-      bfd *n = head->next;
+      bfd *n = head->archive_next;
       bfd_close (head);
       head = n;
     }
@@ -2886,12 +2969,423 @@ gen_lib_file (void)
          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));
+           }
        }
     }
 
   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)
+{
+  /* Error checking.  */
+  if (! list || ! list->tail)
+    return;
+
+  /* Allocate new node.  */
+  dll_name_list_node_type * 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)
+{
+  /* Error checking.  */
+  if (! list || ! list->head)
+    return 0;
+
+  int count = 0;
+  dll_name_list_node_type * 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)
+{
+  /* Error checking.  */
+  if (! list || ! list->head)
+    return;
+
+  dll_name_list_node_type * 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)
+    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);
+    }
+
+  /* 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);
+
+      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);
+}
+
+/* This predicate returns true if section->name matches the desired value.
+   By default, this is .idata$7 (.idata$6 on PPC, or if the import
+   library is ms-style).  */   
+
+static bfd_boolean
+identify_process_section_p (asection * section, bfd_boolean ms_style_implib)
+{
+  static const char * SECTION_NAME =
+#ifdef DLLTOOL_PPC
+  /* dllname is stored in idata$6 on PPC */
+  ".idata$6";
+#else
+  ".idata$7";
+#endif
+  static const char * MS_SECTION_NAME = ".idata$6";
+  
+  const char * section_name =
+    (ms_style_implib ? MS_SECTION_NAME : SECTION_NAME);
+  
+  if (strcmp (section_name, section->name) == 0)
+    return TRUE;
+  return FALSE;
+}
+
+/* If *section has contents and its name is .idata$7 (.data$6 on PPC or if
+   import lib ms-generated) -- and it satisfies several other constraints
+   -- then add the contents of the section to obj->list.  */
+
+static void
+identify_search_section (bfd * abfd, asection * section, void * obj)
+{
+  bfd_byte *data = 0;
+  bfd_size_type datasize;
+  identify_data_type * identify_data = (identify_data_type *)obj;
+  bfd_boolean ms_style = identify_data->ms_style_implib;
+
+  if ((section->flags & SEC_HAS_CONTENTS) == 0)
+    return;
+
+  if (! identify_process_section_p (section, ms_style))
+    return;
+
+  /* Binutils import libs seem distinguish the .idata$7 section that contains
+     the DLL name from other .idata$7 sections by the absence of the
+     SEC_RELOC flag.  */
+  if (!ms_style && ((section->flags & SEC_RELOC) == SEC_RELOC))
+    return;
+
+  /* MS import libs seem to distinguish the .idata$6 section
+     that contains the DLL name from other .idata$6 sections
+     by the presence of the SEC_DATA flag.  */
+  if (ms_style && ((section->flags & SEC_DATA) == 0))
+    return;
+
+  if ((datasize = bfd_section_size (abfd, section)) == 0)
+    return;
+
+  data = (bfd_byte *) xmalloc (datasize + 1);
+  data[0] = '\0';
+
+  bfd_get_section_contents (abfd, section, data, 0, datasize);
+  data[datasize] = '\0';
+
+  /* Use a heuristic to determine if data is a dll name.
+     Possible to defeat this if (a) the library has MANY
+     (more than 0x302f) imports, (b) it is an ms-style 
+     import library, but (c) it is buggy, in that the SEC_DATA
+     flag is set on the "wrong" sections.  This heuristic might
+     also fail to record a valid dll name if the dllname uses
+     a multibyte or unicode character set (is that valid?).
+
+     This heuristic is based on the fact that symbols names in
+     the chosen section -- as opposed to the dll name -- begin
+     at offset 2 in the data. The first two bytes are a 16bit
+     little-endian count, and start at 0x0000. However, the dll
+     name begins at offset 0 in the data. We assume that the
+     dll name does not contain unprintable characters.   */
+  if (data[0] != '\0' && ISPRINT (data[0])
+      && ((datasize < 2) || ISPRINT (data[1])))
+    dll_name_list_append (identify_data->list, data);
+
+  free (data);
+}
+
 /* Run through the information gathered from the .o files and the
    .def file and work out the best stuff.  */
 
@@ -2900,6 +3394,7 @@ pfunc (const void *a, const void *b)
 {
   export_type *ap = *(export_type **) a;
   export_type *bp = *(export_type **) b;
+
   if (ap->ordinal == bp->ordinal)
     return 0;
 
@@ -2916,8 +3411,16 @@ nfunc (const void *a, const void *b)
 {
   export_type *ap = *(export_type **) a;
   export_type *bp = *(export_type **) b;
+  const char *an = ap->name;
+  const char *bn = bp->name;
+
+  if (killat)
+    {
+      an = (an[0] == '@') ? an + 1 : an;
+      bn = (bn[0] == '@') ? bn + 1 : bn;
+    }
 
-  return (strcmp (ap->name, bp->name));
+  return (strcmp (an, bn));
 }
 
 static void
@@ -2937,27 +3440,6 @@ remove_null_names (export_type **ptr)
   d_nfuncs = dst;
 }
 
-static void
-dtab (export_type **ptr ATTRIBUTE_UNUSED)
-{
-#ifdef SACDEBUG
-  int i;
-  for (i = 0; i < d_nfuncs; i++)
-    {
-      if (ptr[i])
-       {
-         printf ("%d %s @ %d %s%s%s\n",
-                 i, ptr[i]->name, ptr[i]->ordinal,
-                 ptr[i]->noname ? "NONAME " : "",
-                 ptr[i]->constant ? "CONSTANT" : "",
-                 ptr[i]->data ? "DATA" : "");
-       }
-      else
-       printf ("empty\n");
-    }
-#endif
-}
-
 static void
 process_duplicates (export_type **d_export_vec)
 {
@@ -2970,7 +3452,6 @@ process_duplicates (export_type **d_export_vec)
       /* Remove duplicates.  */
       qsort (d_export_vec, d_nfuncs, sizeof (export_type *), nfunc);
 
-      dtab (d_export_vec);
       for (i = 0; i < d_nfuncs - 1; i++)
        {
          if (strcmp (d_export_vec[i]->name,
@@ -2988,7 +3469,7 @@ process_duplicates (export_type **d_export_vec)
              if (a->ordinal != -1
                  && b->ordinal != -1)
                /* xgettext:c-format */
-               fatal (_("Error, duplicate EXPORT with oridinals: %s"),
+               fatal (_("Error, duplicate EXPORT with ordinals: %s"),
                      a->name);
 
              /* Merge attributes.  */
@@ -2999,9 +3480,7 @@ process_duplicates (export_type **d_export_vec)
              d_export_vec[i] = 0;
            }
 
-         dtab (d_export_vec);
          remove_null_names (d_export_vec);
-         dtab (d_export_vec);
        }
     }
 
@@ -3085,15 +3564,6 @@ fill_ordinals (export_type **d_export_vec)
     }
 }
 
-static int
-alphafunc (const void *av, const void *bv)
-{
-  const export_type **a = (const export_type **) av;
-  const export_type **b = (const export_type **) bv;
-
-  return strcmp ((*a)->name, (*b)->name);
-}
-
 static void
 mangle_defs (void)
 {
@@ -3129,7 +3599,7 @@ mangle_defs (void)
 
   d_exports_lexically[i] = 0;
 
-  qsort (d_exports_lexically, i, sizeof (export_type *), alphafunc);
+  qsort (d_exports_lexically, i, sizeof (export_type *), nfunc);
 
   /* Fill exp entries with their hint values.  */
   for (i = 0; i < d_nfuncs; i++)
@@ -3160,7 +3630,9 @@ usage (FILE *file, int status)
   fprintf (file, _("   -b --base-file <basefile> Read linker generated base file.\n"));
   fprintf (file, _("   -x --no-idata4            Don't generate idata$4 section.\n"));
   fprintf (file, _("   -c --no-idata5            Don't generate idata$5 section.\n"));
-  fprintf (file, _("   -U --add-underscore       Add underscores to symbols in interface library.\n"));
+  fprintf (file, _("      --use-nul-prefixed-import-tables Use zero prefixed idata$4 and idata$5.\n"));
+  fprintf (file, _("   -U --add-underscore       Add underscores to all symbols in interface library.\n"));
+  fprintf (file, _("      --add-stdcall-underscore Add underscores to stdcall symbols in interface library.\n"));
   fprintf (file, _("   -k --kill-at              Kill @<n> from exported names.\n"));
   fprintf (file, _("   -A --add-stdcall-alias    Add aliases without @<n>.\n"));
   fprintf (file, _("   -p --ext-prefix-alias <prefix> Add aliases with <prefix>.\n"));
@@ -3169,14 +3641,19 @@ usage (FILE *file, int status)
   fprintf (file, _("   -C --compat-implib        Create backward compatible import library.\n"));
   fprintf (file, _("   -n --no-delete            Keep temp files (repeat for extra preservation).\n"));
   fprintf (file, _("   -t --temp-prefix <prefix> Use <prefix> to construct temp file names.\n"));
+  fprintf (file, _("   -I --identify <implib>    Report the name of the DLL associated with <implib>.\n"));
+  fprintf (file, _("      --identify-strict      Causes --identify to report error when multiple DLLs.\n"));
   fprintf (file, _("   -v --verbose              Be verbose.\n"));
   fprintf (file, _("   -V --version              Display the program version.\n"));
   fprintf (file, _("   -h --help                 Display this information.\n"));
+  fprintf (file, _("   @<file>                   Read options from <file>.\n"));
 #ifdef DLLTOOL_MCORE_ELF
   fprintf (file, _("   -M --mcore-elf <outname>  Process mcore-elf object files into <outname>.\n"));
   fprintf (file, _("   -L --linker <name>        Use <name> as the linker.\n"));
   fprintf (file, _("   -F --linker-flags <flags> Pass <flags> to the linker.\n"));
 #endif
+  if (REPORT_BUGS_TO[0] && status == 0)
+    fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
   exit (status);
 }
 
@@ -3184,6 +3661,10 @@ usage (FILE *file, int status)
 #define OPTION_NO_EXPORT_ALL_SYMS      (OPTION_EXPORT_ALL_SYMS + 1)
 #define OPTION_EXCLUDE_SYMS            (OPTION_NO_EXPORT_ALL_SYMS + 1)
 #define OPTION_NO_DEFAULT_EXCLUDES     (OPTION_EXCLUDE_SYMS + 1)
+#define OPTION_ADD_STDCALL_UNDERSCORE  (OPTION_NO_DEFAULT_EXCLUDES + 1)
+#define OPTION_USE_NUL_PREFIXED_IMPORT_TABLES \
+  (OPTION_ADD_STDCALL_UNDERSCORE + 1)
+#define OPTION_IDENTIFY_STRICT         (OPTION_USE_NUL_PREFIXED_IMPORT_TABLES + 1)
 
 static const struct option long_options[] =
 {
@@ -3191,6 +3672,8 @@ static const struct option long_options[] =
   {"dllname", required_argument, NULL, 'D'},
   {"no-idata4", no_argument, NULL, 'x'},
   {"no-idata5", no_argument, NULL, 'c'},
+  {"use-nul-prefixed-import-tables", no_argument, NULL,
+   OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
   {"output-exp", required_argument, NULL, 'e'},
   {"output-def", required_argument, NULL, 'z'},
   {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL_SYMS},
@@ -3201,9 +3684,12 @@ static const struct option long_options[] =
   {"def", required_argument, NULL, 'd'}, /* for compatibility with older versions */
   {"input-def", required_argument, NULL, 'd'},
   {"add-underscore", no_argument, NULL, 'U'},
+  {"add-stdcall-underscore", no_argument, NULL, OPTION_ADD_STDCALL_UNDERSCORE},
   {"kill-at", no_argument, NULL, 'k'},
   {"add-stdcall-alias", no_argument, NULL, 'A'},
   {"ext-prefix-alias", required_argument, NULL, 'p'},
+  {"identify", required_argument, NULL, 'I'},
+  {"identify-strict", no_argument, NULL, OPTION_IDENTIFY_STRICT},
   {"verbose", no_argument, NULL, 'v'},
   {"version", no_argument, NULL, 'V'},
   {"help", no_argument, NULL, 'h'},
@@ -3238,11 +3724,13 @@ main (int ac, char **av)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  expandargv (&ac, &av);
+
   while ((c = getopt_long (ac, av,
 #ifdef DLLTOOL_MCORE_ELF
-                          "m:e:l:aD:d:z:b:xp:cCuUkAS:f:nvVHhM:L:F:",
+                          "m:e:l:aD:d:z:b:xp:cCuUkAS:f:nI:vVHhM:L:F:",
 #else
-                          "m:e:l:aD:d:z:b:xp:cCuUkAS:f:nvVHh",
+                          "m:e:l:aD:d:z:b:xp:cCuUkAS:f:nI:vVHh",
 #endif
                           long_options, 0))
         != EOF)
@@ -3261,6 +3749,15 @@ main (int ac, char **av)
        case OPTION_NO_DEFAULT_EXCLUDES:
          do_default_excludes = FALSE;
          break;
+       case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
+         use_nul_prefixed_import_tables = TRUE;
+         break;
+       case OPTION_ADD_STDCALL_UNDERSCORE:
+         add_stdcall_underscore = 1;
+         break;
+       case OPTION_IDENTIFY_STRICT:
+         identify_strict = 1;
+         break;
        case 'x':
          no_idata4 = 1;
          break;
@@ -3287,7 +3784,10 @@ main (int ac, char **av)
          output_def = fopen (optarg, FOPEN_WT);
          break;
        case 'D':
-         dll_name = optarg;
+         dll_name = (char*) lbasename (optarg);
+         if (dll_name != optarg)
+           non_fatal (_("Path components stripped from dllname, '%s'."),
+                        optarg);
          break;
        case 'l':
          imp_name = optarg;
@@ -3302,6 +3802,9 @@ main (int ac, char **av)
        case 'm':
          mname = optarg;
          break;
+       case 'I':
+         identify_imp_name = optarg;
+         break;
        case 'v':
          verbose = 1;
          break;
@@ -3367,11 +3870,18 @@ main (int ac, char **av)
 
   machine = i;
 
+  /* Check if we generated PE+.  */
+  create_for_pep = strcmp (mname, "i386:x86-64") == 0;
+
   if (!dll_name && exp_name)
     {
-      int len = strlen (exp_name) + 5;
+      /* If we are inferring dll_name from exp_name,
+         strip off any path components, without emitting
+         a warning.  */  
+      const char* exp_basename = lbasename (exp_name); 
+      const int len = strlen (exp_basename) + 5;
       dll_name = xmalloc (len);
-      strcpy (dll_name, exp_name);
+      strcpy (dll_name, exp_basename);
       strcat (dll_name, ".dll");
     }
 
@@ -3421,6 +3931,11 @@ main (int ac, char **av)
   if (output_def)
     gen_def_file ();
 
+  if (identify_imp_name)
+    {
+      identify_dll_for_implib ();
+    }
+
 #ifdef DLLTOOL_MCORE_ELF
   if (mcore_elf_out_file)
     mcore_elf_gen_out_file ();
@@ -3551,7 +4066,7 @@ deduce_name (const char *prog_name)
 #ifdef DLLTOOL_MCORE_ELF
 typedef struct fname_cache
 {
-  char *               filename;
+  const char *         filename;
   struct fname_cache * next;
 }
 fname_cache;
@@ -3559,7 +4074,7 @@ fname_cache;
 static fname_cache fnames;
 
 static void
-mcore_elf_cache_filename (char * filename)
+mcore_elf_cache_filename (const char * filename)
 {
   fname_cache * ptr;
 
This page took 0.051963 seconds and 4 git commands to generate.