Patchwork Prepend -lasan and disallow -static with -faddress-sanitizer

login
register
mail settings
Submitter H.J. Lu
Date Nov. 19, 2012, 4:58 a.m.
Message ID <20121119045854.GA31345@gmail.com>
Download mbox | patch
Permalink /patch/199922/
State New
Headers show

Comments

H.J. Lu - Nov. 19, 2012, 4:58 a.m.
Hi,

This patch prepens -lasan before any other libraries added by language.
It also disallows -static with -faddress-sanitizer.  OK to install?

Thanks.


H.J.
---
2012-11-18  H.J. Lu  <hongjiu.lu@intel.com>

	PR driver/55379
	PR driver/55374
	* gcc.c (prepend_lang_specific_driver): New function.
	(process_command): Use it.

Patch

diff --git a/gcc/gcc.c b/gcc/gcc.c
index 7a275e1..11279be4 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -3609,6 +3609,87 @@  set_option_handlers (struct cl_option_handlers *handlers)
   handlers->handlers[2].mask = CL_TARGET;
 }
 
+/* Prepend command line before language-specific adjustment/addition of
+   flags.  */
+
+void
+prepend_lang_specific_driver (struct cl_decoded_option **in_decoded_options,
+			      unsigned int *in_decoded_options_count,
+			      int *in_added_libraries)
+{
+  unsigned int i, argc;
+
+  /* The new argument list will be contained in this.  */
+  struct cl_decoded_option *new_decoded_options;
+
+  /* The argument list.  */
+  struct cl_decoded_option *decoded_options;
+
+  bool static_link = false;
+  bool add_libasan = false;
+  bool static_libasan = false;
+
+  argc = *in_decoded_options_count;
+  decoded_options = *in_decoded_options;
+
+  for (i = 1; i < argc; i++)
+    switch (decoded_options[i].opt_index)
+      {
+      case OPT_faddress_sanitizer:
+	add_libasan = true;
+	break;
+      case OPT_static:
+	static_link = true;
+	break;
+      case OPT_static_libasan:
+	static_libasan = true;
+	break;
+      }
+
+  if (add_libasan)
+    {
+      /* Add -lasan before language-specific adjustment/addition.  */
+      unsigned int added_argc;
+
+      if (static_link)
+	fatal_error ("cannot specify -static with -faddress-sanitizer");
+
+      added_argc = 1;
+#ifdef HAVE_LD_STATIC_DYNAMIC
+      if (static_libasan)
+	added_argc += 2;
+#endif
+
+      new_decoded_options = XNEWVEC (struct cl_decoded_option,
+				     argc + added_argc);
+
+      i = 0;
+      do
+	{
+	  new_decoded_options[i] = decoded_options[i];
+	  i++;
+	}
+      while (i < argc);
+
+#ifdef HAVE_LD_STATIC_DYNAMIC
+      if (static_libasan)
+	generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
+			 &new_decoded_options[i++]);
+#endif
+      generate_option (OPT_l, "asan", 1, CL_DRIVER,
+		       &new_decoded_options[i++]);
+#ifdef HAVE_LD_STATIC_DYNAMIC
+      if (static_libasan)
+	generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
+			 &new_decoded_options[i++]);
+#endif
+
+      *in_decoded_options_count = i;
+      *in_decoded_options = new_decoded_options;
+      *in_added_libraries = 1;
+    }
+}
+
 /* Create the vector `switches' and its contents.
    Store its length in `n_switches'.  */
 
@@ -3700,6 +3781,11 @@  process_command (unsigned int decoded_options_count,
      or an automatically created GCC_EXEC_PREFIX from
      decoded_options[0].arg.  */
 
+  /* Prepend command line before language-specific adjustment/addition of
+     flags.  */
+  prepend_lang_specific_driver (&decoded_options, &decoded_options_count,
+				&added_libraries);
+
   /* Do language-specific adjustment/addition of flags.  */
   lang_specific_driver (&decoded_options, &decoded_options_count,
 			&added_libraries);