PostHeaderIcon ATmega2560V: EIND and the Three-Byte Return Address

Halfway through this part’s flash there is a boundary that changes the shape of every call. Below it, a jump carries its own destination. Above it, the destination needs a second register, and the return address the call leaves behind is wider than the one below.

An image that crosses that boundary is not simply a larger image. It is an image with two calling conventions in it, and a disassembly that treats it as one will produce a call graph that is wrong in a way that looks correct.

The boundary, in words

The array is 128 K words. A direct jump instruction carries a sixteen-bit word address, which covers the first 64 K words — half the array, 128 KB of bytes. Any destination above that does not fit in the instruction’s field.

destination reached by register involved
below 64 K words relative jump, or direct jump and call none
above 64 K words extended indirect jump and call the high-bit register, through a pointer pair
any address return from subroutine the stack

The table’s middle row is the whole subject. Code in the upper half is only reachable indirectly, which means the address is computed rather than encoded, which means it can be computed wrongly.

Where the high bit comes from

A single register holds the seventeenth bit of the jump target. Before an extended indirect jump or call, that register has to be loaded with the correct value; the pointer pair supplies the low sixteen bits. Two registers, one instruction, one destination.

value in the high-bit register pointer pair effective destination
0 0x0000 start of the array
0 0x8000 32 K words, mid-array
1 0x0000 64 K words, the boundary
1 0x8000 96 K words, upper half

The pair of writes that sets it is short and easy to miss, and it appears immediately before the extended call rather than at the top of the function. That placement is a deliberate optimisation: the value is only needed for the jump, so it is set as late as possible.

The consequence for a reader is unpleasant and unavoidable. The destination of an extended call is not visible at the call site. It is the composition of two registers, one of which was written a few instructions earlier and the other of which may have been computed by arithmetic. Static reading gives a range, not an address.

Three bytes on the stack

A call pushes the return address, and on this part that address is three bytes. An interrupt pushes the address plus the status register, four bytes before any register saving.

That changes arithmetic that is otherwise routine. Stack depth per level is three bytes rather than two, so a nesting depth that fits comfortably on the smaller part may not fit here. A calculation carried over from a 128 KB design understates the stack by a third of the nesting depth, which is exactly the kind of error that survives testing and appears in the field.

It also changes how frames look in memory. A debugger or a hand analysis that reads a frame as two-byte slots will misalign every local variable after the first, and the values will look plausible because they are the neighbouring values of real data.

What a disassembler gets wrong, and how to notice

Three failure modes are common enough to check for explicitly before trusting any output on this part:

  1. The extended jump and call instructions are decoded as unknown, so the function containing them appears to end early and the code after it is attributed to whatever follows.
  2. The extended instructions are decoded but the high-bit register is ignored, so every indirect call is drawn as landing in the lower half. The call graph then has plausible edges to wrong places, which is worse than having none.
  3. The return-address width is assumed to be two bytes, so the stack model is off by one byte per frame and any derived analysis — local variable recovery, buffer sizes, frame pointers — drifts.

Each of the three produces output that looks syntactically fine. The check that catches them is arithmetic rather than visual: pick a function whose stack usage can be verified independently, compute the frame from the calling convention, and compare against what the analysis claims. A one-byte discrepancy per level points at the third failure mode.

Reading a call graph that spans the boundary

The workable approach is to treat the boundary as a partition rather than an inconvenience. Functions below the boundary have fully encoded call targets and can be read bottom-up. Functions above it are reached indirectly, so their entry points have to be established from the other direction: from the pointer values that are written, from tables of function pointers in the flash array, and from the vector table if any handler lives up there.

In practice that means building two graphs and joining them at the known interface — the extended calls. The join is where the risk is, and it is worth documenting which edges are certain and which are inferred. A report that presents an inferred edge as certain is worse than one that labels it.

Why the V suffix is worth a line

The trailing letter marks the low-voltage variant. It changes the supply range and the maximum frequency at the bottom of that range, which changes the timing constants a build is calibrated against. It does not change the array size, the counter width or the calling convention.

So the analysis above applies unchanged, and the only thing the suffix adds is a note in the deliverable: the clock the image expects. A low-voltage build restored onto a high-voltage part runs, and runs fast enough to break every serial timing in the design — a failure that looks like a firmware bug and is a mismatch between silicon and image.

The deliverable

The return set is the flash image at the correct length, the EEPROM separately, the fuse and lock state as read, a disassembly with the extended jump and call annotated with their computed targets where known, a call graph that marks inferred edges as inferred, and the stack model with the three-byte frame stated explicitly.

The data side of the same boundary — the register that extends constant loads and the tables that need it — is covered in ATmega2560 flash recovery. The part whose array fits entirely inside one sixteen-bit address is in ATmega1280V program recovery, and a case where the call graph stays entirely within one bank appears in ATtiny4313 code recovery.

FAQ

Can any single instruction jump anywhere in the array? No. Direct jumps reach half of it; the upper half requires an indirect jump with the high bit supplied from a register.

Does the wider return address change the vector table? No. The table’s location is fixed and its entries are jumps like any other. It is the stack that changes width.

Would a compiler for the smaller part produce a valid image here? It would produce code that runs below the boundary and cannot reach above it. The two parts are not interchangeable in that direction.

Conclusion

Above 128 KB the destination of a call is computed, not encoded, and the evidence of where it went is a register set a few instructions earlier. Add a three-byte return address and the stack model shifts with it. Read those two facts into the analysis from the start and the upper half of a 256 KB image stops being a region of ambiguous edges.

Comments are closed.