You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rCore_stu/ch2/user/src/lib.rs

39 lines
1009 B
Rust

2 years ago
#![no_std]
#![feature(linkage)] // 开启弱链接特性
#![feature(panic_info_message)]
pub mod user_lang_items;
pub use user_lang_items::*;
2 years ago
pub mod syscall;
use syscall::{sys_exit};
extern "C" {
fn start_bss();
fn end_bss();
}
// 只要使用这个包, 那么这里就会作为bin程序的开始
#[no_mangle]
#[link_section = ".text.entry"] // 链接到指定的段
pub extern "C" fn _start() -> ! {
clear_bss();
sys_exit(main()); // 这里执行的main程序是我们 bin文件夹里面的main, 而不是下面的, 下面的main程序只有在bin程序没有main函数的时候才会链接
// 正常是不会走到这一步的, 因为上面已经退出了程序
panic!("unreachable after sys_exit!");
}
2 years ago
#[linkage = "weak"] // 设置我们默认的main函数, 弱链接
#[no_mangle]
fn main() -> i32 {
panic!("Cannot find main!");
}
fn clear_bss() {
unsafe {
(start_bss as usize..end_bss as usize).for_each(|p| (p as *mut u8).write_unaligned(0))
};
}