COMPILING OBJECTS AND OTHER LANGUAGE IMPLEMENTATION ISSUES Credit: Mostly Bryant & O’Hallaron.

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Linking Today Static linking Object files Static & dynamically linked libraries Next time Exceptional control flows.
Advertisements

MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
Linker and Loader. Program building into four stages (C Program) Preprocessing (Preprocessor) It processes include files, conditional compilation instructions.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
CS 4284 Systems Capstone Godmar Back Linking and Loading.
Winter 2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University Linking.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Generating Programs and Linking Professor Rick Han Department of Computer Science University of Colorado at Boulder.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
Processes CSCI 444/544 Operating Systems Fall 2008.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Run time vs. Compile time
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
1 OS Structure, Processes & Process Management. 2 What is a Process? A process is a program during execution.  Program = static file (image)  Process.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Carnegie Mellon 1 Linking Lecture, Apr. 11, 2013 These slides are from website which accompanies the book “Computer Systems: A.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
MIPS coding. SPIM Some links can be found such as:
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Languages and the Machine Chapter 5 CS221. Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions.
Topic 2d High-Level languages and Systems Software
Linking Topics Static linking Dynamic linking Case study: Library interpositioning.
Linking Ⅱ.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
Linking Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
Programs and Processes Jeff Chase Duke University.
F’08 Stack Discipline Aug. 27, 2008 Nathaniel Wesley Filardo Slides stolen from CMU , whence they were stolen from CMU CS 438.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5.
CSc 453 Linking and Loading
Linking I Topics Assembly and symbol resolution Static linking Systems I.
Program Translation and Execution I: Linking Sept. 29, 1998 Topics object files linkers class11.ppt Introduction to Computer Systems.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hello world !!! ASCII representation of hello.c.
Linking Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Lecture 3 Translation.
Reading Condition Codes (Cont.)
Instruction Set Architecture
Slides adapted from Bryant and O’Hallaron
Overview of today’s lecture
Linking Topics Static linking Object files Static libraries Loading
The University of Adelaide, School of Computer Science
Program Execution in Linux
CS 5204 Operating Systems Linking and Loading Godmar Back.
Machine-Level Programming 4 Procedures
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
More examples How many processes does this piece of code create?
CS 4284 Systems Capstone Linking and Loading Godmar Back.
“The course that gives CMU its Zip!”
Memory Allocation CS 217.
Machine-Level Programming III: Procedures Sept 18, 2001
“The course that gives CMU its Zip!”
System Calls David Ferry CSCI 3500 – Operating Systems
Lecture 6: Multiprogramming and Context Switching
Program Execution in Linux
10/6: Lecture Topics C Brainteaser More on Procedure Call
“Way easier than when we were students”
Instructors: Majd Sakr and Khaled Harras
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

COMPILING OBJECTS AND OTHER LANGUAGE IMPLEMENTATION ISSUES Credit: Mostly Bryant & O’Hallaron

2 University of Texas at Austin Word-Oriented Memory Organization Addresses Specify Byte Locations Address of first byte in word Addresses of successive words differ by 4 (32-bit) or 8 (64-bit) bit Words BytesAddr bit Words Addr = ?? Addr = ?? Addr = ?? Addr = ?? Addr = ?? Addr = ??

3 University of Texas at Austin Where do addresses come from? The compilation pipeline prog P : foo() : end P prog P : foo() : end P P: : push... inc SP, x jmp _foo : foo:... P: : push... inc SP, x jmp _foo : foo:... : push... inc SP, 4 jmp 75 :... : push... inc SP, 4 jmp 75 : Library Routines Library Routines Library Routines CompilationAssemblyLinkingLoading : jmp 1175 :... : jmp 175 :... : jmp 175 :...

4 University of Texas at Austin Monomorphic & Polymorphic Monomorphic swap function void swap(int& x, int& y){ int tmp = x; x = y; y = tmp; } Polymorphic function template template void swap(T& x, T& y){ T tmp = x; x = y; y = tmp; } Call like ordinary function float a, b; … ; swap(a,b); … Credit: John Mitchell

5 University of Texas at Austin Obtaining Abstraction C: qsort( (void*)v, N, sizeof(v[0]), compare_int ); C++, using raw C arrays: int v[N]; sort( v, v+N ); C++, using a vector class: vector v(N); sort( v.begin(), v.end() ); Credit: John Mitchell

6 University of Texas at Austin Language concepts Dynamic lookup different code for different object integer “+” different from real “+” Encapsulation Implementer of a concept has detailed view User has “abstract” view Encapsulation separates these two views Subtyping Relation between interfaces Inheritance Relation between implementations Credit: John Mitchell

