How the engine works
Program::open in engine.rs does everything needed to make a file browsable. It runs on a helper thread so the window stays responsive.
1. Read the file format (pe.rs)
Section titled “1. Read the file format (pe.rs)”pe::parse checks the MZ and PE markers, reads the headers and section table, and builds an image: the file laid out the way Windows would load it into memory. From then on every address in the app is a memory address, and reading an address is a plain array lookup.
It also reads:
- imports: for each borrowed function, the address of the slot that will hold its location, and its name as
DLL!Function, - exports: named functions the file offers,
- the function table (64-bit only): the exact start and end of every function that needs stack unwinding information,
- a few header facts: build timestamp, whether it is a DLL, whether it has windows, whether it carries a signature.
Every read is bounds-checked and returns “nothing” on failure. A section with no data stored in the file is treated as empty, so a hostile file cannot claim a gigantic one.
1b. Or read a Linux program (elf.rs)
Section titled “1b. Or read a Linux program (elf.rs)”When the file starts with \x7fELF, elf::parse reads it instead and fills in the same structure, so nothing after this step needs to know which kind of file it is:
- The image comes from the program headers (
PT_LOADsegments), laid out at their memory addresses. A position-independent file is moved up by0x100000(64-bit) or0x10000(32-bit). - Sections come from the section headers when present (
.text,.plt,.got,.bss, …), otherwise from the segments, namedLOAD0 (r-x)and so on. - Imports come from the dynamic relocations: every
JUMP_SLOT,GLOB_DAT,COPYand plain pointer relocation that names a symbol gives one slot address and its name, aslibc.so.6!puts. The library is found through the GNU version tables. Calls reach those slots through PLT stubs, whichProgram::thunk_slotrecognises in all three forms:jmp [rip+X],endbr64; bnd jmp [rip+X], and 32-bitjmp [ebx+X](with ebx holding the GOT address fromDT_PLTGOT). - Function names and exact sizes come from
.symtaband.dynsym. Sizes are trusted only when the full symbol table is present. Plain labels (assembly) name functions that are found by following calls. - Protections: PIE from the file type and loader, NX from
PT_GNU_STACK, RELRO fromPT_GNU_RELROplusBIND_NOW, the canary from an import of__stack_chk_fail.
After the functions are known, Program::find_main reads the first instructions of _start and takes the value put in rdi (64-bit) or the last value pushed (32-bit) before the first call: that is main.
2. Unpack if needed (upx.rs)
Section titled “2. Unpack if needed (upx.rs)”If the file contains a UPX header, it is unpacked and the result replaces what was parsed. See How UPX unpacking works.
3. Find the functions
Section titled “3. Find the functions”Starting points are added in this order, later ones overriding earlier names:
- every entry in the function table, named
sub_<address>, with its exact end, - every export, under its real name,
- the entry point, named
entry_point.
Then the analysis pass adds every destination of a direct call that is not already inside a known function. For files with a function table, only the insides of listed functions are scanned, where the bytes are known to be code. For files without one (32-bit), whole code sections are scanned.
Finally, any function that consists only of jmp [import] takes the import’s name.
4. Find the text
Section titled “4. Find the text”Every section is scanned for runs of five or more printable characters, as ordinary text and as wide text. After analysis, text found inside a code section is dropped unless an instruction refers to it and it is not inside a known function.
5. Record cross-references
Section titled “5. Record cross-references”Each function is decoded once more. For every instruction the engine notes:
- direct calls and jumps to a known function,
- memory operands relative to the instruction pointer (64-bit) or at a fixed address (32-bit),
- immediate values that are addresses inside the image,
but only when the target is an import, a known piece of text or a function. The result is a map from each target to the places that use it, capped at 500 per target.
6. Disassemble on demand
Section titled “6. Disassemble on demand”function_lines(start) produces the lines for one function when it is opened. Each instruction is decoded by iced-x86, formatted, and annotated with:
- a kind: call, jump, conditional jump, return, padding, or data,
- a target when a call or jump goes to a fixed address,
- a comment: the import, function or text the instruction refers to.
A function ends at its listed end if there is one. Otherwise it ends at the next known function or, failing that, at the first ret or unconditional jmp that no earlier jump in the function goes past.
7. Detect packing
Section titled “7. Detect packing”See Packed programs and UPX for the signs. Randomness is Shannon entropy over a section’s bytes.