MIPS: Support `-gnuabi64' target triplet suffix for 64-bit Linux targets
[binutils-gdb.git] / binutils / bucomm.c
index 9e6a02843e6c7ba11545f69a6a31e927d177a9b0..c268fd3d913d38ea2d8acc358c63f931ef0714df 100644 (file)
@@ -1,5 +1,5 @@
 /* bucomm.c -- Bin Utils COMmon code.
-   Copyright (C) 1991-2020 Free Software Foundation, Inc.
+   Copyright (C) 1991-2023 Free Software Foundation, Inc.
 
    This file is part of GNU Binutils.
 
 #include "bfd.h"
 #include "libiberty.h"
 #include "filenames.h"
-
-#include <time.h>              /* ctime, maybe time_t */
+#include <time.h>
 #include <assert.h>
 #include "bucomm.h"
-
-#ifndef HAVE_TIME_T_IN_TIME_H
-#ifndef HAVE_TIME_T_IN_TYPES_H
-typedef long time_t;
-#endif
-#endif
 \f
 /* Error reporting.  */
 
@@ -77,7 +70,6 @@ bfd_nonfatal_message (const char *filename,
 {
   const char *errmsg;
   const char *section_name;
-  va_list args;
   enum bfd_error err = bfd_get_error ();
 
   if (err == bfd_error_no_error)
@@ -86,7 +78,6 @@ bfd_nonfatal_message (const char *filename,
     errmsg = bfd_errmsg (err);
   fflush (stdout);
   section_name = NULL;
-  va_start (args, format);
   fprintf (stderr, "%s", program_name);
 
   if (abfd)
@@ -103,11 +94,13 @@ bfd_nonfatal_message (const char *filename,
 
   if (format)
     {
+      va_list args;
+      va_start (args, format);
       fprintf (stderr, ": ");
       vfprintf (stderr, format, args);
+      va_end (args);
     }
   fprintf (stderr, ": %s\n", errmsg);
-  va_end (args);
 }
 
 void
@@ -149,6 +142,19 @@ non_fatal (const char *format, ...)
   va_end (args);
 }
 
+/* Like xmalloc except that ABFD's objalloc memory is returned.
+   Use objalloc_free_block to free this memory and all more recently
+   allocated, or more usually, leave it to bfd_close to free.  */
+
+void *
+bfd_xalloc (bfd *abfd, size_t size)
+{
+  void *ret = bfd_alloc (abfd, size);
+  if (ret == NULL)
+    bfd_fatal (NULL);
+  return ret;
+}
+
 /* Set the default BFD target based on the configured target.  Doing
    this permits the binutils to be configured for a particular target,
    and linked against a shared BFD library which was configured for a
@@ -167,15 +173,17 @@ set_default_bfd_target (void)
 
 /* After a FALSE return from bfd_check_format_matches with
    bfd_get_error () == bfd_error_file_ambiguously_recognized, print
-   the possible matching targets.  */
+   the possible matching targets and free the list of targets.  */
 
 void
-list_matching_formats (char **p)
+list_matching_formats (char **matching)
 {
   fflush (stdout);
   fprintf (stderr, _("%s: Matching formats:"), program_name);
+  char **p = matching;
   while (*p)
     fprintf (stderr, " %s", *p++);
+  free (matching);
   fputc ('\n', stderr);
 }
 
@@ -435,7 +443,7 @@ display_info (void)
    Mode       User\tGroup\tSize\tDate               Name */
 
 void
-print_arelt_descr (FILE *file, bfd *abfd, bfd_boolean verbose, bfd_boolean offsets)
+print_arelt_descr (FILE *file, bfd *abfd, bool verbose, bool offsets)
 {
   struct stat buf;
 
@@ -447,7 +455,6 @@ print_arelt_descr (FILE *file, bfd *abfd, bfd_boolean verbose, bfd_boolean offse
          char timebuf[40];
          time_t when = buf.st_mtime;
          const char *ctime_result = (const char *) ctime (&when);
-         bfd_size_type size;
 
          /* PR binutils/17605: Check for corrupt time values.  */
          if (ctime_result == NULL)
@@ -458,11 +465,10 @@ print_arelt_descr (FILE *file, bfd *abfd, bfd_boolean verbose, bfd_boolean offse
 
          mode_string (buf.st_mode, modebuf);
          modebuf[10] = '\0';
-         size = buf.st_size;
          /* POSIX 1003.2/D11 says to skip first character (entry type).  */
-         fprintf (file, "%s %ld/%ld %6" BFD_VMA_FMT "u %s ", modebuf + 1,
+         fprintf (file, "%s %ld/%ld %6" PRIu64 " %s ", modebuf + 1,
                   (long) buf.st_uid, (long) buf.st_gid,
-                  size, timebuf);
+                  (uint64_t) buf.st_size, timebuf);
        }
     }
 
@@ -532,7 +538,7 @@ template_in_dir (const char *path)
    as FILENAME.  */
 
 char *
-make_tempname (const char *filename)
+make_tempname (const char *filename, int *ofd)
 {
   char *tmpname = template_in_dir (filename);
   int fd;
@@ -542,15 +548,17 @@ make_tempname (const char *filename)
 #else
   tmpname = mktemp (tmpname);
   if (tmpname == NULL)
-    return NULL;
-  fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
+    fd = -1;
+  else
+    fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
 #endif
   if (fd == -1)
     {
       free (tmpname);
+      bfd_set_error (bfd_error_system_call);
       return NULL;
     }
-  close (fd);
+  *ofd = fd;
   return tmpname;
 }
 
@@ -561,22 +569,26 @@ char *
 make_tempdir (const char *filename)
 {
   char *tmpname = template_in_dir (filename);
+  char *ret;
 
 #ifdef HAVE_MKDTEMP
-  return mkdtemp (tmpname);
+  ret = mkdtemp (tmpname);
 #else
-  tmpname = mktemp (tmpname);
-  if (tmpname == NULL)
-    return NULL;
+  ret = mktemp (tmpname);
 #if defined (_WIN32) && !defined (__CYGWIN32__)
   if (mkdir (tmpname) != 0)
-    return NULL;
+    ret = NULL;
 #else
   if (mkdir (tmpname, 0700) != 0)
-    return NULL;
+    ret = NULL;
 #endif
-  return tmpname;
 #endif
+  if (ret == NULL)
+    {
+      free (tmpname);
+      bfd_set_error (bfd_error_system_call);
+    }
+  return ret;
 }
 
 /* Parse a string into a VMA, with a fatal error if it can't be
@@ -623,6 +635,21 @@ get_file_size (const char * file_name)
   else if (statbuf.st_size < 0)
     non_fatal (_("Warning: '%s' has negative size, probably it is too large"),
                file_name);
+#if defined (_WIN32) && !defined (__CYGWIN__)
+  else if (statbuf.st_size == 0)
+    {
+      /* MS-Windows 'stat' reports the null device as a regular file;
+        fix that.  */
+      int fd = open (file_name, O_RDONLY | O_BINARY);
+      if (isatty (fd))
+       {
+         close (fd);
+         non_fatal (_("Warning: '%s' is not an ordinary file"),
+                    /* libtool wants to see /dev/null in the output.  */
+                    strcasecmp (file_name, "nul") ? file_name : "/dev/null");
+       }
+    }
+#endif
   else
     return statbuf.st_size;
 
@@ -662,18 +689,18 @@ bfd_get_archive_filename (const bfd *abfd)
    is valid for writing.  For security reasons absolute paths
    and paths containing /../ are not allowed.  See PR 17533.  */
 
-bfd_boolean
+bool
 is_valid_archive_path (char const * pathname)
 {
   const char * n = pathname;
 
   if (IS_ABSOLUTE_PATH (n))
-    return FALSE;
+    return false;
 
   while (*n)
     {
       if (*n == '.' && *++n == '.' && ( ! *++n || IS_DIR_SEPARATOR (*n)))
-       return FALSE;
+       return false;
 
       while (*n && ! IS_DIR_SEPARATOR (*n))
        n++;
@@ -681,5 +708,5 @@ is_valid_archive_path (char const * pathname)
        n++;
     }
 
-  return TRUE;
+  return true;
 }