From 89b5126da0bbb34ba9f330bf6d8fb5f0cb26be7a Mon Sep 17 00:00:00 2001 From: Wellington Dias Date: Fri, 30 Oct 2026 01:39:31 -0300 Subject: [PATCH] Declare the clobbers mbedtls's x86-64 multiply asm lacks, so a clang-built hactool verifies signatures. hactool's bundled mbedtls 2.6.1 adds into memory and changes the flags in its MULADDC asm without saying so. gcc builds happen to work; clang, the Linux release's compiler, keeps values in registers across it at -O1 and above, so every RSA check failed (NCA header, NPDM, ACID) and the AppImage's setup stopped at the update merge. Found by installing from the mounted AppImage; a bisection of the mbedtls objects pointed at bignum.o, and the memory and cc clobbers (as later mbedtls releases have them) fix it. With the patch the full install from the AppImage completes and the game runs. --- THIRD_PARTY_NOTICES.md | 2 +- docs/building.md | 6 ++++-- tools/build_hactool.py | 2 +- tools/hactool/mbedtls-asm-clobbers.patch | 20 ++++++++++++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 tools/hactool/mbedtls-asm-clobbers.patch diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 8161f148..b18aca76 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -71,7 +71,7 @@ These tools run on the developer's or player's machine during the build. They ar | Vulkan-Headers and Vulkan-Loader 1.4.363.0 (MSYS2 packages; headers and `libvulkan-1.dll.a` only, no loader DLL) | https://github.com/KhronosGroup | Apache-2.0 OR MIT | `runtime/deps/` | | Vulkan Memory Allocator 3.4.0 (MSYS2 package), header | https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator | MIT | `runtime/deps/` | | Ninja 1.13.2 (`ninja-win.zip`) | https://github.com/ninja-build/ninja/releases/tag/v1.13.2 | Apache-2.0 | `bin/ninja.exe` | -| hactool (SciresM @ `1d64a83450e025622f3468c28fc4164dad2c5ef6` plus `tools/hactool/portable.patch`, from MacNativeGames' `build_hactool()`: `MNG_TITLE_KEYS` and relaxed BKTR layout checks; our `tools/hactool/unicode.patch`: an embedded manifest that makes UTF-8 the process code page), built by `tools/build_hactool.py` with MSYS2 UCRT64 gcc; imports only KERNEL32 and the UCRT | https://github.com/SciresM/hactool | ISC | `bin/hactool.exe` | +| hactool (SciresM @ `1d64a83450e025622f3468c28fc4164dad2c5ef6` plus `tools/hactool/portable.patch`, from MacNativeGames' `build_hactool()`: `MNG_TITLE_KEYS` and relaxed BKTR layout checks; our `tools/hactool/unicode.patch`: an embedded manifest that makes UTF-8 the process code page; our `tools/hactool/mbedtls-asm-clobbers.patch`: the clobbers the bundled mbedtls's x86-64 multiply asm lacks), built by `tools/build_hactool.py` with MSYS2 UCRT64 gcc; imports only KERNEL32 and the UCRT | https://github.com/SciresM/hactool | ISC | `bin/hactool.exe` | | `totk_xlate.exe` (Ryujinx shader translator, NativeAOT build of `tools/xlate/`, see the Ryujinx row above) | built by `tools/build_xlate.py` | MIT | `bin/totk_xlate.exe` | | MacNativeGames shader variant list | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/assets/shader_list.tsv`), Copyright (c) 2026 MacNativeGames contributors | MIT | `data/shader_list.tsv` | diff --git a/docs/building.md b/docs/building.md index 4924f2e2..65ceab0f 100644 --- a/docs/building.md +++ b/docs/building.md @@ -37,13 +37,15 @@ PATH: ### hactool -`tools/build_hactool.py` fetches SciresM/hactool at `1d64a83` into `toolchain\src\hactool`, applies the two patches in +`tools/build_hactool.py` fetches SciresM/hactool at `1d64a83` into `toolchain\src\hactool`, applies the three patches in `tools/hactool/` and builds a static `toolchain\hactool\hactool.exe` with MSYS2 UCRT64 gcc (a few seconds): - `portable.patch`: the parts of MacNativeGames' `build_hactool()` that run on Windows, the `MNG_TITLE_KEYS` variable and the two BKTR fixes in `nca.c` for the subsection table padding and the subsection layout of newer cumulative updates; - `unicode.patch`: a manifest with `UTF-8`, so paths under a non-ASCII user profile - reach it intact. + reach it intact; +- `mbedtls-asm-clobbers.patch`: the `memory` and `cc` clobbers the bundled mbedtls 2.6.1's x86-64 multiply asm lacks. + Without them a clang build (the Linux release) fails every RSA signature check and the update merge stops. hactool reads `-k `; MSYS2 shells set `HOME`, which hides its default key location, so pass `-k` there. diff --git a/tools/build_hactool.py b/tools/build_hactool.py index 8afbb4d2..2ab4297b 100644 --- a/tools/build_hactool.py +++ b/tools/build_hactool.py @@ -19,7 +19,7 @@ from pathlib import Path ROOT = Path(__file__).resolve().parent.parent URL = 'https://github.com/SciresM/hactool.git' COMMIT = '1d64a83450e025622f3468c28fc4164dad2c5ef6' -PATCHES = [ROOT / 'tools' / 'hactool' / n for n in ('portable.patch', 'unicode.patch')] +PATCHES = [ROOT / 'tools' / 'hactool' / n for n in ('portable.patch', 'unicode.patch', 'mbedtls-asm-clobbers.patch')] WIN = os.name == 'nt' MSYS_BASH = Path(r'C:\msys64\usr\bin\bash.exe') EXE = 'hactool.exe' if WIN else 'hactool' diff --git a/tools/hactool/mbedtls-asm-clobbers.patch b/tools/hactool/mbedtls-asm-clobbers.patch new file mode 100644 index 00000000..d0f01ed7 --- /dev/null +++ b/tools/hactool/mbedtls-asm-clobbers.patch @@ -0,0 +1,20 @@ +Applied by tools/build_hactool.py to SciresM/hactool (ISC) at 1d64a83450e025622f3468c28fc4164dad2c5ef6, ours. +The x86-64 multiply-accumulate asm of hactool's bundled mbedtls 2.6.1 (MULADDC in bn_mul.h) adds into memory through +rdi and changes the flags without saying so. gcc happens to keep that code correct; clang (the Linux release's +toolchain) keeps memory values in registers across it at -O1 and above, the RSA arithmetic goes wrong, and every +signature check fails (NCA header, NPDM, ACID), which stops the merge of the update. Later mbedtls releases name the +same two clobbers. + +diff --git a/mbedtls/include/mbedtls/bn_mul.h b/mbedtls/include/mbedtls/bn_mul.h +index cac3f14..c787b0a 100644 +--- a/mbedtls/include/mbedtls/bn_mul.h ++++ b/mbedtls/include/mbedtls/bn_mul.h +@@ -179,7 +179,7 @@ + #define MULADDC_STOP \ + : "+c" (c), "+D" (d), "+S" (s) \ + : "b" (b) \ +- : "rax", "rdx", "r8" \ ++ : "rax", "rdx", "r8", "memory", "cc" \ + ); + + #endif /* AMD64 */