Posts Tagged ‘read arm ic encrypt eeprom’
Read ARM Software: Thumb-2, Literal Pools and Call Graphs
ARM images look easier to reverse than they are. The instruction set is regular, the encoding is documented, and the toolchain support is mature, so a disassembler produces output immediately. The output is frequently wrong in ways that are not obvious, because three features of real ARM code break the assumptions a linear sweep depends on.
The three are: two instruction encodings that can be mixed at function granularity, constant pools embedded in the middle of functions, and calls that go through tables rather than through direct branch instructions.
The mixing problem
ARM cores support a 32-bit encoding and a 16-bit encoding. A function can be written in either, and both can appear in the same image, with the choice recorded in the low bit of the address rather than in the code.
| Transition | Encoded by | Consequence for decoding |
|---|---|---|
| ARM to Thumb | Branch with exchange, or a return to an odd address | The target starts in 16-bit mode |
| Thumb to ARM | Branch with exchange | The target starts in 32-bit mode |
| Function pointer | Low bit of the pointer | The callee’s encoding is in the data |
| Exception return | Status register bit | The resumption encoding may differ |
The third row is why a call graph built from direct branches alone is incomplete. Many real systems dispatch through tables of function pointers, and the table entries carry the encoding bit, which means the disassembler has to consult the data to know how to decode the target.
Literal pools
A constant pool is a block of data placed inside or immediately after a function, loaded with a PC-relative instruction. On architectures with a large addressing range this would be unnecessary; on ARM it is the standard way to load a 32-bit constant cheaply.
| What it looks like | What it is |
|---|---|
| A load from an address a few bytes ahead of the PC | A literal load |
| A block of 32-bit values inside the code | A literal pool |
| Instructions that decode validly after a return | Often pool data |
| A branch over a block of data | A pool skip |
The third row is the failure mode: pool data frequently decodes as valid-looking instructions, so a linear sweep continues through it and produces nonsense that looks like code. The disassembler needs to know where the pool starts and ends, which is derivable from the literal-load offsets but not from the instruction stream alone.
Recovering the function map
The workable sequence builds the map from anchors and validates by structure.
| Step | Method |
|---|---|
| Find the vector table | Fixed location, or a register that points at it |
| Identify the reset handler | First meaningful vector |
| Follow direct branches | Record targets as candidate entries |
| Collect literal pool ranges | From PC-relative load offsets |
| Collect pointer tables | From data regions referenced by code |
| Validate candidates | Alignment, prologue shape, reachability |
The fourth row must be done before the third can be trusted, because a direct branch that lands inside a pool is not a call. On ARM this is common: the pool sits after an unconditional branch, and a misidentified target inside it produces a phantom function.
Prologues exist, unlike on 8-bit parts
One thing ARM gives back is a recognisable function prologue. Most compiled functions begin by pushing a set of registers and, often, adjusting the stack pointer.
| Prologue shape | Meaning |
|---|---|
| Push of several registers including the link register | A normal function call |
| Push of the link register alone | A leaf-ish function that makes calls |
| Subtraction from the stack pointer before any store | A frame with a large local area |
| No push at all | A leaf function, or a tail-called continuation |
The fourth row is the ambiguous case, and it is where a reconstruction can lose a function boundary: a leaf function has no prologue, so the only evidence of its existence is a branch to it.
Indirect calls and the tables behind them
Function pointer tables are the hardest part of the map, because the target is data.
| Table pattern | Recognition |
|---|---|
| Consecutive words in the code address range | A dispatch table |
| Words with the low bit set | Thumb targets |
| A table whose entries are all in one region | A driver or handler table |
| A table indexed by a small integer constant | A state or command dispatch |
The second row is a useful tell: on a mixed image, a table of entries with odd addresses is unambiguously a Thumb function table, which both confirms the table and identifies the encoding of its targets.
Where the reconstruction goes from the map
Once functions are bounded, the reconstruction work is the general problem of source recovery, with ARM-specific severity: a large register file and aggressive scheduling widen the gap between the source and the binary.
| Loss | Severity on ARM |
|---|---|
| Register allocation | High; many registers, many choices |
| Inlining | High; compilers inline aggressively at -O2 |
| Instruction scheduling | High; dual-issue and pipeline filling |
| Macro expansion | High; hardware access idioms are everywhere |
The comparison with other architectures is useful and is treated in cracking microcontroller source code, where the four losses are set out generally.
The validation that closes the loop
A function map can be checked without the original source, and the checks are cheap.
| Check | Detects |
|---|---|
| Every branch target is inside a known function | Missed entries |
| Every function is reachable from the vector table or a table | Phantom entries |
| No instruction overlaps a pool | Pool range errors |
| Recompiled reconstruction matches behaviour | Reconstruction errors |
The third row is a strong internal consistency test. If the map is right, no instruction should reference an address inside a declared pool except through a literal load.
Where the adjacent steps are
The image itself usually comes from outside the chip, and reading ARM firmware explains why. The two-word validity check that should precede any disassembly is in reading ARM Cortex-M firmware. Rebuilding a loadable image around the binary is in extracting an ARM embedded program.
Services at this boundary
Work covers function map recovery with pool and table handling, mixed-encoding disassembly, call graph reconstruction including indirect calls, and reconstruction at an agreed level. It does not cover recovering original identifiers, and it does not cover producing a byte-identical rebuild.
The practical statement is that the instruction set is the easy part of ARM; the structure is what takes the time.