DEV Community

Cover image for From Source Code to Running Process: The 5-Stage Language Processing System Explained
Binary Dose
Binary Dose

Posted on Originally published at binarydose.in

From Source Code to Running Process: The 5-Stage Language Processing System Explained

When software engineers type gcc main.c -o app, it feels like a single seamless command. But behind the scenes, GCC acts as a compiler driver orchestrating an entire language processing toolchain.

1. The Preprocessor (cpp)

The preprocessor operates purely as a text-to-text transformation tool before syntax analysis begins:

  • Header Ingestion: #include <stdio.h> is replaced by the literal content of the header.
  • Macro Expansion: Every instance of #define BUFFER_SIZE 1024 is substituted.
  • Conditional Compilation: #ifdef DEBUG blocks are evaluated and stripped if false.

2. The Compiler (cc1)

The compiler takes pure high-level language and produces target assembly language (.s file). It performs lexical analysis (tokenization), syntax analysis (parsing into AST), semantic validation, intermediate code generation, and machine-independent optimization.

3. The Assembler (as)

Assembly code consists of human-readable mnemonics (mov, add, jmp). The assembler translates these mnemonics directly into machine opcodes, producing a Relocatable Object File (.o or .obj).

4. The Linker (ld)

Object files cannot run independently because external function addresses (like printf from the C standard library) are unresolved placeholders. The linker:

  1. Performs Symbol Resolution across object files and libraries.
  2. Relocates data and text sections to final virtual memory offsets.

5. The Loader

When an executable is launched, the OS kernel's loader reads the binary's ELF or PE headers, allocates virtual memory pages, sets up the process stack and heap, maps shared libraries, and points the CPU's Program Counter register to the entry point _start.

Explore our full interactive compiler curriculum on Binary Dose.

Top comments (0)