Every executable, shared library (.so), and object file (.o) on Linux follows the Executable and Linkable Format (ELF). While tools like readelf and objdump inspect these binaries every day, learning how to parse raw ELF structures directly in C is a core skill for systems programming, malware triage, and embedded toolchain development.

In this guide, I'll walk through how I built a zero-dependency ELF-64 parser in pure C that maps a binary directly into virtual memory using POSIX mmap() and extracts its ELF Header and Section Header Table without relying on libelf.

Why Use mmap() Instead of fread()?

Traditional file parsing allocates a heap buffer with malloc() and copies bytes from disk using read() or fread(). For binary analysis, POSIX mmap() is cleaner and faster:

  • Zero-Copy Access: The Linux kernel maps the file pages directly into your process's virtual address space.
  • Pointer Arithmetic: Any byte at file offset X is accessed directly as base_ptr + X.
  • Struct Overlays: Standard C structs defined in <elf.h> (such as Elf64_Ehdr and Elf64_Shdr) can be cast directly onto memory addresses without manual byte-shifting loops.

Step 1: Mapping the Binary Into Virtual Memory

First, open the target binary in read-only mode, verify its file size using fstat() to prevent out-of-bounds memory reads on truncated files, and map it into RAM:

#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <fcntl.h> #include <unistd.h> #include <sys/stat.h> #include <sys/mman.h> #include <elf.h> int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <elf_file>\n", argv[0]); return 1; } int fd = open(argv[1], O_RDONLY); if (fd < 0) { perror("open"); return 1; } struct stat st; if (fstat(fd, &st) < 0) { perror("fstat"); close(fd); return 1; } // Ensure the file is at least large enough to hold an ELF-64 header if ((size_t)st.st_size < sizeof(Elf64_Ehdr)) { fprintf(stderr, "Error: File too small to be a valid ELF binary\n"); close(fd); return 1; } uint8_t *data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (data == MAP_FAILED) { perror("mmap"); close(fd); return 1; } 

Step 2: Validating Magic Bytes and Parsing the ELF Header

The first 64 bytes of an ELF-64 file always contain the ELF Header (Elf64_Ehdr). By casting data to (Elf64_Ehdr *), we can validate the 4-byte magic signature (0x7F, 'E', 'L', 'F'), verify that the binary is 64-bit (ELFCLASS64), and read key offsets:

 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)data; if (ehdr->e_ident[EI_MAG0] != ELFMAG0 || ehdr->e_ident[EI_MAG1] != ELFMAG1 || ehdr->e_ident[EI_MAG2] != ELFMAG2 || ehdr->e_ident[EI_MAG3] != ELFMAG3 || ehdr->e_ident[EI_CLASS] != ELFCLASS64) { fprintf(stderr, "Error: Not a valid 64-bit ELF file\n"); munmap(data, st.st_size); close(fd); return 1; } printf("==== ELF HEADER ====\n"); printf("Entry Point Address: 0x%lx\n", ehdr->e_entry); printf("Machine Architecture ID: %u\n", ehdr->e_machine); printf("Program Header Offset: %lu (Count: %u)\n", ehdr->e_phoff, ehdr->e_phnum); printf("Section Header Offset: %lu (Count: %u)\n", ehdr->e_shoff, ehdr->e_shnum); 

Key Fields Explained

  • e_entry: The virtual memory address where execution begins (_start).
  • e_machine: Identifies the target ISA (62 corresponds to EM_X86_64, while 183 corresponds to EM_AARCH64).
  • e_shoff & e_shnum: The byte offset where the Section Header Table starts and the total number of section entries.

Step 3: Resolving Section Names via .shstrtab

Each entry in the Section Header Table (Elf64_Shdr) describes a section like .text, .rodata, .data, or .bss. However, sh_name is an integer byte offset, not a string pointer.

To resolve human-readable names, use ehdr->e_shstrndx to locate the Section Header String Table (.shstrtab) entry, calculate its base pointer in memory, and add each section's sh_name offset:

 Elf64_Shdr *shdr = (Elf64_Shdr *)(data + ehdr->e_shoff); Elf64_Shdr *shstrtab = &shdr[ehdr->e_shstrndx]; const char *strtab = (const char *)(data + shstrtab->sh_offset); printf("\n==== SECTION HEADERS ====\n"); for (int i = 0; i < ehdr->e_shnum; i++) { const char *name = strtab + shdr[i].sh_name; printf("[%2d] %-18s Addr: 0x%-8lx Offset: 0x%-8lx Size: %lu\n", i, name, shdr[i].sh_addr, shdr[i].sh_offset, shdr[i].sh_size); } munmap(data, st.st_size); close(fd); return 0; } 

Compiling and Testing

Compile the program with strict warning flags enabled and test it against any compiled Linux binary:

gcc -Wall -Wextra -O2 elf_parser.c -o elf_parser ./elf_parser /bin/ls 

Terminal Output Demo

Wrapping Up

So guys, this is just a beginner-friendly walkthrough, but if you want the full modular source code—including the Program Header security auditor that checks for PIE/ASLR, NX (No-Execute) stacks, and RWX memory segments—and want to understand everything going on under the hood, go check out my GitHub repo:

👉 (https://github.com/boyhowfar89-programmer/raw-elf-analyzer)

Also, read Computer Systems: A Programmer's Perspective (CSAPP) Chapter 7 (Linking)—you'll see the whole picture there!

See you next time! 😁