Professional Documents
Culture Documents
WK 4 - Linking
WK 4 - Linking
Executable files
The OS expects executable files to have a specific format
Header info
Code locations Data locations
Example
#include <stdio.h>
}
CS502 Spring 2006 Linking and Loading
Example
#include <stdio.h> extern int errno; int main () { printf (hello, world\n)
Loading: Copying the loadable image into memory, connecting it with any other programs already loaded, and updating addresses as needed
(In Unix) interpreting file to initialize the process address space (in all systems) kernel image is special (own format)
CS502 Spring 2006 Linking and Loading 6
Compiler
Object (.o) Other Objects (.o)
The linker combines multiple object files and library modules into a single executable file
Addresses also relative to zero
Linker
Executable
Loader
In-memory Image
Loader
Memory
CS502 Spring 2006 Linking and Loading 8
Classic Unix
Linker is inside of gcc command Loader is part of exec 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
CS502 Spring 2006 Linking and Loading 9
Dynamic Loading
Routine is not loaded until it is called Better memory-space utilization; unused routine is never loaded. Useful when large amounts of code are needed to handle infrequently occurring cases. Silbershatz says [incorrectly]
No special support from the operating system is required Must be implemented through program design
CS502 Spring 2006 Linking and Loading 10
E.g.,
void myPrintf (**arg) { static int loaded = 0; if (!loaded ) { load (printf); loaded = 1; printf(arg); } }
CS502 Spring 2006 Linking and Loading 11
Shared Libraries
Observation everyone links to standard libraries (libc.a, etc.) Consume space in
every executable image every process memory at runtime
Run-time Linking/Loading
Printf.c gcc Printf.o ar Shared Library Run-time Loader Linker a.Out (or name of your command) Loader HelloWorld.c gcc HelloWorld.o Save disk space. Startup faster. Might not need all.
Memory
CS502 Spring 2006 Linking and Loading 15
Dynamic Linking
Complete linking postponed until execution time. Stub used to locate the appropriate memoryresident 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.
CS502 Spring 2006 Linking and Loading 16
17
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 help think about Unix exec system call
CS502 Spring 2006 Linking and Loading 18
Linking - Summary
Linker key part of OS not in kernel
Combines object files and libraries into a standard format that the OS loader can interpret Resolves references and does static relocation of addresses Creates information for loader to complete binding process Supports dynamic shared libraries
CS502 Spring 2006 Linking and Loading 19