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 diff --git a/Core/MIPS/MIPSAnalyst.cpp b/Core/MIPS/MIPSAnalyst.cpp index 6af55b0af3..4bfb0e0aeb 100644 --- a/Core/MIPS/MIPSAnalyst.cpp +++ b/Core/MIPS/MIPSAnalyst.cpp @@ -238,6 +238,63 @@ 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. + // 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}; @@ -249,7 +306,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; } @@ -260,11 +326,37 @@ 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 (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; } @@ -273,14 +365,22 @@ 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 knownEnd = furthestBranch == 0 ? addr : furthestBranch; + u32 jumpback = ScanAheadForJumpback(sureTarget, currentFunction.start, knownEnd); + if (jumpback != INVALIDTARGET && jumpback > addr && jumpback > knownEnd) { + furthestBranch = jumpback; + } } - sureTarget = GetJumpTarget(addr); - if (sureTarget != INVALIDTARGET && sureTarget < addr && ((op&0xFC000000)==0x08000000)) { - end = true; - } - //end = true; } } if (end) {