PostHeaderIcon R5F21276KFP: Rebuilding Function Boundaries from the Vectors

A R5F21276KFP reverse engineering job starts with a file and a disassembler, and the disassembler immediately creates a problem it cannot solve. It produces a linear list of instructions with addresses, which is a correct rendering of the bytes and a useless model of the program. Code is organised in functions, functions have entry points and local state, and none of that is marked in the binary.

Recovering those boundaries is the first real analytical step, and on this core it is unusually tractable, because the architecture leaves three independent trails: the vector table, the static base register convention, and a distinctive prologue idiom that the compiler emits for almost every non-trivial function.

Why boundaries are the first problem

Without boundaries there is no call graph, and without a call graph there is no way to answer the questions a customer actually asks: what does this product do, which code handles the serial port, where does the calibration get read.

A flat disassembly also misleads. Data that happens to decode as instructions appears as code, and code that is only reachable through a computed jump appears as data. Both errors are resolved by knowing where functions begin, because a function entry point disambiguates the bytes that follow it. The same problem appears in a different form when the subject is a routine that was copied into RAM before running, as described in R5F212B7SNFP MCU flash memory, where the entry point and the code live at different addresses.

What the vectors give you

The low part of the map holds the fixed vector table, one entry per interrupt source, and each entry is the address of a handler. Those are function entry points stated by the hardware, and they are the only entry points the image hands you for free.

source of entry points reliability how many you get
low fixed vectors exact one per interrupt source
special-page vectors at the top exact one per advanced source
the reset vector exact one
prologue pattern scan high most of the rest
JSR and JSRS targets high all statically called functions
return address stores on the stack medium the dynamically called ones

The last two rows are how you fill the gaps. Every direct call instruction names a target, so scanning the code for call opcodes produces a list of functions that nobody calls through a vector. Adding those to the vector list usually accounts for the majority of the image.

The prologue idiom

The core’s calling convention makes functions visible. A function that uses local variables and calls other functions begins by adjusting the frame pointer and saving the registers it intends to destroy, and the compiler emits that as a short, recognisable sequence: a push-multiple instruction listing the registers to preserve, then an adjustment to the frame pointer.

The sequence is not perfectly uniform, because small leaf functions skip it entirely. But it is uniform enough that scanning for push-multiple instructions near the start of a candidate function corroborates a boundary found by other means, and finds boundaries that the call scan missed when a function is only reached through a function pointer.

Reading the core’s address idioms

Two registers do most of the work in compiled code on this core, and recognising them tells you what a block of instructions is doing before you decode it instruction by instruction.

The static base register points at the global data area. Instructions that reference it with a displacement are touching globals, and the displacement value is a stable identifier: the same global appears with the same displacement in every function that uses it. Collecting those displacements across the image produces a map of the global data layout, which is often more informative than the code itself.

The frame pointer points at the current function’s local frame. Instructions that reference it are touching locals and arguments, and the displacement tells you the frame size. Frame sizes that appear repeatedly suggest a structure being copied or a buffer being passed around.

register what it addresses what the displacement tells you
static base global data which global, consistently across functions
frame pointer locals and arguments the frame layout of this function
the address registers buffers and structures which structure field is being touched
the general registers arithmetic nothing structural

Building the map iteratively

The work is a loop, not a pass. Start with the vector entries, disassemble from each, follow the calls, and record every target. Then scan for prologues to catch what the call graph missed. Then, for each function found, record its static base displacements and frame size.

Run the loop again against the newly found functions. Each pass adds targets, and the passes converge quickly, because the image is small and the call graph is shallow. Most images on this device converge in three passes.

The output is not a decompilation. It is a map: address ranges belonging to functions, entry points, the globals each one touches, and the call relationships between them. That map is what makes the rest of the analysis possible, and it is what a customer can actually use, because it survives being handed to someone else.

The library code you will meet

A predictable fraction of the image is not the customer’s code. Startup, arithmetic helpers, and the routines that implement the compiler’s runtime conventions appear in nearly every image built with the same toolchain, and they are recognisable by their position and their invocation pattern.

Identifying them early saves a great deal of effort, because library routines are called from everywhere and their presence inflates the apparent complexity of the call graph. Once they are labelled as library code, the remaining graph is the application, and it is usually much smaller than it first appeared.

What the map is good for

Three things, in order of how often they are requested. Locating the code that handles a specific peripheral, which is answered by finding the functions that touch the relevant registers. Establishing what a product does at startup, which is answered by walking from the reset vector. And confirming that a recovered image is complete, which is answered by checking that every vector target lands inside a function that the map knows about.

The last one is a real test rather than a formality. A vector entry pointing at an address that no function claims means either a lost block or a boundary that the analysis missed, and both are worth knowing before the image is delivered. Related work on this family includes the capacity and map arguments in R5F212A7SDFA flash program and the small-device reconstruction case in R5F211A2SP flash memory data, where the same vector-first method applies with far fewer functions to map.

Comments are closed.