Handle symbolic link when copying source file
[deliverable/binutils-gdb.git] / binutils / srconv.c
index 1517b1caf38963f080095336e75c3f4d79cc63f3..7abbb66c55c66c6d70546854fc3c0eabd9ba5a3e 100644 (file)
@@ -1,12 +1,11 @@
 /* srconv.c -- Sysroff conversion program
 /* srconv.c -- Sysroff conversion program
-   Copyright 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004
-   Free Software Foundation, Inc.
+   Copyright (C) 1994-2015 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
 
    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,
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
@@ -16,8 +15,8 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
 
    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.  */
 
 /* Written by Steve Chamberlain (sac@cygnus.com)
 
 
 /* Written by Steve Chamberlain (sac@cygnus.com)
 
 
    All debugging information is preserved */
 
 
    All debugging information is preserved */
 
+#include "sysdep.h"
 #include "bfd.h"
 #include "bucomm.h"
 #include "sysroff.h"
 #include "coffgrok.h"
 #include "libiberty.h"
 #include "bfd.h"
 #include "bucomm.h"
 #include "sysroff.h"
 #include "coffgrok.h"
 #include "libiberty.h"
+#include "filenames.h"
 #include "getopt.h"
 
 #include "coff/internal.h"
 #include "getopt.h"
 
 #include "coff/internal.h"
@@ -45,12 +46,12 @@ static char **rnames;
 static int get_member_id (int);
 static int get_ordinary_id (int);
 static char *section_translate (char *);
 static int get_member_id (int);
 static int get_ordinary_id (int);
 static char *section_translate (char *);
-static char *strip_suffix (char *);
-static void checksum (FILE *, char *, int, int);
-static void writeINT (int, char *, int *, int, FILE *);
-static void writeBITS (int, char *, int *, int);
-static void writeBARRAY (barray, char *, int *, int, FILE *);
-static void writeCHARS (char *, char *, int *, int, FILE *);
+static char *strip_suffix (const char *);
+static void checksum (FILE *, unsigned char *, int, int);
+static void writeINT (int, unsigned char *, int *, int, FILE *);
+static void writeBITS (int, unsigned char *, int *, int);
+static void writeBARRAY (barray, unsigned char *, int *, int, FILE *);
+static void writeCHARS (char *, unsigned char *, int *, int, FILE *);
 static void wr_tr (void);
 static void wr_un (struct coff_ofile *, struct coff_sfile *, int, int);
 static void wr_hd (struct coff_ofile *);
 static void wr_tr (void);
 static void wr_un (struct coff_ofile *, struct coff_sfile *, int, int);
 static void wr_hd (struct coff_ofile *);
@@ -141,9 +142,8 @@ section_translate (char *n)
 
 #define DATE "940201073000";   /* Just a time on my birthday */
 
 
 #define DATE "940201073000";   /* Just a time on my birthday */
 
