summaryrefslogtreecommitdiff
path: root/plugins/pdb-ng
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2024-12-22 01:21:02 -0500
committerMason Reed <mason@vector35.com>2025-01-25 00:52:29 -0500
commit6a63c17853009ce7a0071458e39c140a09440230 (patch)
treead59ab3b356b0a2c60423e55f62c996891788788 /plugins/pdb-ng
parentbfa1d409b2d5e734804a5fdb4c68c90a93712f1f (diff)
Rust refactor
Moves a bunch of stuff out of src/types.rs that did not belong: - Confidence - Variable - Function specific stuff - Refactored InstructionInfo, see the msp430 and riscv examples. - Renamed Function::from_raw to Function::ref_from_raw and fixed places where the ref was incremented twice - Fixed FunctionRecognizer leaking functions (see above) - Fixed some apis incorrectly returning Result where Option is clearer - Started to move destructured types to the From trait for converting to an from ffi types, see Location for an example - Started to remove bad module level imports (importing std modules like mem all over the place) - Moved some wrapper handle types to named handle field (this improves readability), see CoreArchitecture for an example - Removed some unintuitive getters, this is bad practice for Rust code, just access the field directly, see DataVariable for an example - General code cleanup, purposely did not run rustfmt, that will be a single seperate commit More rust cleanup - Fixed invalid views being able to invoke UB when dealing with databases - Cleaned up some helper code in dwarf_import - Fixed inverted is_null checks causing crashes! Oops! More rust cleanup Still a WIP, I think branch info is still invalid, need to figure out the issue there. - Fixed some invalid Ref lifetimes when constructing indirectly, see Array<DataVariable> for example - Added some more comments - Removed some "magic" functions like MLIL Function::ssa_variables There are still a bunch of invalid lifetimes that aren't crashing us due to the usage of those API's not living long enough. But they ARE an issue. More rust cleanup Trying to comment more TODO's as I go along. - Renamed llil::Function to llil::LowLevelILFunction for clarity and consistency - Take base structures by ref in StructureBuilder::set_base_structures to prevent premature drops - Added more raw to wrapper conversions - Removed UB prone apis - Getting rid of more core module references, use std! - Removed improper Guard usage in array impls for wrapper types with no context - Untangling the UB of the Label api, still only half done :/ More rust cleanup - Misc formatting - Made Logger ref counted - Fixed leaking name of logger every time something was logged - Fixed the last (hopefully) of the unresolved labels - Simplified some code Fix leak in DebugInfo::AddType componentArray was never freed Add more HLIL related functions to rust More rust cleanup improve the CustomBinaryView init process Canonicalize path in `create_delete_empty` test Link core in rust When linking you must depend on the -sys crate. This is because linker arguments (what says where to find binaryninjacore) are NOT transitive. The top level application crate MUST provide it. For more information see: - https://github.com/rust-lang/cargo/issues/9554#issuecomment-857882964 - https://github.com/oxidecomputer/omicron/pull/225 Remove vendored pdb crate Use cargo to manage the git repo ref instead Fix misc rustdoc warnings Move actual plugins out of `rust/examples` and into `plugins` This is where all shipped public plugins that are not arch/view/platform/lang will be at from now on Originally they were in the rust workspace, meaning they all shared a Cargo.lock which is ill-advised. More rust cleanup - More clarification on plugin/executable requirements - Made examples actually rust examples - Add Display impl for InstructionTextToken - Renamed feature "noexports" to "no_exports" Move under_construction.png to assets directory This really did bother me Remove unneeded `extern crate bindgen` Replace nop'd log::info with println We don't register a compatible log sink so they will just get sent into the void Move inline tests into tests directory This is being done in the hopes of curbing the multi-thousand line files that will occur once we flesh out the tests Format rust code Update rust ci Still need to add support for running tests in ci More rust cleanup - Architecture id's are now typed accordingly - Fix some clippy lints - Make instruction index public in LLIL - Removed useless helper functions - LLIL expressions and instruction indexes are now typed accordingly Generate binaryninjacore-sys documentation This should show binaryninjacore-sys alongside binaryninja crate More rust cleanup - Remove lazy_static dependency - Remove hacky impl Debug for Type and just use the display impl - Add more debug impls - Reorder some top level namespace items - Add first type test Remove unneeded script helper in rust api More rust cleanup - Added main thread handler api - Register a headless main thread handler by default in initialization - Refactor QualifiedName to be properly owned - Loosened some type constraints on some apis involving QualifiedName - Fixed some apis that were crashing due to incorrect param types - Removed extern crate cruft for log crate - Simplified headless initialization using more wrapper apis - Fixed segments leaking because of no ref wrapper, see BinaryViewExt::segment_at - Added rstest to manage headless init in unit tests - Added some more unit tests - Refactored demangler api to be more ergonomic - Fixed minidump plugin not building More rust cleanup - Fixup usage of QualifiedName in plugins - Make QualifiedName more usable Implement rust TypeParser fix Platform TypeParser related functions separate User and System implementations of TypeParserResult Implement rust TypeContainer More rust cleanup - Hopefully fixed the rust.yml CI - Added TypePrinter API (this is still WIP and will crash) - Added TypeParser API - Added TypeContainer API - More work around QualifiedName apis Oh your suppose to do this Add workflow_dispatch trigger to rust.yml More rust fixes - Swapped some usage of raw 255 to MAX_CONFIDENCE, no one likes magic numbers - New InstructionTextToken API, complete with owned data, this still needs a lot of testing. - InstructionTextTokenKind describes a destructured InstructionTextToken, this should make token usage much clearer, some docs pending - Added some misc Default and Debug impls - Updated TypePrinter and architectures to use new InstructionTextToken API Misc formatting changes More rust cleanup - Fixed MANY memory leaks (most due to QualifiedName) - Made StructureBuilder more builder and less structure - Fixed CMakeLists.txt that were globbing entire api, resulting in 100 second slowdown on cmake generation - Added more Debug impl's - Added some more tests - Fixed TypeParserResult UB - Moved the From impls to blank impl for clarity, we have multiple different variants of core to rust for some structures, it should be explicit which one you are choosing. - PossibleValueSet should now be able to allocate so we can go from rust to core with those variants that require allocation - Misc doc code formatting Misc clippy lints and clippy CI Co-authored-by: Michael Krasnitski <michael.krasnitski@gmail.com> Fix typo in rust CI Misc rust formatting Fix misc typos and add typos to rust CI Add cargo workspace This will help tooling and external contributors get a map of the rust crates within binaryninja-api More rust cleanup - Format all rust plugins - Fix some tests that were out of date - Simplify WARP tests to only binaries, building object files from source is a pain - Link to core in all rust plugins - Fix some memory leaks - Update warp insta snapshots - Fix some misc clippy lints Run rust tests in CI This commit also coincides with the creation of the "testing" environment which exposes a BN_SERIAL secret for pulling a headless Binary Ninja Install missing wayland dependency in github CI Apparently its needed for linux file picker for the WARP integration Set the BINARYNINJADIR so rust can find binaryninjacore in CI The chances of this working are low Misc remove unused dependency Rust misc formatting fixes Improve initialization in rust headless scripts Provide sensible errors and validation to rust headless scripts, solves https://github.com/Vector35/binaryninja-api/issues/5796 Add BN_LICENSE environment variable to rust CI We pass the serial to download binary ninja, but we never provided the license for core initialization Fix typo More rust cleanup - Improved binary view initialization (see init_with_opts) - Allow floating license to free itself before initialization - Add initialization unit test - Add better Debug impls for some common types - Use Path api for opening binary views, this is not breaking as it uses the AsRef impl - Bump rayon dependency and constrain dependencies to x.x Update readme and include that directly in the rustdocs More rust documentation changes Add format comment to InitializationOptions::with_license Misc formatting and clippy lint allow More rust cleanup - Remove under_construction.png from the build.rs it has been removed - Use InstructionIndex in more places - Add missing PartialOrd and Ord impls for id types More rust cleanup - Make workflow cloning explicit - Add workflow tests - Add missing property string list getting for settings - Remove IntoActivityName (see https://github.com/Vector35/binaryninja-api/pull/6257) More rust cleanup This commit is half done Misc rust formatting More rust cleanup - Renamed common name conflictions (I will put my justification in the PR) - Fixed invalid instruction retrieval for LLIL - Added common aliases for llil function, instruction and expression types (see my comment in the PR) - Refactored the instruction retrieval for LLIL, MLIL and HLIL - Added instruction index types to MLIL and HLIL - Moved llil module to lowlevelil module (mlil and hlil will be moved as well) - Added preliminary LLIL unit testing Fix typos Misc clippy fixes More rust cleanup - Normalized modules - Split some code out into own files - Fixed some UB in type archive and projects - Improved API around type archives and projects substantially - Added ProgressExecutor abstraction for functions which have a progress callback - Improved background task documentation and added unit tests - Added worker thread api and unit tests - Moved some owned types to ref types, this is still not complete, but this is the path forward. - Add external location/library accessors to the binary view - Added some misc documentation - Replaced mod.rs with the module name source file Still need to normalize some paths and also update some documentation surrounding that change. Update some tests and examples Fix background task tests colliding We were creating multiple background tasks with the same progress text on multiple threads More rust cleanup - Fixed progress executor freeing itself after one iteration - Updated the last of the doc imports - Moved mainthread to main_thread - Made project creation and opening failable We could probably AsRef<ProgressExecutor> to get around the allocation and free inside the function bodies, not high priority as those functions are long running anyways. Move binary view initialization into function body for LLIL test Normalize test file names More rust cleanup - Updated README to clarify offline documentation - Refactored settings api - Added settings example to show dumping settings value and specific properties - Use the workspace to depend on binaryninja and binaryninjacore-sys - Remove binaryninjacore-sys as a workspace member (its not really required) Update workflow test to new settings api More rust cleanup - Rename Label to LowLevelILLabel - Update the functions label map automatically This fixed a major api blunder where the label map is returned as a reference and originally resulted in UB prone lifetime semantics. It was temporarily "fixed" with explicit label updates in the architecture implementation code. But that was less than ideal and was easy to mess up. Now the label map will be updated automatically as the location of labels is now tracked. Misc clippy lints More rust cleanup - Get rid of RawFunctionViewType - Add better Debug impl for Function More rust cleanup - Fixed the documentation icon using local files (thank you @mkrasnitski) - Fixed labels being updated and overwriting the label location used to update the label map More rust cleanup - Added unit tests for MLIL and HLIL - "Fixed" MLIL, LLIL, and HLIL having issues regarding Instruction vs Expression indexes - Renamed CallingConvention to CoreCallingConvention and removed architecture generic - Renamed CallingConventionBase to CallingConvention - Simplified calling convention code and fixed some bugs with implicit registers - Added impl Debug to MLIL and HLIL instructions Still need to at some point add an Expression to MLIL and HLIL. We also might want to look into having the Instruction kind just return the expression kind. Misc clippy lint More rust cleanup - Allow calling conventions to be registered for multiple architectures - Swapped a unreachable statement to an unimplemented statement More rust cleanup - Fixed the issue with PDB types, this has caused me an insane amount of grief - Fixed LLIL visit_tree missing LLIL_LOAD expressions - Added LLIL visitor test - Made all WARP file pickers use the rfd crate Use the dev branch of Binary Ninja in rust CI Misc rust fmt More rust cleanup - Refactored BinaryReader and BinaryWriter - Added some warnings to high_level_il and medium_level_il modules that they are unstable - Add BinaryReader and BinaryWriter tests - Changed BinaryView len to return u64 (that is what the core returns) - Misc formatting changes - Remove extern uses in lib.rs Add impl Debug for BinaryReader and BinaryWriter Turn off broken tests Add more info to the rust README.md More rust cleanup - Make EdgeStyle type not wrap raw - Regression tests for WARP will run on all bins in the out dir now impl rust Collaboration and Remote API Fix typo Update collaboration API Makes collaboration more in line with the refactor. Still a lot of work to do. Namely still need: - Proper errors - _with_opts functions - More ergonomic api - Better connection procedure - Updated documentation - A LOT of unit tests - An example - Typed id's for everything (i dont want BnString as the id!!!) - NEED to refactor the progress callbacks into the new progress api, but we should pull in some of the stuff the collab progress has - Elimination of apis that are dumb helpers Separate out the rust testing and use pull_request_target pull_request_target allows PR's to access the headless license, for this to be safe we need to prevent people from running the job. To prevent the job from being ran we add an environment requirement on testing that a reviewer must review the code and then manually approve it to run. More rust cleanup - Use GroupId instead of u64 - Use ProgressCallback in place of ProgressExecutor - Misc cleanup of FileMetadata - Add `save_to_path` and `save_to_accessor` to save modified binaries - Added binary_view unit tests - Added collaboration unit tests - Fixed a few issues with the collaboration apis - Renamed Command registration functions so that there is no import ambiguity - Split out RemoteUndoEntry - Collaboration apis now have a explicit `_with_progress` set of apis - Misc clippy lint fixes Fix some typos More rust cleanup - Add extra info to README.md - Refactor components api - Add components unit test Add testing and documentation to contributing section in README.md Fix misc doc comments
Diffstat (limited to 'plugins/pdb-ng')
-rw-r--r--plugins/pdb-ng/.gitignore1
-rw-r--r--plugins/pdb-ng/CMakeLists.txt138
-rw-r--r--plugins/pdb-ng/Cargo.toml19
-rw-r--r--plugins/pdb-ng/build.rs15
-rw-r--r--plugins/pdb-ng/demo/Cargo.toml21
-rw-r--r--plugins/pdb-ng/src/lib.rs934
-rw-r--r--plugins/pdb-ng/src/parser.rs505
-rw-r--r--plugins/pdb-ng/src/struct_grouper.rs1164
-rw-r--r--plugins/pdb-ng/src/symbol_parser.rs2043
-rw-r--r--plugins/pdb-ng/src/type_parser.rs2453
10 files changed, 7293 insertions, 0 deletions
diff --git a/plugins/pdb-ng/.gitignore b/plugins/pdb-ng/.gitignore
new file mode 100644
index 00000000..eb5a316c
--- /dev/null
+++ b/plugins/pdb-ng/.gitignore
@@ -0,0 +1 @@
+target
diff --git a/plugins/pdb-ng/CMakeLists.txt b/plugins/pdb-ng/CMakeLists.txt
new file mode 100644
index 00000000..d0282c1a
--- /dev/null
+++ b/plugins/pdb-ng/CMakeLists.txt
@@ -0,0 +1,138 @@
+cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
+
+project(pdb_import_plugin)
+
+file(GLOB PLUGIN_SOURCES CONFIGURE_DEPENDS
+ ${PROJECT_SOURCE_DIR}/Cargo.toml
+ ${PROJECT_SOURCE_DIR}/src/*.rs)
+
+file(GLOB_RECURSE API_SOURCES CONFIGURE_DEPENDS
+ ${PROJECT_SOURCE_DIR}/../../binaryninjacore.h
+ ${PROJECT_SOURCE_DIR}/../../rust/binaryninjacore-sys/build.rs
+ ${PROJECT_SOURCE_DIR}/../../rust/binaryninjacore-sys/Cargo.toml
+ ${PROJECT_SOURCE_DIR}/../../rust/binaryninjacore-sys/src/*
+ ${PROJECT_SOURCE_DIR}/../../rust/Cargo.toml
+ ${PROJECT_SOURCE_DIR}/../../rust/src/*.rs)
+
+if(CMAKE_BUILD_TYPE MATCHES Debug)
+ set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/debug)
+ set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target)
+else()
+ set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/release)
+ set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target --release)
+endif()
+
+if(FORCE_COLORED_OUTPUT)
+ set(CARGO_OPTS ${CARGO_OPTS} --color always)
+endif()
+
+if(DEMO)
+ set(CARGO_FEATURES --features demo --manifest-path ${PROJECT_SOURCE_DIR}/demo/Cargo.toml)
+
+ set(OUTPUT_FILE_NAME ${CMAKE_STATIC_LIBRARY_PREFIX}${PROJECT_NAME}_static${CMAKE_STATIC_LIBRARY_SUFFIX})
+ set(OUTPUT_PDB_NAME ${CMAKE_STATIC_LIBRARY_PREFIX}${PROJECT_NAME}.pdb)
+ set(OUTPUT_FILE_PATH ${CMAKE_BINARY_DIR}/${OUTPUT_FILE_NAME})
+ set(OUTPUT_PDB_PATH ${CMAKE_BINARY_DIR}/${OUTPUT_PDB_NAME})
+
+ set(BINJA_LIB_DIR $<TARGET_FILE_DIR:binaryninjacore>)
+else()
+ set(CARGO_FEATURES "")
+
+ set(OUTPUT_FILE_NAME ${CMAKE_SHARED_LIBRARY_PREFIX}${PROJECT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})
+ set(OUTPUT_PDB_NAME ${CMAKE_SHARED_LIBRARY_PREFIX}${PROJECT_NAME}.pdb)
+ set(OUTPUT_FILE_PATH ${BN_CORE_PLUGIN_DIR}/${OUTPUT_FILE_NAME})
+ set(OUTPUT_PDB_PATH ${BN_CORE_PLUGIN_DIR}/${OUTPUT_PDB_NAME})
+
+ set(BINJA_LIB_DIR ${BN_INSTALL_BIN_DIR})
+endif()
+
+add_custom_target(${PROJECT_NAME} ALL DEPENDS ${OUTPUT_FILE_PATH})
+add_dependencies(${PROJECT_NAME} binaryninjaapi)
+get_target_property(BN_API_SOURCE_DIR binaryninjaapi SOURCE_DIR)
+list(APPEND CMAKE_MODULE_PATH "${BN_API_SOURCE_DIR}/cmake")
+find_package(BinaryNinjaCore REQUIRED)
+
+set_property(TARGET ${PROJECT_NAME} PROPERTY OUTPUT_FILE_PATH ${OUTPUT_FILE_PATH})
+
+find_program(RUSTUP_PATH rustup REQUIRED HINTS ~/.cargo/bin)
+set(RUSTUP_COMMAND ${RUSTUP_PATH} run ${CARGO_STABLE_VERSION} cargo)
+
+if(APPLE)
+ if(UNIVERSAL)
+ if(CMAKE_BUILD_TYPE MATCHES Debug)
+ set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/debug/${OUTPUT_FILE_NAME})
+ set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/debug/${OUTPUT_FILE_NAME})
+ else()
+ set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/release/${OUTPUT_FILE_NAME})
+ set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/release/${OUTPUT_FILE_NAME})
+ endif()
+
+ add_custom_command(
+ OUTPUT ${OUTPUT_FILE_PATH}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BINJA_LIB_DIR}
+ ${RUSTUP_COMMAND} clean --target=aarch64-apple-darwin ${CARGO_OPTS}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BINJA_LIB_DIR}
+ ${RUSTUP_COMMAND} clean --target=x86_64-apple-darwin ${CARGO_OPTS}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BINJA_LIB_DIR}
+ ${RUSTUP_COMMAND} build --target=aarch64-apple-darwin ${CARGO_OPTS} ${CARGO_FEATURES}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BINJA_LIB_DIR}
+ ${RUSTUP_COMMAND} build --target=x86_64-apple-darwin ${CARGO_OPTS} ${CARGO_FEATURES}
+ COMMAND lipo -create ${AARCH64_LIB_PATH} ${X86_64_LIB_PATH} -output ${OUTPUT_FILE_PATH}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES}
+ )
+ else()
+ if(CMAKE_BUILD_TYPE MATCHES Debug)
+ set(LIB_PATH ${PROJECT_BINARY_DIR}/target/debug/${OUTPUT_FILE_NAME})
+ else()
+ set(LIB_PATH ${PROJECT_BINARY_DIR}/target/release/${OUTPUT_FILE_NAME})
+ endif()
+
+ add_custom_command(
+ OUTPUT ${OUTPUT_FILE_PATH}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BINJA_LIB_DIR}
+ ${RUSTUP_COMMAND} clean ${CARGO_OPTS}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BINJA_LIB_DIR}
+ ${RUSTUP_COMMAND} build ${CARGO_OPTS} ${CARGO_FEATURES}
+ COMMAND ${CMAKE_COMMAND} -E copy ${LIB_PATH} ${OUTPUT_FILE_PATH}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES}
+ )
+ endif()
+elseif(WIN32)
+ if(DEMO)
+ add_custom_command(
+ OUTPUT ${OUTPUT_FILE_PATH}
+ COMMAND ${CMAKE_COMMAND} -E env BINARYNINJADIR=${BINJA_LIB_DIR} ${RUSTUP_COMMAND} clean ${CARGO_OPTS}
+ COMMAND ${CMAKE_COMMAND} -E env BINARYNINJADIR=${BINJA_LIB_DIR} ${RUSTUP_COMMAND} build ${CARGO_OPTS} ${CARGO_FEATURES}
+ COMMAND ${CMAKE_COMMAND} -E copy ${TARGET_DIR}/${OUTPUT_FILE_NAME} ${OUTPUT_FILE_PATH}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES}
+ )
+ else()
+ add_custom_command(
+ OUTPUT ${OUTPUT_FILE_PATH}
+ COMMAND ${CMAKE_COMMAND} -E env BINARYNINJADIR=${BINJA_LIB_DIR} ${RUSTUP_COMMAND} clean ${CARGO_OPTS}
+ COMMAND ${CMAKE_COMMAND} -E env BINARYNINJADIR=${BINJA_LIB_DIR} ${RUSTUP_COMMAND} build ${CARGO_OPTS} ${CARGO_FEATURES}
+ COMMAND ${CMAKE_COMMAND} -E copy ${TARGET_DIR}/${OUTPUT_FILE_NAME} ${OUTPUT_FILE_PATH}
+ COMMAND ${CMAKE_COMMAND} -E copy ${TARGET_DIR}/${OUTPUT_PDB_NAME} ${OUTPUT_PDB_PATH}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES}
+ )
+ endif()
+else()
+ add_custom_command(
+ OUTPUT ${OUTPUT_FILE_PATH}
+ COMMAND ${CMAKE_COMMAND} -E env BINARYNINJADIR=${BINJA_LIB_DIR} ${RUSTUP_COMMAND} clean ${CARGO_OPTS}
+ COMMAND ${CMAKE_COMMAND} -E env BINARYNINJADIR=${BINJA_LIB_DIR} ${RUSTUP_COMMAND} build ${CARGO_OPTS} ${CARGO_FEATURES}
+ COMMAND ${CMAKE_COMMAND} -E copy ${TARGET_DIR}/${OUTPUT_FILE_NAME} ${OUTPUT_FILE_PATH}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES}
+ )
+endif()
diff --git a/plugins/pdb-ng/Cargo.toml b/plugins/pdb-ng/Cargo.toml
new file mode 100644
index 00000000..2025de13
--- /dev/null
+++ b/plugins/pdb-ng/Cargo.toml
@@ -0,0 +1,19 @@
+[package]
+name = "pdb-import-plugin"
+version = "0.1.0"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib"]
+
+[dependencies]
+anyhow = "^1.0"
+binaryninja.workspace = true
+binaryninjacore-sys.workspace = true
+itertools = "^0.11"
+log = "0.4"
+pdb = { git = "https://github.com/Vector35/pdb-rs", rev = "6016177" }
+regex = "1"
+
+[features]
+demo = [] \ No newline at end of file
diff --git a/plugins/pdb-ng/build.rs b/plugins/pdb-ng/build.rs
new file mode 100644
index 00000000..ed6cec7d
--- /dev/null
+++ b/plugins/pdb-ng/build.rs
@@ -0,0 +1,15 @@
+fn main() {
+ let link_path = std::env::var_os("DEP_BINARYNINJACORE_PATH")
+ .expect("DEP_BINARYNINJACORE_PATH not specified");
+
+ println!("cargo::rustc-link-lib=dylib=binaryninjacore");
+ println!("cargo::rustc-link-search={}", link_path.to_str().unwrap());
+
+ #[cfg(not(target_os = "windows"))]
+ {
+ println!(
+ "cargo::rustc-link-arg=-Wl,-rpath,{0},-L{0}",
+ link_path.to_string_lossy()
+ );
+ }
+}
diff --git a/plugins/pdb-ng/demo/Cargo.toml b/plugins/pdb-ng/demo/Cargo.toml
new file mode 100644
index 00000000..f9f446b5
--- /dev/null
+++ b/plugins/pdb-ng/demo/Cargo.toml
@@ -0,0 +1,21 @@
+[package]
+name = "pdb-import-plugin-static"
+version = "0.1.0"
+edition = "2021"
+
+[lib]
+crate-type = ["staticlib"]
+path = "../src/lib.rs"
+
+[dependencies]
+anyhow = "^1.0"
+binaryninja = {path = "../../../"}
+home = "^0.5.5"
+itertools = "^0.11"
+log = "0.4"
+pdb = { git = "https://github.com/Vector35/pdb-rs", branch = "master" }
+cab = "^0.4"
+regex = "1"
+
+[features]
+demo = []
diff --git a/plugins/pdb-ng/src/lib.rs b/plugins/pdb-ng/src/lib.rs
new file mode 100644
index 00000000..e30221c9
--- /dev/null
+++ b/plugins/pdb-ng/src/lib.rs
@@ -0,0 +1,934 @@
+// Copyright 2022-2024 Vector 35 Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+#![allow(dead_code)]
+
+use std::collections::HashMap;
+use std::env::{current_dir, current_exe, temp_dir};
+use std::io::Cursor;
+use std::path::PathBuf;
+use std::str::FromStr;
+use std::sync::mpsc;
+use std::{env, fs};
+
+use anyhow::{anyhow, Result};
+use log::{debug, error, info};
+use pdb::PDB;
+
+use binaryninja::binary_view::{BinaryView, BinaryViewBase, BinaryViewExt};
+use binaryninja::debuginfo::{CustomDebugInfoParser, DebugInfo, DebugInfoParser};
+use binaryninja::download_provider::{DownloadInstanceInputOutputCallbacks, DownloadProvider};
+use binaryninja::interaction::{MessageBoxButtonResult, MessageBoxButtonSet};
+use binaryninja::logger::Logger;
+use binaryninja::settings::{QueryOptions, Settings};
+use binaryninja::string::BnString;
+use binaryninja::{interaction, user_directory};
+use parser::PDBParserInstance;
+
+/// PDB Parser!!
+///
+/// General project structure:
+/// - lib.rs: Interaction with DebugInfoParser and plugin actions
+/// - parser.rs: PDB Parser base functionality, puts the internal structures into the DebugInfo
+/// - type_parser.rs: Parses all the TPI type stream information into both named and indexed types
+/// - symbol_parser.rs: Parses, one module at a time, symbol information into named symbols
+/// - struct_grouper.rs: Ugly algorithm for handling union and structure members
+mod parser;
+mod struct_grouper;
+mod symbol_parser;
+mod type_parser;
+
+// struct PDBLoad;
+// struct PDBLoadFile;
+// struct PDBSetSymbolPath;
+
+#[allow(dead_code)]
+struct PDBInfo {
+ path: String,
+ file_name: String,
+ age: u32,
+ guid: Vec<u8>,
+ guid_age_string: String,
+}
+
+fn is_pdb(view: &BinaryView) -> bool {
+ let pdb_magic_bytes = "Microsoft C/C++ MSF 7.00\r\n\x1A\x44\x53\x00\x00\x00";
+ if let Some(raw_view) = view.raw_view() {
+ raw_view.read_vec(0, pdb_magic_bytes.len()) == pdb_magic_bytes.as_bytes()
+ } else {
+ false
+ }
+}
+
+fn default_local_cache() -> Result<String> {
+ // The default value is a directory named "sym" immediately below the program directory
+ // of the calling application. This is sometimes referred to as the default local cache.
+ let current_path = current_exe()?;
+ let parent_path = current_path
+ .parent()
+ .ok_or_else(|| anyhow!("No parent to current exe"))?;
+ let mut cache_path = PathBuf::from(parent_path);
+ cache_path.push("sym");
+ return Ok(cache_path
+ .to_str()
+ .ok_or_else(|| anyhow!("Could not convert cache path to string"))?
+ .to_string());
+}
+
+fn active_local_cache(view: Option<&BinaryView>) -> Result<String> {
+ // Check the local symbol store
+ let mut settings_query_options = view.map(QueryOptions::new_with_view).unwrap_or_default();
+ let settings = Settings::new();
+ let mut local_store_path = settings
+ .get_string_with_opts("pdb.files.localStoreAbsolute", &mut settings_query_options)
+ .to_string();
+ if local_store_path.is_empty() {
+ let relative_local_store = settings
+ .get_string_with_opts("pdb.files.localStoreRelative", &mut settings_query_options)
+ .to_string();
+ local_store_path = user_directory()
+ .join(relative_local_store)
+ .to_string_lossy()
+ .to_string();
+ }
+ if !local_store_path.is_empty() {
+ Ok(local_store_path)
+ } else if let Ok(default_cache) = default_local_cache() {
+ Ok(default_cache)
+ } else if let Ok(current) = current_dir().map(|d| {
+ d.to_str()
+ .expect("Expected current dir to be a valid string")
+ .to_string()
+ }) {
+ Ok(current)
+ } else {
+ Ok(temp_dir()
+ .to_str()
+ .expect("Expected temp dir to be a valid string")
+ .to_string())
+ }
+}
+
+fn parse_sym_srv(symbol_path: &str, default_store: String) -> Result<impl Iterator<Item = String>> {
+ // https://docs.microsoft.com/en-us/windows/win32/debug/using-symsrv
+ // Why
+
+ // ... the symbol path (_NT_SYMBOL_PATH environment variable) can be made up of several path
+ // elements separated by semicolons. If any one or more of these path elements begins with
+ // the text "srv*", then the element is a symbol server and will use SymSrv to locate
+ // symbol files.
+
+ // If the "srv*" text is not specified but the actual path element is a symbol server store,
+ // then the symbol handler will act as if "srv*" were specified. The symbol handler makes
+ // this determination by searching for the existence of a file called "pingme.txt" in
+ // the root directory of the specified path.
+
+ // ... symbol servers are made up of symbol store elements separated by asterisks. There can
+ // be up to 10 symbol stores after the "srv*" prefix.
+
+ let mut sym_srv_results: Vec<String> = vec![];
+
+ // 'path elements separated by semicolons'
+ for path_element in symbol_path.split(';') {
+ // 'begins with the text "srv*"'
+ if path_element.to_lowercase().starts_with("srv*") {
+ // 'symbol store elements separated by asterisks'
+ for store_element in path_element[4..].split('*') {
+ if store_element.is_empty() {
+ sym_srv_results.push(default_store.clone());
+ } else {
+ sym_srv_results.push(store_element.to_string());
+ }
+ }
+ } else if PathBuf::from(path_element).exists() {
+ // 'searching for the existence of a file called "pingme.txt" in the root directory'
+ let pingme_txt = path_element.to_string() + "/" + "pingme.txt";
+ if PathBuf::from(pingme_txt).exists() {
+ sym_srv_results.push(path_element.to_string());
+ }
+ }
+ }
+
+ Ok(sym_srv_results.into_iter())
+}
+
+fn read_from_sym_store(bv: &BinaryView, path: &str) -> Result<(bool, Vec<u8>)> {
+ if !path.contains("://") {
+ // Local file
+ info!("Read local file: {}", path);
+ let conts = fs::read(path)?;
+ return Ok((false, conts));
+ }
+
+ let mut query_options = QueryOptions::new_with_view(bv);
+ if !Settings::new().get_bool_with_opts("network.pdbAutoDownload", &mut query_options) {
+ return Err(anyhow!("Auto download disabled"));
+ }
+
+ // Download from remote
+ let (tx, rx) = mpsc::channel();
+ let write = move |data: &[u8]| -> usize {
+ if tx.send(Vec::from(data)).is_ok() {
+ data.len()
+ } else {
+ 0
+ }
+ };
+
+ info!("GET: {}", path);
+
+ let dp =
+ DownloadProvider::try_default().map_err(|_| anyhow!("No default download provider"))?;
+ let mut inst = dp
+ .create_instance()
+ .map_err(|_| anyhow!("Couldn't create download instance"))?;
+ let result = inst
+ .perform_custom_request(
+ "GET",
+ path,
+ HashMap::<BnString, BnString>::new(),
+ DownloadInstanceInputOutputCallbacks {
+ read: None,
+ write: Some(Box::new(write)),
+ progress: None,
+ },
+ )
+ .map_err(|e| anyhow!(e.to_string()))?;
+ if result.status_code != 200 {
+ return Err(anyhow!("Path does not exist"));
+ }
+
+ let mut expected_length = None;
+ for (k, v) in result.headers.iter() {
+ if k.to_lowercase() == "content-length" {
+ expected_length = Some(usize::from_str(v)?);
+ }
+ }
+
+ let mut data = vec![];
+ while let Ok(packet) = rx.try_recv() {
+ data.extend(packet.into_iter());
+ }
+
+ if let Some(length) = expected_length {
+ if data.len() != length {
+ return Err(anyhow!(format!(
+ "Bad length: expected {} got {}",
+ length,
+ data.len()
+ )));
+ }
+ }
+
+ Ok((true, data))
+}
+
+fn search_sym_store(
+ bv: &BinaryView,
+ store_path: String,
+ pdb_info: &PDBInfo,
+) -> Result<Option<Vec<u8>>> {
+ // https://www.technlg.net/windows/symbol-server-path-windbg-debugging/
+ // For symbol servers, to identify the files path easily, Windbg uses the format
+ // binaryname.pdb/GUID
+
+ // Doesn't actually say what the format is, just gives an example:
+ // https://docs.microsoft.com/en-us/windows/win32/debug/using-symstore
+ // In this example, the lookup path for the acpi.dbg symbol file might look something
+ // like this: \\mybuilds\symsrv\acpi.dbg\37cdb03962040.
+ let base_path = store_path + "/" + &pdb_info.file_name + "/" + &pdb_info.guid_age_string;
+
+ // Three files may exist inside the lookup directory:
+ // 1. If the file was stored, then acpi.dbg will exist there.
+ // 2. If a pointer was stored, then a file called file.ptr will exist and contain the path
+ // to the actual symbol file.
+ // 3. A file called refs.ptr, which contains a list of all the current locations for
+ // acpi.dbg with this timestamp and image size that are currently added to the
+ // symbol store.
+
+ // We don't care about #3 because it says we don't
+
+ let direct_path = base_path.clone() + "/" + &pdb_info.file_name;
+ if let Ok((_remote, conts)) = read_from_sym_store(bv, &direct_path) {
+ return Ok(Some(conts));
+ }
+
+ let file_ptr = base_path.clone() + "/" + "file.ptr";
+ if let Ok((_remote, conts)) = read_from_sym_store(bv, &file_ptr) {
+ let path = String::from_utf8(conts)?;
+ // PATH:https://full/path
+ if let Some(stripped) = path.strip_prefix("PATH:") {
+ if let Ok((_remote, conts)) = read_from_sym_store(bv, stripped) {
+ return Ok(Some(conts));
+ }
+ }
+ }
+
+ Ok(None)
+}
+
+fn parse_pdb_info(view: &BinaryView) -> Option<PDBInfo> {
+ match view.get_metadata::<u64, _>("DEBUG_INFO_TYPE") {
+ Some(Ok(0x53445352 /* 'SDSR' */)) => {}
+ _ => return None,
+ }
+
+ // This is stored in the BV by the PE loader
+ let file_path = match view.get_metadata::<String, _>("PDB_FILENAME") {
+ Some(Ok(md)) => md,
+ _ => return None,
+ };
+ let mut guid = match view.get_metadata::<Vec<u8>, _>("PDB_GUID") {
+ Some(Ok(md)) => md,
+ _ => return None,
+ };
+ let age = match view.get_metadata::<u64, _>("PDB_AGE") {
+ Some(Ok(md)) => md as u32,
+ _ => return None,
+ };
+
+ if guid.len() != 16 {
+ return None;
+ }
+
+ // struct _GUID {
+ // uint32_t Data1;
+ // uint16_t Data2;
+ // uint16_t Data3;
+ // uint8_t Data4[8];
+ // };
+
+ // Endian swap
+ // Data1
+ guid.swap(0, 3);
+ guid.swap(1, 2);
+ // Data2
+ guid.swap(4, 5);
+ // Data3
+ guid.swap(6, 7);
+
+ let guid_age_string = guid
+ .iter()
+ .take(16)
+ .map(|ch| format!("{:02X}", ch))
+ .collect::<Vec<_>>()
+ .join("")
+ + &format!("{:X}", age);
+
+ // Just assume all the paths are /
+ let file_path = if cfg!(windows) {
+ file_path
+ } else {
+ file_path.replace("\\", "/")
+ };
+ let path = file_path;
+ let file_name = if let Some(idx) = path.rfind("\\") {
+ path[(idx + 1)..].to_string()
+ } else if let Some(idx) = path.rfind("/") {
+ path[(idx + 1)..].to_string()
+ } else {
+ path.clone()
+ };
+
+ Some(PDBInfo {
+ path,
+ file_name,
+ age,
+ guid,
+ guid_age_string,
+ })
+}
+
+struct PDBParser;
+impl PDBParser {
+ fn load_from_file(
+ &self,
+ conts: &Vec<u8>,
+ debug_info: &mut DebugInfo,
+ view: &BinaryView,
+ progress: &dyn Fn(usize, usize) -> Result<(), ()>,
+ check_guid: bool,
+ did_download: bool,
+ ) -> Result<()> {
+ let mut pdb = PDB::open(Cursor::new(&conts))?;
+
+ let settings = Settings::new();
+ let mut settings_query_opts = QueryOptions::new_with_view(view);
+
+ if let Some(info) = parse_pdb_info(view) {
+ let pdb_info = &pdb.pdb_information()?;
+ if info.guid.as_slice() != pdb_info.guid.as_bytes() {
+ if check_guid {
+ return Err(anyhow!("PDB GUID does not match"));
+ } else {
+ let ask = settings.get_string_with_opts(
+ "pdb.features.loadMismatchedPDB",
+ &mut settings_query_opts,
+ );
+
+ match ask.as_str() {
+ "true" => {},
+ "ask" => {
+ if interaction::show_message_box(
+ "Mismatched PDB",
+ "This PDB does not look like it matches your binary. Do you want to load it anyway?",
+ MessageBoxButtonSet::YesNoButtonSet,
+ binaryninja::interaction::MessageBoxIcon::QuestionIcon
+ ) == MessageBoxButtonResult::NoButton {
+ return Err(anyhow!("User cancelled mismatched load"));
+ }
+ }
+ _ => {
+ return Err(anyhow!("PDB GUID does not match"));
+ }
+ }
+ }
+ }
+
+ // Microsoft's symbol server sometimes gives us a different version of the PDB
+ // than what we ask for. It's weird, but if they're doing it, I trust it will work.
+ if info.age != pdb_info.age {
+ if info.age > pdb_info.age {
+ // Have not seen this case, so I'm not sure if this is fatal
+ info!("PDB age is older than our binary! Loading it anyway, but there may be missing information.");
+ } else {
+ info!("PDB age is newer than our binary! Loading it anyway, there probably shouldn't be any issues.");
+ }
+ }
+
+ if did_download && settings.get_bool("pdb.files.localStoreCache") {
+ match active_local_cache(Some(view)) {
+ Ok(cache) => {
+ let mut cab_path = PathBuf::from(&cache);
+ cab_path.push(&info.file_name);
+ cab_path.push(
+ pdb_info
+ .guid
+ .as_bytes()
+ .iter()
+ .map(|ch| format!("{:02X}", ch))
+ .collect::<Vec<_>>()
+ .join("")
+ + &format!("{:X}", pdb_info.age),
+ );
+ let has_dir = if cab_path.is_dir() {
+ true
+ } else {
+ match fs::create_dir_all(&cab_path) {
+ Ok(_) => true,
+ Err(e) => {
+ error!("Could not create PDB cache dir: {}", e);
+ false
+ }
+ }
+ };
+ if has_dir {
+ cab_path.push(&info.file_name);
+ match fs::write(&cab_path, conts) {
+ Ok(_) => {
+ info!("Downloaded to: {}", cab_path.to_string_lossy());
+ }
+ Err(e) => error!("Could not write PDB to cache: {}", e),
+ }
+ }
+
+ // Also write with the age we expect in our binary view
+ if info.age < pdb_info.age {
+ let mut cab_path = PathBuf::from(&cache);
+ cab_path.push(&info.file_name);
+ cab_path.push(
+ pdb_info
+ .guid
+ .as_bytes()
+ .iter()
+ .map(|ch| format!("{:02X}", ch))
+ .collect::<Vec<_>>()
+ .join("")
+ + &format!("{:X}", info.age), // XXX: BV's pdb age
+ );
+ let has_dir = if cab_path.is_dir() {
+ true
+ } else {
+ match fs::create_dir_all(&cab_path) {
+ Ok(_) => true,
+ Err(e) => {
+ error!("Could not create PDB cache dir: {}", e);
+ false
+ }
+ }
+ };
+ if has_dir {
+ cab_path.push(&info.file_name);
+ match fs::write(&cab_path, conts) {
+ Ok(_) => {
+ info!("Downloaded to: {}", cab_path.to_string_lossy());
+ }
+ Err(e) => error!("Could not write PDB to cache: {}", e),
+ }
+ }
+ }
+ }
+ Err(e) => error!("Could not get local cache for writing: {}", e),
+ }
+ }
+ } else {
+ if check_guid {
+ return Err(anyhow!("File not compiled with PDB information"));
+ } else {
+ let ask = settings.get_string_with_opts(
+ "pdb.features.loadMismatchedPDB",
+ &mut settings_query_opts,
+ );
+
+ match ask.as_str() {
+ "true" => {},
+ "ask" => {
+ if interaction::show_message_box(
+ "No PDB Information",
+ "This file does not look like it was compiled with a PDB, so your PDB might not correctly apply to the analysis. Do you want to load it anyway?",
+ MessageBoxButtonSet::YesNoButtonSet,
+ binaryninja::interaction::MessageBoxIcon::QuestionIcon
+ ) == MessageBoxButtonResult::NoButton {
+ return Err(anyhow!("User cancelled missing info load"));
+ }
+ }
+ _ => {
+ return Err(anyhow!("File not compiled with PDB information"));
+ }
+ }
+ }
+ }
+
+ let mut inst = match PDBParserInstance::new(debug_info, view, pdb) {
+ Ok(inst) => {
+ info!("Loaded PDB, parsing...");
+ inst
+ }
+ Err(e) => {
+ error!("Could not open PDB: {}", e);
+ return Err(e);
+ }
+ };
+ match inst.try_parse_info(Box::new(|cur, max| {
+ (*progress)(cur, max).map_err(|_| anyhow!("Cancelled"))
+ })) {
+ Ok(()) => {
+ info!("Parsed pdb");
+ Ok(())
+ }
+ Err(e) => {
+ error!("Could not parse PDB: {}", e);
+ if e.to_string() == "Todo" {
+ Ok(())
+ } else {
+ Err(e)
+ }
+ }
+ }
+ }
+}
+
+impl CustomDebugInfoParser for PDBParser {
+ fn is_valid(&self, view: &BinaryView) -> bool {
+ view.type_name().to_string() == "PE" || is_pdb(view)
+ }
+
+ fn parse_info(
+ &self,
+ debug_info: &mut DebugInfo,
+ view: &BinaryView,
+ debug_file: &BinaryView,
+ progress: Box<dyn Fn(usize, usize) -> Result<(), ()>>,
+ ) -> bool {
+ if is_pdb(debug_file) {
+ match self.load_from_file(
+ &debug_file.read_vec(0, debug_file.len() as usize),
+ debug_info,
+ view,
+ &progress,
+ false,
+ false,
+ ) {
+ Ok(_) => return true,
+ Err(e) if e.to_string() == "Cancelled" => return false,
+ Err(_) => {
+ error!("Chosen PDB file failed to load");
+ return false;
+ }
+ }
+ }
+
+ // See if we can get pdb info from the view
+ if let Some(info) = parse_pdb_info(view) {
+ // First, check _NT_SYMBOL_PATH
+ if let Ok(sym_path) = env::var("_NT_SYMBOL_PATH") {
+ let stores = if let Ok(default_cache) = active_local_cache(Some(view)) {
+ parse_sym_srv(&sym_path, default_cache)
+ } else {
+ Err(anyhow!("No local cache found"))
+ };
+ if let Ok(stores) = stores {
+ for store in stores {
+ match search_sym_store(view, store.clone(), &info) {
+ Ok(Some(conts)) => {
+ match self
+ .load_from_file(&conts, debug_info, view, &progress, true, true)
+ {
+ Ok(_) => return true,
+ Err(e) if e.to_string() == "Cancelled" => return false,
+ Err(e) => debug!("Skipping, {}", e.to_string()),
+ }
+ }
+ Ok(None) => {}
+ e => error!("Error searching symbol store {}: {:?}", store, e),
+ }
+ }
+ }
+ }
+
+ // Does the raw path just exist?
+ if PathBuf::from(&info.path).exists() {
+ match fs::read(&info.path) {
+ Ok(conts) => match self
+ .load_from_file(&conts, debug_info, view, &progress, true, false)
+ {
+ Ok(_) => return true,
+ Err(e) if e.to_string() == "Cancelled" => return false,
+ Err(e) => debug!("Skipping, {}", e.to_string()),
+ },
+ Err(e) if e.to_string() == "Cancelled" => return false,
+ Err(e) => debug!("Could not read pdb: {}", e.to_string()),
+ }
+ }
+
+ // Try in the same directory as the file
+ let mut potential_path = PathBuf::from(view.file().filename().to_string());
+ potential_path.pop();
+ potential_path.push(&info.file_name);
+ if potential_path.exists() {
+ match fs::read(
+ potential_path
+ .to_str()
+ .expect("Potential path is a real string"),
+ ) {
+ Ok(conts) => match self
+ .load_from_file(&conts, debug_info, view, &progress, true, false)
+ {
+ Ok(_) => return true,
+ Err(e) if e.to_string() == "Cancelled" => return false,
+ Err(e) => debug!("Skipping, {}", e.to_string()),
+ },
+ Err(e) if e.to_string() == "Cancelled" => return false,
+ Err(e) => debug!("Could not read pdb: {}", e.to_string()),
+ }
+ }
+
+ // Check the local symbol store
+ if let Ok(local_store_path) = active_local_cache(Some(view)) {
+ match search_sym_store(view, local_store_path.clone(), &info) {
+ Ok(Some(conts)) => {
+ match self.load_from_file(&conts, debug_info, view, &progress, true, false)
+ {
+ Ok(_) => return true,
+ Err(e) if e.to_string() == "Cancelled" => return false,
+ Err(e) => debug!("Skipping, {}", e.to_string()),
+ }
+ }
+ Ok(None) => {}
+ e => error!(
+ "Error searching local symbol store {}: {:?}",
+ local_store_path, e
+ ),
+ }
+ }
+
+ // Next, try downloading from all symbol servers in the server list
+ let mut query_options = QueryOptions::new_with_view(view);
+ let server_list = Settings::new()
+ .get_string_list_with_opts("pdb.files.symbolServerList", &mut query_options);
+
+ for server in server_list.iter() {
+ match search_sym_store(view, server.to_string(), &info) {
+ Ok(Some(conts)) => {
+ match self.load_from_file(&conts, debug_info, view, &progress, true, true) {
+ Ok(_) => return true,
+ Err(e) if e.to_string() == "Cancelled" => return false,
+ Err(e) => debug!("Skipping, {}", e.to_string()),
+ }
+ }
+ Ok(None) => {}
+ e => error!("Error searching remote symbol server {}: {:?}", server, e),
+ }
+ }
+ }
+ false
+ }
+}
+
+#[cfg(not(feature = "demo"))]
+#[no_mangle]
+pub extern "C" fn CorePluginDependencies() {
+ use binaryninja::add_optional_plugin_dependency;
+ add_optional_plugin_dependency("view_pe");
+}
+
+#[cfg(not(feature = "demo"))]
+#[no_mangle]
+pub extern "C" fn CorePluginInit() -> bool {
+ init_plugin()
+}
+
+#[cfg(feature = "demo")]
+#[no_mangle]
+pub extern "C" fn PDBPluginInit() -> bool {
+ init_plugin()
+}
+
+fn init_plugin() -> bool {
+ Logger::new("PDB").init();
+ DebugInfoParser::register("PDB", PDBParser {});
+
+ let settings = Settings::new();
+ settings.register_group("pdb", "PDB Loader");
+ settings.register_setting_json(
+ "pdb.files.localStoreAbsolute",
+ r#"{
+ "title" : "Local Symbol Store Absolute Path",
+ "type" : "string",
+ "default" : "",
+ "aliases" : ["pdb.local-store-absolute", "pdb.localStoreAbsolute"],
+ "description" : "Absolute path specifying where the PDB symbol store exists on this machine, overrides relative path.",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "pdb.files.localStoreRelative",
+ r#"{
+ "title" : "Local Symbol Store Relative Path",
+ "type" : "string",
+ "default" : "symbols",
+ "aliases" : ["pdb.local-store-relative", "pdb.localStoreRelative"],
+ "description" : "Path *relative* to the binaryninja _user_ directory, specifying the pdb symbol store. If the Local Symbol Store Absolute Path is specified, this is ignored.",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "pdb.files.localStoreCache",
+ r#"{
+ "title" : "Cache Downloaded PDBs in Local Store",
+ "type" : "boolean",
+ "default" : true,
+ "aliases" : ["pdb.localStoreCache"],
+ "description" : "Store PDBs downloaded from Symbol Servers in the local Symbol Store Path.",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "network.pdbAutoDownload",
+ r#"{
+ "title" : "Enable Auto Downloading PDBs",
+ "type" : "boolean",
+ "default" : true,
+ "aliases" : ["pdb.autoDownload", "pdb.auto-download-pdb"],
+ "description" : "Automatically search for and download pdb files from specified symbol servers.",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "pdb.files.symbolServerList",
+ r#"{
+ "title" : "Symbol Server List",
+ "type" : "array",
+ "sorted" : false,
+ "default" : ["https://msdl.microsoft.com/download/symbols"],
+ "aliases" : ["pdb.symbol-server-list", "pdb.symbolServerList"],
+ "description" : "List of servers to query for pdb symbols.",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "pdb.features.expandRTTIStructures",
+ r#"{
+ "title" : "Expand RTTI Structures",
+ "type" : "boolean",
+ "default" : true,
+ "aliases" : ["pdb.expandRTTIStructures"],
+ "description" : "Create structures for RTTI symbols with variable-sized names and arrays.",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "pdb.features.generateVTables",
+ r#"{
+ "title" : "Generate Virtual Table Structures",
+ "type" : "boolean",
+ "default" : true,
+ "aliases" : ["pdb.generateVTables"],
+ "description" : "Create Virtual Table (VTable) structures for C++ classes found when parsing.",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "pdb.features.loadGlobalSymbols",
+ r#"{
+ "title" : "Load Global Module Symbols",
+ "type" : "boolean",
+ "default" : true,
+ "aliases" : ["pdb.loadGlobalSymbols"],
+ "description" : "Load symbols in the Global module of the PDB. These symbols have generally lower quality types due to relying on the demangler.",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "pdb.features.allowUnnamedVoidSymbols",
+ r#"{
+ "title" : "Allow Unnamed Untyped Symbols",
+ "type" : "boolean",
+ "default" : false,
+ "aliases" : ["pdb.allowUnnamedVoidSymbols"],
+ "description" : "Allow creation of symbols with no name and void types, often used as static local variables. Generally, these are just noisy and not relevant.",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "pdb.features.allowVoidGlobals",
+ r#"{
+ "title" : "Allow Untyped Symbols",
+ "type" : "boolean",
+ "default" : true,
+ "aliases" : ["pdb.allowVoidGlobals"],
+ "description" : "Allow creation of symbols that have no type, and will be created as void-typed symbols. Generally, this happens in a stripped PDB when a Global symbol's mangled name does not contain type information.",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "pdb.features.createMissingNamedTypes",
+ r#"{
+ "title" : "Create Missing Named Types",
+ "type" : "boolean",
+ "default" : true,
+ "aliases" : ["pdb.createMissingNamedTypes"],
+ "description" : "Allow creation of types named by function signatures which are not found in the PDB's types list or the Binary View. These types are usually found in stripped PDBs that have no type information but function signatures reference the stripped types.",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "pdb.features.loadMismatchedPDB",
+ r#"{
+ "title" : "Load Mismatched PDB",
+ "type" : "string",
+ "default" : "ask",
+ "enum" : ["true", "ask", "false"],
+ "enumDescriptions" : [
+ "Always load the PDB",
+ "Use the Interaction system to ask if the PDB should be loaded",
+ "Never load the PDB"
+ ],
+ "aliases" : [],
+ "description" : "If a manually loaded PDB has a mismatched GUID, should it be loaded?",
+ "ignore" : []
+ }"#,
+ );
+
+ settings.register_setting_json(
+ "pdb.features.parseSymbols",
+ r#"{
+ "title" : "Parse PDB Symbols",
+ "type" : "boolean",
+ "default" : true,
+ "aliases" : [],
+ "description" : "Parse Symbol names and types. If you turn this off, you will only load Types.",
+ "ignore" : []
+ }"#,
+ );
+
+ true
+}
+
+#[test]
+fn test_default_cache_path() {
+ println!("{:?}", default_local_cache());
+}
+
+#[test]
+fn test_sym_srv() {
+ assert_eq!(
+ parse_sym_srv(
+ &r"srv*\\mybuilds\mysymbols".to_string(),
+ r"DEFAULT_STORE".to_string()
+ )
+ .expect("parse success")
+ .collect::<Vec<_>>(),
+ vec![r"\\mybuilds\mysymbols".to_string()]
+ );
+ assert_eq!(
+ parse_sym_srv(
+ &r"srv*c:\localsymbols*\\mybuilds\mysymbols".to_string(),
+ r"DEFAULT_STORE".to_string()
+ )
+ .expect("parse success")
+ .collect::<Vec<_>>(),
+ vec![
+ r"c:\localsymbols".to_string(),
+ r"\\mybuilds\mysymbols".to_string()
+ ]
+ );
+ assert_eq!(
+ parse_sym_srv(
+ &r"srv**\\mybuilds\mysymbols".to_string(),
+ r"DEFAULT_STORE".to_string()
+ )
+ .expect("parse success")
+ .collect::<Vec<_>>(),
+ vec![
+ r"DEFAULT_STORE".to_string(),
+ r"\\mybuilds\mysymbols".to_string()
+ ]
+ );
+ assert_eq!(
+ parse_sym_srv(
+ &r"srv*c:\localsymbols*\\NearbyServer\store*https://DistantServer".to_string(),
+ r"DEFAULT_STORE".to_string()
+ )
+ .expect("parse success")
+ .collect::<Vec<_>>(),
+ vec![
+ r"c:\localsymbols".to_string(),
+ r"\\NearbyServer\store".to_string(),
+ r"https://DistantServer".to_string()
+ ]
+ );
+ assert_eq!(
+ parse_sym_srv(
+ &r"srv*c:\DownstreamStore*https://msdl.microsoft.com/download/symbols".to_string(),
+ r"DEFAULT_STORE".to_string()
+ )
+ .expect("parse success")
+ .collect::<Vec<_>>(),
+ vec![
+ r"c:\DownstreamStore".to_string(),
+ r"https://msdl.microsoft.com/download/symbols".to_string()
+ ]
+ );
+}
diff --git a/plugins/pdb-ng/src/parser.rs b/plugins/pdb-ng/src/parser.rs
new file mode 100644
index 00000000..00b44e70
--- /dev/null
+++ b/plugins/pdb-ng/src/parser.rs
@@ -0,0 +1,505 @@
+// Copyright 2022-2024 Vector 35 Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use std::collections::{BTreeMap, HashMap, HashSet};
+use std::env;
+use std::fmt::Display;
+use std::sync::OnceLock;
+
+use anyhow::{anyhow, Result};
+use log::{debug, info};
+use pdb::*;
+
+use crate::symbol_parser::{ParsedDataSymbol, ParsedProcedure, ParsedSymbol};
+use crate::type_parser::ParsedType;
+use binaryninja::architecture::{Architecture, CoreArchitecture};
+use binaryninja::binary_view::{BinaryView, BinaryViewExt};
+use binaryninja::calling_convention::CoreCallingConvention;
+use binaryninja::confidence::{Conf, MIN_CONFIDENCE};
+use binaryninja::debuginfo::{DebugFunctionInfo, DebugInfo};
+use binaryninja::platform::Platform;
+use binaryninja::rc::Ref;
+use binaryninja::settings::{QueryOptions, Settings};
+use binaryninja::types::{
+ EnumerationBuilder, NamedTypeReference, NamedTypeReferenceClass, QualifiedName,
+ StructureBuilder, StructureType, Type, TypeClass,
+};
+use binaryninja::variable::NamedDataVariableWithType;
+
+/// Megastruct for all the parsing
+/// Certain fields are only used by specific files, as marked below.
+/// Why not make new structs for them? Because vvvv this garbage
+pub struct PDBParserInstance<'a, S: Source<'a> + 'a> {
+ /// DebugInfo where types/functions will be stored eventually
+ pub(crate) debug_info: &'a mut DebugInfo,
+ /// Parent binary view (usually during BinaryView::Finalize)
+ pub(crate) bv: &'a BinaryView,
+ /// Default arch of self.bv
+ pub(crate) arch: CoreArchitecture,
+ /// Default calling convention for self.arch
+ pub(crate) default_cc: Ref<CoreCallingConvention>,
+ /// Thiscall calling convention for self.bv, or default_cc if we can't find one
+ pub(crate) thiscall_cc: Ref<CoreCallingConvention>,
+ /// Cdecl calling convention for self.bv, or default_cc if we can't find one
+ pub(crate) cdecl_cc: Ref<CoreCallingConvention>,
+ /// Default platform of self.bv
+ pub(crate) platform: Ref<Platform>,
+ /// pdb-rs structure for making lifetime hell a real place
+ pub(crate) pdb: PDB<'a, S>,
+ /// pdb-rs Mapping of modules to addresses for resolving RVAs
+ pub(crate) address_map: AddressMap<'a>,
+ /// Binja Settings instance (for optimization)
+ pub(crate) settings: Ref<Settings>,
+ /// Binja Settings query instance (for optimization)
+ pub(crate) settings_query_opts: QueryOptions<'a>,
+
+ /// type_parser.rs
+
+ /// TypeIndex -> ParsedType enum used during parsing
+ pub(crate) indexed_types: BTreeMap<TypeIndex, ParsedType>,
+ /// QName -> Binja Type for finished types
+ pub(crate) named_types: BTreeMap<QualifiedName, Ref<Type>>,
+ /// Raw (mangled) name -> TypeIndex for resolving forward references
+ pub(crate) full_type_indices: BTreeMap<String, TypeIndex>,
+ /// Stack of types we're currently parsing
+ pub(crate) type_stack: Vec<TypeIndex>,
+ /// Stack of parent types we're parsing nested types inside of
+ pub(crate) namespace_stack: QualifiedName,
+ /// Type Index -> Does it return on the stack
+ pub(crate) type_default_returnable: BTreeMap<TypeIndex, bool>,
+
+ /// symbol_parser.rs
+
+ /// List of fully parsed symbols from all modules
+ pub(crate) parsed_symbols: Vec<ParsedSymbol>,
+ /// Raw name -> index in parsed_symbols
+ pub(crate) parsed_symbols_by_name: BTreeMap<String, usize>,
+ /// Raw name -> Symbol index for looking up symbols for the currently parsing module (mostly for thunks)
+ pub(crate) named_symbols: BTreeMap<String, SymbolIndex>,
+ /// Parent -> Children symbol index tree for the currently parsing module
+ pub(crate) symbol_tree: BTreeMap<SymbolIndex, Vec<SymbolIndex>>,
+ /// Child -> Parent symbol index mapping, inverse of symbol_tree
+ pub(crate) symbol_parents: BTreeMap<SymbolIndex, SymbolIndex>,
+ /// Stack of (start, end) indices for the current symbols being parsed while constructing the tree
+ pub(crate) symbol_stack: Vec<(SymbolIndex, SymbolIndex)>,
+ /// Index -> parsed symbol for the currently parsing module
+ pub(crate) indexed_symbols: BTreeMap<SymbolIndex, ParsedSymbol>,
+ /// Symbol address -> Symbol for looking up by address
+ pub(crate) addressed_symbols: BTreeMap<u64, Vec<ParsedSymbol>>,
+ /// CPU type of the currently parsing module
+ pub(crate) module_cpu_type: Option<CPUType>,
+}
+
+impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
+ /// Try to create a new parser instance from a given bv/pdb
+ pub fn new(
+ debug_info: &'a mut DebugInfo,
+ bv: &'a BinaryView,
+ mut pdb: PDB<'a, S>,
+ ) -> Result<Self> {
+ let arch = if let Some(arch) = bv.default_arch() {
+ arch
+ } else {
+ return Err(anyhow!("Cannot parse to view with no architecture"));
+ };
+
+ let platform = bv
+ .default_platform()
+ .expect("Expected bv to have a platform");
+
+ let address_map = pdb.address_map()?;
+
+ let default_cc = platform
+ .get_default_calling_convention()
+ .expect("Expected default calling convention");
+
+ let thiscall_cc = Self::find_calling_convention(platform.as_ref(), "thiscall")
+ .unwrap_or(default_cc.clone());
+
+ let cdecl_cc = platform
+ .get_cdecl_calling_convention()
+ .unwrap_or(default_cc.clone());
+
+ Ok(Self {
+ debug_info,
+ bv,
+ arch,
+ default_cc,
+ thiscall_cc,
+ cdecl_cc,
+ platform,
+ pdb,
+ address_map,
+ settings: Settings::new(),
+ settings_query_opts: QueryOptions::new_with_view(bv),
+ indexed_types: Default::default(),
+ named_types: Default::default(),
+ full_type_indices: Default::default(),
+ type_stack: Default::default(),
+ namespace_stack: Default::default(),
+ type_default_returnable: Default::default(),
+ parsed_symbols: Default::default(),
+ parsed_symbols_by_name: Default::default(),
+ named_symbols: Default::default(),
+ symbol_tree: Default::default(),
+ symbol_parents: Default::default(),
+ symbol_stack: Default::default(),
+ indexed_symbols: Default::default(),
+ addressed_symbols: Default::default(),
+ module_cpu_type: None,
+ })
+ }
+
+ /// Try to parse the pdb into the DebugInfo
+ pub fn try_parse_info(
+ &mut self,
+ progress: Box<dyn Fn(usize, usize) -> Result<()> + 'a>,
+ ) -> Result<()> {
+ self.parse_types(Self::split_progress(&progress, 0, &[1.0, 3.0, 0.5, 0.5]))?;
+ for (name, ty) in self.named_types.iter() {
+ self.debug_info.add_type(name, ty.as_ref(), &[]); // TODO : Components
+ }
+
+ info!(
+ "PDB found {} types (before resolving NTRs)",
+ self.named_types.len()
+ );
+
+ if self
+ .settings
+ .get_bool_with_opts("pdb.features.parseSymbols", &mut self.settings_query_opts)
+ {
+ let (symbols, functions) =
+ self.parse_symbols(Self::split_progress(&progress, 1, &[1.0, 3.0, 0.5, 0.5]))?;
+
+ if self.settings.get_bool_with_opts(
+ "pdb.features.createMissingNamedTypes",
+ &mut self.settings_query_opts,
+ ) {
+ self.resolve_missing_ntrs(
+ &symbols,
+ Self::split_progress(&progress, 2, &[1.0, 3.0, 0.5, 0.5]),
+ )?;
+ self.resolve_missing_ntrs(
+ &functions,
+ Self::split_progress(&progress, 3, &[1.0, 3.0, 0.5, 0.5]),
+ )?;
+ }
+
+ info!("PDB found {} types", self.named_types.len());
+ info!("PDB found {} data variables", symbols.len());
+ info!("PDB found {} functions", functions.len());
+
+ let allow_void = self.settings.get_bool_with_opts(
+ "pdb.features.allowVoidGlobals",
+ &mut self.settings_query_opts,
+ );
+
+ let min_confidence_type = Conf::new(Type::void(), MIN_CONFIDENCE);
+ for sym in symbols {
+ match sym {
+ ParsedSymbol::Data(ParsedDataSymbol {
+ address,
+ name,
+ type_,
+ ..
+ }) => {
+ let real_type = type_.as_ref().unwrap_or(&min_confidence_type);
+
+ if real_type.contents.type_class() == TypeClass::VoidTypeClass {
+ if !allow_void {
+ self.log(|| {
+ format!("Not adding void-typed symbol {:?}@{:x}", name, address)
+ });
+ continue;
+ }
+ }
+
+ self.log(|| {
+ format!(
+ "Adding data variable: 0x{:x}: {} {:?}",
+ address, &name.raw_name, real_type
+ )
+ });
+ self.debug_info
+ .add_data_variable_info(NamedDataVariableWithType::new(
+ address,
+ real_type.clone(),
+ name.full_name.unwrap_or(name.raw_name),
+ true,
+ ));
+ }
+ s => {
+ self.log(|| format!("Not adding non-data symbol {:?}", s));
+ }
+ }
+ }
+
+ for sym in functions {
+ match sym {
+ ParsedSymbol::Procedure(ParsedProcedure {
+ address,
+ name,
+ type_,
+ locals: _,
+ ..
+ }) => {
+ self.log(|| {
+ format!(
+ "Adding function: 0x{:x}: {} {:?}",
+ address, &name.raw_name, type_
+ )
+ });
+ self.debug_info.add_function(DebugFunctionInfo::new(
+ Some(name.short_name.unwrap_or(name.raw_name.clone())),
+ Some(name.full_name.unwrap_or(name.raw_name.clone())),
+ Some(name.raw_name),
+ type_.clone().and_then(|conf| {
+ // TODO: When DebugInfo support confidence on function types, remove this
+ if conf.confidence == 0 {
+ None
+ } else {
+ Some(conf.contents)
+ }
+ }),
+ Some(address),
+ Some(self.platform.clone()),
+ vec![], // TODO : Components
+ vec![], //TODO: local variables
+ ));
+ }
+ _ => {}
+ }
+ }
+ }
+
+ Ok(())
+ }
+
+ fn collect_name(
+ &self,
+ name: &NamedTypeReference,
+ unknown_names: &mut HashMap<QualifiedName, NamedTypeReferenceClass>,
+ ) {
+ let used_name = name.name();
+ if let Some(&found) = unknown_names.get(&used_name) {
+ if found != name.class() {
+ // Interesting case, not sure we care
+ self.log(|| {
+ format!(
+ "Mismatch unknown NTR class for {}: {} ?",
+ &used_name,
+ name.class() as u32
+ )
+ });
+ }
+ } else {
+ self.log(|| format!("Found new unused name: {}", &used_name));
+ unknown_names.insert(used_name, name.class());
+ }
+ }
+
+ fn collect_names(
+ &self,
+ ty: &Type,
+ unknown_names: &mut HashMap<QualifiedName, NamedTypeReferenceClass>,
+ ) {
+ match ty.type_class() {
+ TypeClass::StructureTypeClass => {
+ if let Some(structure) = ty.get_structure() {
+ for member in structure.members() {
+ self.collect_names(member.ty.contents.as_ref(), unknown_names);
+ }
+ for base in structure.base_structures() {
+ self.collect_name(base.ty.as_ref(), unknown_names);
+ }
+ }
+ }
+ TypeClass::PointerTypeClass => {
+ if let Some(target) = ty.target() {
+ self.collect_names(target.contents.as_ref(), unknown_names);
+ }
+ }
+ TypeClass::ArrayTypeClass => {
+ if let Some(element_type) = ty.element_type() {
+ self.collect_names(element_type.contents.as_ref(), unknown_names);
+ }
+ }
+ TypeClass::FunctionTypeClass => {
+ if let Some(return_value) = ty.return_value() {
+ self.collect_names(return_value.contents.as_ref(), unknown_names);
+ }
+ if let Some(params) = ty.parameters() {
+ for param in params {
+ self.collect_names(param.ty.contents.as_ref(), unknown_names);
+ }
+ }
+ }
+ TypeClass::NamedTypeReferenceClass => {
+ if let Some(ntr) = ty.get_named_type_reference() {
+ self.collect_name(ntr.as_ref(), unknown_names);
+ }
+ }
+ _ => {}
+ }
+ }
+
+ fn resolve_missing_ntrs(
+ &mut self,
+ symbols: &Vec<ParsedSymbol>,
+ progress: Box<dyn Fn(usize, usize) -> Result<()> + '_>,
+ ) -> Result<()> {
+ let mut unknown_names = HashMap::new();
+ let mut known_names = self
+ .bv
+ .types()
+ .iter()
+ .map(|qnat| qnat.name)
+ .collect::<HashSet<_>>();
+
+ for ty in &self.named_types {
+ known_names.insert(ty.0.clone());
+ }
+
+ let count = symbols.len();
+ for (i, sym) in symbols.iter().enumerate() {
+ match sym {
+ ParsedSymbol::Data(ParsedDataSymbol {
+ type_: Some(type_), ..
+ }) => {
+ self.collect_names(type_.contents.as_ref(), &mut unknown_names);
+ }
+ ParsedSymbol::Procedure(ParsedProcedure {
+ type_: Some(type_),
+ locals,
+ ..
+ }) => {
+ self.collect_names(type_.contents.as_ref(), &mut unknown_names);
+ for l in locals {
+ if let Some(ltype) = &l.type_ {
+ self.collect_names(ltype.contents.as_ref(), &mut unknown_names);
+ }
+ }
+ }
+ _ => {}
+ }
+ progress(i, count)?;
+ }
+
+ for (name, class) in unknown_names.into_iter() {
+ if known_names.contains(&name) {
+ self.log(|| format!("Found referenced name and ignoring: {}", &name));
+ continue;
+ }
+ self.log(|| format!("Adding referenced but unknown type {} (likely due to demangled name and stripped type)", &name));
+ match class {
+ NamedTypeReferenceClass::UnknownNamedTypeClass
+ | NamedTypeReferenceClass::TypedefNamedTypeClass => {
+ self.debug_info.add_type(&name, Type::void().as_ref(), &[]);
+ // TODO : Components
+ }
+ NamedTypeReferenceClass::ClassNamedTypeClass
+ | NamedTypeReferenceClass::StructNamedTypeClass
+ | NamedTypeReferenceClass::UnionNamedTypeClass => {
+ let mut structure = StructureBuilder::new();
+ match class {
+ NamedTypeReferenceClass::ClassNamedTypeClass => {
+ structure.structure_type(StructureType::ClassStructureType);
+ }
+ NamedTypeReferenceClass::StructNamedTypeClass => {
+ structure.structure_type(StructureType::StructStructureType);
+ }
+ NamedTypeReferenceClass::UnionNamedTypeClass => {
+ structure.structure_type(StructureType::UnionStructureType);
+ }
+ _ => {}
+ }
+ structure.width(1);
+ structure.alignment(1);
+
+ self.debug_info.add_type(
+ &name,
+ Type::structure(structure.finalize().as_ref()).as_ref(),
+ &[], // TODO : Components
+ );
+ }
+ NamedTypeReferenceClass::EnumNamedTypeClass => {
+ let enumeration = EnumerationBuilder::new();
+ self.debug_info.add_type(
+ &name,
+ Type::enumeration(
+ enumeration.finalize().as_ref(),
+ self.arch.default_integer_size().try_into()?,
+ false,
+ )
+ .as_ref(),
+ &[], // TODO : Components
+ );
+ }
+ }
+ }
+
+ Ok(())
+ }
+
+ /// Lazy logging function that prints like 20MB of messages
+ pub(crate) fn log<F: FnOnce() -> D, D: Display>(&self, msg: F) {
+ static MEM: OnceLock<bool> = OnceLock::new();
+ let debug_pdb = MEM.get_or_init(|| env::var("BN_DEBUG_PDB").is_ok());
+ if *debug_pdb {
+ let space = "\t".repeat(self.type_stack.len()) + &"\t".repeat(self.symbol_stack.len());
+ let msg = format!("{}", msg());
+ debug!(
+ "{}{}",
+ space,
+ msg.replace("\n", &("\n".to_string() + &space))
+ );
+ }
+ }
+
+ pub(crate) fn split_progress<'b, F: Fn(usize, usize) -> Result<()> + 'b>(
+ original_fn: F,
+ subpart: usize,
+ subpart_weights: &[f64],
+ ) -> Box<dyn Fn(usize, usize) -> Result<()> + 'b> {
+ // Normalize weights
+ let weight_sum: f64 = subpart_weights.iter().sum();
+ if weight_sum < 0.0001 {
+ return Box::new(|_, _| Ok(()));
+ }
+
+ // Keep a running count of weights for the start
+ let mut subpart_starts = vec![];
+ let mut start = 0f64;
+ for w in subpart_weights {
+ subpart_starts.push(start);
+ start += *w;
+ }
+
+ let subpart_start = subpart_starts[subpart] / weight_sum;
+ let weight = subpart_weights[subpart] / weight_sum;
+
+ Box::new(move |cur: usize, max: usize| {
+ // Just use a large number for easy divisibility
+ let steps = 1000000f64;
+ let subpart_size = steps * weight;
+ let subpart_progress = ((cur as f64) / (max as f64)) * subpart_size;
+
+ original_fn(
+ (subpart_start * steps + subpart_progress) as usize,
+ steps as usize,
+ )
+ })
+ }
+}
diff --git a/plugins/pdb-ng/src/struct_grouper.rs b/plugins/pdb-ng/src/struct_grouper.rs
new file mode 100644
index 00000000..219f5198
--- /dev/null
+++ b/plugins/pdb-ng/src/struct_grouper.rs
@@ -0,0 +1,1164 @@
+// Copyright 2022-2024 Vector 35 Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use anyhow::{anyhow, Result};
+use binaryninja::confidence::{Conf, MAX_CONFIDENCE};
+use binaryninja::types::{MemberAccess, MemberScope, StructureBuilder, StructureType, Type};
+use log::{debug, warn};
+use std::cmp::Ordering;
+use std::env;
+use std::fmt::{Debug, Display, Formatter};
+
+use crate::type_parser::ParsedMember;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+struct MemberSize {
+ index: usize,
+ offset: u64,
+ width: u64,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+enum ResolvedGroup {
+ Single(usize),
+ Struct(u64, Vec<ResolvedGroup>),
+ Union(u64, Vec<ResolvedGroup>),
+}
+
+#[derive(Clone, PartialEq, Eq)]
+struct WorkingStruct {
+ index: Option<usize>,
+ offset: u64,
+ width: u64,
+ is_union: bool,
+ children: Vec<WorkingStruct>,
+}
+
+impl PartialOrd<Self> for WorkingStruct {
+ fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+ if self.end() < other.start() {
+ Some(Ordering::Less)
+ } else if other.end() < self.start() {
+ Some(Ordering::Greater)
+ } else if self.is_same(other) {
+ Some(Ordering::Equal)
+ } else {
+ None
+ }
+ }
+}
+
+impl Debug for WorkingStruct {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ if self.children.is_empty() {
+ write!(f, "{:X} -> {:X}", self.start(), self.end())?;
+ if let Some(index) = self.index {
+ write!(f, " (#{:X})", index)?;
+ } else {
+ write!(f, " without index???")?;
+ }
+ Ok(())
+ } else if self.is_union {
+ write!(f, "union {:X} -> {:X} ", self.start(), self.end())?;
+ if let Some(index) = self.index {
+ write!(f, "with index {:X} ??? ", index)?;
+ }
+ f.debug_list().entries(self.children.iter()).finish()
+ } else {
+ write!(f, "struct {:X} -> {:X} ", self.start(), self.end())?;
+ if let Some(index) = self.index {
+ write!(f, "with index {:X} ??? ", index)?;
+ }
+ f.debug_list().entries(self.children.iter()).finish()
+ }
+ }
+}
+
+impl WorkingStruct {
+ pub fn start(&self) -> u64 {
+ self.offset
+ }
+
+ pub fn end(&self) -> u64 {
+ self.offset + self.width
+ }
+
+ pub fn extend_to(&mut self, new_end: u64) {
+ if new_end > self.end() {
+ self.width = new_end - self.offset;
+ }
+ }
+
+ // pub fn overlaps(&self, other: &WorkingStruct) -> bool {
+ // // If A starts after B ends
+ // if self.start() >= other.end() {
+ // return false;
+ // }
+ // // Or if B starts after A ends
+ // if other.start() >= self.end() {
+ // return false;
+ // }
+ // // Otherwise, one of the items starts before the other ends, so there is overlap
+ // return true;
+ // }
+
+ // pub fn contains(&self, other: &WorkingStruct) -> bool {
+ // // If other is fully contained within self
+ // self.start() <= other.start() && self.end() >= other.end()
+ // }
+
+ pub fn is_same(&self, other: &WorkingStruct) -> bool {
+ // If self and other have the same range
+ self.start() == other.start() && self.end() == other.end()
+ }
+
+ pub fn insert(&mut self, other: WorkingStruct, recursion: usize) -> Result<()> {
+ log(|| {
+ format!("{}self: {:#?}", " ".repeat(recursion), self)
+ .replace("\n", &("\n".to_owned() + &" ".repeat(recursion)))
+ });
+ log(|| {
+ format!("{}other: {:#?}", " ".repeat(recursion), other)
+ .replace("\n", &("\n".to_owned() + &" ".repeat(recursion)))
+ });
+
+ self.extend_to(other.end());
+
+ // There are 2 cases we have to deal with here:
+ // a. `other` starts after the end of the last group => insert `other` into the last group
+ // b. `other` starts before the end of the last group => collect all the children inserted after it starts and put them into a struct
+ // start a new struct with `other`
+
+ if self.children.is_empty() {
+ self.children.push(other);
+ return Ok(());
+ }
+
+ // This is really gross.
+ // But also I need to ship this before I leave for France
+ // TODO: Clean this up
+
+ if other.start()
+ >= self
+ .children
+ .last()
+ .ok_or_else(|| anyhow!("Expected we have children #A"))?
+ .end()
+ {
+ self.children.push(other);
+ } else {
+ // Create a structure with fields from self.children
+ if self
+ .children
+ .last()
+ .ok_or_else(|| anyhow!("Expected we have children #B"))?
+ .index
+ .is_none()
+ && self
+ .children
+ .last()
+ .ok_or_else(|| anyhow!("Expected we have children #C"))?
+ .start()
+ < other.start()
+ {
+ self.children
+ .last_mut()
+ .ok_or_else(|| anyhow!("Expected we have children #D"))?
+ .insert(other, recursion + 1)?;
+ return Ok(());
+ }
+
+ // If we're a union, we don't have to bother pushing a struct+union combo
+ if self.is_union {
+ self.children.push(WorkingStruct {
+ index: None,
+ offset: self.offset,
+ width: self.width,
+ is_union: false,
+ children: vec![other],
+ });
+ return Ok(());
+ }
+
+ let mut start_index = None;
+ for (i, child) in self.children.iter().enumerate() {
+ if child.start() >= other.start() {
+ start_index = Some(i);
+ break;
+ }
+ }
+ if start_index.is_none() {
+ return Err(anyhow!(
+ "Struct has overlapping member that cannot be resolved: {:#?}",
+ other
+ ));
+ }
+
+ let struct_start = self.children
+ [start_index.ok_or_else(|| anyhow!("Expected we have start index"))?]
+ .offset;
+ let struct_end = self
+ .children
+ .last()
+ .ok_or_else(|| anyhow!("Expected we have start index"))?
+ .end()
+ .max(other.end());
+
+ let struct_children = self
+ .children
+ .drain(start_index.ok_or_else(|| anyhow!("Expected we have start index"))?..)
+ .collect::<Vec<_>>();
+ self.children.push(WorkingStruct {
+ index: None,
+ offset: struct_start,
+ width: struct_end - struct_start,
+ is_union: true,
+ children: vec![
+ WorkingStruct {
+ index: None,
+ offset: struct_start,
+ width: struct_end - struct_start,
+ is_union: false,
+ children: struct_children,
+ },
+ WorkingStruct {
+ index: None,
+ offset: struct_start,
+ width: struct_end - struct_start,
+ is_union: false,
+ children: vec![other],
+ },
+ ],
+ });
+
+ // union {
+ // struct {
+ // int data0;
+ // int[2] data4;
+ // int dataC;
+ // };
+ // struct {
+ // int newdata0;
+ // ...
+ // };
+ // };
+ }
+
+ // if other.start() < self.children[-1].end() {
+ // take children from other.start() until -1 and put them into a struct
+ // }
+ // else {
+ // add to self.children[-1], extend to fill
+ // }
+
+ Ok(())
+ }
+
+ pub fn to_resolved(mut self) -> ResolvedGroup {
+ if let Some(index) = self.index {
+ ResolvedGroup::Single(index)
+ } else if self.is_union {
+ if self.children.len() == 1 {
+ self.children.remove(0).to_resolved()
+ } else {
+ // Collapse union of unions
+ ResolvedGroup::Union(
+ self.offset,
+ self.children
+ .into_iter()
+ .flat_map(|child| match child.to_resolved() {
+ ResolvedGroup::Union(offset, children) if offset == self.offset => {
+ children
+ }
+ s => vec![s],
+ })
+ .collect(),
+ )
+ }
+ } else {
+ if self.children.len() == 1 {
+ self.children.remove(0).to_resolved()
+ } else {
+ ResolvedGroup::Struct(
+ self.offset,
+ self.children
+ .into_iter()
+ .map(|child| child.to_resolved())
+ .collect(),
+ )
+ }
+ }
+ }
+}
+
+pub fn group_structure(
+ name: &String,
+ members: &Vec<ParsedMember>,
+ structure: &mut StructureBuilder,
+) -> Result<()> {
+ // SO
+ // PDBs handle trivial unions inside structures by just slamming all the fields together into
+ // one big overlappy happy family. We need to reverse this and create out union structures
+ // to properly represent the original source.
+
+ // IN VISUAL FORM (if you are a visual person, like me):
+ // struct {
+ // int foos[2];
+ // __offset(0):
+ // int foo1;
+ // int foo2;
+ // int bar;
+ // }
+ //
+ // Into
+ //
+ // struct {
+ // union {
+ // int foos[2];
+ // struct {
+ // int foo1;
+ // int foo2;
+ // }
+ // }
+ // int bar;
+ // }
+
+ // Into internal rep
+ let reps = members
+ .iter()
+ .enumerate()
+ .map(|(i, member)| MemberSize {
+ index: i,
+ offset: member.offset,
+ width: member.ty.contents.width(),
+ })
+ .collect::<Vec<_>>();
+
+ log(|| format!("{} {:#x?}", name, members));
+ log(|| format!("{} {:#x?}", name, reps));
+
+ // Group them
+ match resolve_struct_groups(reps) {
+ Ok(groups) => {
+ log(|| format!("{} {:#x?}", name, groups));
+
+ // Apply grouped members
+ apply_groups(members, structure, groups, 0);
+ }
+ Err(e) => {
+ warn!("{} Could not resolve structure groups: {}", name, e);
+ for member in members {
+ structure.insert(
+ &member.ty,
+ member.name.clone(),
+ member.offset,
+ false,
+ member.access,
+ member.scope,
+ );
+ }
+ }
+ }
+
+ Ok(())
+}
+
+fn apply_groups(
+ members: &Vec<ParsedMember>,
+ structure: &mut StructureBuilder,
+ groups: Vec<ResolvedGroup>,
+ offset: u64,
+) {
+ for (i, group) in groups.into_iter().enumerate() {
+ match group {
+ ResolvedGroup::Single(index) => {
+ let member = &members[index];
+
+ // TODO : Fix inner-offset being larger than `member.offset`
+
+ if offset > member.offset {
+ structure.insert(
+ &member.ty,
+ member.name.clone(),
+ 0,
+ false,
+ member.access,
+ member.scope,
+ );
+ } else {
+ structure.insert(
+ &member.ty,
+ member.name.clone(),
+ member.offset - offset,
+ false,
+ member.access,
+ member.scope,
+ );
+ }
+ }
+ ResolvedGroup::Struct(inner_offset, children) => {
+ let mut inner = StructureBuilder::new();
+ apply_groups(members, &mut inner, children, inner_offset);
+ structure.insert(
+ &Conf::new(Type::structure(inner.finalize().as_ref()), MAX_CONFIDENCE),
+ format!("__inner{}", i),
+ inner_offset - offset,
+ false,
+ MemberAccess::PublicAccess,
+ MemberScope::NoScope,
+ );
+ }
+ ResolvedGroup::Union(inner_offset, children) => {
+ let mut inner = StructureBuilder::new();
+ inner.structure_type(StructureType::UnionStructureType);
+ apply_groups(members, &mut inner, children, inner_offset);
+ structure.insert(
+ &Conf::new(Type::structure(inner.finalize().as_ref()), MAX_CONFIDENCE),
+ format!("__inner{}", i),
+ inner_offset - offset,
+ false,
+ MemberAccess::PublicAccess,
+ MemberScope::NoScope,
+ );
+ }
+ }
+ }
+}
+
+fn resolve_struct_groups(members: Vec<MemberSize>) -> Result<Vec<ResolvedGroup>> {
+ // See if we care
+ let mut has_overlapping = false;
+ let mut last_end = 0;
+ let mut max_width = 0;
+ for member in &members {
+ if member.offset < last_end {
+ has_overlapping = true;
+ }
+ last_end = member.offset + member.width;
+ max_width = max_width.max(member.offset + member.width);
+ }
+
+ // TODO: has overlapping is probably failing.
+ if !has_overlapping {
+ // Nothing overlaps, just add em directly
+ return Ok(members
+ .into_iter()
+ .map(|member| ResolvedGroup::Single(member.index))
+ .collect());
+ }
+
+ // Yes overlapping
+
+ let mut groups = WorkingStruct {
+ index: None,
+ offset: 0,
+ width: max_width,
+ is_union: false,
+ children: vec![],
+ };
+ for &member in &members {
+ let member_group = WorkingStruct {
+ index: Some(member.index),
+ offset: member.offset,
+ width: member.width,
+ is_union: false,
+ children: vec![],
+ };
+ groups.insert(member_group, 0)?;
+
+ log(|| format!("GROUPS: {:#x?}", groups));
+ }
+
+ Ok(groups
+ .children
+ .into_iter()
+ .map(|child| child.to_resolved())
+ .collect())
+}
+
+#[test]
+fn test_trivial() {
+ assert_eq!(
+ resolve_struct_groups(vec![
+ MemberSize {
+ index: 0,
+ offset: 0,
+ width: 1,
+ },
+ MemberSize {
+ index: 1,
+ offset: 1,
+ width: 1,
+ },
+ MemberSize {
+ index: 2,
+ offset: 2,
+ width: 1,
+ },
+ MemberSize {
+ index: 3,
+ offset: 3,
+ width: 1,
+ },
+ ])
+ .unwrap(),
+ vec![
+ ResolvedGroup::Single(0),
+ ResolvedGroup::Single(1),
+ ResolvedGroup::Single(2),
+ ResolvedGroup::Single(3),
+ ]
+ );
+}
+
+#[test]
+fn test_everything_everywhere() {
+ assert_eq!(
+ resolve_struct_groups(vec![
+ MemberSize {
+ index: 0,
+ offset: 0,
+ width: 1,
+ },
+ MemberSize {
+ index: 1,
+ offset: 0,
+ width: 1,
+ },
+ MemberSize {
+ index: 2,
+ offset: 0,
+ width: 1,
+ },
+ MemberSize {
+ index: 3,
+ offset: 0,
+ width: 1,
+ },
+ ])
+ .unwrap(),
+ vec![ResolvedGroup::Union(
+ 0,
+ vec![
+ ResolvedGroup::Single(0),
+ ResolvedGroup::Single(1),
+ ResolvedGroup::Single(2),
+ ResolvedGroup::Single(3),
+ ]
+ )]
+ );
+}
+
+#[test]
+fn test_unalignend() {
+ assert_eq!(
+ resolve_struct_groups(vec![
+ MemberSize {
+ index: 0,
+ offset: 0,
+ width: 4,
+ },
+ MemberSize {
+ index: 1,
+ offset: 4,
+ width: 8,
+ },
+ MemberSize {
+ index: 2,
+ offset: 12,
+ width: 4,
+ },
+ MemberSize {
+ index: 3,
+ offset: 0,
+ width: 8,
+ },
+ MemberSize {
+ index: 4,
+ offset: 8,
+ width: 8,
+ },
+ ])
+ .unwrap(),
+ vec![ResolvedGroup::Union(
+ 0,
+ vec![
+ ResolvedGroup::Struct(
+ 0,
+ vec![
+ ResolvedGroup::Single(0),
+ ResolvedGroup::Single(1),
+ ResolvedGroup::Single(2),
+ ]
+ ),
+ ResolvedGroup::Struct(0, vec![ResolvedGroup::Single(3), ResolvedGroup::Single(4),]),
+ ]
+ )]
+ );
+}
+
+#[test]
+fn test_heap_vs_chunk_free_header() {
+ assert_eq!(
+ resolve_struct_groups(vec![
+ MemberSize {
+ index: 0,
+ offset: 0,
+ width: 16,
+ },
+ MemberSize {
+ index: 1,
+ offset: 0,
+ width: 8,
+ },
+ MemberSize {
+ index: 2,
+ offset: 8,
+ width: 24,
+ },
+ ])
+ .unwrap(),
+ vec![ResolvedGroup::Union(
+ 0,
+ vec![
+ ResolvedGroup::Single(0),
+ ResolvedGroup::Struct(0, vec![ResolvedGroup::Single(1), ResolvedGroup::Single(2)])
+ ]
+ )]
+ );
+}
+
+#[test]
+fn test_kprcb() {
+ assert_eq!(
+ resolve_struct_groups(vec![
+ MemberSize {
+ index: 0,
+ offset: 0,
+ width: 8,
+ },
+ MemberSize {
+ index: 1,
+ offset: 8,
+ width: 1,
+ },
+ MemberSize {
+ index: 2,
+ offset: 8,
+ width: 1,
+ },
+ MemberSize {
+ index: 3,
+ offset: 9,
+ width: 1,
+ },
+ MemberSize {
+ index: 4,
+ offset: 9,
+ width: 1,
+ },
+ MemberSize {
+ index: 5,
+ offset: 10,
+ width: 1,
+ },
+ MemberSize {
+ index: 6,
+ offset: 11,
+ width: 1,
+ },
+ MemberSize {
+ index: 7,
+ offset: 12,
+ width: 1,
+ },
+ MemberSize {
+ index: 8,
+ offset: 13,
+ width: 1,
+ },
+ MemberSize {
+ index: 9,
+ offset: 14,
+ width: 2,
+ },
+ MemberSize {
+ index: 10,
+ offset: 0,
+ width: 16,
+ },
+ MemberSize {
+ index: 11,
+ offset: 16,
+ width: 1,
+ },
+ MemberSize {
+ index: 12,
+ offset: 17,
+ width: 1,
+ },
+ MemberSize {
+ index: 13,
+ offset: 18,
+ width: 1,
+ },
+ MemberSize {
+ index: 14,
+ offset: 18,
+ width: 1,
+ },
+ MemberSize {
+ index: 15,
+ offset: 19,
+ width: 1,
+ },
+ MemberSize {
+ index: 16,
+ offset: 19,
+ width: 1,
+ },
+ MemberSize {
+ index: 17,
+ offset: 20,
+ width: 4,
+ },
+ MemberSize {
+ index: 18,
+ offset: 16,
+ width: 8,
+ },
+ ])
+ .unwrap(),
+ vec![
+ ResolvedGroup::Union(
+ 0,
+ vec![
+ ResolvedGroup::Struct(
+ 0,
+ vec![
+ ResolvedGroup::Single(0),
+ ResolvedGroup::Union(
+ 8,
+ vec![ResolvedGroup::Single(1), ResolvedGroup::Single(2),]
+ ),
+ ResolvedGroup::Union(
+ 9,
+ vec![ResolvedGroup::Single(3), ResolvedGroup::Single(4),]
+ ),
+ ResolvedGroup::Single(5),
+ ResolvedGroup::Single(6),
+ ResolvedGroup::Single(7),
+ ResolvedGroup::Single(8),
+ ResolvedGroup::Single(9)
+ ]
+ ),
+ ResolvedGroup::Single(10)
+ ]
+ ),
+ ResolvedGroup::Union(
+ 16,
+ vec![
+ ResolvedGroup::Struct(
+ 16,
+ vec![
+ ResolvedGroup::Single(11),
+ ResolvedGroup::Single(12),
+ ResolvedGroup::Union(
+ 18,
+ vec![ResolvedGroup::Single(13), ResolvedGroup::Single(14),]
+ ),
+ ResolvedGroup::Union(
+ 19,
+ vec![ResolvedGroup::Single(15), ResolvedGroup::Single(16),]
+ ),
+ ResolvedGroup::Single(17)
+ ]
+ ),
+ ResolvedGroup::Single(18)
+ ]
+ )
+ ]
+ );
+}
+
+#[ignore]
+#[test]
+fn test_dispatcher_header() {
+ /*
+ XXX: This returns a different grouping which is still valid
+ Basically it turns this:
+ struct {
+ unsigned char data0;
+ union {
+ unsigned char data1;
+ struct {
+ unsigned char data1_2;
+ unsigned char data2;
+ unsigned char data3;
+ };
+ };
+ };
+
+ into this:
+
+ struct {
+ unsigned char data0;
+ union {
+ unsigned char data1;
+ unsigned char data1_2;
+ };
+ unsigned char data2;
+ unsigned char data3;
+ };
+ */
+
+ assert_eq!(
+ resolve_struct_groups(vec![
+ MemberSize {
+ index: 0x0,
+ offset: 0x0,
+ width: 0x4,
+ },
+ MemberSize {
+ index: 0x1,
+ offset: 0x0,
+ width: 0x4,
+ },
+ MemberSize {
+ index: 0x2,
+ offset: 0x0,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x3,
+ offset: 0x1,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x4,
+ offset: 0x2,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x5,
+ offset: 0x3,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x6,
+ offset: 0x0,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x7,
+ offset: 0x1,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x8,
+ offset: 0x1,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x9,
+ offset: 0x2,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0xa,
+ offset: 0x3,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0xb,
+ offset: 0x3,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0xc,
+ offset: 0x0,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0xd,
+ offset: 0x1,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0xe,
+ offset: 0x1,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0xf,
+ offset: 0x2,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x10,
+ offset: 0x3,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x11,
+ offset: 0x0,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x12,
+ offset: 0x1,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x13,
+ offset: 0x1,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x14,
+ offset: 0x2,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x15,
+ offset: 0x3,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x16,
+ offset: 0x0,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x17,
+ offset: 0x1,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x18,
+ offset: 0x2,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x19,
+ offset: 0x2,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x1a,
+ offset: 0x3,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x1b,
+ offset: 0x3,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x1c,
+ offset: 0x0,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x1d,
+ offset: 0x1,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x1e,
+ offset: 0x2,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x1f,
+ offset: 0x3,
+ width: 0x1,
+ },
+ MemberSize {
+ index: 0x20,
+ offset: 0x4,
+ width: 0x4,
+ },
+ MemberSize {
+ index: 0x21,
+ offset: 0x8,
+ width: 0x10,
+ },
+ ])
+ .unwrap(),
+ vec![
+ ResolvedGroup::Union(
+ 0,
+ vec![
+ ResolvedGroup::Single(0x0),
+ ResolvedGroup::Single(0x1),
+ ResolvedGroup::Struct(
+ 0,
+ vec![
+ ResolvedGroup::Single(0x2),
+ ResolvedGroup::Single(0x3),
+ ResolvedGroup::Single(0x4),
+ ResolvedGroup::Single(0x5),
+ ]
+ ),
+ ResolvedGroup::Struct(
+ 0,
+ vec![
+ ResolvedGroup::Single(0x6),
+ ResolvedGroup::Union(
+ 1,
+ vec![
+ ResolvedGroup::Single(0x7),
+ ResolvedGroup::Struct(
+ 1,
+ vec![
+ ResolvedGroup::Single(0x8),
+ ResolvedGroup::Single(0x9),
+ ResolvedGroup::Union(
+ 3,
+ vec![
+ ResolvedGroup::Single(0xa),
+ ResolvedGroup::Single(0xb),
+ ]
+ ),
+ ]
+ ),
+ ]
+ ),
+ ]
+ ),
+ ResolvedGroup::Struct(
+ 0,
+ vec![
+ ResolvedGroup::Single(0xc),
+ ResolvedGroup::Union(
+ 1,
+ vec![
+ ResolvedGroup::Single(0xd),
+ ResolvedGroup::Struct(
+ 1,
+ vec![
+ ResolvedGroup::Single(0xe),
+ ResolvedGroup::Single(0xf),
+ ResolvedGroup::Single(0x10),
+ ]
+ )
+ ]
+ ),
+ ]
+ ),
+ ResolvedGroup::Struct(
+ 0,
+ vec![
+ ResolvedGroup::Single(0x11),
+ ResolvedGroup::Union(
+ 1,
+ vec![
+ ResolvedGroup::Single(0x12),
+ ResolvedGroup::Struct(
+ 1,
+ vec![
+ ResolvedGroup::Single(0x13),
+ ResolvedGroup::Single(0x14),
+ ResolvedGroup::Single(0x15),
+ ]
+ )
+ ]
+ ),
+ ]
+ ),
+ ResolvedGroup::Struct(
+ 0,
+ vec![
+ ResolvedGroup::Single(0x16),
+ ResolvedGroup::Single(0x17),
+ ResolvedGroup::Union(
+ 2,
+ vec![
+ ResolvedGroup::Single(0x18),
+ ResolvedGroup::Struct(
+ 2,
+ vec![
+ ResolvedGroup::Single(0x19),
+ ResolvedGroup::Union(
+ 2,
+ vec![
+ ResolvedGroup::Single(0x1a),
+ ResolvedGroup::Single(0x1b),
+ ]
+ )
+ ]
+ )
+ ]
+ ),
+ ]
+ ),
+ ResolvedGroup::Struct(
+ 0,
+ vec![
+ ResolvedGroup::Single(0x1c),
+ ResolvedGroup::Single(0x1d),
+ ResolvedGroup::Single(0x1e),
+ ResolvedGroup::Single(0x1f),
+ ]
+ ),
+ ]
+ ),
+ ResolvedGroup::Single(0x20),
+ ResolvedGroup::Single(0x21),
+ ]
+ )
+}
+
+#[ignore]
+#[test]
+fn test_bool_modifier() {
+ assert_eq!(
+ resolve_struct_groups(vec![
+ MemberSize {
+ index: 0,
+ offset: 8,
+ width: 1,
+ },
+ MemberSize {
+ index: 1,
+ offset: 12,
+ width: 8,
+ },
+ MemberSize {
+ index: 2,
+ offset: 16,
+ width: 1,
+ },
+ ])
+ .unwrap_err()
+ .to_string(),
+ format!(
+ "Struct has overlapping member that cannot be resolved: {:#?}",
+ MemberSize {
+ index: 2,
+ offset: 16,
+ width: 1,
+ }
+ )
+ );
+}
+
+/// Whoops I'm not in PDBParserInstance
+fn log<F: FnOnce() -> D, D: Display>(msg: F) {
+ // println!("{}", msg());
+ if env::var("BN_DEBUG_PDB").is_ok() {
+ debug!("{}", msg());
+ }
+}
diff --git a/plugins/pdb-ng/src/symbol_parser.rs b/plugins/pdb-ng/src/symbol_parser.rs
new file mode 100644
index 00000000..3bcbb620
--- /dev/null
+++ b/plugins/pdb-ng/src/symbol_parser.rs
@@ -0,0 +1,2043 @@
+// Copyright 2022-2024 Vector 35 Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use binaryninja::confidence::ConfMergeable;
+use std::collections::{BTreeMap, HashMap, HashSet};
+use std::mem;
+use std::sync::OnceLock;
+
+use anyhow::{anyhow, Result};
+use itertools::Itertools;
+use pdb::register::Register::{AMD64, X86};
+use pdb::register::{AMD64Register, X86Register};
+use pdb::Error::UnimplementedSymbolKind;
+use pdb::{
+ AnnotationReferenceSymbol, BasePointerRelativeSymbol, BlockSymbol, BuildInfoSymbol,
+ CallSiteInfoSymbol, CompileFlagsSymbol, ConstantSymbol, DataReferenceSymbol, DataSymbol,
+ DefRangeFramePointerRelativeFullScopeSymbol, DefRangeFramePointerRelativeSymbol,
+ DefRangeRegisterRelativeSymbol, DefRangeRegisterSymbol, DefRangeSubFieldRegisterSymbol,
+ DefRangeSubFieldSymbol, DefRangeSymbol, ExportSymbol, FallibleIterator, FrameProcedureSymbol,
+ InlineSiteSymbol, LabelSymbol, LocalSymbol, MultiRegisterVariableSymbol, ObjNameSymbol,
+ ProcedureReferenceSymbol, ProcedureSymbol, PublicSymbol, RegisterRelativeSymbol,
+ RegisterVariableSymbol, Rva, SeparatedCodeSymbol, Source, Symbol, SymbolData, SymbolIndex,
+ SymbolIter, ThreadStorageSymbol, ThunkSymbol, TrampolineSymbol, TypeIndex,
+ UserDefinedTypeSymbol, UsingNamespaceSymbol,
+};
+
+use crate::PDBParserInstance;
+use binaryninja::architecture::{Architecture, ArchitectureExt, Register, RegisterId};
+use binaryninja::binary_view::BinaryViewBase;
+use binaryninja::confidence::{Conf, MAX_CONFIDENCE, MIN_CONFIDENCE};
+use binaryninja::demangle::demangle_ms;
+use binaryninja::rc::Ref;
+use binaryninja::types::{FunctionParameter, QualifiedName, StructureBuilder, Type, TypeClass};
+use binaryninja::variable::{Variable, VariableSourceType};
+
+const DEMANGLE_CONFIDENCE: u8 = 32;
+
+/// Parsed Data Symbol like globals, etc
+#[derive(Debug, Clone)]
+pub struct SymbolNames {
+ pub raw_name: String,
+ pub short_name: Option<String>,
+ pub full_name: Option<String>,
+}
+
+/// Parsed Data Symbol like globals, etc
+#[derive(Debug, Clone)]
+pub struct ParsedDataSymbol {
+ /// If the symbol comes from the public symbol list (lower quality)
+ pub is_public: bool,
+ /// Absolute address in bv
+ pub address: u64,
+ /// Symbol name
+ pub name: SymbolNames,
+ /// Type if known
+ pub type_: Option<Conf<Ref<Type>>>,
+}
+
+/// Parsed functions and function-y symbols
+#[derive(Debug, Clone)]
+pub struct ParsedProcedure {
+ /// If the symbol comes from the public symbol list (lower quality)
+ pub is_public: bool,
+ /// Absolute address in bv
+ pub address: u64,
+ /// Symbol name
+ pub name: SymbolNames,
+ /// Function type if known
+ pub type_: Option<Conf<Ref<Type>>>,
+ /// List of local variables (TODO: use these)
+ pub locals: Vec<ParsedVariable>,
+}
+
+/// Structure with some information about a procedure
+#[derive(Debug, Clone)]
+pub struct ParsedProcedureInfo {
+ /// Known parameters for the procedure
+ pub params: Vec<ParsedVariable>,
+ /// Known local variables for the procedure
+ pub locals: Vec<ParsedVariable>,
+}
+
+/// One parsed variable / parameter
+#[derive(Debug, Clone)]
+pub struct ParsedVariable {
+ /// Variable name
+ pub name: String,
+ /// Variable type if known
+ pub type_: Option<Conf<Ref<Type>>>,
+ /// Location(s) where the variable is stored. PDB lets you store a variable in multiple locations
+ /// despite binja not really understanding that. Length is probably never zero
+ pub storage: Vec<ParsedLocation>,
+ /// Do we think this is a parameter
+ pub is_param: bool,
+}
+
+#[derive(Debug, Copy, Clone)]
+pub struct ParsedLocation {
+ /// Location information
+ pub location: Variable,
+ /// Is the storage location relative to the base pointer? See [ParsedProcedureInfo.frame_offset]
+ pub base_relative: bool,
+ /// Is the storage location relative to the stack pointer?
+ pub stack_relative: bool,
+}
+
+/// Big enum of all the types of symbols we know how to parse
+#[derive(Debug, Clone)]
+pub enum ParsedSymbol {
+ /// Parsed Data Symbol like globals, etc
+ Data(ParsedDataSymbol),
+ /// Parsed functions and function-y symbols
+ Procedure(ParsedProcedure),
+ /// Structure with some information about a procedure
+ ProcedureInfo(ParsedProcedureInfo),
+ /// One parsed variable / parameter
+ LocalVariable(ParsedVariable),
+ /// Location of a local variable
+ Location(ParsedLocation),
+}
+
+/// This is all done in the parser instance namespace because the lifetimes are impossible to
+/// wrangle otherwise.
+impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
+ pub fn parse_symbols(
+ &mut self,
+ progress: Box<dyn Fn(usize, usize) -> Result<()> + '_>,
+ ) -> Result<(Vec<ParsedSymbol>, Vec<ParsedSymbol>)> {
+ let mut module_count = 0usize;
+ let dbg = self.pdb.debug_information()?;
+ let mut modules = dbg.modules()?;
+ while let Some(_module) = modules.next()? {
+ module_count += 1;
+ }
+
+ let global_symbols = self.pdb.global_symbols()?;
+ let symbols = global_symbols.iter();
+ let parsed = self.parse_mod_symbols(symbols)?;
+ for sym in parsed {
+ match &sym {
+ ParsedSymbol::Data(ParsedDataSymbol {
+ name: SymbolNames { raw_name, .. },
+ ..
+ })
+ | ParsedSymbol::Procedure(ParsedProcedure {
+ name: SymbolNames { raw_name, .. },
+ ..
+ }) => {
+ self.parsed_symbols_by_name
+ .insert(raw_name.clone(), self.parsed_symbols.len());
+ }
+ _ => {}
+ }
+ self.parsed_symbols.push(sym);
+ }
+
+ (progress)(1, module_count + 1)?;
+
+ let dbg = self.pdb.debug_information()?;
+ let mut modules = dbg.modules()?;
+ let mut i = 0;
+ while let Some(module) = modules.next()? {
+ i += 1;
+ (progress)(i + 1, module_count + 1)?;
+
+ self.log(|| {
+ format!(
+ "Module {} {}",
+ module.module_name(),
+ module.object_file_name()
+ )
+ });
+ if let Some(module_info) = self.pdb.module_info(&module)? {
+ let symbols = module_info.symbols()?;
+ let parsed = self.parse_mod_symbols(symbols)?;
+ for sym in parsed {
+ match &sym {
+ ParsedSymbol::Data(ParsedDataSymbol {
+ name: SymbolNames { raw_name, .. },
+ ..
+ })
+ | ParsedSymbol::Procedure(ParsedProcedure {
+ name: SymbolNames { raw_name, .. },
+ ..
+ }) => {
+ self.parsed_symbols_by_name
+ .insert(raw_name.clone(), self.parsed_symbols.len());
+ }
+ _ => {}
+ }
+ self.parsed_symbols.push(sym);
+ }
+ }
+ }
+
+ let use_public = self.settings.get_bool_with_opts(
+ "pdb.features.loadGlobalSymbols",
+ &mut self.settings_query_opts,
+ );
+
+ let mut best_symbols = HashMap::<String, &ParsedSymbol>::new();
+ for sym in &self.parsed_symbols {
+ match sym {
+ ParsedSymbol::Data(ParsedDataSymbol {
+ is_public,
+ address,
+ name:
+ SymbolNames {
+ raw_name,
+ full_name,
+ ..
+ },
+ type_,
+ ..
+ }) => {
+ if *is_public && !use_public {
+ continue;
+ }
+
+ let this_confidence = match type_ {
+ Some(Conf { confidence, .. }) => *confidence,
+ _ => MIN_CONFIDENCE,
+ };
+ let (new_better, old_exists) = match best_symbols.get(raw_name) {
+ Some(ParsedSymbol::Data(ParsedDataSymbol {
+ type_:
+ Some(Conf {
+ confidence: old_conf,
+ ..
+ }),
+ ..
+ })) => (this_confidence > *old_conf, true),
+ Some(ParsedSymbol::Data(ParsedDataSymbol { type_: None, .. })) => {
+ (true, true)
+ }
+ Some(..) => (false, true),
+ _ => (true, false),
+ };
+ if new_better {
+ self.log(|| {
+ format!(
+ "New best symbol (at 0x{:x}) for `{}` / `{}`: {:?}",
+ *address,
+ raw_name,
+ full_name.as_ref().unwrap_or(raw_name),
+ sym
+ )
+ });
+ if old_exists {
+ self.log(|| "Clobbering old definition");
+ }
+ best_symbols.insert(raw_name.clone(), sym);
+ }
+ }
+ _ => {}
+ }
+ }
+
+ let mut best_functions = HashMap::<String, &ParsedSymbol>::new();
+ for sym in &self.parsed_symbols {
+ match sym {
+ ParsedSymbol::Procedure(ParsedProcedure {
+ is_public,
+ address,
+ name:
+ SymbolNames {
+ raw_name,
+ full_name,
+ ..
+ },
+ type_,
+ ..
+ }) => {
+ if *is_public && !use_public {
+ continue;
+ }
+
+ let this_confidence = match type_ {
+ Some(Conf { confidence, .. }) => *confidence,
+ _ => MIN_CONFIDENCE,
+ };
+ let (new_better, old_exists) = match best_functions.get(raw_name) {
+ Some(ParsedSymbol::Procedure(ParsedProcedure {
+ type_:
+ Some(Conf {
+ confidence: old_conf,
+ ..
+ }),
+ ..
+ })) => (this_confidence > *old_conf, true),
+ Some(ParsedSymbol::Procedure(ParsedProcedure { type_: None, .. })) => {
+ (true, true)
+ }
+ Some(..) => (false, true),
+ _ => (true, false),
+ };
+ if new_better {
+ self.log(|| {
+ format!(
+ "New best function (at 0x{:x}) for `{}` / `{}`: {:?}",
+ *address,
+ raw_name,
+ full_name.as_ref().unwrap_or(raw_name),
+ sym
+ )
+ });
+ if old_exists {
+ self.log(|| "Clobbering old definition");
+ }
+ best_functions.insert(raw_name.clone(), sym);
+ }
+ }
+ _ => {}
+ }
+ }
+
+ Ok((
+ best_symbols
+ .into_iter()
+ .map(|(_, sym)| sym.clone())
+ .sorted_by_key(|sym| match sym {
+ ParsedSymbol::Data(ParsedDataSymbol {
+ type_, is_public, ..
+ }) => type_
+ .as_ref()
+ .map(|ty| {
+ if *is_public {
+ ty.confidence / 2
+ } else {
+ ty.confidence
+ }
+ })
+ .unwrap_or(0),
+ ParsedSymbol::Procedure(ParsedProcedure {
+ type_, is_public, ..
+ }) => type_
+ .as_ref()
+ .map(|ty| {
+ if *is_public {
+ ty.confidence / 2
+ } else {
+ ty.confidence
+ }
+ })
+ .unwrap_or(0),
+ _ => 0,
+ })
+ .collect::<Vec<_>>(),
+ best_functions
+ .into_iter()
+ .map(|(_, func)| func.clone())
+ .sorted_by_key(|sym| match sym {
+ ParsedSymbol::Data(ParsedDataSymbol {
+ type_, is_public, ..
+ }) => type_
+ .as_ref()
+ .map(|ty| {
+ if *is_public {
+ ty.confidence / 2
+ } else {
+ ty.confidence
+ }
+ })
+ .unwrap_or(0),
+ ParsedSymbol::Procedure(ParsedProcedure {
+ type_, is_public, ..
+ }) => type_
+ .as_ref()
+ .map(|ty| {
+ if *is_public {
+ ty.confidence / 2
+ } else {
+ ty.confidence
+ }
+ })
+ .unwrap_or(0),
+ _ => 0,
+ })
+ .collect::<Vec<_>>(),
+ ))
+ }
+
+ /// Parse all the symbols in a module, via the given SymbolIter
+ pub fn parse_mod_symbols(&mut self, mut symbols: SymbolIter) -> Result<Vec<ParsedSymbol>> {
+ // Collect tree structure first
+ let mut first = None;
+ let mut last_local = None;
+ let mut top_level_syms = vec![];
+ let mut thunk_syms = vec![];
+ let mut unparsed_syms = BTreeMap::new();
+ while let Some(sym) = symbols.next()? {
+ if first.is_none() {
+ first = Some(sym.index());
+ }
+ unparsed_syms.insert(sym.index(), sym);
+
+ let p = sym.parse();
+ self.log(|| format!("Parsed: {:x?}", p));
+
+ // It's some sort of weird tree structure where SOME symbols have "end" indices
+ // and anything between them and that index is a child symbol
+ // Sometimes there are "end scope" symbols at those end indices but like, sometimes
+ // there aren't? Which makes that entire system seem pointless (or I'm just missing
+ // something and it makes sense to _someone_)
+ if let Some(&(start, _end)) = self.symbol_stack.last() {
+ self.add_symbol_child(start, sym.index());
+ } else {
+ // Place thunk symbols in their own list at the end, so they can reference
+ // other symbols parsed in the module
+ match &p {
+ Ok(SymbolData::Thunk(_)) => {
+ thunk_syms.push(sym.index());
+ }
+ _ => {
+ top_level_syms.push(sym.index());
+ }
+ }
+ }
+ let mut popped = false;
+ while let Some(&(_start, end)) = self.symbol_stack.last() {
+ if sym.index().0 >= end.0 {
+ let _ = self.symbol_stack.pop();
+ popped = true;
+ } else {
+ break;
+ }
+ }
+
+ // These aren't actually used for parsing (I don't trust them) but we can include a little
+ // debug error check here and see if it's ever actually wrong
+ match p {
+ Ok(SymbolData::ScopeEnd) | Ok(SymbolData::InlineSiteEnd) if popped => {}
+ Ok(SymbolData::ScopeEnd) | Ok(SymbolData::InlineSiteEnd) if !popped => {
+ self.log(|| "Did not pop at a scope end??? WTF??");
+ }
+ _ if popped => {
+ self.log(|| "Popped but not at a scope end??? WTF??");
+ }
+ _ => {}
+ }
+
+ // Push new scopes on the stack to build the tree
+ match p {
+ Ok(SymbolData::Procedure(data)) => {
+ self.symbol_stack.push((sym.index(), data.end));
+ }
+ Ok(SymbolData::InlineSite(data)) => {
+ self.symbol_stack.push((sym.index(), data.end));
+ }
+ Ok(SymbolData::Block(data)) => {
+ self.symbol_stack.push((sym.index(), data.end));
+ }
+ Ok(SymbolData::Thunk(data)) => {
+ self.symbol_stack.push((sym.index(), data.end));
+ }
+ Ok(SymbolData::SeparatedCode(data)) => {
+ self.symbol_stack.push((sym.index(), data.end));
+ }
+ Ok(SymbolData::FrameProcedure(..)) => {
+ if let Some(&(_, proc_end)) = self.symbol_stack.last() {
+ self.symbol_stack.push((sym.index(), proc_end));
+ }
+ }
+ Ok(SymbolData::Local(..)) => {
+ last_local = Some(sym.index());
+ }
+ Ok(SymbolData::DefRange(..))
+ | Ok(SymbolData::DefRangeSubField(..))
+ | Ok(SymbolData::DefRangeRegister(..))
+ | Ok(SymbolData::DefRangeFramePointerRelative(..))
+ | Ok(SymbolData::DefRangeFramePointerRelativeFullScope(..))
+ | Ok(SymbolData::DefRangeSubFieldRegister(..))
+ | Ok(SymbolData::DefRangeRegisterRelative(..)) => {
+ // I'd like to retract my previous statement that someone could possibly
+ // understand this:
+ // These symbol types impact the previous symbol, if it was a local
+ // BUT ALSO!! PART III REVENGE OF THE SYM-TH: You can have more than one of
+ // these and they all (?? it's undocumented) apply to the last local, PROBABLY
+ if let Some(last) = last_local {
+ self.add_symbol_child(last, sym.index());
+ } else {
+ self.log(|| format!("Found def range with no last local: {:?}", p));
+ }
+ }
+ _ => {}
+ }
+ }
+ assert!(self.symbol_stack.is_empty());
+ // Add thunks at the end as per above
+ top_level_syms.extend(thunk_syms);
+
+ // Restart and do the processing for real this time
+ if let Some(first) = first {
+ symbols.seek(first);
+ }
+
+ let mut final_symbols = HashSet::new();
+
+ for root_idx in top_level_syms {
+ for child_idx in self.walk_children(root_idx).into_iter() {
+ let &sym = unparsed_syms
+ .get(&child_idx)
+ .expect("should have parsed this");
+
+ self.log(|| format!("Symbol {:?} ", sym.index()));
+ let (name, address) =
+ if let Some(parsed) = self.handle_symbol_index(sym.index(), &sym)? {
+ final_symbols.insert(sym.index());
+ match parsed {
+ ParsedSymbol::Data(ParsedDataSymbol { name, address, .. }) => {
+ (Some(name.clone()), Some(*address))
+ }
+ ParsedSymbol::Procedure(ParsedProcedure { name, address, .. }) => {
+ (Some(name.clone()), Some(*address))
+ }
+ _ => (None, None),
+ }
+ } else {
+ (None, None)
+ };
+
+ if let Some(name) = name {
+ self.named_symbols.insert(name.raw_name, sym.index());
+ }
+ if let Some(address) = address {
+ if !self.addressed_symbols.contains_key(&address) {
+ self.addressed_symbols.insert(address, vec![]);
+ }
+ self.addressed_symbols
+ .get_mut(&address)
+ .expect("just created this")
+ .push(
+ self.indexed_symbols
+ .get(&sym.index())
+ .ok_or_else(|| anyhow!("Can't find sym {} ?", sym.index()))?
+ .clone(),
+ );
+ }
+ }
+ }
+
+ let filtered_symbols = mem::replace(&mut self.indexed_symbols, BTreeMap::new())
+ .into_iter()
+ .filter_map(|(idx, sym)| {
+ if final_symbols.contains(&idx) {
+ Some(sym)
+ } else {
+ None
+ }
+ })
+ .collect::<Vec<_>>();
+
+ // The symbols overlap between modules or something, so we can't keep this info around
+ self.symbol_tree.clear();
+ self.module_cpu_type = None;
+
+ Ok(filtered_symbols)
+ }
+
+ /// Set a symbol to be the parent of another, building the symbol tree
+ fn add_symbol_child(&mut self, parent: SymbolIndex, child: SymbolIndex) {
+ if let Some(tree) = self.symbol_tree.get_mut(&parent) {
+ tree.push(child);
+ } else {
+ self.symbol_tree.insert(parent, Vec::from([child]));
+ }
+
+ self.symbol_parents.insert(child, parent);
+ }
+
+ /// Postorder traversal of children of symbol index (only during this module parse)
+ fn walk_children(&self, sym: SymbolIndex) -> Vec<SymbolIndex> {
+ let mut children = vec![];
+
+ if let Some(tree) = self.symbol_tree.get(&sym) {
+ for &child in tree {
+ children.extend(self.walk_children(child).into_iter());
+ }
+ }
+
+ children.push(sym);
+ children
+ }
+
+ /// Direct children of symbol index (only during this module parse)
+ fn symbol_children(&self, sym: SymbolIndex) -> Vec<SymbolIndex> {
+ if let Some(tree) = self.symbol_tree.get(&sym) {
+ tree.clone()
+ } else {
+ vec![]
+ }
+ }
+
+ /// Direct parent of symbol index (only during this module parse)
+ #[allow(dead_code)]
+ fn symbol_parent(&self, sym: SymbolIndex) -> Option<SymbolIndex> {
+ self.symbol_parents.get(&sym).copied()
+ }
+
+ /// Find symbol by index (only during this module parse)
+ fn lookup_symbol(&self, sym: &SymbolIndex) -> Option<&ParsedSymbol> {
+ self.indexed_symbols.get(sym)
+ }
+
+ /// Parse a new symbol by its index
+ fn handle_symbol_index(
+ &mut self,
+ idx: SymbolIndex,
+ sym: &Symbol,
+ ) -> Result<Option<&ParsedSymbol>> {
+ if let None = self.indexed_symbols.get(&idx) {
+ match sym.parse() {
+ Ok(data) => match self.handle_symbol(idx, &data) {
+ Ok(Some(parsed)) => {
+ self.log(|| format!("Symbol {} parsed into: {:?}", idx, parsed));
+ self.indexed_symbols.insert(idx, parsed);
+ }
+ Ok(None) => {}
+ e => {
+ self.log(|| format!("Error parsing symbol {}: {:?}", idx, e));
+ }
+ },
+ Err(UnimplementedSymbolKind(k)) => {
+ self.log(|| format!("Not parsing unimplemented symbol {}: kind {:x?}", idx, k));
+ }
+ Err(e) => {
+ self.log(|| format!("Could not parse symbol: {}: {}", idx, e));
+ }
+ };
+ }
+
+ Ok(self.indexed_symbols.get(&idx))
+ }
+
+ /// Parse a new symbol's data
+ fn handle_symbol(
+ &mut self,
+ index: SymbolIndex,
+ data: &SymbolData,
+ ) -> Result<Option<ParsedSymbol>> {
+ match data {
+ SymbolData::ScopeEnd => self.handle_scope_end_symbol(index),
+ SymbolData::ObjName(data) => self.handle_obj_name_symbol(index, data),
+ SymbolData::RegisterVariable(data) => self.handle_register_variable_symbol(index, data),
+ SymbolData::Constant(data) => self.handle_constant_symbol(index, data),
+ SymbolData::UserDefinedType(data) => self.handle_user_defined_type_symbol(index, data),
+ SymbolData::MultiRegisterVariable(data) => {
+ self.handle_multi_register_variable_symbol(index, data)
+ }
+ SymbolData::Data(data) => self.handle_data_symbol(index, data),
+ SymbolData::Public(data) => self.handle_public_symbol(index, data),
+ SymbolData::Procedure(data) => self.handle_procedure_symbol(index, data),
+ SymbolData::ThreadStorage(data) => self.handle_thread_storage_symbol(index, data),
+ SymbolData::CompileFlags(data) => self.handle_compile_flags_symbol(index, data),
+ SymbolData::UsingNamespace(data) => self.handle_using_namespace_symbol(index, data),
+ SymbolData::ProcedureReference(data) => {
+ self.handle_procedure_reference_symbol(index, &data)
+ }
+ SymbolData::DataReference(data) => self.handle_data_reference_symbol(index, data),
+ SymbolData::AnnotationReference(data) => {
+ self.handle_annotation_reference_symbol(index, data)
+ }
+ SymbolData::Trampoline(data) => self.handle_trampoline_symbol(index, data),
+ SymbolData::Export(data) => self.handle_export_symbol(index, data),
+ SymbolData::Local(data) => self.handle_local_symbol(index, data),
+ SymbolData::BuildInfo(data) => self.handle_build_info_symbol(index, data),
+ SymbolData::InlineSite(data) => self.handle_inline_site_symbol(index, data),
+ SymbolData::InlineSiteEnd => self.handle_inline_site_end_symbol(index),
+ SymbolData::ProcedureEnd => self.handle_procedure_end_symbol(index),
+ SymbolData::Label(data) => self.handle_label_symbol(index, data),
+ SymbolData::Block(data) => self.handle_block_symbol(index, data),
+ SymbolData::RegisterRelative(data) => self.handle_register_relative_symbol(index, data),
+ SymbolData::Thunk(data) => self.handle_thunk_symbol(index, data),
+ SymbolData::SeparatedCode(data) => self.handle_separated_code_symbol(index, data),
+ SymbolData::DefRange(data) => self.handle_def_range(index, &data),
+ SymbolData::DefRangeSubField(data) => self.handle_def_range_sub_field(index, data),
+ SymbolData::DefRangeRegister(data) => self.handle_def_range_register(index, data),
+ SymbolData::DefRangeFramePointerRelative(data) => {
+ self.handle_def_range_frame_pointer_relative_symbol(index, data)
+ }
+ SymbolData::DefRangeFramePointerRelativeFullScope(data) => {
+ self.handle_def_range_frame_pointer_relative_full_scope_symbol(index, data)
+ }
+ SymbolData::DefRangeSubFieldRegister(data) => {
+ self.handle_def_range_sub_field_register_symbol(index, data)
+ }
+ SymbolData::DefRangeRegisterRelative(data) => {
+ self.handle_def_range_register_relative_symbol(index, data)
+ }
+ SymbolData::BasePointerRelative(data) => {
+ self.handle_base_pointer_relative_symbol(index, data)
+ }
+ SymbolData::FrameProcedure(data) => self.handle_frame_procedure_symbol(index, data),
+ SymbolData::CallSiteInfo(data) => self.handle_call_site_info(index, data),
+ e => Err(anyhow!("Unhandled symbol type {:?}", e)),
+ }
+ }
+
+ fn handle_scope_end_symbol(&mut self, _index: SymbolIndex) -> Result<Option<ParsedSymbol>> {
+ self.log(|| "Got ScopeEnd symbol");
+ Ok(None)
+ }
+
+ fn handle_obj_name_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &ObjNameSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got ObjName symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_register_variable_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &RegisterVariableSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got RegisterVariable symbol: {:?}", data));
+
+ let storage = if let Some(reg) = self.convert_register(data.register) {
+ vec![ParsedLocation {
+ location: Variable {
+ ty: VariableSourceType::RegisterVariableSourceType,
+ index: 0,
+ storage: reg.0 as i64,
+ },
+ base_relative: false,
+ stack_relative: false,
+ }]
+ } else {
+ // TODO: What do we do here?
+ vec![]
+ };
+
+ Ok(Some(ParsedSymbol::LocalVariable(ParsedVariable {
+ name: data.name.to_string().to_string(),
+ type_: self.lookup_type_conf(&data.type_index, false)?,
+ storage,
+ is_param: data.slot.map_or(true, |slot| slot > 0),
+ })))
+ }
+
+ fn handle_constant_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &ConstantSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got Constant symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_user_defined_type_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &UserDefinedTypeSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got UserDefinedType symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_multi_register_variable_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &MultiRegisterVariableSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got MultiRegisterVariable symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_data_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &DataSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got Data symbol: {:?}", data));
+
+ let rva = data.offset.to_rva(&self.address_map).unwrap_or_default();
+ let raw_name = data.name.to_string().to_string();
+ let (t, name) = self.demangle_to_type(&raw_name, rva)?;
+ let name = name.map(|n| n.to_string());
+
+ // Sometimes the demangler REALLY knows what type this is supposed to be, and the
+ // data symbol is actually wrong. So in those cases, let the demangler take precedence
+ // Otherwise-- the demangler is usually wrong and clueless
+ let data_type = t.merge(self.lookup_type_conf(&data.type_index, false)?);
+
+ // Ignore symbols with no name and no type
+ if !self.settings.get_bool_with_opts(
+ "pdb.features.allowUnnamedVoidSymbols",
+ &mut self.settings_query_opts,
+ ) && name.is_none()
+ {
+ if let Some(ty) = &data_type {
+ if ty.contents.type_class() == TypeClass::VoidTypeClass {
+ return Ok(None);
+ }
+ } else {
+ return Ok(None);
+ }
+ }
+
+ let name = SymbolNames {
+ raw_name,
+ short_name: name.clone(),
+ full_name: name,
+ };
+
+ self.log(|| {
+ format!(
+ "DATA: 0x{:x}: {:?} {:?}",
+ self.bv.start() + rva.0 as u64,
+ &name,
+ &data_type
+ )
+ });
+
+ Ok(Some(ParsedSymbol::Data(ParsedDataSymbol {
+ is_public: false,
+ address: self.bv.start() + rva.0 as u64,
+ name,
+ type_: data_type,
+ })))
+ }
+
+ fn handle_public_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &PublicSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got Public symbol: {:?}", data));
+ let rva = data.offset.to_rva(&self.address_map).unwrap_or_default();
+ let raw_name = data.name.to_string().to_string();
+ let (t, name) = self.demangle_to_type(&raw_name, rva)?;
+ let name = name.map(|n| n.to_string());
+
+ let name = SymbolNames {
+ raw_name,
+ short_name: name.clone(),
+ full_name: name,
+ };
+
+ // These are generally low confidence because we only have the demangler to inform us of type
+
+ if data.function {
+ self.log(|| {
+ format!(
+ "PUBLIC FUNCTION: 0x{:x}: {:?} {:?}",
+ self.bv.start() + rva.0 as u64,
+ &name,
+ t
+ )
+ });
+
+ Ok(Some(ParsedSymbol::Procedure(ParsedProcedure {
+ is_public: true,
+ address: self.bv.start() + rva.0 as u64,
+ name,
+ type_: t,
+ locals: vec![],
+ })))
+ } else {
+ self.log(|| {
+ format!(
+ "PUBLIC DATA: 0x{:x}: {:?} {:?}",
+ self.bv.start() + rva.0 as u64,
+ &name,
+ t
+ )
+ });
+
+ Ok(Some(ParsedSymbol::Data(ParsedDataSymbol {
+ is_public: true,
+ address: self.bv.start() + rva.0 as u64,
+ name,
+ type_: t,
+ })))
+ }
+ }
+
+ /// Given a proc symbol index and guessed type (from demangler or tpi), find all the local variables
+ /// and parameters related to that symbol.
+ /// Returns Ok(Some((resolved params, locals))))
+ fn lookup_locals(
+ &self,
+ index: SymbolIndex,
+ type_index: TypeIndex,
+ demangled_type: Option<Conf<Ref<Type>>>,
+ ) -> Result<(Option<Conf<Ref<Type>>>, Vec<ParsedVariable>)> {
+ // So generally speaking, here's the information we have:
+ // - The function type is usually accurate wrt the parameter locations
+ // - The parameter symbols have the names we want for the params
+ // - The parameter symbols are a big ugly mess
+ // We basically want to take the function type from the type, and just fill in the
+ // names of all the parameters. Non-param locals don't really matter since binja
+ // can't handle them anyway.
+
+ // Type parameters order needs to be like this:
+ // 1. `this` pointer (if exists)
+ // 2. Various stack params
+ // 3. Various register params
+ // We assume that if a parameter is found in a register, that is where it is passed.
+ // Otherwise they are in the default order as per the CC
+
+ // Get child objects and search for local variable names
+ let mut locals = vec![];
+ let mut params = vec![];
+ let mut known_frame = false;
+ for child in self.symbol_children(index) {
+ match self.lookup_symbol(&child) {
+ Some(ParsedSymbol::ProcedureInfo(info)) => {
+ params = info.params.clone();
+ locals = info.locals.clone();
+ known_frame = true;
+ }
+ _ => {}
+ }
+ }
+
+ let raw_type = self.lookup_type_conf(&type_index, false)?;
+ let fancy_type = self.lookup_type_conf(&type_index, true)?;
+
+ // Best guess so far in case of error handling
+ let fancier_type = fancy_type
+ .clone()
+ .merge(raw_type.clone())
+ .merge(demangled_type.clone());
+
+ if !known_frame {
+ return Ok((fancier_type, vec![]));
+ }
+
+ // We need both of these to exist (not sure why they wouldn't)
+ let (raw_type, fancy_type) = match (raw_type, fancy_type) {
+ (Some(raw), Some(fancy)) => (raw, fancy),
+ _ => return Ok((fancier_type, vec![])),
+ };
+
+ let raw_params = raw_type.contents.parameters().ok_or(anyhow!("no params"))?;
+ let mut fancy_params = fancy_type
+ .contents
+ .parameters()
+ .ok_or(anyhow!("no params"))?;
+
+ // Collect all the parameters we are expecting from the symbols
+ let mut parsed_params = vec![];
+ for p in &params {
+ let param = FunctionParameter::new(
+ p.type_.clone().merge(Conf::new(
+ Type::int(self.arch.address_size(), false),
+ MIN_CONFIDENCE,
+ )),
+ p.name.clone(),
+ p.storage.first().map(|loc| loc.location),
+ );
+ // Ignore thisptr because it's not technically part of the raw type signature
+ if p.name != "this" {
+ parsed_params.push(param);
+ }
+ }
+ let mut parsed_locals = vec![];
+ for p in &locals {
+ let param = FunctionParameter::new(
+ p.type_.clone().merge(Conf::new(
+ Type::int(self.arch.address_size(), false),
+ MIN_CONFIDENCE,
+ )),
+ p.name.clone(),
+ p.storage.first().map(|loc| loc.location),
+ );
+ // Ignore thisptr because it's not technically part of the raw type signature
+ if p.name != "this" {
+ parsed_locals.push(param);
+ }
+ }
+
+ self.log(|| format!("Raw params: {:#x?}", raw_params));
+ self.log(|| format!("Fancy params: {:#x?}", fancy_params));
+ self.log(|| format!("Parsed params: {:#x?}", parsed_params));
+
+ // We expect one parameter for each unnamed parameter in the marked up type
+ let expected_param_count = fancy_params.iter().filter(|p| p.name.is_empty()).count();
+ // Sanity
+ if expected_param_count != raw_params.len() {
+ return Err(anyhow!(
+ "Mismatched number of formal parameters and interpreted parameters"
+ ));
+ }
+
+ // If we don't have enough parameters to fill the slots, there's a problem here
+ // So just fallback to the unnamed params
+ if expected_param_count > parsed_params.len() {
+ // As per reversing of msdia140.dll (and nowhere else): if a function doesn't have
+ // enough parameter variables declared as parameters, the remaining parameters are
+ // the first however many locals. If you don't have enough of those, idk??
+ if expected_param_count > (parsed_params.len() + parsed_locals.len()) {
+ return Ok((fancier_type, vec![]));
+ }
+ parsed_params.extend(parsed_locals);
+ }
+ let expected_parsed_params = parsed_params
+ .drain(0..expected_param_count)
+ .collect::<Vec<_>>();
+
+ // For all formal parameters, apply names to them in fancy_params
+ // These should be all types in fancy_params that are unnamed (named ones we inserted)
+
+ let mut i = 0;
+ for p in fancy_params.iter_mut() {
+ if p.name.is_empty() {
+ if p.ty.contents != expected_parsed_params[i].ty.contents {
+ self.log(|| {
+ format!(
+ "Suspicious parameter {}: {:?} vs {:?}",
+ i, p, expected_parsed_params[i]
+ )
+ });
+ }
+ if expected_parsed_params[i].name.as_str() == "__formal" {
+ p.name = format!("__formal{}", i);
+ } else {
+ p.name = expected_parsed_params[i].name.clone();
+ }
+ i += 1;
+ }
+ }
+
+ // Now apply the default location for the params from the cc
+ let cc = fancy_type
+ .contents
+ .calling_convention()
+ .map_or_else(|| Conf::new(self.default_cc.clone(), 0), |cc| cc);
+
+ self.log(|| {
+ format!(
+ "Type calling convention: {:?}",
+ fancy_type.contents.calling_convention()
+ )
+ });
+ self.log(|| format!("Default calling convention: {:?}", self.default_cc));
+ self.log(|| format!("Result calling convention: {:?}", cc));
+
+ let locations = cc.contents.variables_for_parameters(&fancy_params, None);
+ for (p, new_location) in fancy_params.iter_mut().zip(locations.into_iter()) {
+ p.location = Some(new_location);
+ }
+
+ self.log(|| format!("Final params: {:#x?}", fancy_params));
+
+ // Use the new locals we've parsed to make the Real Definitely True function type
+ let fancy_type = Conf::new(
+ Type::function_with_opts(
+ &fancy_type
+ .contents
+ .return_value()
+ .ok_or(anyhow!("no ret"))?,
+ fancy_params.as_slice(),
+ fancy_type.contents.has_variable_arguments().contents,
+ cc,
+ fancy_type.contents.stack_adjustment(),
+ ),
+ MAX_CONFIDENCE,
+ );
+
+ let fancier_type = fancy_type
+ .clone()
+ .merge(raw_type.clone())
+ .merge(demangled_type.clone());
+
+ self.log(|| format!("Raw type: {:#x?}", raw_type));
+ self.log(|| format!("Demangled type: {:#x?}", demangled_type));
+ self.log(|| format!("Fancy type: {:#x?}", fancy_type));
+ self.log(|| format!("Result type: {:#x?}", fancier_type));
+
+ Ok((Some(fancier_type), vec![]))
+ }
+
+ fn handle_procedure_symbol(
+ &mut self,
+ index: SymbolIndex,
+ data: &ProcedureSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got Procedure symbol: {:?}", data));
+
+ let rva = data.offset.to_rva(&self.address_map).unwrap_or_default();
+ let address = self.bv.start() + rva.0 as u64;
+
+ let mut raw_name = data.name.to_string().to_string();
+
+ // Generally proc symbols have real types, but use the demangler just in case the microsoft
+ // public pdbs have the function type as `void`
+ let (t, name) = self.demangle_to_type(&raw_name, rva)?;
+ let mut name = name.map(|n| n.to_string());
+
+ // Some proc symbols don't have a mangled name, so try and look up their name
+ if name.is_none() || name.as_ref().expect("just failed none") == &raw_name {
+ // Lookup public symbol with the same name
+ if let Some(others) = self.addressed_symbols.get(&address) {
+ for o in others {
+ match o {
+ ParsedSymbol::Procedure(ParsedProcedure {
+ name: proc_name, ..
+ }) => {
+ if proc_name.full_name.as_ref().unwrap_or(&proc_name.raw_name)
+ == &raw_name
+ {
+ name = Some(raw_name);
+ raw_name = proc_name.raw_name.clone();
+ break;
+ }
+ }
+ _ => {}
+ }
+ }
+ }
+ }
+
+ let (fn_type, locals) = self.lookup_locals(index, data.type_index, t)?;
+
+ let name = SymbolNames {
+ raw_name,
+ short_name: name.clone(),
+ full_name: name,
+ };
+
+ self.log(|| format!("PROC: 0x{:x}: {:?} {:?}", address, &name, &fn_type));
+
+ Ok(Some(ParsedSymbol::Procedure(ParsedProcedure {
+ is_public: false,
+ address,
+ name,
+ type_: fn_type,
+ locals,
+ })))
+ }
+
+ fn handle_thread_storage_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &ThreadStorageSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got ThreadStorage symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_compile_flags_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &CompileFlagsSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got CompileFlags symbol: {:?}", data));
+ self.module_cpu_type = Some(data.cpu_type);
+ Ok(None)
+ }
+
+ fn handle_using_namespace_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &UsingNamespaceSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got UsingNamespace symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_procedure_reference_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &ProcedureReferenceSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got ProcedureReference symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_data_reference_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &DataReferenceSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got DataReference symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_annotation_reference_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &AnnotationReferenceSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got AnnotationReference symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_trampoline_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &TrampolineSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got Trampoline symbol: {:?}", data));
+ let rva = data.thunk.to_rva(&self.address_map).unwrap_or_default();
+ let target_rva = data.target.to_rva(&self.address_map).unwrap_or_default();
+
+ let address = self.bv.start() + rva.0 as u64;
+ let target_address = self.bv.start() + target_rva.0 as u64;
+
+ let mut target_name = None;
+ let mut thunk_name = None;
+
+ let mut fn_type: Option<Conf<Ref<Type>>> = None;
+
+ // These have the same name as their target, so look that up
+ if let Some(syms) = self.addressed_symbols.get(&target_address) {
+ // Take name from the public symbol
+ for sym in syms {
+ match sym {
+ ParsedSymbol::Procedure(proc) if proc.is_public => {
+ fn_type = proc.type_.clone().merge(fn_type);
+ target_name = Some(proc.name.clone());
+ }
+ _ => {}
+ }
+ }
+ // Take type from the non-public symbol if we have one
+ for sym in syms {
+ match sym {
+ ParsedSymbol::Procedure(proc) if !proc.is_public => {
+ fn_type = proc.type_.clone().merge(fn_type);
+ if target_name.is_none() {
+ target_name = Some(proc.name.clone());
+ }
+ }
+ _ => {}
+ }
+ }
+ }
+
+ // And handle the fact that pdb public symbols for trampolines have the name of their target
+ // ugh
+ if let Some(syms) = self.addressed_symbols.get_mut(&address) {
+ if let [ParsedSymbol::Procedure(proc)] = syms.as_mut_slice() {
+ if let Some(tn) = &target_name {
+ if proc.name.raw_name == tn.raw_name
+ || proc.name.full_name.as_ref().unwrap_or(&proc.name.raw_name)
+ == tn.full_name.as_ref().unwrap_or(&tn.raw_name)
+ {
+ // Yeah it's one of these symbols
+ let old_name = proc.name.clone();
+ let new_name = SymbolNames {
+ raw_name: "j_".to_string() + &old_name.raw_name,
+ short_name: old_name.short_name.as_ref().map(|n| "j_".to_string() + n),
+ full_name: old_name.full_name.as_ref().map(|n| "j_".to_string() + n),
+ };
+
+ // I'm so sorry about this
+ // XXX: Update the parsed public symbol's name to use j_ syntax
+ if let Some(idx) = self.named_symbols.remove(&old_name.raw_name) {
+ self.named_symbols.insert(new_name.raw_name.clone(), idx);
+ }
+ if let Some(idx) = self.parsed_symbols_by_name.remove(&old_name.raw_name) {
+ self.parsed_symbols_by_name
+ .insert(new_name.raw_name.clone(), idx);
+ match &mut self.parsed_symbols[idx] {
+ ParsedSymbol::Data(ParsedDataSymbol {
+ name: parsed_name, ..
+ })
+ | ParsedSymbol::Procedure(ParsedProcedure {
+ name: parsed_name,
+ ..
+ }) => {
+ parsed_name.raw_name = new_name.raw_name.clone();
+ parsed_name.short_name = new_name.short_name.clone();
+ parsed_name.full_name = new_name.full_name.clone();
+ }
+ _ => {}
+ }
+ }
+ proc.name = new_name.clone();
+ thunk_name = Some(new_name);
+ }
+ }
+ }
+ }
+
+ if thunk_name.is_none() {
+ if let Some(tn) = target_name {
+ thunk_name = Some(SymbolNames {
+ raw_name: "j_".to_string() + &tn.raw_name,
+ short_name: tn.short_name.as_ref().map(|n| "j_".to_string() + n),
+ full_name: tn.full_name.as_ref().map(|n| "j_".to_string() + n),
+ });
+ }
+ }
+
+ let name = thunk_name.unwrap_or(SymbolNames {
+ raw_name: format!("j_sub_{:x}", target_address),
+ short_name: None,
+ full_name: None,
+ });
+
+ self.log(|| format!("TRAMPOLINE: 0x{:x}: {:?} {:?}", address, &name, &fn_type));
+
+ Ok(Some(ParsedSymbol::Procedure(ParsedProcedure {
+ is_public: false,
+ address,
+ name,
+ type_: fn_type,
+ locals: vec![],
+ })))
+ }
+
+ fn handle_export_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &ExportSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got Export symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_local_symbol(
+ &mut self,
+ index: SymbolIndex,
+ data: &LocalSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got Local symbol: {:?}", data));
+ // Look for definition ranges for this symbol
+ let mut locations: Vec<ParsedLocation> = vec![];
+ for child in self.symbol_children(index) {
+ match self.lookup_symbol(&child) {
+ Some(ParsedSymbol::Location(loc)) => {
+ locations.push(*loc);
+ }
+ _ => {}
+ }
+ }
+
+ Ok(Some(ParsedSymbol::LocalVariable(ParsedVariable {
+ name: data.name.to_string().to_string(),
+ type_: self.lookup_type_conf(&data.type_index, false)?,
+ storage: locations,
+ is_param: data.flags.isparam,
+ })))
+ }
+
+ fn handle_build_info_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &BuildInfoSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got BuildInfo symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_inline_site_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &InlineSiteSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got InlineSite symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_inline_site_end_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| "Got InlineSiteEnd symbol");
+ Ok(None)
+ }
+
+ fn handle_procedure_end_symbol(&mut self, _index: SymbolIndex) -> Result<Option<ParsedSymbol>> {
+ self.log(|| "Got ProcedureEnd symbol");
+ Ok(None)
+ }
+
+ fn handle_label_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &LabelSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got Label symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_block_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &BlockSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got Block symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_register_relative_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &RegisterRelativeSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got RegisterRelative symbol: {:?}", data));
+ match self.lookup_register(data.register) {
+ Some(X86(X86Register::EBP)) | Some(AMD64(AMD64Register::RBP)) => {
+ // Local is relative to base pointer
+ // This is just another way of writing BasePointerRelativeSymbol
+ Ok(Some(ParsedSymbol::LocalVariable(ParsedVariable {
+ name: data.name.to_string().to_string(),
+ type_: self.lookup_type_conf(&data.type_index, false)?,
+ storage: vec![ParsedLocation {
+ location: Variable {
+ ty: VariableSourceType::StackVariableSourceType,
+ index: 0,
+ storage: data.offset as i64,
+ },
+ base_relative: true, // !!
+ stack_relative: false, // !!
+ }],
+ is_param: data.slot.map_or(false, |slot| slot > 0),
+ })))
+ }
+ Some(X86(X86Register::ESP)) | Some(AMD64(AMD64Register::RSP)) => {
+ // Local is relative to stack pointer
+ // This is the same as base pointer case except not base relative (ofc)
+ Ok(Some(ParsedSymbol::LocalVariable(ParsedVariable {
+ name: data.name.to_string().to_string(),
+ type_: self.lookup_type_conf(&data.type_index, false)?,
+ storage: vec![ParsedLocation {
+ location: Variable {
+ ty: VariableSourceType::StackVariableSourceType,
+ index: 0,
+ storage: data.offset as i64,
+ },
+ base_relative: false, // !!
+ stack_relative: true, // !!
+ }],
+ is_param: data.slot.map_or(false, |slot| slot > 0),
+ })))
+ }
+ _ => {
+ // Local is relative to some non-bp register.
+ // This is, of course, totally possible and normal
+ // Binja just can't handle it in the slightest.
+ // Soooooooo ????
+ // TODO
+ Ok(None)
+ }
+ }
+ }
+
+ fn handle_thunk_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &ThunkSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got Thunk symbol: {:?}", data));
+ let rva = data.offset.to_rva(&self.address_map).unwrap_or_default();
+ let raw_name = data.name.to_string().to_string();
+ let address = self.bv.start() + rva.0 as u64;
+
+ let (t, name) = self.demangle_to_type(&raw_name, rva)?;
+ let name = name.map(|n| n.to_string());
+ let mut fn_type = t;
+
+ // These have the same name as their target, so look that up
+ if let Some(&idx) = self.named_symbols.get(&raw_name) {
+ if let Some(ParsedSymbol::Procedure(proc)) = self.indexed_symbols.get(&idx) {
+ fn_type = proc.type_.clone().merge(fn_type);
+ }
+ }
+
+ let mut thunk_name = None;
+
+ // And handle the fact that pdb public symbols for thunks have the name of their target
+ // ugh
+ if let Some(syms) = self.addressed_symbols.get_mut(&address) {
+ if let [ParsedSymbol::Procedure(proc)] = syms.as_mut_slice() {
+ // Yeah it's one of these symbols
+ // Make sure we don't do this twice (does that even happen?)
+ if !proc.name.raw_name.starts_with("j_") {
+ let old_name = proc.name.clone();
+ let new_name = SymbolNames {
+ raw_name: "j_".to_string() + &old_name.raw_name,
+ short_name: Some(
+ "j_".to_string() + old_name.short_name.as_ref().unwrap_or(&raw_name),
+ ),
+ full_name: Some(
+ "j_".to_string() + old_name.full_name.as_ref().unwrap_or(&raw_name),
+ ),
+ };
+
+ // I'm so sorry about this
+ // XXX: Update the parsed public symbol's name to use j_ syntax
+ if let Some(idx) = self.named_symbols.remove(&old_name.raw_name) {
+ self.named_symbols.insert(new_name.raw_name.clone(), idx);
+ }
+ if let Some(idx) = self.parsed_symbols_by_name.remove(&old_name.raw_name) {
+ self.parsed_symbols_by_name
+ .insert(new_name.raw_name.clone(), idx);
+ match &mut self.parsed_symbols[idx] {
+ ParsedSymbol::Data(ParsedDataSymbol {
+ name: parsed_name, ..
+ })
+ | ParsedSymbol::Procedure(ParsedProcedure {
+ name: parsed_name, ..
+ }) => {
+ parsed_name.raw_name = new_name.raw_name.clone();
+ parsed_name.short_name = new_name.short_name.clone();
+ parsed_name.full_name = new_name.full_name.clone();
+ }
+ _ => {}
+ }
+ }
+ proc.name = new_name.clone();
+ thunk_name = Some(new_name);
+ }
+ }
+ }
+
+ let locals = vec![];
+ let name = thunk_name.unwrap_or(SymbolNames {
+ raw_name,
+ short_name: name.clone(),
+ full_name: name,
+ });
+
+ self.log(|| format!("THUNK: 0x{:x}: {:?} {:?}", address, &name, &fn_type));
+
+ Ok(Some(ParsedSymbol::Procedure(ParsedProcedure {
+ is_public: false,
+ address,
+ name,
+ type_: fn_type,
+ locals,
+ })))
+ }
+
+ fn handle_separated_code_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &SeparatedCodeSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got SeparatedCode symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_def_range(
+ &mut self,
+ _index: SymbolIndex,
+ data: &DefRangeSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got DefRange symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_def_range_sub_field(
+ &mut self,
+ _index: SymbolIndex,
+ data: &DefRangeSubFieldSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got DefRangeSubField symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_def_range_register(
+ &mut self,
+ _index: SymbolIndex,
+ data: &DefRangeRegisterSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got DefRangeRegister symbol: {:?}", data));
+ if let Some(reg) = self.convert_register(data.register) {
+ Ok(Some(ParsedSymbol::Location(ParsedLocation {
+ location: Variable {
+ ty: VariableSourceType::RegisterVariableSourceType,
+ index: 0,
+ storage: reg.0 as i64,
+ },
+ base_relative: false,
+ stack_relative: false,
+ })))
+ } else {
+ Ok(None)
+ }
+ }
+
+ fn handle_def_range_frame_pointer_relative_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &DefRangeFramePointerRelativeSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got DefRangeFramePointerRelative symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_def_range_frame_pointer_relative_full_scope_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &DefRangeFramePointerRelativeFullScopeSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| {
+ format!(
+ "Got DefRangeFramePointerRelativeFullScope symbol: {:?}",
+ data
+ )
+ });
+ Ok(None)
+ }
+
+ fn handle_def_range_sub_field_register_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &DefRangeSubFieldRegisterSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got DefRangeSubFieldRegister symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_def_range_register_relative_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &DefRangeRegisterRelativeSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got DefRangeRegisterRelative symbol: {:?}", data));
+ Ok(None)
+ }
+
+ fn handle_base_pointer_relative_symbol(
+ &mut self,
+ _index: SymbolIndex,
+ data: &BasePointerRelativeSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got BasePointerRelative symbol: {:?}", data));
+
+ // These are usually parameters if offset > 0
+
+ Ok(Some(ParsedSymbol::LocalVariable(ParsedVariable {
+ name: data.name.to_string().to_string(),
+ type_: self.lookup_type_conf(&data.type_index, false)?,
+ storage: vec![ParsedLocation {
+ location: Variable {
+ ty: VariableSourceType::StackVariableSourceType,
+ index: 0,
+ storage: data.offset as i64,
+ },
+ base_relative: true,
+ stack_relative: false,
+ }],
+ is_param: data.offset as i64 > 0 || data.slot.map_or(false, |slot| slot > 0),
+ })))
+ }
+
+ fn handle_frame_procedure_symbol(
+ &mut self,
+ index: SymbolIndex,
+ data: &FrameProcedureSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got FrameProcedure symbol: {:?}", data));
+
+ // This symbol generally comes before a proc and all various parameters
+ // It has a lot of information we don't care about, and some information we maybe do?
+ // This function also tries to find all the locals and parameters of the procedure
+
+ let mut params = vec![];
+ let mut locals = vec![];
+ let mut seen_offsets = HashSet::new();
+
+ for child in self.symbol_children(index) {
+ match self.lookup_symbol(&child) {
+ Some(ParsedSymbol::LocalVariable(ParsedVariable {
+ name,
+ type_,
+ storage,
+ is_param,
+ ..
+ })) => {
+ let new_storage = storage.iter().map(|&var| var.location).collect::<Vec<_>>();
+
+ // See if the parameter really is a parameter. Sometimes they don't say they are
+ let mut really_is_param = *is_param;
+ for loc in &new_storage {
+ match loc {
+ Variable {
+ ty: VariableSourceType::RegisterVariableSourceType,
+ ..
+ } => {
+ // Assume register vars are always parameters
+ really_is_param = true;
+ }
+ Variable {
+ ty: VariableSourceType::StackVariableSourceType,
+ storage,
+ ..
+ } if *storage >= 0 => {
+ // Sometimes you can get two locals at the same offset, both rbp+(x > 0)
+ // I'm guessing from looking at dumps from dia2dump that only the first
+ // one is considered a parameter, although there are times that I see
+ // two params at the same offset and both are considered parameters...
+ // This doesn't seem possible (or correct) because they would overlap
+ // and only one would be useful anyway.
+ // Regardless of the mess, Binja can only handle one parameter per slot
+ // so we're just going to use the first one.
+ really_is_param = seen_offsets.insert(*storage);
+ }
+ _ => {}
+ }
+ }
+
+ if really_is_param {
+ params.push(ParsedVariable {
+ name: name.clone(),
+ type_: type_.clone(),
+ storage: new_storage
+ .into_iter()
+ .map(|loc| ParsedLocation {
+ location: loc,
+ // This has been handled now
+ base_relative: false,
+ stack_relative: false,
+ })
+ .collect(),
+ is_param: really_is_param,
+ });
+ } else {
+ locals.push(ParsedVariable {
+ name: name.clone(),
+ type_: type_.clone(),
+ storage: new_storage
+ .into_iter()
+ .map(|loc| ParsedLocation {
+ location: loc,
+ // This has been handled now
+ base_relative: false,
+ stack_relative: false,
+ })
+ .collect(),
+ is_param: really_is_param,
+ });
+ }
+ }
+ Some(ParsedSymbol::Data(_)) => {
+ // Apparently you can have static data symbols as parameters
+ // Because of course you can
+ }
+ None => {}
+ e => self.log(|| format!("Unexpected symbol type in frame: {:?}", e)),
+ }
+ }
+
+ Ok(Some(ParsedSymbol::ProcedureInfo(ParsedProcedureInfo {
+ params,
+ locals,
+ })))
+ }
+
+ fn handle_call_site_info(
+ &mut self,
+ _index: SymbolIndex,
+ data: &CallSiteInfoSymbol,
+ ) -> Result<Option<ParsedSymbol>> {
+ self.log(|| format!("Got CallSiteInfo symbol: {:?}", data));
+ Ok(None)
+ }
+
+ /// Demangle a name and get a type out
+ /// Also fixes void(void) and __s_RTTI_Nonsense
+ fn demangle_to_type(
+ &self,
+ raw_name: &String,
+ rva: Rva,
+ ) -> Result<(Option<Conf<Ref<Type>>>, Option<QualifiedName>)> {
+ let (mut t, mut name) = match demangle_ms(&self.arch, raw_name, true) {
+ Some((name, Some(t))) => (Some(Conf::new(t, DEMANGLE_CONFIDENCE)), name),
+ Some((name, _)) => (None, name),
+ _ => (None, QualifiedName::new(vec![raw_name.clone()])),
+ };
+
+ if let Some(ty) = t.as_ref() {
+ if ty.contents.type_class() == TypeClass::FunctionTypeClass {
+ // demangler makes (void) into (void arg1) which is wrong
+ let parameters = ty.contents.parameters().ok_or(anyhow!("no parameters"))?;
+ if let [p] = parameters.as_slice() {
+ if p.ty.contents.type_class() == TypeClass::VoidTypeClass {
+ t = Some(Conf::new(
+ Type::function(
+ &ty.contents
+ .return_value()
+ .ok_or(anyhow!("no return value"))?,
+ vec![],
+ ty.contents.has_variable_arguments().contents,
+ ),
+ ty.confidence,
+ ))
+ }
+ }
+ }
+ }
+
+ // These have types but they aren't actually set anywhere. So it's the demangler's
+ // job to take care of them, apparently?
+ static MEM: OnceLock<Vec<(String, Vec<String>)>> = OnceLock::new();
+ let name_to_type = MEM.get_or_init(|| {
+ vec![
+ (
+ "`RTTI Complete Object Locator'".to_string(),
+ vec![
+ "_s_RTTICompleteObjectLocator".to_string(),
+ "_s__RTTICompleteObjectLocator".to_string(),
+ "_s__RTTICompleteObjectLocator2".to_string(),
+ ],
+ ),
+ (
+ "`RTTI Class Hierarchy Descriptor'".to_string(),
+ vec![
+ "_s_RTTIClassHierarchyDescriptor".to_string(),
+ "_s__RTTIClassHierarchyDescriptor".to_string(),
+ "_s__RTTIClassHierarchyDescriptor2".to_string(),
+ ],
+ ),
+ (
+ // TODO: This type is dynamic
+ "`RTTI Base Class Array'".to_string(),
+ vec![
+ "_s_RTTIBaseClassArray".to_string(),
+ "_s__RTTIBaseClassArray".to_string(),
+ "_s__RTTIBaseClassArray2".to_string(),
+ ],
+ ),
+ (
+ "`RTTI Base Class Descriptor at (".to_string(),
+ vec![
+ "_s_RTTIBaseClassDescriptor".to_string(),
+ "_s__RTTIBaseClassDescriptor".to_string(),
+ "_s__RTTICBaseClassDescriptor2".to_string(),
+ ],
+ ),
+ (
+ "`RTTI Type Descriptor'".to_string(),
+ vec!["_TypeDescriptor".to_string()],
+ ),
+ ]
+ });
+
+ if let Some(last_name) = name.last() {
+ for (search_name, search_types) in name_to_type.iter() {
+ if last_name.contains(search_name) {
+ for search_type in search_types {
+ let qualified_search_type = QualifiedName::from(search_type);
+ if let Some(ty) = self.named_types.get(&qualified_search_type) {
+ // Fallback in case we don't find a specific one
+ t = Some(Conf::new(
+ Type::named_type_from_type(search_type, ty.as_ref()),
+ DEMANGLE_CONFIDENCE,
+ ));
+
+ if self.settings.get_bool_with_opts(
+ "pdb.features.expandRTTIStructures",
+ &mut self.settings_query_opts.clone(),
+ ) {
+ if let Some((lengthy_type, length)) =
+ self.make_lengthy_type(ty, self.bv.start() + rva.0 as u64)?
+ {
+ // See if we have a type with this length
+ let lengthy_name: QualifiedName =
+ format!("${}$_extraBytes_{}", search_type, length).into();
+
+ if let Some(ty) = self.named_types.get(&lengthy_name) {
+ // Wow!
+ t = Some(Conf::new(
+ Type::named_type_from_type(lengthy_name, ty.as_ref()),
+ DEMANGLE_CONFIDENCE,
+ ));
+ } else {
+ t = Some(Conf::new(lengthy_type, DEMANGLE_CONFIDENCE));
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // VTables have types on their data symbols,
+ if let Some((last, class_name)) = name.split_last() {
+ if last.contains("`vftable'") {
+ let mut vt_name = class_name.with_item("VTable");
+ if last.contains("{for") {
+ // DerivedClass::`vftable'{for `BaseClass'}
+ let mut base_name = last.to_owned();
+ base_name.drain(0..("`vftable'{for `".len()));
+ base_name.drain((base_name.len() - "'}".len())..(base_name.len()));
+ // Multiply inherited classes have multiple vtable types
+ // TODO: Do that
+ vt_name = QualifiedName::new(vec![base_name, "VTable".to_string()]);
+ }
+
+ vt_name = vt_name
+ .replace("class ", "")
+ .replace("struct ", "")
+ .replace("enum ", "");
+
+ if let Some(ty) = self.named_types.get(&vt_name) {
+ t = Some(Conf::new(
+ Type::named_type_from_type(vt_name, ty.as_ref()),
+ DEMANGLE_CONFIDENCE,
+ ));
+ } else {
+ // Sometimes the demangler has trouble with `class Foo` in templates
+ vt_name = vt_name
+ .replace("class ", "")
+ .replace("struct ", "")
+ .replace("enum ", "");
+
+ if let Some(ty) = self.named_types.get(&vt_name) {
+ t = Some(Conf::new(
+ Type::named_type_from_type(vt_name, ty.as_ref()),
+ DEMANGLE_CONFIDENCE,
+ ));
+ } else {
+ t = Some(Conf::new(
+ Type::named_type_from_type(
+ vt_name,
+ Type::structure(StructureBuilder::new().finalize().as_ref())
+ .as_ref(),
+ ),
+ DEMANGLE_CONFIDENCE,
+ ));
+ }
+ }
+ }
+ }
+
+ if let Some(last_name) = name.last_mut() {
+ if last_name.starts_with("__imp_") {
+ last_name.drain(0..("__imp_".len()));
+ }
+ }
+
+ let name = if name.len() == 1 && &name[0] == raw_name && raw_name.starts_with('?') {
+ None
+ } else if name.len() == 1 && name[0].is_empty() {
+ None
+ } else if name.len() > 0 && name[0].starts_with("\x7f") {
+ // Not sure why these exist but they do Weird Stuff
+ name[0].drain(0..1);
+ Some(name)
+ } else {
+ Some(name)
+ };
+
+ Ok((t, name))
+ }
+
+ fn make_lengthy_type(
+ &self,
+ base_type: &Ref<Type>,
+ base_address: u64,
+ ) -> Result<Option<(Ref<Type>, usize)>> {
+ if base_type.type_class() != TypeClass::StructureTypeClass {
+ return Ok(None);
+ }
+ let structure = base_type
+ .get_structure()
+ .ok_or(anyhow!("Expected structure"))?;
+ let mut members = structure.members();
+ let last_member = members.last_mut().ok_or(anyhow!("Not enough members"))?;
+
+ if last_member.ty.contents.type_class() != TypeClass::ArrayTypeClass {
+ return Ok(None);
+ }
+ if last_member.ty.contents.count() != 0 {
+ return Ok(None);
+ }
+
+ let member_element = last_member
+ .ty
+ .contents
+ .element_type()
+ .ok_or(anyhow!("Last member has no type"))?
+ .contents;
+ let member_width = member_element.width();
+
+ // Read member_width bytes from bv starting at that member, until we read all zeroes
+ let member_address = base_address + last_member.offset;
+
+ let mut bytes = vec![0; member_width as usize];
+
+ let mut element_count = 0;
+ while self.bv.read(
+ bytes.as_mut_slice(),
+ member_address + member_width * element_count,
+ ) == member_width as usize
+ {
+ if bytes.iter().all(|&b| b == 0) {
+ break;
+ }
+ element_count += 1;
+ }
+
+ // Make a new copy of the type with the correct element count
+ last_member.ty.contents = Type::array(&member_element, element_count);
+
+ Ok(Some((
+ Type::structure(StructureBuilder::from(members).finalize().as_ref()),
+ element_count as usize,
+ )))
+ }
+
+ /// Sorry about the type names
+ /// Given a pdb::Register (u32), get a pdb::register::Register (big enum with names)
+ fn lookup_register(&self, reg: pdb::Register) -> Option<pdb::register::Register> {
+ if let Some(cpu) = self.module_cpu_type {
+ pdb::register::Register::new(reg, cpu).ok()
+ } else {
+ None
+ }
+ }
+
+ /// Convert a pdb::Register (u32) to a binja register index for the current arch
+ fn convert_register(&self, reg: pdb::Register) -> Option<RegisterId> {
+ match self.lookup_register(reg) {
+ Some(X86(xreg)) => {
+ self.log(|| format!("Register {:?} ==> {:?}", reg, xreg));
+ self.arch
+ .register_by_name(xreg.to_string().to_lowercase())
+ .map(|reg| reg.id())
+ }
+ Some(AMD64(areg)) => {
+ self.log(|| format!("Register {:?} ==> {:?}", reg, areg));
+ self.arch
+ .register_by_name(areg.to_string().to_lowercase())
+ .map(|reg| reg.id())
+ }
+ // TODO: Other arches
+ _ => None,
+ }
+ }
+}
diff --git a/plugins/pdb-ng/src/type_parser.rs b/plugins/pdb-ng/src/type_parser.rs
new file mode 100644
index 00000000..e7f4b06d
--- /dev/null
+++ b/plugins/pdb-ng/src/type_parser.rs
@@ -0,0 +1,2453 @@
+// Copyright 2022-2024 Vector 35 Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use std::collections::HashMap;
+use std::sync::OnceLock;
+
+use crate::struct_grouper::group_structure;
+use crate::PDBParserInstance;
+use anyhow::{anyhow, Result};
+use binaryninja::architecture::Architecture;
+use binaryninja::binary_view::BinaryViewExt;
+use binaryninja::calling_convention::CoreCallingConvention;
+use binaryninja::confidence::{Conf, MAX_CONFIDENCE};
+use binaryninja::platform::Platform;
+use binaryninja::rc::Ref;
+use binaryninja::types::{
+ BaseStructure, EnumerationBuilder, EnumerationMember, FunctionParameter, MemberAccess,
+ MemberScope, NamedTypeReference, NamedTypeReferenceClass, QualifiedName, StructureBuilder,
+ StructureMember, StructureType, Type, TypeBuilder, TypeClass,
+};
+use log::warn;
+use pdb::Error::UnimplementedTypeKind;
+use pdb::{
+ ArgumentList, ArrayType, BaseClassType, BitfieldType, ClassKind, ClassType, EnumerateType,
+ EnumerationType, FallibleIterator, FieldAttributes, FieldList, FunctionAttributes, Indirection,
+ ItemFinder, MemberFunctionType, MemberType, MethodList, MethodType, ModifierType, NestedType,
+ OverloadedMethodType, PointerMode, PointerType, PrimitiveKind, PrimitiveType, ProcedureType,
+ Source, StaticMemberType, TypeData, TypeIndex, UnionType, Variant, VirtualBaseClassType,
+ VirtualFunctionTablePointerType, VirtualFunctionTableType, VirtualTableShapeType,
+};
+use regex::Regex;
+
+static BUILTIN_NAMES: &[&str] = &[
+ "size_t",
+ "ssize_t",
+ "ptrdiff_t",
+ "wchar_t",
+ "wchar16",
+ "wchar32",
+ "bool",
+];
+// const VOID_RETURN_CONFIDENCE: u8 = 16;
+
+/// Function types
+#[derive(Debug, Clone)]
+pub struct ParsedProcedureType {
+ /// Interpreted type of the method, with thisptr, __return, etc
+ pub method_type: Ref<Type>,
+ /// Base method type right outta the pdb with no frills
+ pub raw_method_type: Ref<Type>,
+}
+
+/// Bitfield member type, if we ever get around to implementing these
+#[derive(Debug, Clone)]
+pub struct ParsedBitfieldType {
+ /// Size in bits
+ pub size: u64,
+ /// Bit offset in the current bitfield set
+ pub position: u64,
+ /// Underlying type of the whole bitfield set
+ pub ty: Ref<Type>,
+}
+
+/// Parsed member of a class/structure, basically just binaryninja::StructureMember but with bitfields :(
+#[derive(Debug, Clone)]
+pub struct ParsedMember {
+ /// Member type
+ pub ty: Conf<Ref<Type>>,
+ /// Member name
+ pub name: String,
+ /// Offset in structure
+ pub offset: u64,
+ /// Access flags
+ pub access: MemberAccess,
+ /// Scope doesn't really mean anything in binja
+ pub scope: MemberScope,
+ /// Bitfield size, if this is in a bitfield. Mainly you should just be checking for Some()
+ pub bitfield_size: Option<u64>,
+ /// Bit offset, if this is in a bitfield. Mainly you should just be checking for Some()
+ pub bitfield_position: Option<u64>,
+}
+
+/// Parsed named method of a class
+#[derive(Debug, Clone)]
+pub struct ParsedMethod {
+ /// Attributes from pdb-rs
+ pub attributes: FieldAttributes,
+ /// Name of method
+ pub name: String,
+ /// Type of the method + class info
+ pub method_type: ParsedMemberFunction,
+ /// Offset in class's virtual table, if virtual
+ pub vtable_offset: Option<usize>,
+}
+
+/// One entry in a list of parsed methods? This is just here so overloaded methods have a struct to use
+#[derive(Debug, Clone)]
+pub struct ParsedMethodListEntry {
+ /// Attributes from pdb-rs
+ pub attributes: FieldAttributes,
+ /// Type of the method + class info
+ pub method_type: ParsedMemberFunction,
+ /// Offset in class's virtual table, if virtual
+ pub vtable_offset: Option<usize>,
+}
+
+/// Parsed member function type info
+#[derive(Debug, Clone)]
+pub struct ParsedMemberFunction {
+ /// Attributes from pdb-rs
+ pub attributes: FunctionAttributes,
+ /// Parent class's name
+ pub class_name: String,
+ /// Interpreted type of the method, with thisptr, __return, etc
+ pub method_type: Ref<Type>,
+ /// Base method type right outta the pdb with no frills
+ pub raw_method_type: Ref<Type>,
+ /// Type of thisptr object, if relevant
+ pub this_pointer_type: Option<Ref<Type>>,
+ /// Adjust to thisptr at start, for virtual bases or something
+ pub this_adjustment: usize,
+}
+
+/// Virtual base class, c++ nightmare fuel
+#[derive(Debug, Clone)]
+pub struct VirtualBaseClass {
+ /// Base class name
+ pub base_name: QualifiedName,
+ /// Base class type
+ pub base_type: Ref<Type>,
+ /// Offset in this class where the base's fields are located
+ pub base_offset: u64,
+ /// Type of vbtable, probably
+ pub base_table_type: Ref<Type>,
+ /// Offset of this base in the vbtable
+ pub base_table_offset: u64,
+}
+
+/// Mega enum of all the different types of types we can parse
+#[derive(Debug, Clone)]
+pub enum ParsedType {
+ /// No info other than type data
+ Bare(Ref<Type>),
+ /// Named fully parsed class/enum/union/etc type
+ Named(QualifiedName, Ref<Type>),
+ /// Function procedure
+ Procedure(ParsedProcedureType),
+ /// Bitfield entries
+ BitfieldType(ParsedBitfieldType),
+ /// A list of members for a structure / union
+ FieldList(Vec<ParsedType>),
+ /// One member in a structure/union
+ Member(ParsedMember),
+ /// Base class name and offset details
+ BaseClass(QualifiedName, StructureMember),
+ /// One member in an enumeration
+ Enumerate(EnumerationMember),
+ /// List of arguments to a function
+ ArgumentList(Vec<FunctionParameter>),
+ /// Parsed member function type info
+ MemberFunction(ParsedMemberFunction),
+ /// Parsed named method of a class
+ Method(ParsedMethod),
+ /// List of all the methods in a class
+ MethodList(Vec<ParsedMethodListEntry>),
+ /// (Name, Overloads) equivalent to ParsedMethod
+ OverloadedMethod(String, Vec<ParsedMethodListEntry>),
+ /// Virtual table shape straight outta pdb-rs
+ VTableShape(Vec<u8>),
+ /// Also virtual table shape, but you want a pointer this time
+ VTablePointer(Vec<u8>),
+ /// Virtual base class, c++ nightmare fuel
+ VBaseClass(VirtualBaseClass),
+}
+
+#[allow(non_camel_case_types)]
+#[derive(Debug)]
+pub enum CV_call_t {
+ NEAR_C = 1,
+ FAR_C = 2,
+ NEAR_PASCAL = 3,
+ FAR_PASCAL = 4,
+ NEAR_FAST = 5,
+ FAR_FAST = 6,
+ SKIPPED = 7,
+ NEAR_STD = 8,
+ FAR_STD = 9,
+ NEAR_SYS = 10,
+ FAR_SYS = 11,
+ THISCALL = 12,
+ MIPSCALL = 13,
+ GENERIC = 14,
+ ALPHACALL = 15,
+ PPCCALL = 16,
+ SHCALL = 17,
+ ARMCALL = 18,
+ AM33CALL = 19,
+ TRICALL = 20,
+ SH5CALL = 21,
+ M32RCALL = 22,
+ ALWAYS_INLINED = 23,
+ NEAR_VECTOR = 24,
+ RESERVED = 25,
+}
+
+impl TryFrom<u8> for CV_call_t {
+ type Error = anyhow::Error;
+
+ fn try_from(value: u8) -> Result<Self> {
+ match value {
+ 0 => Err(anyhow!("Empty calling convention")),
+ 1 => Ok(Self::NEAR_C),
+ 2 => Ok(Self::FAR_C),
+ 3 => Ok(Self::NEAR_PASCAL),
+ 4 => Ok(Self::FAR_PASCAL),
+ 5 => Ok(Self::NEAR_FAST),
+ 6 => Ok(Self::FAR_FAST),
+ 7 => Ok(Self::SKIPPED),
+ 8 => Ok(Self::NEAR_STD),
+ 9 => Ok(Self::FAR_STD),
+ 10 => Ok(Self::NEAR_SYS),
+ 11 => Ok(Self::FAR_SYS),
+ 12 => Ok(Self::THISCALL),
+ 13 => Ok(Self::MIPSCALL),
+ 14 => Ok(Self::GENERIC),
+ 15 => Ok(Self::ALPHACALL),
+ 16 => Ok(Self::PPCCALL),
+ 17 => Ok(Self::SHCALL),
+ 18 => Ok(Self::ARMCALL),
+ 19 => Ok(Self::AM33CALL),
+ 20 => Ok(Self::TRICALL),
+ 21 => Ok(Self::SH5CALL),
+ 22 => Ok(Self::M32RCALL),
+ 23 => Ok(Self::ALWAYS_INLINED),
+ 24 => Ok(Self::NEAR_VECTOR),
+ 25 => Ok(Self::RESERVED),
+ e => Err(anyhow!("Unknown CV_call_t convention {}", e)),
+ }
+ }
+}
+
+/// This is all done in the parser instance namespace because the lifetimes are impossible to
+/// wrangle otherwise.
+impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
+ /// Parse all the types in a pdb
+ pub fn parse_types(
+ &mut self,
+ progress: Box<dyn Fn(usize, usize) -> Result<()> + '_>,
+ ) -> Result<()> {
+ // Hack: This is needed for primitive types but it's not defined in the pdb itself
+ self.named_types
+ .insert("HRESULT".into(), Type::int(4, true));
+
+ let type_information = self.pdb.type_information()?;
+ let mut finder = type_information.finder();
+
+ let mut type_count = 0;
+
+ // Do an initial pass on the types to find the full indexes for named types
+ // In case something like an array needs to reference them before they're fully defined
+ let mut prepass_types = type_information.iter();
+ while let Some(ty) = prepass_types.next()? {
+ type_count += 1;
+ finder.update(&prepass_types);
+ match ty.parse() {
+ Ok(TypeData::Class(data)) => {
+ if !data.properties.forward_reference() {
+ self.full_type_indices.insert(
+ data.unique_name
+ .unwrap_or(data.name)
+ .to_string()
+ .to_string(),
+ ty.index(),
+ );
+ }
+ }
+ Ok(TypeData::Enumeration(data)) => {
+ if !data.properties.forward_reference() {
+ self.full_type_indices.insert(
+ data.unique_name
+ .unwrap_or(data.name)
+ .to_string()
+ .to_string(),
+ ty.index(),
+ );
+ }
+ }
+ Ok(TypeData::Union(data)) => {
+ if !data.properties.forward_reference() {
+ self.full_type_indices.insert(
+ data.unique_name
+ .unwrap_or(data.name)
+ .to_string()
+ .to_string(),
+ ty.index(),
+ );
+ }
+ }
+ _ => {}
+ }
+ }
+
+ self.log(|| "Now parsing named types");
+
+ // Parse the types we care about, so that recursion gives us parent relationships for free
+ let mut types = type_information.iter();
+ let mut i = 0;
+ while let Some(ty) = types.next()? {
+ i += 1;
+ (progress)(i, type_count * 2)?;
+
+ match ty.parse() {
+ Ok(TypeData::Class(_)) | Ok(TypeData::Enumeration(_)) | Ok(TypeData::Union(_)) => {
+ self.handle_type_index(ty.index(), &mut finder)?;
+ }
+ _ => {}
+ }
+
+ assert!(self.namespace_stack.is_empty());
+ assert!(self.type_stack.is_empty());
+ }
+
+ self.log(|| "Now parsing unused floating types");
+
+ // Parse the rest because symbols often use them
+ let mut postpass_types = type_information.iter();
+ while let Some(ty) = postpass_types.next()? {
+ i += 1;
+ (progress)(i, type_count * 2)?;
+
+ self.handle_type_index(ty.index(), &mut finder)?;
+ }
+
+ self.log(|| "Now adding all unreferenced named types");
+ // Any referenced named types that are only forward-declared will cause missing type references,
+ // so create empty types for those here.
+ for parsed in self.indexed_types.values() {
+ match parsed {
+ ParsedType::Bare(ty) if ty.type_class() == TypeClass::NamedTypeReferenceClass => {
+ // See if we have this type
+ let name = ty
+ .get_named_type_reference()
+ .ok_or(anyhow!("expected ntr"))?
+ .name();
+ if Self::is_name_anonymous(&name) {
+ continue;
+ }
+ if self.named_types.contains_key(&name) {
+ continue;
+ }
+ // If the bv has this type, DebugInfo will just update us to reference it
+ if self.bv.type_by_name(name.to_owned()).is_some() {
+ continue;
+ }
+
+ self.log(|| format!("Got undefined but referenced named type: {}", &name));
+ let type_class = ty
+ .get_named_type_reference()
+ .ok_or(anyhow!("expected ntr"))?
+ .class();
+
+ let bare_type = match type_class {
+ NamedTypeReferenceClass::ClassNamedTypeClass => Type::structure(
+ StructureBuilder::new()
+ .structure_type(StructureType::ClassStructureType)
+ .finalize()
+ .as_ref(),
+ ),
+ // Missing typedefs are just going to become structures
+ NamedTypeReferenceClass::UnknownNamedTypeClass
+ | NamedTypeReferenceClass::TypedefNamedTypeClass
+ | NamedTypeReferenceClass::StructNamedTypeClass => {
+ Type::structure(StructureBuilder::new().finalize().as_ref())
+ }
+ NamedTypeReferenceClass::UnionNamedTypeClass => Type::structure(
+ StructureBuilder::new()
+ .structure_type(StructureType::UnionStructureType)
+ .finalize()
+ .as_ref(),
+ ),
+ NamedTypeReferenceClass::EnumNamedTypeClass => Type::enumeration(
+ EnumerationBuilder::new().finalize().as_ref(),
+ self.arch.default_integer_size().try_into()?,
+ false,
+ ),
+ };
+
+ self.log(|| format!("Bare type created: {} {}", &name, &bare_type));
+ self.named_types.insert(name, bare_type);
+ }
+ _ => {}
+ }
+ }
+
+ // Cleanup a couple builtin names
+ for &name in BUILTIN_NAMES {
+ let builtin_qualified_name = QualifiedName::from(name);
+ if self.named_types.contains_key(&builtin_qualified_name) {
+ self.named_types.remove(&builtin_qualified_name);
+ self.log(|| format!("Remove builtin type {}", name));
+ }
+ }
+
+ static MEM: OnceLock<Regex> = OnceLock::new();
+ let uint_regex = MEM.get_or_init(|| Regex::new(r"u?int\d+_t").unwrap());
+
+ let float_regex = MEM.get_or_init(|| Regex::new(r"float\d+").unwrap());
+
+ let mut remove_names = vec![];
+ for name in self.named_types.keys() {
+ let name_str = name.to_string();
+ if uint_regex.is_match(&name_str) {
+ remove_names.push(name.clone());
+ }
+ if float_regex.is_match(&name_str) {
+ remove_names.push(name.clone());
+ }
+ }
+ for name in remove_names {
+ self.named_types.remove(&name);
+ self.log(|| format!("Remove builtin type {}", &name));
+ }
+
+ Ok(())
+ }
+
+ /// Lookup a type in the parsed types by its index (ie for a procedure)
+ pub(crate) fn lookup_type(
+ &self,
+ index: &TypeIndex,
+ fancy_procs: bool,
+ ) -> Result<Option<Ref<Type>>> {
+ match self.indexed_types.get(index) {
+ Some(ParsedType::Bare(ty)) => Ok(Some(ty.clone())),
+ Some(ParsedType::Named(name, ty)) => {
+ Ok(Some(Type::named_type_from_type(name.clone(), ty)))
+ }
+ Some(ParsedType::Procedure(ParsedProcedureType {
+ method_type,
+ raw_method_type,
+ })) => {
+ if fancy_procs {
+ Ok(Some(method_type.clone()))
+ } else {
+ Ok(Some(raw_method_type.clone()))
+ }
+ }
+ Some(ParsedType::MemberFunction(ParsedMemberFunction {
+ method_type,
+ raw_method_type,
+ ..
+ })) => {
+ if fancy_procs {
+ Ok(Some(method_type.clone()))
+ } else {
+ Ok(Some(raw_method_type.clone()))
+ }
+ }
+ Some(ParsedType::Member(ParsedMember { ty, .. })) => Ok(Some(ty.contents.clone())),
+ _ => Ok(None),
+ }
+ }
+
+ /// Lookup a type in the parsed types and get a confidence value for it too
+ pub(crate) fn lookup_type_conf(
+ &self,
+ index: &TypeIndex,
+ fancy_procs: bool,
+ ) -> Result<Option<Conf<Ref<Type>>>> {
+ match self.lookup_type(index, fancy_procs)? {
+ Some(ty) if ty.type_class() == TypeClass::VoidTypeClass => Ok(Some(Conf::new(ty, 0))),
+ Some(ty) => {
+ let mut confidence = MAX_CONFIDENCE;
+
+ // Extra check here for void(void) functions, they should get minimum confidence since this
+ // is the signature PDB uses when it doesn't actually know the signature
+ if ty.type_class() == TypeClass::FunctionTypeClass {
+ if let Some(ret) = ty.return_value() {
+ if ret.contents.type_class() == TypeClass::VoidTypeClass {
+ if let Some(params) = ty.parameters() {
+ if params.len() == 0 {
+ confidence = 0;
+ }
+ }
+ }
+ }
+ }
+
+ // Also array of bare function pointers (often seen in vtables)
+ // These should not be marked confidently, as they don't actually know
+ // the types of their contents
+
+ if ty.type_class() == TypeClass::ArrayTypeClass {
+ if let Some(ptr) = ty.element_type() {
+ if ptr.contents.type_class() == TypeClass::PointerTypeClass {
+ if let Some(fun) = ptr.contents.target() {
+ if fun.contents.type_class() == TypeClass::FunctionTypeClass
+ && fun
+ .contents
+ .parameters()
+ .map(|pars| pars.len())
+ .unwrap_or(0)
+ == 0
+ {
+ if let Some(ret) = fun.contents.return_value() {
+ if ret.contents.type_class() == TypeClass::VoidTypeClass {
+ confidence = 0;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ Ok(Some(Conf::new(ty, confidence)))
+ }
+ None => Ok(None),
+ }
+ }
+
+ /// Parse and return a type by its index, used as lookup-or-parse
+ fn handle_type_index(
+ &mut self,
+ ty: TypeIndex,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<&ParsedType>> {
+ if let None = self.indexed_types.get(&ty) {
+ self.log(|| format!("Parsing Type {:x?} ", ty));
+
+ match finder.find(ty).and_then(|item| item.parse()) {
+ Ok(data) => {
+ self.type_stack.push(ty);
+ let handled = self.handle_type(&data, finder);
+ self.type_stack.pop();
+
+ match handled {
+ Ok(Some(parsed)) => {
+ self.log(|| format!("Type {} parsed into: {:?}", ty, parsed));
+ match &*parsed {
+ ParsedType::Named(name, parsed) => {
+ // PDB does this thing where anonymous inner types are represented as
+ // some_type::<anonymous-tag>
+ if !Self::is_name_anonymous(name) {
+ if let Some(_old) =
+ self.named_types.insert(name.clone(), parsed.clone())
+ {
+ warn!("Found two types both named `{}`, only one will be used.", name);
+ }
+ }
+ }
+ _ => {}
+ }
+ self.indexed_types.insert(ty, *parsed);
+ }
+ e => {
+ self.log(|| format!("Error parsing type {}: {:x?}", ty, e));
+ }
+ }
+ }
+ Err(UnimplementedTypeKind(k)) if k != 0 => {
+ warn!("Not parsing unimplemented type {}: kind {:x?}", ty, k);
+ }
+ Err(e) => {
+ self.log(|| format!("Could not parse type: {}: {}", ty, e));
+ }
+ };
+ }
+
+ Ok(self.indexed_types.get(&ty))
+ }
+
+ /// Parse a new type's data
+ fn handle_type(
+ &mut self,
+ data: &TypeData,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ match data {
+ TypeData::Primitive(data) => Ok(self.handle_primitive_type(data, finder)?),
+ TypeData::Class(data) => Ok(self.handle_class_type(data, finder)?),
+ TypeData::Member(data) => Ok(self.handle_member_type(data, finder)?),
+ TypeData::MemberFunction(data) => Ok(self.handle_member_function_type(data, finder)?),
+ TypeData::OverloadedMethod(data) => {
+ Ok(self.handle_overloaded_method_type(data, finder)?)
+ }
+ TypeData::Method(data) => Ok(self.handle_method_type(data, finder)?),
+ TypeData::StaticMember(data) => Ok(self.handle_static_member_type(data, finder)?),
+ TypeData::Nested(data) => Ok(self.handle_nested_type(data, finder)?),
+ TypeData::BaseClass(data) => Ok(self.handle_base_class_type(data, finder)?),
+ TypeData::VirtualBaseClass(data) => {
+ Ok(self.handle_virtual_base_class_type(data, finder)?)
+ }
+ TypeData::VirtualFunctionTable(data) => {
+ Ok(self.handle_virtual_function_table_type(data, finder)?)
+ }
+ TypeData::VirtualTableShape(data) => {
+ Ok(self.handle_virtual_table_shape_type(data, finder)?)
+ }
+ TypeData::VirtualFunctionTablePointer(data) => {
+ Ok(self.handle_virtual_function_table_pointer_type(data, finder)?)
+ }
+ TypeData::Procedure(data) => Ok(self.handle_procedure_type(data, finder)?),
+ TypeData::Pointer(data) => Ok(self.handle_pointer_type(data, finder)?),
+ TypeData::Modifier(data) => Ok(self.handle_modifier_type(data, finder)?),
+ TypeData::Enumeration(data) => Ok(self.handle_enumeration_type(data, finder)?),
+ TypeData::Enumerate(data) => Ok(self.handle_enumerate_type(data, finder)?),
+ TypeData::Array(data) => Ok(self.handle_array_type(data, finder)?),
+ TypeData::Union(data) => Ok(self.handle_union_type(data, finder)?),
+ TypeData::Bitfield(data) => Ok(self.handle_bitfield_type(data, finder)?),
+ TypeData::FieldList(data) => Ok(self.handle_field_list_type(data, finder)?),
+ TypeData::ArgumentList(data) => Ok(self.handle_argument_list_type(data, finder)?),
+ TypeData::MethodList(data) => Ok(self.handle_method_list_type(data, finder)?),
+ _ => Err(anyhow!("Unknown typedata")),
+ }
+ }
+
+ /// Get the raw (mangled) name out of a type, if possible
+ fn type_data_to_raw_name(data: &TypeData) -> Option<String> {
+ match data {
+ TypeData::Class(data) => Some(
+ data.unique_name
+ .unwrap_or(data.name)
+ .to_string()
+ .to_string(),
+ ),
+ TypeData::Enumeration(data) => Some(
+ data.unique_name
+ .unwrap_or(data.name)
+ .to_string()
+ .to_string(),
+ ),
+ TypeData::Union(data) => Some(
+ data.unique_name
+ .unwrap_or(data.name)
+ .to_string()
+ .to_string(),
+ ),
+ _ => None,
+ }
+ }
+
+ fn handle_primitive_type(
+ &mut self,
+ data: &PrimitiveType,
+ _finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Primitive type: {:x?}", data));
+ let base = match data.kind {
+ PrimitiveKind::NoType => Ok(Type::void()),
+ PrimitiveKind::Void => Ok(Type::void()),
+ PrimitiveKind::Char => Ok(Type::int(1, true)),
+ PrimitiveKind::UChar => Ok(Type::int(1, false)),
+ PrimitiveKind::RChar => Ok(Type::int(1, true)),
+ PrimitiveKind::WChar => Ok(Type::wide_char(2)),
+ PrimitiveKind::RChar16 => Ok(Type::wide_char(2)),
+ PrimitiveKind::RChar32 => Ok(Type::wide_char(4)),
+ PrimitiveKind::I8 => Ok(Type::int(1, true)),
+ PrimitiveKind::U8 => Ok(Type::int(1, false)),
+ PrimitiveKind::Short => Ok(Type::int(2, true)),
+ PrimitiveKind::UShort => Ok(Type::int(2, false)),
+ PrimitiveKind::I16 => Ok(Type::int(2, true)),
+ PrimitiveKind::U16 => Ok(Type::int(2, false)),
+ PrimitiveKind::Long => Ok(Type::int(4, true)),
+ PrimitiveKind::ULong => Ok(Type::int(4, false)),
+ PrimitiveKind::I32 => Ok(Type::int(4, true)),
+ PrimitiveKind::U32 => Ok(Type::int(4, false)),
+ PrimitiveKind::Quad => Ok(Type::int(8, true)),
+ PrimitiveKind::UQuad => Ok(Type::int(8, false)),
+ PrimitiveKind::I64 => Ok(Type::int(8, true)),
+ PrimitiveKind::U64 => Ok(Type::int(8, false)),
+ PrimitiveKind::Octa => Ok(Type::int(16, true)),
+ PrimitiveKind::UOcta => Ok(Type::int(16, false)),
+ PrimitiveKind::I128 => Ok(Type::int(16, true)),
+ PrimitiveKind::U128 => Ok(Type::int(16, false)),
+ PrimitiveKind::F16 => Ok(Type::float(2)),
+ PrimitiveKind::F32 => Ok(Type::float(4)),
+ PrimitiveKind::F32PP => Ok(Type::float(4)),
+ PrimitiveKind::F48 => Ok(Type::float(6)),
+ PrimitiveKind::F64 => Ok(Type::float(8)),
+ PrimitiveKind::F80 => Ok(Type::float(10)),
+ PrimitiveKind::F128 => Ok(Type::float(16)),
+ PrimitiveKind::Complex32 => Err(anyhow!("Complex32 unimplmented")),
+ PrimitiveKind::Complex64 => Err(anyhow!("Complex64 unimplmented")),
+ PrimitiveKind::Complex80 => Err(anyhow!("Complex80 unimplmented")),
+ PrimitiveKind::Complex128 => Err(anyhow!("Complex128 unimplmented")),
+ PrimitiveKind::Bool8 => Ok(Type::int(1, false)),
+ PrimitiveKind::Bool16 => Ok(Type::int(2, false)),
+ PrimitiveKind::Bool32 => Ok(Type::int(4, false)),
+ PrimitiveKind::Bool64 => Ok(Type::int(8, false)),
+ // Hack: this isn't always defined
+ PrimitiveKind::HRESULT => Ok(Type::named_type_from_type(
+ "HRESULT",
+ Type::int(4, true).as_ref(),
+ )),
+ _ => Err(anyhow!("Unknown type unimplmented")),
+ }?;
+
+ // TODO: Pointer suffix is not exposed
+ match data.indirection {
+ Some(Indirection::Near16) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
+ &self.arch, &base,
+ ))))),
+ Some(Indirection::Far16) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
+ &self.arch, &base,
+ ))))),
+ Some(Indirection::Huge16) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
+ &self.arch, &base,
+ ))))),
+ Some(Indirection::Near32) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
+ &self.arch, &base,
+ ))))),
+ Some(Indirection::Far32) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
+ &self.arch, &base,
+ ))))),
+ Some(Indirection::Near64) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
+ &self.arch, &base,
+ ))))),
+ Some(Indirection::Near128) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
+ &self.arch, &base,
+ ))))),
+ None => Ok(Some(Box::new(ParsedType::Bare(base)))),
+ }
+ }
+
+ fn handle_class_type(
+ &mut self,
+ data: &ClassType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Class type: {:x?}", data));
+
+ let raw_class_name = data.name.to_string();
+ let class_name = QualifiedName::from(raw_class_name);
+
+ self.log(|| format!("Named: {}", class_name));
+
+ if data.properties.forward_reference() {
+ // Try and find it first
+ if let Some(existing) = self.named_types.get(&class_name) {
+ return Ok(Some(Box::new(ParsedType::Bare(
+ Type::named_type_from_type(class_name, existing),
+ ))));
+ }
+
+ let ntr_class = match data.kind {
+ ClassKind::Class => NamedTypeReferenceClass::ClassNamedTypeClass,
+ ClassKind::Struct => NamedTypeReferenceClass::StructNamedTypeClass,
+ ClassKind::Interface => NamedTypeReferenceClass::StructNamedTypeClass,
+ };
+ return Ok(Some(Box::new(ParsedType::Bare(Type::named_type(
+ &NamedTypeReference::new(ntr_class, class_name),
+ )))));
+ }
+
+ let struct_kind = match &data.kind {
+ ClassKind::Class => StructureType::ClassStructureType,
+ ClassKind::Struct => StructureType::StructStructureType,
+ ClassKind::Interface => StructureType::StructStructureType,
+ };
+
+ let mut structure = StructureBuilder::new();
+ structure.structure_type(struct_kind);
+ structure.width(data.size);
+ structure.packed(data.properties.packed());
+
+ if let Some(fields) = data.fields {
+ self.namespace_stack.push(class_name.to_string());
+ let success = self.parse_structure_fields(&mut structure, fields, finder);
+ self.namespace_stack.pop();
+ let _ = success?;
+ }
+
+ let new_type = Type::structure(structure.finalize().as_ref());
+ Ok(Some(Box::new(ParsedType::Named(class_name, new_type))))
+ }
+
+ /// Handle all the structure field parsing for a given field list, putting the fields into a struct
+ fn parse_structure_fields(
+ &mut self,
+ structure: &mut StructureBuilder,
+ fields: TypeIndex,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<()> {
+ let mut base_classes = vec![];
+ let mut virt_methods = HashMap::new();
+ let mut non_virt_methods = Vec::new();
+
+ let mut members = vec![];
+
+ match self.handle_type_index(fields, finder)? {
+ Some(ParsedType::FieldList(fields)) => {
+ for field in fields {
+ match field {
+ ParsedType::Member(member) => {
+ members.push(member.clone());
+ }
+ b @ ParsedType::BaseClass(..) => {
+ base_classes.push(b.clone());
+ }
+ b @ ParsedType::VBaseClass(..) => {
+ base_classes.push(b.clone());
+ }
+ ParsedType::Named(..) => {}
+ ParsedType::VTablePointer(_vt) => {}
+ ParsedType::Method(method) => {
+ if let Some(offset) = method.vtable_offset {
+ virt_methods.insert(
+ offset,
+ (method.name.clone(), method.method_type.clone()),
+ );
+ } else {
+ non_virt_methods
+ .push((method.name.clone(), method.method_type.clone()));
+ }
+ }
+ ParsedType::OverloadedMethod(name, methods) => {
+ for method in methods {
+ if let Some(offset) = method.vtable_offset {
+ virt_methods
+ .insert(offset, (name.clone(), method.method_type.clone()));
+ }
+ }
+ }
+ f => {
+ return Err(anyhow!("Unexpected field type {:?}", f));
+ }
+ }
+ }
+ }
+ Some(_) => {
+ return Err(anyhow!(
+ "Structure fields list did not parse into member list?"
+ ));
+ }
+ // No fields?
+ None => {}
+ }
+
+ // Combine bitfields into structures
+ let mut combined_bitfield_members = vec![];
+ let mut last_bitfield_offset = u64::MAX;
+ let mut last_bitfield_pos = u64::MAX;
+ let mut last_bitfield_idx = 0;
+ let mut bitfield_builder: Option<StructureBuilder> = None;
+
+ fn bitfield_name(offset: u64, idx: u64) -> String {
+ if idx > 0 {
+ format!("__bitfield{:x}_{}", offset, idx)
+ } else {
+ format!("__bitfield{:x}", offset)
+ }
+ }
+
+ for m in members {
+ match (m.bitfield_position, m.bitfield_size) {
+ (Some(pos), Some(_size)) => {
+ if last_bitfield_offset != m.offset || last_bitfield_pos >= pos {
+ if let Some(mut builder) = bitfield_builder.take() {
+ combined_bitfield_members.push(ParsedMember {
+ ty: Conf::new(
+ Type::structure(builder.finalize().as_ref()),
+ MAX_CONFIDENCE,
+ ),
+ name: bitfield_name(last_bitfield_offset, last_bitfield_idx),
+ offset: last_bitfield_offset,
+ access: MemberAccess::PublicAccess,
+ scope: MemberScope::NoScope,
+ bitfield_size: None,
+ bitfield_position: None,
+ });
+ }
+ let mut new_builder = StructureBuilder::new();
+ new_builder.structure_type(StructureType::UnionStructureType);
+ new_builder.width(m.ty.contents.width());
+ bitfield_builder = Some(new_builder);
+
+ if last_bitfield_offset != m.offset {
+ last_bitfield_idx = 0;
+ } else {
+ last_bitfield_idx += 1;
+ }
+ }
+
+ last_bitfield_pos = pos;
+ last_bitfield_offset = m.offset;
+ bitfield_builder
+ .as_mut()
+ .expect("Invariant")
+ .insert(&m.ty, m.name, 0, false, m.access, m.scope);
+ }
+ (None, None) => {
+ if let Some(mut builder) = bitfield_builder.take() {
+ combined_bitfield_members.push(ParsedMember {
+ ty: Conf::new(
+ Type::structure(builder.finalize().as_ref()),
+ MAX_CONFIDENCE,
+ ),
+ name: bitfield_name(last_bitfield_offset, last_bitfield_idx),
+ offset: last_bitfield_offset,
+ access: MemberAccess::PublicAccess,
+ scope: MemberScope::NoScope,
+ bitfield_size: None,
+ bitfield_position: None,
+ });
+ }
+ last_bitfield_offset = u64::MAX;
+ last_bitfield_pos = u64::MAX;
+ combined_bitfield_members.push(m);
+ }
+ e => return Err(anyhow!("Unexpected bitfield parameters {:?}", e)),
+ }
+ }
+ if let Some(mut builder) = bitfield_builder.take() {
+ combined_bitfield_members.push(ParsedMember {
+ ty: Conf::new(Type::structure(builder.finalize().as_ref()), MAX_CONFIDENCE),
+ name: bitfield_name(last_bitfield_offset, last_bitfield_idx),
+ offset: last_bitfield_offset,
+ access: MemberAccess::PublicAccess,
+ scope: MemberScope::NoScope,
+ bitfield_size: None,
+ bitfield_position: None,
+ });
+ }
+ members = combined_bitfield_members;
+ group_structure(
+ &format!(
+ "`{}`",
+ self.namespace_stack
+ .items
+ .last()
+ .ok_or_else(|| anyhow!("Expected class in ns stack"))?
+ ),
+ &members,
+ structure,
+ )?;
+
+ let mut bases = vec![];
+
+ for base_class in &base_classes {
+ match base_class {
+ ParsedType::BaseClass(name, base) => {
+ let ntr_class = match self.named_types.get(name) {
+ Some(ty) if ty.type_class() == TypeClass::StructureTypeClass => {
+ match ty.get_structure() {
+ Some(str)
+ if str.structure_type()
+ == StructureType::StructStructureType =>
+ {
+ NamedTypeReferenceClass::StructNamedTypeClass
+ }
+ Some(str)
+ if str.structure_type()
+ == StructureType::ClassStructureType =>
+ {
+ NamedTypeReferenceClass::ClassNamedTypeClass
+ }
+ _ => NamedTypeReferenceClass::StructNamedTypeClass,
+ }
+ }
+ _ => NamedTypeReferenceClass::StructNamedTypeClass,
+ };
+ bases.push(BaseStructure::new(
+ NamedTypeReference::new(ntr_class, name.clone()),
+ base.offset,
+ base.ty.contents.width(),
+ ));
+ }
+ ParsedType::VBaseClass(VirtualBaseClass {
+ base_name,
+ base_type,
+ base_offset,
+ ..
+ }) => {
+ let ntr_class = match self.named_types.get(base_name) {
+ Some(ty) if ty.type_class() == TypeClass::StructureTypeClass => {
+ match ty.get_structure() {
+ Some(str)
+ if str.structure_type()
+ == StructureType::StructStructureType =>
+ {
+ NamedTypeReferenceClass::StructNamedTypeClass
+ }
+ Some(str)
+ if str.structure_type()
+ == StructureType::ClassStructureType =>
+ {
+ NamedTypeReferenceClass::ClassNamedTypeClass
+ }
+ _ => NamedTypeReferenceClass::StructNamedTypeClass,
+ }
+ }
+ _ => NamedTypeReferenceClass::StructNamedTypeClass,
+ };
+ bases.push(BaseStructure::new(
+ NamedTypeReference::new(ntr_class, base_name.clone()),
+ *base_offset,
+ base_type.width(),
+ ));
+ warn!(
+ "Class `{}` uses virtual inheritance. Type information may be inaccurate.",
+ self.namespace_stack
+ .items
+ .last()
+ .ok_or_else(|| anyhow!("Expected class in ns stack"))?
+ );
+ }
+ e => return Err(anyhow!("Unexpected base class type: {:x?}", e)),
+ }
+ }
+
+ if bases.len() > 1 {
+ warn!(
+ "Class `{}` has multiple base classes. Type information may be inaccurate.",
+ self.namespace_stack
+ .items
+ .last()
+ .ok_or_else(|| anyhow!("Expected class in ns stack"))?
+ );
+ }
+ structure.base_structures(&bases);
+
+ if self.settings.get_bool_with_opts(
+ "pdb.features.generateVTables",
+ &mut self.settings_query_opts,
+ ) && !virt_methods.is_empty()
+ {
+ let mut vt = StructureBuilder::new();
+
+ let mut vt_bases = vec![];
+
+ for base_class in &base_classes {
+ match base_class {
+ ParsedType::BaseClass(base_name, _base_type) => {
+ let mut vt_base_name = base_name.clone();
+ vt_base_name.items.push("VTable".to_string());
+
+ match self.named_types.get(&vt_base_name) {
+ Some(vt_base_type)
+ if vt_base_type.type_class() == TypeClass::StructureTypeClass =>
+ {
+ let ntr_class =
+ if vt_base_type.type_class() == TypeClass::StructureTypeClass {
+ match vt_base_type.get_structure() {
+ Some(str)
+ if str.structure_type()
+ == StructureType::StructStructureType =>
+ {
+ NamedTypeReferenceClass::StructNamedTypeClass
+ }
+ Some(str)
+ if str.structure_type()
+ == StructureType::ClassStructureType =>
+ {
+ NamedTypeReferenceClass::ClassNamedTypeClass
+ }
+ _ => NamedTypeReferenceClass::StructNamedTypeClass,
+ }
+ } else {
+ NamedTypeReferenceClass::StructNamedTypeClass
+ };
+ vt_bases.push(BaseStructure::new(
+ NamedTypeReference::new(ntr_class, vt_base_name),
+ 0,
+ vt_base_type.width(),
+ ));
+ }
+ e @ Some(_) => {
+ return Err(anyhow!("Unexpected vtable base class: {:?}", e))
+ }
+ None => {
+ // Parent might just not have a vtable
+ }
+ }
+ }
+ ParsedType::VBaseClass(_vbase) => {}
+ e => return Err(anyhow!("Unexpected base class type: {:x?}", e)),
+ }
+ }
+
+ let mut min_width = 0;
+ for base in &vt_bases {
+ min_width = min_width.max(base.width);
+ }
+
+ vt.base_structures(&vt_bases);
+ vt.propagates_data_var_refs(true);
+
+ for (offset, (name, method)) in virt_methods {
+ vt.insert(
+ &Conf::new(
+ Type::pointer(&self.arch, &Conf::new(method.method_type, MAX_CONFIDENCE)),
+ MAX_CONFIDENCE,
+ ),
+ &name,
+ offset as u64,
+ true,
+ MemberAccess::PublicAccess,
+ MemberScope::NoScope,
+ );
+ min_width = min_width.max((offset + self.arch.address_size()) as u64);
+ }
+
+ vt.width(min_width);
+
+ let vt_type = Type::structure(vt.finalize().as_ref());
+ // Need to insert a new named type for the vtable
+ if self.namespace_stack.is_empty() {
+ return Err(anyhow!("Expected class in ns stack"));
+ }
+
+ let mut vt_name = self.namespace_stack.clone();
+ vt_name.items.push("VTable".to_string());
+ self.named_types.insert(vt_name.clone(), vt_type.clone());
+
+ let vt_pointer = Type::pointer(
+ &self.arch,
+ &Conf::new(
+ Type::named_type_from_type(vt_name, vt_type.as_ref()),
+ MAX_CONFIDENCE,
+ ),
+ );
+
+ structure.insert(
+ &Conf::new(vt_pointer, MAX_CONFIDENCE),
+ "vtable",
+ 0,
+ true,
+ MemberAccess::PublicAccess,
+ MemberScope::NoScope,
+ );
+ }
+
+ Ok(())
+ }
+
+ fn handle_member_type(
+ &mut self,
+ data: &MemberType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Member type: {:x?}", data));
+
+ let member_name = data.name.to_string();
+ let member_offset = data.offset;
+ let member_attrs = data.attributes;
+
+ let access = match member_attrs.access() {
+ 1 /* CV_private */ => MemberAccess::PrivateAccess,
+ 2 /* CV_protected */ => MemberAccess::ProtectedAccess,
+ 3 /* CV_public */ => MemberAccess::PublicAccess,
+ _ => return Err(anyhow!("Unknown access"))
+ };
+
+ let scope = MemberScope::NoScope;
+
+ match self.try_type_index_to_bare(data.field_type, finder, true)? {
+ Some(ty) => Ok(Some(Box::new(ParsedType::Member(ParsedMember {
+ ty: Conf::new(ty, MAX_CONFIDENCE),
+ name: member_name.into_owned(),
+ offset: member_offset,
+ access,
+ scope,
+ bitfield_position: None,
+ bitfield_size: None,
+ })))),
+ None => match self.handle_type_index(data.field_type, finder)? {
+ Some(ParsedType::BitfieldType(bitfield)) => {
+ Ok(Some(Box::new(ParsedType::Member(ParsedMember {
+ ty: Conf::new(bitfield.ty.clone(), MAX_CONFIDENCE),
+ name: member_name.into_owned(),
+ offset: member_offset,
+ access,
+ scope,
+ bitfield_position: Some(bitfield.position),
+ bitfield_size: Some(bitfield.size),
+ }))))
+ }
+ e => Err(anyhow!("Unexpected member type: {:x?}", e)),
+ },
+ }
+ }
+
+ fn handle_member_function_type(
+ &mut self,
+ data: &MemberFunctionType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got MemberFunction type: {:x?}", data));
+ let return_type = self.type_index_to_bare(data.return_type, finder, false)?;
+
+ let class_name = match self.handle_type_index(data.class_type, finder)? {
+ Some(ParsedType::Bare(ty)) if ty.type_class() == TypeClass::NamedTypeReferenceClass => {
+ ty.get_named_type_reference()
+ .ok_or(anyhow!("Expected NTR to have NTR"))?
+ .name()
+ .to_string()
+ }
+ e => return Err(anyhow!("Unexpected class type: {:x?}", e)),
+ };
+
+ let this_pointer_type = if let Some(this_pointer_type) = data.this_pointer_type {
+ match self.handle_type_index(this_pointer_type, finder)? {
+ Some(ParsedType::Bare(ty)) => Some(ty.clone()),
+ e => return Err(anyhow!("Unexpected this pointer type: {:x?}", e)),
+ }
+ } else {
+ None
+ };
+
+ let mut arguments = match self.handle_type_index(data.argument_list, finder)? {
+ Some(ParsedType::ArgumentList(args)) => args.clone(),
+ e => return Err(anyhow!("Unexpected argument list type: {:x?}", e)),
+ };
+
+ // It looks like pdb stores varargs by having the final argument be void
+ let mut is_varargs = false;
+ if let Some(last) = arguments.pop() {
+ if last.ty.contents.as_ref().type_class() == TypeClass::VoidTypeClass {
+ is_varargs = true;
+ } else {
+ arguments.push(last);
+ }
+ }
+
+ let mut fancy_return_type = return_type.clone();
+ let mut fancy_arguments = arguments.clone();
+
+ if data.attributes.cxx_return_udt()
+ || !self.can_fit_in_register(data.return_type, finder, true)
+ {
+ // Return UDT??
+ // This probably means the return value got pushed to the stack
+ fancy_return_type =
+ Type::pointer(&self.arch, &Conf::new(return_type.clone(), MAX_CONFIDENCE));
+ fancy_arguments.insert(
+ 0,
+ FunctionParameter::new(
+ Conf::new(fancy_return_type.clone(), MAX_CONFIDENCE),
+ "__return".to_string(),
+ None,
+ ),
+ );
+ }
+
+ if let Some(this_ptr) = &this_pointer_type {
+ self.insert_this_pointer(&mut fancy_arguments, this_ptr.clone())?;
+ }
+
+ let convention = self
+ .cv_call_t_to_calling_convention(data.attributes.calling_convention())
+ .map(|cc| Conf::new(cc, MAX_CONFIDENCE))
+ .unwrap_or({
+ if is_varargs {
+ Conf::new(self.cdecl_cc.clone(), MAX_CONFIDENCE)
+ } else if this_pointer_type.is_some() {
+ Conf::new(self.thiscall_cc.clone(), MAX_CONFIDENCE)
+ } else {
+ Conf::new(self.default_cc.clone(), 16)
+ }
+ });
+
+ let func = Type::function_with_opts(
+ &Conf::new(return_type, MAX_CONFIDENCE),
+ arguments.as_slice(),
+ is_varargs,
+ convention.clone(),
+ Conf::new(0, 0),
+ );
+
+ let fancy_func = Type::function_with_opts(
+ &Conf::new(fancy_return_type, MAX_CONFIDENCE),
+ fancy_arguments.as_slice(),
+ is_varargs,
+ convention,
+ Conf::new(0, 0),
+ );
+
+ Ok(Some(Box::new(ParsedType::MemberFunction(
+ ParsedMemberFunction {
+ attributes: data.attributes,
+ class_name,
+ method_type: fancy_func,
+ raw_method_type: func,
+ this_pointer_type,
+ this_adjustment: data.this_adjustment as usize,
+ },
+ ))))
+ }
+
+ fn handle_overloaded_method_type(
+ &mut self,
+ data: &OverloadedMethodType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got OverloadedMethod type: {:x?}", data));
+ // This is just a MethodList in disguise
+ let method_list = match self.handle_type_index(data.method_list, finder)? {
+ Some(ParsedType::MethodList(list)) => list.clone(),
+ e => return Err(anyhow!("Unexpected method list type: {:x?}", e)),
+ };
+
+ Ok(Some(Box::new(ParsedType::OverloadedMethod(
+ data.name.to_string().to_string(),
+ method_list,
+ ))))
+ }
+
+ fn handle_method_type(
+ &mut self,
+ data: &MethodType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Method type: {:x?}", data));
+
+ let member_function = match self.handle_type_index(data.method_type, finder)? {
+ Some(ParsedType::MemberFunction(func)) => func.clone(),
+ e => return Err(anyhow!("Unexpected method type {:?}", e)),
+ };
+
+ Ok(Some(Box::new(ParsedType::Method(ParsedMethod {
+ attributes: data.attributes,
+ name: data.name.to_string().to_string(),
+ method_type: member_function,
+ vtable_offset: data.vtable_offset.map(|o| o as usize),
+ }))))
+ }
+
+ fn handle_static_member_type(
+ &mut self,
+ data: &StaticMemberType,
+ _finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got StaticMember type: {:x?}", data));
+ // TODO: Not handling these
+ Ok(None)
+ }
+
+ fn handle_nested_type(
+ &mut self,
+ data: &NestedType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Nested type: {:x?}", data));
+ let mut class_name_ns = self.namespace_stack.clone();
+ class_name_ns.push(data.name.to_string().into());
+ let ty = self.type_index_to_bare(data.nested_type, finder, false)?;
+ Ok(Some(Box::new(ParsedType::Named(class_name_ns, ty))))
+ }
+
+ fn handle_base_class_type(
+ &mut self,
+ data: &BaseClassType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got BaseClass type: {:x?}", data));
+
+ let base_offset = data.offset;
+ let base_attrs = data.attributes;
+
+ let (member_name, t) = match self.handle_type_index(data.base_class, finder)? {
+ Some(ParsedType::Named(n, t)) => (n.clone(), t.clone()),
+ Some(ParsedType::Bare(t)) if t.type_class() == TypeClass::NamedTypeReferenceClass => {
+ let name = t
+ .get_named_type_reference()
+ .ok_or(anyhow!("Expected NTR to have NTR"))?
+ .name();
+ (name, t.clone())
+ }
+ e => return Err(anyhow!("Unexpected base class type: {:x?}", e)),
+ };
+
+ // Try to resolve the full base type
+ let resolved_type = match self.try_type_index_to_bare(data.base_class, finder, true)? {
+ Some(ty) => Type::named_type_from_type(member_name.clone(), ty.as_ref()),
+ None => t.clone(),
+ };
+
+ let access = match base_attrs.access() {
+ 1 /* CV_private */ => MemberAccess::PrivateAccess,
+ 2 /* CV_protected */ => MemberAccess::ProtectedAccess,
+ 3 /* CV_public */ => MemberAccess::PublicAccess,
+ _ => return Err(anyhow!("Unknown access"))
+ };
+
+ let scope = MemberScope::NoScope;
+ Ok(Some(Box::new(ParsedType::BaseClass(
+ member_name.clone(),
+ StructureMember::new(
+ Conf::new(resolved_type, MAX_CONFIDENCE),
+ member_name.to_string(),
+ base_offset as u64,
+ access,
+ scope,
+ ),
+ ))))
+ }
+
+ fn handle_virtual_base_class_type(
+ &mut self,
+ data: &VirtualBaseClassType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got VirtualBaseClass type: {:x?}", data));
+
+ let (n, ty) = match self.handle_type_index(data.base_class, finder)? {
+ Some(ParsedType::Named(n, t)) => (n.clone(), t.clone()),
+ Some(ParsedType::Bare(t)) if t.type_class() == TypeClass::NamedTypeReferenceClass => {
+ let name = t
+ .get_named_type_reference()
+ .ok_or(anyhow!("Expected NTR to have NTR"))?
+ .name();
+ (name, t.clone())
+ }
+ e => return Err(anyhow!("Unexpected base class type: {:x?}", e)),
+ };
+
+ // In addition to the base class, we also have a vbtable
+ let vbptr_type = match self.handle_type_index(data.base_pointer, finder)? {
+ Some(ParsedType::Bare(t)) => t.clone(),
+ e => return Err(anyhow!("Unexpected virtual base pointer type: {:x?}", e)),
+ };
+
+ Ok(Some(Box::new(ParsedType::VBaseClass(VirtualBaseClass {
+ base_name: n,
+ base_type: ty,
+ base_offset: data.base_pointer_offset as u64,
+ base_table_type: vbptr_type,
+ base_table_offset: data.virtual_base_offset as u64,
+ }))))
+ }
+
+ fn handle_virtual_function_table_type(
+ &mut self,
+ data: &VirtualFunctionTableType,
+ _finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got VirtualFunctionTableType type: {:x?}", data));
+ Err(anyhow!("VirtualFunctionTableType unimplemented"))
+ }
+
+ fn handle_virtual_table_shape_type(
+ &mut self,
+ data: &VirtualTableShapeType,
+ _finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got VirtualTableShapeType type: {:x?}", data));
+ Ok(Some(Box::new(ParsedType::VTableShape(
+ data.descriptors.clone(),
+ ))))
+ }
+
+ fn handle_virtual_function_table_pointer_type(
+ &mut self,
+ data: &VirtualFunctionTablePointerType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got VirtualFunctionTablePointer type: {:x?}", data));
+ let shape = match self.handle_type_index(data.table, finder)? {
+ Some(ParsedType::VTablePointer(shape)) => shape.clone(),
+ e => {
+ return Err(anyhow!(
+ "Could not parse virtual function table pointer type: {:x?}",
+ e
+ ))
+ }
+ };
+
+ Ok(Some(Box::new(ParsedType::VTablePointer(shape))))
+ }
+
+ fn handle_procedure_type(
+ &mut self,
+ data: &ProcedureType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Procedure type: {:x?}", data));
+ let return_type = if let Some(return_type_index) = data.return_type {
+ self.try_type_index_to_bare(return_type_index, finder, false)?
+ } else {
+ None
+ }
+ .map(|r| Conf::new(r, MAX_CONFIDENCE))
+ .unwrap_or(Conf::new(Type::void(), 0));
+
+ let mut arguments = match self.handle_type_index(data.argument_list, finder)? {
+ Some(ParsedType::ArgumentList(args)) => args.clone(),
+ e => return Err(anyhow!("Unexpected argument list type: {:x?}", e)),
+ };
+
+ // It looks like pdb stores varargs by having the final argument be void
+ let mut is_varargs = false;
+ if let Some(last) = arguments.pop() {
+ if last.ty.contents.as_ref().type_class() == TypeClass::VoidTypeClass {
+ is_varargs = true;
+ } else {
+ arguments.push(last);
+ }
+ }
+
+ let mut fancy_return_type = return_type.clone();
+ let mut fancy_arguments = arguments.clone();
+
+ let mut return_stacky = data.attributes.cxx_return_udt();
+ if let Some(return_type_index) = data.return_type {
+ return_stacky |= !self.can_fit_in_register(return_type_index, finder, true);
+ }
+ if return_stacky {
+ // Stack return via a pointer in the first parameter
+ fancy_return_type = Conf::new(
+ Type::pointer(&self.arch, &return_type.clone()),
+ MAX_CONFIDENCE,
+ );
+ fancy_arguments.insert(
+ 0,
+ FunctionParameter::new(fancy_return_type.clone(), "__return".to_string(), None),
+ );
+ }
+
+ let convention = self
+ .cv_call_t_to_calling_convention(data.attributes.calling_convention())
+ .map(|cc| Conf::new(cc, MAX_CONFIDENCE))
+ .unwrap_or(Conf::new(self.default_cc.clone(), 0));
+ self.log(|| format!("Convention: {:?}", convention));
+
+ let func = Type::function_with_opts(
+ &return_type,
+ arguments.as_slice(),
+ is_varargs,
+ convention.clone(),
+ Conf::new(0, 0),
+ );
+
+ let fancy_func = Type::function_with_opts(
+ &fancy_return_type,
+ fancy_arguments.as_slice(),
+ is_varargs,
+ convention,
+ Conf::new(0, 0),
+ );
+
+ Ok(Some(Box::new(ParsedType::Procedure(ParsedProcedureType {
+ method_type: fancy_func,
+ raw_method_type: func,
+ }))))
+ }
+
+ fn handle_pointer_type(
+ &mut self,
+ data: &PointerType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Pointer type: {:x?}", data));
+ let base = match self.try_type_index_to_bare(data.underlying_type, finder, false)? {
+ Some(ty) => Some(ty.clone()),
+ None => match self.handle_type_index(data.underlying_type, finder)? {
+ Some(ParsedType::VTableShape(descriptors)) => {
+ return Ok(Some(Box::new(ParsedType::VTablePointer(
+ descriptors.clone(),
+ ))));
+ }
+ _ => None,
+ },
+ };
+
+ if let Some(base) = base {
+ Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
+ &self.arch, &base,
+ )))))
+ } else {
+ Ok(None)
+ }
+ }
+
+ fn handle_modifier_type(
+ &mut self,
+ data: &ModifierType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Modifier type: {:x?}", data));
+ let base = self.try_type_index_to_bare(data.underlying_type, finder, false)?;
+
+ if let Some(base) = base {
+ let builder = TypeBuilder::new(base.as_ref());
+ builder.set_const(data.constant);
+ builder.set_volatile(data.volatile);
+ Ok(Some(Box::new(ParsedType::Bare(builder.finalize()))))
+ } else {
+ Ok(None)
+ }
+ }
+
+ fn handle_enumeration_type(
+ &mut self,
+ data: &EnumerationType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Enumeration type: {:x?}", data));
+
+ let raw_enum_name = data.name.to_string();
+ let enum_name = QualifiedName::from(raw_enum_name);
+ self.log(|| format!("Named: {}", enum_name));
+
+ if data.properties.forward_reference() {
+ // Try and find it first
+ if let Some(existing) = self.named_types.get(&enum_name) {
+ return Ok(Some(Box::new(ParsedType::Bare(
+ Type::named_type_from_type(enum_name, existing),
+ ))));
+ }
+
+ let ntr_class = NamedTypeReferenceClass::EnumNamedTypeClass;
+ return Ok(Some(Box::new(ParsedType::Bare(Type::named_type(
+ &NamedTypeReference::new(ntr_class, enum_name),
+ )))));
+ }
+
+ let enumeration = EnumerationBuilder::new();
+
+ match self.handle_type_index(data.fields, finder)? {
+ Some(ParsedType::FieldList(fields)) => {
+ for field in fields {
+ match field {
+ ParsedType::Enumerate(member) => {
+ enumeration.insert(member.name.clone(), member.value);
+ }
+ e => return Err(anyhow!("Unexpected enumerate member: {:?}", e)),
+ }
+ }
+ }
+ // No fields?
+ None => {}
+ e => return Err(anyhow!("Unexpected enumeration field list: {:?}", e)),
+ }
+
+ let underlying = match self.handle_type_index(data.underlying_type, finder)? {
+ Some(ParsedType::Bare(ty)) => ty.clone(),
+ e => return Err(anyhow!("Making enumeration from unexpected type: {:x?}", e)),
+ };
+
+ let new_type = Type::enumeration(
+ enumeration.finalize().as_ref(),
+ // TODO: This looks bad, look at the comment in [`Type::width`].
+ (underlying.width() as usize).try_into()?,
+ underlying.is_signed().contents,
+ );
+
+ Ok(Some(Box::new(ParsedType::Named(enum_name, new_type))))
+ }
+
+ fn handle_enumerate_type(
+ &mut self,
+ data: &EnumerateType,
+ _finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Enumerate type: {:x?}", data));
+ Ok(Some(Box::new(ParsedType::Enumerate(EnumerationMember {
+ name: data.name.to_string().to_string(),
+ value: match data.value {
+ Variant::U8(v) => v as u64,
+ Variant::U16(v) => v as u64,
+ Variant::U32(v) => v as u64,
+ Variant::U64(v) => v as u64,
+ Variant::I8(v) => (v as i64) as u64,
+ Variant::I16(v) => (v as i64) as u64,
+ Variant::I32(v) => (v as i64) as u64,
+ Variant::I64(v) => (v as i64) as u64,
+ },
+ default: false,
+ }))))
+ }
+
+ fn handle_array_type(
+ &mut self,
+ data: &ArrayType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Array type: {:x?}", data));
+ // PDB stores array sizes as TOTAL bytes not element count
+ // So we need to look up the original type's size to know how many there are
+ let base = self.try_type_index_to_bare(data.element_type, finder, true)?;
+
+ if let Some(base) = base {
+ let mut new_type = base;
+ if new_type.width() == 0 {
+ if new_type.width() == 0 {
+ return Err(anyhow!(
+ "Cannot calculate array of 0-size elements: {}",
+ new_type
+ ));
+ }
+ }
+
+ let mut counts = data
+ .dimensions
+ .iter()
+ .map(|t| *t as u64)
+ .collect::<Vec<_>>();
+ for i in 0..counts.len() {
+ for j in i..counts.len() {
+ if counts[j] % new_type.width() != 0 {
+ return Err(anyhow!(
+ "Array stride {} is not a multiple of element {} size {}",
+ counts[j],
+ new_type,
+ new_type.width()
+ ));
+ }
+ counts[j] /= new_type.width();
+ }
+
+ new_type = Type::array(&new_type, counts[i]);
+ }
+
+ Ok(Some(Box::new(ParsedType::Bare(new_type))))
+ } else {
+ Ok(None)
+ }
+ }
+
+ fn handle_union_type(
+ &mut self,
+ data: &UnionType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Union type: {:x?}", data));
+
+ let raw_union_name = data.name.to_string();
+ let union_name = QualifiedName::from(raw_union_name);
+ self.log(|| format!("Named: {}", union_name));
+
+ if data.properties.forward_reference() {
+ // Try and find it first
+ if let Some(existing) = self.named_types.get(&union_name) {
+ return Ok(Some(Box::new(ParsedType::Bare(
+ Type::named_type_from_type(union_name, existing),
+ ))));
+ }
+
+ let ntr_class = NamedTypeReferenceClass::UnionNamedTypeClass;
+ return Ok(Some(Box::new(ParsedType::Bare(Type::named_type(
+ &NamedTypeReference::new(ntr_class, union_name),
+ )))));
+ }
+
+ let mut structure = StructureBuilder::new();
+ structure.structure_type(StructureType::UnionStructureType);
+ structure.width(data.size);
+
+ self.namespace_stack.push(union_name.to_string());
+ let success = self.parse_union_fields(&mut structure, data.fields, finder);
+ self.namespace_stack.pop();
+ let _ = success?;
+
+ let new_type = Type::structure(structure.finalize().as_ref());
+ Ok(Some(Box::new(ParsedType::Named(union_name, new_type))))
+ }
+
+ /// Parse the fields in a union's field list
+ fn parse_union_fields(
+ &mut self,
+ structure: &mut StructureBuilder,
+ fields: TypeIndex,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<()> {
+ let mut union_groups = vec![];
+ let mut last_union_group = u64::MAX;
+
+ match self.handle_type_index(fields, finder) {
+ Ok(Some(ParsedType::FieldList(fields))) => {
+ for field in fields {
+ match field {
+ ParsedType::Member(member) => {
+ if member.offset <= last_union_group {
+ union_groups.push(vec![]);
+ }
+ last_union_group = member.offset;
+ union_groups
+ .last_mut()
+ .expect("invariant")
+ .push(member.clone());
+ }
+ ParsedType::Method(..) => {}
+ ParsedType::Named(..) => {}
+ e => return Err(anyhow!("Unexpected union member type {:?}", e)),
+ }
+ }
+ }
+ e => return Err(anyhow!("Unexpected union field list type {:?}", e)),
+ }
+
+ for (i, group) in union_groups.into_iter().enumerate() {
+ if group.len() == 1 {
+ structure.insert(
+ &group[0].ty,
+ group[0].name.clone(),
+ group[0].offset,
+ false,
+ group[0].access,
+ group[0].scope,
+ );
+ } else {
+ let mut inner_struct = StructureBuilder::new();
+ for member in group {
+ inner_struct.insert(
+ &member.ty,
+ member.name.clone(),
+ member.offset,
+ false,
+ member.access,
+ member.scope,
+ );
+ }
+ structure.insert(
+ &Conf::new(
+ Type::structure(inner_struct.finalize().as_ref()),
+ MAX_CONFIDENCE,
+ ),
+ format!("__inner{:x}", i),
+ 0,
+ false,
+ MemberAccess::PublicAccess,
+ MemberScope::NoScope,
+ );
+ }
+ }
+
+ Ok(())
+ }
+
+ fn handle_bitfield_type(
+ &mut self,
+ data: &BitfieldType,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got Bitfield type: {:x?}", data));
+ Ok(self
+ .try_type_index_to_bare(data.underlying_type, finder, true)?
+ .map(|ty| {
+ Box::new(ParsedType::BitfieldType(ParsedBitfieldType {
+ size: data.length as u64,
+ position: data.position as u64,
+ ty,
+ }))
+ }))
+ }
+
+ fn handle_field_list_type(
+ &mut self,
+ data: &FieldList,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got FieldList type: {:x?}", data));
+
+ let mut fields = vec![];
+ for (i, field) in data.fields.iter().enumerate() {
+ match self.handle_type(field, finder)? {
+ Some(f) => {
+ self.log(|| format!("Inner field {} parsed into {:?}", i, f));
+ fields.push(*f);
+ }
+ None => {
+ self.log(|| format!("Inner field {} parsed into None", i));
+ }
+ }
+ }
+
+ if let Some(cont) = data.continuation {
+ match self.handle_type_index(cont, finder)? {
+ Some(ParsedType::FieldList(cont_fields)) => {
+ fields.extend(cont_fields.clone());
+ }
+ None => {}
+ f => {
+ return Err(anyhow!("Unexpected field list continuation {:?}", f));
+ }
+ }
+ }
+ Ok(Some(Box::new(ParsedType::FieldList(fields))))
+ }
+
+ fn handle_argument_list_type(
+ &mut self,
+ data: &ArgumentList,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got ArgumentList type: {:x?}", data));
+ let mut args = vec![];
+ for &arg in data.arguments.iter() {
+ match self.try_type_index_to_bare(arg, finder, false)? {
+ Some(ty) => {
+ // On x86_32, structures are stored on the stack directly
+ // On x64, they are put into pointers if they are not a int size
+ // TODO: Ugly hack
+ if self.arch.address_size() == 4 || Self::size_can_fit_in_register(ty.width()) {
+ args.push(FunctionParameter::new(
+ Conf::new(ty.clone(), MAX_CONFIDENCE),
+ "".to_string(),
+ None,
+ ));
+ } else {
+ args.push(FunctionParameter::new(
+ Conf::new(Type::pointer(self.arch.as_ref(), &ty), MAX_CONFIDENCE),
+ "".to_string(),
+ None,
+ ));
+ }
+ }
+ e => {
+ return Err(anyhow!("Unexpected argument type {:?}", e));
+ }
+ }
+ }
+ Ok(Some(Box::new(ParsedType::ArgumentList(args))))
+ }
+
+ fn handle_method_list_type(
+ &mut self,
+ data: &MethodList,
+ finder: &mut ItemFinder<TypeIndex>,
+ ) -> Result<Option<Box<ParsedType>>> {
+ self.log(|| format!("Got MethodList type: {:x?}", data));
+
+ let mut list = vec![];
+ for method in &data.methods {
+ match self.handle_type_index(method.method_type, finder)? {
+ Some(ParsedType::MemberFunction(func)) => {
+ list.push(ParsedMethodListEntry {
+ attributes: method.attributes,
+ method_type: func.clone(),
+ vtable_offset: method.vtable_offset.map(|o| o as usize),
+ });
+ }
+ e => return Err(anyhow!("Unexpected method list entry: {:?}", e)),
+ }
+ }
+
+ Ok(Some(Box::new(ParsedType::MethodList(list))))
+ }
+
+ /// Given a type index, get a bare binja type (or fail if not found)
+ /// Optionally, set fully_resolve to true to parse and get the real type back in the case of NTRs
+ fn type_index_to_bare(
+ &mut self,
+ index: TypeIndex,
+ finder: &mut ItemFinder<TypeIndex>,
+ fully_resolve: bool,
+ ) -> Result<Ref<Type>> {
+ match self.try_type_index_to_bare(index, finder, fully_resolve)? {
+ Some(ty) => Ok(ty),
+ None => Err(anyhow!("Unresolved expected type {:?}", index)),
+ }
+ }
+
+ /// Given a type index, try to get a bare binja type
+ /// Optionally, set fully_resolve to true to parse and get the real type back in the case of NTRs
+ fn try_type_index_to_bare(
+ &mut self,
+ index: TypeIndex,
+ finder: &mut ItemFinder<TypeIndex>,
+ fully_resolve: bool,
+ ) -> Result<Option<Ref<Type>>> {
+ let (mut type_, inner) = match self.handle_type_index(index, finder)? {
+ Some(ParsedType::Bare(ty)) => (ty.clone(), None),
+ Some(ParsedType::Named(name, ty)) => (
+ Type::named_type_from_type(name.to_owned(), ty),
+ Some(ty.clone()),
+ ),
+ Some(ParsedType::Procedure(ParsedProcedureType { method_type, .. })) => {
+ (method_type.clone(), Some(method_type.clone()))
+ }
+ Some(ParsedType::MemberFunction(ParsedMemberFunction { method_type, .. })) => {
+ (method_type.clone(), Some(method_type.clone()))
+ }
+ Some(ParsedType::Member(ParsedMember { ty, .. })) => {
+ (ty.contents.clone(), Some(ty.contents.clone()))
+ }
+ _ => return Ok(None),
+ };
+
+ if type_.type_class() == TypeClass::NamedTypeReferenceClass {
+ if type_.width() == 0 {
+ // Replace empty NTR with fully parsed NTR, if we can
+ let name = type_
+ .get_named_type_reference()
+ .ok_or(anyhow!("expected ntr"))?
+ .name();
+ if let Some(full_ntr) = self.named_types.get(&name) {
+ type_ = Type::named_type_from_type(name, full_ntr.as_ref());
+ }
+ }
+ }
+
+ if !fully_resolve {
+ return Ok(Some(type_));
+ }
+
+ if type_.type_class() == TypeClass::NamedTypeReferenceClass {
+ if type_.width() == 0 {
+ // Look up raw name of this type
+ if let Ok(raw) = finder.find(index) {
+ if let Ok(parsed) = raw.parse() {
+ // Have to use raw name here because self.full_type_indices uses raw name
+ // for some reason
+ if let Some(raw_name) = Self::type_data_to_raw_name(&parsed) {
+ if let Some(&full_index) = self.full_type_indices.get(&raw_name) {
+ if let None = self.type_stack.iter().find(|&&idx| idx == full_index)
+ {
+ if full_index != index {
+ return self.try_type_index_to_bare(
+ full_index,
+ finder,
+ fully_resolve,
+ );
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if type_.type_class() == TypeClass::NamedTypeReferenceClass {
+ // PDB does this thing where anonymous inner types are represented as
+ // some_type::<anonymous-tag>
+ let name = type_
+ .get_named_type_reference()
+ .ok_or(anyhow!("expected ntr"))?
+ .name();
+ if Self::is_name_anonymous(&name) {
+ if let Some(inner) = inner.as_ref() {
+ type_ = inner.clone();
+ } else {
+ // Look up raw name of this type
+ if let Ok(raw) = finder.find(index) {
+ if let Ok(parsed) = raw.parse() {
+ // Have to use raw name here because self.full_type_indices uses raw name
+ // for some reason
+ if let Some(raw_name) = Self::type_data_to_raw_name(&parsed) {
+ if let Some(&full_index) = self.full_type_indices.get(&raw_name) {
+ if let None =
+ self.type_stack.iter().find(|&&idx| idx == full_index)
+ {
+ if full_index != index {
+ return self.try_type_index_to_bare(
+ full_index,
+ finder,
+ fully_resolve,
+ );
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ Ok(Some(type_))
+ }
+
+ /// Is this name one of the stupid microsoft unnamed type names
+ fn is_name_anonymous(name: &QualifiedName) -> bool {
+ match name.items.last() {
+ Some(item) if item == "<anonymous-tag>" => true,
+ Some(item) if item.contains("<unnamed-") => true,
+ _ => false,
+ }
+ }
+
+ /// Find a calling convention in the platform
+ pub(crate) fn find_calling_convention(
+ platform: &Platform,
+ name: &str,
+ ) -> Option<Ref<CoreCallingConvention>> {
+ platform
+ .calling_conventions()
+ .iter()
+ .find(|c| c.name().as_str() == name)
+ .map(|g| g.clone())
+ }
+
+ /// Convert pdb calling convention enum to binja
+ fn cv_call_t_to_calling_convention(&self, cv: u8) -> Option<Ref<CoreCallingConvention>> {
+ match CV_call_t::try_from(cv) {
+ Ok(CV_call_t::NEAR_FAST) | Ok(CV_call_t::FAR_FAST) => {
+ self.platform.get_fastcall_calling_convention()
+ }
+ Ok(CV_call_t::NEAR_STD) | Ok(CV_call_t::FAR_STD) => {
+ self.platform.get_stdcall_calling_convention()
+ }
+ Ok(CV_call_t::NEAR_C) | Ok(CV_call_t::FAR_C) => {
+ self.platform.get_cdecl_calling_convention()
+ }
+ Ok(CV_call_t::THISCALL) => {
+ Self::find_calling_convention(self.platform.as_ref(), "thiscall")
+ }
+ Ok(CV_call_t::NEAR_PASCAL) | Ok(CV_call_t::FAR_PASCAL) => {
+ Self::find_calling_convention(self.platform.as_ref(), "pascal")
+ }
+ Ok(CV_call_t::NEAR_SYS) | Ok(CV_call_t::FAR_SYS) => {
+ Self::find_calling_convention(self.platform.as_ref(), "sys")
+ }
+ Ok(CV_call_t::MIPSCALL) => {
+ Self::find_calling_convention(self.platform.as_ref(), "mipscall")
+ }
+ Ok(CV_call_t::ALPHACALL) => {
+ Self::find_calling_convention(self.platform.as_ref(), "alphacall")
+ }
+ Ok(CV_call_t::PPCCALL) => {
+ Self::find_calling_convention(self.platform.as_ref(), "ppccall")
+ }
+ Ok(CV_call_t::SHCALL) => {
+ Self::find_calling_convention(self.platform.as_ref(), "shcall")
+ }
+ Ok(CV_call_t::ARMCALL) => {
+ Self::find_calling_convention(self.platform.as_ref(), "armcall")
+ }
+ Ok(CV_call_t::AM33CALL) => {
+ Self::find_calling_convention(self.platform.as_ref(), "am33call")
+ }
+ Ok(CV_call_t::TRICALL) => {
+ Self::find_calling_convention(self.platform.as_ref(), "tricall")
+ }
+ Ok(CV_call_t::SH5CALL) => {
+ Self::find_calling_convention(self.platform.as_ref(), "sh5call")
+ }
+ Ok(CV_call_t::M32RCALL) => {
+ Self::find_calling_convention(self.platform.as_ref(), "m32rcall")
+ }
+ Ok(CV_call_t::NEAR_VECTOR) => {
+ Self::find_calling_convention(self.platform.as_ref(), "vectorcall")
+ }
+ _ => None,
+ }
+ }
+
+ /// Insert an argument for the thisptr in a function param list
+ fn insert_this_pointer(
+ &self,
+ parameters: &mut Vec<FunctionParameter>,
+ this_type: Ref<Type>,
+ ) -> Result<()> {
+ parameters.insert(
+ 0,
+ FunctionParameter::new(
+ Conf::new(this_type, MAX_CONFIDENCE),
+ "this".to_string(),
+ None,
+ ),
+ );
+
+ Ok(())
+ }
+
+ /// Does this type get returned in rax? Or should we put it on the stack?
+ pub fn can_fit_in_register(
+ &mut self,
+ index: TypeIndex,
+ finder: &mut ItemFinder<TypeIndex>,
+ treat_references_like_pointers: bool,
+ ) -> bool {
+ // TLDR "This is impossible so we're making a best-guess"
+ // GET READY OKAY
+
+ // "A scalar return value that can fit into 64 bits, including the __m64 type, is returned
+ // through RAX. Non-scalar types including floats, doubles, and vector types such as __m128,
+ // __m128i, __m128d are returned in XMM0. The state of unused bits in the value returned
+ // in RAX or XMM0 is undefined.
+
+ // "User-defined types can be returned by value from global functions and static member
+ // functions. To return a user-defined type by value in RAX, it must have a length of
+ // 1, 2, 4, 8, 16, 32, or 64 bits. It must also have no user-defined constructor, destructor,
+ // or copy assignment operator. It can have no private or protected non-static data members,
+ // and no non-static data members of reference type. It can't have base classes or virtual
+ // functions. And, it can only have data members that also meet these requirements.
+ // (This definition is essentially the same as a C++03 POD type. Because the definition has
+ // changed in the C++11 standard, we don't recommend using std::is_pod for this test.)
+ // Otherwise, the caller must allocate memory for the return value and pass a pointer to it
+ // as the first argument. The remaining arguments are then shifted one argument to the right.
+ // The same pointer must be returned by the callee in RAX."
+
+ // - length of 1, 2, 4, 8, 16, 32, or 64 bits
+ // - no user-defined constructor
+ // - no user-defined destructor
+ // - no user-defined copy assignment operator
+ // - no private data members
+ // - no protected data members
+ // - no reference data members
+ // - no base classes
+ // - no virtual functions
+
+ // This one is incorrect, so we're not including it:
+ // - all members meet these requirements
+ // https://godbolt.org/z/hsTxrxq9c extremely cool
+
+ // Are we going to implement all of this?
+ // No? We're just going to do something close and leave it to the users to figure out the rest
+ // There's no way I'm digging through all nonsense
+
+ // Glenn: "Never mind this got deleted... MS has done it again"
+ // After a quick GitHub discussion (https://github.com/MicrosoftDocs/cpp-docs/issues/4152)
+ // I've determined this is unknowable.
+ // Microsoft does it again!!!!
+
+ if let Some(&returnable) = self.type_default_returnable.get(&index) {
+ returnable
+ } else {
+ let returnable =
+ self.can_fit_in_register_impl(index, finder, treat_references_like_pointers);
+ self.log(|| format!("Type {} is default returnable: {}", index, returnable));
+ self.type_default_returnable.insert(index, returnable);
+ returnable
+ }
+ }
+
+ fn size_can_fit_in_register(size: u64) -> bool {
+ matches!(size, 0 | 1 | 2 | 4 | 8)
+ }
+
+ // Memoized... because this has gotta be real slow
+ fn can_fit_in_register_impl(
+ &mut self,
+ index: TypeIndex,
+ finder: &mut ItemFinder<TypeIndex>,
+ treat_references_like_pointers: bool,
+ ) -> bool {
+ let ty = match finder.find(index) {
+ Ok(item) => match item.parse() {
+ Ok(ty) => ty,
+ Err(_) => return false,
+ },
+ Err(_) => return false,
+ };
+
+ fn get_fields<'a>(
+ index: TypeIndex,
+ finder: &mut ItemFinder<'a, TypeIndex>,
+ ) -> Result<Vec<TypeData<'a>>> {
+ match finder.find(index).and_then(|fields| fields.parse()) {
+ Ok(TypeData::FieldList(fields)) => {
+ if let Some(cont) = fields.continuation {
+ Ok(fields
+ .fields
+ .into_iter()
+ .chain(get_fields(cont, finder)?)
+ .collect::<Vec<_>>())
+ } else {
+ Ok(fields.fields)
+ }
+ }
+ _ => Err(anyhow!("can't lookup fields")),
+ }
+ }
+
+ match ty {
+ TypeData::Primitive(_) => true,
+ TypeData::Pointer(p) => match p.attributes.pointer_mode() {
+ PointerMode::Pointer => true,
+ PointerMode::Member => true,
+ PointerMode::MemberFunction => true,
+ // - no reference data members
+ PointerMode::LValueReference => treat_references_like_pointers,
+ PointerMode::RValueReference => treat_references_like_pointers,
+ },
+ TypeData::Array(a) => {
+ Self::size_can_fit_in_register(*a.dimensions.last().unwrap_or(&0) as u64)
+ && self.can_fit_in_register(a.element_type, finder, false)
+ }
+ TypeData::Modifier(m) => {
+ self.can_fit_in_register(m.underlying_type, finder, treat_references_like_pointers)
+ }
+ TypeData::Enumeration(e) => self.can_fit_in_register(e.underlying_type, finder, false),
+ TypeData::Class(c) => {
+ if c.properties.forward_reference() {
+ if let Some(raw_name) = c.unique_name {
+ if let Some(&full) = self
+ .full_type_indices
+ .get(&raw_name.to_string().to_string())
+ {
+ return self.can_fit_in_register(
+ full,
+ finder,
+ treat_references_like_pointers,
+ );
+ }
+ }
+ // Can't look up, assume not
+ return false;
+ }
+
+ // - length of 1, 2, 4, 8, 16, 32, or 64 bits
+ if !Self::size_can_fit_in_register(c.size) {
+ return false;
+ }
+
+ // - no user-defined constructor
+ // - no user-defined destructor
+ // - no user-defined copy assignment operator
+ if c.properties.constructors() || c.properties.overloaded_assignment() {
+ return false;
+ }
+
+ // - no base classes
+ if c.derived_from.is_some() {
+ return false;
+ }
+ // - no virtual functions
+ if c.vtable_shape.is_some() {
+ return false;
+ }
+
+ let fields = if let Some(fields_idx) = c.fields {
+ if let Ok(fields) = get_fields(fields_idx, finder) {
+ fields
+ } else {
+ return false;
+ }
+ } else {
+ // No fields?
+ return true;
+ };
+
+ for field in fields {
+ match field {
+ TypeData::Member(m) => {
+ // - no private data members
+ // - no protected data members
+ if m.attributes.access() == 1 || m.attributes.access() == 2 {
+ return false;
+ }
+ }
+ TypeData::OverloadedMethod(m) => {
+ match finder.find(m.method_list).and_then(|l| l.parse()) {
+ Ok(TypeData::MethodList(list)) => {
+ for m in list.methods {
+ // - no virtual functions
+ if m.attributes.is_virtual() {
+ return false;
+ }
+ }
+ }
+ _ => return false,
+ }
+ }
+ TypeData::Method(m) => {
+ // - no virtual functions
+ if m.attributes.is_virtual() {
+ return false;
+ }
+ }
+ // - no base classes
+ TypeData::BaseClass(_) => return false,
+ TypeData::VirtualBaseClass(_) => return false,
+ TypeData::VirtualFunctionTable(_) => return false,
+ TypeData::VirtualTableShape(_) => return false,
+ TypeData::VirtualFunctionTablePointer(_) => return false,
+ _ => {}
+ }
+ }
+ true
+ }
+ TypeData::Union(u) => {
+ if u.properties.forward_reference() {
+ if let Some(raw_name) = u.unique_name {
+ if let Some(&full) = self
+ .full_type_indices
+ .get(&raw_name.to_string().to_string())
+ {
+ return self.can_fit_in_register(
+ full,
+ finder,
+ treat_references_like_pointers,
+ );
+ }
+ }
+ // Can't look up, assume not
+ return false;
+ }
+
+ // - length of 1, 2, 4, 8, 16, 32, or 64 bits
+ if !Self::size_can_fit_in_register(u.size) {
+ return false;
+ }
+
+ // - no user-defined constructor
+ // - no user-defined destructor
+ // - no user-defined copy assignment operator
+ if u.properties.constructors() || u.properties.overloaded_assignment() {
+ return false;
+ }
+
+ let fields = if let Ok(fields) = get_fields(u.fields, finder) {
+ fields
+ } else {
+ return false;
+ };
+
+ for field in fields {
+ match field {
+ TypeData::Member(m) => {
+ // - no private data members
+ // - no protected data members
+ if m.attributes.access() == 1 || m.attributes.access() == 2 {
+ return false;
+ }
+ }
+ TypeData::OverloadedMethod(m) => {
+ match finder.find(m.method_list).and_then(|l| l.parse()) {
+ Ok(TypeData::MethodList(list)) => {
+ for m in list.methods {
+ // - no virtual functions
+ if m.attributes.is_virtual() {
+ return false;
+ }
+ }
+ }
+ _ => return false,
+ }
+ }
+ TypeData::Method(m) => {
+ // - no virtual functions
+ if m.attributes.is_virtual() {
+ return false;
+ }
+ }
+ // - no base classes
+ TypeData::BaseClass(_) => return false,
+ TypeData::VirtualBaseClass(_) => return false,
+ TypeData::VirtualFunctionTable(_) => return false,
+ TypeData::VirtualTableShape(_) => return false,
+ TypeData::VirtualFunctionTablePointer(_) => return false,
+ _ => {}
+ }
+ }
+ true
+ }
+ _ => false,
+ }
+ }
+}