静态库、动态库回顾

回顾一下库相关的知识点,总结备忘一下。在某种情况下,你有了如下的代码,结构如下

c 复制代码
//pra.h
#include <stdio.h>
void test_01();
//pra.c
#include "pra.h"
void test_01() {
    printf("xxxxxxx----->%s %s()\n", "pra", __FUNCTION__);
}

//test.h
#include <stdio.h>
void test_01();
__attribute ((visibility("default"))) void test_02();
//test.c
#include "test.h"

void test_01() {
    printf("xxxxxxx----->%s()\n", __FUNCTION__);
}

__attribute ((visibility("default"))) void test_02() {
    printf("xxxxxxx----->%s()\n", __FUNCTION__);
}

//main.c
#include "pra/pra.h"
#include "test/test.h"

int main() {
    test_01();
    return 0;
}

假设,pra和test分别代表第三方动态库。

小白需要让编译上述代码并运行,编译过程如下

c 复制代码
//生成动态库libtest.so
gcc -fPIC -shared test.c -o libtest.so
//生成动态库libpra.so
gcc -fPIC -shared pra.c -o libpra.so

1、两个动态库含有同名函数

a. 不做特殊处理的时候

链接的时候,哪个库在前,用哪个库里面的函数实现

b. 通过fvisibility进行控制

假设,main函数只希望执行libtest.so的test_01函数

可以在编译libpra.so加入-fvisibility=hidden

bash 复制代码
gcc -fPIC -shared -fvisibility=hidden pra.c -o libpra.so

这样随意调整动态库顺序,最终只会调用libtest.so对应的函数

通过readelf查看一下,可以看到libpra.so中test_01函数是LOCAL.

2、两个动态库含有同名函数

bash 复制代码
ar -rv libtest.a test.o
ar -rv libpra.a pra.o

哪个静态库在前,用哪个静态库的函数

3、反编译

3.1 查看动态库源码

编译代码的时候需要加入-g参数

bash 复制代码
gcc -fPIC -shared -g pra.c -o libpra.so
objdump --section=.text -S libpra.so

可以得到如下的结果,最后一小节就是源码了

3.2 查看静态库源码

先生成静态库

bash 复制代码
gcc -g -c pra.c -o pra.o
ar -rv libpra.a pra.o

静态库的反编译需要先将.a文件还原成.o文件,再查看。

bash 复制代码
ar -x libpra.a //会得到pra.o
objdump --section=.text -S pra.o
相关推荐
zzzzzz31016 小时前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode16 小时前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
A小辣椒3 天前
TShark:Wireshark CLI 功能
linux
A小辣椒3 天前
TShark:基础知识
linux
AlfredZhao3 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao3 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
clint4563 天前
C++进阶(1)——前景提要
c++
用户9718356334664 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
夜悊4 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴4 天前
CMake 021: IF 条件判据详诠
c++·cmake