#!/usr/bin/env bash # dd-directcontroller: build the XInput DLLs DroidDeck-Bionic puts in a prefix for games set to start without Steam # (launch.sh). Used by tools/bionic-runtime/build.sh. # # build.sh toolchain fetch the pinned llvm-mingw into $LLVM_MINGW (default ~/.cache/...) # build.sh dlls /{arm64x,i386}/.dll for every XInput DLL name, each export table # (both views of an ARM64X image), import list and machine type checked # build.sh host /host_test (native, cc): the ring snapshot reader against a writer # # The PE side uses bylaws/llvm-mingw (its arm64ec target does not predefine __x86_64__, unlike upstream's). # One C source (src/dd_directcontroller.c) is linked once per DLL name with that name's .def file (def/), so each # DLL has exactly the exports and ordinals of the Windows DLL it stands in for. i386 .def files get stdcall # decorations here and the link strips them (--kill-at). # # arm64x (system32 of the arm64ec layer): the source is compiled for ARM64EC and for native ARM64 and both # objects are linked into one ARM64X image (lld: -m arm64xpe, the .def for the EC view, -defarm64native for # the native view; each view gets its own CRT, the native one taken from the toolchain's own aarch64 link # line). An x64 game loads the EC view, a native ARM64 process (Wine's own control.exe, ...) the ARM64 view; # an ARM64EC-only image fails there with c000007b. i386 goes to syswow64 for 32-bit games. # # Every DLL carries a VERSIONINFO resource with the file version of the Wine builtin DLL it replaces, so a # program reading it sees the same number as before. set -euo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" LLVM_MINGW_VER=20250920 case "$(uname -m)" in aarch64|arm64) LLVM_MINGW_HOST=aarch64; HOST_SUFFIX=-aarch64 LLVM_MINGW_SHA256=bce5cc755c613515fd44e1ee9523123d854103abae147571adb645450036274d ;; *) LLVM_MINGW_HOST=x86_64; HOST_SUFFIX= LLVM_MINGW_SHA256=8dd8c34fc051a50c2fae86015f35057f8aae93fe1e19b34537ef1269a8b4c772 ;; esac LLVM_MINGW_TAR="llvm-mingw-${LLVM_MINGW_VER}-ucrt-ubuntu-22.04-${LLVM_MINGW_HOST}.tar.xz" LLVM_MINGW_URL="https://github.com/bylaws/llvm-mingw/releases/download/${LLVM_MINGW_VER}/${LLVM_MINGW_TAR}" LLVM_MINGW="${LLVM_MINGW:-$HOME/.cache/llvm-mingw-bylaws-${LLVM_MINGW_VER}${HOST_SUFFIX}}" DLLS="xinput1_1 xinput1_2 xinput1_3 xinput1_4 xinput9_1_0 xinputuap" ARCHES="arm64x i386" MARKER="dd-directcontroller: " say() { printf '== dd-directcontroller: %s\n' "$*"; } die() { printf '::error::dd-directcontroller: %s\n' "$*" >&2; exit 1; } toolchain() { if [ ! -x "$LLVM_MINGW/bin/arm64ec-w64-mingw32-clang" ]; then say "fetching $LLVM_MINGW_TAR" local tmp; tmp="$(mktemp -d)" curl -fsSL --retry 5 --retry-delay 5 -o "$tmp/t.tar.xz" "$LLVM_MINGW_URL" echo "$LLVM_MINGW_SHA256 $tmp/t.tar.xz" | sha256sum -c - mkdir -p "$LLVM_MINGW" tar -xJf "$tmp/t.tar.xz" -C "$LLVM_MINGW" --strip-components=1 rm -rf "$tmp" fi export PATH="$LLVM_MINGW/bin:$PATH" for t in i686 arm64ec aarch64; do command -v "$t-w64-mingw32-clang" >/dev/null || die "llvm-mingw has no $t-w64-mingw32-clang" done for t in i686 aarch64; do command -v "$t-w64-mingw32-windres" >/dev/null || die "llvm-mingw has no $t-w64-mingw32-windres" done say "llvm-mingw $LLVM_MINGW_VER: $(arm64ec-w64-mingw32-clang --version | head -1)" } windres_for() { case "$1" in i386) echo i686-w64-mingw32-windres ;; arm64x) echo aarch64-w64-mingw32-windres ;; *) die "unknown arch $1" ;; esac } # File version of the Wine builtin each DLL replaces (dlls//version.rc). DLL_VERSIONS='xinput1_1:9,12,589,0 xinput1_2:9,14,701,0 xinput1_3:9,15,779,0 xinput1_4:6,3,9600,16384 xinput9_1_0:9,10,455,0 xinputuap:10,0,19041,746' version_res() { # version_res -> path of a COFF object with the DLL's VERSIONINFO local ver="" kv rc="$3/$2.rc" obj="$3/$2-ver-$1.o" for kv in $DLL_VERSIONS; do [ "${kv%%:*}" = "$2" ] && ver="${kv#*:}"; done [ -n "$ver" ] || die "no version for $2 in DLL_VERSIONS" cat >"$rc" < -> path of the .def to link with local src="$HERE/def/$2.def" if [ "$1" != i386 ]; then echo "$src"; return; fi awk -v table="$I386_DECOR" 'BEGIN { n = split(table, a, " "); for (i = 1; i <= n; i++) { split(a[i], kv, ":"); m[kv[1]] = kv[2] } } ($1 in m) { sub($1, $1 "@" m[$1]) } { print }' "$src" > "$3/$2.def" echo "$3/$2.def" } # expected " " lines of a .def (NONAME exports have no name) def_exports() { awk 'f && NF { ord = ""; for (i = 2; i <= NF; i++) if ($i ~ /^@[0-9]+$/) ord = substr($i, 2); name = $1; for (i = 2; i <= NF; i++) if ($i == "NONAME") name = "-"; print (ord == "" ? "?" : ord), name } /^EXPORTS/ { f = 1 }' "$1" | sort } # actual " " lines of a DLL (unused ordinals between the numbered ones have RVA 0 and are # skipped). dll_exports [main|hybrid]: llvm-readobj prints an ARM64X image's native view first, then # its EC view inside "HybridObject {". dll_exports() { llvm-readobj --coff-exports "$1" | awk -v view="${2:-main}" ' /HybridObject \{/ { h = 1; next } (view == "main" && h) || (view == "hybrid" && !h) { next } /^ *Export \{/ { o = ""; n = "-"; r = "" } /^ *Ordinal:/ { o = $2 } /^ *Name:/ { if (NF >= 2) n = $2 } /^ *RVA:/ { r = $2 } /^ *\}/ { if (o != "" && r != "" && r != "0x0") print o, n; o = "" }' | sort } compare_exports() { # compare_exports