1
0
forked from Rust/Core

Avoid gcc dependency

This commit is contained in:
Kitsu 2021-12-09 16:25:28 +03:00
parent 345624c745
commit 881e7b2ad3
9 changed files with 113 additions and 75 deletions

View File

@ -14,4 +14,4 @@ lto = "thin"
cstr_core = { version = "0.2.4", default-features = false, features = ["nightly"] } cstr_core = { version = "0.2.4", default-features = false, features = ["nightly"] }
[build-dependencies] [build-dependencies]
cc = "1.0.72" nasm-rs = "0.2.2"

26
Makefile.toml Normal file
View File

@ -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"] }

7
README.md Normal file
View File

@ -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`.

View File

@ -1,8 +1,5 @@
fn main() { fn main() {
println!("cargo:rerun-if-changed=src/syscalls.c"); println!("cargo:rerun-if-changed=src/syscalls.S");
cc::Build::new() nasm_rs::compile_library_args("libsyscalls.a", &["src/syscalls.S"], &["-f elf32"])
.file("src/syscalls.c") .expect("failed to compile assembly");
.flag("-fno-PIC") // for some reason `pic(false)` doesn't work
.static_flag(true)
.compile("syscalls");
} }

View File

@ -7,18 +7,9 @@
"has-elf-tls": false, "has-elf-tls": false,
"has-rpath": true, "has-rpath": true,
"is-builtin": false, "is-builtin": false,
"linker-flavor": "gcc", "linker-flavor": "ld.lld",
"linker": "gcc", "linker": "rust-lld",
"llvm-target": "i686-unknown-none-code32", "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, "max-atomic-width": 32,
"os": "none", "os": "none",
"relocation-model": "static", "relocation-model": "static",

2
link.x
View File

@ -2,8 +2,6 @@ PATH_SIZE = 1024;
PARAMS_SIZE = 256; PARAMS_SIZE = 256;
STACK_SIZE = 1024; STACK_SIZE = 1024;
OUTPUT_FORMAT("binary")
SECTIONS SECTIONS
{ {
. = 0x24; . = 0x24;

View File

@ -1,16 +1,22 @@
#[link(name = "syscalls")] #[link(name = "syscalls")]
extern "C" { extern "C" {
#[link_name = "_start_window_draw"]
pub fn start_window_draw(); pub fn start_window_draw();
#[link_name = "_end_window_draw"]
pub fn end_window_draw(); pub fn end_window_draw();
#[link_name = "exit0"] #[link_name = "_exit"]
pub fn exit() -> !; pub fn exit() -> !;
#[link_name = "_define_window"]
pub fn define_window(ebx: u32, ecx: u32, edx: u32, edi: u32); 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); pub fn display_message(ebx: u32, ecx: u32, edx: u32, edi: u32, esi: u32);
#[link_name = "_wait_event"]
pub fn wait_event() -> u32; pub fn wait_event() -> u32;
#[link_name = "_pressed_key"]
pub fn pressed_key() -> u32; pub fn pressed_key() -> u32;
} }

67
src/syscalls.S Normal file
View File

@ -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

View File

@ -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;
}