I have a program foo
that can load plugins via dlopen()
. I'm writing one such plugin foobar
that interfaces to functionality provided by a third-party library libfoo
.
On Linux, I'm building foobar
like this:
gcc -fpic -c -o foobar.o foobar.c
gcc -fpic -shared -o foobar.so foobar.o -lbar
So far so good.
The problem is that this doesn't detect undefined symbols, for example because of bugs in the code or mismatches between header files and library. The link succeeds, and you only get an error later when you load the plugin or when you invoke something in the plugin (depending on dlopen()
flags).
To detect undefined symbols in a shared library, one would normally use -Wl,-z,defs
or perhaps -Wl,--no-allow-shlib-undefined
. But this will fail because it will also report symbols that are supposed to be found in the foo
program at run time. I would like to detect undefined symbols except those provided by the hosting program.
On Mac OS X, for example, this is done by the -bundle_loader
option. The equivalent link command would be
gcc -bundle -o foobar.so foobar.o -lbar -bundle_loader=foo
and that detects undefined symbols in the way I want.
How does one do this on GNU/Linux or with GNU ld in general? (I have looked through all the options listed on the ld
man page, but none looked promising.)