Stack Debugging. Young W. Lim Mon. Young W. Lim Stack Debugging Mon 1 / 63

Size: px
Start display at page:

Download "Stack Debugging. Young W. Lim Mon. Young W. Lim Stack Debugging Mon 1 / 63"

Transcription

1 Stack Debugging Young W. Lim Mon Young W. Lim Stack Debugging Mon 1 / 63

2 Outline 1 Based on 2 Introduction Compiling to IA32 Assembly Checking /proc/<pid>/maps file Checking the main stack frame 3 Stack Frames Using gdb Stack Frame Pointers Functions and Stack Frames 4 Calling Convention Function Call and Return Enter and Leave Instructions Function Prologue and Epilogue example codes and output results handling arguments and return value analyzing func2() analyzing func1() analyzing Young W. Limmain() Stack Debugging Mon 2 / 63

3 Based on "Self-service Linux: Mastering the Art of Problem Determination", Mark Wilding "Computer Architecture: A Programmer s Perspective", Bryant & O Hallaron I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License. CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short: you are free to share and make derivative works of the file under the conditions that you appropriately attribute it, and that you distribute it only under a license compatible with this one. Young W. Lim Stack Debugging Mon 3 / 63

4 preparation : for Linux Mint install packages lib32gcc1 lib32gcc-dbg gcc-multilib gcc -m32 t.c Young W. Lim Stack Debugging Mon 4 / 63

5 stack frame address range stack frame starts from 0xc stack bottom : 0xc stack address range 0xbfffe000 (low address) 0xc (high address) stack grows toward lower address from HIGH address (0xc ) to LOW address (0xbfffe000) Young W. Lim Stack Debugging Mon 5 / 63

6 stack frame in the Linux Mint stack address range 0xc (from HIGH address) 0xbfffe000 (to LOW address) 0x stack address range in the Linux Mint 0xfffc9000 (from HIGH address) 0xfffa8000 (to LOW address) 0x these addresses are not fixed Young W. Lim Stack Debugging Mon 6 / 63

7 code for displaying /proc/<pid>/maps local variables (stack variables) : var local variable address : 0xff9e00e8 #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char *argv[]) { int var = 5; char cmd[64]; } printf(" &var = %p\n", &var ); sprintf(cmd, "cat /proc/%d/maps", getpid()); system(cmd); Young W. Lim Stack Debugging Mon 7 / 63

8 result for displaying /proc/<pid>/maps &var = 0xff9e00e r-xp : /home/young/a.out a000 r--p : /home/young/a.out 0804a b000 rw-p : /home/young/a.out 0851b c000 rw-p :00 0 [heap] f75fb000-f75fc000 rw-p :00 0 f75fc000-f77a9000 r-xp : /lib32/libc-2.23.so f77a9000-f77aa p 001ad000 08: /lib32/libc-2.23.so f77aa000-f77ac000 r--p 001ad000 08: /lib32/libc-2.23.so f77ac000-f77ad000 rw-p 001af000 08: /lib32/libc-2.23.so f77ad000-f77b1000 rw-p :00 0 f77ce000-f77d0000 r--p :00 0 [vvar] f77d0000-f77d2000 r-xp :00 0 [vdso] f77d2000-f77f4000 r-xp : /lib32/ld-2.23.so f77f4000-f77f5000 rw-p :00 0 f77f5000-f77f6000 r--p : /lib32/ld-2.23.so f77f6000-f77f7000 rw-p : /lib32/ld-2.23.so ff9c1000-ff9e2000 rw-p :00 0 [stack] Young W. Lim Stack Debugging Mon 8 / 63

9 fields in /proc/<pid>/maps (from 1 ) address: the address space in the process that it occupies perms: a set of permissions offset: the offset into the mapping dev: the device (major:minor) inode: the inode on that device. pathname: shows the name associated file for this mapping address perms offset dev inode pathname fffa8000-fffc9000 rw-p :00 0 [stack] (1) ff9c1000-ff9e2000 rw-p :00 0 [stack] (2) 1 how-to-identify-stack-and-heap-segments-in-proc-pid-maps-file Young W. Lim Stack Debugging Mon 9 / 63

10 perms & inode fields perms: a set of permissions r = read w = write x = execute s = shared p = private (copy on write) inode: the inode on that device. 0 indicates that no inode is associated with the memory region like BSS (uninitialized data) Young W. Lim Stack Debugging Mon 10 / 63

11 pathname field pathname If the mapping is associated with a file: the name of the associated file for this mapping If the mapping is not associated with a file: [heap] = the heap of the program [stack] = the stack of the main process [stack:1001] = the stack of the thread with tid 1001 [vdso] = the "virtual dynamic shared object", the kernel system call handler empty = the mapping is anonymous. Young W. Lim Stack Debugging Mon 11 / 63

12 stack elements in /proc/<pid>/maps file addr(stack_var) = 0xff9200e8 address perms offset dev inode pathname ff9c1000-ff9e2000 rw-p :00 0 [stack] [from HIGH] ff9e2000 V ff9e00e8 : &var V [to LOW ] ff9c1000 Young W. Lim Stack Debugging Mon 12 / 63

13 ELF stack contents [from HIGH] ff9e stack bottom 1 path name specified in exec() 2 environment variables 3 argv strings 4 argc 5 aux vectors [from HIGH] ff9e2000 1, 2, 3, 4, 5 V ff9e00e8 : &var [to LOW ] ff9c1000 Young W. Lim Stack Debugging Mon 13 / 63

14 code for checking the main stack frame (1) #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> extern char **environ; int main(int argc, char *argv[]) { int var = 5; char cmd[64]; printf(" *environ = %p\n", *environ ); printf("1. argv[0] = %p\n", argv[0] ); printf("2. environ = %p\n", environ ); printf("3. argv = %p\n", argv ); printf("4. &argc = %p\n", &argc ); printf(" &var = %p\n", &var); Young W. Lim Stack Debugging Mon 14 / 63

15 code for checking the main stack frame (2) printf(" *environ = %s\n", *environ ); printf("1. argv[0] = %s\n", argv[0] ); printf("2. *environ = %p\n", *environ ); printf("3. *argv = %p\n", *argv ); printf("4. argc = %d\n", argc ); printf(" var = %d\n", var); sprintf(cmd, "cat /proc/%d/maps", getpid()); system(cmd); return 0; } Young W. Lim Stack Debugging Mon 15 / 63

16 results for the main stack frame (1)... 0xffcc [from HIGH] *environ = 0xffce938b 1. argv[0] = 0xffce environ = 0xffce866c 3. argv = 0xffce &argc = 0xffce85d0 &var = 0xffce8568 V... 0xffcea000...[to LOW ] *environ = LC_PAPER=ko_KR.UTF-8 1. argv[0] =./a.out 2. *environ = 0xffce938b 3. *argv = 0xffce argc = 1 var = 5 Young W. Lim Stack Debugging Mon 16 / 63

17 results for the main stack frame (2) r-xp : /home/young/a.out a000 r--p : /home/young/a.out 0804a b000 rw-p : /home/young/a.out 08ca cc4000 rw-p :00 0 [heap] f75fd000-f75fe000 rw-p :00 0 f75fe000-f77ab000 r-xp : /lib32/libc-2.23.so f77ab000-f77ac p 001ad000 08: /lib32/libc-2.23.so f77ac000-f77ae000 r--p 001ad000 08: /lib32/libc-2.23.so f77ae000-f77af000 rw-p 001af000 08: /lib32/libc-2.23.so f77af000-f77b3000 rw-p :00 0 f77d0000-f77d2000 r--p :00 0 [vvar] f77d2000-f77d4000 r-xp :00 0 [vdso] f77d4000-f77f6000 r-xp : /lib32/ld-2.23.so f77f6000-f77f7000 rw-p :00 0 f77f7000-f77f8000 r--p : /lib32/ld-2.23.so f77f8000-f77f9000 rw-p : /lib32/ld-2.23.so ffcc9000-ffcea000 rw-p :00 0 [stack] Young W. Lim Stack Debugging Mon 17 / 63

