Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/ffi-rust #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ OBJS_BOOTPACK = \
$(OUT_DIR)/kernel/obj/tek.o \
$(OUT_DIR)/lib/libstdio.a \
$(OUT_DIR)/lib/libstring.a \
$(OUT_DIR)/lib/libstdlib.a
$(OUT_DIR)/lib/libstdlib.a \
$(OUT_DIR)/kernel/rust/rustlib.a # FIXME: いい感じにリンクできない

PREMAKE = \
mkdir -p $(OUT_DIR)/kernel/obj &&\
Expand All @@ -32,11 +33,15 @@ MAKE = make -r
DEL = rm -f

CC = i386-elf-gcc
# FIXME: いい感じにリンクできない
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$(OUT_DIR)/kernel/rust/rustlib.aをいい感じに混ぜたい

# CFLAGS = -m32 -fno-builtin -L$(OUT_DIR)/kernel/rust/rustlib.a
CFLAGS = -m32 -fno-builtin
# COPTION = -march=i486 -nostdlib -L $(OUT_DIR)/kernel/rust/rustlib.a
COPTION = -march=i486 -nostdlib

default :
$(PREMAKE)
$(MAKE) -C rust
$(MAKE) $(OUT_DIR)/kernel/ipl10.bin
$(MAKE) $(OUT_DIR)/kernel/vaughan.sys

Expand Down
2 changes: 2 additions & 0 deletions src/kernel/boot.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "boot.h"

void Boot(void) {
int rust_int = rust_function();

struct BootInfo *binfo = (struct BootInfo *)ADR_BOOT_INFO;

/* event queue */
Expand Down
1 change: 1 addition & 0 deletions src/kernel/boot.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../include/stdio.h"
#include "../include/string.h"
#include "rust.h"

/*
*
Expand Down
1 change: 1 addition & 0 deletions src/kernel/rust.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int rust_function(void);
8 changes: 8 additions & 0 deletions src/kernel/rust/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[build]
target = "dos.json"

[unstable]
build-std = ["core"]

[target.dos]
linker = "x86_64-elf-gcc"
7 changes: 7 additions & 0 deletions src/kernel/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/kernel/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[profile.dev]
opt-level = 1

[profile.release]
opt-level = "s" #or "z"
12 changes: 12 additions & 0 deletions src/kernel/rust/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
MAKE = make -r
OUT_DIR = ../../../target

default :
mkdir -p $(OUT_DIR)/kernel/rust
$(MAKE) $(OUT_DIR)/kernel/rust/rustlib.a

$(OUT_DIR)/kernel/rust/rustlib.a: target/dos/release/librust.rlib
i386-elf-ar rcs $@ target/dos/release/librust.rlib

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1年越しですが、koba789さんの配信アーカイブ見て来ました

エラーメッセージにある通り、ranlibを実行しないといけないのでは?
この行の後に ranlib $@i386-elf-ranlib $@ かも?)をすると良いかと思います


target/dos/release/librust.rlib:
cargo build --release --target=dos.json
36 changes: 36 additions & 0 deletions src/kernel/rust/dos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"arch": "x86",
"cpu": "i386",
"data-layout": "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128",
"disable-redzone": true,
"dynamic-linking": false,
"exe-suffix": ".com",
"executables": false,
"features": "-mmx,-sse,+soft-float",
"linker-flavor": "gcc",
"linker-is-gnu": true,
"llvm-target": "i386-unknown-none-code32",
"max-atomic-width": 32,
"os": "none",
"panic-strategy": "abort",
"position-independent-executables": false,
"pre-link-args": {
"gcc": [
"-Wl,--as-needed",
"-Wl,-z,noexecstack",
"-Wl,--gc-sections",
"-Wl,-melf_i386",
"-m32",
"-nostdlib",
"-march=i386",
"-ffreestanding",
"-fno-pie"
]
},
"relocation-model": "static",
"relro-level": "off",
"target-c-int-width": "32",
"target-endian": "little",
"target-pointer-width": "32",
"vendor": "unknown"
}
6 changes: 6 additions & 0 deletions src/kernel/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![no_std]

#[no_mangle]
pub extern "C" fn rust_function() -> i32 {
42
}
2 changes: 1 addition & 1 deletion src/kernel/tek.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../include/setjmp.h"
#include "boot.h"
// #include <string.h>
#define NULL 0
// #define NULL 0

typedef unsigned char UCHAR;
typedef unsigned int UINT32;
Expand Down