rdynamic和-whole-archive
遇到如下情况,主程序通过dlopen来打开.so文件,但是.so用到了主程序的log函数。
编译so时,通过引用主程序头文件来编译通过,头文件有log函数声明:
extern "C" {
void print()
}
在主程序的.c文件里有函数的具体实现。
但是dlopen后运行so中函数时,出现找不到相应的symbol。
这时候就需要在编译主程序ld时加上参数-rdynamic,该参数的作用是:将指示连接器把所有符号(而不仅仅只是程序已使用到的外部符号,但不包括静态符号,比如被static修饰的函数)都添加到动态符号表(即.dynsym表)里,以便那些通过dlopen()或backtrace()(这一系列函数使用.dynsym表内符号)这样的函数使用。
-rdynamic
Pass the flag ‘-export-dynamic’ to the ELF linker, on targets that support
it. This instructs the linker to add all symbols, not only used ones, to the
dynamic symbol table. This option is needed for some uses of dlopen or to
allow obtaining backtraces from within a program.
-g是编译选项,而-rdynamic是链接选项
参考:http://www.lenky.info/archives/2013/01/2190
小例子:
a.cc
- #include "stdio.h"
- #include <dlfcn.h>
- extern "C" {
- void print()
- {
- printf("I am in so file!\n");
- }
- void fun()
- {
- void * err = dlopen("./libtmp.so", RTLD_LAZY);
- printf("dlopen = %p\n", err);
- if (err == NULL) {
- printf("err=%s\n", dlerror());
- }
- }
- }
a.h
- extern "C" void print();
- extern "C" void fun(); // 函数声明和定义都要有extern “C”,或者都没有,否则调用时出现undefined symbol fun
- #define NODE_MODULE \
- extern "C" { \
- static void print_main() __attribute__((constructor)) // dlopen时会自动调用该contructor函数</span>
- static void print_main() { \
- print(); \
- } \
- }
so.cc
- #include "a.h"
- #include "stdio.h"
- NODE_MODULE
foo.h
- <span style="font-size:18px;">void foo();</span>
foo.cc
- #include "stdio.h"
- void foo()
- {
- printf("foo === \n");
- }
main.cc
- #include "a.h"
- int main(void)
- {
- fun();
- return 0;
- }
Makefile
- all:dynamic
- libtmp.so:so.cc
- g++ -fPIC -shared -o $@ $^
- a.o:
- g++ -c a.cc -fPIC
- liba.a:a.o
- ar -r $@ $^
- libso.so: foo.cc liba.a
- g++ -fPIC -shared -o $@ $< -L./ -la -Wl,--whole-archive -la -Wl,--no-whole-archive -ldl
- dynamic:libso.so libtmp.so
- g++ -o $@ main.cc -Wl,--rpath=. -L./ -lso rdynamic
- clean:
- rm dynamic liba.a a.o libtmp.so
运行dynamic后输出为:
- I am in so file!
- dlopen = 0xdeb030
如果没有-rdynamic,则输出为:
- dlopen = (nil)
- err=./libtmp.so: undefined symbol: print
如果没有-Wl,--whole-archive -la -Wl,--no-whole-archive,也会有错误:undefined symbol: print
--whole-archive 可以把 在其后面出现的静态库包含的函数和变量输出到动态库,--no-whole-archive 则关掉这个特性
使用readelf -s libso.so | grep fun来查看libso.so的符号表里是否有fun这个函数暴露出来。有--whole-archive的可以查到fun,而没有--whole-archive的,则找不到fun
先理清一下code
可执行文件dynamic依赖与libso.so,而libso.so有包含liba.a,在liba.a的函数fun调用dlopen来打开libtmp.so
主函数调用liba.a的函数来打开libtmp.so
-fvisibility=hidden
设置默认的ELF镜像中符号的可见性为隐藏。使用这个特性可以非常充分的提高连接和加载共享库的性能,生成更加优化的代码,提供近乎完美的API输出和防止符号碰撞。我们强烈建议你在编译任何共享库的时候使用该选项。
-fvisibility-inlines-hidden
默认隐藏所有内联函数,从而减小导出符号表的大小,既能缩减文件的大小,还能提高运行性能,我们强烈建议你在编译任何共享库的时候使用该选项
所以编译的时候也不能有-fvisibility=hidden和-fvisibility-inlines-hidden。如果有,也会在dlopen时造成错误:undefined symbol
总结:
本实例虽小,但用到了不少编译选项
a: __attribute__((constructor))
主程序main函数之前被执行或dlopen时被执行
b: -rdynamic
ld时将动态库的的所有符号都输出到符号表,以便dlopen和backtrace也能调用
c: --whole-archive -la -Wl,--no-whole-archive
静态库的符号导入到动态库的符号表中,默认是hidden的
d: -fvisibility=hidden和-fvisibility-inlines-hidden
ELF镜像中符号的可见性为隐藏(在实验过程中不太好用,待研究)
在编译nodejs第三方模块时都会碰到这样的问题,第三方模块依赖与nodejs进行编译,而第三方模块又是通过dlopen来打开的,这就要求nodejs编译时将一下第三方模块需要的函数都暴露出来。
参考:
http://www.fx114.net/qa-225-106759.aspx
http://os.chinaunix.net/a2010/0112/1060/000001060902_3.shtml
rdynamic和-whole-archive的更多相关文章
- 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file
我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...
- Flashback Data Archive ( Oracle Total Recall ) introduced in 11g
Flashback Data Archive feature is part of Oracle Total Recall technology. Flashback Data Archive fea ...
- iOS 归档archive使用方法
归档是一种很常用的文件储存方法,几乎任何类型的对象都能够被归档储存,文件将被保存成自定 义类型的文件,相对于NSUserDefault具有更好的保密性. 1.使用archiveRootObject ...
- 转:HAR(HTTP Archive)规范
HAR(HTTP Archive),是一个用来储存HTTP请求/响应信息的通用文件格式,基于JSON.这个格式的出现可以使HTTP监测工具以一种通用的格式导出所收集的数据,这些数据可以被其他支持HAR ...
- http://www.cnblogs.com/Lawson/archive/2012/09/03/2669122.html
http://www.cnblogs.com/Lawson/archive/2012/09/03/2669122.html
- alter system switch logfile与alter system archive log current的区别
以前知道 ALTER SYSTEM SWITCH LOGFILE对单实例数据库或RAC中的当前实例执行日志切换, ALTER SYSTEM ARCHIVE LOG CURRENT会对数据库中的所有实例 ...
- Ubuntu W: GPG error: http://archive.ubuntukey....NO_PUBKEY 8D5A09
在用 sudo apt-get update 时出现这样的报错: W: GPG error: http://archive.ubuntukylin.com:10006/ubuntukylin xeni ...
- Linux JDK 安装及卸载 http://www.cnblogs.com/benio/archive/2010/09/14/1825909.html
参考:http://www.cnblogs.com/benio/archive/2010/09/14/1825909.html
- ant 介绍 http://blog.csdn.net/sunjavaduke/archive/2007/03/08/1523819.aspx
转自: 本内容包含了Ant的历史简要介绍,Ant的功能以及Ant框架的介绍,并对下载安装使用Ant进行了示例介绍,同时通过一个Java程序讲解了Ant的基本使用方法. 1. Ant简介:这 ...
- http://www.cnblogs.com/youring2/archive/2011/03/28/1997694.html
http://www.cnblogs.com/youring2/archive/2011/03/28/1997694.html
随机推荐
- UVa 10624 - Super Number
题目大意 给定两个数n和m,如果长度为m的数满足对于每个i(n<=i<=m),数字的前i位都能被i整除,那么这个数就是超级数,求出字典序最小的符合要求的超级数. 分析 直接暴力搜索 #in ...
- [转载]新手入门:Spring的一些学习方法及意见
原文地址:新手入门:Spring的一些学习方法及意见作者:飞扬飞扬xyz Spring简介: 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您 ...
- 260. Single Number III
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- 刚查了,Z3795不支持EPT,即WP8开发必须的SLAT,看来只能作为简单的WINDOWS备机了
刚查了,Z3795不支持EPT,即WP8开发必须的SLAT,看来只能作为简单的WINDOWS备机了,也就只能做做文档编辑,脚本编写之类的. 数据来源 http://ark.intel.com/zh-C ...
- ZOJ 1042 W’s Cipher
原题链接 题目大意:按照规则解码.26个字母分成三组,每一组按照顺时针移位编码.现在已知移动的位数,要求解码. 解法:以前看过一本古典密码学的书,百度贴吧密码吧也有很多经典的加密方法,想什么凯撒移位. ...
- c 函数及指针学习 7
1.结构的存储分配 1 2 printf("%d \n",sizeof(char)); printf("%d \n",sizeof(int)); int 类型为 ...
- ros与下位机通信常用的c++ boost串口应用--22
摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 一.首先移植c++ boost 库: 1. 先去 Boost官网 下载最新的Boost版本, 我 ...
- leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- linux之df命令
介绍: Linux中df命令可以用来显示目前在Linux系统上的文件系统的磁盘使用情况统计.这些工具可以方便地知道哪些文件系统消耗多少内存.此外,如果被拾起,并且提供一个特定的文件名作为df命令的参数 ...
- C语言二重指针与malloc
(内容主要源于网上,只是加入了些自己的剖析) 假设有一个二重指针: char **p; 同时有一个指针数组 char *name[4]; 如何引用p呢? 首先我们有程序代码如下 #include &l ...