pdb-bindgen-codegen 0.1.0

Build-time discovery, PDB acquisition, and binding generation for pdb-bindgen.
Documentation
use super::{parse, utf8};
use crate::pdb::error::Error;
use crate::pdb::ir::{Database, Function, Global, Rva, TypeId};
use ms_pdb::syms::{SymData, SymKind};

pub(crate) fn normalize_symbols<'a>(
    symbols: impl Iterator<Item = ms_pdb::syms::Sym<'a>>,
    sections: &[ms_pdb::IMAGE_SECTION_HEADER],
    database: &mut Database,
) -> Result<(), Error> {
    normalize(symbols, sections, database, true)
}

pub(crate) fn normalize_global_data<'a>(
    symbols: impl Iterator<Item = ms_pdb::syms::Sym<'a>>,
    sections: &[ms_pdb::IMAGE_SECTION_HEADER],
    database: &mut Database,
) -> Result<(), Error> {
    normalize(symbols, sections, database, false)
}

fn normalize<'a>(
    symbols: impl Iterator<Item = ms_pdb::syms::Sym<'a>>,
    sections: &[ms_pdb::IMAGE_SECTION_HEADER],
    database: &mut Database,
    include_procedures: bool,
) -> Result<(), Error> {
    for symbol in symbols {
        match symbol.kind {
            SymKind::S_GPROC32 | SymKind::S_LPROC32 if include_procedures => {
                let SymData::Proc(procedure) = symbol.parse().map_err(parse("parse procedure"))?
                else {
                    return Err(Error::Parse {
                        operation: "parse procedure",
                        detail: "procedure leaf decoded as a different symbol kind".into(),
                    });
                };
                database.add_function(Function {
                    name: utf8(procedure.name, "read function name")?,
                    signature: TypeId(procedure.fixed.proc_type.get().0),
                    rva: rva(
                        sections,
                        procedure.fixed.offset_segment.segment(),
                        procedure.fixed.offset_segment.offset(),
                    )?,
                });
            }
            SymKind::S_GDATA32 | SymKind::S_LDATA32 => {
                let SymData::Data(data) = symbol.parse().map_err(parse("parse data symbol"))?
                else {
                    return Err(Error::Parse {
                        operation: "parse data symbol",
                        detail: "data leaf decoded as a different symbol kind".into(),
                    });
                };
                database.add_global(Global {
                    name: utf8(data.name, "read global name")?,
                    ty: TypeId(data.header.type_.get().0),
                    rva: rva(
                        sections,
                        data.header.offset_segment.segment(),
                        data.header.offset_segment.offset(),
                    )?,
                });
            }
            _ => {}
        }
    }
    Ok(())
}

pub(crate) fn rva(
    sections: &[ms_pdb::IMAGE_SECTION_HEADER],
    section: u16,
    offset: u32,
) -> Result<Rva, Error> {
    let index = usize::from(
        section
            .checked_sub(1)
            .ok_or(Error::InvalidSection { section, offset })?,
    );
    let header = sections
        .get(index)
        .ok_or(Error::InvalidSection { section, offset })?;
    header
        .virtual_address
        .checked_add(offset)
        .map(Rva)
        .ok_or(Error::InvalidSection { section, offset })
}