Lecture Slides are adapted from Prof. Valeriu Beiu (Washington State University, Spring 2005, EE 334)
Download ReportTranscript Lecture Slides are adapted from Prof. Valeriu Beiu (Washington State University, Spring 2005, EE 334)
Lecture Slides are adapted from Prof. Valeriu Beiu (Washington State University, Spring 2005, EE 334) C program: foo.c Compiler Assembly program: foo.s Assembler Object (machine lang. module): foo.o Linker lib.o Executable (machine lang. prgm): a.out Loader Memory 2 Input: High-level language code ◦ E.g. C, Java Output: Assembly language code ◦ E.g. MIPS, x86 Note: Output may contain pseudoinstructions ◦ Instructions that assembler understands but are not in machine 3 C program: foo.c Compiler Assembly program: foo.s Assembler Object (machine lang. module): foo.o Linker lib.o Executable (machine lang. prgm): a.out Loader Memory 4 Reads and uses directives Replace pseudoinstructions Produce machine language Creates object file 5 Give directions to assembler, but does not produce machine instructions: .text: .data: .globl sym: .asciiz str: .word w1…wn: 6 E.g. ◦ move $t0, $t1 add $t0, $t1, $zero ◦ subu $sp, $sp, 32 addiu $sp, $sp, -32 ◦ ble $t0, 100, loop slti $at, $t0, 101 bne $at, $zero, loop ◦ la $a0, str lui $at, left(str) ori $a0, $at, right(str) ◦ mul $t7, $t6, $t6 mult $t6, $t6 mflo $t7 7 Arithmetic, logical, shifts, etc. ◦ All necessary info is already within the instruction What about branches? ◦ PC-Relative ◦ Once pseudoinstructions are replaced, we know by how many instructions to branch What about jumps (j and jal)? What about references to data? ◦ la is translated to lui and ori 8 Jumps and references to data? ◦ Require full 32-bit addresses to instructions or data ◦ Can’t be determined yet Must wait to see where code will appear in final program Use two tables ◦ Symbol table ◦ Relocation table 9 Symbol table ◦ List of items in this file that may be used by this and other files ◦ First pass: record label-address pairs ◦ Second pass: produce machine code Relocation table ◦ Line numbers of items for this file which needs the address completed/fixed later Any label jumped to by j or jal (internal or external) Any absolute address of piece of data (e.g. la $s0, label) 10 Object file header Text segment Data segment Relocation table Symbol table Debugging information 11 C program: foo.c Compiler Assembly program: foo.s Assembler Object (machine lang. module): foo.o Linker lib.o Executable (machine lang. prgm): a.out Loader Memory 12 Combines several object (.o) files into a single executable (“linking”) Enables separate compilation of files/modules ◦ Changes to one file do not require recompilation of whole program Think of editing/recompiling the source for Window OS! Link Editor name from editing the “links” in jump and link instructions 13 Step 1: Take text segment from each .o file and put them together Step 2: Take data segment from each .o file, put them together, and concatenate this onto the end of the text segments Step 3: Resolve references ◦ Go through relocation table and handle each entry using the symbol table Fill in the absolute addresses 14 Output of linker: ◦ Executable file containing text and data (plus header) ◦ May not have library object files resolved if dynamically loaded 15 C program: foo.c Compiler Assembly program: foo.s Assembler Object (machine lang. module): foo.o Linker lib.o Executable (machine lang. prgm): a.out Loader Memory 16 Executable files are stored on disk When one is to be run, loader must load it into memory and start it running Loader is the operating system (one of the OS tasks) 17 Reads executable file’s header to determine size of text and data segments Creates new address space for program to hold the text and data segments, plus a stack segment Copies instructions and data from executable file into the new address space 18 Copies program’s arguments onto the stack Initializes machine registers Jumps to start-up routine ◦ Copies program’s arguments from stack to registers ◦ Sets the PC 19 Both the loader and linker are part of the operating system ◦ Modules can be linked and loaded at runtime If a module is needed and already loaded, it need not be loaded again DLL – Dynamic-Link Library 20 C program: foo.c Compiler Assembly program: foo.s Assembler Object (machine lang. module): foo.o Linker lib.o Executable (machine lang. prgm): a.out Loader Memory 21