Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* bucomm.c -- Bin Utils COMmon code.
  2.    Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002,
  3.    2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
  4.    Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU Binutils.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 3 of the License, or
  11.    (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  21.    02110-1301, USA.  */
  22. /* We might put this in a library someday so it could be dynamically
  23.    loaded, but for now it's not necessary.  */
  24.  
  25. #include "sysdep.h"
  26. #include "bfd.h"
  27. #include "libiberty.h"
  28. #include "filenames.h"
  29. #include "libbfd.h"
  30.  
  31. #include <time.h>               /* ctime, maybe time_t */
  32. #include <assert.h>
  33. #include "bucomm.h"
  34.  
  35. #ifndef HAVE_TIME_T_IN_TIME_H
  36. #ifndef HAVE_TIME_T_IN_TYPES_H
  37. typedef long time_t;
  38. #endif
  39. #endif
  40.  
  41. static const char * endian_string (enum bfd_endian);
  42. static int display_target_list (void);
  43. static int display_info_table (int, int);
  44. static int display_target_tables (void);
  45. /* Error reporting.  */
  46.  
  47. char *program_name;
  48.  
  49. void
  50. bfd_nonfatal (const char *string)
  51. {
  52.   const char *errmsg;
  53.  
  54.   errmsg = bfd_errmsg (bfd_get_error ());
  55.   fflush (stdout);
  56.   if (string)
  57.     fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
  58.   else
  59.     fprintf (stderr, "%s: %s\n", program_name, errmsg);
  60. }
  61.  
  62. /* Issue a non fatal error message.  FILENAME, or if NULL then BFD,
  63.    are used to indicate the problematic file.  SECTION, if non NULL,
  64.    is used to provide a section name.  If FORMAT is non-null, then it
  65.    is used to print additional information via vfprintf.  Finally the
  66.    bfd error message is printed.  In summary, error messages are of
  67.    one of the following forms:
  68.  
  69.    PROGRAM:file: bfd-error-message
  70.    PROGRAM:file[section]: bfd-error-message
  71.    PROGRAM:file: printf-message: bfd-error-message
  72.    PROGRAM:file[section]: printf-message: bfd-error-message.  */
  73.  
  74. void
  75. bfd_nonfatal_message (const char *filename,
  76.                       const bfd *abfd,
  77.                       const asection *section,
  78.                       const char *format, ...)
  79. {
  80.   const char *errmsg;
  81.   const char *section_name;
  82.   va_list args;
  83.  
  84.   errmsg = bfd_errmsg (bfd_get_error ());
  85.   fflush (stdout);
  86.   section_name = NULL;
  87.   va_start (args, format);
  88.   fprintf (stderr, "%s", program_name);
  89.  
  90.   if (abfd)
  91.     {
  92.       if (!filename)
  93.         filename = bfd_get_archive_filename (abfd);
  94.       if (section)
  95.         section_name = bfd_get_section_name (abfd, section);
  96.     }
  97.   if (section_name)
  98.     fprintf (stderr, ":%s[%s]", filename, section_name);
  99.   else
  100.     fprintf (stderr, ":%s", filename);
  101.  
  102.   if (format)
  103.     {
  104.       fprintf (stderr, ": ");
  105.       vfprintf (stderr, format, args);
  106.     }
  107.   fprintf (stderr, ": %s\n", errmsg);
  108.   va_end (args);
  109. }
  110.  
  111. void
  112. bfd_fatal (const char *string)
  113. {
  114.   bfd_nonfatal (string);
  115.   xexit (1);
  116. }
  117.  
  118. void
  119. report (const char * format, va_list args)
  120. {
  121.   fflush (stdout);
  122.   fprintf (stderr, "%s: ", program_name);
  123.   vfprintf (stderr, format, args);
  124.   putc ('\n', stderr);
  125. }
  126.  
  127. void
  128. fatal VPARAMS ((const char *format, ...))
  129. {
  130.   VA_OPEN (args, format);
  131.   VA_FIXEDARG (args, const char *, format);
  132.  
  133.   report (format, args);
  134.   VA_CLOSE (args);
  135.   xexit (1);
  136. }
  137.  
  138. void
  139. non_fatal VPARAMS ((const char *format, ...))
  140. {
  141.   VA_OPEN (args, format);
  142.   VA_FIXEDARG (args, const char *, format);
  143.  
  144.   report (format, args);
  145.   VA_CLOSE (args);
  146. }
  147.  
  148. /* Set the default BFD target based on the configured target.  Doing
  149.    this permits the binutils to be configured for a particular target,
  150.    and linked against a shared BFD library which was configured for a
  151.    different target.  */
  152.  
  153. void
  154. set_default_bfd_target (void)
  155. {
  156.   /* The macro TARGET is defined by Makefile.  */
  157.   const char *target = TARGET;
  158.  
  159.   if (! bfd_set_default_target (target))
  160.     fatal (_("can't set BFD default target to `%s': %s"),
  161.            target, bfd_errmsg (bfd_get_error ()));
  162. }
  163.  
  164. /* After a FALSE return from bfd_check_format_matches with
  165.    bfd_get_error () == bfd_error_file_ambiguously_recognized, print
  166.    the possible matching targets.  */
  167.  
  168. void
  169. list_matching_formats (char **p)
  170. {
  171.   fflush (stdout);
  172.   fprintf (stderr, _("%s: Matching formats:"), program_name);
  173.   while (*p)
  174.     fprintf (stderr, " %s", *p++);
  175.   fputc ('\n', stderr);
  176. }
  177.  
  178. /* List the supported targets.  */
  179.  
  180. void
  181. list_supported_targets (const char *name, FILE *f)
  182. {
  183.   int t;
  184.   const char **targ_names;
  185.  
  186.   if (name == NULL)
  187.     fprintf (f, _("Supported targets:"));
  188.   else
  189.     fprintf (f, _("%s: supported targets:"), name);
  190.  
  191.   targ_names = bfd_target_list ();
  192.   for (t = 0; targ_names[t] != NULL; t++)
  193.     fprintf (f, " %s", targ_names[t]);
  194.   fprintf (f, "\n");
  195.   free (targ_names);
  196. }
  197.  
  198. /* List the supported architectures.  */
  199.  
  200. void
  201. list_supported_architectures (const char *name, FILE *f)
  202. {
  203.   const char ** arch;
  204.   const char ** arches;
  205.  
  206.   if (name == NULL)
  207.     fprintf (f, _("Supported architectures:"));
  208.   else
  209.     fprintf (f, _("%s: supported architectures:"), name);
  210.  
  211.   for (arch = arches = bfd_arch_list (); *arch; arch++)
  212.     fprintf (f, " %s", *arch);
  213.   fprintf (f, "\n");
  214.   free (arches);
  215. }
  216. /* The length of the longest architecture name + 1.  */
  217. #define LONGEST_ARCH sizeof ("powerpc:common")
  218.  
  219. static const char *
  220. endian_string (enum bfd_endian endian)
  221. {
  222.   switch (endian)
  223.     {
  224.     case BFD_ENDIAN_BIG: return _("big endian");
  225.     case BFD_ENDIAN_LITTLE: return _("little endian");
  226.     default: return _("endianness unknown");
  227.     }
  228. }
  229.  
  230. /* List the targets that BFD is configured to support, each followed
  231.    by its endianness and the architectures it supports.  */
  232.  
  233. static int
  234. display_target_list (void)
  235. {
  236.   char *dummy_name;
  237.   int t;
  238.   int ret = 1;
  239.  
  240.   dummy_name = make_temp_file (NULL);
  241.   for (t = 0; bfd_target_vector[t]; t++)
  242.     {
  243.       const bfd_target *p = bfd_target_vector[t];
  244.       bfd *abfd = bfd_openw (dummy_name, p->name);
  245.       int a;
  246.  
  247.       printf (_("%s\n (header %s, data %s)\n"), p->name,
  248.               endian_string (p->header_byteorder),
  249.               endian_string (p->byteorder));
  250.  
  251.       if (abfd == NULL)
  252.         {
  253.           bfd_nonfatal (dummy_name);
  254.           ret = 0;
  255.           continue;
  256.         }
  257.  
  258.       if (! bfd_set_format (abfd, bfd_object))
  259.         {
  260.           if (bfd_get_error () != bfd_error_invalid_operation)
  261.             {
  262.               bfd_nonfatal (p->name);
  263.               ret = 0;
  264.             }
  265.           bfd_close_all_done (abfd);
  266.           continue;
  267.         }
  268.  
  269.       for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
  270.         if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
  271.           printf ("  %s\n",
  272.                   bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
  273.       bfd_close_all_done (abfd);
  274.     }
  275.   unlink (dummy_name);
  276.   free (dummy_name);
  277.  
  278.   return ret;
  279. }
  280.  
  281. /* Print a table showing which architectures are supported for entries
  282.    FIRST through LAST-1 of bfd_target_vector (targets across,
  283.    architectures down).  */
  284.  
  285. static int
  286. display_info_table (int first, int last)
  287. {
  288.   int t;
  289.   int ret = 1;
  290.   char *dummy_name;
  291.   int a;
  292.  
  293.   /* Print heading of target names.  */
  294.   printf ("\n%*s", (int) LONGEST_ARCH, " ");
  295.   for (t = first; t < last && bfd_target_vector[t]; t++)
  296.     printf ("%s ", bfd_target_vector[t]->name);
  297.   putchar ('\n');
  298.  
  299.   dummy_name = make_temp_file (NULL);
  300.   for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
  301.     if (strcmp (bfd_printable_arch_mach ((enum bfd_architecture) a, 0),
  302.                 "UNKNOWN!") != 0)
  303.       {
  304.         printf ("%*s ", (int) LONGEST_ARCH - 1,
  305.                 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
  306.         for (t = first; t < last && bfd_target_vector[t]; t++)
  307.           {
  308.             const bfd_target *p = bfd_target_vector[t];
  309.             bfd_boolean ok = TRUE;
  310.             bfd *abfd = bfd_openw (dummy_name, p->name);
  311.  
  312.             if (abfd == NULL)
  313.               {
  314.                 bfd_nonfatal (p->name);
  315.                 ret = 0;
  316.                 ok = FALSE;
  317.               }
  318.  
  319.             if (ok)
  320.               {
  321.                 if (! bfd_set_format (abfd, bfd_object))
  322.                   {
  323.                     if (bfd_get_error () != bfd_error_invalid_operation)
  324.                       {
  325.                         bfd_nonfatal (p->name);
  326.                         ret = 0;
  327.                       }
  328.                     ok = FALSE;
  329.                   }
  330.               }
  331.  
  332.             if (ok)
  333.               {
  334.                 if (! bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
  335.                   ok = FALSE;
  336.               }
  337.  
  338.             if (ok)
  339.               printf ("%s ", p->name);
  340.             else
  341.               {
  342.                 int l = strlen (p->name);
  343.                 while (l--)
  344.                   putchar ('-');
  345.                 putchar (' ');
  346.               }
  347.             if (abfd != NULL)
  348.               bfd_close_all_done (abfd);
  349.           }
  350.         putchar ('\n');
  351.       }
  352.   unlink (dummy_name);
  353.   free (dummy_name);
  354.  
  355.   return ret;
  356. }
  357.  
  358. /* Print tables of all the target-architecture combinations that
  359.    BFD has been configured to support.  */
  360.  
  361. static int
  362. display_target_tables (void)
  363. {
  364.   int t;
  365.   int columns;
  366.   int ret = 1;
  367.   char *colum;
  368.  
  369.   columns = 0;
  370.   colum = getenv ("COLUMNS");
  371.   if (colum != NULL)
  372.     columns = atoi (colum);
  373.   if (columns == 0)
  374.     columns = 80;
  375.  
  376.   t = 0;
  377.   while (bfd_target_vector[t] != NULL)
  378.     {
  379.       int oldt = t, wid;
  380.  
  381.       wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
  382.       ++t;
  383.       while (wid < columns && bfd_target_vector[t] != NULL)
  384.         {
  385.           int newwid;
  386.  
  387.           newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
  388.           if (newwid >= columns)
  389.             break;
  390.           wid = newwid;
  391.           ++t;
  392.         }
  393.       if (! display_info_table (oldt, t))
  394.         ret = 0;
  395.     }
  396.  
  397.   return ret;
  398. }
  399.  
  400. int
  401. display_info (void)
  402. {
  403.   printf (_("BFD header file version %s\n"), BFD_VERSION_STRING);
  404.   if (! display_target_list () || ! display_target_tables ())
  405.     return 1;
  406.   else
  407.     return 0;
  408. }
  409. /* Display the archive header for an element as if it were an ls -l listing:
  410.  
  411.    Mode       User\tGroup\tSize\tDate               Name */
  412.  
  413. void
  414. print_arelt_descr (FILE *file, bfd *abfd, bfd_boolean verbose)
  415. {
  416.   struct stat buf;
  417.  
  418.   if (verbose)
  419.     {
  420.       if (bfd_stat_arch_elt (abfd, &buf) == 0)
  421.         {
  422.           char modebuf[11];
  423.           char timebuf[40];
  424.           time_t when = buf.st_mtime;
  425.           const char *ctime_result = (const char *) ctime (&when);
  426.           bfd_size_type size;
  427.  
  428.           /* POSIX format:  skip weekday and seconds from ctime output.  */
  429.           sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
  430.  
  431.           mode_string (buf.st_mode, modebuf);
  432.           modebuf[10] = '\0';
  433.           size = buf.st_size;
  434.           /* POSIX 1003.2/D11 says to skip first character (entry type).  */
  435.           fprintf (file, "%s %ld/%ld %6" BFD_VMA_FMT "u %s ", modebuf + 1,
  436.                    (long) buf.st_uid, (long) buf.st_gid,
  437.                    size, timebuf);
  438.         }
  439.     }
  440.  
  441.   fprintf (file, "%s\n", bfd_get_filename (abfd));
  442. }
  443.  
  444. /* Return a path for a new temporary file in the same directory
  445.    as file PATH.  */
  446.  
  447. static char *
  448. template_in_dir (const char *path)
  449. {
  450. #define template "stXXXXXX"
  451.   const char *slash = strrchr (path, '/');
  452.   char *tmpname;
  453.   size_t len;
  454.  
  455. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  456.   {
  457.     /* We could have foo/bar\\baz, or foo\\bar, or d:bar.  */
  458.     char *bslash = strrchr (path, '\\');
  459.  
  460.     if (slash == NULL || (bslash != NULL && bslash > slash))
  461.       slash = bslash;
  462.     if (slash == NULL && path[0] != '\0' && path[1] == ':')
  463.       slash = path + 1;
  464.   }
  465. #endif
  466.  
  467.   if (slash != (char *) NULL)
  468.     {
  469.       len = slash - path;
  470.       tmpname = (char *) xmalloc (len + sizeof (template) + 2);
  471.       memcpy (tmpname, path, len);
  472.  
  473. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  474.       /* If tmpname is "X:", appending a slash will make it a root
  475.          directory on drive X, which is NOT the same as the current
  476.          directory on drive X.  */
  477.       if (len == 2 && tmpname[1] == ':')
  478.         tmpname[len++] = '.';
  479. #endif
  480.       tmpname[len++] = '/';
  481.     }
  482.   else
  483.     {
  484.       tmpname = (char *) xmalloc (sizeof (template));
  485.       len = 0;
  486.     }
  487.  
  488.   memcpy (tmpname + len, template, sizeof (template));
  489.   return tmpname;
  490. #undef template
  491. }
  492.  
  493. /* Return the name of a created temporary file in the same directory
  494.    as FILENAME.  */
  495.  
  496. char *
  497. make_tempname (char *filename)
  498. {
  499.   char *tmpname = template_in_dir (filename);
  500.   int fd;
  501.  
  502. #ifdef HAVE_MKSTEMP
  503.   fd = mkstemp (tmpname);
  504. #else
  505.   tmpname = mktemp (tmpname);
  506.   if (tmpname == NULL)
  507.     return NULL;
  508.   fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
  509. #endif
  510.   if (fd == -1)
  511.     {
  512.       free (tmpname);
  513.       return NULL;
  514.     }
  515.   close (fd);
  516.   return tmpname;
  517. }
  518.  
  519. /* Return the name of a created temporary directory inside the
  520.    directory containing FILENAME.  */
  521.  
  522. char *
  523. make_tempdir (char *filename)
  524. {
  525.   char *tmpname = template_in_dir (filename);
  526.  
  527. /*
  528. #ifdef HAVE_MKDTEMP
  529.   return mkdtemp (tmpname);
  530. #else
  531.   tmpname = mktemp (tmpname);
  532.   if (tmpname == NULL)
  533.     return NULL;
  534. #if defined (_WIN32) && !defined (__CYGWIN32__)
  535.   if (mkdir (tmpname) != 0)
  536.     return NULL;
  537. #else
  538.   if (mkdir (tmpname, 0700) != 0)
  539.     return NULL;
  540. #endif
  541.   return tmpname;
  542. #endif
  543. */
  544.   return NULL;
  545. }
  546.  
  547. /* Parse a string into a VMA, with a fatal error if it can't be
  548.    parsed.  */
  549.  
  550. bfd_vma
  551. parse_vma (const char *s, const char *arg)
  552. {
  553.   bfd_vma ret;
  554.   const char *end;
  555.  
  556.   ret = bfd_scan_vma (s, &end, 0);
  557.  
  558.   if (*end != '\0')
  559.     fatal (_("%s: bad number: %s"), arg, s);
  560.  
  561.   return ret;
  562. }
  563.  
  564. /* Returns the size of the named file.  If the file does not
  565.    exist, or if it is not a real file, then a suitable non-fatal
  566.    error message is printed and (off_t) -1 is returned.  */
  567.  
  568. off_t
  569. get_file_size (const char * file_name)
  570. {
  571.   struct stat statbuf;
  572.  
  573.   if (stat (file_name, &statbuf) < 0)
  574.     {
  575.       if (errno == ENOENT)
  576.         non_fatal (_("'%s': No such file"), file_name);
  577.       else
  578.         non_fatal (_("Warning: could not locate '%s'.  reason: %s"),
  579.                    file_name, strerror (errno));
  580.     }
  581.   else if (! S_ISREG (statbuf.st_mode))
  582.     non_fatal (_("Warning: '%s' is not an ordinary file"), file_name);
  583.   else if (statbuf.st_size < 0)
  584.     non_fatal (_("Warning: '%s' has negative size, probably it is too large"),
  585.                file_name);
  586.   else
  587.     return statbuf.st_size;
  588.  
  589.   return (off_t) -1;
  590. }
  591.  
  592. /* Return the filename in a static buffer.  */
  593.  
  594. const char *
  595. bfd_get_archive_filename (const bfd *abfd)
  596. {
  597.   static size_t curr = 0;
  598.   static char *buf;
  599.   size_t needed;
  600.  
  601.   assert (abfd != NULL);
  602.  
  603.   if (!abfd->my_archive)
  604.     return bfd_get_filename (abfd);
  605.  
  606.   needed = (strlen (bfd_get_filename (abfd->my_archive))
  607.             + strlen (bfd_get_filename (abfd)) + 3);
  608.   if (needed > curr)
  609.     {
  610.       if (curr)
  611.         free (buf);
  612.       curr = needed + (needed >> 1);
  613.       buf = (char *) bfd_malloc (curr);
  614.       /* If we can't malloc, fail safe by returning just the file name.
  615.          This function is only used when building error messages.  */
  616.       if (!buf)
  617.         {
  618.           curr = 0;
  619.           return bfd_get_filename (abfd);
  620.         }
  621.     }
  622.   sprintf (buf, "%s(%s)", bfd_get_filename (abfd->my_archive),
  623.            bfd_get_filename (abfd));
  624.   return buf;
  625. }
  626.