* cris.h (R_CRIS_32_IE): New relocation.
[deliverable/binutils-gdb.git] / binutils / dlltool.c
index 8ee61d322004ff4111096b8a840a59f0b1b379b7..89508f32ae9b5dd67af4dcb8c7341fcf8ccac8be 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,
-   2005, 2006, 2007 Free Software Foundation, Inc.
+   2005, 2006, 2007, 2008 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,
 
 #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"
 
@@ -270,7 +271,7 @@ 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
 
@@ -351,6 +352,8 @@ static int no_idata4;
 static int no_idata5;
 static char *exp_name;
 static char *imp_name;
+static char *identify_imp_name;
+static char *identify_dll_name;
 static char *head_label;
 static char *imp_name_lab;
 static char *dll_name;
@@ -711,7 +714,7 @@ 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 *);
@@ -723,6 +726,11 @@ 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 identify_dll_for_implib (void);
+static void identify_search_archive (bfd*);
+static void identify_search_member (bfd*, bfd*);
+static bfd_boolean identify_process_section_p (asection *);
+static void identify_search_section (bfd *, asection *, void *);
 static int pfunc (const void *, const void *);
 static int nfunc (const void *, const void *);
 static void remove_null_names (export_type **);
@@ -1205,7 +1213,7 @@ run (const char *what, char *args)
 
   if (pid == -1)
     {
-      inform (strerror (errno));
+      inform ("%s", strerror (errno));
 
       fatal (errmsg_fmt, errmsg_arg);
     }
@@ -1545,7 +1553,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
     }
 
@@ -1583,18 +1591,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,
@@ -1603,12 +1614,20 @@ 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;
+        {
+#ifndef DLLTOOL_MX86_64
+         /* Relocation via HIGHLOW.  */
+          needed = ((needed - page_addr) | 0x3000) & 0xffff;
+#else
+         /* Relocation via DIR64.  */
+         needed = ((needed - page_addr) | 0xa000) & 0xffff;
+#endif
+       }
 
-      fprintf (f, "\t%s\t0x%lx\n", ASM_SHORT, needed);
+      fprintf (f, "\t%s\t0x%lx\n", ASM_SHORT, (long) needed);
     }
 
   /* And padding */
@@ -1825,8 +1844,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);
@@ -1890,7 +1909,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++)
        {
@@ -1976,12 +1995,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");
@@ -1991,8 +2010,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");
@@ -2000,8 +2020,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++)
            {
@@ -2812,6 +2832,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 ();
@@ -2827,7 +2848,7 @@ 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)
        {
@@ -2846,14 +2867,14 @@ gen_lib_file (void)
          alias_exp.forward = exp->forward;
          alias_exp.next = exp->next;
          n = make_one_lib_file (&alias_exp, i + PREFIX_ALIAS_BASE);
-         n->next = head;
+         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))
@@ -2864,7 +2885,7 @@ gen_lib_file (void)
 
   while (head != NULL)
     {
-      bfd *n = head->next;
+      bfd *n = head->archive_next;
       bfd_close (head);
       head = n;
     }
@@ -2905,6 +2926,193 @@ gen_lib_file (void)
   inform (_("Created lib file"));
 }
 
