Loader Design Options

Linkage editor
The difference between a linkage editor and a linking loader:

• •

A linking loader performs all linking and relocation operations, including automatic library search, and loads the linked program into memory for execution. A linkage editor produces a linked version of the program, called a load module or an executable image, which is normally written to a file for later execution.

2

1

On Windows or UNIX On Windows or UNIX operating systems, we use aa operating systems, we use linkage editor (e.g., link and ld) linkage editor (e.g., link and ld) to generate an executable to generate an executable program. program.

Linkage editor
When the user is ready to run the linked program, a simple relocating loader can be used to load the program into memory. The only object code modification necessary is the addition of an actual address to relative values within the program. The linkage editor performs relocation of all control sections relative to the start of the linked program.

All items that need to be modified at load time have values that are relative to the start of the linked program.

• This means that the loading can be accomplished in one
pass with no external symbol table required.
4

2

Linkage editor
If a program is to be executed many times without being reassembled, the use of a linkage editor can substantially reduces the overhead required.

• Resolution of external references and library
searching are only performed once.

5

Linkage editor
Even though all linking has been performed, information concerning external references is often retained in the linked program.

• This allows subsequent relinking of the •
program to replace control sections, modify external references, etc. If this information is not retained, the linked program cannot be reprocessed by the linkage editor; it can only be loaded and executed.
6

3

Linkage editor
For example, replace a new version of a subroutine without using the original versions of all of the other subroutines.

• INCLUDE PLANNER(PROGLIB) • DELETE PROJECT • INCLUDE PROJECT(NEWLIB) • REPLACE PLANNER(PROGLIB)
7

Linkage editor
Linkage editors can be used to build packages of subroutines or other control sections that are generally used together.

If the program often uses a set of subroutines (e.g. formatted I/O in FORTRAN) that have a large number of cross-references among them, exactly the same set of cross-references would need to be processed for almost every program linked.

This represents a substantial amount of overhead.

The linkage editor could be used to combine the appropriate subroutines into a package with a command sequence like following:
INLCUDE READR(FTNLIB) INCLUDE WRITER(FNTLIB) INCLUDE BLOCK(FNTLIB) INCLUDE ENCODE(FNTLIB) INCLUDE DECODE(FNTLIB) ... SAVE FNTIO(SUBLIB)

8

4

Linkage editor
Linkage editors often allow the user to specify that external references are not to be resolved by automatic library search.

• • •

For example, 100 FORTRAN programs use FNTIO. It means 100 copies of FNTIO would be stored. By using the command to specify no library search be performed during linkage editor, only the external references between user-written routines would be resolved. A linking loader could then be used to combine the linked user routines with FTNIO.

9

Dynamic linking
Linkage editors perform linking before the program is loaded for execution. Linking loaders perform these same operations at load time. Dynamic linking postpones the linking function until execution time.

• A subroutine is loaded and linked to the test
of the program when it is first called.

10

5

Dynamic linking application
Dynamic linking is often used to allow several executing programs to share one copy of a subroutine or library. For example, a single copy of the standard C library can be loaded into memory. All C programs currently in execution can be linked to this one copy, instead of linking a separate copy into each object program.

11

Dynamic linking application
In an object-oriented system, dynamic linking is often used for references to software object.

• This allows the implementation of the object
and its method to be determined at the time the program is run. (e.g., C++)

The implementation can be changed at any time, without affecting the program that makes use of the object.
12

6

Dynamic linking advantage
Some subroutines such as those that diagnose errors may never need to be called at all can be loaded only when they are needed.

Without using dynamic linking, these subroutines must be loaded and linked every time the program is run.

Using dynamic linking can save both space for storing the object program on disk and in memory, and time for loading the bigger object program.
13

Dynamic linking implementation
One possible way to implement dynamic linking.

A subroutine that is to be dynamically loaded must be called via an operating system service request.

• This method can also be thought of as a request to a
part of the loader that is kept in memory during execution of the program

• •

Instead of executing a JSUB instruction to an external symbol, the program makes a load-and-call service request to the OS. The parameter of this request is the symbolic name of the routine to be called.
14

7

Dynamic linking implementation
The OS examines its internal tables to determine whether the subroutine is already loaded. If needed, the subroutine is loaded from the library. Then control is passed from the OS to the subroutine being called. When the called subroutine completes its processing, it returns to its caller (operating system). The OS then returns control to the program that issues the request. After the subroutine is completed, the memory that was allocated to it may be released.
15

Dynamic linking implementation
However, often this is not done immediately. If the subroutine is retained in memory, it can be used by later calls to the same subroutine without loading the same subroutine multiple times. Control can simply pass from the dynamic loader to the called routine directly.
16

8

Example

Issue a load-and-call service request

Load the called subroutine into memory
17

Control is passed to the loaded subroutine.

Control is returned The called subroutine to the loader and this time is already loaded. later returned to the user program

9