wk 4 -- linking
TRANSCRIPT
-
7/31/2019 Wk 4 -- Linking
1/19
Linking and Loading 1CS502 Spring 2006
Linking & Loading
CS-502 Operating Systems
{This topic is not covered in Tannenbaum.Silbershatz et al devote about 2 pages to it in 8.1}
-
7/31/2019 Wk 4 -- Linking
2/19
Linking and Loading 2CS502 Spring 2006
What happens to your program
after it is compiled, but before it
can be run?
-
7/31/2019 Wk 4 -- Linking
3/19
Linking and Loading 3CS502 Spring 2006
Executable files
The OS expects executable files to have aspecific format
Header info
Code locations Data locations
Code & data
Symbol Table
List of names of things defined in your program andwhere they are defined
List of names of things defined elsewhere that areused by your program, and where they are used.
-
7/31/2019 Wk 4 -- Linking
4/19
Linking and Loading 4CS502 Spring 2006
Example
#include
int main () {
printf (hello,
world\n)
}
Symbol defined in
your program and
used elsewhere
main
Symbol defined
elsewhere and used by
your program
printf
-
7/31/2019 Wk 4 -- Linking
5/19
Linking and Loading 5CS502 Spring 2006
Example
#include
extern int errno;
int main () {
printf (hello,
world\n)
}
Symbol defined in
your program and
used elsewhere
main
Symbol defined
elsewhere and used by
your program
printf
errno
-
7/31/2019 Wk 4 -- Linking
6/19
Linking and Loading 6CS502 Spring 2006
Two-step operation (in most systems)
Linking: Combining a set of programs, includinglibrary routines, to create a loadable image
a) Resolving symbols defined within the set
b) Listing symbols needing to be resolved by loader
Loading: Copying the loadable image intomemory, connecting it with any other programs
already loaded, and updating addresses as needed (In Unix) interpreting file to initialize the processaddress space
(in all systems) kernel image is special (own format)
-
7/31/2019 Wk 4 -- Linking
7/19
Linking and Loading 7CS502 Spring 2006
From source code to a process
Binding is the process of mappingnames to addresses
Most compilers produce relocatableobject code
Addresses relative tozero
The linker combines multiple object
files and library modules into a singleexecutable file
Addresses also relative tozero
The Loader reads the executable file
Allocates memory
Maps addresses within file to physicalmemory addresses
Resolves names of dynamic libraryitems
Source
(.c, .cc)
Object
(.o)
Executable
In-memory Image
Compiler
Linker
Loader
Other Objects
(.o)
Dynamic libraries(.dll)
Static libraries
(.a)
-
7/31/2019 Wk 4 -- Linking
8/19
Linking and Loading 8CS502 Spring 2006
Static Linking and Loading
Printf.c
Printf.o
StaticLibrary
gcc
ar
Linke
r
Memory
HelloWorld.c
gcc
HelloWorld.o
Loader
a.Out
(or name of
your command)
-
7/31/2019 Wk 4 -- Linking
9/19
Linking and Loading 9CS502 Spring 2006
Classic Unix
Linker is inside ofgcc command
Loader is part ofexec system call
Executable image contains all object and library
modules needed by program Entire image is loaded at once
Every image contains copy of common library
routines Every loaded program contain duplicate copy of
library routines
-
7/31/2019 Wk 4 -- Linking
10/19
Linking and Loading 10
CS502 Spring 2006
Dynamic Loading
Routine is not loaded until it is called
Better memory-space utilization; unusedroutine is never loaded.
Useful when large amounts of code areneeded to handle infrequently occurringcases.
Silbershatz says [incorrectly] No special support from the operating system is
required
Must be implemented through program design
-
7/31/2019 Wk 4 -- Linking
11/19
-
7/31/2019 Wk 4 -- Linking
12/19
Linking and Loading 12
CS502 Spring 2006
Linker-assisted Dynamic Loading
Programmer marks modules as dynamicto linker
For function call to a dynamic function
Call is indirect through a link table Each link table initialized with address small stub of
code to locate and load module.
When loaded, loader replaces link table entry with
address of loaded function When unloaded, loader replaces table entry withstub address
Static data cannot be made dynamic
-
7/31/2019 Wk 4 -- Linking
13/19
Linking and Loading 13
CS502 Spring 2006
Shared Libraries
Observationeveryone links to standardlibraries (libc.a, etc.)
Consume space in
every executable image every process memory at runtime
Would it be possible to share the commonlibraries?
Automatically load at runtime?
-
7/31/2019 Wk 4 -- Linking
14/19
Linking and Loading 14
CS502 Spring 2006
Shared libraries (continued)
Libraries designated as shared .so, .dll, etc.
Supported by corresponding .a libraries containingsymbol information
Linker sets up symbols to be resolved atruntime
Loader: Is library already in memory?
If so, map into new process space map, an operation to be defined
If not, load and then map
-
7/31/2019 Wk 4 -- Linking
15/19
Linking and Loading 15
CS502 Spring 2006
Run-time Linking/LoadingPrintf.c
Printf.o
Shared
Library
gcc
arLinker
Memory
gcc
Loader
Save disk space.Startup faster.
Might not need all.
Run-time
Loader
HelloWorld.c
HelloWorld.o
a.Out
(or name of
your command)
-
7/31/2019 Wk 4 -- Linking
16/19
Linking and Loading 16
CS502 Spring 2006
Dynamic Linking
Complete linking postponed until execution time.
Stub used to locate the appropriate memory-
resident library routine.
Stub replaces itself with the address of the routine,and executes the routine.
Operating system needs to check if routine is in
processes memory address space.
Dynamic linking is particularly useful for
libraries.
-
7/31/2019 Wk 4 -- Linking
17/19
Linking and Loading 17
CS502 Spring 2006
Dynamic Shared Libraries
Static shared libraries requires address
space pre-allocation
Dynamic shared librariesaddress binding
at runtime
Code must be position independent
At runtime, references are resolved as
Library_relative_address + library_base_address
-
7/31/2019 Wk 4 -- Linking
18/19
Linking and Loading 18
CS502 Spring 2006
Overlays(primarily of historical interest)
Keep in memory only those instructions and data
that are needed at any given time.
Needed when process is larger than amount of
memory allocated to it. Can be implemented by user
no special support needed from operating system, but
programming design of overlay structure is complex
Can be done with OS helpthink about Unix exec
system call
-
7/31/2019 Wk 4 -- Linking
19/19
Linking and Loading 19
CS502 Spring 2006
Linking - Summary
Linkerkey part of OSnot in kernel
Combines object files and libraries into a
standard format that the OS loader can
interpretResolves references and does static relocation
of addresses
Creates information for loader to completebinding process
Supports dynamic shared libraries