feat(relinker): write a desktop entry with the game icon for Linux executables #353

Merged
adriadam10 merged 3 commits from upstream-pr/353 into main 2026-10-11 10:59:31 +00:00
adriadam10 commented 2026-10-02 11:45:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-02T11:45:30Z.


What

Linux counterpart of #350. ELF has no icon resource; desktops take the icon from a freedesktop .desktop entry. When sce_sys/icon0.png sits beside the input executable, the relinker now writes next to the output:

  • <output>.png: the icon, byte for byte (PNG signature and IHDR checked).
  • <output>.desktop: Type=Application, Name, Exec, Path (output directory), Icon, Terminal=false, Categories=Game;. It is marked executable so file managers (Dolphin, Nautilus) offer to launch it.

Name is the titleName from sce_sys/param.json, parsed with the existing libkernel ParamJsonParser (same language choice as sceAppContent/AppMetadata), with ™ ® © and line breaks removed and spaces collapsed. Without param.json, the output file name is used. Exec is quoted and escaped as the Desktop Entry spec requires (", `, $, \, %). Paths are absolute because the spec has no relative form.

Without icon0.png nothing extra is written and the output is unchanged. A malformed icon0.png or param.json fails the relink with the file path, like #350 does for Windows. No new flags or dependencies; only the C++ standard library.

No conflict with #350: the two PRs touch different lines (git merge-tree is clean), and the TechnicalDebt entry that #350 removes is left for it.

Tested

Linux (CachyOS, GCC 16), Release: full ctest (109) passes, including the new C++ test linux_desktop_entry (elfpatcher/tests/LinuxDesktopEntryTests.cpp, registered next to file_writer_tests, so it doesn't depend on Python).

  • The test covers: no icon0.png (no files written), title cleanup (Some™ Game®:\n Edition© → Some Game: Edition), byte-identical icon, exec bit, the output name as fallback with % and $ in the path, a non-PNG icon, and a malformed param.json.
  • desktop-file-validate accepts the generated entry, including one whose path has a space, % and $.
  • Negative check: writing the raw title instead of the cleaned one breaks the entry on the line break, and the test fails (Malformed desktop entry line: Edition©).

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 (complements #350)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4716031878 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/353). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-02T11:45:30Z. --- ### What Linux counterpart of #350. ELF has no icon resource; desktops take the icon from a freedesktop `.desktop` entry. When `sce_sys/icon0.png` sits beside the input executable, the relinker now writes next to the output: - `<output>.png`: the icon, byte for byte (PNG signature and IHDR checked). - `<output>.desktop`: `Type=Application`, `Name`, `Exec`, `Path` (output directory), `Icon`, `Terminal=false`, `Categories=Game;`. It is marked executable so file managers (Dolphin, Nautilus) offer to launch it. `Name` is the `titleName` from `sce_sys/param.json`, parsed with the existing libkernel `ParamJsonParser` (same language choice as `sceAppContent`/AppMetadata), with ™ ® © and line breaks removed and spaces collapsed. Without `param.json`, the output file name is used. `Exec` is quoted and escaped as the Desktop Entry spec requires (`"`, `` ` ``, `$`, `\`, `%`). Paths are absolute because the spec has no relative form. Without `icon0.png` nothing extra is written and the output is unchanged. A malformed `icon0.png` or `param.json` fails the relink with the file path, like #350 does for Windows. No new flags or dependencies; only the C++ standard library. No conflict with #350: the two PRs touch different lines (`git merge-tree` is clean), and the TechnicalDebt entry that #350 removes is left for it. ### Tested Linux (CachyOS, GCC 16), Release: full ctest (109) passes, including the new C++ test `linux_desktop_entry` (`elfpatcher/tests/LinuxDesktopEntryTests.cpp`, registered next to `file_writer_tests`, so it doesn't depend on Python). - The test covers: no `icon0.png` (no files written), title cleanup (`Some™ Game®:\n Edition© ` → `Some Game: Edition`), byte-identical icon, exec bit, the output name as fallback with `%` and `$` in the path, a non-PNG icon, and a malformed `param.json`. - `desktop-file-validate` accepts the generated entry, including one whose path has a space, `%` and `$`. - Negative check: writing the raw title instead of the cleaned one breaks the entry on the line break, and the test fails (`Malformed desktop entry line: Edition©`). ### 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 (complements #350) - [x] AI-assisted: yes
QDarkRex commented 2026-10-02 14:11:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QDarkRex on 2026-10-02T14:11:19Z.


Built f2cc42e on Ubuntu 24.04 (GCC 13): relinker ctest 16/16 including linux_desktop_entry. I also ran the writer on paths with spaces, $HOME, backticks, ", \, %u, a leading - and Japanese names: desktop-file-validate accepts every entry and Exec/Path/Icon decode back to the exact paths, so both escaping layers hold.

Two small things:

  • The entry is written after the executable (main.cpp, fileWriter.Write then LinuxDesktopEntryWriter().Write), so a malformed icon0.png or param.json prints FAIL and exits 2 with a finished ELF already on disk. Checking the icon and title before writing the executable would keep failure all-or-nothing, as in #350.
  • escapeValue only doubles \. An output directory whose name contains a newline produces an entry that desktop-file-validate rejects; escaping \n, \t, \r (or throwing on control characters) would close that.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5954362923 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/353#issuecomment-5954362923). Originally posted by [QDarkRex](https://github.com/QDarkRex) on 2026-10-02T14:11:19Z. --- Built f2cc42e on Ubuntu 24.04 (GCC 13): relinker ctest 16/16 including `linux_desktop_entry`. I also ran the writer on paths with spaces, `$HOME`, backticks, `"`, `\`, `%u`, a leading `-` and Japanese names: `desktop-file-validate` accepts every entry and `Exec`/`Path`/`Icon` decode back to the exact paths, so both escaping layers hold. Two small things: - The entry is written after the executable (`main.cpp`, `fileWriter.Write` then `LinuxDesktopEntryWriter().Write`), so a malformed `icon0.png` or `param.json` prints `FAIL` and exits 2 with a finished ELF already on disk. Checking the icon and title before writing the executable would keep failure all-or-nothing, as in #350. - `escapeValue` only doubles `\`. An output directory whose name contains a newline produces an entry that `desktop-file-validate` rejects; escaping `\n`, `\t`, `\r` (or throwing on control characters) would close that.
oneandonlydean commented 2026-10-04 05:42:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:42:59Z.


I agree with both of QDarkRex's points: validating icon0.png and param.json before writing the executable would keep a failed relink from leaving an ELF behind, and escaping (or rejecting) control characters in Exec, Path and Icon keeps the entry valid for any output path. Could you address those? Writing the .desktop and .png next to every Linux output is also a behaviour change, so I'll leave the go-ahead on that to @boykopovar

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977022181 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/353#issuecomment-5977022181). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:42:59Z. --- I agree with both of QDarkRex's points: validating icon0.png and param.json before writing the executable would keep a failed relink from leaving an ELF behind, and escaping (or rejecting) control characters in Exec, Path and Icon keeps the entry valid for any output path. Could you address those? Writing the .desktop and .png next to every Linux output is also a behaviour change, so I'll leave the go-ahead on that to @boykopovar
adriadam10 commented 2026-10-04 06:42:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T06:42:00Z.


Thanks @QDarkRex for testing it and @oneandonlydean for the review. Both points are done, rebased onto main:

  • All-or-nothing: the writer is split into Prepare (reads and checks icon0.png and param.json, builds the entry text) and Write. main.cpp calls Prepare before fileWriter.Write, so a malformed icon or title fails before the ELF is on disk. The failure tests now check that neither the .desktop nor the .png was written.
  • Control characters: an output path containing any control character (\n, \t, \r, 0x7f, ...) is refused with a clear error, rather than escaped. A newline can't round-trip through Exec anyway. The test covers those four, and it fails if the check is removed.

linux_desktop_entry and the other relinker tests pass on Linux. The go-ahead on writing the .desktop/.png next to every Linux output stays with boykopovar, as you said.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977402712 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/353#issuecomment-5977402712). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T06:42:00Z. --- Thanks @QDarkRex for testing it and @oneandonlydean for the review. Both points are done, rebased onto main: - **All-or-nothing:** the writer is split into `Prepare` (reads and checks icon0.png and param.json, builds the entry text) and `Write`. `main.cpp` calls `Prepare` before `fileWriter.Write`, so a malformed icon or title fails before the ELF is on disk. The failure tests now check that neither the `.desktop` nor the `.png` was written. - **Control characters:** an output path containing any control character (`\n`, `\t`, `\r`, 0x7f, ...) is refused with a clear error, rather than escaped. A newline can't round-trip through `Exec` anyway. The test covers those four, and it fails if the check is removed. `linux_desktop_entry` and the other relinker tests pass on Linux. The go-ahead on writing the `.desktop`/`.png` next to every Linux output stays with boykopovar, as you said.
oneandonlydean commented 2026-10-04 07:39:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T07:39:54Z.


Thanks, both points are handled: Prepare reads and checks icon0.png and param.json before the executable is written, and a path with a control character is refused with a clear error.

One small gap is left for all-or-nothing: in main.cpp, Prepare still runs after the --registry JSON and the guest modules are written, so a malformed icon0.png or param.json now leaves those files behind instead of the ELF. Prepare only needs the input and output paths, so calling it before the first fileWriter.Write (above the registry write) would leave nothing on disk when it fails, the same as the Windows icon, which WindowsPePatcher reads inside Patch before anything is written.

The go-ahead on writing the .desktop and .png next to every Linux output is still with @boykopovar.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977779346 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/353#issuecomment-5977779346). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T07:39:54Z. --- Thanks, both points are handled: `Prepare` reads and checks icon0.png and param.json before the executable is written, and a path with a control character is refused with a clear error. One small gap is left for all-or-nothing: in `main.cpp`, `Prepare` still runs after the `--registry` JSON and the guest modules are written, so a malformed icon0.png or param.json now leaves those files behind instead of the ELF. `Prepare` only needs the input and output paths, so calling it before the first `fileWriter.Write` (above the registry write) would leave nothing on disk when it fails, the same as the Windows icon, which `WindowsPePatcher` reads inside `Patch` before anything is written. The go-ahead on writing the .desktop and .png next to every Linux output is still with @boykopovar.
adriadam10 commented 2026-10-04 09:02:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T09:02:17Z.


Thanks, done and rebased onto main:

  • Prepare now runs before the first fileWriter.Write (above the --registry write and the guest modules), so a malformed icon0.png or param.json leaves nothing on disk. Checked with a synthetic input, a broken icon0.png and --registry: FAIL, exit 2, output directory empty; with no icon the executable and registry are written as before.

linux_desktop_entry and the full Release ctest pass on Linux (only agc_driver_pixel_helper_lanes fails locally, also on main).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5978339840 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/353#issuecomment-5978339840). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T09:02:17Z. --- Thanks, done and rebased onto main: - `Prepare` now runs before the first `fileWriter.Write` (above the `--registry` write and the guest modules), so a malformed icon0.png or param.json leaves nothing on disk. Checked with a synthetic input, a broken icon0.png and `--registry`: `FAIL`, exit 2, output directory empty; with no icon the executable and registry are written as before. `linux_desktop_entry` and the full Release ctest pass on Linux (only `agc_driver_pixel_helper_lanes` fails locally, also on main).
oneandonlydean commented 2026-10-04 09:51:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T09:51:51Z.


Thanks, that closes it: Prepare now runs before the registry, the guest modules and the executable are written, so a bad icon0.png or param.json leaves nothing behind. Merged with current main it builds, and linux_desktop_entry and the rest of ctest pass here. From my side only the go-ahead from @boykopovar on writing the .desktop and .png next to every Linux output is left.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5978689988 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/353#issuecomment-5978689988). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T09:51:51Z. --- Thanks, that closes it: `Prepare` now runs before the registry, the guest modules and the executable are written, so a bad icon0.png or param.json leaves nothing behind. Merged with current main it builds, and `linux_desktop_entry` and the rest of ctest pass here. From my side only the go-ahead from @boykopovar on writing the .desktop and .png next to every Linux output is left.
adriadam10 commented 2026-10-11 10:45:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T10:45:39Z.


Rebased onto current main and adapted to what landed since:

  • macOS output: the entry was written for every non-Windows output, so --macos would have got a Linux .desktop too. It's now written only for Linux output (!toWindows && !toMacos).
  • Paths: main.cpp and the writer go through Io::NativePath/Io::Utf8Path like the rest of the relinker (#b74774f, non-ASCII paths on Windows hosts). On Linux these are the identity.
  • Test: new linux_desktop_entry_output relinks a fixture end to end: no icon → no entry; icon + param.json → .desktop + .png with Name/Exec/Path/Icon checked and desktop-file-validate clean (when installed); --macos and --windows → neither file; malformed icon → nothing on disk. It fails on the --macos case without the fix.

All 101 relinker tests pass locally on Linux.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6108207810 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/353#issuecomment-6108207810). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T10:45:39Z. --- Rebased onto current main and adapted to what landed since: - **macOS output:** the entry was written for every non-Windows output, so `--macos` would have got a Linux `.desktop` too. It's now written only for Linux output (`!toWindows && !toMacos`). - **Paths:** `main.cpp` and the writer go through `Io::NativePath`/`Io::Utf8Path` like the rest of the relinker (#b74774f, non-ASCII paths on Windows hosts). On Linux these are the identity. - **Test:** new `linux_desktop_entry_output` relinks a fixture end to end: no icon → no entry; icon + param.json → `.desktop` + `.png` with Name/Exec/Path/Icon checked and `desktop-file-validate` clean (when installed); `--macos` and `--windows` → neither file; malformed icon → nothing on disk. It fails on the `--macos` case without the fix. All 101 relinker tests pass locally on Linux.
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!353
No description provided.