如何在GNU / Linux的可执行文件导出特定符号 [英] How to export specific symbol from executables in GNU/Linux
问题描述
虽然通过加载动态库::的dlopen()
,从可执行文件导出元件可以通过 -rdynamic
完成选项,但出口可执行文件,这将导致更大的二进制大小的所有符号
有没有只导出特定功能(S)的方式?
例如,我有如下testlib.cpp和main.cpp中:
testlib.cpp
的extern无效func_export(int i)以;为externC无效func_test(无效)
{
func_export(4);
}
的main.cpp
的#include< cstdio>
#包括LT&;&dlfcn.h中GT;无效func_export(int i)以
{
:: fprintf中(标准错误,%S:%d个\\ N,__func__,I);
}无效func_not_export(int i)以
{
:: fprintf中(标准错误,%S:%d个\\ N,__func__,I);
}无效的typedef(* void_func)(无效);INT主要(无效)
{
void *的手柄= NULL;
void_func FUNC = NULL;
处理= ::的dlopen(./ libtestlib.so,RTLD_NOW | RTLD_GLOBAL);
如果(手柄== NULL){
fprintf中(标准错误,无法打开lib目录下:%S \\ n,:: dlerror获得());
返回1;
}
FUNC = reinter pret_cast< void_func>(::对dlsym(手柄,func_test)); 如果(FUNC == NULL){
fprintf中(标准错误,无法获取符号\\ n);
返回1;
}
FUNC();
返回0;
}
编译:
G ++ -fPIC -shared -o libtestlib.so testlib.cpp
G ++ -c -o main.o中的main.cpp
我想通过动态库中使用func_export,但隐藏func_not_export。
如果链路与-rdynamic, G ++ -o主要-ldl -rdynamic main.o中
,这两个函数被导出。
如果不-rdynamic链接, G ++ -o main_no_rdynamic -ldl main.o中
,我得到了运行时错误无法打开lib目录下:./libtestlib.so:未定义的符号:_Z11func_exporti
是否有可能实现,只有导出的特定功能的要求?
有没有只导出特定功能(S)的方式?
块引用>我们需要这个功能,并且加入
- 出口动态符号
选项,黄金链接器的这里。如果您使用的是黄金,构建一个新版本,你将所有设置。
如果您使用的不是黄金,也许你应该 - 它的速度更快,而且有你需要的功能
While loading dynamic libraries by
::dlopen()
, exporting symbols from executables can be done by-rdynamic
option, but it exports all the symbols of the executable, which results in bigger binary size.Is there a way to export just specific function(s)?
For example, I have testlib.cpp and main.cpp as below:
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: %d\n", __func__, i); } void func_not_export(int i) { ::fprintf(stderr, "%s: %d\n", __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: %s\n", ::dlerror()); return 1; } func = reinterpret_cast<void_func>(::dlsym(handle, "func_test")); if (func == NULL) { fprintf(stderr, "Unable to get symbol\n"); return 1; } func(); return 0; }
Compile:
g++ -fPIC -shared -o libtestlib.so testlib.cpp g++ -c -o main.o main.cpp
I want func_export to be used by the dynamic library, but hide the func_not_export.
If link with -rdynamic,
g++ -o main -ldl -rdynamic main.o
, both functions are exported.If not link with -rdynamic,
g++ -o main_no_rdynamic -ldl main.o
, I got runtime errorUnable to open lib: ./libtestlib.so: undefined symbol: _Z11func_exporti
Is it possible to achieve the requirement that only export the specific function?
解决方案Is there a way to export just specific function(s)?
We needed this functionality, and added
--export-dynamic-symbol
option to the Gold linker here.If you are using Gold, build a recent version and you'll be all set.
If you are not using Gold, perhaps you should -- it's much faster, and has the functionality you need.
这篇关于如何在GNU / Linux的可执行文件导出特定符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!