1 / 11

Program Linking

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);}

clowery
Download Presentation

Program Linking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Program Linking

  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. Diagrammatic View Prog1(){ } Mytransform() { } x Z file1 file3 Mysqrt(){ } file2 y 3 separate files!!!

  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. 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. 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. 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. 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. 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

  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. 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

More Related