Skip to content

Instantly share code, notes, and snippets.

@andreafioraldi
Created November 28, 2020 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreafioraldi/b584254ebdbdcf758c857bed7df31f51 to your computer and use it in GitHub Desktop.
Save andreafioraldi/b584254ebdbdcf758c857bed7df31f51 to your computer and use it in GitHub Desktop.
Use rust ctor crate on no_std when producing an ELF
#![no_std]
#![no_main]
extern crate libc;
#[ctor::ctor]
fn foo() {
unsafe { libc::puts("foo()\x00".as_ptr() as *const i8); }
}
#[ctor::ctor]
fn bar() {
unsafe { libc::puts("bar()\x00".as_ptr() as *const i8); }
}
fn main() {
unsafe { libc::puts("main()\x00".as_ptr() as *const i8); }
}
extern "C" {
#[no_mangle]
static mut __init_array_start: [Option<unsafe extern "C" fn() -> ()>; 0];
#[no_mangle]
static mut __init_array_end: [Option<unsafe extern "C" fn() -> ()>; 0];
}
pub unsafe fn __rustctor_init_array() {
let count = __init_array_end.as_mut_ptr().offset_from(__init_array_start.as_mut_ptr()) as usize;
for i in 0..count {
(*__init_array_start.as_mut_ptr().offset(i as isize)).expect("non-null function pointer")();
}
}
#[no_mangle]
pub extern "C" fn _start() -> ! {
unsafe { __rustctor_init_array(); }
main();
loop {}
}
#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment