一.目标文件的类型:
- 可重定位目标文件: 包含了二进制的代码和数据,可以与其他可重定位目标文件合并,并创建一个可执行目标文件。(.o文件,使用-c命令生成)
- 共享目标文件。它是一种在加载或者运行时进行链接的特殊可重定位目标文件。
文件名 | 文件格式 | ELF Type | 编译方式 |
---|---|---|---|
hello.s | txt | N/A | -S |
hello.o | elf | Relocatable file | -c |
libhello.a | elf | Relocatable file | ar -r libhello.a hello.o |
libhello.so | elf | Shared object file | gcc -shared -fPIC -o libhello.so hello.o |
a.out | elf | Shared object file |
二.链接的原则
- libc库中的函数不需要链接。
- 对于传参为常量的数学函数调用,生成可执行文件过程中可能将其优化,而无需调用该函数。
- 库链接一般放在命令行结尾。
- 当存在同名的动态库和静态库时:默认选择动态库,-static参数选择静态库
参考:https://www.yanbinghu.com/2019/06/27/47343.html
三.常用命令
静态库的反解析:
ar -x libhello.a