6

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.)

4

1 回答 1

0

据我所知,没有办法做到这一点。但是,如果 'foo' 的结构在您的控制之下,您可以将 'foo' 中的所有逻辑移动到导出正确符号的新共享库中,然后将您的插件和主线链接到该库。

于 2012-08-28T18:09:44.880 回答