Subject: Re: dlopen() status
To: None <port-alpha@netbsd.org>
From: James Macnicol <James.Macnicol@faceng.anu.edu.au>
List: port-alpha
Date: 01/06/2001 11:39:50
> If it's in the main program, you will need to link your program with the
> option:
>
> cc ... -Wl,-E
>
> (or more verbosely,)
>
> cc ... -Wl,--export-dynamic

	Okay, I've narrowed down the problem a bit more.  NetBSD doesn't seem
to quite be compatible with Solaris (or Linux) on this.  It turns out that
if you use NULL instead of RTLD_DEFAULT then you can indeed lookup symbols
in the current executable.  Unfortunately, it doesn't find symbols in 
shared libraries opened by dlopen.  Consider the following :

miranda(0):~/tmp/dl% cat foo.c
#include <stdio.h>

void foo(void)
{
  printf("Hello, world!\n");
}
miranda(0):~/tmp/dl% gcc -Wall -fPIC -o foo.o -c foo.c
miranda(0):~/tmp/dl% gcc -Wall -shared -Wl,--export-dynamic -o foo.so foo.o
miranda(0):~/tmp/dl% cat dl.c
#include <stdlib.h>
#include <dlfcn.h>

int main(void)
{
  void (*foo)(void);

  dlopen("./foo.so", RTLD_NOW|RTLD_GLOBAL);
  foo = dlsym(RTLD_DEFAULT, "foo");
  foo();

  return (0);
}
miranda(0):~/tmp/dl% gcc -Wall -Wl,--export-dynamic -o dl dl.c
miranda(0):~/tmp/dl% ./dl
Hello, world!

	That was on Solaris (with a gcc that uses the GNU binutils).
If you do the same thing, replacing RTLD_DEFAULT by NULL on NetBSD you
get a segfault because dlsym returns NULL.  If foo was in the same
file as main() then this doesn't happen.

        The point therefore, is that dlsym should search all runtime loaded
shlibs for symbols, as well as the main program, when the first argument to
dlsym() is RTLD_DEFAULT (or NULL in NetBSD's case).




James