静态库、动态库回顾

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

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
相关推荐
0xCC说逆向8 分钟前
Windows图形界面(GUI)-MFC-C/C++ - CStatic
c语言·开发语言·c++·windows·mfc·win32
一叶之秋141211 分钟前
C&C++内存魔术:掌控无形资源
开发语言·c++
hotslow40 分钟前
【LVS】负载均衡之NAT模式
linux·服务器·lvs
飞大圣43 分钟前
Linux 内核源码分析---Ext 文件系统
linux
Java程序之猿1 小时前
Linux内网环境部署thingsboard(离线部署)
linux·服务器·thingsboard
jianlai_1 小时前
虚拟机ubuntu22.04找不到ttyUSB*端口
linux·运维·服务器·ros/ros2
bantinghy1 小时前
figlet字符画命令工具
linux
LanUU1231 小时前
库移植的过程(以sqlite3移植为例)
linux·arm开发·学习·vim
martian6651 小时前
C++新手入门学习教程(完整版)
c++·学习·初学者教程·c++新手入门教程
码农学徒_2 小时前
C++基础
c++