Skip to content

Commit 20e0f51

Browse files
resolve comments
1 parent 44a3c6b commit 20e0f51

File tree

3 files changed

+26
-46
lines changed

3 files changed

+26
-46
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LINDFS_DIRS := \
1919
var/run
2020

2121
.PHONY: build
22-
build: sysroot lind-boot lindfs
22+
build: generate-syscall-mappings sysroot lind-boot lindfs
2323
@echo "Build complete"
2424

2525
.PHONY: all
@@ -66,6 +66,10 @@ build_glibc:
6666
build-dir:
6767
mkdir -p $(BUILD_DIR)
6868

69+
.PHONY: generate-syscall-mappings
70+
generate-syscall-mappings: build-dir
71+
python3 scripts/generate_syscall_mappings.py
72+
6973
.PHONY: sync-sysroot
7074
sync-sysroot:
7175
$(RM) -r $(SYSROOT_DIR)

docs/contribute/toolchain.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,19 @@ options.
3535
Matching builtins are available in the *lind-wasm* repo under
3636
[`src/glibc/wasi`](https://github.com/Lind-Project/lind-wasm/tree/main/src/glibc/wasi).
3737

38-
3. __Build *glibc* and generate sysroot__ (see [`make sysroot`](https://github.com/Lind-Project/lind-wasm/blob/main/Makefile))
38+
3. __Generate syscall mappings__ (see [`make generate-syscall-mappings`](https://github.com/Lind-Project/lind-wasm/blob/main/Makefile))
39+
40+
Auto-generates syscall mapping constants from `src/glibc/lind_syscall/lind_syscall_num.h`
41+
and produces `src/sysdefs/src/constants/syscall_const.rs`. This must run before building *glibc*
42+
and *wasmtime* to ensure Rust code has the correct syscall definitions.
43+
44+
```bash
45+
python3 scripts/generate_syscall_mappings.py
46+
```
47+
48+
The source of truth is the Linux x86_64 syscall table as defined in the *glibc* header file.
49+
50+
4. __Build *glibc* and generate sysroot__ (see [`make sysroot`](https://github.com/Lind-Project/lind-wasm/blob/main/Makefile))
3951
1. Configure and compile *glibc* for the *WASI* target with *Clang*
4052

4153
2. Compile extra files:
@@ -51,7 +63,7 @@ options.
5163
along with headers and a pre-built C runtime into a
5264
sysroot directory structure as required by *Clang*.
5365

54-
4. __Build custom wasmtime__ (see [`make wasmtime`](https://github.com/Lind-Project/lind-wasm/blob/main/Makefile))
66+
5. __Build custom wasmtime__ (see [`make wasmtime`](https://github.com/Lind-Project/lind-wasm/blob/main/Makefile))
5567

5668
Builds `src/wasmtime` workspace. Custom dependencies `fdtables`, `RawPOSIX`
5769
and `sysdefs` are included in the build automatically.

scripts/generate_syscall_mappings.py

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
# Auto-generates syscall mapping constants for lind-wasm.
33
#
44
# Reads syscall definitions from glibc's lind_syscall_num.h and generates:
5-
# - src/sysdefs/src/constants/syscall_const.rs (full set)
6-
# - src/wasmtime/crates/lind-utils/src/lind_syscall_numbers.rs (minimal set)
5+
# - src/sysdefs/src/constants/syscall_const.rs (full set of syscalls)
6+
#
7+
# This script should be run before building lind-boot and glibc.
78
#
89
# Source of truth: src/glibc/lind_syscall/lind_syscall_num.h
910

@@ -62,37 +63,7 @@ def generate_rust_constants(syscalls: dict[str, int]) -> str:
6263
return "\n".join(lines)
6364

6465

65-
def get_minimal_syscalls() -> dict[str, int]:
66-
"""Return the minimal set of syscalls needed by Wasmtime runtime."""
67-
return {
68-
'MMAP_SYSCALL': 9,
69-
'CLONE_SYSCALL': 56,
70-
'FORK_SYSCALL': 57,
71-
'EXEC_SYSCALL': 59,
72-
'EXIT_SYSCALL': 60,
73-
'EXIT_GROUP_SYSCALL': 231,
74-
}
75-
76-
77-
def generate_wasmtime_minimal(minimal: dict[str, int]) -> str:
78-
"""Generate minimal Rust constants for wasmtime lind_syscall_numbers.rs."""
79-
lines = [
80-
"// Minimal set of syscall numbers used by Wasmtime side for Lind",
81-
"// Keeps the runtime minimal and the rawposix dispatcher handles the rest",
82-
"// Source of truth: Linux x86_64 syscall table",
83-
"// https://github.com/torvalds/linux/blob/v6.16-rc1/arch/x86/entry/syscalls/syscall_64.tbl",
84-
"// (Historical overview: https://filippo.io/linux-syscall-table/)",
85-
"// Keep these in sync with glibc's lind_syscall_num.h and RawPOSIX dispatcher",
86-
]
87-
88-
# Sort by syscall number
89-
sorted_syscalls = sorted(minimal.items(), key=lambda x: x[1])
90-
91-
for name, number in sorted_syscalls:
92-
lines.append(f"pub const {name}: i32 = {number};")
9366

94-
lines.append("")
95-
return "\n".join(lines)
9667

9768

9869
def write_file(path: str, content: str) -> None:
@@ -104,31 +75,24 @@ def write_file(path: str, content: str) -> None:
10475

10576

10677
def main() -> None:
107-
"""Generate all syscall mapping files."""
78+
"""Generate syscall mapping constants from glibc header."""
10879
# Find workspace root (should be run from lind-wasm directory)
10980
workspace_root = Path(__file__).parent.parent
11081

11182
c_header = workspace_root / "src/glibc/lind_syscall/lind_syscall_num.h"
11283
rust_sysdefs_out = workspace_root / "src/sysdefs/src/constants/syscall_const.rs"
113-
rust_wasmtime_out = workspace_root / "src/wasmtime/crates/lind-utils/src/lind_syscall_numbers.rs"
11484

11585
print("Parsing syscall definitions...")
11686
syscalls = parse_c_header(str(c_header))
11787
print(f"Found {len(syscalls)} syscall definitions")
11888

119-
# Generate full Rust constants for sysdefs
89+
# Generate Rust constants for sysdefs
12090
print("\nGenerating sysdefs constants...")
12191
rust_content = generate_rust_constants(syscalls)
12292
write_file(str(rust_sysdefs_out), rust_content)
12393

124-
# Generate minimal constants for wasmtime
125-
print("Generating wasmtime minimal constants...")
126-
minimal = get_minimal_syscalls()
127-
wasmtime_content = generate_wasmtime_minimal(minimal)
128-
write_file(str(rust_wasmtime_out), wasmtime_content)
129-
130-
print("\nDone!")
94+
print("Done!")
13195

13296

13397
if __name__ == "__main__":
134-
main()
98+
main()

0 commit comments

Comments
 (0)