Skip to main content

kh_loader/
error.rs

1//! Loader and image-planning errors.
2
3use thiserror::Error;
4
5use kh_runtime::MapError;
6
7/// Failures while reading or validating a Mach-O image.
8#[derive(Debug, Error)]
9pub enum LoadError {
10    /// Filesystem or I/O failure.
11    #[error("I/O error: {0}")]
12    Io(#[from] std::io::Error),
13
14    /// Bytes are not a recognized Mach-O (or supported fat) image.
15    #[error("not a supported Mach-O binary: {0}")]
16    NotMachO(String),
17
18    /// Image architecture is not arm64.
19    #[error("unsupported architecture: {0}")]
20    UnsupportedArch(String),
21
22    /// Host/guest page geometry could not be established.
23    #[error("page layout error: {0}")]
24    PageLayout(String),
25
26    /// Guest memory mapping failed.
27    #[error("map error: {0}")]
28    Map(#[from] MapError),
29
30    /// Install-name resolution failed hard (e.g. invalid encoding).
31    #[error("path resolve: {0}")]
32    Resolve(String),
33
34    /// Dependency graph exceeded the dylib cap.
35    #[error("too many dylibs (limit {0})")]
36    TooManyDylibs(usize),
37
38    /// A dependency path existed but is not `MH_DYLIB`.
39    #[error("dependent image is not MH_DYLIB: {0}")]
40    NotDylib(String),
41
42    /// Strong undefined symbol with no export in the process set (PR2 bind).
43    #[error("unresolved symbol {name}")]
44    UnresolvedSymbol {
45        /// Symbol name (nlist spelling).
46        name: String,
47    },
48
49    /// GOT section missing or too small for undefined imports (PR2 bind).
50    #[error("GOT section missing or too small for {count} undefined symbols")]
51    MissingGot {
52        /// Number of undefined external symbols that needed GOT slots.
53        count: usize,
54    },
55
56    /// Main image slid but fixed `adrp` GOT addressing requires preferred base.
57    #[error("main image slid ({slide:#x}) but GOT adrp requires preferred placement: {path}")]
58    ImageSlid {
59        /// Host path of the main image.
60        path: String,
61        /// Applied slide.
62        slide: u64,
63    },
64
65    /// Feature not implemented yet (scaffolding).
66    #[error("not implemented: {0}")]
67    NotImplemented(&'static str),
68}
69
70impl LoadError {
71    /// Process exit code for the CLI (`2` = wrong kind/arch, `1` = other).
72    #[must_use]
73    pub fn exit_code(&self) -> u8 {
74        match self {
75            Self::NotMachO(_) | Self::UnsupportedArch(_) | Self::NotDylib(_) => 2,
76            _ => 1,
77        }
78    }
79}