前言
试陈述C源代码到可执行程序的过程
一、主要过程
1. 环境配置
ubuntu 20.042. 示例源码
/*$ cat helloworld.c*/
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
}
3. 编译过程
$gcc helloworld.c
$tree .
.
├── a.out
└── helloworld.c
$ ./a.out
Hello World!
二、过程展开
1. gcc 在哪
$which gcc
/usr/bin/gcc
$ls -l /usr/bin/gcc
usr/bin/gcc -> gcc-9
$which gcc-9
/usr/bin/gcc-9
2. 编译步骤
helloworld.c 到a.out 依次经过预处理,编译 ,汇编,链接 4个过程
"$ gcc helloworld.c " 默认执行全部4个过程(默认过程我们看不到任何调试信息)
然而
我们使用编译参数可以打断编译过程,并得到便于理解它的输出信息
预处理
-E 选项帮助我们得到一个完整的源代码 helloworld.i(少有人能读懂)
$gcc -E helloworld.c -o helloworld.i
$tree .
.
├── helloworld.c
└── helloworld.i
编译
-S 选项帮助我们得到完整的汇编代码helloworld.s(极少人能读懂)
$gcc -S helloworld.i -o helloworld.s
$tree .
├── helloworld.c
├── helloworld.i
└── helloworld.s
汇编
-c 选项帮助我们得到完整的二进制代码helloworld.o(没人能读懂)
$gcc -c helloworld.s -o helloworld.o
链接
-v 选项 帮助我们得到链接的具体过程
你玩过拼图吗?链接相当于把helloworld.o 同其他系统隐藏的拼图拼接出a.out
备注:
所谓“系统隐藏拼图”指代 “cc7OZHLi.o crtendS.o crtn.o” 这类我们全然不知的二进制文件(他们通常由操作系统发行版本提供,对于操作系统使用者,他们相当于黑箱子,我们在此不对其进行展开)
$gcc -v helloworld.c
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64'
as -v --64 -o /tmp/cc7OZHLi.o /tmp/ccSXAdQl.s
GNU assembler version 2.34 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.34
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/9/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/9/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper -plugin-opt=-fresolution=/tmp/ccEXZ8ri.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -z relro /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/9/../../.. /tmp/cc7OZHLi.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-linux-gnu/9/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64'
3. 挖掘问题
源码在哪
$ gcc -H helloworld.c
解释:
helloworld.c 是自己编写的源代码,但是它通过include的形式插入了别人的代码(系统代码)
-H 选项帮助我们得到头文件迭代包含的路径。
.
..
...
分别对应着头文件包含的层级
命令输出:
. /usr/include/stdio.h
.. /usr/include/x86_64-linux-gnu/bits/libc-header-start.h
... /usr/include/features.h
.... /usr/include/x86_64-linux-gnu/sys/cdefs.h
..... /usr/include/x86_64-linux-gnu/bits/wordsize.h
..... /usr/include/x86_64-linux-gnu/bits/long-double.h
.... /usr/include/x86_64-linux-gnu/gnu/stubs.h
..... /usr/include/x86_64-linux-gnu/gnu/stubs-64.h
.. /usr/lib/gcc/x86_64-linux-gnu/9/include/stddef.h
.. /usr/lib/gcc/x86_64-linux-gnu/9/include/stdarg.h
.. /usr/include/x86_64-linux-gnu/bits/types.h
... /usr/include/x86_64-linux-gnu/bits/wordsize.h
... /usr/include/x86_64-linux-gnu/bits/timesize.h
... /usr/include/x86_64-linux-gnu/bits/typesizes.h
... /usr/include/x86_64-linux-gnu/bits/time64.h
.. /usr/include/x86_64-linux-gnu/bits/types/__fpos_t.h
... /usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h
.. /usr/include/x86_64-linux-gnu/bits/types/__fpos64_t.h
.. /usr/include/x86_64-linux-gnu/bits/types/__FILE.h
.. /usr/include/x86_64-linux-gnu/bits/types/FILE.h
.. /usr/include/x86_64-linux-gnu/bits/types/struct_FILE.h
.. /usr/include/x86_64-linux-gnu/bits/stdio_lim.h
.. /usr/include/x86_64-linux-gnu/bits/sys_errlist.h
Multiple include guards may be useful for:
/usr/include/x86_64-linux-gnu/bits/libc-header-start.h
/usr/include/x86_64-linux-gnu/bits/long-double.h
/usr/include/x86_64-linux-gnu/bits/sys_errlist.h
/usr/include/x86_64-linux-gnu/bits/time64.h
/usr/include/x86_64-linux-gnu/bits/timesize.h
/usr/include/x86_64-linux-gnu/bits/typesizes.h
/usr/include/x86_64-linux-gnu/gnu/stubs-64.h
/usr/include/x86_64-linux-gnu/gnu/stubs.h
/usr/lib/gcc/x86_64-linux-gnu/9/include/stddef.h
更多问题
部分问题或者由于与本文设定较远或者深入,不便展开(期待挖掘更过简单有用的问题)