220 likes | 414 Views
系統程式. Supplementation. #include <stdio.h> int main() { printf(“Hello World<br>”); return 0; } $gcc hello.c $./a.out Hello World. The Compilation Process. Source code hello.c. Preprocessing (gcc -E). Header Files stdio.h …. Compilation (cc1). Preprocessed hello.i. Assembly
E N D
系統程式 Supplementation
#include <stdio.h> int main() { printf(“Hello World\n”); return 0; } $gcc hello.c $./a.out Hello World
The Compilation Process Source code hello.c Preprocessing (gcc -E) Header Files stdio.h … Compilation (cc1) Preprocessed hello.i Assembly (as) Assembly hello.s Object Files hello.o Linking (ld) Static Library libc.a … Executable a.out
Compilation Preprocessing gcc -E hello.c -o hello.i Compilation gcc -S hello.i -o hello.s Or cc1 hello.c
Assembly as hello.s -o hello.o or gcc -c hello.s -o hello.o Or gcc -c hello.c -o hello.o
Linking ld -static /usr/lib/crt1.o /usr/lib/crti.o … ld -static crt1.o crti.o crtbeginT.o hello.o –start-group –lgcc –lgcc_eh –llc –end-group crtend.o crtn.o
Object File Format • Executable • Windows: PE (Portable Executable) • Linux: ELF(Executable Linkable Format)
Section (Program Block) int g_init_var = 84; int g_uninit_var; void func1(int i) { printf(“%d\n”, i); } int main() { static int s_var = 85; static int s_var2; int a = 1; int b; func1(s_var + s_var2 + a + b); return 0; } BSS: Block Start by Symbol
gcc -c so.c objdump -h so.o
ABI • Application Binary Interface • Issue • data type, size, and alignment • calling convention • Memory allocation of struct, union and array • system call • C++ name mangling, exception propagation, and calling convention
readelf 7F 45 4C 46 readelf –h so.o ‘E’’L’’F’
Static Linking • gcc -c a.c b.c • We get a.o b.o • ld a.o b.o -e main -o ab • file ab • ab: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
Objdump –d a.o Objdump –d ab
Memory Layout 0xFFFFFFFF Kernel Space 0xC0000000 Stack unused Heap Read/write sections (.data, .bss) Read only sections (.init, .rodata, .text) 0x08048000 reserved 0x00000000 Linux 2.4.x
Stack • Last In First Out (LIFO) ebp Stack pop esp push
Stack Frame (Active Record) Caller’s Freame ebp Callee’sFreame esp
Heap • malloc()