"""Stamp and check the upscale bridge DLLs. stamp DLL write Wine's builtin signature at 0x40, as winebuild --builtin does verify DLL DEF SRC OBJDUMP the exports `OBJDUMP -p` reports must be the .def names plus SRC's dllexports """ import re import subprocess import sys from pathlib import Path # tools/winebuild/spec32.c: static const char builtin_signature[32] = "Wine builtin DLL"; SIGNATURE = b'Wine builtin DLL'.ljust(32, b'\0') OFFSET = 0x40 def stamp(dll): data = bytearray(Path(dll).read_bytes()) if data[:2] != b'MZ': sys.exit(f'{dll}: not a PE file') lfanew = int.from_bytes(data[0x3c:0x40], 'little') if lfanew < OFFSET + len(SIGNATURE): sys.exit(f'{dll}: not enough space ({lfanew:#x}) for the Wine signature') data[OFFSET:OFFSET + len(SIGNATURE)] = SIGNATURE Path(dll).write_bytes(data) if Path(dll).read_bytes()[OFFSET:OFFSET + len(SIGNATURE)] != SIGNATURE: sys.exit(f'{dll}: Wine signature did not stick') def def_names(path): names, exports = set(), False for line in Path(path).read_text().splitlines(): line = line.split(';', 1)[0].strip() if not line: continue if line.upper() == 'EXPORTS': exports = True elif exports: names.add(line.split()[0].split('=', 1)[0]) return names def source_names(path): text = Path(path).read_text() return {re.findall(r'\w+', head)[-1] for head in re.findall(r'__declspec\(dllexport\)([^(;{]*)\(', text)} def exports(dll, objdump): report = subprocess.run([objdump, '-p', dll], check=True, capture_output=True, text=True).stdout if 'file format pei-x86-64' not in report: sys.exit(f'{dll}: not an x86-64 PE image') table = report.split('[Ordinal/Name Pointer] Table', 1) if len(table) != 2: sys.exit(f'{dll}: no export table') # One row per export, "[ 0] +base[ 1] 0000 name" (binutils 2.45) or "[ 0] name" (older): the rows # start with an index in brackets; the column titles some versions print do not. names = [] for line in table[1].splitlines()[1:]: if not line.strip(): if names: break continue if re.match(r'\s*\[\s*\d+\]', line): names.append(line.split()[-1]) if len(names) != len(set(names)): sys.exit(f'{dll}: duplicate exports') return set(names) def verify(dll, definition, source, objdump): found = exports(dll, objdump) expected = def_names(definition) | source_names(source) missing, extra = sorted(expected - found), sorted(found - expected) if missing or extra: sys.exit(f'{dll}: exports do not match {Path(definition).name}: missing {missing}, unexpected {extra}') print(f'{Path(dll).name}: {len(found)} exports match {Path(definition).name}') if __name__ == '__main__': if sys.argv[1:2] == ['stamp'] and len(sys.argv) == 3: stamp(sys.argv[2]) elif sys.argv[1:2] == ['verify'] and len(sys.argv) == 6: verify(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]) else: sys.exit(__doc__)