-static
-char *
-strip_suffix (char *name)
+static char *
+strip_suffix (const char *name)
 {
   int i;
   char *res;
 {
   int i;
   char *res;
@@ -158,17 +158,18 @@ strip_suffix (char *name)
 
 /* IT LEN stuff CS */
 static void
 
 /* IT LEN stuff CS */
 static void
-checksum (FILE *file, char *ptr, int size, int code)
+checksum (FILE *ffile, unsigned char *ptr, int size, int ccode)
 {
   int j;
   int last;
   int sum = 0;
   int bytes = size / 8;
 
 {
   int j;
   int last;
   int sum = 0;
   int bytes = size / 8;
 
-  last = !(code & 0xff00);
+  last = !(ccode & 0xff00);
   if (size & 0x7)
   if (size & 0x7)
-    abort ();
-  ptr[0] = code | (last ? 0x80 : 0);
+    fatal (_("Checksum failure"));
+
+  ptr[0] = ccode | (last ? 0x80 : 0);
   ptr[1] = bytes + 1;
 
   for (j = 0; j < bytes; j++)
   ptr[1] = bytes + 1;
 
   for (j = 0; j < bytes; j++)
@@ -176,12 +177,14 @@ checksum (FILE *file, char *ptr, int size, int code)
 
   /* Glue on a checksum too.  */
   ptr[bytes] = ~sum;
 
   /* Glue on a checksum too.  */
   ptr[bytes] = ~sum;
-  fwrite (ptr, bytes + 1, 1, file);
+  if (fwrite (ptr, bytes + 1, 1, ffile) != 1)
+    /* FIXME: Return error status.  */
+    fatal (_("Failed to write checksum"));
 }
 
 
 static void
 }
 
 
 static void
-writeINT (int n, char *ptr, int *idx, int size, FILE *file)
+writeINT (int n, unsigned char *ptr, int *idx, int size, FILE *ffile)
 {
   int byte = *idx / 8;
 
 {
   int byte = *idx / 8;
 
@@ -193,7 +196,7 @@ writeINT (int n, char *ptr, int *idx, int size, FILE *file)
   if (byte > 240)
     {
       /* Lets write out that record and do another one.  */
   if (byte > 240)
     {
       /* Lets write out that record and do another one.  */
-      checksum (file, ptr, *idx, code | 0x1000);
+      checksum (ffile, ptr, *idx, code | 0x1000);
       *idx = 16;
       byte = *idx / 8;
     }
       *idx = 16;
       byte = *idx / 8;
     }
@@ -216,13 +219,13 @@ writeINT (int n, char *ptr, int *idx, int size, FILE *file)
       ptr[byte + 3] = n >> 0;
       break;
     default:
       ptr[byte + 3] = n >> 0;
       break;
     default:
-      abort ();
+      fatal (_("Unsupported integer write size: %d"), size);
     }
   *idx += size * 8;
 }
 
 static void
     }
   *idx += size * 8;
 }
 
 static void
-writeBITS (int val, char *ptr, int *idx, int size)
+writeBITS (int val, unsigned char *ptr, int *idx, int size)
 {
   int byte = *idx / 8;
   int bit = *idx % 8;
 {
   int byte = *idx / 8;
   int bit = *idx % 8;
@@ -239,25 +242,25 @@ writeBITS (int val, char *ptr, int *idx, int size)
 }
 
 static void
 }
 
 static void
-writeBARRAY (barray data, char *ptr, int *idx, int size ATTRIBUTE_UNUSED,
-            FILE *file)
+writeBARRAY (barray data, unsigned char *ptr, int *idx,
+            int size ATTRIBUTE_UNUSED, FILE *ffile)
 {
   int i;
 
 {
   int i;
 
-  writeINT (data.len, ptr, idx, 1, file);
+  writeINT (data.len, ptr, idx, 1, ffile);
   for (i = 0; i < data.len; i++)
   for (i = 0; i < data.len; i++)
-    writeINT (data.data[i], ptr, idx, 1, file);
+    writeINT (data.data[i], ptr, idx, 1, ffile);
 }
 
 static void
 }
 
 static void
-writeCHARS (char *string, char *ptr, int *idx, int size, FILE *file)
+writeCHARS (char *string, unsigned char *ptr, int *idx, int size, FILE *ffile)
 {
   int i = *idx / 8;
 
   if (i > 240)
     {
       /* Lets write out that record and do another one.  */
 {
   int i = *idx / 8;
 
   if (i > 240)
     {
       /* Lets write out that record and do another one.  */
-      checksum (file, ptr, *idx, code | 0x1000);
+      checksum (ffile, ptr, *idx, code | 0x1000);
       *idx = 16;
       i = *idx / 8;
     }
       *idx = 16;
       i = *idx / 8;
     }
@@ -299,7 +302,10 @@ wr_tr (void)
       0x03,                    /* RL */
       0xfd,                    /* CS */
     };
       0x03,                    /* RL */
       0xfd,                    /* CS */
     };
