Mastering the GCC Toolchain: From Source Code to ELF Executable

This guide walks through the complete GCC compilation pipeline—preprocessing, compilation, assembly, and linking—using a simple C "Hello World" program, detailing each step, relevant commands, and how to inspect the resulting ELF binary.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering the GCC Toolchain: From Source Code to ELF Executable

Programming languages are categorized as machine, assembly, or high-level languages; high-level languages must be translated into machine code via either compilation or interpretation. This article focuses on the compilation path for C/C++ programs using the GNU Compiler Collection (GCC) on Linux.

GCC Toolchain Overview

GCC (GNU Compiler Collection) includes the compiler itself, the Binutils suite (addr2line, ar, objcopy, objdump, as, ld, ldd, readelf, size, etc.), and the C runtime library (CRT). These tools work together to transform source code into an executable ELF binary.

Preparation

A simple #include <stdio.h> program that prints "Hello World" is used as the example. The source file hello.c is compiled step by step.

1. Preprocessing

Expands macros, processes #include directives, removes comments, and adds line markers for debugging. $ gcc -E hello.c -o hello.i The resulting hello.i file can be inspected as plain text.

2. Compilation

Performs lexical, syntax, and semantic analysis, then generates assembly code. $ gcc -S hello.i -o hello.s The hello.s file contains pure assembly instructions.

3. Assembly

Converts assembly into object files ( .o) using Binutils' as or GCC's -c option.

$ gcc -c hello.s -o hello.o
# or
$ as -c hello.s -o hello.o

The object file is in ELF format.

4. Linking

Static linking copies code from static libraries into the executable; dynamic linking records references to shared libraries.

Linker resolves symbols and performs relocation.

# Dynamic linking (default)
$ gcc hello.c -o hello
$ size hello
$ ldd hello

# Static linking
$ gcc -static hello.c -o hello
$ size hello
$ ldd hello   # reports "not a dynamic executable"

The final ELF executable contains sections such as .text, .rodata, .data, .bss, and .debug.

Analyzing the ELF File

Use readelf -S hello to list sections.

Use objdump -D hello for disassembly.

Use objdump -S hello to interleave source code with assembly.

$ readelf -S hello
$ objdump -D hello
$ gcc -g hello.c -o hello
$ objdump -S hello

These commands reveal instruction addresses, opcode bytes, and the mapping back to the original C source.

Key Tools Summary

GCC : Compiler driver.

Binutils : Collection of binary utilities (addr2line, ar, as, ld, objdump, readelf, size, etc.).

C Runtime Library (CRT) : Provides implementations of standard C library functions.

Understanding each stage and the associated commands enables developers to debug, optimize, and control the build process of C/C++ applications on Linux.

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 programminglinkingBinutils
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.