Skip to content

Main 函数

main 函数细节

Rust Before Main

rustc 是编译器前端,LLVM 是编译器后端

在 Cargo.toml 修改配置

[profile.release]
strip = true # 去除 debug 信息等内容
opt-level = "z" # 优化大小而非速度
lto = true # link time optimization
panic = "abort" # 取消报错信息

不使用 std 库与 main 函数

#![no_std]
#![no_main]

extern crate libc;

#[no_mangle]
pub extern "C" fn main() -> isize {
    0
}

#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

直接调用 __start cargo rustc --release -- -C link-arg=-nostartfiles

main 函数调用顺序 execve (sys call) -> dynamic loader -> __start -> __lib_start_main -> main

dynamic loader -> map memory, load deps, relocations -> jump to __start