+/* identify_dll_for_implib
+
+   This is the main implementation for the --identify option.
+   Given the name of an import library in identify_imp_name,
+   search all archive members for an .idata$7 section
+   (.idata$6 on PPC). This section will consist of a single
+   char* constant, indicating the name of the DLL represented
+   by the import library.
+
+   It is possible to construct an import library that has
+   two members with a non-empty .idata$7 section, but these
+   are not often seen in normal operation.  In this case,
+   an error is flagged.
+*/   
+static void 
+identify_dll_for_implib (void)
+{
+  bfd* abfd = NULL;
+
+  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);
+    }
+
+  identify_search_archive (abfd);
+
+  if (!bfd_close (abfd))
+    bfd_fatal (identify_imp_name);
+
+  if (identify_dll_name && *identify_dll_name)
+    {
+      printf ("%s\n",identify_dll_name);
+      free (identify_dll_name);
+      identify_dll_name = NULL;
+    }
+  else
+    {
+      fatal ("Unable to determine dll name for %s (not an import library?)", identify_imp_name);
+    }
+}
+
+/* identify_search_archive
+
+   Loop over all members of the archive, inspecting 
+   each for the presence of an .idata$7 (.idata$6 on PPC)
+   section with non-empty contents.
+*/   
+static void
+identify_search_archive (bfd* abfd)
+{
+  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))
+        {
+          identify_search_member (arfile, abfd);
+        }
+      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);
+    }
+}
+
+/* identify_search_member
+
+   Search all sections of an archive member for the 
+   one with section name of .idata$7 (.idata$6 on PPC)
+   and non-empty contents.
+*/   
+static void
+identify_search_member (bfd* abfd, bfd* archive_bfd ATTRIBUTE_UNUSED)
+{
+  bfd_map_over_sections (abfd, identify_search_section, NULL);
+}
+
+/* identify_process_section_p
+
+   This predicate returns true if section->name
+   is .idata$7 (.idata$6 on PPC).
+*/   
+static bfd_boolean
+identify_process_section_p (asection * section)
+{
+  static const char * SECTION_NAME =
+#ifdef DLLTOOL_PPC
+  /* dllname is stored in idata$6 on PPC */
+  ".idata$6";
+#else
+  ".idata$7";
+#endif
+
+  if (strcmp (SECTION_NAME, section->name) == 0)
+    return TRUE;
+  return FALSE;
+}
+
+/* identify_search_section
+
+   If *section has contents and its name is .idata$7
+   (.data$6 on PPC) then store the contents in 
+   identify_dll_name as an xmalloc'ed array.
+
+   However, if identify_dll_name already has
+   a value, flag an error. We don't know how to handle
+   import libraries that directly reference more than
+   one DLL. (This is different than forwarded symbols.
+   Such import libraries are not seen in normal operation,
+   and must be specifically constructed.)
+*/   
+static void
+identify_search_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
+{
+  bfd_byte *data = 0;
+  bfd_size_type datasize;
+
+  if ((section->flags & SEC_HAS_CONTENTS) == 0)
+    return;
+
+  if (! identify_process_section_p (section))
+    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';
+
+  if (data[0] != '\0')
+    {
+      if (identify_dll_name != NULL)
+        {
+          if (*identify_dll_name != '\0')
+            {
+              /* The import library specifies two different DLLs.
+                 Treat this as an error. */
+              fatal ("Import library `%s' specifies two or more dlls: `%s' and `%s'",
+                     identify_imp_name, identify_dll_name, data);
+            }
+          else
+            {
+              /* For some reason memory was allocated, but the
+                 contents were empty. Free the memory and continue. */
+              free (identify_dll_name);
+            }
+        }
+      identify_dll_name = xstrdup ((char*) data);
+    }
+
+  free (data);
+}
+
 /* Run through the information gathered from the .o files and the
    .def file and work out the best stuff.  */
 
@@ -2987,7 +3195,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.  */
@@ -3158,6 +3366,7 @@ 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, _("   -v --verbose              Be verbose.\n"));
   fprintf (file, _("   -V --version              Display the program version.\n"));
   fprintf (file, _("   -h --help                 Display this information.\n"));
@@ -3198,6 +3407,7 @@ static const struct option long_options[] =
   {"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'},
   {"verbose", no_argument, NULL, 'v'},
   {"version", no_argument, NULL, 'V'},
   {"help", no_argument, NULL, 'h'},
@@ -3236,9 +3446,9 @@ main (int ac, char **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)
@@ -3304,6 +3514,9 @@ main (int ac, char **av)
        case 'm':
          mname = optarg;
          break;
+       case 'I':
+         identify_imp_name = optarg;
+         break;
        case 'v':
          verbose = 1;
          break;
@@ -3427,6 +3640,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 ();
@@ -3557,7 +3775,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;
@@ -3565,7 +3783,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.029037 seconds and 4 git commands to generate.