From Source to Executable: Object Modules, Linking & ELF
From Source to Executable: Object Modules, Linking & ELF
This page describes how source code becomes an executable program, what object modules are, and how linking combines them into a runnable file.
From source to binary
A program written in assembly language must be translated before a computer can execute it. The process involves two major steps: 1. Assembling – translating human-readable mnemonics into machine code. 2. Linking – combining one or more assembled parts into a complete executable.
Modularization
Large programs are divided into smaller source files for several reasons:
- Easier teamwork: multiple developers can work on different files.
- Maintainability: smaller modules are easier to test and modify.
- Reuse: functions and routines can be reused in other programs.
When building, each source file is assembled into an object module. Afterwards, all object modules are linked into a single executable.
Object modules
An object module contains:
- Translated program code (machine instructions).
- References to procedures and data that may be external.
- Data objects such as constants and strings.
- Empty placeholders for uninitialized data.
- Debug information.
On Linux, object files and executables use the **ELF** format (Executable and Linkable Format). This format allows the linker to combine all necessary parts into one final binary.
Linking
The linker resolves external references between object modules and libraries. It ensures that all procedure and data addresses are connected correctly.
Example:
- The assembler produces `file1.o` and `file2.o`.
- The linker combines them to create `program`.
If an external reference cannot be found, the linker reports an error. These errors usually mean a missing file, a mistyped symbol name, or a missing library.
Executable files
The linker’s output is an executable program that can be run directly by the operating system. It contains:
- Machine code (instructions).
- Memory layout (segments for code, data, and stack).
- Information for the OS loader (entry point, required libraries).
The development cycle
1. Edit the source file (`.asm`). 2. Assemble the file to produce an object module (`.o`). 3. Link all object modules into an executable. 4. Test and debug the program.
This process is the same regardless of programming language—only the tools differ.