Debian Codegraph @

third_party/rust/goblin/src/pe/debug.rs

raw
1 use crate::error;
2 use scroll::{Pread, Pwrite, SizeWith};
3
4 use crate::pe::data_directories;
5 use crate::pe::options;
6 use crate::pe::section_table;
7 use crate::pe::utils;
8
9 #[derive(Debug, PartialEq, Copy, Clone, Default)]
10 pub struct DebugData<'a> {
11 pub image_debug_directory: ImageDebugDirectory,
12 pub codeview_pdb70_debug_info: Option<CodeviewPDB70DebugInfo<'a>>,
13 pub codeview_pdb20_debug_info: Option<CodeviewPDB20DebugInfo<'a>>,
14 }
15
16 impl<'a> DebugData<'a> {
17 pub fn parse(
18 bytes: &'a [u8],
19 dd: data_directories::DataDirectory,
20 sections: &[section_table::SectionTable],
21 file_alignment: u32,
22 ) -> error::Result<Self> {
23 Self::parse_with_opts(
24 bytes,
25 dd,
26 sections,
27 file_alignment,
28 &options::ParseOptions::default(),
29 )
30 }
31
32 pub fn parse_with_opts(
33 bytes: &'a [u8],
34 dd: data_directories::DataDirectory,
35 sections: &[section_table::SectionTable],
36 file_alignment: u32,
37 opts: &options::ParseOptions,
38 ) -> error::Result<Self> {
39 let image_debug_directory =
40 ImageDebugDirectory::parse_with_opts(bytes, dd, sections, file_alignment, opts)?;
41 let codeview_pdb70_debug_info =
42 CodeviewPDB70DebugInfo::parse_with_opts(bytes, &image_debug_directory, opts)?;
43 let codeview_pdb20_debug_info =
44 CodeviewPDB20DebugInfo::parse_with_opts(bytes, &image_debug_directory, opts)?;
45
46 Ok(DebugData {
47 image_debug_directory,
48 codeview_pdb70_debug_info,
49 codeview_pdb20_debug_info,
50 })
51 }
52
53 /// Return this executable's debugging GUID, suitable for matching against a PDB file.
54 pub fn guid(&self) -> Option<[u8; 16]> {
55 self.codeview_pdb70_debug_info.map(|pdb70| pdb70.signature)
56 }
57 }
58
59 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms680307(v=vs.85).aspx
60 #[repr(C)]
61 #[derive(Debug, PartialEq, Copy, Clone, Default, Pread, Pwrite, SizeWith)]
62 pub struct ImageDebugDirectory {
63 pub characteristics: u32,
64 pub time_date_stamp: u32,
65 pub major_version: u16,
66 pub minor_version: u16,
67 pub data_type: u32,
68 pub size_of_data: u32,
69 pub address_of_raw_data: u32,
70 pub pointer_to_raw_data: u32,
71 }
72
73 pub const IMAGE_DEBUG_TYPE_UNKNOWN: u32 = 0;
74 pub const IMAGE_DEBUG_TYPE_COFF: u32 = 1;
75 pub const IMAGE_DEBUG_TYPE_CODEVIEW: u32 = 2;
76 pub const IMAGE_DEBUG_TYPE_FPO: u32 = 3;
77 pub const IMAGE_DEBUG_TYPE_MISC: u32 = 4;
78 pub const IMAGE_DEBUG_TYPE_EXCEPTION: u32 = 5;
79 pub const IMAGE_DEBUG_TYPE_FIXUP: u32 = 6;
80 pub const IMAGE_DEBUG_TYPE_BORLAND: u32 = 9;
81
82 impl ImageDebugDirectory {
83 #[allow(unused)]
84 fn parse(
85 bytes: &[u8],
86 dd: data_directories::DataDirectory,
87 sections: &[section_table::SectionTable],
88 file_alignment: u32,
89 ) -> error::Result<Self> {
90 Self::parse_with_opts(
91 bytes,
92 dd,
93 sections,
94 file_alignment,
95 &options::ParseOptions::default(),
96 )
97 }
98
99 pub(crate) fn parse_with_opts(
100 bytes: &[u8],
101 dd: data_directories::DataDirectory,
102 sections: &[section_table::SectionTable],
103 file_alignment: u32,
104 opts: &options::ParseOptions,
105 ) -> error::Result<Self> {
106 let rva = dd.virtual_address as usize;
107 let offset = utils::find_offset(rva, sections, file_alignment, opts).ok_or_else(|| {
108 error::Error::Malformed(format!(
109 "Cannot map ImageDebugDirectory rva {:#x} into offset",
110 rva
111 ))
112 })?;
113 let idd: Self = bytes.pread_with(offset, scroll::LE)?;
114 Ok(idd)
115 }
116 }
117
118 pub const CODEVIEW_PDB70_MAGIC: u32 = 0x5344_5352;
119 pub const CODEVIEW_PDB20_MAGIC: u32 = 0x3031_424e;
120 pub const CODEVIEW_CV50_MAGIC: u32 = 0x3131_424e;
121 pub const CODEVIEW_CV41_MAGIC: u32 = 0x3930_424e;
122
123 // http://llvm.org/doxygen/CVDebugRecord_8h_source.html
124 #[repr(C)]
125 #[derive(Debug, PartialEq, Copy, Clone, Default)]
126 pub struct CodeviewPDB70DebugInfo<'a> {
127 pub codeview_signature: u32,
128 pub signature: [u8; 16],
129 pub age: u32,
130 pub filename: &'a [u8],
131 }
132
133 impl<'a> CodeviewPDB70DebugInfo<'a> {
134 pub fn parse(bytes: &'a [u8], idd: &ImageDebugDirectory) -> error::Result<Option<Self>> {
135 Self::parse_with_opts(bytes, idd, &options::ParseOptions::default())
136 }
137
138 pub fn parse_with_opts(
139 bytes: &'a [u8],
140 idd: &ImageDebugDirectory,
141 opts: &options::ParseOptions,
142 ) -> error::Result<Option<Self>> {
143 if idd.data_type != IMAGE_DEBUG_TYPE_CODEVIEW {
144 // not a codeview debug directory
145 // that's not an error, but it's not a CodeviewPDB70DebugInfo either
146 return Ok(None);
147 }
148
149 // ImageDebugDirectory.pointer_to_raw_data stores a raw offset -- not a virtual offset -- which we can use directly
150 let mut offset: usize = match opts.resolve_rva {
151 true => idd.pointer_to_raw_data as usize,
152 false => idd.address_of_raw_data as usize,
153 };
154
155 // calculate how long the eventual filename will be, which doubles as a check of the record size
156 let filename_length = idd.size_of_data as isize - 24;
157 if filename_length < 0 {
158 // the record is too short to be plausible
159 return Err(error::Error::Malformed(format!(
160 "ImageDebugDirectory size of data seems wrong: {:?}",
161 idd.size_of_data
162 )));
163 }
164 let filename_length = filename_length as usize;
165
166 // check the codeview signature
167 let codeview_signature: u32 = bytes.gread_with(&mut offset, scroll::LE)?;
168 if codeview_signature != CODEVIEW_PDB70_MAGIC {
169 return Ok(None);
170 }
171
172 // read the rest
173 let mut signature: [u8; 16] = [0; 16];
174 signature.copy_from_slice(bytes.gread_with(&mut offset, 16)?);
175 let age: u32 = bytes.gread_with(&mut offset, scroll::LE)?;
176 if let Some(filename) = bytes.get(offset..offset + filename_length) {
177 Ok(Some(CodeviewPDB70DebugInfo {
178 codeview_signature,
179 signature,
180 age,
181 filename,
182 }))
183 } else {
184 Err(error::Error::Malformed(format!(
185 "ImageDebugDirectory seems corrupted: {:?}",
186 idd
187 )))
188 }
189 }
190 }
191
192 // http://llvm.org/doxygen/CVDebugRecord_8h_source.html
193 #[repr(C)]
194 #[derive(Debug, PartialEq, Copy, Clone, Default)]
195 pub struct CodeviewPDB20DebugInfo<'a> {
196 pub codeview_signature: u32,
197 pub codeview_offset: u32,
198 pub signature: u32,
199 pub age: u32,
200 pub filename: &'a [u8],
201 }
202
203 impl<'a> CodeviewPDB20DebugInfo<'a> {
204 pub fn parse(bytes: &'a [u8], idd: &ImageDebugDirectory) -> error::Result<Option<Self>> {
205 Self::parse_with_opts(bytes, idd, &options::ParseOptions::default())
206 }
207
208 pub fn parse_with_opts(
209 bytes: &'a [u8],
210 idd: &ImageDebugDirectory,
211 opts: &options::ParseOptions,
212 ) -> error::Result<Option<Self>> {
213 if idd.data_type != IMAGE_DEBUG_TYPE_CODEVIEW {
214 // not a codeview debug directory
215 // that's not an error, but it's not a CodeviewPDB20DebugInfo either
216 return Ok(None);
217 }
218
219 // ImageDebugDirectory.pointer_to_raw_data stores a raw offset -- not a virtual offset -- which we can use directly
220 let mut offset: usize = match opts.resolve_rva {
221 true => idd.pointer_to_raw_data as usize,
222 false => idd.address_of_raw_data as usize,
223 };
224
225 // calculate how long the eventual filename will be, which doubles as a check of the record size
226 let filename_length = idd.size_of_data as isize - 16;
227 if filename_length < 0 {
228 // the record is too short to be plausible
229 return Err(error::Error::Malformed(format!(
230 "ImageDebugDirectory size of data seems wrong: {:?}",
231 idd.size_of_data
232 )));
233 }
234 let filename_length = filename_length as usize;
235
236 // check the codeview signature
237 let codeview_signature: u32 = bytes.gread_with(&mut offset, scroll::LE)?;
238 if codeview_signature != CODEVIEW_PDB20_MAGIC {
239 return Ok(None);
240 }
241 let codeview_offset: u32 = bytes.gread_with(&mut offset, scroll::LE)?;
242
243 // read the rest
244 let signature: u32 = bytes.gread_with(&mut offset, scroll::LE)?;
245 let age: u32 = bytes.gread_with(&mut offset, scroll::LE)?;
246 if let Some(filename) = bytes.get(offset..offset + filename_length) {
247 Ok(Some(CodeviewPDB20DebugInfo {
248 codeview_signature,
249 codeview_offset,
250 signature,
251 age,
252 filename,
253 }))
254 } else {
255 Err(error::Error::Malformed(format!(
256 "ImageDebugDirectory seems corrupted: {:?}",
257 idd
258 )))
259 }
260 }
261 }