Browse Source

initial

master
Emil 8 months ago
commit
ec135c2cf4
No known key found for this signature in database GPG Key ID: 5432DB986FDBCF8A
9 changed files with 100 additions and 0 deletions
  1. +3
    -0
      .gitignore
  2. +17
    -0
      Makefile
  3. +14
    -0
      README
  4. +6
    -0
      plug/Makefile
  5. +7
    -0
      plug/plug.c
  6. +10
    -0
      plug/plug.h
  7. +6
    -0
      root.mk
  8. +6
    -0
      src/Makefile
  9. +31
    -0
      src/main.c

+ 3
- 0
.gitignore View File

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

+ 17
- 0
Makefile 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
- 0
README 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
- 0
plug/Makefile View File

@@ -0,0 +1,6 @@
include ../root.mk

CFLAGS += -shared -fPIE

libplug.so: plug.o
${LINK.c} $+ -o $@

+ 7
- 0
plug/plug.c View File

@@ -0,0 +1,7 @@
#include <stdio.h>

void
plug(void)
{
printf("Plugin works.\n");
}

+ 10
- 0
plug/plug.h 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
- 0
root.mk 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
- 0
src/Makefile View File

@@ -0,0 +1,6 @@
include ../root.mk

CPPFLAGS += -I../plug -L../plug

plugin: main.o
${LINK.c} $+ -o $@ ${LDLIBS}

+ 31
- 0
src/main.c 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);
}
}