Repository navigation
feat(relinker): route SDK link, symlink, readlink and pathconf stubs to libkernel_sys #2057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b5996a8
321894c
d4a4e84
84b4c0b
de2f348
dae5aac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| #ifndef RELINKER_ANALYSIS_SDKSYSCALLROUTEIMPORT_HPP | ||
| #define RELINKER_ANALYSIS_SDKSYSCALLROUTEIMPORT_HPP | ||
|
|
||
| #include <relinker/analysis/SdkSyscallRoutes.hpp> | ||
| #include <domain/ImportModule.hpp> | ||
| #include <domain/Types.hpp> | ||
| #include <elfpatcher/general/ElfConstants.hpp> | ||
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <limits> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <vector> | ||
|
|
||
| namespace Relinker { | ||
|
|
||
| inline bool IsSdkSyscallRouteLibrary(const std::string_view library) { | ||
| return library.empty() || library.starts_with("libkernel"); | ||
| } | ||
|
|
||
| inline bool IsSdkSyscallRouteName(const SdkSyscallRoute& route, std::string_view nid) { | ||
| nid = nid.substr(0, nid.find('#')); | ||
| return nid == route.Export || nid == route.Nid || (nid.size() == route.Export.size() + 12 && nid.starts_with(route.Export) && nid.ends_with("_nid_postfix")); | ||
| } | ||
|
|
||
| inline bool IsSdkSyscallRouteSymbolType(const std::uint8_t symbolType) { | ||
| return symbolType == Elfpatcher::STT_FUNC || symbolType == Elfpatcher::STT_NOTYPE; | ||
| } | ||
|
|
||
| inline unsigned SdkSyscallRouteRelocationRank(const std::uint32_t relocationType) { | ||
| if (relocationType == Elfpatcher::R_X86_64_JUMP_SLOT) return 3; | ||
| if (relocationType == Elfpatcher::R_X86_64_GLOB_DAT) return 2; | ||
| if (relocationType == Elfpatcher::R_X86_64_64) return 1; | ||
| return 0; | ||
| } | ||
|
|
||
| inline std::string SdkSyscallRouteLibraryName(const std::vector<std::string>& libraries) { | ||
| const auto library = std::find_if(libraries.begin(), libraries.end(), [](const std::string& name) { return Domain::NeededLibraryName(name) == SdkSyscallRouteLibrary; }); | ||
| return library == libraries.end() ? std::string(SdkSyscallRouteLibrary) : *library; | ||
| } | ||
|
|
||
| inline std::string SdkSyscallRouteNeededLibrary(std::vector<std::string>& libraries) { | ||
| auto library = SdkSyscallRouteLibraryName(libraries); | ||
| if (std::find(libraries.begin(), libraries.end(), library) == libraries.end()) libraries.push_back(library); | ||
| return library; | ||
| } | ||
|
|
||
| inline bool IsSdkSyscallRouteReachable(const Domain::VirtualAddress target, const Domain::VirtualAddress rip) { | ||
| const auto distance = static_cast<std::int64_t>(target) - static_cast<std::int64_t>(rip); | ||
| return distance >= std::numeric_limits<std::int32_t>::min() && distance <= std::numeric_limits<std::int32_t>::max(); | ||
| } | ||
|
|
||
| struct SdkSyscallRouteSlot { | ||
| std::size_t Header; | ||
| Domain::VirtualAddress Address; | ||
| }; | ||
|
|
||
| inline std::optional<SdkSyscallRouteSlot> FindSdkSyscallRouteSlot(const std::vector<Domain::ProgramHeader>& headers, const Domain::VirtualAddress rip) { | ||
| constexpr std::uint64_t page = 0x1000; | ||
| constexpr auto limit = std::numeric_limits<std::uint64_t>::max(); | ||
| const auto covers = [](const Domain::ProgramHeader& header, const std::uint64_t begin, const std::uint64_t end) { | ||
| return header.MappedAddress < end && (header.MappedAddress > limit - header.MemorySize || begin < header.MappedAddress + header.MemorySize); | ||
| }; | ||
| for (std::size_t index = 0; index < headers.size(); ++index) { | ||
| const auto& load = headers[index]; | ||
| if (load.Type != Elfpatcher::PT_LOAD || (load.Flags & Elfpatcher::PF_W) == 0 || load.MemorySize == 0) continue; | ||
| if (load.MemorySize > limit - 2 * page || load.MappedAddress > limit - 2 * page - load.MemorySize) continue; | ||
| const auto end = load.MappedAddress + load.MemorySize; | ||
| const auto address = (end + 7) & ~std::uint64_t{7}; | ||
| if (address + 8 > ((end + page - 1) & ~(page - 1))) continue; | ||
| const bool taken = std::any_of(headers.begin(), headers.end(), [&](const Domain::ProgramHeader& other) { | ||
| if (&other == &load || other.MemorySize == 0) return false; | ||
| if (other.Type == Elfpatcher::PT_GNU_RELRO && covers(other, end - 1, address + 8)) return true; | ||
| if (other.Type != Elfpatcher::PT_LOAD && other.Type != Elfpatcher::PT_TLS && other.Type != Elfpatcher::PT_DYNAMIC) return false; | ||
| return covers(other, end, address + 8); | ||
| }); | ||
| if (taken) continue; | ||
| if (!IsSdkSyscallRouteReachable(address, rip)) continue; | ||
| return SdkSyscallRouteSlot{index, address}; | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| inline std::string SdkSyscallRouteFailure(const SdkSyscallRoute& route, const std::string& where = {}, const std::string& reason = "there is no room for an 8-byte import slot after a writable PT_LOAD segment within reach of the stub") { | ||
| return "Homebrew ps5-payload-dev syscall stub " + std::to_string(route.Number) + " cannot be routed: missing libkernel " + std::string(route.Export) + " import" + where + ", and " + reason; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #ifndef RELINKER_ANALYSIS_SDKSYSCALLROUTES_HPP | ||
| #define RELINKER_ANALYSIS_SDKSYSCALLROUTES_HPP | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <cstdint> | ||
| #include <string_view> | ||
|
|
||
| namespace Relinker { | ||
|
|
||
| struct SdkSyscallRoute { | ||
| std::uint32_t Number; | ||
| std::string_view Export; | ||
| std::string_view Nid; | ||
| }; | ||
|
|
||
| inline constexpr std::array<SdkSyscallRoute, 4> SdkSyscallRoutes{{ | ||
| {9, "link", "Pd7KtJ1gybk"}, | ||
| {57, "symlink", "waIF+5fE5HQ"}, | ||
| {58, "readlink", "pd02UI9TbOA"}, | ||
| {191, "pathconf", "oc98+zOImus"}, | ||
| }}; | ||
|
|
||
| inline constexpr std::string_view SdkSyscallRouteLibrary = "libkernel_sys.prx"; | ||
|
|
||
| inline const SdkSyscallRoute* FindSdkSyscallRoute(const std::uint32_t number) { | ||
| const auto route = std::find_if(SdkSyscallRoutes.begin(), SdkSyscallRoutes.end(), [&](const SdkSyscallRoute& candidate) { return candidate.Number == number; }); | ||
| return route == SdkSyscallRoutes.end() ? nullptr : &*route; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| #include <relinker/guest/GuestImage.hpp> | ||
| #include <relinker/analysis/PathconfImport.hpp> | ||
| #include <relinker/analysis/SdkSyscallRouteImport.hpp> | ||
| #include <relinker/analysis/ReplacedPltStubs.hpp> | ||
| #include <relinker/analysis/SceCodeExtent.hpp> | ||
| #include <relinker/analysis/UnusedNidFilter/EhFrameReader.hpp> | ||
|
|
@@ -26,12 +26,12 @@ namespace Relinker { | |
|
|
||
| namespace { | ||
|
|
||
| std::optional<Domain::VirtualAddress> FindGuestPathconfImportAddress(const GuestImage& image) { | ||
| std::optional<Domain::VirtualAddress> FindGuestRouteImportAddress(const GuestImage& image, const SdkSyscallRoute& route, const Domain::VirtualAddress rip) { | ||
| std::optional<Domain::VirtualAddress> candidate; | ||
| for (const auto* table : {&image.Dynamic.RelaPltData, &image.Dynamic.RelaData}) { | ||
| for (std::size_t position = 0; position + 24 <= table->size(); position += 24) { | ||
| const auto info = Io::ReadU64(*table, position + 8); | ||
| const unsigned rank = PathconfRelocationRank(static_cast<std::uint32_t>(info)); | ||
| const unsigned rank = SdkSyscallRouteRelocationRank(static_cast<std::uint32_t>(info)); | ||
| if (rank == 0) { | ||
| continue; | ||
| } | ||
|
|
@@ -44,10 +44,13 @@ std::optional<Domain::VirtualAddress> FindGuestPathconfImportAddress(const Guest | |
| continue; | ||
| } | ||
| const auto& symbol = image.Symbols[symbolIndex]; | ||
| if (symbol.Section != 0 || !IsPathconfImportSymbolType(symbol.Info & 0x0F) || !IsPathconfImportLibrary(symbol.Library) || !IsPathconfImportName(symbol.Name)) { | ||
| if (symbol.Section != 0 || !IsSdkSyscallRouteSymbolType(symbol.Info & 0x0F) || !IsSdkSyscallRouteLibrary(symbol.Library) || !IsSdkSyscallRouteName(route, symbol.Name)) { | ||
| continue; | ||
| } | ||
| const auto target = Io::ReadU64(*table, position); | ||
| if (!IsSdkSyscallRouteReachable(target, rip)) { | ||
| continue; | ||
| } | ||
| if (rank == 3) { | ||
| return target; | ||
| } | ||
|
|
@@ -585,14 +588,45 @@ std::vector<GuestArtifact> GuestModuleBuilder::Build(const std::filesystem::path | |
| } | ||
|
|
||
| for (const auto& routed : routedStubs) { | ||
| if (routed.SyscallNumber == 191) { | ||
| const auto pathconfTarget = FindGuestPathconfImportAddress(image); | ||
| if (pathconfTarget.has_value()) { | ||
| const Domain::VirtualAddress stubVAddr = header.MappedAddress + routed.StubOffset; | ||
| if (const auto* route = FindSdkSyscallRoute(routed.SyscallNumber)) { | ||
| const Domain::VirtualAddress stubVAddr = header.MappedAddress + routed.StubOffset; | ||
| auto routeTarget = FindGuestRouteImportAddress(image, *route, stubVAddr + 6); | ||
| auto bundled = findGuest(SdkSyscallRouteLibraryName(image.Dependencies)); | ||
| if (bundled == guestNames.end()) | ||
| bundled = std::find_if(guestNames.begin(), guestNames.end(), [&](const auto& entry) { return ModuleStem(entry.first, windows) == ModuleStem(std::string(SdkSyscallRouteLibrary), false); }); | ||
| const bool bundledLibrary = bundled != guestNames.end(); | ||
| if (!routeTarget && !bundledLibrary) { | ||
| if (const auto slot = FindSdkSyscallRouteSlot(image.Headers, stubVAddr + 6)) { | ||
| auto& load = image.Headers[slot->Header]; | ||
| load.MemorySize = slot->Address + 8 - load.MappedAddress; | ||
| const auto libraryName = SdkSyscallRouteNeededLibrary(image.Dependencies); | ||
| const auto host = hostLibrary(libraryName); | ||
| if (std::find(moduleHostLibraries[index].begin(), moduleHostLibraries[index].end(), host) == moduleHostLibraries[index].end()) moduleHostLibraries[index].push_back(host); | ||
|
Comment on lines
+602
to
+604
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Report omitted libraries after routing adds them back. With Emit the "left out" message only after the final dependency list is built, or check the final
|
||
| if (windows && uniqueHosts.insert(host).second) { | ||
| hostLibraries.push_back(host); | ||
| dynamic.NeededLibraries.push_back(host); | ||
| addNeeded(host); | ||
| } | ||
| if (image.Dynamic.DynStrData.size() > std::numeric_limits<std::uint32_t>::max()) throw Domain::RelinkerException("Guest string table too large"); | ||
| const auto symbol = image.Symbols.size(); | ||
| image.Symbols.push_back({std::string(route->Nid), 0x12, 0, 0, 0, 0, libraryName, std::string(route->Nid)}); | ||
| const auto entry = image.Dynamic.DynSymData.size(); | ||
| image.Dynamic.DynSymData.resize(entry + 24, 0); | ||
| Io::WriteU32(image.Dynamic.DynSymData, entry, static_cast<std::uint32_t>(image.Dynamic.DynStrData.size())); | ||
| image.Dynamic.DynSymData[entry + 4] = 0x12; | ||
| Io::AppendString(image.Dynamic.DynStrData, std::string(route->Nid)); | ||
| Io::AppendU64(image.Dynamic.RelaData, slot->Address); | ||
| Io::AppendU64(image.Dynamic.RelaData, (static_cast<std::uint64_t>(symbol) << 32) | 6); | ||
| Io::AppendU64(image.Dynamic.RelaData, 0); | ||
| systemImports[index].insert(symbol); | ||
| routeTarget = slot->Address; | ||
| } | ||
| } | ||
| if (routeTarget.has_value()) { | ||
| if (routed.StubOffset + routed.StubLength > code.size()) | ||
| throw Domain::RelinkerException("Syscall route patch exceeds guest executable segment", stubVAddr); | ||
| const Domain::VirtualAddress ripAfterDisp = stubVAddr + 6; | ||
| const std::int64_t disp = static_cast<std::int64_t>(*pathconfTarget) - static_cast<std::int64_t>(ripAfterDisp); | ||
| const std::int64_t disp = static_cast<std::int64_t>(*routeTarget) - static_cast<std::int64_t>(ripAfterDisp); | ||
| if (disp < std::numeric_limits<std::int32_t>::min() || disp > std::numeric_limits<std::int32_t>::max()) | ||
| throw Domain::RelinkerException("Syscall stub relocation distance exceeds 32-bit displacement", stubVAddr); | ||
| const auto disp32 = static_cast<std::uint32_t>(static_cast<std::int32_t>(disp)); | ||
|
|
@@ -620,7 +654,8 @@ std::vector<GuestArtifact> GuestModuleBuilder::Build(const std::filesystem::path | |
| image.Bytes[header.Offset + opcodeOffset] = 0x0F; | ||
| image.Bytes[header.Offset + opcodeOffset + 1] = 0x0B; | ||
| } else { | ||
| throw Domain::RelinkerException("Homebrew ps5-payload-dev syscall stub 191 cannot be routed: missing libkernel pathconf import in " + Io::Utf8Path(image.SourcePath), header.MappedAddress + routed.StubOffset); | ||
| const auto where = " in " + Io::Utf8Path(image.SourcePath); | ||
| throw Domain::RelinkerException(bundledLibrary ? SdkSyscallRouteFailure(*route, where, bundled->first + " is a bundled module, so no import of it is added") : SdkSyscallRouteFailure(*route, where), stubVAddr); | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bundled
libkernel_sysmodules with a bare SONAME bypass the route guard.A bundled module whose
DT_SONAMEislibkernel_sys, without.prx, is registered under that bare name. The guard at lines 594-597 looks uplibkernel_sys.prxexactly and compares the stem of each registered name withlibkernel_sys.ModuleStemreturns an empty string for the bare name, so neither check matches. A sibling module that uses a routedlink,symlink,readlink, orpathconfstub, and that does not depend onlibkernel_sys, can then take the host-import path. The bundled-library rule requires that path to refuse or trap the stub.Match the bare SONAME
libkernel_sysin this guard as well aslibkernel_sys.prx. The fix must keep the existing Windows case-folding behavior.