build(libs): keep import libraries stable so a source edit relinks one library #4721

Merged
fdfawef324 merged 1 commit from upstream-pr/4721 into main 2026-10-14 17:08:36 +00:00
fdfawef324 commented 2026-10-12 19:44:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-12T19:44:57Z.


What

A libc or libkernel source edit relinked and re-patched every library: 124 links and 124 NID patches for one changed .cpp. This change makes that edit relink and re-patch only the library that changed. A change to libc's export list still rebuilds everything that depends on it.

Two dependencies caused the rebuild:

  • Import libraries. Every library links liblibc.dll.a and liblibkernel.dll.a. GNU ld 2.46 rewrites an import library on every link with new archive timestamps, in the member headers and in the symbol map header. The bytes are otherwise identical. The link edge already has restat, but the file always changes, so nothing was pruned. On MinGW, core/libs/CMakeLists.txt now changes the shared library link rule:

    • ld writes the import library to <implib>.new with SOURCE_DATE_EPOCH=0, which gives zero member timestamps.
    • ranlib -D zeroes the symbol map timestamp.
    • cmake -E copy_if_different replaces the import library only when its bytes differ.

    The configure step fails if the rule has no -Wl,--out-implib,<TARGET_IMPLIB> or if ranlib is missing. Linux and macOS links are unchanged.

  • NID patch steps. Each patch step depended on the unpatched libc.prx. It only uses the names libc exports. nid_patcher --write-export-list writes those names, normalized and sorted, to build/core/libs/libc.exports, and leaves the file untouched when they did not change. The patch steps read the file with the new --preserve-export-list option and depend on it instead of libc.prx. This applies to every platform.

A malformed list throws: missing final newline, empty name, duplicate name or empty file. So does giving both reference options.

SOURCE_DATE_EPOCH=0 also sets the export directory TimeDateStamp of each .prx to 0. Without it, that field is the link time. The COFF header TimeDateStamp still holds the link time. The unwinder's image key in Unwind.cpp uses that header field, so it is unaffected.

Why this is still needed with CMAKE_LINK_DEPENDS_NO_SHARED=ON: that option drops the import libraries from the dependent link edges altogether. It is opt-in, so CI and default builds do not get it. It is also unsafe when an export is removed or renamed, because the dependents are not relinked against the new import library. With this change, default builds skip the relinks only when the import library bytes are unchanged, and still relink when an export changes. The option did not cover the 124 re-patches either. Those depended on libc.prx itself.

Sources:

Tested

Windows Server 2025, RTX 3060, GCC 15.2 (winlibs, binutils 2.46), Ninja, Release.

Red on main. I extended nid_patcher_macho (core/libs/nid/tests/test_macho_nids.py) with check_export_list. It checks four things:

  • the list holds the sorted, normalized names;
  • it is not rewritten when a reference with different bytes has the same exports (the mtime is pinned and checked);
  • it is rewritten when the exports change;
  • --preserve-export-list keeps the same exports that --preserve-exports keeps. Malformed lists and two references are refused.

On main the test fails at the first step, because main treats --write-export-list as a library name: FAIL: ...listed.exports: cannot open. With the fix it passes (1.46 s).

Rebuild after touching one file. Each run is ninja -d explain -j 4 libs after touch, on a machine shared with other builds, so wall times are noisy. The edge counts are exact. I measured the default configuration by temporarily setting CMAKE_LINK_DEPENDS_NO_SHARED OFF in core/libs; that line is not committed.

Touched file Configuration Before (main) After
libc/src/Strings.cpp default 1 compile, 124 links, 124 patches: 74.9 s wall, 571.9 s of edge time 1 compile, 1 link, export list, libc patch: 4.3 s wall, 7.8 s of edge time
libkernel/Time/Time.cpp default 1 compile, 123 links, 123 patches: 89.1 s wall, 666.6 s of edge time 1 compile, 1 link, libkernel patch: 3.4 s wall, 5.2 s of edge time
libc/src/Strings.cpp NO_SHARED=ON 1 compile, 1 link, 124 patches: 7.8 s wall, 50.0 s of edge time same 4 steps as default: 5.3 s wall, 8.9 s of edge time
libkernel/Time/Time.cpp NO_SHARED=ON not measured 1 compile, 1 link, libkernel patch: 5.6 s wall

In every "after" run, ninja -d explain names only libc.prx (or libkernel.prx) as dirty after the object file, and a second ninja libs reports no work to do.

An export change still rebuilds everything. I removed a temporary libc export, so libc.exports and liblibc.dll.a changed. The rebuild relinked and re-patched all 124 libraries.

