DOC PREVIEW
Berkeley COMPSCI 61C - Lecture Notes

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (1)Garcia, Fall 2004 © UCBLecturer PSOE Dan Garciawww.cs.berkeley.edu/~ddgarciainst.eecs.berkeley.edu/~cs61cCS61C : Machine Structures Lecture 19 – Running a Program IIaka Compiling, Assembling, Linking, Loading (CALL) 2004-10-13Holiday present?⇒ Segway’s new ideain transportation is called theCentaur, which allows for lean-forward acceleration, wheelieturns, and an enviable ride. Bethe first on your block!!!!!!!!!!!!!www.segway.com/centaurCS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (2)Garcia, Fall 2004 © UCBWhere Are We Now?C program: foo.cAssembly program: foo.sExecutable(mach lang pgm): a.outCompilerAssemblerLinkerLoaderMemoryObject(mach lang module): foo.olib.oCS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (3)Garcia, Fall 2004 © 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 intoa single executable (“linking”)• Enable Separate Compilation of files• Changes to one file do not requirerecompilation of whole program- Windows NT source is >40 M lines of code!• Link Editor name from editing the “links”in jump and link instructionsCS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (4)Garcia, Fall 2004 © 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 2CS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (5)Garcia, Fall 2004 © 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, andconcatenate this onto end of textsegments.• Step 3: Resolve References• Go through Relocation Table and handleeach entry• That is, fill in all absolute addressesCS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (6)Garcia, Fall 2004 © UCBFour Types of Addresses• PC-Relative Addressing (beq, bne):never relocate• Absolute Address (j, jal): alwaysrelocate• External Reference (usually jal):always relocate• Data Reference (often lui and ori):always relocateCS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (7)Garcia, Fall 2004 © UCBAbsolute Addresses in MIPS• Which instructions need relocationediting?• J-format: jump, jump and linkj/jal xxxxx• Loads and stores to variables in staticarea, relative to global pointerlw/sw $gp $x address• What about conditional branches?beq/bne $rs $rt address• PC-relative addressing preserved evenif code movesCS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (8)Garcia, Fall 2004 © UCBResolving References (1/2)• Linker assumes first word of first textsegment 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 bejumped to (internal or external) and eachpiece of data being referencedCS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (9)Garcia, Fall 2004 © UCBResolving References (2/2)• To resolve references:• search for reference (data or label) in allsymbol tables• if not found, search library files(for example, for printf)• once absolute address is determined, fillin the machine code appropriately• Output of linker: executable filecontaining text and data (plus header)CS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (10)Garcia, Fall 2004 © UCBStatic vs Dynamically linked libraries• What we’ve described is the traditionalway to create a static-linked approach• The library is now part of the executable,so if the library updates we don’t get thefix (have to recompile if we have source)• In includes the entire library even if not allof it will be used.• An alternative is dynamically linkedlibraries (DLL), common on Windows &UNIX platforms• 1st run overhead for dynamic linker-loader• Having executable isn’t enough anymore!CS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (11)Garcia, Fall 2004 © UCBWhere Are We Now?C program: foo.cAssembly program: foo.sExecutable(mach lang pgm): a.outCompilerAssemblerLinkerLoaderMemoryObject(mach lang module): foo.olib.oCS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (12)Garcia, Fall 2004 © 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 toload it into memory and start itrunning.• In reality, loader is the operatingsystem (OS)• loading is one of the OS tasksCS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (13)Garcia, Fall 2004 © UCBLoader (2/3)• So what does a loader do?• Reads executable file’s header todetermine size of text and datasegments• Creates new address space forprogram large enough to hold text anddata segments, along with a stacksegment• Copies instructions and data fromexecutable file into the new addressspace (this may be anywhere inmemory)CS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (14)Garcia, Fall 2004 © UCBLoader (3/3)• Copies arguments passed to theprogram onto the stack• Initializes machine registers• Most registers cleared, but stack pointerassigned address of 1st free stacklocation• Jumps to start-up routine that copiesprogram’s arguments from stack toregisters and sets the PC• If main routine returns, start-up routineterminates program with the exit systemcallCS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (15)Garcia, Fall 2004 © UCBAdministrivia• If you have points taken off for “notenough comments” by your reader forHW2 or HW3, then email your readerbefore next Monday (freeze day).• Friday will be Intro to SynchronousDigital Systems (not Caches)• Anonymous Survey in lab this weekCS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (16)Garcia, Fall 2004 © UCBExample: C ⇒ Asm ⇒ Obj ⇒ Exe ⇒ Run #include <stdio.h>int main (int argc,


View Full Document

Berkeley COMPSCI 61C - Lecture Notes

Documents in this Course
SIMD II

SIMD II

8 pages

Midterm

Midterm

7 pages

Lecture 7

Lecture 7

31 pages

Caches

Caches

7 pages

Lecture 9

Lecture 9

24 pages

Lecture 1

Lecture 1

28 pages

Lecture 2

Lecture 2

25 pages

VM II

VM II

4 pages

Midterm

Midterm

10 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?