PostHeaderIcon Four Steps Toward Source, Three Points of No Return

“Can you get the source code back” is the question that starts most reverse engineering engagements and the one that has the least satisfactory answer. The honest answer is that a binary contains enough information to reconstruct something that compiles and behaves identically, and not enough to reconstruct what the developer wrote.

The difference is not a matter of effort. Three specific kinds of information are discarded by the compiler, and no amount of analysis recovers them. Knowing which three sets the expectation before the work starts.

Stage one: disassembly

The first stage turns bytes into instructions, and it is the only stage that is fully mechanical.

The output is a listing of instructions at addresses, with the control flow made explicit. On a modern architecture with a decompiler-friendly debug format this stage is minutes; without it, the main work is deciding where code begins, which is the subject of finding the code.

The artefact is complete in the sense that it loses nothing. Every byte of code is represented, and every instruction is shown as the processor sees it.

Stage Input Output Automation
Disassembly Binary Instruction listing Complete
Decompilation Listing plus flow C-like functions High, with errors
Structure recovery C-like functions Types, structs, globals Partial
Naming Structure plus behaviour Readable program Manual

Stage two: decompilation

The second stage groups instructions into functions and expresses them in a C-like form. It is where the tooling is best and where the results are most often mistaken for source.

A decompiler recovers the control flow reliably and the expressions approximately. It produces code that reads like C and does not compile, because the types are wrong, the variable scopes are invented, and the expressions contain casts that no developer wrote.

Decompiler output Reliable Why
Control flow Yes It is in the instructions
Arithmetic expressions Mostly Recoverable from the operations
Local variable count Approximate Register allocation decides
Types No Nothing in the binary records them
Structure layout Partially Offsets are evidence, not definition

The fourth row is the first point of no return. A thirty-two-bit value in a register is an integer, a pointer, a float, or four characters, and the instruction that loaded it does not say which. A decompiler guesses from how the value is used, and the guess is right perhaps three times in four.

Stage three: structure recovery

The third stage is where a human is required, and it is the stage that determines whether the output is useful.

Recovering structure means deciding what the global variables are, how they group into structures, what the function signatures are, and which functions belong to which module. The evidence is usage: a global that is always accessed at offsets 0, 4 and 12 from a base pointer is a structure with three fields at those offsets; a function whose first argument is always a pointer to such a structure has that structure as its first parameter.

Evidence Inference Confidence
Constant offsets from a base Field layout High, but padding unknown
Same base passed to several functions Shared structure High
Base allocated with a size Structure size Medium; size may be rounded
Field used as a pointer Pointer member High
Field compared against small values Enumeration or flags Medium

The second row of the table above hides the second point of no return. The offsets are known exactly, and the size of the structure is known approximately, but the order of fields and the padding between them are gone. Two structures with the same field types in different orders can produce the same offsets after padding, and the binary cannot distinguish them.

Stage four: naming

The fourth stage is entirely manual and entirely about the reader.

Naming means replacing sub_8004C10 with something a person can follow. It is done from behaviour: a function that writes to a UART data register is a transmit function; a function called from an interrupt vector that reads a timer is a tick handler; a function that compares a buffer against a constant is a check.

The output of this stage is the only artefact that a customer can actually use, and it is also the only one whose quality cannot be measured mechanically.

Naming source Example Reliability
Peripheral register access uart_send High
Vector table position systick_handler High
String constants used error_message_7 Medium
Call graph position main_loop Medium
Behaviour under test checksum_verify High, with effort

The three points of no return

The three losses are worth stating precisely, because they are the reason a quotation for “recovering source” should be a quotation for something narrower.

Types are the first. The binary records operations, not types. A signed and an unsigned comparison differ by one instruction and a decompiler can often tell them apart; an integer and a pointer of the same width are indistinguishable except by how they are used, and a pointer that is never dereferenced looks exactly like an integer.

Structure layout is the second. Offsets survive, order and padding do not. A structure whose fields were declared in a different order but padded to the same total size produces identical access patterns.

Inlining is the third and the most consequential for readability. A function that was inlined by the compiler does not exist as a callable entity in the binary, so the recovered program has no such function. Conversely, a piece of code that the compiler duplicated into several call sites appears as several separate functions, and the decompiler will present them as such.

Lost What survives Consequence
Type identity Operations and widths Casts everywhere in the recovered code
Field order and padding Offsets and total size Structures that work but look wrong
Inlining decisions The resulting instructions Function boundaries that never existed
Macro expansion The expanded code Repetition that was written once
Optimisation choices The chosen form Expressions no developer would write

The fourth and fifth rows are not among the three points of no return because they are recoverable in principle — a macro is a repeated pattern and an optimisation is a choice — but in practice they are not worth recovering, and the result reads as code that was generated rather than written.

What to ask for instead

The productive reframing is to replace “recover the source” with a question about purpose.

If the purpose is to understand how a specific behaviour is implemented, the deliverable is a documented function, not a program. That requires stages one to three for one function and produces something far more useful than a whole decompiled image.

If the purpose is to modify the behaviour, the deliverable is a patched binary and the analysis needed to place the patch, which is the work described in patching IC code.

If the purpose is to rebuild a maintainable product, the deliverable is a specification and a new implementation, because a recovered program is correct and unmaintainable in equal measure.

Purpose Deliverable Stage reached
Understand one behaviour Documented function Three, for one function
Modify one behaviour Patched image One, plus a patch
Rebuild the product Specification plus new code Three, as input to a rewrite
Prove equivalence Test suite plus model One and two

Where the service fits

We quote by stage, not by image. A stage-one deliverable is a listing with the flow resolved. A stage-two deliverable adds decompiled functions with the tool’s output reviewed rather than trusted. A stage-three deliverable adds structures, signatures and a region map with uncertainty marked. Stage four is quoted by function, because it is manual and its cost is proportional to how much of the program matters.

What we will not do is describe stage two as recovered source. A decompiled image compiles only after a week of type corrections, and a customer who expects that week to be included in a stage-two quotation will be disappointed.

The function-level work that makes stage three possible is described in MCU break, and the reason a read image is sometimes encrypted at rest — which defeats all four stages at once — is in decrypting IC flash.

Comments are closed.