PostHeaderIcon After the Read: Three Obstacles Between Machine Code and Readable Code

Suppose the image is on disk. The protection has been dealt with, the array has been read, and the bytes are there. The job now looks finished to anyone who has not done it before, and it is in fact about a third done.

What follows is the part that determines whether the deliverable is useful. A raw disassembly is a list of instructions with plausible addresses. A readable program is one where the boundaries between functions are known, the call graph is intact, and the parts that came from a library are labelled as such. Getting from one to the other means crossing three obstacles, and they are crossed in a specific order.

Obstacle one: code and data share the same space

Every disassembler has to decide, for each byte, whether it is an instruction. On a von Neumann machine this is ambiguous in principle and resolved by heuristics in practice. On a Harvard machine the instruction space is separate, which sounds like it removes the ambiguity and in fact only moves it: the literal pools that hold constant data live in the instruction space, interleaved with code.

The visual symptom is familiar. A region of the listing shows instructions that make no sense — a sequence of plausible opcodes that do not form a coherent flow, often ending in a branch to an address in the middle of another function. That region is a literal pool, and the disassembler has been decoding constants as code.

Symptom Likely reality How to confirm
Senseless instructions after a branch Literal pool References to the region as data
Same bytes decode two ways Overlapping functions Two entry points into one region
Long runs of identical bytes Padding or erased flash Boundary of the used region
Code that is never reached Dead code or a jump table Cross-references from a computed branch

The method is not to guess better but to work from known anchors. The vector table is the first anchor: its entries are function addresses, so everything it points at is code. The reset handler is the second: it copies initialised data, clears bss, and calls the C runtime, and its structure is recognisable across compilers.

From those anchors, the work proceeds by following the control flow that the anchors imply. A branch to an address makes that address code. A load from an address makes that address data. The two rules are applied recursively, and the regions that remain unclaimed are the ones that need human judgement.

Obstacle two: the targets that are not in the instruction

A direct branch carries its destination in the instruction, so following it is trivial. The problem is the branches that do not: function pointer tables, switch jump tables, and the dispatch inside a real-time operating system.

A switch statement over a dense range compiles to a table of addresses indexed by the value, and the table is data. A disassembler that has not identified the table sees a computed branch with no successors, and everything beyond it becomes unreachable — which in a recursive-descent disassembler means everything beyond it is not decoded at all.

Indirection Where the target lives How to recover it
Switch jump table A data table near the branch Locate the table, read the entries
Function pointer array A data table in flash or RAM Locate initialisers, read the entries
RTOS dispatch A ready list in RAM Identify the scheduler, read the structure
C++ virtual call A vtable in flash Recover the object layout first

The consequence of missing these is severe and specific: whole regions of the program appear to be data. In a typical image with a protocol stack, missing the dispatch table can hide a third of the code, and the resulting listing looks like a small program with a large data section.

The recovery method is to work backwards from the branch. A computed branch has a base register and an index, and the base is loaded from somewhere in the preceding few instructions. That load identifies the table. Reading the table’s entries as addresses, and checking that they land inside the image and are aligned to instruction boundaries, confirms the identification.

Obstacle three: half the image is library code

A typical firmware image is between forty and eighty percent library code, and none of it was written by the customer. Startup code, the C runtime, division and floating-point support, memcpy and its friends, the vendor’s peripheral drivers, and on connected products a TCP/IP stack and a crypto library.

Treating that code as if it were the customer’s produces a report in which the interesting logic is buried under thousands of lines that are identical across every product using the same toolchain.

Library category Identification signal Why it matters
Compiler runtime Known prologue, called from startup Not the customer’s IP
Division and soft float Shift-subtract loops Time-consuming to analyse, rarely interesting
Vendor peripheral drivers Register accesses at fixed addresses Reveals the memory map
Crypto library Constant tables, characteristic loops Often the actual target
Protocol stack State machine with a dispatch table Usually the largest single block

Identification is done by signature matching against known builds of the same toolchain, by recognising the register access patterns of the vendor’s drivers, and by looking for the constant tables that crypto and protocol code cannot avoid carrying.

The last row is the one worth the effort. In a connected product the protocol stack is usually the largest piece of customer-relevant code, and identifying it as a block — rather than as hundreds of individual functions — is what makes the rest of the image legible.

What the three obstacles have in common

All three are problems of deciding what a byte is for, and all three are solved by anchors rather than by heuristics. The vector table, the reset handler, the table identified from a computed branch, the signature of a known library: each of these is a fact, and each one propagates.

The order matters because the anchors depend on each other. Code and data cannot be separated until the tables are found, and the tables cannot be found reliably until the library code is recognised, because library code contains tables of its own that look like customer data.

Order Obstacle Anchor used Output
1 Library identification Signatures, register patterns Blocks labelled
2 Code and data separation Vector table, reset handler Regions classified
3 Indirect targets Computed branches, table bases Call graph complete

Where the deliverable lands

After the three obstacles are crossed, the image has functions with boundaries, a call graph with most edges present, and labelled library blocks. That is the state in which the customer’s own logic becomes visible and in which a question about behaviour can be answered.

It is still not source code, and the difference is worth stating because it is the most common misunderstanding in this work. Recovering source means recovering types, structure layouts and inline expansions, and those are not in the image; the limits are set out in MCU source code reverse engineering.

What the customer usually needs is something narrower and more achievable: the ability to find the function that decides a particular thing. That is what a complete call graph delivers, and it is why the three obstacles are worth crossing rather than working around.

The cost of skipping a step

The three obstacles are presented in order because skipping one makes the next impossible, and the failure is not obvious until much later.

Skipping library identification produces a listing in which the customer’s forty functions are buried among two thousand library functions, and every subsequent question takes ten times longer to answer. The symptom is a disassembly that “looks complete” and in which nothing can be found.

Skipping code and data separation produces a listing in which literal pools are decoded as instructions. The symptom is a set of functions that appear to call addresses in the middle of other functions, and a call graph that contains edges no compiler would generate.

Skipping indirect target recovery produces a listing in which a third of the program is missing. The symptom is the most deceptive of the three, because everything visible is correct and the absence is invisible.

Skipped step Visible symptom Time lost
Library identification Nothing can be found Days per question
Code and data separation Impossible call edges Days
Indirect target recovery A third of the code absent Weeks, or a wrong conclusion

The third row is the one that produces wrong answers rather than slow ones. An analysis performed on an image with a missing third does not merely take longer; it reaches conclusions about how the device behaves that are confidently incorrect, because the code that implements the behaviour was never seen.

The service at this layer

The work here begins after the read and ends before any behavioural analysis. We deliver a reconstructed image with functions delimited, library code identified and labelled, the call graph resolved where the tables allow it, and a map of the regions that remain uncertain with the reason for each.

That last item is the one customers value most, because a map of uncertainty is what allows a follow-up question to be answered in an hour rather than a week. A listing with no uncertainty marked is a listing that is wrong somewhere and does not say where.

Where the customer’s goal is a loadable image rather than a readable one, none of this is necessary, and the cheaper deliverable described in extracting MCU firmware is the right one. Where the goal is a patch, the boundaries established here are the prerequisite for the work in patching IC code.

Comments are closed.