Files
kdm-re/tools/unicorn_diff/udiff/metadata.py
T

29 lines
1.1 KiB
Python

"""Read per-function calling conventions from the recovered RVA database."""
import json
from pathlib import Path
from typing import Final
from .abi import AbiError, CallConv
RVADB_PATH: Final = Path(__file__).resolve().parents[3] / "re" / "rvadb.json"
def load_calling_convention(
address: int, metadata_path: Path = RVADB_PATH,
) -> CallConv:
"""Resolve an RVA or VA; preserve unknown rather than guessing an ABI."""
with metadata_path.open(encoding="utf-8") as source:
database = json.load(source)
image_base = int(database["image_base"], 0)
rva = address - image_base if address >= image_base else address
for function in database["functions"]:
if int(function["rva"], 0) == rva:
recorded = function["calling_convention"]
try:
return CallConv(recorded.removeprefix("__"))
except ValueError as error:
raise AbiError(
detail=f"unsupported convention {recorded!r} at RVA {rva:#x}",
) from error
raise AbiError(detail=f"function RVA {rva:#x} absent from {metadata_path}")