Creating pointer to top of stack

I'm trying to point a boot loader at the top of my stack which is declared like so:

const MAX_STACK_SIZE: usize = 1024 * 256;
#[repr(C, align(4))] // Comply with system-V ABI
struct Stack([u8; MAX_STACK_SIZE]);
static STACK: Stack = Stack([0; MAX_STACK_SIZE]);

Specifically, the pointer needs to be one index past the end of the array to be at the top of the stack. However, doing this:

#[link_section = ".stivale2hdr"]
static BOOT_LOADER_HEADER: StivaleHeader = StivaleHeader::new()
    .stack(&STACK.0[MAX_STACK_SIZE] as *const u8)
    .tags(&FB_TAG as *const StivaleFramebufferHeaderTag as *const ());

Doesn't evaluate properly (compiler error), obviously, so how do I force Rust to do this anyway? Normally, I'd avoid unsafe code like this, but if I set it to MAX_STACK_SIZE - 1, my stack won't be aligned properly, and the System-V ABI mandates that the stack be 16-byte aligned.

The easiest way would be .as_ptr_range().end, but it looks like that's not stable in const yet.

1 Like

That worked! I'm on rust Nightly, and it didn't complain when I used it without a feature gate, so I'm assuming its stable in const contexts in rust 1.60.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.