"""Inspect a PE image (exe or dll) for porting work. Usage: python -I pe_inspect.py [--game-dir DIR] [--out DIR] Writes /.json and /.md, where is the image's full file name (game.exe.json, game.dll.md), so an exe and a dll with the same stem get separate reports. Images with the same file name from different directories get a numbered suffix (x.exe-2.json), and the name used is printed to stderr. The same file given twice with the same import search folder is inspected once. Reads the file only; never runs it. The report also lists run-time load candidates: DLL names the image holds as ASCII or UTF-16 strings without importing them, where a LoadLibrary* the import table doesn't show would point (docs/runtime_imports.md). Each DLL's "where" is bundled (in the game folder, any case), system, shim or, on a host that isn't Windows and has no Wine prefix, unverified (classify_dll). On Windows, system means System32 or the OS API-set schema; elsewhere the Wine prefix's system32 ($WINEPREFIX, else ~/.wine) and its apisetschema.dll stand in, and the report's "system_dlls" says which was used. Standard library only: the headers, the section table, the import, delay-import, export, TLS and relocation directories are parsed with struct, so the tool runs under `python -I` without third-party packages and is covered by the same standard-library-only CI job as the other tools. """ from __future__ import annotations import argparse import json import math import os import re import struct import sys from collections import Counter from collections.abc import Iterator sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from report_paths import same_report_path # noqa: E402 same_output_path = same_report_path # True when writing the second would truncate the first SYSTEM32 = os.path.join(os.environ.get("SystemRoot", r"C:\Windows"), "System32") # Whether this host is Windows: there SYSTEM32 and the OS API-set schema answer `system`; elsewhere a Wine prefix's # system32 does (classify_dll). A module flag so that a test can stand in for either host. IS_WINDOWS = os.name == "nt" RUNTIME_SHIMS = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "runtime", "shims") MACHINES = {0x8664: "x86-64", 0x14C: "x86", 0xAA64: "ARM64"} SUBSYSTEMS = {2: "GUI", 3: "Console", 14: "Xbox"} DIR_EXPORT = 0 DIR_IMPORT = 1 DIR_EXCEPTION = 3 DIR_BASERELOC = 5 DIR_TLS = 9 DIR_DELAY_IMPORT = 13 MAX_ENTRIES = 65536 # a table this long is not a table; stops a malformed image from looping forever class PeFormatError(ValueError): """The file is not a PE image this parser can read.""" def entropy(data: bytes) -> float: if not data: return 0.0 n = len(data) return -sum(c / n * math.log2(c / n) for c in Counter(data).values()) # An MSVC decorated name can be 4,096 characters (the documented limit of a decorated name); with the 512 this # was, a long template import became "?" and a DLL with a long name lost its whole entry. NAME_LIMIT = 4097 def _cstring(data: bytes, offset: int, limit: int = NAME_LIMIT) -> str | None: end = data.find(b"\0", offset, min(len(data), offset + limit)) if end < 0: return None return data[offset:end].decode(errors="replace") class PeImage: """The parts of a PE file this tool reports on, read in place from the file bytes.""" def __init__(self, data: bytes): self.data = data if data[:2] != b"MZ": raise PeFormatError("no MZ header") if len(data) < 0x40: # The MZ signature is there; the file is just too short to carry the MS-DOS header whose dword at # 0x3c holds e_lfanew. Saying "no MZ header" here would blame a signature that is present. raise PeFormatError(f"file is {len(data)} bytes, shorter than the 64-byte MS-DOS header") self.nt = struct.unpack_from(" len(data): raise PeFormatError(f"file is {len(data)} bytes, too short to hold the PE header at 0x{self.nt:x}") if data[self.nt:self.nt + 4] != b"PE\0\0": raise PeFormatError("no PE signature") (self.machine, self.section_count, self.timestamp, _symbol_table, _symbols, opt_size, self.characteristics) = struct.unpack_from(" len(data): raise PeFormatError("truncated optional header") magic = struct.unpack_from(" len(data): raise PeFormatError("truncated optional header") self.is64 = magic == 0x20B self.entry_rva = struct.unpack_from(" self.size_of_image: raise PeFormatError(f"SizeOfHeaders {self.size_of_headers} exceeds SizeOfImage {self.size_of_image}") # NumberOfRvaAndSizes counts the data directories: a slot past it is not a directory, whatever bytes it # holds (runtime/src/pe_loader.cpp reads them the same way). The count is the dword before the table. opt_end = min(len(data), opt + opt_size) dirs = opt + (112 if self.is64 else 96) count = struct.unpack_from(" len(data): raise PeFormatError("truncated section table") vsize, rva, rawsize, rawptr = struct.unpack_from(" tuple[int, int]: return self.directories[index] if index < len(self.directories) else (0, 0) def import_descriptors(self, index: int, *, delay: bool = False) -> Iterator[tuple[int, int, int, bool]]: """Yield (dll_rva, iat_rva, lookup_rva, va_entries) for each import descriptor.""" rva, size = self.directory(index) if not rva or not size: return step = 32 if delay else 20 for i in range(min(size // step, MAX_ENTRIES)): at = self.offset(rva + i * step, step) if at is None: break va_entries = False if delay: attributes, dll_rva, _handle, iat_rva, lookup_rva = struct.unpack_from("<5I", self.data, at) if not dll_rva: break # With dlattrRva clear, these DWORDs and named lookup entries hold legacy VAs. if not attributes & 1: va_entries = True dll_rva -= self.image_base iat_rva -= self.image_base lookup_rva -= self.image_base else: lookup_rva, _stamp, _forwarder, dll_rva, iat_rva = struct.unpack_from("<5I", self.data, at) if not dll_rva: break yield dll_rva, iat_rva, lookup_rva, va_entries def _range(self, rva: int) -> tuple[int, int] | None: """Contiguous file [start, end) backed by headers or the single section holding `rva`.""" if rva < 0: return None if rva < self.size_of_headers: end = min(self.size_of_headers, len(self.data)) return (rva, end) if rva < end else None for s in self.sections: # A section is VirtualSize bytes in memory (SizeOfRawData when that is 0). File bytes past it are not # mapped, as in runtime/src/pe_loader.cpp: with SizeOfRawData larger than the span to the next # section, max(vsize, rawsize) answered the next section's RVAs from this one's raw tail. span = s["vsize"] or s["rawsize"] if s["rva"] <= rva < s["rva"] + span: delta = rva - s["rva"] mapped = min(s["rawsize"], span) if delta >= mapped: return None at = s["rawptr"] + delta end = min(s["rawptr"] + mapped, len(self.data)) return (at, end) if at < end else None return None def offset(self, rva: int, size: int = 1) -> int | None: """File offset of an RVA span wholly backed by headers or one section.""" if size <= 0: return None bounds = self._range(rva) if bounds is None: return None start, end = bounds return start if start + size <= end else None def section_data(self, section: dict) -> bytes: at = section["rawptr"] if at >= len(self.data): return b"" return self.data[at:at + min(section["rawsize"], len(self.data) - at)] def string_at(self, rva: int) -> str | None: bounds = self._range(rva) if bounds is None: return None start, end = bounds return _cstring(self.data, start, min(NAME_LIMIT, end - start)) def word(self, at: int) -> int: return struct.unpack_from(" list[str]: """Names (or '#ordinal') of one lookup table, which is terminated by a zero entry.""" ordinal_flag = 1 << (63 if self.is64 else 31) out = [] width = 8 if self.is64 else 4 for i in range(MAX_ENTRIES): at = self.offset(lookup_rva + i * width, width) if at is None: break value = self.word(at) if not value: break if value & ordinal_flag: out.append(f"#{value & 0xFFFF}") continue if va_entries: value -= self.image_base if value <= 0: out.append("?") continue name = self.string_at(value + 2) # skip the 2-byte hint out.append(name if name is not None else "?") return out def _imports(img: PeImage, kind: str, index: int, delay: bool = False) -> list[dict]: entries = [] for dll_rva, iat_rva, int_rva, va_entries in img.import_descriptors(index, delay=delay): dll = img.string_at(dll_rva) if dll_rva > 0 else None if dll is None: continue lookup = int_rva if int_rva > 0 else iat_rva entries.append({"dll": dll, "kind": kind, "functions": img.thunk_names(lookup, va_entries=va_entries) if lookup > 0 else []}) return entries def _inspect_exports(img: PeImage) -> dict: rva, size = img.directory(DIR_EXPORT) if not rva or size < 40: return { "exports": [], "forwarders": {}, "invalid_export_count": 0, "invalid_exports": [], } at = img.offset(rva, 40) if at is None: start = img.offset(rva) if start is not None and start + 40 > len(img.data): raise PeFormatError("truncated export directory") return { "exports": [], "forwarders": {}, "invalid_export_count": 0, "invalid_exports": [], } (_chars, _stamp, _major, _minor, _name, base, functions, name_count, addr_functions, addr_names, addr_ordinals) = struct.unpack_from(" int | None: if index >= functions: return None slot = img.offset(addr_functions + index * 4, 4) if slot is None: return None return struct.unpack_from(" None: nonlocal invalid_export_count invalid_export_count += 1 if len(invalid_exports) < 5: invalid_exports.append({ "symbol": key, "ordinal": ord_num, "rva": hex(exp_rva), "reason": reason, }) def process_export(key: str, index: int, exp_rva: int) -> None: ord_num = base + index if dir_begin <= exp_rva < dir_end: target = img.string_at(exp_rva) if not target: record_invalid(key, ord_num, exp_rva, "unreadable_forwarder") target = "?" else: dot = target.rfind(".") if dot <= 0 or dot + 1 >= len(target) or any(ord(c) < 0x20 or ord(c) >= 0x7F for c in target): record_invalid(key, ord_num, exp_rva, "malformed_forwarder") else: fwd_sym = target[dot + 1:] if fwd_sym.startswith("#"): ord_str = fwd_sym[1:] if not ord_str.isdigit() or int(ord_str) <= 0 or int(ord_str) > 0xFFFF: record_invalid(key, ord_num, exp_rva, "malformed_forwarder") forwarders[key] = target forwarder_info[key] = (ord_num, exp_rva, target) elif exp_rva >= img.size_of_image: record_invalid(key, ord_num, exp_rva, "out_of_bounds") named = set() for i in range(min(name_count, MAX_ENTRIES)): name_at = img.offset(addr_names + i * 4, 4) ord_at = img.offset(addr_ordinals + i * 2, 2) if name_at is None or ord_at is None: break symbol_rva = struct.unpack_from("= functions: record_invalid(key, base + index, 0, "out_of_range_ordinal") continue exp_rva = get_export_rva(index) if exp_rva is None or exp_rva == 0: continue names.append(key) process_export(key, index, exp_rva) named.add(index) for i in range(min(functions, MAX_ENTRIES)): if i not in named: exp_rva = get_export_rva(i) if exp_rva is not None and exp_rva != 0: key = f"#{base + i}" names.append(key) process_export(key, i, exp_rva) own_mod = img.string_at(_name) if _name else "" def _stem(m: str) -> str: s = m.lower() return s[:-4] if s.endswith(".dll") else s own_stem = _stem(own_mod) if own_mod else "" if own_stem: for fwd_key, (ord_num, exp_rva, target) in forwarder_info.items(): if target == "?" or "." not in target: continue dot = target.rfind(".") if dot <= 0 or _stem(target[:dot]) != own_stem: continue visited = [fwd_key] curr_target = target has_loop = False while True: c_dot = curr_target.rfind(".") if c_dot <= 0 or _stem(curr_target[:c_dot]) != own_stem: break next_sym = curr_target[c_dot + 1:] if next_sym in visited or len(visited) >= 8: has_loop = True break if next_sym not in forwarders: break visited.append(next_sym) curr_target = forwarders[next_sym] if curr_target == "?": break if has_loop: record_invalid(fwd_key, ord_num, exp_rva, "forwarder_loop") return { "exports": names, "forwarders": forwarders, "invalid_export_count": invalid_export_count, "invalid_exports": invalid_exports, } def _exports(img: PeImage) -> list[str]: return _inspect_exports(img)["exports"] # The resource directory (data directory 2) and its RT_VERSION block. The version block names the build, and its # values are free UTF-16 text that can name the title, so the report never holds a value: the fixed info's numbers, # the languages, the standard key names and, per value, its length and SHA-256. Neither are the names of named # resource types or of a key outside VERSION_KEYS, which are counted or hashed instead. A damaged tree or block is # reported in "problems" (offsets and counts only) and never stops the report. DIR_RESOURCE = 2 RT_VERSION = 16 RT_NAMES = {1: "CURSOR", 2: "BITMAP", 3: "ICON", 4: "MENU", 5: "DIALOG", 6: "STRING", 7: "FONTDIR", 8: "FONT", 9: "ACCELERATOR", 10: "RCDATA", 11: "MESSAGETABLE", 12: "GROUP_CURSOR", 14: "GROUP_ICON", 16: "VERSION", 17: "DLGINCLUDE", 19: "PLUGPLAY", 20: "VXD", 21: "ANICURSOR", 22: "ANIICON", 23: "HTML", 24: "MANIFEST"} # The StringFileInfo keys the VERSIONINFO documentation defines; any other key is reported by its hash. VERSION_KEYS = frozenset({"Comments", "CompanyName", "FileDescription", "FileVersion", "InternalName", "LegalCopyright", "LegalTrademarks", "OriginalFilename", "PrivateBuild", "ProductName", "ProductVersion", "SpecialBuild"}) VS_FIXED_SIGNATURE = 0xFEEF04BD VS_FF = {0x01: "DEBUG", 0x02: "PRERELEASE", 0x04: "PATCHED", 0x08: "PRIVATEBUILD", 0x10: "INFOINFERRED", 0x20: "SPECIALBUILD"} VOS = {0x00000: "UNKNOWN", 0x00004: "WINDOWS32", 0x10000: "DOS", 0x40000: "NT", 0x10001: "DOS_WINDOWS16", 0x10004: "DOS_WINDOWS32", 0x40004: "NT_WINDOWS32"} VFT = {0: "UNKNOWN", 1: "APP", 2: "DLL", 3: "DRV", 4: "FONT", 5: "VXD", 7: "STATIC_LIB"} VERSION_KEY_LIMIT = 256 # characters; a block key longer than this is not a key def _sha256(text: str) -> str: import hashlib # here, not with the imports at the top, which other open changes edit return hashlib.sha256(text.encode("utf-8", "surrogatepass")).hexdigest() def _align4(at: int) -> int: return (at + 3) & ~3 def _vs_block(data: bytes, at: int, end: int) -> dict | None: """One version block (wLength, wValueLength, wType, szKey, Value, Children) at `at`, read no further than `end`: the key, where its value and children start and where it ends. None when the header or key doesn't fit, or the length is shorter than a header.""" if at + 6 > end: return None length, value_length, kind = struct.unpack_from(" end} def _vs_children(data: bytes, block: dict, what: str, problems: list[str]) -> list[dict]: """The child blocks of `block`. Zero bytes after the last child are padding; a child that can't be read ends the list with a problem.""" out, at = [], block["children"] while at + 6 <= block["end"] and len(out) < MAX_ENTRIES: if not struct.unpack_from(" dict: """VS_VERSIONINFO read from the bytes of one RT_VERSION resource. String values are returned as length and SHA-256 (of the value as UTF-8, without its terminating NUL), never as text.""" out = {"fixed": None, "translations": [], "string_tables": [], "string_count": 0, "other_blocks": 0} root = _vs_block(data, 0, len(data)) if root is None: problems.append("version block: VS_VERSIONINFO header is malformed") return out if root["cut"]: problems.append(f"version block: declares more bytes than the resource's {len(data)}") if root["key"] != "VS_VERSION_INFO": problems.append("version block: key is not VS_VERSION_INFO") fixed_at, fixed_end = root["value"], root["value_end"] if fixed_end - fixed_at >= 52: (signature, struct_version, file_ms, file_ls, product_ms, product_ls, flags_mask, flags, os_id, file_type, subtype, date_ms, date_ls) = struct.unpack_from("<13I", data, fixed_at) if signature != VS_FIXED_SIGNATURE: problems.append(f"version block: VS_FIXEDFILEINFO signature {signature:#x} is not " f"{VS_FIXED_SIGNATURE:#x}") else: out["fixed"] = { "file_version": f"{file_ms >> 16}.{file_ms & 0xFFFF}.{file_ls >> 16}.{file_ls & 0xFFFF}", "product_version": f"{product_ms >> 16}.{product_ms & 0xFFFF}.{product_ls >> 16}." f"{product_ls & 0xFFFF}", "struct_version": f"{struct_version >> 16}.{struct_version & 0xFFFF}", "flags_mask": flags_mask, "flags": flags, "flag_names": [name for bit, name in VS_FF.items() if flags & flags_mask & bit], "os": os_id, "os_name": VOS.get(os_id), "type": file_type, "type_name": VFT.get(file_type), "subtype": subtype, "date": (date_ms << 32) | date_ls, } elif fixed_end > fixed_at: problems.append(f"version block: VS_FIXEDFILEINFO is {fixed_end - fixed_at} bytes, not 52") for child in _vs_children(data, root, "VS_VERSIONINFO", problems): if child["key"] == "StringFileInfo": for table in _vs_children(data, child, "StringFileInfo", problems): key = table["key"] lang = key.lower() if re.fullmatch(r"[0-9A-Fa-f]{8}", key) else None if lang is None: problems.append("version block: a string table's key is not 8 hex digits") strings = [] for s in _vs_children(data, table, "string table", problems): raw = data[s["value"]:s["end"]] value = raw[:len(raw) & ~1].decode("utf-16-le", errors="replace").split("\0", 1)[0] entry = {"key": s["key"]} if s["key"] in VERSION_KEYS else {"key": None, "key_sha256": _sha256(s["key"])} entry.update({"chars": len(value), "sha256": _sha256(value)}) strings.append(entry) out["string_tables"].append({"lang_codepage": lang, "strings": strings}) out["string_count"] += len(strings) elif child["key"] == "VarFileInfo": for var in _vs_children(data, child, "VarFileInfo", problems): if var["key"] != "Translation": out["other_blocks"] += 1 continue for at in range(var["value"], var["value_end"] - 3, 4): lang, codepage = struct.unpack_from(" dict: """The resource directory: the types it holds and the first RT_VERSION block (_version_info). The tree is read three levels deep (type, name, language) inside the file bytes the directory's RVA maps to; a directory reached a second time (a loop, or two entries sharing a subdirectory) is not read again.""" out = {"present": False, "types": [], "named_types": 0, "version": None, "problems": []} problems = out["problems"] rva, size = img.directory(DIR_RESOURCE) if not rva or not size: return out out["present"] = True bounds = img._range(rva) if bounds is None: problems.append(f"resource directory RVA {rva:#x} has no file bytes") return out base, end = bounds data = img.data seen: set[int] = set() budget = [MAX_ENTRIES] def entries(offset: int, level: str) -> list[tuple[int | None, int, bool]]: """(id, None for a named entry; child offset; child is a directory) of the directory at `offset`.""" if offset in seen: problems.append(f"resource tree: {level} directory at +{offset:#x} is reached a second time " "(a loop or a shared subdirectory)") return [] seen.add(offset) at = base + offset if at + 16 > end: problems.append(f"resource tree: {level} directory at +{offset:#x} is past the end of its file bytes") return [] named, ids = struct.unpack_from(" end: problems.append(f"resource tree: {level} directory at +{offset:#x} is cut short after {i} of " f"{named + ids} entries") break if budget[0] <= 0: problems.append(f"resource tree: more than {MAX_ENTRIES} entries; the rest are not read") break budget[0] -= 1 name, child = struct.unpack_from(" end: problems.append(f"resource tree: data entry at +{leaf_at:#x} is past the end of its file bytes") continue data_rva, data_size, codepage = struct.unpack_from(" dict: """_resources, with a table it misreads anyway reported as a problem instead of stopping the report.""" try: return _resources(img) except (struct.error, IndexError) as e: return {"present": True, "types": [], "named_types": 0, "version": None, "problems": [f"resource directory: truncated or corrupt table ({e})"]} def _export_module_name(img: PeImage) -> str: """The module name the export directory records (its Name field), or "" when there is none.""" rva, size = img.directory(DIR_EXPORT) at = img.offset(rva, 40) if rva and size >= 40 else None if at is None: return "" return img.string_at(struct.unpack_from(" int: rva, size = img.directory(DIR_TLS) header_size = 40 if img.is64 else 24 callbacks_end = 32 if img.is64 else 16 if not rva or size < header_size: return 0 at = img.offset(rva, header_size) if at is None: start = img.offset(rva) if start is not None and start + callbacks_end > len(img.data): raise PeFormatError("truncated TLS directory") return 0 callbacks_va = struct.unpack_from(" dict: """Exception Directory (.pdata / DIR_EXCEPTION = 3) validation and function table parsing. Matches runtime/src/pe_loader.cpp (Loader::register_unwind) bounds, sorting, non-overlapping intervals, and zero-length entries (e.g. symcryptk.dll). Non-x64 architectures (e.g. x86, ARM64) do not use x64 12-byte RUNTIME_FUNCTION tables and are gracefully rejected with informative format metadata. """ rva, size = img.directory(DIR_EXCEPTION) present = bool(rva and size) if not present: return { "present": False, "rva": "0x0", "size": 0, "entry_count": 0, "parsed_count": 0, "trailing_bytes": 0, "mapped": False, "truncated": False, "zero_length_count": 0, "inverted_count": 0, "out_of_bounds_count": 0, "unsorted_count": 0, "overlap_count": 0, "min_begin": None, "max_end": None, "valid": True, "sample_errors": [], } is_x64 = img.machine == 0x8664 is_arm64 = img.machine == 0xAA64 entry_size = 8 if is_arm64 else 12 entry_count = size // entry_size trailing_bytes = size % entry_size bounds = img._range(rva) if not is_x64: if is_arm64: fmt = "arm64" reason = "ARM64 architecture uses 8-byte packed unwind records (not 12-byte RUNTIME_FUNCTION)" elif img.machine == 0x14C: fmt = "non-x64" reason = "x86 architecture uses stack-based SEH (no 12-byte RUNTIME_FUNCTION table)" else: fmt = f"unsupported ({MACHINES.get(img.machine, hex(img.machine))})" reason = f"unsupported machine architecture 0x{img.machine:x} (not x86-64)" if bounds is None: mapped = False truncated = False else: start, end = bounds mapped = True needed_bytes = entry_count * entry_size truncated = max(0, end - start) < needed_bytes return { "present": True, "rva": hex(rva), "size": size, "entry_count": entry_count, "parsed_count": 0, "trailing_bytes": trailing_bytes, "mapped": mapped, "truncated": truncated, "zero_length_count": 0, "inverted_count": 0, "out_of_bounds_count": 0, "unsorted_count": 0, "overlap_count": 0, "min_begin": None, "max_end": None, "valid": True, "sample_errors": [], "format": fmt, "rejection_reason": reason, } if bounds is None: return { "present": True, "rva": hex(rva), "size": size, "entry_count": entry_count, "parsed_count": 0, "trailing_bytes": trailing_bytes, "mapped": False, "truncated": False, "zero_length_count": 0, "inverted_count": 0, "out_of_bounds_count": 0, "unsorted_count": 0, "overlap_count": 0, "min_begin": None, "max_end": None, "valid": False, "sample_errors": [{"index": 0, "begin": "0x0", "end": "0x0", "unwind": "0x0", "reason": "unmapped"}], } start, end = bounds available = max(0, end - start) needed_bytes = entry_count * 12 truncated = available < needed_bytes can_parse = min(needed_bytes, available) // 12 parsed_count = can_parse mv = memoryview(img.data) pdata_view = mv[start:start + can_parse * 12] previous_begin = 0 previous_end = 0 min_begin = None max_end = None zero_length_count = 0 inverted_count = 0 out_of_bounds_count = 0 unsorted_count = 0 overlap_count = 0 sample_errors = [] def add_error(idx: int, b: int, e: int, u: int, reason: str) -> None: if len(sample_errors) < 5: sample_errors.append({ "index": idx, "begin": hex(b), "end": hex(e), "unwind": hex(u), "reason": reason, }) for i, (b, e, u) in enumerate(struct.iter_unpack(" max_end: max_end = e entry_failed = False if b == e: zero_length_count += 1 elif e < b: inverted_count += 1 add_error(i, b, e, u, "inverted") entry_failed = True info = u & ~1 if (b > img.size_of_image or e > img.size_of_image or info > img.size_of_image or (img.size_of_image - info < 4)): out_of_bounds_count += 1 if not entry_failed: add_error(i, b, e, u, "out_of_bounds") entry_failed = True if i > 0: if b < previous_begin: unsorted_count += 1 if not entry_failed: add_error(i, b, e, u, "unsorted") entry_failed = True elif b < previous_end: overlap_count += 1 if not entry_failed: add_error(i, b, e, u, "overlap") entry_failed = True previous_begin = b if e >= b: previous_end = max(previous_end, e) valid = ( not truncated and inverted_count == 0 and out_of_bounds_count == 0 and unsorted_count == 0 and overlap_count == 0 ) return { "present": True, "rva": hex(rva), "size": size, "entry_count": entry_count, "parsed_count": parsed_count, "trailing_bytes": trailing_bytes, "mapped": True, "truncated": truncated, "zero_length_count": zero_length_count, "inverted_count": inverted_count, "out_of_bounds_count": out_of_bounds_count, "unsorted_count": unsorted_count, "overlap_count": overlap_count, "min_begin": hex(min_begin) if min_begin is not None else None, "max_end": hex(max_end) if max_end is not None else None, "valid": valid, "sample_errors": sample_errors, } _apiset_cache: dict[str, bool] = {} def _apiset_resolves(name: str) -> bool: """True if this Windows' API-set schema maps the contract to a host DLL (ntdll query; nothing is loaded).""" if name not in _apiset_cache: _apiset_cache[name] = _query_apiset(name) return _apiset_cache[name] def _query_apiset(name: str) -> bool: if os.name != "nt": return False try: import ctypes from ctypes import wintypes class UNICODE_STRING(ctypes.Structure): _fields_ = [("Length", wintypes.USHORT), ("MaximumLength", wintypes.USHORT), ("Buffer", wintypes.LPWSTR)] query = ctypes.WinDLL("ntdll").ApiSetQueryApiSetPresence query.argtypes = [ctypes.POINTER(UNICODE_STRING), ctypes.POINTER(ctypes.c_ubyte)] query.restype = ctypes.c_long base = name[:-4] if name.lower().endswith(".dll") else name buf = ctypes.create_unicode_buffer(base) text = ctypes.cast(buf, wintypes.LPWSTR) us = UNICODE_STRING(len(base) * 2, (len(base) + 1) * 2, text) present = ctypes.c_ubyte(0) status = query(ctypes.byref(us), ctypes.byref(present)) return status == 0 and bool(present.value) except (OSError, AttributeError, ValueError): return False # A DLL file name as an image stores it for LoadLibrary*: ASCII or UTF-16LE, letters, digits, '_', '-' and '.'. # A UTF-16 name starts after a NUL (or at the start), so the tail of an ASCII string followed by one isn't read as one. _DLL_ASCII = re.compile(rb"[A-Za-z0-9_][A-Za-z0-9_.\-]{0,63}\.[dD][lL][lL](?![A-Za-z0-9_])") _DLL_WIDE = re.compile(rb"(? list[dict]: """DLL names the image holds as strings but doesn't import: candidates for a run-time LoadLibrary*, which the import table doesn't show (XCurl.dll and libHttpClient.GDK.dll reach xgameruntime.dll only this way, docs/runtime_imports.md). `imported` is the lower-cased names from the import tables; the image's own names (its file name and the name in its export directory) are left out too. Each entry is {"name", "encoding": "ascii" | "utf-16" | "both"}, sorted by name. A string is not proof of a load, only of where to look.""" seen: dict[str, set[str]] = {} for m in _DLL_ASCII.finditer(data): seen.setdefault(m.group().decode("ascii").lower(), set()).add("ascii") for m in _DLL_WIDE.finditer(data): seen.setdefault(m.group().decode("utf-16-le").lower(), set()).add("utf-16") own = (own_name,) if isinstance(own_name, str) else own_name skip = imported | {n.lower() for n in own if n} return [{"name": n, "encoding": "both" if len(e) == 2 else next(iter(e))} for n, e in sorted(seen.items()) if n not in skip] # Host DLLs the runtime's shim registry does not own although a SHIM_TABLE lists them: only a few of their functions # are replaced (runtime/shims/registry.cpp, owns_dll). API sets are never owned either. _SHIM_HOST_DLLS = frozenset({"kernel32.dll", "kernelbase.dll", "msvcrt.dll", "ucrtbase.dll", "ucrtbased.dll", "vulkan-1.dll", "ntdll.dll", "user32.dll", "ws2_32.dll", "wsock32.dll", "advapi32.dll"}) _SHIM_TABLE_DLL = re.compile(r'[{(]\s*"([A-Za-z0-9_.\-]+\.[dD][lL][lL])"\s*[,)]') _shim_dlls_cache: dict[str, frozenset[str]] = {} def _shim_table_bodies(text: str) -> list[str]: """The argument text of every SHIM_TABLE(...) in a source file (parentheses matched; export names hold none).""" bodies = [] at = text.find("SHIM_TABLE(") while at >= 0: start = i = at + len("SHIM_TABLE(") depth = 1 while i < len(text) and depth: depth += {"(": 1, ")": -1}.get(text[i], 0) i += 1 bodies.append(text[start:i - 1]) at = text.find("SHIM_TABLE(", i) return bodies def runtime_shim_dlls(folder: str = RUNTIME_SHIMS) -> frozenset[str]: """Lower-cased names of the DLLs the runtime's shims own (owns_dll in runtime/shims/registry.cpp): every DLL a SHIM_TABLE in `folder`'s .cpp files lists, less the host DLLs and API sets the registry only patches. Empty when the sources are not there (a copy of this tool outside the repository).""" key = os.path.abspath(folder) if key not in _shim_dlls_cache: names: set[str] = set() try: files = sorted(n for n in os.listdir(folder) if n.endswith(".cpp")) except OSError: files = [] for n in files: try: with open(os.path.join(folder, n), encoding="utf-8", errors="replace") as f: text = f.read() except OSError: continue for body in _shim_table_bodies(text): names.update(m.lower() for m in _SHIM_TABLE_DLL.findall(body)) _shim_dlls_cache[key] = frozenset( n for n in names if n not in _SHIM_HOST_DLLS and not n.startswith(("api-ms-", "ext-ms-"))) return _shim_dlls_cache[key] _listing_cache: dict[tuple[str, int], dict[str, str]] = {} def _file_names(folder: str) -> dict[str, str]: """{lower-cased name: name on disk} of the files in `folder` (empty when it can't be read). Read once per folder state: the key holds the folder's mtime, which changes when a file is added, removed or renamed.""" try: stamp = os.stat(folder).st_mtime_ns except OSError: return {} key = (os.path.abspath(folder), stamp) if key not in _listing_cache: try: with os.scandir(folder) as entries: _listing_cache[key] = {e.name.lower(): e.name for e in entries if e.is_file()} except OSError: _listing_cache[key] = {} return _listing_cache[key] def wine_system32() -> str | None: """On a host that isn't Windows, the Wine prefix's system32, which stands in for System32: $WINEPREFIX/drive_c/windows/system32 when WINEPREFIX is set, else ~/.wine's. None when that folder is missing (a WINEPREFIX that names no prefix is not replaced by ~/.wine, which would answer for another prefix).""" prefix = os.environ.get("WINEPREFIX") or os.path.join(os.path.expanduser("~"), ".wine") path = os.path.join(prefix, "drive_c", "windows", "system32") return path if os.path.isdir(path) else None def system_dll_source() -> tuple[str, str | None]: """Where `system` is checked on this host: ("windows", SYSTEM32), ("wine-prefix", ), or ("none", None) on a host that isn't Windows and has no Wine prefix.""" if IS_WINDOWS: return "windows", SYSTEM32 path = wine_system32() return ("wine-prefix", path) if path else ("none", None) _wine_apiset_cache: dict[tuple[str, int], frozenset[str]] = {} def _schema_contracts(data: bytes) -> frozenset[str]: """The contracts an API-set schema (an .apiset section) maps to a host DLL. Version 6 only, the layout of Windows 10 and later and of Wine: a header of version, size, flags, count, entry offset, hash offset and hash factor; a 24-byte namespace entry of flags, name offset, name length, hashed length, value offset and value count; a 20-byte value entry of flags, name offset, name length, value offset and value length. Each contract is the lower-cased name cut at its hashed length (up to the last hyphen), which is what a lookup compares.""" if len(data) < 28 or struct.unpack_from(" len(data): break _flags, name_at, _name_len, hashed_len, values_at, value_count = struct.unpack_from("<6I", data, at) if name_at + hashed_len > len(data): continue hosts = 0 for v in range(min(value_count, MAX_ENTRIES)): value = values_at + v * 20 if value + 20 > len(data): break hosts += struct.unpack_from(" 0 if hosts: found.add(data[name_at:name_at + hashed_len].decode("utf-16-le", errors="replace").lower()) return frozenset(found) def wine_apisets(system32: str) -> frozenset[str]: """The API-set contracts the prefix's apisetschema.dll resolves (_schema_contracts); empty when the file, its .apiset section or a version-6 schema is missing. Wine resolves API sets this way and installs no file for most of them.""" disk = _file_names(system32).get("apisetschema.dll") if disk is None: return frozenset() path = os.path.join(system32, disk) try: key = (os.path.abspath(path), os.stat(path).st_mtime_ns) except OSError: return frozenset() if key not in _wine_apiset_cache: try: with open(path, "rb") as f: img = PeImage(f.read()) section = next((s for s in img.sections if s["name"] == ".apiset"), None) _wine_apiset_cache[key] = _schema_contracts(img.section_data(section) if section else b"") except (OSError, PeFormatError, struct.error): _wine_apiset_cache[key] = frozenset() return _wine_apiset_cache[key] def _apiset_key(name: str) -> str: """An API-set name as a schema lookup compares it: lower case, without ".dll" and the part after the last hyphen.""" base = name.lower() if base.endswith(".dll"): base = base[:-4] return base.rsplit("-", 1)[0] def _bundled(name: str, game_dir: str | None) -> bool: """Ships in the game folder, compared without case as Windows does: an import of MSVCP140.dll finds the package's msvcp140.dll on a case-sensitive file system too.""" if not game_dir: return False return os.path.isfile(os.path.join(game_dir, name)) or name.lower() in _file_names(game_dir) # The values of an import's "where". "unverified" is reported only on a host that isn't Windows and has no Wine # prefix: the DLL is not bundled and the runtime's shims don't own it, so it is most likely a system DLL, but there # was nothing to check it against. The Markdown report shows it as "system (unverified)". CLASSES = ("bundled", "system", "shim", "unverified") LABELS = {"unverified": "system (unverified)"} def classify_dll(name: str, game_dir: str | None) -> str: """One of CLASSES. bundled = ships beside the game; system = present on this Windows, or in the Wine prefix's system32 (or its API-set schema) on another host; shim = must be reimplemented; unverified = on a host that isn't Windows and has no Wine prefix, neither bundled nor owned by the runtime's shims (runtime_shim_dlls).""" if _bundled(name, game_dir): return "bundled" low = name.lower() apiset = low.startswith("api-ms-win-") or low.startswith("ext-ms-win-") if IS_WINDOWS: if apiset: # API sets resolve through the OS schema: ask this Windows whether it knows the contract. return "system" if _apiset_resolves(name) else "shim" if os.path.isfile(os.path.join(SYSTEM32, name)): return "system" return "shim" system32 = wine_system32() if system32 is None: # Nothing to check against: only a module the runtime's shims own is known to need a shim. return "shim" if not apiset and low in runtime_shim_dlls() else "unverified" if low in _file_names(system32): return "system" if apiset: return "system" if _apiset_key(name) in wine_apisets(system32) else "shim" return "shim" def inspect(path: str, game_dir: str | None) -> dict: """The report for one image. A file cut off inside a table raises PeFormatError like any other bad image; the struct.error it used to raise escaped main() and stopped a multi-file run at that file.""" try: return _inspect(path, game_dir) except struct.error as e: raise PeFormatError(f"truncated or corrupt table: {e}") from None def _inspect(path: str, game_dir: str | None) -> dict: with open(path, "rb") as f: img = PeImage(f.read()) sections = [] for s in img.sections: data = img.section_data(s) e = entropy(data) sections.append({ "name": s["name"], "rva": s["rva"], "vsize": s["vsize"], "rawsize": s["rawsize"], "exec": s["exec"], "write": s["write"], "entropy": round(e, 3), "looks_encrypted": e > 7.9 and len(data) > 4096, }) imports = _imports(img, "normal", DIR_IMPORT) + _imports(img, "delay", DIR_DELAY_IMPORT, delay=True) for entry in imports: entry["where"] = classify_dll(entry["dll"], game_dir) exp = _inspect_exports(img) exports = exp["exports"] own_names = (os.path.basename(path), _export_module_name(img)) loads = dll_strings(img.data, {i["dll"].lower() for i in imports}, own_names) for entry in loads: entry["where"] = classify_dll(entry["name"], game_dir) pdata = _inspect_pdata(img) reloc_rva, reloc_size = img.directory(DIR_BASERELOC) checked, system_dir = system_dll_source() return { "file": os.path.basename(path), "size": os.path.getsize(path), "machine": MACHINES.get(img.machine, hex(img.machine)), "timestamp": img.timestamp, "is_dll": bool(img.characteristics & 0x2000), "subsystem": SUBSYSTEMS.get(img.subsystem, img.subsystem), "image_base": hex(img.image_base), "size_of_image": img.size_of_image, "entry_rva": hex(img.entry_rva), "has_relocs": bool(reloc_rva and reloc_size), "dynamic_base": bool(img.dll_characteristics & 0x40), "resources": resources(img), "sections": sections, "imports": imports, "export_count": len(exports), "exports": exports, "forwarders": exp["forwarders"], "forwarder_count": len(exp["forwarders"]), "invalid_export_count": exp["invalid_export_count"], "invalid_exports": exp["invalid_exports"], "pdata_functions": pdata["entry_count"], "pdata": pdata, "tls_callbacks": _tls_callbacks(img), "runtime_load_candidates": loads, # Where `system` was checked: "windows" (System32 and the OS API-set schema), "wine-prefix" (its system32 # and apisetschema.dll) or "none" (no Wine prefix on a host that isn't Windows: see "unverified"). "system_dlls": {"checked": checked, "dir": system_dir}, } def system_dlls_note(r: dict) -> str | None: """One line on where system DLLs were checked, for a report made on a host that isn't Windows; None otherwise.""" info = r.get("system_dlls") or {} if info.get("checked") == "wine-prefix": return f"System DLLs checked in the Wine prefix's `{info['dir']}`." if info.get("checked") == "none": return ("System DLLs could not be checked: no Wine prefix (set `WINEPREFIX`, or make `~/.wine`). A DLL that " "is not bundled and that the runtime's shims don't own is listed as `system (unverified)`.") return None def to_markdown(r: dict) -> str: pdata_summary = f"{r['pdata_functions']:,}" p = r.get("pdata") if p and p.get("present"): if p.get("format"): pdata_summary += f" ({p['format']})" elif p.get("valid"): notes = [] if p.get("zero_length_count"): notes.append(f"{p['zero_length_count']} zero-length") if p.get("trailing_bytes"): notes.append(f"{p['trailing_bytes']} trailing bytes ignored") suffix = f", {', '.join(notes)}" if notes else "" pdata_summary += f" (OK{suffix})" else: issues = [] if not p.get("mapped"): issues.append("unmapped") if p.get("truncated"): issues.append("truncated") if p.get("inverted_count"): issues.append(f"{p['inverted_count']} inverted") if p.get("out_of_bounds_count"): issues.append(f"{p['out_of_bounds_count']} out of bounds") if p.get("unsorted_count"): issues.append(f"{p['unsorted_count']} unsorted") if p.get("overlap_count"): issues.append(f"{p['overlap_count']} overlapping") pdata_summary += f" (INVALID: {', '.join(issues)})" exp_summary = f"{r['export_count']:,}" fwd_count = r.get("forwarder_count", 0) inv_count = r.get("invalid_export_count", 0) exp_parts = [] if fwd_count > 0: exp_parts.append(f"{fwd_count} forwarded") if inv_count > 0: inv_details = [f"{inv['symbol']} ({inv['reason']})" for inv in r.get("invalid_exports", [])] details_str = f": {', '.join(inv_details)}" if inv_details else "" exp_parts.append(f"INVALID: {inv_count} invalid{details_str}") if exp_parts: exp_summary += f" ({'; '.join(exp_parts)})" out = [f"# {r['file']}", "", f"- {r['machine']}, {'DLL' if r['is_dll'] else 'EXE'}, subsystem {r['subsystem']}, {r['size']:,} bytes", f"- image base {r['image_base']}, size {r['size_of_image']:,}, entry RVA {r['entry_rva']}", f"- relocations: {'yes' if r['has_relocs'] else 'NO (must load at preferred base)'}; " f"ASLR flag: {r['dynamic_base']}", f"- .pdata functions: {pdata_summary}; TLS callbacks: {r['tls_callbacks']}; exports: {exp_summary}", "", "## Sections", "", "| name | rva | vsize | exec | write | entropy | |", "|---|---|---|---|---|---|---|"] for s in r["sections"]: flag = "**encrypted?**" if s["looks_encrypted"] else "" out.append(f"| {s['name']} | {s['rva']:#x} | {s['vsize']:,} | {s['exec']} | {s['write']} | {s['entropy']} | {flag} |") out += ["", "## Imports", ""] by_where = Counter() for imp in r["imports"]: by_where[imp["where"]] += len(imp["functions"]) out.append(", ".join(f"{LABELS.get(k, k)}: {v}" for k, v in sorted(by_where.items())) or "none") note = system_dlls_note(r) if note: out += ["", note] out += ["", "| dll | kind | resolves | functions |", "|---|---|---|---|"] for imp in sorted(r["imports"], key=lambda i: (i["where"] != "shim", i["dll"].lower())): out.append(f"| {imp['dll']} | {imp['kind']} | {LABELS.get(imp['where'], imp['where'])} | " f"{len(imp['functions'])} |") loads = r.get("runtime_load_candidates", []) if loads: out += ["", "## Run-time load candidates", "", "DLL names held as strings but not imported: a `LoadLibrary*` the import table doesn't show " "(a string is where to look, not proof of a load).", "", "| dll | encoding | resolves |", "|---|---|---|"] out += [f"| {d['name']} | {d['encoding']} | {LABELS.get(d['where'], d['where'])} |" for d in loads] shims = [i for i in r["imports"] if i["where"] == "shim"] if shims: out += ["", "## Needs checking or a shim", ""] for imp in shims: out.append(f"### {imp['dll']} ({imp['where']})") out.append("") out.append(", ".join(f"`{f}`" for f in imp["functions"])) out.append("") out += resources_markdown(r.get("resources")) forwarders = r.get("forwarders", {}) if forwarders: out += ["", "## Forwarded Exports", "", "| export | target |", "|---|---|"] items = sorted(forwarders.items()) for exp_name, target in items[:50]: esc_name = exp_name.replace("|", r"\|") esc_target = target.replace("|", r"\|") out.append(f"| `{esc_name}` | `{esc_target}` |") if len(items) > 50: out.append(f"| ... and {len(items) - 50} more | |") return "\n".join(out) + "\n" def resources_markdown(res: dict | None) -> list[str]: """The resource section of the Markdown report: types and counts, then the version block without its values.""" if not res or not res["present"]: return ["", "## Resources", "", "none"] out = ["", "## Resources", ""] types = ", ".join(f"{t['type']} {t['entries']} ({t['bytes']:,} bytes)" for t in res["types"]) out.append(f"- types: {types or 'none'}" + (f"; named types: {res['named_types']}" if res["named_types"] else "")) out += [f"- problem: {p}" for p in res["problems"]] v = res["version"] if v is None: out += ["", "No version block (RT_VERSION)."] return out out += ["", "### Version block", ""] lang = "-" if v["language"] is None else f"{v['language']:#06x}" out.append(f"- resource language {lang}, code page {v['codepage']}, {v['size']:,} bytes") f = v["fixed"] if f: out.append(f"- file version {f['file_version']}, product version {f['product_version']}") out.append(f"- flags: {', '.join(f['flag_names']) or 'none'} (flags {f['flags']:#x}, mask " f"{f['flags_mask']:#x}); OS {f['os_name'] or hex(f['os'])}; type " f"{f['type_name'] or f['type']}, subtype {f['subtype']}; date {f['date']}") else: out.append("- no VS_FIXEDFILEINFO") out.append(f"- translations: {', '.join(v['translations']) or 'none'}; string tables: " f"{', '.join(t['lang_codepage'] or '?' for t in v['string_tables']) or 'none'}; " f"other blocks: {v['other_blocks']}") if v["string_count"]: out += ["", f"{v['string_count']} string values, not shown: each is listed by key with its length and the " "start of its SHA-256 (the JSON has the full hash). A key outside the standard names is listed by " "its hash too.", "", "| table | key | chars | sha256 |", "|---|---|---|---|"] for t in v["string_tables"]: for s in t["strings"]: key = s["key"] or f"(other, {s['key_sha256'][:16]})" out.append(f"| {t['lang_codepage'] or '?'} | {key} | {s['chars']} | {s['sha256'][:16]} |") return out def version_summary(r: dict) -> str | None: """The stdout line for an image with a version block: numbers and counts only.""" res = r.get("resources") or {} v = res.get("version") if v is None: return f"{r['file']}: resource directory problems: {len(res['problems'])}" if res.get("problems") else None f = v["fixed"] numbers = f"file {f['file_version']}, product {f['product_version']}" if f else "no fixed info" return (f"{r['file']}: version block {numbers}, {v['string_count']} string keys (values not shown)" + (f", problems: {len(res['problems'])}" if res["problems"] else "")) def main(argv: list[str] | None = None) -> int: ap = argparse.ArgumentParser() ap.add_argument("image", nargs="+") ap.add_argument("--game-dir") ap.add_argument("--out", default=os.path.join(os.path.dirname(__file__), "..", "reports", "pe")) a = ap.parse_args(argv) if a.game_dir is not None and not os.path.isdir(a.game_dir): print(f"ERROR: --game-dir {a.game_dir} does not exist or is not a directory", file=sys.stderr) return 2 missing = [img for img in a.image if not os.path.exists(img)] if missing: for img in missing: print(f"ERROR: {img}: does not exist", file=sys.stderr) return 2 if os.path.isfile(a.out): print(f"cannot create output directory {a.out}: path is a file, not a directory", file=sys.stderr) return 1 try: os.makedirs(a.out, exist_ok=True) except OSError as e: print(f"cannot create output directory {a.out}: {e.strerror or e}", file=sys.stderr) return 1 rc = 0 seen_paths: set[tuple[str, str]] = set() used_names: set[str] = set() notes: list[str] = [] for img in a.image: search_dir = a.game_dir or os.path.dirname(os.path.abspath(img)) key = (os.path.normcase(os.path.realpath(img)), os.path.normcase(os.path.abspath(search_dir))) if key in seen_paths: print(f"{img}: given more than once; inspected once", file=sys.stderr) continue seen_paths.add(key) try: r = inspect(img, search_dir) except (PeFormatError, OSError) as e: print(f"{img}: not a PE image ({e})", file=sys.stderr) rc = 1 continue # One report per image: the full file name keeps game.exe and game.dll apart, and a numbered suffix keeps # a/x.exe and b/x.exe apart. Compared case-insensitively, as Windows file names are. name = r["file"] n = 1 while name.lower() in used_names: n += 1 name = f"{r['file']}-{n}" used_names.add(name.lower()) if n > 1: print(f"{img}: another input is also named {r['file']}; report written as {name}.json/.md", file=sys.stderr) stem = os.path.join(a.out, name) json_path = stem + ".json" md_path = stem + ".md" collision = None for other in a.image: if same_output_path(other, json_path): collision = json_path break if same_output_path(other, md_path): collision = md_path break if collision: print(f"{img}: report {collision} would overwrite input file", file=sys.stderr) rc = 1 continue try: with open(json_path, "w", encoding="utf-8", newline="\n") as f: json.dump(r, f, indent=1) with open(md_path, "w", encoding="utf-8", newline="\n") as f: f.write(to_markdown(r)) version = version_summary(r) # numbers and counts only, never a value of the block if version: print(version) except OSError as e: print(f"{img}: cannot write report ({e.strerror or e})", file=sys.stderr) rc = 1 continue enc = [s["name"] for s in r["sections"] if s["looks_encrypted"]] shim = sum(len(i["functions"]) for i in r["imports"] if i["where"] == "shim") unverified = sum(len(i["functions"]) for i in r["imports"] if i["where"] == "unverified") pdata_status = "" p = r.get("pdata") if p and p.get("present"): if p.get("format"): pdata_status = p["format"] else: pdata_status = "OK" if p.get("valid") else "INVALID" pdata_col = f"{r['pdata_functions']} {pdata_status}".strip() print(f"{r['file']:<40} {r['machine']:<7} subsys={r['subsystem']!s:<7} pdata={pdata_col:<9} " f"shim-imports={shim:<5} encrypted={','.join(enc) or '-'}" + (f" unverified-imports={unverified}" if unverified else "")) note = system_dlls_note(r) if note and note not in notes: notes.append(note) for note in notes: # once per run, after the per-image lines; only on a host that isn't Windows print(note.replace("`", "")) return rc if __name__ == "__main__": try: # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode sys.stdout.reconfigure(errors="replace") except (AttributeError, ValueError, OSError): pass sys.exit(main())