3

我有几个从构建系统中吐出的目标文件(来自 C++)。它们有几个extern "C"我想在程序中使用的链接符号,并且可以通过dlopen/dlsym从其他地方访问。

当使用 gcc 编译成可执行文件时,这些符号未列出使用nm -D <executable-here>(即 afaik 它们不是动态符号)。

如何让它们在编译后的可执行文件中作为动态符号公开?

我可以更改目标文件和可执行文件的构建标志,但是更改 C++ 文件在最终可执行文件中的结束方式(即不首先将它们变成目标文件)是困难的。

(GCC 4.8, ld 2.24)

编辑:我遇到了这个问题,这可能是我要问的,也可能不是,但我不完全确定。 在静态二进制文件上使用 dlsym

4

1 回答 1

3

您可能想看看--export-dynamicld 选项:

   -E
   --export-dynamic
   --no-export-dynamic
       When creating a dynamically linked executable, using the -E option
       or the --export-dynamic option causes the linker to add all symbols
       to the dynamic symbol table.  The dynamic symbol table is the set
       of symbols which are visible from dynamic objects at run time.

       If you do not use either of these options (or use the
       --no-export-dynamic option to restore the default behavior), the
       dynamic symbol table will normally contain only those symbols which
       are referenced by some dynamic object mentioned in the link.

       If you use "dlopen" to load a dynamic object which needs to refer
       back to the symbols defined by the program, rather than some other
       dynamic object, then you will probably need to use this option when
       linking the program itself.

       You can also use the dynamic list to control what symbols should be
       added to the dynamic symbol table if the output format supports it.
       See the description of --dynamic-list.

       Note that this option is specific to ELF targeted ports.  PE
       targets support a similar function to export all symbols from a DLL
       or EXE; see the description of --export-all-symbols below.

此外,如果链接期间没有任何对象引用您的外部符号,您可能希望将它们放入--dynamic-list以确保它们被导出。


例子:

$ cat test.cc
#include <stdio.h>

int main() {
    printf("Hello, world\n");
}

extern "C" void export_this() {
    printf("Hello, world from export_this\n");
}

$ g++ -o test -W{all,extra} -Wl,--export-dynamic test.cc

$ ./test
Hello, world

$ nm --dynamic test | grep export_this
00000000004007f5 T export_this # <---- here you go
于 2014-10-10T08:32:51.450 回答