fix(mods): code modules made for the console load as the console loads them #3300
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!3300
Loading…
Reference in a new issue
No description provided.
Delete branch "llun/Longshot:fix/mod-modules-console-order"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Mods made for the console or an emulator that bring their own code module (
exefs/subsdk1tosubsdk9, beside an.ipsformain) did not work. kuribox's Super Hero Boost (OOT-SuperHeroBoostMod,MIT) is one: stamina, sprint, an extended HUD, a button remap and more, all in a
subsdk9. On v3.1.1, which loadedno code module, players who installed it had a boot crash (#81's crash-bot signature
e2587375ba1d: the patch'sfirst hook sends the first file open into
sdk,nn::crypto's AES, guest0x1). Onmainthe build leaves it outinstead, so it does nothing. Three things stood in the way, each a place where Longshot was stricter than the
console's loader (a fourth rule comes with the first):
mod's
subsdk9loads right after the game'ssubsdk0, beforesdk. A mod links its module at that address, andits patch in
mainbranches to it by a fixed distance.mainplaced the module aftersdk, so those brancheslanded in
sdk. Now a mod'ssubsdk<n>takes the place its file name has in the loader's order, andsdkmoves upby the modules' room (
image::build::layout,take_modules). With no mod module the game's layout is the same asbefore; a module with relocations works at either place. A mod's
subsdk0loads after the game's, not in its place(the console would replace the game's).
zeroes MOD0's bss inside that. Super Hero Boost's MOD0 bss ends 0x20 past the header's count (its fields count from
the module, not from MOD0), inside the last page: fine on the console, refused on
main("the bss ... is not insidethe module's ... bytes"). Now the room is counted in pages (
Nso::read_tables); a bss past the page is stillrefused.
blatf_833e98f0+0x1c4at its quick select.That
blis whereitems.shortcutConfirm(the mod API's custom items) is hooked: ajumpsite before the call,whose signature takes the call with any target (
94000000/fc000000). The site is still found and still runs, butmainrefused the mod's whole patch file for it. Now a site's signature and guard words are needed as their masksread them, and the word a site is hooked at may change in one case only: a call its signature takes with any
target, where the site lets it run (
before) or skips that call alone (ajumpto the next instruction).Whichever function the call reaches, the site runs or skips it whole. Anything else there stays refused: a branch
(a jump site's own jump would pass its new place, as at
cloth.sphere), a call a jump skips more than, a site thatreplaces the instruction, narrows its branch or runs after its call, and a jump's target
(
build-driver'sclashes::needs,stopped).subsdk9twice) cannot both have it at that name's place, where theirpatches branch. The mod lower in the list wins, as for patch bytes and RomFS files; the other's module is left out
with why, and its mod's code patches with it, since they would branch into the winner's code
(
game_files::modules::one_of_a_name). Eden differs: its layered exefs takes the first mod's file in name orderand still applies the other mod's patches over it.
These are general rules: nothing names Super Hero Boost. Any console mod with a fixed-address code module, a bss in its
last page, or a call it retargets under a site placed before it, is helped the same way. #2497's save-profiles mod
ships a
subsdk9and an.ipstoo and may be the same case (not tested: its files aren't on this PC).How to see it on
main: put the mod'sexefs/subsdk9andE0F3CCFAF50065AE4EA01EB5AEB13B1A45DA3348.ipsin aprofile's
mods/zz-Super-Hero-Boost/exefs/, switch it on and accept its code, build. The build log saysexefs: refused ... .ips (it changes f_833e98f0+0x1c4, ... items.shortcutConfirm ...)andexefs: left out ... subsdk9 (... the bss 0x20020..0x8f2750 is not inside the module's 0x8f2730 bytes), and the gamehas no stamina. Fix only those two and the module lands after
sdk, where the patch's branches don't reach it.Read first:
builder/crates/image/src/build.rs(load_slot,take_modules,layout), thenbuilder/crates/build-driver/src/clashes.rs(needs), thenbuilder/crates/game-files/src/nso.rs(read_tables).Docs:
docs/modding/unsafe.mdanddocs/dev/reference/mods.mdsay where a code module loads.See #2497
See #81
Testing
Tested: Windows 11 (build 26300), RTX 5080, driver 621.47, headless through the PC's job queue. The mod's
module and patch built from kuribox's sources at 0.12.1 (
6f2602b), the release's standalone variant (the samefeatures and link address), with this PC's llvm-mingw clang; its build verified all 29 of its hook sites against this
game's
main. A test profile with the mod as an emulator user has it (mods/zz-Super-Hero-Boost/exefs/), switched onand its code accepted in the builder's window, built with this branch's builder:
exefs: module local/zz-Super-Hero-Boost/exefs/subsdk9,exefs: code ... .ips (192 bytes),image: 5 modules.castle-gate, A held with the left stick forward for 7 s, then let go: the mod's staminaring shows beside Link and drains while he sprints (picture below). Read from the game's memory through the
bridge (
HOS_CONTROL_MEM=ro): the mod's settings blockSHBCFG01at its console address (main + 0x4F9D000), itsinput hooks' counters rising every frame, its sprint flag 0 standing, 1 sprinting, 0 after; its stamina 75.0 full,
62.8, 37.6, 12.3 while sprinting, 75.0 again after.
main's game (the shared base, which is whatmainbuilds with this mod: its code left out): noring, and Link covers less ground in the same frames.
kokiri-startas well: in play, 60 fps, no crash.d22aa8d61's builder): the build takes the patch and the module; the sprint flag0, 1, 0 and the stamina 75.0, 25.4, 75.0; the menu opens and its cursor moves; 59.6 fps; no FATAL or exception.
-andR3together, sent through the bridge): it opens and is drawn over the game, itscursor moves (picture below). The menu paints itself by wrapping three NVN entry points in the game's table and
copying its picture onto the frame's HDR buffer; on Longshot its counters show the picture made (704x360,
R11G11B10F) and copied every frame. It opens only while the mod's own item-button HUD is on (
button_mappingandhud_custom_itemsin its settings block), which its factory settings leave off and its app turns on: with them offit sees the buttons (
0x820) but never takes the game as in play, the mod's rule, not Longshot's. This run had bothset in the test copy, as the app writes them.
The checks, on Windows (llvm-mingw, gnullvm Rust): each fix's test fails on
mainfor the bug's reason and passes here(
a_mods_code_module_loads_where_the_console_loads_it,a_bss_inside_the_modules_last_page_is_read,a_call_retargeted_where_the_site_runs_or_skips_it_whole_is_no_clash, an invented site's words; andtwo_mods_with_a_module_of_one_name_keep_the_later, new with the review);cargo test -p image -p game-files -p build-driver,clippy
-D warningsand rustfmt on the changed crates.mise run test -- --jobs 12on this branch over0b284cc76: 899 passed, 2 failed, 55 skipped.test_lifterstops atffi-gen'sthe_repositorys_files_are_current(host/game_host.h'srx_menu_openhas no doc comment: it fails onmaintoo, left out in #3294); its rustfmt and clippy steps pass, and as cargo stops atffi-gen,cargo test -p image -p game-files -p build-driverran on its own after the rebase: all pass.test_sync_watch_eventtimed out under the suite's load and passes alone (3.4 s).Still to test:
exefs/subsdk9and.ips) and sprint, expecting the stamina ring and nocrash, as on Windows (by hand: the mod's files are not in the repository).
Measurements
Verdict: no cost for a game without a mod's code module (its layout is unchanged); with Super Hero Boost the
game holds 60 fps and its build takes as long as on
main, where the mod is left out.Setup: Windows 11 (26300), RTX 5080, driver 621.47; the test profile's builds over this PC's warm store; the game
headless from
castle-gate, the status's fps read twice in play. Observed, not interleaved timed runs: a build's timehere is the store's, and the fps is the game's 60 cap.
8c0beeec5(the mod left out)deb7b7612(the mod built in)The numbers still hold on main
0b284cc76: it adds only #3294 over8c0beeec5, which changes checks, tests and the fog rows' label in the menu, nothing the build, the image or the lift does. Each later build of a profile with such a mod reuses itssdkunits from the store (the item-HUD rebuild: 47 s).Risks
sdk: its first build after this changecompiles
sdkagain (minutes, once; switching such a mod already needed a build). A module with relocations isplaced at another address than before; it is relocated as before.
sdk's, as on theconsole (it started after on
main). Super Hero Boost's module has none.beforeorjumpsite'sinstruction or a signature or guard word; the site is still found, and the lift still stops on one it can't place.
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.tests/that reads changed source is updated, not weakened. (The checks that read the image crate,test_re,test_doc_commands,test_dev_ab,test_dev_update,test_options_path,test_framelog_sink, passunchanged: none reads the functions this changes.)
mainand here, for a change that shows on screen.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Credit: Super Hero Boost is kuribox's work (MIT); nothing of it is in this PR.
🤖 Generated with Claude Code
The system's loader gives a module its segments' end plus the header's bss size in whole pages and zeroes MOD0's bss there. Super Hero Boost's subsdk9 names a MOD0 bss 0x20 past the header's count, inside the page: it loads on the console and in the emulators, and main refuses it ("the bss ... is not inside the module's ... bytes"). Co-Authored-By: Claude Opus 5.5 <[email protected]>Tested in game: Windows 11 (26300), RTX 5080, driver 621.47, headless through the job queue. kuribox's Super Hero Boost (built from its 0.12.1 sources, the release's standalone variant) in a test profile as an emulator user has it (exefs/subsdk9 + the .ips), code accepted: the build takes the module and the patch (image: 5 modules); from castle-gate, A held with the stick forward: the stamina ring shows and drains, the mod's sprint flag and stamina read from memory follow it (75.0 -> 12.3 -> 75.0), 60 fps, no crash; its in-game menu (- and R3) opens and draws over the game with its item HUD on. Same spot on main: no ring, Link slower. kokiri-start: in play, 60 fps.
Pictures from the runs in Testing (Windows 11, RTX 5080, castle-gate, the same frame after A held with the stick forward).
main (the shared base: the game main builds with this mod, its code left out): no stamina ring.

