PostHeaderIcon PIC18F26K20: Recovery Produces an Image, Not Source Code

The request is usually phrased as “recover the source”. It is a reasonable way to describe the goal — something that can be modified, rebuilt and maintained — and it is not what a flash array contains. The array contains instructions and constants. Everything a source file carries beyond that was discarded by the compiler.

Understanding the size of that gap is not pedantry. It determines what can be done with a recovered image, what has to be rebuilt, and what nobody should promise.

What the compiler discards

Present in source Present in the image
Instructions Yes
Constant values Yes
Function boundaries Partially — called from somewhere, or not
Names No
Types No
Comments, structure No
Loop constructs No — only branches

The first two are the program. The rest is the part that makes a program maintainable, and it is gone in a way that no tool recovers. A disassembler produces labels like code_0x1A42, because that is the only name the information supports.

What an image does support

Three things, and they are more than they sound:

Behaviour. The instructions, executed, produce observable effects. That is the program’s meaning, and it survives complete loss of structure.

Structure, partially. A region called from twelve places is a utility. A region with a jump table is a dispatcher. A region that reads a table and multiplies is a filter. These are inferences, and they are usually right.

Layout. Where code lives, where tables live, how much space each occupies. That is a map of the application’s size and complexity.

What is missing is the vocabulary: what the variables mean, what the functions are for, what the constants represent.

The reconstruction gap in practice

A team that receives an image and needs to modify it faces a specific, bounded problem. Their options:

  • Patch the image directly. Change a constant, redirect a branch. Feasible for small changes and requires no source.
  • Reconstruct a source that compiles to the same behaviour. Feasible for small programs, and rarely byte-identical.
  • Rebuild from specification. Feasible always, and produces something maintainable rather than something equal.

The second option is what people usually expect from “recovery”, and it is the least reliable of the three: the result is a program that behaves like the original and shares none of its code, maintained by people who have never seen either.

Why byte-identical matters less than it seems

There is a persistent expectation that a recovered program should rebuild to the same binary. It cannot, in general, and it is not required. What is required is that the replacement satisfies the same constraints:

  • the same interfaces, at the same rates;
  • the same timing behaviour;
  • the same responses to the same inputs, including faults;
  • the same use of non-volatile storage.

Those four are testable. Byte-identity is not even a good proxy for them, since a compiler change produces different bytes with identical behaviour.

The 64K case

On this part the array is large enough to hold a substantial application, which makes the structure question more pressing. A 64K image has real modularity: multiple files’ worth of code, a scheduler, protocol stacks, tables.

That modularity is visible in the call graph, and the call graph is the closest thing to a file structure that survives. Reconstructing along the call graph’s clusters produces something that resembles the original’s organisation, which is why a disciplined reader can produce a source tree that a maintainer recognises — even though every name is invented.

Honest statements to make

  • The image can be recovered; the source cannot.
  • Behaviour can be characterised; intent has to be inferred.
  • A reconstruction can be maintained; a copy can only be patched.
  • A byte-identical rebuild is not a realistic objective, and not a useful one.

Saying these four things at the start prevents the most common disappointment in this work, which is a technically successful recovery being received as a failure because it was not what was imagined.

Related reading

FAQ

Can source code be recovered from a microcontroller? No. Instructions can. Names, types and structure were discarded at build time.

Is a disassembly usable for maintenance? For patching, yes. For development, a reconstruction is usually better.

Why not aim for a byte-identical rebuild? Because compiler and library differences make it unattainable, and behaviour is the objective that matters.

What is the most valuable output of recovery? A characterised behaviour plus an image: the first for rebuilding, the second for replacement.

Does array size change the answer? It changes the scale, not the nature. Larger images have more structure to infer, and no more names.

Conclusion

Recovery yields an image, and an image is behaviour rather than source. The gap is not closable by effort, and recognising it early turns an impossible expectation into a tractable one: characterise the behaviour, keep the image, and rebuild only where maintenance is required.

Comments are closed.