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

Similar documents
Stack Discipline Jan. 19, 2018

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

CS213. Machine-Level Programming III: Procedures

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

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

Systems I. Machine-Level Programming V: Procedures

Machine-level Programming (3)

X86 Assembly -Procedure II:1

Machine-Level Programming III: Procedures

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

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

Machine Programming 3: Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures

Sungkyunkwan University

University of Washington

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Giving credit where credit is due

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

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Assembly Language: Function Calls

Machine- Level Programming III: Switch Statements and IA32 Procedures

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

Assembly Language: Function Calls" Goals of this Lecture"

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

Assembly Language: Function Calls" Goals of this Lecture"

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

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

CS429: Computer Organization and Architecture

hnp://

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

CS241 Computer Organization Spring 2015 IA

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Machine-Level Programming I: Introduction Jan. 30, 2001

Machine Programming 1: Introduction

X86 Stack Calling Function POV

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View.

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

Machine-Level Programming III: Procedures

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

CSE 351: Week 4. Tom Bergan, TA

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

IA32 Stack Discipline Aug 30, 2017

Page 1. IA32 Stack CISC 360. Machine-Level Programming III: Procedures Sept. 22, IA32 Stack Popping Stack Bottom. IA32 Stack Pushing

x86 assembly CS449 Fall 2017

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

Machine-level Programs Procedure

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

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

The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002

THEORY OF COMPILATION

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

211: Computer Architecture Summer 2016

Machine-Level Programming Introduction

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

W4118: PC Hardware and x86. Junfeng Yang

Data Representa/ons: IA32 + x86-64

Processes (Intro) Yannis Smaragdakis, U. Athens

Instruction Set Architecture

MACHINE-LEVEL PROGRAMMING I: BASICS

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

IA32 Processors The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, X86 Evolution: Programmer s View

Instruction Set Architecture

! Starting in 1978 with ! Added more features as time goes on. ! Still support old features, although obsolete

Systems Programming and Computer Architecture ( )

CS429: Computer Organization and Architecture

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

CS241 Computer Organization Spring Addresses & Pointers

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CISC 360 Instruction Set Architecture

Instruction Set Architecture

Lecture 4 CIS 341: COMPILERS

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

CPEG421/621 Tutorial

Process Layout and Function Calls

University*of*Washington*

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

CIT Week13 Lecture

X86 Assembly -Data II:1

Today: Machine Programming I: Basics

The Hardware/Software Interface CSE351 Spring 2015

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

Machine- level Programming

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

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Intel assembly language using gcc

x86 Assembly Tutorial COS 318: Fall 2017

Ge-ng at things on the chip you can t easily reach from C

Machine Program: Procedure. Zhaoguo Wang

ANITA S SUPER AWESOME RECITATION SLIDES

Process Layout, Function Calls, and the Heap

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Giving credit where credit is due

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

Transcription:

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 make sure you're at least on the waiting list today Prerequisite/self-assessment status Please fill out the web form today Mid-Term Exam Two plausible days When I ask you to fill out the web form, please do so promptly 2 15-410, S 06

Outline Topics Process memory model IA32 stack organization Register saving conventions Before & after main() Project 0 3 15-410, S 06

Why Only 32? You may have learned x86-64 aka EMT64 aka AMD64 Why will 410 be x86 / IA32? x86-64 is simpler than x86(-32) for user program code Lots of registers, registers more orthogonal x86-64 is not simpler for kernel code Machine begins in 16-bit mode, then 32, finally 64 You don't have time to write 3264 transition code If we gave it to you, it would be a big black box There are still a lot more 32-bit machines in the world...upon which to run your personal OS 4 15-410, S 06

Private Address Spaces Each Linux process has its own private address space. 0xffffffff 0xc0000000 kernel virtual memory (code, data, heap, stack) user stack (created at runtime) memory invisible to user code (stack pointer) Warning: details vary by OS and kernel 0x40000000 memory mapped region for shared libraries run-time heap (managed by malloc) read/write segment (.data,.bss) read-only segment (.init,.text,.rodata) version! 0x08048000 unused 0 5 15-410, S 06 brk loaded from the executable file

Upper 2 hex digits of address FF C0 BF 80 7F Red Hat v. 6.2 ~1920MB memory limit 40 3F 08 00 Heap Shared Libraries Heap Data Text Linux Memory Layout Heap Runtime stack (8MB limit by default) Dynamically allocated storage When call malloc, calloc, new Shared/Dynamic Libraries aka Shared Objects Library routines (e.g., printf, malloc) Linked into object code when first executed Windows has DLLs (semantic differences) Data, BSS Statically allocated data (BSS starts all-zero) e.g., arrays & variables declared in code Text, RODATA Text - Executable machine instructions RODATA Read-only (e.g., const ) String literals 6 15-410, S 06

