2006-06-15 04:19:30 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "MIPSAnalysis.h"
|
|
|
|
#include "MIPS.h"
|
|
|
|
|
2013-02-05 03:37:58 +00:00
|
|
|
CMIPSAnalysis::CMIPSAnalysis(CMIPS* ctx)
|
|
|
|
: m_ctx(ctx)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2012-04-01 00:47:39 +00:00
|
|
|
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CMIPSAnalysis::~CMIPSAnalysis()
|
|
|
|
{
|
2006-10-12 06:16:48 +00:00
|
|
|
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMIPSAnalysis::Clear()
|
|
|
|
{
|
2012-04-01 00:47:39 +00:00
|
|
|
m_subroutines.clear();
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 03:37:58 +00:00
|
|
|
void CMIPSAnalysis::Analyse(uint32 start, uint32 end, uint32 entryPoint)
|
|
|
|
{
|
|
|
|
AnalyseSubroutines(start, end, entryPoint);
|
2017-01-21 21:27:58 +00:00
|
|
|
AnalyseStringReferences();
|
2013-02-05 03:37:58 +00:00
|
|
|
}
|
|
|
|
|
2014-08-17 01:42:19 +00:00
|
|
|
void CMIPSAnalysis::InsertSubroutine(uint32 start, uint32 end, uint32 stackAllocStart, uint32 stackAllocEnd, uint32 stackSize, uint32 returnAddrPos)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-08-17 01:42:19 +00:00
|
|
|
assert(FindSubroutine(start) == nullptr);
|
|
|
|
assert(FindSubroutine(end) == nullptr);
|
2012-04-01 00:47:39 +00:00
|
|
|
|
|
|
|
SUBROUTINE subroutine;
|
2014-08-17 01:42:19 +00:00
|
|
|
subroutine.start = start;
|
|
|
|
subroutine.end = end;
|
|
|
|
subroutine.stackAllocStart = stackAllocStart;
|
|
|
|
subroutine.stackAllocEnd = stackAllocEnd;
|
|
|
|
subroutine.stackSize = stackSize;
|
|
|
|
subroutine.returnAddrPos = returnAddrPos;
|
|
|
|
|
|
|
|
m_subroutines.insert(std::make_pair(start, subroutine));
|
2012-04-01 00:47:39 +00:00
|
|
|
}
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-08-17 01:42:19 +00:00
|
|
|
const CMIPSAnalysis::SUBROUTINE* CMIPSAnalysis::FindSubroutine(uint32 address) const
|
2013-02-05 03:37:58 +00:00
|
|
|
{
|
2014-08-17 01:42:19 +00:00
|
|
|
auto subroutineIterator = m_subroutines.lower_bound(address);
|
2013-02-05 03:37:58 +00:00
|
|
|
if(subroutineIterator == std::end(m_subroutines)) return nullptr;
|
|
|
|
|
|
|
|
auto& subroutine = subroutineIterator->second;
|
2014-08-17 01:42:19 +00:00
|
|
|
if(address >= subroutine.start && address <= subroutine.end)
|
2013-02-05 03:37:58 +00:00
|
|
|
{
|
|
|
|
return &subroutine;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 00:47:39 +00:00
|
|
|
void CMIPSAnalysis::ChangeSubroutineStart(uint32 currStart, uint32 newStart)
|
|
|
|
{
|
|
|
|
auto subroutineIterator = m_subroutines.find(currStart);
|
|
|
|
assert(subroutineIterator != std::end(m_subroutines));
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2012-04-01 00:47:39 +00:00
|
|
|
SUBROUTINE subroutine(subroutineIterator->second);
|
2014-08-17 01:42:19 +00:00
|
|
|
subroutine.start = newStart;
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2012-04-01 00:47:39 +00:00
|
|
|
m_subroutines.erase(subroutineIterator);
|
|
|
|
m_subroutines.insert(SubroutineList::value_type(newStart, subroutine));
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
2013-10-27 04:21:49 +00:00
|
|
|
void CMIPSAnalysis::ChangeSubroutineEnd(uint32 start, uint32 newEnd)
|
|
|
|
{
|
|
|
|
assert(start < newEnd);
|
|
|
|
|
|
|
|
auto subroutineIterator = m_subroutines.find(start);
|
|
|
|
assert(subroutineIterator != std::end(m_subroutines));
|
|
|
|
|
|
|
|
auto& subroutine(subroutineIterator->second);
|
2014-08-17 01:42:19 +00:00
|
|
|
subroutine.end = newEnd;
|
2013-10-27 04:21:49 +00:00
|
|
|
}
|
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
void CMIPSAnalysis::AnalyseSubroutines(uint32 start, uint32 end, uint32 entryPoint)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
start &= ~0x3;
|
|
|
|
end &= ~0x3;
|
2007-11-22 19:49:28 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
auto subroutinesBefore = m_subroutines.size();
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
FindSubroutinesByStackAllocation(start, end);
|
|
|
|
FindSubroutinesByJumpTargets(start, end, entryPoint);
|
|
|
|
ExpandSubroutines(start, end);
|
|
|
|
|
2016-06-23 12:52:54 +00:00
|
|
|
uint32 subroutinesAdded = static_cast<uint32>(m_subroutines.size() - subroutinesBefore);
|
2017-05-29 05:01:32 +00:00
|
|
|
printf("CMIPSAnalysis: Found %d subroutines in the range [0x%08X, 0x%08X].\r\n", subroutinesAdded, start, end);
|
2014-08-17 01:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsStackFreeingInstruction(uint32 opcode)
|
|
|
|
{
|
|
|
|
return (opcode & 0xFFFF0000) == 0x27BD0000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMIPSAnalysis::FindSubroutinesByStackAllocation(uint32 start, uint32 end)
|
|
|
|
{
|
|
|
|
uint32 candidate = start;
|
|
|
|
while(candidate != end)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
uint32 returnAddr = 0;
|
|
|
|
uint32 opcode = m_ctx->m_pMemoryMap->GetInstruction(candidate);
|
|
|
|
if((opcode & 0xFFFF0000) == 0x27BD0000)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
//Found the head of a routine (stack allocation)
|
|
|
|
uint32 stackAmount = 0 - (int16)(opcode & 0xFFFF);
|
|
|
|
//Look for a JR RA
|
|
|
|
uint32 tempAddr = candidate;
|
|
|
|
while(tempAddr != end)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
opcode = m_ctx->m_pMemoryMap->GetInstruction(tempAddr);
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2017-03-02 00:29:03 +00:00
|
|
|
//Check SW/SD/SQ RA, 0x????(SP)
|
2014-08-17 01:27:09 +00:00
|
|
|
if(
|
2017-03-02 00:29:03 +00:00
|
|
|
((opcode & 0xFFFF0000) == 0xAFBF0000) || //SW
|
|
|
|
((opcode & 0xFFFF0000) == 0xFFBF0000) || //SD
|
|
|
|
((opcode & 0xFFFF0000) == 0x7FBF0000)) //SQ
|
2014-08-17 01:27:09 +00:00
|
|
|
{
|
|
|
|
returnAddr = (opcode & 0xFFFF);
|
|
|
|
}
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
//Check for JR RA or J
|
|
|
|
if((opcode == 0x03E00008) || ((opcode & 0xFC000000) == 0x08000000))
|
|
|
|
{
|
|
|
|
//Check if there's a stack unwinding instruction above or below
|
2012-04-01 00:47:39 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
//Check above
|
|
|
|
//ADDIU SP, SP, 0x????
|
|
|
|
//JR RA
|
2012-04-01 00:47:39 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
opcode = m_ctx->m_pMemoryMap->GetInstruction(tempAddr - 4);
|
|
|
|
if(IsStackFreeingInstruction(opcode))
|
|
|
|
{
|
|
|
|
if(stackAmount == (int16)(opcode & 0xFFFF))
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
//That's good...
|
|
|
|
InsertSubroutine(candidate, tempAddr + 4, candidate, tempAddr - 4, stackAmount, returnAddr);
|
|
|
|
candidate = tempAddr + 4;
|
|
|
|
break;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
2014-08-17 01:27:09 +00:00
|
|
|
}
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
//Check below
|
|
|
|
//JR RA
|
|
|
|
//ADDIU SP, SP, 0x????
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
opcode = m_ctx->m_pMemoryMap->GetInstruction(tempAddr + 4);
|
|
|
|
if(IsStackFreeingInstruction(opcode))
|
|
|
|
{
|
|
|
|
if(stackAmount == (int16)(opcode & 0xFFFF))
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
//That's good
|
|
|
|
InsertSubroutine(candidate, tempAddr + 4, candidate, tempAddr + 4, stackAmount, returnAddr);
|
|
|
|
candidate = tempAddr + 4;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
2014-08-17 01:27:09 +00:00
|
|
|
break;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
2014-08-17 01:27:09 +00:00
|
|
|
//No stack unwinding was found... just forget about this one
|
|
|
|
//break;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
2014-08-17 01:27:09 +00:00
|
|
|
tempAddr += 4;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-17 01:27:09 +00:00
|
|
|
candidate += 4;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
2014-08-17 01:27:09 +00:00
|
|
|
}
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
void CMIPSAnalysis::FindSubroutinesByJumpTargets(uint32 start, uint32 end, uint32 entryPoint)
|
|
|
|
{
|
2012-04-01 00:47:39 +00:00
|
|
|
//Second pass : Search for all JAL targets then scan for functions
|
2014-08-17 01:27:09 +00:00
|
|
|
std::set<uint32> subroutineAddresses;
|
|
|
|
for(uint32 address = start; address <= end; address += 4)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
uint32 opcode = m_ctx->m_pMemoryMap->GetInstruction(address);
|
|
|
|
if(
|
|
|
|
(opcode & 0xFC000000) == 0x0C000000 ||
|
|
|
|
(opcode & 0xFC000000) == 0x08000000)
|
2012-04-01 00:47:39 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
uint32 jumpTarget = (opcode & 0x03FFFFFF) * 4;
|
|
|
|
if(jumpTarget < start) continue;
|
|
|
|
if(jumpTarget >= end) continue;
|
|
|
|
subroutineAddresses.insert(jumpTarget);
|
2012-04-01 00:47:39 +00:00
|
|
|
}
|
2014-08-17 01:27:09 +00:00
|
|
|
}
|
2006-10-12 06:16:48 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
if(entryPoint != -1)
|
|
|
|
{
|
|
|
|
subroutineAddresses.insert(entryPoint);
|
|
|
|
}
|
2012-05-21 06:31:50 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
for(const auto& subroutineAddress : subroutineAddresses)
|
|
|
|
{
|
|
|
|
if(subroutineAddress == 0) continue;
|
|
|
|
|
|
|
|
//Don't bother if we already found it
|
|
|
|
if(FindSubroutine(subroutineAddress)) continue;
|
|
|
|
|
|
|
|
//Otherwise, try to find a function that already exists
|
|
|
|
for(uint32 address = subroutineAddress; address <= end; address += 4)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
uint32 opcode = m_ctx->m_pMemoryMap->GetInstruction(address);
|
2012-04-01 00:47:39 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
//Check for JR RA or J
|
|
|
|
if((opcode == 0x03E00008) || ((opcode & 0xFC000000) == 0x08000000))
|
|
|
|
{
|
|
|
|
InsertSubroutine(subroutineAddress, address + 4, 0, 0, 0, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto subroutine = FindSubroutine(address);
|
|
|
|
if(subroutine)
|
|
|
|
{
|
|
|
|
//Function already exists, merge.
|
2014-08-17 01:42:19 +00:00
|
|
|
ChangeSubroutineStart(subroutine->start, subroutineAddress);
|
2014-08-17 01:27:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMIPSAnalysis::ExpandSubroutines(uint32 executableStart, uint32 executableEnd)
|
|
|
|
{
|
|
|
|
static const uint32 searchLimit = 0x1000;
|
2012-04-01 00:47:39 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
const auto& findFreeSubroutineEnd =
|
|
|
|
[this](uint32 begin, uint32 end) -> uint32
|
|
|
|
{
|
|
|
|
for(uint32 address = begin; address <= begin + searchLimit; address += 4)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
if(FindSubroutine(address) != nullptr) return MIPS_INVALID_PC;
|
|
|
|
|
|
|
|
uint32 opcode = m_ctx->m_pMemoryMap->GetInstruction(address);
|
2012-04-01 00:47:39 +00:00
|
|
|
|
|
|
|
//Check for JR RA or J
|
2014-08-17 01:27:09 +00:00
|
|
|
if((opcode == 0x03E00008) || ((opcode & 0xFC000000) == 0x08000000))
|
2012-04-01 00:47:39 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
//+4 for delay slot
|
|
|
|
return address + 4;
|
2012-04-01 00:47:39 +00:00
|
|
|
}
|
2015-03-24 02:15:59 +00:00
|
|
|
|
|
|
|
//Check for BEQ R0, R0, $label
|
|
|
|
if((opcode & 0xFFFF0000) == 0x10000000)
|
|
|
|
{
|
|
|
|
//+4 for delay slot
|
|
|
|
return address + 4;
|
|
|
|
}
|
2014-08-17 01:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return MIPS_INVALID_PC;
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto& subroutinePair : m_subroutines)
|
|
|
|
{
|
|
|
|
auto& subroutine = subroutinePair.second;
|
|
|
|
|
|
|
|
//Don't bother if subroutine is not in our range
|
2014-08-17 01:42:19 +00:00
|
|
|
if(subroutine.start < executableStart) continue;
|
|
|
|
if(subroutine.end > executableEnd) continue;
|
2012-04-01 00:47:39 +00:00
|
|
|
|
2014-08-17 01:27:09 +00:00
|
|
|
//Search for branch targets that fall in space not allocated for a subroutine
|
2014-08-17 01:42:19 +00:00
|
|
|
for(uint32 address = subroutine.start; address <= subroutine.end; address += 4)
|
2014-08-17 01:27:09 +00:00
|
|
|
{
|
|
|
|
uint32 opcode = m_ctx->m_pMemoryMap->GetInstruction(address);
|
|
|
|
|
|
|
|
auto branchType = m_ctx->m_pArch->IsInstructionBranch(m_ctx, address, opcode);
|
|
|
|
if(branchType != MIPS_BRANCH_NORMAL) continue;
|
|
|
|
|
|
|
|
uint32 branchTarget = m_ctx->m_pArch->GetInstructionEffectiveAddress(m_ctx, address, opcode);
|
|
|
|
|
|
|
|
//Check if pointing inside our subroutine. If so, don't bother
|
2014-08-17 01:42:19 +00:00
|
|
|
if(branchTarget >= subroutine.start && branchTarget <= subroutine.end) continue;
|
2014-08-17 01:27:09 +00:00
|
|
|
|
|
|
|
//Branch could be out of subroutine range, but that would be weird and we don't want to handle that
|
2014-08-17 01:42:19 +00:00
|
|
|
if(branchTarget < subroutine.start) continue;
|
2014-08-17 01:27:09 +00:00
|
|
|
|
|
|
|
//Check if branch is outside our search limit
|
2014-08-17 01:42:19 +00:00
|
|
|
if(branchTarget > (subroutine.end + searchLimit)) continue;
|
2014-08-17 01:27:09 +00:00
|
|
|
|
|
|
|
//Doesn't make sense if target is outside range
|
|
|
|
if(branchTarget >= executableEnd) continue;
|
|
|
|
|
|
|
|
//If there's already a subroutine there, don't bother
|
|
|
|
if(FindSubroutine(branchTarget) != nullptr) continue;
|
|
|
|
|
|
|
|
uint32 routineEnd = findFreeSubroutineEnd(branchTarget, executableEnd);
|
|
|
|
if(routineEnd == MIPS_INVALID_PC)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check invariant
|
|
|
|
assert(FindSubroutine(routineEnd) == nullptr);
|
|
|
|
|
|
|
|
//Check if we need to update stackAllocEnd
|
|
|
|
uint32 endOpcode = m_ctx->m_pMemoryMap->GetInstruction(routineEnd);
|
|
|
|
|
|
|
|
if(IsStackFreeingInstruction(endOpcode))
|
|
|
|
{
|
|
|
|
uint16 stackAmount = static_cast<int16>(endOpcode & 0xFFFF);
|
2014-08-17 01:42:19 +00:00
|
|
|
if(stackAmount == subroutine.stackSize)
|
2012-04-01 00:47:39 +00:00
|
|
|
{
|
2014-08-17 01:42:19 +00:00
|
|
|
subroutine.stackAllocEnd = std::max<uint32>(subroutine.stackAllocEnd, routineEnd);
|
2012-04-01 00:47:39 +00:00
|
|
|
}
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
2014-08-17 01:27:09 +00:00
|
|
|
|
2014-08-17 01:42:19 +00:00
|
|
|
subroutine.end = std::max<uint32>(subroutine.end, routineEnd);
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-01 00:47:39 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 03:37:58 +00:00
|
|
|
static bool TryGetStringAtAddress(CMIPS* context, uint32 address, std::string& result)
|
2012-04-01 00:47:39 +00:00
|
|
|
{
|
2013-02-05 03:37:58 +00:00
|
|
|
uint8 byteBefore = context->m_pMemoryMap->GetByte(address - 1);
|
|
|
|
if(byteBefore != 0) return false;
|
|
|
|
while(1)
|
2012-04-01 00:47:39 +00:00
|
|
|
{
|
2013-02-05 03:37:58 +00:00
|
|
|
uint8 byte = context->m_pMemoryMap->GetByte(address);
|
|
|
|
if(byte == 0) break;
|
|
|
|
if(byte > 0x7F) return false;
|
|
|
|
if((byte < 0x20) &&
|
|
|
|
(byte != '\t') &&
|
|
|
|
(byte != '\n') &&
|
|
|
|
(byte != '\r'))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
result += byte;
|
|
|
|
address++;
|
2012-04-01 00:47:39 +00:00
|
|
|
}
|
2016-06-23 12:56:08 +00:00
|
|
|
return (result.length() > 1);
|
2013-02-05 03:37:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 21:27:58 +00:00
|
|
|
void CMIPSAnalysis::AnalyseStringReferences()
|
2013-02-05 03:37:58 +00:00
|
|
|
{
|
|
|
|
for(auto subroutinePair : m_subroutines)
|
2012-04-01 00:47:39 +00:00
|
|
|
{
|
2013-02-05 03:37:58 +00:00
|
|
|
const auto& subroutine = subroutinePair.second;
|
|
|
|
uint32 registerValue[0x20] = { 0 };
|
|
|
|
bool registerWritten[0x20] = { false };
|
2014-08-17 01:42:19 +00:00
|
|
|
for(uint32 address = subroutine.start; address <= subroutine.end; address += 4)
|
2013-02-05 03:37:58 +00:00
|
|
|
{
|
2014-08-17 01:27:09 +00:00
|
|
|
uint32 op = m_ctx->m_pMemoryMap->GetInstruction(address);
|
2013-02-05 03:37:58 +00:00
|
|
|
|
|
|
|
//LUI
|
|
|
|
if((op & 0xFC000000) == 0x3C000000)
|
|
|
|
{
|
|
|
|
uint32 rt = (op >> 16) & 0x1F;
|
|
|
|
uint32 imm = static_cast<int16>(op);
|
|
|
|
registerWritten[rt] = true;
|
|
|
|
registerValue[rt] = imm << 16;
|
|
|
|
}
|
|
|
|
//ADDIU
|
|
|
|
else if((op & 0xFC000000) == 0x24000000)
|
|
|
|
{
|
|
|
|
uint32 rs = (op >> 21) & 0x1F;
|
|
|
|
uint32 imm = static_cast<int16>(op);
|
2016-06-23 12:56:08 +00:00
|
|
|
if(registerWritten[rs])
|
2013-02-05 03:37:58 +00:00
|
|
|
{
|
|
|
|
//Check string
|
|
|
|
uint32 targetAddress = registerValue[rs] + imm;
|
|
|
|
registerWritten[rs] = false;
|
2017-01-21 21:27:58 +00:00
|
|
|
|
|
|
|
std::string stringConstant;
|
|
|
|
if(TryGetStringAtAddress(m_ctx, targetAddress, stringConstant))
|
2013-02-05 03:37:58 +00:00
|
|
|
{
|
2017-01-21 21:27:58 +00:00
|
|
|
if(m_ctx->m_Comments.Find(address) == nullptr)
|
2013-02-05 03:37:58 +00:00
|
|
|
{
|
2017-01-21 21:27:58 +00:00
|
|
|
m_ctx->m_Comments.InsertTag(address, stringConstant.c_str());
|
2013-02-05 03:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-01 00:47:39 +00:00
|
|
|
}
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
2012-05-21 06:31:50 +00:00
|
|
|
|
2012-05-24 05:57:31 +00:00
|
|
|
static bool IsValidProgramAddress(uint32 address)
|
|
|
|
{
|
|
|
|
return (address != 0) && ((address & 0x03) == 0);
|
|
|
|
}
|
|
|
|
|
2012-05-21 06:31:50 +00:00
|
|
|
CMIPSAnalysis::CallStackItemArray CMIPSAnalysis::GetCallStack(CMIPS* context, uint32 pc, uint32 sp, uint32 ra)
|
|
|
|
{
|
2015-02-08 02:51:56 +00:00
|
|
|
uint32 physicalSp = context->m_pAddrTranslator(context, sp);
|
|
|
|
|
2012-05-21 06:31:50 +00:00
|
|
|
CallStackItemArray result;
|
|
|
|
|
|
|
|
{
|
2014-08-27 05:56:32 +00:00
|
|
|
auto routine = context->m_analysis->FindSubroutine(pc);
|
|
|
|
if(!routine)
|
2012-05-24 05:57:31 +00:00
|
|
|
{
|
2014-08-27 05:56:32 +00:00
|
|
|
if(IsValidProgramAddress(pc)) result.push_back(pc);
|
|
|
|
if(pc != ra)
|
|
|
|
{
|
|
|
|
if(IsValidProgramAddress(ra)) result.push_back(ra);
|
|
|
|
}
|
|
|
|
return result;
|
2012-05-24 05:57:31 +00:00
|
|
|
}
|
2012-05-21 06:31:50 +00:00
|
|
|
|
2014-08-27 05:56:32 +00:00
|
|
|
//We need to get to a state where we're ready to dig into the previous function's
|
|
|
|
//stack
|
2012-05-21 06:31:50 +00:00
|
|
|
|
2014-08-27 05:56:32 +00:00
|
|
|
//Check if we need to check into the stack to get the RA
|
|
|
|
if(context->m_analysis->FindSubroutine(ra) == routine)
|
|
|
|
{
|
2015-02-08 02:51:56 +00:00
|
|
|
ra = context->m_pMemoryMap->GetWord(physicalSp + routine->returnAddrPos);
|
|
|
|
physicalSp += routine->stackSize;
|
2014-08-27 05:56:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//We haven't called a sub routine yet... The RA is good, but we
|
|
|
|
//don't know wether stack memory has been allocated or not
|
2012-05-21 06:31:50 +00:00
|
|
|
|
2014-08-27 05:56:32 +00:00
|
|
|
//ADDIU SP, SP, 0x????
|
|
|
|
//If the PC is after this instruction, then, we've allocated stack
|
2012-05-21 06:31:50 +00:00
|
|
|
|
2014-08-27 05:56:32 +00:00
|
|
|
if(pc > routine->stackAllocStart)
|
2012-05-21 06:31:50 +00:00
|
|
|
{
|
2014-08-27 05:56:32 +00:00
|
|
|
if(pc <= routine->stackAllocEnd)
|
|
|
|
{
|
2015-02-08 02:51:56 +00:00
|
|
|
physicalSp += routine->stackSize;
|
2014-08-27 05:56:32 +00:00
|
|
|
}
|
2012-05-21 06:31:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
//Add the current function
|
2012-05-24 05:57:31 +00:00
|
|
|
result.push_back(pc);
|
2012-05-21 06:31:50 +00:00
|
|
|
|
|
|
|
//Go to previous routine
|
2012-05-24 05:57:31 +00:00
|
|
|
pc = ra;
|
2012-05-21 06:31:50 +00:00
|
|
|
|
|
|
|
//Check if we can go on...
|
2014-08-27 05:56:32 +00:00
|
|
|
auto routine = context->m_analysis->FindSubroutine(pc);
|
2012-05-24 05:57:31 +00:00
|
|
|
if(!routine)
|
|
|
|
{
|
|
|
|
if(IsValidProgramAddress(ra)) result.push_back(ra);
|
|
|
|
break;
|
|
|
|
}
|
2012-05-21 06:31:50 +00:00
|
|
|
|
|
|
|
//Get the next RA
|
2015-02-08 02:51:56 +00:00
|
|
|
ra = context->m_pMemoryMap->GetWord(physicalSp + routine->returnAddrPos);
|
|
|
|
physicalSp += routine->stackSize;
|
2014-08-27 05:56:32 +00:00
|
|
|
|
|
|
|
if((pc == ra) && (routine->stackSize == 0))
|
|
|
|
{
|
|
|
|
if(IsValidProgramAddress(ra)) result.push_back(ra);
|
|
|
|
break;
|
|
|
|
}
|
2012-05-21 06:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|