Skip to content

Instantly share code, notes, and snippets.

@Feandil
Last active March 18, 2017 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Feandil/55c104a4e9f5e6c72ce610b152ce37c3 to your computer and use it in GitHub Desktop.
Save Feandil/55c104a4e9f5e6c72ce610b152ce37c3 to your computer and use it in GitHub Desktop.
GCC-Plugin reproducer
#include "bversion.h"
#if BUILDING_GCC_VERSION >= 6000
#include "gcc-plugin.h"
#else
#include "plugin.h"
#endif
#define __visible __attribute__((visibility("default")))
__visible int plugin_is_GPL_compatible;
static struct plugin_info basic_plugin_info = {
.version = "201703181846",
.help = "Very basic plugin that does nothing",
};
__visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
{
const char * const plugin_name = plugin_info->base_name;
register_callback(plugin_name, PLUGIN_INFO, NULL, &basic_plugin_info);
return 0;
}
int main(void)
{
return 0;
}
--- a/plugin.c
+++ b/plugin.c
@@ -143,6 +143,25 @@
return base_name;
}
+void dump_test_plugin_adresses(const char * context)
+{
+ int i;
+ fprintf(stderr, "Testing known plugins %s\n", context);
+ for (i = 1; i < 12; ++i) {
+ char * name;
+ if (asprintf(&name, "basic_plugin_%i", i) < 0) {
+ fprintf(stderr, " %i: memory failure!\n", i);
+ break;
+ }
+ char *** slot = (char***) htab_find_slot (plugin_name_args_tab, name, NO_INSERT);
+ if (slot == NULL)
+ fprintf(stderr, " %i(%s): %p\n", i, name, slot);
+ else
+ fprintf(stderr, " %i(%s): %p (%s)\n", i, name, slot, **slot);
+ free(name);
+ }
+}
+
/* Create a plugin_name_args object for the given plugin and insert it
to the hash table. This function is called when
@@ -187,7 +202,8 @@
if (!plugin_name_args_tab)
plugin_name_args_tab = htab_create (10, htab_hash_string, htab_str_eq,
NULL);
-
+ dump_test_plugin_adresses("Before insertion");
+ fprintf(stderr, "Inserting %s\n", base_name);
slot = htab_find_slot (plugin_name_args_tab, base_name, INSERT);
/* If the same plugin (name) has been specified earlier, either emit an
@@ -207,6 +223,7 @@
plugin->full_name = plugin_name;
*slot = plugin;
+ dump_test_plugin_adresses("After insertion");
}
HOST_GCC:=g++
TARGET_GCC:=gcc
GCCPLUGINS_DIR:= $(shell $(TARGET_GCC) -print-file-name=plugin)
CXXFLAGS+= -I$(GCCPLUGINS_DIR)/include -fPIC -fno-rtti -O2 -std=gnu++98 -fvisibility=hidden
PLUGIN_NUMBER:= 11
NUMBERS = $(shell seq 1 $(PLUGIN_NUMBER))
PLUGINS = $(addsuffix .so, $(addprefix basic_plugin_, $(NUMBERS)))
LOAD_PLUGINS = $(addprefix -fplugin=./,$(PLUGINS))
test: $(PLUGINS)
$(TARGET_GCC) $(LOAD_PLUGINS) -fplugin-arg-basic_plugin_1-enable empty.c -o empty
basic_plugin.so: basic_plugin.c
$(HOST_GCC) -shared $(CXXFLAGS) $^ -o $@
basic_plugin_%.so: basic_plugin.so
cp $< $@
clean:
-rm basic_plugin*.so
-rm empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment