Uncomment switch analyser.

This commit is contained in:
Skyth 2024-09-18 10:41:45 +03:00
parent 594a339a77
commit fc43264f72

View File

@ -198,142 +198,142 @@ int main()
//auto op = PPC_OP(insn.instruction); //auto op = PPC_OP(insn.instruction);
//auto xop = PPC_XOP(insn.instruction); //auto xop = PPC_XOP(insn.instruction);
//auto printTable = [&](const SwitchTable& table) auto printTable = [&](const SwitchTable& table)
// { {
// println("[[switch]]"); println("[[switch]]");
// println("base = 0x{:X}", table.base); println("base = 0x{:X}", table.base);
// println("r = {}", table.r); println("r = {}", table.r);
// println("default = 0x{:X}", table.defaultLabel); println("default = 0x{:X}", table.defaultLabel);
// println("labels = ["); println("labels = [");
// for (const auto& label : table.labels) for (const auto& label : table.labels)
// { {
// println(" 0x{:X},", label); println(" 0x{:X},", label);
// } }
// println("]"); println("]");
// println(""); println("");
// }; };
//std::vector<SwitchTable> switches{}; std::vector<SwitchTable> switches{};
//auto insertTable = [&](size_t base, size_t defaultLabel, size_t r, size_t nLabels, uint32_t type) auto insertTable = [&](size_t base, size_t defaultLabel, size_t r, size_t nLabels, uint32_t type)
// { {
// auto& sw = switches.emplace_back(); auto& sw = switches.emplace_back();
// sw.base = base; sw.base = base;
// sw.defaultLabel = defaultLabel; sw.defaultLabel = defaultLabel;
// sw.r = r; sw.r = r;
// sw.labels.resize(nLabels); sw.labels.resize(nLabels);
// sw.type = type; sw.type = type;
// }; };
//println("# Generated by PowerAnalyse"); println("# Generated by PowerAnalyse");
//insertTable(0x830ADAD8, 0x830ADB28, 11, 0x1B, SWITCH_COMPUTED); insertTable(0x830ADAD8, 0x830ADB28, 11, 0x1B, SWITCH_COMPUTED);
//insertTable(0x830AE1B0, 0x830AE21C, 11, 0x1B, SWITCH_BYTEOFFSET); insertTable(0x830AE1B0, 0x830AE21C, 11, 0x1B, SWITCH_BYTEOFFSET);
//insertTable(0x82CFE120, 0x82CFDE68, 11, 0x10, SWITCH_SHORTOFFSET); insertTable(0x82CFE120, 0x82CFDE68, 11, 0x10, SWITCH_SHORTOFFSET);
//println("# ---- MANUAL JUMPTABLE ----"); println("# ---- MANUAL JUMPTABLE ----");
//for (auto& table : switches) for (auto& table : switches)
//{ {
// ReadTable(image, table); ReadTable(image, table);
// printTable(table); printTable(table);
//} }
//auto scanPattern = [&](uint32_t* pattern, size_t count, size_t type) auto scanPattern = [&](uint32_t* pattern, size_t count, size_t type)
// { {
// for (const auto& section : image.sections) for (const auto& section : image.sections)
// { {
// if (!(section.flags & SectionFlags_Code)) if (!(section.flags & SectionFlags_Code))
// { {
// continue; continue;
// } }
// size_t base = section.base; size_t base = section.base;
// uint8_t* data = section.data; uint8_t* data = section.data;
// uint8_t* dataStart = section.data; uint8_t* dataStart = section.data;
// uint8_t* dataEnd = section.data + section.size; uint8_t* dataEnd = section.data + section.size;
// while (data < dataEnd && data != nullptr) while (data < dataEnd && data != nullptr)
// { {
// data = (uint8_t*)SearchMask(data, pattern, count, dataEnd - data); data = (uint8_t*)SearchMask(data, pattern, count, dataEnd - data);
// if (data != nullptr) if (data != nullptr)
// { {
// SwitchTable table{}; SwitchTable table{};
// table.type = type; table.type = type;
// ScanTable((uint32_t*)data, base + (data - dataStart), table); ScanTable((uint32_t*)data, base + (data - dataStart), table);
// // std::println("{:X} ; jmptable - {}", base + (data - dataStart), table.labels.size()); // std::println("{:X} ; jmptable - {}", base + (data - dataStart), table.labels.size());
// if (table.base != 0) if (table.base != 0)
// { {
// ReadTable(image, table); ReadTable(image, table);
// printTable(table); printTable(table);
// switches.emplace_back(std::move(table)); switches.emplace_back(std::move(table));
// } }
// data += 4; data += 4;
// } }
// continue; continue;
// } }
// } }
// }; };
//uint32_t absoluteSwitch[] = uint32_t absoluteSwitch[] =
//{ {
// PPC_INST_LIS, PPC_INST_LIS,
// PPC_INST_ADDI, PPC_INST_ADDI,
// PPC_INST_RLWINM, PPC_INST_RLWINM,
// PPC_INST_LWZX, PPC_INST_LWZX,
// PPC_INST_MTCTR, PPC_INST_MTCTR,
// PPC_INST_BCTR, PPC_INST_BCTR,
//}; };
//uint32_t computedSwitch[] = uint32_t computedSwitch[] =
//{ {
// PPC_INST_LIS, PPC_INST_LIS,
// PPC_INST_ADDI, PPC_INST_ADDI,
// PPC_INST_LBZX, PPC_INST_LBZX,
// PPC_INST_RLWINM, PPC_INST_RLWINM,
// PPC_INST_LIS, PPC_INST_LIS,
// PPC_INST_ADDI, PPC_INST_ADDI,
// PPC_INST_ADD, PPC_INST_ADD,
// PPC_INST_MTCTR, PPC_INST_MTCTR,
//}; };
//uint32_t offsetSwitch[] = uint32_t offsetSwitch[] =
//{ {
// PPC_INST_LIS, PPC_INST_LIS,
// PPC_INST_ADDI, PPC_INST_ADDI,
// PPC_INST_LBZX, PPC_INST_LBZX,
// PPC_INST_LIS, PPC_INST_LIS,
// PPC_INST_ADDI, PPC_INST_ADDI,
// PPC_INST_ADD, PPC_INST_ADD,
// PPC_INST_MTCTR, PPC_INST_MTCTR,
//}; };
//uint32_t wordOffsetSwitch[] = uint32_t wordOffsetSwitch[] =
//{ {
// PPC_INST_LIS, PPC_INST_LIS,
// PPC_INST_ADDI, PPC_INST_ADDI,
// PPC_INST_RLWINM, PPC_INST_RLWINM,
// PPC_INST_LHZX, PPC_INST_LHZX,
// PPC_INST_LIS, PPC_INST_LIS,
// PPC_INST_ADDI, PPC_INST_ADDI,
// PPC_INST_ADD, PPC_INST_ADD,
// PPC_INST_MTCTR, PPC_INST_MTCTR,
//}; };
//println("# ---- ABSOLUTE JUMPTABLE ----"); println("# ---- ABSOLUTE JUMPTABLE ----");
//scanPattern(absoluteSwitch, std::size(absoluteSwitch), SWITCH_ABSOLUTE); scanPattern(absoluteSwitch, std::size(absoluteSwitch), SWITCH_ABSOLUTE);
//println("# ---- COMPUTED JUMPTABLE ----"); println("# ---- COMPUTED JUMPTABLE ----");
//scanPattern(computedSwitch, std::size(computedSwitch), SWITCH_COMPUTED); scanPattern(computedSwitch, std::size(computedSwitch), SWITCH_COMPUTED);
//println("# ---- OFFSETED JUMPTABLE ----"); println("# ---- OFFSETED JUMPTABLE ----");
//scanPattern(offsetSwitch, std::size(offsetSwitch), SWITCH_BYTEOFFSET); scanPattern(offsetSwitch, std::size(offsetSwitch), SWITCH_BYTEOFFSET);
//scanPattern(wordOffsetSwitch, std::size(wordOffsetSwitch), SWITCH_SHORTOFFSET); scanPattern(wordOffsetSwitch, std::size(wordOffsetSwitch), SWITCH_SHORTOFFSET);
//FILE* f = fopen("out/switches.toml", "w"); FILE* f = fopen("out/switches.toml", "w");
//fwrite(out.data(), 1, out.size(), f); fwrite(out.data(), 1, out.size(), f);
//fclose(f); fclose(f);
uint32_t cxxFrameHandler = std::byteswap(0x831B1C90); uint32_t cxxFrameHandler = std::byteswap(0x831B1C90);
uint32_t cSpecificFrameHandler = std::byteswap(0x8324B3BC); uint32_t cSpecificFrameHandler = std::byteswap(0x8324B3BC);