静态库、动态库回顾

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

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
相关推荐
2401_841495644 小时前
【操作系统】进程同步与互斥实验报告
c++·算法·操作系统·进程·并发·同步·互斥
Yana.nice4 小时前
Linux 只保留 30 天内日志(find命令删除日志文件)
linux·运维·chrome
fqbqrr5 小时前
2607C++,soui与安卓
c++·soui
DFT计算杂谈8 小时前
无 Root 权限在 Tesla K80 零门槛部署 DeepSeek 大模型
linux·服务器·网络·数据库·机器学习
fqbqrr8 小时前
2607C++,使用微软detours勾挂工具
c++
Zhang~Ling9 小时前
从 fopen 到 struct file:从零开始拆解 Linux 文件 I/O
linux·运维·服务器
DeeplyMind9 小时前
Linux 深入 per-VMA lock:Linux 缺页路径如何摆脱 mmap_lock
linux·per-vma lock
爱写代码的森9 小时前
蒙三方库 | harmony-utils之FileUtil文件重命名与属性查询详解
linux·运维·服务器·华为·harmonyos·鸿蒙·huawei
XMAIPC_Robot10 小时前
软硬协同实时控制|RK3588业务调度+FPGA硬件时序,ethercat实现半导体设备微秒级响应(125us)
linux·arm开发·人工智能·fpga开发
重生的黑客11 小时前
Linux 进程优先级、切换与调度:从孤儿进程到 O(1) 调度模型
linux·运维·服务器·进程优先级·nice