Archived
1
0
This commit is contained in:
Emil 2023-09-05 19:40:53 -06:00
commit ec135c2cf4
No known key found for this signature in database
GPG Key ID: 5432DB986FDBCF8A
9 changed files with 100 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.so
*.o
plugin

17
Makefile Normal file
View File

@ -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

14
README Normal file
View File

@ -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.

6
plug/Makefile Normal file
View File

@ -0,0 +1,6 @@
include ../root.mk
CFLAGS += -shared -fPIE
libplug.so: plug.o
${LINK.c} $+ -o $@

7
plug/plug.c Normal file
View File

@ -0,0 +1,7 @@
#include <stdio.h>
void
plug(void)
{
printf("Plugin works.\n");
}

10
plug/plug.h Normal file
View File

@ -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

6
root.mk Normal file
View File

@ -0,0 +1,6 @@
CFLAGS := -std=c89 -Wall -Wextra -Wpedantic -Wshadow -Wundef
CPPFLAGS := -D_FORTIFY_SOURCE=2
LDLIBS := -lplug
.c.o:
${COMPILE.c} $< -o $@

6
src/Makefile Normal file
View File

@ -0,0 +1,6 @@
include ../root.mk
CPPFLAGS += -I../plug -L../plug
plugin: main.o
${LINK.c} $+ -o $@ ${LDLIBS}

31
src/main.c Normal file
View File

@ -0,0 +1,31 @@
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#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);
}
}