[3/3] binutils: read from stdin if input file is -

Message ID 20180213092559.56981-4-ahmad@a3f.at
State New
Headers show
Series
  • binutils: read from stdin if input file is -
Related show

Commit Message

Ahmad Fatoum Feb. 13, 2018, 9:25 a.m.
This changes the behavior of:
dlltool, nlmconv, nm, objcopy, objdump and size

Opening files named - now requires prefixing ./ or similar.
---
 binutils/dlltool.c |  9 ++++++++-
 binutils/nlmconv.c |  5 ++++-
 binutils/nm.c      | 14 +++++++++++---
 binutils/objcopy.c | 38 ++++++++++++++++++++++++++++++--------
 binutils/objdump.c | 10 ++++++++--
 binutils/size.c    | 10 ++++++++--
 6 files changed, 69 insertions(+), 17 deletions(-)

-- 
2.16.1

Patch

diff --git a/binutils/dlltool.c b/binutils/dlltool.c
index 189907a0b0..37d244e0e7 100644
--- a/binutils/dlltool.c
+++ b/binutils/dlltool.c
@@ -1679,7 +1679,14 @@  scan_open_obj_file (bfd *abfd)
 static void
 scan_obj_file (const char *filename)
 {
-  bfd * f = bfd_openr (filename, 0);
+  bfd *f;
+  const char *filename_bfd = filename;
+  if (strcmp (filename_bfd, "-") == 0)
+    {
+      filename_bfd = NULL;
+      filename = "<stdin>";
+    }
+  f = bfd_openr (filename_bfd, 0);
 
   if (!f)
     /* xgettext:c-format */
diff --git a/binutils/nlmconv.c b/binutils/nlmconv.c
index 68941f80d0..ad9f04d1dd 100644
--- a/binutils/nlmconv.c
+++ b/binutils/nlmconv.c
@@ -199,6 +199,7 @@  main (int argc, char **argv)
   int len;
   char *modname;
   char **matching;
+  bfd_boolean read_stdin = FALSE;
 
 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
   setlocale (LC_MESSAGES, "");
@@ -261,6 +262,8 @@  main (int argc, char **argv)
     {
       input_file = argv[optind];
       ++optind;
+      if (strcmp (input_file, "-") == 0)
+        read_stdin = TRUE;
       if (optind < argc)
 	{
 	  output_file = argv[optind];
@@ -329,7 +332,7 @@  main (int argc, char **argv)
       show_usage (stderr, 1);
     }
 
-  inbfd = bfd_openr (input_file, input_format);
+  inbfd = bfd_openr (read_stdin ? NULL : input_file, input_format);
   if (inbfd == NULL)
     bfd_fatal (input_file);
 
diff --git a/binutils/nm.c b/binutils/nm.c
index 696eb7817e..77739a42f7 100644
--- a/binutils/nm.c
+++ b/binutils/nm.c
@@ -1299,11 +1299,19 @@  display_file (char *filename)
   bfd_boolean retval = TRUE;
   bfd *file;
   char **matching;
+  const char *filename_bfd = filename;
 
-  if (get_file_size (filename) < 1)
-    return FALSE;
+  if (strcmp (filename_bfd, "-") == 0)
+    {
+      filename_bfd= NULL;
+      filename = "<stdin>";
+    }
+  else if (get_file_size (filename) < 1)
+    {
+      return FALSE;
+    }
 
-  file = bfd_openr (filename, target ? target : plugin_target);
+  file = bfd_openr (filename_bfd, target ? target : plugin_target);
   if (file == NULL)
     {
       bfd_nonfatal (filename);
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index 8cdf27a87e..a7e5405646 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -3420,9 +3420,15 @@  copy_file (const char *input_filename, const char *output_filename,
   bfd *ibfd;
   char **obj_matching;
   char **core_matching;
-  off_t size = get_file_size (input_filename);
+  off_t size;
+  const char *filename_bfd = input_filename;
 
-  if (size < 1)
+  if (strcmp (filename_bfd, "-") == 0)
+    {
+        filename_bfd = NULL;
+        input_filename = "<stdin>";
+    }
+  else if ((size = get_file_size (input_filename)) < 1)
     {
       if (size == 0)
 	non_fatal (_("error: the input file '%s' is empty"),
@@ -3433,7 +3439,7 @@  copy_file (const char *input_filename, const char *output_filename,
 
   /* To allow us to do "strip *" without dying on the first
      non-object file, failures are nonfatal.  */
-  ibfd = bfd_openr (input_filename, input_target);
+  ibfd = bfd_openr (filename_bfd, input_target);
   if (ibfd == NULL)
     {
       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
@@ -4685,6 +4691,7 @@  copy_main (int argc, char *argv[])
   bfd_boolean show_version = FALSE;
   bfd_boolean change_warn = TRUE;
   bfd_boolean formats_info = FALSE;
+  bfd_boolean read_stdin = FALSE;
   int c;
   struct stat statbuf;
   const bfd_arch_info_type *input_arch = NULL;
@@ -5393,6 +5400,8 @@  copy_main (int argc, char *argv[])
     copy_usage (stderr, 1);
 
   input_filename = argv[optind];
+  if (strcmp (input_filename, "-") == 0)
+      read_stdin = TRUE;
   if (optind + 1 < argc)
     output_filename = argv[optind + 1];
 
@@ -5459,9 +5468,13 @@  copy_main (int argc, char *argv[])
     }
 
   if (preserve_dates)
-    if (stat (input_filename, & statbuf) < 0)
-      fatal (_("warning: could not locate '%s'.  System error message: %s"),
-	     input_filename, strerror (errno));
+    {
+      if (read_stdin)
+        fatal (_("warning: can't preserve date of input: <stdin>"));
+      if (stat (input_filename, & statbuf) < 0)
+        fatal (_("warning: could not locate '%s'.  System error message: %s"),
+               input_filename, strerror (errno));
+    }
 
   /* If there is no destination file, or the source and destination files
      are the same, then create a temp and rename the result into the input.  */
@@ -5481,8 +5494,17 @@  copy_main (int argc, char *argv[])
       if (preserve_dates)
 	set_times (tmpname, &statbuf);
       if (tmpname != output_filename)
-	status = (smart_rename (tmpname, input_filename,
-				preserve_dates) != 0);
+        {
+          if (read_stdin)
+            {
+               /* Might be nifty to dump the object to stdout if no destination file was
+                * specified. This requires ensuring nothing else writes to stdout though */
+               unlink_if_ordinary (tmpname);
+               fatal (_("error: source file <stdin> can't be reused as destination file"));
+            }
+
+          status = (smart_rename (tmpname, input_filename, preserve_dates) != 0);
+       }
     }
   else
     unlink_if_ordinary (tmpname);
diff --git a/binutils/objdump.c b/binutils/objdump.c
index 37a9f0d2e1..f3797e420b 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -3758,14 +3758,20 @@  static void
 display_file (char *filename, char *target, bfd_boolean last_file)
 {
   bfd *file;
+  const char *filename_bfd = filename;
 
-  if (get_file_size (filename) < 1)
+  if (strcmp (filename_bfd, "-") == 0)
+    {
+      filename_bfd = NULL;
+      filename = "<stdin>";
+    }
+  else if (get_file_size (filename) < 1)
     {
       exit_status = 1;
       return;
     }
 
-  file = bfd_openr (filename, target);
+  file = bfd_openr (filename_bfd, target);
   if (file == NULL)
     {
       nonfatal (filename);
diff --git a/binutils/size.c b/binutils/size.c
index 47f14fce33..82080d8bee 100644
--- a/binutils/size.c
+++ b/binutils/size.c
@@ -385,14 +385,20 @@  static void
 display_file (char *filename)
 {
   bfd *file;
+  const char *filename_bfd = filename;
 
-  if (get_file_size (filename) < 1)
+  if (strcmp (filename_bfd, "-") == 0)
+    {
+      filename_bfd = NULL;
+      filename = "<stdin>";
+    }
+  else if (get_file_size (filename) < 1)
     {
       return_code = 1;
       return;
     }
 
-  file = bfd_openr (filename, target);
+  file = bfd_openr (filename_bfd, target);
   if (file == NULL)
     {
       bfd_nonfatal (filename);