Tools: preserve duplicate XDAT payloads in fssm_unpack

This commit is contained in:
Kam471
2026-11-21 19:48:29 +00:00
committed by Kam471
parent 4c75ce537a
commit 65d62ffdc8
2 changed files with 95 additions and 13 deletions

View File

@ -62,6 +62,21 @@ def make_xdat(entries, pre_padding=b""):
return pre_padding + xdat_header + bytes(payloads)
def make_xdat_list(entries, pre_padding=b""):
"""Like make_xdat, but entries is an ordered list so duplicate tags can be represented."""
count = len(entries)
table_len = 12 + 12 * count
dxil_base_offset = len(pre_padding) + table_len
table_entries = []
payloads = bytearray()
for tag, payload in entries:
off = dxil_base_offset + len(payloads)
table_entries.append(struct.pack("<III", tag, off, len(payload)))
payloads.extend(payload)
xdat_header = b"XDAT" + struct.pack("<II", count, 8) + b"".join(table_entries)
return pre_padding + xdat_header + bytes(payloads)
def make_dxbc(chunks, digest=bytes(16), version=1):
count = len(chunks)
head_len = 32 + 4 * count
@ -140,6 +155,43 @@ class TestXdatEntries(unittest.TestCase):
self.assertEqual(parsed, entries)
class TestDuplicateXdatPayloads(unittest.TestCase):
def test_duplicate_tag_keeps_order_and_selects_first_or_last(self):
first = make_fssm(version=0x0101, instructions=[(0x100, S_ENDPGM)])
last = make_fssm(version=0x0102, instructions=[(0x100, S_ENDPGM)])
xdat = make_xdat_list([
(0x12, b"\x11" * 16),
(0x0D, make_xbch(first)),
(0x0D, make_xbch(last)),
])
entries = fssm_unpack.xdat_entries_all(xdat)
self.assertEqual([tag for tag, _ in entries], [0x12, 0x0D, 0x0D])
self.assertEqual(entries[1][1], make_xbch(first))
self.assertEqual(entries[2][1], make_xbch(last))
self.assertEqual(fssm_unpack.xdat_entries(xdat)[0x0D], make_xbch(last))
dxbc = make_dxbc([(b"DXIL", xdat)])
data, _ = fssm_unpack.unpack(dxbc)
self.assertEqual(data, last)
data, _ = fssm_unpack.unpack(dxbc, payload="first")
self.assertEqual(data, first)
with self.assertRaises(ValueError):
fssm_unpack.unpack(dxbc, payload="middle")
def test_cli_payload_first(self):
first = make_fssm(version=0x0101, instructions=[(0x100, S_ENDPGM)])
last = make_fssm(version=0x0102, instructions=[(0x100, S_ENDPGM)])
xdat = make_xdat_list([(0x0D, make_xbch(first)), (0x0D, make_xbch(last))])
dxbc = make_dxbc([(b"DXIL", xdat)])
with tempfile.TemporaryDirectory() as tmp:
with open(os.path.join(tmp, "dup.dxbc"), "wb") as f:
f.write(dxbc)
p = subprocess.run([sys.executable, "-I", TOOL, tmp, "--payload", "first"],
capture_output=True, text=True)
self.assertEqual(p.returncode, 0, p.stderr)
with open(os.path.join(tmp, "fssm", "dup.fssm"), "rb") as f:
self.assertEqual(f.read(), first)
class TestXbchContainerCarvingAndUnpack(unittest.TestCase):
def test_unpack_valid_container(self):
fssm_payload = make_fssm(version=0x0101, instructions=[(0x100, S_ENDPGM)])

View File

