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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use std::borrow::Cow;
use std::fmt;
use std::io::Cursor;
use std::marker::PhantomData;
use failure::Fail;
use fallible_iterator::FallibleIterator;
use parking_lot::RwLock;
use pdb::{AddressMap, MachineType, SymbolData};
use symbolic_common::{Arch, AsSelf, CodeId, DebugId, Uuid};
use crate::base::*;
use crate::private::{HexFmt, Parse};
type Pdb<'d> = pdb::PDB<'d, Cursor<&'d [u8]>>;
const MAGIC_BIG: &[u8] = b"Microsoft C/C++ MSF 7.00\r\n\x1a\x44\x53\x00\x00\x00";
#[derive(Debug, Fail)]
pub enum PdbError {
#[fail(display = "invalid PDB file")]
BadObject(#[fail(cause)] pdb::Error),
}
pub struct PdbObject<'d> {
pdb: RwLock<Pdb<'d>>,
debug_info: pdb::DebugInformation<'d>,
pdb_info: pdb::PDBInformation<'d>,
public_syms: pdb::SymbolTable<'d>,
data: &'d [u8],
}
unsafe impl Send for PdbObject<'_> {}
unsafe impl Sync for PdbObject<'_> {}
impl<'d> PdbObject<'d> {
pub fn test(data: &[u8]) -> bool {
data.starts_with(MAGIC_BIG)
}
pub fn parse(data: &'d [u8]) -> Result<Self, PdbError> {
let mut pdb = Pdb::open(Cursor::new(data)).map_err(PdbError::BadObject)?;
let dbi = pdb.debug_information().map_err(PdbError::BadObject)?;
let pdbi = pdb.pdb_information().map_err(PdbError::BadObject)?;
let pubi = pdb.global_symbols().map_err(PdbError::BadObject)?;
Ok(PdbObject {
pdb: RwLock::new(pdb),
debug_info: dbi,
pdb_info: pdbi,
public_syms: pubi,
data,
})
}
pub fn file_format(&self) -> FileFormat {
FileFormat::Pdb
}
pub fn code_id(&self) -> Option<CodeId> {
None
}
pub fn debug_id(&self) -> DebugId {
match Uuid::from_slice(&self.pdb_info.guid.as_bytes()[..]) {
Ok(uuid) => DebugId::from_parts(uuid, self.pdb_info.age),
Err(_) => DebugId::default(),
}
}
pub fn arch(&self) -> Arch {
self.debug_info
.machine_type()
.ok()
.map(arch_from_machine)
.unwrap_or_default()
}
pub fn kind(&self) -> ObjectKind {
ObjectKind::Debug
}
pub fn load_address(&self) -> u64 {
0
}
pub fn has_symbols(&self) -> bool {
true
}
pub fn symbols(&self) -> PdbSymbolIterator<'d, '_> {
PdbSymbolIterator {
symbols: self.public_syms.iter(),
address_map: self.pdb.write().address_map().ok(),
}
}
pub fn symbol_map(&self) -> SymbolMap<'d> {
self.symbols().collect()
}
pub fn has_debug_info(&self) -> bool {
false
}
pub fn debug_session(&self) -> Result<PdbDebugSession<'d>, PdbError> {
Ok(PdbDebugSession { _ph: PhantomData })
}
pub fn has_unwind_info(&self) -> bool {
false
}
pub fn data(&self) -> &'d [u8] {
self.data
}
}
impl fmt::Debug for PdbObject<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PdbObject")
.field("debug_id", &self.debug_id())
.field("arch", &self.arch())
.field("load_address", &HexFmt(self.load_address()))
.field("has_symbols", &self.has_symbols())
.field("has_debug_info", &self.has_debug_info())
.field("has_unwind_info", &self.has_unwind_info())
.finish()
}
}
impl<'slf, 'd: 'slf> AsSelf<'slf> for PdbObject<'d> {
type Ref = PdbObject<'slf>;
fn as_self(&'slf self) -> &Self::Ref {
unsafe { std::mem::transmute(self) }
}
}
impl<'d> Parse<'d> for PdbObject<'d> {
type Error = PdbError;
fn test(data: &[u8]) -> bool {
Self::test(data)
}
fn parse(data: &'d [u8]) -> Result<Self, PdbError> {
Self::parse(data)
}
}
impl<'d> ObjectLike for PdbObject<'d> {
type Error = PdbError;
type Session = PdbDebugSession<'d>;
fn file_format(&self) -> FileFormat {
self.file_format()
}
fn code_id(&self) -> Option<CodeId> {
self.code_id()
}
fn debug_id(&self) -> DebugId {
self.debug_id()
}
fn arch(&self) -> Arch {
self.arch()
}
fn kind(&self) -> ObjectKind {
self.kind()
}
fn load_address(&self) -> u64 {
self.load_address()
}
fn has_symbols(&self) -> bool {
self.has_symbols()
}
fn symbols(&self) -> DynIterator<'_, Symbol<'_>> {
Box::new(self.symbols())
}
fn symbol_map(&self) -> SymbolMap<'_> {
self.symbol_map()
}
fn has_debug_info(&self) -> bool {
self.has_debug_info()
}
fn debug_session(&self) -> Result<Self::Session, Self::Error> {
self.debug_session()
}
fn has_unwind_info(&self) -> bool {
self.has_unwind_info()
}
}
pub(crate) fn arch_from_machine(machine: MachineType) -> Arch {
match machine {
MachineType::X86 => Arch::X86,
MachineType::Amd64 => Arch::Amd64,
MachineType::Arm => Arch::Arm,
MachineType::Arm64 => Arch::Arm64,
MachineType::PowerPC => Arch::Ppc,
_ => Arch::Unknown,
}
}
pub struct PdbSymbolIterator<'d, 'o> {
symbols: pdb::SymbolIter<'o>,
address_map: Option<AddressMap<'d>>,
}
impl<'d, 'o> Iterator for PdbSymbolIterator<'d, 'o> {
type Item = Symbol<'d>;
fn next(&mut self) -> Option<Self::Item> {
let address_map = self.address_map.as_ref()?;
while let Ok(Some(symbol)) = self.symbols.next() {
if let Ok(SymbolData::PublicSymbol(public)) = symbol.parse() {
if !public.function {
continue;
}
let address = match public.offset.to_rva(address_map) {
Some(address) => address,
None => continue,
};
let name = symbol.name().ok().map(|name| {
let cow = name.to_string();
Cow::from(String::from(if cow.starts_with('_') {
&cow[1..]
} else {
&cow
}))
});
return Some(Symbol {
name,
address: u64::from(address.0),
size: 0,
});
}
}
None
}
}
#[derive(Debug)]
pub struct PdbDebugSession<'d> {
_ph: PhantomData<&'d ()>,
}
impl<'d> PdbDebugSession<'d> {
pub fn functions(&mut self) -> PdbFunctionIterator<'_> {
std::iter::empty()
}
}
impl DebugSession for PdbDebugSession<'_> {
type Error = PdbError;
fn functions(&mut self) -> DynIterator<'_, Result<Function<'_>, Self::Error>> {
Box::new(self.functions())
}
}
pub type PdbFunctionIterator<'s> = std::iter::Empty<Result<Function<'s>, PdbError>>;