[binutils-gdb] Stop the assembler from overwriting its output file.

Nick Clifton nickc@sourceware.org
Thu Apr 12 14:09:00 GMT 2018


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=67f846b59b32f3d704c601669409c2584383fea9

commit 67f846b59b32f3d704c601669409c2584383fea9
Author: John Darrington <john@darrington.wattle.id.au>
Date:   Thu Apr 12 15:07:02 2018 +0100

    Stop the assembler from overwriting its output file.
    
    	* as.c (main): Fail if the output is the same as one of the input
    	files.
    	* testsuite/gas/all/gas.exp: Test the new feature.

Diff:
---
 gas/ChangeLog                 |  6 ++++++
 gas/as.c                      | 22 ++++++++++++++++++++++
 gas/testsuite/gas/all/gas.exp |  2 ++
 3 files changed, 30 insertions(+)

diff --git a/gas/ChangeLog b/gas/ChangeLog
index f1c081a..d87d3e7 100644
--- a/gas/ChangeLog
+++ b/gas/ChangeLog
@@ -1,3 +1,9 @@
+2018-04-12  John Darrington  <john@darrington.wattle.id.au>
+
+	* as.c (main): Fail if the output is the same as one of the input
+	files.
+	* testsuite/gas/all/gas.exp: Test the new feature.
+
 2018-04-12  Nick Clifton  <nickc@redhat.com>
 
 	* po/es.po: Updated Spanish translation.
diff --git a/gas/as.c b/gas/as.c
index f6da1b1..6e8ec56 100644
--- a/gas/as.c
+++ b/gas/as.c
@@ -1171,6 +1171,7 @@ int
 main (int argc, char ** argv)
 {
   char ** argv_orig = argv;
+  struct stat sob;
 
   int macro_strip_at;
 
@@ -1218,6 +1219,27 @@ main (int argc, char ** argv)
   /* Call parse_args before any of the init/begin functions
      so that switches like --hash-size can be honored.  */
   parse_args (&argc, &argv);
+
+  if (argc > 1 && stat (out_file_name, &sob) == 0)
+    {
+      int i;
+
+      for (i = 1; i < argc; ++i)
+	{
+	  struct stat sib;
+
+	  if (stat (argv[i], &sib) == 0)
+	    {
+	      if (sib.st_ino == sob.st_ino)
+		{
+		  /* Don't let as_fatal remove the output file!  */
+		  out_file_name = NULL;
+		  as_fatal (_("The input and output files must be distinct"));
+		}
+	    }
+	}
+    }
+
   symbol_begin ();
   frag_init ();
   subsegs_begin ();
diff --git a/gas/testsuite/gas/all/gas.exp b/gas/testsuite/gas/all/gas.exp
index 0355a03..b3d662a 100644
--- a/gas/testsuite/gas/all/gas.exp
+++ b/gas/testsuite/gas/all/gas.exp
@@ -73,6 +73,8 @@ if {    ![istarget alpha*-*-*vms*]
 gas_test_error "equiv1.s" "" ".equiv for symbol already set to another one"
 gas_test_error "equiv2.s" "" ".equiv for symbol already set to an expression"
 
+gas_test_error "none.s" "-o $srcdir/$subdir/none.s" "Output file must be distinct from input"
+
 # .equ works differently on some targets.
 case $target_triplet in {
     { hppa*-*-* } { }



More information about the Binutils-cvs mailing list