hermit_entry/
note.rs

1use core::mem;
2
3use crate::HermitVersion;
4
5/// Defines the hermit entry version in the note section.
6///
7/// This macro must be used in a module that is guaranteed to be linked.
8/// See <https://github.com/rust-lang/rust/issues/99721>.
9#[macro_export]
10macro_rules! define_entry_version {
11    () => {
12        #[used]
13        #[link_section = ".note.hermit.entry-version"]
14        static ENTRY_VERSION: $crate::_Note = $crate::_Note::entry_version();
15    };
16}
17
18#[repr(C)]
19#[doc(hidden)]
20pub struct _Note {
21    header: Nhdr32,
22    name: [u8; 8],
23    data: [u8; 1],
24}
25
26impl _Note {
27    pub const fn entry_version() -> Self {
28        Self {
29            header: Nhdr32 {
30                n_namesz: 7,
31                n_descsz: 1,
32                n_type: crate::NT_HERMIT_ENTRY_VERSION,
33            },
34            name: *b"HERMIT\0\0",
35            data: [crate::HERMIT_ENTRY_VERSION],
36        }
37    }
38}
39
40#[repr(C)]
41struct Nhdr32 {
42    n_namesz: u32,
43    n_descsz: u32,
44    n_type: u32,
45}
46
47/// Defines the current Hermit kernel version in the note section.
48///
49/// The version is saved in `.note.ABI-tag` in accordance with [LSB].
50///
51/// [LSB]: https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/noteabitag.html
52#[macro_export]
53macro_rules! define_abi_tag {
54    () => {
55        #[used]
56        #[link_section = ".note.ABI-tag"]
57        static ABI_TAG: $crate::_AbiTag = $crate::_AbiTag::new($crate::HermitVersion {
58            major: $crate::_parse_u128(::core::env!("CARGO_PKG_VERSION_MAJOR")) as u32,
59            minor: $crate::_parse_u128(::core::env!("CARGO_PKG_VERSION_MINOR")) as u32,
60            patch: $crate::_parse_u128(::core::env!("CARGO_PKG_VERSION_PATCH")) as u32,
61        });
62    };
63}
64
65#[repr(C)]
66#[doc(hidden)]
67pub struct _AbiTag {
68    header: Nhdr32,
69    name: [u8; 4],
70    data: [u32; 4],
71}
72
73impl _AbiTag {
74    pub const fn new(version: HermitVersion) -> Self {
75        Self {
76            header: Nhdr32 {
77                n_namesz: mem::size_of::<[u8; 4]>() as u32,
78                n_descsz: mem::size_of::<[u32; 4]>() as u32,
79                n_type: crate::NT_GNU_ABI_TAG,
80            },
81            name: *b"GNU\0",
82            data: [
83                crate::ELF_NOTE_OS_HERMIT,
84                version.major,
85                version.minor,
86                version.patch,
87            ],
88        }
89    }
90}