Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .init_array #69

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
2 changes: 1 addition & 1 deletion crates/sel4-microkit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub use message::{
/// See the [`protection_domain`] attribute macro for more detail.
#[macro_export]
macro_rules! declare_protection_domain {
{
{
init = $init:expr $(,)?
} => {
$crate::_private::declare_protection_domain! {
Expand Down
2 changes: 1 addition & 1 deletion crates/sel4-panicking/env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn default_abort_hook(info: Option<&AbortInfo>) {

/// Prints via a link-time hook.
///
/// This function uses the following externally defined symobol:
/// This function uses the following externally defined symbol:
///
/// ```rust
/// extern "Rust" {
Expand Down
51 changes: 26 additions & 25 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 Expand Up @@ -88,31 +89,31 @@ sel4_panicking_env::register_debug_put_char!(sel4::debug_put_char);
#[macro_export]
macro_rules! declare_root_task {
{
main = $main:expr $(,)?
} => {
$crate::_private::declare_root_task! {
main = $main,
stack_size = $crate::_private::DEFAULT_STACK_SIZE,
}
};
{
main = $main:expr,
stack_size = $stack_size:expr $(,)?
} => {
$crate::_private::declare_main!($main);
$crate::_private::declare_stack!($stack_size);
};
{
main = $main:expr,
$(stack_size = $stack_size:expr,)?
heap_size = $heap_size:expr $(,)?
} => {
$crate::_private::declare_heap!($heap_size);
$crate::_private::declare_root_task! {
main = $main,
$(stack_size = $stack_size,)?
}
};
main = $main:expr $(,)?
} => {
$crate::_private::declare_root_task! {
main = $main,
stack_size = $crate::_private::DEFAULT_STACK_SIZE,
}
};
{
main = $main:expr,
stack_size = $stack_size:expr $(,)?
} => {
$crate::_private::declare_main!($main);
$crate::_private::declare_stack!($stack_size);
};
{
main = $main:expr,
$(stack_size = $stack_size:expr,)?
heap_size = $heap_size:expr $(,)?
} => {
$crate::_private::declare_heap!($heap_size);
$crate::_private::declare_root_task! {
main = $main,
$(stack_size = $stack_size,)?
}
};
}

#[doc(hidden)]
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
Loading