18 using gdb to check stack frames use gdb break func3 run backtrace main() -> func1() -> func2() -> func3() var str i c 3 "Hello, world!" 1 \ #3 #2 #1 #0 Young W. Lim Stack Debugging Mon 18 / 63

19 example code #include <stdio.h> #include <unistd.h> void func3( int *a ) { int c = \0 ; printf("pid = %d; ", getpid()); printf("press <Enter> \n"); c = fgetc( stdin ); void func2( char *s ) { int i = 1; func3( &i ); printf("i = %d \n", i); } void func1( int m ) { char str[] = "Hello, world!"; func2( str ); } } printf("c=%c\n", c); *a = 9; int main(void) { int var = 3; func1( var ); return 0; } Young W. Lim Stack Debugging Mon 19 / 63

20 gdb commands gdb a.out ---> GNU gdb (Ubuntu ubuntu1~16.04) Copyright (C) 2016 Free Software Foundation, Inc.... (gdb) break func3 Punto de interrupción 1 at 0x : file t.c, line 7. (gdb) run Starting program: /home/young/a.out Breakpoint 1, func3 (a=0xffffd068) at t.c:7 7 int c = \0 ; (gdb) backtrace #0 func3 (a=0xffffd068) at t.c:7 #1 0x080485ab in func2 (s=0xffffd09e "Hello, world!") at t.c:19 #2 0x e in func1 (m=3) at t.c:26 #3 0x in main () at t.c:32 (gdb) Young W. Lim Stack Debugging Mon 20 / 63

21 gdb stack frame numbering [from HIGH] - stack bottom 1 main() - gdb #3 2 func1() - gdb #2 3 func2() - gdb #1 4 func3() - gdb #0 break point #0 func3 (a=0xffffd068) at t.c:7 #1 0x080485ab in func2 (s=0xffffd09e "Hello, world!") at t.c:19 #2 0x e in func1 (m=3) at t.c:26 #3 0x in main () at t.c:32 Young W. Lim Stack Debugging Mon 21 / 63

22 Stack frame pointers (1) Full Downward Stack BP_old old stack frame V... SP_old BP_new new stack frame V... SP_new pushl movl subl %ebp %esp, %ebp $16, %esp Young W. Lim Stack Debugging Mon 22 / 63

23 Stack frame pointers (2) BP_old old stack frame V... SP_old BP_new new stack frame V... SP_new - old BP must be saved on the stack - pushl %ebp - stack grows downward - decreasing new SP for manual push operation BP_old old stack frame V [BP_old] SP_old BP_new new stack frame V [xxxxx]... SP_new pushl movl subl %ebp %esp, %ebp $16, %esp Young W. Lim Stack Debugging Mon 23 / 63

24 c source code #include <stdio.h> void func1( int m ) { int i = 99; } int main(void) { int i = 3; gcc -m32 -S -Wall t.c t.c -> t.s t.c func1( i ); return 0; } Young W. Lim Stack Debugging Mon 24 / 63

25 generated assembly (1).file.text.globl.type "t.c" func1 func1:.lfb0:.lfe0:.cfi_startproc pushl %ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8 movl %esp, %ebp.cfi_def_cfa_register 5 subl $16, %esp movl $99, -4(%ebp) nop leave.cfi_restore 5.cfi_def_cfa 4, 4 ret.cfi_endproc.size.globl.type func1,.-func1 main Young W. Lim Stack Debugging Mon 25 / 63

26 generated assembly (2) main:.lfb1:.lfe1:.cfi_startproc pushl %ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8 movl %esp, %ebp.cfi_def_cfa_register 5 subl $16, %esp movl $3, -4(%ebp) pushl -4(%ebp) call func1 addl $4, %esp movl $0, %eax leave.cfi_restore 5.cfi_def_cfa 4, 4 ret.cfi_endproc.size main,.-main.ident "GCC: (Ubuntu ubuntu1~ ).section.note.GNU-stack,"",@progbits Young W. Lim Stack Debugging Mon 26 / 63

