From 881e7b2ad32b446bec2a03a74908ed48463e9027 Mon Sep 17 00:00:00 2001 From: Kitsu Date: Thu, 9 Dec 2021 16:25:28 +0300 Subject: [PATCH] Avoid gcc dependency --- Cargo.toml | 2 +- Makefile.toml | 26 ++++++++++++++++++ README.md | 7 +++++ build.rs | 9 +++---- i686-kolibri.json | 13 ++------- link.x | 2 -- src/sys.rs | 8 +++++- src/syscalls.S | 67 +++++++++++++++++++++++++++++++++++++++++++++++ src/syscalls.c | 54 -------------------------------------- 9 files changed, 113 insertions(+), 75 deletions(-) create mode 100644 Makefile.toml create mode 100644 README.md create mode 100644 src/syscalls.S delete mode 100644 src/syscalls.c diff --git a/Cargo.toml b/Cargo.toml index ef353ff..25a8053 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,4 @@ lto = "thin" cstr_core = { version = "0.2.4", default-features = false, features = ["nightly"] } [build-dependencies] -cc = "1.0.72" +nasm-rs = "0.2.2" diff --git a/Makefile.toml b/Makefile.toml new file mode 100644 index 0000000..06c4b9e --- /dev/null +++ b/Makefile.toml @@ -0,0 +1,26 @@ +[env.production] +RELEASE_FLAG = "--release" + +[tasks.default] +alias = "all" + +[tasks.all] +dependencies = ["objcopy"] + +[tasks.clean] +command = "cargo" +args = ["clean"] + +[tasks.build-1] +command = "cargo" +args = ["build", "@@remove-empty(RELEASE_FLAG)"] + +[tasks.build] +command = "cargo" +args = ["build"] + +[tasks.objcopy] +command = "cargo" +args = ["objcopy", "@@remove-empty(RELEASE_FLAG)", "--", "-Obinary"] +dependencies = ["build-1"] +install_crate = { crate_name = "cargo-binutils", binary = "rust-objcopy", test_arg = ["--help"] } diff --git a/README.md b/README.md new file mode 100644 index 0000000..67c6b4e --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Hello-world example for KolibriOS + +Project uses [cargo-make](https://github.com/sagiegurari/cargo-make) for building steps. +Also you need a working [NASM](https://nasm.us/). + +Once installed building is trivial then: `cargo make --profile production` produces +a ready-to-use binary at `target/i686-kolibri/release/hw_kolibri`. diff --git a/build.rs b/build.rs index 3d1264e..9b64d49 100644 --- a/build.rs +++ b/build.rs @@ -1,8 +1,5 @@ fn main() { - println!("cargo:rerun-if-changed=src/syscalls.c"); - cc::Build::new() - .file("src/syscalls.c") - .flag("-fno-PIC") // for some reason `pic(false)` doesn't work - .static_flag(true) - .compile("syscalls"); + println!("cargo:rerun-if-changed=src/syscalls.S"); + nasm_rs::compile_library_args("libsyscalls.a", &["src/syscalls.S"], &["-f elf32"]) + .expect("failed to compile assembly"); } diff --git a/i686-kolibri.json b/i686-kolibri.json index 1b76fbd..99c03c9 100644 --- a/i686-kolibri.json +++ b/i686-kolibri.json @@ -7,18 +7,9 @@ "has-elf-tls": false, "has-rpath": true, "is-builtin": false, - "linker-flavor": "gcc", - "linker": "gcc", + "linker-flavor": "ld.lld", + "linker": "rust-lld", "llvm-target": "i686-unknown-none-code32", - "pre-link-args": { - "gcc": [ - "-nostdlib", - "-Wl,--build-id=none", - "-Wl,--as-needed", - "-Wl,-z,noexecstack", - "-m32" - ] - }, "max-atomic-width": 32, "os": "none", "relocation-model": "static", diff --git a/link.x b/link.x index b60ee55..75b506f 100644 --- a/link.x +++ b/link.x @@ -2,8 +2,6 @@ PATH_SIZE = 1024; PARAMS_SIZE = 256; STACK_SIZE = 1024; -OUTPUT_FORMAT("binary") - SECTIONS { . = 0x24; diff --git a/src/sys.rs b/src/sys.rs index 045df8f..3347f46 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -1,16 +1,22 @@ #[link(name = "syscalls")] extern "C" { + #[link_name = "_start_window_draw"] pub fn start_window_draw(); + #[link_name = "_end_window_draw"] pub fn end_window_draw(); - #[link_name = "exit0"] + #[link_name = "_exit"] pub fn exit() -> !; + #[link_name = "_define_window"] pub fn define_window(ebx: u32, ecx: u32, edx: u32, edi: u32); + #[link_name = "_display_message"] pub fn display_message(ebx: u32, ecx: u32, edx: u32, edi: u32, esi: u32); + #[link_name = "_wait_event"] pub fn wait_event() -> u32; + #[link_name = "_pressed_key"] pub fn pressed_key() -> u32; } diff --git a/src/syscalls.S b/src/syscalls.S new file mode 100644 index 0000000..cf8ae8a --- /dev/null +++ b/src/syscalls.S @@ -0,0 +1,67 @@ +BITS 32 + +section .text + global _exit + global _start_window_draw + global _end_window_draw + global _define_window + global _display_message + global _wait_event + global _pressed_key + +_exit: + mov eax, -1 + int 0x40 + +_start_window_draw: + mov eax, 0xc + mov ebx, 1 + int 0x40 + ret + +_end_window_draw: + mov eax, 0xc + mov ebx, 2 + int 0x40 + ret + +_define_window: + push edi + push ebx + xor eax, eax + mov ebx, dword [esp + 0x14] + mov ecx, dword [esp + 0x18] + mov edx, dword [esp + 0x1c] + mov edi, dword [esp + 0x20] + mov ebx, 2 + int 0x40 + pop ebx + pop edi + ret + +_display_message: + push edi + push esi + push ebx + mov eax, 4 + mov ebx, dword [esp + 0x14] + mov ecx, dword [esp + 0x18] + mov edx, dword [esp + 0x1c] + mov esi, dword [esp + 0x20] + mov edi, dword [esp + 0x24] + mov ebx, 2 + int 0x40 + pop ebx + pop esi + pop edi + ret + +_wait_event: + mov eax, 0xa + int 0x40 + ret + +_pressed_key: + mov eax, 2 + int 0x40 + ret diff --git a/src/syscalls.c b/src/syscalls.c deleted file mode 100644 index 81a6f86..0000000 --- a/src/syscalls.c +++ /dev/null @@ -1,54 +0,0 @@ -void start_window_draw() { - __asm__ volatile ( - "int $0x40;" - :: "a"(12), "b"(1) - ); -} - -void end_window_draw() { - __asm__ volatile ( - "int $0x40;" - :: "a"(12), "b"(2) - ); -} - -void exit0() { - __asm__ volatile ( - "int $0x40;" - :: "a"(-1) - ); -} - -void define_window(unsigned ebx, unsigned ecx, unsigned edx, unsigned edi) { - __asm__ volatile ( - "int $0x40;" - :: "a"(0), "b"(ebx), "c"(ecx), "d"(edx), "D"(edi) - : "memory" - ); -} - -void display_message(unsigned ebx, unsigned ecx, unsigned edx, unsigned edi, unsigned esi) { - __asm__ volatile ( - "int $0x40;" - :: "a"(4), "b"(ebx), "c"(ecx), "d"(edx), "D"(edi), "S"(esi) - : "memory" - ); -} - -unsigned wait_event() { - unsigned res; - __asm__ volatile ( - "int $0x40;" - : "=r"(res) : "a"(10) - ); - return res; -} - -unsigned pressed_key() { - unsigned res; - __asm__ volatile ( - "int $0x40;" - : "=r"(res) : "a"(2) - ); - return res; -}