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]

Re: [patch][plugins] Check the gcc version


> I will try to write a patch to use only headers.

It is attached. I works by creating plugin-version.h while building
gcc. That header file contains

-------------------------------------------------------
...
static char basever[] = "4.5.0";
static char datestamp[] = " 20090427";
static char devphase[] = " (experimental)";
static char revision[] = "" "FOO";
static struct plugin_gcc_version plugin_gcc_version = {basever, datestamp,
             devphase, revision,
             configuration_arguments};
------------------------------------------------

It is included by plugins.c and should also be included by any plugin
that wants to know the gcc version it is being build for.

At runtime, gcc passes its version to the plugin. The plugin can do
any checks it wants with it. In particular, it can check that the
running gcc is the one the plugin was compiled for.

One problem with the patch is that plugin-version.h is created by
running "gcc -E -P". This will not work if gcc is being compiled by
another compiler. What is the portable way to run the preprocessor?

Cheers,
-- 
Rafael Avila de Espindola

Google | Gordon House | Barrow Street | Dublin 4 | Ireland
Registered in Dublin, Ireland | Registration Number: 368047
diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index e01f0fb..bc6e99e 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -1158,7 +1158,6 @@ OBJS-common = \
 	params.o \
 	passes.o \
 	plugin.o \
-	plugin-version.o \
 	pointer-set.o \
 	postreload-gcse.o \
 	postreload.o \
@@ -2475,13 +2474,13 @@ passes.o : passes.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(TREE_H) \
    gt-passes.h $(DF_H) $(PREDICT_H)
 
 plugin.o : plugin.c $(PLUGIN_H) $(CONFIG_H) $(SYSTEM_H) coretypes.h \
-   $(TOPLEV_H) $(TREE_H) $(TREE_PASS_H) intl.h
+   $(TOPLEV_H) $(TREE_H) $(TREE_PASS_H) intl.h plugin-version.h
 
-plugin-version.o : plugin-version.c $(SYSTEM_H) gcc-plugin.h configargs.h
-	$(CC) $(ALL_CFLAGS) $(ALL_CPPFLAGS) \
+plugin-version.h : plugin-version.h.in configargs.h
+	$(CC) -E -P $(ALL_CFLAGS) $(ALL_CPPFLAGS) \
 	-DBASEVER=$(BASEVER_s) -DDATESTAMP=$(DATESTAMP_s) \
-	-DREVISION=$(REVISION_s) -DDEVPHASE=$(DEVPHASE_s) -c \
-	-DPLUGIN $(srcdir)/plugin-version.c $(OUTPUT_OPTION)
+	-DREVISION=$(REVISION_s) -DDEVPHASE=$(DEVPHASE_s)  \
+	-DPLUGIN -x c-header $(srcdir)/plugin-version.h.in $(OUTPUT_OPTION)
 
 main.o : main.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(TOPLEV_H)
 
diff --git a/gcc/gcc-plugin.h b/gcc/gcc-plugin.h
index 543dc93..3c10130 100644
--- a/gcc/gcc-plugin.h
+++ b/gcc/gcc-plugin.h
@@ -78,11 +78,10 @@ struct plugin_gcc_version
   const char *configuration_arguments;
 };
 
-extern struct plugin_gcc_version plugin_gcc_version;
-
 /* The default version check. Compares every field in VERSION. */
 
