I am in the process of trying to port rp-l2tp to openbsd.
I have a problem with dlopen(). rp-l2tp calls dlopen() to load its
sync-pppd.so module, and this in turn has callbacks to functions defined in
the main program. However under OpenBSD these callbacks fail to link.
Here's a simple test to demonstrate.
==> Makefile <==
all: test1 test2
test1: test1.c test.h
gcc -Wall -rdynamic -o test1 test1.c -ldl
test2: test2.c test.h
gcc -Wall -shared -o test2.so test2.c
==> test1.c <==
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "test.h"
int bar(const char *msg)
{
fprintf(stderr, "Message: %s\n", msg);
return 0;
}
int main(int argc, char **argv) {
void *handle;
int (*my_foo)(void);
char *error;
handle = dlopen ("./test2.so", RTLD_NOW);
if (!handle) {
fprintf (stderr, "%s\n", dlerror());
exit(1);
}
dlerror(); /* Clear any existing error */
*(void **) (&my_foo) = dlsym(handle, "foo");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}
(*my_foo)();
dlclose(handle);
return 0;
}
==> test2.c <==
#include "test.h"
int foo(void)
{
bar("hello, world!");
return 0;
}
==> test.h <==
int foo(void);
int bar(const char *);
If I run this under Linux (Ubuntu-6.06) it works:
$ make
gcc -Wall -rdynamic -o test1 test1.c -ldl
gcc -Wall -shared -o test2.so test2.c
$ ./test1
Message: hello, world!
$
However, I don't know what I need to modify to make it work under OpenBSD.
You need to remove the '-dl' from the Makefile for it to compile, but then:
# make
gcc -Wall -rdynamic -o test1 test1.c
test1.c: In function `main':
test1.c:25: warning: assignment discards qualifiers from pointer target type
gcc -Wall -shared -o test2.so test2.c
# ./test1
./test1:./test2.so: undefined symbol 'bar'
Cannot load specified object
#
Under Linux it seems that '-rdynamic' is the magic incantation, because
without this it also fails. However I don't know what the OpenBSD equivalent
is.
Could someone provide me with the necessary clue please?
Thanks,
Brian.