我们最近被要求提供一个库的Linux版本,之前我们是在Linux下开发的,在Windows上部署库通常容易得多.我们遇到的问题是将导出的符号剥离为仅expose 接口中的符号.想要这样做有三个很好的理由
- 保护我们技术的专有方面不被出口符号expose .
- 以防止用户遇到符号名称冲突的问题.
- 加快图书馆的加载速度(至少我听说是这样).
举个简单的例子:
测验cpp
#include <cmath>
float private_function(float f)
{
return std::abs(f);
}
extern "C" float public_function(float f)
{
return private_function(f);
}
用(g++4.3.2,ld 2.18.93.20081009)编译
g++ -shared -o libtest.so 测验cpp -s
并用
nm -DC libtest.so
给予
w _Jv_RegisterClasses
0000047c T private_function(float)
000004ba W std::abs(float)
0000200c A __bss_start
w __cxa_finalize
w __gmon_start__
0000200c A _edata
00002014 A _end
00000508 T _fini
00000358 T _init
0000049b T public_function
显然不够.所以接下来我们将公共函数重新声明为
extern "C" float __attribute__ ((visibility ("default")))
public_function(float f)
并用
g++ -shared -o libtest.so 测验cpp -s -fvisibility=hidden
which 给予
w _Jv_RegisterClasses
0000047a W std::abs(float)
0000200c A __bss_start
w __cxa_finalize
w __gmon_start__
0000200c A _edata
00002014 A _end
000004c8 T _fini
00000320 T _init
0000045b T public_function
这很好,除了std::absexpose 在外.更麻烦的是,当我们开始链接到我们控制之外的其他(静态)库时,all of the symbols we use from those libraries get exported.此外,当我们开始使用STL容器时:
#include <vector>
struct private_struct
{
float f;
};
void other_private_function()
{
std::vector<private_struct> v;
}
我们最终得到了来自C++库的许多额外出口.
00000b30 W __gnu_cxx::new_allocator<private_struct>::deallocate(private_struct*, unsigned int)
00000abe W __gnu_cxx::new_allocator<private_struct>::new_allocator()
00000a90 W __gnu_cxx::new_allocator<private_struct>::~new_allocator()
00000ac4 W std::allocator<private_struct>::allocator()
00000a96 W std::allocator<private_struct>::~allocator()
00000ad8 W std::_Vector_base<private_struct, std::allocator<private_struct> >::_Vector_impl::_Vector_impl()
00000aaa W std::_Vector_base<private_struct, std::allocator<private_struct> >::_Vector_impl::~_Vector_impl()
00000b44 W std::_Vector_base<private_struct, std::allocator<private_struct> >::_M_deallocate(private_struct*, unsigned int)
00000a68 W std::_Vector_base<private_struct, std::allocator<private_struct> >::_M_get_Tp_allocator()
00000b08 W std::_Vector_base<private_struct, std::allocator<private_struct> >::_Vector_base()
00000b6e W std::_Vector_base<private_struct, std::allocator<private_struct> >::~_Vector_base()
00000b1c W std::vector<private_struct, std::allocator<private_struct> >::vector()
00000bb2 W std::vector<private_struct, std::allocator<private_struct> >::~vector()
注意:如果启用了优化功能,则需要确保实际使用了向量,这样编译器就不会优化未使用的符号.
我相信我的同事已经设法构建了一个特别的解决方案,包括版本文件和修改STL头(!)这似乎有效,但我想问:
Is there a clean way to strip all unnecessary symbols (IE ones that are not part of the exposed library functionality) from a linux shared library?我已经try 了很多关于g++和ld的选项,但几乎没有成功,所以我更喜欢已知有效的答案,而不是相信有效的答案.
特别地:
- 不导出(封闭源代码)静态库中的符号.
- 不会导出标准库中的符号.
- 不会导出对象文件中的非公共符号.
我们导出的接口是C.
我知道其他类似的问题,所以:
- NOT sharing all classes with shared library
- How to REALLY strip a binary in MacOs
- GNU linker: alternative to --version-script to list exported symbols at the command line?
但在答案上几乎没有成功.