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 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);
// }
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("");
// };
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& sw = switches.emplace_back();
// sw.base = base;
// sw.defaultLabel = defaultLabel;
// sw.r = r;
// sw.labels.resize(nLabels);
// sw.type = type;
// };
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("# 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);
//}
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;
// }
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);
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);
if (data != nullptr)
{
SwitchTable table{};
table.type = type;
ScanTable((uint32_t*)data, base + (data - dataStart), table);
// // std::println("{:X} ; jmptable - {}", base + (data - dataStart), table.labels.size());
// if (table.base != 0)
// {
// ReadTable(image, table);
// printTable(table);
// switches.emplace_back(std::move(table));
// }
// std::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;
// }
// }
// };
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 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 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 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,
//};
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("# ---- ABSOLUTE JUMPTABLE ----");
scanPattern(absoluteSwitch, std::size(absoluteSwitch), SWITCH_ABSOLUTE);
//println("# ---- COMPUTED JUMPTABLE ----");
//scanPattern(computedSwitch, std::size(computedSwitch), SWITCH_COMPUTED);
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);
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);
FILE* f = fopen("out/switches.toml", "w");
fwrite(out.data(), 1, out.size(), f);
fclose(f);
uint32_t cxxFrameHandler = std::byteswap(0x831B1C90);
uint32_t cSpecificFrameHandler = std::byteswap(0x8324B3BC);