[PR #625] [MERGED] Force PIE usage on linux #1674

Closed
opened 2026-02-27 21:13:31 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/shadps4-emu/shadPS4/pull/625
Author: @Random06457
Created: 8/28/2024
Status: Merged
Merged: 9/1/2024
Merged by: @raphaelthegreat

Base: mainHead: pie


📝 Commits (5)

  • ccd9b5d force PIE usage on linux
  • 65b107a get rid of explicit -pie linker flag
  • 1fef188 change UNIX with UNIX AND NOT APPLE in CMakeLists.txt
  • 24bf84a cmake: add proper check for PIE support at link time
  • 4541a74 Merge pull request #1 from Mixaill/pie

📊 Changes

1 file changed (+15 additions, -3 deletions)

View changed files

📝 CMakeLists.txt (+15 -3)

📄 Description

It seems the project currently has an implicit requirement that emulator code be located at an address > 40 bits. Otherwise the code gets overwritten when the PS4 memory is allocated, resulting in memory corruptions.

Some linux distributions (e.g. NixOS and fedora(?)) come with a GCC binary built without the --enable-default-pie configuration enabled, which means binaries produces by such systems do not have ASLR support by default.

This causes the compiler to emit relocations which aren't compatible with a high base address (e.g. R_X86_64_32) and the linker to map the base address at a fixed address, which happens to be 0x400000 according to GNU's default linker script.

To address this issue, this PR forces GCC to produce a PIE executable via the CMAKE_POSITION_INDEPENDENT_CODE cmake variable and adds the -pie linker flag to UNIX targets.

Note that I am not 100% sure that enabling PIE guarantees that the base address is going to be > 40 bits. I only looked at the linux source tree briefly and it seems to be very configuration-dependant. However in practice the 40 bit assumption seems to hold so far, so I guess worst case scenario it only happens once and relaunching the emulator will do just fine. And if it happens too frequently, a proper linker script can be added to force the base address.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/shadps4-emu/shadPS4/pull/625 **Author:** [@Random06457](https://github.com/Random06457) **Created:** 8/28/2024 **Status:** ✅ Merged **Merged:** 9/1/2024 **Merged by:** [@raphaelthegreat](https://github.com/raphaelthegreat) **Base:** `main` ← **Head:** `pie` --- ### 📝 Commits (5) - [`ccd9b5d`](https://github.com/shadps4-emu/shadPS4/commit/ccd9b5deb41f9d1d047d45dfe479f3d8f6130e53) force PIE usage on linux - [`65b107a`](https://github.com/shadps4-emu/shadPS4/commit/65b107a448583b3c2432fa9f7e83726df34544df) get rid of explicit -pie linker flag - [`1fef188`](https://github.com/shadps4-emu/shadPS4/commit/1fef1885ea5e6cb5866ee7e68519c1453cd1df50) change UNIX with UNIX AND NOT APPLE in CMakeLists.txt - [`24bf84a`](https://github.com/shadps4-emu/shadPS4/commit/24bf84a4fa2eb16379a1d328cd708166065adadf) cmake: add proper check for PIE support at link time - [`4541a74`](https://github.com/shadps4-emu/shadPS4/commit/4541a746054104b03cef52fa1f5090ef8a49a1b6) Merge pull request #1 from Mixaill/pie ### 📊 Changes **1 file changed** (+15 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+15 -3) </details> ### 📄 Description It seems the project currently has an implicit requirement that emulator code be located at an address > 40 bits. Otherwise the code gets overwritten when the PS4 memory is allocated, resulting in memory corruptions. Some linux distributions (e.g. NixOS and fedora(?)) come with a GCC binary built without the `--enable-default-pie` configuration enabled, which means binaries produces by such systems do not have ASLR support by default. This causes the compiler to emit relocations which aren't compatible with a high base address (e.g. `R_X86_64_32`) and the linker to map the base address at a fixed address, which happens to be `0x400000` according to GNU's default linker script. To address this issue, this PR forces GCC to produce a PIE executable via the `CMAKE_POSITION_INDEPENDENT_CODE` cmake variable and adds the `-pie` linker flag to `UNIX` targets. Note that I am not 100% sure that enabling PIE guarantees that the base address is going to be > 40 bits. I only looked at the linux source tree briefly and it seems to be very configuration-dependant. However in practice the 40 bit assumption seems to hold so far, so I guess worst case scenario it only happens once and relaunching the emulator will do just fine. And if it happens too frequently, a proper linker script can be added to force the base address. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-27 21:13:31 +03:00
Sign in to join this conversation.
No milestone
No project
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.

Dependencies

No dependencies set.

Reference
starred/shadPS4#1674
No description provided.