@ -9,14 +9,22 @@ Layout found in GTA VI's Xbox shaders (shader model 6.6 containers):
tag 0x12 16-byte hash
tag 0x0d 'XBCH' u32 ?, u32 stored, u32 0, u32 unpacked, u32 0, then zlib -> 'FSSM' blob
tag 0x07 DxilProgramHeader with 'NOBC' (no LLVM bitcode is shipped)
Some containers carry **two distinct tag-0x0D payloads** (195 of 26,503 measured). Both decompress to
valid FSSM blobs with the same code offset but different code sizes; the first is 64 or 128 bytes
longer. `xdat_entries_all` preserves their order, `xdat_entries` keeps the previous last-wins mapping
for compatibility, and `unpack()` defaults to the last payload (the one the existing corpus used) with
`payload="first"` available for the XDAT-order/SCARAB choice. The CLI exposes `--payload`.
PRIV part = Xbox private metadata
The FSSM blob is the precompiled RDNA2 program plus its metadata. It contains the s_endpgm (0xBF810000) that ends
the code.
Usage:
python -I fssm_unpack.py <carved_shader_dir> (writes <dir>/fssm/<name>.fssm and fssm_index.json)
python -I fssm_unpack.py <carved_shader_dir> [--payload first|last]
(writes <dir>/fssm/<name>.fssm and fssm_index.json)
"""
import argparse
import json
import os
import struct
@ -33,22 +41,38 @@ def parts(d: bytes) -> dict:
return out
def xdat_entries(dxil: bytes) -> dict:
def xdat_entries_all(dxil: bytes) -> list:
"""All XDAT entries in directory order, as (tag, payload) pairs."""
x = dxil.find(b"XDAT")
if x < 0:
return {}
return []
count, hdr = struct.unpack_from("<II", dxil, x + 4)
ents = {}
out = []
for k in range(count):
tag, off, size = struct.unpack_from("<III", dxil, x + 12 + 12 * k)
ents[tag] = dxil[off:off + size]
return ents
out.append((tag, dxil[off:off + size]))
return out
def unpack(dxbc: bytes):
def xdat_entries(dxil: bytes) -> dict:
"""Last payload per tag, kept for callers that want the old single-value view."""
return {tag: payload for tag, payload in xdat_entries_all(dxil)}
def unpack(dxbc: bytes, payload: str = "last"):
p = parts(dxbc)
ents = xdat_entries(p.get(b"DXIL", b""))
blob = ents.get(0x0D)
dxil = p.get(b"DXIL", b"")
all_entries = xdat_entries_all(dxil)
ents = {tag: value for tag, value in all_entries}
blobs = [value for tag, value in all_entries if tag == 0x0D]
if not blobs:
return None, ents
if payload == "first":
blob = blobs[0]
elif payload == "last":
blob = blobs[-1]
else:
raise ValueError(f"payload must be 'first' or 'last', not {payload!r}")
if not blob or blob[:4] != b"XBCH":
return None, ents
unpacked = struct.unpack_from("<I", blob, 16)[0]
@ -58,8 +82,13 @@ def unpack(dxbc: bytes):
return data, ents
def main() -> int:
src = sys.argv[1]
def main(argv=None) -> int:
ap = argparse.ArgumentParser(description="Unpack the FSSM program from each carved shader container.")
ap.add_argument("src", help="carved shader directory")
ap.add_argument("--payload", choices=("first", "last"), default="last",
help="tag-0x0D payload when a container has more than one (default: last)")
a = ap.parse_args(argv)
src = a.src
out = os.path.join(src, "fssm")
os.makedirs(out, exist_ok=True)
rows, problems = [], Counter()
@ -69,7 +98,7 @@ def main() -> int:
with open(os.path.join(src, name), "rb") as f:
d = f.read()
try:
fssm, ents = unpack(d)
fssm, ents = unpack(d, payload=a.payload)
except Exception as e: # noqa: BLE001
problems[type(e).__name__] += 1
continue
@ -80,9 +109,10 @@ def main() -> int:
with open(os.path.join(out, stem + ".fssm"), "wb") as f:
f.write(fssm)
ends = [i for i in range(0, len(fssm) - 3, 4) if fssm[i:i + 4] == b"\x00\x00\x81\xbf"]
payloads = sum(1 for tag, _ in xdat_entries_all(parts(d).get(b"DXIL", b"")) if tag == 0x0D)
rows.append({"name": stem, "size": len(fssm), "magic": fssm[:4].decode("latin-1"),
"version": struct.unpack_from("<H", fssm, 4)[0], "endpgm": ends,
"tags": sorted(hex(t) for t in ents)})
"tags": sorted(hex(t) for t in ents), "xbch_payloads": payloads})
with open(os.path.join(src, "fssm_index.json"), "w", encoding="utf-8") as f:
json.dump(rows, f)
print(f"{len(rows)} FSSM blobs, problems: {dict(problems)}")