7 University of Texas at Austin Virtual functions (vptr & vtable) class Base { // Inserted by compiler! FunctionPointer *__vptr; public: virtual void function1() {}; virtual void function2() {}; }; class D1: public Base { public: virtual void function1() {}; }; class D2: public Base { public: virtual void function2() {}; }; Credit: learncpp.com

8 University of Texas at Austin text binary Compiler ( gcc -S ) Assembler ( gcc or as ) Linker ( gcc or ld ) C program ( p1.c p2.c ) Asm program ( p1.s p2.s ) Object program ( p1.o p2.o ) Executable program ( p ) Static libraries (.a ) Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc –O1 p1.c p2.c -o p Use basic optimizations ( -O1 ) Put resulting binary in file p

9 University of Texas at Austin Compiling Into Assembly C Code int sum(int x, int y) { int t = x+y; return t; } Generated IA32 Assembly sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax popl %ebp ret Obtain with command /usr/local/bin/gcc –O1 -S code.c Produces file code.s Some compilers use instruction “ leave ”

10 University of Texas at Austin Call Chain Example yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); } yoo who amI Example Call Chain amI Procedure amI() is recursive

11 University of Texas at Austin Frame Pointer: %ebp Stack Frames Contents Local variables Return information Temporary space Management Space allocated when enter procedure “Set-up” code Deallocated when return “Finish” code Stack Pointer: %esp Stack “Top” Previous Frame Frame for proc

12 University of Texas at Austin Example yoo who amI yoo %ebp %esp Stack yoo yoo(…) { who(); } yoo(…) { who(); }

13 University of Texas at Austin yoo(…) { who(); } yoo(…) { who(); } Example yoo who amI yoo %ebp %esp Stack yoo who who(…) { amI(); amI(); } who(…) { amI(); amI(); }

14 University of Texas at Austin yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } Example yoo who amI yoo %ebp %esp Stack yoo who amI amI(…) { amI(); } amI(…) { amI(); }

15 University of Texas at Austin Example yoo who amI yoo %ebp %esp Stack yoo who amI yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); }

16 University of Texas at Austin Example yoo who amI yoo %ebp %esp Stack yoo who amI yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); }

17 University of Texas at Austin Example yoo who amI yoo %ebp %esp Stack yoo who amI yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); }

18 University of Texas at Austin Example yoo who amI yoo %ebp %esp Stack yoo who amI yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); }

19 University of Texas at Austin Example yoo who amI yoo %ebp %esp Stack yoo who yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); }

20 University of Texas at Austin Example yoo who amI yoo %ebp %esp Stack yoo who amI yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); }

21 University of Texas at Austin Example yoo who amI yoo %ebp %esp Stack yoo who yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); }

22 University of Texas at Austin Example yoo who amI yoo %ebp %esp Stack yoo yoo(…) { who(); } yoo(…) { who(); }

23 University of Texas at Austin IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) “Argument build:” Parameters for function about to call Local variables If can’t keep in registers Saved register context Old frame pointer Caller Stack Frame Return address Pushed by call instruction Arguments for this call Return Addr Saved Registers + Local Variables Argument Build Old %ebp Arguments Caller Frame Frame pointer %ebp Stack pointer %esp

