diff --git a/ch2/os/Cargo.toml b/ch2/os/Cargo.toml new file mode 100644 index 0000000..60b4d6d --- /dev/null +++ b/ch2/os/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "os" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/ch2/os/src/main.rs b/ch2/os/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/ch2/os/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/ch2/user/Cargo.toml b/ch2/user/Cargo.toml new file mode 100644 index 0000000..5e47670 --- /dev/null +++ b/ch2/user/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "user_lib" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } + +[profile.release] +debug = true diff --git a/ch2/user/Makefile b/ch2/user/Makefile new file mode 100644 index 0000000..3935639 --- /dev/null +++ b/ch2/user/Makefile @@ -0,0 +1,16 @@ +MODE := release +TARGET := riscv64gc-unknown-none-elf + + +RUST_FLAGS := -Clink-arg=-Tsrc/linker.ld # 使用我们自己的链接脚本 +RUST_FLAGS += -Cforce-frame-pointers=yes # 强制编译器生成帧指针 +RUST_FLAGS:=$(strip ${RUST_FLAGS}) + +# 编译elf文件 +build_elf: clean + CARGO_BUILD_RUSTFLAGS="$(RUST_FLAGS)" \ + cargo build --$(MODE) --target=$(TARGET) + + +clean: + rm -rf ./target* \ No newline at end of file diff --git a/ch2/user/src/lib.rs b/ch2/user/src/lib.rs new file mode 100644 index 0000000..04ead1b --- /dev/null +++ b/ch2/user/src/lib.rs @@ -0,0 +1,8 @@ +#![no_std] +#![feature(linkage)] // 开启弱链接特性 + +#[linkage = "weak"] // 设置我们默认的main函数, 弱链接 +#[no_mangle] +fn main() -> i32 { + panic!("Cannot find main!"); +} \ No newline at end of file diff --git a/ch2/user/src/linker.ld b/ch2/user/src/linker.ld new file mode 100644 index 0000000..ae6752b --- /dev/null +++ b/ch2/user/src/linker.ld @@ -0,0 +1,32 @@ +OUTPUT_ARCH(riscv) +ENTRY(_start) + +/*设置用户程序链接的基础起始位置为0x80400000*/ +BASE_ADDRESS = 0x80400000; + +SECTIONS +{ + . = BASE_ADDRESS; + .text : { + *(.text.entry) + *(.text .text.*) + } + .rodata : { + *(.rodata .rodata.*) + *(.srodata .srodata.*) + } + .data : { + *(.data .data.*) + *(.sdata .sdata.*) + } + .bss : { + start_bss = .; + *(.bss .bss.*) + *(.sbss .sbss.*) + end_bss = .; + } + /DISCARD/ : { + *(.eh_frame) + *(.debug*) + } +} \ No newline at end of file