1use alloc::{
2 boxed::Box,
3 string::{String, ToString},
4};
5use core::{any::Any, fmt::Display};
6
7#[derive(Debug)]
9pub enum Error {
10 LoaderError { err: elf_loader::Error },
12 FindLibError { msg: String },
14 FindSymbolError { msg: String },
16 IteratorPhdrError { err: Box<dyn Any> },
18 ParseLdCacheError { msg: String },
20 InvalidPath,
22 Unsupported,
24 #[cfg(feature = "std")]
26 IO(std::io::Error),
27 #[cfg(not(feature = "std"))]
29 IO(String),
30}
31
32impl Display for Error {
33 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34 match self {
35 Error::LoaderError { err } => write!(f, "{err}"),
36 Error::FindLibError { msg } => write!(f, "{msg}"),
37 Error::FindSymbolError { msg } => write!(f, "{msg}"),
38 Error::IteratorPhdrError { err } => write!(f, "iterator phdr error: {err:?}"),
39 Error::ParseLdCacheError { msg } => write!(f, "{msg}"),
40 Error::InvalidPath => write!(f, "invalid path"),
41 Error::Unsupported => write!(f, "unsupported"),
42 #[cfg(feature = "std")]
43 Error::IO(err) => write!(f, "IO error: {err}"),
44 #[cfg(not(feature = "std"))]
45 Error::IO(msg) => write!(f, "IO error: {msg}"),
46 }
47 }
48}
49
50#[cfg(feature = "std")]
51impl std::error::Error for Error {}
52
53#[cfg(feature = "std")]
54impl From<std::io::Error> for Error {
55 fn from(value: std::io::Error) -> Self {
56 Error::IO(value)
57 }
58}
59
60impl From<elf_loader::Error> for Error {
61 #[cold]
62 fn from(value: elf_loader::Error) -> Self {
63 Error::LoaderError { err: value }
64 }
65}
66
67#[cold]
68#[inline(never)]
69pub(crate) fn find_lib_error(msg: impl ToString) -> Error {
70 Error::FindLibError {
71 msg: msg.to_string(),
72 }
73}
74
75#[cold]
76#[inline(never)]
77pub(crate) fn find_symbol_error(msg: impl ToString) -> Error {
78 Error::FindSymbolError {
79 msg: msg.to_string(),
80 }
81}
82
83#[cold]
84#[inline(never)]
85pub(crate) fn parse_ld_cache_error(msg: impl ToString) -> Error {
86 Error::ParseLdCacheError {
87 msg: msg.to_string(),
88 }
89}