Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. c-programming
  3. elf-parser
6 min read

Project: ELF Parser

Time to put the whole section to work. This project combines structs, binary file reading, and a real-world format into one small tool. ELF — the Executable and Linkable Format — is the format of every program, shared library, and object file on Linux. We are going to read an ELF file's header and print what is inside, the same job the standard readelf -h tool does. It is also your first step toward the reverse-engineering and binary-exploitation courses, which live and breathe this format.

The ELF Header

Every ELF file begins with a fixed-size header that describes the rest of the file. The first four bytes are a magic number that identifies the format: 0x7f followed by the ASCII letters E, L, F. After that come fields recording whether the file is 32- or 64-bit, its type, the CPU it targets, the entry point (the address where execution begins), and offsets to the other tables in the file.

Elf64_Ehdr — the first bytes of every ELF file offsetfieldmeaning 0e_ident[16]magic 7f 45 4c 46, then class + endianness 16e_typeEXEC, or DYN for a PIE 18e_machinetarget CPU, e.g. x86-64 24e_entryentry point — where execution begins 40e_shoffoffset to the section header table 60e_shnumnumber of section headers

You can see that magic with your own eyes. Compile any program and dump its first sixteen bytes:

$ gcc target.c -o target$ xxd -l 16 target00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............

There it is: 7f 45 4c 46 — \x7f, E, L, F — the signature at the very start of the file.

Mapping It to a struct

Because the header's layout is fixed and well-defined, it maps perfectly onto a C struct — and you do not even have to write it. The standard header <elf.h> already defines Elf64_Ehdr for the 64-bit header, with every field we just described:

c
1#include <elf.h>
2
3Elf64_Ehdr header; // e_ident, e_type, e_machine, e_entry, e_shoff, e_shnum, ...

This is the clean case of the struct padding discussion: the ELF standard deliberately chose field sizes and orders so the struct's in-memory layout matches the file's on-disk layout exactly. That means you can read the entire header in a single fread and trust the fields to line up.

Reading the File

Reading it is the binary I/O pattern from earlier: open in binary mode, fread the header straight into the struct, and sanity-check the magic before trusting anything:

c
1FILE *fp = fopen(argv[1], "rb");
2Elf64_Ehdr header;
3fread(&

EI_MAG0 and friends are named indices from <elf.h> into the e_ident array — e_ident[EI_MAG0] is that first 0x7f byte.

Printing the Fields

With the header in hand, printing its fields is just reading struct members. Here is the whole tool:

elfparse.c
1#include <elf.h>
2#include <stdio.h>
3
4
$ gcc elfparse.c -o elfparse$ ./elfparse targetmagic:        \x7fELF (valid)class:        ELF64type:         DYN (PIE)machine:      x86-64entry point:  0x1050sections:     31 headers at offset 13968

Every field came straight out of the header we read. To prove it is right, run the real readelf -h on the same file and compare — the class, type, machine, and entry point all match:

$ readelf -h target  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00  Class:                             ELF64  Type:                              DYN (Position-Independent Executable file)  Machine:                           Advanced Micro Devices X86-64  Entry point address:               0x1050

That is a genuinely useful program in about thirty lines: it reads a binary format, maps it onto a struct, and interprets the fields.

Stretch: Section and Program Headers

The header is only the doorway. Two of its fields, e_shoff and e_phoff, are offsets to arrays of more headers — the section headers (Elf64_Shdr, there are e_shnum of them) that describe the code, data, and symbol regions, and the program headers (Elf64_Phdr) that tell the kernel how to load the file into memory. As a stretch goal, fseek to e_shoff and read the section headers in a loop, exactly as you read the main header, to list what the binary contains.

When you are ready, the next project moves from files to the network — a client and server talking over a socket.

header
,
sizeof
(
header
)
,
1
,
fp
)
;
4fclose(fp);
5
6if (header.e_ident[EI_MAG0] != 0x7f || header.e_ident[EI_MAG1] != 'E')
7{
8 printf("not an ELF file\n");
9 return 1;
10}
int
main
(
int
argc
,
char
*
argv
[
]
)
5{
6 FILE *fp = fopen(argv[1], "rb");
7 if (fp == NULL)
8 {
9 printf("could not open %s\n", argv[1]);
10 return 1;
11 }
12
13 Elf64_Ehdr header;
14 fread(&header, sizeof(header), 1, fp);
15 fclose(fp);
16
17 if (header.e_ident[EI_MAG0] != 0x7f || header.e_ident[EI_MAG1] != 'E')
18 {
19 printf("not an ELF file\n");
20 return 1;
21 }
22
23 printf("magic: \\x7fELF (valid)\n");
24 printf("class: %s\n", header.e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64" : "ELF32");
25 printf("type: %s\n", header.e_type == ET_DYN ? "DYN (PIE)" : "EXEC");
26 printf("machine: %s\n", header.e_machine == EM_X86_64 ? "x86-64" : "other");
27 printf("entry point: 0x%lx\n", header.e_entry);
28 printf("sections: %u headers at offset %lu\n", header.e_shnum, header.e_shoff);
29 return 0;
30}