All writings · tech · Computer science

Tale of Executable programs

This article explains how a piece of code transforms from being written in your code editor to being executed by the CPU, focusing on the journey up to memory loading.

From Code to Executable

You start by writing source code in a human-readable programming language of your choice. What happens next depends on whether you’re using a compiled or interpreted language:

Since this series is focused on Go, a compiled language, we’ll focus on the compilation process.

Executable File Sections

An executable file, such as an ELF file, is organized into distinct sections:

For a deeper dive into ELF files, check out this article.

Launching the Program

Once the executable is ready, the operating system (OS) steps in to run it. Here’s the process:

1. Request

You initiate execution by double-clicking the file or running it via the command line.

2. Process Creation

The OS creates a process, assigning it a unique Process ID (PID). A process is an instance of the program in execution, managed by the OS to allocate resources like memory and CPU time.

3. Virtual Address Space

The OS assigns the process a private virtual address space, a large, linear range of memory addresses starting at 0. These addresses are virtual—an abstraction mapped to physical memory by the OS.

Why Virtual Address Space?

4. Loading

The OS loader reads the ELF file’s sections (e.g., .text, .data) and places them into the process’s virtual address space, preparing them for CPU access and execution.

5. Dynamic Linking

If you are working with a programming language like C, linking process becomes very crucial. Whenever you write a software, you often rely on external libraries and packages. For example, you use #include<stdio.h> pre-processor, that makes sure your compiled software also includes the entire code present in the stdio.h header file.

Then, while writing your code, you can use functions like printf which are provided by the stdio.h header file. It is actually at the linking step, that all the external libraries and dependencies are loaded into the virtual memory, to make sure the actual implementation of the printf function is available when it is used anywhere.

This concludes the life-cycle of a program all the way from being written to being loaded into the memory.

Next articles in the series go more in-depth. They are about how the execution works. This would involve understanding things like


Originally published at prateeksingh.life/writings/tech/cs/tale-of-executable-program. Save to Instapaper.