-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmod.rs
2363 lines (2103 loc) · 74.8 KB
/
mod.rs
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 pdb Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use std::fmt;
use scroll::{ctx::TryFromCtx, Endian, Pread, LE};
use crate::common::*;
use crate::msf::*;
use crate::FallibleIterator;
mod annotations;
mod constants;
use self::constants::*;
pub use self::constants::{CPUType, SourceLanguage};
pub use self::annotations::*;
/// The raw type discriminator for `Symbols`.
pub type SymbolKind = u16;
/// Represents a symbol from the symbol table.
///
/// A `Symbol` is represented internally as a `&[u8]`, and in general the bytes inside are not
/// inspected in any way before calling any of the accessor methods.
///
/// To avoid copying, `Symbol`s exist as references to data owned by the parent `SymbolTable`.
/// Therefore, a `Symbol` may not outlive its parent `SymbolTable`.
#[derive(Copy, Clone, PartialEq)]
pub struct Symbol<'t> {
index: SymbolIndex,
data: &'t [u8],
}
impl<'t> Symbol<'t> {
/// The index of this symbol in the containing symbol stream.
#[inline]
pub fn index(&self) -> SymbolIndex {
self.index
}
/// Returns the kind of symbol identified by this Symbol.
#[inline]
pub fn raw_kind(&self) -> SymbolKind {
debug_assert!(self.data.len() >= 2);
self.data.pread_with(0, LE).unwrap_or_default()
}
/// Returns the raw bytes of this symbol record, including the symbol type and extra data, but
/// not including the preceding symbol length indicator.
#[inline]
pub fn raw_bytes(&self) -> &'t [u8] {
self.data
}
/// Parse the symbol into the `SymbolData` it contains.
#[inline]
pub fn parse(&self) -> Result<SymbolData<'t>> {
self.raw_bytes().pread_with(0, ())
}
/// Returns whether this symbol starts a scope.
///
/// If `true`, this symbol has a `parent` and an `end` field, which contains the offset of the
/// corrsponding end symbol.
pub fn starts_scope(&self) -> bool {
matches!(
self.raw_kind(),
S_GPROC16
| S_GPROC32
| S_GPROC32_ST
| S_GPROCMIPS
| S_GPROCMIPS_ST
| S_GPROCIA64
| S_GPROCIA64_ST
| S_LPROC16
| S_LPROC32
| S_LPROC32_ST
| S_LPROC32_DPC
| S_LPROCMIPS
| S_LPROCMIPS_ST
| S_LPROCIA64
| S_LPROCIA64_ST
| S_LPROC32_DPC_ID
| S_GPROC32_ID
| S_GPROCMIPS_ID
| S_GPROCIA64_ID
| S_BLOCK16
| S_BLOCK32
| S_BLOCK32_ST
| S_WITH16
| S_WITH32
| S_WITH32_ST
| S_THUNK16
| S_THUNK32
| S_THUNK32_ST
| S_SEPCODE
| S_GMANPROC
| S_GMANPROC_ST
| S_LMANPROC
| S_LMANPROC_ST
| S_INLINESITE
| S_INLINESITE2
)
}
/// Returns whether this symbol declares the end of a scope.
pub fn ends_scope(&self) -> bool {
matches!(self.raw_kind(), S_END | S_PROC_ID_END | S_INLINESITE_END)
}
}
impl<'t> fmt::Debug for Symbol<'t> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Symbol{{ kind: 0x{:x} [{} bytes] }}",
self.raw_kind(),
self.data.len()
)
}
}
fn parse_symbol_name<'t>(buf: &mut ParseBuffer<'t>, kind: SymbolKind) -> Result<RawString<'t>> {
if kind < S_ST_MAX {
// Pascal-style name
buf.parse_u8_pascal_string()
} else {
// NUL-terminated name
buf.parse_cstring()
}
}
fn parse_optional_name<'t>(
buf: &mut ParseBuffer<'t>,
kind: SymbolKind,
) -> Result<Option<RawString<'t>>> {
if kind < S_ST_MAX {
// ST variants do not specify a name
Ok(None)
} else {
// NUL-terminated name
buf.parse_cstring().map(Some)
}
}
fn parse_optional_index(buf: &mut ParseBuffer<'_>) -> Result<Option<SymbolIndex>> {
Ok(match buf.parse()? {
SymbolIndex(0) => None,
index => Some(index),
})
}
// data types are defined at:
// https://github.com/Microsoft/microsoft-pdb/blob/082c5290e5aff028ae84e43affa8be717aa7af73/include/cvinfo.h#L3038
// constants defined at:
// https://github.com/Microsoft/microsoft-pdb/blob/082c5290e5aff028ae84e43affa8be717aa7af73/include/cvinfo.h#L2735
// decoding reference:
// https://github.com/Microsoft/microsoft-pdb/blob/082c5290e5aff028ae84e43affa8be717aa7af73/cvdump/dumpsym7.cpp#L264
/// Information parsed from a [`Symbol`] record.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SymbolData<'t> {
/// End of a scope, such as a procedure.
ScopeEnd,
/// Name of the object file of this module.
ObjName(ObjNameSymbol<'t>),
/// A Register variable.
RegisterVariable(RegisterVariableSymbol<'t>),
/// A constant value.
Constant(ConstantSymbol<'t>),
/// A user defined type.
UserDefinedType(UserDefinedTypeSymbol<'t>),
/// A Register variable spanning multiple registers.
MultiRegisterVariable(MultiRegisterVariableSymbol<'t>),
/// Static data, such as a global variable.
Data(DataSymbol<'t>),
/// A public symbol with a mangled name.
Public(PublicSymbol<'t>),
/// A procedure, such as a function or method.
Procedure(ProcedureSymbol<'t>),
/// A thread local variable.
ThreadStorage(ThreadStorageSymbol<'t>),
/// Flags used to compile a module.
CompileFlags(CompileFlagsSymbol<'t>),
/// A using namespace directive.
UsingNamespace(UsingNamespaceSymbol<'t>),
/// Reference to a [`ProcedureSymbol`].
ProcedureReference(ProcedureReferenceSymbol<'t>),
/// Reference to an imported variable.
DataReference(DataReferenceSymbol<'t>),
/// Reference to an annotation.
AnnotationReference(AnnotationReferenceSymbol<'t>),
/// Trampoline thunk.
Trampoline(TrampolineSymbol),
/// An exported symbol.
Export(ExportSymbol<'t>),
/// A local symbol in optimized code.
Local(LocalSymbol<'t>),
/// Reference to build information.
BuildInfo(BuildInfoSymbol),
/// The callsite of an inlined function.
InlineSite(InlineSiteSymbol<'t>),
/// End of an inline callsite.
InlineSiteEnd,
/// End of a procedure.
ProcedureEnd,
/// A label.
Label(LabelSymbol<'t>),
/// A block.
Block(BlockSymbol<'t>),
/// Data allocated relative to a register.
RegisterRelative(RegisterRelativeSymbol<'t>),
/// A thunk.
Thunk(ThunkSymbol<'t>),
/// A block of separated code.
SeparatedCode(SeparatedCodeSymbol),
}
impl<'t> SymbolData<'t> {
/// Returns the name of this symbol if it has one.
pub fn name(&self) -> Option<RawString<'t>> {
match self {
Self::ScopeEnd => None,
Self::ObjName(data) => Some(data.name),
Self::RegisterVariable(_) => None,
Self::Constant(data) => Some(data.name),
Self::UserDefinedType(data) => Some(data.name),
Self::MultiRegisterVariable(_) => None,
Self::Data(data) => Some(data.name),
Self::Public(data) => Some(data.name),
Self::Procedure(data) => Some(data.name),
Self::ThreadStorage(data) => Some(data.name),
Self::CompileFlags(_) => None,
Self::UsingNamespace(data) => Some(data.name),
Self::ProcedureReference(data) => data.name,
Self::DataReference(data) => data.name,
Self::AnnotationReference(data) => Some(data.name),
Self::Trampoline(_) => None,
Self::Export(data) => Some(data.name),
Self::Local(data) => Some(data.name),
Self::InlineSite(_) => None,
Self::BuildInfo(_) => None,
Self::InlineSiteEnd => None,
Self::ProcedureEnd => None,
Self::Label(data) => Some(data.name),
Self::Block(data) => Some(data.name),
Self::RegisterRelative(data) => Some(data.name),
Self::Thunk(data) => Some(data.name),
Self::SeparatedCode(_) => None,
}
}
}
impl<'t> TryFromCtx<'t> for SymbolData<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], _ctx: ()) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let kind = buf.parse()?;
let symbol = match kind {
S_END => SymbolData::ScopeEnd,
S_OBJNAME | S_OBJNAME_ST => SymbolData::ObjName(buf.parse_with(kind)?),
S_REGISTER | S_REGISTER_ST => SymbolData::RegisterVariable(buf.parse_with(kind)?),
S_CONSTANT | S_CONSTANT_ST | S_MANCONSTANT => {
SymbolData::Constant(buf.parse_with(kind)?)
}
S_UDT | S_UDT_ST | S_COBOLUDT | S_COBOLUDT_ST => {
SymbolData::UserDefinedType(buf.parse_with(kind)?)
}
S_MANYREG | S_MANYREG_ST | S_MANYREG2 | S_MANYREG2_ST => {
SymbolData::MultiRegisterVariable(buf.parse_with(kind)?)
}
S_LDATA32 | S_LDATA32_ST | S_GDATA32 | S_GDATA32_ST | S_LMANDATA | S_LMANDATA_ST
| S_GMANDATA | S_GMANDATA_ST => SymbolData::Data(buf.parse_with(kind)?),
S_PUB32 | S_PUB32_ST => SymbolData::Public(buf.parse_with(kind)?),
S_LPROC32 | S_LPROC32_ST | S_GPROC32 | S_GPROC32_ST | S_LPROC32_ID | S_GPROC32_ID
| S_LPROC32_DPC | S_LPROC32_DPC_ID => SymbolData::Procedure(buf.parse_with(kind)?),
S_LTHREAD32 | S_LTHREAD32_ST | S_GTHREAD32 | S_GTHREAD32_ST => {
SymbolData::ThreadStorage(buf.parse_with(kind)?)
}
S_COMPILE2 | S_COMPILE2_ST | S_COMPILE3 => {
SymbolData::CompileFlags(buf.parse_with(kind)?)
}
S_UNAMESPACE | S_UNAMESPACE_ST => SymbolData::UsingNamespace(buf.parse_with(kind)?),
S_PROCREF | S_PROCREF_ST | S_LPROCREF | S_LPROCREF_ST => {
SymbolData::ProcedureReference(buf.parse_with(kind)?)
}
S_TRAMPOLINE => Self::Trampoline(buf.parse_with(kind)?),
S_DATAREF | S_DATAREF_ST => SymbolData::DataReference(buf.parse_with(kind)?),
S_ANNOTATIONREF => SymbolData::AnnotationReference(buf.parse_with(kind)?),
S_EXPORT => SymbolData::Export(buf.parse_with(kind)?),
S_LOCAL => SymbolData::Local(buf.parse_with(kind)?),
S_BUILDINFO => SymbolData::BuildInfo(buf.parse_with(kind)?),
S_INLINESITE | S_INLINESITE2 => SymbolData::InlineSite(buf.parse_with(kind)?),
S_INLINESITE_END => SymbolData::InlineSiteEnd,
S_PROC_ID_END => SymbolData::ProcedureEnd,
S_LABEL32 | S_LABEL32_ST => SymbolData::Label(buf.parse_with(kind)?),
S_BLOCK32 | S_BLOCK32_ST => SymbolData::Block(buf.parse_with(kind)?),
S_REGREL32 => SymbolData::RegisterRelative(buf.parse_with(kind)?),
S_THUNK32 | S_THUNK32_ST => SymbolData::Thunk(buf.parse_with(kind)?),
S_SEPCODE => SymbolData::SeparatedCode(buf.parse_with(kind)?),
other => return Err(Error::UnimplementedSymbolKind(other)),
};
Ok((symbol, buf.pos()))
}
}
/// A Register variable.
///
/// Symbol kind `S_REGISTER`, or `S_REGISTER_ST`
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RegisterVariableSymbol<'t> {
/// Identifier of the variable type.
pub type_index: TypeIndex,
/// The register this variable is stored in.
pub register: Register,
/// Name of the variable.
pub name: RawString<'t>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for RegisterVariableSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = RegisterVariableSymbol {
type_index: buf.parse()?,
register: buf.parse()?,
name: parse_symbol_name(&mut buf, kind)?,
};
Ok((symbol, buf.pos()))
}
}
/// A Register variable spanning multiple registers.
///
/// Symbol kind `S_MANYREG`, `S_MANYREG_ST`, `S_MANYREG2`, or `S_MANYREG2_ST`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MultiRegisterVariableSymbol<'t> {
/// Identifier of the variable type.
pub type_index: TypeIndex,
/// Most significant register first.
pub registers: Vec<(Register, RawString<'t>)>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for MultiRegisterVariableSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let type_index = buf.parse()?;
let count = match kind {
S_MANYREG2 | S_MANYREG2_ST => buf.parse::<u16>()?,
_ => u16::from(buf.parse::<u8>()?),
};
let mut registers = Vec::with_capacity(count as usize);
for _ in 0..count {
registers.push((buf.parse()?, parse_symbol_name(&mut buf, kind)?));
}
let symbol = MultiRegisterVariableSymbol {
type_index,
registers,
};
Ok((symbol, buf.pos()))
}
}
// CV_PUBSYMFLAGS_e
const CVPSF_CODE: u32 = 0x1;
const CVPSF_FUNCTION: u32 = 0x2;
const CVPSF_MANAGED: u32 = 0x4;
const CVPSF_MSIL: u32 = 0x8;
/// A public symbol with a mangled name.
///
/// Symbol kind `S_PUB32`, or `S_PUB32_ST`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PublicSymbol<'t> {
/// The public symbol refers to executable code.
pub code: bool,
/// The public symbol is a function.
pub function: bool,
/// The symbol is in managed code (native or IL).
pub managed: bool,
/// The symbol is managed IL code.
pub msil: bool,
/// Start offset of the symbol.
pub offset: PdbInternalSectionOffset,
/// Mangled name of the symbol.
pub name: RawString<'t>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for PublicSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let flags = buf.parse::<u32>()?;
let symbol = PublicSymbol {
code: flags & CVPSF_CODE != 0,
function: flags & CVPSF_FUNCTION != 0,
managed: flags & CVPSF_MANAGED != 0,
msil: flags & CVPSF_MSIL != 0,
offset: buf.parse()?,
name: parse_symbol_name(&mut buf, kind)?,
};
Ok((symbol, buf.pos()))
}
}
/// Static data, such as a global variable.
///
/// Symbol kinds:
/// - `S_LDATA32` and `S_LDATA32_ST` for local unmanaged data
/// - `S_GDATA32` and `S_GDATA32_ST` for global unmanaged data
/// - `S_LMANDATA32` and `S_LMANDATA32_ST` for local managed data
/// - `S_GMANDATA32` and `S_GMANDATA32_ST` for global managed data
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DataSymbol<'t> {
/// Whether this data is global or local.
pub global: bool,
/// Whether this data is managed or unmanaged.
pub managed: bool,
/// Type identifier of the type of data.
pub type_index: TypeIndex,
/// Code offset of the start of the data region.
pub offset: PdbInternalSectionOffset,
/// Name of the data variable.
pub name: RawString<'t>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for DataSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = DataSymbol {
global: matches!(kind, S_GDATA32 | S_GDATA32_ST | S_GMANDATA | S_GMANDATA_ST),
managed: matches!(
kind,
S_LMANDATA | S_LMANDATA_ST | S_GMANDATA | S_GMANDATA_ST
),
type_index: buf.parse()?,
offset: buf.parse()?,
name: parse_symbol_name(&mut buf, kind)?,
};
Ok((symbol, buf.pos()))
}
}
/// Reference to an imported procedure.
///
/// Symbol kind `S_PROCREF`, `S_PROCREF_ST`, `S_LPROCREF`, or `S_LPROCREF_ST`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ProcedureReferenceSymbol<'t> {
/// Whether the referenced procedure is global or local.
pub global: bool,
/// SUC of the name.
pub sum_name: u32,
/// Symbol index of the referenced [`ProcedureSymbol`].
///
/// Note that this symbol might be located in a different module.
pub symbol_index: SymbolIndex,
/// Index of the module in [`DebugInformation::modules`](crate::DebugInformation::modules)
/// containing the actual symbol.
pub module: Option<usize>,
/// Name of the procedure reference.
pub name: Option<RawString<'t>>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for ProcedureReferenceSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = ProcedureReferenceSymbol {
global: matches!(kind, S_PROCREF | S_PROCREF_ST),
sum_name: buf.parse()?,
symbol_index: buf.parse()?,
module: buf.parse::<u16>()?.checked_sub(1).map(usize::from),
name: parse_optional_name(&mut buf, kind)?,
};
Ok((symbol, buf.pos()))
}
}
/// Reference to an imported variable.
///
/// Symbol kind `S_DATAREF`, or `S_DATAREF_ST`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DataReferenceSymbol<'t> {
/// SUC of the name.
pub sum_name: u32,
/// Symbol index of the referenced [`DataSymbol`].
///
/// Note that this symbol might be located in a different module.
pub symbol_index: SymbolIndex,
/// Index of the module in [`DebugInformation::modules`](crate::DebugInformation::modules)
/// containing the actual symbol.
pub module: Option<usize>,
/// Name of the data reference.
pub name: Option<RawString<'t>>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for DataReferenceSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = DataReferenceSymbol {
sum_name: buf.parse()?,
symbol_index: buf.parse()?,
module: buf.parse::<u16>()?.checked_sub(1).map(usize::from),
name: parse_optional_name(&mut buf, kind)?,
};
Ok((symbol, buf.pos()))
}
}
/// Reference to an annotation.
///
/// Symbol kind `S_ANNOTATIONREF`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AnnotationReferenceSymbol<'t> {
/// SUC of the name.
pub sum_name: u32,
/// Symbol index of the referenced symbol.
///
/// Note that this symbol might be located in a different module.
pub symbol_index: SymbolIndex,
/// Index of the module in [`DebugInformation::modules`](crate::DebugInformation::modules)
/// containing the actual symbol.
pub module: Option<usize>,
/// Name of the annotation reference.
pub name: RawString<'t>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for AnnotationReferenceSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = AnnotationReferenceSymbol {
sum_name: buf.parse()?,
symbol_index: buf.parse()?,
module: buf.parse::<u16>()?.checked_sub(1).map(usize::from),
name: parse_symbol_name(&mut buf, kind)?,
};
Ok((symbol, buf.pos()))
}
}
/// Subtype of [`TrampolineSymbol`].
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TrampolineType {
/// An incremental thunk.
Incremental,
/// Branch island thunk.
BranchIsland,
/// An unknown thunk type.
Unknown,
}
/// Trampoline thunk.
///
/// Symbol kind `S_TRAMPOLINE`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TrampolineSymbol {
/// Trampoline symbol subtype.
pub tramp_type: TrampolineType,
/// Code size of the thunk.
pub size: u16,
/// Code offset of the thunk.
pub thunk: PdbInternalSectionOffset,
/// Code offset of the thunk target.
pub target: PdbInternalSectionOffset,
}
impl TryFromCtx<'_, SymbolKind> for TrampolineSymbol {
type Error = Error;
fn try_from_ctx(this: &'_ [u8], _kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let tramp_type = match buf.parse::<u16>()? {
0x00 => TrampolineType::Incremental,
0x01 => TrampolineType::BranchIsland,
_ => TrampolineType::Unknown,
};
let size = buf.parse()?;
let thunk_offset = buf.parse()?;
let target_offset = buf.parse()?;
let thunk_section = buf.parse()?;
let target_section = buf.parse()?;
let symbol = Self {
tramp_type,
size,
thunk: PdbInternalSectionOffset::new(thunk_section, thunk_offset),
target: PdbInternalSectionOffset::new(target_section, target_offset),
};
Ok((symbol, buf.pos()))
}
}
/// A constant value.
///
/// Symbol kind `S_CONSTANT`, or `S_CONSTANT_ST`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ConstantSymbol<'t> {
/// Whether this constant has metadata type information.
pub managed: bool,
/// The type of this constant or metadata token.
pub type_index: TypeIndex,
/// The value of this constant.
pub value: Variant,
/// Name of the constant.
pub name: RawString<'t>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for ConstantSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = ConstantSymbol {
managed: kind == S_MANCONSTANT,
type_index: buf.parse()?,
value: buf.parse()?,
name: parse_symbol_name(&mut buf, kind)?,
};
Ok((symbol, buf.pos()))
}
}
/// A user defined type.
///
/// Symbol kind `S_UDT`, or `S_UDT_ST`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct UserDefinedTypeSymbol<'t> {
/// Identifier of the type.
pub type_index: TypeIndex,
/// Name of the type.
pub name: RawString<'t>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for UserDefinedTypeSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = UserDefinedTypeSymbol {
type_index: buf.parse()?,
name: parse_symbol_name(&mut buf, kind)?,
};
Ok((symbol, buf.pos()))
}
}
/// A thread local variable.
///
/// Symbol kinds:
/// - `S_LTHREAD32`, `S_LTHREAD32_ST` for local thread storage.
/// - `S_GTHREAD32`, or `S_GTHREAD32_ST` for global thread storage.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ThreadStorageSymbol<'t> {
/// Whether this is a global or local thread storage.
pub global: bool,
/// Identifier of the stored type.
pub type_index: TypeIndex,
/// Code offset of the thread local.
pub offset: PdbInternalSectionOffset,
/// Name of the thread local.
pub name: RawString<'t>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for ThreadStorageSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = ThreadStorageSymbol {
global: matches!(kind, S_GTHREAD32 | S_GTHREAD32_ST),
type_index: buf.parse()?,
offset: buf.parse()?,
name: parse_symbol_name(&mut buf, kind)?,
};
Ok((symbol, buf.pos()))
}
}
// CV_PROCFLAGS:
const CV_PFLAG_NOFPO: u8 = 0x01;
const CV_PFLAG_INT: u8 = 0x02;
const CV_PFLAG_FAR: u8 = 0x04;
const CV_PFLAG_NEVER: u8 = 0x08;
const CV_PFLAG_NOTREACHED: u8 = 0x10;
const CV_PFLAG_CUST_CALL: u8 = 0x20;
const CV_PFLAG_NOINLINE: u8 = 0x40;
const CV_PFLAG_OPTDBGINFO: u8 = 0x80;
/// Flags of a [`ProcedureSymbol`].
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ProcedureFlags {
/// Frame pointer is present (not omitted).
pub nofpo: bool,
/// Interrupt return.
pub int: bool,
/// Far return.
pub far: bool,
/// Procedure does not return.
pub never: bool,
/// Procedure is never called.
pub notreached: bool,
/// Custom calling convention.
pub cust_call: bool,
/// Marked as `noinline`.
pub noinline: bool,
/// Debug information for optimized code is present.
pub optdbginfo: bool,
}
impl<'t> TryFromCtx<'t, Endian> for ProcedureFlags {
type Error = scroll::Error;
fn try_from_ctx(this: &'t [u8], le: Endian) -> scroll::Result<(Self, usize)> {
let (value, size) = u8::try_from_ctx(this, le)?;
let flags = Self {
nofpo: value & CV_PFLAG_NOFPO != 0,
int: value & CV_PFLAG_INT != 0,
far: value & CV_PFLAG_FAR != 0,
never: value & CV_PFLAG_NEVER != 0,
notreached: value & CV_PFLAG_NOTREACHED != 0,
cust_call: value & CV_PFLAG_CUST_CALL != 0,
noinline: value & CV_PFLAG_NOINLINE != 0,
optdbginfo: value & CV_PFLAG_OPTDBGINFO != 0,
};
Ok((flags, size))
}
}
/// A procedure, such as a function or method.
///
/// Symbol kinds:
/// - `S_GPROC32`, `S_GPROC32_ST` for global procedures
/// - `S_LPROC32`, `S_LPROC32_ST` for local procedures
/// - `S_LPROC32_DPC` for DPC procedures
/// - `S_GPROC32_ID`, `S_LPROC32_ID`, `S_LPROC32_DPC_ID` for procedures referencing types from the
/// ID stream rather than the Type stream.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ProcedureSymbol<'t> {
/// Whether this is a global or local procedure.
pub global: bool,
/// Indicates Deferred Procedure Calls (DPC).
pub dpc: bool,
/// The parent scope that this procedure is nested in.
pub parent: Option<SymbolIndex>,
/// The end symbol of this procedure.
pub end: SymbolIndex,
/// The next procedure symbol.
pub next: Option<SymbolIndex>,
/// The length of the code block covered by this procedure.
pub len: u32,
/// Start offset of the procedure's body code, which marks the end of the prologue.
pub dbg_start_offset: u32,
/// End offset of the procedure's body code, which marks the start of the epilogue.
pub dbg_end_offset: u32,
/// Identifier of the procedure type.
///
/// The type contains the complete signature, including parameters, modifiers and the return
/// type.
pub type_index: TypeIndex,
/// Code offset of the start of this procedure.
pub offset: PdbInternalSectionOffset,
/// Detailed flags of this procedure.
pub flags: ProcedureFlags,
/// The full, demangled name of the procedure.
pub name: RawString<'t>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for ProcedureSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = ProcedureSymbol {
global: matches!(kind, S_GPROC32 | S_GPROC32_ST | S_GPROC32_ID),
dpc: matches!(kind, S_LPROC32_DPC | S_LPROC32_DPC_ID),
parent: parse_optional_index(&mut buf)?,
end: buf.parse()?,
next: parse_optional_index(&mut buf)?,
len: buf.parse()?,
dbg_start_offset: buf.parse()?,
dbg_end_offset: buf.parse()?,
type_index: buf.parse()?,
offset: buf.parse()?,
flags: buf.parse()?,
name: parse_symbol_name(&mut buf, kind)?,
};
Ok((symbol, buf.pos()))
}
}
/// The callsite of an inlined function.
///
/// Symbol kind `S_INLINESITE`, or `S_INLINESITE2`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct InlineSiteSymbol<'t> {
/// Index of the parent function.
///
/// This might either be a [`ProcedureSymbol`] or another `InlineSiteSymbol`.
pub parent: Option<SymbolIndex>,
/// The end symbol of this callsite.
pub end: SymbolIndex,
/// Identifier of the type describing the inline function.
pub inlinee: IdIndex,
/// The total number of invocations of the inline function.
pub invocations: Option<u32>,
/// Binary annotations containing the line program of this call site.
pub annotations: BinaryAnnotations<'t>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for InlineSiteSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = InlineSiteSymbol {
parent: parse_optional_index(&mut buf)?,
end: buf.parse()?,
inlinee: buf.parse()?,
invocations: match kind {
S_INLINESITE2 => Some(buf.parse()?),
_ => None,
},
annotations: BinaryAnnotations::new(buf.take(buf.len())?),
};
Ok((symbol, buf.pos()))
}
}
/// Reference to build information.
///
/// Symbol kind `S_BUILDINFO`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BuildInfoSymbol {
/// Index of the build information record.
pub id: IdIndex,
}
impl<'t> TryFromCtx<'t, SymbolKind> for BuildInfoSymbol {
type Error = Error;
fn try_from_ctx(this: &'t [u8], _kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = Self { id: buf.parse()? };
Ok((symbol, buf.pos()))
}
}
/// Name of the object file of this module.
///
/// Symbol kind `S_OBJNAME`, or `S_OBJNAME_ST`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ObjNameSymbol<'t> {
/// Signature.
pub signature: u32,
/// Path to the object file.
pub name: RawString<'t>,
}
impl<'t> TryFromCtx<'t, SymbolKind> for ObjNameSymbol<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let symbol = ObjNameSymbol {
signature: buf.parse()?,
name: parse_symbol_name(&mut buf, kind)?,
};
Ok((symbol, buf.pos()))
}
}
/// A version number refered to by `CompileFlagsSymbol`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CompilerVersion {
/// The major version number.
pub major: u16,
/// The minor version number.
pub minor: u16,
/// The build (patch) version number.
pub build: u16,
/// The QFE (quick fix engineering) number.
pub qfe: Option<u16>,
}
impl<'t> TryFromCtx<'t, bool> for CompilerVersion {
type Error = Error;
fn try_from_ctx(this: &'t [u8], has_qfe: bool) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let version = Self {
major: buf.parse()?,
minor: buf.parse()?,
build: buf.parse()?,
qfe: if has_qfe { Some(buf.parse()?) } else { None },
};
Ok((version, buf.pos()))
}
}
/// Compile flags declared in `CompileFlagsSymbol`.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CompileFlags {
/// Compiled for edit and continue.
pub edit_and_continue: bool,
/// Compiled without debugging info.
pub no_debug_info: bool,
/// Compiled with `LTCG`.
pub link_time_codegen: bool,
/// Compiled with `/bzalign`.
pub no_data_align: bool,
/// Managed code or data is present.
pub managed: bool,
/// Compiled with `/GS`.
pub security_checks: bool,
/// Compiled with `/hotpatch`.
pub hot_patch: bool,
/// Compiled with `CvtCIL`.
pub cvtcil: bool,
/// This is a MSIL .NET Module.
pub msil_module: bool,
/// Compiled with `/sdl`.
pub sdl: bool,
/// Compiled with `/ltcg:pgo` or `pgo:`.
pub pgo: bool,
/// This is a .exp module.
pub exp_module: bool,
}
impl<'t> TryFromCtx<'t, SymbolKind> for CompileFlags {
type Error = Error;
fn try_from_ctx(this: &'t [u8], kind: SymbolKind) -> Result<(Self, usize)> {
let is_compile3 = kind == S_COMPILE3;
let raw = this.pread_with::<u16>(0, LE)?;
this.pread::<u8>(2)?; // unused
let flags = Self {
edit_and_continue: raw & 1 != 0,