mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(relinker): write GOT stub slots as DIR64 preferred-VA
The Windows PE lazy-import GOT slot was written as a bare 4-byte RVA, but the .reloc entries generated for the .gtls/data sections use IMAGE_REL_BASED_DIR64, which only adds the load delta to whatever is already stored at the slot. A bare RVA therefore resolved at runtime to (loadBase - ImageBase + rva) instead of the intended stub address, pointing into unmapped memory whenever the module was not loaded at its preferred base. Write the full 8-byte preferred-VA form (ImageBase + stubRva) instead, matching DIR64 semantics, and generate a base relocation entry for each lazy-stub GOT slot. The .reloc section is now built after the lazy stubs are written into their target sections rather than before, since it previously captured only the relocations known at that point and missed the newly patched slots entirely.
This commit is contained in:
@@ -30,9 +30,11 @@ void writeDiagnosticsImports(const std::vector<PeImport>& imports) {
|
||||
|
||||
void writeGotStub(std::vector<PeSection>& sections, const std::uint32_t targetRva, const std::uint32_t stubRva) {
|
||||
for (auto& section : sections) {
|
||||
if (targetRva < section.Rva || targetRva - section.Rva > section.Data.size() - 4)
|
||||
if (section.Data.size() < 8 || targetRva < section.Rva || targetRva - section.Rva > section.Data.size() - 8)
|
||||
continue;
|
||||
Io::WriteU32(section.Data, targetRva - section.Rva, stubRva);
|
||||
// A DIR64 base relocation only adds the load delta to the stored value, so the slot must hold the
|
||||
// preferred-VA form (ImageBase + rva); a bare RVA would end up as (loadBase - ImageBase + rva) at runtime.
|
||||
Io::WriteU64(section.Data, targetRva - section.Rva, ImageBase + stubRva);
|
||||
return;
|
||||
}
|
||||
throw Domain::RelinkerException("Lazy import GOT slot is not contained in any section", targetRva);
|
||||
@@ -72,12 +74,6 @@ std::vector<std::uint8_t> WindowsPePatcher::Patch(const std::vector<std::uint8_t
|
||||
}
|
||||
directories[9] = WindowsTlsBuilder().Build(sourceElf, originalHeaders, image, sections, relocations.BaseRelocations, nextRva);
|
||||
WindowsTrampolineBuilder().Build(trampolines, image, sections, nextRva);
|
||||
auto relocationData = relocationBuilder.BuildBaseRelocations(relocations.BaseRelocations);
|
||||
if (!relocationData.empty()) {
|
||||
directories[5] = {nextRva, CheckedRva(relocationData.size())};
|
||||
sections.push_back({".reloc", nextRva, SectionRead | 0x02000040u, std::move(relocationData)});
|
||||
nextRva = AlignRva(nextRva + sections.back().Data.size());
|
||||
}
|
||||
const WindowsImportBuilder importBuilder;
|
||||
auto nativeImports = importBuilder.Build(nextRva);
|
||||
directories[1] = nativeImports.Directory;
|
||||
@@ -96,11 +92,20 @@ std::vector<std::uint8_t> WindowsPePatcher::Patch(const std::vector<std::uint8_t
|
||||
auto entry = WindowsEntryStubBuilder().Build(nextRva, image.GetEntryRva(), nativeImports, libraries, relocations.Imports, runPath, lazyBinding, dependencyDiagnostics, dynamicSection.GuestModules);
|
||||
directories[3] = entry.ExceptionDirectory;
|
||||
const auto entryRva = entry.Code.Rva;
|
||||
nextRva = AlignRva(entry.Code.Rva + entry.Code.Data.size());
|
||||
sections.push_back(std::move(nativeImports.Section));
|
||||
sections.push_back(std::move(entry.Data));
|
||||
sections.push_back(std::move(entry.Code));
|
||||
for (const auto& lazyStub : entry.LazyStubs)
|
||||
for (const auto& lazyStub : entry.LazyStubs) {
|
||||
writeGotStub(sections, lazyStub.TargetRva, lazyStub.StubRva);
|
||||
relocations.BaseRelocations.push_back(lazyStub.TargetRva);
|
||||
}
|
||||
auto relocationData = relocationBuilder.BuildBaseRelocations(relocations.BaseRelocations);
|
||||
if (!relocationData.empty()) {
|
||||
directories[5] = {nextRva, CheckedRva(relocationData.size())};
|
||||
sections.push_back({".reloc", nextRva, SectionRead | 0x02000040u, std::move(relocationData)});
|
||||
nextRva = AlignRva(nextRva + sections.back().Data.size());
|
||||
}
|
||||
return WindowsPeWriter().Write(sections, entryRva, directories, _windowsGui);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user