314 lines
8.5 KiB
C++
Raw Normal View History

2024-09-09 21:09:37 +06:00
#include <cassert>
2024-12-13 18:31:55 +03:00
#include <iterator>
2024-09-07 18:00:09 +06:00
#include <file.h>
#include <disasm.h>
2024-09-07 22:56:20 +06:00
#include <image.h>
2024-09-13 20:54:18 +06:00
#include <xbox.h>
2024-12-13 18:31:55 +03:00
#include <fmt/core.h>
#include "function.h"
2024-09-07 18:00:09 +06:00
2024-09-18 13:07:07 +06:00
#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); // lis
pOffset = insn.operands[1] << 16; // Upper 16 bits
2024-09-18 13:07:07 +06:00
ppc::Disassemble(code + 2, table.base + 8, insn); // addi (skip rlwinm at +4)
pOffset += insn.operands[2]; // Lower 16 bits
2024-09-18 13:07:07 +06:00
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)
{
2024-09-18 13:32:30 +06:00
table.defaultLabel = insn.operands[1];
2024-09-18 13:07:07 +06:00
}
}
else if (cr != -1)
{
if (insn.opcode->id == PPC_INST_CMPLWI && insn.operands[0] == cr)
{
table.r = insn.operands[1];
2024-09-18 13:32:30 +06:00
table.labels.resize(insn.operands[2] + 1);
2024-09-18 13:07:07 +06:00
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);
2024-12-13 18:31:55 +03:00
fmt::println("0x{:X}, // {}", ByteSwap(insn.opcode->opcode | (insn.instruction & insn.opcode->mask)), insn.opcode->name);
2024-09-18 13:07:07 +06:00
}
}
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;
}
2025-02-27 00:11:11 +03:00
static std::string out;
template<class... Args>
static void println(fmt::format_string<Args...> fmt, Args&&... args)
2024-09-07 18:00:09 +06:00
{
2025-02-27 00:11:11 +03:00
fmt::vformat_to(std::back_inserter(out), fmt.get(), fmt::make_format_args(args...));
out += '\n';
};
2025-02-27 00:11:11 +03:00
int main(int argc, char** argv)
{
if (argc < 3)
2024-09-18 13:07:07 +06:00
{
2025-02-27 00:11:11 +03:00
printf("Usage: XenonAnalyse [input XEX file path] [output jump table TOML file path]");
return EXIT_SUCCESS;
}
const auto file = LoadFile(argv[1]);
auto image = Image::ParseImage(file.data(), file.size());
2024-09-18 13:07:07 +06:00
2024-09-18 10:41:45 +03:00
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);
}
2024-09-18 13:07:07 +06:00
2024-09-18 10:41:45 +03:00
println("]");
println("");
};
std::vector<SwitchTable> switches{};
2025-02-27 00:11:11 +03:00
println("# Generated by XenonAnalyse");
2024-09-18 10:41:45 +03:00
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);
2024-12-13 18:31:55 +03:00
// fmt::println("{:X} ; jmptable - {}", base + (data - dataStart), table.labels.size());
2024-09-18 10:41:45 +03:00
if (table.base != 0)
{
ReadTable(image, table);
printTable(table);
switches.emplace_back(std::move(table));
}
data += 4;
}
continue;
}
}
};
// adjusted for tag 2
uint32_t absoluteSwitch[] =
{
PPC_INST_LIS,
PPC_INST_RLWINM, // (slwi alias)
PPC_INST_ADDI,
PPC_INST_LWZX,
PPC_INST_MTCTR,
PPC_INST_BCTR
};
2024-09-18 10:41:45 +03:00
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);
2025-02-27 00:11:11 +03:00
std::ofstream f(argv[2]);
f.write(out.data(), out.size());
2024-09-07 22:56:20 +06:00
2025-02-27 00:11:11 +03:00
return EXIT_SUCCESS;
2024-09-07 22:56:20 +06:00
}