This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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] Prevent LTO wrappers to process a recursive execution


Hello.

To make LTO wrappers (gcc-nm, gcc-ar, gcc-ranlib) more smart, I would like to prevent execution
of the same binary by these wrapper. For LTO testing I symlink ar (nm, ranlib) to these wrappers instead
of hacking a build system to respect NM (AR, RANLIB) environment variables. The only problem with that solution
is that these wrappers recursively executes themselves as the first folder in PATH is set to the location with wrappers.

Following patch presents such recursion.

Patch can bootstrap&regtest on x86_64-linux-gnu.

Ready for trunk?
Thanks,
Martin
>From dfe0486ad7babe3d6de349001d4790684dc94bfb Mon Sep 17 00:00:00 2001
From: marxin <mliska@suse.cz>
Date: Fri, 22 Apr 2016 17:57:23 +0200
Subject: [PATCH] Prevent LTO wrappers to process a recursive execution

gcc/ChangeLog:

2016-04-22  Martin Liska  <mliska@suse.cz>

	* file-find.c (remove_prefix): New function.
	* file-find.h (remove_prefix): Declare the function.
	* gcc-ar.c (main): Skip a folder of the wrapper if
	a wrapped binary would point to the same file.
---
 gcc/file-find.c | 35 +++++++++++++++++++++++++++++++++++
 gcc/file-find.h |  1 +
 gcc/gcc-ar.c    |  8 ++++++++
 3 files changed, 44 insertions(+)

diff --git a/gcc/file-find.c b/gcc/file-find.c
index 289ef28..1066da9 100644
--- a/gcc/file-find.c
+++ b/gcc/file-find.c
@@ -208,3 +208,38 @@ prefix_from_string (const char *p, struct path_prefix *pprefix)
     }
   free (nstore);
 }
+
+void
+remove_prefix (const char *prefix, struct path_prefix *pprefix)
+{
+  struct prefix_list *remove, **prev, **remove_prev = NULL;
+  int max_len = 0;
+
+  if (pprefix->plist)
+    {
+      prev = &pprefix->plist;
+      for (struct prefix_list *pl = pprefix->plist; pl->next; pl = pl->next)
+	{
+	  if (strcmp (prefix, pl->prefix) == 0)
+	    {
+	      remove = pl;
+	      remove_prev = prev;
+	      continue;
+	    }
+
+	  int l = strlen (pl->prefix);
+	  if (l > max_len)
+	    max_len = l;
+
+	  prev = &pl;
+	}
+
+      if (remove_prev)
+	{
+	  *remove_prev = remove->next;
+	  free (remove);
+	}
+
+      pprefix->max_len = max_len;
+    }
+}
diff --git a/gcc/file-find.h b/gcc/file-find.h
index 5ad9a5f..19a4746 100644
--- a/gcc/file-find.h
+++ b/gcc/file-find.h
@@ -41,6 +41,7 @@ extern void find_file_set_debug (bool);
 extern char *find_a_file (struct path_prefix *, const char *, int);
 extern void add_prefix (struct path_prefix *, const char *);
 extern void add_prefix_begin (struct path_prefix *, const char *);
+extern void remove_prefix (const char *prefix, struct path_prefix *);
 extern void prefix_from_env (const char *, struct path_prefix *);
 extern void prefix_from_string (const char *, struct path_prefix *);
 
diff --git a/gcc/gcc-ar.c b/gcc/gcc-ar.c
index 45ba361..a02dccb 100644
--- a/gcc/gcc-ar.c
+++ b/gcc/gcc-ar.c
@@ -194,6 +194,14 @@ main (int ac, char **av)
 #ifdef CROSS_DIRECTORY_STRUCTURE
       real_exe_name = concat (target_machine, "-", PERSONALITY, NULL);
 #endif
+      /* Do not search original location in the same folder.  */
+      char *exe_folder = lrealpath (av[0]);
+      exe_folder[strlen (exe_folder) - strlen (lbasename (exe_folder))] = '\0';
+      char *location = concat (exe_folder, PERSONALITY, NULL);
+
+      if (access (location, X_OK) == 0)
+	remove_prefix (exe_folder, &path);
+
       exe_name = find_a_file (&path, real_exe_name, X_OK);
       if (!exe_name)
 	{
-- 
2.8.1


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