initial
This commit is contained in:
commit
ec135c2cf4
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
*.so
|
||||
*.o
|
||||
plugin
|
17
Makefile
Normal file
17
Makefile
Normal 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
14
README
Normal 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
6
plug/Makefile
Normal file
@ -0,0 +1,6 @@
|
||||
include ../root.mk
|
||||
|
||||
CFLAGS += -shared -fPIE
|
||||
|
||||
libplug.so: plug.o
|
||||
${LINK.c} $+ -o $@
|
7
plug/plug.c
Normal file
7
plug/plug.c
Normal file
@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void
|
||||
plug(void)
|
||||
{
|
||||
printf("Plugin works.\n");
|
||||
}
|
10
plug/plug.h
Normal file
10
plug/plug.h
Normal 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
6
root.mk
Normal 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
6
src/Makefile
Normal 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
31
src/main.c
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user