Skip to content

Lesson 4 of 13 · 20m

Two File Formats, One Job

Sections are for the linker and segments are for the loader — how execve and CreateProcess turn a file into a process, and why VAs, file offsets, and TLS callbacks matter.

When you run ./crackme, the file on disk is not what executes. The kernel reads the ELF header, maps the file's PT_LOAD segments into a fresh address space, notices the PT_INTERP entry naming /lib64/ld-linux-x86-64.so.2, and hands control to that — the dynamic linker — which maps the needed shared libraries, applies the data relocations, and leaves each function import to resolve on its first call through the PLT (unless the link ran -z now, which resolves every import before entry executes). On Windows it's CreateProcess → the kernel maps the PE's sections → ntdll's loader walks the .idata import table → and before the entry point runs, every TLS callback in the binary has already executed. The wrong mental model is "the file is the program." The file is a container the loader interprets; the program is what exists in memory afterward.

Sections are for the linker, segments are for the loader

readelf -S showed you sections — .text, .rodata, .data — which exist so the linker can organize the file. readelf -lW shows program headers — LOAD segments — which are what the kernel actually maps:

$ readelf -lW crackme
Type           Offset   VirtAddr         PhysAddr         FileSiz  MemSiz   Flg
LOAD           0x000000 0x0000000000000000 ...              0x001018 0x001018 R
LOAD           0x001000 0x0000000000001000 ...              0x0001a3 0x0001a3 R E
LOAD           0x002000 0x0000000000002000 ...              0x0000d4 0x0000d4 R
LOAD           0x002dd8 0x0000000000003dd8 ...              0x000258 0x000260 RW

The VirtAddr/Offset/FileSiz/MemSiz columns are the file↔memory translation you'll need every time you patch. VA 0x4010 in the last LOAD lives at file offset 0x4010 - 0x3dd8 + 0x2dd8 = 0x3010 — subtract the segment's VirtAddr, add its Offset, don't guess. Note FileSiz < MemSiz on that segment: the tail is .bss, zero-initialized memory that takes no bytes in the file at all.

Imports and the GOT/PLT machinery

On ELF: .dynsym names the imports, .rela.plt/rela.dyn hold the relocations, .got.plt is the table of resolved addresses. call printf@plt jumps to a .plt stub that jumps *GOT — lazily resolved on first call, or eagerly when LD_BIND_NOW/BIND_NOW (-Wl,-z,now, the default on most distro builds) is in effect. readelf -r dumps the relocation table — a complete list of every external call the binary can make.

Lazy binding is a two-step the stub performs once: the GOT entry initially points back into .plt, which pushes the relocation index and falls into _dl_runtime_resolve; the resolver writes the real address into the GOT and jumps to it, so every later call through that stub goes straight to libc. -z now resolves the whole table before entry instead — startup cost in exchange for a GOT that can then be mapped read-only (full RELRO), which is why distro build flags default to it.

On PE the equivalent lives in .idata: the Import Directory lists DLL + function name pairs, and the loader writes resolved addresses into the IAT — the table your call qword [rip+X] instructions indirect through. objdump -x crackme.exe | grep -A30 "Import Table" or dumpbin /imports shows the same feature list the ELF import table does.

Relocations and PIE

A PIE binary has no fixed home: ET_DYN in the ELF header means "map me anywhere and fix up the pointers." That's why the listing's 0x1180 lives at base + 0x1180 at runtime, and why readelf -r on a PIE binary is full of R_X86_64_RELATIVE entries — the linker adds the load base to each absolute pointer stored in the file. ASLR is the same mechanism worn as a security feature.

The two formats differ in naming, not anatomy. A PE carries .text (code), .rdata (constants + imports/exports directories), .data (writable globals), .pdata (unwind info — a ready-made function boundary list), and .reloc (the fixup table the loader uses when ASLR moves the image). It also ships freebies ELF doesn't: an export table naming every function it offers other binaries, a version-info resource with CompanyName and FileDescription strings, and an Authenticode signature block. objdump -x crackme.exe dumps all of it — headers, sections, imports, exports — in one shot.

Field notes

  • TLS callbacks run before WinMain/main — a classic place for anti-debug and early initialization. If code runs before your entry breakpoint, check the TLS directory (objdump -x shows it).
  • .pdata on 64-bit PE files is the unwind table — a reliable function boundary list, which makes it a nice recovery source for disassemblers.
  • On ELF, checksec (from pwntools, or checksec --file=) prints the posture in one line: RELRO level, NX, PIE, canary. Know it before you design a patch or an exploit.
  • LD_DEBUG=bindings,libs ./crackme makes the dynamic linker narrate every symbol it resolves — the cheapest possible trace of a binary's imports in action.

Keep reading

Related articles from the SudoSecurity blog.