Skip to content

Commit 0fe862e

Browse files
committed
Emerald: Implement panic unwind using unwinding crate
This is simliar to what xous target does, we just copies and changed how we get the `eh_frame` to use the new feature we added in `emerald_std`
1 parent b5ef9a3 commit 0fe862e

File tree

9 files changed

+37
-7
lines changed

9 files changed

+37
-7
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,15 +1212,15 @@ dependencies = [
12121212

12131213
[[package]]
12141214
name = "emerald_kernel_user_link"
1215-
version = "0.2.7"
1215+
version = "0.2.8"
12161216
dependencies = [
12171217
"compiler_builtins",
12181218
"rustc-std-workspace-core",
12191219
]
12201220

12211221
[[package]]
12221222
name = "emerald_std"
1223-
version = "0.2.8"
1223+
version = "0.2.9"
12241224
dependencies = [
12251225
"compiler_builtins",
12261226
"emerald_kernel_user_link",

compiler/rustc_target/src/spec/base/emerald.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn opts() -> TargetOptions {
99
// position_independent_executables: false,
1010
// static_position_independent_executables: false,
1111
// has_thread_local: false,
12-
panic_strategy: PanicStrategy::Abort,
12+
panic_strategy: PanicStrategy::Unwind,
1313
..Default::default()
1414
}
1515
}

library/panic_unwind/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ cfg_if::cfg_if! {
5050
all(target_family = "windows", target_env = "gnu"),
5151
target_os = "psp",
5252
target_os = "xous",
53+
target_os = "emerald",
5354
target_os = "solid_asp3",
5455
all(target_family = "unix", not(target_os = "espidf")),
5556
all(target_vendor = "fortanix", target_env = "sgx"),

library/std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ r-efi-alloc = { version = "1.0.0", features = ['rustc-dep-of-std'] }
6161
# This is from `https://github.com/Amjad50/Emerald`, i.e. it must be run from that context
6262
# TODO: for now we are using relative path, as we are using `libm` from `git` and we can't publish
6363
# that. Fix it later
64-
emerald_std = { version = "0.2.8", features = ['rustc-dep-of-std'], path = "../../../../libraries/emerald_std" }
64+
emerald_std = { version = "0.2.9", features = ['rustc-dep-of-std'], path = "../../../../libraries/emerald_std" }
6565

6666
[features]
6767
backtrace = [

library/std/src/sys/pal/emerald/os.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,30 @@ use crate::io;
77
use crate::marker::PhantomData;
88
use crate::os::emerald::prelude::OsStringExt;
99
use crate::path::{self, PathBuf};
10+
use crate::ptr::addr_of;
1011
use crate::sys::common::small_c_string::run_path_with_cstr;
1112
use crate::sys::pal::emerald::syscall_to_io_error;
1213

14+
#[cfg(not(test))]
15+
#[cfg(feature = "panic_unwind")]
16+
mod eh_unwinding {
17+
pub(crate) struct EhFrameFinder(usize /* eh_frame */);
18+
pub(crate) static mut EH_FRAME_SETTINGS: EhFrameFinder = EhFrameFinder(0);
19+
impl EhFrameFinder {
20+
pub(crate) fn init(&mut self, eh_frame: usize) {
21+
self.0 = eh_frame;
22+
}
23+
}
24+
unsafe impl unwind::EhFrameFinder for EhFrameFinder {
25+
fn find(&self, _pc: usize) -> Option<unwind::FrameInfo> {
26+
Some(unwind::FrameInfo {
27+
text_base: None,
28+
kind: unwind::FrameInfoKind::EhFrame(self.0),
29+
})
30+
}
31+
}
32+
}
33+
1334
// This function is needed by the panic runtime. The symbol is named in
1435
// pre-link args for the target specification, so keep that in sync.
1536
#[no_mangle]
@@ -22,6 +43,13 @@ extern "C" {
2243

2344
#[no_mangle]
2445
pub extern "C" fn _start(argc: isize, argv: *const *const u8) -> ! {
46+
#[cfg(not(test))]
47+
#[cfg(feature = "panic_unwind")]
48+
unsafe {
49+
eh_unwinding::EH_FRAME_SETTINGS
50+
.init(emerald_std::process::process_metadata().eh_frame_adress);
51+
unwind::set_custom_eh_frame_finder(&*addr_of!(eh_unwinding::EH_FRAME_SETTINGS)).ok();
52+
}
2553
exit(unsafe { main(argc, argv) });
2654
}
2755

library/std/src/sys/personality/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ cfg_if::cfg_if! {
3030
target_os = "psp",
3131
target_os = "xous",
3232
target_os = "solid_asp3",
33+
target_os = "emerald",
3334
all(target_family = "unix", not(target_os = "espidf"), not(target_os = "l4re")),
3435
all(target_vendor = "fortanix", target_env = "sgx"),
3536
))] {

library/unwind/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ libc = { version = "0.2.140", features = ['rustc-dep-of-std'], default-features
1919
compiler_builtins = "0.1.0"
2020
cfg-if = "1.0"
2121

22-
[target.'cfg(target_os = "xous")'.dependencies]
22+
[target.'cfg(any(target_os = "xous", target_os = "emerald"))'.dependencies]
2323
unwinding = { version = "0.2.1", features = ['rustc-dep-of-std', 'unwinder', 'fde-custom'], default-features = false }
2424

2525
[features]

library/unwind/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ cfg_if::cfg_if! {
2626
))] {
2727
mod libunwind;
2828
pub use libunwind::*;
29-
} else if #[cfg(target_os = "xous")] {
29+
} else if #[cfg(any(target_os = "xous", target_os = "emerald"))] {
3030
mod unwinding;
3131
pub use unwinding::*;
3232
} else {

library/unwind/src/unwinding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(nonstandard_style)]
22

3-
use libc::{c_int, c_void};
3+
use core::ffi::{c_int, c_void};
44

55
#[repr(C)]
66
#[derive(Copy, Clone, PartialEq)]

0 commit comments

Comments
 (0)