1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use std::{fmt, iter, mem, slice};
use crate::{util::CStr};
use crate::{Error, Result};
use crate::util::AlignTo;
use super::{Align, Pe, image::*};
#[derive(Copy, Clone)]
pub struct Debug<'a, P> {
pe: P,
image: &'a [IMAGE_DEBUG_DIRECTORY],
}
impl<'a, P: Pe<'a>> Debug<'a, P> {
pub(crate) fn try_from(pe: P) -> Result<Debug<'a, P>> {
let datadir = pe.data_directory().get(IMAGE_DIRECTORY_ENTRY_DEBUG).ok_or(Error::Bounds)?;
let (len, rem) = (
datadir.Size as usize / mem::size_of::<IMAGE_DEBUG_DIRECTORY>(),
datadir.Size as usize % mem::size_of::<IMAGE_DEBUG_DIRECTORY>(),
);
if rem != 0 {
return Err(Error::Invalid);
}
let image = pe.derva_slice(datadir.VirtualAddress, len)?;
Ok(Debug { pe, image })
}
pub fn pe(&self) -> P {
self.pe
}
pub fn image(&self) -> &'a [IMAGE_DEBUG_DIRECTORY] {
self.image
}
pub fn pdb_file_name(&self) -> Option<&'a CStr> {
self.into_iter()
.filter_map(|dir| dir.entry().ok().and_then(Entry::as_code_view).map(|cv| cv.pdb_file_name()))
.next()
}
pub fn iter(&self) -> Iter<'a, P> {
Iter {
pe: self.pe,
iter: self.image.iter()
}
}
}
impl<'a, P: Pe<'a>> IntoIterator for Debug<'a, P> {
type Item = Dir<'a, P>;
type IntoIter = Iter<'a, P>;
fn into_iter(self) -> Iter<'a, P> {
self.iter()
}
}
impl<'a, P: Pe<'a>> fmt::Debug for Debug<'a, P> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(*self).finish()
}
}
#[derive(Clone)]
pub struct Iter<'a, P> {
pe: P,
iter: slice::Iter<'a, IMAGE_DEBUG_DIRECTORY>,
}
impl<'a, P: Pe<'a>> Iter<'a, P> {
pub fn image(&self) -> &'a [IMAGE_DEBUG_DIRECTORY] {
self.iter.as_slice()
}
}
impl<'a, P: Pe<'a>> Iterator for Iter<'a, P> {
type Item = Dir<'a, P>;
fn next(&mut self) -> Option<Dir<'a, P>> {
self.iter.next().map(|image| Dir { pe: self.pe, image })
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
fn count(self) -> usize {
self.iter.count()
}
fn nth(&mut self, n: usize) -> Option<Dir<'a, P>> {
self.iter.nth(n).map(|image| Dir { pe: self.pe, image })
}
}
impl<'a, P: Pe<'a>> DoubleEndedIterator for Iter<'a, P> {
fn next_back(&mut self) -> Option<Dir<'a, P>> {
self.iter.next_back().map(|image| Dir { pe: self.pe, image })
}
}
impl<'a, P: Pe<'a>> ExactSizeIterator for Iter<'a, P> {}
impl<'a, P: Pe<'a>> iter::FusedIterator for Iter<'a, P> {}
#[derive(Copy, Clone)]
pub struct Dir<'a, P> {
pe: P,
image: &'a IMAGE_DEBUG_DIRECTORY,
}
impl<'a, P: Pe<'a>> Dir<'a, P> {
pub fn pe(&self) -> P {
self.pe
}
pub fn image(&self) -> &'a IMAGE_DEBUG_DIRECTORY {
self.image
}
pub fn data(&self) -> Option<&'a [u8]> {
let image = self.pe.image();
let size = self.image.SizeOfData as usize;
let offset = match self.pe.align() {
Align::File => self.image.PointerToRawData,
Align::Section => self.image.AddressOfRawData,
} as usize;
image.get(offset..offset.wrapping_add(size))
}
pub fn entry(&self) -> Result<Entry<'a>> {
match self.image.Type {
IMAGE_DEBUG_TYPE_CODEVIEW => Ok(Entry::CodeView(code_view(&self)?)),
IMAGE_DEBUG_TYPE_MISC => Ok(Entry::Dbg(dbg(&self)?)),
IMAGE_DEBUG_TYPE_POGO => Ok(Entry::Pgo(pgo(&self)?)),
_ => Ok(Entry::Unknown(self.data()))
}
}
}
impl<'a, P: Pe<'a>> fmt::Debug for Dir<'a, P> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Dir")
.field("type", &crate::stringify::DebugType(self.image.Type).to_str().ok_or(self.image.Type))
.field("time_date_stamp", &self.image.TimeDateStamp)
.field("version", &self.image.Version)
.field("entry", &self.entry())
.finish()
}
}
pub use crate::wrap::debug::{Entry, CodeView, Dbg, Pgo, PgoIter, PgoItem};
fn code_view<'a, P: Pe<'a>>(dir: &Dir<'a, P>) -> Result<CodeView<'a>> {
let bytes = dir.data().ok_or(Error::Bounds)?;
if bytes.len() < 16 {
return Err(Error::Bounds);
}
if !(cfg!(feature = "unsafe_alignment") || bytes.as_ptr().aligned_to(4)) {
return Err(Error::Misaligned);
}
let cv_signature = unsafe { &*(bytes.as_ptr() as *const [u8; 4]) };
match cv_signature {
b"NB10" => {
if bytes.len() < 16 {
return Err(Error::Bounds);
}
let image = unsafe { &*(bytes.as_ptr() as *const IMAGE_DEBUG_CV_INFO_PDB20) };
let pdb_file_name = CStr::from_bytes(&bytes[16..]).ok_or(Error::Encoding)?;
Ok(CodeView::Cv20 { image, pdb_file_name })
},
b"RSDS" => {
if bytes.len() < 24 {
return Err(Error::Bounds);
}
let image = unsafe { &*(bytes.as_ptr() as *const IMAGE_DEBUG_CV_INFO_PDB70) };
let pdb_file_name = CStr::from_bytes(&bytes[24..]).ok_or(Error::Encoding)?;
Ok(CodeView::Cv70 { image, pdb_file_name })
},
_ => Err(Error::BadMagic),
}
}
fn dbg<'a, P: Pe<'a>>(dir: &Dir<'a, P>) -> Result<Dbg<'a>> {
let data = dir.data().ok_or(Error::Bounds)?;
if data.len() < mem::size_of::<IMAGE_DEBUG_MISC>() {
return Err(Error::Bounds);
}
if !(cfg!(feature = "unsafe_alignment") || data.as_ptr().aligned_to(4)) {
return Err(Error::Misaligned);
}
let image = unsafe { &*(data.as_ptr() as *const IMAGE_DEBUG_MISC) };
Ok(Dbg { image })
}
fn pgo<'a, P: Pe<'a>>(dir: &Dir<'a, P>) -> Result<Pgo<'a>> {
let data = dir.data().ok_or(Error::Bounds)?;
if data.len() < 4 {
return Err(Error::Bounds);
}
if !(cfg!(feature = "unsafe_alignment") || data.as_ptr().aligned_to(4)) {
return Err(Error::Misaligned);
}
let len = data.len() / 4;
let image = unsafe { slice::from_raw_parts(data.as_ptr() as *const u32, len) };
Ok(Pgo { image })
}
#[cfg(feature = "serde")]
mod serde {
use crate::util::serde_helper::*;
use super::{Pe, Debug, Dir};
impl<'a, P: Pe<'a>> Serialize for Debug<'a, P> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_seq(self.into_iter())
}
}
impl<'a, P: Pe<'a>> Serialize for Dir<'a, P> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let is_human_readable = serializer.is_human_readable();
let mut state = serializer.serialize_struct("Dir", 4)?;
if is_human_readable {
state.serialize_field("type", &crate::stringify::DebugType(self.image.Type).to_str())?;
}
else {
state.serialize_field("type", &self.image.Type)?;
}
state.serialize_field("time_date_stamp", &self.image.TimeDateStamp)?;
state.serialize_field("version", &self.image.Version)?;
state.serialize_field("entry", &self.entry().ok())?;
state.end()
}
}
}
#[cfg(test)]
pub(crate) fn test<'a, P: Pe<'a>>(pe: P) -> Result<()> {
let debug = pe.debug()?;
for dir in debug {
let _data = dir.data();
match dir.entry() {
Ok(Entry::CodeView(cv)) => {
let _format = cv.format();
let _pdb_file_name = cv.pdb_file_name();
},
Ok(Entry::Dbg(_dbg)) => (),
Ok(Entry::Pgo(pgo)) => {
for _sec in pgo {}
},
Ok(Entry::Unknown(_data)) => (),
Err(_) => (),
}
}
Ok(())
}