From acac847af2107e7486887ce1f332cca90d457058 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 10 Apr 2013 21:03:43 -0700 Subject: [PATCH] Cleanup sceKernelGetModuleIdByAddress(). Actual firmware seems to accept any address in the range, and also correct the error result. Now people won't think this is broken anymore. --- Core/ELF/ElfReader.cpp | 2 +- Core/ELF/ElfReader.h | 6 +++++ Core/HLE/sceKernel.h | 18 +++++++-------- Core/HLE/sceKernelModule.cpp | 43 ++++++++++++++++++++++++------------ 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/Core/ELF/ElfReader.cpp b/Core/ELF/ElfReader.cpp index ea037a40c7..3a934c413b 100644 --- a/Core/ELF/ElfReader.cpp +++ b/Core/ELF/ElfReader.cpp @@ -200,7 +200,7 @@ bool ElfReader::LoadInto(u32 loadAddress) totalEnd = p->p_vaddr + p->p_memsz; } } - u32 totalSize = totalEnd - totalStart; + totalSize = totalEnd - totalStart; if (!bRelocate) { // Binary is prerelocated, load it where the first segment starts diff --git a/Core/ELF/ElfReader.h b/Core/ELF/ElfReader.h index 561543a105..11de4fcc90 100644 --- a/Core/ELF/ElfReader.h +++ b/Core/ELF/ElfReader.h @@ -111,6 +111,11 @@ public: return vaddr; } + u32 GetTotalSize() + { + return totalSize; + } + // More indepth stuff:) bool LoadInto(u32 vaddr); bool LoadSymbols(); @@ -127,6 +132,7 @@ private: u32 *sectionAddrs; bool bRelocate; u32 entryPoint; + u32 totalSize; u32 vaddr; u32 segmentVAddr[32]; }; diff --git a/Core/HLE/sceKernel.h b/Core/HLE/sceKernel.h index ffedb37e3f..6b66a1e914 100644 --- a/Core/HLE/sceKernel.h +++ b/Core/HLE/sceKernel.h @@ -397,22 +397,20 @@ public: return static_cast(pool[realHandle]); } - template - T* GetByModuleByEntryAddr(u32 entryAddr) + template + void Iterate(bool func(T *, ArgT), ArgT arg) { - for (int i = 0; i < maxCount; ++i) + for (int i = 0; i < maxCount; i++) { - T* t = dynamic_cast(pool[i]); - + if (!occupied[i]) + continue; + T *t = dynamic_cast(pool[i]); if (t) { - if (t->nm.entry_addr == entryAddr) - { - return t; - } + if (!func(t, arg)) + break; } } - return 0; } static u32 GetMissingErrorCode() { return -1; } // TODO diff --git a/Core/HLE/sceKernelModule.cpp b/Core/HLE/sceKernelModule.cpp index 48af444eef..bc0b854940 100644 --- a/Core/HLE/sceKernelModule.cpp +++ b/Core/HLE/sceKernelModule.cpp @@ -165,12 +165,14 @@ public: { p.Do(nm); p.Do(memoryBlockAddr); + p.Do(memoryBlockSize); p.DoMarker("Module"); } NativeModule nm; u32 memoryBlockAddr; + u32 memoryBlockSize; bool isFake; }; @@ -316,6 +318,7 @@ Module *__KernelLoadELFFromPtr(const u8 *ptr, u32 loadAddress, std::string *erro return 0; } module->memoryBlockAddr = reader.GetVaddr(); + module->memoryBlockSize = reader.GetTotalSize(); struct libent { @@ -893,24 +896,36 @@ u32 sceKernelStopUnloadSelfModuleWithStatus(u32 moduleId, u32 argSize, u32 argp, return 0; } +struct GetModuleIdByAddressArg +{ + u32 addr; + SceUID result; +}; + +bool __GetModuleIdByAddressIterator(Module *module, GetModuleIdByAddressArg *state) +{ + const u32 start = module->memoryBlockAddr, size = module->memoryBlockSize; + printf("%08x - %08x ? %08x\n", start, start + size, state->addr); + if (start <= state->addr && start + size > state->addr) + { + state->result = module->GetUID(); + return false; + } + return true; +} + u32 sceKernelGetModuleIdByAddress(u32 moduleAddr) { - ERROR_LOG(HLE,"HACKIMPL sceKernelGetModuleIdByAddress(%08x)", moduleAddr); + GetModuleIdByAddressArg state; + state.addr = moduleAddr; + state.result = SCE_KERNEL_ERROR_UNKNOWN_MODULE; - if ((moduleAddr & 0xFFFF0000) == 0x08800000) - { - return mainModuleID; - } + kernelObjects.Iterate(&__GetModuleIdByAddressIterator, &state); + if (state.result == SCE_KERNEL_ERROR_UNKNOWN_MODULE) + ERROR_LOG(HLE, "sceKernelGetModuleIdByAddress(%08x): module not found", moduleAddr) else - { - Module* foundMod= kernelObjects.GetByModuleByEntryAddr(moduleAddr); - - if(foundMod) - { - return foundMod->GetUID(); - } - } - return 0; + DEBUG_LOG(HLE, "%x=sceKernelGetModuleIdByAddress(%08x)", state.result, moduleAddr); + return state.result; } u32 sceKernelGetModuleId()