05linking and Loading

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Linking and Loading

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 translated


source procedures
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

Assign addresses to each object module in Linker Address Space

Module Length Starting Address


Name
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 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 Old New
Module D
A BRANCH TO 200 BRANCH T0 200
1505
MOVE P TO X MOVE P TO X
1504
CALL B CALL 401
Module C
B BRANCH TO 300 BRANCH TO 702
1003
MOVE Q TO X MOVE Q TO X
1002
CALL C CALL 1003

Module B C BRANCH TO 200 BRANCH TO 1004


MOVE R TO X MOVE R TO X
401
400 CALL D CALL 1505

Module A D BRANCH TO 200 BRANCH TO 1706


MOVE S TO X MOVE S TO X
0
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 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 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

• Programmer overload to work with memory addresses


• Program needs to be loaded at same location each time

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

• 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 to an
incorrect memory address.
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, P1 and P2
P1 indicates that it wants dynamic linking DL address
Translator creates a linkage segment P2
• Trap address contains address of Dynamic Linker
• Next entry is the name of the procedure, P2
Replaces in P1 the call to P2 with Indirect Mode JSR
to linkage segment address’, twice

Dynamic Linker Actions


Read next address of linkage segment
Locate P2 in the file directory of owner of P1 Address of P2
Load address of P2 in first location of linkage segment P2
Return to P1

Action of P1
The first JSRI causes transfer to DL
Call P2 becomes
The second JSR is executed to call P2 JSRI LS
JSRI LS
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 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

You might also like