<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[alexjmigidet]]></title><description><![CDATA[alexjmigidet]]></description><link>https://boyhowfar.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 04 Dec 2026 23:21:03 GMT</lastBuildDate><atom:link href="https://boyhowfar.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Parse ELF-64 Binary Headers and Section Tables in C Using POSIX mmap().]]></title><description><![CDATA[Every executable, shared library (.so), and object file (.o) on Linux follows the Executable and Linkable Format (ELF). While command-line utilities like…]]></description><link>https://boyhowfar.hashnode.dev/how-to-parse-elf-64-binary-headers-and-section-tables-in-c-using-posix-mmap</link><guid isPermaLink="true">https://boyhowfar.hashnode.dev/how-to-parse-elf-64-binary-headers-and-section-tables-in-c-using-posix-mmap</guid><category><![CDATA[C]]></category><category><![CDATA[Linux]]></category><category><![CDATA[elf]]></category><category><![CDATA[parser]]></category><category><![CDATA[binary analysis]]></category><category><![CDATA[posix]]></category><category><![CDATA[Systems Programming]]></category><dc:creator><![CDATA[Boy Howfar]]></dc:creator><pubDate>Sun, 29 Nov 2026 21:49:01 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6b0c4314df5c85286b9a5cc7/68936b91-32e6-4946-8362-a828c7ccc318.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Every executable, shared library (<code>.so</code>), and object file (<code>.o</code>) on Linux follows the <strong>Executable and Linkable Format (ELF)</strong>. While command-line utilities like <code>readelf</code> and <code>objdump</code> inspect these binaries every day, understanding how to parse raw ELF structures directly in C is a foundational skill for systems programming, malware triage, and embedded toolchain development.</p>
<p>In this guide, we will build a zero-dependency ELF-64 parser in C that maps a binary directly into virtual memory using POSIX <code>mmap()</code> and extracts its ELF Header and Section Header Table without relying on <code>libelf</code>.</p>
<hr />
<p><strong>Why Use</strong> <code>mmap()</code> <strong>Instead of</strong> <code>fread()</code><strong>?</strong></p>
<p>Traditional file parsing allocates a heap buffer with <code>malloc()</code> and copies bytes from disk using <code>read()</code> or <code>fread()</code>. For binary analysis, POSIX <code>mmap()</code> is cleaner and faster:</p>
<ul>
<li><strong>Zero-Copy Access:</strong> The operating system maps the file pages directly into the process's virtual address space.</li>
<li><strong>Pointer Arithmetic:</strong> Any byte at file offset <code>X</code> is accessed directly as <code>base_ptr + X</code>.</li>
<li><strong>Struct Overlays:</strong> Standard C structs defined in <code>&lt;elf.h&gt;</code> (such as <code>Elf64_Ehdr</code> and <code>Elf64_Shdr</code>) can be cast directly onto memory addresses without manual byte-shifting loops.</li>
</ul>
<hr />
<p>Step 1: Mapping the Binary Into Virtual Memory</p>
<p>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 memory:</p>
<pre><code class="language-c">#include &lt;stdio.h&gt; 
#include &lt;stdlib.h&gt;
#include &lt;stdint.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;sys/mman.h&gt;
#include &lt;elf.h&gt;

int main(int argc, char *argv[]) {
 if (argc != 2) {
fprintf(stderr, "Usage: %s &lt;elf_file&gt;\n", argv[0]); return 1;
}
int fd = open(argv[1], O_RDONLY);
if (fd &lt; 0) {
    perror("open");
    return 1;
}

struct stat st;
if (fstat(fd, &amp;st) &lt; 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 &lt; 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;
}
</code></pre>
<p>Step 2: Validating Magic Bytes and Parsing the ELF Header</p>
<p>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:</p>
<pre><code class="language-c"> Elf64_Ehdr *ehdr = (Elf64_Ehdr *)data;
if (ehdr-&gt;e_ident[EI_MAG0] != ELFMAG0 ||
    ehdr-&gt;e_ident[EI_MAG1] != ELFMAG1 ||
    ehdr-&gt;e_ident[EI_MAG2] != ELFMAG2 ||
    ehdr-&gt;e_ident[EI_MAG3] != ELFMAG3 ||
    ehdr-&gt;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-&gt;e_entry);
printf("Machine Architecture ID: %u\n", ehdr-&gt;e_machine);
printf("Program Header Offset:   %lu (Count: %u)\n", ehdr-&gt;e_phoff, ehdr-&gt;e_phnum);
printf("Section Header Offset:   %lu (Count: %u)\n", ehdr-&gt;e_shoff, ehdr-&gt;e_shnum);
</code></pre>
<p>Key Fields Explained</p>
<p>• e_entry: The virtual memory address where execution begins (_start).</p>
<p>• e_machine: Identifies the target ISA (for example, 62 corresponds to EM_X86_64, while 183 corresponds to EM_AARCH64).</p>
<p>• e_shoff &amp; e_shnum: The byte offset where the Section Header Table starts and the total number of section entries.</p>
<p>Step 3: Resolving Section Names via .shstrtab</p>
<p>Each entry in the Section Header Table (Elf64_Shdr) describes a section such as .text, .rodata, .data, or .bss. However, sh_name is an integer byte offset, not a string pointer. To resolve human-readable names, use ehdr-&gt;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:</p>
<pre><code class="language-c">Elf64_Shdr *shdr = (Elf64_Shdr *)(data + ehdr-&gt;e_shoff);
Elf64_Shdr *shstrtab = &amp;shdr[ehdr-&gt;e_shstrndx]; 
const char *strtab = (const char *)(data + shstrtab-&gt;sh_offset);

printf("\n==== SECTION HEADERS ====\n");
for (int i = 0; i &lt; ehdr-&gt;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;

}
</code></pre>
<p>Compiling and Testing</p>
<p>Compile the program with strict warning flags enabled and test it against any compiled Linux binary (such as /bin/ls or a compiled C sample): bash gcc -Wall -Wextra -O2 elf_parser.c -o elf_parser ./elf_parser /bin/ls.</p>
<p>Sample Output:</p>
<p>==== ELF HEADER ====</p>
<p>Entry Point Address: 0x1060</p>
<p>Machine Architecture ID: 62</p>
<p>Program Header Offset: 64 (Count: 14)</p>
<p>Section Header Offset: 13968 (Count: 31)</p>
<p>==== SECTION HEADERS ====</p>
<p>[ 0] Addr: 0x0 Offset: 0x0 Size: 0</p>
<p>[14] .text Addr: 0x1060 Offset: 0x1060 Size: 263</p>
<p>[16] .rodata Addr: 0x2000 Offset: 0x2000 Size: 14</p>
<p>[25] .data Addr: 0x4000 Offset: 0x3000 Size: 16</p>
<p>[26] .bss Addr: 0x4010 Offset: 0x3010 Size: 8</p>
<p>Notice how <code>.text</code> begins at virtual address <code>0x1060</code>—matching <code>e_entry</code>—and <code>.bss</code> occupies zero bytes on disk (<code>Offset: 0x3010</code> matches the following section's offset).</p>
<p>So guys this is just beginner friendly type but if you want to the full source and also have the understanding of is going on go check of my github <a href="https://github.com/boyhowfar89-programmer/raw-elf-analyzer">https://github.com/boyhowfar89-programmer/raw-elf-analyzer</a> and read the book "Computer Systems: A Programmer Perspective" on Chapter 7 i think 🤔🤔 yeah which is Linking, you will see the whole picture their...</p>
<p>See you next time 😁😁😁.......</p>
]]></content:encoded></item></channel></rss>