1 / 22

系統程式

系統程式. Supplementation. #include &lt;stdio.h&gt; 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

sammy
Télécharger la présentation

系統程式

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 系統程式 Supplementation

  2. #include <stdio.h> int main() { printf(“Hello World\n”); return 0; } $gcc hello.c $./a.out Hello World

  3. 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

  4. Compilation Preprocessing gcc -E hello.c -o hello.i Compilation gcc -S hello.i -o hello.s Or cc1 hello.c

  5. Assembly as hello.s -o hello.o or gcc -c hello.s -o hello.o Or gcc -c hello.c -o hello.o

  6. 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

  7. Object File Format • Executable • Windows: PE (Portable Executable) • Linux: ELF(Executable Linkable Format)

  8. 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

  9. gcc -c so.c objdump -h so.o

  10. objdump –s -d so.o

  11. 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 

  12. ELF file structure

  13. readelf 7F 45 4C 46 readelf –h so.o ‘E’’L’’F’

  14. 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

  15. Objdump –d a.o Objdump –d ab

  16. 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

  17. Stack • Last In First Out (LIFO) ebp Stack pop esp push

  18. Stack Frame (Active Record) Caller’s Freame ebp Callee’s Freame esp

  19. Heap • malloc()

More Related