This PR, with kuribox's Super Hero Boost: the mod's stamina ring beside Link, draining; Link further out (its sprint is 125%).

This PR, the mod's in-game menu (- and R3, then Down), with its item HUD on in the test copy:

Possibly related, for context:
Measurements: the base main moved to
0b284cc76(#3294: checks, tests and a menu label only); the numbers still hold, said in the description.WIP: fix(mods): code modules made for the console load as the console loads themto fix(mods): code modules made for the console load as the console loads them@review
Recommendation: Request changes. The hook clash check can accept a patch that changes the behavior a native hook replaces, and the new test commits instructions copied from the game. The intended mod works in the reported Windows run, but these problems need edits before merge.
The change places mod code modules before the SDK, accepts BSS within the last allocated page, and permits some patches at hook sites. It enables the tested mod, but the broader clash rule can produce incorrect game behavior.
Blocking (3): must change before merging.
builder/crates/build-driver/src/clashes.rs:43: A jump site's masked instruction is not always independent of the native hook's behavior. (more on the line) Do: Keep jump-site instructions exact unless the site explicitly establishes that changing the instruction is safe, or otherwise validate the changed target against the hook's semantics. Add a test for a retargeted jump whose callback takes the skip path.builder/crates/build-driver/src/clashes.rs:183: The new test copies a sequence of the game's instructions into another committed file. (more on the line) Do: Use invented instruction words, a synthetic address, and a synthetic site to exercise the same masked-call behavior.builder/crates/image/src/build.rs:184: Two enabled mods with the same subsdk name are both placed, although their fixed-address patches can target only the first module's console slot. (more on the line) Do: Resolve duplicate ExeFS module names according to the mod overlay precedence, mark the displaced module and its incompatible patch clearly, and test two enabled mods using the same slot.@fixcan do L1, L2 and L3.Questions (1) (not confirmed from the code; they don't hold the recommendation)
builder/crates/build-driver/src/clashes.rs:198: Is there a current patch-refusal failure this test misses?Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes for the tested mod: the reported Windows run built its module and patch, showed the stamina ring, and observed stamina drain and recover. Linux and the save-profiles mod remain untested.
Other systems: Module ordering also changes symbol resolution and initialization order for builds with code mods. Hook clash detection affects existing native game hooks as well as the tested mod; the reported run does not cover every such hook.
Switches: None added, changed, or removed.
Reviewed:
deb7b7612dagainst0b284cc767: Read the repository rules, PR template, commit messages, diff, changed Rust code and documentation, and relevant site definitions and resolver code. Skimmed unrelated tests and generated material; the linked screenshots were not available in the closed network.; nothing run (a code review).Checked and fine:
Testing: No commands from the project were run. The author reports game-free crate tests and an in-game Windows run; Linux remains to be tested, and CI is still running.
Overlaps and merge order: None found in the supplied review context.
Automated code review by BeanBot (gpt-6.1-terra), asked for by BeanBot, as llun took it out of WIP. Nothing was run; a maintainer decides.
@ -13,0 +40,4 @@masked(entry + (g.offset / 4) as i64, &g.signature);}if let Some(off) = c.offset {let k = entry + if c.form.at_offset() { (off / 4) as i64 } else { 0 };L1 (blocking): A jump site's masked instruction is not always independent of the native hook's behavior. Retargeting its branch can change the path that the hook substitutes.
When: At cloth.sphere, the hook runs before a masked unconditional branch into the original record loop. A patch can retarget that branch and pass this check; when the native callback returns nonzero, the jump site instead skips to its fixed original destination, bypassing the mod's new target.
Do: Keep jump-site instructions exact unless the site explicitly establishes that changing the instruction is safe, or otherwise validate the changed target against the hook's semantics. Add a test for a retargeted jump whose callback takes the skip path.
@ -130,0 +180,4 @@wrapper: None,// (items.shortcutConfirm's: four moves, any call, a load)signature: Some(sites::Signature::parse("52800023 52800024 2a1903e2 aa1503e1 94000000/fc000000 f9403668").unwrap(),L2 (blocking): The new test copies a sequence of the game's instructions into another committed file. The repository rule prohibits committing game code or material made from the game.
When: The test identifies these words as the real shortcutConfirm signature and repeats the instruction sequence from the game's site table.
Do: Use invented instruction words, a synthetic address, and a synthetic site to exercise the same masked-call behavior.
@ -146,0 +181,4 @@let mut modules = Vec::with_capacity(game.len() + mods.len());let mut mods = mods.into_iter().peekable();for g in game {while let Some(m) = mods.next_if(|m| load_slot(&m.name) < load_slot(&g.name)) {L3 (blocking): Two enabled mods with the same subsdk name are both placed, although their fixed-address patches can target only the first module's console slot.
When: If two console mods each supply subsdk9 linked for the same slot and each patches main to call its own module, layout puts the second module after the first. The second mod's fixed branch still reaches the first module at that offset, potentially executing unrelated code or crashing.
Do: Resolve duplicate ExeFS module names according to the mod overlay precedence, mark the displaced module and its incompatible patch clearly, and test two enabled mods using the same slot.
Thanks, all three made (
19be0a53d,59382510d,d22aa8d61), and the in-game run done again on them (Testing).before) or skips that call alone (ajumpwhosetois the next instruction). Whichever function the call reaches, the site runs or skips it whole. A branch there, a call a jump skips more than, and every other form stay refused. The test has the branch case (cloth.sphere's shape) and a jump past more than the call. The skip path itself is the site's own jump, which this rule now only allows over a call it skips whole; there is no game-free way to run the callback here, so the rule is tested where it decides.11111111 22222222 33333333 94000000/fc000000 44444444), a made-up address, nothing from the game. (The earlier words weregame/sites/sites.toml's own committed signature, but a test needs none of them.)game_files::modules::one_of_a_name, called by the read stage before the lists are written). The other's module is left out with why (exefs_modules.txt, the Mods tab), and its mod's code patch files are refused with it (Unsupported, the note naming the module), since they branch to that place; its data patches stay. Test:two_mods_with_a_module_of_one_name_keep_the_later. For the record, Eden takes the first mod's file in name order and still applies the other's patches over it; the list's one rule was chosen on purpose (the PR's Summary, item 4).needsandstoppedmake;refuse's other check, a patch that makes the signature match a second place, is unchanged by this PR.cargo test -p game-files -p build-driver -p image, clippy-D warningsand rustfmt pass on the head.@review
Recommendation: Approve. The three requested fixes are in place: unsafe retargeting remains refused, the test uses invented words, and duplicate module names no longer produce two placed modules. The reported Windows run shows the target mod working after the fixes. Recommend approval; not approved on the forge.
Console mods can load code modules in loader order, use BSS space within the final page, and retarget a compatible hook-site call. Duplicate module names now have a defined winner, at the cost of leaving the other mod’s module and code patches out.
Since the last review (on
deb7b7612d)Resolved: L1 (A jump site's masked instruction is not always independent of the native hook's behavior.…); L2 (The new test copies a sequence of the game's instructions into another committed file. The…); L3 (Two enabled mods with the same subsdk name are both placed, although their fixed-address p…)
Checks: Still running:
PR description / Title and description (pull_request_target).Also noticed (2; not asked for, not blocking)
More: does it help, other systems, switches, what was read
Does it help: Yes. The reported Windows run shows Super Hero Boost’s stamina changing from 75.0 to 25.4 and back to 75.0, with its menu working. This is a functional fix; the reported single build and capped-FPS observations do not establish a performance gain.
Other systems: The builder and resulting module layout affect Windows and Linux. The reported game run covers Windows on NVIDIA; a Linux mod build and play run and the save-profiles mod remain untested.
Switches: None added, changed or removed.
Reviewed:
d22aa8d610against0b284cc767: Read the changes since the ledger, the affected selection and clash paths, AGENTS.md, CONTRIBUTING.md and the pull request template. Skimmed the remaining pull request diff and recorded test results; the other changed code files were not read in full.; nothing run (a code review).Checked and fine:
Testing: Read-only review; no tests or project scripts were run. The author reports passing changed-crate tests, clippy and rustfmt, plus an in-game Windows run after the fixes.
Overlaps and merge order: Not checked; the network is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by llun. Nothing was run; a maintainer decides.