refer to for more details:
https://medium.com/@earthtojhuang/gcc-main-c-9f6ee2e6894b
https://www.geeksforgeeks.org/compiling-a-c-program-behind-the-scenes/
Compiling, linking, loading:
- Preprocessor takes C source code (ASCII text), expands #include etc, produces C source code
- Compiler takes C source code (ASCII text), produces assembly language (also ASCII text)
- Assembler takes assembly language (ASCII text), produces .o file (binary, machine-readable!)
- Linker takes multiple '.o's, produces a single program image (binary)
- Loader loads the program image into memory at run-time and starts it executing
// tst.c
#include <stdio.h>
main()
{
printf("hello world\n");
}
$ gcc -S tst.c # the output is tst.s
$ gcc -c tst.c # the output is tst.o
$ file tst.*
tst.c: C source, ASCII text
tst.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
tst.s: assembler source, ASCII text
gcc -S --> Compile but don't assemble
gcc -c --> Preprocess, compile, and assemble, but don't link
disassembly/反汇编
objdump -S a.out > tst.asm