rust-ctor icon indicating copy to clipboard operation
rust-ctor copied to clipboard

Ordering using `.init_array.N`

Open maxwase opened this issue 3 years ago • 0 comments

It would be nice if user could override execution order without changing code location.

The following code will panic because DATA is not initialized yet

#[ctor]
static ACTUAL_DATA: u8 = *DATA;

#[ctor]
static DATA: u8 = 1;

fn main() {
    println!("{}", *ACTUAL_DATA);
}
fatal runtime error: failed to initiate panic, error 5
IOT instruction (core dumped)

If we swap two statics, it works normally.

My suggestion is to have ordering argument, so we can write

#[ctor(ordering = 2)]
static ACTUAL_DATA: u8 = *DATA;

#[ctor(ordering = 1)]
static DATA: u8 = 1;

And as a result generate something like this:

#[link_section = ".init_array.1"]

maxwase avatar Aug 19 '22 17:08 maxwase