Skip to content

Linkers, Loaders and Libraries

1.2.2 Applications generation · Paper 1 · 3 flashcards · 3 questions

Definition
After source code has been translated into object code, additional systems software is needed to combine separate modules and load the final program into memory ready for execution. These tasks are handled by linkers and loaders.

Linker

Definition
A linker is a utility program that combines one or more compiled object files (modules) and any required library routines into a single executable program, resolving references between them.
Key Point
Linker Responsibilities:
• 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
Key Point
Static vs Dynamic Linking:

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

Definition
A loader is a part of the operating system responsible for copying an executable program from secondary storage into main memory and preparing it for execution.
Key Point
Loader Responsibilities:
• 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

Key Point
Typical Build and Run Pipeline:
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
Exam Tip
Easy way to remember:
• 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

Definition
A library is a collection of pre-compiled, tested routines written by other programmers that a program can import and call.
Pros & Cons
Pros & Cons (Using Libraries):
AdvantagesDisadvantages
Saves development time, because common code does not have to be written againThe 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 neededA library may be changed or withdrawn by its author
The routines are often optimised and written by specialists, so less programming knowledge is neededA program may carry library code that it never uses
The same routines are used by many programs, which gives consistency between applications
Key Point
How library code reaches the program:
• 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