PostHeaderIcon ATmega861A: Telling Library Code From Yours in Eight Kilobytes

Every compiled image contains two kinds of code: what the developer wrote and what the toolchain supplied. At eight kilobytes both are present in quantity, and the ability to tell them apart is what makes an analysis tractable — because one half can be skimmed and the other cannot.

The boundary is not marked. It has to be inferred, and there are reliable signals.

Signals that identify library code

Repetitive structure. Multiplication, division, and long-integer arithmetic compile into tight loops with a characteristic shape. Two such loops in an image usually mean the same routine was emitted twice, or that one is multiply and one is divide.

No peripheral access. Library routines touch memory, not registers. A function whose body contains no write to a hardware register address is almost certainly compiler-generated.

Called from many places, calls nothing. A leaf function with many callers and no callees is either a utility or a hardware abstraction the developer wrote. Its body distinguishes the two.

Fixed, recognisable prologues. Saving and restoring registers around a computation produces a repeated pattern at entry and exit, which is a strong hint that the body was generated rather than written.

Signals that identify application code

The complement is easier to spot:

  • Writes to peripheral registers with addresses in the hardware range.
  • Reads of status flags followed by conditional branches.
  • Delay loops sized with round numbers.
  • Table lookups whose index comes from an analogue reading.

That last item is where the product usually is. A table indexed by an ADC result means a calibration curve, a display segment map, or a threshold set — and all three are design decisions rather than mechanical translation.

Signal Library Application
Register writes none frequent
Flag polling rare constant
Table lookups no common
Register save/restore yes only if large

Why the distinction saves time

An image at this size may contain a kilobyte of arithmetic support. Reading it instruction by instruction is wasted effort; recognising it as “16-bit divide” and moving on is the correct level of attention.

Conversely, treating application code as library code is the expensive error, because the application is where the design lives and where the constants are. Skimming it produces a report that describes the compiler rather than the product.

The discipline is therefore: classify first, then read selectively. Classification is a matter of scanning for register access, which is fast and reliable.

The A revision in this context

The revision affects the die, not the toolchain. It may change the signature byte, and it may change how a peripheral behaves at the edge of its specification, which in turn changes whether a workaround appears in the application half.

That is the useful connection: when comparing two images of the same product built for two revisions, the differences concentrate in the workarounds. Library code is identical; application code differs in exactly the places the die changed.

Reading order

  1. Signature, fuses, lock byte.
  2. Array read, verified.
  3. Reset vector, then initialisation.
  4. Classify functions by register access — library or application.
  5. Extract constants from the application half, noting scale factors.

Step 5 is the deliverable. Constants without their scale factors are numbers, and numbers without context are the usual reason a reconstruction fails on first build.

Related notes

FAQ

Can the boundary be found automatically? Partly. Register-access scanning finds most application code; the rest needs judgement.

Is library code worth reading at all? Only to identify it. Its behaviour is known.

Why do the same routines appear twice? Separate translation units or an inlined variant beside the out-of-line one.

Does the revision change the library half? No. The compiler output is the same; only the application’s workarounds differ.

Conclusion

Eight kilobytes is the size at which an image stops being readable as a whole and starts needing classification. Register access is the discriminator, and applying it first turns a long list of functions into two short lists, one of which can be skimmed and one of which is the product.

Comments are closed.