From eac8b1a84d27e75b040173162a0ceaf4d718bde8 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 1 Dec 2013 19:43:40 -0800 Subject: [PATCH 1/5] Scan for functions even without a .text section. It's possible this may be wrong, but every game I've seen loads the funcs in this area. Sometimes there's data after the funcs before the stubs, but it's better than no funcs. --- Core/ELF/ElfReader.cpp | 2 +- Core/HLE/sceKernelModule.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Core/ELF/ElfReader.cpp b/Core/ELF/ElfReader.cpp index 0f2a29f9be..f7c0f7ccc6 100644 --- a/Core/ELF/ElfReader.cpp +++ b/Core/ELF/ElfReader.cpp @@ -29,7 +29,7 @@ const char *ElfReader::GetSectionName(int section) return 0; int nameOffset = sections[section].sh_name; - char *ptr = (char*)GetSectionDataPtr(header->e_shstrndx); + const char *ptr = (const char *)GetSectionDataPtr(header->e_shstrndx); if (ptr) return ptr + nameOffset; diff --git a/Core/HLE/sceKernelModule.cpp b/Core/HLE/sceKernelModule.cpp index 4202611707..fef5918b64 100644 --- a/Core/HLE/sceKernelModule.cpp +++ b/Core/HLE/sceKernelModule.cpp @@ -838,6 +838,8 @@ Module *__KernelLoadELFFromPtr(const u8 *ptr, u32 loadAddress, std::string *erro u32_le *entryPos = (u32_le *)Memory::GetPointer(modinfo->libstub); u32_le *entryEnd = (u32_le *)Memory::GetPointer(modinfo->libstubend); + u32_le firstImportStubAddr = 0; + bool needReport = false; while (entryPos < entryEnd) { PspLibStubEntry *entry = (PspLibStubEntry *)entryPos; @@ -882,6 +884,9 @@ Module *__KernelLoadELFFromPtr(const u8 *ptr, u32 loadAddress, std::string *erro func.stubAddr = entry->firstSymAddr + i * 8; module->ImportFunc(func); } + + if (firstImportStubAddr == NULL || firstImportStubAddr > entry->firstSymAddr) + firstImportStubAddr = entry->firstSymAddr; } else if (entry->numFuncs > 0) { WARN_LOG_REPORT(LOADER, "Module entry with %d imports but no valid address", entry->numFuncs); needReport = true; @@ -945,6 +950,15 @@ Module *__KernelLoadELFFromPtr(const u8 *ptr, u32 loadAddress, std::string *erro Reporting::ReportMessage("Module linking debug info:\n%s", debugInfo.c_str()); } + if (textSection == -1) { + u32 textStart = reader.GetVaddr(); + u32 textEnd = firstImportStubAddr - 4; +#if !defined(USING_GLES2) + if (!reader.LoadSymbols()) + MIPSAnalyst::ScanForFunctions(textStart, textEnd); +#endif + } + // Look at the exports, too. struct PspLibEntEntry From b2971314b83e192a8cc4489212a8a6d6aab1ddfa Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 1 Dec 2013 20:23:16 -0800 Subject: [PATCH 2/5] Fix hash detection of branches to jr ra. This makes it more correctly identify functions. --- Core/MIPS/MIPSAnalyst.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Core/MIPS/MIPSAnalyst.cpp b/Core/MIPS/MIPSAnalyst.cpp index 6af55b0af3..9b215a48f3 100644 --- a/Core/MIPS/MIPSAnalyst.cpp +++ b/Core/MIPS/MIPSAnalyst.cpp @@ -262,9 +262,10 @@ namespace MIPSAnalyst { } } if (op == MIPS_MAKE_JR_RA()) { - if (furthestBranch >= addr) { + // If a branch goes to the jr ra, it's still ending here. + if (furthestBranch > addr) { looking = true; - addr+=4; + addr += 4; } else { end = true; } From 43be44947f822586911ea7b29801ee75773824fa Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 1 Dec 2013 20:25:51 -0800 Subject: [PATCH 3/5] Correctly reuse symbol map entries when scanning. Before we created one huge function in place of the old. --- Core/MIPS/MIPSAnalyst.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Core/MIPS/MIPSAnalyst.cpp b/Core/MIPS/MIPSAnalyst.cpp index 9b215a48f3..a4044069cd 100644 --- a/Core/MIPS/MIPSAnalyst.cpp +++ b/Core/MIPS/MIPSAnalyst.cpp @@ -249,7 +249,16 @@ namespace MIPSAnalyst { for (addr = startAddr; addr <= endAddr; addr+=4) { SymbolInfo syminfo; if (symbolMap.GetSymbolInfo(&syminfo, addr, ST_FUNCTION)) { - addr = syminfo.address + syminfo.size; + addr = syminfo.address + syminfo.size - 4; + + // We still need to insert the func for hashing purposes. + currentFunction.start = syminfo.address; + currentFunction.end = syminfo.address + syminfo.size - 4; + functions.push_back(currentFunction); + currentFunction.start = addr + 4; + furthestBranch = 0; + looking = false; + end = false; continue; } From 1bccfa4141599a19c7d0c319b8cffbf800dd9141 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 1 Dec 2013 21:28:09 -0800 Subject: [PATCH 4/5] Look for downward jumps for elses when scanning. Otherwise, we often end the function earlier than it should be. --- Core/MIPS/MIPSAnalyst.cpp | 68 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/Core/MIPS/MIPSAnalyst.cpp b/Core/MIPS/MIPSAnalyst.cpp index a4044069cd..97edce9cfb 100644 --- a/Core/MIPS/MIPSAnalyst.cpp +++ b/Core/MIPS/MIPSAnalyst.cpp @@ -238,6 +238,57 @@ namespace MIPSAnalyst { } } + static u32 ScanAheadForJumpback(u32 fromAddr, u32 knownStart, u32 knownEnd) { + static const u32 MAX_AHEAD_SCAN = 0x1000; + + // Code might jump halfway up to before fromAddr, but after knownEnd. + // In that area, there could be another jump up to the valid range. + // So we track that for a second scan. + u32 closestJumpbackAddr = INVALIDTARGET; + u32 closestJumpbackTarget = fromAddr; + + // We assume the furthest jumpback is within the func. + u32 furthestJumpbackAddr = INVALIDTARGET; + + for (u32 ahead = fromAddr; ahead < fromAddr + MAX_AHEAD_SCAN; ahead += 4) { + MIPSOpcode aheadOp = Memory::Read_Instruction(ahead); + u32 target = GetBranchTargetNoRA(ahead); + if (target == INVALIDTARGET && ((aheadOp & 0xFC000000) == 0x08000000)) { + target = GetJumpTarget(ahead); + } + + if (target != INVALIDTARGET) { + // Only if it comes back up to known code within this func. + if (target >= knownStart && target <= knownEnd) { + furthestJumpbackAddr = ahead; + } + // But if it jumps above fromAddr, we should scan that area too... + if (target < closestJumpbackTarget && target < fromAddr && target > knownEnd) { + closestJumpbackAddr = ahead; + closestJumpbackTarget = target; + } + } + } + + if (closestJumpbackAddr != INVALIDTARGET && furthestJumpbackAddr == INVALIDTARGET) { + for (u32 behind = closestJumpbackTarget; behind < fromAddr; behind += 4) { + MIPSOpcode behindOp = Memory::Read_Instruction(behind); + u32 target = GetBranchTargetNoRA(behind); + if (target == INVALIDTARGET && ((behindOp & 0xFC000000) == 0x08000000)) { + target = GetJumpTarget(behind); + } + + if (target != INVALIDTARGET) { + if (target >= knownStart && target <= knownEnd) { + furthestJumpbackAddr = closestJumpbackAddr; + } + } + } + } + + return furthestJumpbackAddr; + } + void ScanForFunctions(u32 startAddr, u32 endAddr /*, std::vector knownEntries*/) { Function currentFunction = {startAddr}; @@ -283,14 +334,21 @@ namespace MIPSAnalyst { if (looking) { if (addr >= furthestBranch) { u32 sureTarget = GetSureBranchTarget(addr); + // Regular j only, jals are to new funcs. + if (sureTarget == INVALIDTARGET && ((op & 0xFC000000) == 0x08000000)) { + sureTarget = GetJumpTarget(addr); + } + if (sureTarget != INVALIDTARGET && sureTarget < addr) { end = true; + } else if (sureTarget != INVALIDTARGET) { + // Okay, we have a downward jump. Might be an else or a tail call... + // If there's a jump back upward in spitting distance of it, it's an else. + u32 jumpback = ScanAheadForJumpback(sureTarget, currentFunction.start, furthestBranch); + if (jumpback != INVALIDTARGET && jumpback > addr && jumpback > furthestBranch) { + furthestBranch = jumpback; + } } - sureTarget = GetJumpTarget(addr); - if (sureTarget != INVALIDTARGET && sureTarget < addr && ((op&0xFC000000)==0x08000000)) { - end = true; - } - //end = true; } } if (end) { From 505f509503d2a436fbb175d5504cf4c4bb937386 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 1 Dec 2013 21:56:28 -0800 Subject: [PATCH 5/5] Detect tail call when scanning functions. --- Core/MIPS/MIPSAnalyst.cpp | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/Core/MIPS/MIPSAnalyst.cpp b/Core/MIPS/MIPSAnalyst.cpp index 97edce9cfb..4bfb0e0aeb 100644 --- a/Core/MIPS/MIPSAnalyst.cpp +++ b/Core/MIPS/MIPSAnalyst.cpp @@ -240,6 +240,12 @@ namespace MIPSAnalyst { static u32 ScanAheadForJumpback(u32 fromAddr, u32 knownStart, u32 knownEnd) { static const u32 MAX_AHEAD_SCAN = 0x1000; + // Maybe a bit high... just to make sure we don't get confused by recursive tail recursion. + static const u32 MAX_FUNC_SIZE = 0x20000; + + if (fromAddr > knownEnd + MAX_FUNC_SIZE) { + return INVALIDTARGET; + } // Code might jump halfway up to before fromAddr, but after knownEnd. // In that area, there could be another jump up to the valid range. @@ -320,6 +326,31 @@ namespace MIPSAnalyst { if (target > furthestBranch) { furthestBranch = target; } + } else if ((op & 0xFC000000) == 0x08000000) { + u32 sureTarget = GetJumpTarget(addr); + // Check for a tail call. Might not even have a jr ra. + if (sureTarget != INVALIDTARGET && sureTarget < currentFunction.start) { + if (furthestBranch > addr) { + looking = true; + addr += 4; + } else { + end = true; + } + } else if (sureTarget != INVALIDTARGET && sureTarget > addr && sureTarget > furthestBranch) { + // A jump later. Probably tail, but let's check if it jumps back. + u32 knownEnd = furthestBranch == 0 ? addr : furthestBranch; + u32 jumpback = ScanAheadForJumpback(sureTarget, currentFunction.start, knownEnd); + if (jumpback != INVALIDTARGET && jumpback > addr && jumpback > knownEnd) { + furthestBranch = jumpback; + } else { + if (furthestBranch > addr) { + looking = true; + addr += 4; + } else { + end = true; + } + } + } } if (op == MIPS_MAKE_JR_RA()) { // If a branch goes to the jr ra, it's still ending here. @@ -344,8 +375,9 @@ namespace MIPSAnalyst { } else if (sureTarget != INVALIDTARGET) { // Okay, we have a downward jump. Might be an else or a tail call... // If there's a jump back upward in spitting distance of it, it's an else. - u32 jumpback = ScanAheadForJumpback(sureTarget, currentFunction.start, furthestBranch); - if (jumpback != INVALIDTARGET && jumpback > addr && jumpback > furthestBranch) { + u32 knownEnd = furthestBranch == 0 ? addr : furthestBranch; + u32 jumpback = ScanAheadForJumpback(sureTarget, currentFunction.start, knownEnd); + if (jumpback != INVALIDTARGET && jumpback > addr && jumpback > knownEnd) { furthestBranch = jumpback; } }