Skip to content

coff: keep a global function's name when another unit has a static copy of the same name - #15

Closed
patricksnape wants to merge 3 commits into
openblack:coff_prototypefrom
patricksnape:fold-static-global-alias
Closed

patricksnape wants to merge 3 commits into
openblack:coff_prototypefrom
patricksnape:fold-static-global-alias

Conversation

@patricksnape

Copy link
Copy Markdown

Two units can each define a function with the same name, one static and one global:

// a.cpp: a private helper
static void DrawCircle() { ... }

// b.cpp: a public function with the same name, called from elsewhere
void DrawCircle() { ... }

Both mangle to ?DrawCircle@@YAXXZ. Only b.cpp's copy can be called from other units, so it is the one that has to keep the name. dtk gets this wrong in two ways:

  1. Split. When a name appears twice, dtk keeps it on the copy at the lower address. If that copy is a.cpp's static, b.cpp's global is renamed fn_0x…. Every reference from another unit then points at fn_0x…, which the recompiled b.obj doesn't define, so the link fails with undefined symbol: fn_0x….
    Fix: keep the name on the lowest exported copy.
  2. Fold. If the two bodies are identical, the linker's identical-code folding keeps one, for example a.cpp's, and points b.cpp's global name at it. To describe that, symbols.txt needs the name twice at one address: a global alias and a local function. dtk merges the two into one line, and coff fold treats the static as already defining the name. b.cpp's callers stay undefined.
    Fix: keep the two entries apart, and export the folded name on the static's address.

In bw1-decomp, this is what stops FrontEndSetupGame.cpp from linking. Its code is exact in every version, but 1.00 can't link at all (case 2), and Creature Isle has to leave the static copies unnamed (case 1).

Worked example (Black & White, bw1-decomp)

MPFEAlexDialog.cpp defines two global helpers:

void DrawCircle(uint16_t* image, int x, int y, int radius, uint16_t colour);
ServerLandscapeMap::Point* IsThereAPoint(LHLinkedList<ServerLandscapeMap::Point*>* points, int x, int y);

FrontEndSetupGame.cpp has its own static copies with the same signatures, so the same mangled names: ?DrawCircle@@YAXPAGHHHG@Z and ?IsThereAPoint@@YAPAUPoint@ServerLandscapeMap@@PAV?$LHLinkedList@PAUPoint@ServerLandscapeMap@@@@HH@Z. FrontEndSetupGame comes earlier in the link order, so its static copies sit at lower addresses than AlexDialog's globals.

1. Split: the static copy takes the global name (Creature Isle)

Creature Isle's symbols.txt, with FrontEndSetupGame's statics named:

?IsThereAPoint@@...HH@Z = .text:0x0055D710; // type:function size:0x30 scope:local    <- FrontEndSetupGame (static)
?DrawCircle@@YAXPAGHHHG@Z = .text:0x0055D740; // type:function size:0x90 scope:local   <- FrontEndSetupGame (static)
?DrawCircle@@YAXPAGHHHG@Z = .text:0x006345E0; // type:function size:0x89 scope:global  <- MPFEAlexDialog
?IsThereAPoint@@...HH@Z = .text:0x00634970; // type:function size:0x30 scope:global   <- MPFEAlexDialog

The duplicate-name pass in split_write_coff keeps an exported name at its lowest address, but it takes that minimum over every occurrence, private ones included. The static at 0x0055D740 is lowest, so the global DrawCircle at 0x006345E0 is renamed fn_0x006345e0. A data table in another unit points at it, so the split objects now reference fn_0x006345e0. The recompiled MPFEAlexDialog.obj defines ?DrawCircle@@YAXPAGHHHG@Z, not fn_0x006345e0, and the link fails:

undefined symbol: fn_0x006345e0

The same thing already happens, silently, in 1.10, 1.20 and 1.30, where FrontEndSetupGame's statics are named: AlexDialog's two globals come out of the split as fn_0x0061b880 and fn_0x0061bc10 (1.20). Nothing outside AlexDialog references them there, so the link still succeeds, but the real names are gone.