Same patched output. For 10 libraries, I patched once against libc.prx (--preserve-exports) and once against libc.exports (--preserve-export-list). Both results are byte-identical to each other and to the built libs/*.prx: libkernel, libSceAudioIn, libSceNet, libSceVideoOut, libSceLibcInternal, libScePad, libSceSaveData, libSceAgc, libSceFiber and libSceUserService.

Determinism. After a relink of libc, the ranlib -D output is byte-identical. Without the change, two links of libc gave import libraries that differ in 1106 bytes, all of them archive date fields.

Tests that link the new import libraries pass (13/13): nid_patcher_macho, patched_libraries, guest_posix_scalar_io, guest_kernel_sys, guest_offline_imports, guest_memory_pool_stats, guest_time_conversion, guest_wide_strings, guest_math, guest_libc_threads, guest_libc_static_imports, guest_kernel_sync and guest_kernel_fcntl.

Not tested on Linux or macOS. There the change only switches the patch steps to libc.exports, which nid_patcher_macho covers. Not tested with a title.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4834220472 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4721). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-12T19:44:57Z. --- ### What A libc or libkernel source edit relinked and re-patched every library: 124 links and 124 NID patches for one changed `.cpp`. This change makes that edit relink and re-patch only the library that changed. A change to libc's export list still rebuilds everything that depends on it. Two dependencies caused the rebuild: - **Import libraries.** Every library links `liblibc.dll.a` and `liblibkernel.dll.a`. GNU ld 2.46 rewrites an import library on every link with new archive timestamps, in the member headers and in the symbol map header. The bytes are otherwise identical. The link edge already has `restat`, but the file always changes, so nothing was pruned. On MinGW, `core/libs/CMakeLists.txt` now changes the shared library link rule: - ld writes the import library to `<implib>.new` with `SOURCE_DATE_EPOCH=0`, which gives zero member timestamps. - `ranlib -D` zeroes the symbol map timestamp. - `cmake -E copy_if_different` replaces the import library only when its bytes differ. The configure step fails if the rule has no `-Wl,--out-implib,<TARGET_IMPLIB>` or if ranlib is missing. Linux and macOS links are unchanged. - **NID patch steps.** Each patch step depended on the unpatched `libc.prx`. It only uses the names libc exports. `nid_patcher --write-export-list` writes those names, normalized and sorted, to `build/core/libs/libc.exports`, and leaves the file untouched when they did not change. The patch steps read the file with the new `--preserve-export-list` option and depend on it instead of `libc.prx`. This applies to every platform. A malformed list throws: missing final newline, empty name, duplicate name or empty file. So does giving both reference options. `SOURCE_DATE_EPOCH=0` also sets the export directory `TimeDateStamp` of each `.prx` to 0. Without it, that field is the link time. The COFF header `TimeDateStamp` still holds the link time. The unwinder's image key in `Unwind.cpp` uses that header field, so it is unaffected. **Why this is still needed with `CMAKE_LINK_DEPENDS_NO_SHARED=ON`:** that option drops the import libraries from the dependent link edges altogether. It is opt-in, so CI and default builds do not get it. It is also unsafe when an export is removed or renamed, because the dependents are not relinked against the new import library. With this change, default builds skip the relinks only when the import library bytes are unchanged, and still relink when an export changes. The option did not cover the 124 re-patches either. Those depended on `libc.prx` itself. Sources: - [SOURCE_DATE_EPOCH specification](https://reproducible-builds.org/specs/source-date-epoch/) - GNU binutils documentation for `ranlib -D` and `ld --out-implib` - [ninja `restat`](https://ninja-build.org/manual.html#ref_rule) ### Tested Windows Server 2025, RTX 3060, GCC 15.2 (winlibs, binutils 2.46), Ninja, Release. **Red on main.** I extended `nid_patcher_macho` (`core/libs/nid/tests/test_macho_nids.py`) with `check_export_list`. It checks four things: - the list holds the sorted, normalized names; - it is not rewritten when a reference with different bytes has the same exports (the mtime is pinned and checked); - it is rewritten when the exports change; - `--preserve-export-list` keeps the same exports that `--preserve-exports` keeps. Malformed lists and two references are refused. On main the test fails at the first step, because main treats `--write-export-list` as a library name: `FAIL: ...listed.exports: cannot open`. With the fix it passes (1.46 s). **Rebuild after touching one file.** Each run is `ninja -d explain -j 4 libs` after `touch`, on a machine shared with other builds, so wall times are noisy. The edge counts are exact. I measured the default configuration by temporarily setting `CMAKE_LINK_DEPENDS_NO_SHARED OFF` in `core/libs`; that line is not committed. | Touched file | Configuration | Before (main) | After | |---|---|---|---| | `libc/src/Strings.cpp` | default | 1 compile, 124 links, 124 patches: 74.9 s wall, 571.9 s of edge time | 1 compile, 1 link, export list, libc patch: 4.3 s wall, 7.8 s of edge time | | `libkernel/Time/Time.cpp` | default | 1 compile, 123 links, 123 patches: 89.1 s wall, 666.6 s of edge time | 1 compile, 1 link, libkernel patch: 3.4 s wall, 5.2 s of edge time | | `libc/src/Strings.cpp` | `NO_SHARED=ON` | 1 compile, 1 link, 124 patches: 7.8 s wall, 50.0 s of edge time | same 4 steps as default: 5.3 s wall, 8.9 s of edge time | | `libkernel/Time/Time.cpp` | `NO_SHARED=ON` | not measured | 1 compile, 1 link, libkernel patch: 5.6 s wall | In every "after" run, `ninja -d explain` names only `libc.prx` (or `libkernel.prx`) as dirty after the object file, and a second `ninja libs` reports `no work to do`. **An export change still rebuilds everything.** I removed a temporary libc export, so `libc.exports` and `liblibc.dll.a` changed. The rebuild relinked and re-patched all 124 libraries. **Same patched output.** For 10 libraries, I patched once against `libc.prx` (`--preserve-exports`) and once against `libc.exports` (`--preserve-export-list`). Both results are byte-identical to each other and to the built `libs/*.prx`: libkernel, libSceAudioIn, libSceNet, libSceVideoOut, libSceLibcInternal, libScePad, libSceSaveData, libSceAgc, libSceFiber and libSceUserService. **Determinism.** After a relink of libc, the `ranlib -D` output is byte-identical. Without the change, two links of libc gave import libraries that differ in 1106 bytes, all of them archive date fields. **Tests that link the new import libraries pass (13/13):** `nid_patcher_macho`, `patched_libraries`, `guest_posix_scalar_io`, `guest_kernel_sys`, `guest_offline_imports`, `guest_memory_pool_stats`, `guest_time_conversion`, `guest_wide_strings`, `guest_math`, `guest_libc_threads`, `guest_libc_static_imports`, `guest_kernel_sync` and `guest_kernel_fcntl`. Not tested on Linux or macOS. There the change only switches the patch steps to `libc.exports`, which `nid_patcher_macho` covers. Not tested with a title. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-13 12:27:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T12:27:15Z.


Windows test: head 555ac9fc3 merged onto current main ff8fe4dbc, together with 6 other open pull requests in one build (the merges were conflict-free); a failure would have been bisected per PR. Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest for the tests this change adds or touches: nid_patcher_macho passed; python tools/check_conventions.py --base upstream/main passed for the combined tree. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6145523441 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4721#issuecomment-6145523441). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T12:27:15Z. --- Windows test: head `555ac9fc3` merged onto current `main` `ff8fe4dbc`, together with 6 other open pull requests in one build (the merges were conflict-free); a failure would have been bisected per PR. Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j2`: full build 0 errors; ctest for the tests this change adds or touches: nid_patcher_macho passed; `python tools/check_conventions.py --base upstream/main` passed for the combined tree. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
adriadam10 commented 2026-10-13 15:12:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T15:12:58Z.


Checked the diff, nid_patcher_macho (export list written only on change, malformed lists and double reference refused, same preserved exports as --preserve-exports) and the core/libs build and tests on Linux (26/26). The MinGW link rule is covered by the Windows build and @xXPBSXx's Windows run; merging once it is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148766432 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4721#issuecomment-6148766432). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T15:12:58Z. --- Checked the diff, `nid_patcher_macho` (export list written only on change, malformed lists and double reference refused, same preserved exports as `--preserve-exports`) and the core/libs build and tests on Linux (26/26). The MinGW link rule is covered by the Windows build and @xXPBSXx's Windows run; merging once it is green.
adriadam10 commented 2026-10-14 16:54:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T16:54:45Z.


Re-checked after the rebase: the change is the same, only context lines moved. Core/libs build and tests on current main pass on Linux (32/32, incl. nid_patcher_macho). The MinGW link rule is covered by the Windows build and @xXPBSXx's Windows run; merging once it is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6173645999 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4721#issuecomment-6173645999). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T16:54:45Z. --- Re-checked after the rebase: the change is the same, only context lines moved. Core/libs build and tests on current main pass on Linux (32/32, incl. `nid_patcher_macho`). The MinGW link rule is covered by the Windows build and @xXPBSXx's Windows run; merging once it is green.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4721
No description provided.