You are on page 1of 10

SYSTEM PROGRAMMING

Chapter Three:
Linkers and Loaders
This unit aims at discussing of the Linker and Loader typical system programs.

After you have studied this unit, you will be able to:

• Know what a linker tool is


• Understand tasks of linker
• Explain the types of linking
• Know about Loaders
• Understand Loading Schemes

Translation Hierarchy

Compiler-Translates high-level language program into assembly language.

Assembler-Converts assembly language programs into object files. Object files contain
a combination of: Machine instructions, data and information needed to place
instructions properly in memory.

Flow of Execution

COMPILED BY: EPHREM W. 1


SYSTEM PROGRAMMING

Flow of Execution

Process for Producing an Executable File

Linker

Tool that merges the object files produced by separate compilation or assembly and
creates an executable file. Tasks of a linker: -

• Searches the program to find library routines used by program,

e.g. printf(), sqrt(), strcat() and various other.

COMPILED BY: EPHREM W. 2


SYSTEM PROGRAMMING

• Determines the memory locations that code from each module will occupy and
relocates its instructions by adjusting absolute references.

Relocation, which modifies the object program so that it can be loaded at an address
different from the location originally specified. It combines two or more separate object
programs and supplies the information needed to allow references between them.

Linking Concepts

Computer programs typically comprise several parts or modules; all these


parts/modules need not be contained within a single object file, and in such case refer
to each other by means of symbols. Typically, an object file can contain three kinds of
symbols:

• Publicly defined symbols, which allow it to be called by other modules, also


called as public definition.
• Externally defined symbols (undefined symbols), which calls the other modules
where these symbols are defined, also called as external reference.
• Local symbols, used internally within the object file to facilitate relocation.

Types of Linking

Static Linking

Static linking occurs when a calling program is linked to a called program in a


single executable module. When the program is loaded, the operating system places into
memory a single file that contains the executable code and data. The result of statically
linked programs is an .EXE file or dynamic link library (DLL) subprogram that contains
the executable code for multiple programs. This file includes both the calling program
and the called program.

Advantage
• Static linking creates self-contained, independent programs. In other words, the
executable program consists of one part (the .EXE file) that you need to keep track of.

COMPILED BY: EPHREM W. 3


SYSTEM PROGRAMMING

Disadvantages
• You cannot change the behavior of executable files without relinking them.
• External called programs cannot be shared, requiring that duplicate copies of
programs be loaded in memory if more than one calling program needs to access them.

Dynamic linking

Many operating system environments allow dynamic linking, that is the


postponing of the resolving of some undefined symbols until a program is run. This
means that the executable code still contains undefined symbols, plus a list of objects
or libraries that will provide definitions for these. Loading the program will load these
objects/libraries as well, and perform a final linking.

Advantages

Often-used libraries (for example the standard system libraries) need to be stored
in only one location, not duplicated in every single binary. If an error in a library function
is corrected by replacing the library, all programs using it dynamically will benefit from
the correction after restarting them. Programs that included this function by static
linking would have to be re-linked first.

Disadvantages

Known on the Windows platform as "DLL Hell", an incompatible updated DLL will
break executable that depended on the behavior of the previous DLL. A program,
together with the libraries it uses, might be certified (e.g. as to correctness,
documentation requirements, or performance) as a package, but not if components can
be replaced.

Loader

Loader is a system program that loads machine codes of a program into the
system memory. Loading a program involves reading the contents of executable file into
memory. Once loading is complete, the operating system starts the program by passing
control to the loaded program.

COMPILED BY: EPHREM W. 4


SYSTEM PROGRAMMING

Basic Loading Functions

✓ Loading - allocates memory location and brings the object program into memory
for execution – done by Loader.
✓ Linking - combines two or more separate object programs and supplies the
information needed to allow references between them – done by Linker.
✓ Relocation - modifies the object program so that it can be loaded at an address
different from the location originally specified – by Linking Loader.

Steps Involved in Loading

✓ Executable file’s header is read to determine the size of text and data segments.
✓ Instructions and data are copied into address space.
✓ Arguments passed to the program are copied on the stack.
✓ Machine registers including the stack pointer are initialized.
✓ The control is transferred to a start-up routine that copies the program’s
arguments from the stack to registers and calls the program’s main routine.

Loading Schemes

✓ Compile and Go loader


✓ General Loading Scheme
• Absolute Loader
• Relocating Loader
• Direct Linking loader

Compile/Assemble and Go loader