A static cannot be referenced from another unit, so it has no claim on the exported name. The fix keeps an exported name at its lowest exported address (kept_exported_address). Private copies are still handled by the existing local rules. Today bw1-decomp leaves Creature Isle's two copies unnamed (fn_0055D710, fn_0055D740) so that the global keeps its name.

2. Fold: a global function folded onto a static of the same name (1.00)

1.00 was linked with identical-code folding, and AlexDialog's IsThereAPoint has the same bytes as FrontEndSetupGame's static copy. The linker kept one body, at 0x0051BEA0 in FrontEndSetupGame's range, and AlexDialog's global name points into it. To describe that, symbols.txt needs the same name twice at one address:

?IsThereAPoint@@...HH@Z = .text:0x0051BEA0; // type:label scope:global alias           <- AlexDialog's global, folded here
?IsThereAPoint@@...HH@Z = .text:0x0051BEA0; // type:function size:0x30 scope:local     <- FrontEndSetupGame's own static

Three places in dtk turn those two lines into one:

  • ObjSymbols::add merges a symbol into an existing one with the same name at the same address, so the function is merged into the alias.
  • write_symbols de-duplicates on (name, section, address), so it writes only one of the lines back.
  • coff fold skips an alias whose name the unit already defines. The unit's definition here is its static function, which AlexDialog's reference cannot bind to. The reference stays undefined, and the link fails.

The fix:

  • ObjSymbols::add never merges into an alias.
  • write_symbols keys on alias-ness too.
  • fold sees that the unit's definition of the name is a static function and the alias is a global. It then also defines the name as an external on the static function's address.

With both fixes, FrontEndSetupGame.cpp links in all five versions (it is MatchingFor four of them today) and every executable hash still passes.

Not covered here: the reverse direction, where a unit's own static is folded onto another unit's copy (bw1-decomp's LHLogDebugBreak units).

Tests

cargo test: 40 pass, 5 of them new:

  • a_symbol_does_not_merge_into_an_alias_of_its_own_name (symbols.rs)
  • exports_a_static_function_another_units_global_copy_folded_onto (fold)
  • duplicate_name_tests, three cases (split: which copy keeps the name)

bw1-decomp, all five versions (purge builds):

  • Config unchanged, this dtk instead of v0.0.35: every hash passes and symbols.txt is not rewritten. Matched function counts are the same in every version. The only report change is the two AlexDialog functions above, which get their real names back in 1.10, 1.20 and 1.30 (100% under both names).
  • With FrontEndSetupGame.cpp set to Matching in every version, 1.00's fold lines added (the two from the example, plus two ordinary aliases for other functions folded into the unit), and Creature Isle's statics named: 1.00 and Creature Isle link, and all five executable hashes pass.

🤖 Generated with Claude Code

patricksnape and others added 3 commits November 17, 2026 19:12
… address

The duplicate-name pass kept the lowest-address occurrence of an exported
name and renamed the rest, taking the lowest address over every occurrence.
A unit's static function below another unit's global function of the same
name then cost the global one its name (fn_0x...), and a compiled unit that
defines the real name left references to the placeholder undefined.

Co-Authored-By: Claude <noreply@anthropic.com>
…ame name

With identical-code folding, a unit's global function can be merged onto an
earlier unit's static function that has the same mangled name. The symbols
file then lists the name twice at one address: the static function and a
global alias. The symbol table merged the two lines, the symbols writer
dropped one, and the fold took the unit's static definition as serving the
alias, so the other unit's reference stayed undefined. Keep the alias a
separate symbol and line, and define the name a second time as an external
on the static function.

Co-Authored-By: Claude <noreply@anthropic.com>
Move the choice into kept_exported_address and test it: a private copy at a
lower address does not take the name, exported copies keep the lowest address,
and private copies alone keep none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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