Linker
• Combine multiple object code modules into one executable
• Resolve external references (calls to functions or variables defined in other modules)
• Link in precompiled library routines (e.g., maths, I/O, string handling)
• Assign final memory addresses to code and data so modules work together
• Produce a single load module ready to be run
Static Linking
• All required library code is copied into the executable at link time
• Executable is larger but self-contained
• No dependency on external libraries at runtime
• Updating a library requires re-linking the program
Dynamic Linking
• Library code is not copied in; only references are stored
• Libraries (e.g., DLLs, .so files) are linked at load time or runtime
• Smaller executables and shared libraries save memory across programs
• Libraries can be updated independently of the program
Loader
• Locate the executable file on secondary storage
• Allocate memory for the program (code, data, stack, heap)
• Copy the program's instructions and data into the allocated memory
• Adjust addresses if the program is loaded at a different location than expected (relocation)
• Pass control to the program's entry point so execution can begin
Relationship Between Translators, Linkers and Loaders
1. Translator (compiler/assembler) converts each source file into an object file
2. Linker combines object files and library routines into one executable
3. Loader copies the executable into memory and starts execution
• Translator - turns source code into object code
• Linker - joins object code modules and libraries into one executable
• Loader - places the executable into memory so the CPU can run it
Libraries
| Advantages | Disadvantages |
|---|---|
| Saves development time, because common code does not have to be written again | The programmer relies on code they did not write and may not be able to inspect |
| The routines have already been tested, so less testing and debugging is needed | A library may be changed or withdrawn by its author |
| The routines are often optimised and written by specialists, so less programming knowledge is needed | A program may carry library code that it never uses |
| The same routines are used by many programs, which gives consistency between applications |
• The linker combines the program's object code with the library routines it calls, or adds the addresses that let the program call shared library files when it runs
• The loader copies the executable into memory together with any library files it requires