-extern bool plugin_default_version_check(struct plugin_gcc_version *version);
+extern bool plugin_default_version_check(struct plugin_gcc_version *,
+					 struct plugin_gcc_version *);
 
 /* Function type for the plugin initialization routine. Each plugin module
    should define this as an externally-visible function with name
diff --git a/gcc/plugin-version.c b/gcc/plugin-version.c
deleted file mode 100644
index b6f35a9..0000000
--- a/gcc/plugin-version.c
+++ /dev/null
@@ -1,36 +0,0 @@
-/* Version information for plugins.
-   Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
-   Free Software Foundation, Inc.
-
-This file is part of GCC.
-
-GCC is free software; you can redistribute it and/or modify it under
-the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 3, or (at your option) any later
-version.
-
-GCC is distributed in the hope that it will be useful, but WITHOUT ANY
-WARRANTY; without even the implied warranty of MERCHANTABILITY or
-FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-for more details.
-
-You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING3.  If not see
-<http://www.gnu.org/licenses/>.  */
-
-#include "system.h"
-#include "gcc-plugin.h"
-#include "configargs.h"
-
-static char basever[] = BASEVER;
-static char datestamp[] = DATESTAMP;
-static char devphase[] = DEVPHASE;
-static char revision[] = REVISION;
-
-/* FIXME plugins: We should make the version information more precise.
-   One way to do is to add a checksum. */
-
-struct plugin_gcc_version plugin_gcc_version = {basever, datestamp, devphase,
-						revision,
-						configuration_arguments};
diff --git a/gcc/plugin-version.h.in b/gcc/plugin-version.h.in
new file mode 100644
index 0000000..7488dca
--- /dev/null
+++ b/gcc/plugin-version.h.in
@@ -0,0 +1,35 @@
+/* Version information for plugins.
+   Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+   Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+
+#include "configargs.h"
+
+static char basever[] = BASEVER;
+static char datestamp[] = DATESTAMP;
+static char devphase[] = DEVPHASE;
+static char revision[] = REVISION;
+
+/* FIXME plugins: We should make the version information more precise.
+   One way to do is to add a checksum. */
+
+static struct plugin_gcc_version plugin_gcc_version = {basever, datestamp,
+						       devphase, revision,
+						       configuration_arguments};
diff --git a/gcc/plugin.c b/gcc/plugin.c
index c406c38..6070308 100644
--- a/gcc/plugin.c
+++ b/gcc/plugin.c
@@ -38,6 +38,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "intl.h"
 #include "plugin.h"
 #include "timevar.h"
+#include "plugin-version.h"
 
 /* Event names as strings.  Keep in sync with enum plugin_event.  */
 const char *plugin_event_name[] =
@@ -99,7 +100,6 @@ static struct pass_list_node *prev_added_pass_node;
 /* Each plugin should define an initialization function with exactly
    this name.  */
 static const char *str_plugin_init_func_name = "plugin_init";
-static const char *str_plugin_gcc_version_name = "plugin_gcc_version";
 #endif
 
 /* Helper function for the hash table that compares the base_name of the
@@ -567,10 +567,8 @@ try_init_one_plugin (struct plugin_name_args *plugin)
 {
   void *dl_handle;
   plugin_init_func plugin_init;
-  struct plugin_gcc_version *version;
   char *err;
   PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
-  PTR_UNION_TYPE (struct plugin_gcc_version*) version_union;
 
   dl_handle = dlopen (plugin->full_name, RTLD_NOW);
   if (!dl_handle)
@@ -593,12 +591,9 @@ try_init_one_plugin (struct plugin_name_args *plugin)
       return false;
     }
 
-  PTR_UNION_AS_VOID_PTR (version_union) =
-      dlsym (dl_handle, str_plugin_gcc_version_name);
-  version = PTR_UNION_AS_CAST_PTR (version_union);
-
   /* Call the plugin-provided initialization routine with the arguments.  */
-  if ((*plugin_init) (plugin->base_name, version, plugin->argc, plugin->argv))
+  if ((*plugin_init) (plugin->base_name, &plugin_gcc_version, plugin->argc,
+		      plugin->argv))
     {
       error ("Fail to initialize plugin %s", plugin->full_name);
       return false;
@@ -816,22 +811,23 @@ debug_active_plugins (void)
 /* The default version check. Compares every field in VERSION. */
 
 bool
-plugin_default_version_check(struct plugin_gcc_version *version)
+plugin_default_version_check(struct plugin_gcc_version *gcc_version,
+			     struct plugin_gcc_version *plugin_version)
 {
   /* version is NULL if the plugin was not linked with plugin-version.o */
-  if (!version)
+  if (!gcc_version || !plugin_version)
     return false;
 
-  if (strcmp (version->basever, plugin_gcc_version.basever))
+  if (strcmp (gcc_version->basever, plugin_version->basever))
     return false;
-  if (strcmp (version->datestamp, plugin_gcc_version.datestamp))
+  if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
     return false;
-  if (strcmp (version->devphase, plugin_gcc_version.devphase))
+  if (strcmp (gcc_version->devphase, plugin_version->devphase))
     return false;
-  if (strcmp (version->revision, plugin_gcc_version.revision))
+  if (strcmp (gcc_version->revision, plugin_version->revision))
     return false;
-  if (strcmp (version->configuration_arguments,
-	      plugin_gcc_version.configuration_arguments))
+  if (strcmp (gcc_version->configuration_arguments,
+	      plugin_version->configuration_arguments))
     return false;
   return true;
 }

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