gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

Implement main shim

Open bjorn3 opened this issue 4 years ago • 3 comments

Libstd does some extra work in addition to what the libc startup does like setting up a stack guard, adding a handler for SIGSEGV to give a nice error message on stack overflows, storing the program arguments in a static and running the main function within std::panic::catch_unwind to ensure that panicking doesn't immediately abort due to a missing unwind catcher. For reference the main shim calls https://github.com/rust-lang/rust/blob/a2cd91ceb0f156cb442d75e12dc77c3d064cdde4/library/std/src/rt.rs#L60 with the main function as first argument when using libstd. There are also a few other (unstable) options like marking a function with #[start], in which case argc and argv are directly passed to that function without any additional arguments: https://github.com/bjorn3/rustc_codegen_cranelift/blob/3ea8915d4a247b5b3c4cfb3424c230ccd2645b17/example/alloc_example.rs#L30

Originally posted by @bjorn3 in https://github.com/Rust-GCC/gccrs/pull/137#discussion_r555210692

bjorn3 avatar Jan 11 '21 17:01 bjorn3

Thanks for your contribution fellow Rustacean

github-actions[bot] avatar Jan 11 '21 17:01 github-actions[bot]

The main shim is codegened at https://github.com/rust-lang/rust/blob/015d2bc3fec48cef3cbfaec71c54fa31ce420853/compiler/rustc_codegen_ssa/src/base.rs#L345-L439. Note that https://github.com/rust-lang/rust/pull/84062 will change the logic a bit.

bjorn3 avatar Apr 13 '21 15:04 bjorn3

I think I know where/how to conceptually do such a thing in GCC; will provide details at some later point in time.

tschwinge avatar Apr 13 '21 15:04 tschwinge

I've been reading the discussion about #[start] and I found no mention of envp ?

P-E-P avatar Mar 14 '23 09:03 P-E-P

Rust's standard library doesn't need envp (it uses libc functions and the global variable instead), so rustc doesn't expose this argument.

bjorn3 avatar Mar 14 '23 10:03 bjorn3