如何从GNU/Linux的可执行文件中导出特定的符号
How to export specific symbol from executables in GNU/Linux
通过::dlopen()
加载动态库时,可以通过-rdynamic
选项导出可执行文件中的符号,但它会导出可执行文件中的所有符号,从而导致二进制文件的大小变大。
有一种方法来导出只是特定的功能(s)?
例如,我有testlib.cpp和main.cpp如下:
testlib.cpp
extern void func_export(int i);
extern "C" void func_test(void)
{
func_export(4);
}
main.cpp
#include <cstdio>
#include <dlfcn.h>
void func_export(int i)
{
::fprintf(stderr, "%s: %dn", __func__, i);
}
void func_not_export(int i)
{
::fprintf(stderr, "%s: %dn", __func__, i);
}
typedef void (*void_func)(void);
int main(void)
{
void* handle = NULL;
void_func func = NULL;
handle = ::dlopen("./libtestlib.so", RTLD_NOW | RTLD_GLOBAL);
if (handle == NULL) {
fprintf(stderr, "Unable to open lib: %sn", ::dlerror());
return 1;
}
func = reinterpret_cast<void_func>(::dlsym(handle, "func_test"));
if (func == NULL) {
fprintf(stderr, "Unable to get symboln");
return 1;
}
func();
return 0;
}
编译:
g++ -fPIC -shared -o libtestlib.so testlib.cpp
g++ -c -o main.o main.cpp
我希望动态库使用func_export,但隐藏func_not_export。
如果使用-rdynamic链接,g++ -o main -ldl -rdynamic main.o
,两个函数都导出。
如果没有链接-rdynamic,g++ -o main_no_rdynamic -ldl main.o
,我得到运行时错误Unable to open lib: ./libtestlib.so: undefined symbol: _Z11func_exporti
是否有可能实现只导出特定功能的要求?
有一种方法来导出只是特定的功能(s)?
我们需要这个功能,并在这里为黄金链接器添加了--export-dynamic-symbol
选项。
如果你使用的是Gold,构建一个最新的版本,你就会一切就绪。
如果你没有使用Gold,也许你应该使用它——它要快得多,并且有你需要的功能。
相关文章:
- 错误:当我从"WinDbg"打开可执行文件时,找不到符号文件。默认导出 ntdll 的符号.dll
- 使用共享库编译可执行文件时仅链接所需的符号
- 不同的数学符号绑定与共享库与 dlopen 并直接链接到可执行文件 (Linux)
- 在可执行文件中使用带有符号链接的相对路径
- 生成文件问题(体系结构x86_64的未定义符号:"_main",引用自:主可执行文件的隐式入口/启动)
- 如何使用macOS中的clang从可执行文件中剥离所有本地符号名称
- 静态库中的符号有时会链接到可执行文件中,有时则不会
- LD_BIND_NOW:符号查找错误,但可执行文件仍在运行
- 从 GCC 可执行文件中删除符号和 RTTI 文本
- 无法从使用 Cmake 创建的可执行文件中删除符号名称
- 为什么 GNU ld 在链接可执行文件和共享对象时以不同的方式解析符号
- 是否可以不让我的可执行文件中的符号可用于动态打开的库
- 建筑x86_64的未定义符号:... "_main" ,引用自:主可执行文件的隐式入口/启动
- 减少调试符号的占用(可执行文件膨胀到4 GB)
- 是否有可能调试最初由可执行文件生成的已删除符号的核心文件?
- 使用LLVM的可执行文件的动态符号解析
- 如何从GNU/Linux的可执行文件中导出特定的符号
- 是否有理由不从可执行文件中删除符号?
- 如何将不带调试符号的静态库链接到在VS2008上带调试的可执行文件
- JNI共享库中未定义的符号,可执行文件正在工作