How to set a kernel entry point: error: pointers cannot be cast to integers during const eval

I'd like to create a struct (e.g. multiboot2) in a binary (i.e. a kernel) where the struct includes a pointer to the kernel entry point. But the compiler won't let me do it, giving me the pointers cannot be cast to integers during const eval error.

How can I achieve this?

Thanks

#![no_std]
#![no_main]

#[repr(C)]
struct Multiboot2 {
    entry_point: u32,
}

static _multiboot_header: Multiboot2 = Multiboot2{
    entry_point: main as usize as u32,
};

fn main() {
    loop{};
}

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

I'm aware this address isn't available until the linker runs. Maybe there is some incantation of extern that will let the compiler leave that unfilled until link time?

The type of the field has to be a pointer type.

#[repr(C)]
struct Multiboot2 {
    entry_point: fn(),
}

static _multiboot_header: Multiboot2 = Multiboot2{
    entry_point: main,
};
2 Likes

Nice, TY