feat: PDB import via RawPDB, no msdia140.dll dependency

Replace DIA SDK COM-based PDB importer with RawPDB (MolecularMatters)
which reads PDB files directly via memory-mapped I/O. Adds File menu
"Import PDB..." dialog with type filtering, selection, and progress.

- Vendor raw_pdb into third_party/
- Two-phase API: enumeratePdbTypes() + importPdbSelected()
- Full recursive import of structs/unions/arrays/pointers/bitfields
- PDB import dialog with name filter, select-all, type count
- Benchmark: 1654 types from ntkrnlmp.pdb in 16ms
- Reorganize import/export files into src/imports/
This commit is contained in:
IChooseYou
2026-02-21 17:18:24 -07:00
parent 3a76b03c85
commit 1d7d384b93
100 changed files with 11627 additions and 17 deletions

View File

@@ -31,6 +31,15 @@ endif()
find_package(QScintilla REQUIRED)
# RawPDB — direct PDB file reader (no DIA SDK / msdia140.dll dependency)
file(GLOB RAW_PDB_SRCS third_party/raw_pdb/src/*.cpp)
add_library(raw_pdb STATIC ${RAW_PDB_SRCS})
target_include_directories(raw_pdb PUBLIC third_party/raw_pdb/src)
target_compile_features(raw_pdb PRIVATE cxx_std_11)
if(WIN32)
target_link_libraries(raw_pdb PRIVATE rpcrt4)
endif()
add_executable(Reclass
src/main.cpp
src/editor.h
@@ -60,12 +69,16 @@ add_executable(Reclass
src/themes/thememanager.cpp
src/themes/themeeditor.h
src/themes/themeeditor.cpp
src/import_reclass_xml.h
src/import_reclass_xml.cpp
src/import_source.h
src/import_source.cpp
src/export_reclass_xml.h
src/export_reclass_xml.cpp
src/imports/import_reclass_xml.h
src/imports/import_reclass_xml.cpp
src/imports/import_source.h
src/imports/import_source.cpp
src/imports/export_reclass_xml.h
src/imports/export_reclass_xml.cpp
src/imports/import_pdb.h
src/imports/import_pdb.cpp
src/imports/import_pdb_dialog.h
src/imports/import_pdb_dialog.cpp
src/mainwindow.h
src/optionsdialog.h
src/optionsdialog.cpp
@@ -94,7 +107,7 @@ target_link_libraries(Reclass PRIVATE
${_QT_WINEXTRAS}
)
if(WIN32)
target_link_libraries(Reclass PRIVATE dbghelp dwmapi psapi)
target_link_libraries(Reclass PRIVATE dbghelp dwmapi psapi raw_pdb)
endif()
add_executable(ReclassMcpBridge tools/rcx-mcp-stdio.cpp)
@@ -188,19 +201,19 @@ if(BUILD_TESTING)
add_test(NAME test_generator COMMAND test_generator)
add_executable(test_import_xml tests/test_import_xml.cpp
src/import_reclass_xml.cpp src/format.cpp src/compose.cpp src/addressparser.cpp)
src/imports/import_reclass_xml.cpp src/format.cpp src/compose.cpp src/addressparser.cpp)
target_include_directories(test_import_xml PRIVATE src)
target_link_libraries(test_import_xml PRIVATE ${QT}::Core ${QT}::Test)
add_test(NAME test_import_xml COMMAND test_import_xml)
add_executable(test_import_source tests/test_import_source.cpp
src/import_source.cpp src/format.cpp src/compose.cpp src/addressparser.cpp)
src/imports/import_source.cpp src/format.cpp src/compose.cpp src/addressparser.cpp)
target_include_directories(test_import_source PRIVATE src)
target_link_libraries(test_import_source PRIVATE ${QT}::Core ${QT}::Test)
add_test(NAME test_import_source COMMAND test_import_source)
add_executable(test_export_xml tests/test_export_xml.cpp
src/export_reclass_xml.cpp src/import_reclass_xml.cpp src/format.cpp src/compose.cpp src/addressparser.cpp)
src/imports/export_reclass_xml.cpp src/imports/import_reclass_xml.cpp src/format.cpp src/compose.cpp src/addressparser.cpp)
target_include_directories(test_export_xml PRIVATE src)
target_link_libraries(test_export_xml PRIVATE ${QT}::Core ${QT}::Test)
add_test(NAME test_export_xml COMMAND test_export_xml)
@@ -217,6 +230,22 @@ if(BUILD_TESTING)
target_link_libraries(test_addressparser PRIVATE ${QT}::Core ${QT}::Test)
add_test(NAME test_addressparser COMMAND test_addressparser)
if(WIN32)
add_executable(test_import_pdb tests/test_import_pdb.cpp
src/imports/import_pdb.cpp src/format.cpp src/compose.cpp src/addressparser.cpp)
target_include_directories(test_import_pdb PRIVATE src)
target_link_libraries(test_import_pdb PRIVATE
${QT}::Core ${QT}::Test raw_pdb)
add_test(NAME test_import_pdb COMMAND test_import_pdb)
add_executable(bench_import_pdb tests/bench_import_pdb.cpp
src/imports/import_pdb.cpp src/format.cpp src/compose.cpp src/addressparser.cpp)
target_include_directories(bench_import_pdb PRIVATE src)
target_link_libraries(bench_import_pdb PRIVATE
${QT}::Core ${QT}::Test raw_pdb)
add_test(NAME bench_import_pdb COMMAND bench_import_pdb)
endif()
# ── UI tests (require Qt::Widgets / QScintilla / display — skip on headless CI) ──
option(BUILD_UI_TESTS "Build tests that require a display (Qt Widgets)" ON)
if(BUILD_UI_TESTS)

971
src/imports/import_pdb.cpp Normal file
View File

@@ -0,0 +1,971 @@
#include "import_pdb.h"
#ifdef _WIN32
#include <windows.h>
#include <QFile>
#include <QHash>
#include <QPair>
#include <QSet>
// ── RawPDB headers ──
#include "PDB.h"
#include "PDB_RawFile.h"
#include "PDB_TPIStream.h"
#include "PDB_TPITypes.h"
#include "PDB_DBIStream.h"
#include "PDB_InfoStream.h"
#include "PDB_CoalescedMSFStream.h"
#include "Foundation/PDB_Memory.h"
namespace rcx {
// ── Memory-mapped file (mirrors ExampleMemoryMappedFile) ──
struct MappedFile {
HANDLE hFile = INVALID_HANDLE_VALUE;
HANDLE hMapping = nullptr;
const void* base = nullptr;
size_t size = 0;
bool open(const QString& path) {
hFile = CreateFileW(reinterpret_cast<const wchar_t*>(path.utf16()),
GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, nullptr);
if (hFile == INVALID_HANDLE_VALUE) return false;
hMapping = CreateFileMappingW(hFile, nullptr, PAGE_READONLY, 0, 0, nullptr);
if (!hMapping) { close(); return false; }
base = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
if (!base) { close(); return false; }
BY_HANDLE_FILE_INFORMATION info;
if (!GetFileInformationByHandle(hFile, &info)) { close(); return false; }
size = (static_cast<size_t>(info.nFileSizeHigh) << 32) | info.nFileSizeLow;
return true;
}
void close() {
if (base) { UnmapViewOfFile(base); base = nullptr; }
if (hMapping) { CloseHandle(hMapping); hMapping = nullptr; }
if (hFile != INVALID_HANDLE_VALUE) { CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; }
size = 0;
}
~MappedFile() { close(); }
MappedFile() = default;
MappedFile(const MappedFile&) = delete;
MappedFile& operator=(const MappedFile&) = delete;
};
// ── TypeTable (mirrors ExampleTypeTable) ──
// Builds an O(1) lookup table from type index → record pointer.
class TypeTable {
public:
explicit TypeTable(const PDB::TPIStream& tpiStream) {
m_firstIndex = tpiStream.GetFirstTypeIndex();
m_lastIndex = tpiStream.GetLastTypeIndex();
m_count = tpiStream.GetTypeRecordCount();
const PDB::DirectMSFStream& ds = tpiStream.GetDirectMSFStream();
m_stream = PDB::CoalescedMSFStream(ds, ds.GetSize(), 0u);
m_records = PDB_NEW_ARRAY(const PDB::CodeView::TPI::Record*, m_count);
uint32_t idx = 0;
tpiStream.ForEachTypeRecordHeaderAndOffset(
[this, &idx](const PDB::CodeView::TPI::RecordHeader&, size_t offset) {
m_records[idx++] = m_stream.GetDataAtOffset<const PDB::CodeView::TPI::Record>(offset);
});
}
~TypeTable() { PDB_DELETE_ARRAY(m_records); }
uint32_t firstIndex() const { return m_firstIndex; }
uint32_t lastIndex() const { return m_lastIndex; }
size_t count() const { return m_count; }
const PDB::CodeView::TPI::Record* get(uint32_t typeIndex) const {
if (typeIndex < m_firstIndex || typeIndex >= m_lastIndex) return nullptr;
return m_records[typeIndex - m_firstIndex];
}
private:
uint32_t m_firstIndex = 0;
uint32_t m_lastIndex = 0;
size_t m_count = 0;
const PDB::CodeView::TPI::Record** m_records = nullptr;
PDB::CoalescedMSFStream m_stream;
TypeTable(const TypeTable&) = delete;
TypeTable& operator=(const TypeTable&) = delete;
};
// ── Leaf numeric helpers (variable-length integer encoding) ──
using TRK = PDB::CodeView::TPI::TypeRecordKind;
static uint8_t leafSize(TRK kind) {
if (kind < TRK::LF_NUMERIC) return sizeof(TRK); // value is the kind itself
switch (kind) {
case TRK::LF_CHAR: return sizeof(TRK) + sizeof(uint8_t);
case TRK::LF_SHORT:
case TRK::LF_USHORT: return sizeof(TRK) + sizeof(uint16_t);
case TRK::LF_LONG:
case TRK::LF_ULONG: return sizeof(TRK) + sizeof(uint32_t);
case TRK::LF_QUADWORD:
case TRK::LF_UQUADWORD: return sizeof(TRK) + sizeof(uint64_t);
default: return sizeof(TRK);
}
}
static const char* leafName(const char* data, TRK kind) {
return data + leafSize(kind);
}
static uint64_t leafValue(const char* data, TRK kind) {
if (kind < TRK::LF_NUMERIC) {
return static_cast<uint16_t>(kind);
}
const char* p = data + sizeof(TRK);
switch (kind) {
case TRK::LF_CHAR: return *reinterpret_cast<const uint8_t*>(p);
case TRK::LF_SHORT: return *reinterpret_cast<const int16_t*>(p);
case TRK::LF_USHORT: return *reinterpret_cast<const uint16_t*>(p);
case TRK::LF_LONG: return *reinterpret_cast<const int32_t*>(p);
case TRK::LF_ULONG: return *reinterpret_cast<const uint32_t*>(p);
case TRK::LF_QUADWORD: return *reinterpret_cast<const int64_t*>(p);
case TRK::LF_UQUADWORD: return *reinterpret_cast<const uint64_t*>(p);
default: return 0;
}
}
// ── Primitive type index mapping (< 0x1000) ──
static NodeKind mapPrimitiveType(uint32_t typeIndex) {
uint32_t base = typeIndex & 0xFF;
switch (base) {
// void
case 0x03: return NodeKind::Hex8;
// signed char
case 0x10: return NodeKind::Int8;
// unsigned char
case 0x20: return NodeKind::UInt8;
// real char
case 0x70: return NodeKind::Int8;
// wchar
case 0x71: return NodeKind::UInt16;
// char8
case 0x7c: return NodeKind::UInt8;
// char16
case 0x7a: return NodeKind::UInt16;
// char32
case 0x7b: return NodeKind::UInt32;
// short
case 0x11: return NodeKind::Int16;
// ushort
case 0x21: return NodeKind::UInt16;
// long
case 0x12: return NodeKind::Int32;
// ulong
case 0x22: return NodeKind::UInt32;
// int8
case 0x68: return NodeKind::Int8;
// uint8
case 0x69: return NodeKind::UInt8;
// int16
case 0x72: return NodeKind::Int16;
// uint16
case 0x73: return NodeKind::UInt16;
// int32
case 0x74: return NodeKind::Int32;
// uint32
case 0x75: return NodeKind::UInt32;
// quad (int64)
case 0x13: return NodeKind::Int64;
// uquad (uint64)
case 0x23: return NodeKind::UInt64;
// int64
case 0x76: return NodeKind::Int64;
// uint64
case 0x77: return NodeKind::UInt64;
// float
case 0x40: return NodeKind::Float;
// double
case 0x41: return NodeKind::Double;
// bool
case 0x30: return NodeKind::Bool;
case 0x31: return NodeKind::UInt16; // bool16
case 0x32: return NodeKind::UInt32; // bool32
case 0x33: return NodeKind::UInt64; // bool64
// HRESULT
case 0x08: return NodeKind::UInt32;
// bit
case 0x60: return NodeKind::UInt8;
// int128 / uint128 approximation
case 0x78: return NodeKind::Hex64; // int128 → Hex64 (best we can do)
case 0x79: return NodeKind::Hex64; // uint128
default: return NodeKind::Hex32;
}
}
static NodeKind hexForSize(uint64_t len) {
switch (len) {
case 1: return NodeKind::Hex8;
case 2: return NodeKind::Hex16;
case 4: return NodeKind::Hex32;
case 8: return NodeKind::Hex64;
default: return NodeKind::Hex32;
}
}
// ── Helper: read the leaf kind from the start of LF_UNION.data ──
// (LF_UNION lacks the lfEasy member that LF_CLASS has)
static TRK unionLeafKind(const char* data) {
return *reinterpret_cast<const TRK*>(data);
}
// ── Import context ──
struct PdbCtx {
NodeTree tree;
const TypeTable* tt = nullptr;
QHash<uint32_t, uint64_t> typeCache; // typeIndex → nodeId
uint64_t importUDT(uint32_t typeIndex);
void importFieldList(uint32_t fieldListIndex, uint64_t parentId);
void importMemberType(uint32_t typeIndex, int offset, const QString& name, uint64_t parentId);
// Resolve LF_MODIFIER chain to underlying type index
uint32_t unwrapModifier(uint32_t typeIndex) const {
if (typeIndex < tt->firstIndex()) return typeIndex;
const auto* rec = tt->get(typeIndex);
if (!rec) return typeIndex;
if (rec->header.kind == TRK::LF_MODIFIER)
return rec->data.LF_MODIFIER.type;
return typeIndex;
}
};
uint64_t PdbCtx::importUDT(uint32_t typeIndex) {
if (typeIndex < tt->firstIndex()) return 0;
auto it = typeCache.find(typeIndex);
if (it != typeCache.end()) return it.value();
const auto* rec = tt->get(typeIndex);
if (!rec) return 0;
const char* name = nullptr;
uint32_t fieldListIndex = 0;
uint16_t fieldCount = 0;
bool isUnion = false;
const char* sizeData = nullptr;
if (rec->header.kind == TRK::LF_STRUCTURE || rec->header.kind == TRK::LF_CLASS) {
// Skip forward references — find the definition
if (rec->data.LF_CLASS.property.fwdref) return 0;
fieldCount = rec->data.LF_CLASS.count;
fieldListIndex = rec->data.LF_CLASS.field;
sizeData = rec->data.LF_CLASS.data;
name = leafName(sizeData, rec->data.LF_CLASS.lfEasy.kind);
} else if (rec->header.kind == TRK::LF_UNION) {
if (rec->data.LF_UNION.property.fwdref) return 0;
isUnion = true;
fieldCount = rec->data.LF_UNION.count;
fieldListIndex = rec->data.LF_UNION.field;
sizeData = rec->data.LF_UNION.data;
name = leafName(sizeData, unionLeafKind(sizeData));
} else {
return 0;
}
(void)fieldCount;
QString qname = name ? QString::fromUtf8(name) : QStringLiteral("<anon>");
Node s;
s.kind = NodeKind::Struct;
s.name = qname;
s.structTypeName = qname;
s.classKeyword = isUnion ? QStringLiteral("union") : QStringLiteral("struct");
s.parentId = 0;
s.collapsed = true;
int idx = tree.addNode(s);
uint64_t nodeId = tree.nodes[idx].id;
typeCache[typeIndex] = nodeId;
importFieldList(fieldListIndex, nodeId);
return nodeId;
}
void PdbCtx::importFieldList(uint32_t fieldListIndex, uint64_t parentId) {
const auto* rec = tt->get(fieldListIndex);
if (!rec || rec->header.kind != TRK::LF_FIELDLIST) return;
auto maximumSize = rec->header.size - sizeof(uint16_t);
QSet<QPair<int,int>> bitfieldSlots;
for (size_t i = 0; i < maximumSize; ) {
auto* field = reinterpret_cast<const PDB::CodeView::TPI::FieldList*>(
reinterpret_cast<const uint8_t*>(&rec->data.LF_FIELD.list) + i);
if (field->kind == TRK::LF_MEMBER) {
// Extract offset from variable-length leaf
uint16_t offset = 0;
if (field->data.LF_MEMBER.lfEasy.kind < TRK::LF_NUMERIC)
offset = *reinterpret_cast<const uint16_t*>(field->data.LF_MEMBER.offset);
else
offset = static_cast<uint16_t>(leafValue(field->data.LF_MEMBER.offset,
field->data.LF_MEMBER.lfEasy.kind));
const char* memberName = leafName(field->data.LF_MEMBER.offset,
field->data.LF_MEMBER.lfEasy.kind);
uint32_t memberType = field->data.LF_MEMBER.index;
QString qname = memberName ? QString::fromUtf8(memberName) : QString();
// Check for bitfield type
uint32_t resolvedType = unwrapModifier(memberType);
const auto* typeRec = tt->get(resolvedType);
if (typeRec && typeRec->header.kind == TRK::LF_BITFIELD) {
uint32_t underlying = typeRec->data.LF_BITFIELD.type;
uint8_t bitLen = typeRec->data.LF_BITFIELD.length;
(void)bitLen;
// Determine slot size from underlying type
uint64_t slotSize = 4;
if (underlying < tt->firstIndex()) {
NodeKind k = mapPrimitiveType(underlying);
slotSize = sizeForKind(k);
}
auto key = qMakePair((int)offset, (int)slotSize);
if (!bitfieldSlots.contains(key)) {
bitfieldSlots.insert(key);
Node n;
n.kind = hexForSize(slotSize);
n.name = qname;
n.parentId = parentId;
n.offset = offset;
tree.addNode(n);
}
} else {
importMemberType(memberType, offset, qname, parentId);
}
// Advance past this LF_MEMBER
i += static_cast<size_t>(memberName - reinterpret_cast<const char*>(field));
i += strnlen(memberName, maximumSize - i - 1) + 1;
i = (i + 3) & ~size_t(3); // align to 4
}
else if (field->kind == TRK::LF_BCLASS) {
const char* leafEnd = leafName(field->data.LF_BCLASS.offset,
field->data.LF_BCLASS.lfEasy.kind);
i += static_cast<size_t>(leafEnd - reinterpret_cast<const char*>(field));
i = (i + 3) & ~size_t(3);
}
else if (field->kind == TRK::LF_VBCLASS || field->kind == TRK::LF_IVBCLASS) {
TRK vbpKind = *reinterpret_cast<const TRK*>(field->data.LF_IVBCLASS.vbpOffset);
uint8_t vbpSize1 = leafSize(vbpKind);
TRK vbtKind = *reinterpret_cast<const TRK*>(field->data.LF_IVBCLASS.vbpOffset + vbpSize1);
uint8_t vbpSize2 = leafSize(vbtKind);
i += sizeof(PDB::CodeView::TPI::FieldList::Data::LF_VBCLASS) + vbpSize1 + vbpSize2;
i = (i + 3) & ~size_t(3);
}
else if (field->kind == TRK::LF_INDEX) {
// Continuation of field list in another record
importFieldList(field->data.LF_INDEX.type, parentId);
i += sizeof(PDB::CodeView::TPI::FieldList::Data::LF_INDEX);
i = (i + 3) & ~size_t(3);
}
else if (field->kind == TRK::LF_VFUNCTAB) {
i += sizeof(PDB::CodeView::TPI::FieldList::Data::LF_VFUNCTAB);
i = (i + 3) & ~size_t(3);
}
else if (field->kind == TRK::LF_NESTTYPE) {
const char* nestName = field->data.LF_NESTTYPE.name;
i += static_cast<size_t>(nestName - reinterpret_cast<const char*>(field));
i += strnlen(nestName, maximumSize - i - 1) + 1;
i = (i + 3) & ~size_t(3);
}
else if (field->kind == TRK::LF_STMEMBER) {
const char* smName = field->data.LF_STMEMBER.name;
i += static_cast<size_t>(smName - reinterpret_cast<const char*>(field));
i += strnlen(smName, maximumSize - i - 1) + 1;
i = (i + 3) & ~size_t(3);
}
else if (field->kind == TRK::LF_METHOD) {
const char* mName = field->data.LF_METHOD.name;
i += static_cast<size_t>(mName - reinterpret_cast<const char*>(field));
i += strnlen(mName, maximumSize - i - 1) + 1;
i = (i + 3) & ~size_t(3);
}
else if (field->kind == TRK::LF_ONEMETHOD) {
// Determine if it has a vbaseoff field
auto prop = static_cast<PDB::CodeView::TPI::MethodProperty>(
field->data.LF_ONEMETHOD.attributes.mprop);
const char* mName;
if (prop == PDB::CodeView::TPI::MethodProperty::Intro ||
prop == PDB::CodeView::TPI::MethodProperty::PureIntro)
mName = reinterpret_cast<const char*>(field->data.LF_ONEMETHOD.vbaseoff) + sizeof(uint32_t);
else
mName = reinterpret_cast<const char*>(field->data.LF_ONEMETHOD.vbaseoff);
i += static_cast<size_t>(mName - reinterpret_cast<const char*>(field));
i += strnlen(mName, maximumSize - i - 1) + 1;
i = (i + 3) & ~size_t(3);
}
else if (field->kind == TRK::LF_ENUMERATE) {
const char* eName = leafName(field->data.LF_ENUMERATE.value,
field->data.LF_ENUMERATE.lfEasy.kind);
i += static_cast<size_t>(eName - reinterpret_cast<const char*>(field));
i += strnlen(eName, maximumSize - i - 1) + 1;
i = (i + 3) & ~size_t(3);
}
else {
break; // unknown field kind, stop
}
}
}
void PdbCtx::importMemberType(uint32_t typeIndex, int offset, const QString& name, uint64_t parentId) {
// Handle primitive type indices (< 0x1000)
if (typeIndex < tt->firstIndex()) {
uint32_t ptrMode = (typeIndex >> 8) & 0xF;
if (ptrMode == 0x04 || ptrMode == 0x05) {
// 32-bit pointer to a base type
Node n;
n.kind = NodeKind::Pointer32;
n.name = name;
n.parentId = parentId;
n.offset = offset;
n.collapsed = true;
tree.addNode(n);
return;
}
if (ptrMode == 0x06) {
// 64-bit pointer to a base type
Node n;
n.kind = NodeKind::Pointer64;
n.name = name;
n.parentId = parentId;
n.offset = offset;
n.collapsed = true;
tree.addNode(n);
return;
}
if (ptrMode != 0x00) {
// Some other pointer mode (near, far, huge) — treat as 32-bit
Node n;
n.kind = NodeKind::Pointer32;
n.name = name;
n.parentId = parentId;
n.offset = offset;
n.collapsed = true;
tree.addNode(n);
return;
}
// Direct base type
Node n;
n.kind = mapPrimitiveType(typeIndex);
n.name = name;
n.parentId = parentId;
n.offset = offset;
tree.addNode(n);
return;
}
const auto* rec = tt->get(typeIndex);
if (!rec) {
Node n;
n.kind = NodeKind::Hex32;
n.name = name;
n.parentId = parentId;
n.offset = offset;
tree.addNode(n);
return;
}
switch (rec->header.kind) {
case TRK::LF_MODIFIER:
importMemberType(rec->data.LF_MODIFIER.type, offset, name, parentId);
break;
case TRK::LF_POINTER: {
uint32_t ptrSize = rec->data.LF_POINTER.attr.size;
uint32_t pointee = rec->data.LF_POINTER.utype;
// Unwrap modifier on pointee
uint32_t realPointee = unwrapModifier(pointee);
Node n;
n.kind = (ptrSize <= 4) ? NodeKind::Pointer32 : NodeKind::Pointer64;
n.name = name;
n.parentId = parentId;
n.offset = offset;
n.collapsed = true;
// Check if pointee is a UDT
if (realPointee >= tt->firstIndex()) {
const auto* pointeeRec = tt->get(realPointee);
if (pointeeRec) {
if (pointeeRec->header.kind == TRK::LF_STRUCTURE ||
pointeeRec->header.kind == TRK::LF_CLASS ||
pointeeRec->header.kind == TRK::LF_UNION) {
// If this is a forward ref, search for the definition
uint32_t defIndex = realPointee;
bool isFwd = false;
if (pointeeRec->header.kind == TRK::LF_UNION)
isFwd = pointeeRec->data.LF_UNION.property.fwdref;
else
isFwd = pointeeRec->data.LF_CLASS.property.fwdref;
if (isFwd) {
// Need to find the non-fwdref definition by name
const char* typeName = nullptr;
if (pointeeRec->header.kind == TRK::LF_UNION)
typeName = leafName(pointeeRec->data.LF_UNION.data, unionLeafKind(pointeeRec->data.LF_UNION.data));
else
typeName = leafName(pointeeRec->data.LF_CLASS.data,
pointeeRec->data.LF_CLASS.lfEasy.kind);
if (typeName) {
// Linear scan for the definition (cached after first import)
for (uint32_t ti = tt->firstIndex(); ti < tt->lastIndex(); ti++) {
const auto* candidate = tt->get(ti);
if (!candidate) continue;
if (candidate->header.kind != pointeeRec->header.kind) continue;
bool candidateFwd;
const char* candidateName;
if (candidate->header.kind == TRK::LF_UNION) {
candidateFwd = candidate->data.LF_UNION.property.fwdref;
candidateName = leafName(candidate->data.LF_UNION.data, unionLeafKind(candidate->data.LF_UNION.data));
} else {
candidateFwd = candidate->data.LF_CLASS.property.fwdref;
candidateName = leafName(candidate->data.LF_CLASS.data,
candidate->data.LF_CLASS.lfEasy.kind);
}
if (!candidateFwd && candidateName && strcmp(candidateName, typeName) == 0) {
defIndex = ti;
break;
}
}
}
}
n.refId = importUDT(defIndex);
} else if (pointeeRec->header.kind == TRK::LF_PROCEDURE ||
pointeeRec->header.kind == TRK::LF_MFUNCTION) {
n.kind = (ptrSize <= 4) ? NodeKind::FuncPtr32 : NodeKind::FuncPtr64;
}
}
}
tree.addNode(n);
break;
}
case TRK::LF_STRUCTURE:
case TRK::LF_CLASS:
case TRK::LF_UNION: {
// Embedded struct/union
uint32_t defIndex = typeIndex;
// Handle forward reference
bool isFwd = false;
if (rec->header.kind == TRK::LF_UNION)
isFwd = rec->data.LF_UNION.property.fwdref;
else
isFwd = rec->data.LF_CLASS.property.fwdref;
if (isFwd) {
const char* typeName = nullptr;
if (rec->header.kind == TRK::LF_UNION)
typeName = leafName(rec->data.LF_UNION.data, unionLeafKind(rec->data.LF_UNION.data));
else
typeName = leafName(rec->data.LF_CLASS.data, rec->data.LF_CLASS.lfEasy.kind);
if (typeName) {
for (uint32_t ti = tt->firstIndex(); ti < tt->lastIndex(); ti++) {
const auto* candidate = tt->get(ti);
if (!candidate) continue;
if (candidate->header.kind != rec->header.kind) continue;
bool candidateFwd;
const char* candidateName;
if (candidate->header.kind == TRK::LF_UNION) {
candidateFwd = candidate->data.LF_UNION.property.fwdref;
candidateName = leafName(candidate->data.LF_UNION.data, unionLeafKind(candidate->data.LF_UNION.data));
} else {
candidateFwd = candidate->data.LF_CLASS.property.fwdref;
candidateName = leafName(candidate->data.LF_CLASS.data,
candidate->data.LF_CLASS.lfEasy.kind);
}
if (!candidateFwd && candidateName && strcmp(candidateName, typeName) == 0) {
defIndex = ti;
break;
}
}
}
}
uint64_t refId = importUDT(defIndex);
const char* typeName = nullptr;
bool isUnion = (rec->header.kind == TRK::LF_UNION);
if (isUnion)
typeName = leafName(rec->data.LF_UNION.data, unionLeafKind(rec->data.LF_UNION.data));
else
typeName = leafName(rec->data.LF_CLASS.data, rec->data.LF_CLASS.lfEasy.kind);
Node n;
n.kind = NodeKind::Struct;
n.name = name;
n.structTypeName = typeName ? QString::fromUtf8(typeName) : QString();
n.classKeyword = isUnion ? QStringLiteral("union") : QStringLiteral("struct");
n.parentId = parentId;
n.offset = offset;
n.refId = refId;
n.collapsed = true;
tree.addNode(n);
break;
}
case TRK::LF_ARRAY: {
uint32_t elemType = rec->data.LF_ARRAY.elemtype;
uint64_t totalSize = leafValue(rec->data.LF_ARRAY.data,
*reinterpret_cast<const TRK*>(rec->data.LF_ARRAY.data));
// Get element size
uint64_t elemSize = 0;
uint32_t realElemType = unwrapModifier(elemType);
if (realElemType < tt->firstIndex()) {
NodeKind ek = mapPrimitiveType(realElemType);
elemSize = sizeForKind(ek);
} else {
const auto* elemRec = tt->get(realElemType);
if (elemRec) {
if (elemRec->header.kind == TRK::LF_STRUCTURE || elemRec->header.kind == TRK::LF_CLASS) {
const char* sizeData = elemRec->data.LF_CLASS.data;
elemSize = leafValue(sizeData, elemRec->data.LF_CLASS.lfEasy.kind);
} else if (elemRec->header.kind == TRK::LF_UNION) {
const char* sizeData = elemRec->data.LF_UNION.data;
elemSize = leafValue(sizeData, *reinterpret_cast<const TRK*>(sizeData));
} else if (elemRec->header.kind == TRK::LF_POINTER) {
elemSize = elemRec->data.LF_POINTER.attr.size;
} else if (elemRec->header.kind == TRK::LF_ENUM) {
// Size of enum's underlying type
uint32_t ut = elemRec->data.LF_ENUM.utype;
if (ut < tt->firstIndex()) {
NodeKind ek = mapPrimitiveType(ut);
elemSize = sizeForKind(ek);
} else {
elemSize = 4;
}
} else if (elemRec->header.kind == TRK::LF_ARRAY) {
// Nested array — get total size
elemSize = leafValue(elemRec->data.LF_ARRAY.data,
*reinterpret_cast<const TRK*>(elemRec->data.LF_ARRAY.data));
}
}
}
int count = (elemSize > 0) ? static_cast<int>(totalSize / elemSize) : 1;
Node n;
n.kind = NodeKind::Array;
n.name = name;
n.parentId = parentId;
n.offset = offset;
n.arrayLen = count;
// Determine element kind
if (realElemType < tt->firstIndex()) {
n.elementKind = mapPrimitiveType(realElemType);
} else {
const auto* elemRec = tt->get(realElemType);
if (elemRec) {
if (elemRec->header.kind == TRK::LF_STRUCTURE ||
elemRec->header.kind == TRK::LF_CLASS ||
elemRec->header.kind == TRK::LF_UNION) {
n.elementKind = NodeKind::Struct;
n.refId = importUDT(realElemType);
const char* tn = nullptr;
if (elemRec->header.kind == TRK::LF_UNION)
tn = leafName(elemRec->data.LF_UNION.data, unionLeafKind(elemRec->data.LF_UNION.data));
else
tn = leafName(elemRec->data.LF_CLASS.data, elemRec->data.LF_CLASS.lfEasy.kind);
if (tn) n.structTypeName = QString::fromUtf8(tn);
} else if (elemRec->header.kind == TRK::LF_POINTER) {
uint32_t sz = elemRec->data.LF_POINTER.attr.size;
n.elementKind = (sz <= 4) ? NodeKind::Pointer32 : NodeKind::Pointer64;
} else {
n.elementKind = hexForSize(elemSize);
}
}
}
tree.addNode(n);
break;
}
case TRK::LF_ENUM: {
// Map enum to its underlying integer type
uint32_t utype = rec->data.LF_ENUM.utype;
Node n;
if (utype < tt->firstIndex()) {
n.kind = mapPrimitiveType(utype);
} else {
n.kind = NodeKind::UInt32; // fallback
}
n.name = name;
n.parentId = parentId;
n.offset = offset;
tree.addNode(n);
break;
}
case TRK::LF_PROCEDURE:
case TRK::LF_MFUNCTION: {
Node n;
n.kind = NodeKind::Hex64;
n.name = name;
n.parentId = parentId;
n.offset = offset;
tree.addNode(n);
break;
}
case TRK::LF_BITFIELD: {
uint32_t underlying = rec->data.LF_BITFIELD.type;
uint64_t slotSize = 4;
if (underlying < tt->firstIndex()) {
NodeKind k = mapPrimitiveType(underlying);
slotSize = sizeForKind(k);
}
Node n;
n.kind = hexForSize(slotSize);
n.name = name;
n.parentId = parentId;
n.offset = offset;
tree.addNode(n);
break;
}
default: {
// Unknown complex type — emit as Hex32
Node n;
n.kind = NodeKind::Hex32;
n.name = name;
n.parentId = parentId;
n.offset = offset;
tree.addNode(n);
break;
}
}
}
// ── Helper: open PDB and build type table ──
struct PdbFile {
MappedFile mapped;
PDB::RawFile* rawFile = nullptr;
PDB::TPIStream* tpiStream = nullptr;
TypeTable* typeTable = nullptr;
~PdbFile() {
delete typeTable;
delete tpiStream;
delete rawFile;
}
bool open(const QString& pdbPath, QString* errorMsg) {
auto setErr = [&](const QString& msg) { if (errorMsg) *errorMsg = msg; };
if (!QFile::exists(pdbPath)) {
setErr(QStringLiteral("PDB file not found: ") + pdbPath);
return false;
}
if (!mapped.open(pdbPath)) {
setErr(QStringLiteral("Failed to memory-map PDB file: ") + pdbPath);
return false;
}
if (PDB::ValidateFile(mapped.base, mapped.size) != PDB::ErrorCode::Success) {
setErr(QStringLiteral("Invalid PDB file: ") + pdbPath);
return false;
}
rawFile = new PDB::RawFile(PDB::CreateRawFile(mapped.base));
if (PDB::HasValidTPIStream(*rawFile) != PDB::ErrorCode::Success) {
setErr(QStringLiteral("PDB has no valid TPI stream: ") + pdbPath);
return false;
}
tpiStream = new PDB::TPIStream(PDB::CreateTPIStream(*rawFile));
typeTable = new TypeTable(*tpiStream);
return true;
}
};
// ── Public API: enumeratePdbTypes ──
QVector<PdbTypeInfo> enumeratePdbTypes(const QString& pdbPath, QString* errorMsg) {
PdbFile pdb;
if (!pdb.open(pdbPath, errorMsg)) return {};
const TypeTable& tt = *pdb.typeTable;
QVector<PdbTypeInfo> result;
for (uint32_t ti = tt.firstIndex(); ti < tt.lastIndex(); ti++) {
const auto* rec = tt.get(ti);
if (!rec) continue;
bool isUDT = (rec->header.kind == TRK::LF_STRUCTURE ||
rec->header.kind == TRK::LF_CLASS ||
rec->header.kind == TRK::LF_UNION);
if (!isUDT) continue;
const char* name = nullptr;
uint16_t fieldCount = 0;
bool isUnion = false;
uint64_t size = 0;
if (rec->header.kind == TRK::LF_UNION) {
if (rec->data.LF_UNION.property.fwdref) continue;
isUnion = true;
fieldCount = rec->data.LF_UNION.count;
const char* sizeData = rec->data.LF_UNION.data;
TRK sizeKind = *reinterpret_cast<const TRK*>(sizeData);
size = leafValue(sizeData, sizeKind);
name = leafName(sizeData, sizeKind);
} else {
if (rec->data.LF_CLASS.property.fwdref) continue;
fieldCount = rec->data.LF_CLASS.count;
const char* sizeData = rec->data.LF_CLASS.data;
size = leafValue(sizeData, rec->data.LF_CLASS.lfEasy.kind);
name = leafName(sizeData, rec->data.LF_CLASS.lfEasy.kind);
}
if (!name || name[0] == '\0') continue;
// Skip anonymous types with compiler-generated names
if (name[0] == '<') continue;
PdbTypeInfo info;
info.typeIndex = ti;
info.name = QString::fromUtf8(name);
info.size = size;
info.childCount = fieldCount;
info.isUnion = isUnion;
result.append(info);
}
return result;
}
// ── Public API: importPdbSelected ──
NodeTree importPdbSelected(const QString& pdbPath,
const QVector<uint32_t>& typeIndices,
QString* errorMsg,
ProgressCb progressCb) {
PdbFile pdb;
if (!pdb.open(pdbPath, errorMsg)) return {};
PdbCtx ctx;
ctx.tt = pdb.typeTable;
int total = typeIndices.size();
for (int i = 0; i < total; i++) {
ctx.importUDT(typeIndices[i]);
if (progressCb && !progressCb(i + 1, total)) {
if (errorMsg) *errorMsg = QStringLiteral("Import cancelled");
return ctx.tree; // return partial result
}
}
if (ctx.tree.nodes.isEmpty()) {
if (errorMsg) *errorMsg = QStringLiteral("No types imported");
}
return ctx.tree;
}
// ── Public API: importPdb (legacy) ──
NodeTree importPdb(const QString& pdbPath, const QString& structFilter, QString* errorMsg) {
PdbFile pdb;
if (!pdb.open(pdbPath, errorMsg)) return {};
const TypeTable& tt = *pdb.typeTable;
PdbCtx ctx;
ctx.tt = &tt;
for (uint32_t ti = tt.firstIndex(); ti < tt.lastIndex(); ti++) {
const auto* rec = tt.get(ti);
if (!rec) continue;
bool isUDT = (rec->header.kind == TRK::LF_STRUCTURE ||
rec->header.kind == TRK::LF_CLASS ||
rec->header.kind == TRK::LF_UNION);
if (!isUDT) continue;
bool fwdref = false;
const char* name = nullptr;
if (rec->header.kind == TRK::LF_UNION) {
fwdref = rec->data.LF_UNION.property.fwdref;
name = leafName(rec->data.LF_UNION.data, unionLeafKind(rec->data.LF_UNION.data));
} else {
fwdref = rec->data.LF_CLASS.property.fwdref;
name = leafName(rec->data.LF_CLASS.data, rec->data.LF_CLASS.lfEasy.kind);
}
if (fwdref) continue;
if (!name) continue;
if (!structFilter.isEmpty()) {
if (QString::fromUtf8(name) != structFilter) continue;
}
ctx.importUDT(ti);
// If filtering to a single struct, stop after finding it
if (!structFilter.isEmpty()) break;
}
if (ctx.tree.nodes.isEmpty()) {
if (!structFilter.isEmpty()) {
if (errorMsg) *errorMsg = QStringLiteral("Type '") + structFilter +
QStringLiteral("' not found in PDB");
} else {
if (errorMsg) *errorMsg = QStringLiteral("No types found in PDB");
}
}
return ctx.tree;
}
} // namespace rcx
#else // !_WIN32
namespace rcx {
QVector<PdbTypeInfo> enumeratePdbTypes(const QString&, QString* errorMsg) {
if (errorMsg) *errorMsg = QStringLiteral("PDB import requires Windows");
return {};
}
NodeTree importPdbSelected(const QString&, const QVector<uint32_t>&,
QString* errorMsg, ProgressCb) {
if (errorMsg) *errorMsg = QStringLiteral("PDB import requires Windows");
return {};
}
NodeTree importPdb(const QString&, const QString&, QString* errorMsg) {
if (errorMsg) *errorMsg = QStringLiteral("PDB import requires Windows");
return {};
}
} // namespace rcx
#endif

34
src/imports/import_pdb.h Normal file
View File

@@ -0,0 +1,34 @@
#pragma once
#include "core.h"
#include <QVector>
#include <functional>
namespace rcx {
struct PdbTypeInfo {
uint32_t typeIndex; // TPI type index
QString name; // struct/class/union name
uint64_t size; // sizeof in bytes
int childCount; // direct member count
bool isUnion; // union vs struct/class
};
// Phase 1: Enumerate all UDT types in the PDB (fast scan, no recursive import).
QVector<PdbTypeInfo> enumeratePdbTypes(const QString& pdbPath,
QString* errorMsg = nullptr);
// Phase 2: Import selected types with full recursive child types.
// progressCb is called with (current, total) for each top-level type;
// return false from the callback to cancel the import.
using ProgressCb = std::function<bool(int current, int total)>;
NodeTree importPdbSelected(const QString& pdbPath,
const QVector<uint32_t>& typeIndices,
QString* errorMsg = nullptr,
ProgressCb progressCb = {});
// Legacy single-call API: import one struct by name (or all if filter empty).
NodeTree importPdb(const QString& pdbPath,
const QString& structFilter = {},
QString* errorMsg = nullptr);
} // namespace rcx

View File

@@ -0,0 +1,184 @@
#include "import_pdb_dialog.h"
#include "import_pdb.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QCheckBox>
#include <QListWidget>
#include <QLabel>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QFileDialog>
#include <QMessageBox>
#include <QApplication>
namespace rcx {
PdbImportDialog::PdbImportDialog(QWidget* parent)
: QDialog(parent)
{
setWindowTitle("Import from PDB");
resize(520, 480);
auto* layout = new QVBoxLayout(this);
// PDB path row
auto* pathRow = new QHBoxLayout;
pathRow->addWidget(new QLabel("PDB File:"));
m_pathEdit = new QLineEdit;
m_pathEdit->setPlaceholderText("Select a PDB file...");
pathRow->addWidget(m_pathEdit);
m_browseBtn = new QPushButton("...");
m_browseBtn->setFixedWidth(32);
pathRow->addWidget(m_browseBtn);
layout->addLayout(pathRow);
// Filter row
auto* filterRow = new QHBoxLayout;
filterRow->addWidget(new QLabel("Filter:"));
m_filterEdit = new QLineEdit;
m_filterEdit->setPlaceholderText("Type name filter...");
m_filterEdit->setEnabled(false);
filterRow->addWidget(m_filterEdit);
layout->addLayout(filterRow);
// Select all checkbox
m_selectAll = new QCheckBox("Select All");
m_selectAll->setEnabled(false);
layout->addWidget(m_selectAll);
// Type list
m_typeList = new QListWidget;
m_typeList->setEnabled(false);
layout->addWidget(m_typeList);
// Count label
m_countLabel = new QLabel("No PDB loaded");
layout->addWidget(m_countLabel);
// Buttons
m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
m_buttons->button(QDialogButtonBox::Ok)->setText("Import");
m_buttons->button(QDialogButtonBox::Ok)->setEnabled(false);
layout->addWidget(m_buttons);
connect(m_browseBtn, &QPushButton::clicked, this, &PdbImportDialog::browsePdb);
connect(m_pathEdit, &QLineEdit::returnPressed, this, &PdbImportDialog::loadPdb);
connect(m_filterEdit, &QLineEdit::textChanged, this, &PdbImportDialog::filterChanged);
connect(m_selectAll, &QCheckBox::toggled, this, &PdbImportDialog::selectAllToggled);
connect(m_typeList, &QListWidget::itemChanged, this, &PdbImportDialog::updateSelectionCount);
connect(m_buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
}
QString PdbImportDialog::pdbPath() const {
return m_pathEdit->text();
}
QVector<uint32_t> PdbImportDialog::selectedTypeIndices() const {
QVector<uint32_t> result;
for (int i = 0; i < m_typeList->count(); i++) {
auto* item = m_typeList->item(i);
if (item->checkState() == Qt::Checked) {
uint32_t typeIndex = item->data(Qt::UserRole).toUInt();
result.append(typeIndex);
}
}
return result;
}
void PdbImportDialog::browsePdb() {
QString path = QFileDialog::getOpenFileName(this,
"Select PDB File", {},
"PDB Files (*.pdb);;All Files (*)");
if (path.isEmpty()) return;
m_pathEdit->setText(path);
loadPdb();
}
void PdbImportDialog::loadPdb() {
QString path = m_pathEdit->text();
if (path.isEmpty()) return;
m_typeList->clear();
m_allTypes.clear();
m_countLabel->setText("Loading...");
m_typeList->setEnabled(false);
m_filterEdit->setEnabled(false);
m_selectAll->setEnabled(false);
m_buttons->button(QDialogButtonBox::Ok)->setEnabled(false);
QApplication::processEvents();
QString error;
QVector<PdbTypeInfo> types = enumeratePdbTypes(path, &error);
if (types.isEmpty()) {
m_countLabel->setText(error.isEmpty() ? "No types found" : error);
return;
}
m_allTypes.reserve(types.size());
for (const auto& t : types) {
TypeItem item;
item.typeIndex = t.typeIndex;
item.name = t.name;
item.childCount = t.childCount;
item.isUnion = t.isUnion;
m_allTypes.append(item);
}
// Sort by name
std::sort(m_allTypes.begin(), m_allTypes.end(),
[](const TypeItem& a, const TypeItem& b) { return a.name < b.name; });
m_filterEdit->setEnabled(true);
m_selectAll->setEnabled(true);
m_typeList->setEnabled(true);
populateList();
}
void PdbImportDialog::populateList() {
m_typeList->blockSignals(true);
m_typeList->clear();
QString filter = m_filterEdit->text();
bool selectAll = m_selectAll->isChecked();
for (const auto& t : m_allTypes) {
if (!filter.isEmpty() && !t.name.contains(filter, Qt::CaseInsensitive))
continue;
QString label = QStringLiteral("%1 (%2 fields)")
.arg(t.name).arg(t.childCount);
auto* item = new QListWidgetItem(label, m_typeList);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(selectAll ? Qt::Checked : Qt::Unchecked);
item->setData(Qt::UserRole, t.typeIndex);
}
m_typeList->blockSignals(false);
updateSelectionCount();
}
void PdbImportDialog::filterChanged(const QString&) {
populateList();
}
void PdbImportDialog::selectAllToggled(bool) {
populateList();
}
void PdbImportDialog::updateSelectionCount() {
int checked = 0;
int total = m_typeList->count();
for (int i = 0; i < total; i++) {
if (m_typeList->item(i)->checkState() == Qt::Checked)
checked++;
}
m_countLabel->setText(QStringLiteral("%1 of %2 types selected")
.arg(checked).arg(m_allTypes.size()));
m_buttons->button(QDialogButtonBox::Ok)->setEnabled(checked > 0);
}
} // namespace rcx

View File

@@ -0,0 +1,53 @@
#pragma once
#include <QDialog>
#include <QVector>
#include <cstdint>
class QLineEdit;
class QCheckBox;
class QListWidget;
class QLabel;
class QDialogButtonBox;
class QPushButton;
namespace rcx {
struct PdbTypeInfo;
class PdbImportDialog : public QDialog {
Q_OBJECT
public:
explicit PdbImportDialog(QWidget* parent = nullptr);
QString pdbPath() const;
QVector<uint32_t> selectedTypeIndices() const;
private slots:
void browsePdb();
void loadPdb();
void filterChanged(const QString& text);
void selectAllToggled(bool checked);
void updateSelectionCount();
private:
QLineEdit* m_pathEdit;
QPushButton* m_browseBtn;
QLineEdit* m_filterEdit;
QCheckBox* m_selectAll;
QListWidget* m_typeList;
QLabel* m_countLabel;
QDialogButtonBox* m_buttons;
struct TypeItem {
uint32_t typeIndex;
QString name;
int childCount;
bool isUnion;
};
QVector<TypeItem> m_allTypes;
void populateList();
};
} // namespace rcx

View File

@@ -1,9 +1,11 @@
#include "mainwindow.h"
#include "providerregistry.h"
#include "generator.h"
#include "import_reclass_xml.h"
#include "import_source.h"
#include "export_reclass_xml.h"
#include "imports/import_reclass_xml.h"
#include "imports/import_source.h"
#include "imports/export_reclass_xml.h"
#include "imports/import_pdb.h"
#include "imports/import_pdb_dialog.h"
#include "mcp/mcp_bridge.h"
#include <QApplication>
#include <QMainWindow>
@@ -41,6 +43,7 @@
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QDialog>
#include <QProgressDialog>
#include <Qsci/qsciscintilla.h>
#include <Qsci/qscilexercpp.h>
#include <QProxyStyle>
@@ -431,6 +434,7 @@ void MainWindow::createMenus() {
Qt5Qt6AddAction(file, "Export ReClass &XML...", QKeySequence::UnknownKey, QIcon(), this, &MainWindow::exportReclassXmlAction);
Qt5Qt6AddAction(file, "Import from &Source...", QKeySequence::UnknownKey, QIcon(), this, &MainWindow::importFromSource);
Qt5Qt6AddAction(file, "&Import ReClass XML...", QKeySequence::UnknownKey, QIcon(), this, &MainWindow::importReclassXml);
Qt5Qt6AddAction(file, "Import &PDB...", QKeySequence::UnknownKey, QIcon(), this, &MainWindow::importPdb);
// Examples submenu — scan once at init
{
QDir exDir(QCoreApplication::applicationDirPath() + "/examples");
@@ -1804,6 +1808,56 @@ void MainWindow::importFromSource() {
m_statusLabel->setText(QStringLiteral("Imported %1 classes from source").arg(classCount));
}
// ── Import PDB ──
void MainWindow::importPdb() {
rcx::PdbImportDialog dlg(this);
if (dlg.exec() != QDialog::Accepted) return;
QString pdbPath = dlg.pdbPath();
QVector<uint32_t> indices = dlg.selectedTypeIndices();
if (indices.isEmpty()) return;
QProgressDialog progress("Importing types...", "Cancel", 0, indices.size(), this);
progress.setWindowModality(Qt::WindowModal);
progress.setMinimumDuration(200);
bool cancelled = false;
QString error;
NodeTree tree = rcx::importPdbSelected(pdbPath, indices, &error,
[&](int current, int total) -> bool {
progress.setMaximum(total);
progress.setValue(current);
QApplication::processEvents();
if (progress.wasCanceled()) {
cancelled = true;
return false;
}
return true;
});
progress.close();
if (tree.nodes.isEmpty()) {
if (!cancelled)
QMessageBox::warning(this, "Import Failed", error.isEmpty()
? QStringLiteral("No types imported") : error);
return;
}
int classCount = 0;
for (const auto& n : tree.nodes)
if (n.parentId == 0 && n.kind == rcx::NodeKind::Struct) classCount++;
auto* doc = new rcx::RcxDocument(this);
doc->tree = std::move(tree);
m_mdiArea->closeAllSubWindows();
createTab(doc);
rebuildWorkspaceModel();
m_statusLabel->setText(QStringLiteral("Imported %1 classes from %2")
.arg(classCount).arg(QFileInfo(pdbPath).fileName()));
}
// ── Type Aliases Dialog ──
void MainWindow::showTypeAliasesDialog() {

View File

@@ -53,6 +53,7 @@ private slots:
void exportReclassXmlAction();
void importFromSource();
void importReclassXml();
void importPdb();
void showTypeAliasesDialog();
void editTheme();
void showOptionsDialog();

View File

@@ -0,0 +1,82 @@
#include <QtTest/QtTest>
#include "core.h"
#include "imports/import_pdb.h"
using namespace rcx;
class BenchImportPdb : public QObject {
Q_OBJECT
private slots:
void benchEnumerateAll();
void benchImportAll();
};
static const QString kPdbPath = QStringLiteral(
"C:/Symbols/ntkrnlmp.pdb/0762CF42EF7F3E8116EF7329ADAA09A31/ntkrnlmp.pdb");
void BenchImportPdb::benchEnumerateAll() {
if (!QFile::exists(kPdbPath))
QSKIP("ntkrnlmp.pdb not found at expected path");
QString err;
QElapsedTimer timer;
timer.start();
QVector<PdbTypeInfo> types = enumeratePdbTypes(kPdbPath, &err);
qint64 elapsed = timer.elapsed();
QVERIFY2(!types.isEmpty(), qPrintable(err));
qDebug() << "enumeratePdbTypes:" << types.size() << "types in" << elapsed << "ms";
}
void BenchImportPdb::benchImportAll() {
if (!QFile::exists(kPdbPath))
QSKIP("ntkrnlmp.pdb not found at expected path");
// Phase 1: enumerate
QString err;
QElapsedTimer timer;
timer.start();
QVector<PdbTypeInfo> types = enumeratePdbTypes(kPdbPath, &err);
qint64 enumerateMs = timer.elapsed();
QVERIFY2(!types.isEmpty(), qPrintable(err));
// Collect all type indices
QVector<uint32_t> indices;
indices.reserve(types.size());
for (const auto& t : types)
indices.append(t.typeIndex);
// Phase 2: import all
timer.restart();
int lastProgress = 0;
NodeTree tree = importPdbSelected(kPdbPath, indices, &err,
[&](int cur, int total) -> bool {
// Report progress at 25% intervals
int pct = (cur * 100) / total;
if (pct >= lastProgress + 25) {
qDebug() << " progress:" << cur << "/" << total
<< "(" << pct << "%)";
lastProgress = pct;
}
return true;
});
qint64 importMs = timer.elapsed();
QVERIFY2(!tree.nodes.isEmpty(), qPrintable(err));
// Count root structs
int rootCount = 0;
for (const auto& n : tree.nodes)
if (n.parentId == 0 && n.kind == NodeKind::Struct) rootCount++;
qDebug() << "";
qDebug() << "=== PDB Import Benchmark (ntkrnlmp.pdb) ===";
qDebug() << " Enumerate:" << types.size() << "types in" << enumerateMs << "ms";
qDebug() << " Import all:" << rootCount << "root structs,"
<< tree.nodes.size() << "total nodes in" << importMs << "ms";
qDebug() << " Total:" << (enumerateMs + importMs) << "ms";
qDebug() << "============================================";
}
QTEST_MAIN(BenchImportPdb)
#include "bench_import_pdb.moc"

View File

@@ -1,8 +1,8 @@
#include <QtTest/QtTest>
#include <QTemporaryFile>
#include "core.h"
#include "export_reclass_xml.h"
#include "import_reclass_xml.h"
#include "imports/export_reclass_xml.h"
#include "imports/import_reclass_xml.h"
using namespace rcx;

237
tests/test_import_pdb.cpp Normal file
View File

@@ -0,0 +1,237 @@
#include <QtTest/QtTest>
#include "core.h"
#include "imports/import_pdb.h"
using namespace rcx;
class TestImportPdb : public QObject {
Q_OBJECT
private slots:
void missingFileReturnsError();
void importKProcess();
void verifyDispatcherHeader();
void verifyListEntry();
void importFilteredStruct();
void enumerateTypes();
void importSelected();
};
static const QString kPdbPath = QStringLiteral(
"C:/Symbols/ntkrnlmp.pdb/0762CF42EF7F3E8116EF7329ADAA09A31/ntkrnlmp.pdb");
// Find a root struct by structTypeName
static int findRootStruct(const NodeTree& tree, const QString& name) {
for (int i = 0; i < tree.nodes.size(); i++) {
if (tree.nodes[i].parentId == 0 &&
tree.nodes[i].kind == NodeKind::Struct &&
tree.nodes[i].structTypeName == name)
return i;
}
return -1;
}
// Find a child of parentId by name
static int findChildNode(const NodeTree& tree, uint64_t parentId, const QString& name) {
for (int i = 0; i < tree.nodes.size(); i++) {
if (tree.nodes[i].parentId == parentId && tree.nodes[i].name == name)
return i;
}
return -1;
}
void TestImportPdb::missingFileReturnsError() {
QString err;
NodeTree tree = importPdb(QStringLiteral("C:/nonexistent.pdb"), {}, &err);
QVERIFY(tree.nodes.isEmpty());
QVERIFY(!err.isEmpty());
}
void TestImportPdb::importKProcess() {
if (!QFile::exists(kPdbPath))
QSKIP("ntkrnlmp.pdb not found at expected path");
QString err;
NodeTree tree = importPdb(kPdbPath, QStringLiteral("_KPROCESS"), &err);
QVERIFY2(!tree.nodes.isEmpty(), qPrintable(err));
// Find _KPROCESS root struct
int kpIdx = findRootStruct(tree, QStringLiteral("_KPROCESS"));
QVERIFY2(kpIdx >= 0, "Expected _KPROCESS root struct");
uint64_t kpId = tree.nodes[kpIdx].id;
// Verify Header field at offset 0 → embedded _DISPATCHER_HEADER
int headerIdx = findChildNode(tree, kpId, QStringLiteral("Header"));
QVERIFY2(headerIdx >= 0, "Expected 'Header' child of _KPROCESS");
QCOMPARE(tree.nodes[headerIdx].kind, NodeKind::Struct);
QCOMPARE(tree.nodes[headerIdx].structTypeName, QStringLiteral("_DISPATCHER_HEADER"));
QCOMPARE(tree.nodes[headerIdx].offset, 0);
// Verify ProfileListHead at offset 0x18 → embedded _LIST_ENTRY
int profileIdx = findChildNode(tree, kpId, QStringLiteral("ProfileListHead"));
QVERIFY2(profileIdx >= 0, "Expected 'ProfileListHead' child of _KPROCESS");
QCOMPARE(tree.nodes[profileIdx].kind, NodeKind::Struct);
QCOMPARE(tree.nodes[profileIdx].structTypeName, QStringLiteral("_LIST_ENTRY"));
QCOMPARE(tree.nodes[profileIdx].offset, 0x18);
}
void TestImportPdb::verifyDispatcherHeader() {
if (!QFile::exists(kPdbPath))
QSKIP("ntkrnlmp.pdb not found at expected path");
QString err;
NodeTree tree = importPdb(kPdbPath, QStringLiteral("_KPROCESS"), &err);
QVERIFY2(!tree.nodes.isEmpty(), qPrintable(err));
// _DISPATCHER_HEADER should be imported as a transitive dependency
int dhIdx = findRootStruct(tree, QStringLiteral("_DISPATCHER_HEADER"));
QVERIFY2(dhIdx >= 0, "_DISPATCHER_HEADER should be imported as a dependency");
uint64_t dhId = tree.nodes[dhIdx].id;
auto kids = tree.childrenOf(dhId);
QVERIFY2(!kids.isEmpty(), "_DISPATCHER_HEADER should have children (fields)");
// Look for WaitListHead — a _LIST_ENTRY at offset 0x10 in most builds
int waitIdx = findChildNode(tree, dhId, QStringLiteral("WaitListHead"));
QVERIFY2(waitIdx >= 0, "Expected 'WaitListHead' in _DISPATCHER_HEADER");
QCOMPARE(tree.nodes[waitIdx].kind, NodeKind::Struct);
QCOMPARE(tree.nodes[waitIdx].structTypeName, QStringLiteral("_LIST_ENTRY"));
}
void TestImportPdb::verifyListEntry() {
if (!QFile::exists(kPdbPath))
QSKIP("ntkrnlmp.pdb not found at expected path");
QString err;
NodeTree tree = importPdb(kPdbPath, QStringLiteral("_KPROCESS"), &err);
QVERIFY2(!tree.nodes.isEmpty(), qPrintable(err));
// _LIST_ENTRY should be imported (used by ProfileListHead and others)
int leIdx = findRootStruct(tree, QStringLiteral("_LIST_ENTRY"));
QVERIFY2(leIdx >= 0, "_LIST_ENTRY should be imported");
uint64_t leId = tree.nodes[leIdx].id;
// Flink at offset 0 — pointer to _LIST_ENTRY
int flinkIdx = findChildNode(tree, leId, QStringLiteral("Flink"));
QVERIFY2(flinkIdx >= 0, "Expected 'Flink' in _LIST_ENTRY");
QCOMPARE(tree.nodes[flinkIdx].kind, NodeKind::Pointer64);
QCOMPARE(tree.nodes[flinkIdx].offset, 0);
// Blink at offset 8 — pointer to _LIST_ENTRY
int blinkIdx = findChildNode(tree, leId, QStringLiteral("Blink"));
QVERIFY2(blinkIdx >= 0, "Expected 'Blink' in _LIST_ENTRY");
QCOMPARE(tree.nodes[blinkIdx].kind, NodeKind::Pointer64);
QCOMPARE(tree.nodes[blinkIdx].offset, 8);
// Both should point back to _LIST_ENTRY (self-referencing)
QCOMPARE(tree.nodes[flinkIdx].refId, leId);
QCOMPARE(tree.nodes[blinkIdx].refId, leId);
}
void TestImportPdb::importFilteredStruct() {
if (!QFile::exists(kPdbPath))
QSKIP("ntkrnlmp.pdb not found at expected path");
QString err;
NodeTree tree = importPdb(kPdbPath, QStringLiteral("_LIST_ENTRY"), &err);
QVERIFY2(!tree.nodes.isEmpty(), qPrintable(err));
int leIdx = findRootStruct(tree, QStringLiteral("_LIST_ENTRY"));
QVERIFY(leIdx >= 0);
// _LIST_ENTRY only references itself, so exactly 1 root struct
int rootCount = 0;
for (const auto& n : tree.nodes)
if (n.parentId == 0 && n.kind == NodeKind::Struct) rootCount++;
QCOMPARE(rootCount, 1);
}
void TestImportPdb::enumerateTypes() {
if (!QFile::exists(kPdbPath))
QSKIP("ntkrnlmp.pdb not found at expected path");
QString err;
QVector<PdbTypeInfo> types = enumeratePdbTypes(kPdbPath, &err);
QVERIFY2(!types.isEmpty(), qPrintable(err));
// Should have hundreds of types in ntkrnlmp
QVERIFY2(types.size() > 100,
qPrintable(QStringLiteral("Expected >100 types, got %1").arg(types.size())));
// Verify _KPROCESS is present
bool foundKProcess = false;
bool foundListEntry = false;
for (const auto& t : types) {
if (t.name == QStringLiteral("_KPROCESS")) {
foundKProcess = true;
QVERIFY2(t.childCount > 0, "_KPROCESS should have children");
QVERIFY2(t.size > 0, "_KPROCESS should have non-zero size");
}
if (t.name == QStringLiteral("_LIST_ENTRY")) {
foundListEntry = true;
}
}
QVERIFY2(foundKProcess, "_KPROCESS not found in enumerated types");
QVERIFY2(foundListEntry, "_LIST_ENTRY not found in enumerated types");
}
void TestImportPdb::importSelected() {
if (!QFile::exists(kPdbPath))
QSKIP("ntkrnlmp.pdb not found at expected path");
// First enumerate to find _LIST_ENTRY's type index
QString err;
QVector<PdbTypeInfo> types = enumeratePdbTypes(kPdbPath, &err);
QVERIFY2(!types.isEmpty(), qPrintable(err));
uint32_t listEntryIdx = 0;
bool found = false;
for (const auto& t : types) {
if (t.name == QStringLiteral("_LIST_ENTRY")) {
listEntryIdx = t.typeIndex;
found = true;
break;
}
}
QVERIFY2(found, "_LIST_ENTRY not found in enumeration");
// Import just _LIST_ENTRY
QVector<uint32_t> indices = { listEntryIdx };
int progressCalls = 0;
NodeTree tree = importPdbSelected(kPdbPath, indices, &err,
[&](int cur, int total) -> bool {
progressCalls++;
Q_UNUSED(total);
Q_ASSERT(cur <= total);
return true; // don't cancel
});
QVERIFY2(!tree.nodes.isEmpty(), qPrintable(err));
QVERIFY(progressCalls > 0);
// Verify _LIST_ENTRY root struct
int leIdx = findRootStruct(tree, QStringLiteral("_LIST_ENTRY"));
QVERIFY2(leIdx >= 0, "_LIST_ENTRY should be imported");
// Flink and Blink
uint64_t leId = tree.nodes[leIdx].id;
int flinkIdx = findChildNode(tree, leId, QStringLiteral("Flink"));
QVERIFY2(flinkIdx >= 0, "Expected 'Flink' in _LIST_ENTRY");
QCOMPARE(tree.nodes[flinkIdx].kind, NodeKind::Pointer64);
int blinkIdx = findChildNode(tree, leId, QStringLiteral("Blink"));
QVERIFY2(blinkIdx >= 0, "Expected 'Blink' in _LIST_ENTRY");
QCOMPARE(tree.nodes[blinkIdx].kind, NodeKind::Pointer64);
// Self-referencing pointers
QCOMPARE(tree.nodes[flinkIdx].refId, leId);
QCOMPARE(tree.nodes[blinkIdx].refId, leId);
// Only 1 root struct
int rootCount = 0;
for (const auto& n : tree.nodes)
if (n.parentId == 0 && n.kind == NodeKind::Struct) rootCount++;
QCOMPARE(rootCount, 1);
}
QTEST_MAIN(TestImportPdb)
#include "test_import_pdb.moc"

View File

@@ -1,6 +1,6 @@
#include <QtTest/QtTest>
#include "core.h"
#include "import_source.h"
#include "imports/import_source.h"
using namespace rcx;

View File

@@ -1,6 +1,6 @@
#include <QtTest/QtTest>
#include "core.h"
#include "import_reclass_xml.h"
#include "imports/import_reclass_xml.h"
using namespace rcx;

431
third_party/raw_pdb/.gitignore vendored Normal file
View File

@@ -0,0 +1,431 @@
# CLion
.idea/
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
*.env
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
[Dd]ebug/x64/
[Dd]ebugPublic/x64/
[Rr]elease/x64/
[Rr]eleases/x64/
bin/x64/
obj/x64/
[Dd]ebug/x86/
[Dd]ebugPublic/x86/
[Rr]elease/x86/
[Rr]eleases/x86/
bin/x86/
obj/x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
[Aa][Rr][Mm]64[Ee][Cc]/
bld/
[Oo]bj/
[Oo]ut/
[Ll]og/
[Ll]ogs/
# Build results on 'Bin' directories
**/[Bb]in/*
# Uncomment if you have tasks that rely on *.refresh files to move binaries
# (https://github.com/github/gitignore/pull/3736)
#!**/[Bb]in/*.refresh
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
*.trx
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Approval Tests result files
*.received.*
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.idb
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
# but not Directory.Build.rsp, as it configures directory-level build defaults
!Directory.Build.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.tlog
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
*.dsw
*.dsp
# Visual Studio 6 technical files
*.ncb
*.aps
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
**/.paket/paket.exe
paket-files/
# FAKE - F# Make
**/.fake/
# CodeRush personal settings
**/.cr/personal
# Python Tools for Visual Studio (PTVS)
**/__pycache__/
*.pyc
# Cake - Uncomment if you are using it
#tools/**
#!tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
MSBuild_Logs/
# AWS SAM Build and Temporary Artifacts folder
.aws-sam
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
**/.mfractor/
# Local History for Visual Studio
**/.localhistory/
# Visual Studio History (VSHistory) files
.vshistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
**/.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd
# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
# Local History for Visual Studio Code
.history/
# Built Visual Studio Code Extensions
*.vsix
# Windows Installer files from build outputs
*.cab
*.msi
*.msix
*.msm
*.msp

9
third_party/raw_pdb/CMakeLists.txt vendored Normal file
View File

@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.16)
project(raw_pdb)
set(CMAKE_CXX_STANDARD 11)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
add_subdirectory(src)

25
third_party/raw_pdb/LICENSE vendored Normal file
View File

@@ -0,0 +1,25 @@
BSD 2-Clause License
Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

190
third_party/raw_pdb/README.md vendored Normal file
View File

@@ -0,0 +1,190 @@
# RawPDB
**RawPDB** is a C++11 library that directly reads Microsoft Program DataBase PDB files. The code is extracted almost directly from <a href="https://liveplusplus.tech/">Live++ 2</a>, a battle-tested hot-reload tool for C++.
## Design
**RawPDB** gives you direct access to the stream data contained in a PDB file. It does not attempt to offer abstractions for iterating symbols, translation units, contributions, etc.
Building a high-level abstraction over the provided low-level data is an ill-fated attempt that can never really be performant for everybody, because different tools like debuggers, hot-reload tools (e.g. <a href="https://liveplusplus.tech/">Live++</a>), profilers (e.g. <a href="https://superluminal.eu/">Superluminal</a>), need to perform different queries against the stored data.
We therefore believe the best solution is to offer direct access to the underlying data, with applications bringing that data into their own structures.
## Goal
Eventually, we want **RawPDB** to become the de-facto replacement of <a href="https://docs.microsoft.com/en-us/visualstudio/debugger/debug-interface-access/debug-interface-access-sdk">Microsoft's DIA SDK</a> that most C++ developers (have to) use.
## Features
* Fast - **RawPDB** works directly with memory-mapped data, so only the data from the streams you touch affect performance. It is orders of magnitudes faster than the DIA SDK, and faster than comparable LLVM code
* Scalable - **RawPDB's** API gives you access to individual streams that can all be read concurrently in a trivial fashion, since all returned data structures are immutable. There are no locks or waits anywhere inside the library
* Lightweight - **RawPDB** is small and compiles in roughly 1 second
* Allocation-friendly - **RawPDB** performs only a few allocations, and those can be overridden easily by changing the underlying macro
* No STL - **RawPDB** does not need any STL containers or algorithms
* No exceptions - **RawPDB** does not use exceptions
* No RTTI - **RawPDB** does not need RTTI or use class hierarchies
* High-quality code - **RawPDB** compiles clean under -Wall
## Building
The code compiles clean under Visual Studio 2015, 2017, 2019, or 2022. A solution for Visual Studio 2019 is included.
## Performance
Running the **Symbols** and **Contributions** examples on a 1GiB PDB yields the following output:
<pre>
Opening PDB file C:\Development\llvm-project\build\tools\clang\unittests\Tooling\RelWithDebInfo\ToolingTests.pdb
Running example "Symbols"
| Reading image section stream
| ---> done in 0.066ms
| Reading module info stream
| ---> done in 0.562ms
| Reading symbol record stream
| ---> done in 25.185ms
| Reading public symbol stream
| ---> done in 1.133ms
| Storing public symbols
| ---> done in 46.171ms (212023 elements)
| Reading global symbol stream
| ---> done in 1.381ms
| Storing global symbols
| ---> done in 12.769ms (448957 elements)
| Storing symbols from modules
| ---> done in 145.849ms (2243 elements)
---> done in 233.694ms (539611 elements)
</pre>
<pre>
Opening PDB file C:\Development\llvm-project\build\tools\clang\unittests\Tooling\RelWithDebInfo\ToolingTests.pdb
Running example "Contributions"
| Reading image section stream
| ---> done in 0.066ms
| Reading module info stream
| ---> done in 0.594ms
| Reading section contribution stream
| ---> done in 9.839ms
| Storing contributions
| ---> done in 67.346ms (630924 elements)
| std::sort contributions
| ---> done in 19.218ms
---> done in 97.283ms
20 largest contributions:
1: 1896496 bytes from LLVMAMDGPUCodeGen.dir\RelWithDebInfo\AMDGPUInstructionSelector.obj
2: 1700720 bytes from LLVMHexagonCodeGen.dir\RelWithDebInfo\HexagonInstrInfo.obj
3: 1536470 bytes from LLVMRISCVCodeGen.dir\RelWithDebInfo\RISCVISelDAGToDAG.obj
4: 1441408 bytes from LLVMAArch64CodeGen.dir\RelWithDebInfo\AArch64InstructionSelector.obj
5: 1187048 bytes from LLVMRISCVCodeGen.dir\RelWithDebInfo\RISCVInstructionSelector.obj
6: 1026504 bytes from LLVMARMCodeGen.dir\RelWithDebInfo\ARMInstructionSelector.obj
7: 952080 bytes from LLVMAMDGPUDesc.dir\RelWithDebInfo\AMDGPUMCTargetDesc.obj
8: 849888 bytes from LLVMX86Desc.dir\RelWithDebInfo\X86MCTargetDesc.obj
9: 712176 bytes from LLVMHexagonCodeGen.dir\RelWithDebInfo\HexagonInstrInfo.obj
10: 679035 bytes from LLVMX86CodeGen.dir\RelWithDebInfo\X86ISelDAGToDAG.obj
11: 525174 bytes from LLVMAMDGPUDesc.dir\RelWithDebInfo\AMDGPUMCTargetDesc.obj
12: 523035 bytes from * Linker *
13: 519312 bytes from LLVMRISCVDesc.dir\RelWithDebInfo\RISCVMCTargetDesc.obj
14: 512496 bytes from LLVMVEDesc.dir\RelWithDebInfo\VEMCTargetDesc.obj
15: 498768 bytes from LLVMX86CodeGen.dir\RelWithDebInfo\X86InstructionSelector.obj
16: 483528 bytes from LLVMMipsCodeGen.dir\RelWithDebInfo\MipsInstructionSelector.obj
17: 449472 bytes from LLVMAMDGPUCodeGen.dir\RelWithDebInfo\AMDGPUISelDAGToDAG.obj
18: 444246 bytes from C:\Development\llvm-project\build\tools\clang\lib\Basic\obj.clangBasic.dir\RelWithDebInfo\DiagnosticIDs.obj
19: 371584 bytes from LLVMAArch64CodeGen.dir\RelWithDebInfo\AArch64ISelDAGToDAG.obj
20: 370272 bytes from LLVMNVPTXDesc.dir\RelWithDebInfo\NVPTXMCTargetDesc.obj
</pre>
This is at least an order of magnitude faster than DIA, even though the example code is completely serial and uses std::vector, std::string, and std::sort, which are used for illustration purposes only.
When reading streams in a concurrent fashion, you will most likely be limited by the speed at which the OS can bring the data into your process.
Running the **Lines** example on a 1.37 GiB PDB yields the following output:
<pre>
Opening PDB file C:\pdb-test-files\clang-debug.pdb
Version 20000404, signature 1658696914, age 1, GUID 563dd8f1-f32b-459b-8c2beae0e70bc19b
Running example "Lines"
| Reading image section stream
| ---> done in 0.313ms
| Reading module info stream
| ---> done in 0.403ms
| Reading names stream
| ---> done in 0.126ms
| Storing lines from modules
| ---> done in 306.720ms (1847 elements)
| std::sort sections
| ---> done in 103.090ms (4023680 elements)
</pre>
## Supported streams
**RawPDB** gives you access to the following PDB stream data:
* DBI stream data
* Public symbols
* Global symbols
* Modules
* Module symbols
* Module lines (C13 line information)
* Image sections
* Info stream
* "/names" stream
* Section contributions
* Source files
* IPI stream data
* TPI stream data
Furthermore, PDBs linked using /DEBUG:FASTLINK are not supported. These PDBs do not contain much information, since private symbol information is distributed among object files and library files.
## Documentation
If you are unfamiliar with the basic structure of a PDB file, the <a href="https://llvm.org/docs/PDB/index.html">LLVM documentation</a> serves as a good introduction.
Consult the example code to see how to read and parse the PDB streams.
## Directory structure
* bin: contains final binary output files (.exe and .pdb)
* build: contains Visual Studio 2019 solution and project files
* lib: contains the RawPDB library output files (.lib and .pdb)
* src: contains the RawPDB source code, as well as example code
* temp: contains intermediate build artefacts
## Examples
### Symbols (<a href="https://github.com/MolecularMatters/raw_pdb/blob/main/src/Examples/ExampleSymbols.cpp">ExampleSymbols.cpp</a>)
A basic example that shows how to load symbols from public, global, and module streams.
### Contributions (<a href="https://github.com/MolecularMatters/raw_pdb/blob/main/src/Examples/ExampleContributions.cpp">ExampleContributions.cpp</a>)
A basic example that shows how to load contributions, sort them by size, and output the 20 largest ones along with the object file they originated from.
### Function symbols (<a href="https://github.com/MolecularMatters/raw_pdb/blob/main/src/Examples/ExampleFunctionSymbols.cpp">ExampleFunctionSymbols.cpp</a>)
An example intended for profiler developers that shows how to enumerate all function symbols and retrieve or compute their code size.
### Function variables (<a href="https://github.com/MolecularMatters/raw_pdb/blob/main/src/Examples/ExampleFunctionVariables.cpp">ExampleFunctionVariables.cpp</a>)
An example intended for debugger developers that shows how to enumerate all function records needed for displaying function variables.
### Lines (<a href="https://github.com/MolecularMatters/raw_pdb/blob/main/src/Examples/ExampleLines.cpp">ExampleLines.cpp</a>)
An example that shows to how to load line information for all modules.
### Types (<a href="https://github.com/MolecularMatters/raw_pdb/blob/main/src/Examples/ExampleTypes.cpp">ExampleTypes.cpp</a>)
An example that prints all type records.
### PDBSize (<a href="https://github.com/MolecularMatters/raw_pdb/blob/main/src/Examples/ExamplePDBSize.cpp">ExamplePDBSize.cpp</a>)
An example that could serve as a starting point for people wanting to investigate and optimize the size of their PDBs.
## Sponsoring or supporting RawPDB
We have chosen a very liberal license to let **RawPDB** be used in as many scenarios as possible, including commercial applications. If you would like to support its development, consider licensing <a href="https://liveplusplus.tech/">Live++</a> instead. Not only do you give something back, but get a great productivity enhancement on top!

12
third_party/raw_pdb/raw_pdb.natvis vendored Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="PDB::ArrayView&lt;*&gt;">
<DisplayString>{{ size={m_length} }}</DisplayString>
<Expand>
<ArrayItems>
<Size>m_length</Size>
<ValuePointer>m_data</ValuePointer>
</ArrayItems>
</Expand>
</Type>
</AutoVisualizer>

112
third_party/raw_pdb/src/CMakeLists.txt vendored Normal file
View File

@@ -0,0 +1,112 @@
set(SOURCES
Foundation/PDB_ArrayView.h
Foundation/PDB_Assert.h
Foundation/PDB_BitOperators.h
Foundation/PDB_BitUtil.h
Foundation/PDB_CRT.h
Foundation/PDB_Forward.h
Foundation/PDB_Log.h
Foundation/PDB_Macros.h
Foundation/PDB_Memory.h
Foundation/PDB_Move.h
Foundation/PDB_Platform.h
Foundation/PDB_PointerUtil.h
Foundation/PDB_TypeTraits.h
Foundation/PDB_Warnings.h
PDB.cpp
PDB.h
PDB_CoalescedMSFStream.cpp
PDB_CoalescedMSFStream.h
PDB_DBIStream.cpp
PDB_DBIStream.h
PDB_DBITypes.cpp
PDB_DBITypes.h
PDB_DirectMSFStream.cpp
PDB_DirectMSFStream.h
PDB_ErrorCodes.h
PDB_GlobalSymbolStream.cpp
PDB_GlobalSymbolStream.h
PDB_ImageSectionStream.cpp
PDB_ImageSectionStream.h
PDB_InfoStream.cpp
PDB_InfoStream.h
PDB_IPIStream.cpp
PDB_IPIStream.h
PDB_IPITypes.h
PDB_ModuleInfoStream.cpp
PDB_ModuleInfoStream.h
PDB_ModuleLineStream.cpp
PDB_ModuleLineStream.h
PDB_ModuleSymbolStream.cpp
PDB_ModuleSymbolStream.h
PDB_NamesStream.cpp
PDB_NamesStream.h
PDB_PCH.cpp
PDB_PCH.h
PDB_PublicSymbolStream.cpp
PDB_PublicSymbolStream.h
PDB_RawFile.cpp
PDB_RawFile.h
PDB_SectionContributionStream.cpp
PDB_SectionContributionStream.h
PDB_SourceFileStream.cpp
PDB_SourceFileStream.h
PDB_TPIStream.cpp
PDB_TPIStream.h
PDB_TPITypes.h
PDB_Types.cpp
PDB_Types.h
PDB_Util.h
)
source_group(src FILES
${SOURCES}
)
add_library(raw_pdb
${SOURCES}
)
target_include_directories(raw_pdb
PUBLIC
.
)
target_precompile_headers(raw_pdb
PRIVATE
PDB_PCH.h
)
option(RAWPDB_BUILD_EXAMPLES "Build Examples" ON)
if (RAWPDB_BUILD_EXAMPLES)
add_subdirectory(Examples)
endif()
if (UNIX)
include(GNUInstallDirs)
install(
TARGETS raw_pdb
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)
file(GLOB_RECURSE HEADER_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/*.h"
)
file(GLOB_RECURSE HEADER_FILES_FOUNDATION
"${CMAKE_CURRENT_SOURCE_DIR}/Foundation/*.h"
)
install(
FILES ${HEADER_FILES}
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/raw_pdb/"
)
install(
FILES ${HEADER_FILES_FOUNDATION}
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/raw_pdb/Foundation"
)
endif (UNIX)

View File

@@ -0,0 +1,39 @@
project(Examples)
set(SOURCES
ExampleContributions.cpp
ExampleFunctionSymbols.cpp
ExampleFunctionVariables.cpp
ExampleIPI.cpp
ExampleLines.cpp
ExampleMain.cpp
ExampleMemoryMappedFile.cpp
ExampleMemoryMappedFile.h
ExamplePDBSize.cpp
Examples_PCH.cpp
Examples_PCH.h
ExampleSymbols.cpp
ExampleTimedScope.cpp
ExampleTimedScope.h
ExampleTypes.cpp
ExampleTypeTable.cpp
ExampleTypeTable.h
)
source_group(src FILES
${SOURCES}
)
add_executable(Examples
${SOURCES}
)
target_link_libraries(Examples
PUBLIC
raw_pdb
)
target_precompile_headers(Examples
PUBLIC
Examples_PCH.h
)

View File

@@ -0,0 +1,96 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"
#include "ExampleTimedScope.h"
#include "PDB_RawFile.h"
#include "PDB_DBIStream.h"
namespace
{
// we don't have to store std::string in the contributions, since all the data is memory-mapped anyway.
// we do it in this example to ensure that we don't "cheat" when reading the PDB file. memory-mapped data will only
// be faulted into the process once it's touched, so actually copying the string data makes us touch the needed data,
// giving us a real performance measurement.
struct Contribution
{
std::string objectFile;
uint32_t rva;
uint32_t size;
};
}
void ExampleContributions(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream);
void ExampleContributions(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream)
{
TimedScope total("\nRunning example \"Contributions\"");
// in order to keep the example easy to understand, we load the PDB data serially.
// note that this can be improved a lot by reading streams concurrently.
// prepare the image section stream first. it is needed for converting section + offset into an RVA
TimedScope sectionScope("Reading image section stream");
const PDB::ImageSectionStream imageSectionStream = dbiStream.CreateImageSectionStream(rawPdbFile);
sectionScope.Done();
// prepare the module info stream for matching contributions against files
TimedScope moduleScope("Reading module info stream");
const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
moduleScope.Done();
// read contribution stream
TimedScope contributionScope("Reading section contribution stream");
const PDB::SectionContributionStream sectionContributionStream = dbiStream.CreateSectionContributionStream(rawPdbFile);
contributionScope.Done();
std::vector<Contribution> contributions;
{
TimedScope scope("Storing contributions");
const PDB::ArrayView<PDB::DBI::SectionContribution> sectionContributions = sectionContributionStream.GetContributions();
const size_t count = sectionContributions.GetLength();
contributions.reserve(count);
for (const PDB::DBI::SectionContribution& contribution : sectionContributions)
{
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(contribution.section, contribution.offset);
if (rva == 0u)
{
printf("Contribution has invalid RVA\n");
continue;
}
const PDB::ModuleInfoStream::Module& module = moduleInfoStream.GetModule(contribution.moduleIndex);
contributions.push_back(Contribution { module.GetName().Decay(), rva, contribution.size });
}
scope.Done(count);
}
TimedScope sortScope("std::sort contributions");
std::sort(contributions.begin(), contributions.end(), [](const Contribution& lhs, const Contribution& rhs)
{
return lhs.size > rhs.size;
});
sortScope.Done();
total.Done();
// log the 20 largest contributions
{
printf("20 largest contributions:\n");
const size_t countToShow = std::min<size_t>(20ul, contributions.size());
for (size_t i = 0u; i < countToShow; ++i)
{
const Contribution& contribution = contributions[i];
printf("%zu: %u bytes from %s\n", i + 1u, contribution.size, contribution.objectFile.c_str());
}
}
}

View File

@@ -0,0 +1,262 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"
#include "ExampleTimedScope.h"
#include "PDB_RawFile.h"
#include "PDB_DBIStream.h"
namespace
{
// in this example, we are only interested in function symbols: function name, RVA, and size.
// this is what most profilers need, they aren't interested in any other data.
struct FunctionSymbol
{
std::string name;
uint32_t rva;
uint32_t size;
const PDB::CodeView::DBI::Record* frameProc;
};
}
void ExampleFunctionSymbols(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream);
void ExampleFunctionSymbols(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream)
{
TimedScope total("\nRunning example \"Function symbols\"");
// in order to keep the example easy to understand, we load the PDB data serially.
// note that this can be improved a lot by reading streams concurrently.
// prepare the image section stream first. it is needed for converting section + offset into an RVA
TimedScope sectionScope("Reading image section stream");
const PDB::ImageSectionStream imageSectionStream = dbiStream.CreateImageSectionStream(rawPdbFile);
sectionScope.Done();
// prepare the module info stream for grabbing function symbols from modules
TimedScope moduleScope("Reading module info stream");
const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
moduleScope.Done();
// prepare symbol record stream needed by the public stream
TimedScope symbolStreamScope("Reading symbol record stream");
const PDB::CoalescedMSFStream symbolRecordStream = dbiStream.CreateSymbolRecordStream(rawPdbFile);
symbolStreamScope.Done();
// note that we only use unordered_set in order to keep the example code easy to understand.
// using other hash set implementations like e.g. abseil's Swiss Tables (https://abseil.io/about/design/swisstables) is *much* faster.
std::vector<FunctionSymbol> functionSymbols;
std::unordered_set<uint32_t> seenFunctionRVAs;
// start by reading the module stream, grabbing every function symbol we can find.
// in most cases, this gives us ~90% of all function symbols already, along with their size.
{
TimedScope scope("Storing function symbols from modules");
const PDB::ArrayView<PDB::ModuleInfoStream::Module> modules = moduleInfoStream.GetModules();
for (const PDB::ModuleInfoStream::Module& module : modules)
{
if (!module.HasSymbolStream())
{
continue;
}
const PDB::ModuleSymbolStream moduleSymbolStream = module.CreateSymbolStream(rawPdbFile);
moduleSymbolStream.ForEachSymbol([&functionSymbols, &seenFunctionRVAs, &imageSectionStream](const PDB::CodeView::DBI::Record* record)
{
// only grab function symbols from the module streams
const char* name = nullptr;
uint32_t rva = 0u;
uint32_t size = 0u;
if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_FRAMEPROC)
{
functionSymbols[functionSymbols.size() - 1].frameProc = record;
return;
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_THUNK32)
{
if (record->data.S_THUNK32.thunk == PDB::CodeView::DBI::ThunkOrdinal::TrampolineIncremental)
{
// we have never seen incremental linking thunks stored inside a S_THUNK32 symbol, but better safe than sorry
name = "ILT";
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_THUNK32.section, record->data.S_THUNK32.offset);
size = 5u;
}
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_TRAMPOLINE)
{
// incremental linking thunks are stored in the linker module
name = "ILT";
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_TRAMPOLINE.thunkSection, record->data.S_TRAMPOLINE.thunkOffset);
size = 5u;
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LPROC32)
{
name = record->data.S_LPROC32.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LPROC32.section, record->data.S_LPROC32.offset);
size = record->data.S_LPROC32.codeSize;
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GPROC32)
{
name = record->data.S_GPROC32.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GPROC32.section, record->data.S_GPROC32.offset);
size = record->data.S_GPROC32.codeSize;
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LPROC32_ID)
{
name = record->data.S_LPROC32_ID.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LPROC32_ID.section, record->data.S_LPROC32_ID.offset);
size = record->data.S_LPROC32_ID.codeSize;
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GPROC32_ID)
{
name = record->data.S_GPROC32_ID.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GPROC32_ID.section, record->data.S_GPROC32_ID.offset);
size = record->data.S_GPROC32_ID.codeSize;
}
if (rva == 0u)
{
return;
}
functionSymbols.push_back(FunctionSymbol { name, rva, size, nullptr });
seenFunctionRVAs.emplace(rva);
});
}
scope.Done(modules.GetLength());
}
// we don't need to touch global symbols in this case.
// most of the data we need can be obtained from the module symbol streams, and the global symbol stream only offers data symbols on top of that, which we are not interested in.
// however, there can still be public function symbols we haven't seen yet in any of the modules, especially for PDBs that don't provide module-specific information.
// read public symbols
TimedScope publicScope("Reading public symbol stream");
const PDB::PublicSymbolStream publicSymbolStream = dbiStream.CreatePublicSymbolStream(rawPdbFile);
publicScope.Done();
{
TimedScope scope("Storing public function symbols");
const PDB::ArrayView<PDB::HashRecord> hashRecords = publicSymbolStream.GetRecords();
const size_t count = hashRecords.GetLength();
for (const PDB::HashRecord& hashRecord : hashRecords)
{
const PDB::CodeView::DBI::Record* record = publicSymbolStream.GetRecord(symbolRecordStream, hashRecord);
if (record->header.kind != PDB::CodeView::DBI::SymbolRecordKind::S_PUB32)
{
// normally, a PDB only contains S_PUB32 symbols in the public symbol stream, but we have seen PDBs that also store S_CONSTANT as public symbols.
// ignore these.
continue;
}
if ((PDB_AS_UNDERLYING(record->data.S_PUB32.flags) & PDB_AS_UNDERLYING(PDB::CodeView::DBI::PublicSymbolFlags::Function)) == 0u)
{
// ignore everything that is not a function
continue;
}
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_PUB32.section, record->data.S_PUB32.offset);
if (rva == 0u)
{
// certain symbols (e.g. control-flow guard symbols) don't have a valid RVA, ignore those
continue;
}
// check whether we already know this symbol from one of the module streams
const auto it = seenFunctionRVAs.find(rva);
if (it != seenFunctionRVAs.end())
{
// we know this symbol already, ignore it
continue;
}
// this is a new function symbol, so store it.
// note that we don't know its size yet.
functionSymbols.push_back(FunctionSymbol { record->data.S_PUB32.name, rva, 0u, nullptr });
}
scope.Done(count);
}
// we still need to find the size of the public function symbols.
// this can be deduced by sorting the symbols by their RVA, and then computing the distance between the current and the next symbol.
// this works since functions are always mapped to executable pages, so they aren't interleaved by any data symbols.
TimedScope sortScope("std::sort function symbols");
std::sort(functionSymbols.begin(), functionSymbols.end(), [](const FunctionSymbol& lhs, const FunctionSymbol& rhs)
{
return lhs.rva < rhs.rva;
});
sortScope.Done();
const size_t symbolCount = functionSymbols.size();
if (symbolCount != 0u)
{
TimedScope computeScope("Computing function symbol sizes");
size_t foundCount = 0u;
// we have at least 1 symbol.
// compute missing symbol sizes by computing the distance from this symbol to the next.
// note that this includes "int 3" padding after the end of a function. if you don't want that, but the actual number of bytes of
// the function's code, your best bet is to use a disassembler instead.
for (size_t i = 0u; i < symbolCount - 1u; ++i)
{
FunctionSymbol& currentSymbol = functionSymbols[i];
if (currentSymbol.size != 0u)
{
// the symbol's size is already known
continue;
}
const FunctionSymbol& nextSymbol = functionSymbols[i + 1u];
const size_t size = nextSymbol.rva - currentSymbol.rva;
(void)size; // unused
++foundCount;
}
// we know have the sizes of all symbols, except the last.
// this can be found by going through the contributions, if needed.
FunctionSymbol& lastSymbol = functionSymbols[symbolCount - 1u];
if (lastSymbol.size == 0u)
{
// bad luck, we can't deduce the last symbol's size, so have to consult the contributions instead.
// we do a linear search in this case to keep the code simple.
const PDB::SectionContributionStream sectionContributionStream = dbiStream.CreateSectionContributionStream(rawPdbFile);
const PDB::ArrayView<PDB::DBI::SectionContribution> sectionContributions = sectionContributionStream.GetContributions();
for (const PDB::DBI::SectionContribution& contribution : sectionContributions)
{
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(contribution.section, contribution.offset);
if (rva == 0u)
{
printf("Contribution has invalid RVA\n");
continue;
}
if (rva == lastSymbol.rva)
{
lastSymbol.size = contribution.size;
break;
}
if (rva > lastSymbol.rva)
{
// should have found the contribution by now
printf("Unknown contribution for symbol %s at RVA 0x%X", lastSymbol.name.c_str(), lastSymbol.rva);
break;
}
}
}
computeScope.Done(foundCount);
}
total.Done(functionSymbols.size());
}

View File

@@ -0,0 +1,382 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"
#include "ExampleTimedScope.h"
#include "ExampleTypeTable.h"
#include "PDB_RawFile.h"
#include "PDB_DBIStream.h"
#include "PDB_TPIStream.h"
using SymbolRecordKind = PDB::CodeView::DBI::SymbolRecordKind;
static std::string GetVariableTypeName(const TypeTable& typeTable, uint32_t typeIndex)
{
// Defined in ExampleTypes.cpp
extern std::string GetTypeName(const TypeTable & typeTable, uint32_t typeIndex);
std::string typeName = GetTypeName(typeTable, typeIndex);
// Remove any '%s' substring used to insert a variable/field name.
const uint64_t markerPos = typeName.find("%s");
if (markerPos != typeName.npos)
{
typeName.erase(markerPos, 2);
}
return typeName;
}
static void Printf(uint32_t indent, const char* format, ...)
{
va_list args;
va_start(args, format);
printf("%*s", indent * 4, "");
vprintf(format, args);
va_end(args);
}
void ExampleFunctionVariables(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::TPIStream& tpiStream);
void ExampleFunctionVariables(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::TPIStream& tpiStream)
{
TimedScope total("\nRunning example \"Function variables\"");
TimedScope typeTableScope("Create TypeTable");
TypeTable typeTable(tpiStream);
typeTableScope.Done();
// in order to keep the example easy to understand, we load the PDB data serially.
// note that this can be improved a lot by reading streams concurrently.
// prepare the image section stream first. it is needed for converting section + offset into an RVA
TimedScope sectionScope("Reading image section stream");
const PDB::ImageSectionStream imageSectionStream = dbiStream.CreateImageSectionStream(rawPdbFile);
sectionScope.Done();
// prepare the module info stream for grabbing function symbols from modules
TimedScope moduleScope("Reading module info stream");
const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
moduleScope.Done();
// prepare symbol record stream needed by the public stream
TimedScope symbolStreamScope("Reading symbol record stream");
const PDB::CoalescedMSFStream symbolRecordStream = dbiStream.CreateSymbolRecordStream(rawPdbFile);
symbolStreamScope.Done();
{
TimedScope scope("Printing function variable records from modules\n");
const PDB::ArrayView<PDB::ModuleInfoStream::Module> modules = moduleInfoStream.GetModules();
uint32_t blockLevel = 0;
uint32_t recordCount = 0;
for (const PDB::ModuleInfoStream::Module& module : modules)
{
if (!module.HasSymbolStream())
{
continue;
}
const PDB::ModuleSymbolStream moduleSymbolStream = module.CreateSymbolStream(rawPdbFile);
moduleSymbolStream.ForEachSymbol([&typeTable, &imageSectionStream, &blockLevel, &recordCount](const PDB::CodeView::DBI::Record* record)
{
const SymbolRecordKind kind = record->header.kind;
const PDB::CodeView::DBI::Record::Data& data = record->data;
if (kind == SymbolRecordKind::S_END)
{
PDB_ASSERT(blockLevel > 0, "Block level for S_END is 0");
blockLevel--;
Printf(blockLevel, "S_END\n");
if (blockLevel == 0)
{
Printf(0, "\n");
}
}
else if(kind == SymbolRecordKind::S_SKIP)
{
Printf(blockLevel, "S_SKIP\n");
}
else if (kind == SymbolRecordKind::S_BLOCK32)
{
const uint32_t offset = imageSectionStream.ConvertSectionOffsetToRVA(data.S_BLOCK32.section, data.S_BLOCK32.offset);
Printf(blockLevel, "S_BLOCK32: '%s' | Code Offset 0x%X\n", data.S_BLOCK32.name, offset);
blockLevel++;
}
else if (kind == SymbolRecordKind::S_LABEL32)
{
Printf(blockLevel, "S_LABEL32: '%s' | Offset 0x%X\n", data.S_LABEL32.name, data.S_LABEL32.offset);
}
else if(kind == SymbolRecordKind::S_CONSTANT)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_CONSTANT.typeIndex);
Printf(blockLevel, "S_CONSTANT: '%s' -> '%s' | Value 0x%X\n", typeName.c_str(), data.S_CONSTANT.name, data.S_CONSTANT.value);
}
else if(kind == SymbolRecordKind::S_LOCAL)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_LOCAL.typeIndex);
Printf(blockLevel, "S_LOCAL: '%s' -> '%s' | Param: %s | Optimized Out: %s\n", typeName.c_str(), data.S_LOCAL.name, data.S_LOCAL.flags.fIsParam ? "True" : "False", data.S_LOCAL.flags.fIsOptimizedOut ? "True" : "False");
}
else if (kind == SymbolRecordKind::S_DEFRANGE_REGISTER)
{
Printf(blockLevel, "S_DEFRANGE_REGISTER: Register 0x%X\n", data.S_DEFRANGE_REGISTER.reg);
}
else if(kind == SymbolRecordKind::S_DEFRANGE_FRAMEPOINTER_REL)
{
Printf(blockLevel, "S_DEFRANGE_FRAMEPOINTER_REL: Frame Pointer Offset 0x%X | Range Start 0x%X | Range Section Start 0x%X | Range Length %u\n",
data.S_DEFRANGE_FRAMEPOINTER_REL.offsetFramePointer,
data.S_DEFRANGE_FRAMEPOINTER_REL.range.offsetStart,
data.S_DEFRANGE_FRAMEPOINTER_REL.range.isectionStart,
data.S_DEFRANGE_FRAMEPOINTER_REL.range.length);
}
else if(kind == SymbolRecordKind::S_DEFRANGE_SUBFIELD_REGISTER)
{
Printf(blockLevel, "S_DEFRANGE_SUBFIELD_REGISTER: Register %u | Parent offset 0x%X | Range Start 0x%X | Range Section Start 0x%X | Range Length %u\n",
data.S_DEFRANGE_SUBFIELD_REGISTER.reg,
data.S_DEFRANGE_SUBFIELD_REGISTER.offsetParent,
data.S_DEFRANGE_SUBFIELD_REGISTER.range.offsetStart,
data.S_DEFRANGE_SUBFIELD_REGISTER.range.isectionStart,
data.S_DEFRANGE_SUBFIELD_REGISTER.range.length);
}
else if (kind == SymbolRecordKind::S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE)
{
Printf(blockLevel, "S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE: Offset 0x%X\n", data.S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE.offsetFramePointer);
}
else if (kind == SymbolRecordKind::S_DEFRANGE_REGISTER_REL)
{
Printf(blockLevel, "S_DEFRANGE_REGISTER_REL: Base Register %u | Parent offset 0x%X | Base Register Offset 0x%X | Range Start 0x%X | Range Section Start 0x%X | Range Length %u\n",
data.S_DEFRANGE_REGISTER_REL.baseRegister,
data.S_DEFRANGE_REGISTER_REL.offsetParent,
data.S_DEFRANGE_REGISTER_REL.offsetBasePointer,
data.S_DEFRANGE_REGISTER_REL.offsetParent,
data.S_DEFRANGE_REGISTER_REL.range.offsetStart,
data.S_DEFRANGE_REGISTER_REL.range.isectionStart,
data.S_DEFRANGE_REGISTER_REL.range.length);
}
else if(kind == SymbolRecordKind::S_FILESTATIC)
{
Printf(blockLevel, "S_FILESTATIC: '%s'\n", data.S_FILESTATIC.name);
}
else if (kind == SymbolRecordKind::S_INLINESITE)
{
Printf(blockLevel, "S_INLINESITE: Parent 0x%X\n", data.S_INLINESITE.parent);
blockLevel++;
}
else if (kind == SymbolRecordKind::S_INLINESITE_END)
{
PDB_ASSERT(blockLevel > 0, "Block level for S_INLINESITE_END is 0");
blockLevel--;
Printf(blockLevel, "S_INLINESITE_END:\n");
}
else if (kind == SymbolRecordKind::S_CALLEES)
{
Printf(blockLevel, "S_CALLEES: Count %u\n", data.S_CALLEES.count);
}
else if (kind == SymbolRecordKind::S_CALLERS)
{
Printf(blockLevel, "S_CALLERS: Count %u\n", data.S_CALLERS.count);
}
else if (kind == SymbolRecordKind::S_INLINEES)
{
Printf(blockLevel, "S_INLINEES: Count %u\n", data.S_INLINEES.count);
}
else if (kind == SymbolRecordKind::S_LDATA32)
{
if (blockLevel > 0)
{
// Not sure why some type index 0 (T_NO_TYPE) are included in some PDBs.
if (data.S_LDATA32.typeIndex != 0) // PDB::CodeView::TPI::TypeIndexKind::T_NOTYPE)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_LDATA32.typeIndex);
Printf(blockLevel, "S_LDATA32: '%s' -> '%s'\n", data.S_LDATA32.name, typeName.c_str());
}
}
}
else if (kind == SymbolRecordKind::S_LTHREAD32)
{
if (blockLevel > 0)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_LTHREAD32.typeIndex);
Printf(blockLevel, "S_LTHREAD32: '%s' -> '%s'\n", data.S_LTHREAD32.name, typeName.c_str());
}
}
else if (kind == SymbolRecordKind::S_UDT)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_UDT.typeIndex);
Printf(blockLevel, "S_UDT: '%s' -> '%s'\n", data.S_UDT.name, typeName.c_str());
}
else if (kind == PDB::CodeView::DBI::SymbolRecordKind::S_REGISTER)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_REGSYM.typeIndex);
Printf(blockLevel, "S_REGSYM: '%s' -> '%s' | Register %i\n",
data.S_REGSYM.name, typeName.c_str(),
data.S_REGSYM.reg);
}
else if (kind == PDB::CodeView::DBI::SymbolRecordKind::S_BPREL32)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_BPRELSYM32.typeIndex);
Printf(blockLevel, "S_BPRELSYM32: '%s' -> '%s' | BP register Offset 0x%X\n",
data.S_BPRELSYM32.name, typeName.c_str(),
data.S_BPRELSYM32.offset);
}
else if (kind == PDB::CodeView::DBI::SymbolRecordKind::S_REGREL32)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_REGREL32.typeIndex);
Printf(blockLevel, "S_REGREL32: '%s' -> '%s' | Register %i | Register Offset 0x%X\n",
data.S_REGREL32.name, typeName.c_str(),
data.S_REGREL32.reg,
data.S_REGREL32.offset);
}
else if(kind == SymbolRecordKind::S_FRAMECOOKIE)
{
Printf(blockLevel, "S_FRAMECOOKIE: Offset 0x%X | Register %u | Type %u\n",
data.S_FRAMECOOKIE.offset,
data.S_FRAMECOOKIE.reg,
data.S_FRAMECOOKIE.cookietype);
}
else if(kind == SymbolRecordKind::S_CALLSITEINFO)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_CALLSITEINFO.typeIndex);
Printf(blockLevel, "S_CALLSITEINFO: '%s' | Offset 0x%X | Section %u\n", typeName.c_str(), data.S_CALLSITEINFO.offset, data.S_CALLSITEINFO.section);
}
else if(kind == SymbolRecordKind::S_HEAPALLOCSITE)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_HEAPALLOCSITE.typeIndex);
Printf(blockLevel, "S_HEAPALLOCSITE: '%s' | Offset 0x%X | Section %u | Instruction Length %u\n", typeName.c_str(),
data.S_HEAPALLOCSITE.offset,
data.S_HEAPALLOCSITE.section,
data.S_HEAPALLOCSITE.instructionLength);
}
else if (kind == SymbolRecordKind::S_FRAMEPROC)
{
Printf(blockLevel, "S_FRAMEPROC: Size %u | Padding %u | Padding Offset 0x%X | Callee Registers Size %u\n",
data.S_FRAMEPROC.cbFrame,
data.S_FRAMEPROC.cbPad,
data.S_FRAMEPROC.offPad,
data.S_FRAMEPROC.cbSaveRegs);
}
else if (kind == SymbolRecordKind::S_ANNOTATION)
{
Printf(blockLevel, "S_ANNOTATION: Offset 0x%X | Count %u\n", data.S_ANNOTATIONSYM.offset, data.S_ANNOTATIONSYM.annotationsCount);
// print N null-terminated annotation strings, skipping their null-terminators to get to the next string
const char* annotation = data.S_ANNOTATIONSYM.annotations;
for (int i = 0; i < data.S_ANNOTATIONSYM.annotationsCount; ++i, annotation += strlen(annotation) + 1)
Printf(blockLevel + 1, "S_ANNOTATION.%u: %s\n", i, annotation);
PDB_ASSERT(annotation <= (const char*)record + record->header.size + sizeof(record->header.size),
"Annotation strings end beyond the record size %X; annotaions count: %u", record->header.size, data.S_ANNOTATIONSYM.annotationsCount);
}
else if (kind == SymbolRecordKind::S_THUNK32)
{
PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
if (data.S_THUNK32.thunk == PDB::CodeView::DBI::ThunkOrdinal::TrampolineIncremental)
{
// we have never seen incremental linking thunks stored inside a S_THUNK32 symbol, but better safe than sorry
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_THUNK32.section, data.S_THUNK32.offset);
Printf(blockLevel, "Function: 'ILT/Thunk' | RVA 0x%X\n", rva);
}
else
{
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_THUNK32.section, data.S_THUNK32.offset);
Printf(blockLevel, "S_THUNK32 Function '%s' | RVA 0x%X\n", data.S_THUNK32.name, rva);
blockLevel++;
}
}
else if (kind == SymbolRecordKind::S_TRAMPOLINE)
{
PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
// incremental linking thunks are stored in the linker module
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_TRAMPOLINE.thunkSection, data.S_TRAMPOLINE.thunkOffset);
Printf(blockLevel, "Function 'ILT/Trampoline' | RVA 0x%X\n", rva);
}
else if (kind == SymbolRecordKind::S_LPROC32)
{
PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_LPROC32.section, data.S_LPROC32.offset);
Printf(blockLevel, "S_LPROC32 Function '%s' | RVA 0x%X\n", data.S_LPROC32.name, rva);
blockLevel++;
}
else if (kind == SymbolRecordKind::S_GPROC32)
{
PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_GPROC32.section, data.S_GPROC32.offset);
Printf(blockLevel, "S_GPROC32 Function '%s' | RVA 0x%X\n", data.S_GPROC32.name, rva);
blockLevel++;
}
else if (kind == SymbolRecordKind::S_LPROC32_ID)
{
PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_LPROC32_ID.section, data.S_LPROC32_ID.offset);
Printf(blockLevel, "S_LPROC32_ID Function '%s' | RVA 0x%X\n", data.S_LPROC32_ID.name, rva);
blockLevel++;
}
else if (kind == SymbolRecordKind::S_GPROC32_ID)
{
PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_GPROC32_ID.section, data.S_GPROC32_ID.offset);
Printf(blockLevel, "S_GPROC32_ID Function '%s' | RVA 0x%X\n", data.S_GPROC32_ID.name, rva);
blockLevel++;
}
else if (kind == SymbolRecordKind::S_REGREL32_INDIR)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_REGREL32_INDIR.typeIndex);
Printf(blockLevel, "S_REGREL32_INDIR: '%s' -> '%s' | Register %i | Unknown1 0x%X | Unknown2 0x%X\n",
data.S_REGREL32_INDIR.name, typeName.c_str(),
data.S_REGREL32_INDIR.unknown1,
data.S_REGREL32_INDIR.unknown1);
}
else if (kind == SymbolRecordKind::S_REGREL32_ENCTMP)
{
const std::string typeName = GetVariableTypeName(typeTable, data.S_REGREL32.typeIndex);
Printf(blockLevel, "S_REGREL32_ENCTMP: '%s' -> '%s' | Register %i | Register Offset 0x%X\n",
data.S_REGREL32.name, typeName.c_str(),
data.S_REGREL32.reg,
data.S_REGREL32.offset);
}
else if (kind == SymbolRecordKind::S_UNAMESPACE)
{
Printf(blockLevel, "S_UNAMESPACE: '%s'\n", data.S_UNAMESPACE.name);
}
else if (kind == SymbolRecordKind::S_ARMSWITCHTABLE)
{
Printf(blockLevel, "S_ARMSWITCHTABLE: "
"Switch Type: %u | Num Entries: %u | Base Section: %u | Base Offset: 0x%X | "
"Branch Section: %u | Branch Offset: 0x%X | Table Section: %u | Table Offset: 0x%X\n",
data.S_ARMSWITCHTABLE.switchType,
data.S_ARMSWITCHTABLE.numEntries,
data.S_ARMSWITCHTABLE.sectionBase,
data.S_ARMSWITCHTABLE.offsetBase,
data.S_ARMSWITCHTABLE.sectionBranch,
data.S_ARMSWITCHTABLE.offsetBranch,
data.S_ARMSWITCHTABLE.sectionTable,
data.S_ARMSWITCHTABLE.offsetTable);
}
else
{
// We only care about records inside functions.
if (blockLevel > 0)
{
PDB_ASSERT(false, "Unhandled record kind 0x%X with block level %u\n", static_cast<uint16_t>(kind), blockLevel);
}
}
recordCount++;
});
}
scope.Done(recordCount);
}
}

View File

@@ -0,0 +1,198 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"
#include "ExampleTimedScope.h"
#include "ExampleTypeTable.h"
#include "PDB_RawFile.h"
#include "PDB_InfoStream.h"
#include "PDB_IPIStream.h"
#include "PDB_TPIStream.h"
static std::string GetTypeNameIPI(const TypeTable& typeTable, uint32_t typeIndex)
{
// Defined in ExampleTypes.cpp
extern std::string GetTypeName(const TypeTable & typeTable, uint32_t typeIndex);
std::string typeName = GetTypeName(typeTable, typeIndex);
// Remove any '%s' substring used to insert a variable/field name.
const uint64_t markerPos = typeName.find("%s");
if (markerPos != typeName.npos)
{
typeName.erase(markerPos, 2);
}
return typeName;
}
void ExampleIPI(const PDB::RawFile& rawPdbFile, const PDB::InfoStream& infoStream, const PDB::TPIStream& tpiStream, const PDB::IPIStream& ipiStream);
void ExampleIPI(const PDB::RawFile& rawPdbFile, const PDB::InfoStream& infoStream, const PDB::TPIStream& tpiStream, const PDB::IPIStream& ipiStream)
{
if (!infoStream.HasIPIStream())
{
return;
}
TimedScope total("\nRunning example \"IPI\"");
TimedScope typeTableScope("Create TypeTable");
TypeTable typeTable(tpiStream);
typeTableScope.Done();
// prepare names stream for grabbing file paths from lines
TimedScope namesScope("Reading names stream");
const PDB::NamesStream namesStream = infoStream.CreateNamesStream(rawPdbFile);
namesScope.Done();
const uint32_t firstTypeIndex = ipiStream.GetFirstTypeIndex();
PDB::ArrayView<const PDB::CodeView::IPI::Record*> records = ipiStream.GetTypeRecords();
std::vector<const char*> strings;
strings.resize(records.GetLength(), nullptr);
size_t index = 0;
for (const PDB::CodeView::IPI::Record* record : records)
{
const PDB::CodeView::IPI::RecordHeader& header = record->header;
if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_STRING_ID)
{
strings[index] = record->data.LF_STRING_ID.name;
}
index++;
}
uint32_t identifier = firstTypeIndex;
std::string typeName, parentTypeName;
printf("\n --- IPI Records ---\n\n");
for(const PDB::CodeView::IPI::Record* record : records)
{
const PDB::CodeView::IPI::RecordHeader& header = record->header;
if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_FUNC_ID)
{
typeName = GetTypeNameIPI(typeTable, record->data.LF_FUNC_ID.typeIndex);
printf("Kind: 'LF_FUNC_ID' Size: %i ID: %u\n", header.size, identifier);
printf(" Scope ID: %u\n Type: '%s'\n Name: '%s'\n\n",
record->data.LF_FUNC_ID.scopeId,
typeName.c_str(),
record->data.LF_FUNC_ID.name);
}
else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_MFUNC_ID)
{
typeName = GetTypeNameIPI(typeTable, record->data.LF_MFUNC_ID.typeIndex);
parentTypeName = GetTypeNameIPI(typeTable, record->data.LF_MFUNC_ID.parentTypeIndex);
printf("Kind: 'LF_MFUNC_ID' Size: %i ID: %u\n", header.size, identifier);
printf(" Parent Type: '%s'\n Type: '%s'\n Name: '%s'\n\n",
parentTypeName.c_str(),
typeName.c_str(),
record->data.LF_MFUNC_ID.name);
}
else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_BUILDINFO)
{
printf("Kind: 'LF_BUILDINFO' Size: %u ID: %u\n", header.size, identifier);
if (record->data.LF_BUILDINFO.count == 0)
{
continue;
}
printf("Strings: '%s'", strings[record->data.LF_BUILDINFO.typeIndices[0] - firstTypeIndex]);
for (uint32_t i = 1, size = record->data.LF_BUILDINFO.count; i < size; ++i)
{
const uint32_t stringIndex = record->data.LF_BUILDINFO.typeIndices[i];
if (stringIndex == 0)
{
printf(", ''");
}
else
{
printf(", '%s'", strings[stringIndex - firstTypeIndex]);
}
}
printf("\n\n");
}
else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_SUBSTR_LIST)
{
printf("Kind: 'LF_SUBSTR_LIST' Size: %u ID: %u\n", header.size, identifier);
if (record->data.LF_SUBSTR_LIST.count == 0)
{
continue;
}
printf(" Strings: '%s'", strings[record->data.LF_SUBSTR_LIST.typeIndices[0] - firstTypeIndex]);
for (uint32_t i = 1, size = record->data.LF_SUBSTR_LIST.count; i < size; ++i)
{
const uint32_t stringIndex = record->data.LF_SUBSTR_LIST.typeIndices[i];
if (stringIndex == 0)
{
printf(", ''");
}
else
{
printf(", '%s'", strings[stringIndex - firstTypeIndex]);
}
}
printf("\n\n");
}
else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_STRING_ID)
{
printf("Kind: 'LF_STRING_ID' Size: %u ID: %u\n", header.size, identifier);
printf(" Substring ID: %u\n Name: '%s'\n\n", record->data.LF_STRING_ID.id, record->data.LF_STRING_ID.name);
}
else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_UDT_SRC_LINE)
{
typeName = GetTypeNameIPI(typeTable, record->data.LF_UDT_SRC_LINE.typeIndex);
const uint32_t stringIndex = record->data.LF_UDT_SRC_LINE.stringIndex;
printf("Kind: 'LF_UDT_SRC_LINE' Size: %u ID: %u\n", header.size, identifier);
printf(" Type: '%s'\n Source Path: %s\n Line: %u\n\n",
typeName.c_str(),
strings[stringIndex - firstTypeIndex],
record->data.LF_UDT_SRC_LINE.line);
}
else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_UDT_MOD_SRC_LINE)
{
typeName = GetTypeNameIPI(typeTable, record->data.LF_UDT_MOD_SRC_LINE.typeIndex);
const char* string = namesStream.GetFilename(record->data.LF_UDT_MOD_SRC_LINE.stringIndex);
printf("Kind: 'LF_UDT_SRC_LINE' Size: %u ID: %u\n", header.size, identifier);
printf(" Type: '%s'\n Source Path: %s\n Line: %u\n Module Index: %u\n\n",
typeName.c_str(),
string,
record->data.LF_UDT_MOD_SRC_LINE.line,
record->data.LF_UDT_MOD_SRC_LINE.moduleIndex);
}
else
{
printf("Kind: 0x%X Size: %u ID: %u\n\n", static_cast<uint32_t>(header.kind), header.size, identifier);
}
identifier++;
}
}

View File

@@ -0,0 +1,268 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"
#include "ExampleTimedScope.h"
#include "Foundation/PDB_PointerUtil.h"
#include "PDB_RawFile.h"
#include "PDB_DBIStream.h"
#include "PDB_InfoStream.h"
#include <cstring>
namespace
{
struct Section
{
uint16_t index;
uint32_t offset;
size_t lineIndex;
};
struct Filename
{
uint32_t fileChecksumOffset;
uint32_t namesFilenameOffset;
PDB::CodeView::DBI::ChecksumKind checksumKind;
uint8_t checksumSize;
uint8_t checksum[32];
};
struct Line
{
uint32_t lineNumber;
uint32_t codeSize;
size_t filenameIndex;
};
}
void ExampleLines(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::InfoStream& infoStream);
void ExampleLines(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::InfoStream& infoStream)
{
if (!infoStream.HasNamesStream())
{
printf("PDB has no '/names' stream for looking up filenames for lines, skipping \"Lines\" example.");
return;
}
TimedScope total("\nRunning example \"Lines\"");
// prepare the image section stream first. it is needed for converting section + offset into an RVA
TimedScope sectionScope("Reading image section stream");
const PDB::ImageSectionStream imageSectionStream = dbiStream.CreateImageSectionStream(rawPdbFile);
sectionScope.Done();
// prepare the module info stream for grabbing function symbols from modules
TimedScope moduleScope("Reading module info stream");
const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
moduleScope.Done();
// prepare names stream for grabbing file paths from lines
TimedScope namesScope("Reading names stream");
const PDB::NamesStream namesStream = infoStream.CreateNamesStream(rawPdbFile);
namesScope.Done();
// keeping sections and lines separate, as sorting the smaller Section struct is 2x faster in release builds
// than having all the fields in one big Line struct and sorting those.
std::vector<Section> sections;
std::vector<Filename> filenames;
std::vector<Line> lines;
{
TimedScope scope("Storing lines from modules");
const PDB::ArrayView<PDB::ModuleInfoStream::Module> modules = moduleInfoStream.GetModules();
for (const PDB::ModuleInfoStream::Module& module : modules)
{
if (!module.HasLineStream())
{
continue;
}
const PDB::ModuleLineStream moduleLineStream = module.CreateLineStream(rawPdbFile);
const size_t moduleFilenamesStartIndex = filenames.size();
const PDB::CodeView::DBI::FileChecksumHeader* moduleFileChecksumHeader = nullptr;
moduleLineStream.ForEachSection([&moduleLineStream, &namesStream, &moduleFileChecksumHeader, &sections, &filenames, &lines](const PDB::CodeView::DBI::LineSection* lineSection)
{
if (lineSection->header.kind == PDB::CodeView::DBI::DebugSubsectionKind::S_LINES)
{
moduleLineStream.ForEachLinesBlock(lineSection,
[&lineSection, &sections, &filenames, &lines](const PDB::CodeView::DBI::LinesFileBlockHeader* linesBlockHeader, const PDB::CodeView::DBI::Line* blocklines, const PDB::CodeView::DBI::Column* blockColumns)
{
if (linesBlockHeader->numLines == 0)
{
return;
}
const PDB::CodeView::DBI::Line& firstLine = blocklines[0];
const uint16_t sectionIndex = lineSection->linesHeader.sectionIndex;
const uint32_t sectionOffset = lineSection->linesHeader.sectionOffset;
const uint32_t fileChecksumOffset = linesBlockHeader->fileChecksumOffset;
const size_t filenameIndex = filenames.size();
// there will be duplicate filenames for any real world pdb.
// ideally the filenames would be stored in a map with the filename or checksum as the key.
// but that would complicate the logic in this example and therefore just use a vector to make it easier to understand.
filenames.push_back({ fileChecksumOffset, 0, PDB::CodeView::DBI::ChecksumKind::None, 0, {0} });
sections.push_back({ sectionIndex, sectionOffset, lines.size() });
// initially set code size of first line to 0, will be updated in loop below.
lines.push_back({ firstLine.linenumStart, 0, filenameIndex });
for(uint32_t i = 1, size = linesBlockHeader->numLines; i < size; ++i)
{
const PDB::CodeView::DBI::Line& line = blocklines[i];
// calculate code size of previous line by using the current line offset.
lines.back().codeSize = line.offset - blocklines[i-1].offset;
sections.push_back({ sectionIndex, sectionOffset + line.offset, lines.size() });
lines.push_back({ line.linenumStart, 0, filenameIndex });
}
// calc code size of last line
lines.back().codeSize = lineSection->linesHeader.codeSize - blocklines[linesBlockHeader->numLines-1].offset;
// columns are optional
if (blockColumns == nullptr)
{
return;
}
for (uint32_t i = 0, size = linesBlockHeader->numLines; i < size; ++i)
{
const PDB::CodeView::DBI::Column& column = blockColumns[i];
(void)column;
}
});
}
else if (lineSection->header.kind == PDB::CodeView::DBI::DebugSubsectionKind::S_FILECHECKSUMS)
{
// how to read checksums and their filenames from the Names Stream
moduleLineStream.ForEachFileChecksum(lineSection, [&namesStream](const PDB::CodeView::DBI::FileChecksumHeader* fileChecksumHeader)
{
const char* filename = namesStream.GetFilename(fileChecksumHeader->filenameOffset);
(void)filename;
});
// store the checksum header for the module, as there might be more lines after the checksums.
// so lines will get their checksum header values assigned after processing all line sections in the module.
PDB_ASSERT(moduleFileChecksumHeader == nullptr, "Module File Checksum Header already set");
moduleFileChecksumHeader = &lineSection->checksumHeader;
}
else if (lineSection->header.kind == PDB::CodeView::DBI::DebugSubsectionKind::S_INLINEELINES)
{
if (lineSection->inlineeHeader.kind == PDB::CodeView::DBI::InlineeSourceLineKind::Signature)
{
moduleLineStream.ForEachInlineeSourceLine(lineSection, [](const PDB::CodeView::DBI::InlineeSourceLine* inlineeSourceLine)
{
(void)inlineeSourceLine;
});
}
else
{
moduleLineStream.ForEachInlineeSourceLineEx(lineSection, [](const PDB::CodeView::DBI::InlineeSourceLineEx* inlineeSourceLineEx)
{
for (uint32_t i = 0; i < inlineeSourceLineEx->extraLines; ++i)
{
const uint32_t checksumOffset = inlineeSourceLineEx->extrafileChecksumOffsets[i];
(void)checksumOffset;
}
});
}
}
else
{
PDB_ASSERT(false, "Line Section kind 0x%X not handled", static_cast<uint32_t>(lineSection->header.kind));
}
});
// assign checksum values for each filename added in this module
for (size_t i = moduleFilenamesStartIndex, size = filenames.size(); i < size; ++i)
{
Filename& filename = filenames[i];
// look up the filename's checksum header in the module's checksums section
const PDB::CodeView::DBI::FileChecksumHeader* checksumHeader = PDB::Pointer::Offset<const PDB::CodeView::DBI::FileChecksumHeader*>(moduleFileChecksumHeader, filename.fileChecksumOffset);
PDB_ASSERT(checksumHeader->checksumKind >= PDB::CodeView::DBI::ChecksumKind::None &&
checksumHeader->checksumKind <= PDB::CodeView::DBI::ChecksumKind::SHA256,
"Invalid checksum kind %u", static_cast<uint16_t>(checksumHeader->checksumKind));
// store checksum values in filname struct
filename.namesFilenameOffset = checksumHeader->filenameOffset;
filename.checksumKind = checksumHeader->checksumKind;
filename.checksumSize = checksumHeader->checksumSize;
std::memcpy(filename.checksum, checksumHeader->checksum, checksumHeader->checksumSize);
}
}
scope.Done(modules.GetLength());
TimedScope sortScope("std::sort sections");
// sort sections, so we can iterate over lines by address order.
std::sort(sections.begin(), sections.end(), [](const Section& lhs, const Section& rhs)
{
if (lhs.index == rhs.index)
{
return lhs.offset < rhs.offset;
}
return lhs.index < rhs.index;
});
sortScope.Done(sections.size());
// Disabled by default, as it will print a lot of lines for large PDBs :-)
#if 0
// DIA2Dump style lines output
static const char hexChars[17] = "0123456789ABCDEF";
char checksumString[128];
printf("*** LINES RAW PDB\n");
const char* prevFilename = nullptr;
for (const Section& section : sections)
{
const Line& line = lines[section.lineIndex];
const Filename& lineFilename = filenames[line.filenameIndex];
const char* filename = namesStream.GetFilename(lineFilename.namesFilenameOffset);
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(section.index, section.offset);
// only print filename for a line if it is different from the previous one.
if (filename != prevFilename)
{
for (size_t i = 0, j = 0; i < lineFilename.checksumSize; i++, j+=2)
{
checksumString[j] = hexChars[lineFilename.checksum[i] >> 4];
checksumString[j+1] = hexChars[lineFilename.checksum[i] & 0xF];
}
checksumString[lineFilename.checksumSize * 2] = '\0';
printf(" line %u at [0x%08X][0x%04X:0x%08X], len = 0x%X %s (0x%02X: %s)\n",
line.lineNumber, rva, section.index, section.offset, line.codeSize,
filename, static_cast<uint32_t>(lineFilename.checksumKind), checksumString);
prevFilename = filename;
}
else
{
printf(" line %u at [0x%08X][0x%04X:0x%08X], len = 0x%X\n",
line.lineNumber, rva, section.index, section.offset, line.codeSize);
}
}
#endif
}
}

View File

@@ -0,0 +1,200 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"
#include "ExampleMemoryMappedFile.h"
#include "PDB.h"
#include "PDB_RawFile.h"
#include "PDB_InfoStream.h"
#include "PDB_DBIStream.h"
#include "PDB_TPIStream.h"
#include "PDB_IPIStream.h"
#include "PDB_NamesStream.h"
namespace
{
PDB_NO_DISCARD static bool IsError(PDB::ErrorCode errorCode)
{
switch (errorCode)
{
case PDB::ErrorCode::Success:
return false;
case PDB::ErrorCode::InvalidSuperBlock:
printf("Invalid Superblock\n");
return true;
case PDB::ErrorCode::InvalidFreeBlockMap:
printf("Invalid free block map\n");
return true;
case PDB::ErrorCode::InvalidStream:
printf("Invalid stream\n");
return true;
case PDB::ErrorCode::InvalidSignature:
printf("Invalid stream signature\n");
return true;
case PDB::ErrorCode::InvalidStreamIndex:
printf("Invalid stream index\n");
return true;
case PDB::ErrorCode::InvalidDataSize:
printf("Invalid data size\n");
return true;
case PDB::ErrorCode::UnknownVersion:
printf("Unknown version\n");
return true;
}
// only ErrorCode::Success means there wasn't an error, so all other paths have to assume there was an error
return true;
}
PDB_NO_DISCARD static bool HasValidDBIStreams(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream)
{
// check whether the DBI stream offers all sub-streams we need
if (IsError(dbiStream.HasValidSymbolRecordStream(rawPdbFile)))
{
return false;
}
if (IsError(dbiStream.HasValidPublicSymbolStream(rawPdbFile)))
{
return false;
}
if (IsError(dbiStream.HasValidGlobalSymbolStream(rawPdbFile)))
{
return false;
}
if (IsError(dbiStream.HasValidSectionContributionStream(rawPdbFile)))
{
return false;
}
if (IsError(dbiStream.HasValidImageSectionStream(rawPdbFile)))
{
return false;
}
return true;
}
}
// declare all examples
extern void ExamplePDBSize(const PDB::RawFile&, const PDB::DBIStream&);
extern void ExampleTPISize(const PDB::TPIStream& tpiStream, const char* outPath);
extern void ExampleContributions(const PDB::RawFile&, const PDB::DBIStream&);
extern void ExampleSymbols(const PDB::RawFile&, const PDB::DBIStream&);
extern void ExampleFunctionSymbols(const PDB::RawFile&, const PDB::DBIStream&);
extern void ExampleFunctionVariables(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::TPIStream&);
extern void ExampleLines(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::InfoStream& infoStream);
extern void ExampleTypes(const PDB::TPIStream&);
extern void ExampleIPI(const PDB::RawFile& rawPdbFile, const PDB::InfoStream& infoStream, const PDB::TPIStream& tpiStream, const PDB::IPIStream& ipiStream);
int main(int argc, char** argv)
{
if (argc != 2)
{
printf("Usage: Examples <PDB path>\nError: Incorrect usage\n");
return 1;
}
printf("Opening PDB file %s\n", argv[1]);
// try to open the PDB file and check whether all the data we need is available
MemoryMappedFile::Handle pdbFile = MemoryMappedFile::Open(argv[1]);
if (!pdbFile.baseAddress)
{
printf("Cannot memory-map file %s\n", argv[1]);
return 1;
}
if (IsError(PDB::ValidateFile(pdbFile.baseAddress, pdbFile.len)))
{
MemoryMappedFile::Close(pdbFile);
return 2;
}
const PDB::RawFile rawPdbFile = PDB::CreateRawFile(pdbFile.baseAddress);
if (IsError(PDB::HasValidDBIStream(rawPdbFile)))
{
MemoryMappedFile::Close(pdbFile);
return 3;
}
const PDB::InfoStream infoStream(rawPdbFile);
if (infoStream.UsesDebugFastLink())
{
printf("PDB was linked using unsupported option /DEBUG:FASTLINK\n");
MemoryMappedFile::Close(pdbFile);
return 4;
}
const auto h = infoStream.GetHeader();
printf("Version %u, signature %u, age %u, GUID %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x\n",
static_cast<uint32_t>(h->version), h->signature, h->age,
h->guid.Data1, h->guid.Data2, h->guid.Data3,
h->guid.Data4[0], h->guid.Data4[1], h->guid.Data4[2], h->guid.Data4[3], h->guid.Data4[4], h->guid.Data4[5], h->guid.Data4[6], h->guid.Data4[7]);
const PDB::DBIStream dbiStream = PDB::CreateDBIStream(rawPdbFile);
if (!HasValidDBIStreams(rawPdbFile, dbiStream))
{
MemoryMappedFile::Close(pdbFile);
return 5;
}
if (IsError(PDB::HasValidTPIStream(rawPdbFile)))
{
MemoryMappedFile::Close(pdbFile);
return 5;
}
const PDB::TPIStream tpiStream = PDB::CreateTPIStream(rawPdbFile);
PDB::IPIStream ipiStream;
// It's perfectly possible that an old PDB does not have an IPI stream.
if(infoStream.HasIPIStream())
{
PDB::ErrorCode error = PDB::HasValidIPIStream(rawPdbFile);
if (error != PDB::ErrorCode::InvalidStream && IsError(error))
{
MemoryMappedFile::Close(pdbFile);
return 5;
}
ipiStream = PDB::CreateIPIStream(rawPdbFile);
}
// run all examples
ExamplePDBSize(rawPdbFile, dbiStream);
ExampleContributions(rawPdbFile, dbiStream);
ExampleSymbols(rawPdbFile, dbiStream);
ExampleFunctionSymbols(rawPdbFile, dbiStream);
ExampleFunctionVariables(rawPdbFile, dbiStream, tpiStream);
ExampleLines(rawPdbFile, dbiStream, infoStream);
ExampleTypes(tpiStream);
ExampleIPI(rawPdbFile, infoStream, tpiStream, ipiStream);
// uncomment to dump type sizes to a CSV
// ExampleTPISize(tpiStream, "output.csv");
MemoryMappedFile::Close(pdbFile);
return 0;
}

View File

@@ -0,0 +1,100 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"
#include "ExampleMemoryMappedFile.h"
MemoryMappedFile::Handle MemoryMappedFile::Open(const char* path)
{
#ifdef _WIN32
void* file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, nullptr);
if (file == INVALID_HANDLE_VALUE)
{
return Handle { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, nullptr, 0 };
}
void* fileMapping = CreateFileMappingW(file, nullptr, PAGE_READONLY, 0, 0, nullptr);
if (fileMapping == nullptr)
{
CloseHandle(file);
return Handle { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, nullptr, 0 };
}
void* baseAddress = MapViewOfFile(fileMapping, FILE_MAP_READ, 0, 0, 0);
if (baseAddress == nullptr)
{
CloseHandle(fileMapping);
CloseHandle(file);
return Handle { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, nullptr, 0 };
}
BY_HANDLE_FILE_INFORMATION fileInformation;
const bool getInformationResult = GetFileInformationByHandle(file, &fileInformation);
if (!getInformationResult)
{
UnmapViewOfFile(baseAddress);
CloseHandle(fileMapping);
CloseHandle(file);
return Handle { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, nullptr, 0 };
}
const size_t fileSizeHighBytes = static_cast<size_t>(fileInformation.nFileSizeHigh) << 32;
const size_t fileSizeLowBytes = fileInformation.nFileSizeLow;
const size_t fileSize = fileSizeHighBytes | fileSizeLowBytes;
return Handle { file, fileMapping, baseAddress, fileSize };
#else
struct stat fileSb;
int file = open(path, O_RDONLY);
if (file == INVALID_HANDLE_VALUE)
{
return Handle { INVALID_HANDLE_VALUE, nullptr, 0 };
}
if (fstat(file, &fileSb) == -1)
{
close(file);
return Handle { INVALID_HANDLE_VALUE, nullptr, 0 };
}
void* baseAddress = mmap(nullptr, fileSb.st_size, PROT_READ, MAP_PRIVATE, file, 0);
if (baseAddress == MAP_FAILED)
{
close(file);
return Handle { INVALID_HANDLE_VALUE, nullptr, 0 };
}
return Handle { file, baseAddress, static_cast<size_t>(fileSb.st_size) };
#endif
}
void MemoryMappedFile::Close(Handle& handle)
{
#ifdef _WIN32
UnmapViewOfFile(handle.baseAddress);
CloseHandle(handle.fileMapping);
CloseHandle(handle.file);
handle.file = nullptr;
handle.fileMapping = nullptr;
#else
munmap(handle.baseAddress, handle.len);
close(handle.file);
handle.file = 0;
#endif
handle.baseAddress = nullptr;
}

View File

@@ -0,0 +1,29 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#ifndef _WIN32
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define INVALID_HANDLE_VALUE ((long)-1)
#endif
namespace MemoryMappedFile
{
struct Handle
{
#ifdef _WIN32
void* file;
void* fileMapping;
#else
int file;
#endif
void* baseAddress;
size_t len;
};
Handle Open(const char* path);
void Close(Handle& handle);
}

View File

@@ -0,0 +1,124 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"
#include "ExampleTimedScope.h"
#include "PDB_RawFile.h"
#include "PDB_DBIStream.h"
namespace
{
struct Stream
{
std::string name;
uint32_t size;
};
}
void ExamplePDBSize(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream);
void ExamplePDBSize(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream)
{
TimedScope total("\nRunning example \"PDBSize\"");
std::vector<Stream> streams;
// print show general statistics
printf("General\n");
printf("-------\n");
{
const PDB::SuperBlock* superBlock = rawPdbFile.GetSuperBlock();
printf("PDB page size (block size): %u\n", superBlock->blockSize);
printf("PDB block count: %u\n", superBlock->blockCount);
const size_t rawSize = static_cast<size_t>(superBlock->blockSize) * static_cast<size_t>(superBlock->blockCount);
printf("PDB raw size: %zu MiB (%zu GiB)\n", rawSize >> 20u, rawSize >> 30u);
}
// print the sizes of all known streams
printf("\n");
printf("Sizes of known streams\n");
printf("----------------------\n");
{
const uint32_t streamCount = rawPdbFile.GetStreamCount();
const uint32_t tpiStreamSize = (streamCount > 2u) ? rawPdbFile.GetStreamSize(2u) : 0u;
const uint32_t dbiStreamSize = (streamCount > 3u) ? rawPdbFile.GetStreamSize(3u) : 0u;
const uint32_t ipiStreamSize = (streamCount > 4u) ? rawPdbFile.GetStreamSize(4u) : 0u;
printf("TPI stream size: %u KiB (%u MiB)\n", tpiStreamSize >> 10u, tpiStreamSize >> 20u);
printf("DBI stream size: %u KiB (%u MiB)\n", dbiStreamSize >> 10u, dbiStreamSize >> 20u);
printf("IPI stream size: %u KiB (%u MiB)\n", ipiStreamSize >> 10u, ipiStreamSize >> 20u);
streams.push_back(Stream { "TPI", tpiStreamSize });
streams.push_back(Stream { "DBI", dbiStreamSize });
streams.push_back(Stream { "IPI", ipiStreamSize });
const uint32_t globalSymbolStreamSize = rawPdbFile.GetStreamSize(dbiStream.GetHeader().globalStreamIndex);
const uint32_t publicSymbolStreamSize = rawPdbFile.GetStreamSize(dbiStream.GetHeader().publicStreamIndex);
const uint32_t symbolRecordStreamSize = rawPdbFile.GetStreamSize(dbiStream.GetHeader().symbolRecordStreamIndex);
printf("Global symbol stream size: %u KiB (%u MiB)\n", globalSymbolStreamSize >> 10u, globalSymbolStreamSize >> 20u);
printf("Public symbol stream size: %u KiB (%u MiB)\n", publicSymbolStreamSize >> 10u, publicSymbolStreamSize >> 20u);
printf("Symbol record stream size: %u KiB (%u MiB)\n", symbolRecordStreamSize >> 10u, symbolRecordStreamSize >> 20u);
streams.emplace_back(Stream { "Global", globalSymbolStreamSize });
streams.emplace_back(Stream { "Public", publicSymbolStreamSize });
streams.emplace_back(Stream { "Symbol", symbolRecordStreamSize });
}
// print the sizes of all module streams
printf("\n");
printf("Sizes of module streams\n");
printf("-----------------------\n");
{
const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
const PDB::ArrayView<PDB::ModuleInfoStream::Module> modules = moduleInfoStream.GetModules();
for (const PDB::ModuleInfoStream::Module& module : modules)
{
const PDB::DBI::ModuleInfo* moduleInfo = module.GetInfo();
const char* name = module.GetName().Decay();
const char* objectName = module.GetObjectName().Decay();
const uint16_t streamIndex = module.HasSymbolStream() ? moduleInfo->moduleSymbolStreamIndex : 0u;
const uint32_t moduleStreamSize = (streamIndex != 0u) ? rawPdbFile.GetStreamSize(streamIndex) : 0u;
printf("Module %s (%s) stream size: %u KiB (%u MiB)\n", name, objectName, moduleStreamSize >> 10u, moduleStreamSize >> 20u);
streams.push_back(Stream { name, moduleStreamSize });
}
}
// sort the streams by their size
std::sort(streams.begin(), streams.end(), [](const Stream& lhs, const Stream& rhs)
{
return lhs.size > rhs.size;
});
// log the 20 largest stream
{
printf("\n");
printf("Sizes of 20 largest streams:\n");
const size_t countToShow = std::min<size_t>(20ul, streams.size());
for (size_t i = 0u; i < countToShow; ++i)
{
const Stream& stream = streams[i];
printf("%zu: %u KiB (%u MiB) from stream %s\n", i + 1u, stream.size >> 10u, stream.size >> 20u, stream.name.c_str());
}
}
// print the raw stream sizes
printf("\n");
printf("Raw sizes of all streams\n");
printf("------------------------\n");
{
const uint32_t streamCount = rawPdbFile.GetStreamCount();
for (uint32_t i = 0u; i < streamCount; ++i)
{
const uint32_t streamSize = rawPdbFile.GetStreamSize(i);
printf("Stream %u size: %u KiB (%u MiB)\n", i, streamSize >> 10u, streamSize >> 20u);
}
}
}

View File

@@ -0,0 +1,238 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"
#include "ExampleTimedScope.h"
#include "PDB_RawFile.h"
#include "PDB_DBIStream.h"
namespace
{
// we don't have to store std::string in the symbols, since all the data is memory-mapped anyway.
// we do it in this example to ensure that we don't "cheat" when reading the PDB file. memory-mapped data will only
// be faulted into the process once it's touched, so actually copying the string data makes us touch the needed data,
// giving us a real performance measurement.
struct Symbol
{
std::string name;
uint32_t rva;
};
}
void ExampleSymbols(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream);
void ExampleSymbols(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream)
{
TimedScope total("\nRunning example \"Symbols\"");
// in order to keep the example easy to understand, we load the PDB data serially.
// note that this can be improved a lot by reading streams concurrently.
// prepare the image section stream first. it is needed for converting section + offset into an RVA
TimedScope sectionScope("Reading image section stream");
const PDB::ImageSectionStream imageSectionStream = dbiStream.CreateImageSectionStream(rawPdbFile);
sectionScope.Done();
// prepare the module info stream for matching contributions against files
TimedScope moduleScope("Reading module info stream");
const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
moduleScope.Done();
// prepare symbol record stream needed by both public and global streams
TimedScope symbolStreamScope("Reading symbol record stream");
const PDB::CoalescedMSFStream symbolRecordStream = dbiStream.CreateSymbolRecordStream(rawPdbFile);
symbolStreamScope.Done();
std::vector<Symbol> symbols;
// read public symbols
TimedScope publicScope("Reading public symbol stream");
const PDB::PublicSymbolStream publicSymbolStream = dbiStream.CreatePublicSymbolStream(rawPdbFile);
publicScope.Done();
{
TimedScope scope("Storing public symbols");
const PDB::ArrayView<PDB::HashRecord> hashRecords = publicSymbolStream.GetRecords();
const size_t count = hashRecords.GetLength();
symbols.reserve(count);
for (const PDB::HashRecord& hashRecord : hashRecords)
{
const PDB::CodeView::DBI::Record* record = publicSymbolStream.GetRecord(symbolRecordStream, hashRecord);
if (record->header.kind != PDB::CodeView::DBI::SymbolRecordKind::S_PUB32)
{
// normally, a PDB only contains S_PUB32 symbols in the public symbol stream, but we have seen PDBs that also store S_CONSTANT as public symbols.
// ignore these.
continue;
}
const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_PUB32.section, record->data.S_PUB32.offset);
if (rva == 0u)
{
// certain symbols (e.g. control-flow guard symbols) don't have a valid RVA, ignore those
continue;
}
symbols.push_back(Symbol { record->data.S_PUB32.name, rva });
}
scope.Done(count);
}
// read global symbols
TimedScope globalScope("Reading global symbol stream");
const PDB::GlobalSymbolStream globalSymbolStream = dbiStream.CreateGlobalSymbolStream(rawPdbFile);
globalScope.Done();
{
TimedScope scope("Storing global symbols");
const PDB::ArrayView<PDB::HashRecord> hashRecords = globalSymbolStream.GetRecords();
const size_t count = hashRecords.GetLength();
symbols.reserve(symbols.size() + count);
for (const PDB::HashRecord& hashRecord : hashRecords)
{
const PDB::CodeView::DBI::Record* record = globalSymbolStream.GetRecord(symbolRecordStream, hashRecord);
const char* name = nullptr;
uint32_t rva = 0u;
if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GDATA32)
{
name = record->data.S_GDATA32.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GDATA32.section, record->data.S_GDATA32.offset);
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GTHREAD32)
{
name = record->data.S_GTHREAD32.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GTHREAD32.section, record->data.S_GTHREAD32.offset);
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LDATA32)
{
name = record->data.S_LDATA32.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LDATA32.section, record->data.S_LDATA32.offset);
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LTHREAD32)
{
name = record->data.S_LTHREAD32.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LTHREAD32.section, record->data.S_LTHREAD32.offset);
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_UDT)
{
name = record->data.S_UDT.name;
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_UDT_ST)
{
name = record->data.S_UDT_ST.name;
}
if (rva == 0u)
{
// certain symbols (e.g. control-flow guard symbols) don't have a valid RVA, ignore those
continue;
}
symbols.push_back(Symbol { name, rva });
}
scope.Done(count);
}
// read module symbols
{
TimedScope scope("Storing symbols from modules");
const PDB::ArrayView<PDB::ModuleInfoStream::Module> modules = moduleInfoStream.GetModules();
for (const PDB::ModuleInfoStream::Module& module : modules)
{
if (!module.HasSymbolStream())
{
continue;
}
const PDB::ModuleSymbolStream moduleSymbolStream = module.CreateSymbolStream(rawPdbFile);
moduleSymbolStream.ForEachSymbol([&symbols, &imageSectionStream](const PDB::CodeView::DBI::Record* record)
{
const char* name = nullptr;
uint32_t rva = 0u;
if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_THUNK32)
{
if (record->data.S_THUNK32.thunk == PDB::CodeView::DBI::ThunkOrdinal::TrampolineIncremental)
{
// we have never seen incremental linking thunks stored inside a S_THUNK32 symbol, but better be safe than sorry
name = "ILT";
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_THUNK32.section, record->data.S_THUNK32.offset);
}
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_TRAMPOLINE)
{
// incremental linking thunks are stored in the linker module
name = "ILT";
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_TRAMPOLINE.thunkSection, record->data.S_TRAMPOLINE.thunkOffset);
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_BLOCK32)
{
// blocks never store a name and are only stored for indicating whether other symbols are children of this block
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LABEL32)
{
// labels don't have a name
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LPROC32)
{
name = record->data.S_LPROC32.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LPROC32.section, record->data.S_LPROC32.offset);
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GPROC32)
{
name = record->data.S_GPROC32.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GPROC32.section, record->data.S_GPROC32.offset);
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LPROC32_ID)
{
name = record->data.S_LPROC32_ID.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LPROC32_ID.section, record->data.S_LPROC32_ID.offset);
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GPROC32_ID)
{
name = record->data.S_GPROC32_ID.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GPROC32_ID.section, record->data.S_GPROC32_ID.offset);
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_REGREL32)
{
name = record->data.S_REGREL32.name;
// You can only get the address while running the program by checking the register value and adding the offset
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LDATA32)
{
name = record->data.S_LDATA32.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LDATA32.section, record->data.S_LDATA32.offset);
}
else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LTHREAD32)
{
name = record->data.S_LTHREAD32.name;
rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LTHREAD32.section, record->data.S_LTHREAD32.offset);
}
if (rva == 0u)
{
// certain symbols (e.g. control-flow guard symbols) don't have a valid RVA, ignore those
return;
}
symbols.push_back(Symbol { name, rva });
});
}
scope.Done(modules.GetLength());
}
total.Done(symbols.size());
}

View File

@@ -0,0 +1,54 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"
#include "ExampleTimedScope.h"
namespace
{
static unsigned int g_indent = 0u;
static void PrintIndent(void)
{
printf("%.*s", g_indent * 2u, "| | | | | | | | ");
}
}
TimedScope::TimedScope(const char* message)
: m_begin(std::chrono::high_resolution_clock::now())
{
PrintIndent();
++g_indent;
printf("%s\n", message);
}
void TimedScope::Done(void) const
{
--g_indent;
PrintIndent();
const double milliSeconds = ReadMilliseconds();
printf("---> done in %.3fms\n", milliSeconds);
}
void TimedScope::Done(size_t count) const
{
--g_indent;
PrintIndent();
const double milliSeconds = ReadMilliseconds();
printf("---> done in %.3fms (%zu elements)\n", milliSeconds, count);
}
double TimedScope::ReadMilliseconds(void) const
{
const std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
const std::chrono::duration<double> seconds = now - m_begin;
return seconds.count() * 1000.0;
}

View File

@@ -0,0 +1,22 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Foundation/PDB_Macros.h"
#include <chrono>
class TimedScope
{
public:
explicit TimedScope(const char* message);
void Done(void) const;
void Done(size_t count) const;
private:
double ReadMilliseconds(void) const;
const std::chrono::high_resolution_clock::time_point m_begin;
PDB_DISABLE_COPY_MOVE(TimedScope);
};

View File

@@ -0,0 +1,41 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"
#include "ExampleTypeTable.h"
#include "Foundation/PDB_Memory.h"
TypeTable::TypeTable(const PDB::TPIStream& tpiStream) PDB_NO_EXCEPT
: typeIndexBegin(tpiStream.GetFirstTypeIndex()), typeIndexEnd(tpiStream.GetLastTypeIndex()),
m_recordCount(tpiStream.GetTypeRecordCount())
{
// Create coalesced stream from TPI stream, so the records can be referenced directly using pointers.
const PDB::DirectMSFStream& directStream = tpiStream.GetDirectMSFStream();
m_stream = PDB::CoalescedMSFStream(directStream, directStream.GetSize(), 0);
// types in the TPI stream are accessed by their index from other streams.
// however, the index is not stored with types in the TPI stream directly, but has to be built while walking the stream.
// similarly, because types are variable-length records, there are no direct offsets to access individual types.
// we therefore walk the TPI stream once, and store pointers to the records for trivial O(1) array lookup by index later.
m_records = PDB_NEW_ARRAY(const PDB::CodeView::TPI::Record*, m_recordCount);
// parse the CodeView records
uint32_t typeIndex = 0u;
tpiStream.ForEachTypeRecordHeaderAndOffset([this, &typeIndex](const PDB::CodeView::TPI::RecordHeader& header, size_t offset)
{
// The header includes the record kind and size, which can be stored along with offset
// to allow for lazy loading of the types on-demand directly from the TPIStream::GetDirectMSFStream()
// using DirectMSFStream::ReadAtOffset(...). Thus not needing a CoalescedMSFStream to look up the types.
(void)header;
const PDB::CodeView::TPI::Record* record = m_stream.GetDataAtOffset<const PDB::CodeView::TPI::Record>(offset);
m_records[typeIndex] = record;
++typeIndex;
});
}
TypeTable::~TypeTable() PDB_NO_EXCEPT
{
PDB_DELETE_ARRAY(m_records);
}

View File

@@ -0,0 +1,49 @@
#pragma once
#include "PDB_TPIStream.h"
#include "PDB_CoalescedMSFStream.h"
class TypeTable
{
public:
explicit TypeTable(const PDB::TPIStream& tpiStream) PDB_NO_EXCEPT;
~TypeTable() PDB_NO_EXCEPT;
// Returns the index of the first type, which is not necessarily zero.
PDB_NO_DISCARD inline uint32_t GetFirstTypeIndex(void) const PDB_NO_EXCEPT
{
return typeIndexBegin;
}
// Returns the index of the last type.
PDB_NO_DISCARD inline uint32_t GetLastTypeIndex(void) const PDB_NO_EXCEPT
{
return typeIndexEnd;
}
PDB_NO_DISCARD inline const PDB::CodeView::TPI::Record* GetTypeRecord(uint32_t typeIndex) const PDB_NO_EXCEPT
{
if (typeIndex < typeIndexBegin || typeIndex > typeIndexEnd)
return nullptr;
return m_records[typeIndex - typeIndexBegin];
}
// Returns a view of all type records.
// Records identified by a type index can be accessed via "allRecords[typeIndex - firstTypeIndex]".
PDB_NO_DISCARD inline PDB::ArrayView<const PDB::CodeView::TPI::Record*> GetTypeRecords(void) const PDB_NO_EXCEPT
{
return PDB::ArrayView<const PDB::CodeView::TPI::Record*>(m_records, m_recordCount);
}
private:
uint32_t typeIndexBegin;
uint32_t typeIndexEnd;
size_t m_recordCount;
const PDB::CodeView::TPI::Record **m_records;
PDB::CoalescedMSFStream m_stream;
PDB_DISABLE_COPY(TypeTable);
};

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,4 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "Examples_PCH.h"

View File

@@ -0,0 +1,53 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Warnings.h"
// The following clang warnings must be disabled for the examples to build with 0 warnings
#if PDB_COMPILER_CLANG
# pragma clang diagnostic ignored "-Wformat-nonliteral" // format string is not a string literal
# pragma clang diagnostic ignored "-Wswitch-default" // switch' missing 'default' label
# pragma clang diagnostic ignored "-Wcast-align" // increases required alignment from X to Y
# pragma clang diagnostic ignored "-Wold-style-cast" // use of old-style cast
#endif
#if PDB_COMPILER_MSVC
# pragma warning(push, 0)
#elif PDB_COMPILER_CLANG
# pragma clang diagnostic push
#endif
#if PDB_COMPILER_MSVC
// we compile without exceptions
# define _ALLOW_RTCc_IN_STL
// triggered by Windows.h
# pragma warning (disable : 4668)
// triggered by xlocale in VS 2017
# pragma warning (disable : 4625) // copy constructor was implicitly defined as deleted
# pragma warning (disable : 4626) // assignment operator was implicitly defined as deleted
# pragma warning (disable : 5026) // move constructor was implicitly defined as deleted
# pragma warning (disable : 5027) // move assignment operator was implicitly defined as deleted
# pragma warning (disable : 4774) // format string expected in argument 1 is not a string literal
#endif
#ifdef _WIN32
# define NOMINMAX
# include <Windows.h>
# undef cdecl
#endif
# include <vector>
# include <unordered_set>
# include <chrono>
# include <string>
# include <algorithm>
# include <cstdarg>
#if PDB_COMPILER_MSVC
# pragma warning(pop)
#elif PDB_COMPILER_CLANG
# pragma clang diagnostic pop
#endif

View File

@@ -0,0 +1,68 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "PDB_Macros.h"
#include "PDB_Assert.h"
namespace PDB
{
// A read-only view into arrays of any type and length.
template <typename T>
class PDB_NO_DISCARD ArrayView
{
public:
// Constructs an array view from a C array with explicit length.
inline constexpr explicit ArrayView(const T* const array, size_t length) PDB_NO_EXCEPT
: m_data(array)
, m_length(length)
{
}
PDB_DEFAULT_COPY_CONSTRUCTOR(ArrayView);
PDB_DEFAULT_MOVE_CONSTRUCTOR(ArrayView);
// Provides read-only access to the underlying array.
PDB_NO_DISCARD inline constexpr const T* Decay(void) const PDB_NO_EXCEPT
{
return m_data;
}
// Returns the length of the view.
PDB_NO_DISCARD inline constexpr size_t GetLength(void) const PDB_NO_EXCEPT
{
return m_length;
}
// Returns the i-th element.
PDB_NO_DISCARD inline const T& operator[](size_t i) const PDB_NO_EXCEPT
{
PDB_ASSERT(i < GetLength(), "Index %zu out of bounds [0, %zu).", i, GetLength());
return m_data[i];
}
// ------------------------------------------------------------------------------------------------
// Range-based for-loop support
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD inline const T* begin(void) const PDB_NO_EXCEPT
{
return m_data;
}
PDB_NO_DISCARD inline const T* end(void) const PDB_NO_EXCEPT
{
return m_data + m_length;
}
private:
const T* const m_data;
const size_t m_length;
PDB_DISABLE_MOVE_ASSIGNMENT(ArrayView);
PDB_DISABLE_COPY_ASSIGNMENT(ArrayView);
};
}

View File

@@ -0,0 +1,27 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "PDB_Macros.h"
#include "PDB_Log.h"
PDB_PUSH_WARNING_CLANG
PDB_DISABLE_WARNING_CLANG("-Wgnu-zero-variadic-macro-arguments")
PDB_DISABLE_WARNING_CLANG("-Wreserved-identifier")
extern "C" void __cdecl __debugbreak(void);
#if PDB_COMPILER_MSVC
# pragma intrinsic(__debugbreak)
#endif
#ifdef _DEBUG
# define PDB_ASSERT(_condition, _msg, ...) (_condition) ? (void)true : (PDB_LOG_ERROR(_msg, ##__VA_ARGS__), __debugbreak())
#else
# define PDB_ASSERT(_condition, _msg, ...) PDB_NOOP(_condition, _msg, ##__VA_ARGS__)
#endif
PDB_POP_WARNING_CLANG

View File

@@ -0,0 +1,23 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "PDB_Macros.h"
#define PDB_DEFINE_BIT_OPERATORS(_type) \
PDB_NO_DISCARD inline constexpr _type operator|(_type lhs, _type rhs) PDB_NO_EXCEPT \
{ \
return static_cast<_type>(PDB_AS_UNDERLYING(lhs) | PDB_AS_UNDERLYING(rhs)); \
} \
\
PDB_NO_DISCARD inline constexpr _type operator&(_type lhs, _type rhs) PDB_NO_EXCEPT \
{ \
return static_cast<_type>(PDB_AS_UNDERLYING(lhs) & PDB_AS_UNDERLYING(rhs)); \
} \
\
PDB_NO_DISCARD inline constexpr _type operator~(_type value) PDB_NO_EXCEPT \
{ \
return static_cast<_type>(~PDB_AS_UNDERLYING(value)); \
}

View File

@@ -0,0 +1,73 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "PDB_Assert.h"
#ifdef _WIN32
PDB_PUSH_WARNING_CLANG
PDB_DISABLE_WARNING_CLANG("-Wreserved-identifier")
extern "C" unsigned char _BitScanForward(unsigned long* _Index, unsigned long _Mask);
PDB_POP_WARNING_CLANG
# if PDB_COMPILER_MSVC
# pragma intrinsic(_BitScanForward)
# endif
#endif
namespace PDB
{
namespace BitUtil
{
// Returns whether the given unsigned value is a power of two.
template <typename T>
PDB_NO_DISCARD inline constexpr bool IsPowerOfTwo(T value) PDB_NO_EXCEPT
{
PDB_ASSERT(value != 0u, "Invalid value.");
return (value & (value - 1u)) == 0u;
}
// Rounds the given unsigned value up to the next multiple.
template <typename T>
PDB_NO_DISCARD inline constexpr T RoundUpToMultiple(T numToRound, T multipleOf) PDB_NO_EXCEPT
{
PDB_ASSERT(IsPowerOfTwo(multipleOf), "Multiple must be a power-of-two.");
return (numToRound + (multipleOf - 1u)) & ~(multipleOf - 1u);
}
// Finds the position of the first set bit in the given value starting from the LSB, e.g. FindFirstSetBit(0b00000010) == 1.
// This operation is also known as CTZ (Count Trailing Zeros).
template <typename T>
PDB_NO_DISCARD inline uint32_t FindFirstSetBit(T value) PDB_NO_EXCEPT;
template <>
PDB_NO_DISCARD inline uint32_t FindFirstSetBit(uint32_t value) PDB_NO_EXCEPT
{
PDB_ASSERT(value != 0u, "Invalid value.");
#ifdef _WIN32
unsigned long result = 0ul;
_BitScanForward(&result, value);
#else
unsigned int result = 0u;
result = static_cast<unsigned int>(__builtin_ffs(static_cast<int>(value)));
if (result)
{
--result;
}
#endif
return result;
}
}
}

View File

@@ -0,0 +1,10 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
// Original raw_pdb forward-declares CRT functions to avoid pulling in headers,
// but this conflicts with MinGW's headers when compiled alongside Qt.
// Include the real headers instead.
#include <cstdio>
#include <cstring>

View File

@@ -0,0 +1,9 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
// See Jonathan Mueller's blog for replacing std::move and std::forward:
// https://foonathan.net/2021/09/move-forward/
#define PDB_FORWARD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)

View File

@@ -0,0 +1,15 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "PDB_Macros.h"
#include "PDB_CRT.h"
PDB_PUSH_WARNING_CLANG
PDB_DISABLE_WARNING_CLANG("-Wgnu-zero-variadic-macro-arguments")
#define PDB_LOG_ERROR(_format, ...) printf(_format, ##__VA_ARGS__)
PDB_POP_WARNING_CLANG

View File

@@ -0,0 +1,126 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "PDB_Platform.h"
#include "PDB_TypeTraits.h"
// ------------------------------------------------------------------------------------------------
// ATTRIBUTES
// ------------------------------------------------------------------------------------------------
// Indicates to the compiler that the return value of a function or class should not be ignored.
#if PDB_CPP_17
# define PDB_NO_DISCARD [[nodiscard]]
#else
# define PDB_NO_DISCARD
#endif
// Indicates to the compiler that a function does not throw an exception.
#define PDB_NO_EXCEPT noexcept
// ------------------------------------------------------------------------------------------------
// SPECIAL MEMBER FUNCTIONS
// ------------------------------------------------------------------------------------------------
// Default special member functions.
#define PDB_DEFAULT_COPY_CONSTRUCTOR(_name) _name(const _name&) PDB_NO_EXCEPT = default
#define PDB_DEFAULT_COPY_ASSIGNMENT(_name) _name& operator=(const _name&) PDB_NO_EXCEPT = default
#define PDB_DEFAULT_MOVE_CONSTRUCTOR(_name) _name(_name&&) PDB_NO_EXCEPT = default
#define PDB_DEFAULT_MOVE_ASSIGNMENT(_name) _name& operator=(_name&&) PDB_NO_EXCEPT = default
// Default copy member functions.
#define PDB_DEFAULT_COPY(_name) PDB_DEFAULT_COPY_CONSTRUCTOR(_name); PDB_DEFAULT_COPY_ASSIGNMENT(_name)
// Default move member functions.
#define PDB_DEFAULT_MOVE(_name) PDB_DEFAULT_MOVE_CONSTRUCTOR(_name); PDB_DEFAULT_MOVE_ASSIGNMENT(_name)
// Single macro to default all copy and move member functions.
#define PDB_DEFAULT_COPY_MOVE(_name) PDB_DEFAULT_COPY(_name); PDB_DEFAULT_MOVE(_name)
// Disable special member functions.
#define PDB_DISABLE_COPY_CONSTRUCTOR(_name) _name(const _name&) PDB_NO_EXCEPT = delete
#define PDB_DISABLE_COPY_ASSIGNMENT(_name) _name& operator=(const _name&) PDB_NO_EXCEPT = delete
#define PDB_DISABLE_MOVE_CONSTRUCTOR(_name) _name(_name&&) PDB_NO_EXCEPT = delete
#define PDB_DISABLE_MOVE_ASSIGNMENT(_name) _name& operator=(_name&&) PDB_NO_EXCEPT = delete
// Disable copy member functions.
#define PDB_DISABLE_COPY(_name) PDB_DISABLE_COPY_CONSTRUCTOR(_name); PDB_DISABLE_COPY_ASSIGNMENT(_name)
// Disable move member functions.
#define PDB_DISABLE_MOVE(_name) PDB_DISABLE_MOVE_CONSTRUCTOR(_name); PDB_DISABLE_MOVE_ASSIGNMENT(_name)
// Single macro to disable all copy and move member functions.
#define PDB_DISABLE_COPY_MOVE(_name) PDB_DISABLE_COPY(_name); PDB_DISABLE_MOVE(_name)
// ------------------------------------------------------------------------------------------------
// COMPILER WARNINGS
// ------------------------------------------------------------------------------------------------
#if PDB_COMPILER_MSVC
# define PDB_PRAGMA(_x) __pragma(_x)
# define PDB_PUSH_WARNING_MSVC PDB_PRAGMA(warning(push))
# define PDB_SUPPRESS_WARNING_MSVC(_number) PDB_PRAGMA(warning(suppress : _number))
# define PDB_DISABLE_WARNING_MSVC(_number) PDB_PRAGMA(warning(disable : _number))
# define PDB_POP_WARNING_MSVC PDB_PRAGMA(warning(pop))
# define PDB_PUSH_WARNING_CLANG
# define PDB_DISABLE_WARNING_CLANG(_diagnostic)
# define PDB_POP_WARNING_CLANG
#elif PDB_COMPILER_CLANG
# define PDB_PRAGMA(_x) _Pragma(#_x)
# define PDB_PUSH_WARNING_MSVC
# define PDB_SUPPRESS_WARNING_MSVC(_number)
# define PDB_DISABLE_WARNING_MSVC(_number)
# define PDB_POP_WARNING_MSVC
# define PDB_PUSH_WARNING_CLANG PDB_PRAGMA(clang diagnostic push)
# define PDB_DISABLE_WARNING_CLANG(_diagnostic) PDB_PRAGMA(clang diagnostic ignored _diagnostic)
# define PDB_POP_WARNING_CLANG PDB_PRAGMA(clang diagnostic pop)
#elif PDB_COMPILER_GCC
# define PDB_PRAGMA(_x) _Pragma(#_x)
# define PDB_PUSH_WARNING_MSVC
# define PDB_SUPPRESS_WARNING_MSVC(_number)
# define PDB_DISABLE_WARNING_MSVC(_number)
# define PDB_POP_WARNING_MSVC
# define PDB_PUSH_WARNING_CLANG
# define PDB_DISABLE_WARNING_CLANG(_diagnostic)
# define PDB_POP_WARNING_CLANG
#endif
// ------------------------------------------------------------------------------------------------
// MISCELLANEOUS
// ------------------------------------------------------------------------------------------------
// Trick to make other macros require a semicolon at the end.
#define PDB_REQUIRE_SEMICOLON static_assert(true, "")
// Defines a C-like flexible array member.
#define PDB_FLEXIBLE_ARRAY_MEMBER(_type, _name) \
PDB_PUSH_WARNING_MSVC \
PDB_PUSH_WARNING_CLANG \
PDB_DISABLE_WARNING_MSVC(4200) \
PDB_DISABLE_WARNING_CLANG("-Wzero-length-array") \
_type _name[0]; \
PDB_POP_WARNING_MSVC \
PDB_POP_WARNING_CLANG \
PDB_REQUIRE_SEMICOLON
// Casts any value to the value of the underlying type.
#define PDB_AS_UNDERLYING(_value) static_cast<typename PDB::underlying_type<decltype(_value)>::type>(_value)
// Signals to the compiler that a function should be ignored, but have its argument list parsed (and "used", so as to not generate "unused variable" warnings).
#if PDB_COMPILER_MSVC
# define PDB_NOOP __noop
#else
# define PDB_NOOP(...) (void)sizeof(__VA_ARGS__)
#endif

View File

@@ -0,0 +1,11 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#define PDB_NEW(_type) new _type
#define PDB_NEW_ARRAY(_type, _length) new _type[_length]
#define PDB_DELETE(_ptr) delete _ptr
#define PDB_DELETE_ARRAY(_ptr) delete[] _ptr

View File

@@ -0,0 +1,11 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "PDB_TypeTraits.h"
// See Jonathan Mueller's blog for replacing std::move and std::forward:
// https://foonathan.net/2020/09/move-forward/
#define PDB_MOVE(...) static_cast<PDB::remove_reference<decltype(__VA_ARGS__)>::type&&>(__VA_ARGS__)

View File

@@ -0,0 +1,45 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
// determine the compiler/toolchain used
#if defined(__clang__)
# define PDB_COMPILER_MSVC 0
# define PDB_COMPILER_CLANG 1
# define PDB_COMPILER_GCC 0
#elif defined(_MSC_VER)
# define PDB_COMPILER_MSVC 1
# define PDB_COMPILER_CLANG 0
# define PDB_COMPILER_GCC 0
#elif defined(__GNUC__)
# define PDB_COMPILER_MSVC 0
# define PDB_COMPILER_CLANG 0
# define PDB_COMPILER_GCC 1
#else
# error("Unknown compiler.");
#endif
// check whether C++17 is available
#if __cplusplus >= 201703L
# define PDB_CPP_17 1
#else
# define PDB_CPP_17 0
#endif
// define used standard types
typedef decltype(sizeof(0)) size_t;
static_assert(sizeof(sizeof(0)) == sizeof(size_t), "Wrong size.");
typedef int int32_t;
static_assert(sizeof(int32_t) == 4u, "Wrong size.");
typedef unsigned char uint8_t;
static_assert(sizeof(uint8_t) == 1u, "Wrong size.");
typedef unsigned short uint16_t;
static_assert(sizeof(uint16_t) == 2u, "Wrong size.");
typedef unsigned int uint32_t;
static_assert(sizeof(uint32_t) == 4u, "Wrong size.");

View File

@@ -0,0 +1,33 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "PDB_Macros.h"
#include "PDB_TypeTraits.h"
namespace PDB
{
namespace Pointer
{
// Offsets any pointer by a given number of bytes.
template <typename T, typename U, typename V>
PDB_NO_DISCARD inline T Offset(U* anyPointer, V howManyBytes) PDB_NO_EXCEPT
{
static_assert(PDB::is_pointer<T>::value == true, "Type T must be a pointer type.");
union
{
T as_T;
U* as_U_ptr;
char* as_char_ptr;
};
as_U_ptr = anyPointer;
as_char_ptr += howManyBytes;
return as_T;
}
}
}

View File

@@ -0,0 +1,65 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
// provide our own type traits to avoid pulling in unnecessary includes
namespace PDB
{
template <class T>
struct is_pointer
{
static constexpr bool value = false;
};
template <class T>
struct is_pointer<T*>
{
static constexpr bool value = true;
};
template <class T>
struct is_pointer<T* const>
{
static constexpr bool value = true;
};
template <class T>
struct is_pointer<T* volatile>
{
static constexpr bool value = true;
};
template <class T>
struct is_pointer<T* const volatile>
{
static constexpr bool value = true;
};
template <class T>
struct remove_reference
{
using type = T;
};
template <class T>
struct remove_reference<T&>
{
using type = T;
};
template <class T>
struct remove_reference<T&&>
{
using type = T;
};
template <class T>
struct underlying_type
{
using type = __underlying_type(T);
};
}

View File

@@ -0,0 +1,45 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "PDB_Platform.h"
#if PDB_COMPILER_MSVC
// some warnings were introduced with different versions of Visual Studio, so we disable this warning instead of using a bunch of #if/#endif
# pragma warning (disable : 4619) // there is no warning number N
// we compile with exceptions disabled
# pragma warning (disable : 4530) // C++ exception handler used, but unwind semantics are not enabled.Specify / EHsc
# pragma warning (disable : 4577) // 'noexcept' used with no exception handling mode specified; termination on exception is not guaranteed. Specify /EHsc
// ignore purely informational warnings
# pragma warning (disable : 4514) // unreferenced inline function has been removed
# pragma warning (disable : 4710) // function not inlined
# pragma warning (disable : 4711) // function selected for automatic inline expansion
# pragma warning (disable : 4820) // 'N' bytes padding added after data member 'm_member'
# pragma warning (disable : 5045) // Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
#elif PDB_COMPILER_CLANG
// turn on absolutely all available Clang warnings
# pragma clang diagnostic warning "-Wall"
# pragma clang diagnostic warning "-Wextra"
# pragma clang diagnostic warning "-Weverything"
# pragma clang diagnostic warning "-Wpedantic"
// these warnings contradict -Weverything
# pragma clang diagnostic ignored "-Wc++98-compat"
# pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
// this warning is triggered for templates which are explicitly instantiated.
// forgetting to instantiate the template would trigger a linker error anyway, so we disable this warning.
# pragma clang diagnostic ignored "-Wundefined-func-template"
// we don't strive for C++20 compatibility
# pragma clang diagnostic ignored "-Wc++20-compat"
// some structures will have to be padded
# pragma clang diagnostic ignored "-Wpadded"
// it's impossible to write C++ code using raw pointers without triggering this warning
# pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
#endif

55
third_party/raw_pdb/src/PDB.cpp vendored Normal file
View File

@@ -0,0 +1,55 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB.h"
#include "PDB_Types.h"
#include "PDB_Util.h"
#include "PDB_RawFile.h"
#include "Foundation/PDB_PointerUtil.h"
#include "Foundation/PDB_CRT.h"
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::ValidateFile(const void* data, size_t size) PDB_NO_EXCEPT
{
// validate whether there is enough size for the super block
if (size < sizeof(SuperBlock))
{
return ErrorCode::InvalidDataSize;
}
// validate the super block
const SuperBlock* superBlock = Pointer::Offset<const SuperBlock*>(data, 0u);
{
// validate header magic
if (memcmp(superBlock->fileMagic, SuperBlock::MAGIC, sizeof(SuperBlock::MAGIC)) != 0)
{
return ErrorCode::InvalidSuperBlock;
}
// validate whether enough size is provided for the PDB file
// blockCount * blockSize is the size of the PDB file on disk
if (size < superBlock->blockCount * superBlock->blockSize)
{
return ErrorCode::InvalidDataSize;
}
// validate free block map.
// the free block map should always reside at either index 1 or 2.
if (superBlock->freeBlockMapIndex != 1u && superBlock->freeBlockMapIndex != 2u)
{
return ErrorCode::InvalidFreeBlockMap;
}
}
return ErrorCode::Success;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::RawFile PDB::CreateRawFile(const void* data) PDB_NO_EXCEPT
{
return RawFile(data);
}

21
third_party/raw_pdb/src/PDB.h vendored Normal file
View File

@@ -0,0 +1,21 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "PDB_ErrorCodes.h"
// https://llvm.org/docs/PDB/index.html
namespace PDB
{
class RawFile;
// Validates whether a PDB file is valid.
PDB_NO_DISCARD ErrorCode ValidateFile(const void* data, size_t size) PDB_NO_EXCEPT;
// Creates a raw PDB file that must have been validated.
PDB_NO_DISCARD RawFile CreateRawFile(const void* data) PDB_NO_EXCEPT;
}

View File

@@ -0,0 +1,169 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_CoalescedMSFStream.h"
#include "PDB_Util.h"
#include "PDB_DirectMSFStream.h"
#include "Foundation/PDB_PointerUtil.h"
#include "Foundation/PDB_Memory.h"
#include "Foundation/PDB_CRT.h"
namespace
{
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD static bool AreBlockIndicesContiguous(const uint32_t* blockIndices, uint32_t blockSize, uint32_t streamSize) PDB_NO_EXCEPT
{
const uint32_t blockCount = PDB::ConvertSizeToBlockCount(streamSize, blockSize);
// start with the first index, checking if all following indices are contiguous (N, N+1, N+2, ...)
uint32_t expectedIndex = blockIndices[0];
for (uint32_t i = 1u; i < blockCount; ++i)
{
++expectedIndex;
if (blockIndices[i] != expectedIndex)
{
return false;
}
}
return true;
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::CoalescedMSFStream::CoalescedMSFStream(void) PDB_NO_EXCEPT
: m_ownedData(nullptr)
, m_data(nullptr)
, m_size(0u)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::CoalescedMSFStream::CoalescedMSFStream(CoalescedMSFStream&& other) PDB_NO_EXCEPT
: m_ownedData(PDB_MOVE(other.m_ownedData))
, m_data(PDB_MOVE(other.m_data))
, m_size(PDB_MOVE(other.m_size))
{
other.m_ownedData = nullptr;
other.m_data = nullptr;
other.m_size = 0u;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::CoalescedMSFStream& PDB::CoalescedMSFStream::operator=(CoalescedMSFStream&& other) PDB_NO_EXCEPT
{
if (this != &other)
{
PDB_DELETE_ARRAY(m_ownedData);
m_ownedData = PDB_MOVE(other.m_ownedData);
m_data = PDB_MOVE(other.m_data);
m_size = PDB_MOVE(other.m_size);
other.m_ownedData = nullptr;
other.m_data = nullptr;
other.m_size = 0u;
}
return *this;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::CoalescedMSFStream::CoalescedMSFStream(const void* data, uint32_t blockSize, const uint32_t* blockIndices, uint32_t streamSize) PDB_NO_EXCEPT
: m_ownedData(nullptr)
, m_data(nullptr)
, m_size(streamSize)
{
if (AreBlockIndicesContiguous(blockIndices, blockSize, streamSize))
{
// fast path, all block indices are contiguous, so we don't have to copy any data at all.
// instead, we directly point into the memory-mapped file at the correct offset.
const uint32_t index = blockIndices[0];
const size_t fileOffset = PDB::ConvertBlockIndexToFileOffset(index, blockSize);
m_data = Pointer::Offset<const Byte*>(data, fileOffset);
}
else
{
// slower path, we need to copy disjunct blocks into our own data array, block by block
m_ownedData = PDB_NEW_ARRAY(Byte, streamSize);
m_data = m_ownedData;
Byte* destination = m_ownedData;
// copy full blocks first
const uint32_t fullBlockCount = streamSize / blockSize;
for (uint32_t i = 0u; i < fullBlockCount; ++i)
{
const uint32_t index = blockIndices[i];
// read one single block at the correct offset in the stream
const size_t fileOffset = PDB::ConvertBlockIndexToFileOffset(index, blockSize);
const void* sourceData = Pointer::Offset<const void*>(data, fileOffset);
memcpy(destination, sourceData, blockSize);
destination += blockSize;
}
// account for non-full blocks
const uint32_t remainingBytes = streamSize - (fullBlockCount * blockSize);
if (remainingBytes != 0u)
{
const uint32_t index = blockIndices[fullBlockCount];
// read remaining bytes at correct offset in the stream
const size_t fileOffset = PDB::ConvertBlockIndexToFileOffset(index, blockSize);
const void* sourceData = Pointer::Offset<const void*>(data, fileOffset);
memcpy(destination, sourceData, remainingBytes);
}
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::CoalescedMSFStream::CoalescedMSFStream(const DirectMSFStream& directStream, uint32_t size, uint32_t offset) PDB_NO_EXCEPT
: m_ownedData(nullptr)
, m_data(nullptr)
, m_size(size)
{
const DirectMSFStream::IndexAndOffset indexAndOffset = directStream.GetBlockIndexForOffset(offset);
// Note: we need to add the offset within the block to the size of the stream to determine if the block
// indices are contiguous. This is needed to deal with the case where reading the requested number of bytes
// from the specified offset would cross a block boundary. For example, if the offset within the block is
// 64 and we want to read 4096 bytes with a block size of 4096, we need to consider *two* block indices,
// not *one*, even though 4096 / 4096 = 1.
if (AreBlockIndicesContiguous(directStream.GetBlockIndices() + indexAndOffset.index, directStream.GetBlockSize(), indexAndOffset.offsetWithinBlock + size))
{
// fast path, all block indices inside the direct stream from (data + offset) to (data + offset + size) are contiguous
const size_t offsetWithinData = directStream.GetDataOffsetForIndexAndOffset(indexAndOffset);
m_data = Pointer::Offset<const Byte*>(directStream.GetData(), offsetWithinData);
}
else
{
// slower path, we need to copy from disjunct blocks, which is performed by the direct stream
m_ownedData = PDB_NEW_ARRAY(Byte, size);
m_data = m_ownedData;
directStream.ReadAtOffset(m_ownedData, size, offset);
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::CoalescedMSFStream::~CoalescedMSFStream(void) PDB_NO_EXCEPT
{
PDB_DELETE_ARRAY(m_ownedData);
}

View File

@@ -0,0 +1,71 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Assert.h"
#include "Foundation/PDB_Macros.h"
#include "PDB_Types.h"
// https://llvm.org/docs/PDB/index.html#the-msf-container
// https://llvm.org/docs/PDB/MsfFile.html
namespace PDB
{
class PDB_NO_DISCARD DirectMSFStream;
// provides access to a coalesced version of an MSF stream.
// inherently thread-safe, the stream doesn't carry any internal offset or similar.
// coalesces all blocks into a contiguous stream of data upon construction.
// very fast individual reads, useful when almost all data of a stream is needed anyway.
class PDB_NO_DISCARD CoalescedMSFStream
{
public:
CoalescedMSFStream(void) PDB_NO_EXCEPT;
CoalescedMSFStream(CoalescedMSFStream&& other) PDB_NO_EXCEPT;
CoalescedMSFStream& operator=(CoalescedMSFStream&& other) PDB_NO_EXCEPT;
explicit CoalescedMSFStream(const void* data, uint32_t blockSize, const uint32_t* blockIndices, uint32_t streamSize) PDB_NO_EXCEPT;
// Creates a coalesced stream from a direct stream at any offset.
explicit CoalescedMSFStream(const DirectMSFStream& directStream, uint32_t size, uint32_t offset) PDB_NO_EXCEPT;
~CoalescedMSFStream(void) PDB_NO_EXCEPT;
// Returns the size of the stream.
PDB_NO_DISCARD inline size_t GetSize(void) const PDB_NO_EXCEPT
{
return m_size;
}
// Provides read-only access to the data.
template <typename T>
PDB_NO_DISCARD inline const T* GetDataAtOffset(size_t offset) const PDB_NO_EXCEPT
{
return reinterpret_cast<const T*>(m_data + offset);
}
template <typename T>
PDB_NO_DISCARD inline size_t GetPointerOffset(const T* pointer) const PDB_NO_EXCEPT
{
const Byte* bytePointer = reinterpret_cast<const Byte*>(pointer);
const Byte* dataEnd = m_data + m_size;
PDB_ASSERT(bytePointer >= m_data && bytePointer <= dataEnd, "Pointer 0x%p not within stream range [0x%p:0x%p]",
static_cast<const void*>(bytePointer), static_cast<const void*>(m_data), static_cast<const void*>(dataEnd));
return static_cast<size_t>(bytePointer - m_data);
}
private:
// contiguous, coalesced data, can be null
Byte* m_ownedData;
// either points to the owned data that has been copied from disjunct blocks, or points to the
// memory-mapped data directly in case all stream blocks are contiguous.
const Byte* m_data;
size_t m_size;
PDB_DISABLE_COPY(CoalescedMSFStream);
};
}

View File

@@ -0,0 +1,335 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_DBIStream.h"
#include "PDB_RawFile.h"
namespace
{
// the DBI stream always resides at index 3
static constexpr const uint32_t DBIStreamIndex = 3u;
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD static inline uint32_t GetModuleInfoSubstreamOffset(const PDB::DBI::StreamHeader& /* dbiHeader */) PDB_NO_EXCEPT
{
return sizeof(PDB::DBI::StreamHeader);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD static inline uint32_t GetSectionContributionSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
{
return GetModuleInfoSubstreamOffset(dbiHeader) + dbiHeader.moduleInfoSize;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD static inline uint32_t GetSectionMapSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
{
return GetSectionContributionSubstreamOffset(dbiHeader) + dbiHeader.sectionContributionSize;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD static inline uint32_t GetSourceInfoSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
{
return GetSectionMapSubstreamOffset(dbiHeader) + dbiHeader.sectionMapSize;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD static inline uint32_t GetTypeServerMapSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
{
return GetSourceInfoSubstreamOffset(dbiHeader) + dbiHeader.sourceInfoSize;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD static inline uint32_t GetECSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
{
return GetTypeServerMapSubstreamOffset(dbiHeader) + dbiHeader.typeServerMapSize;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD static inline uint32_t GetDebugHeaderSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
{
return GetECSubstreamOffset(dbiHeader) + dbiHeader.ecSize;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD static inline bool HasDebugHeaderSubstream(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
{
return dbiHeader.optionalDebugHeaderSize != 0u;
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::DBIStream::DBIStream(void) PDB_NO_EXCEPT
: m_header()
, m_stream()
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::DBIStream::DBIStream(const RawFile& file, const DBI::StreamHeader& header) PDB_NO_EXCEPT
: m_header(header)
, m_stream(file.CreateMSFStream<DirectMSFStream>(DBIStreamIndex))
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::HasValidDBIStream(const RawFile& file) PDB_NO_EXCEPT
{
DirectMSFStream stream = file.CreateMSFStream<DirectMSFStream>(DBIStreamIndex);
if (stream.GetSize() < sizeof(DBI::StreamHeader))
{
return ErrorCode::InvalidStream;
}
const DBI::StreamHeader header = stream.ReadAtOffset<DBI::StreamHeader>(0u);
if (header.signature != DBI::StreamHeader::Signature)
{
return ErrorCode::InvalidSignature;
}
else if (header.version != DBI::StreamHeader::Version::V70)
{
return ErrorCode::UnknownVersion;
}
return ErrorCode::Success;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::DBIStream PDB::CreateDBIStream(const RawFile& file) PDB_NO_EXCEPT
{
DirectMSFStream stream = file.CreateMSFStream<DirectMSFStream>(DBIStreamIndex);
const DBI::StreamHeader header = stream.ReadAtOffset<DBI::StreamHeader>(0u);
return DBIStream { file, header };
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::DBIStream::HasValidSymbolRecordStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
return (m_header.symbolRecordStreamIndex != PDB::NilStreamIndex) ? ErrorCode::Success : ErrorCode::InvalidStreamIndex;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::DBIStream::HasValidImageSectionStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
// the debug header stream is optional. if it's not there, we can't get the image section stream either.
if (!HasDebugHeaderSubstream(m_header))
{
return ErrorCode::InvalidStreamIndex;
}
// find the debug header sub-stream
const uint32_t debugHeaderOffset = GetDebugHeaderSubstreamOffset(m_header);
// validate that we have enough data to read the debug header
// (the header field optionalDebugHeaderSize might claim there's a debug header,
// but the stream might not have enough data - this happens with some .ni.pdb files)
if (debugHeaderOffset + sizeof(DBI::DebugHeader) > m_stream.GetSize())
{
return ErrorCode::InvalidStream;
}
const DBI::DebugHeader& debugHeader = m_stream.ReadAtOffset<DBI::DebugHeader>(debugHeaderOffset);
if (debugHeader.sectionHeaderStreamIndex == DBI::DebugHeader::InvalidStreamIndex)
{
return ErrorCode::InvalidStreamIndex;
}
return ErrorCode::Success;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::DBIStream::HasValidPublicSymbolStream(const RawFile& file) const PDB_NO_EXCEPT
{
if (m_header.publicStreamIndex == PDB::NilStreamIndex)
{
return ErrorCode::InvalidStreamIndex;
}
DirectMSFStream publicStream = file.CreateMSFStream<DirectMSFStream>(m_header.publicStreamIndex);
// the public symbol stream always begins with a header, we are not interested in that.
// following the public symbol stream header is a hash table header.
const HashTableHeader hashHeader = publicStream.ReadAtOffset<HashTableHeader>(sizeof(PublicStreamHeader));
if (hashHeader.signature != HashTableHeader::Signature)
{
return ErrorCode::InvalidSignature;
}
else if (hashHeader.version != HashTableHeader::Version)
{
return ErrorCode::UnknownVersion;
}
return ErrorCode::Success;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::DBIStream::HasValidGlobalSymbolStream(const RawFile& file) const PDB_NO_EXCEPT
{
if (m_header.globalStreamIndex == PDB::NilStreamIndex)
{
return ErrorCode::InvalidStreamIndex;
}
DirectMSFStream globalStream = file.CreateMSFStream<DirectMSFStream>(m_header.globalStreamIndex);
// the global symbol stream starts with a hash table header
const HashTableHeader hashHeader = globalStream.ReadAtOffset<HashTableHeader>(0u);
if (hashHeader.signature != HashTableHeader::Signature)
{
return ErrorCode::InvalidSignature;
}
else if (hashHeader.version != HashTableHeader::Version)
{
return ErrorCode::UnknownVersion;
}
return ErrorCode::Success;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::DBIStream::HasValidSectionContributionStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
if (m_header.sectionContributionSize < sizeof(DBI::SectionContribution::Version))
{
return ErrorCode::InvalidStream;
}
// find the section contribution sub-stream
// https://llvm.org/docs/PDB/DbiStream.html#section-contribution-substream
const uint32_t streamOffset = GetSectionContributionSubstreamOffset(m_header);
const DBI::SectionContribution::Version version = m_stream.ReadAtOffset<DBI::SectionContribution::Version>(streamOffset);
if (version != DBI::SectionContribution::Version::Ver60)
{
return ErrorCode::UnknownVersion;
}
return ErrorCode::Success;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::CoalescedMSFStream PDB::DBIStream::CreateSymbolRecordStream(const RawFile& file) const PDB_NO_EXCEPT
{
// the symbol record stream holds the actual CodeView data of the symbols
return file.CreateMSFStream<CoalescedMSFStream>(m_header.symbolRecordStreamIndex);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ImageSectionStream PDB::DBIStream::CreateImageSectionStream(const RawFile& file) const PDB_NO_EXCEPT
{
// find the debug header sub-stream
const uint32_t debugHeaderOffset = GetDebugHeaderSubstreamOffset(m_header);
const DBI::DebugHeader& debugHeader = m_stream.ReadAtOffset<DBI::DebugHeader>(debugHeaderOffset);
// from there, grab the section header stream
return ImageSectionStream(file, debugHeader.sectionHeaderStreamIndex);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::PublicSymbolStream PDB::DBIStream::CreatePublicSymbolStream(const RawFile& file) const PDB_NO_EXCEPT
{
DirectMSFStream publicStream = file.CreateMSFStream<DirectMSFStream>(m_header.publicStreamIndex);
// the public symbol stream always begins with a header, we are not interested in that.
// following the public symbol stream header is a hash table header.
// we use this to work out how many symbol records are referenced by the public symbol stream.
const HashTableHeader hashHeader = publicStream.ReadAtOffset<HashTableHeader>(sizeof(PublicStreamHeader));
const uint32_t recordCount = hashHeader.size / sizeof(HashRecord);
return PublicSymbolStream(file, m_header.publicStreamIndex, recordCount);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::GlobalSymbolStream PDB::DBIStream::CreateGlobalSymbolStream(const RawFile& file) const PDB_NO_EXCEPT
{
DirectMSFStream globalStream = file.CreateMSFStream<DirectMSFStream>(m_header.globalStreamIndex);
// the global symbol stream starts with a hash table header.
// we use this to work out how many symbol records are referenced by the global symbol stream.
const HashTableHeader hashHeader = globalStream.ReadAtOffset<HashTableHeader>(0u);
const uint32_t recordCount = hashHeader.size / sizeof(HashRecord);
return GlobalSymbolStream(file, m_header.globalStreamIndex, recordCount);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::SourceFileStream PDB::DBIStream::CreateSourceFileStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
// find the source info sub-stream
// https://llvm.org/docs/PDB/DbiStream.html#file-info-substream
const uint32_t streamOffset = GetSourceInfoSubstreamOffset(m_header);
return SourceFileStream(m_stream, m_header.sourceInfoSize, streamOffset);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::SectionContributionStream PDB::DBIStream::CreateSectionContributionStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
// find the section contribution sub-stream
// https://llvm.org/docs/PDB/DbiStream.html#section-contribution-substream
const uint32_t streamOffset = GetSectionContributionSubstreamOffset(m_header);
return SectionContributionStream(m_stream, m_header.sectionContributionSize - sizeof(DBI::SectionContribution::Version), streamOffset + sizeof(DBI::SectionContribution::Version));
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ModuleInfoStream PDB::DBIStream::CreateModuleInfoStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
// find the module info sub-stream
// https://llvm.org/docs/PDB/DbiStream.html#module-info-substream
const uint32_t streamOffset = GetModuleInfoSubstreamOffset(m_header);
return ModuleInfoStream(m_stream, m_header.moduleInfoSize, streamOffset);
}

65
third_party/raw_pdb/src/PDB_DBIStream.h vendored Normal file
View File

@@ -0,0 +1,65 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "PDB_ErrorCodes.h"
#include "PDB_DBITypes.h"
#include "PDB_CoalescedMSFStream.h"
#include "PDB_DirectMSFStream.h"
#include "PDB_ImageSectionStream.h"
#include "PDB_PublicSymbolStream.h"
#include "PDB_GlobalSymbolStream.h"
#include "PDB_SourceFileStream.h"
#include "PDB_SectionContributionStream.h"
#include "PDB_ModuleInfoStream.h"
// PDB DBI Stream
// https://llvm.org/docs/PDB/DbiStream.html
namespace PDB
{
class RawFile;
class PDB_NO_DISCARD DBIStream
{
public:
DBIStream(void) PDB_NO_EXCEPT;
explicit DBIStream(const RawFile& file, const DBI::StreamHeader& header) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(DBIStream);
PDB_NO_DISCARD ErrorCode HasValidSymbolRecordStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD ErrorCode HasValidImageSectionStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD ErrorCode HasValidPublicSymbolStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD ErrorCode HasValidGlobalSymbolStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD ErrorCode HasValidSectionContributionStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD CoalescedMSFStream CreateSymbolRecordStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD ImageSectionStream CreateImageSectionStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD PublicSymbolStream CreatePublicSymbolStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD GlobalSymbolStream CreateGlobalSymbolStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD SourceFileStream CreateSourceFileStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD SectionContributionStream CreateSectionContributionStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD ModuleInfoStream CreateModuleInfoStream(const RawFile& file) const PDB_NO_EXCEPT;
PDB_NO_DISCARD const DBI::StreamHeader& GetHeader(void) const PDB_NO_EXCEPT
{
return m_header;
}
private:
DBI::StreamHeader m_header;
DirectMSFStream m_stream;
PDB_DISABLE_COPY(DBIStream);
};
// Returns whether the given raw file provides a valid DBI stream.
PDB_NO_DISCARD ErrorCode HasValidDBIStream(const RawFile& file) PDB_NO_EXCEPT;
// Creates the DBI stream from a raw file.
PDB_NO_DISCARD DBIStream CreateDBIStream(const RawFile& file) PDB_NO_EXCEPT;
}

View File

@@ -0,0 +1,9 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_DBITypes.h"
const uint32_t PDB::DBI::StreamHeader::Signature = 0xffffffffu;
const uint16_t PDB::DBI::DebugHeader::InvalidStreamIndex = 0xFFFFu;

928
third_party/raw_pdb/src/PDB_DBITypes.h vendored Normal file
View File

@@ -0,0 +1,928 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_BitOperators.h"
namespace PDB
{
namespace DBI
{
// https://llvm.org/docs/PDB/DbiStream.html#stream-header
// https://github.com/microsoft/microsoft-pdb/blob/master/PDB/dbi/dbi.h#L124
struct StreamHeader
{
static const uint32_t Signature;
enum class PDB_NO_DISCARD Version : uint32_t
{
VC41 = 930803u,
V50 = 19960307u,
V60 = 19970606u,
V70 = 19990903u,
V110 = 20091201u
};
uint32_t signature;
Version version;
uint32_t age;
uint16_t globalStreamIndex; // index of the global symbol stream
uint16_t toolchain;
uint16_t publicStreamIndex; // index of the public symbol stream
uint16_t pdbDllVersion;
uint16_t symbolRecordStreamIndex; // index of the symbol record stream
uint16_t pdbDllRbld;
uint32_t moduleInfoSize;
uint32_t sectionContributionSize;
uint32_t sectionMapSize;
uint32_t sourceInfoSize;
uint32_t typeServerMapSize;
uint32_t mfcTypeServerIndex;
uint32_t optionalDebugHeaderSize;
uint32_t ecSize;
uint16_t flags;
uint16_t machine;
uint32_t padding;
};
// https://llvm.org/docs/PDB/DbiStream.html#optional-debug-header-stream
struct DebugHeader
{
static const uint16_t InvalidStreamIndex;
uint16_t fpoDataStreamIndex; // IMAGE_DEBUG_TYPE_FPO
uint16_t exceptionDataStreamIndex; // IMAGE_DEBUG_TYPE_EXCEPTION
uint16_t fixupDataStreamIndex; // IMAGE_DEBUG_TYPE_FIXUP
uint16_t omapToSrcDataStreamIndex; // IMAGE_DEBUG_TYPE_OMAP_TO_SRC
uint16_t omapFromSrcDataStreamIndex; // IMAGE_DEBUG_TYPE_OMAP_FROM_SRC
uint16_t sectionHeaderStreamIndex; // a dump of all section headers (IMAGE_SECTION_HEADER) from the original executable
uint16_t tokenDataStreamIndex;
uint16_t xdataStreamIndex;
uint16_t pdataStreamIndex;
uint16_t newFpoDataStreamIndex;
uint16_t originalSectionHeaderDataStreamIndex;
};
// https://llvm.org/docs/PDB/DbiStream.html#section-contribution-substream
struct SectionContribution
{
enum class PDB_NO_DISCARD Version : uint32_t
{
Ver60 = 0xeffe0000u + 19970605u,
V2 = 0xeffe0000u + 20140516u
};
uint16_t section;
uint16_t padding;
uint32_t offset;
uint32_t size;
uint32_t characteristics;
uint16_t moduleIndex;
uint16_t padding2;
uint32_t dataCrc;
uint32_t relocationCrc;
};
// https://llvm.org/docs/PDB/DbiStream.html#module-info-substream
struct ModuleInfo
{
uint32_t unused;
SectionContribution sectionContribution;
uint16_t flags;
uint16_t moduleSymbolStreamIndex;
uint32_t symbolSize;
uint32_t c11Size;
uint32_t c13Size;
uint16_t sourceFileCount;
uint16_t padding;
uint32_t unused2;
uint32_t sourceFileNameIndex;
uint32_t pdbFilePathNameIndex;
};
}
namespace CodeView
{
namespace DBI
{
// code view type records that can appear in a DBI stream.
// this list is not exhaustive, but only contains what we need so far.
// https://llvm.org/docs/PDB/CodeViewSymbols.html
// https://llvm.org/docs/PDB/TpiStream.html#tpi-vs-ipi-stream
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2735
enum class PDB_NO_DISCARD SymbolRecordKind : uint16_t
{
S_END = 0x0006u, // block, procedure, "with" or thunk end
S_SKIP = 0x0007u, // Reserve symbol space in $$Symbols table
S_FRAMEPROC = 0x1012u, // extra frame and proc information
S_ANNOTATION = 0x1019u, // annotation string literals ("__annotation" intrinsic, e.g. via NT_ASSERT)
S_OBJNAME = 0x1101u, // full path to the original compiled .obj. can point to remote locations and temporary files, not necessarily the file that was linked into the executable
S_THUNK32 = 0x1102u, // thunk start
S_BLOCK32 = 0x1103u, // block start
S_LABEL32 = 0x1105u, // code label
S_REGISTER = 0x1106u, // register variable
S_CONSTANT = 0x1107u, // constant symbol
S_BPREL32 = 0x110Bu, // BP-relative address (almost like S_REGREL32)
S_LDATA32 = 0x110Cu, // (static) local data
S_GDATA32 = 0x110Du, // global data
S_PUB32 = 0x110Eu, // public symbol
S_LPROC32 = 0x110Fu, // local procedure start
S_GPROC32 = 0x1110u, // global procedure start
S_REGREL32 = 0x1111u, // register relative address
S_LTHREAD32 = 0x1112u, // (static) thread-local data
S_GTHREAD32 = 0x1113u, // global thread-local data
S_UNAMESPACE = 0x1124u, // using namespace
S_PROCREF = 0x1125u, // reference to function in any compiland
S_LPROCREF = 0x1127u, // local reference to function in any compiland
S_TRAMPOLINE = 0x112Cu, // incremental linking trampoline
S_SEPCODE = 0x1132u, // separated code (from the compiler)
S_SECTION = 0x1136u, // a COFF section in an executable
S_COFFGROUP = 0x1137u, // original COFF group before it was merged into executable sections by the linker, e.g. .CRT$XCU, .rdata, .bss, .lpp_prepatch_hooks
S_CALLSITEINFO = 0x1139u, // Indirect call site information
S_FRAMECOOKIE = 0x113Au, // Security cookie information
S_COMPILE3 = 0x113Cu, // replacement for S_COMPILE2, more info
S_ENVBLOCK = 0x113Du, // environment block split off from S_COMPILE2
S_LOCAL = 0x113Eu, // defines a local symbol in optimized code
S_DEFRANGE_REGISTER = 0x1141u, // ranges for en-registered symbol
S_DEFRANGE_FRAMEPOINTER_REL = 0x1142u, // range for stack symbol.
S_DEFRANGE_SUBFIELD_REGISTER = 0x1143u, // ranges for en-registered field of symbol
S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE = 0x1144u, // range for stack symbol span valid full scope of function body, gap might apply.
S_DEFRANGE_REGISTER_REL = 0x1145u, // range for symbol address as register + offset.
S_LPROC32_ID = 0x1146u, // S_PROC symbol that references ID instead of type
S_GPROC32_ID = 0x1147u, // S_PROC symbol that references ID instead of type
S_BUILDINFO = 0x114Cu, // build info/environment details of a compiland/translation unit
S_INLINESITE = 0x114Du, // inlined function callsite
S_INLINESITE_END = 0x114Eu,
S_PROC_ID_END = 0x114Fu,
S_FILESTATIC = 0x1153u,
S_LPROC32_DPC = 0x1155u,
S_LPROC32_DPC_ID = 0x1156u,
S_ARMSWITCHTABLE = 0x1159u,
S_CALLEES = 0x115Au,
S_CALLERS = 0x115Bu,
S_INLINESITE2 = 0x115Du, // extended inline site information
S_HEAPALLOCSITE = 0x115Eu, // heap allocation site
S_INLINEES = 0x1168u, // https://llvm.org/docs/PDB/CodeViewSymbols.html#s-inlinees-0x1168
S_REGREL32_INDIR = 0x1171u,
S_REGREL32_ENCTMP = 0x1179u,
S_UDT = 0x1108u, // user-defined type
S_UDT_ST = 0x1003u, // user-defined structured types
};
// https://docs.microsoft.com/en-us/visualstudio/debugger/debug-interface-access/thunk-ordinal
enum class PDB_NO_DISCARD ThunkOrdinal : uint8_t
{
NoType,
ThisAdjustor,
VirtualCall,
PCode,
DelayLoad,
TrampolineIncremental,
TrampolineBranchIsland
};
enum class PDB_NO_DISCARD TrampolineType : uint16_t
{
Incremental,
BranchIsland
};
enum class PDB_NO_DISCARD CookieType : uint8_t
{
COPY = 0,
XOR_SP,
XOR_BP,
XOR_R13,
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvconst.h#L392
enum class PDB_NO_DISCARD Register : uint16_t
{
EAX = 17,
ECX = 18,
EDX = 19,
EBX = 20,
ESP = 21,
EBP = 22,
ESI = 23,
EDI = 24,
RAX = 328,
RBX = 329,
RCX = 330,
RDX = 331,
RSI = 332,
RDI = 333,
RBP = 334,
RSP = 335,
R8 = 336,
R9 = 337,
R10 = 338,
R11 = 339,
R12 = 340,
R13 = 341,
R14 = 342,
R15 = 343,
RIP = 33, // also EIP for x32
EFLAGS = 34, // same for x64 and x32
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L3038
enum class PDB_NO_DISCARD ProcedureFlags : uint8_t
{
None = 0u,
NoFPO = 1u << 0u,
InterruptReturn = 1u << 1u,
FarReturn = 1u << 2u,
NoReturn = 1u << 3u,
Unreachable = 1u << 4u,
CustomCallingConvention = 1u << 5u,
NoInline = 1u << 6u,
OptimizedDebugInformation = 1u << 7u
};
PDB_DEFINE_BIT_OPERATORS(ProcedureFlags);
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L3676
enum class PDB_NO_DISCARD PublicSymbolFlags : uint32_t
{
None = 0u,
Code = 1u << 0u, // set if public symbol refers to a code address
Function = 1u << 1u, // set if public symbol is a function
ManagedCode = 1u << 2u, // set if managed code (native or IL)
ManagedILCode = 1u << 3u // set if managed IL code
};
PDB_DEFINE_BIT_OPERATORS(PublicSymbolFlags);
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L3341
enum class PDB_NO_DISCARD CompileSymbolFlags : uint32_t
{
None = 0u,
SourceLanguageMask = 0xFFu,
EC = 1u << 8u,
NoDebugInfo = 1u << 9u,
LTCG = 1u << 10u,
NoDataAlign = 1u << 11u,
ManagedCodeOrDataPresent = 1u << 12u,
SecurityChecks = 1u << 13u,
HotPatch = 1u << 14u,
CVTCIL = 1u << 15u,
MSILModule = 1u << 16u,
SDL = 1u << 17u,
PGO = 1u << 18u,
Exp = 1u << 19u
};
PDB_DEFINE_BIT_OPERATORS(CompileSymbolFlags);
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvconst.h#L324
enum class PDB_NO_DISCARD CPUType : uint16_t
{
Intel8080 = 0x0,
Intel8086 = 0x1,
Intel80286 = 0x2,
Intel80386 = 0x3,
Intel80486 = 0x4,
Pentium = 0x5,
PentiumII = 0x6,
PentiumPro = PentiumII,
PentiumIII = 0x7,
MIPS = 0x10,
MIPSR4000 = MIPS,
MIPS16 = 0x11,
MIPS32 = 0x12,
MIPS64 = 0x13,
MIPSI = 0x14,
MIPSII = 0x15,
MIPSIII = 0x16,
MIPSIV = 0x17,
MIPSV = 0x18,
M68000 = 0x20,
M68010 = 0x21,
M68020 = 0x22,
M68030 = 0x23,
M68040 = 0x24,
Alpha = 0x30,
Alpha21164 = 0x31,
Alpha21164A = 0x32,
Alpha21264 = 0x33,
Alpha21364 = 0x34,
PPC601 = 0x40,
PPC603 = 0x41,
PPC604 = 0x42,
PPC620 = 0x43,
PPCFP = 0x44,
PPCBE = 0x45,
SH3 = 0x50,
SH3E = 0x51,
SH3DSP = 0x52,
SH4 = 0x53,
SHMedia = 0x54,
ARM3 = 0x60,
ARM4 = 0x61,
ARM4T = 0x62,
ARM5 = 0x63,
ARM5T = 0x64,
ARM6 = 0x65,
ARM_XMAC = 0x66,
ARM_WMMX = 0x67,
ARM7 = 0x68,
Omni = 0x70,
IA64 = 0x80,
IA64_1 = 0x80,
IA64_2 = 0x81,
CEE = 0x90,
AM33 = 0xA0,
M32R = 0xB0,
TriCore = 0xC0,
X64 = 0xD0,
AMD64 = X64,
EBC = 0xE0,
Thumb = 0xF0,
ARMNT = 0xF4,
ARM64 = 0xF6,
HybridX86ARM64 = 0xF7,
ARM64EC = 0xF8,
ARM64X = 0xF9,
D3D11_Shader = 0x100
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L3100
// represents an address range, used for optimized code debug info
struct LocalVariableAddressRange // defines a range of addresses
{
uint32_t offsetStart;
uint16_t isectionStart;
uint16_t length;
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L3108
// Represents the holes in overall address range, all address is pre-bbt.
// it is for compress and reduce the amount of relocations need.
struct LocalVariableAddressGap
{
uint16_t offset; // relative offset from the beginning of the live range.
uint16_t length; // length of this gap.
};
// https://github.com/microsoft/microsoft-pdb/blob/0fe89a942f9a0f8e061213313e438884f4c9b876/include/cvinfo.h#L4366
// https://github.com/microsoft/microsoft-pdb/blob/0fe89a942f9a0f8e061213313e438884f4c9b876/cvdump/dumpsym7.cpp#L5518
enum class ARMSwitchType : uint16_t
{
INT1 = 0, // signed byte
UINT1 = 1, // unsigned byte
INT2 = 2, // signed two byte
UINT2 = 3, // unsigned two byte
INT4 = 4, // signed four byte
UINT4 = 5, // unsigned four byte
POINTER = 6,
UINT1SHL1 = 7, // unsigned byte scaled by two
UINT2SHL1 = 8, // unsigned two byte scaled by two
INT1SHL1 = 9, // signed byte scaled by two
INT2SHL1 = 10, // signed two byte scaled by two
TBB = UINT1SHL1,
TBH = UINT2SHL1,
};
// https://llvm.org/docs/PDB/CodeViewTypes.html#leaf-types
struct RecordHeader
{
uint16_t size; // record length, not including this 2-byte field
SymbolRecordKind kind; // record kind
};
// all CodeView records are stored as a header, followed by variable-length data.
// internal Record structs such as S_PUB32, S_GDATA32, etc. correspond to the data layout of a CodeView record of that kind.
struct Record
{
RecordHeader header;
union Data
{
#pragma pack(push, 1)
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4069
struct
{
uint32_t cbFrame; // count of bytes of total frame of procedure
uint32_t cbPad; // count of bytes of padding in the frame
uint32_t offPad; // offset (relative to frame poniter) to where
// padding starts
uint32_t cbSaveRegs; // count of bytes of callee save registers
uint32_t offExHdlr; // offset of exception handler
uint16_t sectExHdlr; // section id of exception handler
struct {
uint32_t fHasAlloca : 1; // function uses _alloca()
uint32_t fHasSetJmp : 1; // function uses setjmp()
uint32_t fHasLongJmp : 1; // function uses longjmp()
uint32_t fHasInlAsm : 1; // function uses inline asm
uint32_t fHasEH : 1; // function has EH states
uint32_t fInlSpec : 1; // function was speced as inline
uint32_t fHasSEH : 1; // function has SEH
uint32_t fNaked : 1; // function is __declspec(naked)
uint32_t fSecurityChecks : 1; // function has buffer security check introduced by /GS.
uint32_t fAsyncEH : 1; // function compiled with /EHa
uint32_t fGSNoStackOrdering : 1; // function has /GS buffer checks, but stack ordering couldn't be done
uint32_t fWasInlined : 1; // function was inlined within another function
uint32_t fGSCheck : 1; // function is __declspec(strict_gs_check)
uint32_t fSafeBuffers : 1; // function is __declspec(safebuffers)
uint32_t encodedLocalBasePointer : 2; // record function's local pointer explicitly.
uint32_t encodedParamBasePointer : 2; // record function's parameter pointer explicitly.
uint32_t fPogoOn : 1; // function was compiled with PGO/PGU
uint32_t fValidCounts : 1; // Do we have valid Pogo counts?
uint32_t fOptSpeed : 1; // Did we optimize for speed?
uint32_t fGuardCF : 1; // function contains CFG checks (and no write checks)
uint32_t fGuardCFW : 1; // function contains CFW checks and/or instrumentation
uint32_t pad : 9; // must be zero
} flags;
} S_FRAMEPROC;
struct
{
uint32_t offset;
uint16_t section;
uint16_t annotationsCount; // number of zero-terminated annotation strings
PDB_FLEXIBLE_ARRAY_MEMBER(char, annotations); // sequence of zero-terminated annotation strings
} S_ANNOTATIONSYM;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L3696
struct
{
PublicSymbolFlags flags;
uint32_t offset;
uint16_t section;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_PUB32;
struct
{
uint32_t typeIndex;
uint32_t offset;
uint16_t section;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_GDATA32, S_GTHREAD32, S_LDATA32, S_LTHREAD32;
struct
{
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_UNAMESPACE;
struct
{
uint32_t signature;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_OBJNAME;
struct
{
TrampolineType type;
uint16_t size;
uint32_t thunkOffset;
uint32_t targetOffset;
uint16_t thunkSection;
uint16_t targetSection;
} S_TRAMPOLINE;
struct
{
uint16_t sectionNumber;
uint8_t alignment;
uint32_t rva;
uint32_t length;
uint32_t characteristics;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_SECTION;
struct
{
uint32_t size;
uint32_t characteristics;
uint32_t offset;
uint16_t section;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_COFFGROUP;
struct
{
uint32_t offset ; // offset of call site
uint16_t section; // section index of call site
uint16_t padding; // alignment padding field, must be zero
uint32_t typeIndex; // type index describing function signature
} S_CALLSITEINFO;
struct
{
uint32_t offset; // Frame relative offset
uint16_t reg; // Register index
CookieType cookietype; // Type of the cookie
uint8_t flags; // Flags describing this cookie
} S_FRAMECOOKIE;
struct
{
uint32_t parent;
uint32_t end;
uint32_t next;
uint32_t offset;
uint16_t section;
uint16_t length;
ThunkOrdinal thunk;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_THUNK32;
struct
{
uint32_t parent;
uint32_t end;
uint32_t next;
uint32_t codeSize;
uint32_t debugStart;
uint32_t debugEnd;
uint32_t typeIndex;
uint32_t offset;
uint16_t section;
ProcedureFlags flags;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_LPROC32, S_GPROC32, S_LPROC32_ID, S_GPROC32_ID, S_LPROC32_DPC, S_LPROC32_DPC_ID;
struct
{
uint32_t offset;
uint32_t typeIndex;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_BPRELSYM32;
struct
{
uint32_t offset;
uint32_t typeIndex;
Register reg;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_REGREL32, S_REGREL32_ENCTMP;
struct
{
uint32_t typeIndex;
Register reg;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_REGSYM;
struct
{
uint32_t parent;
uint32_t end;
uint32_t codeSize;
uint32_t offset;
uint16_t section;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_BLOCK32;
struct
{
uint32_t offset;
uint16_t section;
ProcedureFlags flags;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_LABEL32;
struct
{
uint32_t typeIndex;
uint16_t value;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_CONSTANT;
struct
{
uint32_t typeIndex; // refers to a type index in the IPI stream
} S_BUILDINFO;
struct
{
uint32_t parent; // pointer to the inliner
uint32_t end; // pointer to this block's end
uint32_t inlinee; // CV_ItemId of inlinee
PDB_FLEXIBLE_ARRAY_MEMBER(uint8_t, binaryAnnotations);
} S_INLINESITE;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4199
struct
{
uint32_t typeIndex; // type index
uint32_t moduleFilenameOffset; // index of mod filename in stringtable
struct
{
uint16_t fIsParam : 1; // variable is a parameter
uint16_t fAddrTaken : 1; // address is taken
uint16_t fCompGenx : 1; // variable is compiler generated
uint16_t fIsAggregate : 1; // the symbol is splitted in temporaries,
// which are treated by compiler as
// independent entities
uint16_t fIsAggregated : 1; // Counterpart of fIsAggregate - tells
// that it is a part of a fIsAggregate symbol
uint16_t fIsAliased : 1; // variable has multiple simultaneous lifetimes
uint16_t fIsAlias : 1; // represents one of the multiple simultaneous lifetimes
uint16_t fIsRetValue : 1; // represents a function return value
uint16_t fIsOptimizedOut : 1; // variable has no lifetimes
uint16_t fIsEnregGlob : 1; // variable is an enregistered global
uint16_t fIsEnregStat : 1; // variable is an enregistered static
uint16_t unused : 5; // must be zero
} flags;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_FILESTATIC;
struct
{
CompileSymbolFlags flags;
CPUType machine;
uint16_t versionFrontendMajor;
uint16_t versionFrontendMinor;
uint16_t versionFrontendBuild;
uint16_t versionFrontendQFE;
uint16_t versionBackendMajor;
uint16_t versionBackendMinor;
uint16_t versionBackendBuild;
uint16_t versionBackendQFE;
PDB_FLEXIBLE_ARRAY_MEMBER(char, version);
} S_COMPILE3;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L3372
struct
{
uint8_t flags;
PDB_FLEXIBLE_ARRAY_MEMBER(char, strings);
} S_ENVBLOCK;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4190
struct
{
uint32_t typeIndex;
struct
{
uint16_t fIsParam : 1; // variable is a parameter
uint16_t fAddrTaken : 1; // address is taken
uint16_t fCompGenx : 1; // variable is compiler generated
uint16_t fIsAggregate : 1; // the symbol is splitted in temporaries,
// which are treated by compiler as
// independent entities
uint16_t fIsAggregated : 1; // Counterpart of fIsAggregate - tells
// that it is a part of a fIsAggregate symbol
uint16_t fIsAliased : 1; // variable has multiple simultaneous lifetimes
uint16_t fIsAlias : 1; // represents one of the multiple simultaneous lifetimes
uint16_t fIsRetValue : 1; // represents a function return value
uint16_t fIsOptimizedOut : 1; // variable has no lifetimes
uint16_t fIsEnregGlob : 1; // variable is an enregistered global
uint16_t fIsEnregStat : 1; // variable is an enregistered static
uint16_t unused : 5; // must be zero
} flags;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_LOCAL;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4236
struct
{
uint16_t reg; // Register to hold the value of the symbol
struct
{
uint16_t maybe : 1; // May have no user name on one of control flow path.
uint16_t padding : 15; // Padding for future use.
} attribute; // Attribute of the register range.
LocalVariableAddressRange range; // Range of addresses where this program is valid
PDB_FLEXIBLE_ARRAY_MEMBER(LocalVariableAddressGap, gaps); // The value is not available in following gaps.
} S_DEFRANGE_REGISTER;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4245
struct
{
uint32_t offsetFramePointer;
LocalVariableAddressRange range; // Range of addresses where this program is valid
PDB_FLEXIBLE_ARRAY_MEMBER(LocalVariableAddressGap, gaps); // The value is not available in following gaps.
} S_DEFRANGE_FRAMEPOINTER_REL;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4265
struct
{
uint16_t reg; // Register to hold the value of the symbol
struct
{
uint16_t maybe : 1; // May have no user name on one of control flow path.
uint16_t padding : 15; // Padding for future use.
} attribute; // Attribute of the register range.
uint32_t offsetParent : 12; // Offset in parent variable.
uint32_t padding : 20; // Padding for future use.
LocalVariableAddressRange range; // Range of addresses where this program is valid
PDB_FLEXIBLE_ARRAY_MEMBER(LocalVariableAddressGap, gaps); // The value is not available in following gaps.
} S_DEFRANGE_SUBFIELD_REGISTER;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4255
struct
{
uint32_t offsetFramePointer; // offset to frame pointer
} S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4279
struct
{
uint16_t baseRegister; // Register to hold the base pointer of the symbol
uint16_t spilledUDTMember : 1; // Spilled member for s.i.
uint16_t padding : 3; // Padding for future use.
uint16_t offsetParent : 12; // Offset in parent variable.
uint32_t offsetBasePointer; // offset to register
LocalVariableAddressRange range; // Range of addresses where this program is valid
PDB_FLEXIBLE_ARRAY_MEMBER(LocalVariableAddressGap, gaps); // The value is not available in following gaps.
} S_DEFRANGE_REGISTER_REL;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4500
struct
{
uint32_t offset; // offset of call site
uint16_t section; // section index of call site
uint16_t instructionLength; // length of heap allocation call instruction
uint32_t typeIndex; // type index describing function signature
} S_HEAPALLOCSITE;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4402
struct
{
uint32_t offsetBase; // Section-relative offset to the base for switch offsets
uint16_t sectionBase; // Section index of the base for switch offsets
ARMSwitchType switchType; // type of each entry
uint32_t offsetBranch; // Section-relative offset to the table branch instruction
uint32_t offsetTable; // Section-relative offset to the start of the table
uint16_t sectionBranch; // Section index of the table branch instruction
uint16_t sectionTable; // Section index of the table
uint32_t numEntries; // number of switch table entries
} S_ARMSWITCHTABLE;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4382
struct
{
uint32_t count; // Number of functions
PDB_FLEXIBLE_ARRAY_MEMBER(uint32_t, funcs); // List of functions, dim == count
// uint32_t invocations[CV_ZEROLEN]; Followed by a parallel array of
// invocation counts. Counts > reclen are assumed to be zero
} S_CALLERS, S_CALLEES, S_INLINEES;
struct
{
uint32_t typeIndex;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_UDT, S_UDT_ST;
struct
{
uint32_t unknown1;
uint32_t typeIndex;
uint32_t unknown2;
Register reg;
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} S_REGREL32_INDIR;
#pragma pack(pop)
} data;
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4576
enum class PDB_NO_DISCARD DebugSubsectionKind : uint32_t
{
S_IGNORE = 0x80000000, // if this bit is set in a subsection type then ignore the subsection contents
S_SYMBOLS = 0xF1,
S_LINES = 0xF2,
S_STRINGTABLE = 0xF3,
S_FILECHECKSUMS = 0xF4,
S_FRAMEDATA = 0xF5,
S_INLINEELINES = 0xF6,
S_CROSSSCOPEIMPORTS = 0xF7,
S_CROSSSCOPEEXPORTS = 0xF8,
S_IL_LINES = 0xF9,
S_FUNC_MDTOKEN_MAP = 0xFA,
S_TYPE_MDTOKEN_MAP = 0xFB,
S_MERGED_ASSEMBLYINPUT = 0xFC,
S_COFF_SYMBOL_RVA = 0xFD,
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4596
struct DebugSubsectionHeader
{
DebugSubsectionKind kind;
uint32_t size;
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4617
struct Line
{
uint32_t offset; // Offset to start of code bytes for line number
uint32_t linenumStart : 24; // line where statement/expression starts
uint32_t deltaLineEnd : 7; // delta to line where statement ends (optional)
uint32_t fStatement : 1; // true if a statement linenumber, else an expression line num
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4630
struct Column
{
uint16_t start;
uint16_t end;
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4601
struct LinesHeader
{
uint32_t sectionOffset;
uint16_t sectionIndex;
struct
{
uint16_t fHasColumns : 1;
uint16_t pad : 15;
} flags;
uint32_t codeSize;
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4608
struct LinesFileBlockHeader
{
uint32_t fileChecksumOffset;
uint32_t numLines;
uint32_t size;
// Line lines[numLines];
// Column columns[numLines]; Might not be present
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvconst.h#L88
enum class PDB_NO_DISCARD ChecksumKind : uint8_t
{
None = 0,
MD5 = 1,
SHA1 = 2,
SHA256 = 3,
};
// https://github.com/microsoft/microsoft-pdb/blob/master/cvdump/dumpsym7.cpp#L1097
struct FileChecksumHeader
{
uint32_t filenameOffset;
uint8_t checksumSize;
ChecksumKind checksumKind;
PDB_FLEXIBLE_ARRAY_MEMBER(uint8_t, checksum);
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4822
enum class InlineeSourceLineKind : uint32_t
{
Signature = 0,
SignatureEx = 1,
};
struct InlineeSourceLineHeader
{
InlineeSourceLineKind kind;
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L4825
struct InlineeSourceLine
{
uint32_t inlinee;
uint32_t fileChecksumOffset;
uint32_t lineNumber;
};
struct InlineeSourceLineEx
{
uint32_t inlinee;
uint32_t fileChecksumOffset;
uint32_t lineNumber;
uint32_t extraLines;
PDB_FLEXIBLE_ARRAY_MEMBER(uint32_t, extrafileChecksumOffsets);
};
// Combine DebugSubsectionHeader and first subsection header into one struct.
struct LineSection
{
DebugSubsectionHeader header;
union
{
LinesHeader linesHeader;
FileChecksumHeader checksumHeader;
InlineeSourceLineHeader inlineeHeader;
};
};
}
}
}

View File

@@ -0,0 +1,115 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_DirectMSFStream.h"
#include "Foundation/PDB_PointerUtil.h"
#include "Foundation/PDB_BitUtil.h"
#include "Foundation/PDB_Assert.h"
#include "Foundation/PDB_CRT.h"
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::DirectMSFStream::DirectMSFStream(void) PDB_NO_EXCEPT
: m_data(nullptr)
, m_blockIndices(nullptr)
, m_blockSize(0u)
, m_size(0u)
, m_blockSizeLog2(0u)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::DirectMSFStream::DirectMSFStream(const void* data, uint32_t blockSize, const uint32_t* blockIndices, uint32_t streamSize) PDB_NO_EXCEPT
: m_data(data)
, m_blockIndices(blockIndices)
, m_blockSize(blockSize)
, m_size(streamSize)
, m_blockSizeLog2(BitUtil::FindFirstSetBit(blockSize))
{
PDB_ASSERT(BitUtil::IsPowerOfTwo(blockSize), "MSF block size must be a power of two.");
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
void PDB::DirectMSFStream::ReadAtOffset(void* destination, size_t size, size_t offset) const PDB_NO_EXCEPT
{
PDB_ASSERT(destination != nullptr, "Destination buffer not set");
PDB_ASSERT(offset + size <= m_size, "Not enough data left to read.");
// work out which block and offset within the block the read offset corresponds to
size_t blockIndex = offset >> m_blockSizeLog2;
const size_t offsetWithinBlock = offset & (m_blockSize - 1u);
// work out the offset within the data based on the block indices
size_t offsetWithinData = (static_cast<size_t>(m_blockIndices[blockIndex]) << m_blockSizeLog2) + offsetWithinBlock;
const size_t bytesLeftInBlock = m_blockSize - offsetWithinBlock;
if (bytesLeftInBlock >= size)
{
// fast path, all the data can be read in one go
const void* const sourceData = Pointer::Offset<const void*>(m_data, offsetWithinData);
memcpy(destination, sourceData, size);
}
else
{
// slower path, data is scattered across several blocks.
// read remaining bytes in current block first.
{
const void* const sourceData = Pointer::Offset<const void*>(m_data, offsetWithinData);
memcpy(destination, sourceData, bytesLeftInBlock);
}
// read remaining bytes from blocks
size_t bytesLeftToRead = size - bytesLeftInBlock;
while (bytesLeftToRead != 0u)
{
// advance to the next block
++blockIndex;
offsetWithinData = static_cast<size_t>(m_blockIndices[blockIndex]) << m_blockSizeLog2;
void* const destinationData = Pointer::Offset<void*>(destination, size - bytesLeftToRead);
const void* const sourceData = Pointer::Offset<const void*>(m_data, offsetWithinData);
if (bytesLeftToRead > m_blockSize)
{
// copy a whole block at once
memcpy(destinationData, sourceData, m_blockSize);
bytesLeftToRead -= m_blockSize;
}
else
{
// copy remaining bytes
memcpy(destinationData, sourceData, bytesLeftToRead);
bytesLeftToRead -= bytesLeftToRead;
}
}
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::DirectMSFStream::IndexAndOffset PDB::DirectMSFStream::GetBlockIndexForOffset(uint32_t offset) const PDB_NO_EXCEPT
{
// work out which block and offset within the block the offset corresponds to
const uint32_t blockIndex = offset >> m_blockSizeLog2;
const uint32_t offsetWithinBlock = offset & (m_blockSize - 1u);
return IndexAndOffset { blockIndex, offsetWithinBlock };
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD size_t PDB::DirectMSFStream::GetDataOffsetForIndexAndOffset(const IndexAndOffset& indexAndOffset) const PDB_NO_EXCEPT
{
// work out the offset within the data based on the block indices
const size_t offsetWithinData = (static_cast<size_t>(m_blockIndices[indexAndOffset.index]) << m_blockSizeLog2) + indexAndOffset.offsetWithinBlock;
return offsetWithinData;
}

View File

@@ -0,0 +1,84 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
// https://llvm.org/docs/PDB/index.html#the-msf-container
// https://llvm.org/docs/PDB/MsfFile.html
namespace PDB
{
// provides direct access to the data of an MSF stream.
// inherently thread-safe, the stream doesn't carry any internal offset or similar.
// trivial to construct.
// slower individual reads, but pays off when not all data of a stream is needed.
class PDB_NO_DISCARD DirectMSFStream
{
public:
DirectMSFStream(void) PDB_NO_EXCEPT;
explicit DirectMSFStream(const void* data, uint32_t blockSize, const uint32_t* blockIndices, uint32_t streamSize) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(DirectMSFStream);
// Reads a number of bytes from the stream.
void ReadAtOffset(void* destination, size_t size, size_t offset) const PDB_NO_EXCEPT;
// Reads from the stream.
template <typename T>
PDB_NO_DISCARD inline T ReadAtOffset(size_t offset) const PDB_NO_EXCEPT
{
T data;
ReadAtOffset(&data, sizeof(T), offset);
return data;
}
// Returns the block size of the stream.
PDB_NO_DISCARD inline uint32_t GetBlockSize(void) const PDB_NO_EXCEPT
{
return m_blockSize;
}
// Returns the size of the stream.
PDB_NO_DISCARD inline uint32_t GetSize(void) const PDB_NO_EXCEPT
{
return m_size;
}
private:
friend class CoalescedMSFStream;
struct IndexAndOffset
{
uint32_t index;
uint32_t offsetWithinBlock;
};
// Returns the block index and offset within the block that correspond to the given offset.
PDB_NO_DISCARD IndexAndOffset GetBlockIndexForOffset(uint32_t offset) const PDB_NO_EXCEPT;
// Returns the offset into the data that corresponds to the given indices and offset within a block.
PDB_NO_DISCARD size_t GetDataOffsetForIndexAndOffset(const IndexAndOffset& indexAndOffset) const PDB_NO_EXCEPT;
// Provides read-only access to the memory-mapped data.
PDB_NO_DISCARD inline const void* GetData(void) const PDB_NO_EXCEPT
{
return m_data;
}
// Provides read-only access to the block indices.
PDB_NO_DISCARD inline const uint32_t* GetBlockIndices(void) const PDB_NO_EXCEPT
{
return m_blockIndices;
}
const void* m_data;
const uint32_t* m_blockIndices;
uint32_t m_blockSize;
uint32_t m_size;
uint32_t m_blockSizeLog2;
PDB_DISABLE_COPY(DirectMSFStream);
};
}

View File

@@ -0,0 +1,26 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
namespace PDB
{
enum class PDB_NO_DISCARD ErrorCode : unsigned int
{
Success = 0u,
// main PDB validation
InvalidDataSize,
InvalidSuperBlock,
InvalidFreeBlockMap,
// stream validation
InvalidStream,
InvalidSignature,
InvalidStreamIndex,
UnknownVersion
};
}

View File

@@ -0,0 +1,43 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_GlobalSymbolStream.h"
#include "PDB_RawFile.h"
#include "PDB_Types.h"
#include "PDB_DBITypes.h"
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::GlobalSymbolStream::GlobalSymbolStream(void) PDB_NO_EXCEPT
: m_stream()
, m_hashRecords(nullptr)
, m_count(0u)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::GlobalSymbolStream::GlobalSymbolStream(const RawFile& file, uint16_t streamIndex, uint32_t count) PDB_NO_EXCEPT
: m_stream(file.CreateMSFStream<CoalescedMSFStream>(streamIndex))
, m_hashRecords(m_stream.GetDataAtOffset<HashRecord>(sizeof(HashTableHeader)))
, m_count(count)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD const PDB::CodeView::DBI::Record* PDB::GlobalSymbolStream::GetRecord(const CoalescedMSFStream& symbolRecordStream, const HashRecord& hashRecord) const PDB_NO_EXCEPT
{
// hash record offsets start at 1, not at 0
const uint32_t headerOffset = hashRecord.offset - 1u;
// the offset doesn't point to the global symbol directly, but to the CodeView record:
// https://llvm.org/docs/PDB/CodeViewSymbols.html
const CodeView::DBI::Record* record = symbolRecordStream.GetDataAtOffset<const CodeView::DBI::Record>(headerOffset);
return record;
}

View File

@@ -0,0 +1,49 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_ArrayView.h"
#include "PDB_CoalescedMSFStream.h"
namespace PDB
{
class RawFile;
struct HashRecord;
namespace CodeView
{
namespace DBI
{
struct Record;
}
}
class PDB_NO_DISCARD GlobalSymbolStream
{
public:
GlobalSymbolStream(void) PDB_NO_EXCEPT;
explicit GlobalSymbolStream(const RawFile& file, uint16_t streamIndex, uint32_t count) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(GlobalSymbolStream);
// Turns a given hash record into a DBI record using the given symbol stream.
PDB_NO_DISCARD const CodeView::DBI::Record* GetRecord(const CoalescedMSFStream& symbolRecordStream, const HashRecord& hashRecord) const PDB_NO_EXCEPT;
// Returns a view of all the records in the stream.
PDB_NO_DISCARD inline ArrayView<HashRecord> GetRecords(void) const PDB_NO_EXCEPT
{
return ArrayView<HashRecord>(m_hashRecords, m_count);
}
private:
CoalescedMSFStream m_stream;
const HashRecord* m_hashRecords;
uint32_t m_count;
PDB_DISABLE_COPY(GlobalSymbolStream);
};
}

View File

@@ -0,0 +1,140 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_IPIStream.h"
#include "PDB_RawFile.h"
#include "PDB_Util.h"
#include "PDB_DirectMSFStream.h"
#include "PDB_InfoStream.h"
#include "Foundation/PDB_Memory.h"
namespace
{
// the IPI stream always resides at index 4
static constexpr const uint32_t IPIStreamIndex = 4u;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::IPIStream::IPIStream(void) PDB_NO_EXCEPT
: m_header()
, m_stream()
, m_records(nullptr)
, m_recordCount(0u)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::IPIStream::IPIStream(IPIStream&& other) PDB_NO_EXCEPT
: m_header(PDB_MOVE(other.m_header))
, m_stream(PDB_MOVE(other.m_stream))
, m_records(PDB_MOVE(other.m_records))
, m_recordCount(PDB_MOVE(other.m_recordCount))
{
other.m_records = nullptr;
other.m_recordCount = 0u;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::IPIStream& PDB::IPIStream::operator=(IPIStream&& other) PDB_NO_EXCEPT
{
if (this != &other)
{
PDB_DELETE_ARRAY(m_records);
m_header = PDB_MOVE(other.m_header);
m_stream = PDB_MOVE(other.m_stream);
m_records = PDB_MOVE(other.m_records);
m_recordCount = PDB_MOVE(other.m_recordCount);
other.m_records = nullptr;
other.m_recordCount = 0u;
}
return *this;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::IPIStream::IPIStream(const RawFile& file, const IPI::StreamHeader& header) PDB_NO_EXCEPT
: m_header(header)
, m_stream(file.CreateMSFStream<CoalescedMSFStream>(IPIStreamIndex))
, m_records(nullptr)
, m_recordCount(GetLastTypeIndex() - GetFirstTypeIndex())
{
// types in the IPI stream are accessed by their index from other streams.
// however, the index is not stored with types in the IPI stream directly, but has to be built while walking the stream.
// similarly, because types are variable-length records, there are no direct offsets to access individual types.
// we therefore walk the IPI stream once, and store pointers to the records for trivial O(N) array lookup by index later.
m_records = PDB_NEW_ARRAY(const CodeView::IPI::Record*, m_recordCount);
// ignore the stream's header
size_t offset = sizeof(IPI::StreamHeader);
// parse the CodeView records
uint32_t typeIndex = 0u;
while (offset < m_stream.GetSize())
{
// https://llvm.org/docs/PDB/CodeViewTypes.html
const CodeView::IPI::Record* record = m_stream.GetDataAtOffset<const CodeView::IPI::Record>(offset);
const uint32_t recordSize = GetCodeViewRecordSize(record);
m_records[typeIndex] = record;
// position the stream offset at the next record
offset += sizeof(CodeView::IPI::RecordHeader) + recordSize;
++typeIndex;
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::IPIStream::~IPIStream(void) PDB_NO_EXCEPT
{
PDB_DELETE_ARRAY(m_records);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::HasValidIPIStream(const RawFile& file) PDB_NO_EXCEPT
{
const PDB::InfoStream infoStream(file);
if (!infoStream.HasIPIStream())
{
return ErrorCode::InvalidStream;
}
DirectMSFStream stream = file.CreateMSFStream<DirectMSFStream>(IPIStreamIndex);
if (stream.GetSize() < sizeof(IPI::StreamHeader))
{
return ErrorCode::InvalidStream;
}
const IPI::StreamHeader header = stream.ReadAtOffset<IPI::StreamHeader>(0u);
if (header.version != IPI::StreamHeader::Version::V80)
{
return ErrorCode::UnknownVersion;
}
return ErrorCode::Success;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::IPIStream PDB::CreateIPIStream(const RawFile& file) PDB_NO_EXCEPT
{
DirectMSFStream stream = file.CreateMSFStream<DirectMSFStream>(IPIStreamIndex);
const IPI::StreamHeader header = stream.ReadAtOffset<IPI::StreamHeader>(0u);
return IPIStream { file, header };
}

66
third_party/raw_pdb/src/PDB_IPIStream.h vendored Normal file
View File

@@ -0,0 +1,66 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_ArrayView.h"
#include "PDB_ErrorCodes.h"
#include "PDB_IPITypes.h"
#include "PDB_CoalescedMSFStream.h"
// PDB IPI stream
// https://llvm.org/docs/PDB/TpiStream.html
namespace PDB
{
class RawFile;
class PDB_NO_DISCARD IPIStream
{
public:
IPIStream(void) PDB_NO_EXCEPT;
IPIStream(IPIStream&& other) PDB_NO_EXCEPT;
IPIStream& operator=(IPIStream&& other) PDB_NO_EXCEPT;
explicit IPIStream(const RawFile& file, const IPI::StreamHeader& header) PDB_NO_EXCEPT;
~IPIStream(void) PDB_NO_EXCEPT;
// Returns the index of the first type, which is not necessarily zero.
PDB_NO_DISCARD inline uint32_t GetFirstTypeIndex(void) const PDB_NO_EXCEPT
{
return m_header.typeIndexBegin;
}
// Returns the index of the last type.
PDB_NO_DISCARD inline uint32_t GetLastTypeIndex(void) const PDB_NO_EXCEPT
{
return m_header.typeIndexEnd;
}
// Returns a view of all type records.
// Records identified by a type index can be accessed via "allRecords[typeIndex - firstTypeIndex]".
PDB_NO_DISCARD inline ArrayView<const CodeView::IPI::Record*> GetTypeRecords(void) const PDB_NO_EXCEPT
{
return ArrayView<const CodeView::IPI::Record*>(m_records, m_recordCount);
}
private:
IPI::StreamHeader m_header;
CoalescedMSFStream m_stream;
const CodeView::IPI::Record** m_records;
size_t m_recordCount;
PDB_DISABLE_COPY(IPIStream);
};
// ------------------------------------------------------------------------------------------------
// General
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD ErrorCode HasValidIPIStream(const RawFile& file) PDB_NO_EXCEPT;
PDB_NO_DISCARD IPIStream CreateIPIStream(const RawFile& file) PDB_NO_EXCEPT;
}

144
third_party/raw_pdb/src/PDB_IPITypes.h vendored Normal file
View File

@@ -0,0 +1,144 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
namespace PDB
{
namespace IPI
{
// https://llvm.org/docs/PDB/TpiStream.html#tpi-header
struct StreamHeader
{
enum class PDB_NO_DISCARD Version : uint32_t
{
V40 = 19950410u,
V41 = 19951122u,
V50 = 19961031u,
V70 = 19990903u,
V80 = 20040203u
};
Version version;
uint32_t headerSize;
uint32_t typeIndexBegin;
uint32_t typeIndexEnd;
uint32_t typeRecordBytes;
uint16_t hashStreamIndex;
uint16_t hashAuxStreamIndex;
uint32_t hashKeySize;
uint32_t hashBucketCount;
uint32_t hashValueBufferOffset;
uint32_t hashValueBufferLength;
uint32_t indexOffsetBufferOffset;
uint32_t indexOffsetBufferLength;
uint32_t hashAdjBufferOffset;
uint32_t hashAdjBufferLength;
};
}
namespace CodeView
{
namespace IPI
{
// code view type records that can appear in an IPI stream
// https://llvm.org/docs/PDB/CodeViewTypes.html
// https://llvm.org/docs/PDB/TpiStream.html#tpi-vs-ipi-stream
enum class PDB_NO_DISCARD TypeRecordKind : uint16_t
{
LF_FUNC_ID = 0x1601u, // global function ID
LF_MFUNC_ID = 0x1602u, // member function ID
LF_BUILDINFO = 0x1603u, // build information
LF_SUBSTR_LIST = 0x1604u, // similar to LF_ARGLIST for a list of substrings
LF_STRING_ID = 0x1605u, // string ID
LF_UDT_SRC_LINE = 0x1606u, // source and line on where an UDT (User Defined Type) is defined, generated by the compiler
LF_UDT_MOD_SRC_LINE = 0x1607u // module, source and line on where an UDT is defined, generated by the linker
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1715
enum class PDB_NO_DISCARD BuildInfoType : uint8_t
{
CurrentDirectory, // compiler working directory
BuildTool, // tool path
SourceFile, // path to source file, relative or absolute
TypeServerPDB, // path to PDB file
CommandLine // command-line used to build the source file
};
struct RecordHeader
{
uint16_t size; // record length, not including this 2-byte field
TypeRecordKind kind; // record kind
};
// all CodeView records are stored as a header, followed by variable-length data.
// internal Record structs such as S_PUB32, S_GDATA32, etc. correspond to the data layout of a CodeView record of that kind.
struct Record
{
RecordHeader header;
union Data
{
#pragma pack(push, 1)
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1680
struct
{
uint32_t scopeId; // parent scope of the ID, 0 if global
uint32_t typeIndex; // function type
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} LF_FUNC_ID;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1687
struct
{
uint32_t parentTypeIndex; // parent scope of the ID, 0 if global
uint32_t typeIndex; // function type
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} LF_MFUNC_ID;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1694
struct
{
uint32_t id; // ID to list of sub-string IDs
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} LF_STRING_ID;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1700
struct
{
uint32_t typeIndex; // UDT's type index
uint32_t stringIndex; // index to LF_STRING_ID record where source file name is saved
uint32_t line; // line number
} LF_UDT_SRC_LINE;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1707
struct
{
uint32_t typeIndex; // UDT's type index
uint32_t stringIndex; // index into '/names' string table where source file name is saved
uint32_t line; // line number
uint16_t moduleIndex; // module that contributes this UDT definition
} LF_UDT_MOD_SRC_LINE;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2043
struct
{
uint32_t count;
PDB_FLEXIBLE_ARRAY_MEMBER(uint32_t, typeIndices);
} LF_SUBSTR_LIST;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1726
struct
{
uint16_t count;
PDB_FLEXIBLE_ARRAY_MEMBER(uint32_t, typeIndices);
} LF_BUILDINFO;
#pragma pack(pop)
} data;
};
}
}
}

View File

@@ -0,0 +1,47 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_ImageSectionStream.h"
#include "PDB_RawFile.h"
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ImageSectionStream::ImageSectionStream(void) PDB_NO_EXCEPT
: m_stream()
, m_headers(nullptr)
, m_count(0u)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ImageSectionStream::ImageSectionStream(const RawFile& file, uint16_t streamIndex) PDB_NO_EXCEPT
: m_stream(file.CreateMSFStream<CoalescedMSFStream>(streamIndex))
, m_headers(m_stream.GetDataAtOffset<IMAGE_SECTION_HEADER>(0u))
, m_count(m_stream.GetSize() / sizeof(IMAGE_SECTION_HEADER))
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD uint32_t PDB::ImageSectionStream::ConvertSectionOffsetToRVA(uint16_t oneBasedSectionIndex, uint32_t offsetInSection) const PDB_NO_EXCEPT
{
if (oneBasedSectionIndex == 0u)
{
// should never happen, but prevent underflow
return 0u;
}
else if (oneBasedSectionIndex > m_count)
{
// this symbol is "contained" in a section that is neither part of the PDB, nor the EXE.
// it is a special compiler-generated or linker-generated symbol such as CFG symbols (e.g. __guard_fids_count, __guard_flags).
// we can safely ignore those symbols.
return 0u;
}
return m_headers[oneBasedSectionIndex - 1u].VirtualAddress + offsetInSection;
}

View File

@@ -0,0 +1,42 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_ArrayView.h"
#include "PDB_Types.h"
#include "PDB_CoalescedMSFStream.h"
namespace PDB
{
class RawFile;
struct IMAGE_SECTION_HEADER;
class PDB_NO_DISCARD ImageSectionStream
{
public:
ImageSectionStream(void) PDB_NO_EXCEPT;
explicit ImageSectionStream(const RawFile& file, uint16_t streamIndex) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(ImageSectionStream);
// Converts a one-based section offset into an RVA.
PDB_NO_DISCARD uint32_t ConvertSectionOffsetToRVA(uint16_t oneBasedSectionIndex, uint32_t offsetInSection) const PDB_NO_EXCEPT;
// Returns a view of all the sections in the stream.
PDB_NO_DISCARD inline ArrayView<IMAGE_SECTION_HEADER> GetImageSections(void) const PDB_NO_EXCEPT
{
return ArrayView<IMAGE_SECTION_HEADER>(m_headers, m_count);
}
private:
CoalescedMSFStream m_stream;
const IMAGE_SECTION_HEADER* m_headers;
size_t m_count;
PDB_DISABLE_COPY(ImageSectionStream);
};
}

View File

@@ -0,0 +1,102 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_InfoStream.h"
#include "PDB_RawFile.h"
#include "Foundation/PDB_CRT.h"
namespace
{
// the PDB info stream always resides at index 1
static constexpr const uint32_t InfoStreamIndex = 1u;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::InfoStream::InfoStream(void) PDB_NO_EXCEPT
: m_stream()
, m_header(nullptr)
, m_namesStreamIndex(0)
, m_usesDebugFastlink(false)
, m_hasIPIStream(false)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::InfoStream::InfoStream(const RawFile& file) PDB_NO_EXCEPT
: m_stream(file.CreateMSFStream<CoalescedMSFStream>(InfoStreamIndex))
, m_header(m_stream.GetDataAtOffset<const Header>(0u))
, m_namesStreamIndex(0)
, m_usesDebugFastlink(false)
, m_hasIPIStream(false)
{
// the info stream starts with the header, followed by the named stream map, followed by the feature codes
// https://llvm.org/docs/PDB/PdbStream.html#named-stream-map
size_t streamOffset = sizeof(Header);
const NamedStreamMap* namedStreamMap = m_stream.GetDataAtOffset<const NamedStreamMap>(streamOffset);
streamOffset += sizeof(NamedStreamMap) + namedStreamMap->length;
const SerializedHashTable::Header* hashTableHeader = m_stream.GetDataAtOffset<const SerializedHashTable::Header>(streamOffset);
streamOffset += sizeof(SerializedHashTable::Header);
const SerializedHashTable::BitVector* presentBitVector = m_stream.GetDataAtOffset<const SerializedHashTable::BitVector>(streamOffset);
streamOffset += sizeof(SerializedHashTable::BitVector) + sizeof(uint32_t) * presentBitVector->wordCount;
const SerializedHashTable::BitVector* deletedBitVector = m_stream.GetDataAtOffset<const SerializedHashTable::BitVector>(streamOffset);
streamOffset += sizeof(SerializedHashTable::BitVector) + sizeof(uint32_t) * deletedBitVector->wordCount;
// the hash table entries can be used to identify the indices of certain common streams like:
// "/UDTSRCLINEUNDONE"
// "/src/headerblock"
// "/LinkInfo"
// "/TMCache"
// "/names"
const NamedStreamMap::HashTableEntry* namedStreamMapHashEntries = m_stream.GetDataAtOffset<const NamedStreamMap::HashTableEntry>(streamOffset);
// Find "/names" stream, used to look up filenames for lines.
for (uint32_t i = 0, size = hashTableHeader->size; i < size; ++i)
{
const NamedStreamMap::HashTableEntry& entry = namedStreamMapHashEntries[i];
const char* streamName = &namedStreamMap->stringTable[entry.stringTableOffset];
if (strcmp("/names", streamName) == 0)
{
m_namesStreamIndex = entry.streamIndex;
}
}
streamOffset += sizeof(NamedStreamMap::HashTableEntry) * hashTableHeader->size;
// read feature codes by consuming remaining bytes
// https://llvm.org/docs/PDB/PdbStream.html#pdb-feature-codes
const FeatureCode* featureCodes = m_stream.GetDataAtOffset<const FeatureCode>(streamOffset);
const size_t remainingBytes = m_stream.GetSize() - streamOffset;
const size_t count = remainingBytes / sizeof(FeatureCode);
for (size_t i=0u; i < count; ++i)
{
FeatureCode code = featureCodes[i];
if (code == PDB::FeatureCode::MinimalDebugInfo)
{
m_usesDebugFastlink = true;
}
else if (code == PDB::FeatureCode::VC110 || code == PDB::FeatureCode::VC140)
{
m_hasIPIStream = true;
}
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::NamesStream PDB::InfoStream::CreateNamesStream(const RawFile& file) const PDB_NO_EXCEPT
{
return NamesStream(file, m_namesStreamIndex);
}

View File

@@ -0,0 +1,62 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "PDB_Types.h"
#include "PDB_CoalescedMSFStream.h"
#include "PDB_NamesStream.h"
namespace PDB
{
class RawFile;
// PDB Info Stream
// https://llvm.org/docs/PDB/PdbStream.html
class PDB_NO_DISCARD InfoStream
{
public:
InfoStream(void) PDB_NO_EXCEPT;
explicit InfoStream(const RawFile& file) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(InfoStream);
// Returns the header of the stream.
PDB_NO_DISCARD inline const Header* GetHeader(void) const PDB_NO_EXCEPT
{
return m_header;
}
// Returns whether the module has a names stream.
PDB_NO_DISCARD inline bool HasNamesStream(void) const PDB_NO_EXCEPT
{
return (m_namesStreamIndex != 0u);
}
// Returns whether the PDB file was linked using /DEBUG:FASTLINK.
PDB_NO_DISCARD inline bool UsesDebugFastLink(void) const PDB_NO_EXCEPT
{
return m_usesDebugFastlink;
}
// Returns whether the PDB file has an IPI stream.
PDB_NO_DISCARD inline bool HasIPIStream(void) const PDB_NO_EXCEPT
{
return m_hasIPIStream;
}
// Create names stream
PDB_NO_DISCARD NamesStream CreateNamesStream(const RawFile& file) const PDB_NO_EXCEPT;
private:
CoalescedMSFStream m_stream;
const Header* m_header;
uint32_t m_namesStreamIndex;
bool m_usesDebugFastlink;
bool m_hasIPIStream;
PDB_DISABLE_COPY(InfoStream);
};
}

View File

@@ -0,0 +1,184 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_ModuleInfoStream.h"
#include "Foundation/PDB_Memory.h"
#include "Foundation/PDB_CRT.h"
namespace
{
static constexpr const char* LinkerSymbolName("* Linker *");
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD static inline size_t EstimateModuleCount(size_t streamSize) PDB_NO_EXCEPT
{
// work out how many modules are stored in the stream at most.
// the module info is stored in variable-length records, so we can't determine the exact number without walking the stream.
return streamSize / sizeof(PDB::DBI::ModuleInfo);
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ModuleInfoStream::Module::Module(void) PDB_NO_EXCEPT
: m_info(nullptr)
, m_name(nullptr)
, m_nameLength(0u)
, m_objectName(nullptr)
, m_objectNameLength(0u)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ModuleInfoStream::Module::Module(const DBI::ModuleInfo* info, const char* name, size_t nameLength, const char* objectName, size_t objectNameLength) PDB_NO_EXCEPT
: m_info(info)
, m_name(name)
, m_nameLength(nameLength)
, m_objectName(objectName)
, m_objectNameLength(objectNameLength)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD bool PDB::ModuleInfoStream::Module::HasSymbolStream(void) const PDB_NO_EXCEPT
{
const uint16_t streamIndex = m_info->moduleSymbolStreamIndex;
// some modules don't have a symbol stream, i.e. no additional debug information is present.
// this usually happens when private symbols are stripped from a PDB.
return (streamIndex != 0xFFFFu);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD bool PDB::ModuleInfoStream::Module::HasLineStream(void) const PDB_NO_EXCEPT
{
return (m_info->c13Size > 0);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ModuleSymbolStream PDB::ModuleInfoStream::Module::CreateSymbolStream(const RawFile& file) const PDB_NO_EXCEPT
{
PDB_ASSERT(HasSymbolStream(), "Module symbol stream index is invalid.");
return ModuleSymbolStream(file, m_info->moduleSymbolStreamIndex, m_info->symbolSize);
}
PDB_NO_DISCARD PDB::ModuleLineStream PDB::ModuleInfoStream::Module::CreateLineStream(const RawFile& file) const PDB_NO_EXCEPT
{
PDB_ASSERT(HasLineStream(), "Module line stream is not present.");
return ModuleLineStream(file, m_info->moduleSymbolStreamIndex, m_info->symbolSize + m_info->c11Size + m_info->c13Size, m_info->symbolSize + m_info->c11Size);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ModuleInfoStream::ModuleInfoStream(void) PDB_NO_EXCEPT
: m_stream()
, m_modules(nullptr)
, m_moduleCount(0u)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ModuleInfoStream::ModuleInfoStream(ModuleInfoStream&& other) PDB_NO_EXCEPT
: m_stream(PDB_MOVE(other.m_stream))
, m_modules(PDB_MOVE(other.m_modules))
, m_moduleCount(PDB_MOVE(other.m_moduleCount))
{
other.m_modules = nullptr;
other.m_moduleCount = 0u;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ModuleInfoStream& PDB::ModuleInfoStream::operator=(ModuleInfoStream&& other) PDB_NO_EXCEPT
{
if (this != &other)
{
PDB_DELETE_ARRAY(m_modules);
m_stream = PDB_MOVE(other.m_stream);
m_modules = PDB_MOVE(other.m_modules);
m_moduleCount = PDB_MOVE(other.m_moduleCount);
other.m_modules = nullptr;
other.m_moduleCount = 0u;
}
return *this;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ModuleInfoStream::ModuleInfoStream(const DirectMSFStream& directStream, uint32_t size, uint32_t offset) PDB_NO_EXCEPT
: m_stream(directStream, size, offset)
, m_modules(nullptr)
, m_moduleCount(0u)
{
m_modules = PDB_NEW_ARRAY(Module, EstimateModuleCount(size));
size_t streamOffset = 0u;
while (streamOffset < size)
{
const DBI::ModuleInfo* moduleInfo = m_stream.GetDataAtOffset<const DBI::ModuleInfo>(streamOffset);
streamOffset += sizeof(DBI::ModuleInfo);
const char* name = m_stream.GetDataAtOffset<const char>(streamOffset);
const size_t nameLength = strlen(name);
streamOffset += nameLength + 1u;
const char* objectName = m_stream.GetDataAtOffset<const char>(streamOffset);
const size_t objectNameLength = strlen(objectName);
streamOffset += objectNameLength + 1u;
// the stream is aligned to 4 bytes
streamOffset = BitUtil::RoundUpToMultiple<size_t>(streamOffset, 4ul);
m_modules[m_moduleCount] = Module(moduleInfo, name, nameLength, objectName, objectNameLength);
++m_moduleCount;
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ModuleInfoStream::~ModuleInfoStream(void) PDB_NO_EXCEPT
{
PDB_DELETE_ARRAY(m_modules);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD const PDB::ModuleInfoStream::Module* PDB::ModuleInfoStream::FindLinkerModule(void) const PDB_NO_EXCEPT
{
const size_t count = m_moduleCount;
for (size_t i = 0u; i < count; ++i)
{
// with both MSVC cl.exe and Clang, the linker symbol is the last one to be stored, so start searching from the end
const Module& module = m_modules[count - i - 1u];
// check if this is the linker symbol
if (strcmp(module.GetName().Decay(), LinkerSymbolName) == 0)
{
return &module;
}
}
return nullptr;
}

View File

@@ -0,0 +1,104 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_ArrayView.h"
#include "PDB_CoalescedMSFStream.h"
#include "PDB_ModuleSymbolStream.h"
#include "PDB_ModuleLineStream.h"
namespace PDB
{
class PDB_NO_DISCARD DirectMSFStream;
class PDB_NO_DISCARD ModuleInfoStream
{
public:
class PDB_NO_DISCARD Module
{
public:
Module(void) PDB_NO_EXCEPT;
explicit Module(const DBI::ModuleInfo* info, const char* name, size_t nameLength, const char* objectName, size_t objectNameLength) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(Module);
// Returns whether the module has a symbol stream.
PDB_NO_DISCARD bool HasSymbolStream(void) const PDB_NO_EXCEPT;
// Returns whether the module has a line stream.
PDB_NO_DISCARD bool HasLineStream(void) const PDB_NO_EXCEPT;
// Creates a symbol stream for the module.
PDB_NO_DISCARD ModuleSymbolStream CreateSymbolStream(const RawFile& file) const PDB_NO_EXCEPT;
// Create a line stream for the module
PDB_NO_DISCARD ModuleLineStream CreateLineStream(const RawFile& file) const PDB_NO_EXCEPT;
// Returns the PDB module info.
PDB_NO_DISCARD inline const DBI::ModuleInfo* GetInfo(void) const PDB_NO_EXCEPT
{
return m_info;
}
// Returns the name of the module.
PDB_NO_DISCARD inline ArrayView<char> GetName(void) const PDB_NO_EXCEPT
{
return ArrayView<char>(m_name, m_nameLength);
}
// Returns the name of the object file of the module.
PDB_NO_DISCARD inline ArrayView<char> GetObjectName(void) const PDB_NO_EXCEPT
{
return ArrayView<char>(m_objectName, m_objectNameLength);
}
private:
// the module info is stored in variable-length arrays inside the stream, so rather than store an array directly,
// we need to store pointers to the individual data items inside the stream.
const DBI::ModuleInfo* m_info;
// the module name, e.g. the path to an object file or import library such as "Import:kernel32.dll"
const char* m_name;
size_t m_nameLength;
// the name of the object file. either the same as the module name, or the path to the archive that contained the module
const char* m_objectName;
size_t m_objectNameLength;
PDB_DISABLE_COPY(Module);
};
ModuleInfoStream(void) PDB_NO_EXCEPT;
ModuleInfoStream(ModuleInfoStream&& other) PDB_NO_EXCEPT;
ModuleInfoStream& operator=(ModuleInfoStream&& other) PDB_NO_EXCEPT;
explicit ModuleInfoStream(const DirectMSFStream& directStream, uint32_t size, uint32_t offset) PDB_NO_EXCEPT;
~ModuleInfoStream(void) PDB_NO_EXCEPT;
// Tries to find the linker module corresponding to the linker, i.e. the module named "* Linker *".
PDB_NO_DISCARD const Module* FindLinkerModule(void) const PDB_NO_EXCEPT;
// Returns the module with the given index.
PDB_NO_DISCARD inline const Module& GetModule(uint32_t index) const PDB_NO_EXCEPT
{
return m_modules[index];
}
// Returns a view of all modules in the info stream.
PDB_NO_DISCARD inline ArrayView<Module> GetModules(void) const PDB_NO_EXCEPT
{
return ArrayView<Module>(m_modules, m_moduleCount);
}
private:
CoalescedMSFStream m_stream;
Module* m_modules;
size_t m_moduleCount;
PDB_DISABLE_COPY(ModuleInfoStream);
};
}

View File

@@ -0,0 +1,31 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_ModuleLineStream.h"
#include "PDB_RawFile.h"
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ModuleLineStream::ModuleLineStream(void) PDB_NO_EXCEPT
: m_stream(), m_c13LineInfoOffset(0)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ModuleLineStream::ModuleLineStream(const RawFile& file, uint16_t streamIndex, uint32_t streamSize, size_t c13LineInfoOffset) PDB_NO_EXCEPT
: m_stream(file.CreateMSFStream<CoalescedMSFStream>(streamIndex, streamSize)), m_c13LineInfoOffset(c13LineInfoOffset)
{
// https://llvm.org/docs/PDB/ModiStream.html
// struct ModiStream {
// uint32_t Signature;
// uint8_t Symbols[SymbolSize - 4];
// uint8_t C11LineInfo[C11Size];
// uint8_t C13LineInfo[C13Size];
// uint32_t GlobalRefsSize;
// uint8_t GlobalRefs[GlobalRefsSize];
// };
}

View File

@@ -0,0 +1,151 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_BitUtil.h"
#include "PDB_DBITypes.h"
#include "PDB_Util.h"
#include "PDB_CoalescedMSFStream.h"
namespace PDB
{
class RawFile;
class PDB_NO_DISCARD ModuleLineStream
{
public:
ModuleLineStream(void) PDB_NO_EXCEPT;
explicit ModuleLineStream(const RawFile& file, uint16_t streamIndex, uint32_t streamSize, size_t c13LineInfoOffset) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(ModuleLineStream);
template <typename F>
void ForEachSection(F&& functor) const PDB_NO_EXCEPT
{
size_t offset = m_c13LineInfoOffset;
// read the line stream sections
while (offset < m_stream.GetSize())
{
const CodeView::DBI::LineSection* section = m_stream.GetDataAtOffset<const CodeView::DBI::LineSection>(offset);
functor(section);
offset = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::DebugSubsectionHeader) + section->header.size, 4u);
}
}
template <typename F>
void ForEachLinesBlock(const CodeView::DBI::LineSection* section, F&& functor) const PDB_NO_EXCEPT
{
PDB_ASSERT(section->header.kind == CodeView::DBI::DebugSubsectionKind::S_LINES,
"DebugSubsectionHeader::Kind %X != S_LINES (%X)",
static_cast<uint32_t>(section->header.kind), static_cast<uint32_t>(CodeView::DBI::DebugSubsectionKind::S_LINES));
size_t offset = m_stream.GetPointerOffset(section);
const size_t headerEnd = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::DebugSubsectionHeader) + section->header.size, 4u);
offset = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::DebugSubsectionHeader) + sizeof(CodeView::DBI::LinesHeader), 4u);
// read all blocks of lines
while (offset < headerEnd)
{
const CodeView::DBI::LinesFileBlockHeader* linesBlockHeader = m_stream.GetDataAtOffset<const CodeView::DBI::LinesFileBlockHeader>(offset);
const CodeView::DBI::Line* blockLines = m_stream.GetDataAtOffset<const CodeView::DBI::Line>(offset + sizeof(CodeView::DBI::LinesFileBlockHeader));
const size_t blockColumnsOffset = sizeof(CodeView::DBI::LinesFileBlockHeader) + (linesBlockHeader->numLines * (sizeof(CodeView::DBI::Line)));
const CodeView::DBI::Column* blockColumns = blockColumnsOffset < linesBlockHeader->size ? m_stream.GetDataAtOffset<const CodeView::DBI::Column>(offset) : nullptr;
functor(linesBlockHeader, blockLines, blockColumns);
offset = BitUtil::RoundUpToMultiple<size_t>(offset + linesBlockHeader->size, 4u);
}
PDB_ASSERT(offset == headerEnd, "Mismatch between offset %zu and header end %zu when reading lines blocks", offset, headerEnd);
}
template <typename F>
void ForEachFileChecksum(const CodeView::DBI::LineSection* section, F&& functor) const PDB_NO_EXCEPT
{
PDB_ASSERT(section->header.kind == CodeView::DBI::DebugSubsectionKind::S_FILECHECKSUMS,
"DebugSubsectionHeader::Kind %X != S_FILECHECKSUMS (%X)",
static_cast<uint32_t>(section->header.kind), static_cast<uint32_t>(CodeView::DBI::DebugSubsectionKind::S_FILECHECKSUMS));
size_t offset = m_stream.GetPointerOffset(section);
const size_t headerEnd = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::DebugSubsectionHeader) + section->header.size, 4u);
offset = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::DebugSubsectionHeader), 4u);
// read all file checksums
while (offset < headerEnd)
{
const CodeView::DBI::FileChecksumHeader* fileChecksumHeader = m_stream.GetDataAtOffset<const CodeView::DBI::FileChecksumHeader>(offset);
functor(fileChecksumHeader);
offset = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::FileChecksumHeader) + fileChecksumHeader->checksumSize, 4u);
}
PDB_ASSERT(offset == headerEnd, "Mismatch between offset %zu and header end %zu when reading file checksums", offset, headerEnd);
}
template <typename F>
void ForEachInlineeSourceLine(const CodeView::DBI::LineSection* section, F&& functor) const PDB_NO_EXCEPT
{
PDB_ASSERT(section->header.kind == CodeView::DBI::DebugSubsectionKind::S_INLINEELINES,
"DebugSubsectionHeader::Kind %X != S_INLINEELINES (%X)",
static_cast<uint32_t>(section->header.kind), static_cast<uint32_t>(CodeView::DBI::DebugSubsectionKind::S_INLINEELINES));
PDB_ASSERT(section->inlineeHeader.kind == CodeView::DBI::InlineeSourceLineKind::Signature,
"InlineeSourceLineKind %X != :InlineeSourceLineKind::Signature (%X)", static_cast<uint32_t>(section->header.kind), static_cast<uint32_t>(CodeView::DBI::InlineeSourceLineKind::Signature));
size_t offset = m_stream.GetPointerOffset(section);
const size_t headerEnd = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::DebugSubsectionHeader) + section->header.size, 4u);
offset = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::DebugSubsectionHeader) + sizeof(CodeView::DBI::InlineeSourceLineHeader), 4u);
// read all file checksums
while (offset < headerEnd)
{
const CodeView::DBI::InlineeSourceLine* inlineeSourceLine = m_stream.GetDataAtOffset<const CodeView::DBI::InlineeSourceLine>(offset);
functor(inlineeSourceLine);
offset = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::InlineeSourceLine), 4u);
}
}
template <typename F>
void ForEachInlineeSourceLineEx(const CodeView::DBI::LineSection* section, F&& functor) const PDB_NO_EXCEPT
{
PDB_ASSERT(section->header.kind == CodeView::DBI::DebugSubsectionKind::S_INLINEELINES,
"DebugSubsectionHeader::Kind %X != S_INLINEELINES (%X)", static_cast<uint32_t>(section->header.kind), static_cast<uint32_t>(CodeView::DBI::DebugSubsectionKind::S_INLINEELINES));
PDB_ASSERT(section->inlineeHeader.kind == CodeView::DBI::InlineeSourceLineKind::SignatureEx,
"InlineeSourceLineKind %X != :InlineeSourceLineKind::SignatureEx (%X)", static_cast<uint32_t>(section->header.kind), static_cast<uint32_t>(CodeView::DBI::InlineeSourceLineKind::SignatureEx));
size_t offset = m_stream.GetPointerOffset(section);
const size_t headerEnd = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::DebugSubsectionHeader) + section->header.size, 4u);
offset = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::DebugSubsectionHeader) + sizeof(CodeView::DBI::InlineeSourceLineHeader), 4u);
// read all file checksums
while (offset < headerEnd)
{
const CodeView::DBI::InlineeSourceLineEx* inlineeSourceLineEx = m_stream.GetDataAtOffset<const CodeView::DBI::InlineeSourceLineEx>(offset);
functor(inlineeSourceLineEx);
offset = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::InlineeSourceLineEx) + (inlineeSourceLineEx->extraLines * sizeof(uint32_t)), 4u);
}
}
private:
CoalescedMSFStream m_stream;
size_t m_c13LineInfoOffset;
PDB_DISABLE_COPY(ModuleLineStream);
};
}

View File

@@ -0,0 +1,61 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_ModuleSymbolStream.h"
#include "PDB_RawFile.h"
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ModuleSymbolStream::ModuleSymbolStream(void) PDB_NO_EXCEPT
: m_stream()
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::ModuleSymbolStream::ModuleSymbolStream(const RawFile& file, uint16_t streamIndex, uint32_t symbolStreamSize) PDB_NO_EXCEPT
: m_stream(file.CreateMSFStream<CoalescedMSFStream>(streamIndex, symbolStreamSize))
{
// https://llvm.org/docs/PDB/ModiStream.html
// struct ModiStream {
// uint32_t Signature;
// uint8_t Symbols[SymbolSize - 4];
// uint8_t C11LineInfo[C11Size];
// uint8_t C13LineInfo[C13Size];
// uint32_t GlobalRefsSize;
// uint8_t GlobalRefs[GlobalRefsSize];
// };
// we are only interested in the symbols, but not the line information or global refs.
// the coalesced stream is therefore only built for the symbols, not all the data in the stream.
// this potentially saves a lot of memory and performance on large PDBs.
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD const PDB::CodeView::DBI::Record* PDB::ModuleSymbolStream::FindRecord(CodeView::DBI::SymbolRecordKind kind) const PDB_NO_EXCEPT
{
// ignore the stream's 4-byte signature
size_t offset = sizeof(uint32_t);
// parse the CodeView records
while (offset < m_stream.GetSize())
{
// https://llvm.org/docs/PDB/CodeViewTypes.html
const CodeView::DBI::Record* record = m_stream.GetDataAtOffset<const CodeView::DBI::Record>(offset);
if (record->header.kind == kind)
{
return record;
}
const uint32_t recordSize = GetCodeViewRecordSize(record);
// position the module stream offset at the next record
offset = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::RecordHeader) + recordSize, 4u);
}
return nullptr;
}

View File

@@ -0,0 +1,70 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_BitUtil.h"
#include "PDB_DBITypes.h"
#include "PDB_Util.h"
#include "PDB_CoalescedMSFStream.h"
namespace PDB
{
class RawFile;
class PDB_NO_DISCARD ModuleSymbolStream
{
public:
ModuleSymbolStream(void) PDB_NO_EXCEPT;
explicit ModuleSymbolStream(const RawFile& file, uint16_t streamIndex, uint32_t symbolStreamSize) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(ModuleSymbolStream);
// Returns a record's parent record.
template <typename T>
PDB_NO_DISCARD inline const CodeView::DBI::Record* GetParentRecord(const T& record) const PDB_NO_EXCEPT
{
return m_stream.GetDataAtOffset<const CodeView::DBI::Record>(record.parent);
}
// Returns a record's end record.
template <typename T>
PDB_NO_DISCARD inline const CodeView::DBI::Record* GetEndRecord(const T& record) const PDB_NO_EXCEPT
{
return m_stream.GetDataAtOffset<const CodeView::DBI::Record>(record.end);
}
// Finds a record of a certain kind.
PDB_NO_DISCARD const CodeView::DBI::Record* FindRecord(CodeView::DBI::SymbolRecordKind Kind) const PDB_NO_EXCEPT;
// Iterates all records in the stream.
template <typename F>
void ForEachSymbol(F&& functor) const PDB_NO_EXCEPT
{
// ignore the stream's 4-byte signature
size_t offset = sizeof(uint32_t);
// parse the CodeView records
while (offset < m_stream.GetSize())
{
// https://llvm.org/docs/PDB/CodeViewTypes.html
const CodeView::DBI::Record* record = m_stream.GetDataAtOffset<const CodeView::DBI::Record>(offset);
const uint32_t recordSize = GetCodeViewRecordSize(record);
functor(record);
// position the module stream offset at the next record
offset = BitUtil::RoundUpToMultiple<size_t>(offset + sizeof(CodeView::DBI::RecordHeader) + recordSize, 4u);
}
}
private:
CoalescedMSFStream m_stream;
PDB_DISABLE_COPY(ModuleSymbolStream);
};
}

View File

@@ -0,0 +1,28 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_NamesStream.h"
#include "PDB_RawFile.h"
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::NamesStream::NamesStream(void) PDB_NO_EXCEPT
: m_stream()
, m_header(nullptr)
, m_stringTable(nullptr)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::NamesStream::NamesStream(const RawFile& file, uint32_t streamIndex) PDB_NO_EXCEPT
: m_stream(file.CreateMSFStream<CoalescedMSFStream>(streamIndex))
, m_header(m_stream.GetDataAtOffset<const NamesHeader>(0u))
, m_stringTable(nullptr)
{
// grab a pointer into the string table
m_stringTable = m_stream.GetDataAtOffset<char>(sizeof(NamesHeader));
}

View File

@@ -0,0 +1,48 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "PDB_Types.h"
#include "PDB_CoalescedMSFStream.h"
namespace PDB
{
class RawFile;
struct NamesHeader
{
uint32_t magic;
uint32_t hashVersion;
uint32_t size;
};
class PDB_NO_DISCARD NamesStream
{
public:
NamesStream(void) PDB_NO_EXCEPT;
explicit NamesStream(const RawFile& file, uint32_t streamIndex) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(NamesStream);
// Returns the header of the stream.
PDB_NO_DISCARD inline const NamesHeader* GetHeader(void) const PDB_NO_EXCEPT
{
return m_header;
}
PDB_NO_DISCARD inline const char* GetFilename(uint32_t filenameOffset) const PDB_NO_EXCEPT
{
return m_stringTable + filenameOffset;
}
private:
CoalescedMSFStream m_stream;
const NamesHeader* m_header;
const char* m_stringTable;
PDB_DISABLE_COPY(NamesStream);
};
}

4
third_party/raw_pdb/src/PDB_PCH.cpp vendored Normal file
View File

@@ -0,0 +1,4 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"

20
third_party/raw_pdb/src/PDB_PCH.h vendored Normal file
View File

@@ -0,0 +1,20 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
// this needs to be the first include, since it determines the platform/toolchain we're compiling for
#include "Foundation/PDB_Platform.h"
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_Warnings.h"
// library includes
#include "Foundation/PDB_Log.h"
#include "Foundation/PDB_Assert.h"
#include "Foundation/PDB_Move.h"
#include "Foundation/PDB_Forward.h"
#include "Foundation/PDB_Memory.h"
#include "Foundation/PDB_ArrayView.h"
#include "Foundation/PDB_BitUtil.h"
#include "Foundation/PDB_BitOperators.h"
#include "Foundation/PDB_PointerUtil.h"

View File

@@ -0,0 +1,43 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_PublicSymbolStream.h"
#include "PDB_RawFile.h"
#include "PDB_Types.h"
#include "PDB_DBITypes.h"
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::PublicSymbolStream::PublicSymbolStream(void) PDB_NO_EXCEPT
: m_stream()
, m_hashRecords(nullptr)
, m_count(0u)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::PublicSymbolStream::PublicSymbolStream(const RawFile& file, uint16_t streamIndex, uint32_t count) PDB_NO_EXCEPT
: m_stream(file.CreateMSFStream<CoalescedMSFStream>(streamIndex))
, m_hashRecords(m_stream.GetDataAtOffset<HashRecord>(sizeof(PublicStreamHeader) + sizeof(HashTableHeader)))
, m_count(count)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD const PDB::CodeView::DBI::Record* PDB::PublicSymbolStream::GetRecord(const CoalescedMSFStream& symbolRecordStream, const HashRecord& hashRecord) const PDB_NO_EXCEPT
{
// hash record offsets start at 1, not at 0
const uint32_t headerOffset = hashRecord.offset - 1u;
// the offset doesn't point to the public symbol directly, but to the CodeView record:
// https://llvm.org/docs/PDB/CodeViewSymbols.html
const CodeView::DBI::Record* record = symbolRecordStream.GetDataAtOffset<const CodeView::DBI::Record>(headerOffset);
return record;
}

View File

@@ -0,0 +1,49 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_ArrayView.h"
#include "PDB_CoalescedMSFStream.h"
namespace PDB
{
class RawFile;
struct HashRecord;
namespace CodeView
{
namespace DBI
{
struct Record;
}
}
class PDB_NO_DISCARD PublicSymbolStream
{
public:
PublicSymbolStream(void) PDB_NO_EXCEPT;
explicit PublicSymbolStream(const RawFile& file, uint16_t streamIndex, uint32_t count) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(PublicSymbolStream);
// Turns a given hash record into a DBI record using the given symbol stream.
PDB_NO_DISCARD const CodeView::DBI::Record* GetRecord(const CoalescedMSFStream& symbolRecordStream, const HashRecord& hashRecord) const PDB_NO_EXCEPT;
// Returns a view of all the records in the stream.
PDB_NO_DISCARD inline ArrayView<HashRecord> GetRecords(void) const PDB_NO_EXCEPT
{
return ArrayView<HashRecord>(m_hashRecords, m_count);
}
private:
CoalescedMSFStream m_stream;
const HashRecord* m_hashRecords;
uint32_t m_count;
PDB_DISABLE_COPY(PublicSymbolStream);
};
}

147
third_party/raw_pdb/src/PDB_RawFile.cpp vendored Normal file
View File

@@ -0,0 +1,147 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_RawFile.h"
#include "PDB_Types.h"
#include "PDB_Util.h"
#include "PDB_DirectMSFStream.h"
#include "Foundation/PDB_PointerUtil.h"
#include "Foundation/PDB_Memory.h"
#include "Foundation/PDB_Assert.h"
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::RawFile::RawFile(RawFile&& other) PDB_NO_EXCEPT
: m_data(PDB_MOVE(other.m_data))
, m_superBlock(PDB_MOVE(other.m_superBlock))
, m_directoryStream(PDB_MOVE(other.m_directoryStream))
, m_streamCount(PDB_MOVE(other.m_streamCount))
, m_streamSizes(PDB_MOVE(other.m_streamSizes))
, m_streamBlocks(PDB_MOVE(other.m_streamBlocks))
{
other.m_data = nullptr;
other.m_superBlock = nullptr;
other.m_streamCount = 0u;
other.m_streamSizes = nullptr;
other.m_streamBlocks = nullptr;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::RawFile& PDB::RawFile::operator=(RawFile&& other) PDB_NO_EXCEPT
{
if (this != &other)
{
PDB_DELETE_ARRAY(m_streamBlocks);
m_data = PDB_MOVE(other.m_data);
m_superBlock = PDB_MOVE(other.m_superBlock);
m_directoryStream = PDB_MOVE(other.m_directoryStream);
m_streamCount = PDB_MOVE(other.m_streamCount);
m_streamSizes = PDB_MOVE(other.m_streamSizes);
m_streamBlocks = PDB_MOVE(other.m_streamBlocks);
other.m_data = nullptr;
other.m_superBlock = nullptr;
other.m_streamCount = 0u;
other.m_streamSizes = nullptr;
other.m_streamBlocks = nullptr;
}
return *this;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::RawFile::RawFile(const void* data) PDB_NO_EXCEPT
: m_data(data)
, m_superBlock(Pointer::Offset<const SuperBlock*>(data, 0u))
, m_directoryStream()
, m_streamCount(0u)
, m_streamSizes(nullptr)
, m_streamBlocks(nullptr)
{
// the SuperBlock stores an array of indices of blocks that make up the indices of directory blocks, which need to be stitched together to form the directory.
// the blocks holding the indices of directory blocks are not necessarily contiguous, so they need to be coalesced first.
const uint32_t directoryBlockCount = PDB::ConvertSizeToBlockCount(m_superBlock->directorySize, m_superBlock->blockSize);
// the directory is made up of directoryBlockCount blocks, so we need that many indices to be read from the blocks that make up the indices
CoalescedMSFStream directoryIndicesStream(data, m_superBlock->blockSize, m_superBlock->directoryBlockIndices, directoryBlockCount * sizeof(uint32_t));
// these are the indices of blocks making up the directory stream, now guaranteed to be contiguous
const uint32_t* directoryIndices = directoryIndicesStream.GetDataAtOffset<uint32_t>(0u);
m_directoryStream = CoalescedMSFStream(data, m_superBlock->blockSize, directoryIndices, m_superBlock->directorySize);
// https://llvm.org/docs/PDB/MsfFile.html#the-stream-directory
// parse the directory from its contiguous version. the directory matches the following struct:
// struct StreamDirectory
// {
// uint32_t streamCount;
// uint32_t streamSizes[streamCount];
// uint32_t streamBlocks[streamCount][];
// };
m_streamCount = *m_directoryStream.GetDataAtOffset<uint32_t>(0u);
// we can assign pointers into the stream directly, since the RawFile keeps ownership of the directory stream
m_streamSizes = m_directoryStream.GetDataAtOffset<uint32_t>(sizeof(uint32_t));
const uint32_t* directoryStreamBlocks = m_directoryStream.GetDataAtOffset<uint32_t>(sizeof(uint32_t) + sizeof(uint32_t) * m_streamCount);
// prepare indices for directly accessing individual streams
m_streamBlocks = PDB_NEW_ARRAY(const uint32_t*, m_streamCount);
const uint32_t* indicesForCurrentBlock = directoryStreamBlocks;
for (uint32_t i = 0u; i < m_streamCount; ++i)
{
const uint32_t sizeInBytes = GetStreamSize(i);
const uint32_t blockCount = ConvertSizeToBlockCount(sizeInBytes, m_superBlock->blockSize);
m_streamBlocks[i] = indicesForCurrentBlock;
indicesForCurrentBlock += blockCount;
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::RawFile::~RawFile(void) PDB_NO_EXCEPT
{
PDB_DELETE_ARRAY(m_streamBlocks);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
template <typename T>
PDB_NO_DISCARD T PDB::RawFile::CreateMSFStream(uint32_t streamIndex) const PDB_NO_EXCEPT
{
PDB_ASSERT(streamIndex != PDB::NilStreamIndex, "Invalid stream index.");
PDB_ASSERT(streamIndex < m_streamCount, "Invalid stream index.");
return T(m_data, m_superBlock->blockSize, m_streamBlocks[streamIndex], GetStreamSize(streamIndex));
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
template <typename T>
PDB_NO_DISCARD T PDB::RawFile::CreateMSFStream(uint32_t streamIndex, uint32_t streamSize) const PDB_NO_EXCEPT
{
PDB_ASSERT(streamIndex != PDB::NilStreamIndex, "Invalid stream index.");
PDB_ASSERT(streamIndex < m_streamCount, "Invalid stream index.");
PDB_ASSERT(streamSize <= GetStreamSize(streamIndex), "Invalid stream size.");
return T(m_data, m_superBlock->blockSize, m_streamBlocks[streamIndex], streamSize);
}
// explicit template instantiation
template PDB::CoalescedMSFStream PDB::RawFile::CreateMSFStream<PDB::CoalescedMSFStream>(uint32_t streamIndex) const PDB_NO_EXCEPT;
template PDB::DirectMSFStream PDB::RawFile::CreateMSFStream<PDB::DirectMSFStream>(uint32_t streamIndex) const PDB_NO_EXCEPT;
template PDB::CoalescedMSFStream PDB::RawFile::CreateMSFStream<PDB::CoalescedMSFStream>(uint32_t streamIndex, uint32_t streamSize) const PDB_NO_EXCEPT;
template PDB::DirectMSFStream PDB::RawFile::CreateMSFStream<PDB::DirectMSFStream>(uint32_t streamIndex, uint32_t streamSize) const PDB_NO_EXCEPT;

66
third_party/raw_pdb/src/PDB_RawFile.h vendored Normal file
View File

@@ -0,0 +1,66 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "PDB_CoalescedMSFStream.h"
// https://llvm.org/docs/PDB/index.html
namespace PDB
{
struct SuperBlock;
class PDB_NO_DISCARD RawFile
{
public:
RawFile(RawFile&& other) PDB_NO_EXCEPT;
RawFile& operator=(RawFile&& other) PDB_NO_EXCEPT;
explicit RawFile(const void* data) PDB_NO_EXCEPT;
~RawFile(void) PDB_NO_EXCEPT;
// Creates any type of MSF stream.
template <typename T>
PDB_NO_DISCARD T CreateMSFStream(uint32_t streamIndex) const PDB_NO_EXCEPT;
// Creates any type of MSF stream with the given size.
template <typename T>
PDB_NO_DISCARD T CreateMSFStream(uint32_t streamIndex, uint32_t streamSize) const PDB_NO_EXCEPT;
// Returns the SuperBlock.
PDB_NO_DISCARD inline const SuperBlock* GetSuperBlock(void) const PDB_NO_EXCEPT
{
return m_superBlock;
}
// Returns the number of streams in the PDB file.
PDB_NO_DISCARD inline uint32_t GetStreamCount(void) const PDB_NO_EXCEPT
{
return m_streamCount;
}
// Returns the size of the stream with the given index, taking into account nil page sizes.
PDB_NO_DISCARD inline uint32_t GetStreamSize(uint32_t streamIndex) const PDB_NO_EXCEPT
{
const uint32_t streamSize = m_streamSizes[streamIndex];
return (streamSize == NilPageSize) ? 0u : streamSize;
}
private:
const void* m_data;
const SuperBlock* m_superBlock;
CoalescedMSFStream m_directoryStream;
// stream directory
uint32_t m_streamCount;
const uint32_t* m_streamSizes;
const uint32_t** m_streamBlocks;
PDB_DISABLE_COPY(RawFile);
};
}

View File

@@ -0,0 +1,25 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_SectionContributionStream.h"
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::SectionContributionStream::SectionContributionStream(void) PDB_NO_EXCEPT
: m_stream()
, m_contributions(nullptr)
, m_count(0u)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::SectionContributionStream::SectionContributionStream(const DirectMSFStream& directStream, uint32_t size, uint32_t offset) PDB_NO_EXCEPT
: m_stream(directStream, size, offset)
, m_contributions(m_stream.GetDataAtOffset<DBI::SectionContribution>(0u))
, m_count(size / sizeof(DBI::SectionContribution))
{
}

View File

@@ -0,0 +1,38 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_ArrayView.h"
#include "PDB_DBITypes.h"
#include "PDB_CoalescedMSFStream.h"
namespace PDB
{
class PDB_NO_DISCARD DirectMSFStream;
class PDB_NO_DISCARD SectionContributionStream
{
public:
SectionContributionStream(void) PDB_NO_EXCEPT;
explicit SectionContributionStream(const DirectMSFStream& directStream, uint32_t size, uint32_t offset) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(SectionContributionStream);
// Returns a view of all section contributions in the stream.
PDB_NO_DISCARD inline ArrayView<DBI::SectionContribution> GetContributions(void) const PDB_NO_EXCEPT
{
return ArrayView<DBI::SectionContribution>(m_contributions, m_count);
}
private:
CoalescedMSFStream m_stream;
const DBI::SectionContribution* m_contributions;
size_t m_count;
PDB_DISABLE_COPY(SectionContributionStream);
};
}

View File

@@ -0,0 +1,68 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_SourceFileStream.h"
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::SourceFileStream::SourceFileStream(void) PDB_NO_EXCEPT
: m_stream()
, m_moduleCount(0u)
, m_moduleIndices(nullptr)
, m_moduleFileCounts(nullptr)
, m_fileNameOffsets(nullptr)
, m_stringTable(nullptr)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::SourceFileStream::SourceFileStream(const DirectMSFStream& directStream, uint32_t size, uint32_t offset) PDB_NO_EXCEPT
: m_stream(directStream, size, offset)
, m_moduleCount(0u)
, m_moduleIndices(nullptr)
, m_moduleFileCounts(nullptr)
, m_fileNameOffsets(nullptr)
, m_stringTable(nullptr)
{
// we are going to consume the whole source info sub-stream, so create a coalesced stream for faster read operations and direct access.
// the sub-stream has the following layout:
// struct SourceInfoSubstream
// {
// uint16_t moduleCount;
// uint16_t sourceFileCount;
// uint16_t moduleIndices[moduleCount];
// uint16_t moduleFileCounts[moduleCount];
// uint32_t fileNameOffsets[realSourceFileCount];
// char stringTable[][realSourceFileCount];
// };
m_moduleCount = *m_stream.GetDataAtOffset<uint16_t>(0u);
size_t readOffset = sizeof(uint16_t);
// skip number of source files. this would only support 64k unique files and is no longer used.
// the number of source files is computed dynamically instead.
readOffset += sizeof(uint16_t);
// grab direct pointers into the stream data
m_moduleIndices = m_stream.GetDataAtOffset<uint16_t>(readOffset);
readOffset += sizeof(uint16_t) * m_moduleCount;
m_moduleFileCounts = m_stream.GetDataAtOffset<uint16_t>(readOffset);
readOffset += sizeof(uint16_t) * m_moduleCount;
// count the actual number of source files
size_t sourceFileCount = 0u;
for (unsigned int i = 0u; i < m_moduleCount; ++i)
{
sourceFileCount += m_moduleFileCounts[i];
}
m_fileNameOffsets = m_stream.GetDataAtOffset<uint32_t>(readOffset);
readOffset += sizeof(uint32_t) * sourceFileCount;
// grab a pointer into the string table
m_stringTable = m_stream.GetDataAtOffset<char>(readOffset);
}

View File

@@ -0,0 +1,65 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_ArrayView.h"
#include "PDB_CoalescedMSFStream.h"
namespace PDB
{
class PDB_NO_DISCARD DirectMSFStream;
class PDB_NO_DISCARD SourceFileStream
{
public:
SourceFileStream(void) PDB_NO_EXCEPT;
explicit SourceFileStream(const DirectMSFStream& directStream, uint32_t size, uint32_t offset) PDB_NO_EXCEPT;
PDB_DEFAULT_MOVE(SourceFileStream);
// Returns the number of modules.
PDB_NO_DISCARD inline uint32_t GetModuleCount(void) const PDB_NO_EXCEPT
{
return m_moduleCount;
}
// Returns a view of all the filename offsets for the module with the given index.
PDB_NO_DISCARD inline ArrayView<uint32_t> GetModuleFilenameOffsets(size_t moduleIndex) const PDB_NO_EXCEPT
{
const uint16_t moduleStartIndex = m_moduleIndices[moduleIndex];
const uint16_t moduleFileCount = m_moduleFileCounts[moduleIndex];
return ArrayView<uint32_t>(m_fileNameOffsets + moduleStartIndex, moduleFileCount);
}
// Returns a filename for the given filename offset.
PDB_NO_DISCARD inline const char* GetFilename(uint32_t filenameOffset) const PDB_NO_EXCEPT
{
return m_stringTable + filenameOffset;
}
private:
CoalescedMSFStream m_stream;
// the number of modules
uint32_t m_moduleCount;
// the indices into the file name offsets, for each module
const uint16_t* m_moduleIndices;
// the number of files, for each module
const uint16_t* m_moduleFileCounts;
// the filename offsets into the string table, for all modules
const uint32_t* m_fileNameOffsets;
// the string table storing all filenames
const char* m_stringTable;
PDB_DISABLE_COPY(SourceFileStream);
};
}

View File

@@ -0,0 +1,86 @@
#include "PDB_PCH.h"
#include "PDB_TPIStream.h"
#include "PDB_RawFile.h"
#include "PDB_DirectMSFStream.h"
#include "Foundation/PDB_Memory.h"
namespace
{
// the TPI stream always resides at index 2
static constexpr const uint32_t TPIStreamIndex = 2u;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::TPIStream::TPIStream(void) PDB_NO_EXCEPT
: m_stream()
, m_header()
, m_recordCount(0u)
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::TPIStream::TPIStream(TPIStream&& other) PDB_NO_EXCEPT
: m_stream(PDB_MOVE(other.m_stream))
, m_header(PDB_MOVE(other.m_header))
, m_recordCount(PDB_MOVE(other.m_recordCount))
{
other.m_recordCount = 0u;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::TPIStream& PDB::TPIStream::operator=(TPIStream&& other) PDB_NO_EXCEPT
{
if (this != &other)
{
m_stream = PDB_MOVE(other.m_stream);
m_header = PDB_MOVE(other.m_header);
m_recordCount = PDB_MOVE(other.m_recordCount);
other.m_recordCount = 0u;
}
return *this;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::TPIStream::TPIStream(const RawFile& file) PDB_NO_EXCEPT
: m_stream(file.CreateMSFStream<DirectMSFStream>(TPIStreamIndex)),
m_header(m_stream.ReadAtOffset<TPI::StreamHeader>(0u)),
m_recordCount(GetLastTypeIndex() - GetFirstTypeIndex())
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::HasValidTPIStream(const RawFile& file) PDB_NO_EXCEPT
{
DirectMSFStream stream = file.CreateMSFStream<DirectMSFStream>(TPIStreamIndex);
if (stream.GetSize() < sizeof(TPI::StreamHeader))
{
return ErrorCode::InvalidStream;
}
const TPI::StreamHeader header = stream.ReadAtOffset<TPI::StreamHeader>(0u);
if (header.version != TPI::StreamHeader::Version::V80)
{
return ErrorCode::UnknownVersion;
}
return ErrorCode::Success;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::TPIStream PDB::CreateTPIStream(const RawFile& file) PDB_NO_EXCEPT
{
return TPIStream { file };
}

85
third_party/raw_pdb/src/PDB_TPIStream.h vendored Normal file
View File

@@ -0,0 +1,85 @@
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_ArrayView.h"
#include "PDB_ErrorCodes.h"
#include "PDB_TPITypes.h"
#include "PDB_DirectMSFStream.h"
#include "PDB_Util.h"
// PDB TPI stream
// https://llvm.org/docs/PDB/TpiStream.html
namespace PDB
{
class RawFile;
class PDB_NO_DISCARD TPIStream
{
public:
TPIStream(void) PDB_NO_EXCEPT;
TPIStream(TPIStream&& other) PDB_NO_EXCEPT;
TPIStream& operator=(TPIStream&& other) PDB_NO_EXCEPT;
explicit TPIStream(const RawFile& file) PDB_NO_EXCEPT;
PDB_NO_DISCARD inline const DirectMSFStream& GetDirectMSFStream(void) const PDB_NO_EXCEPT
{
return m_stream;
}
// Returns the index of the first type, which is not necessarily zero.
PDB_NO_DISCARD inline uint32_t GetFirstTypeIndex(void) const PDB_NO_EXCEPT
{
return m_header.typeIndexBegin;
}
// Returns the index of the last type.
PDB_NO_DISCARD inline uint32_t GetLastTypeIndex(void) const PDB_NO_EXCEPT
{
return m_header.typeIndexEnd;
}
// Returns the number of type records.
PDB_NO_DISCARD inline size_t GetTypeRecordCount(void) const PDB_NO_EXCEPT
{
return m_recordCount;
}
CodeView::TPI::RecordHeader ReadTypeRecordHeader(size_t offset) const PDB_NO_EXCEPT
{
const CodeView::TPI::RecordHeader header = m_stream.ReadAtOffset<CodeView::TPI::RecordHeader>(offset);
return header;
}
template <typename F>
void ForEachTypeRecordHeaderAndOffset(F&& functor) const PDB_NO_EXCEPT
{
// ignore the stream's header
size_t offset = sizeof(TPI::StreamHeader);
while (offset < m_stream.GetSize())
{
const CodeView::TPI::RecordHeader header = ReadTypeRecordHeader(offset);
functor(header, offset);
// position the stream offset at the next record
offset += sizeof(CodeView::TPI::RecordHeader) + header.size - sizeof(uint16_t);
}
}
private:
DirectMSFStream m_stream;
TPI::StreamHeader m_header;
size_t m_recordCount;
PDB_DISABLE_COPY(TPIStream);
};
// Returns whether the given raw file provides a valid TPI stream.
PDB_NO_DISCARD ErrorCode HasValidTPIStream(const RawFile& file) PDB_NO_EXCEPT;
// Creates the TPI stream from a raw file.
PDB_NO_DISCARD TPIStream CreateTPIStream(const RawFile& file) PDB_NO_EXCEPT;
}

867
third_party/raw_pdb/src/PDB_TPITypes.h vendored Normal file
View File

@@ -0,0 +1,867 @@
#pragma once
#include "Foundation/PDB_Macros.h"
#include "Foundation/PDB_BitOperators.h"
namespace PDB
{
namespace TPI
{
// https://llvm.org/docs/PDB/TpiStream.html#stream-header
struct StreamHeader
{
enum class PDB_NO_DISCARD Version : uint32_t
{
V40 = 19950410u,
V41 = 19951122u,
V50 = 19961031u,
V70 = 19990903u,
V80 = 20040203u
};
Version version;
uint32_t headerSize;
uint32_t typeIndexBegin;
uint32_t typeIndexEnd;
uint32_t typeRecordBytes;
uint16_t hashStreamIndex;
uint16_t hashAuxStreamIndex;
uint32_t hashKeySize;
uint32_t numHashBuckets;
int32_t hashValueBufferOffset;
uint32_t hashValueBufferLength;
int32_t indexOffsetBufferOffset;
uint32_t indexOffsetBufferLength;
int32_t hashAdjBufferOffset;
uint32_t hashAdjBufferLength;
};
}
namespace CodeView
{
namespace TPI
{
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L772
enum class PDB_NO_DISCARD TypeRecordKind : uint16_t
{
LF_POINTER = 0x1002u,
LF_MODIFIER = 0x1001u,
LF_PROCEDURE = 0x1008u,
LF_MFUNCTION = 0x1009u,
LF_LABEL = 0x000eu,
LF_ARGLIST = 0x1201u,
LF_FIELDLIST = 0x1203u,
LF_VTSHAPE = 0x000au,
LF_BITFIELD = 0x1205u,
LF_METHODLIST = 0x1206u,
LF_ENDPRECOMP = 0x0014u,
LF_BCLASS = 0x001400u,
LF_VBCLASS = 0x001401u,
LF_IVBCLASS = 0x001402u,
LF_FRIENDFCN_ST = 0x001403u,
LF_INDEX = 0x001404u,
LF_MEMBER_ST = 0x001405u,
LF_STMEMBER_ST = 0x001406u,
LF_METHOD_ST = 0x001407u,
LF_NESTTYPE_ST = 0x001408u,
LF_VFUNCTAB = 0x001409u,
LF_FRIENDCLS = 0x00140Au,
LF_ONEMETHOD_ST = 0x00140Bu,
LF_VFUNCOFF = 0x00140Cu,
LF_NESTTYPEEX_ST = 0x00140Du,
LF_MEMBERMODIFY_ST = 0x00140Eu,
LF_MANAGED_ST = 0x00140Fu,
LF_SMAX = 0x001500u,
LF_TYPESERVER = 0x001501u,
LF_ENUMERATE = 0x001502u,
LF_ARRAY = 0x001503u,
LF_CLASS = 0x001504u,
LF_STRUCTURE = 0x001505u,
LF_UNION = 0x001506u,
LF_ENUM = 0x001507u,
LF_DIMARRAY = 0x001508u,
LF_PRECOMP = 0x001509u,
LF_ALIAS = 0x00150Au,
LF_DEFARG = 0x00150Bu,
LF_FRIENDFCN = 0x00150Cu,
LF_MEMBER = 0x00150Du,
LF_STMEMBER = 0x00150Eu,
LF_METHOD = 0x00150Fu,
LF_NESTTYPE = 0x001510u,
LF_ONEMETHOD = 0x001511u,
LF_NESTTYPEEX = 0x001512u,
LF_MEMBERMODIFY = 0x001513u,
LF_MANAGED = 0x001514u,
LF_TYPESERVER2 = 0x001515u,
LF_CLASS2 = 0x001608u,
LF_STRUCTURE2 = 0x001609u,
LF_NUMERIC = 0x8000u,
LF_CHAR = 0x8000u,
LF_SHORT = 0x8001u,
LF_USHORT = 0x8002u,
LF_LONG = 0x8003u,
LF_ULONG = 0x8004u,
LF_REAL32 = 0x8005u,
LF_REAL64 = 0x8006u,
LF_REAL80 = 0x8007u,
LF_REAL128 = 0x8008u,
LF_QUADWORD = 0x8009u,
LF_UQUADWORD = 0x800au,
LF_REAL48 = 0x800bu,
LF_COMPLEX32 = 0x800cu,
LF_COMPLEX64 = 0x800du,
LF_COMPLEX80 = 0x800eu,
LF_COMPLEX128 = 0x800fu,
LF_VARSTRING = 0x8010u,
LF_OCTWORD = 0x8017u,
LF_UOCTWORD = 0x8018u,
LF_DECIMAL = 0x8019u,
LF_DATE = 0x801au,
LF_UTF8STRING = 0x801bu,
LF_REAL16 = 0x801cu
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L328
// https://github.com/ValveSoftware/wine/blob/cd165953c8b379a78418711f07417022e503c81b/include/wine/mscvpdb.h
enum class TypeIndexKind : uint16_t
{
T_NOTYPE = 0x0000u, // uncharacterized type (no type)
T_ABS = 0x0001u, // absolute symbol
T_SEGMENT = 0x0002u, // segment type
T_VOID = 0x0003u, // void
T_HRESULT = 0x0008u, // OLE/COM HRESULT
T_32PHRESULT = 0x0408u, // OLE/COM HRESULT __ptr32 *
T_64PHRESULT = 0x0608u, // OLE/COM HRESULT __ptr64 *
// Emitted due to a compiler bug?
// 0x0600 bits appears to indicate a 64-bit pointer, but it has no type?
// Seen as type index for C11 "_Atomic uint32_t*" variable and constant.
T_UNKNOWN_0600 = 0x0600u,
T_PVOID = 0x0103u, // near pointer to void
T_PFVOID = 0x0203u, // far pointer to void
T_PHVOID = 0x0303u, // huge pointer to void
T_32PVOID = 0x0403u, // 32 bit pointer to void
T_32PFVOID = 0x0503u, // 16:32 pointer to void
T_64PVOID = 0x0603u, // 64 bit pointer to void
T_CURRENCY = 0x0004u, // BASIC 8 byte currency value
T_NBASICSTR = 0x0005u, // Near BASIC string
T_FBASICSTR = 0x0006u, // Far BASIC string
T_NOTTRANS = 0x0007u, // type not translated by cvpack
T_BIT = 0x0060u, // bit
T_PASCHAR = 0x0061u, // Pascal CHAR
T_BOOL32FF = 0x0062u, // 32-bit BOOL where true is 0xffffffff
T_CHAR = 0x0010u, // 8 bit signed
T_PCHAR = 0x0110u, // 16 bit pointer to 8 bit signed
T_PFCHAR = 0x0210u, // 16:16 far pointer to 8 bit signed
T_PHCHAR = 0x0310u, // 16:16 huge pointer to 8 bit signed
T_32PCHAR = 0x0410u, // 32 bit pointer to 8 bit signed
T_32PFCHAR = 0x0510u, // 16:32 pointer to 8 bit signed
T_64PCHAR = 0x0610u, // 64 bit pointer to 8 bit signed
T_UCHAR = 0x0020u, // 8 bit unsigned
T_PUCHAR = 0x0120u, // 16 bit pointer to 8 bit unsigned
T_PFUCHAR = 0x0220u, // 16:16 far pointer to 8 bit unsigned
T_PHUCHAR = 0x0320u, // 16:16 huge pointer to 8 bit unsigned
T_32PUCHAR = 0x0420u, // 32 bit pointer to 8 bit unsigned
T_32PFUCHAR = 0x0520u, // 16:32 pointer to 8 bit unsigned
T_64PUCHAR = 0x0620u, // 64 bit pointer to 8 bit unsigned
T_RCHAR = 0x0070u, // really a char
T_PRCHAR = 0x0170u, // 16 bit pointer to a real char
T_PFRCHAR = 0x0270u, // 16:16 far pointer to a real char
T_PHRCHAR = 0x0370u, // 16:16 huge pointer to a real char
T_32PRCHAR = 0x0470u, // 32 bit pointer to a real char
T_32PFRCHAR = 0x0570u, // 16:32 pointer to a real char
T_64PRCHAR = 0x0670u, // 64 bit pointer to a real char
// wide character types
T_WCHAR = 0x0071u, // wide char
T_PWCHAR = 0x0171u, // 16 bit pointer to a wide char
T_PFWCHAR = 0x0271u, // 16:16 far pointer to a wide char
T_PHWCHAR = 0x0371u, // 16:16 huge pointer to a wide char
T_32PWCHAR = 0x0471u, // 32 bit pointer to a wide char
T_32PFWCHAR = 0x0571u, // 16:32 pointer to a wide char
T_64PWCHAR = 0x0671u, // 64 bit pointer to a wide char
// 8-bit unicode char
T_CHAR8 = 0x007c, // 8-bit unicode char (C++ 20)
T_PCHAR8 = 0x017c, // Near pointer to 8-bit unicode char
T_PFCHAR8 = 0x027c, // Far pointer to 8-bit unicode char
T_PHCHAR8 = 0x037c, // Huge pointer to 8-bit unicode char
T_32PCHAR8 = 0x047c, // 16:32 near pointer to 8-bit unicode char
T_32PFCHAR8 = 0x057c, // 16:32 far pointer to 8-bit unicode char
T_64PCHAR8 = 0x067c, // 64 bit near pointer to 8-bit unicode char
// 16-bit unicode char
T_CHAR16 = 0x007au, // 16-bit unicode char
T_PCHAR16 = 0x017au, // 16 bit pointer to a 16-bit unicode char
T_PFCHAR16 = 0x027au, // 16:16 far pointer to a 16-bit unicode char
T_PHCHAR16 = 0x037au, // 16:16 huge pointer to a 16-bit unicode char
T_32PCHAR16 = 0x047au, // 32 bit pointer to a 16-bit unicode char
T_32PFCHAR16 = 0x057au, // 16:32 pointer to a 16-bit unicode char
T_64PCHAR16 = 0x067au, // 64 bit pointer to a 16-bit unicode char
// 32-bit unicode char
T_CHAR32 = 0x007bu, // 32-bit unicode char
T_PCHAR32 = 0x017bu, // 16 bit pointer to a 32-bit unicode char
T_PFCHAR32 = 0x027bu, // 16:16 far pointer to a 32-bit unicode char
T_PHCHAR32 = 0x037bu, // 16:16 huge pointer to a 32-bit unicode char
T_32PCHAR32 = 0x047bu, // 32 bit pointer to a 32-bit unicode char
T_32PFCHAR32 = 0x057bu, // 16:32 pointer to a 32-bit unicode char
T_64PCHAR32 = 0x067bu, // 64 bit pointer to a 32-bit unicode char
// 8 bit int types
T_INT1 = 0x0068u, // 8 bit signed int
T_PINT1 = 0x0168u, // 16 bit pointer to 8 bit signed int
T_PFINT1 = 0x0268u, // 16:16 far pointer to 8 bit signed int
T_PHINT1 = 0x0368u, // 16:16 huge pointer to 8 bit signed int
T_32PINT1 = 0x0468u, // 32 bit pointer to 8 bit signed int
T_32PFINT1 = 0x0568u, // 16:32 pointer to 8 bit signed int
T_64PINT1 = 0x0668u, // 64 bit pointer to 8 bit signed int
T_UINT1 = 0x0069u, // 8 bit unsigned int
T_PUINT1 = 0x0169u, // 16 bit pointer to 8 bit unsigned int
T_PFUINT1 = 0x0269u, // 16:16 far pointer to 8 bit unsigned int
T_PHUINT1 = 0x0369u, // 16:16 huge pointer to 8 bit unsigned int
T_32PUINT1 = 0x0469u, // 32 bit pointer to 8 bit unsigned int
T_32PFUINT1 = 0x0569u, // 16:32 pointer to 8 bit unsigned int
T_64PUINT1 = 0x0669u, // 64 bit pointer to 8 bit unsigned int
// 16 bit short types
T_SHORT = 0x0011u, // 16 bit signed
T_PSHORT = 0x0111u, // 16 bit pointer to 16 bit signed
T_PFSHORT = 0x0211u, // 16:16 far pointer to 16 bit signed
T_PHSHORT = 0x0311u, // 16:16 huge pointer to 16 bit signed
T_32PSHORT = 0x0411u, // 32 bit pointer to 16 bit signed
T_32PFSHORT = 0x0511u, // 16:32 pointer to 16 bit signed
T_64PSHORT = 0x0611u, // 64 bit pointer to 16 bit signed
T_USHORT = 0x0021u,
T_PUSHORT = 0x0121u,
T_PFUSHORT = 0x0221u,
T_PHUSHORT = 0x0321u,
T_32PUSHORT = 0x0421u,
T_32PFUSHORT = 0x0521u,
T_64PUSHORT = 0x0621u,
T_INT2 = 0x0072u,
T_PINT2 = 0x0172u,
T_PFINT2 = 0x0272u,
T_PHINT2 = 0x0372u,
T_32PINT2 = 0x0472u,
T_32PFINT2 = 0x0572u,
T_64PINT2 = 0x0672u,
T_UINT2 = 0x0073u,
T_PUINT2 = 0x0173u,
T_PFUINT2 = 0x0273u,
T_PHUINT2 = 0x0373u,
T_32PUINT2 = 0x0473u,
T_32PFUINT2 = 0x0573u,
T_64PUINT2 = 0x0673u,
T_LONG = 0x0012u,
T_PLONG = 0x0112u,
T_PFLONG = 0x0212u,
T_PHLONG = 0x0312u,
T_32PLONG = 0x0412u,
T_32PFLONG = 0x0512u,
T_64PLONG = 0x0612u,
T_ULONG = 0x0022u,
T_PULONG = 0x0122u,
T_PFULONG = 0x0222u,
T_PHULONG = 0x0322u,
T_32PULONG = 0x0422u,
T_32PFULONG = 0x0522u,
T_64PULONG = 0x0622u,
T_INT4 = 0x0074u,
T_PINT4 = 0x0174u,
T_PFINT4 = 0x0274u,
T_PHINT4 = 0x0374u,
T_32PINT4 = 0x0474u,
T_32PFINT4 = 0x0574u,
T_64PINT4 = 0x0674u,
T_UINT4 = 0x0075u,
T_PUINT4 = 0x0175u,
T_PFUINT4 = 0x0275u,
T_PHUINT4 = 0x0375u,
T_32PUINT4 = 0x0475u,
T_32PFUINT4 = 0x0575u,
T_64PUINT4 = 0x0675u,
T_QUAD = 0x0013u,
T_PQUAD = 0x0113u,
T_PFQUAD = 0x0213u,
T_PHQUAD = 0x0313u,
T_32PQUAD = 0x0413u,
T_32PFQUAD = 0x0513u,
T_64PQUAD = 0x0613u,
T_UQUAD = 0x0023u,
T_PUQUAD = 0x0123u,
T_PFUQUAD = 0x0223u,
T_PHUQUAD = 0x0323u,
T_32PUQUAD = 0x0423u,
T_32PFUQUAD = 0x0523u,
T_64PUQUAD = 0x0623u,
T_INT8 = 0x0076u,
T_PINT8 = 0x0176u,
T_PFINT8 = 0x0276u,
T_PHINT8 = 0x0376u,
T_32PINT8 = 0x0476u,
T_32PFINT8 = 0x0576u,
T_64PINT8 = 0x0676u,
T_UINT8 = 0x0077u,
T_PUINT8 = 0x0177u,
T_PFUINT8 = 0x0277u,
T_PHUINT8 = 0x0377u,
T_32PUINT8 = 0x0477u,
T_32PFUINT8 = 0x0577u,
T_64PUINT8 = 0x0677u,
T_OCT = 0x0014u,
T_POCT = 0x0114u,
T_PFOCT = 0x0214u,
T_PHOCT = 0x0314u,
T_32POCT = 0x0414u,
T_32PFOCT = 0x0514u,
T_64POCT = 0x0614u,
T_UOCT = 0x0024u,
T_PUOCT = 0x0124u,
T_PFUOCT = 0x0224u,
T_PHUOCT = 0x0324u,
T_32PUOCT = 0x0424u,
T_32PFUOCT = 0x0524u,
T_64PUOCT = 0x0624u,
T_INT16 = 0x0078u,
T_PINT16 = 0x0178u,
T_PFINT16 = 0x0278u,
T_PHINT16 = 0x0378u,
T_32PINT16 = 0x0478u,
T_32PFINT16 = 0x0578u,
T_64PINT16 = 0x0678u,
T_UINT16 = 0x0079u,
T_PUINT16 = 0x0179u,
T_PFUINT16 = 0x0279u,
T_PHUINT16 = 0x0379u,
T_32PUINT16 = 0x0479u,
T_32PFUINT16 = 0x0579u,
T_64PUINT16 = 0x0679u,
T_REAL32 = 0x0040u,
T_PREAL32 = 0x0140u,
T_PFREAL32 = 0x0240u,
T_PHREAL32 = 0x0340u,
T_32PREAL32 = 0x0440u,
T_32PFREAL32 = 0x0540u,
T_64PREAL32 = 0x0640u,
T_REAL48 = 0x0044u,
T_PREAL48 = 0x0144u,
T_PFREAL48 = 0x0244u,
T_PHREAL48 = 0x0344u,
T_32PREAL48 = 0x0444u,
T_32PFREAL48 = 0x0544u,
T_64PREAL48 = 0x0644u,
T_REAL64 = 0x0041u,
T_PREAL64 = 0x0141u,
T_PFREAL64 = 0x0241u,
T_PHREAL64 = 0x0341u,
T_32PREAL64 = 0x0441u,
T_32PFREAL64 = 0x0541u,
T_64PREAL64 = 0x0641u,
T_REAL80 = 0x0042u,
T_PREAL80 = 0x0142u,
T_PFREAL80 = 0x0242u,
T_PHREAL80 = 0x0342u,
T_32PREAL80 = 0x0442u,
T_32PFREAL80 = 0x0542u,
T_64PREAL80 = 0x0642u,
T_REAL128 = 0x0043u,
T_PREAL128 = 0x0143u,
T_PFREAL128 = 0x0243u,
T_PHREAL128 = 0x0343u,
T_32PREAL128 = 0x0443u,
T_32PFREAL128 = 0x0543u,
T_64PREAL128 = 0x0643u,
T_CPLX32 = 0x0050u,
T_PCPLX32 = 0x0150u,
T_PFCPLX32 = 0x0250u,
T_PHCPLX32 = 0x0350u,
T_32PCPLX32 = 0x0450u,
T_32PFCPLX32 = 0x0550u,
T_64PCPLX32 = 0x0650u,
T_CPLX64 = 0x0051u,
T_PCPLX64 = 0x0151u,
T_PFCPLX64 = 0x0251u,
T_PHCPLX64 = 0x0351u,
T_32PCPLX64 = 0x0451u,
T_32PFCPLX64 = 0x0551u,
T_64PCPLX64 = 0x0651u,
T_CPLX80 = 0x0052u,
T_PCPLX80 = 0x0152u,
T_PFCPLX80 = 0x0252u,
T_PHCPLX80 = 0x0352u,
T_32PCPLX80 = 0x0452u,
T_32PFCPLX80 = 0x0552u,
T_64PCPLX80 = 0x0652u,
T_CPLX128 = 0x0053u,
T_PCPLX128 = 0x0153u,
T_PFCPLX128 = 0x0253u,
T_PHCPLX128 = 0x0353u,
T_32PCPLX128 = 0x0453u,
T_32PFCPLX128 = 0x0553u,
T_64PCPLX128 = 0x0653u,
T_BOOL08 = 0x0030u,
T_PBOOL08 = 0x0130u,
T_PFBOOL08 = 0x0230u,
T_PHBOOL08 = 0x0330u,
T_32PBOOL08 = 0x0430u,
T_32PFBOOL08 = 0x0530u,
T_64PBOOL08 = 0x0630u,
T_BOOL16 = 0x0031u,
T_PBOOL16 = 0x0131u,
T_PFBOOL16 = 0x0231u,
T_PHBOOL16 = 0x0331u,
T_32PBOOL16 = 0x0431u,
T_32PFBOOL16 = 0x0531u,
T_64PBOOL16 = 0x0631u,
T_BOOL32 = 0x0032u,
T_PBOOL32 = 0x0132u,
T_PFBOOL32 = 0x0232u,
T_PHBOOL32 = 0x0332u,
T_32PBOOL32 = 0x0432u,
T_32PFBOOL32 = 0x0532u,
T_64PBOOL32 = 0x0632u,
T_BOOL64 = 0x0033u,
T_PBOOL64 = 0x0133u,
T_PFBOOL64 = 0x0233u,
T_PHBOOL64 = 0x0333u,
T_32PBOOL64 = 0x0433u,
T_32PFBOOL64 = 0x0533u,
T_64PBOOL64 = 0x0633u,
T_NCVPTR = 0x01F0u,
T_FCVPTR = 0x02F0u,
T_HCVPTR = 0x03F0u,
T_32NCVPTR = 0x04F0u,
T_32FCVPTR = 0x05F0u,
T_64NCVPTR = 0x06F0u
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvconst.h#L31
enum class CallingConvention : uint8_t
{
NEAR_C = 0x00u, // near right to left pushu, caller pops stack
FAR_C = 0x01u, // far right to left pushu, caller pops stack
NEAR_PASCAL = 0x02u,// near left to right pushu, callee pops stack
FAR_PASCAL = 0x03u, // far left to right pushu, callee pops stack
NEAR_FAST = 0x04u, // near left to right push with regsu, callee pops stack
FAR_FAST = 0x05u, // far left to right push with regsu, callee pops stack
SKIPPED = 0x06u, // skipped (unused) call index
NEAR_STD = 0x07u, // near standard call
FAR_STD = 0x08u, // far standard call
NEAR_SYS = 0x09u, // near sys call
FAR_SYS = 0x0au, // far sys call
THISCALL = 0x0bu, // this call (this passed in register)
MIPSCALL = 0x0cu, // Mips call
GENERIC = 0x0du, // Generic call sequence
ALPHACALL = 0x0eu, // Alpha call
PPCCALL = 0x0fu, // PPC call
SHCALL = 0x10u, // Hitachi SuperH call
ARMCALL = 0x11u, // ARM call
AM33CALL = 0x12u, // AM33 call
TRICALL = 0x13u, // TriCore Call
SH5CALL = 0x14u, // Hitachi SuperH-5 call
M32RCALL = 0x15u, // M32R Call
CLRCALL = 0x16u, // clr call
INLINE = 0x17u, // Marker for routines always inlined and thus lacking a convention
NEAR_VECTOR = 0x18u,// near left to right push with regsu, callee pops stack
RESERVED = 0x19u // first unused call enumeration
// Do NOT add any more machine specific conventions. This is to be used for
// calling conventions in the source only (e.g. __cdeclu, __stdcall).
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1049
enum class MethodProperty : uint8_t
{
Vanilla = 0x00u,
Virtual = 0x01u,
Static = 0x02u,
Friend = 0x03u,
Intro = 0x04u,
PureVirt = 0x05u,
PureIntro = 0x06u
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1120
struct TypeProperty
{
uint16_t packed : 1; // true if structure is packed
uint16_t ctor : 1; // true if constructors or destructors present
uint16_t ovlops : 1; // true if overloaded operators present
uint16_t isnested : 1; // true if this is a nested class
uint16_t cnested : 1; // true if this class contains nested types
uint16_t opassign : 1; // true if overloaded assignment (=)
uint16_t opcast : 1; // true if casting methods
uint16_t fwdref : 1; // true if forward reference (incomplete defn)
uint16_t scoped : 1; // scoped definition
uint16_t hasuniquename : 1; // true if there is a decorated name following the regular name
uint16_t sealed : 1; // true if class cannot be used as a base class
uint16_t hfa : 2; // CV_HFA_e
uint16_t intrinsic : 1; // true if class is an intrinsic type (e.g. __m128d)
uint16_t mocom : 2; // CV_MOCOM_UDe
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1142
struct MemberAttributes
{
uint16_t access : 2; // access protection CV_access_t
uint16_t mprop : 3; // method properties CV_methodprop_t
uint16_t pseudo : 1; // compiler generated fcn and does not exist
uint16_t noinherit : 1; // true if class cannot be inherited
uint16_t noconstruct : 1; // true if class cannot be constructed
uint16_t compgenx : 1; // compiler generated fcn and does exist
uint16_t sealed : 1; // true if method cannot be overridden
uint16_t unused : 6; // unused
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1156
struct FunctionAttributes
{
uint8_t cxxreturnudt : 1; // true if C++ style ReturnUDT
uint8_t ctor : 1; // true if func is an instance constructor
uint8_t ctorvbase : 1; // true if func is an instance constructor of a class with virtual bases
uint8_t unused : 5; // unused
};
struct RecordHeader
{
uint16_t size; // record length, not including this 2-byte field
TypeRecordKind kind; // record kind
};
struct LeafEasy
{
TypeRecordKind kind; // record kind
};
struct FieldList
{
TypeRecordKind kind; // record kind
union Data
{
#pragma pack(push, 1)
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2499
struct
{
MemberAttributes attributes; // method attribute
uint32_t index; // type index of base class
union
{
PDB_FLEXIBLE_ARRAY_MEMBER(char, offset); // variable length offset of base within class
LeafEasy lfEasy;
};
}LF_BCLASS;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2521
struct
{
MemberAttributes attributes; // attribute
uint32_t index; // type index of direct virtual base class
uint32_t vbpIndex; // type index of virtual base pointer
PDB_FLEXIBLE_ARRAY_MEMBER(char, vbpOffset); // virtual base pointer offset from address point
} LF_VBCLASS, LF_IVBCLASS;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2483
// index leaf - contains type index of another leaf
// a major use of this leaf is to allow the compilers to emit a
// long complex list (LF_FIELD) in smaller pieces.
struct
{
uint16_t pad0; // internal padding, must be 0
uint32_t type; // type index of referenced leaf
} LF_INDEX;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2615
struct
{
uint16_t pad0; // internal padding, must be 0.
uint32_t type; // type index of pointer
}LF_VFUNCTAB;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2683
struct
{
MemberAttributes attributes;
union
{
PDB_FLEXIBLE_ARRAY_MEMBER(char, value);
LeafEasy lfEasy;
};
} LF_ENUMERATE;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2693
struct
{
uint16_t pad0; // internal padding, must be 0
uint32_t index; // index of nested type definition
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
}LF_NESTTYPE;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2650
struct
{
uint16_t count; // number of occurrences of function
uint32_t mList; // index to LF_METHODLIST record
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
}LF_METHOD;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2671
struct
{
MemberAttributes attributes; // method attribute
uint32_t index; // index to type record for procedure
PDB_FLEXIBLE_ARRAY_MEMBER(uint32_t, vbaseoff); // offset in vfunctable if
}LF_ONEMETHOD;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2580
struct
{
MemberAttributes attributes;
uint32_t index; // type index of referenced leaf
union
{
PDB_FLEXIBLE_ARRAY_MEMBER(char, offset);
LeafEasy lfEasy;
};
} LF_MEMBER;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2592
struct
{
MemberAttributes attributes;
uint32_t index; // index of type record for field
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
}LF_STMEMBER;
#pragma pack(pop)
} data;
};
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2131
struct MethodListEntry
{
MemberAttributes attributes; // method attribute
uint16_t pad0; // internal padding, must be 0
uint32_t index; // index to type record for procedure
PDB_FLEXIBLE_ARRAY_MEMBER(uint32_t, vbaseoff); // offset in vfunctable if virtual, empty otherwise.
};
// all CodeView records are stored as a header, followed by variable-length data.
// internal Record structs such as S_PUB32, S_GDATA32, etc. correspond to the data layout of a CodeView record of that kind.
struct Record
{
RecordHeader header;
union Data
{
#pragma pack(push, 1)
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2144
struct
{
// This is actually a list of the MethodListEntry type above, but it has flexible
// size, so you need to manually iterate.
PDB_FLEXIBLE_ARRAY_MEMBER(char, mList);
} LF_METHODLIST;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1801
struct
{
uint32_t rvtype; // type index of return value
uint32_t classtype; // type index of containing class
uint32_t thistype; // type index of this pointer (model specific)
uint8_t calltype; // calling convention (call_t)
FunctionAttributes funcattr; // attributes
uint16_t parmcount; // number of parameters
uint32_t arglist; // type index of argument list
int32_t thisadjust; // this adjuster (long because pad required anyway)
} LF_MFUNCTION;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1460
struct
{
uint32_t type; // modified type
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1090
struct
{
uint16_t MOD_const : 1;
uint16_t MOD_volatile : 1;
uint16_t MOD_unaligned : 1;
uint16_t MOD_unused : 13;
} attr; // modifier attribute modifier_t
} LF_MODIFIER;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1508
struct
{
uint32_t utype; // type index of the underlying type
struct PointerAttributes
{
uint32_t ptrtype : 5; // ordinal specifying pointer type (CV_ptrtype_e)
uint32_t ptrmode : 3; // ordinal specifying pointer mode (CV_ptrmode_e)
uint32_t isflat32 : 1; // TRUE if 0:32 pointer
uint32_t isvolatile : 1; // TRUE if volatile pointer
uint32_t isconst : 1; // TRUE if const pointer
uint32_t isunaligned : 1; // TRUE if unaligned pointer
uint32_t isrestrict : 1; // TRUE if restricted pointer (allow agressive opts)
uint32_t size : 6; // size of pointer (in bytes)
uint32_t ismocom : 1; // TRUE if it is a MoCOM pointer (^ or %)
uint32_t islref : 1; // TRUE if it is this pointer of member function with & ref-qualifier
uint32_t isrref : 1; // TRUE if it is this pointer of member function with && ref-qualifier
uint32_t unused : 10; // pad out to 32-bits for following cv_typ_t's
} attr;
union
{
struct
{
uint32_t pmclass; // index of containing class for pointer to member
uint16_t pmenum; // enumeration specifying pm format (CV_pmtype_e)
} pm;
uint16_t bseg; // base segment if PTR_BASE_SEG
PDB_FLEXIBLE_ARRAY_MEMBER(uint8_t, Sym); // copy of base symbol record (including length)
struct
{
uint32_t index; // type index if CV_PTR_BASE_TYPE
PDB_FLEXIBLE_ARRAY_MEMBER(char, name); // name of base type
} btype;
} pbase;
} LF_POINTER;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1775
struct
{
uint32_t rvtype; // type index of return value
CallingConvention calltype; // calling convention (CV_call_t)
FunctionAttributes funcattr; // attributes
uint16_t parmcount; // number of parameters
uint32_t arglist; // type index of argument list
} LF_PROCEDURE;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2043
struct
{
uint32_t count; // number of arguments
PDB_FLEXIBLE_ARRAY_MEMBER(uint32_t, arg);
} LF_ARGLIST;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2164
struct
{
uint32_t type;
uint8_t length;
uint8_t position;
PDB_FLEXIBLE_ARRAY_MEMBER(char, data);
} LF_BITFIELD;
struct
{
uint32_t elemtype; // type index of element type
uint32_t idxtype; // type index of indexing type
PDB_FLEXIBLE_ARRAY_MEMBER(char, data); // variable length data specifying size in bytes and name
} LF_ARRAY;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1631
struct
{
uint16_t count; // count of number of elements in class
TypeProperty property; // property attribute field
uint32_t field; // type index of LF_FIELD descriptor list
uint32_t derived; // type index of derived from list if not zero
uint32_t vshape; // type index of vshape table for this class
union
{
PDB_FLEXIBLE_ARRAY_MEMBER(char, data);
LeafEasy lfEasy;
};
} LF_CLASS;
struct
{
uint16_t count; // count of number of elements in class
uint32_t property; // property attribute field
uint32_t field; // type index of LF_FIELD descriptor list
uint32_t derived; // type index of derived from list if not zero
uint32_t vshape; // type index of vshape table for this class
union
{
PDB_FLEXIBLE_ARRAY_MEMBER(char, data);
LeafEasy lfEasy;
};
} LF_CLASS2;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1647
struct
{
uint16_t count; // count of number of elements in class
TypeProperty property; // property attribute field
uint32_t field; // type index of LF_FIELD descriptor list
PDB_FLEXIBLE_ARRAY_MEMBER(char, data);
} LF_UNION;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L1752
struct
{
uint16_t count; // count of number of elements in class
TypeProperty property; // property attribute field
uint32_t utype; // underlying type of the enum
uint32_t field; // type index of LF_FIELD descriptor list
PDB_FLEXIBLE_ARRAY_MEMBER(char, name);
} LF_ENUM;
// https://github.com/microsoft/microsoft-pdb/blob/master/include/cvinfo.h#L2112
struct
{
FieldList list;
} LF_FIELD;
#pragma pack(pop)
} data;
};
}
}
}

12
third_party/raw_pdb/src/PDB_Types.cpp vendored Normal file
View File

@@ -0,0 +1,12 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#include "PDB_PCH.h"
#include "PDB_Types.h"
// https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/msf/msf.cpp#L962
const char PDB::SuperBlock::MAGIC[30u] = "Microsoft C/C++ MSF 7.00\r\n\x1a\x44\x53";
const uint32_t PDB::HashTableHeader::Signature = 0xffffffffu;
const uint32_t PDB::HashTableHeader::Version = 0xeffe0000u + 19990810u;

167
third_party/raw_pdb/src/PDB_Types.h vendored Normal file
View File

@@ -0,0 +1,167 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
namespace PDB
{
// emulating std::byte from C++17 to make the intention clear that we're dealing with untyped data in certain cases, without actually requiring C++17
enum class Byte : unsigned char {};
// PDB files have the notion of "nil" pages, denoted by a special size
// https://github.com/microsoft/microsoft-pdb/blob/master/PDB/msf/msf.cpp#L177
const uint32_t NilPageSize = 0xffffffffu;
// PDB files have the notion of a "nil" stream index
// https://github.com/microsoft/microsoft-pdb/blob/master/PDB/include/msf.h#L45
const uint16_t NilStreamIndex = 0xffffu;
// this matches the definition in guiddef.h, but we don't want to pull that in
struct GUID
{
uint32_t Data1;
uint16_t Data2;
uint16_t Data3;
uint8_t Data4[8];
};
static_assert(sizeof(GUID) == 16u, "Size mismatch.");
// this matches the definition in winnt.h, but we don't want to pull that in
struct IMAGE_SECTION_HEADER
{
uint8_t Name[8];
union
{
uint32_t PhysicalAddress;
uint32_t VirtualSize;
} Misc;
uint32_t VirtualAddress;
uint32_t SizeOfRawData;
uint32_t PointerToRawData;
uint32_t PointerToRelocations;
uint32_t PointerToLinenumbers;
uint16_t NumberOfRelocations;
uint16_t NumberOfLinenumbers;
uint32_t Characteristics;
};
static_assert(sizeof(IMAGE_SECTION_HEADER) == 40u, "Size mismatch.");
// https://llvm.org/docs/PDB/MsfFile.html#msf-superblock
struct PDB_NO_DISCARD SuperBlock
{
static const char MAGIC[30u];
char fileMagic[30u];
char padding[2u];
uint32_t blockSize;
uint32_t freeBlockMapIndex; // index of the free block map
uint32_t blockCount; // number of blocks in the file
uint32_t directorySize; // size of the stream directory in bytes
uint32_t unknown;
PDB_FLEXIBLE_ARRAY_MEMBER(uint32_t, directoryBlockIndices); // indices of the blocks that make up the directory indices
};
// https://llvm.org/docs/PDB/PdbStream.html#stream-header
struct Header
{
enum class PDB_NO_DISCARD Version : uint32_t
{
VC2 = 19941610u,
VC4 = 19950623u,
VC41 = 19950814u,
VC50 = 19960307u,
VC98 = 19970604u,
VC70Dep = 19990604u,
VC70 = 20000404u,
VC80 = 20030901u,
VC110 = 20091201u,
VC140 = 20140508u
};
Version version;
uint32_t signature;
uint32_t age;
GUID guid;
};
// https://llvm.org/docs/PDB/PdbStream.html
struct NamedStreamMap
{
uint32_t length;
PDB_FLEXIBLE_ARRAY_MEMBER(char, stringTable);
struct HashTableEntry
{
uint32_t stringTableOffset;
uint32_t streamIndex;
};
};
// https://llvm.org/docs/PDB/HashTable.html
struct SerializedHashTable
{
struct Header
{
uint32_t size;
uint32_t capacity;
};
struct BitVector
{
uint32_t wordCount;
PDB_FLEXIBLE_ARRAY_MEMBER(uint32_t, words);
};
};
// https://llvm.org/docs/PDB/PdbStream.html#pdb-feature-codes
enum class PDB_NO_DISCARD FeatureCode : uint32_t
{
VC110 = 20091201,
VC140 = 20140508,
// https://github.com/microsoft/microsoft-pdb/blob/master/PDB/include/pdbcommon.h#L23
NoTypeMerge = 0x4D544F4E, // "NOTM"
MinimalDebugInfo = 0x494E494D // "MINI", i.e. executable was linked with /DEBUG:FASTLINK
};
// header of the public stream, based on PSGSIHDR defined here:
// https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h#L240
struct PublicStreamHeader
{
uint32_t symHash;
uint32_t addrMap;
uint32_t thunkCount;
uint32_t sizeOfThunk;
uint16_t isectThunkTable;
uint16_t padding;
uint32_t offsetThunkTable;
uint16_t sectionCount;
uint16_t padding2;
};
// header of the hash tables used by the public and global symbol stream, based on GSIHashHdr defined here:
// https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h#L62
struct HashTableHeader
{
static const uint32_t Signature;
static const uint32_t Version;
uint32_t signature;
uint32_t version;
uint32_t size;
uint32_t bucketCount;
};
// hash record, based on HRFile defined here:
// https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h#L8
struct HashRecord
{
uint32_t offset; // offset into the symbol record stream
uint32_t cref;
};
}

56
third_party/raw_pdb/src/PDB_Util.h vendored Normal file
View File

@@ -0,0 +1,56 @@
// Copyright 2011-2022, Molecular Matters GmbH <office@molecular-matters.com>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
#pragma once
#include "Foundation/PDB_Macros.h"
namespace PDB
{
// Converts a block index into a file offset, based on the block size of the PDB file
PDB_NO_DISCARD inline size_t ConvertBlockIndexToFileOffset(uint32_t blockIndex, uint32_t blockSize) PDB_NO_EXCEPT
{
// cast to size_t to avoid potential overflow in 64-bit
return static_cast<size_t>(blockIndex) * static_cast<size_t>(blockSize);
}
// Calculates how many blocks are needed for a certain number of bytes
PDB_NO_DISCARD inline uint32_t ConvertSizeToBlockCount(uint32_t sizeInBytes, uint32_t blockSize) PDB_NO_EXCEPT
{
// integer ceil to account for non-full blocks
return static_cast<uint32_t>((static_cast<size_t>(sizeInBytes) + blockSize - 1u) / blockSize);
};
// Returns the actual size of the data associated with a CodeView record, not including the size of the header
template <typename T>
PDB_NO_DISCARD inline uint32_t GetCodeViewRecordSize(const T* record) PDB_NO_EXCEPT
{
// the stored size includes the size of the 'kind' field, but not the size of the 'size' field itself
return record->header.size - sizeof(uint16_t);
}
template <typename Header, typename T>
PDB_NO_DISCARD inline size_t GetNameLength(const Header& header, const T& record) PDB_NO_EXCEPT
{
// we can estimate the length of the string from the size of the record
const size_t estimatedLength = header.size - sizeof(uint16_t) - sizeof(T);
if (estimatedLength == 0u)
{
return estimatedLength;
}
// we still need to account for padding after the string to find the real length
size_t nullTerminatorCount = 0u;
for (/* nothing */; nullTerminatorCount < estimatedLength; ++nullTerminatorCount)
{
if (record.name[estimatedLength - nullTerminatorCount - 1u] != '\0')
{
break;
}
}
const size_t length = estimatedLength - nullTerminatorCount;
return length;
}
}