Skip to content

Instantly share code, notes, and snippets.

@flatcap
Created May 8, 2020 23:42
Show Gist options
  • Save flatcap/cefbe776d5c3be7783ff60f525ee490d to your computer and use it in GitHub Desktop.
Save flatcap/cefbe776d5c3be7783ff60f525ee490d to your computer and use it in GitHub Desktop.
plugin
#include <stdio.h>
#include <dlfcn.h>
const int (*foo_pointer)(void);
int main(int argc, char *argv[])
{
void *handle = NULL;
char *error = NULL;
if (argc != 2)
return 1;
handle = dlopen(argv[1], RTLD_NOW);
if (!handle) {
printf ("dlopen failed: %s\n", dlerror());
return 1;
}
foo_pointer = dlsym(handle, "foo");
error = dlerror();
if (error) {
printf("dlsym failed: %s\n", error);
dlclose(handle);
return 1;
}
printf ("foo returns: %d\n", (*foo_pointer)());
dlclose(handle);
return 0;
}
#include <stdio.h>
extern int foo ();
int main ()
{
printf ("%d\n", foo ());
return 0;
}
extern int foo ();
int bar ()
{
return foo ();
}
int foo ()
{
return 42;
}
# dlopen test
CC = gcc
RM = rm -fr
SRC = foo1.c foo2.c foo3.c
OBJ = $(SRC:.c=.o)
OUT = foo1 foo2.so foo3.so dlopen
CFLAGS += -g
CFLAGS += -fPIC
all: $(SRC) $(OBJ) $(OUT)
.c.o:
$(CC) $(CFLAGS) -c $< -o $@
foo1: foo1.o foo2.so foo3.so
$(CC) -o $@ foo1.o foo2.so foo3.so
foo2.so: foo2.o foo3.so
$(CC) -shared -o foo2.so foo2.o foo3.so
foo3.so: foo3.o
$(CC) -shared -o foo3.so foo3.o
clean:
$(RM) foo[123].o
$(RM) foo[23].so
$(RM) foo1 dlopen
dlopen: dlopen.c
$(CC) -o $@ dlopen.c -ldl
run: foo1 dlopen foo3.so
LD_LIBRARY_PATH=. foo1
LD_LIBRARY_PATH=. dlopen foo3.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment