110 likes | 120 Views
Program Linking. 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);}
E N D
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?
Diagrammatic View Prog1(){ } Mytransform() { } x Z file1 file3 Mysqrt(){ } file2 y 3 separate files!!!
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
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???
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
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".
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
The Linkage Editor • Reads in the object files (files1,2,3) created by the compilerall 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
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.
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