27 func1() analysis func1:.lfb0:.lfe0: function prologue pushl %ebp movl %esp, %ebp subl $16, %esp movl $99, -4(%ebp) nop function epilogue leave ret void func1( int m ) { // the local stack variable // stored at %ebp-4 int i = 99; } Young W. Lim Stack Debugging Mon 27 / 63

28 main() analysis main:.lfb1:.lfe1: function prologue pushl %ebp movl %esp, %ebp subl $16, %esp movl $3, -4(%ebp) pushl -4(%ebp) call func1 addl $4, %esp movl $0, %eax function epilogue leave ret int main(void) { // local stack variable // at %ebp-4 int i = 3; // the argument $3 // the value at %ebp-4 // pushed on the stack // before calling func1() func1( i ); // return value $0 // is stored in %eax return 0; } Young W. Lim Stack Debugging Mon 28 / 63

29 Omitting frame pointer compiler option -fomit-frame-pointer -fno-omit-frame-pointer Young W. Lim Stack Debugging Mon 29 / 63

30 Function Call 1 push the return address (%esp -= 4, then push %eip) 2 jump to the start of the called function return address the address of the instruction immediately following the call instruction - Direct call : call label - Indirect call : call *operand operand : - offset Imm - a base register Eb - an index register Ei - a scalefactor s Young W. Lim Stack Debugging Mon 30 / 63

31 Function Return 1 pops the return address from the stack 2 jump to the return address location the SP must points to the location where the correct return address can be stored leave instruction does this stack preparation Young W. Lim Stack Debugging Mon 31 / 63

32 enter instruction prepares the stack frame for entering to the callee equivalent instructions as follows - push the current %ebp - new %ebp points where this old %ebp is saved - the last stack content is this savd %ebp - the 2nd last stack content is the return address (^) push %ebp ;; push the old %ebp mov %esp %ebp ;; new %ebp = the old %esp... <--%ebp <--%esp0..r.addr.. %ebp0 <--%esp <-- %ebp Young W. Lim Stack Debugging Mon 32 / 63

33 leave instruction prepares the stack frame for returning to the caller equivalent instructions as follows - current %esp points where the old %ebp was saved - restore %ebp by this saved old %ebp - the last stack content is the saved old %ebp - the 2nd last stack content is the return address (^) movl %ebp, %esp pop %ebp ;; restored %esp1 = the current %ebp ;; restored %ebp1 = the saved old %ebp <--%ebp0 <--%ebp r.addr.. <--%esp1 %ebp0 <-- %ebp %ebp0 <--%esp Young W. Lim Stack Debugging Mon 33 / 63

34 Function Prologue 1 pushl %ebp stores the BP (base pointer) of the previous frame on the stack decrements SP by 4 bytes (long: 32bits) 2 movl %esp, %ebp updates the BP (base pointer) with the SP (stack pointer) BP always points to the current stack bottom (higher address) SP always points to the current stack top (lower address) SP always points to full (non-empty) word 3 subl $16, %esp to push the local variable i (int, 4 bytes) stack grows downward SP points 4 bytes lower address 16-byte alignment Young W. Lim Stack Debugging Mon 34 / 63

35 Function Epilogue 1 leave movl %ebp, %esp popl %ebp 2 ret function prologue pushl %ebp movl %esp, %ebp old BP stored on the stack old SP becomes new BP new BP points to the stored old BP function epilogue movl %ebp, %esp popl %ebp current BP points to the stored old BP this BP becomes new SP take the stored old BP Young W. Lim Stack Debugging Mon 35 / 63

36 example C codes for checking the X86 calling convention int func2( int i, int j, int k ) { int val = i+j+k; main() --> func1() --> func2() val val val 0 0 i+j+k func1(a) func2(b,c,d)... #include <stdio.h> #define A 1 #define B 2 #define C 3 #define D 4 return val; } int func1( int m ) { int val = 0; val = func2( B, C, D ); return val; } int main(void) { int val = 0; val = func1( A ); return val; } Young W. Lim Stack Debugging Mon 36 / 63

37 checking calling convention - generated assembly (1).file "t.c".text....globl func2.type func2: [AAA].size func2,.-func2....globl func1.type func1: [BBB].size func1,.-func1....globl main.type main: [CCC].size main,.-main....ident "GCC:....section... func2: [AAA].LFB0: [[ func2 ]].LFE0: func1: [BBB].LFB1: [[ func1 ]].LFE1: main: [CCC].LFB2: [[ main ]].LFE2: Young W. Lim Stack Debugging Mon 37 / 63

38 checking calling convention - generated assembly (2) func2: [AAA].LFB0:.cfi_startproc pushl %ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8 movl %esp, %ebp.cfi_def_cfa_register 5 subl $16, %esp movl 8(%ebp), %edx movl 12(%ebp), %eax addl %eax, %edx movl 16(%ebp), %eax addl %edx, %eax movl %eax, -4(%ebp) movl -4(%ebp), %eax leave.cfi_restore 5.cfi_def_cfa 4, 4 ret.cfi_endproc.lfe0 func1: [BBB].LFB1:.cfi_startproc pushl %ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8 movl %esp, %ebp.cfi_def_cfa_register 5 subl $16, %esp movl $0, -4(%ebp) pushl $4 pushl $3 pushl $2 call func2 addl $12, %esp movl %eax, -4(%ebp) movl -4(%ebp), %eax leave.cfi_restore 5.cfi_def_cfa 4, 4 ret.cfi_endproc.lfe1: Young W. Lim Stack Debugging Mon 38 / 63

39 checking calling convention - generated assembly (3) main:.lfb2: [CCC].cfi_startproc pushl %ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8 movl %esp, %ebp.cfi_def_cfa_register 5 subl $16, %esp movl $0, -4(%ebp) pushl $1 call func1 addl $4, %esp movl %eax, -4(%ebp) movl -4(%ebp), %eax leave.cfi_restore 5.cfi_def_cfa 4, 4 ret.cfi_endproc.lfe2: Young W. Lim Stack Debugging Mon 39 / 63

40 pushing arguments on to the stack frame storing arguments onto the stack from the right to the left in the reverse order the first argument is save at the last func(a, B, C, D); < push D -> push C -> push B -> push A Young W. Lim Stack Debugging Mon 40 / 63

41 storing return value to %eax regsiter store the return value in the %eax register movl leave ret -4(%ebp), %eax stack grows downward %ebp : BP %ebp-4 : 4 bytes downward from BP the word at %ebp-4 is moved to %eax a local stack variable is stored here its value is the return value the old BP is stored at %ebp Young W. Lim Stack Debugging Mon 41 / 63

42 1. analyzing func2() - (1) func2:.lfb0: pushl %ebp movl %esp, %ebp subl $16, %esp movl 8(%ebp), %edx movl 12(%ebp), %eax addl %eax, %edx movl 16(%ebp), %eax addl %edx, %eax movl %eax, -4(%ebp)... movl -4(%ebp), %eax leave ret int func2( int i, int j, int k ) { int val = i+j+k; return val; } main() -> func1() -> func2() - just after func2() is called - %ebp & %esp of the caller func1() - [old %ebp] %ebp of main() %ebp - 0 : [old %ebp] <-- %ebp %ebp - 4 : $0 %ebp - 8 : $4 %ebp - c : $3 %ebp -10 : $2 %ebp -14 : [Ret address] <-- %esp Young W. Lim Stack Debugging Mon 42 / 63

43 1. analyzing func2() - (2) func2:.lfb0: pushl %ebp movl %esp, %ebp subl $16, %esp movl 8(%ebp), %edx movl 12(%ebp), %eax addl %eax, %edx movl 16(%ebp), %eax addl %edx, %eax movl %eax, -4(%ebp)... movl -4(%ebp), %eax leave ret - after function prolog is executed - [old %ebp] %ebp of func1() %ebp - 4 : $0 %ebp - 8 : $4 %ebp+10 %ebp - c : $3 %ebp+c %ebp -10 : $2 %ebp+8 %ebp -14 : [Ret address] %ebp+4 %ebp -18 : [old %ebp] <-- %esp <-- %ebp - just before leave %ebp +14 : $0 %ebp +10 : $4 %ebp + c : $3 %ebp + 8 : $2 %ebp + 4 : [Ret address] %ebp %ebp - 4 : [val]... %ebp -10 : : [old %ebp] <-- %ebp <-- %esp Young W. Lim Stack Debugging Mon 43 / 63

44 2. analyzing func1() - (1) func1:.lfb1: pushl %ebp movl %esp, %ebp subl $16, %esp movl $0, -4(%ebp)... pushl $4 pushl $3 pushl $2... call func2 addl $12, %esp movl %eax, -4(%ebp)... movl -4(%ebp), %eax leave ret.lfe1: #define B 2 #define C 3 #define D 4 int func1( int m ) { int val = 0; val = func2( B, C, D ); return val; } - push arguements on the stack - from the right argument - to the left argument - just after executing the function prologue %ebp - 0 : [old %ebp] <-- %ebp <= %esp %ebp - 4 : %ebp - 8 : %ebp - c : %ebp -10 : <-- %esp <= %esp-16 Young W. Lim Stack Debugging Mon 44 / 63

45 2. analyzing func1() - (2) func1:.lfb1: pushl %ebp movl %esp, %ebp subl $16, %esp movl $0, -4(%ebp)... pushl $4 pushl $3 pushl $2... call func2 addl $12, %esp movl %eax, -4(%ebp)... movl -4(%ebp), %eax leave ret.lfe1: - just after call func2 - call instruction pushes the return address on the stack %ebp - 4 : $0 (init val) %esp+10 %ebp - 8 : $4 %esp+c %ebp - c : $3 %esp+8 %ebp -10 : $2 %esp+4 %ebp +14 : [ret addr] <-- %esp - just after ret from func2 - ret instruction pops the return address from the stack %ebp - 4 : [updated val] %esp+c %ebp - 8 : $4 %esp+8 %ebp - c : $3 %esp+4 %ebp -10 : $2 <-- %esp Young W. Lim Stack Debugging Mon 45 / 63

46 2. analyzing func1() - (3) func1:.lfb1: pushl %ebp movl %esp, %ebp subl $16, %esp movl $0, -4(%ebp)... pushl $4 pushl $3 pushl $2... call func2 addl $12, %esp movl %eax, -4(%ebp)... movl -4(%ebp), %eax leave ret.lfe1: - after returning from func2() decrease stack by 12 bytes - the SP now points to the stack variable val - the return value from func2 in %eax - val is updated with this return value - this value is stored to %eax the return value of func1() %ebp - 4 : XX <-- %esp %ebp - 8 : $4 %ebp - c : $3 %ebp -10 : $2 Young W. Lim Stack Debugging Mon 46 / 63

47 3. analyzing main() - (1) main:.lfb2: pushl %ebp movl %esp, %ebp subl $16, %esp movl $0, -4(%ebp)... pushl $1... call func1 addl $4, %esp movl %eax, -4(%ebp)... movl -4(%ebp), %eax leave ret.lfe2: #define A 1 int main(void) { int val = 0; val = func1( A ); return val; } - just after executing the function prologue %ebp - 0 : [old %ebp] <-- %ebp <= %esp %ebp - 4 : %ebp - 8 : %ebp - c : %ebp -10 : <-- %esp <= %esp-16 Young W. Lim Stack Debugging Mon 47 / 63

48 3. analyzing main() - (2) main:.lfb2: pushl %ebp movl %esp, %ebp subl $16, %esp movl $0, -4(%ebp)... pushl $1... call func1 addl $4, %esp movl %eax, -4(%ebp)... movl -4(%ebp), %eax leave ret.lfe2: - just after call func1 - call instruction pushes the return address on the stack %ebp - 4 : $0 (init val) %esp+14 %ebp - 8 : %esp+10 %ebp - c : %esp+c %ebp -10 : %esp+8 %ebp +14 : $1 %esp+4 %ebp +18 : [ret addr] <-- %esp - just after ret from func1 - ret instruction pops the return address from the stack %ebp - 4 : $0 (init val) %esp+10 %ebp - 8 : %esp+c %ebp - c : %esp+8 %ebp -10 : %esp+4 %ebp +14 : $1 <-- %esp } Young W. Lim Stack Debugging Mon 48 / 63

49 3. analyzing main() - (3) main:.lfb2: pushl %ebp movl %esp, %ebp subl $16, %esp movl $0, -4(%ebp)... pushl $1... call func1 addl $4, %esp movl %eax, -4(%ebp)... movl -4(%ebp), %eax leave ret.lfe2: - after returning from func1() decrease stack by 4 bytes - the return value from func2 in %eax - val is updated with this return value - this value is stored to %eax the return value of main %ebp - 4 : XX %ebp - 8 : %ebp - c : %ebp -10 : } <-- %esp Young W. Lim Stack Debugging Mon 49 / 63

50 call by reference example code #include <stdio.h> #include <unistd.h> void func3( int *a ) { int c = \0 ; printf("pid = %d; ", getpid()); printf("press <Enter> \n"); c = fgetc( stdin ); void func2( char *s ) { int i = 1; func3( &i ); printf("i = %d \n", i); } void func1( int m ) { char str[] = "Hello, world!"; func2( str ); } } printf("c=%c\n", c); *a = 9; int main(void) { int var = 3; func1( var ); return 0; } Young W. Lim Stack Debugging Mon 50 / 63

51 example code summary main( ) {local: var=3} func1(var) func1( m ) {local: str[]="hello, world!"} func2(str) func2( *s ) {local: i=1} func3(&i) print i func3( *a ) {local: c= \0 } print pid & Press <Enter> c=fgetc(stdin) print c *a=9 Young W. Lim Stack Debugging Mon 51 / 63

52 call by reference : generated assembly structure.lc3:.string "i = %d \n".file "t.c".text.section.rodata.globl func2.lc0:.type "pid = %d; " func2:....lc1:.size func2,.-func2.string "Press <Enter> ".globl func1.lc2:.type "c=%c\n" func1:....text.size func1,.-func1.globl func3.globl main.type func3:... main:....size func3,.-func3.size main,.-main.section.rodata.ident "GCC: (Ubuntu ubuntu1~.section.note.GNU-stack,"",@p Young W. Lim Stack Debugging Mon 52 / 63

53 call by reference : printf string constants.lc0:.lc1:.lc2:.lc3:.string "pid = %d; ".string "Press <Enter> ".string "c=%c\n".string "i = %d \n" void func3( int *a ) {... printf("pid = %d; ", getpid()); printf("press <Enter> \n");... printf("c=%c\n", c);... } void func2( char *s ) {... printf("i = %d \n", i); } Young W. Lim Stack Debugging Mon 53 / 63

54 call by reference : main (1) main:.lfb3:.lfe3: leal 4(%esp), %ecx ;; %ecx = %esp+4 andl $-16, %esp ;; 0xFFFFFFF0 pushl -4(%ecx) ;; push [%ecx-4] %esp -= 4 pushl %ebp ;; push %ebp %esp -= 4 movl %esp, %ebp ;; %ebp = %esp pushl %ecx ;; push %ecx %esp -= 4 subl $20, %esp ;; %esp -= 20 movl $3, -12(%ebp) ;; [%ebp-12] = 3 ;; int var = 3; subl $12, %esp ;; %esp -= 12 pushl -12(%ebp) ;; push [%ebp-12]... call func1 ;; func1( var ); addl $16, %esp ;; %esp += 16 movl $0, %eax ;; %eax = 0 ;; return 0; movl -4(%ebp), %ecx ;; %ecx = [%ebp-4] leave ;; %esp = %ebp pop %ebp leal -4(%ecx), %esp ;; %esp = %ecx-4 ret Young W. Lim Stack Debugging Mon 54 / 63

55 call by reference : main (2) main:.lfb3: %ecx = %esp+4 %ecx= init %esp+4 0xFFFFFFF0 16-byte alignment of %esp push [%ecx-4] %esp -= 4 push init %esp %esp -= 4 push %ebp %esp -= 4 push init %ebp %esp -= 4 %ebp = %esp new %ebp = %esp (aligned %esp-8) push %ecx %esp -= 4 push init %esp+4 %esp -= 4 %esp -= 20 enlarge %esp by 20 %esp -= 20 [%ebp-12] = 3 ;; int var = 3; (%ebp-12) = (%ecx) = (init %esp+4) %esp -= 12 enlarge %esp by 12 %esp -= 12 push [%ebp-12] push arg (=var) %exp -= 4 func1( var ); function call %esp += 16 contrict %esp by 16 %esp += 16 %eax = 0 ;; return 0; return value of main() = 0 %ecx = [%ebp-4] %esp = %ebp pop %ebp %esp = %ecx-4 ;; %ecx = init %esp+4 ;; 16-byte alignment %esp Young W. Lim Stack Debugging ;; %ebp = %esp Mon 55 / 63

56 call by reference : func1 (1) func1:.lfb2: call func2 ;; func2( str ); pushl %ebp ;; push %ebp %esp -= 4 movl %esp, %ebp ;; %ebp = %esp subl $40, %esp ;; %esp -= 40 movl %gs:20, %eax ;; %eax = [%gs:20] movl %eax, -12(%ebp) ;; [%ebp-12] = %eax xorl %eax, %eax ;; %eax = 0 movl $ , -26(%ebp) ;; [%ebp-26] = "lleh" movl $ , -22(%ebp) ;; [%ebp-22] = "w,o" movl $ , -18(%ebp) ;; [%ebp-18] = "dlro" movw $33, -14(%ebp) ;; [%ebp-14] =! subl $12, %esp ;; %esp -= 12 leal -26(%ebp), %eax ;; %eax = %ebp-26 pushl %eax ;; push %eax %esp -= 4 Young W. Lim Stack Debugging Mon 56 / 63

57 call by reference : func1 (2).L5:.LFE2:... call func2 ;; func2( str ); addl $16, %esp ;; %esp += 16 nop movl -12(%ebp), %eax ;; %eax = [%ebp-12] xorl %gs:20, %eax ;; %eax ^= %gs:20 je.l5... call stack_chk_fail leave ret char str[] = "Hello, world!"; $ x6C6C6548 lleh $ x77202C6F w,o $ x646C726F dlro Young W. Lim Stack Debugging Mon 57 / 63

58 call by reference : func2 (1) func2:.lfb1: pushl %ebp ;; push %ebp %esp -= 4; movl %esp, %ebp ;; %ebp = %esp subl $40, %esp ;; %esp -= 40 movl 8(%ebp), %eax ;; %eax = [%ebp+8] movl %eax, -28(%ebp) ;; [%ebp-28] = %eax movl %gs:20, %eax ;; %eax = %gs:20 movl %eax, -12(%ebp) ;; [%ebp-12] = %eax xorl %eax, %eax ;; %eax = 0 movl $1, -16(%ebp) ;; [%ebp-16] = 1 ;; int i = 1; subl $12, %esp ;; %esp -= 12 leal -16(%ebp), %eax ;; %eax = %ebp-16 pushl %eax ;; push %eax %esp -= 4... Young W. Lim Stack Debugging Mon 58 / 63

59 call by reference : func2 (2).L3:.LFE1: call func3 ;; func3( &i ); addl $16, %esp ;; %esp -= 16 movl -16(%ebp), %eax ;; %eax = [%ebp-16] subl $8, %esp ;; %esp -= 8 pushl %eax ;; push %eax %esp -= 4 pushl $.LC3 ;; push addr(.lc3) %esp -= 4... call printf ;; printf("i = %d \n", i); addl $16, %esp ;; %esp += 16 nop movl -12(%ebp), %eax ;; %eax = [%ebp-12] xorl %gs:20, %eax ;; %eax ^= %gs:20 je.l3... call stack_chk_fail leave ret Young W. Lim Stack Debugging Mon 59 / 63

60 call by reference : func3 (1) func3:.lfb0: pushl %ebp ;; push %ebp %esp -= 4 movl %esp, %ebp ;; %ebp = %esp subl $24, %esp ;; %esp -= 24 movl $0, -12(%ebp) ;; [%ebp-12] = 0 ;; int c = \0 ;... call getpid ;; getpid() subl $8, %esp ;; %esp -= 8 pushl %eax ;; push %eax %esp -= 4 pushl $.LC0 ;; push addr(.lc0) %esp -= 4... call printf ;; printf("pid = %d; ", getpid()); addl $16, %esp ;; %esp += 16 subl $12, %esp ;; %esp -= 12 pushl $.LC1 ;; push addr(.lc1) %esp -=4... Young W. Lim Stack Debugging Mon 60 / 63

61 call by reference : func3 (2).LFE0: call puts ;; printf("press <Enter> \n"); addl $16, %esp ;; %esp += 16 movl stdin, %eax ;; %eax = stdin subl $12, %esp ;; %esp -= 12 pushl %eax ;; push %eax %esp -= 4... call fgetc ;; c = fgetc( stdin ); addl $16, %esp ;; %esp -= 16 movl %eax, -12(%ebp) ;; [%ebp-12] = %eax subl $8, %esp ;; %esp -= 8 pushl -12(%ebp) ;; push [%ebp-12] %esp -= 12 pushl $.LC2 ;; push addr(.lc2) %esp -= 4... call printf ;; printf("c=%c\n", c); addl $16, %esp ;; %esp += 16 movl 8(%ebp), %eax ;; %eax = [%ebp+8] ;; *a = 9; movl $9, (%eax) ;; %eax = 9 nop leave ret Young W. Lim Stack Debugging Mon 61 / 63

62 Linux Stack Protector %gs:20 (1) 1 Stack protector works by putting predefined pattern at the start of the stack frame and verifying that it hasn t been overwritten when returning from the function. The pattern is called stack canary and unfortunately gcc requires it to be at a fixed offset from %gs. On x86_64, the offset is 40 bytes and on x86_32 20 bytes. x86_64 and x86_32 use segment registers differently and thus handles this requirement differently. On x86_64, %gs is shared by percpu area and stack canary. All percpu symbols are zero based and %gs points to the base of percpu area. The first occupant of the percpu area is always irq_stack_union which contains stack_canary at offset 40. Userland %gs is always saved and restored on kernel entry and exit using swapgs, so stack protector doesn t add any complexity there. On x86_32, it s slightly more complicated. As in x86_64, %gs is used for userland TLS. Unfortunately, some processors are much slower at loading segment registers with different value when entering and leaving the kernel, so the kernel uses %fs for percpu area and manages %gs lazily so that %gs is switched only when necessary, usually during task switch. As gcc requires the stack canary at %gs:20, %gs can t be managed lazily if stack protector is enabled, so the kernel saves and restores Younguserland W. Lim %gs on kernel entry Stack Debugging and exit. This behavior is Mon 62 / 63

63 Linux Stack Protector %gs:20 (2) 1 - putting predefined pattern at the start of the stack frame - verifying that it hasn t been overwritten when returning - stack canary - a fixed offset from %gs (gcc) - 40 bytes for x86_64-20 bytes for x86 - segment reg %gs Young W. Lim Stack Debugging Mon 63 / 63

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40 Stack Debugging Young W. Lim 2017-07-22 Sat Young W. Lim Stack Debugging 2017-07-22 Sat 1 / 40 Outline 1 Introduction References Compiling to IA32 Assembly Checking /proc//maps file Checking Stack

More information

Stack Debugging. Young W. Lim Tue. Young W. Lim Stack Debugging Tue 1 / 70

Stack Debugging. Young W. Lim Tue. Young W. Lim Stack Debugging Tue 1 / 70 Stack Debugging Young W. Lim 2017-08-08 Tue Young W. Lim Stack Debugging 2017-08-08 Tue 1 / 70 Outline 1 Based on 2 Introduction Compiling to IA32 Assembly Checking /proc//maps file Checking the main

More information

Stack Debugging. Young W. Lim Mon. Young W. Lim Stack Debugging Mon 1 / 77

Stack Debugging. Young W. Lim Mon. Young W. Lim Stack Debugging Mon 1 / 77 Stack Debugging Young W. Lim 2017-08-14 Mon Young W. Lim Stack Debugging 2017-08-14 Mon 1 / 77 Outline 1 Based on 2 Introduction Compiling to IA32 Assembly Checking /proc//maps file Checking the main

More information

Stack Debugging. Young W. Lim Fri. Young W. Lim Stack Debugging Fri 1 / 79

Stack Debugging. Young W. Lim Fri. Young W. Lim Stack Debugging Fri 1 / 79 Stack Debugging Young W. Lim 2017-08-18 Fri Young W. Lim Stack Debugging 2017-08-18 Fri 1 / 79 Outline 1 Based on 2 Introduction Compiling to IA32 Assembly Checking /proc//maps file Checking the main

More information

Stack Debugging. Young W. Lim Wed. Young W. Lim Stack Debugging Wed 1 / 84

Stack Debugging. Young W. Lim Wed. Young W. Lim Stack Debugging Wed 1 / 84 Stack Debugging Young W. Lim 2017-08-23 Wed Young W. Lim Stack Debugging 2017-08-23 Wed 1 / 84 Outline 1 Based on 2 Introduction Compiling to IA32 Assembly Checking /proc//maps file Checking the main

More information

Stack Debugging. Young W. Lim Mon. Young W. Lim Stack Debugging Mon 1 / 90

Stack Debugging. Young W. Lim Mon. Young W. Lim Stack Debugging Mon 1 / 90 Stack Debugging Young W. Lim 2017-08-28 Mon Young W. Lim Stack Debugging 2017-08-28 Mon 1 / 90 Outline 1 Based on 2 Introduction Compiling to IA32 Assembly Checking /proc//maps file Checking the main

More information

Stack Debugging. Young W. Lim Young W. Lim Stack Debugging / 96

Stack Debugging. Young W. Lim Young W. Lim Stack Debugging / 96 Stack Debugging Young W. Lim 2017-09-02 Young W. Lim Stack Debugging 2017-09-02 1 / 96 Outline 1 Based on 2 Introduction Compiling to IA32 Assembly Stack Frame 3 Workings of Stack Frames Stack Frame Pointers

More information

Stack Debugging. Young W. Lim Thr. Young W. Lim Stack Debugging Thr 1 / 12

Stack Debugging. Young W. Lim Thr. Young W. Lim Stack Debugging Thr 1 / 12 Stack Debugging Young W. Lim 2017-07-13 Thr Young W. Lim Stack Debugging 2017-07-13 Thr 1 / 12 Outline 1 Introduction References Compiling to IA32 Assembly Checking /proc//maps file Young W. Lim Stack

More information

Stack Tutorial. Young W. Lim Sat. Young W. Lim Stack Tutorial Sat 1 / 15

Stack Tutorial. Young W. Lim Sat. Young W. Lim Stack Tutorial Sat 1 / 15 Stack Tutorial Young W. Lim 2016-10-15 Sat Young W. Lim Stack Tutorial 2016-10-15 Sat 1 / 15 Outline 1 Introduction References Stack Background Transferring Control Young W. Lim Stack Tutorial 2016-10-15

More information

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29 Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29 Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure

More information

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27 Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27 Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure

More information

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19 Access Young W. Lim 2017-06-10 Sat Young W. Lim Access 2017-06-10 Sat 1 / 19 Outline 1 Introduction References IA32 Operand Forms Data Movement Instructions Data Movement Examples Young W. Lim Access 2017-06-10

More information

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18 Access Young W. Lim 2017-01-27 Fri Young W. Lim Access 2017-01-27 Fri 1 / 18 Outline 1 Introduction References IA32 Operand Forms Data Movement Instructions Young W. Lim Access 2017-01-27 Fri 2 / 18 Based

More information

Lab 10: Introduction to x86 Assembly

Lab 10: Introduction to x86 Assembly CS342 Computer Security Handout # 8 Prof. Lyn Turbak Wednesday, Nov. 07, 2012 Wellesley College Revised Nov. 09, 2012 Lab 10: Introduction to x86 Assembly Revisions: Nov. 9 The sos O3.s file on p. 10 was

More information

GDB Tutorial. Young W. Lim Fri. Young W. Lim GDB Tutorial Fri 1 / 24

GDB Tutorial. Young W. Lim Fri. Young W. Lim GDB Tutorial Fri 1 / 24 GDB Tutorial Young W. Lim 2016-02-19 Fri Young W. Lim GDB Tutorial 2016-02-19 Fri 1 / 24 Outline 1 Introduction Young W. Lim GDB Tutorial 2016-02-19 Fri 2 / 24 Based on Self-service Linux: Mastering the

More information

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24 GDB Tutorial Young W. Lim 2016-09-29 Thr Young W. Lim GDB Tutorial 2016-09-29 Thr 1 / 24 Outline 1 Introduction Young W. Lim GDB Tutorial 2016-09-29 Thr 2 / 24 Based on "Self-service Linux: Mastering the

More information

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32 GDB Tutorial Young W. Lim 2017-02-14 Tue Young W. Lim GDB Tutorial 2017-02-14 Tue 1 / 32 Outline 1 Introduction Young W. Lim GDB Tutorial 2017-02-14 Tue 2 / 32 Based on "Self-service Linux: Mastering the

More information

Link 2. Object Files

Link 2. Object Files Link 2. Object Files Young W. Lim 2017-09-23 Sat Young W. Lim Link 2. Object Files 2017-09-23 Sat 1 / 40 Outline 1 Linking - 2. Object Files Based on Oject Files ELF Sections Example Program Source Codes

More information

Link 2. Object Files

Link 2. Object Files Link 2. Object Files Young W. Lim 2017-09-20 Wed Young W. Lim Link 2. Object Files 2017-09-20 Wed 1 / 33 Outline 1 Linking - 2. Object Files Based on Oject Files ELF Sections Example Program Source Codes

More information

X86 Stack Calling Function POV

X86 Stack Calling Function POV X86 Stack Calling Function POV Computer Systems Section 3.7 Stack Frame Reg Value ebp xffff FFF0 esp xffff FFE0 eax x0000 000E Memory Address Value xffff FFF8 xffff FFF4 x0000 0004 xffff FFF4 x0000 0003

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and returning" Passing parameters" Storing local variables" Handling registers without interference"

More information

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls" 1 Lecture Goals! Challenges of supporting functions" Providing information for the called function" Function arguments and local variables" Allowing

More information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and urning" Passing parameters" Storing local variables" Handling registers without interference"

More information

x86 assembly CS449 Fall 2017

x86 assembly CS449 Fall 2017 x86 assembly CS449 Fall 2017 x86 is a CISC CISC (Complex Instruction Set Computer) e.g. x86 Hundreds of (complex) instructions Only a handful of registers RISC (Reduced Instruction Set Computer) e.g. MIPS

More information

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang Project 3 Questions CMSC 313 Lecture 12 How C functions pass parameters UMBC, CMSC313, Richard Chang Last Time Stack Instructions: PUSH, POP PUSH adds an item to the top of the stack POP

More information

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and urning Passing parameters Storing local variables Handling registers without interference Returning

More information

CPS104 Recitation: Assembly Programming

CPS104 Recitation: Assembly Programming CPS104 Recitation: Assembly Programming Alexandru Duțu 1 Facts OS kernel and embedded software engineers use assembly for some parts of their code some OSes had their entire GUIs written in assembly in

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013 CONST POINTERS CONST POINTERS 4 ways to declare pointers in combination with const:!! int *ptr! const int *ptr!

More information

CS241 Computer Organization Spring 2015 IA

CS241 Computer Organization Spring 2015 IA CS241 Computer Organization Spring 2015 IA-32 2-10 2015 Outline! Review HW#3 and Quiz#1! More on Assembly (IA32) move instruction (mov) memory address computation arithmetic & logic instructions (add,

More information

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016 CS 31: Intro to Systems Functions and the Stack Martin Gagne Swarthmore College February 23, 2016 Reminders Late policy: you do not have to send me an email to inform me of a late submission before the

More information

ASSEMBLY III: PROCEDURES. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung ASSEMBLY III: PROCEDURES Jo, Heeseung IA-32 STACK (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

Assembly III: Procedures. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung Assembly III: Procedures Jo, Heeseung IA-32 Stack (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

CMSC 313 Lecture 12 [draft] How C functions pass parameters

CMSC 313 Lecture 12 [draft] How C functions pass parameters CMSC 313 Lecture 12 [draft] How C functions pass parameters UMBC, CMSC313, Richard Chang Last Time Stack Instructions: PUSH, POP PUSH adds an item to the top of the stack POP removes an

More information

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly III: Procedures Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu IA-32 (1) Characteristics Region of memory managed with stack discipline

More information

Buffer Overflow Attack

Buffer Overflow Attack Buffer Overflow Attack What every applicant for the hacker should know about the foundation of buffer overflow attacks By (Dalgona@wowhacker.org) Email: zinwon@gmail.com 2005 9 5 Abstract Buffer overflow.

More information

Assembly Language Programming - III

Assembly Language Programming - III Assembly Language Programming - III GDB Debugger Please refer to the handout New GDB commands (power.s) info registers (prints all register values) print/d $eax (prints individual register value. Note

More information

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) Function Calls COS 217 Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) 1 Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register

More information

Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17

Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17 Arrays Young W. Lim 2017-02-06 Mon Young W. Lim Arrays 2017-02-06 Mon 1 / 17 Outline 1 Introduction References Array Background Young W. Lim Arrays 2017-02-06 Mon 2 / 17 Based on "Self-service Linux: Mastering

More information

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110 Questions 1 Question 4.1 1: (Solution, p 5) Define the fetch-execute cycle as it relates to a computer processing a program. Your definition should describe the primary purpose of each phase. Question

More information

CS213. Machine-Level Programming III: Procedures

CS213. Machine-Level Programming III: Procedures CS213 Machine-Level Programming III: Procedures Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region of memory managed with stack discipline Grows toward

More information

CPEG421/621 Tutorial

CPEG421/621 Tutorial CPEG421/621 Tutorial Compiler data representation system call interface calling convention Assembler object file format object code model Linker program initialization exception handling relocation model

More information

An Experience Like No Other. Stack Discipline Aug. 30, 2006

An Experience Like No Other. Stack Discipline Aug. 30, 2006 15-410 An Experience Like No Other Discipline Aug. 30, 2006 Bruce Maggs Dave Eckhardt Slides originally stolen from 15-213 15-410, F 06 Synchronization Registration If you're here but not registered, please

More information

Machine-level Programming (3)

Machine-level Programming (3) Machine-level Programming (3) Procedures A: call A call A return Two issues How to return to the correct position? How to pass arguments and return values between callee to caller? 2 Procedure Control

More information

Stack Discipline Jan. 19, 2018

Stack Discipline Jan. 19, 2018 15-410 An Experience Like No Other Discipline Jan. 19, 2018 Dave Eckhardt Brian Railing Slides originally stolen from 15-213 1 15-410, S 18 Synchronization Registration The wait list will probably be done

More information

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize: Simple C Program Helloworld.c Programming and Debugging Assembly under Linux slides by Alexandre Denault int main(int argc, char *argv[]) { } printf("hello World"); Programming and Debugging Assembly under

More information

CPSC W Term 2 Problem Set #3 - Solution

CPSC W Term 2 Problem Set #3 - Solution 1. (a) int gcd(int a, int b) { if (a == b) urn a; else if (a > b) urn gcd(a - b, b); else urn gcd(a, b - a); CPSC 313 06W Term 2 Problem Set #3 - Solution.file "gcdrec.c".globl gcd.type gcd, @function

More information

x86 assembly CS449 Spring 2016

x86 assembly CS449 Spring 2016 x86 assembly CS449 Spring 2016 CISC vs. RISC CISC [Complex instruction set Computing] - larger, more feature-rich instruction set (more operations, addressing modes, etc.). slower clock speeds. fewer general

More information

Intel assembly language using gcc

Intel assembly language using gcc QOTD Intel assembly language using gcc Assembly language programming is difficult. Make no mistake about that. It is not for wimps and weaklings. - Tanenbaum s 6th, page 519 These notes are a supplement

More information

Systems I. Machine-Level Programming V: Procedures

Systems I. Machine-Level Programming V: Procedures Systems I Machine-Level Programming V: Procedures Topics abstraction and implementation IA32 stack discipline Procedural Memory Usage void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp

More information

Machine-Level Programming III: Procedures

Machine-Level Programming III: Procedures Machine-Level Programming III: Procedures IA32 Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address address of top element Bottom Increasing

More information

Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19

Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19 Arrays Young W. Lim 2017-02-08 Wed Young W. Lim Arrays 2017-02-08 Wed 1 / 19 Outline 1 Introduction References Array Background Young W. Lim Arrays 2017-02-08 Wed 2 / 19 Based on "Self-service Linux: Mastering

More information

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls 1 Lecture Goals Challenges of supporting functions Providing information for the called function Function arguments and local variables Allowing the

More information

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 15-213 The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables class07.ppt

More information

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

1 /* file cpuid2.s */ 4.asciz The processor Vendor ID is %s \n 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start. 1 /* file cpuid2.s */ 2.section.data 3 output: 4.asciz "The processor Vendor ID is %s \n" 5.section.bss 6.lcomm buffer, 12 7.section.text 8.globl _start 9 _start: 10 movl $0, %eax 11 cpuid 12 movl $buffer,

More information

Sungkyunkwan University

Sungkyunkwan University Switch statements IA 32 Procedures Stack Structure Calling Conventions Illustrations of Recursion & Pointers long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case

More information

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions CS220 Logic Design Outline Calling Conventions Multi-module Programs 1 Calling and Returning We have already seen how the call instruction is used to execute a subprogram. call pushes the address of the

More information

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call Call without Parameter Value Transfer What are involved? ESP Stack Pointer Register Grows by 4 for EIP (return address) storage Stack -- Memory which holds register contents Will keep the EIP of the next

More information

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address IA32 Procedures 1 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Stack BoDom Increasing Addresses Register contains lowest stack address address of top element Stack

More information

W4118: PC Hardware and x86. Junfeng Yang

W4118: PC Hardware and x86. Junfeng Yang W4118: PC Hardware and x86 Junfeng Yang A PC How to make it do something useful? 2 Outline PC organization x86 instruction set gcc calling conventions PC emulation 3 PC board 4 PC organization One or more

More information

System calls and assembler

System calls and assembler System calls and assembler Michal Sojka sojkam1@fel.cvut.cz ČVUT, FEL License: CC-BY-SA 4.0 System calls (repetition from lectures) A way for normal applications to invoke operating system (OS) kernel's

More information

Process Layout and Function Calls

Process Layout and Function Calls Process Layout and Function Calls CS 6 Spring 07 / 8 Process Layout in Memory Stack grows towards decreasing addresses. is initialized at run-time. Heap grow towards increasing addresses. is initialized

More information

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26 Day06 A Young W. Lim 2017-09-20 Wed Young W. Lim Day06 A 2017-09-20 Wed 1 / 26 Outline 1 Based on 2 C Program Control Overview for, while, do... while break and continue Relational and Logical Operators

More information

Link 4. Relocation. Young W. Lim Thr. Young W. Lim Link 4. Relocation Thr 1 / 26

Link 4. Relocation. Young W. Lim Thr. Young W. Lim Link 4. Relocation Thr 1 / 26 Link 4. Relocation Young W. Lim 2017-09-14 Thr Young W. Lim Link 4. Relocation 2017-09-14 Thr 1 / 26 Outline 1 Linking - 4. Relocation Based on Relocation Relocation Entries Relocating Symbol Reference

More information

CIT Week13 Lecture

CIT Week13 Lecture CIT 3136 - Week13 Lecture Runtime Environments During execution, allocation must be maintained by the generated code that is compatible with the scope and lifetime rules of the language. Typically there

More information

X86 Assembly -Procedure II:1

X86 Assembly -Procedure II:1 X86 Assembly -Procedure II:1 IA32 Object Code Setup Label.L61 becomes address 0x8048630 Label.L62 becomes address 0x80488dc Assembly Code switch_eg:... ja.l61 # if > goto default jmp *.L62(,%edx,4) # goto

More information

CSE 351: Week 4. Tom Bergan, TA

CSE 351: Week 4. Tom Bergan, TA CSE 35 Week 4 Tom Bergan, TA Does this code look okay? int binarysearch(int a[], int length, int key) { int low = 0; int high = length - ; while (low

More information

Machine- Level Programming III: Switch Statements and IA32 Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures Machine- Level Programming III: Switch Statements and IA32 Procedures CS 485: Systems Programming Fall 2015 Instructor: James Griffioen Adapted from slides by R. Bryant and D. O Hallaron (hjp://csapp.cs.cmu.edu/public/instructors.html)

More information

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G CS 33: Week 3 Discussion x86 Assembly (v1.0) Section 1G Announcements - HW2 due Sunday - MT1 this Thursday! - Lab2 out Info Name: Eric Kim (Section 1G, 2-4 PM, BH 5419) Office Hours (Boelter 2432) - Wed

More information

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta 1 The IA-32 Stack and Function Calls CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta 2 Important Registers used with the Stack EIP: ESP: EBP: 3 Important Registers used with the Stack EIP:

More information

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11 X86 Debug Computer Systems Section 3.11 GDB is a Source Level debugger We have learned how to debug at the C level Now, C has been translated to X86 assembler! How does GDB play the shell game? Makes it

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 10 Activation Records THEORY OF COMPILATION EranYahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec10.pptx Reference: Dragon 7.1,7.2. MCD 6.3,6.4.2 1 You are here Compiler txt Source Lexical

More information

Machine Programming 3: Procedures

Machine Programming 3: Procedures Machine Programming 3: Procedures CS61, Lecture 5 Prof. Stephen Chong September 15, 2011 Announcements Assignment 2 (Binary bomb) due next week If you haven t yet please create a VM to make sure the infrastructure

More information

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. Implementing Threads Operating Systems In Depth II 1 Copyright 2018 Thomas W Doeppner All rights reserved The Unix Address Space stack dynamic bss data text Operating Systems In Depth II 2 Copyright 2018

More information

Section 4: Threads and Context Switching

Section 4: Threads and Context Switching CS162 September 19-20, 2017 Contents 1 Warmup 2 1.1 Hello World............................................ 2 2 Vocabulary 2 3 Problems 3 3.1 Join................................................ 3 3.2

More information

Processes (Intro) Yannis Smaragdakis, U. Athens

Processes (Intro) Yannis Smaragdakis, U. Athens Processes (Intro) Yannis Smaragdakis, U. Athens Process: CPU Virtualization Process = Program, instantiated has memory, code, current state What kind of memory do we have? registers + address space Let's

More information

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2 CS162 September 15, 2016 Contents 1 Warmup 2 1.1 Hello World............................................ 2 2 Vocabulary 2 3 Problems 3 3.1 Join................................................ 3 3.2 Stack

More information

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018 238P: Operating Systems Lecture 7: Basic Architecture of a Program Anton Burtsev January, 2018 What is a program? What parts do we need to run code? Parts needed to run a program Code itself By convention

More information

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner HrwCC A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner Universität Salzburg VP Compiler Construction June 26, 2007 Overview 1 Introduction Basic properties Features 2 Selected

More information

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han s and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Announcements Homework Set #2 due Friday at 11 am - extension Program Assignment #1 due Tuesday Feb. 15 at 11 am - note extension

More information

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42 Link 3. Symbols Young W. Lim 2017-09-11 Mon Young W. Lim Link 3. Symbols 2017-09-11 Mon 1 / 42 Outline 1 Linking - 3. Symbols Based on Symbols Symbol Tables Symbol Table Examples main.o s symbol table

More information

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element Machine Representa/on of Programs: Procedures Instructors: Sanjeev Se(a 1 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Stack BoGom Increasing Addresses Register

More information

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1 Homework In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, 361-367 Practice Exam 1 1 In-line Assembly Code The gcc compiler allows you to put assembly instructions in-line

More information

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction E I P CPU isters Condition Codes Addresses Data Instructions Memory Object Code Program Data OS Data Topics Assembly Programmer

More information

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux CNIT 127: Exploit Development Ch 2: Stack Overflows in Linux Stack-based Buffer Overflows Most popular and best understood exploitation method Aleph One's "Smashing the Stack for Fun and Profit" (1996)

More information

Sistemi Operativi. Lez. 16 Elementi del linguaggio Assembler AT&T

Sistemi Operativi. Lez. 16 Elementi del linguaggio Assembler AT&T Sistemi Operativi Lez. 16 Elementi del linguaggio Assembler AT&T Data Sizes Three main data sizes Byte (b): 1 byte Word (w): 2 bytes Long (l): 4 bytes Separate assembly-language instructions E.g., addb,

More information

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find).

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find). Scope and storage class of variables The scope of a variable refers to those portions of a program wherein it may be accessed. Failure to understand scoping rules can lead to two problems: (1) Syntax errors

More information

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows) Secure Programming Lecture 3: Memory Corruption I (Stack Overflows) David Aspinall, Informatics @ Edinburgh 24th January 2017 Outline Roadmap Memory corruption vulnerabilities Instant Languages and Runtimes

More information

Link 4. Relocation. Young W. Lim Wed. Young W. Lim Link 4. Relocation Wed 1 / 22

Link 4. Relocation. Young W. Lim Wed. Young W. Lim Link 4. Relocation Wed 1 / 22 Link 4. Relocation Young W. Lim 2017-09-13 Wed Young W. Lim Link 4. Relocation 2017-09-13 Wed 1 / 22 Outline 1 Linking - 4. Relocation Based on Relocation Relocation Entries Relocating Symbol Reference

More information

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly Raluca Popa Spring 2018 CS 161 Computer Security Discussion 1 Week of January 22, 2018: GDB and x86 assembly Objective: Studying memory vulnerabilities requires being able to read assembly and step through

More information

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16 Day06 A Young W. Lim 2017-09-18 Mon Young W. Lim Day06 A 2017-09-18 Mon 1 / 16 Outline 1 Based on 2 Introduction C Program Control Young W. Lim Day06 A 2017-09-18 Mon 2 / 16 Based on "C How to Program",

More information

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006 C Compilation Model Comp-206 : Introduction to Software Systems Lecture 9 Alexandre Denault Computer Science McGill University Fall 2006 Midterm Date: Thursday, October 19th, 2006 Time: from 16h00 to 17h30

More information

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017 ICS143A: Principles of Operating Systems Midterm recap, sample questions Anton Burtsev February, 2017 Describe the x86 address translation pipeline (draw figure), explain stages. Address translation What

More information

x86 Assembly Tutorial COS 318: Fall 2017

x86 Assembly Tutorial COS 318: Fall 2017 x86 Assembly Tutorial COS 318: Fall 2017 Project 1 Schedule Design Review: Monday 9/25 Sign up for 10-min slot from 3:00pm to 7:00pm Complete set up and answer posted questions (Official) Precept: Monday

More information

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012 19:211 Computer Architecture Lecture 10 Fall 20 Topics:Chapter 3 Assembly Language 3.2 Register Transfer 3. ALU 3.5 Assembly level Programming We are now familiar with high level programming languages

More information

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p text C program (p1.c p2.c) Compiler (gcc -S) text Asm

More information

Process Layout, Function Calls, and the Heap

Process Layout, Function Calls, and the Heap Process Layout, Function Calls, and the Heap CS 6 Spring 20 Prof. Vern Paxson TAs: Devdatta Akhawe, Mobin Javed, Matthias Vallentin January 9, 20 / 5 2 / 5 Outline Process Layout Function Calls The Heap

More information

You may work with a partner on this quiz; both of you must submit your answers.

You may work with a partner on this quiz; both of you must submit your answers. Instructions: Choose the best answer for each of the following questions. It is possible that several answers are partially correct, but one answer is best. It is also possible that several answers are

More information

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final Spring 2012 Name: Page Points Score 1 14 2 7 3 6 4 5 5 5 6 5 7 10 8 11 9 10 10 9 11 8 12 10 Total: 100 Instructions The exam is closed book, closed notes. You may not use a calculator, cell phone, etc.

More information

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson X86 Review Process Layout, ISA, etc. CS642: Computer Security Drew Davidson davidson@cs.wisc.edu From Last Time ACL-based permissions (UNIX style) Read, Write, execute can be restricted on users and groups

More information