Professional Documents
Culture Documents
Chapter Three
Chapter Three
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:
Translation Hierarchy
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
Flow of Execution
Linker
Tool that merges the object files produced by separate compilation or assembly and
creates an executable file. Tasks of a linker: -
• 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
Types of Linking
Static Linking
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.
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
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.
✓ 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.
✓ 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
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.
Advantages
Disadvantages
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
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.
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.
• 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
Subroutine linkage
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
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
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