Debian Codegraph @

third_party/rust/macho-unwind-info/src/raw/impls.rs

raw
1 use std::fmt::Debug;
2
3 use super::format::{
4 CompactUnwindInfoHeader, CompressedPage, Opcode, PageEntry, RegularFunctionEntry, RegularPage,
5 };
6 use super::unaligned::U32;
7 use crate::error::ReadError;
8 use crate::num_display::HexNum;
9 use crate::reader::Reader;
10
11 type Result<T> = std::result::Result<T, ReadError>;
12
13 impl CompactUnwindInfoHeader {
14 pub fn parse(data: &[u8]) -> Result<&Self> {
15 data.read_at::<CompactUnwindInfoHeader>(0)
16 .ok_or(ReadError::Header)
17 }
18
19 pub fn global_opcodes_offset(&self) -> u32 {
20 self.global_opcodes_offset.into()
21 }
22
23 pub fn global_opcodes_len(&self) -> u32 {
24 self.global_opcodes_len.into()
25 }
26
27 pub fn pages_offset(&self) -> u32 {
28 self.pages_offset.into()
29 }
30
31 pub fn pages_len(&self) -> u32 {
32 self.pages_len.into()
33 }
34
35 /// Return the list of global opcodes.
36 pub fn global_opcodes<'data>(&self, data: &'data [u8]) -> Result<&'data [Opcode]> {
37 data.read_slice_at::<Opcode>(
38 self.global_opcodes_offset().into(),
39 self.global_opcodes_len() as usize,
40 )
41 .ok_or(ReadError::GlobalOpcodes)
42 }
43
44 /// Return the list of pages.
45 pub fn pages<'data>(&self, data: &'data [u8]) -> Result<&'data [PageEntry]> {
46 data.read_slice_at::<PageEntry>(self.pages_offset().into(), self.pages_len() as usize)
47 .ok_or(ReadError::Pages)
48 }
49 }
50
51 impl RegularPage {
52 pub fn parse(data: &[u8], page_offset: u64) -> Result<&Self> {
53 data.read_at::<Self>(page_offset)
54 .ok_or(ReadError::RegularPage)
55 }
56
57 pub fn functions_offset(&self) -> u16 {
58 self.functions_offset.into()
59 }
60
61 pub fn functions_len(&self) -> u16 {
62 self.functions_len.into()
63 }
64
65 pub fn functions<'data>(
66 &self,
67 data: &'data [u8],
68 page_offset: u32,
69 ) -> Result<&'data [RegularFunctionEntry]> {
70 let relative_functions_offset = self.functions_offset();
71 let functions_len: usize = self.functions_len().into();
72 let functions_offset = page_offset as u64 + relative_functions_offset as u64;
73 data.read_slice_at::<RegularFunctionEntry>(functions_offset, functions_len)
74 .ok_or(ReadError::RegularPageFunctions)
75 }
76 }
77
78 impl CompressedPage {
79 pub fn parse(data: &[u8], page_offset: u64) -> Result<&Self> {
80 data.read_at::<Self>(page_offset)
81 .ok_or(ReadError::CompressedPage)
82 }
83
84 pub fn functions_offset(&self) -> u16 {
85 self.functions_offset.into()
86 }
87
88 pub fn functions_len(&self) -> u16 {
89 self.functions_len.into()
90 }
91
92 pub fn local_opcodes_offset(&self) -> u16 {
93 self.local_opcodes_offset.into()
94 }
95
96 pub fn local_opcodes_len(&self) -> u16 {
97 self.local_opcodes_len.into()
98 }
99
100 pub fn functions<'data>(&self, data: &'data [u8], page_offset: u32) -> Result<&'data [U32]> {
101 let relative_functions_offset = self.functions_offset();
102 let functions_len: usize = self.functions_len().into();
103 let functions_offset = page_offset as u64 + relative_functions_offset as u64;
104 data.read_slice_at::<U32>(functions_offset, functions_len)
105 .ok_or(ReadError::CompressedPageFunctions)
106 }
107
108 /// Return the list of local opcodes.
109 pub fn local_opcodes<'data>(
110 &self,
111 data: &'data [u8],
112 page_offset: u32,
113 ) -> Result<&'data [Opcode]> {
114 let relative_local_opcodes_offset = self.local_opcodes_offset();
115 let local_opcodes_len: usize = self.local_opcodes_len().into();
116 let local_opcodes_offset = page_offset as u64 + relative_local_opcodes_offset as u64;
117 data.read_slice_at::<Opcode>(local_opcodes_offset, local_opcodes_len)
118 .ok_or(ReadError::LocalOpcodes)
119 }
120 }
121
122 impl Opcode {
123 pub fn opcode(&self) -> u32 {
124 self.0.into()
125 }
126 }
127
128 impl RegularFunctionEntry {
129 pub fn address(&self) -> u32 {
130 self.address.into()
131 }
132
133 pub fn opcode(&self) -> u32 {
134 self.opcode.opcode()
135 }
136 }
137
138 impl PageEntry {
139 pub fn page_offset(&self) -> u32 {
140 self.page_offset.into()
141 }
142
143 pub fn first_address(&self) -> u32 {
144 self.first_address.into()
145 }
146
147 pub fn lsda_index_offset(&self) -> u32 {
148 self.lsda_index_offset.into()
149 }
150
151 pub fn page_kind(&self, data: &[u8]) -> Result<u32> {
152 let kind = *data
153 .read_at::<U32>(self.page_offset().into())
154 .ok_or(ReadError::PageKind)?;
155 Ok(kind.into())
156 }
157 }
158
159 impl Debug for PageEntry {
160 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
161 f.debug_struct("PageEntry")
162 .field("first_address", &HexNum(self.first_address()))
163 .field("page_offset", &HexNum(self.page_offset()))
164 .field("lsda_index_offset", &HexNum(self.lsda_index_offset()))
165 .finish()
166 }
167 }