PowerPoint PresentationWhere Are We Now?Link Editor/Linker (1/3)Link Editor/Linker (2/3)Link Editor/Linker (3/3)Four Types of Addresses we’ll discussAbsolute Addresses in MIPSResolving References (1/2)Resolving References (2/2)Static vs Dynamically linked librariesSlide 11Loader (1/3)Loader (2/3)Loader (3/3)AdministriviaExample: C Asm Obj Exe RunExample: C Asm Obj Exe RunSlide 18Symbol Table EntriesSlide 20Slide 21Slide 22Slide 23Peer InstructionPeer Instruction AnswerThings to Remember (1/3)Things to Remember (2/3)Things to Remember 3/3CS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (1)Garcia 2005 © UCBLecturer PSOE Dan Garciawww.cs.berkeley.edu/~ddgarciainst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 19 – Running a Program IIaka Compiling, Assembling, Linking, Loading (CALL)Napster NOT hacked Actually, it was that theyfigured out how to download the stream into a file (ala putting a mike to the speakers) with no quality loss. Users/Napster happy. Apple? :(www.techreview.com/articles/05/02/wo/wo_hellweg021805.aspCS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (2)Garcia 2005 © UCBWhere Are We Now?C program: foo.cAssembly program: foo.sExecutable(mach lang pgm): a.outCompilerAssemblerLinkerLoaderMemoryObject(mach lang module): foo.olib.oCS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (3)Garcia 2005 © UCBLink Editor/Linker (1/3)•Input: Object Code, information tables(e.g., foo.o for MIPS)•Output: Executable Code(e.g., a.out for MIPS)•Combines several object (.o) files into a single executable (“linking”) •Enable Separate Compilation of files•Changes to one file do not require recompilation of whole program-Windows NT source is >40 M lines of code! •Link Editor name from editing the “links” in jump and link instructionsCS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (4)Garcia 2005 © UCBLink Editor/Linker (2/3).o file 1text 1data 1info 1.o file 2text 2data 2info 2Linkera.outRelocated text 1Relocated text 2Relocated data 1Relocated data 2CS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (5)Garcia 2005 © UCBLink Editor/Linker (3/3)•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 end of text segments.•Step 3: Resolve References•Go through Relocation Table and handle each entry•That is, fill in all absolute addressesCS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (6)Garcia 2005 © UCBFour Types of Addresses we’ll discuss•PC-Relative Addressing (beq, bne): never relocate•Absolute Address (j, jal): always relocate•External Reference (usually jal): always relocate•Data Reference (often lui and ori): always relocateCS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (7)Garcia 2005 © UCBAbsolute Addresses in MIPS•Which instructions need relocation editing?•J-format: jump, jump and linkj/jal xxxxx•Loads and stores to variables in static area, relative to global pointerlw/sw $gp $x address•What about conditional branches?beq/bne $rs $rt address•PC-relative addressing preserved even if code movesCS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (8)Garcia 2005 © UCBResolving References (1/2)•Linker assumes first word of first text segment is at address 0x00000000.•Linker knows:•length of each text and data segment•ordering of text and data segments•Linker calculates:•absolute address of each label to be jumped to (internal or external) and each piece of data being referencedCS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (9)Garcia 2005 © UCBResolving References (2/2)•To resolve references:•search for reference (data or label) in all symbol tables•if not found, search library files (for example, for printf)•once absolute address is determined, fill in the machine code appropriately•Output of linker: executable file containing text and data (plus header)CS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (10)Garcia 2005 © UCBStatic vs Dynamically linked libraries•What we’ve described is the traditional way to create a static-linked approach•The library is now part of the executable, so if the library updates we don’t get the fix (have to recompile if we have source)•In includes the entire library even if not all of it will be used.•An alternative is dynamically linked libraries (DLL), common on Windows & UNIX platforms•1st run overhead for dynamic linker-loader•Having executable isn’t enough anymore!CS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (11)Garcia 2005 © UCBWhere Are We Now?C program: foo.cAssembly program: foo.sExecutable(mach lang pgm): a.outCompilerAssemblerLinkerLoaderMemoryObject(mach lang module): foo.olib.oCS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (12)Garcia 2005 © UCBLoader (1/3)•Input: Executable Code(e.g., a.out for MIPS)•Output: (program is run)•Executable files are stored on disk.•When one is run, loader’s job is to load it into memory and start it running.•In reality, loader is the operating system (OS) •loading is one of the OS tasksCS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (13)Garcia 2005 © UCBLoader (2/3)•So what does a loader do?•Reads executable file’s header to determine size of text and data segments•Creates new address space for program large enough to hold text and data segments, along with a stack segment•Copies instructions and data from executable file into the new address space (this may be anywhere in memory)CS61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (14)Garcia 2005 © UCBLoader (3/3)•Copies arguments passed to the program onto the stack•Initializes machine registers•Most registers cleared, but stack pointer assigned address of 1st free stack location•Jumps to start-up routine that copies program’s arguments from stack to registers and sets the PC•If main routine returns, start-up routine terminates program with the exit system callCS61C L19 Running a Program aka Compiling, Assembling,
View Full Document