Compilation, assembly, and link steps are not separated from program execution
all in single pass. The intermediate forms of the program are generally kept in RAM, and
not saved to the file system.

COMPILED BY: EPHREM W. 5


SYSTEM PROGRAMMING

Advantages

• The user need not be concerned with the separate steps of


compilation, assembling, linking, loading, and executing.
• They are simple to implement.

Disadvantages

• There is wastage of memory space due to the presence of the


assembler.
• The code must be reprocessed every time it is run.
• Systems with multiple modules, possibly in different languages,
cannot be handled naturally within this framework.

Compile-and-go systems were popular in academic environments, where student


programs were small, compiled many times, usually executed quickly and, once
debugged, seldom needed to be re-executed.

COMPILED BY: EPHREM W. 6


SYSTEM PROGRAMMING

General Loading Scheme

In this scheme the output of assembler is saved into secondary storage and
loaded whenever code is to be executed. Assembled program could be loaded, in the
same area into memory where assembler was stored (because now assembling is
completed assembler is removed from main memory). Function of loader is to accept
instruction, data and other information in object format and put it into memory in
executable format.

Advantages

• No need to put assembler in main memory all the time it can be removed after
assembly is completed. Assembled program is stored in secondary storage in
object form.
• Loader is required to put object code in memory in executable format, which is
smaller in size. Every time we run program we do not have to assemble it.
• Even if subroutines are in different languages, their object code & linkage
conventions will be in one language.

Disadvantage

• Addition of a new program ‘Loader’ into system.

COMPILED BY: EPHREM W. 7


SYSTEM PROGRAMMING

Types of General Loader

Absolute Loader

The simplest type of loader scheme which fits to the general model is called an
absolute loader. Assembler produces object code, which is stored on secondary storage
(instead of being directly loaded into main memory like in compile -&-go). Loader in turn
accepts this object code, places it into core at prescribed place by assembler for
execution.

Functions in absolute loading

✓ Allocation – done by programmer


✓ Linking – done by programmer
✓ Relocation –done by assembler
✓ Loading – done by loader

Advantages: -
This scheme makes more memory available to the user, an assembler is not in
memory at load time.
• Simple to implement.
• No linking or relocation
Disadvantages: -
• Programmer must specify to the assembler, the address of memory location
where program is to be loaded.

COMPILED BY: EPHREM W. 8


SYSTEM PROGRAMMING

• When program contains more than one subroutine, then programmer must
remember the address of each and have to use it explicitly in other subroutines
to perform subroutine linkages.
• Programmer must be careful not to assign same or overlapping locations to two
subroutines.

Bootstrap Loader

Special Type of Absolute Loader. When a computer is first tuned on or restarted


bootstrap loader is executed. This bootstrap loads the first program to be run by
computer that is the OS. It loads the first address 0x80.

Subroutine linkage

The problem is: MAIN program A wishes to transfer to subprogram B. However,


the assembler does not know this symbol and declare it as an undefined symbol (error)
unless a special mechanism has been provided. This mechanism has been implemented
with a direct-linking or a relocating loader. The assembler pseudo-op EXTRN followed
by a list of symbols indicates that these symbols are defined in another programs.
Correspondingly if a symbol is defined in one program and referenced in others, we
insert it into a symbol list following the pseudo-op ENTRY. The assembler will also
inform the loader that these symbols may be referenced by other programs.

Consider the following example:

MAIN START
EXTRN SUBROUT
----------
L 15, = A(SUBROUT)
BALR 14,15 // reg 14 will return addr of caller.
--------
END
The subroutine can be defined at other location as::
SUBROUT START
USING *,15
----------
BR 14
EN

COMPILED BY: EPHREM W. 9


SYSTEM PROGRAMMING

Relocation

Execution of the object program using any part of the available and sufficient
memory. The object program is loaded into memory wherever there is room for it. The
actual starting address of the object program is not known until load time.

Relocating Loader

To avoid possible reassembling of all subroutines when a single subroutine is


changed, and to perform the task of allocation and linking for programmer, relocating
loaders are developed. In this scheme, assembler assembles each procedure segment
independently and passes on the text and information after relocation & inter segment
references to the loader.

Summary

✓ Linker is a tool that merges the object files produced by separate compilation or
assembly and creates an executable file.
✓ Types of Linking: static and dynamic linking
✓ Loader is a system program that loads machine codes of a program into the
system memory.
✓ Loading Schemes: compile/assemble and go loader & general loading scheme

END OF CHAPTER THREE!

COMPILED BY: EPHREM W. 10

You might also like