Repository navigation
[AUTOMATED] fix(p1): relocobjects - a COFF .obj exposed only its first function (DIV-70) - #286
Merged
Merged
Conversation
…t function (DIV-70) A pre-link object stores every section at the section-relative VMA the linker will later reassign -- i.e. all of them at 0. The ELF arm of that was already handled (DIV-8: an ET_REL has no program headers at all, so the faithful loader mapped zero bytes and the synthetic layout was the fix), but the COFF arm was invisible because a COFF file *does* answer `file.segments()`: the loader mapped every segment on top of the others at address 0, `find_section` returned whichever sorted first, and the funcsym walk -- deduped by address -- kept the first symbol and dropped the rest. With one `.text` per object that is survivable, which is why the `coff_obj.obj` fixture never caught it. With function-level linking (MSVC `/Gy`, clang `-ffunction-sections`, one COMDAT `.text` per function -- the default for real game and engine builds) every function but one disappears: on a 13-function Command & Conquer `combat.obj`, `kuna functions` reported 1. The choice is now per-format rather than per-symptom. `ObjectFormat:: relocatable_layout` decides whether a file needs the synthetic layout (ELF: relocatable *and* no program headers, the historical gate verbatim; COFF: relocatable, full stop; PE/Mach-O: never, so a Mach-O `.o` keeps today's path), and `ObjectFormat::is_alloc_section` answers which sections are memory-resident (ELF `SHF_ALLOC`; COFF `Characteristics` content bits minus the link-time-only `.drectve` / `LNK_REMOVE` / discardable `.debug$S` sections). Three corrections ride along inside the now-shared layout: - a REL-style relocation table stores its addend in the field being patched (COFF `DIR32`/`REL32`, and 32-bit ELF `.rel.*`), which the RELA-only math had been discarding -- the in-place value is now read back signed and added; - COFF `ADDR32NB` (an RVA, what `.pdata`/`.xdata` are built from) resolves against the synthetic image base instead of warning and skipping; - an empty section no longer takes a load VMA, which would otherwise alias the next section's address (a COFF object leads with empty `.text`/`.data`/`.bss` placeholders). The Listing/xref tier now declines a synthetically laid-out object: it parses the raw `object` view, so its addresses are pre-link ones in a different space than the Listing decodes in, and seeding it with them invented a phantom entry beside each real function (`sub_60` for a symbol the loader placed at 0x400060). Nothing is lost -- a pre-link object always carries the symbol table the linker is about to consume, which the loader registers, rebased. Measured: combat.obj 1 -> 13 functions, 34 -> 1,017 lines of C, externs named and .bss/.rdata references resolved; ptx.o `kuna functions` 119 -> 93 entries (phantoms only) with decompile-all byte-identical at 26 functions; sample_ aarch64.o loses its 2 duplicate phantoms. Linked images are untouched by construction and by measurement: /usr/bin/ls (278 functions) and a 3,015- function PE decompile byte-identical vs main. Gates: make test 675/675 PARITY OK, make test-stages PARITY OK (433/433), make rust-test green, make check-spec green. New gate `verify_coff_comdat` over a vendored `coff_comdat_i386.obj`. Web: `integrations/web` parity/glue/worker suites green, and the real browser UI (headless Chrome over CDP) lists all 13 functions of combat.obj and decompiles a non-first COMDAT on click. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01DQNaY1pwg8rskYRksC9qbh
mahaloz
added a commit
that referenced
this pull request
Aug 17, 2026
…ocatable objects (DIV-79) (#309) [AUTOMATED] Fixes GH-289. `relocobjects` (DIV-8/DIV-70) lays an ELF ET_REL `.o` / COFF `.obj` out synthetically above RELOC_BASE, so every address the engine holds is post-layout. The load-time analysis passes never learned that: each re-parses the same file through its own `object::File` and computes pre-link, section-relative addresses. Both spaces then landed in one inventory -- `kuna functions ptx.o` reported 95 entries (26 real plus 27 phantoms at 0x0/0x20/0x34/...) while `decompile-all` reported the correct 26, breaking the DIV-68 agreement invariant. Silently, string literals and DWARF-named globals were keyed to pre-link addresses and never attached to the loaded image. The phantoms are not a missing base address: 26 are `.eh_frame` FDE `initial_location` fields whose PC-relative relocation the linker has not applied (an unrelocated PC-relative field reads back as its own section offset), and 1 is DWARF, where `DW_AT_low_pc` reads 0 for every subprogram -- as does every `DW_FORM_strp`, so the whole object's DWARF collapsed onto one function named after `.debug_str`+0. #286 fixed the Listing/xref half by declining. Declining here would throw real information away (a -g `.o` carries full DWARF), so this rebases instead -- and rebases the analyzer tier's INPUT rather than each output fact, because a fact is a bare u64 by the time it reaches AnalysisOutput and every section of a relocatable object sits at address 0, which makes `.text`+0x20 and `.rodata`+0x20 the same number. loader/kuna_relocrebase.rs re-presents the object: each laid-out section carries the loader's own relocated bytes and its load VMA (ELF sh_addr, COFF VirtualAddress); each unlaid `.debug_*` section has its relocations applied here (a target in a laid-out section resolves to its load VMA, a debug-to-debug target to its own section-relative offset); and each ELF symbol defined in a laid-out section has st_value shifted by ITS OWN section's delta -- the layout is non-contiguous, so there is no single global offset. A COFF symbol needs no shift (`object` reports it as VirtualAddress + value). A fact that still lands in no laid-out section is dropped, not passed through unrebased; a NoReturnFact is the one exception, kept with its address zeroed so the commit's name fallback still fires. Measured on the in-repo fixtures: ptx.o `functions` 95 -> 68 with zero entries below RELOC_BASE and `decompile-all` still 26 (the two now agree), recognized string literals 2 -> 41, DWARF parameter names and types applied (`char to_uchar(char ch)`, `ignore_case`/`folded_chars` instead of `dat_403520`/`0x403160`); arm_thumb_le32.o goes from decompiling nothing to both functions (the ARM TMode paints now land); msvc_mangled.obj recovers its `Bar *this`. Every executable in tests/bug-repro/ is byte-identical, and `--option relocrebase off` is byte-identical to origin/main on all 14 audited targets. Loader-tier gate, so it uses the i386_pie_plt/relocobjects env bridge (KUNA_RELOCREBASE): the analyzer tier runs inside `load file`, upstream of every per-function option, and it cannot be deferred to the commit because it changes what every pass reads. Claude-Session: https://claude.ai/code/session_01VBn8vgwoHqPFnnV4ZMApfq Co-authored-by: Claude Fable 5 <[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.
The defect
A pre-link object stores every section at the section-relative VMA the linker will later reassign — i.e. all of them at 0. The ELF arm of that was already handled (DIV-8: an
ET_RELhas no program headers at all, so the faithful loader mapped zero bytes and the synthetic layout was the fix), but the COFF arm was invisible because a COFF file does answerfile.segments(): the loader mapped every segment on top of the others at address 0,find_sectionreturned whichever sorted first, and the funcsym walk — deduped by address — kept the first symbol and dropped the rest.With one
.textper object that is survivable, which is why thecoff_obj.objfixture never caught it. With function-level linking (MSVC/Gy, clang-ffunction-sections, one COMDAT.textper function — the default for real game and engine builds) every function but one disappears:The fix
The choice is now per-format rather than per-symptom, behind the existing default-ON
relocobjectsoption (no new flag):ObjectFormat::relocatable_layout— does this file need the synthetic layout? ELF: relocatable and no program headers (the historical gate, moved verbatim). COFF: relocatable, full stop. PE/Mach-O: never, so a Mach-O.okeeps today's path.ObjectFormat::is_alloc_section— which sections are memory-resident? ELFSHF_ALLOC; COFFCharacteristicscontent bits minus the link-time-only sections (.drectve/LNK_INFO,LNK_REMOVE, discardable.debug$S/.debug$T).Everything below those two questions is now shared, so
elf_reloc.rsis renamedreloc_object.rs. Three corrections ride along inside it:DIR32/REL32, and 32-bit ELF.rel.*), which the RELA-only math had been discarding — the in-place value is read back signed and added;ADDR32NB(an RVA, what.pdata/.xdataare built from) resolves against the synthetic image base instead of warning and skipping;.text/.data/.bssplaceholders).Separately, the Listing/xref tier declines a synthetically laid-out object. It parses the raw
objectview, so its addresses are pre-link ones — a different address space than the Listing decodes in — and seeding the walk with them invented a phantom entry beside each real function (sub_60for a symbol the loader placed at0x400060). Nothing is lost: a pre-link object always carries the symbol table the linker is about to consume, which the loader already registers, rebased.Measured
combat.obj(i386 MSVC, 13 COMDAT functions)ptx.o—kuna functionsdecompile-allbyte-identical at 26 functions)sample_aarch64.o—kuna functionspe_min.obj—kuna functionssub_60)Every
combat.objfunction decompiles, with its externs named (__ftol,MapClass::operator,SpotLightClass::SpotLightClass) and its.bss/.rdatareferences resolved. Whole-objectdecompile-all0.23 s → 2.28 s (a 13× larger job).Linked images are untouched — by construction (
relocatable_layoutis false for them, so not a byte of the mapped-image path runs differently) and by measurement against amainbuild:/usr/bin/ls278 functions and a 3,015-function PE both byte-identical; the ELF/Mach-O object fixtures (ptx.o,fid_lib_x86_64.o,macho_min.o) byte-identical too. The existing single-.textCOFF fixture keeps its names and bodies (its addresses simply rebase from0x0/0x10), andverify_coff_objectpasses unchanged.Tests
New gate
verify_coff_comdatover a vendoredcoff_comdat_i386.obj(three COMDAT.textsections, a.bss, an undefined extern, both COFF relocation shapes, anLNK_REMOVEsection, and the empty placeholder sections): asserts the object really does report every section at VMA 0, that all three functions survive at distinct rebased VMAs, and that a non-first COMDAT function decompiles its real body.Gates:
make test675/675 PARITY OK (no re-pin) ·make test-stagesPARITY OK (433/433) ·make rust-testgreen ·make check-specgreen ·kuna catalog --checkgreen.Web UI
integrations/webrebuilt:parity.mjs19/19 native==wasm,glue.mjs/worker.mjs/fnfilter.mjs/auto-mode.mjs/zip.mjsgreen. The real browser UI (headless Chrome driven over CDP, uploading through the actual file input) lists all 13combat.objfunctions in the sidebar and decompiles a non-first COMDAT (Wide_Area_Damage) on click.Known follow-up (pre-existing, filed separately)
The load-time analysis passes (DWARF, the byte-pattern scan, strings) still read the same pre-link addresses, so an object built with debug info keeps a few phantom low-address entries in
kuna functions(ptx.o's remaining 67). That is unchanged by this PR — it is the ELF arm's existing behavior — and the fix is to hand those passes the section→VMA map rather than to decline; tracked in #289.Docs:
docs/spec/01-program-prep.md+00-overview.md,phases.tomloption prose (+ regenerateddocs/options.mdand the catalog byte-compat fixture), DIV-70 indocs/history.md.🤖 Generated with Claude Code
https://claude.ai/code/session_01DQNaY1pwg8rskYRksC9qbh