-  fwrite (b, 1, sizeof (b), file);
+
+  if (fwrite (b, sizeof (b), 1, file) != 1)
+    /* FIXME: Return error status.  */
+    fatal (_("Failed to write TR block"));
 }
 
 static void
 }
 
 static void
@@ -317,12 +323,8 @@ wr_un (struct coff_ofile *ptr, struct coff_sfile *sfile, int first,
     un.format = FORMAT_OM;
   un.spare1 = 0;
 
     un.format = FORMAT_OM;
   un.spare1 = 0;
 
-#if 1
-  un.nsections = ptr->nsections - 1;   /*  Don't count the abs section.  */
-#else
-  /*NEW - only count sections with size.  */
-  un.nsections = nsecs;
-#endif
+  /* Don't count the abs section.  */
+  un.nsections = ptr->nsections - 1;
 
   un.nextdefs = 0;
   un.nextrefs = 0;
 
   un.nextdefs = 0;
   un.nextrefs = 0;
@@ -394,7 +396,8 @@ wr_hd (struct coff_ofile *p)
          toolname = "C_H8/300S";
          break;
        default:
          toolname = "C_H8/300S";
          break;
        default:
-         abort();
+         fatal (_("Unrecognized H8300 sub-architecture: %ld"),
+                bfd_get_mach (abfd));
        }
       rnames = rname_h8300;
       break;
        }
       rnames = rname_h8300;
       break;
@@ -411,10 +414,10 @@ wr_hd (struct coff_ofile *p)
       rnames = rname_sh;
       break;
     default:
       rnames = rname_sh;
       break;
     default:
-      abort ();
+      fatal (_("Unsupported architecture: %d"), bfd_get_arch (abfd));
     }
 
     }
 
-  if (! bfd_get_file_flags(abfd) & EXEC_P)
+  if (! (bfd_get_file_flags(abfd) & EXEC_P))
     {
       hd.ep = 0;
     }
     {
       hd.ep = 0;
     }
@@ -708,6 +711,7 @@ walk_tree_type_1 (struct coff_sfile *sfile, struct coff_symbol *symbol,
       {
        struct IT_dpt dpt;
 
       {
        struct IT_dpt dpt;
 
+       dpt.dunno = 0;
        walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
        dpt.neg = 0x1001;
        sysroff_swap_dpt_out (file, &dpt);
        walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
        dpt.neg = 0x1001;
        sysroff_swap_dpt_out (file, &dpt);
@@ -864,7 +868,7 @@ walk_tree_type_1 (struct coff_sfile *sfile, struct coff_symbol *symbol,
       break;
 
     default:
       break;
 
     default:
-      abort ();
+      fatal (_("Unrecognised type: %d"), type->type);
     }
 }
 
     }
 }
 
@@ -993,7 +997,7 @@ walk_tree_symbol (struct coff_sfile *sfile, struct coff_section *section ATTRIBU
       return;
 
     default:
       return;
 
     default:
-      abort ();
+      fatal (_("Unrecognised coff symbol type: %d"), symbol->type->type);
     }
 
   if (symbol->where->where == coff_where_member_of_struct)
     }
 
   if (symbol->where->where == coff_where_member_of_struct)
@@ -1055,7 +1059,7 @@ walk_tree_symbol (struct coff_sfile *sfile, struct coff_section *section ATTRIBU
       break;
 
     default:
       break;
 
     default:
-      abort ();
+      fatal (_("Unrecognised coff symbol visibility: %d"), symbol->visible->type);
     }
 
   dsy.dlength = symbol->type->size;
     }
 
   dsy.dlength = symbol->type->size;
@@ -1081,7 +1085,7 @@ walk_tree_symbol (struct coff_sfile *sfile, struct coff_section *section ATTRIBU
       break;
 
     default:
       break;
 
     default:
-      abort ();
+      fatal (_("Unrecognised coff symbol location: %d"), symbol->where->where);
     }
 
   switch (symbol->where->where)
     }
 
   switch (symbol->where->where)
