Mastering GCC: From Source Code to Executable ELF Binaries

This article explains how high‑level C/C++ programs are transformed into processor‑executable binary code on Linux, covering preprocessing, compilation, assembly, linking, the GCC toolchain, Binutils utilities, C runtime libraries, and ELF file analysis with practical command examples.

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

GCC Toolchain Overview

GCC (GNU Compiler Collection) is the standard compilation suite on Linux, consisting of the compiler itself and supporting tools such as Binutils and the C runtime library.

Binutils Utilities

Binutils provides a set of binary handling tools, including addr2line , ar , objcopy , objdump , as , ld , ldd , readelf , and size . These tools are essential for development and debugging.

addr2line – converts program addresses to source file and line numbers.

as – the assembler.

ld – the linker.

ar – creates static libraries.

ldd – shows shared‑library dependencies of an executable.

objcopy – converts object files between formats.

objdump – disassembles binaries.

readelf – displays ELF file information.

size – reports section sizes of an executable.

C Runtime Library

The C standard defines syntax and a standard library of headers (e.g., stdio.h) that declare functions like printf. Implementations require a C runtime library (CRT) to provide the actual code for these functions. C++ has a similar runtime library.

Preparation

All examples use a Linux environment. A simple "Hello World" program written in C serves as the demonstration source:

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

Compilation Process

The transformation from source to executable consists of four steps:

Preprocessing – expands macros, includes header files, removes comments, adds line numbers, and retains #pragma directives. Command: gcc -E hello.c -o hello.i Compilation – performs lexical, syntactic, and semantic analysis, then generates assembly code. Command: gcc -S hello.i -o hello.s Assembly – translates assembly instructions into machine code, producing an object file ( .o). Commands: gcc -c hello.s -o hello.o or as -c hello.s -o hello.o Linking – combines object files and libraries into a final ELF executable, handling symbol resolution and relocation. Both static and dynamic linking are possible. Commands:

Dynamic linking (default): gcc hello.c -o hello Static linking:

gcc -static hello.c -o hello

ELF File Analysis

An ELF executable contains several sections, such as .text (code), .rodata (read‑only data), .data (initialized data), .bss (uninitialized data), and .debug (debug symbols).

Useful commands: readelf -S hello – lists section headers. objdump -D hello – disassembles the binary. objdump -S hello – shows disassembly mixed with source code (requires -g during compilation).

Dynamic linking searches library paths in the order: -L options, LIBRARY_PATH environment variable, then default directories ( /lib, /usr/lib, /usr/local/lib). At runtime, the loader checks LD_LIBRARY_PATH, /etc/ld.so.conf, and default paths.

Static linking produces a larger executable but eliminates runtime dependencies, as shown by the size and ldd command outputs.

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.