| 1 |
use std::fmt::Debug; |
| 2 |
use zerocopy_derive::{FromBytes, FromZeroes, Unaligned}; |
| 3 |
|
| 4 |
use super::unaligned::{U16, U32}; |
| 5 |
|
| 6 |
// Written with help from https://gankra.github.io/blah/compact-unwinding/ |
| 7 |
|
| 8 |
/// The `__unwind_info` header. |
| 9 |
#[derive(Unaligned, FromZeroes, FromBytes, Debug, Clone, Copy)] |
| 10 |
#[repr(C)] |
| 11 |
pub struct CompactUnwindInfoHeader { |
| 12 |
/// The version. Only version 1 is currently defined |
| 13 |
pub version: U32, |
| 14 |
|
| 15 |
/// The array of U32 global opcodes (offset relative to start of root page). |
| 16 |
/// |
| 17 |
/// These may be indexed by "compressed" second-level pages. |
| 18 |
pub global_opcodes_offset: U32, |
| 19 |
pub global_opcodes_len: U32, |
| 20 |
|
| 21 |
/// The array of U32 global personality codes (offset relative to start of root page). |
| 22 |
/// |
| 23 |
/// Personalities define the style of unwinding that an unwinder should use, |
| 24 |
/// and how to interpret the LSDA functions for a function (see below). |
| 25 |
pub personalities_offset: U32, |
| 26 |
pub personalities_len: U32, |
| 27 |
|
| 28 |
/// The array of [`PageEntry`]'s describing the second-level pages |
| 29 |
/// (offset relative to start of root page). |
| 30 |
pub pages_offset: U32, |
| 31 |
pub pages_len: U32, |
| 32 |
// After this point there are several dynamically-sized arrays whose precise |
| 33 |
// order and positioning don't matter, because they are all accessed using |
| 34 |
// offsets like the ones above. The arrays are: |
| 35 |
|
| 36 |
// global_opcodes: [u32; global_opcodes_len], |
| 37 |
// personalities: [u32; personalities_len], |
| 38 |
// pages: [PageEntry; pages_len], |
| 39 |
// lsdas: [LsdaEntry; unknown_len], |
| 40 |
} |
| 41 |
|
| 42 |
/// One element of the array of pages. |
| 43 |
#[derive(Unaligned, FromZeroes, FromBytes, Clone, Copy)] |
| 44 |
#[repr(C)] |
| 45 |
pub struct PageEntry { |
| 46 |
/// The first address mapped by this page. |
| 47 |
/// |
| 48 |
/// This is useful for binary-searching for the page that can map |
| 49 |
/// a specific address in the binary (the primary kind of lookup |
| 50 |
/// performed by an unwinder). |
| 51 |
pub first_address: U32, |
| 52 |
|
| 53 |
/// Offset of the second-level page. |
| 54 |
/// |
| 55 |
/// This may point to either a [`RegularPage`] or a [`CompressedPage`]. |
| 56 |
/// Which it is can be determined by the 32-bit "kind" value that is at |
| 57 |
/// the start of both layouts. |
| 58 |
pub page_offset: U32, |
| 59 |
|
| 60 |
/// Base offset into the lsdas array that functions in this page will be |
| 61 |
/// relative to. |
| 62 |
pub lsda_index_offset: U32, |
| 63 |
} |
| 64 |
|
| 65 |
/// A non-compressed page. |
| 66 |
#[derive(Unaligned, FromZeroes, FromBytes, Debug, Clone, Copy)] |
| 67 |
#[repr(C)] |
| 68 |
pub struct RegularPage { |
| 69 |
/// Always 2 (use to distinguish from CompressedPage). |
| 70 |
pub kind: U32, |
| 71 |
|
| 72 |
/// The Array of [`RegularFunctionEntry`]'s (offset relative to **start of this page**). |
| 73 |
pub functions_offset: U16, |
| 74 |
pub functions_len: U16, |
| 75 |
} |
| 76 |
|
| 77 |
/// A "compressed" page. |
| 78 |
#[derive(Unaligned, FromZeroes, FromBytes, Debug, Clone, Copy)] |
| 79 |
#[repr(C)] |
| 80 |
pub struct CompressedPage { |
| 81 |
/// Always 3 (use to distinguish from RegularPage). |
| 82 |
pub kind: U32, |
| 83 |
|
| 84 |
/// The array of compressed u32 function entries (offset relative to **start of this page**). |
| 85 |
/// |
| 86 |
/// Entries are a u32 that contains two packed values (from highest to lowest bits): |
| 87 |
/// * 8 bits: opcode index |
| 88 |
/// * 0..global_opcodes_len => index into global palette |
| 89 |
/// * global_opcodes_len..255 => index into local palette (subtract global_opcodes_len) |
| 90 |
/// * 24 bits: instruction address |
| 91 |
/// * address is relative to this page's first_address! |
| 92 |
pub functions_offset: U16, |
| 93 |
pub functions_len: U16, |
| 94 |
|
| 95 |
/// The array of u32 local opcodes for this page (offset relative to **start of this page**). |
| 96 |
pub local_opcodes_offset: U16, |
| 97 |
pub local_opcodes_len: U16, |
| 98 |
} |
| 99 |
|
| 100 |
/// An opcode. |
| 101 |
#[derive(Unaligned, FromZeroes, FromBytes, Debug, Clone, Copy)] |
| 102 |
#[repr(C)] |
| 103 |
pub struct Opcode(pub U32); |
| 104 |
|
| 105 |
/// A function entry from a non-compressed page. |
| 106 |
#[derive(Unaligned, FromZeroes, FromBytes, Debug, Clone, Copy)] |
| 107 |
#[repr(C)] |
| 108 |
pub struct RegularFunctionEntry { |
| 109 |
/// The address in the binary for this function entry (absolute). |
| 110 |
pub address: U32, |
| 111 |
|
| 112 |
/// The opcode for this address. |
| 113 |
pub opcode: Opcode, |
| 114 |
} |