@@ -1126,7 +1130,7 @@ walk_tree_symbol (struct coff_sfile *sfile, struct coff_section *section ATTRIBU
       break;
 
     default:
       break;
 
     default:
-      abort ();
+      fatal (_("Unrecognised coff symbol location: %d"), symbol->where->where);
     }
 
   if (symbol->where->where == coff_where_register)
     }
 
   if (symbol->where->where == coff_where_register)
@@ -1155,7 +1159,7 @@ walk_tree_symbol (struct coff_sfile *sfile, struct coff_section *section ATTRIBU
       break;
 
     default:
       break;
 
     default:
-      abort ();
+      fatal (_("Unrecognised coff symbol visibility: %d"), symbol->visible->type);
     }
 
   dsy.sfn = 0;
     }
 
   dsy.sfn = 0;
@@ -1200,6 +1204,8 @@ walk_tree_sfile (struct coff_section *section, struct coff_sfile *sfile)
 static void
 wr_program_structure (struct coff_ofile *p, struct coff_sfile *sfile)
 {
 static void
 wr_program_structure (struct coff_ofile *p, struct coff_sfile *sfile)
 {
+  if (p->nsections < 4)
+    return;
   walk_tree_sfile (p->sections + 4, sfile);
 }
 
   walk_tree_sfile (p->sections + 4, sfile);
 }
 
@@ -1208,11 +1214,6 @@ wr_du (struct coff_ofile *p, struct coff_sfile *sfile, int n)
 {
   struct IT_du du;
   int lim;
 {
   struct IT_du du;
   int lim;
-#if 0
-  struct coff_symbol *symbol;
-  static int incit = 0x500000;
-  int used = 0;
-#endif
   int i;
   int j;
   unsigned int *lowest = (unsigned *) nints (p->nsections);
   int i;
   int j;
   unsigned int *lowest = (unsigned *) nints (p->nsections);
@@ -1234,47 +1235,6 @@ wr_du (struct coff_ofile *p, struct coff_sfile *sfile, int n)
       highest[i] = 0;
     }
 
       highest[i] = 0;
     }
 
