Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2004, D. J. Foreman 1 Program Linking. © 2004, D. J. Foreman 2 Program Content  Given the following file: Float Mysqrt(float); void Prog1 () { extern.

Similar presentations


Presentation on theme: "© 2004, D. J. Foreman 1 Program Linking. © 2004, D. J. Foreman 2 Program Content  Given the following file: Float Mysqrt(float); void Prog1 () { extern."— Presentation transcript:

1 © 2004, D. J. Foreman 1 Program Linking

2 © 2004, D. J. Foreman 2 Program Content  Given the following file: Float Mysqrt(float); void Prog1 () { extern float y; float X; int Z; X=Mysqrt (y); // written by me Z=myTransform (X);// written by someone else } int myTransform (float x) {return floor(x);}  Where are: x, y, Z & Mysqrt?

3 © 2004, D. J. Foreman 3 Diagrammatic View Prog1() { } Mytransform() { } x Z Mysqrt() { } y 3 separate files!!! file1 file2 file3

4 © 2004, D. J. Foreman 4 Required executable  Must include all three files  All three files must be "aware" of each other  Arrangement of all three (inside the single executable file) must be programmer definable

5 © 2004, D. J. Foreman 5 What is the problem?  Prog1 must have access to actual locations of all the pieces  But y's actual location at runtime is unknown at compile time  And myTransform is only available as a previously compiled module (I have no source code for it!!!)  How will Prog1 get at the pieces???

6 © 2004, D. J. Foreman 6 Look at the compiled code  X=Mysqrt (y); generates the following: load reg1 with the address of y load reg2 with address of Mysqrt branch to address in reg2, saving NSI in reg3 (NSI=Next Sequential Instruction)  Z=myTransform(X); generates the following: load reg1 with the address of X load reg2 with address of myTransform branch to address in reg2, saving NSI in reg3

7 © 2004, D. J. Foreman 7 How the compiler helps  Sets aside space in the object file for address of "Mysqrt"  Fills in the start address of "Mysqrt" when it is compiled! ■ Remember: every instruction takes up address space. Compiler just adds them up to find out where things are.  Sets aside space (in file1) for the address of "myTransform" and flags it as being external and unknown. Same for "y".

8 © 2004, D. J. Foreman 8 What about myTransform?  "myTransform" was separately compiled ■ Compiler created a "here I am flag" for a constant containing the offset of myTransform within the object file (file3) ■ Same happened for file containing "y"  So now all we have to do is "connect the dots" ■ That is, fill in the addresses where needed ■ And create one big executable file

9 © 2004, D. J. Foreman 9 The Linkage Editor  Reads in the object files (files1,2,3) created by the compiler all object files on a system have a standard format for that system ■ All inputs are now one big file  Scans the file for "external, unknown" flags and save the place where it's needed  Scans the file for "here I am" flags and writes the address into the places where they are needed

10 © 2004, D. J. Foreman 10 Results  Now have a fully executable file with all addresses resolved relative to the start of the file  But the file does not load at location "0"  So we still have one more step.

11 © 2004, D. J. Foreman 11 The Loader  Addresses in executable are relative to 0  Program loads at a new location  Loader re-reads the file looking for the flags, then fills in the ACTUAL runtime addresses  Just need to add the start address to the address in the file (simple addition)  File in memory is now ready to run


Download ppt "© 2004, D. J. Foreman 1 Program Linking. © 2004, D. J. Foreman 2 Program Content  Given the following file: Float Mysqrt(float); void Prog1 () { extern."

Similar presentations


Ads by Google