Repository navigation
[AUTOMATED] feat(p1): dynrelocs — apply a linked image's dynamic relocations (DIV-84) - #328
Merged
Merged
Conversation
kuna's ELF loader maps the PT_LOAD bytes the *linker* wrote, which is not the image a process runs. Every slot filled by a dynamic relocation (R_*_RELATIVE / GLOB_DAT / JUMP_SLOT) is left at 0 for the run-time loader, and in a PIE -- the default link mode of every current toolchain -- that is the whole .got. A call through such a slot reads a null target and can never resolve, so kuna rendered `(*dat_e0dc8)(...)` for a callee that is a named function in the very same image. `dynrelocs` (default-ON, DIV-84) walks .rela.dyn / .rel.dyn / .rela.plt at load and writes the value the dynamic loader would store: RELATIVE takes the load bias plus the addend; GLOB_DAT/JUMP_SLOT take the symbol address, and ONLY when that symbol is defined in this same image -- an undefined one is an import whose value lives elsewhere, so it is skipped and the existing PLT/import naming path is untouched. x86-64, AArch64, i386 and ARM; RELA and REL (in-place addend) tables both. Applying the relocation is only half of it: .got is SHF_WRITE, so nothing downstream would trust it, and the constant fold of read-only storage is gated program-wide by `option readonly` (default off; turning it on would fold every .rodata read in the program). The narrow warrant is PT_GNU_RELRO -- the linker's own statement that the range is mprotected read-only before main. The written slots RELRO covers are reported both through getReadonly (painting Varnode::readonly) and separately as Architecture::dynreloc_const, where ActionVarnodeProps folds them with global propagation still off. So the blast radius is exactly the relocated RELRO slots, not the program's read-only data. Loader-tier, so the gate is the KUNA_DYNRELOCS env bridge (the image is snapshotted inside `load file`, upstream of every per-function `option`); `--option dynrelocs off` restores the pre-fix bytes exactly. [AUTOMATED] Generated by an agent run. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_016WUuE4zdiEqZFWLL2YEUk9
Records the measured effect of the default-ON flip (unnamed indirect call sites 2,853 -> 560 on a static-PIE Rust binary; zero-collateral on grep/sort/betaflight; faillog and libselinux.so.1 change only to name a global they could not name before) and the second-order finding that `securitycheck` (DIV-82) was measured dead on that binary -- all seven rustc panic helpers are reached through GOT slots, so it stripped 0 functions before and 85 after. Also guards `resolve` against an ET_REL object explicitly, so the function's own contract matches its doc rather than relying on the caller having taken the `relocobjects` branch first. [AUTOMATED] Generated by an agent run. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_016WUuE4zdiEqZFWLL2YEUk9
…l in DIV-84 Independent verification of the DIV-84 numbers reproduced the headline (unnamed indirect call sites 2,860 -> 560, functions carrying one 1,174 -> 276) and the whole-binary cost (129.9 s -> 168.3 s), but also turned up a consequence the row did not record: FakeCrypt's error count goes 2 -> 5. Three functions cross --mode fast's 10 s per-function budget once their calls resolve, and a reader of the row would otherwise conclude nothing was lost. Re-measured in isolation to rule out contention -- the slowdown is deterministic (6.7->18.6, 12.6->20.6, 8.0->12.7 s). The trade is stated plainly: 3 functions lost against 2,293 call sites named, recoverable with `--option dynrelocs off` or a larger per-function budget. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_016WUuE4zdiEqZFWLL2YEUk9
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.
The gap
kuna's ELF loader maps the
PT_LOADbytes the linker wrote, which is not the image a process runs. Every slot filled by a dynamic relocation —R_*_RELATIVE,R_*_GLOB_DAT,R_*_JUMP_SLOT— is left at 0 for the run-time loader to complete, and in a PIE (the default link mode of every current toolchain) that is the whole.gotplus every relocated function pointer in.data.rel.ro.So a call through a pointer the linker itself resolved reads a null target and can never deindirect. kuna printed
(*dat_e0dc8)(...)for a callee it had already named. Zero is not an ambiguity to be judged — it is a byte the run-time image never holds.ET_REL.ofiles were already covered (relocobjects/relocrebase); linked images were not.What this does
New loader-tier option
dynrelocs(on|off, default ON, DIV-84).1. Apply the relocations.
kuna-analysis/src/loader/kuna_dynrelocs.rs (resolve)walks.rela.dyn/.rel.dyn/.rela.pltand writes what the dynamic loader would:RELATIVE→ load bias + addend (kuna maps a linked image at its declared vaddrs, so the bias is 0);GLOB_DAT/JUMP_SLOT→ the symbol address, and only when that symbol is defined in this same image. An undefined one is an import whose value lives in another object; there is nothing to write, the existing PLT/import naming already covers the call, and it is skipped — so that path (DIV-8/DIV-9) is byte-identical, lazy-binding stub contents included.COPY/IRELATIVE/TLS*are not statically knowable and are skipped.Architectures by relocation triple: x86-64, AArch64, i386 and ARM. Both
RELA(explicit addend) andREL(in-place addend, 32-bit ELF) tables. All four are covered by hand-built synthetic-ELF unit tests; x86-64 additionally by a real PIE fixture and by the measurements below. i386 is done, not deferred.2. Make the value foldable — the half that actually turns the call into a name.
.gotisSHF_WRITE, so nothing downstream trusts it, and the constant fold of read-only storage is gated program-wide byoption readonly(readonlypropagate, default off; turning it on would fold every.rodataread in the program — a far larger change than this one, and measurably so).The narrow warrant is
PT_GNU_RELRO: the linker's own statement that the range ismprotected read-only once startup relocation finishes. The written slots RELRO covers are reported twice — throughgetReadonly(paintingVarnode::readonly, as for any read-only section) and separately asObjectLoadImage::dynreloc_const_ranges→Architecture::dynreloc_const→ the per-function handle, whereActionVarnodePropsfolds a read-only varnode inside one of those ranges with global propagation still off.So the blast radius is exactly the relocated RELRO slots, not the program's read-only data. Slots outside RELRO (a
RELATIVEpointer in ordinary.data) are still filled in — that IS the value at process start — but are never declared constant, because the program may legitimately overwrite them.The two halves are useless apart: relocating without the constancy leaves the load unfolded; declaring constancy without relocating would fold the call target to zero.
Loader-tier, so the gate is the
KUNA_DYNRELOCSenv bridge (the image is snapshotted insideload file, upstream of every per-functionoption), set by the CLI on thedecomp_dbgsubprocess and on its own process for the in-process drivers — therelocrebase/relocobjects/i386_pie_pltpattern.--option dynrelocs offrestores the pre-fix bytes exactly.Measurements
Test binary: a 4.9 MB static-PIE Rust executable (1,539
R_X86_64_RELATIVE, 640 of them pointing into.text). Not committable; not committed.1. The showcase —
kuna decompile ./FakeCrypt --addr 0x55300Off vs on, same build, whole diff:
2 unnamed indirect calls → 0, plus one
dat_...argument resolving to a named global. No other line changes.2. Whole-binary —
kuna decompile-all ./FakeCrypt --json3. C collateral (option off vs on, whole-binary
decompile-all)tests/bug-repro/grep(PIE)tests/bug-repro/sort(PIE)tests/bug-repro/faillogtests/bug-repro/libselinux.so.1tests/bug-repro/betaflight_STM32F405.elf(ARM firmware)Function inventories are identical on every one.
grep/sortare PIE and do have.rela.dyn, but their RELATIVE slots are.init_array/.data.rel.roentries that no function reads as a call target, so nothing moves.Every one of the 33 changed functions is an improvement.
faillog:libselinux.so.1— a GOT slot for a defined global now reads its own name, and the pointer temp disappears:No regressions found in any of the 33.
4. Speed (interleaved, never batched)
scripts.pipeline.timeit, showcase function, median of 5, budget +5%)kuna functionson the 4.9 MB PIE — load + discovery only, min of 5decompile-all grep, min of 5decompile-all libselinux.so.1, min of 5decompile-all faillog, min of 5decompile-allon the 4.9 MB PIE, min of 2Reported honestly: the last row is a real cost. It is not the relocation walk — the load-only measurement isolates that at −0.5%, and every binary where nothing new resolves is within noise. It is the price of decompiling 2,293 newly-resolved direct calls (prototype recovery, deindirect restarts) that were previously dead ends. Per-function, resolving the call makes the decompile faster, which is what the sanctioned gate measures.
5.
securitycheck(#325, DIV-82) — it was measured dead on this binaryAll seven rustc panic helpers in this binary are reached only through GOT slots (one
R_X86_64_RELATIVEeach:panic_bounds_check@0xe0cd0,slice_error_fail@0xe0d00, …). Withdynrelocsoff,securitycheckcannot see a single one:securitycheckchangesdynrelocsoffdynrelocsonGates (all four, verbatim)
docs/baseline.jsonanddocs/baseline-stages.jsonare unchanged — the XML<binaryimage>datatest path never reachesbootstrap_from_object, and the fold gate is additionally inert whereverdynreloc_constis empty.Bookkeeping
phases.toml[[settable]]row with all 17 fields (tier = analysis,change_kind = correctness-fix,source_decompiler = kuna— chosen to dodge every pinned bucket intests/stages/kuna-catalog.xml, which therefore needs no edit and forces no stages-baseline re-record).catalog_bytecompat.rs114,kuna_phases/tests.rs114 / tiers (24, 50, 40) /},\n113 / live readers 34,phase_catalog.json114,docs/options.md114.docs/spec/01-program-prep.md§1.2 (the mechanism, two paragraphs, next torelocobjects/relocrebase) + §1.1 env-bridge list;docs/spec/03-ssa-and-simplification.md(theActionVarnodePropsfold exception);docs/spec/00-overview.mdenv-bridge table — which was stale at "Four loader gates" and is corrected to seven (KUNA_RELOCREBASE,KUNA_IFUNCFPRET,KUNA_TYPEDEPTHwere missing).docs/history.mdDIV-84.cet_pie_x86_64(relocations applied, imports untouched, only the RELRO slots constant) +tests/verify_dynrelocs_gate.rsin its own test binary, because the gate is a process-global env var. Notests/stages/*.xml— the loader-tier precedent, which also spares thekuna-base/src/xml.rscorpus-count collision.Known limitation
object's dynamic-relocation walk finds the tables through section headers. An image stripped of its section header table has no reachable.rela.dyn, so the pass is inert there rather than wrong. Reading the tables fromDT_RELA/DT_REL/DT_JMPRELinstead would close that; it is a self-contained follow-up.Also scoped out deliberately: only the loader image is relocated, not the byte view the analysis passes re-parse. So e.g.
.init_arraydiscovery still reads zeroes on a PIE. That is therelocrebase-style "rebased view" shape and belongs in its own PR with its own discovery measurements.🤖 Generated with Claude Code
https://claude.ai/code/session_016WUuE4zdiEqZFWLL2YEUk9