-  /* Look through all the symbols and try and work out the extents in this
-     source file.  */
-#if 0
-  for (symbol = sfile->scope->vars_head;
-       symbol;
-       symbol = symbol->next)
-    {
-      if (symbol->type->type == coff_secdef_type)
-       {
-         unsigned int low = symbol->where->offset;
-         unsigned int high = symbol->where->offset + symbol->type->size - 1;
-         struct coff_section *section = symbol->where->section;
-
-         int sn = section->number;
-         if (low < lowest[sn])
-           lowest[sn] = low;
-         if (high > highest[sn])
-           highest[sn] = high;
-       }
-    }
-
-  for (i = 0; i < du.sections; i++)
-    {
-      if (highest[i] == 0)
-       lowest[i] = highest[i] = incit;
-
-      du.san[used] = i;
-      du.length[used] = highest[i] - lowest[i];
-      du.address[used] = bfd_get_file_flags (abfd) & EXEC_P ? lowest[i] : 0;
-
-      if (debug)
-       {
-         printf (" section %6s 0x%08x..0x%08x\n",
-                 p->sections[i + 1].name,
-                 lowest[i],
-                 highest[i]);
-       }
-      used++;
-    }
-#endif
-
   lim = du.sections;
   for (j = 0; j < lim; j++)
     {
   lim = du.sections;
   for (j = 0; j < lim; j++)
     {
@@ -1328,22 +1288,8 @@ wr_dus (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile)
   dus.spare = nints (dus.ns);
   dus.ndir = 0;
   /* Find the filenames.  */
   dus.spare = nints (dus.ns);
   dus.ndir = 0;
   /* Find the filenames.  */
-#if 0
-  i = 0;
-
-  for (sfile = p->source_head;
-       sfile;
-       sfile = sfile->next)
-    {
-      dus.drb[i] = 0;
-      dus.spare[i] = 0;
-      dus.fname[i] = sfile->name;
-      i++;
-    }
-#else
   dus.drb[0] = 0;
   dus.fname[0] = sfile->name;
   dus.drb[0] = 0;
   dus.fname[0] = sfile->name;
-#endif
 
   sysroff_swap_dus_out (file, &dus);
 
 
   sysroff_swap_dus_out (file, &dus);
 
@@ -1362,69 +1308,6 @@ static void
 wr_dln (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile,
        int n ATTRIBUTE_UNUSED)
 {
 wr_dln (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile,
        int n ATTRIBUTE_UNUSED)
 {
-#if 0
-  if (n == 0)
-    {
-      /* Count up all the linenumbers */
-      struct coff_symbol *sy;
-      int lc = 0;
-      struct IT_dln dln;
-
-      int idx;
-
-      for (sy = p->symbol_list_head;
-          sy;
-          sy = sy->next_in_ofile_list)
-       {
-         struct coff_type *t = sy->type;
-         if (t->type == coff_function_type)
-           {
-             struct coff_line *l = t->u.function.lines;
-             lc += l->nlines;
-           }
-       }
-
-      dln.sfn = nints (lc);
-      dln.sln = nints (lc);
-      dln.lln = nints (lc);
-      dln.section = nints (lc);
-
-      dln.from_address = nints (lc);
-      dln.to_address = nints (lc);
-
-
-      dln.neg = 0x1001;
-
-      dln.nln = lc;
-
-      /* Run through once more and fill up the structure */
-      idx = 0;
-      for (sy = p->symbol_list_head;
-          sy;
-          sy = sy->next_in_ofile_list)
-       {
-         if (sy->type->type == coff_function_type)
-           {
-             int i;
-             struct coff_line *l = sy->type->u.function.lines;
-             for (i = 0; i < l->nlines; i++)
-               {
-                 dln.section[idx] = sy->where->section->number;
-                 dln.sfn[idx] = n;
-                 dln.sln[idx] = l->lines[i];
-                 dln.from_address[idx] = l->addresses[i];
-                 if (idx)
-                   dln.to_address[idx - 1] = dln.from_address[idx];
-                 idx++;
-               }
-           }
-         n++;
-       }
-      sysroff_swap_dln_out (file, &dln);
-    }
-
-#endif
-#if 1
   /* Count up all the linenumbers */
 
   struct coff_symbol *sy;
   /* Count up all the linenumbers */
 
   struct coff_symbol *sy;
@@ -1491,7 +1374,6 @@ wr_dln (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile,
     }
   if (lc)
     sysroff_swap_dln_out (file, &dln);
     }
   if (lc)
     sysroff_swap_dln_out (file, &dln);
-#endif
 }
 
 /* Write the global symbols out to the debug info.  */
 }
 
 /* Write the global symbols out to the debug info.  */
@@ -1579,7 +1461,10 @@ wr_cs (void)
     0x00,                      /* dot */
     0xDE                       /* CS */
   };
     0x00,                      /* dot */
     0xDE                       /* CS */
   };
-  fwrite (b, 1, sizeof (b), file);
+
+  if (fwrite (b, sizeof (b), 1, file) != 1)
+    /* FIXME: Return error status.  */
+    fatal (_("Failed to write CS struct"));
 }
 
 /* Write out the SC records for a unit.  Create an SC
 }
 
 /* Write out the SC records for a unit.  Create an SC
@@ -1692,17 +1577,11 @@ wr_sc (struct coff_ofile *ptr, struct coff_sfile *sfile)
        {
          sc.contents = CONTENTS_CODE;
        }
        {
          sc.contents = CONTENTS_CODE;
        }
-#if 0
-      /* NEW */
-      if (sc.length)
-       {
-#endif
-         sysroff_swap_sc_out (file, &sc);
-         scount++;
-#if 0
-       }
-#endif
+
+      sysroff_swap_sc_out (file, &sc);
+      scount++;
     }
     }
+  free (info);
   return scount;
 }
 
   return scount;
 }
 
