-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgenerate_syscall_mappings.py
More file actions
98 lines (71 loc) · 2.97 KB
/
generate_syscall_mappings.py
File metadata and controls
98 lines (71 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
# Auto-generates syscall mapping constants for lind-wasm.
#
# Reads syscall definitions from glibc's lind_syscall_num.h and generates:
# - src/sysdefs/src/constants/syscall_const.rs (full set of syscalls)
#
# This script should be run before building lind-boot and glibc.
#
# Source of truth: src/glibc/lind_syscall/lind_syscall_num.h
from __future__ import annotations
import re
import sys
from pathlib import Path
def parse_c_header(header_path: str) -> dict[str, int]:
"""Parse syscall constants from glibc's lind_syscall_num.h header file."""
syscalls = {}
try:
with open(header_path) as f:
content = f.read()
except FileNotFoundError:
print(f"Error: Could not find {header_path}", file=sys.stderr)
sys.exit(1)
# Match #define SYSCALL_NAME number
pattern = r'#define\s+(\w+)\s+(\d+)'
for match in re.finditer(pattern, content):
name, number = match.groups()
syscalls[name] = int(number)
if not syscalls:
print(f"Error: Could not parse any syscalls from {header_path}", file=sys.stderr)
sys.exit(1)
return syscalls
def generate_rust_constants(syscalls: dict[str, int]) -> str:
"""Generate Rust constants for sysdefs/constants/syscall_const.rs."""
lines = [
"//! Syscall number constants for the Lind platform.",
"//!",
"//! Source of truth: Linux x86_64 syscall table",
"//! https://github.com/torvalds/linux/blob/v6.16-rc1/arch/x86/entry/syscalls/syscall_64.tbl",
"//! (Historical overview: https://filippo.io/linux-syscall-table/)",
"//!",
"//! Keep these in sync with glibc's lind_syscall_num.h and RawPOSIX dispatcher.",
"",
]
# Sort by syscall number for readability
sorted_syscalls = sorted(syscalls.items(), key=lambda x: x[1])
for name, number in sorted_syscalls:
lines.append(f"pub const {name}: i32 = {number};")
lines.append("")
return "\n".join(lines)
def write_file(path: str, content: str) -> None:
"""Write content to file, creating directories if needed."""
Path(path).parent.mkdir(parents=True, exist_ok=True)
with open(path, 'w') as f:
f.write(content)
print(f"Generated: {path}")
def main() -> None:
"""Generate syscall mapping constants from glibc header."""
# Find workspace root (should be run from lind-wasm directory)
workspace_root = Path(__file__).parent.parent
c_header = workspace_root / "src/glibc/lind_syscall/lind_syscall_num.h"
rust_sysdefs_out = workspace_root / "src/sysdefs/src/constants/syscall_const.rs"
print("Parsing syscall definitions...")
syscalls = parse_c_header(str(c_header))
print(f"Found {len(syscalls)} syscall definitions")
# Generate Rust constants for sysdefs
print("\nGenerating sysdefs constants...")
rust_content = generate_rust_constants(syscalls)
write_file(str(rust_sysdefs_out), rust_content)
print("Done!")
if __name__ == "__main__":
main()