Repository navigation
feat(analysis): stripped ARM Cortex-M Thumb function discovery - #168
Merged
Merged
Conversation
On a stripped bare-metal Cortex-M image kuna decoded everything in A32 (the
default TMode=0 from ARMt.pspec) and found only one function: nothing painted
Thumb (no $t/STT_FUNC markers, no libc idiom, and a tiny image yields no
prologue seeds) and the ELF entry seed was the raw odd (undecodable) address.
Three additive, ARM-gated changes in kuna-analysis fix it (always-on, no
settable, no DIV: the trigger is a confirmed Cortex-M hardware vector table, so
this is a strict, closer-to-Ghidra discovery-correctness fix, a no-op on x86-64
and on any ARM object without the vector-table signature):
(A) Mask the odd ARM e_entry LSB in collect_entries oracle 1 (the Thumb function
lives at the even VMA). Also unlocks the reset->main call tree.
(B) New oracle cortexm_vector_entries: detect the vector table empirically
(word[0] an SRAM stack pointer 0x2000_0000..=0x3FFF_FFFF, word[1] == e_entry),
then harvest the odd Thumb handler pointers (LSB-masked) as function-start
seeds.
(C) New cortexm_thumb_paints: region-paint TMode=1 across every executable
section once a table is confirmed (ARMv6/7/8-M is Thumb-only; a Thumb BL does
not globalset the callee mode, so main needs the region paint). Wired into
both the analysis commit path (EntryDiscoveryPass::run) and the Listing walk's
ContextPainter.
Results: libopencm3 button 1 -> 31 functions (main @0x8000204 now decodes as a
real Thumb body, not A32 garbage); betaflight STM32F405 ~1470 -> 1830. x86-64
(coreutils/ptx) byte-identical (257). Gates: make test 675/675 PARITY OK;
test-stages + rust-test have no NEW failures (the 3 pre-existing CATALOG #6/#7 +
ghangr-iteexpr failures are identical on pristine main); check-spec green.
Updated the collect_entries_crossarch_includes_main ARM expectation (0x3dd ->
0x3dc), added the cortexm_vector_harvest unit test, and spec section 1.5.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_0197dWa5QYabZofMfLDC1YP4
mahaloz
added a commit
that referenced
this pull request
Jul 30, 2026
A whole-binary run reported the same function several times -- once per name the entry carried, and on ARM once more per odd-address Thumb twin. The in-repo arm_thumb_linked_le32 fixture holds exactly two functions and decompile-all emitted six records for them; decompile-project exported a .c with a duplicate definition and two functions that do not exist. Three producers, all upstream of one enumeration: 1. commit_analysis_output guards the symbol-table insert with find_function but calls register_symbol unconditionally, and register_symbol retains by NAME -- so an already-named entry gains a second record under the generated sub_<addr>. FID/PDB/objc renames leave their old placeholder behind the same way. 2. existing_function_addrs recorded the raw sym.address(). On 32-bit ARM the Thumb mode bit lives IN st_value (the fixture records compute at 0x100b9, _start at 0x100d7, while both functions start at the even VMA), so the odd address was seeded as a function start and re-emitted by funcdisc_recursive. 0x100b9 is not an instruction boundary, so it decompiled to a bogus empty body. The same defect broke the funcsym-skip: PR #168 masks the e_entry candidate but compared it against this unmasked set, which is why kuna functions duplicated even with the Listing off. 3. A debug-info pass emits a name the linker symbol table lacks (macho_dwarf.o carries _l0+first_byte at 0x0). That one is information. ConsoleProgram::function_entries_canonical now yields one record per entry address, keeping the most informative name and carrying the rest as aliases; thumb_masked folds the mode bit out of the symbol stream at the source, so no phantom is seeded, decoded or decompiled. Name preference ranks a real symbol above a synthesized _INIT_<i>/_FINI_<i>/_DT_INIT/_DT_FINI table name, which outranks a sub_/func_/FUN_/LAB_ placeholder; ties prefer the unprefixed spelling, then the shorter name, then lexicographic order. find_entry_by_name searches aliases, so --functions sub_100b8 still selects compute. All four surfaces share it -- decompile-all, functions, decompile-project and kuna-wasm. The aliases array is additive in every JSON document. --addr additionally tolerates an odd ARM address, folding onto the entry only when the enumeration already knows it; a genuinely odd x86-64 entry is untouched. No --option: emitting one function several times is wrong output, not a judgment call, and there is no driver-tier option machinery. Recorded as DIV-32. No catalog counts, no options.md regen, no baseline re-pin. Measured: arm_thumb_linked_le32 6->2 entries, fmt_arm 32->14, fmt_aarch64 28->14, fmt_riscv64 17->9; x86-64 and libselinux.so.1 (621) unchanged. Every address removed is an odd ARM phantom, none gained, and 713/713 surviving functions decompile to byte-identical code with no reported name that did not already exist at that address. arm_decompile_all_defaults_funcstart_patterns_on moves to entrymain_arm: it asserted default > funcstart_patterns off on the two-function ARM fixture, where the extra entries it counted WERE the duplicates. On entrymain_arm the pass finds three functions nothing else does (10 vs 13), so the assertion is meaningful again. Fixes #197. Claude-Session: https://claude.ai/code/session_01279bXJ6UBQdhfHE4tUQTKg Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes catastrophic under-discovery on stripped ARM Cortex-M Thumb firmware. On these bare-metal, fully-stripped binaries kuna was decoding the whole (Thumb-only) image as A32 and discovering almost nothing — e.g.
libopencm3/button.elfyielded 1 function (an empty reset handler) vs ida's 29.Root cause
kuna paints ARM
TMode=Thumb only from$t/STT_FUNC markers (absent when stripped) and picks the A32-defaultARM:LE:32:v8language (TMode=0). Big firmware (betaflight) escapes because its 468funcstart_patternsprologue seeds let SLEIGHglobalsetpropagate Thumb through the call graph — but a tiny image yields 0 seeds, so nothing bootstraps Thumb, and the ELF entry is seeded at its raw odd (Thumb-bit) address, which is undecodable.Fix (three additive, ARM-gated changes)
collect_entriesmasks the odd ARM entry LSB to its even decode VMA.cortexm_vector_entries: detect the Cortex-M vector table empirically (word[0]an SRAM stack pointer +word[1] == e_entry) and harvest its Thumb handler pointers as function-start seeds.cortexm_thumb_paints: once a vector table confirms Cortex-M, region-paintTMode=1across the executable section (ARMv6/7/8-M is Thumb-only). Wired into both the engine-commit path and the recursive-descent walk'sContextPainter, so recursively-discovered targets likemaindecode as Thumb too.Always-on, no settable / no DIV: the trigger is a confirmed Cortex-M hardware vector table, so it fires only on stripped bare-metal firmware — a discovery-correctness improvement toward Ghidra, a verified no-op on x86-64 and on symbol-bearing/hosted ARM.
Verification
button.elf: 1 → 31 functions;main@0x8000204 decodes with a real Thumb body.betaflight_STM32F405.elf: ~1470 → 1830 (ghidra ~1822).coreutils/ptx: 257, byte-identical.make test675/675,make test-stages261/261,make rust-testgreen,make check-specOK.Files:
analyzers/entry/mod.rs,listing/context.rs,docs/spec/01-program-prep.md,docs/PROGRESS.md.🤖 Generated with Claude Code