@@ -1823,15 +1702,18 @@ align (int x)
    ordinary defs - dunno why, but thats what hitachi does with 'em.  */
 
 static void
    ordinary defs - dunno why, but thats what hitachi does with 'em.  */
 
 static void
-prescan (struct coff_ofile *tree)
+prescan (struct coff_ofile *otree)
 {
   struct coff_symbol *s;
   struct coff_section *common_section;
 
 {
   struct coff_symbol *s;
   struct coff_section *common_section;
 
+  if (otree->nsections < 3)
+    return;
+
   /* Find the common section - always section 3.  */
   /* Find the common section - always section 3.  */
-  common_section = tree->sections + 3;
+  common_section = otree->sections + 3;
 
 
-  for (s = tree->symbol_list_head;
+  for (s = otree->symbol_list_head;
        s;
        s = s->next_in_ofile_list)
     {
        s;
        s = s->next_in_ofile_list)
     {
@@ -1851,19 +1733,20 @@ prescan (struct coff_ofile *tree)
 char *program_name;
 
 static void
 char *program_name;
 
 static void
-show_usage (FILE *file, int status)
+show_usage (FILE *ffile, int status)
 {
 {
-  fprintf (file, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
-  fprintf (file, _("Convert a COFF object file into a SYSROFF object file\n"));
-  fprintf (file, _(" The options are:\n\
+  fprintf (ffile, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
+  fprintf (ffile, _("Convert a COFF object file into a SYSROFF object file\n"));
+  fprintf (ffile, _(" The options are:\n\
   -q --quick       (Obsolete - ignored)\n\
   -n --noprescan   Do not perform a scan to convert commons into defs\n\
   -d --debug       Display information about what is being done\n\
   -q --quick       (Obsolete - ignored)\n\
   -n --noprescan   Do not perform a scan to convert commons into defs\n\
   -d --debug       Display information about what is being done\n\
+  @<file>          Read options from <file>\n\
   -h --help        Display this information\n\
   -v --version     Print the program's version number\n"));
 
   -h --help        Display this information\n\
   -v --version     Print the program's version number\n"));
 
-  if (status == 0)
-    fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
+  if (REPORT_BUGS_TO[0] && status == 0)
+    fprintf (ffile, _("Report bugs to %s\n"), REPORT_BUGS_TO);
   exit (status);
 }
 
   exit (status);
 }
 
@@ -1895,6 +1778,9 @@ main (int ac, char **av)
 
   program_name = av[0];
   xmalloc_set_program_name (program_name);
 
   program_name = av[0];
   xmalloc_set_program_name (program_name);
+  bfd_set_error_program_name (program_name);
+
+  expandargv (&ac, &av);
 
   while ((opt = getopt_long (ac, av, "dHhVvqn", long_options,
                             (int *) NULL))
 
   while ((opt = getopt_long (ac, av, "dHhVvqn", long_options,
                             (int *) NULL))
@@ -1940,7 +1826,7 @@ main (int ac, char **av)
          ++optind;
          if (optind < ac)
            show_usage (stderr, 1);
          ++optind;
          if (optind < ac)
            show_usage (stderr, 1);
-         if (strcmp (input_file, output_file) == 0)
+         if (filename_cmp (input_file, output_file) == 0)
            {
              fatal (_("input and output files must be different"));
            }
            {
              fatal (_("input and output files must be different"));
            }
@@ -2004,10 +1890,12 @@ main (int ac, char **av)
     printf ("ids %d %d\n", base1, base2);
 
   tree = coff_grok (abfd);
     printf ("ids %d %d\n", base1, base2);
 
   tree = coff_grok (abfd);
+  if (tree)
+    {
+      if (!noprescan)
+       prescan (tree);
 
 
-  if (!noprescan)
-    prescan (tree);
-
-  wr_module (tree);
+      wr_module (tree);
+    }
   return 0;
 }
   return 0;
 }
This page took 0.032623 seconds and 4 git commands to generate.