#!/usr/bin/env python3 # documentation precedes the symbol table; the boot header must skip those bytes. # usage: python3 tests/drivers/genome_blob_test.py [build/lsp] (the documented language server image) import pathlib import re import struct import sys folder = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else 'build/lsp') image = (folder / 'lsp_genome.bin').read_bytes() org = int(re.search(r'#define GORG (?:0x)?([0-9A-Fa-f]+)', (folder / 'memmap.h').read_text())[1], 16) words = list(struct.unpack('<%dq' % (len(image) // 8), image)) length = len(words) assert words[6] == org + length cursor = words[5] - org assert 7 <= cursor < length records = {} while words[cursor]: kind, value, signature = words[cursor:cursor + 3] assert kind in (1, 2, 3, 4, 6, 8), (cursor, kind) cursor += 3 name = '' while words[cursor]: assert 0 < words[cursor] < 128, (cursor, words[cursor]) name += chr(words[cursor]) cursor += 1 cursor += 1 records[name] = (kind, value, signature) assert cursor == length - 1 assert len(records) > 1000 and records['run.start'][0] == 1 assert records['run.reserve_code'][0] == 1 and records['run.build_image'][0] == 1 assert any(name.startswith('doc ') for name in records), 'the fixture contains documentation bytes' for name, (kind, value, _) in records.items(): if kind == 8: assert org <= value < org + length and words[value - org] >= 0, name print('ok documented genome starts its symbol table at the header address')