use super::{parse, utf8};
use crate::pdb::error::Error;
use crate::pdb::ir::{
Alignment, CallingConvention, Database, Field, PointerKind, PrimitiveKind, Record, RecordKind,
Signature, Type, TypeId, same_record,
};
use ms_pdb::types::{TypeData, TypeIndex};
use std::collections::BTreeMap;
struct ForwardRecord {
name: String,
unique_name: Option<String>,
}
pub(crate) fn normalize_types(
stream: &ms_pdb::tpi::TypeStream<Vec<u8>>,
) -> Result<Database, Error> {
let expected = usize::try_from(stream.num_types()).map_err(|_| Error::Parse {
operation: "read TPI",
detail: "type count does not fit usize".into(),
})?;
let mut iter = stream.iter_type_records();
let mut db = Database::default();
let mut forwards = BTreeMap::new();
for offset in 0..expected {
let record = iter.next().ok_or_else(|| Error::Parse {
operation: "read TPI",
detail: "truncated type record stream".into(),
})?;
let offset = u32::try_from(offset).map_err(|_| Error::Parse {
operation: "read TPI",
detail: "type index overflow".into(),
})?;
let index = TypeId(
stream
.type_index_begin()
.0
.checked_add(offset)
.ok_or_else(|| Error::Parse {
operation: "read TPI",
detail: "type index overflow".into(),
})?,
);
let kind = record.kind;
normalize_record(
stream,
index,
kind,
record.parse().map_err(|_| Error::Parse {
operation: "parse TPI record",
detail: format!("type index 0x{:x}", index.0),
})?,
&mut db,
&mut forwards,
)?;
}
if !iter.rest().is_empty() {
return Err(Error::Parse {
operation: "read TPI",
detail: "type record count does not consume stream".into(),
});
}
resolve_forwards(&mut db, forwards);
validate_references(stream, &mut db)?;
Ok(db)
}
pub(crate) fn validate_references(
stream: &ms_pdb::tpi::TypeStream<Vec<u8>>,
db: &mut Database,
) -> Result<(), Error> {
let mut references = Vec::new();
for record in db.records.values().flatten() {
references.extend(record.fields.iter().map(|field| (record.id, field.ty)));
}
for (owner, ty) in &db.types {
match ty {
Type::Pointer { target, .. } | Type::Modifier { target, .. } => {
references.push((*owner, *target))
}
Type::Array { element, index, .. } => {
references.push((*owner, *element));
references.push((*owner, *index));
}
Type::Bitfield { storage, .. } => references.push((*owner, *storage)),
Type::Procedure(signature) => {
references.push((*owner, signature.return_type));
references.extend(
signature
.arguments
.iter()
.copied()
.map(|argument| (*owner, argument)),
);
}
Type::MemberFunction {
signature,
class,
this,
..
} => {
references.push((*owner, *class));
references.push((*owner, *this));
references.push((*owner, signature.return_type));
references.extend(
signature
.arguments
.iter()
.copied()
.map(|argument| (*owner, argument)),
);
}
Type::Absent | Type::Void | Type::Primitive { .. } | Type::Record(_) => {}
}
}
for (owner, reference) in references {
if stream.is_primitive(TypeIndex(reference.0)) {
add_primitive(db, reference)?;
} else if reference.0 < stream.type_index_begin().0
|| reference.0 >= stream.type_index_end().0
|| !db.types.contains_key(&reference)
{
return Err(Error::InvalidReference {
owner: owner.0,
reference: reference.0,
detail: "out of range or unsupported record",
});
}
}
let functions: Vec<_> = db
.functions
.values()
.flatten()
.map(|function| (function.name.clone(), function.signature))
.collect();
for (name, reference) in functions {
if stream.is_primitive(TypeIndex(reference.0)) {
add_primitive(db, reference)?;
} else if reference.0 < stream.type_index_begin().0
|| reference.0 >= stream.type_index_end().0
|| !db.types.contains_key(&reference)
{
return Err(Error::InvalidSymbolType {
kind: "function",
name,
reference: reference.0,
detail: "out of range or unsupported record",
});
}
match db.types.get(&reference) {
Some(Type::Procedure(_) | Type::MemberFunction { .. }) => {}
Some(_) => {
return Err(Error::InvalidSymbolType {
kind: "function",
name,
reference: reference.0,
detail: "function symbol does not reference a procedure signature",
});
}
None => {
return Err(Error::InvalidSymbolType {
kind: "function",
name,
reference: reference.0,
detail: "out of range or unsupported record",
});
}
}
}
let globals: Vec<_> = db
.globals
.values()
.flatten()
.map(|global| (global.name.clone(), global.ty))
.collect();
for (name, reference) in globals {
if stream.is_primitive(TypeIndex(reference.0)) {
add_primitive(db, reference)?;
} else if reference.0 < stream.type_index_begin().0
|| reference.0 >= stream.type_index_end().0
|| !db.types.contains_key(&reference)
{
return Err(Error::InvalidSymbolType {
kind: "global",
name,
reference: reference.0,
detail: "out of range or unsupported record",
});
}
}
Ok(())
}
fn add_primitive(db: &mut Database, reference: TypeId) -> Result<(), Error> {
let value = primitive(reference.0).ok_or(Error::UnsupportedPrimitive { index: reference.0 })?;
if let Type::Pointer { target, .. } = &value {
let target_value =
primitive(target.0).ok_or(Error::UnsupportedPrimitive { index: target.0 })?;
db.types.entry(*target).or_insert(target_value);
}
db.types.entry(reference).or_insert(value);
Ok(())
}
pub(crate) fn primitive(index: u32) -> Option<Type> {
if index == 0x0000 {
return Some(Type::Absent);
}
if index == 0x0003 {
return Some(Type::Void);
}
if index >> 8 == 0 {
return scalar_primitive(index).map(|(kind, size, alignment)| Type::Primitive {
kind,
size,
alignment: Alignment::Known(alignment),
});
}
if index >> 8 == 0x06 {
let target = TypeId(index & 0xff);
primitive(target.0)?;
return Some(Type::Pointer {
target,
size: 8,
alignment: Alignment::Known(8),
kind: PointerKind::Pointer,
is_const: false,
is_volatile: false,
is_unaligned: false,
is_restrict: false,
});
}
None
}
fn scalar_primitive(index: u32) -> Option<(PrimitiveKind, u64, u64)> {
let (kind, size) = match index {
0x08 | 0x11 | 0x12 | 0x13 | 0x68 | 0x72 | 0x74 | 0x76 => (
PrimitiveKind::Signed,
match index {
0x11 | 0x72 => 2,
0x12 | 0x08 | 0x74 => 4,
0x13 | 0x76 => 8,
_ => 1,
},
),
0x20 | 0x21 | 0x22 | 0x23 | 0x69 | 0x73 | 0x75 | 0x77 => (
PrimitiveKind::Unsigned,
match index {
0x21 | 0x73 => 2,
0x22 | 0x75 => 4,
0x23 | 0x77 => 8,
_ => 1,
},
),
0x30 | 0x31 | 0x32 | 0x33 | 0x62 => (
PrimitiveKind::Boolean,
match index {
0x31 => 2,
0x32 | 0x62 => 4,
0x33 => 8,
_ => 1,
},
),
0x40 => (PrimitiveKind::Float, 4),
0x41 => (PrimitiveKind::Float, 8),
0x10 | 0x70 | 0x71 | 0x7a | 0x7b | 0x7c => (
PrimitiveKind::Character,
match index {
0x71 | 0x7a => 2,
0x7b => 4,
_ => 1,
},
),
_ => return None,
};
Some((kind, size, size))
}
fn normalize_record(
stream: &ms_pdb::tpi::TypeStream<Vec<u8>>,
id: TypeId,
leaf: ms_pdb::types::Leaf,
data: TypeData<'_>,
db: &mut Database,
forwards: &mut BTreeMap<TypeId, ForwardRecord>,
) -> Result<(), Error> {
match data {
TypeData::Struct(value) => {
if value.fixed.property.get().fwdref() {
forwards.insert(
id,
ForwardRecord {
name: utf8(value.name, "read UDT name")?,
unique_name: optional_utf8(value.unique_name, "read unique UDT name")?,
},
);
return Ok(());
}
let kind = match leaf {
ms_pdb::types::Leaf::LF_CLASS => RecordKind::Class,
ms_pdb::types::Leaf::LF_INTERFACE => RecordKind::Interface,
ms_pdb::types::Leaf::LF_STRUCTURE => RecordKind::Struct,
_ => {
return Err(Error::Parse {
operation: "parse TPI record",
detail: "structure has incompatible leaf kind".into(),
});
}
};
let size = u64::try_from(value.length).map_err(|_| Error::Parse {
operation: "read UDT size",
detail: format!("type index 0x{:x}", id.0),
})?;
let name = utf8(value.name, "read UDT name")?;
let fields = fields(
stream,
id,
value.fixed.field_list.get(),
usize::from(value.fixed.num_elements.get()),
)?;
db.add_record(Record {
id,
name,
unique_name: optional_utf8(value.unique_name, "read unique UDT name")?,
kind,
size,
alignment: Alignment::Unavailable,
packed: value.fixed.property.get().packed(),
fields,
});
db.add_type(id, Type::Record(id));
}
TypeData::Union(value) => {
if value.fixed.property.get().fwdref() {
forwards.insert(
id,
ForwardRecord {
name: utf8(value.name, "read union name")?,
unique_name: optional_utf8(value.unique_name, "read unique union name")?,
},
);
return Ok(());
}
let size = u64::try_from(value.length).map_err(|_| Error::Parse {
operation: "read union size",
detail: format!("type index 0x{:x}", id.0),
})?;
let name = utf8(value.name, "read union name")?;
let fields = fields(
stream,
id,
value.fixed.fields.get(),
usize::from(value.fixed.count.get()),
)?;
db.add_record(Record {
id,
name,
unique_name: optional_utf8(value.unique_name, "read unique union name")?,
kind: RecordKind::Union,
size,
alignment: Alignment::Unavailable,
packed: value.fixed.property.get().packed(),
fields,
});
db.add_type(id, Type::Record(id));
}
TypeData::Pointer(value) => {
let size = pointer_size(value.fixed.attr().size())?;
let attributes = value.fixed.attr();
if attributes.pointer_kind() != 12 {
return Err(Error::Parse {
operation: "read pointer type",
detail: format!(
"AMD64 pointer has unsupported kind {}",
attributes.pointer_kind()
),
});
}
let kind = match attributes.mode() {
0 => PointerKind::Pointer,
1 => PointerKind::LValueReference,
4 => PointerKind::RValueReference,
mode => {
return Err(Error::Parse {
operation: "read pointer type",
detail: format!("unsupported pointer mode {mode}"),
});
}
};
db.add_type(
id,
Type::Pointer {
target: type_id(value.fixed.ty.get()),
size,
alignment: Alignment::Known(size),
kind,
is_const: attributes.r#const(),
is_volatile: attributes.volatile(),
is_unaligned: attributes.unaligned(),
is_restrict: attributes.restrict(),
},
);
}
TypeData::Modifier(value) => db.add_type(
id,
Type::Modifier {
target: type_id(value.underlying_type.get()),
is_const: value.is_const(),
is_volatile: value.is_volatile(),
is_unaligned: value.is_unaligned(),
},
),
TypeData::Array(value) => db.add_type(
id,
Type::Array {
element: type_id(value.fixed.element_type.get()),
index: type_id(value.fixed.index_type.get()),
size: u64::try_from(value.len).map_err(|_| Error::Parse {
operation: "read array size",
detail: format!("type index 0x{:x}", id.0),
})?,
},
),
TypeData::Bitfield(value) => db.add_type(
id,
Type::Bitfield {
storage: type_id(value.underlying_type.get()),
position: value.position,
length: value.length,
},
),
TypeData::Proc(value) => db.add_type(
id,
Type::Procedure(signature(
stream,
value.return_value.get(),
value.call,
value.num_params.get(),
value.arg_list.get(),
)?),
),
TypeData::MemberFunc(value) => db.add_type(
id,
Type::MemberFunction {
signature: signature(
stream,
value.return_value.get(),
value.call,
value.num_params.get(),
value.arg_list.get(),
)?,
class: type_id(value.class.get()),
this: type_id(value.this.get()),
this_adjust: value.this_adjust.get(),
},
),
_ => {}
}
Ok(())
}
fn fields(
stream: &ms_pdb::tpi::TypeStream<Vec<u8>>,
owner: TypeId,
list: TypeIndex,
expected: usize,
) -> Result<Vec<Field>, Error> {
if list.0 == 0 {
return if expected == 0 {
Ok(Vec::new())
} else {
Err(Error::InvalidReference {
owner: owner.0,
reference: 0,
detail: "non-empty UDT has no field list",
})
};
}
let mut result = Vec::new();
let mut seen = std::collections::BTreeSet::new();
let mut next = Some(list);
let mut decoded = 0usize;
while let Some(index) = next.take() {
if !seen.insert(index.0) {
return Err(Error::Cycle { type_id: index.0 });
}
let record = stream.record(index).map_err(parse("read field list"))?;
let TypeData::FieldList(list) = record.parse().map_err(parse("parse field list"))? else {
return Err(Error::InvalidReference {
owner: index.0,
reference: index.0,
detail: "field-list reference has wrong kind",
});
};
let mut parser = ms_pdb::codeview::parser::Parser::new(list.bytes);
while !parser.is_empty() {
skip_field_padding(&mut parser)?;
if parser.is_empty() {
break;
}
let field = ms_pdb::types::fields::Field::parse(&mut parser)
.map_err(parse("parse field list"))?;
match field {
ms_pdb::types::fields::Field::Member(member) => {
decoded = decoded.checked_add(1).ok_or(Error::Parse {
operation: "read field list",
detail: "element count overflow".into(),
})?;
result.push(Field {
name: utf8(member.name, "read field name")?,
ty: type_id(member.ty),
offset: u64::try_from(member.offset).map_err(|_| Error::Parse {
operation: "read field offset",
detail: "negative or non-integer offset".into(),
})?,
});
}
ms_pdb::types::fields::Field::Index(index) => {
if next.replace(index).is_some() {
return Err(Error::Parse {
operation: "read field list",
detail: "multiple continuation records".into(),
});
}
skip_field_padding(&mut parser)?;
if !parser.is_empty() {
return Err(Error::Parse {
operation: "read field list",
detail: "continuation is not terminal".into(),
});
}
}
ms_pdb::types::fields::Field::BaseClass(_)
| ms_pdb::types::fields::Field::DirectVirtualBaseClass(_)
| ms_pdb::types::fields::Field::IndirectVirtualBaseClass(_) => {
return Err(Error::UnsupportedLayout {
type_id: owner.0,
detail: "base classes",
});
}
_ => {
decoded = decoded.checked_add(1).ok_or(Error::Parse {
operation: "read field list",
detail: "element count overflow".into(),
})?;
}
}
}
}
if decoded != expected {
return Err(Error::Parse {
operation: "read field list",
detail: format!("UDT declares {expected} elements but decoded {decoded}"),
});
}
Ok(result)
}
fn skip_field_padding(parser: &mut ms_pdb::codeview::parser::Parser<'_>) -> Result<(), Error> {
let rest = parser.peek_rest();
let Some(&first) = rest.first() else {
return Ok(());
};
if first < 0xf0 {
return Ok(());
}
let count = usize::from(first - 0xf0);
if !(1..=3).contains(&count)
|| rest.len() < count
|| rest[..count]
.iter()
.enumerate()
.any(|(offset, byte)| *byte != first - offset as u8)
{
return Err(Error::Parse {
operation: "parse field list",
detail: "invalid CodeView field-list padding".into(),
});
}
parser.skip(count).map_err(parse("parse field list"))
}
fn signature(
stream: &ms_pdb::tpi::TypeStream<Vec<u8>>,
return_type: TypeIndex,
call: u8,
count: u16,
args: TypeIndex,
) -> Result<Signature, Error> {
let record = stream.record(args).map_err(parse("read argument list"))?;
let TypeData::ArgList(args) = record.parse().map_err(|_| Error::Parse {
operation: "parse argument list",
detail: "invalid argument list".into(),
})?
else {
return Err(Error::Parse {
operation: "read argument list",
detail: "argument-list reference has wrong kind".into(),
});
};
let declared = usize::from(count);
let variadic = args.args.len() == declared + 1
&& args
.args
.last()
.is_some_and(|argument| argument.get().0 == 0);
if args.args.len() != declared && !variadic {
return Err(Error::Parse {
operation: "read procedure",
detail: "argument count disagrees with argument list".into(),
});
}
Ok(Signature {
return_type: type_id(return_type),
arguments: args
.args
.iter()
.take(declared)
.copied()
.map(|arg| type_id(arg.get()))
.collect(),
calling_convention: calling_convention(call),
variadic,
})
}
fn type_id(index: TypeIndex) -> TypeId {
TypeId(index.0)
}
fn optional_utf8(
value: Option<&ms_pdb::BStr>,
operation: &'static str,
) -> Result<Option<String>, Error> {
value.map(|name| utf8(name, operation)).transpose()
}
fn resolve_forwards(db: &mut Database, forwards: BTreeMap<TypeId, ForwardRecord>) {
for (forward_id, forward) in forwards {
let mut candidates: Vec<_> = db
.records
.values()
.flatten()
.filter(|record| {
if let Some(unique_name) = &forward.unique_name {
record.unique_name.as_ref() == Some(unique_name)
} else {
record.name == forward.name
}
})
.collect();
candidates.sort_by_key(|record| record.id);
let Some(first) = candidates.first() else {
continue;
};
if candidates
.iter()
.all(|candidate| same_record(first, candidate))
{
db.add_type(forward_id, Type::Record(first.id));
}
}
}
fn pointer_size(encoded: u32) -> Result<u64, Error> {
match encoded {
8 => Ok(8),
other => Err(Error::Parse {
operation: "read pointer size",
detail: format!("AMD64 pointer has unsupported encoded size {other}"),
}),
}
}
fn calling_convention(value: u8) -> CallingConvention {
match value {
0 => CallingConvention::NearC,
1 => CallingConvention::FarC,
2 => CallingConvention::NearPascal,
4 => CallingConvention::NearFast,
7 => CallingConvention::NearStd,
9 => CallingConvention::NearSys,
11 => CallingConvention::ThisCall,
22 => CallingConvention::ClrCall,
other => CallingConvention::Unknown(other),
}
}