use std::borrow::Cow;
use std::fmt;
use std::result;
use crate::common::*;
use crate::msf::*;
use crate::FallibleIterator;
#[derive(Debug)]
pub struct DebugInformation<'s> {
stream: Stream<'s>,
header: DBIHeader,
header_len: usize,
}
impl<'s> DebugInformation<'s> {
pub(crate) fn parse(stream: Stream<'s>) -> Result<Self> {
let mut buf = stream.parse_buffer();
let header = DBIHeader::parse_buf(&mut buf)?;
let header_len = buf.pos();
Ok(DebugInformation {
stream,
header,
header_len,
})
}
pub(crate) fn header(&self) -> DBIHeader {
self.header
}
pub fn machine_type(&self) -> Result<MachineType> {
Ok(self.header.machine_type.into())
}
pub fn age(&self) -> Option<u32> {
match self.header.age {
0 => None,
age => Some(age),
}
}
pub fn modules(&self) -> Result<ModuleIter<'_>> {
let mut buf = self.stream.parse_buffer();
buf.take(self.header_len)?;
let modules_buf = buf.take(self.header.module_list_size as usize)?;
Ok(ModuleIter {
buf: modules_buf.into(),
})
}
pub fn section_contributions(&self) -> Result<DBISectionContributionIter<'_>> {
let mut buf = self.stream.parse_buffer();
buf.take(self.header_len + self.header.module_list_size as usize)?;
let contributions_buf = buf.take(self.header.section_contribution_size as usize)?;
DBISectionContributionIter::parse(contributions_buf.into())
}
}
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
#[allow(missing_docs)]
pub enum HeaderVersion {
V41,
V50,
V60,
V70,
V110,
OtherValue(u32),
}
impl From<u32> for HeaderVersion {
#[allow(clippy::inconsistent_digit_grouping)]
fn from(v: u32) -> Self {
match v {
93_08_03 => HeaderVersion::V41,
1996_03_07 => HeaderVersion::V50,
1997_06_06 => HeaderVersion::V60,
1999_09_03 => HeaderVersion::V70,
2009_12_01 => HeaderVersion::V110,
_ => HeaderVersion::OtherValue(v),
}
}
}
#[derive(Debug, Copy, Clone)]
pub(crate) struct DBIHeader {
pub signature: u32,
pub version: HeaderVersion,
pub age: u32,
pub gs_symbols_stream: StreamIndex,
pub internal_version: u16,
pub ps_symbols_stream: StreamIndex,
pub pdb_dll_build_version: u16,
pub symbol_records_stream: StreamIndex,
pub pdb_dll_rbld_version: u16,
pub module_list_size: u32,
pub section_contribution_size: u32,
pub section_map_size: u32,
pub file_info_size: u32,
pub type_server_map_size: u32,
pub mfc_type_server_index: u32,
pub debug_header_size: u32,
pub ec_substream_size: u32,
pub flags: u16,
pub machine_type: u16,
pub reserved: u32,
}
impl DBIHeader {
pub fn parse(stream: Stream<'_>) -> Result<Self> {
Self::parse_buf(&mut stream.parse_buffer())
}
fn parse_buf(buf: &mut ParseBuffer<'_>) -> Result<Self> {
let header = DBIHeader {
signature: buf.parse_u32()?,
version: From::from(buf.parse_u32()?),
age: buf.parse_u32()?,
gs_symbols_stream: buf.parse()?,
internal_version: buf.parse_u16()?,
ps_symbols_stream: buf.parse()?,
pdb_dll_build_version: buf.parse_u16()?,
symbol_records_stream: buf.parse()?,
pdb_dll_rbld_version: buf.parse_u16()?,
module_list_size: buf.parse_u32()?,
section_contribution_size: buf.parse_u32()?,
section_map_size: buf.parse_u32()?,
file_info_size: buf.parse_u32()?,
type_server_map_size: buf.parse_u32()?,
mfc_type_server_index: buf.parse_u32()?,
debug_header_size: buf.parse_u32()?,
ec_substream_size: buf.parse_u32()?,
flags: buf.parse_u16()?,
machine_type: buf.parse_u16()?,
reserved: buf.parse_u32()?,
};
if header.signature != u32::max_value() {
return Err(Error::UnimplementedFeature("ancient DBI header"));
}
Ok(header)
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MachineType {
Unknown = 0x0,
Am33 = 0x13,
Amd64 = 0x8664,
Arm = 0x1C0,
Arm64 = 0xAA64,
ArmNT = 0x1C4,
Ebc = 0xEBC,
X86 = 0x14C,
Ia64 = 0x200,
M32R = 0x9041,
Mips16 = 0x266,
MipsFpu = 0x366,
MipsFpu16 = 0x466,
PowerPC = 0x1F0,
PowerPCFP = 0x1F1,
R4000 = 0x166,
RiscV32 = 0x5032,
RiscV64 = 0x5064,
RiscV128 = 0x5128,
SH3 = 0x1A2,
SH3DSP = 0x1A3,
SH4 = 0x1A6,
SH5 = 0x1A8,
Thumb = 0x1C2,
WceMipsV2 = 0x169,
Invalid = 0xffff,
}
impl fmt::Display for MachineType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
MachineType::Invalid => write!(f, "Invalid"),
MachineType::Unknown => write!(f, "Unknown"),
MachineType::Am33 => write!(f, "Am33"),
MachineType::Amd64 => write!(f, "Amd64"),
MachineType::Arm => write!(f, "Arm"),
MachineType::Arm64 => write!(f, "Arm64"),
MachineType::ArmNT => write!(f, "ArmNT"),
MachineType::Ebc => write!(f, "Ebc"),
MachineType::X86 => write!(f, "X86"),
MachineType::Ia64 => write!(f, "Ia64"),
MachineType::M32R => write!(f, "M32R"),
MachineType::Mips16 => write!(f, "Mips16"),
MachineType::MipsFpu => write!(f, "MipsFpu"),
MachineType::MipsFpu16 => write!(f, "MipsFpu16"),
MachineType::PowerPC => write!(f, "PowerPC"),
MachineType::PowerPCFP => write!(f, "PowerPCFP"),
MachineType::R4000 => write!(f, "R4000"),
MachineType::RiscV32 => write!(f, "RiscV32"),
MachineType::RiscV64 => write!(f, "RiscV64"),
MachineType::RiscV128 => write!(f, "RiscV128"),
MachineType::SH3 => write!(f, "SH3"),
MachineType::SH3DSP => write!(f, "SH3DSP"),
MachineType::SH4 => write!(f, "SH4"),
MachineType::SH5 => write!(f, "SH5"),
MachineType::Thumb => write!(f, "Thumb"),
MachineType::WceMipsV2 => write!(f, "WceMipsV2"),
}
}
}
impl From<u16> for MachineType {
fn from(value: u16) -> Self {
match value {
0xffff => MachineType::Invalid,
0x0 => MachineType::Unknown,
0x13 => MachineType::Am33,
0x8664 => MachineType::Amd64,
0x1C0 => MachineType::Arm,
0xAA64 => MachineType::Arm64,
0x1C4 => MachineType::ArmNT,
0xEBC => MachineType::Ebc,
0x14C => MachineType::X86,
0x200 => MachineType::Ia64,
0x9041 => MachineType::M32R,
0x266 => MachineType::Mips16,
0x366 => MachineType::MipsFpu,
0x466 => MachineType::MipsFpu16,
0x1F0 => MachineType::PowerPC,
0x1F1 => MachineType::PowerPCFP,
0x166 => MachineType::R4000,
0x5032 => MachineType::RiscV32,
0x5064 => MachineType::RiscV64,
0x5128 => MachineType::RiscV128,
0x1A2 => MachineType::SH3,
0x1A3 => MachineType::SH3DSP,
0x1A6 => MachineType::SH4,
0x1A8 => MachineType::SH5,
0x1C2 => MachineType::Thumb,
0x169 => MachineType::WceMipsV2,
_ => MachineType::Unknown,
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct DBISectionContribution {
pub offset: PdbInternalSectionOffset,
pub size: u32,
pub characteristics: u32,
pub module: u16,
pub data_crc: u32,
pub reloc_crc: u32,
}
impl DBISectionContribution {
fn parse(buf: &mut ParseBuffer<'_>) -> Result<Self> {
let section = buf.parse_u16()?;
let _padding = buf.parse_u16()?;
let offset = buf.parse_u32()?;
let size = buf.parse_u32()?;
let characteristics = buf.parse_u32()?;
let module = buf.parse_u16()?;
let _padding = buf.parse_u16()?;
Ok(DBISectionContribution {
offset: PdbInternalSectionOffset { offset, section },
size,
characteristics,
module,
data_crc: buf.parse_u32()?,
reloc_crc: buf.parse_u32()?,
})
}
}
#[derive(Debug, Copy, Clone)]
pub(crate) struct DBIModuleInfo {
pub opened: u32,
pub section: DBISectionContribution,
pub flags: u16,
pub stream: StreamIndex,
pub symbols_size: u32,
pub lines_size: u32,
pub c13_lines_size: u32,
pub files: u16,
_padding: u16,
pub filename_offsets: u32,
pub source: u32,
pub compiler: u32,
}
impl DBIModuleInfo {
fn parse(buf: &mut ParseBuffer<'_>) -> Result<Self> {
Ok(DBIModuleInfo {
opened: buf.parse_u32()?,
section: DBISectionContribution::parse(buf)?,
flags: buf.parse_u16()?,
stream: buf.parse()?,
symbols_size: buf.parse_u32()?,
lines_size: buf.parse_u32()?,
c13_lines_size: buf.parse_u32()?,
files: buf.parse_u16()?,
_padding: buf.parse_u16()?,
filename_offsets: buf.parse_u32()?,
source: buf.parse_u32()?,
compiler: buf.parse_u32()?,
})
}
}
#[derive(Debug, Clone)]
pub struct Module<'m> {
info: DBIModuleInfo,
module_name: RawString<'m>,
object_file_name: RawString<'m>,
}
impl<'m> Module<'m> {
pub(crate) fn info(&self) -> &DBIModuleInfo {
&self.info
}
pub fn module_name(&self) -> Cow<'m, str> {
self.module_name.to_string()
}
pub fn object_file_name(&self) -> Cow<'m, str> {
self.object_file_name.to_string()
}
}
#[derive(Debug)]
pub struct ModuleIter<'m> {
buf: ParseBuffer<'m>,
}
impl<'m> FallibleIterator for ModuleIter<'m> {
type Item = Module<'m>;
type Error = Error;
fn next(&mut self) -> result::Result<Option<Self::Item>, Self::Error> {
if self.buf.is_empty() {
return Ok(None);
}
let info = DBIModuleInfo::parse(&mut self.buf)?;
let module_name = self.buf.parse_cstring()?;
let object_file_name = self.buf.parse_cstring()?;
self.buf.align(4)?;
Ok(Some(Module {
info,
module_name,
object_file_name,
}))
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
#[allow(missing_docs)]
enum DBISectionContributionStreamVersion {
V60,
V2,
OtherValue(u32),
}
impl From<u32> for DBISectionContributionStreamVersion {
fn from(v: u32) -> Self {
const V60: u32 = 0xeffe_0000 + 19_970_605;
const V2: u32 = 0xeffe_0000 + 20_140_516;
match v {
V60 => DBISectionContributionStreamVersion::V60,
V2 => DBISectionContributionStreamVersion::V2,
_ => DBISectionContributionStreamVersion::OtherValue(v),
}
}
}
#[derive(Debug)]
pub struct DBISectionContributionIter<'c> {
buf: ParseBuffer<'c>,
version: DBISectionContributionStreamVersion,
}
impl<'c> DBISectionContributionIter<'c> {
fn parse(mut buf: ParseBuffer<'c>) -> Result<Self> {
let version = buf.parse_u32()?.into();
Ok(Self { buf, version })
}
}
impl<'c> FallibleIterator for DBISectionContributionIter<'c> {
type Item = DBISectionContribution;
type Error = Error;
fn next(&mut self) -> result::Result<Option<Self::Item>, Self::Error> {
if self.buf.is_empty() {
return Ok(None);
}
let contribution = DBISectionContribution::parse(&mut self.buf)?;
if self.version == DBISectionContributionStreamVersion::V2 {
self.buf.parse_u32()?;
}
Ok(Some(contribution))
}
}
#[derive(Debug, Copy, Clone)]
pub(crate) struct DBIExtraStreams {
pub fpo: StreamIndex,
pub exception: StreamIndex,
pub fixup: StreamIndex,
pub omap_to_src: StreamIndex,
pub omap_from_src: StreamIndex,
pub section_headers: StreamIndex,
pub token_rid_map: StreamIndex,
pub xdata: StreamIndex,
pub pdata: StreamIndex,
pub framedata: StreamIndex,
pub original_section_headers: StreamIndex,
}
impl DBIExtraStreams {
pub(crate) fn new(debug_info: &DebugInformation<'_>) -> Result<Self> {
let header = debug_info.header;
let offset = debug_info.header_len
+ (header.module_list_size
+ header.section_contribution_size
+ header.section_map_size
+ header.file_info_size
+ header.type_server_map_size
+ header.mfc_type_server_index
+ header.ec_substream_size) as usize;
let mut buf = debug_info.stream.parse_buffer();
buf.take(offset)?;
let bytes = buf.take(header.debug_header_size as _)?;
let mut extra_streams_buf = ParseBuffer::from(bytes);
Self::parse(&mut extra_streams_buf)
}
pub(crate) fn parse(buf: &mut ParseBuffer<'_>) -> Result<Self> {
if buf.len() % 2 != 0 {
return Err(Error::InvalidStreamLength("DbgDataHdr"));
}
fn next_index(buf: &mut ParseBuffer<'_>) -> Result<StreamIndex> {
if buf.is_empty() {
Ok(StreamIndex::none())
} else {
buf.parse()
}
}
Ok(DBIExtraStreams {
fpo: next_index(buf)?,
exception: next_index(buf)?,
fixup: next_index(buf)?,
omap_to_src: next_index(buf)?,
omap_from_src: next_index(buf)?,
section_headers: next_index(buf)?,
token_rid_map: next_index(buf)?,
xdata: next_index(buf)?,
pdata: next_index(buf)?,
framedata: next_index(buf)?,
original_section_headers: next_index(buf)?,
})
}
}
#[cfg(test)]
mod tests {
use crate::dbi::*;
#[test]
fn test_dbi_extra_streams() {
let bytes = vec![0xff, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff, 0xff, 0x05, 0x06];
let mut buf = ParseBuffer::from(bytes.as_slice());
let extra_streams = DBIExtraStreams::parse(&mut buf).expect("parse");
assert_eq!(extra_streams.fpo, StreamIndex::none());
assert_eq!(extra_streams.exception, StreamIndex(0x0201));
assert_eq!(extra_streams.fixup, StreamIndex(0x0403));
assert_eq!(extra_streams.omap_to_src, StreamIndex::none());
assert_eq!(extra_streams.omap_from_src, StreamIndex(0x0605));
assert_eq!(extra_streams.section_headers, StreamIndex::none());
assert_eq!(extra_streams.token_rid_map, StreamIndex::none());
assert_eq!(extra_streams.original_section_headers, StreamIndex::none());
}
}