24 University of Texas at Austin Program to Process We write a program in e.g., C. A compiler turns that program into an instruction list. The CPU interprets the instruction list (which is more a graph of basic blocks). void X (int b) { if(b == 1) { … int main() { int a = 2; X(a); }

25 University of Texas at Austin Process in Memory Program to process. void X (int b) { if(b == 1) { … int main() { int a = 2; X(a); } What you wrote What is in memory. void X (int b) { if(b == 1) { … int main() { int a = 2; X(a); } Code main; a = 2 X; b = 2 Heap Stack

26 University of Texas at Austin Processes and Process Management A program consists of code and data On running a program, the OS loader: Reads and interprets the executable file Sets up the process’s memory to contain the code & data from executable Pushes argc, argv on the stack Sets the CPU registers & calls _start() Program starts executing at _start() _start(args) { initialize_language_runtime(); ret = main(args); exit(ret) } Process is now running from program file When main() returns, runtime calls exit system call which destroys the process and returns all resources

pid = 127 open files = “.history” last_cpu = 0 pid = 128 open files = “.history” last_cpu = 0 A shell forks and then execs a calculator int pid = fork(); if(pid == 0) { close(“.history”); exec(“/bin/calc”); } else { wait(pid); int pid = fork(); if(pid == 0) { close(“.history”); exec(“/bin/calc”); } else { wait(pid); Process Control Blocks (PCBs) OS USER int pid = fork(); if(pid == 0) { close(“.history”); exec(“/bin/calc”); } else { wait(pid); int calc_main(){ int q = 7; do_init(); ln = get_input(); exec_in(ln); pid = 128 open files = last_cpu = 0 int pid = fork(); if(pid == 0) { close(“.history”); exec(“/bin/calc”); } else { wait(pid);

pid = 127 open files = “.history” last_cpu = 0 pid = 128 open files = “.history” last_cpu = 0 A shell forks and then execs a calculator int shell_main() { int a = 2; … Code main; a = 2 Heap Stack 0xFC0933CA int shell_main() { int a = 2; … Code main; a = 2 Heap Stack 0xFC0933CA int calc_main() { int q = 7; … Code Heap Stack 0x pid = 128 open files = last_cpu = 0 Process Control Blocks (PCBs) OS USER

29 University of Texas at Austin Anatomy of an address space Code Header Initialized data Executable File Code Initialized data Heap Stack DLL’s mapped segments Process’s address space Inaccessible

30 University of Texas at Austin Linker Symbols Global symbols Symbols defined by module m that can be referenced by other modules. E.g.: non- static C functions and non- static global variables. External symbols Global symbols that are referenced by module m but defined by some other module. Local symbols Symbols that are defined and referenced exclusively by module m. E.g.: C functions and variables defined with the static attribute. Local linker symbols are not local program variables

31 University of Texas at Austin Resolving Symbols int buf[2] = {1, 2}; int main() { swap(); return 0; } main.c extern int buf[]; int *bufp0 = &buf[0]; static int *bufp1; void swap() { int temp; bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp; } swap.c Global External Local Global Linker knows nothing of temp Global

32 University of Texas at Austin Relocating Code and Data main() main.o int *bufp0=&buf[0] swap() swap.o int buf[2]={1,2} Headers main() swap() 0 System code int *bufp0=&buf[0] int buf[2]={1,2} System data More system code System data Relocatable Object FilesExecutable Object File.text.data.text.data.text.data.symtab.debug.data int *bufp1.bss System code static int *bufp1.bss Even though private to swap, requires allocation in.bss

33 University of Texas at Austin Strong and Weak Symbols Program symbols are either strong or weak Strong: procedures and initialized globals Weak: uninitialized globals int foo=5; p1() { } int foo; p2() { } p1.cp2.c strong weak strong

34 University of Texas at Austin Linker’s Symbol Rules Rule 1: Multiple strong symbols are not allowed Each item can be defined only once Otherwise: Linker error Rule 2: Given a strong symbol and multiple weak symbol, choose the strong symbol References to the weak symbol resolve to the strong symbol Rule 3: If there are multiple weak symbols, pick an arbitrary one Can override this with gcc –fno-common

35 University of Texas at Austin Linker Puzzles int x; p1() {} int x; p2() {} int x; int y; p1() {} double x; p2() {} int x=7; int y=5; p1() {} double x; p2() {} int x=7; p1() {} int x; p2() {} int x; p1() {} Link time error: two strong symbols ( p1 ) References to x will refer to the same uninitialized int. Is this what you really want? Writes to x in p2 might overwrite y ! Evil! Writes to x in p2 will overwrite y ! Nasty! Nightmare scenario: two identical weak structs, compiled by different compilers with different alignment rules. References to x will refer to the same initialized variable.

36 University of Texas at Austin Using Static Libraries Linker’s algorithm for resolving external references: Scan.o files and.a files in the command line order. During scan, keep a list of the current unresolved references. As each new.o or.a file, obj, is encountered, try to resolve each unresolved reference against the symbols defined in obj. If any entries in the unresolved list at end of scan, then error. Problem: Command line order matters! Moral: put libraries at the end of the command line. unix> gcc -L. libtest.o -lmine unix> gcc -L. -lmine libtest.o libtest.o: In function `main': libtest.o(.text+0x4): undefined reference to `libfun'

37 University of Texas at Austin Loading Executable Object Files ELF header Program header table (required for executables).text section.data section.bss section.symtab.debug Section header table (required for relocatables) 0 Executable Object File Kernel virtual memory Memory-mapped region for shared libraries Run-time heap (created by malloc ) User stack (created at runtime) Unused 0 %esp (stack pointer) Memory outside 32-bit address space brk 0x x xf7e9ddc0 Read/write segment (. data,. bss ) Read-only segment (.init,. text,.rodata ) Loaded from the executable file.rodata section.line.init section.strtab

38 University of Texas at Austin Definitions Architecture: (also instruction set architecture: ISA) The parts of a processor design that one needs to understand to write assembly code. Examples: instruction set specification, registers. Microarchitecture: Implementation of the architecture. Examples: cache sizes and core frequency. Example ISAs (Intel): x86, ARM