Skip to main content

logicaffeine_forge/
stencil_model.rs

1//! The stencil data model — shared between build.rs (which CONSTRUCTS these
2//! from parsed object files) and the runtime (which copies and patches them).
3//! Included via `include!` from both sides, so it must stay dependency-free.
4
5/// One extracted stencil: machine code plus the holes to patch.
6#[derive(Debug)]
7pub struct Stencil {
8    /// The stencil's symbol name in the object file.
9    pub name: &'static str,
10    /// The extracted machine code.
11    pub code: &'static [u8],
12    /// Every patchable site within `code`.
13    pub relocs: &'static [Reloc],
14}
15
16/// One patchable site inside a stencil's code.
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub struct Reloc {
19    /// Byte offset of the relocation site within `code`.
20    pub offset: u32,
21    /// The normalized relocation kind (decides the patcher).
22    pub kind: RelocKind,
23    /// Which hole this site resolves.
24    pub target: HoleId,
25    /// Constant folded into the patched value (`S + A - P` convention).
26    pub addend: i64,
27}
28
29/// Normalized relocation kinds across Mach-O / ELF / COFF, for the two
30/// architectures the JIT targets.
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub enum RelocKind {
33    /// aarch64 `b`/`bl` imm26 (±128 MiB, 4-byte aligned).
34    Branch26,
35    /// aarch64 `adrp` hi21 page, direct.
36    Page21,
37    /// aarch64 `add`/`ldr` lo12, direct. `scale` is the access-size shift the
38    /// patcher must apply (0 for `add`, 3 for 8-byte `ldr`).
39    PageOff12 {
40        /// Access-size shift the patcher applies (0 for `add`, 3 for 8-byte `ldr`).
41        scale: u8,
42    },
43    /// aarch64 `adrp` hi21 page → GOT slot.
44    GotPage21,
45    /// aarch64 `ldr` lo12 → GOT slot (8-byte scaled).
46    GotPageOff12,
47    /// x86-64 rip-relative disp32 (`jmp`/`call`/`lea`/`mov`).
48    Rel32,
49    /// x86-64 rip-relative disp32 → GOT/IAT slot.
50    GotRel32,
51    /// 8-byte absolute address, little-endian.
52    Abs64,
53}
54
55/// Which hole a relocation targets, decoded from the hole symbol's name.
56#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
57pub enum HoleId {
58    /// `logos_hole_cont_N` — a continuation (branch target).
59    Cont(u8),
60    /// `LOGOS_HOLE_I64_N` — a 64-bit constant.
61    ConstI64(u8),
62}