unc_vm_compiler/
section.rs1use crate::lib::std::vec::Vec;
9use crate::Relocation;
10use unc_vm_types::entity::entity_impl;
11
12#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
14#[archive_attr(derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug))]
15#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
16pub struct SectionIndex(u32);
17
18entity_impl!(SectionIndex);
19
20entity_impl!(ArchivedSectionIndex);
21
22#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Copy, Clone, PartialEq, Eq)]
26pub enum CustomSectionProtection {
27 Read,
29
30 ReadExecute,
32}
33
34#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq)]
39pub struct CustomSection {
40 pub protection: CustomSectionProtection,
42
43 pub bytes: SectionBody,
50
51 pub relocations: Vec<Relocation>,
53}
54
55#[derive(Clone, Copy)]
59pub struct CustomSectionRef<'a> {
60 pub protection: CustomSectionProtection,
62
63 pub bytes: &'a [u8],
65}
66
67impl<'a> From<&'a CustomSection> for CustomSectionRef<'a> {
68 fn from(section: &'a CustomSection) -> Self {
69 CustomSectionRef { protection: section.protection, bytes: section.bytes.as_slice() }
70 }
71}
72
73impl<'a> From<&'a ArchivedCustomSection> for CustomSectionRef<'a> {
74 fn from(section: &'a ArchivedCustomSection) -> Self {
75 CustomSectionRef {
76 protection: Result::<_, std::convert::Infallible>::unwrap(
77 rkyv::Deserialize::deserialize(§ion.protection, &mut rkyv::Infallible),
78 ),
79 bytes: §ion.bytes.0[..],
80 }
81 }
82}
83
84#[derive(
86 rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq, Default,
87)]
88pub struct SectionBody(Vec<u8>);
89
90impl SectionBody {
91 pub fn new_with_vec(contents: Vec<u8>) -> Self {
93 Self(contents)
94 }
95
96 pub fn as_ptr(&self) -> *const u8 {
98 self.0.as_ptr()
99 }
100
101 pub fn len(&self) -> usize {
103 self.0.len()
104 }
105
106 pub fn as_slice(&self) -> &[u8] {
108 self.0.as_slice()
109 }
110
111 pub fn is_empty(&self) -> bool {
113 self.0.is_empty()
114 }
115}