Professional Documents
Culture Documents
3 Machine-Independent Loader
Features
o Loading and linking are often thought of as
operating system service functions.
o Linkage Editors
n Produces a linked version of the program (often called a
load module or an executable image), which is written to
a file or library for later execution
Linkage Editors (Cont.)
o A linkage editor
n Perform relocation of all control sections relative
to the start of the linked program,
n Resolve all external reference
n Output a relocatable module for later execution
o A simple relocating loader can be used to
load the program into memory
n One-pass without external symbol table required
Linking Loader vs. Linkage Editor
Linking Loader vs. Linkage Editor
o Linking loader
n Searches libraries and resolves external
references every time the program is executed.
n Avoid the writing and reading the linked program.
o Linkage editor
n Resolution of external reference and library
searching are only performed once
Linking Loader vs. Linkage Editor
(Cont.)
o Linking loader
n Suitable when a program is reassembled for
nearly every execution
o In a program development and testing environment
o When a program is used so infrequently that it is not
worthwhile to store the assembled and linked version.
o Linkage editor
n If a program is to be executed many times
without being reassembled, the use of a linkage
editor substantially reduces the overhead required.
Additional Functions of Linkage Editors
o Replacement of subroutines in the linked
program
INCLUDE PLANNER(PROGLIB)
DELETE PROJECT {delete from existing PLANNER}
INCLUDE PROJECT(NEWLIB) {include new version}
REPLACE PLANNER(PROGLIB)
=> New version of PLANNER
Additional Functions of Linkage Editors
(Cont.)
o Build packages of subroutines or other control
sections that are generally used together
n For example: build a new linked module FINIO instead of
search all subroutines in FINLIB
INCLUDE READR(FTNLIB)
INCLUDE WRITER(FTNLIB)
INCLUDE BLOCK(FTNLIB)
INCLUDE DEBLOCK(FTNLIB)
INCLUDE ENCODE(FTNLIB)
INCLUDE DECODE(FTNLIB)
.
SAVE FTNIO(SUBLIB)
Additional Functions of Linkage Editors
(Cont.)
o Specify that external references are not to be
resolved by automatic library search
n Thus, only the external references between user-written
routines would be resolved
o i.e., does not include library
n Can avoid multiple storage of common libraries in
programs.
o If 100 programs using the routines on the same library
n A total copy of 100 libraries would to stored, waste space
n Need a linking loader to combine the common libraries at
execution time.
Linking Time
o Linkage editors : before load time
o Linking loaders : at load time
o Dynamic linking : after load time
n A scheme that postpones the linking function
until execution time.
n A subroutine is loaded and linked to the rest of
the program when it is first called
n Other names: dynamic loading, load on call
3.4.2 Dynamic Linking
o Advantages
n Load the routines when they are needed, the time
and memory space will be saved.
n Avoid the necessity of loading the entire library
for each execution
o i.e. load the routines only when they are needed
n Allow several executing programs to share one
copy of a subroutine or library (Dynamic Link
Library, DLL)
Implementation of Dynamic Linking
o Need the help of OS
n Dynamic loader is one part of the OS
n OS should provide load-and-call system call
o Instead of executing a JSUB instruction, the program
makes a load-and-call service request to the OS
n The parameter of this request is the symbolic name of the
routine to be called
o Processing procedures of load-and-call:
n Pass control to OS’s dynamic loader
n OS checks the routine in memory or not. If in memory,
pass control to the routine. If not, load the routine and
pass control to the routine.
Implementation of Dynamic Linking
(Cont.)
o Pass the control: (Fig. 3.14)
n Fig. 3.14 (a) User program -> OS (dynamic loader)
n Fig. 3.14 (b) OS: load the subroutine
n Fig. 3.14 (c) OS -> Subroutine
n Fig. 3.14 (d) Subroutine -> OS -> User program
n Fig. 3.14 (e) A second call to the same routine may not
require load operation
o After the subroutine is completed, the memory that
was allocated to load it may be released
n A second call to the same routine may not require load
operation
Loading and Calling a Subroutine
Using Dynamic Linking
Issue a load-and-
call request Load the routine from
the specified library
Loading and Calling a Subroutine
Using Dynamic Linking (Cont.)
Jump back to the
dynamic loader
ret1=a();
ret2=b(); int b() b.c
{
printf("\n ret from a()= %d", ret1); return 5;
printf("\n ret from b()= %d", ret2); }
}
Example of Programs Using Libraries
(Cont.)
o Compile source programs to object programs
n gcc –c main.c
n gcc –c a.c
n gcc –c b.c
o Create/add/replace object programs into library
under SunOS
n ar –r libtmp.a a.o b.o
o List contents of library under SunOS
n ar –t libtmp.a
o Delete object programs from library under SunOS
n ar –d libtmp.a b.o
Example of Programs Using Libraries
(Cont.)
o Using library in programs
n Ex. gcc main.o libtmp.a –o prog
n Ex. gcc main.o –L. –ltmp –o prog
o Linking editor
n ld under SunOS
o References
n man gcc under SunOS
n man ar under SunOS
n man ld under SunOS