From a88f3a1690c287cfbb77271a2d4e3eaff185554a Mon Sep 17 00:00:00 2001 From: Toby Jaffey Date: Mon, 8 Dec 2025 10:13:53 +0000 Subject: [PATCH] Workaround for Rust having different system for templating constants to GNU as. Having a separate file is ugly, but it allows for a single source of truth for constants Works for C, asm, zig and rust. https://stackoverflow.com/questions/79840723/shared-assembly-between-rust-and-c-using-preprocessor --- apps/crt0.S | 6 +++++- apps/non-rust-crt0-hack.S | 1 + apps/rust-hello/src/main.rs | 2 +- apps/zig-mandel/build.zig | 2 +- apps/zigtris/build.zig | 2 +- 5 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 apps/non-rust-crt0-hack.S diff --git a/apps/crt0.S b/apps/crt0.S index fc49156..5b8687e 100644 --- a/apps/crt0.S +++ b/apps/crt0.S @@ -6,7 +6,11 @@ _start: # sp is already setup by vm sw ra,12(sp) jal ra, main -li a7, 0x138 #IOREQ_HALT (Rust assembler doesn't pick up def...) +#if 1 +#include "non-rust-crt0-hack.s" +#else +li a7, {IOREQ_HALT} +#endif ecall .section .data diff --git a/apps/non-rust-crt0-hack.S b/apps/non-rust-crt0-hack.S new file mode 100644 index 0000000..6616332 --- /dev/null +++ b/apps/non-rust-crt0-hack.S @@ -0,0 +1 @@ +li a7,IOREQ_HALT diff --git a/apps/rust-hello/src/main.rs b/apps/rust-hello/src/main.rs index ac7a866..92eb70e 100644 --- a/apps/rust-hello/src/main.rs +++ b/apps/rust-hello/src/main.rs @@ -9,7 +9,7 @@ use core::panic::PanicInfo; include!(concat!(env!("OUT_DIR"), "/bindings.rs")); // startup code -global_asm!(include_str!("../../crt0.S")/*, IOREQ_HALT = const IOREQ_HALT*/); +global_asm!(include_str!("../../crt0.S"), IOREQ_HALT = const IOREQ_HALT); fn syscall(id: u32, n: u32) -> u32 { let mut value; diff --git a/apps/zig-mandel/build.zig b/apps/zig-mandel/build.zig index 0d1fbd9..a16e00b 100644 --- a/apps/zig-mandel/build.zig +++ b/apps/zig-mandel/build.zig @@ -37,7 +37,7 @@ pub fn build(b: *std.Build) void { b.installArtifact(exe); - exe.addAssemblyFile(b.path("../crt0.s")); + exe.addAssemblyFile(b.path("../crt0.S")); exe.setLinkerScript(b.path("../linker.ld")); exe.addIncludePath(b.path("../../common")); diff --git a/apps/zigtris/build.zig b/apps/zigtris/build.zig index 2d70ddc..72709ac 100644 --- a/apps/zigtris/build.zig +++ b/apps/zigtris/build.zig @@ -52,7 +52,7 @@ pub fn build(b: *std.Build) void { b.installArtifact(exe); - exe.addAssemblyFile(b.path("../crt0.s")); + exe.addAssemblyFile(b.path("../crt0.S")); exe.setLinkerScript(b.path("../linker.ld")); exe.addIncludePath(b.path("../../common"));