The executable ELF format
What you'll learn: You will understand how the compiler packages your finished code into a standardized "container" file that the operating system knows how to run.
Once your C code has been compiled into machine code and all the separate parts are linked together, you are left with a single file. On Linux and most Unix-like systems, this file uses the ELF format, which stands for Executable and Linkable Format.
The IKEA Furniture Analogy
Think of a raw binary file like a pile of loose wood and screws. Without instructions, you don’t know if it’s a chair or a bookshelf. The ELF format acts like the IKEA box and the instruction manual combined. It provides the structure that tells the Operating System (OS) exactly how to "assemble" the program in your computer's memory so it can start running.
Inside the ELF Container
An ELF file isn't just a random stream of 1s and 0s. It is organized into specific sections:
- The Header: This is the "shipping label." It tells the OS which processor the file was built for (like x86 or ARM) and the Entry Point, which is the memory address where the code actually starts.
- The .text Section: This contains the actual machine code—the instructions your CPU will execute.
- The .data Section: This holds your global and static variables that have been assigned a value.
- Symbol Table: A map of all the functions and variables used, which is essential if you ever need to debug the program later.
What it looks like
You don't write ELF code; the Linker generates it for you. If you have a simple program:
#include <stdio.h>
int main() {
printf("Hello, ELF!\n");
return 0;
}
When you run gcc main.c -o my_program, the linker wraps that printf call and the "Hello" string into the ELF structure. If you were to peak inside the resulting my_program file with a tool like readelf, you would see the OS-level headers that turn your simple C logic into a formal, runnable application.
Without this standardized format, every programmer would have to write their own custom code just to tell the computer how to load their program into RAM. The ELF format handles that "handshake" between your code and the hardware.
Key takeaway
The ELF format is a standardized container that organizes machine code and data so the operating system can load and execute your program correctly.