obkrnl/imgfmt/elf/
mod.rs

1use core::ops::Deref;
2
3/// Single ELF note.
4#[repr(C)]
5pub struct Note<const N: usize, const D: usize> {
6    hdr: NoteHdr,
7    name: [u8; N],
8    desc: NoteDesc<D>,
9}
10
11impl<const N: usize, const D: usize> Note<N, D> {
12    /// # Safety
13    /// `name` must contains NUL as a last element.
14    pub const unsafe fn new(name: [u8; N], ty: u32, desc: [u8; D]) -> Self {
15        Self {
16            hdr: NoteHdr {
17                name_len: N as _,
18                desc_len: D as _,
19                ty,
20            },
21            name,
22            desc: NoteDesc(desc),
23        }
24    }
25}
26
27/// Implementation of `Elf64_Nhdr` and `Elf32_Nhdr` structure.
28#[repr(C)]
29pub struct NoteHdr {
30    /// n_namesz.
31    pub name_len: u32,
32    /// n_descsz.
33    pub desc_len: u32,
34    /// n_type.
35    pub ty: u32,
36}
37
38/// Note description.
39#[repr(C, align(4))]
40pub struct NoteDesc<const L: usize>([u8; L]);
41
42impl<const L: usize> Deref for NoteDesc<L> {
43    type Target = [u8; L];
44
45    fn deref(&self) -> &Self::Target {
46        &self.0
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    use core::mem::offset_of;
54
55    #[test]
56    fn note() {
57        assert_eq!(offset_of!(Note::<3, 1>, name), 12);
58        assert_eq!(offset_of!(Note::<3, 1>, desc), 16);
59    }
60}