7 15-410, S 06 Linux Memory Allocation Linked BF 7F 3F 80 40 00 Libraries Text Data 08 Some Heap BF 7F 3F 80 40 00 Libraries Text Data Heap 08 More Heap BF 7F 3F 80 40 00 Libraries Text Data Heap Heap 08 Initially BF 7F 3F 80 40 00 Text Data 08

IA32 Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address address of top element stack pointer Bottom Increasing Addresses Pointer Grows Down Top 8 15-410, S 06

IA32 Pushing Pushing pushl Src Fetch operand at Src Decrement by 4 Write operand at address given by Bottom Increasing Addresses Pointer -4 Grows Down Top 9 15-410, S 06

IA32 Popping Popping popl Dest Read operand at address given by Increment by 4 Bottom Increasing Addresses Write to Dest Pointer +4 Grows Down Top 10 15-410, S 06

Operation Examples pushl %eax popl %edx 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x108 123 0x108 123 0x108 123 0x104 213 0x104 213 %eax 213 %eax 213 %eax 213 %edx 555 %edx 555 %edx 555 213 0x108 0x108 0x104 0x104 0x108 11 15-410, S 06

Procedure Control Flow Use stack to support procedure call and return Procedure call: call labelpush return address on stack; Jump to label Return address value Address of instruction after call Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax Return address = 0x8048553 Procedure return: ret Pop address from stack; Jump to address 12 15-410, S 06

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x108 0x108 0x104 %eip 0x804854e %eip 0x804854e 0x8048b90 %eip is program counter 13 15-410, S 06

Procedure Return Example 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 0x104 0x104 0x108 %eip 0x8048591 %eip 0x8048591 0x8048553 %eip is program counter 14 15-410, S 06

-Based Languages Languages that Support Recursion e.g., C, Pascal, Java Code must be Reentrant Multiple simultaneous instantiations of single procedure Need some place to store state of each instantiation Arguments Local variables Return pointer (maybe) Weird things (static links, exception handling, ) Discipline State for given procedure needed for limited time From time of call to time of return Callee returns before caller does Allocated in Frames State for single procedure instantiation 15 15-410, S 06

Call Chain Example Code Structure yoo( ) { who(); } who( ) { ami(); ami(); } Procedure ami() recursive ami( ) { ami(); } Call Chain yoo who ami ami ami ami 16 15-410, S 06

Frames Contents Local variables Return information Temporary space Management Pointers Space allocated when enter procedure Set-up code Deallocated when return Finish code pointer indicates stack top Frame pointer %ebp indicates start of current frame Pointer 17 15-410, S 06 yoo Frame Pointer %ebp who proc ami Top

IA32/Linux Frame Current Frame ( Top to Bottom) Parameters for function about to call Argument build Local variables If can't keep in registers Saved register context Old frame pointer Caller Frame Return address Pushed by call instruction Caller Frame Frame Pointer (%ebp) Arguments for this call Pointer () Arguments Return Addr Old %ebp Saved Registers + Local Variables Argument Build 18 15-410, S 06

swap int zip1 = 15213; int zip2 = 91125; void call_swap() { swap(&zip1, &zip2); } Calling swap from call_swap call_swap: pushl $zip2 pushl $zip1 call swap # Global Var # Global Var void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } &zip2 &zip1 Rtn adr Resulting 19 15-410, S 06

swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp, popl %ebp ret Set Up Body Finish 20 15-410, S 06

swap Setup #1 Entering %ebp Resulting %ebp &zip2 &zip1 Rtn adr yp xp Rtn adr Old %ebp swap: pushl %ebp movl,%ebp pushl %ebx 21 15-410, S 06

swap Setup #2 Entering %ebp Resulting &zip2 &zip1 Rtn adr swap: pushl %ebp movl,%ebp pushl %ebx yp xp Rtn adr Old %ebp %ebp 22 15-410, S 06

swap Setup #3 Entering %ebp Resulting &zip2 &zip1 Rtn adr swap: pushl %ebp movl,%ebp pushl %ebx yp xp Rtn adr Old %ebp Old %ebx %ebp 23 15-410, S 06

Effect of swap Setup Entering %ebp Offset (relative to %ebp) Resulting &zip2 12 yp &zip1 8 xp Rtn adr 4 0 Rtn adr Old %ebp %ebp Old %ebx movl 12(%ebp),%ecx # get yp movl 8(%ebp),%edx # get xp... Body 24 15-410, S 06

swap Finish #1 swap s Offset Offset 12 yp 12 yp 8 xp 8 xp 4 0-4 Rtn adr Old %ebp Old %ebx %ebp 4 0-4 Rtn adr Old %ebp Old %ebx %ebp Observation Saved & restored register %ebx movl -4(%ebp),%ebx movl %ebp, popl %ebp ret 25 15-410, S 06

