Friday, December 19, 2025

Linker in OS

A linker (or link editor) is a system utility that combines multiple separately compiled object files and library modules into a single, cohesive executable file. It serves as a bridge between the translation phase (compilation/assembly) and the execution phase. 

Key Functions of a Linker

  • Symbol Resolution: The linker scans object files to match function and variable references (symbols) with their actual definitions. For example, if a program calls printf(), the linker finds the implementation of printf in the standard C library.
  • Relocation: Since compilers typically assume a program starts at address zero, the linker "relocates" these code and data segments to their final assigned memory addresses.
  • Code Consolidation: It merges various sections (like .text for code and .data for variables) from different modules into one continuous block. 


Types of Linking
Feature Static LinkingDynamic Linking
TimingPerformed during compilationPerformed at load or run time
File SizeLarger (includes all library code)Smaller (only contains references)
MemoryRedundant; each program has its own copyEfficient; multiple programs share one copy
UpdatesRequires recompilation if libraries changeLibraries can be updated independently
ExampleWindows .exe or Linux .a filesWindows .dll or Linux .so files
Practical Examples
  • C Programming: When you run the command gcc main.c utils.c -o myprog, the compiler first generates main.o and utils.o. The linker then "glues" these together with the standard C library to create the final myprog executable.
  • Shared Libraries: Most modern programs on Windows use dynamic linking for the DirectX or C++ Runtime libraries. This allows many different games or apps to use the same system files rather than each app carrying its own 100MB copy.

No comments:

Post a Comment