This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH/RFC] ld checks for input/output file conflicts


Hi all,

I added some checks to ld, in order to avoid strange behaviour when
the input file is the same as the output or there are input file 
duplications. Especially in the first case, when the input is identical
to the output file, the file gets truncated, which is not so "user
friendly".

Comments?

ld/ChangeLog

2002-10-05  Elias Athanasopoulos  <eathan@otenet.gr>

	* ldlang.c (lang_file_exist): New function.
	(new_afile): Check if the file is already added to the list.
	Abort if the filename to be added matches the linker output
	filename.
	* ldlang.h: Add prototype for lang_file_exist.
	* lexsup.c (parse_args): Abort if the output filename matches
	one of the input filenames.


--- ldlang.c.orig	Fri Oct  4 19:15:58 2002
+++ ldlang.c	Sat Oct  5 20:05:26 2002
@@ -476,6 +476,27 @@ new_statement (type, size, list)
   return new;
 }
 
+/* Check if a file exist in the input_file_chain list */
+
+boolean
+lang_file_exist (name)
+    const char *name;
+{
+  lang_input_statement_type *p;
+    
+  for (p = (lang_input_statement_type *) input_file_chain.head;
+      p != (lang_input_statement_type *) NULL;
+      p = (lang_input_statement_type *) p->next_real_file)
+    {
+      if (p->filename != (char *) NULL
+          && name != (char *) NULL
+          && strcmp (p->filename, name) == 0)
+            return 1;
+    }
+
+    return 0;
+}
+
 /* Build a new input file node for the language.  There are several
    ways in which we treat an input file, eg, we only look at symbols,
    or prefix it with a -l etc.
@@ -493,7 +514,20 @@ new_afile (name, file_type, target, add_
      boolean add_to_list;
 {
   lang_input_statement_type *p;
-
+  
+  if (lang_file_exist (name))
+    {
+      einfo ("%P%F: ignoring input file %s, it has been already used.\n", name);
+      return NULL;
+    }
+  
+  /* We abort if the input file is identical with the output. */
+  if (name != NULL && output_filename != NULL && !strcmp (name, output_filename))
+    {
+      einfo ("%P%F: input file %s has been used as output. Aborting.\n", name);
+      xexit (1);
+    } 
+  
   if (add_to_list)
     p = new_stat (lang_input_statement, stat_ptr);
   else


--- ldlang.h.orig	Fri Oct  4 19:14:05 2002
+++ ldlang.h	Fri Oct  4 20:37:50 2002
@@ -483,5 +483,6 @@ extern void lang_register_vers_node
 	   struct bfd_elf_version_deps *));
 boolean unique_section_p PARAMS ((const char *));
 extern void lang_add_unique PARAMS ((const char *));
+extern boolean lang_file_exist PARAMS ((const char *));
 
 #endif


--- lexsup.c.orig	Fri Oct  4 18:54:41 2002
+++ lexsup.c	Sat Oct  5 19:43:53 2002
@@ -796,7 +796,12 @@ parse_args (argc, argv)
 	  link_info.optimize = strtoul (optarg, NULL, 0) ? true : false;
 	  break;
 	case 'o':
-	  lang_add_output (optarg, 0);
+          if (lang_file_exist (optarg))
+            {
+              einfo ("%P%F: output file %s has been used as input. Aborting.\n", optarg);
+              xexit (1);
+            }
+          lang_add_output (optarg, 0);
 	  break;
 	case OPTION_OFORMAT:
 	  lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]