Skip to content

Commit

Permalink
Add support for .init_array
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <[email protected]>
  • Loading branch information
nspin committed Jan 25, 2024
1 parent d80404b commit 14aef12
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/private/support/sel4-simple-task/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub unsafe extern "C" fn cont_fn(cont_arg: *mut sel4_runtime_common::ContArg) ->
CONFIG.set(config.clone()).unwrap();
sel4_runtime_common::set_eh_frame_finder().unwrap();
sel4_panicking::set_hook(&panic_hook);
sel4_runtime_common::run_ctors();
__sel4_simple_task_main(config.arg());
} else {
let endpoint = Endpoint::from_bits(thread_config.endpoint().unwrap());
Expand Down
1 change: 1 addition & 0 deletions crates/sel4-microkit/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn inner_entry() -> ! {

unsafe {
sel4::set_ipc_buffer(get_ipc_buffer());
sel4_runtime_common::run_ctors();
__sel4_microkit__main();
}

Expand Down
1 change: 1 addition & 0 deletions crates/sel4-root-task/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn inner_entry(bootinfo: *const sel4::sys::seL4_BootInfo) -> ! {
unsafe {
let bootinfo = sel4::BootInfo::from_ptr(bootinfo);
sel4::set_ipc_buffer(bootinfo.ipc_buffer());
sel4_runtime_common::run_ctors();
__sel4_root_task__main(&bootinfo);
}

Expand Down
39 changes: 39 additions & 0 deletions crates/sel4-runtime-common/src/ctors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Copyright 2024, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use core::mem;
use core::ptr;
use core::slice;

use sel4_panicking_env::abort;

type Ctor = unsafe extern "C" fn();

extern "C" {
static __init_array_start: Ctor;
static __init_array_end: Ctor;
}

pub unsafe fn run_ctors() {
let start = ptr::addr_of!(__init_array_start);
let end = ptr::addr_of!(__init_array_end);

// Cast to usize for comparison, otherwise rustc seems to apply an erroneous optimization
// assuming __init_array_start != __init_array_end.
if start as usize != end as usize {
if start.align_offset(mem::size_of::<Ctor>()) != 0
|| end.align_offset(mem::size_of::<Ctor>()) != 0
{
abort!("'.init_array' section is not properly aligned");
}

let len = (end as usize - start as usize) / mem::size_of::<Ctor>();
let ctors = slice::from_raw_parts(start, len);
for ctor in ctors {
(ctor)();
}
}
}
4 changes: 4 additions & 0 deletions crates/sel4-runtime-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#![no_std]
#![feature(cfg_target_thread_local)]

mod ctors;

pub use ctors::run_ctors;

#[cfg(feature = "start")]
mod start;

Expand Down

0 comments on commit 14aef12

Please sign in to comment.