Subversion Repositories Kolibri OS

Rev

Rev 5217 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* bucomm.c -- Bin Utils COMmon code.
  2.    Copyright (C) 1991-2015 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GNU Binutils.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  19.    02110-1301, USA.  */
  20. /* We might put this in a library someday so it could be dynamically
  21.    loaded, but for now it's not necessary.  */
  22.  
  23. #include "sysdep.h"
  24. #include "bfd.h"
  25. #include "libiberty.h"
  26. #include "filenames.h"
  27. #include "libbfd.h"
  28.  
  29. #include <time.h>               /* ctime, maybe time_t */
  30. #include <assert.h>
  31. #include "bucomm.h"
  32.  
  33. #ifndef HAVE_TIME_T_IN_TIME_H
  34. #ifndef HAVE_TIME_T_IN_TYPES_H
  35. typedef long time_t;
  36. #endif
  37. #endif
  38.  
  39. static const char * endian_string (enum bfd_endian);
  40. static int display_target_list (void);
  41. static int display_info_table (int, int);
  42. static int display_target_tables (void);
  43. /* Error reporting.  */
  44.  
  45. char *program_name;
  46.  
  47. void
  48. bfd_nonfatal (const char *string)
  49. {
  50.   const char *errmsg;
  51.  
  52.   errmsg = bfd_errmsg (bfd_get_error ());
  53.   fflush (stdout);
  54.   if (string)
  55.     fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
  56.   else
  57.     fprintf (stderr, "%s: %s\n", program_name, errmsg);
  58. }
  59.  
  60. /* Issue a non fatal error message.  FILENAME, or if NULL then BFD,
  61.    are used to indicate the problematic file.  SECTION, if non NULL,
  62.    is used to provide a section name.  If FORMAT is non-null, then it
  63.    is used to print additional information via vfprintf.  Finally the
  64.    bfd error message is printed.  In summary, error messages are of
  65.    one of the following forms:
  66.  
  67.    PROGRAM:file: bfd-error-message
  68.    PROGRAM:file[section]: bfd-error-message
  69.    PROGRAM:file: printf-message: bfd-error-message
  70.    PROGRAM:file[section]: printf-message: bfd-error-message.  */
  71.  
  72. void
  73. bfd_nonfatal_message (const char *filename,
  74.                       const bfd *abfd,
  75.                       const asection *section,
  76.                       const char *format, ...)
  77. {
  78.   const char *errmsg;
  79.   const char *section_name;
  80.   va_list args;
  81.  
  82.   errmsg = bfd_errmsg (bfd_get_error ());
  83.   fflush (stdout);
  84.   section_name = NULL;
  85.   va_start (args, format);
  86.   fprintf (stderr, "%s", program_name);
  87.  
  88.   if (abfd)
  89.     {
  90.       if (!filename)
  91.         filename = bfd_get_archive_filename (abfd);
  92.       if (section)
  93.         section_name = bfd_get_section_name (abfd, section);
  94.     }
  95.   if (section_name)
  96.     fprintf (stderr, ":%s[%s]", filename, section_name);
  97.   else
  98.     fprintf (stderr, ":%s", filename);
  99.  
  100.   if (format)
  101.     {
  102.       fprintf (stderr, ": ");
  103.       vfprintf (stderr, format, args);
  104.     }
  105.   fprintf (stderr, ": %s\n", errmsg);
  106.   va_end (args);
  107. }
  108.  
  109. void
  110. bfd_fatal (const char *string)
  111. {
  112.   bfd_nonfatal (string);
  113.   xexit (1);
  114. }
  115.  
  116. void
  117. report (const char * format, va_list args)
  118. {
  119.   fflush (stdout);
  120.   fprintf (stderr, "%s: ", program_name);
  121.   vfprintf (stderr, format, args);
  122.   putc ('\n', stderr);
  123. }
  124.  
  125. void
  126. fatal (const char *format, ...)
  127. {
  128.   va_list args;
  129.  
  130.   va_start (args, format);
  131.  
  132.   report (format, args);
  133.   va_end (args);
  134.   xexit (1);
  135. }
  136.  
  137. void
  138. non_fatal (const char *format, ...)
  139. {
  140.   va_list args;
  141.  
  142.   va_start (args, format);
  143.  
  144.   report (format, args);
  145.   va_end (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.           /* PR binutils/17605: Check for corrupt time values.  */
  429.           if (ctime_result == NULL)
  430.             sprintf (timebuf, _("<time data corrupt>"));
  431.           else
  432.           /* POSIX format:  skip weekday and seconds from ctime output.  */
  433.           sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
  434.  
  435.           mode_string (buf.st_mode, modebuf);
  436.           modebuf[10] = '\0';
  437.           size = buf.st_size;
  438.           /* POSIX 1003.2/D11 says to skip first character (entry type).  */
  439.           fprintf (file, "%s %ld/%ld %6" BFD_VMA_FMT "u %s ", modebuf + 1,
  440.                    (long) buf.st_uid, (long) buf.st_gid,
  441.                    size, timebuf);
  442.         }
  443.     }
  444.  
  445.   fprintf (file, "%s\n", bfd_get_filename (abfd));
  446. }
  447.  
  448. /* Return a path for a new temporary file in the same directory
  449.    as file PATH.  */
  450.  
  451. static char *
  452. template_in_dir (const char *path)
  453. {
  454. #define template "stXXXXXX"
  455.   const char *slash = strrchr (path, '/');
  456.   char *tmpname;
  457.   size_t len;
  458.  
  459. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  460.   {
  461.     /* We could have foo/bar\\baz, or foo\\bar, or d:bar.  */
  462.     char *bslash = strrchr (path, '\\');
  463.  
  464.     if (slash == NULL || (bslash != NULL && bslash > slash))
  465.       slash = bslash;
  466.     if (slash == NULL && path[0] != '\0' && path[1] == ':')
  467.       slash = path + 1;
  468.   }
  469. #endif
  470.  
  471.   if (slash != (char *) NULL)
  472.     {
  473.       len = slash - path;
  474.       tmpname = (char *) xmalloc (len + sizeof (template) + 2);
  475.       memcpy (tmpname, path, len);
  476.  
  477. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  478.       /* If tmpname is "X:", appending a slash will make it a root
  479.          directory on drive X, which is NOT the same as the current
  480.          directory on drive X.  */
  481.       if (len == 2 && tmpname[1] == ':')
  482.         tmpname[len++] = '.';
  483. #endif
  484.       tmpname[len++] = '/';
  485.     }
  486.   else
  487.     {
  488.       tmpname = (char *) xmalloc (sizeof (template));
  489.       len = 0;
  490.     }
  491.  
  492.   memcpy (tmpname + len, template, sizeof (template));
  493.   return tmpname;
  494. #undef template
  495. }
  496.  
  497. /* Return the name of a created temporary file in the same directory
  498.    as FILENAME.  */
  499.  
  500. char *
  501. make_tempname (char *filename)
  502. {
  503.   char *tmpname = template_in_dir (filename);
  504.   int fd;
  505.  
  506. #ifdef HAVE_MKSTEMP
  507.   fd = mkstemp (tmpname);
  508. #else
  509.   tmpname = mktemp (tmpname);
  510.   if (tmpname == NULL)
  511.     return NULL;
  512.   fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
  513. #endif
  514.   if (fd == -1)
  515.     {
  516.       free (tmpname);
  517.       return NULL;
  518.     }
  519.   close (fd);
  520.   return tmpname;
  521. }
  522.  
  523. /* Return the name of a created temporary directory inside the
  524.    directory containing FILENAME.  */
  525.  
  526. char *
  527. make_tempdir (char *filename)
  528. {
  529.   char *tmpname = template_in_dir (filename);
  530.  
  531. /*
  532. #ifdef HAVE_MKDTEMP
  533.   return mkdtemp (tmpname);
  534. #else
  535.   tmpname = mktemp (tmpname);
  536.   if (tmpname == NULL)
  537.     return NULL;
  538. #if defined (_WIN32) && !defined (__CYGWIN32__)
  539.   if (mkdir (tmpname) != 0)
  540.     return NULL;
  541. #else
  542.   if (mkdir (tmpname, 0700) != 0)
  543.     return NULL;
  544. #endif
  545.   return tmpname;
  546. #endif
  547. */
  548.   return NULL;
  549. }
  550.  
  551. /* Parse a string into a VMA, with a fatal error if it can't be
  552.    parsed.  */
  553.  
  554. bfd_vma
  555. parse_vma (const char *s, const char *arg)
  556. {
  557.   bfd_vma ret;
  558.   const char *end;
  559.  
  560.   ret = bfd_scan_vma (s, &end, 0);
  561.  
  562.   if (*end != '\0')
  563.     fatal (_("%s: bad number: %s"), arg, s);
  564.  
  565.   return ret;
  566. }
  567.  
  568. /* Returns the size of the named file.  If the file does not
  569.    exist, or if it is not a real file, then a suitable non-fatal
  570.    error message is printed and (off_t) -1 is returned.  */
  571.  
  572. off_t
  573. get_file_size (const char * file_name)
  574. {
  575.   struct stat statbuf;
  576.  
  577.   if (stat (file_name, &statbuf) < 0)
  578.     {
  579.       if (errno == ENOENT)
  580.         non_fatal (_("'%s': No such file"), file_name);
  581.       else
  582.         non_fatal (_("Warning: could not locate '%s'.  reason: %s"),
  583.                    file_name, strerror (errno));
  584.     }
  585.   else if (! S_ISREG (statbuf.st_mode))
  586.     non_fatal (_("Warning: '%s' is not an ordinary file"), file_name);
  587.   else if (statbuf.st_size < 0)
  588.     non_fatal (_("Warning: '%s' has negative size, probably it is too large"),
  589.                file_name);
  590.   else
  591.     return statbuf.st_size;
  592.  
  593.   return (off_t) -1;
  594. }
  595.  
  596. /* Return the filename in a static buffer.  */
  597.  
  598. const char *
  599. bfd_get_archive_filename (const bfd *abfd)
  600. {
  601.   static size_t curr = 0;
  602.   static char *buf;
  603.   size_t needed;
  604.  
  605.   assert (abfd != NULL);
  606.  
  607.   if (!abfd->my_archive)
  608.     return bfd_get_filename (abfd);
  609.  
  610.   needed = (strlen (bfd_get_filename (abfd->my_archive))
  611.             + strlen (bfd_get_filename (abfd)) + 3);
  612.   if (needed > curr)
  613.     {
  614.       if (curr)
  615.         free (buf);
  616.       curr = needed + (needed >> 1);
  617.       buf = (char *) bfd_malloc (curr);
  618.       /* If we can't malloc, fail safe by returning just the file name.
  619.          This function is only used when building error messages.  */
  620.       if (!buf)
  621.         {
  622.           curr = 0;
  623.           return bfd_get_filename (abfd);
  624.         }
  625.     }
  626.   sprintf (buf, "%s(%s)", bfd_get_filename (abfd->my_archive),
  627.            bfd_get_filename (abfd));
  628.   return buf;
  629. }
  630.  
  631. /* Returns TRUE iff PATHNAME, a filename of an archive member,
  632.    is valid for writing.  For security reasons absolute paths
  633.    and paths containing /../ are not allowed.  See PR 17533.  */
  634.  
  635. bfd_boolean
  636. is_valid_archive_path (char const * pathname)
  637. {
  638.   const char * n = pathname;
  639.  
  640.   if (IS_ABSOLUTE_PATH (n))
  641.     return FALSE;
  642.  
  643.   while (*n)
  644.     {
  645.       if (*n == '.' && *++n == '.' && ( ! *++n || IS_DIR_SEPARATOR (*n)))
  646.         return FALSE;
  647.  
  648.       while (*n && ! IS_DIR_SEPARATOR (*n))
  649.         n++;
  650.       while (IS_DIR_SEPARATOR (*n))
  651.         n++;
  652.     }
  653.  
  654.   return TRUE;
  655. }
  656.