Resolving symbols against shared libraries w/o link

From: Haakon Riiser (hakonrk_at_fys.uio.no)
Date: 06/22/04


Date: 22 Jun 2004 02:41:28 +0200

I would like to use dlopen() to resolve symbols at runtime instead
of having ld.so do it at startup, and I do /not/ want to use
dlsym() on every symbol. After some RTFM'ing and experimenting,
I found a way to do it:

  /* test.c (headers are not included) */
  int main(int argc, char *argv[])
  {
          char *err;
          void *handle = dlopen("hello.so", RTLD_LAZY | RTLD_GLOBAL);
          if ((err = dlerror())) {
                  printf("%s\n", err);
                  return 1;
          }
          hello();
          return 0;
  }

  /* hello.c (headers are not included) */
  void hello(void) { printf("hello, world\n"); }

These two files are compiled and linked like this:

  $ gcc -shared hello.c -o hello.so
  $ gcc -fpic -Wl,--warn-unresolved-symbols test.c -ldl -o test
  /tmp/ccwoUUFk.o(.text+0x76): In function `main':
  : warning: undefined reference to `hello'

Then,

  $ LD_LIBRARY_PATH=. ./test
  hello, world

So, this works, but there are some problems. I had to make all
unresolved symbols give warnings instead of errors, and there
are two problems with that -- one is that I don't want warnings
for symbols that are defined in hello.so, and the other is that
I want the link to fail for unresolved symbols that are /not/
defined in hello.so.

It seems to me that this could easily be solved by telling ld to
resolve against a shared library, but to omit the part where it
adds the library path to the dependency list in the ELF dynamic
segment. Did I miss something in the ld manpage?

-- 
 Haakon