mirror of
				https://github.com/hedge-dev/XenonRecomp.git
				synced 2025-11-04 06:47:09 +00:00 
			
		
		
		
	Rebrand to XenonRecomp.
This commit is contained in:
		
							
								
								
									
										14
									
								
								XenonAnalyse/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								XenonAnalyse/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
# cmake_minimum_required (VERSION 3.16)
 | 
			
		||||
 | 
			
		||||
project("XenonAnalyse")
 | 
			
		||||
 | 
			
		||||
add_executable(XenonAnalyse 
 | 
			
		||||
    "main.cpp" 
 | 
			
		||||
    "function.cpp")
 | 
			
		||||
 | 
			
		||||
target_link_libraries(XenonAnalyse PRIVATE XenonUtils fmt::fmt)
 | 
			
		||||
 | 
			
		||||
add_library(LibXenonAnalyse "function.cpp")
 | 
			
		||||
target_include_directories(LibXenonAnalyse PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
 | 
			
		||||
target_link_libraries(LibXenonAnalyse PUBLIC XenonUtils)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										246
									
								
								XenonAnalyse/function.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										246
									
								
								XenonAnalyse/function.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,246 @@
 | 
			
		||||
#include "function.h"
 | 
			
		||||
#include <disasm.h>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <bit>
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <cassert>
 | 
			
		||||
#include <byteswap.h>
 | 
			
		||||
 | 
			
		||||
size_t Function::SearchBlock(size_t address) const
 | 
			
		||||
{
 | 
			
		||||
    if (address < base)
 | 
			
		||||
    {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (size_t i = 0; i < blocks.size(); i++)
 | 
			
		||||
    {
 | 
			
		||||
        const auto& block = blocks[i];
 | 
			
		||||
        const auto begin = base + block.base;
 | 
			
		||||
        const auto end = begin + block.size;
 | 
			
		||||
 | 
			
		||||
        if (begin != end)
 | 
			
		||||
        {
 | 
			
		||||
            if (address >= begin && address < end)
 | 
			
		||||
            {
 | 
			
		||||
                return i;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else // fresh block
 | 
			
		||||
        {
 | 
			
		||||
            if (address == begin)
 | 
			
		||||
            {
 | 
			
		||||
                return i;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Function Function::Analyze(const void* code, size_t size, size_t base)
 | 
			
		||||
{
 | 
			
		||||
    Function fn{ base, 0 };
 | 
			
		||||
 | 
			
		||||
    if (*((uint32_t*)code + 1) == 0x04000048) // shifted ptr tail call
 | 
			
		||||
    {
 | 
			
		||||
        fn.size = 0x8;
 | 
			
		||||
        return fn;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto& blocks = fn.blocks;
 | 
			
		||||
    blocks.reserve(8);
 | 
			
		||||
    blocks.emplace_back();
 | 
			
		||||
 | 
			
		||||
    const auto* data = (uint32_t*)code;
 | 
			
		||||
    const auto* dataStart = data;
 | 
			
		||||
    const auto* dataEnd = (uint32_t*)((uint8_t*)code + size);
 | 
			
		||||
    std::vector<size_t> blockStack{};
 | 
			
		||||
    blockStack.reserve(32);
 | 
			
		||||
    blockStack.emplace_back();
 | 
			
		||||
 | 
			
		||||
    #define RESTORE_DATA() if (!blockStack.empty()) data = (dataStart + ((blocks[blockStack.back()].base + blocks[blockStack.back()].size) / sizeof(*data))) - 1; // continue adds one
 | 
			
		||||
 | 
			
		||||
    // TODO: Branch fallthrough
 | 
			
		||||
    for (; data <= dataEnd ; ++data)
 | 
			
		||||
    {
 | 
			
		||||
        const size_t addr = base + ((data - dataStart) * sizeof(*data));
 | 
			
		||||
        if (blockStack.empty())
 | 
			
		||||
        {
 | 
			
		||||
            break; // it's hideover
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        auto& curBlock = blocks[blockStack.back()];
 | 
			
		||||
        DEBUG(const auto blockBase = curBlock.base);
 | 
			
		||||
        const uint32_t instruction = ByteSwap(*data);
 | 
			
		||||
 | 
			
		||||
        const uint32_t op = PPC_OP(instruction);
 | 
			
		||||
        const uint32_t xop = PPC_XOP(instruction);
 | 
			
		||||
        const uint32_t isLink = PPC_BL(instruction); // call
 | 
			
		||||
 | 
			
		||||
        ppc_insn insn;
 | 
			
		||||
        ppc::Disassemble(data, addr, insn);
 | 
			
		||||
 | 
			
		||||
        // Sanity check
 | 
			
		||||
        assert(addr == base + curBlock.base  + curBlock.size);
 | 
			
		||||
        if (curBlock.projectedSize != -1 && curBlock.size >= curBlock.projectedSize) // fallthrough
 | 
			
		||||
        {
 | 
			
		||||
            blockStack.pop_back();
 | 
			
		||||
            RESTORE_DATA();
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        curBlock.size += 4;
 | 
			
		||||
        if (op == PPC_OP_BC) // conditional branches all originate from one opcode, thanks RISC
 | 
			
		||||
        {
 | 
			
		||||
            if (isLink) // just a conditional call, nothing to see here
 | 
			
		||||
            {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // TODO: carry projections over to false
 | 
			
		||||
            curBlock.projectedSize = -1;
 | 
			
		||||
            blockStack.pop_back();
 | 
			
		||||
 | 
			
		||||
            // TODO: Handle absolute branches?
 | 
			
		||||
            assert(!PPC_BA(instruction));
 | 
			
		||||
            const size_t branchDest = addr + PPC_BD(instruction);
 | 
			
		||||
 | 
			
		||||
            // true/false paths
 | 
			
		||||
            // left block: false case
 | 
			
		||||
            // right block: true case
 | 
			
		||||
            const size_t lBase = (addr - base) + 4;
 | 
			
		||||
            const size_t rBase = (addr + PPC_BD(instruction)) - base;
 | 
			
		||||
 | 
			
		||||
            // these will be -1 if it's our first time seeing these blocks
 | 
			
		||||
            auto lBlock = fn.SearchBlock(base + lBase);
 | 
			
		||||
 | 
			
		||||
            if (lBlock == -1)
 | 
			
		||||
            {
 | 
			
		||||
                blocks.emplace_back(lBase, 0).projectedSize = rBase - lBase;
 | 
			
		||||
                lBlock = blocks.size() - 1;
 | 
			
		||||
 | 
			
		||||
                // push this first, this gets overriden by the true case as it'd be further away
 | 
			
		||||
                DEBUG(blocks[lBlock].parent = blockBase);
 | 
			
		||||
                blockStack.emplace_back(lBlock);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            size_t rBlock = fn.SearchBlock(base + rBase);
 | 
			
		||||
            if (rBlock == -1)
 | 
			
		||||
            {
 | 
			
		||||
                blocks.emplace_back(branchDest - base, 0);
 | 
			
		||||
                rBlock = blocks.size() - 1;
 | 
			
		||||
 | 
			
		||||
                DEBUG(blocks[rBlock].parent = blockBase);
 | 
			
		||||
                blockStack.emplace_back(rBlock);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            RESTORE_DATA();
 | 
			
		||||
        }
 | 
			
		||||
        else if (op == PPC_OP_B || instruction == 0 || (op == PPC_OP_CTR && (xop == 16 || xop == 528))) // b, blr, end padding
 | 
			
		||||
        {
 | 
			
		||||
            if (!isLink)
 | 
			
		||||
            {
 | 
			
		||||
                blockStack.pop_back();
 | 
			
		||||
 | 
			
		||||
                if (op == PPC_OP_B)
 | 
			
		||||
                {
 | 
			
		||||
                    assert(!PPC_BA(instruction));
 | 
			
		||||
                    const size_t branchDest = addr + PPC_BI(instruction);
 | 
			
		||||
 | 
			
		||||
                    const size_t branchBase = branchDest - base;
 | 
			
		||||
                    const size_t branchBlock = fn.SearchBlock(branchDest);
 | 
			
		||||
 | 
			
		||||
                    if (branchDest < base)
 | 
			
		||||
                    {
 | 
			
		||||
                        // Branches before base are just tail calls, no need to chase after those
 | 
			
		||||
                        RESTORE_DATA();
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // carry over our projection if blocks are next to each other
 | 
			
		||||
                    const bool isContinuous = branchBase == curBlock.base + curBlock.size;
 | 
			
		||||
                    size_t sizeProjection = (size_t)-1;
 | 
			
		||||
 | 
			
		||||
                    if (curBlock.projectedSize != -1 && isContinuous)
 | 
			
		||||
                    {
 | 
			
		||||
                        sizeProjection = curBlock.projectedSize - curBlock.size;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    if (branchBlock == -1)
 | 
			
		||||
                    {
 | 
			
		||||
                        blocks.emplace_back(branchBase, 0, sizeProjection);
 | 
			
		||||
 | 
			
		||||
                        blockStack.emplace_back(blocks.size() - 1);
 | 
			
		||||
                        
 | 
			
		||||
                        DEBUG(blocks.back().parent = blockBase);
 | 
			
		||||
                        RESTORE_DATA();
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                else if (op == PPC_OP_CTR)
 | 
			
		||||
                {
 | 
			
		||||
                    // 5th bit of BO tells cpu to ignore the counter, which is a blr/bctr otherwise it's conditional
 | 
			
		||||
                    const bool conditional = !(PPC_BO(instruction) & 0x10);
 | 
			
		||||
                    if (conditional)
 | 
			
		||||
                    {
 | 
			
		||||
                        // right block's just going to return
 | 
			
		||||
                        const size_t lBase = (addr - base) + 4;
 | 
			
		||||
                        size_t lBlock = fn.SearchBlock(lBase);
 | 
			
		||||
                        if (lBlock == -1)
 | 
			
		||||
                        {
 | 
			
		||||
                            blocks.emplace_back(lBase, 0);
 | 
			
		||||
                            lBlock = blocks.size() - 1;
 | 
			
		||||
 | 
			
		||||
                            DEBUG(blocks[lBlock].parent = blockBase);
 | 
			
		||||
                            blockStack.emplace_back(lBlock);
 | 
			
		||||
                            RESTORE_DATA();
 | 
			
		||||
                            continue;
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                RESTORE_DATA();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else if (insn.opcode == nullptr)
 | 
			
		||||
        {
 | 
			
		||||
            blockStack.pop_back();
 | 
			
		||||
            RESTORE_DATA();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Sort and invalidate discontinuous blocks
 | 
			
		||||
    if (blocks.size() > 1)
 | 
			
		||||
    {
 | 
			
		||||
        std::sort(blocks.begin(), blocks.end(), [](const Block& a, const Block& b)
 | 
			
		||||
        {
 | 
			
		||||
            return a.base < b.base;
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        size_t discontinuity = -1;
 | 
			
		||||
        for (size_t i = 0; i < blocks.size() - 1; i++)
 | 
			
		||||
        {
 | 
			
		||||
            if (blocks[i].base + blocks[i].size >= blocks[i + 1].base)
 | 
			
		||||
            {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            discontinuity = i + 1;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (discontinuity != -1)
 | 
			
		||||
        {
 | 
			
		||||
            blocks.erase(blocks.begin() + discontinuity, blocks.end());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn.size = 0;
 | 
			
		||||
    for (const auto& block : blocks)
 | 
			
		||||
    {
 | 
			
		||||
        // pick the block furthest away
 | 
			
		||||
        fn.size = std::max(fn.size, block.base + block.size);
 | 
			
		||||
    }
 | 
			
		||||
    return fn;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										51
									
								
								XenonAnalyse/function.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								XenonAnalyse/function.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <cstddef>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
#ifdef _DEBUG
 | 
			
		||||
#define DEBUG(X) X
 | 
			
		||||
#else
 | 
			
		||||
#define DEBUG(X)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
struct Function
 | 
			
		||||
{
 | 
			
		||||
    struct Block
 | 
			
		||||
    {
 | 
			
		||||
        size_t base{};
 | 
			
		||||
        size_t size{};
 | 
			
		||||
        size_t projectedSize{ static_cast<size_t>(-1) }; // scratch
 | 
			
		||||
        DEBUG(size_t parent{});
 | 
			
		||||
 | 
			
		||||
        Block() 
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Block(size_t base, size_t size)
 | 
			
		||||
            : base(base), size(size) 
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Block(size_t base, size_t size, size_t projectedSize) 
 | 
			
		||||
            : base(base), size(size), projectedSize(projectedSize)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    size_t base{};
 | 
			
		||||
    size_t size{};
 | 
			
		||||
    std::vector<Block> blocks{};
 | 
			
		||||
 | 
			
		||||
    Function()
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Function(size_t base, size_t size)
 | 
			
		||||
        : base(base), size(size)
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    size_t SearchBlock(size_t address) const;
 | 
			
		||||
    static Function Analyze(const void* code, size_t size, size_t base);
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										453
									
								
								XenonAnalyse/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										453
									
								
								XenonAnalyse/main.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,453 @@
 | 
			
		||||
#include <cassert>
 | 
			
		||||
#include <iterator>
 | 
			
		||||
#include <file.h>
 | 
			
		||||
#include <disasm.h>
 | 
			
		||||
#include <image.h>
 | 
			
		||||
#include <xbox.h>
 | 
			
		||||
#include <fmt/core.h>
 | 
			
		||||
#include "function.h"
 | 
			
		||||
 | 
			
		||||
#define SWITCH_ABSOLUTE 0
 | 
			
		||||
#define SWITCH_COMPUTED 1
 | 
			
		||||
#define SWITCH_BYTEOFFSET 2
 | 
			
		||||
#define SWITCH_SHORTOFFSET 3
 | 
			
		||||
 | 
			
		||||
struct SwitchTable
 | 
			
		||||
{
 | 
			
		||||
    std::vector<size_t> labels{};
 | 
			
		||||
    size_t base{};
 | 
			
		||||
    size_t defaultLabel{};
 | 
			
		||||
    uint32_t r{};
 | 
			
		||||
    uint32_t type{};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void ReadTable(Image& image, SwitchTable& table)
 | 
			
		||||
{
 | 
			
		||||
    uint32_t pOffset;
 | 
			
		||||
    ppc_insn insn;
 | 
			
		||||
    auto* code = (uint32_t*)image.Find(table.base);
 | 
			
		||||
    ppc::Disassemble(code, table.base, insn);
 | 
			
		||||
    pOffset = insn.operands[1] << 16;
 | 
			
		||||
 | 
			
		||||
    ppc::Disassemble(code + 1, table.base + 4, insn);
 | 
			
		||||
    pOffset += insn.operands[2];
 | 
			
		||||
 | 
			
		||||
    if (table.type == SWITCH_ABSOLUTE)
 | 
			
		||||
    {
 | 
			
		||||
        const auto* offsets = (be<uint32_t>*)image.Find(pOffset);
 | 
			
		||||
        for (size_t i = 0; i < table.labels.size(); i++)
 | 
			
		||||
        {
 | 
			
		||||
            table.labels[i] = offsets[i];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else if (table.type == SWITCH_COMPUTED)
 | 
			
		||||
    {
 | 
			
		||||
        uint32_t base;
 | 
			
		||||
        uint32_t shift;
 | 
			
		||||
        const auto* offsets = (uint8_t*)image.Find(pOffset);
 | 
			
		||||
 | 
			
		||||
        ppc::Disassemble(code + 4, table.base + 0x10, insn);
 | 
			
		||||
        base = insn.operands[1] << 16;
 | 
			
		||||
 | 
			
		||||
        ppc::Disassemble(code + 5, table.base + 0x14, insn);
 | 
			
		||||
        base += insn.operands[2];
 | 
			
		||||
 | 
			
		||||
        ppc::Disassemble(code + 3, table.base + 0x0C, insn);
 | 
			
		||||
        shift = insn.operands[2];
 | 
			
		||||
 | 
			
		||||
        for (size_t i = 0; i < table.labels.size(); i++)
 | 
			
		||||
        {
 | 
			
		||||
            table.labels[i] = base + (offsets[i] << shift);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else if (table.type == SWITCH_BYTEOFFSET || table.type == SWITCH_SHORTOFFSET)
 | 
			
		||||
    {
 | 
			
		||||
        if (table.type == SWITCH_BYTEOFFSET)
 | 
			
		||||
        {
 | 
			
		||||
            const auto* offsets = (uint8_t*)image.Find(pOffset);
 | 
			
		||||
            uint32_t base;
 | 
			
		||||
 | 
			
		||||
            ppc::Disassemble(code + 3, table.base + 0x0C, insn);
 | 
			
		||||
            base = insn.operands[1] << 16;
 | 
			
		||||
 | 
			
		||||
            ppc::Disassemble(code + 4, table.base + 0x10, insn);
 | 
			
		||||
            base += insn.operands[2];
 | 
			
		||||
 | 
			
		||||
            for (size_t i = 0; i < table.labels.size(); i++)
 | 
			
		||||
            {
 | 
			
		||||
                table.labels[i] = base + offsets[i];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else if (table.type == SWITCH_SHORTOFFSET)
 | 
			
		||||
        {
 | 
			
		||||
            const auto* offsets = (be<uint16_t>*)image.Find(pOffset);
 | 
			
		||||
            uint32_t base;
 | 
			
		||||
 | 
			
		||||
            ppc::Disassemble(code + 4, table.base + 0x10, insn);
 | 
			
		||||
            base = insn.operands[1] << 16;
 | 
			
		||||
 | 
			
		||||
            ppc::Disassemble(code + 5, table.base + 0x14, insn);
 | 
			
		||||
            base += insn.operands[2];
 | 
			
		||||
 | 
			
		||||
            for (size_t i = 0; i < table.labels.size(); i++)
 | 
			
		||||
            {
 | 
			
		||||
                table.labels[i] = base + offsets[i];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        assert(false);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ScanTable(const uint32_t* code, size_t base, SwitchTable& table)
 | 
			
		||||
{
 | 
			
		||||
    ppc_insn insn;
 | 
			
		||||
    uint32_t cr{ (uint32_t)-1 };
 | 
			
		||||
    for (int i = 0; i < 32; i++)
 | 
			
		||||
    {
 | 
			
		||||
        ppc::Disassemble(&code[-i], base - (4 * i), insn);
 | 
			
		||||
        if (insn.opcode == nullptr)
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (cr == -1 && (insn.opcode->id == PPC_INST_BGT || insn.opcode->id == PPC_INST_BGTLR || insn.opcode->id == PPC_INST_BLE || insn.opcode->id == PPC_INST_BLELR))
 | 
			
		||||
        {
 | 
			
		||||
            cr = insn.operands[0];
 | 
			
		||||
            if (insn.opcode->operands[1] != 0)
 | 
			
		||||
            {
 | 
			
		||||
                table.defaultLabel = insn.operands[1];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else if (cr != -1)
 | 
			
		||||
        {
 | 
			
		||||
            if (insn.opcode->id == PPC_INST_CMPLWI && insn.operands[0] == cr)
 | 
			
		||||
            {
 | 
			
		||||
                table.r = insn.operands[1];
 | 
			
		||||
                table.labels.resize(insn.operands[2] + 1);
 | 
			
		||||
                table.base = base;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MakeMask(const uint32_t* instructions, size_t count)
 | 
			
		||||
{
 | 
			
		||||
    ppc_insn insn;
 | 
			
		||||
    for (size_t i = 0; i < count; i++)
 | 
			
		||||
    {
 | 
			
		||||
        ppc::Disassemble(&instructions[i], 0, insn);
 | 
			
		||||
        fmt::println("0x{:X}, // {}", ByteSwap(insn.opcode->opcode | (insn.instruction & insn.opcode->mask)), insn.opcode->name);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void* SearchMask(const void* source, const uint32_t* compare, size_t compareCount, size_t size)
 | 
			
		||||
{
 | 
			
		||||
    assert(size % 4 == 0);
 | 
			
		||||
    uint32_t* src = (uint32_t*)source;
 | 
			
		||||
    size_t count = size / 4;
 | 
			
		||||
    ppc_insn insn;
 | 
			
		||||
 | 
			
		||||
    for (size_t i = 0; i < count; i++)
 | 
			
		||||
    {
 | 
			
		||||
        size_t c = 0;
 | 
			
		||||
        for (c = 0; c < compareCount; c++)
 | 
			
		||||
        {
 | 
			
		||||
            ppc::Disassemble(&src[i + c], 0, insn);
 | 
			
		||||
            if (insn.opcode == nullptr || insn.opcode->id != compare[c])
 | 
			
		||||
            {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (c == compareCount)
 | 
			
		||||
        {
 | 
			
		||||
            return &src[i];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return nullptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    const auto file = LoadFile("private/default.xex");
 | 
			
		||||
    auto image = Image::ParseImage(file.data(), file.size());
 | 
			
		||||
 | 
			
		||||
    std::string out;
 | 
			
		||||
    auto println = [&]<class... Args>(fmt::format_string<Args...> fmt, Args&&... args)
 | 
			
		||||
    {
 | 
			
		||||
        fmt::vformat_to(std::back_inserter(out), fmt.get(), fmt::make_format_args(args...));
 | 
			
		||||
        out += '\n';
 | 
			
		||||
    };
 | 
			
		||||
    //for (const auto& section : image.sections)
 | 
			
		||||
    //{
 | 
			
		||||
    //    image.symbols.emplace(section.name, section.base, section.size, Symbol_Section);
 | 
			
		||||
    //}
 | 
			
		||||
 | 
			
		||||
    // MakeMask((uint32_t*)image.Find(0x82C40D84), 6);
 | 
			
		||||
 | 
			
		||||
    //auto data = "\x4D\x99\x00\x20";
 | 
			
		||||
    //auto data2 = ByteSwap((2129));
 | 
			
		||||
    //ppc_insn insn;
 | 
			
		||||
    //ppc_insn insn2;
 | 
			
		||||
    //ppc::Disassemble(data, 0, insn);
 | 
			
		||||
    //ppc::Disassemble(&data2, 0, insn2);
 | 
			
		||||
    //auto op = PPC_OP(insn.instruction);
 | 
			
		||||
    //auto xop = PPC_XOP(insn.instruction);
 | 
			
		||||
 | 
			
		||||
    auto printTable = [&](const SwitchTable& table)
 | 
			
		||||
        {
 | 
			
		||||
            println("[[switch]]");
 | 
			
		||||
            println("base = 0x{:X}", table.base);
 | 
			
		||||
            println("r = {}", table.r);
 | 
			
		||||
            println("default = 0x{:X}", table.defaultLabel);
 | 
			
		||||
            println("labels = [");
 | 
			
		||||
            for (const auto& label : table.labels)
 | 
			
		||||
            {
 | 
			
		||||
                println("    0x{:X},", label);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            println("]");
 | 
			
		||||
            println("");
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
    std::vector<SwitchTable> switches{};
 | 
			
		||||
 | 
			
		||||
    auto insertTable = [&](size_t base, size_t defaultLabel, size_t r, size_t nLabels, uint32_t type)
 | 
			
		||||
        {
 | 
			
		||||
            auto& sw = switches.emplace_back();
 | 
			
		||||
            sw.base = base;
 | 
			
		||||
            sw.defaultLabel = defaultLabel;
 | 
			
		||||
            sw.r = r;
 | 
			
		||||
            sw.labels.resize(nLabels);
 | 
			
		||||
            sw.type = type;
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
    println("# Generated by PowerAnalyse");
 | 
			
		||||
    insertTable(0x830ADAD8, 0x830ADB28, 11, 0x1B, SWITCH_COMPUTED);
 | 
			
		||||
    insertTable(0x830AE1B0, 0x830AE21C, 11, 0x1B, SWITCH_BYTEOFFSET);
 | 
			
		||||
    insertTable(0x82CFE120, 0x82CFDE68, 11, 0x10, SWITCH_SHORTOFFSET);
 | 
			
		||||
 | 
			
		||||
    println("# ---- MANUAL JUMPTABLE ----");
 | 
			
		||||
    for (auto& table : switches)
 | 
			
		||||
    {
 | 
			
		||||
        ReadTable(image, table);
 | 
			
		||||
        printTable(table);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto scanPattern = [&](uint32_t* pattern, size_t count, size_t type)
 | 
			
		||||
        {
 | 
			
		||||
            for (const auto& section : image.sections)
 | 
			
		||||
            {
 | 
			
		||||
                if (!(section.flags & SectionFlags_Code))
 | 
			
		||||
                {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                size_t base = section.base;
 | 
			
		||||
                uint8_t* data = section.data;
 | 
			
		||||
                uint8_t* dataStart = section.data;
 | 
			
		||||
                uint8_t* dataEnd = section.data + section.size;
 | 
			
		||||
                while (data < dataEnd && data != nullptr)
 | 
			
		||||
                {
 | 
			
		||||
                    data = (uint8_t*)SearchMask(data, pattern, count, dataEnd - data);
 | 
			
		||||
 | 
			
		||||
                    if (data != nullptr)
 | 
			
		||||
                    {
 | 
			
		||||
                        SwitchTable table{};
 | 
			
		||||
                        table.type = type;
 | 
			
		||||
                        ScanTable((uint32_t*)data, base + (data - dataStart), table);
 | 
			
		||||
 | 
			
		||||
                        // fmt::println("{:X} ; jmptable - {}", base + (data - dataStart), table.labels.size());
 | 
			
		||||
                        if (table.base != 0)
 | 
			
		||||
                        {
 | 
			
		||||
                            ReadTable(image, table);
 | 
			
		||||
                            printTable(table);
 | 
			
		||||
                            switches.emplace_back(std::move(table));
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                        data += 4;
 | 
			
		||||
                    }
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
    uint32_t absoluteSwitch[] =
 | 
			
		||||
    {
 | 
			
		||||
        PPC_INST_LIS,
 | 
			
		||||
        PPC_INST_ADDI,
 | 
			
		||||
        PPC_INST_RLWINM,
 | 
			
		||||
        PPC_INST_LWZX,
 | 
			
		||||
        PPC_INST_MTCTR,
 | 
			
		||||
        PPC_INST_BCTR,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    uint32_t computedSwitch[] =
 | 
			
		||||
    {
 | 
			
		||||
        PPC_INST_LIS,
 | 
			
		||||
        PPC_INST_ADDI,
 | 
			
		||||
        PPC_INST_LBZX,
 | 
			
		||||
        PPC_INST_RLWINM,
 | 
			
		||||
        PPC_INST_LIS,
 | 
			
		||||
        PPC_INST_ADDI,
 | 
			
		||||
        PPC_INST_ADD,
 | 
			
		||||
        PPC_INST_MTCTR,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    uint32_t offsetSwitch[] =
 | 
			
		||||
    {
 | 
			
		||||
        PPC_INST_LIS,
 | 
			
		||||
        PPC_INST_ADDI,
 | 
			
		||||
        PPC_INST_LBZX,
 | 
			
		||||
        PPC_INST_LIS,
 | 
			
		||||
        PPC_INST_ADDI,
 | 
			
		||||
        PPC_INST_ADD,
 | 
			
		||||
        PPC_INST_MTCTR,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    uint32_t wordOffsetSwitch[] =
 | 
			
		||||
    {
 | 
			
		||||
        PPC_INST_LIS,
 | 
			
		||||
        PPC_INST_ADDI,
 | 
			
		||||
        PPC_INST_RLWINM,
 | 
			
		||||
        PPC_INST_LHZX,
 | 
			
		||||
        PPC_INST_LIS,
 | 
			
		||||
        PPC_INST_ADDI,
 | 
			
		||||
        PPC_INST_ADD,
 | 
			
		||||
        PPC_INST_MTCTR,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    println("# ---- ABSOLUTE JUMPTABLE ----");
 | 
			
		||||
    scanPattern(absoluteSwitch, std::size(absoluteSwitch), SWITCH_ABSOLUTE);
 | 
			
		||||
 | 
			
		||||
    println("# ---- COMPUTED JUMPTABLE ----");
 | 
			
		||||
    scanPattern(computedSwitch, std::size(computedSwitch), SWITCH_COMPUTED);
 | 
			
		||||
 | 
			
		||||
    println("# ---- OFFSETED JUMPTABLE ----");
 | 
			
		||||
    scanPattern(offsetSwitch, std::size(offsetSwitch), SWITCH_BYTEOFFSET);
 | 
			
		||||
    scanPattern(wordOffsetSwitch, std::size(wordOffsetSwitch), SWITCH_SHORTOFFSET);
 | 
			
		||||
 | 
			
		||||
    FILE* f = fopen("out/switches.toml", "w");
 | 
			
		||||
    fwrite(out.data(), 1, out.size(), f);
 | 
			
		||||
    fclose(f);
 | 
			
		||||
 | 
			
		||||
    uint32_t cxxFrameHandler = ByteSwap(0x831B1C90);
 | 
			
		||||
    uint32_t cSpecificFrameHandler = ByteSwap(0x8324B3BC);
 | 
			
		||||
    image.symbols.emplace("__CxxFrameHandler", 0x831B1C90, 0x38, Symbol_Function);
 | 
			
		||||
    image.symbols.emplace("__C_specific_handler", 0x8324B3BC, 0x38, Symbol_Function);
 | 
			
		||||
    image.symbols.emplace("memcpy", 0x831B0ED0, 0x488, Symbol_Function);
 | 
			
		||||
    image.symbols.emplace("memset", 0x831B0BA0, 0xA0, Symbol_Function);
 | 
			
		||||
    image.symbols.emplace("blkmov", 0x831B1358, 0xA8, Symbol_Function);
 | 
			
		||||
 | 
			
		||||
    image.symbols.emplace(fmt::format("sub_{:X}", 0x82EF5D78), 0x82EF5D78, 0x3F8, Symbol_Function);
 | 
			
		||||
 | 
			
		||||
    // auto fnd = Function::Analyze(image.Find(0x82C40D58), image.size, 0x82C40D58);
 | 
			
		||||
 | 
			
		||||
    std::vector<Function> functions;
 | 
			
		||||
    auto& pdata = *image.Find(".pdata");
 | 
			
		||||
    size_t count = pdata.size / sizeof(IMAGE_CE_RUNTIME_FUNCTION);
 | 
			
		||||
    auto* pf = (IMAGE_CE_RUNTIME_FUNCTION*)pdata.data;
 | 
			
		||||
    for (size_t i = 0; i < count; i++)
 | 
			
		||||
    {
 | 
			
		||||
        auto fn = pf[i];
 | 
			
		||||
        fn.BeginAddress = ByteSwap(fn.BeginAddress);
 | 
			
		||||
        fn.Data = ByteSwap(fn.Data);
 | 
			
		||||
 | 
			
		||||
        auto& f = functions.emplace_back();
 | 
			
		||||
        f.base = fn.BeginAddress;
 | 
			
		||||
        f.size = fn.FunctionLength * 4;
 | 
			
		||||
 | 
			
		||||
        if (f.base == 0x82BD7420)
 | 
			
		||||
        {
 | 
			
		||||
            __builtin_debugtrap();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        image.symbols.emplace(fmt::format("sub_{:X}", f.base), f.base, f.size, Symbol_Function);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto sym = image.symbols.find(0x82BD7420);
 | 
			
		||||
 | 
			
		||||
    std::vector<Function> missingFunctions;
 | 
			
		||||
    for (const auto& section : image.sections) 
 | 
			
		||||
    {
 | 
			
		||||
        if (!(section.flags & SectionFlags_Code))
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        size_t base = section.base;
 | 
			
		||||
        uint8_t* data = section.data;
 | 
			
		||||
        uint8_t* dataEnd = section.data + section.size;
 | 
			
		||||
        const Symbol* prevSymbol = nullptr;
 | 
			
		||||
        while (data < dataEnd)
 | 
			
		||||
        {
 | 
			
		||||
            if (*(uint32_t*)data == 0)
 | 
			
		||||
            {
 | 
			
		||||
                data += 4;
 | 
			
		||||
                base += 4;
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (*(uint32_t*)data == cxxFrameHandler || *(uint32_t*)data == cSpecificFrameHandler)
 | 
			
		||||
            {
 | 
			
		||||
                data += 8;
 | 
			
		||||
                base += 8;
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            auto fnSymbol = image.symbols.find(base);
 | 
			
		||||
            if (fnSymbol != image.symbols.end() && fnSymbol->type == Symbol_Function)
 | 
			
		||||
            {
 | 
			
		||||
                assert(fnSymbol->address == base);
 | 
			
		||||
 | 
			
		||||
                prevSymbol = &*fnSymbol;
 | 
			
		||||
                base += fnSymbol->size;
 | 
			
		||||
                data += fnSymbol->size;
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                auto& missingFn = missingFunctions.emplace_back(Function::Analyze(data, dataEnd - data, base));
 | 
			
		||||
                base += missingFn.size;
 | 
			
		||||
                data += missingFn.size;
 | 
			
		||||
                
 | 
			
		||||
                fmt::println("sub_{:X}", missingFn.base);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //ppc_insn insn;
 | 
			
		||||
    //uint8_t c[4] = { 0x10, 0x00, 0x59, 0xC3 };
 | 
			
		||||
    //ppc::Disassemble(c, 0x831D6C64, insn);
 | 
			
		||||
    //fmt::println("{:20}{}", insn.opcode->name, insn.op_str);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    const auto entrySymbol = image.symbols.find(image.entry_point);
 | 
			
		||||
    assert(entrySymbol != image.symbols.end());
 | 
			
		||||
 | 
			
		||||
    const auto entrySize = entrySymbol->size;
 | 
			
		||||
    image.symbols.erase(entrySymbol);
 | 
			
		||||
 | 
			
		||||
    image.symbols.emplace("_start", image.entry_point, entrySize, Symbol_Function);
 | 
			
		||||
 | 
			
		||||
    fmt::println("FUNCTIONS");
 | 
			
		||||
    for (const auto& fn : functions)
 | 
			
		||||
    {
 | 
			
		||||
        fmt::println("\tsub_{:X}", fn.base);
 | 
			
		||||
    }
 | 
			
		||||
    fmt::println("");
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    fmt::println("SECTIONS");
 | 
			
		||||
    for (const auto& section : image.sections)
 | 
			
		||||
    {
 | 
			
		||||
        printf("Section %.8s\n", section.name.c_str());
 | 
			
		||||
        printf("\t%X-%X\n", section.base, section.base + section.size);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fmt::println("");
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user