Fundamentals 13 min read

Mastering GCC: From Source Code to ELF Executable Explained

This article walks through the complete GCC toolchain workflow—preprocessing, compilation, assembly, and linking—using a simple C "Hello World" program, explains the roles of GCC, Binutils and the C runtime, and shows how to inspect the resulting ELF binary with common Linux utilities.

Open Source Linux
Open Source Linux
Open Source Linux
Mastering GCC: From Source Code to ELF Executable Explained

GCC Toolchain Overview

GCC (GNU Compiler Collection) is the standard compiler suite on Linux, accompanied by Binutils and the C runtime library (CRT). Together they transform C/C++ source code into an executable ELF binary.

Four Compilation Steps

Preprocessing (Preprocessing)

Compilation (Compilation)

Assembly (Assembly)

Linking (Linking)

Preparing a Sample Program

We use a minimal #include <stdio.h> program that prints "Hello World!":

#include <stdio.h>
// Simple Hello World program
int main(void) {
    printf("Hello World! 
");
    return 0;
}

Preprocessing

The preprocessor expands macros, resolves #include directives, removes comments, and adds line information. Command: gcc -E hello.c -o hello.i The resulting hello.i file can be viewed as plain text.

Compilation

Compilation translates the preprocessed file into assembly code. Command: gcc -S hello.i -o hello.s The generated hello.s contains assembly instructions for the target processor.

Assembly

Assembly converts the assembly source into object files (.o). Commands:

gcc -c hello.s -o hello.o   # using GCC
as -c hello.s -o hello.o   # using Binutils directly

The object file is in ELF format.

Linking

Linking combines object files and libraries into a final executable. It can be static or dynamic. Commands:

gcc hello.c -o hello          # dynamic linking
gcc -static hello.c -o hello  # static linking

Use size to view segment sizes and ldd to list dynamic dependencies.

Analyzing the ELF File

An ELF binary consists of sections such as .text (code), .rodata (read‑only data), .data (initialized data), .bss (uninitialized data), and .debug (debug symbols). The readelf -S command lists these sections, and objdump -D disassembles the code. Using objdump -S mixes source and assembly for easier inspection.

Key Tools

GCC – compiler driver

Binutils – includes as, ld, objdump, readelf, size, etc.

C Runtime Library – provides standard C functions and startup code.

These steps illustrate how high‑level C/C++ code becomes machine‑readable binary on Linux.

GCC workflow diagram
GCC workflow diagram
GCC toolchain components
GCC toolchain components
ELF sections
ELF sections
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CompilationLinuxELFC programmingToolchaingcc
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.