Files
neoLegacy-mac/match-iggy-functions.py

116 lines
4.2 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import csv
from collections import defaultdict
from pathlib import Path
def read_hashes(directory: Path):
records = []
for path in sorted(directory.glob("*.tsv")):
with path.open(newline="", encoding="utf-8") as handle:
for row in csv.DictReader(handle, delimiter="\t"):
row["module"] = path.name.removesuffix(".obj.tsv").removesuffix(".tsv")
records.append(row)
return records
def useful_name(name: str) -> bool:
leaf = name.rsplit("::", 1)[-1]
return not leaf.startswith(("FUN_", "LAB_", "SUB_", "$LN", "switchD_"))
def make_match(windows, rows, name, kind):
modules = sorted({row["module"] for row in rows if row["name"] == name})
return {
"windows_address": windows["address"],
"windows_name": windows["name"],
"recovered_name": name,
"durango_modules": ",".join(modules),
"match_kind": kind,
"code_units": windows["code_units"],
"full_hash": windows["full_hash"],
}
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("durango", type=Path)
parser.add_argument("windows", type=Path)
parser.add_argument("output", type=Path)
args = parser.parse_args()
candidates = defaultdict(list)
for row in read_hashes(args.durango):
if useful_name(row["name"]):
candidates[(row["full_hash"], row["code_units"])].append(row)
matches = []
pending = []
for windows in read_hashes(args.windows):
rows = candidates.get((windows["full_hash"], windows["code_units"]), [])
names = sorted({row["name"] for row in rows})
kind = "full"
if len(names) > 1:
specific = [
row for row in rows
if row["specific_hash"] == windows["specific_hash"]
and row["specific_extra"] == windows["specific_extra"]
]
specific_names = sorted({row["name"] for row in specific})
if len(specific_names) == 1:
rows = specific
names = specific_names
kind = "specific"
if len(names) != 1:
if rows:
pending.append((windows, rows))
continue
matches.append(make_match(windows, rows, names[0], kind))
changed = True
while changed:
changed = False
ordered = sorted(matches, key=lambda row: int(row["windows_address"], 16))
remaining = []
for windows, rows in pending:
address = int(windows["address"], 16)
before = [row for row in ordered if int(row["windows_address"], 16) < address]
after = [row for row in ordered if int(row["windows_address"], 16) > address]
if not before or not after:
remaining.append((windows, rows))
continue
left_modules = set(before[-1]["durango_modules"].split(","))
right_modules = set(after[0]["durango_modules"].split(","))
neighborhood = left_modules & right_modules
if len(neighborhood) != 1:
remaining.append((windows, rows))
continue
module = next(iter(neighborhood))
local = [row for row in rows if row["module"] == module]
names = sorted({row["name"] for row in local})
if len(names) != 1:
remaining.append((windows, rows))
continue
matches.append(make_match(windows, local, names[0], "module-context"))
changed = True
pending = remaining
matches.sort(key=lambda row: int(row["windows_address"], 16))
args.output.parent.mkdir(parents=True, exist_ok=True)
with args.output.open("w", newline="", encoding="utf-8") as handle:
fieldnames = list(matches[0]) if matches else [
"windows_address", "windows_name", "recovered_name",
"durango_modules", "match_kind", "code_units", "full_hash"
]
writer = csv.DictWriter(handle, fieldnames=fieldnames, delimiter="\t")
writer.writeheader()
writer.writerows(matches)
print(f"matched {len(matches)} Windows functions; {len(pending)} hashes remain ambiguous")
return 0
if __name__ == "__main__":
raise SystemExit(main())