Repository navigation
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
Closed
patricksnape wants to merge 3 commits into
patricksnape wants to merge 3 commits into
Conversation
… 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>
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.
Two units can each define a function with the same name, one
staticand one global:Both mangle to
?DrawCircle@@YAXXZ. Onlyb.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:a.cpp's static,b.cpp's global is renamedfn_0x…. Every reference from another unit then points atfn_0x…, which the recompiledb.objdoesn't define, so the link fails withundefined symbol: fn_0x….Fix: keep the name on the lowest exported copy.
a.cpp's, and pointsb.cpp's global name at it. To describe that,symbols.txtneeds the name twice at one address: a global alias and a local function. dtk merges the two into one line, andcoff foldtreats 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.cppfrom 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.cppdefines two global helpers:FrontEndSetupGame.cpphas its ownstaticcopies with the same signatures, so the same mangled names:?DrawCircle@@YAXPAGHHHG@Zand?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:The duplicate-name pass in
split_write_coffkeeps 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 globalDrawCircleat 0x006345E0 is renamedfn_0x006345e0. A data table in another unit points at it, so the split objects now referencefn_0x006345e0. The recompiledMPFEAlexDialog.objdefines?DrawCircle@@YAXPAGHHHG@Z, notfn_0x006345e0, and the link fails: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_0x0061b880andfn_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
IsThereAPointhas 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.txtneeds the same name twice at one address:Three places in dtk turn those two lines into one:
ObjSymbols::addmerges a symbol into an existing one with the same name at the same address, so the function is merged into the alias.write_symbolsde-duplicates on (name, section, address), so it writes only one of the lines back.coff foldskips 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::addnever merges into an alias.write_symbolskeys on alias-ness too.foldsees 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.cpplinks in all five versions (it isMatchingForfour 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
LHLogDebugBreakunits).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):
symbols.txtis 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).FrontEndSetupGame.cppset toMatchingin 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