From 4b017882af9a2919ea9777461714e60e51c4321b Mon Sep 17 00:00:00 2001 From: zhangxinyu <840317537@qq.com> Date: Wed, 17 May 2023 14:08:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E6=89=A7=E8=A1=8C=E5=89=8D=E7=8E=AF=E5=A2=83=E7=9A=84?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96(=E5=88=9D=E5=A7=8B=E5=8C=96bss?= =?UTF-8?q?=E6=AE=B5=E4=BB=A5=E5=8F=8A=E9=80=80=E5=87=BA=E9=80=BB=E8=BE=91?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch2/user/src/bin/00hello_world.rs | 3 ++- ch2/user/src/lib.rs | 29 +++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/ch2/user/src/bin/00hello_world.rs b/ch2/user/src/bin/00hello_world.rs index 965827b..6995fb6 100644 --- a/ch2/user/src/bin/00hello_world.rs +++ b/ch2/user/src/bin/00hello_world.rs @@ -4,6 +4,7 @@ use user_lib::*; #[no_mangle] -fn main() { +fn main() -> i32 { println!("123"); + 0 } \ No newline at end of file diff --git a/ch2/user/src/lib.rs b/ch2/user/src/lib.rs index e859d12..23b813a 100644 --- a/ch2/user/src/lib.rs +++ b/ch2/user/src/lib.rs @@ -2,12 +2,37 @@ #![feature(linkage)] // 开启弱链接特性 #![feature(panic_info_message)] -pub mod syscall; + pub mod user_lang_items; pub use user_lang_items::*; +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!"); +} + #[linkage = "weak"] // 设置我们默认的main函数, 弱链接 #[no_mangle] fn main() -> i32 { panic!("Cannot find main!"); -} \ No newline at end of file +} + +fn clear_bss() { + unsafe { + (start_bss as usize..end_bss as usize).for_each(|p| (p as *mut u8).write_unaligned(0)) + }; +}