#!/usr/bin/env python3 """Compile the guest thread programs with clang and list their code. generate write program.txt next to this file --check recompile in memory and fail if the committed listing differs program.txt holds one line per item: `code `, `entry ` and `import ` for each 32-bit RIP-relative reference to an import slot. Nothing is executed and no linker is needed: the test places the code and fills in the references. Only clang and the Python standard library are used. """ import struct import subprocess import sys import tempfile from pathlib import Path HERE = Path(__file__).resolve().parent OUT = HERE / "program.txt" FLAGS = ["-target", "x86_64-pc-windows-msvc", "-O1", "-ffreestanding", "-fno-builtin", "-nostdinc", "-fno-stack-protector", "-fno-asynchronous-unwind-tables", "-mno-implicit-float", "-fno-jump-tables", "-fno-optimize-sibling-calls", "-fno-ident", "-Wno-infinite-recursion"] REL32 = range(4, 10) # IMAGE_REL_AMD64_REL32 to REL32_5 def listing(): with tempfile.TemporaryDirectory() as directory: obj = Path(directory, "program.obj") subprocess.run(["clang", *FLAGS, "-c", HERE / "program.c", "-o", obj], check=True) data = obj.read_bytes() machine, sections, _, symbols_at, symbol_count, optional, _ = struct.unpack_from("