Skip to content

[AUTOMATED] fix(p1): relocobjects - a COFF .obj exposed only its first function (DIV-70) - #286

Merged
mahaloz merged 1 commit into
mainfrom
feat/coff-objects
Aug 12, 2026
Merged

mahaloz merged 1 commit into
mainfrom
feat/coff-objects

Conversation

@mahaloz

@mahaloz mahaloz commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

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_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:

before:  kuna functions combat.obj   ->   1 function   (all 13 collide at 0x0)
after:   kuna functions combat.obj   ->  13 functions at distinct VMAs

The fix

The choice is now per-format rather than per-symptom, behind the existing default-ON relocobjects option (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 .o keeps today's path.
  • ObjectFormat::is_alloc_section — which sections are memory-resident? ELF SHF_ALLOC; COFF Characteristics content 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.rs is renamed reloc_object.rs. Three corrections ride along inside it:

  1. 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 read back signed and added;
  2. COFF ADDR32NB (an RVA, what .pdata/.xdata are built from) resolves against the synthetic image base instead of warning and skipping;
  3. 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).

Separately, the Listing/xref tier declines a synthetically laid-out object. It parses the raw object view, 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_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 already registers, rebased.

Measured

before after
combat.obj (i386 MSVC, 13 COMDAT functions) 1 function, 34 lines of C 13 functions, 1,017 lines
ptx.o — kuna functions 119 entries 93 (phantoms only; decompile-all byte-identical at 26 functions)
sample_aarch64.o — kuna functions 8 entries 6 (drops the 2 duplicate phantoms)
pe_min.obj — kuna functions 3 entries (incl. phantom sub_60) 2 real ones

Every combat.obj function decompiles, with its externs named (__ftol, MapClass::operator, SpotLightClass::SpotLightClass) and its .bss/.rdata references resolved. Whole-object decompile-all 0.23 s → 2.28 s (a 13× larger job).

Linked images are untouched — by construction (relocatable_layout is false for them, so not a byte of the mapped-image path runs differently) and by measurement against a main build: /usr/bin/ls 278 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-.text COFF fixture keeps its names and bodies (its addresses simply rebase from 0x0/0x10), and verify_coff_object passes unchanged.

Tests

New gate verify_coff_comdat over a vendored coff_comdat_i386.obj (three COMDAT .text sections, a .bss, an undefined extern, both COFF relocation shapes, an LNK_REMOVE section, 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 test 675/675 PARITY OK (no re-pin) · make test-stages PARITY OK (433/433) · make rust-test green · make check-spec green · kuna catalog --check green.

Web UI

integrations/web rebuilt: parity.mjs 19/19 native==wasm, glue.mjs/worker.mjs/fnfilter.mjs/auto-mode.mjs/zip.mjs green. The real browser UI (headless Chrome driven over CDP, uploading through the actual file input) lists all 13 combat.obj functions 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.toml option prose (+ regenerated docs/options.md and the catalog byte-compat fixture), DIV-70 in docs/history.md.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DQNaY1pwg8rskYRksC9qbh

…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
mahaloz merged commit 01d867f into main Aug 12, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/coff-objects branch August 12, 2026 12:54
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant