Mastering GCC: From C Source to ELF Executable in Four Steps
This guide walks through the complete GCC compilation pipeline—preprocessing, compilation, assembly, and linking—demonstrates how to build a simple C program, inspect the resulting ELF file, and use Binutils tools to analyze sections and disassemble the binary.
Introduction
Computer programming languages are divided into machine language, assembly language, and high‑level languages. High‑level languages must be translated into machine code, either by a compiler (e.g., C, C++, Java) or an interpreter (e.g., Python, Ruby, MATLAB, JavaScript). This article explains how a C/C++ program is turned into processor‑executable binary code using the GCC toolchain.
GCC Toolchain Overview
GCC (GNU Compiler Collection) is the standard compilation suite on Linux. The toolchain consists of gcc , the binutils collection (including addr2line, ar, objcopy, objdump, as, ld, readelf, size, etc.), and the C runtime library.
Four‑Step Build Process
Preprocessing – expands macros, processes #include and conditional directives, removes comments, adds line numbers, and retains #pragma directives. Example command: $ gcc -E hello.c -o hello.i Compilation – performs lexical, syntactic, and semantic analysis on the preprocessed file and generates assembly code. Example command: $ gcc -S hello.i -o hello.s Assembly – translates assembly into object files ( .o) using as or gcc -c. Example commands:
$ gcc -c hello.s -o hello.o $ as -c hello.s -o hello.oLinking – combines object files and libraries into an ELF executable. Supports static linking ( -static) and dynamic linking. Example commands and size comparison:
$ gcc hello.c -o hello $ size hello $ ldd hello $ gcc -static hello.c -o hello $ size hello $ ldd helloAnalyzing the ELF File
After linking, the result is an ELF (Executable and Linkable Format) file composed of sections such as .text, .rodata, .data, .bss, and .debug. The following command lists section headers: $ readelf -S hello Key sections: .text – executable code. .rodata – read‑only data (constants). .data – initialized global/static variables. .bss – uninitialized global/static variables. .debug – debugging symbols.
Disassembling the ELF
Because an ELF file cannot be opened as plain text, use objdump to view its instructions. Full disassembly: $ objdump -D hello Mixed source and assembly view (requires -g when compiling):
$ gcc -o hello -g hello.c $ objdump -S helloThe output shows each C line alongside its generated machine instructions, making it easy to trace how source maps to binary.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
