25 Oct 2019

静态链接

静态链接

静态库

命名规则lib(.*).a

静态库生成

void fuc()
{
	puts("A1_D4i");
}
gcc -c fuc.c -o libfuc.o
ar -r libfuc.a libfuc.o

静态链接

在链接阶段,将目标文件与引用的库一起打包到可执行文件中

有无函数库对程序运行没有影响,移植方便,但是目标文件和引用的库合成为一个可执行文件,浪费空间

样例

#include <stdio.h>
int	main(int argc, char const *argv[])
{
	puts("hello world");
	return 0;
}
gcc hello.c -static -o hellos
file hellos
hellos: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 3.2.0, BuildID[sha1]=19408caa4a17c699fedb6b397880a6752eaa4751, not stripped
ls -hl
total 2.8M
-rwxrwxrwx 1 aidai aidai 8.2K Oct 25 21:22 hello
-rwxrwxrwx 1 aidai aidai 825K Oct 25 21:17 hellos

和动态链接的hello比一下大小,可以看到静态链接大了很多

ida中对比

动态链接

6

静态链接

7

在puts断

pwndbg> break puts
Breakpoint 1 at 0x410210

5

查看puts的地址,可以看到储存着puts代码


Tags:
0 comments



本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议CC BY-NC-ND 4.0)进行许可。

This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License (CC BY-NC-ND 4.0).