swap Finish #2 swap s Offset swap s Offset 12 8 4 0-4 yp xp Rtn adr Old %ebp Old %ebx %ebp 12 8 4 0 yp xp Rtn adr Old %ebp %ebp movl -4(%ebp),%ebx movl %ebp, popl %ebp ret 26 15-410, S 06

swap Finish #3 swap s Offset 12 8 4 0 yp xp Rtn adr Old %ebp %ebp swap s Offset 12 8 4 yp xp Rtn adr %ebp movl -4(%ebp),%ebx movl %ebp, popl %ebp ret 27 15-410, S 06

swap Finish #4 swap s Offset 12 8 4 yp xp Rtn adr %ebp &zip2 &zip1 %ebp Exiting Observation Saved & restored register %ebx Didn't do so for %eax, %ecx, or %edx movl -4(%ebp),%ebx movl %ebp, popl %ebp ret 28 15-410, S 06

Register Saving Conventions When procedure yoo calls who: yoo is the caller, who is the callee Can Register be Used for Temporary Storage? yoo: movl $15213, %edx call who addl %edx, %eax ret who: movl 8(%ebp), %edx addl $91125, %edx ret Contents of register %edx overwritten by who 29 15-410, S 06

Register Saving Conventions When procedure yoo calls who: yoo is the caller, who is the callee Can Register be Used for Temporary Storage? Definitions Caller Save register Caller saves temporary in its frame before calling Callee Save register Conventions Callee saves temporary in its frame before using Which registers are caller-save, callee-save? 30 15-410, S 06

IA32/Linux Register Usage Integer Registers Two have special uses %ebp, Three managed as callee-save %ebx, %esi, %edi Old values saved on stack prior to using Three managed as caller-save %eax, %edx, %ecx Do what you please, but expect any callee to do so, as well Register %eax also stores returned value Caller-Save Temporaries Callee-Save Temporaries Special %eax %edx %ecx %ebx %esi %edi %ebp 31 15-410, S 06

Summary The Makes Recursion Work Private storage for each instance of procedure call Instantiations don't clobber each other Addressing of locals + arguments can be relative to stack positions Can be managed by stack discipline Procedures return in inverse order of calls IA32 Procedures Combination of Instructions + Conventions call / ret instructions Register usage conventions Caller / Callee save %ebp and frame organization conventions 32 15-410, S 06

Before & After main() int main(int argc, char *argv[]) { if (argc > 1) { printf( %s\n, argv[1]); } else { char * av[3] = { 0, 0, 0 }; av[0] = argv[0]; av[1] = Fred ; execvp(av[0], av); } return (1); } 33 15-410, S 06

The Mysterious Parts argc, argv Strings from one program Available while another program is running Which part of the memory map are they in? How did they get there? What happens when main() does return(1) return(1)??? There's no more program to run...right? Where does the 1 go? How does it get there? 410 students should seek to abolish mystery 34 15-410, S 06

The Mysterious Parts argc, argv Strings from one program Available while another program is running Inter-process sharing/information transfer is OS's job OS copies strings from old address space to new in exec() Traditionally placed below bottom of stack Other weird things (environment, auxiliary vector) (above argv) arg vector main() printf()... 35 15-410, S 06

The Mysterious Parts What happens when main() does return(1) return(1)??? Defined by C to have same effect as exit(1) But how?? The main() wrapper Receives argc, argv from OS Calls main(), then calls exit() Provided by C library, traditionally in crt0.s Often has a strange name /* not actual code */ void ~~main(int argc, char *argv[]) { exit(main(argc, argv); } 36 15-410, S 06

Project 0 - Crawler C/Assembly function Can be called by any C function Prints stack frames in a symbolic way --- Trace Follows--- Function fun3(c='c', d=2.090000d), in Function fun2(f=35.000000f), in Function fun1(count=0), in Function fun1(count=1), in Function fun1(count=2), in... 37 15-410, S 06

Project 0 - Crawler Conceptually easy Calling convention specifies layout of stack is just memory - available for you to inspect Key questions How do I know 0x80334720 is fun1? How do I know fun3()'s second parameter is called d? 38 15-410, S 06

Project 0 Data Flow fun.c tb.c tb_globals.c symbol-table array many slots, blank 39 15-410, S 06

Project 0 Data Flow - Compilation libtraceback.a fun.o tb.o tb_globals.o 40 15-410, S 06

Project 0 Data Flow - Linking fun fun.o tb.o tb_globals.o debugger info 41 15-410, S 06

Project 0 Data Flow - P0 Post- Linking fun mutate symtabgen simplify fun.o tb.o tb_globals.o debugger info 42 15-410, S 06

Summary Review of stack knowledge What makes main() special Project 0 overview Look for handout this evening Start interviewing Project 2/3/4 partners! 43 15-410, S 06