commit ec135c2cf41c8d681415b3b170e652421c8142a5 Author: Emil Date: Tue Sep 5 19:40:53 2023 -0600 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..38ae8bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.so +*.o +plugin diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..518dd8e --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +#!/bin/make -f + +PREFIX := . + +CP := cp -f + +all: plug/libplug.so src/plugin + +plug/libplug.so: + make -C plug + $(CP) $@ $(PREFIX) + +src/plugin: + make -C src + $(CP) $@ $(PREFIX) + +.PHONY: all plug/libplug.so src/plugin diff --git a/README b/README new file mode 100644 index 0000000..4cad8e5 --- /dev/null +++ b/README @@ -0,0 +1,14 @@ +plug/ contains the dynamically loaded library, +and of course, src/ has the core program. + +You'll have to use LD_LIBRARY_PATH='.' ./plugin +This could be remedied with a little messing with +get/setenv. + +There's a good reason for the fragmentation and +recursion of the Makefiles, as how the files are +compiled vary at compliation and link time +(link-time not so applicable in this case, but + it is effected by CFLAGS anyways). + +Copy as you wish, credit not. diff --git a/plug/Makefile b/plug/Makefile new file mode 100644 index 0000000..6c827ae --- /dev/null +++ b/plug/Makefile @@ -0,0 +1,6 @@ +include ../root.mk + +CFLAGS += -shared -fPIE + +libplug.so: plug.o + ${LINK.c} $+ -o $@ diff --git a/plug/plug.c b/plug/plug.c new file mode 100644 index 0000000..61e524d --- /dev/null +++ b/plug/plug.c @@ -0,0 +1,7 @@ +#include + +void +plug(void) +{ + printf("Plugin works.\n"); +} diff --git a/plug/plug.h b/plug/plug.h new file mode 100644 index 0000000..aaec119 --- /dev/null +++ b/plug/plug.h @@ -0,0 +1,10 @@ +#ifndef PLUG_H_ + +#define BIND(lib, func, func_name) *(void **) (&func) = dlsym(lib, func_name) + +typedef void (*plug_t)(void); + +static plug_t plug; + +#define PLUG_H_ +#endif diff --git a/root.mk b/root.mk new file mode 100644 index 0000000..1c92c3f --- /dev/null +++ b/root.mk @@ -0,0 +1,6 @@ +CFLAGS := -std=c89 -Wall -Wextra -Wpedantic -Wshadow -Wundef +CPPFLAGS := -D_FORTIFY_SOURCE=2 +LDLIBS := -lplug + +.c.o: + ${COMPILE.c} $< -o $@ diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..72d7624 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,6 @@ +include ../root.mk + +CPPFLAGS += -I../plug -L../plug + +plugin: main.o + ${LINK.c} $+ -o $@ ${LDLIBS} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..2330b9c --- /dev/null +++ b/src/main.c @@ -0,0 +1,31 @@ +#include +#include +#include + +#include "plug.h" + +int +main (void) +{ + int x = 0; + char * libplug_fn = "libplug.so"; + void * libplug = NULL; + while (1) + { + if (libplug) { dlclose(libplug); } + libplug = dlopen(libplug_fn, RTLD_NOW); + BIND(libplug, plug, "plug"); + if (!libplug) + { fprintf(stderr, "ERR: Cannot load dynamic library %s: %s\n", libplug_fn, dlerror()); } + else + { + *(void **) (&plug) = dlsym(libplug, "plug"); + printf("frame %d: ", x++); + if (plug) + { plug(); } + else + { fprintf(stderr, "ERR: Cannot find %s symbol in %s: %s\n", "plug", libplug_fn, dlerror()); } + } + sleep(2); + } +}