Linking and Loading Linking and Loading Programs may

  • Slides: 16
Download presentation
Linking and Loading

Linking and Loading

Linking and Loading Programs may contain multiple separately translated modules public/entry points and extern

Linking and Loading Programs may contain multiple separately translated modules public/entry points and extern references must be linked No spurious externs and entry points are allowed The linker resolves all external references and produces integrated object code: absolute load module This object code can be subsequently loaded Programs that perform the above functions are called by various names like: 1) Linker 2) Loader 3) Linking loader

The Linking Editor Generation of an absolute load module from a collection of independently

The Linking Editor Generation of an absolute load module from a collection of independently translated source procedures

Module Address Spaces Each module has its own address space. P, Q, R, S,

Module Address Spaces Each module has its own address space. P, Q, R, S, X are registers

Linker Action Step 1: Construct table of all object modules, their lengths, start address

Linker Action Step 1: Construct table of all object modules, their lengths, start address Assign addresses to each object module in Linker Address Space Module Name Length Starting Address A 401 0 B 601 401 C 501 1003 D 301 1505 Step II: Calculate the new addresses of labels, and symbols referring to memory Find all instructions in a module containing memory address. Add a relocation constant to take into account the new start address of the module to each

Linker Action Step III: Find addresses of external references and link them Consult the

Linker Action Step III: Find addresses of external references and link them Consult the external and entry point parts in output of assembler Check that there are no duplicate entry point names Find all instructions referencing entry points and insert the address of each Note: The linked program occupies virtual address space Binding could be done by the loader

Program Address Space 1806 Module D 1505 Module Old New A BRANCH TO 200

Program Address Space 1806 Module D 1505 Module Old New A BRANCH TO 200 BRANCH T 0 200 MOVE P TO X CALL B CALL 401 BRANCH TO 300 BRANCH TO 702 MOVE Q TO X CALL C CALL 1003 BRANCH TO 200 BRANCH TO 1004 MOVE R TO X CALL D CALL 1505 BRANCH TO 200 BRANCH TO 1706 MOVE S TO X 1504 Module C 1003 B 1002 Module B C 401 400 Module A 0 D

The Loader The linker produces a load module (. exe file) in virtual address

The Loader The linker produces a load module (. exe file) in virtual address space Loading Options If linker specifies a physical address then load there Ask the Operating System to supply a block of memory of required size and load in the specified block Adjust virtual addresses with respect to start address of memory block Base register Ask OS for memory; keep start address in the base register If swapping occurs then refresh the base register and readjust addresses Paging Use page start address

The Loader Load Module 1 Load Module n Loader When a load module is

The Loader Load Module 1 Load Module n Loader When a load module is to be executed, the Run/Execute command invokes the Loader OS: get memory

Loader: Binding Revisited The time at which the physical address corresponding to a symbol

Loader: Binding Revisited The time at which the physical address corresponding to a symbol is determined is called the binding time. The later the binding, the more the benefits possible to optimize machine resources good program maintenance properties Binding time possibilities 1 When the program is written • • 2 Programmer overload to work with memory addresses Program needs to be loaded at same location each time When the program is translated. • • Program to be located at determined location only Separately translated modules may show location conflict

Binding Revisited 3. When the program is linked but before it is loaded. •

Binding Revisited 3. When the program is linked but before it is loaded. • Program to be loaded at linker specified memory locations 4. When the program is loaded. • If program is swapped out then must be reloaded at same location 5. When a base register used for addressing is loaded. • Operand address changes during program execution is not possible 6. When the instruction containing the address is executed. • Known as late binding or dynamic binding/linking, exploited in OOPL

Dynamic Relocation The relocated binary program moved up 300 addresses. Many instructions now refer

Dynamic Relocation The relocated binary program moved up 300 addresses. Many instructions now refer to an incorrect memory address.

Dynamic Linking loading Programs have procedures that are only called under unusual circumstances Compilers

Dynamic Linking loading Programs have procedures that are only called under unusual circumstances Compilers have procedures for compiling rarely used statements handling error conditions that seldom occur Only a few routines of a library are needed Dynamic Linking: Link procedures as they are needed When procedure to be dynamically linked is first called Generate a software trap Trap handling routine issues a call to the procedure Procedure is located Memory obtained, procedure loaded and executed Control goes back to calling program

Mechanism for Trap Two separately translated procedures, P 1 and P 2 P 1

Mechanism for Trap Two separately translated procedures, P 1 and P 2 P 1 indicates that it wants dynamic linking Translator creates a linkage segment • Trap address contains address of Dynamic Linker • Next entry is the name of the procedure, P 2 Replaces in P 1 the call to P 2 with Indirect Mode JSR to linkage segment address’, twice Dynamic Linker Actions Read next address of linkage segment Locate P 2 in the file directory of owner of P 1 Load address of P 2 in first location of linkage segment Return to P 1 Action of P 1 The first JSRI causes transfer to DL The second JSR is executed to call P 2 DL address P 2 Address of P 2 Call P 2 becomes JSRI LS

Static Vs Dynamic Linking Static linking: linker copies all external routines used in the

Static Vs Dynamic Linking Static linking: linker copies all external routines used in the program into the load module. Disadvantages • more disk space and memory: same routine copied in the several programs that use them • programs to be recompiled if the external routines change Advantages • Faster: no run time overhead • Portable: does not require the presence of external routines on the system where it is run.

Static Vs Dynamic Linking Dynamic linking: place the name of external routines in the

Static Vs Dynamic Linking Dynamic linking: place the name of external routines in the load module Disadvantage • Run time overhead • Lack of Portability Advantages • Space savings as same copy of external routine is used • No impact if the external routine is changed: good program maintenance