std.pdb: obey naming conventions

These names aren't matching any formal specification; they're mostly
just ripped from LLVM code. Therefore, we should definitely follow Zig
naming conventions here.
This commit is contained in:
mlugg 2024-08-29 20:37:01 +01:00
parent 4330c40596
commit ba8d3f69ca
No known key found for this signature in database
GPG Key ID: 3F5B7DCCBF4AF02E
3 changed files with 389 additions and 387 deletions

View File

@ -63,18 +63,18 @@ pub fn deinit(self: *Pdb) void {
} }
pub fn parseDbiStream(self: *Pdb) !void { pub fn parseDbiStream(self: *Pdb) !void {
var stream = self.getStream(pdb.StreamType.Dbi) orelse var stream = self.getStream(pdb.StreamType.dbi) orelse
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
const reader = stream.reader(); const reader = stream.reader();
const header = try reader.readStruct(std.pdb.DbiStreamHeader); const header = try reader.readStruct(std.pdb.DbiStreamHeader);
if (header.VersionHeader != 19990903) // V70, only value observed by LLVM team if (header.version_header != 19990903) // V70, only value observed by LLVM team
return error.UnknownPDBVersion; return error.UnknownPDBVersion;
// if (header.Age != age) // if (header.Age != age)
// return error.UnmatchingPDB; // return error.UnmatchingPDB;
const mod_info_size = header.ModInfoSize; const mod_info_size = header.mod_info_size;
const section_contrib_size = header.SectionContributionSize; const section_contrib_size = header.section_contribution_size;
var modules = std.ArrayList(Module).init(self.allocator); var modules = std.ArrayList(Module).init(self.allocator);
errdefer modules.deinit(); errdefer modules.deinit();
@ -143,7 +143,7 @@ pub fn parseDbiStream(self: *Pdb) !void {
} }
pub fn parseInfoStream(self: *Pdb) !void { pub fn parseInfoStream(self: *Pdb) !void {
var stream = self.getStream(pdb.StreamType.Pdb) orelse var stream = self.getStream(pdb.StreamType.pdb) orelse
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
const reader = stream.reader(); const reader = stream.reader();
@ -168,23 +168,23 @@ pub fn parseInfoStream(self: *Pdb) !void {
try reader.readNoEof(name_bytes); try reader.readNoEof(name_bytes);
const HashTableHeader = extern struct { const HashTableHeader = extern struct {
Size: u32, size: u32,
Capacity: u32, capacity: u32,
fn maxLoad(cap: u32) u32 { fn maxLoad(cap: u32) u32 {
return cap * 2 / 3 + 1; return cap * 2 / 3 + 1;
} }
}; };
const hash_tbl_hdr = try reader.readStruct(HashTableHeader); const hash_tbl_hdr = try reader.readStruct(HashTableHeader);
if (hash_tbl_hdr.Capacity == 0) if (hash_tbl_hdr.capacity == 0)
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity)) if (hash_tbl_hdr.size > HashTableHeader.maxLoad(hash_tbl_hdr.capacity))
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
const present = try readSparseBitVector(&reader, self.allocator); const present = try readSparseBitVector(&reader, self.allocator);
defer self.allocator.free(present); defer self.allocator.free(present);
if (present.len != hash_tbl_hdr.Size) if (present.len != hash_tbl_hdr.size)
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
const deleted = try readSparseBitVector(&reader, self.allocator); const deleted = try readSparseBitVector(&reader, self.allocator);
defer self.allocator.free(deleted); defer self.allocator.free(deleted);
@ -212,19 +212,19 @@ pub fn getSymbolName(self: *Pdb, module: *Module, address: u64) ?[]const u8 {
var symbol_i: usize = 0; var symbol_i: usize = 0;
while (symbol_i != module.symbols.len) { while (symbol_i != module.symbols.len) {
const prefix = @as(*align(1) pdb.RecordPrefix, @ptrCast(&module.symbols[symbol_i])); const prefix: *align(1) pdb.RecordPrefix = @ptrCast(&module.symbols[symbol_i]);
if (prefix.RecordLen < 2) if (prefix.record_len < 2)
return null; return null;
switch (prefix.RecordKind) { switch (prefix.record_kind) {
.S_LPROC32, .S_GPROC32 => { .lproc32, .gproc32 => {
const proc_sym = @as(*align(1) pdb.ProcSym, @ptrCast(&module.symbols[symbol_i + @sizeOf(pdb.RecordPrefix)])); const proc_sym: *align(1) pdb.ProcSym = @ptrCast(&module.symbols[symbol_i + @sizeOf(pdb.RecordPrefix)]);
if (address >= proc_sym.CodeOffset and address < proc_sym.CodeOffset + proc_sym.CodeSize) { if (address >= proc_sym.code_offset and address < proc_sym.code_offset + proc_sym.code_size) {
return std.mem.sliceTo(@as([*:0]u8, @ptrCast(&proc_sym.Name[0])), 0); return std.mem.sliceTo(@as([*:0]u8, @ptrCast(&proc_sym.name[0])), 0);
} }
}, },
else => {}, else => {},
} }
symbol_i += prefix.RecordLen + @sizeOf(u16); symbol_i += prefix.record_len + @sizeOf(u16);
} }
return null; return null;
@ -238,44 +238,44 @@ pub fn getLineNumberInfo(self: *Pdb, module: *Module, address: u64) !std.debug.S
var skip_len: usize = undefined; var skip_len: usize = undefined;
const checksum_offset = module.checksum_offset orelse return error.MissingDebugInfo; const checksum_offset = module.checksum_offset orelse return error.MissingDebugInfo;
while (sect_offset != subsect_info.len) : (sect_offset += skip_len) { while (sect_offset != subsect_info.len) : (sect_offset += skip_len) {
const subsect_hdr = @as(*align(1) pdb.DebugSubsectionHeader, @ptrCast(&subsect_info[sect_offset])); const subsect_hdr: *align(1) pdb.DebugSubsectionHeader = @ptrCast(&subsect_info[sect_offset]);
skip_len = subsect_hdr.Length; skip_len = subsect_hdr.length;
sect_offset += @sizeOf(pdb.DebugSubsectionHeader); sect_offset += @sizeOf(pdb.DebugSubsectionHeader);
switch (subsect_hdr.Kind) { switch (subsect_hdr.kind) {
.Lines => { .lines => {
var line_index = sect_offset; var line_index = sect_offset;
const line_hdr = @as(*align(1) pdb.LineFragmentHeader, @ptrCast(&subsect_info[line_index])); const line_hdr: *align(1) pdb.LineFragmentHeader = @ptrCast(&subsect_info[line_index]);
if (line_hdr.RelocSegment == 0) if (line_hdr.reloc_segment == 0)
return error.MissingDebugInfo; return error.MissingDebugInfo;
line_index += @sizeOf(pdb.LineFragmentHeader); line_index += @sizeOf(pdb.LineFragmentHeader);
const frag_vaddr_start = line_hdr.RelocOffset; const frag_vaddr_start = line_hdr.reloc_offset;
const frag_vaddr_end = frag_vaddr_start + line_hdr.CodeSize; const frag_vaddr_end = frag_vaddr_start + line_hdr.code_size;
if (address >= frag_vaddr_start and address < frag_vaddr_end) { if (address >= frag_vaddr_start and address < frag_vaddr_end) {
// There is an unknown number of LineBlockFragmentHeaders (and their accompanying line and column records) // There is an unknown number of LineBlockFragmentHeaders (and their accompanying line and column records)
// from now on. We will iterate through them, and eventually find a SourceLocation that we're interested in, // from now on. We will iterate through them, and eventually find a SourceLocation that we're interested in,
// breaking out to :subsections. If not, we will make sure to not read anything outside of this subsection. // breaking out to :subsections. If not, we will make sure to not read anything outside of this subsection.
const subsection_end_index = sect_offset + subsect_hdr.Length; const subsection_end_index = sect_offset + subsect_hdr.length;
while (line_index < subsection_end_index) { while (line_index < subsection_end_index) {
const block_hdr = @as(*align(1) pdb.LineBlockFragmentHeader, @ptrCast(&subsect_info[line_index])); const block_hdr: *align(1) pdb.LineBlockFragmentHeader = @ptrCast(&subsect_info[line_index]);
line_index += @sizeOf(pdb.LineBlockFragmentHeader); line_index += @sizeOf(pdb.LineBlockFragmentHeader);
const start_line_index = line_index; const start_line_index = line_index;
const has_column = line_hdr.Flags.LF_HaveColumns; const has_column = line_hdr.flags.have_columns;
// All line entries are stored inside their line block by ascending start address. // All line entries are stored inside their line block by ascending start address.
// Heuristic: we want to find the last line entry // Heuristic: we want to find the last line entry
// that has a vaddr_start <= address. // that has a vaddr_start <= address.
// This is done with a simple linear search. // This is done with a simple linear search.
var line_i: u32 = 0; var line_i: u32 = 0;
while (line_i < block_hdr.NumLines) : (line_i += 1) { while (line_i < block_hdr.num_lines) : (line_i += 1) {
const line_num_entry = @as(*align(1) pdb.LineNumberEntry, @ptrCast(&subsect_info[line_index])); const line_num_entry: *align(1) pdb.LineNumberEntry = @ptrCast(&subsect_info[line_index]);
line_index += @sizeOf(pdb.LineNumberEntry); line_index += @sizeOf(pdb.LineNumberEntry);
const vaddr_start = frag_vaddr_start + line_num_entry.Offset; const vaddr_start = frag_vaddr_start + line_num_entry.offset;
if (address < vaddr_start) { if (address < vaddr_start) {
break; break;
} }
@ -283,19 +283,19 @@ pub fn getLineNumberInfo(self: *Pdb, module: *Module, address: u64) !std.debug.S
// line_i == 0 would mean that no matching pdb.LineNumberEntry was found. // line_i == 0 would mean that no matching pdb.LineNumberEntry was found.
if (line_i > 0) { if (line_i > 0) {
const subsect_index = checksum_offset + block_hdr.NameIndex; const subsect_index = checksum_offset + block_hdr.name_index;
const chksum_hdr = @as(*align(1) pdb.FileChecksumEntryHeader, @ptrCast(&module.subsect_info[subsect_index])); const chksum_hdr: *align(1) pdb.FileChecksumEntryHeader = @ptrCast(&module.subsect_info[subsect_index]);
const strtab_offset = @sizeOf(pdb.StringTableHeader) + chksum_hdr.FileNameOffset; const strtab_offset = @sizeOf(pdb.StringTableHeader) + chksum_hdr.file_name_offset;
try self.string_table.?.seekTo(strtab_offset); try self.string_table.?.seekTo(strtab_offset);
const source_file_name = try self.string_table.?.reader().readUntilDelimiterAlloc(self.allocator, 0, 1024); const source_file_name = try self.string_table.?.reader().readUntilDelimiterAlloc(self.allocator, 0, 1024);
const line_entry_idx = line_i - 1; const line_entry_idx = line_i - 1;
const column = if (has_column) blk: { const column = if (has_column) blk: {
const start_col_index = start_line_index + @sizeOf(pdb.LineNumberEntry) * block_hdr.NumLines; const start_col_index = start_line_index + @sizeOf(pdb.LineNumberEntry) * block_hdr.num_lines;
const col_index = start_col_index + @sizeOf(pdb.ColumnNumberEntry) * line_entry_idx; const col_index = start_col_index + @sizeOf(pdb.ColumnNumberEntry) * line_entry_idx;
const col_num_entry = @as(*align(1) pdb.ColumnNumberEntry, @ptrCast(&subsect_info[col_index])); const col_num_entry: *align(1) pdb.ColumnNumberEntry = @ptrCast(&subsect_info[col_index]);
break :blk col_num_entry.StartColumn; break :blk col_num_entry.start_column;
} else 0; } else 0;
const found_line_index = start_line_index + line_entry_idx * @sizeOf(pdb.LineNumberEntry); const found_line_index = start_line_index + line_entry_idx * @sizeOf(pdb.LineNumberEntry);
@ -303,7 +303,7 @@ pub fn getLineNumberInfo(self: *Pdb, module: *Module, address: u64) !std.debug.S
return .{ return .{
.file_name = source_file_name, .file_name = source_file_name,
.line = line_num_entry.Flags.Start, .line = line_num_entry.flags.start,
.column = column, .column = column,
}; };
} }
@ -334,12 +334,12 @@ pub fn getModule(self: *Pdb, index: usize) !?*Module {
return mod; return mod;
// At most one can be non-zero. // At most one can be non-zero.
if (mod.mod_info.C11ByteSize != 0 and mod.mod_info.C13ByteSize != 0) if (mod.mod_info.c11_byte_size != 0 and mod.mod_info.c13_byte_size != 0)
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
if (mod.mod_info.C13ByteSize == 0) if (mod.mod_info.c13_byte_size == 0)
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
const stream = self.getStreamById(mod.mod_info.ModuleSymStream) orelse const stream = self.getStreamById(mod.mod_info.module_sym_stream) orelse
return error.MissingDebugInfo; return error.MissingDebugInfo;
const reader = stream.reader(); const reader = stream.reader();
@ -347,23 +347,23 @@ pub fn getModule(self: *Pdb, index: usize) !?*Module {
if (signature != 4) if (signature != 4)
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
mod.symbols = try self.allocator.alloc(u8, mod.mod_info.SymByteSize - 4); mod.symbols = try self.allocator.alloc(u8, mod.mod_info.sym_byte_size - 4);
errdefer self.allocator.free(mod.symbols); errdefer self.allocator.free(mod.symbols);
try reader.readNoEof(mod.symbols); try reader.readNoEof(mod.symbols);
mod.subsect_info = try self.allocator.alloc(u8, mod.mod_info.C13ByteSize); mod.subsect_info = try self.allocator.alloc(u8, mod.mod_info.c13_byte_size);
errdefer self.allocator.free(mod.subsect_info); errdefer self.allocator.free(mod.subsect_info);
try reader.readNoEof(mod.subsect_info); try reader.readNoEof(mod.subsect_info);
var sect_offset: usize = 0; var sect_offset: usize = 0;
var skip_len: usize = undefined; var skip_len: usize = undefined;
while (sect_offset != mod.subsect_info.len) : (sect_offset += skip_len) { while (sect_offset != mod.subsect_info.len) : (sect_offset += skip_len) {
const subsect_hdr = @as(*align(1) pdb.DebugSubsectionHeader, @ptrCast(&mod.subsect_info[sect_offset])); const subsect_hdr: *align(1) pdb.DebugSubsectionHeader = @ptrCast(&mod.subsect_info[sect_offset]);
skip_len = subsect_hdr.Length; skip_len = subsect_hdr.length;
sect_offset += @sizeOf(pdb.DebugSubsectionHeader); sect_offset += @sizeOf(pdb.DebugSubsectionHeader);
switch (subsect_hdr.Kind) { switch (subsect_hdr.kind) {
.FileChecksums => { .file_checksums => {
mod.checksum_offset = sect_offset; mod.checksum_offset = sect_offset;
break; break;
}, },
@ -400,30 +400,30 @@ const Msf = struct {
const superblock = try in.readStruct(pdb.SuperBlock); const superblock = try in.readStruct(pdb.SuperBlock);
// Sanity checks // Sanity checks
if (!std.mem.eql(u8, &superblock.FileMagic, pdb.SuperBlock.file_magic)) if (!std.mem.eql(u8, &superblock.file_magic, pdb.SuperBlock.expect_magic))
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
if (superblock.FreeBlockMapBlock != 1 and superblock.FreeBlockMapBlock != 2) if (superblock.free_block_map_block != 1 and superblock.free_block_map_block != 2)
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
const file_len = try file.getEndPos(); const file_len = try file.getEndPos();
if (superblock.NumBlocks * superblock.BlockSize != file_len) if (superblock.num_blocks * superblock.block_size != file_len)
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
switch (superblock.BlockSize) { switch (superblock.block_size) {
// llvm only supports 4096 but we can handle any of these values // llvm only supports 4096 but we can handle any of these values
512, 1024, 2048, 4096 => {}, 512, 1024, 2048, 4096 => {},
else => return error.InvalidDebugInfo, else => return error.InvalidDebugInfo,
} }
const dir_block_count = blockCountFromSize(superblock.NumDirectoryBytes, superblock.BlockSize); const dir_block_count = blockCountFromSize(superblock.num_directory_bytes, superblock.block_size);
if (dir_block_count > superblock.BlockSize / @sizeOf(u32)) if (dir_block_count > superblock.block_size / @sizeOf(u32))
return error.UnhandledBigDirectoryStream; // cf. BlockMapAddr comment. return error.UnhandledBigDirectoryStream; // cf. BlockMapAddr comment.
try file.seekTo(superblock.BlockSize * superblock.BlockMapAddr); try file.seekTo(superblock.block_size * superblock.block_map_addr);
const dir_blocks = try allocator.alloc(u32, dir_block_count); const dir_blocks = try allocator.alloc(u32, dir_block_count);
for (dir_blocks) |*b| { for (dir_blocks) |*b| {
b.* = try in.readInt(u32, .little); b.* = try in.readInt(u32, .little);
} }
var directory = MsfStream.init( var directory = MsfStream.init(
superblock.BlockSize, superblock.block_size,
file, file,
dir_blocks, dir_blocks,
); );
@ -439,7 +439,7 @@ const Msf = struct {
const Nil = 0xFFFFFFFF; const Nil = 0xFFFFFFFF;
for (stream_sizes) |*s| { for (stream_sizes) |*s| {
const size = try directory.reader().readInt(u32, .little); const size = try directory.reader().readInt(u32, .little);
s.* = if (size == Nil) 0 else blockCountFromSize(size, superblock.BlockSize); s.* = if (size == Nil) 0 else blockCountFromSize(size, superblock.block_size);
} }
const streams = try allocator.alloc(MsfStream, stream_count); const streams = try allocator.alloc(MsfStream, stream_count);
@ -454,15 +454,15 @@ const Msf = struct {
var j: u32 = 0; var j: u32 = 0;
while (j < size) : (j += 1) { while (j < size) : (j += 1) {
const block_id = try directory.reader().readInt(u32, .little); const block_id = try directory.reader().readInt(u32, .little);
const n = (block_id % superblock.BlockSize); const n = (block_id % superblock.block_size);
// 0 is for pdb.SuperBlock, 1 and 2 for FPMs. // 0 is for pdb.SuperBlock, 1 and 2 for FPMs.
if (block_id == 0 or n == 1 or n == 2 or block_id * superblock.BlockSize > file_len) if (block_id == 0 or n == 1 or n == 2 or block_id * superblock.block_size > file_len)
return error.InvalidBlockIndex; return error.InvalidBlockIndex;
blocks[j] = block_id; blocks[j] = block_id;
} }
stream.* = MsfStream.init( stream.* = MsfStream.init(
superblock.BlockSize, superblock.block_size,
file, file,
blocks, blocks,
); );
@ -470,7 +470,7 @@ const Msf = struct {
} }
const end = directory.pos; const end = directory.pos;
if (end - begin != superblock.NumDirectoryBytes) if (end - begin != superblock.num_directory_bytes)
return error.InvalidStreamDirectory; return error.InvalidStreamDirectory;
return Msf{ return Msf{

View File

@ -732,14 +732,14 @@ pub const Module = switch (native_os) {
fn getSymbolFromPdb(self: *@This(), relocated_address: usize) !?std.debug.Symbol { fn getSymbolFromPdb(self: *@This(), relocated_address: usize) !?std.debug.Symbol {
var coff_section: *align(1) const coff.SectionHeader = undefined; var coff_section: *align(1) const coff.SectionHeader = undefined;
const mod_index = for (self.pdb.?.sect_contribs) |sect_contrib| { const mod_index = for (self.pdb.?.sect_contribs) |sect_contrib| {
if (sect_contrib.Section > self.coff_section_headers.len) continue; if (sect_contrib.section > self.coff_section_headers.len) continue;
// Remember that SectionContribEntry.Section is 1-based. // Remember that SectionContribEntry.Section is 1-based.
coff_section = &self.coff_section_headers[sect_contrib.Section - 1]; coff_section = &self.coff_section_headers[sect_contrib.section - 1];
const vaddr_start = coff_section.virtual_address + sect_contrib.Offset; const vaddr_start = coff_section.virtual_address + sect_contrib.offset;
const vaddr_end = vaddr_start + sect_contrib.Size; const vaddr_end = vaddr_start + sect_contrib.size;
if (relocated_address >= vaddr_start and relocated_address < vaddr_end) { if (relocated_address >= vaddr_start and relocated_address < vaddr_end) {
break sect_contrib.ModuleIndex; break sect_contrib.module_index;
} }
} else { } else {
// we have no information to add to the address // we have no information to add to the address

View File

@ -20,297 +20,297 @@ const ArrayList = std.ArrayList;
/// https://llvm.org/docs/PDB/DbiStream.html#stream-header /// https://llvm.org/docs/PDB/DbiStream.html#stream-header
pub const DbiStreamHeader = extern struct { pub const DbiStreamHeader = extern struct {
VersionSignature: i32, version_signature: i32,
VersionHeader: u32, version_header: u32,
Age: u32, age: u32,
GlobalStreamIndex: u16, global_stream_index: u16,
BuildNumber: u16, build_number: u16,
PublicStreamIndex: u16, public_stream_index: u16,
PdbDllVersion: u16, pdb_dll_version: u16,
SymRecordStream: u16, sym_record_stream: u16,
PdbDllRbld: u16, pdb_dll_rbld: u16,
ModInfoSize: u32, mod_info_size: u32,
SectionContributionSize: u32, section_contribution_size: u32,
SectionMapSize: u32, section_map_size: u32,
SourceInfoSize: i32, source_info_size: i32,
TypeServerSize: i32, type_server_size: i32,
MFCTypeServerIndex: u32, mfc_type_server_index: u32,
OptionalDbgHeaderSize: i32, optional_dbg_header_size: i32,
ECSubstreamSize: i32, ec_substream_size: i32,
Flags: u16, flags: u16,
Machine: u16, machine: u16,
Padding: u32, padding: u32,
}; };
pub const SectionContribEntry = extern struct { pub const SectionContribEntry = extern struct {
/// COFF Section index, 1-based /// COFF Section index, 1-based
Section: u16, section: u16,
Padding1: [2]u8, padding1: [2]u8,
Offset: u32, offset: u32,
Size: u32, size: u32,
Characteristics: u32, characteristics: u32,
ModuleIndex: u16, module_index: u16,
Padding2: [2]u8, padding2: [2]u8,
DataCrc: u32, data_crc: u32,
RelocCrc: u32, reloc_crc: u32,
}; };
pub const ModInfo = extern struct { pub const ModInfo = extern struct {
Unused1: u32, unused1: u32,
SectionContr: SectionContribEntry, section_contr: SectionContribEntry,
Flags: u16, flags: u16,
ModuleSymStream: u16, module_sym_stream: u16,
SymByteSize: u32, sym_byte_size: u32,
C11ByteSize: u32, c11_byte_size: u32,
C13ByteSize: u32, c13_byte_size: u32,
SourceFileCount: u16, source_file_count: u16,
Padding: [2]u8, padding: [2]u8,
Unused2: u32, unused2: u32,
SourceFileNameIndex: u32, source_file_name_index: u32,
PdbFilePathNameIndex: u32, pdb_file_path_name_index: u32,
// These fields are variable length // These fields are variable length
//ModuleName: char[], //module_name: char[],
//ObjFileName: char[], //obj_file_name: char[],
}; };
pub const SectionMapHeader = extern struct { pub const SectionMapHeader = extern struct {
/// Number of segment descriptors /// Number of segment descriptors
Count: u16, count: u16,
/// Number of logical segment descriptors /// Number of logical segment descriptors
LogCount: u16, log_count: u16,
}; };
pub const SectionMapEntry = extern struct { pub const SectionMapEntry = extern struct {
/// See the SectionMapEntryFlags enum below. /// See the SectionMapEntryFlags enum below.
Flags: u16, flags: u16,
/// Logical overlay number /// Logical overlay number
Ovl: u16, ovl: u16,
/// Group index into descriptor array. /// Group index into descriptor array.
Group: u16, group: u16,
Frame: u16, frame: u16,
/// Byte index of segment / group name in string table, or 0xFFFF. /// Byte index of segment / group name in string table, or 0xFFFF.
SectionName: u16, section_name: u16,
/// Byte index of class in string table, or 0xFFFF. /// Byte index of class in string table, or 0xFFFF.
ClassName: u16, class_name: u16,
/// Byte offset of the logical segment within physical segment. If group is set in flags, this is the offset of the group. /// Byte offset of the logical segment within physical segment. If group is set in flags, this is the offset of the group.
Offset: u32, offset: u32,
/// Byte count of the segment or group. /// Byte count of the segment or group.
SectionLength: u32, section_length: u32,
}; };
pub const StreamType = enum(u16) { pub const StreamType = enum(u16) {
Pdb = 1, pdb = 1,
Tpi = 2, tpi = 2,
Dbi = 3, dbi = 3,
Ipi = 4, ipi = 4,
}; };
/// Duplicate copy of SymbolRecordKind, but using the official CV names. Useful /// Duplicate copy of SymbolRecordKind, but using the official CV names. Useful
/// for reference purposes and when dealing with unknown record types. /// for reference purposes and when dealing with unknown record types.
pub const SymbolKind = enum(u16) { pub const SymbolKind = enum(u16) {
S_COMPILE = 1, compile = 1,
S_REGISTER_16t = 2, register_16t = 2,
S_CONSTANT_16t = 3, constant_16t = 3,
S_UDT_16t = 4, udt_16t = 4,
S_SSEARCH = 5, ssearch = 5,
S_SKIP = 7, skip = 7,
S_CVRESERVE = 8, cvreserve = 8,
S_OBJNAME_ST = 9, objname_st = 9,
S_ENDARG = 10, endarg = 10,
S_COBOLUDT_16t = 11, coboludt_16t = 11,
S_MANYREG_16t = 12, manyreg_16t = 12,
S_RETURN = 13, @"return" = 13,
S_ENTRYTHIS = 14, entrythis = 14,
S_BPREL16 = 256, bprel16 = 256,
S_LDATA16 = 257, ldata16 = 257,
S_GDATA16 = 258, gdata16 = 258,
S_PUB16 = 259, pub16 = 259,
S_LPROC16 = 260, lproc16 = 260,
S_GPROC16 = 261, gproc16 = 261,
S_THUNK16 = 262, thunk16 = 262,
S_BLOCK16 = 263, block16 = 263,
S_WITH16 = 264, with16 = 264,
S_LABEL16 = 265, label16 = 265,
S_CEXMODEL16 = 266, cexmodel16 = 266,
S_VFTABLE16 = 267, vftable16 = 267,
S_REGREL16 = 268, regrel16 = 268,
S_BPREL32_16t = 512, bprel32_16t = 512,
S_LDATA32_16t = 513, ldata32_16t = 513,
S_GDATA32_16t = 514, gdata32_16t = 514,
S_PUB32_16t = 515, pub32_16t = 515,
S_LPROC32_16t = 516, lproc32_16t = 516,
S_GPROC32_16t = 517, gproc32_16t = 517,
S_THUNK32_ST = 518, thunk32_st = 518,
S_BLOCK32_ST = 519, block32_st = 519,
S_WITH32_ST = 520, with32_st = 520,
S_LABEL32_ST = 521, label32_st = 521,
S_CEXMODEL32 = 522, cexmodel32 = 522,
S_VFTABLE32_16t = 523, vftable32_16t = 523,
S_REGREL32_16t = 524, regrel32_16t = 524,
S_LTHREAD32_16t = 525, lthread32_16t = 525,
S_GTHREAD32_16t = 526, gthread32_16t = 526,
S_SLINK32 = 527, slink32 = 527,
S_LPROCMIPS_16t = 768, lprocmips_16t = 768,
S_GPROCMIPS_16t = 769, gprocmips_16t = 769,
S_PROCREF_ST = 1024, procref_st = 1024,
S_DATAREF_ST = 1025, dataref_st = 1025,
S_ALIGN = 1026, @"align" = 1026,
S_LPROCREF_ST = 1027, lprocref_st = 1027,
S_OEM = 1028, oem = 1028,
S_TI16_MAX = 4096, ti16_max = 4096,
S_REGISTER_ST = 4097, register_st = 4097,
S_CONSTANT_ST = 4098, constant_st = 4098,
S_UDT_ST = 4099, udt_st = 4099,
S_COBOLUDT_ST = 4100, coboludt_st = 4100,
S_MANYREG_ST = 4101, manyreg_st = 4101,
S_BPREL32_ST = 4102, bprel32_st = 4102,
S_LDATA32_ST = 4103, ldata32_st = 4103,
S_GDATA32_ST = 4104, gdata32_st = 4104,
S_PUB32_ST = 4105, pub32_st = 4105,
S_LPROC32_ST = 4106, lproc32_st = 4106,
S_GPROC32_ST = 4107, gproc32_st = 4107,
S_VFTABLE32 = 4108, vftable32 = 4108,
S_REGREL32_ST = 4109, regrel32_st = 4109,
S_LTHREAD32_ST = 4110, lthread32_st = 4110,
S_GTHREAD32_ST = 4111, gthread32_st = 4111,
S_LPROCMIPS_ST = 4112, lprocmips_st = 4112,
S_GPROCMIPS_ST = 4113, gprocmips_st = 4113,
S_COMPILE2_ST = 4115, compile2_st = 4115,
S_MANYREG2_ST = 4116, manyreg2_st = 4116,
S_LPROCIA64_ST = 4117, lprocia64_st = 4117,
S_GPROCIA64_ST = 4118, gprocia64_st = 4118,
S_LOCALSLOT_ST = 4119, localslot_st = 4119,
S_PARAMSLOT_ST = 4120, paramslot_st = 4120,
S_ANNOTATION = 4121, annotation = 4121,
S_GMANPROC_ST = 4122, gmanproc_st = 4122,
S_LMANPROC_ST = 4123, lmanproc_st = 4123,
S_RESERVED1 = 4124, reserved1 = 4124,
S_RESERVED2 = 4125, reserved2 = 4125,
S_RESERVED3 = 4126, reserved3 = 4126,
S_RESERVED4 = 4127, reserved4 = 4127,
S_LMANDATA_ST = 4128, lmandata_st = 4128,
S_GMANDATA_ST = 4129, gmandata_st = 4129,
S_MANFRAMEREL_ST = 4130, manframerel_st = 4130,
S_MANREGISTER_ST = 4131, manregister_st = 4131,
S_MANSLOT_ST = 4132, manslot_st = 4132,
S_MANMANYREG_ST = 4133, manmanyreg_st = 4133,
S_MANREGREL_ST = 4134, manregrel_st = 4134,
S_MANMANYREG2_ST = 4135, manmanyreg2_st = 4135,
S_MANTYPREF = 4136, mantypref = 4136,
S_UNAMESPACE_ST = 4137, unamespace_st = 4137,
S_ST_MAX = 4352, st_max = 4352,
S_WITH32 = 4356, with32 = 4356,
S_MANYREG = 4362, manyreg = 4362,
S_LPROCMIPS = 4372, lprocmips = 4372,
S_GPROCMIPS = 4373, gprocmips = 4373,
S_MANYREG2 = 4375, manyreg2 = 4375,
S_LPROCIA64 = 4376, lprocia64 = 4376,
S_GPROCIA64 = 4377, gprocia64 = 4377,
S_LOCALSLOT = 4378, localslot = 4378,
S_PARAMSLOT = 4379, paramslot = 4379,
S_MANFRAMEREL = 4382, manframerel = 4382,
S_MANREGISTER = 4383, manregister = 4383,
S_MANSLOT = 4384, manslot = 4384,
S_MANMANYREG = 4385, manmanyreg = 4385,
S_MANREGREL = 4386, manregrel = 4386,
S_MANMANYREG2 = 4387, manmanyreg2 = 4387,
S_UNAMESPACE = 4388, unamespace = 4388,
S_DATAREF = 4390, dataref = 4390,
S_ANNOTATIONREF = 4392, annotationref = 4392,
S_TOKENREF = 4393, tokenref = 4393,
S_GMANPROC = 4394, gmanproc = 4394,
S_LMANPROC = 4395, lmanproc = 4395,
S_ATTR_FRAMEREL = 4398, attr_framerel = 4398,
S_ATTR_REGISTER = 4399, attr_register = 4399,
S_ATTR_REGREL = 4400, attr_regrel = 4400,
S_ATTR_MANYREG = 4401, attr_manyreg = 4401,
S_SEPCODE = 4402, sepcode = 4402,
S_LOCAL_2005 = 4403, local_2005 = 4403,
S_DEFRANGE_2005 = 4404, defrange_2005 = 4404,
S_DEFRANGE2_2005 = 4405, defrange2_2005 = 4405,
S_DISCARDED = 4411, discarded = 4411,
S_LPROCMIPS_ID = 4424, lprocmips_id = 4424,
S_GPROCMIPS_ID = 4425, gprocmips_id = 4425,
S_LPROCIA64_ID = 4426, lprocia64_id = 4426,
S_GPROCIA64_ID = 4427, gprocia64_id = 4427,
S_DEFRANGE_HLSL = 4432, defrange_hlsl = 4432,
S_GDATA_HLSL = 4433, gdata_hlsl = 4433,
S_LDATA_HLSL = 4434, ldata_hlsl = 4434,
S_LOCAL_DPC_GROUPSHARED = 4436, local_dpc_groupshared = 4436,
S_DEFRANGE_DPC_PTR_TAG = 4439, defrange_dpc_ptr_tag = 4439,
S_DPC_SYM_TAG_MAP = 4440, dpc_sym_tag_map = 4440,
S_ARMSWITCHTABLE = 4441, armswitchtable = 4441,
S_POGODATA = 4444, pogodata = 4444,
S_INLINESITE2 = 4445, inlinesite2 = 4445,
S_MOD_TYPEREF = 4447, mod_typeref = 4447,
S_REF_MINIPDB = 4448, ref_minipdb = 4448,
S_PDBMAP = 4449, pdbmap = 4449,
S_GDATA_HLSL32 = 4450, gdata_hlsl32 = 4450,
S_LDATA_HLSL32 = 4451, ldata_hlsl32 = 4451,
S_GDATA_HLSL32_EX = 4452, gdata_hlsl32_ex = 4452,
S_LDATA_HLSL32_EX = 4453, ldata_hlsl32_ex = 4453,
S_FASTLINK = 4455, fastlink = 4455,
S_INLINEES = 4456, inlinees = 4456,
S_END = 6, end = 6,
S_INLINESITE_END = 4430, inlinesite_end = 4430,
S_PROC_ID_END = 4431, proc_id_end = 4431,
S_THUNK32 = 4354, thunk32 = 4354,
S_TRAMPOLINE = 4396, trampoline = 4396,
S_SECTION = 4406, section = 4406,
S_COFFGROUP = 4407, coffgroup = 4407,
S_EXPORT = 4408, @"export" = 4408,
S_LPROC32 = 4367, lproc32 = 4367,
S_GPROC32 = 4368, gproc32 = 4368,
S_LPROC32_ID = 4422, lproc32_id = 4422,
S_GPROC32_ID = 4423, gproc32_id = 4423,
S_LPROC32_DPC = 4437, lproc32_dpc = 4437,
S_LPROC32_DPC_ID = 4438, lproc32_dpc_id = 4438,
S_REGISTER = 4358, register = 4358,
S_PUB32 = 4366, pub32 = 4366,
S_PROCREF = 4389, procref = 4389,
S_LPROCREF = 4391, lprocref = 4391,
S_ENVBLOCK = 4413, envblock = 4413,
S_INLINESITE = 4429, inlinesite = 4429,
S_LOCAL = 4414, local = 4414,
S_DEFRANGE = 4415, defrange = 4415,
S_DEFRANGE_SUBFIELD = 4416, defrange_subfield = 4416,
S_DEFRANGE_REGISTER = 4417, defrange_register = 4417,
S_DEFRANGE_FRAMEPOINTER_REL = 4418, defrange_framepointer_rel = 4418,
S_DEFRANGE_SUBFIELD_REGISTER = 4419, defrange_subfield_register = 4419,
S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE = 4420, defrange_framepointer_rel_full_scope = 4420,
S_DEFRANGE_REGISTER_REL = 4421, defrange_register_rel = 4421,
S_BLOCK32 = 4355, block32 = 4355,
S_LABEL32 = 4357, label32 = 4357,
S_OBJNAME = 4353, objname = 4353,
S_COMPILE2 = 4374, compile2 = 4374,
S_COMPILE3 = 4412, compile3 = 4412,
S_FRAMEPROC = 4114, frameproc = 4114,
S_CALLSITEINFO = 4409, callsiteinfo = 4409,
S_FILESTATIC = 4435, filestatic = 4435,
S_HEAPALLOCSITE = 4446, heapallocsite = 4446,
S_FRAMECOOKIE = 4410, framecookie = 4410,
S_CALLEES = 4442, callees = 4442,
S_CALLERS = 4443, callers = 4443,
S_UDT = 4360, udt = 4360,
S_COBOLUDT = 4361, coboludt = 4361,
S_BUILDINFO = 4428, buildinfo = 4428,
S_BPREL32 = 4363, bprel32 = 4363,
S_REGREL32 = 4369, regrel32 = 4369,
S_CONSTANT = 4359, constant = 4359,
S_MANCONSTANT = 4397, manconstant = 4397,
S_LDATA32 = 4364, ldata32 = 4364,
S_GDATA32 = 4365, gdata32 = 4365,
S_LMANDATA = 4380, lmandata = 4380,
S_GMANDATA = 4381, gmandata = 4381,
S_LTHREAD32 = 4370, lthread32 = 4370,
S_GTHREAD32 = 4371, gthread32 = 4371,
}; };
pub const TypeIndex = u32; pub const TypeIndex = u32;
@ -320,28 +320,28 @@ pub const TypeIndex = u32;
// we should define RecordPrefix as part of the ProcSym structure. // we should define RecordPrefix as part of the ProcSym structure.
// This might be important when we start generating PDB in self-hosted with our own PE linker. // This might be important when we start generating PDB in self-hosted with our own PE linker.
pub const ProcSym = extern struct { pub const ProcSym = extern struct {
Parent: u32, parent: u32,
End: u32, end: u32,
Next: u32, next: u32,
CodeSize: u32, code_size: u32,
DbgStart: u32, dbg_start: u32,
DbgEnd: u32, dbg_end: u32,
FunctionType: TypeIndex, function_type: TypeIndex,
CodeOffset: u32, code_offset: u32,
Segment: u16, segment: u16,
Flags: ProcSymFlags, flags: ProcSymFlags,
Name: [1]u8, // null-terminated name: [1]u8, // null-terminated
}; };
pub const ProcSymFlags = packed struct { pub const ProcSymFlags = packed struct {
HasFP: bool, has_fp: bool,
HasIRET: bool, has_iret: bool,
HasFRET: bool, has_fret: bool,
IsNoReturn: bool, is_no_return: bool,
IsUnreachable: bool, is_unreachable: bool,
HasCustomCallingConv: bool, has_custom_calling_conv: bool,
IsNoInline: bool, is_no_inline: bool,
HasOptimizedDebugInfo: bool, has_optimized_debug_info: bool,
}; };
pub const SectionContrSubstreamVersion = enum(u32) { pub const SectionContrSubstreamVersion = enum(u32) {
@ -351,11 +351,11 @@ pub const SectionContrSubstreamVersion = enum(u32) {
}; };
pub const RecordPrefix = extern struct { pub const RecordPrefix = extern struct {
/// Record length, starting from &RecordKind. /// Record length, starting from &record_kind.
RecordLen: u16, record_len: u16,
/// Record kind enum (SymRecordKind or TypeRecordKind) /// Record kind enum (SymRecordKind or TypeRecordKind)
RecordKind: SymbolKind, record_kind: SymbolKind,
}; };
/// The following variable length array appears immediately after the header. /// The following variable length array appears immediately after the header.
@ -364,19 +364,19 @@ pub const RecordPrefix = extern struct {
/// Each `LineBlockFragmentHeader` as specified below. /// Each `LineBlockFragmentHeader` as specified below.
pub const LineFragmentHeader = extern struct { pub const LineFragmentHeader = extern struct {
/// Code offset of line contribution. /// Code offset of line contribution.
RelocOffset: u32, reloc_offset: u32,
/// Code segment of line contribution. /// Code segment of line contribution.
RelocSegment: u16, reloc_segment: u16,
Flags: LineFlags, flags: LineFlags,
/// Code size of this line contribution. /// Code size of this line contribution.
CodeSize: u32, code_size: u32,
}; };
pub const LineFlags = packed struct { pub const LineFlags = packed struct {
/// CV_LINES_HAVE_COLUMNS /// CV_LINES_HAVE_COLUMNS
LF_HaveColumns: bool, have_columns: bool,
unused: u15, unused: u15,
}; };
@ -389,107 +389,109 @@ pub const LineBlockFragmentHeader = extern struct {
/// checksums buffer. The checksum entry then /// checksums buffer. The checksum entry then
/// contains another offset into the string /// contains another offset into the string
/// table of the actual name. /// table of the actual name.
NameIndex: u32, name_index: u32,
NumLines: u32, num_lines: u32,
/// code size of block, in bytes /// code size of block, in bytes
BlockSize: u32, block_size: u32,
}; };
pub const LineNumberEntry = extern struct { pub const LineNumberEntry = extern struct {
/// Offset to start of code bytes for line number /// Offset to start of code bytes for line number
Offset: u32, offset: u32,
Flags: packed struct(u32) { flags: Flags,
pub const Flags = packed struct(u32) {
/// Start line number /// Start line number
Start: u24, start: u24,
/// Delta of lines to the end of the expression. Still unclear. /// Delta of lines to the end of the expression. Still unclear.
// TODO figure out the point of this field. // TODO figure out the point of this field.
End: u7, end: u7,
IsStatement: bool, is_statement: bool,
}, };
}; };
pub const ColumnNumberEntry = extern struct { pub const ColumnNumberEntry = extern struct {
StartColumn: u16, start_column: u16,
EndColumn: u16, end_column: u16,
}; };
/// Checksum bytes follow. /// Checksum bytes follow.
pub const FileChecksumEntryHeader = extern struct { pub const FileChecksumEntryHeader = extern struct {
/// Byte offset of filename in global string table. /// Byte offset of filename in global string table.
FileNameOffset: u32, file_name_offset: u32,
/// Number of bytes of checksum. /// Number of bytes of checksum.
ChecksumSize: u8, checksum_size: u8,
/// FileChecksumKind /// FileChecksumKind
ChecksumKind: u8, checksum_kind: u8,
}; };
pub const DebugSubsectionKind = enum(u32) { pub const DebugSubsectionKind = enum(u32) {
None = 0, none = 0,
Symbols = 0xf1, symbols = 0xf1,
Lines = 0xf2, lines = 0xf2,
StringTable = 0xf3, string_table = 0xf3,
FileChecksums = 0xf4, file_checksums = 0xf4,
FrameData = 0xf5, frame_data = 0xf5,
InlineeLines = 0xf6, inlinee_lines = 0xf6,
CrossScopeImports = 0xf7, cross_scope_imports = 0xf7,
CrossScopeExports = 0xf8, cross_scope_exports = 0xf8,
// These appear to relate to .Net assembly info. // These appear to relate to .Net assembly info.
ILLines = 0xf9, il_lines = 0xf9,
FuncMDTokenMap = 0xfa, func_md_token_map = 0xfa,
TypeMDTokenMap = 0xfb, type_md_token_map = 0xfb,
MergedAssemblyInput = 0xfc, merged_assembly_input = 0xfc,
CoffSymbolRVA = 0xfd, coff_symbol_rva = 0xfd,
}; };
pub const DebugSubsectionHeader = extern struct { pub const DebugSubsectionHeader = extern struct {
/// codeview::DebugSubsectionKind enum /// codeview::DebugSubsectionKind enum
Kind: DebugSubsectionKind, kind: DebugSubsectionKind,
/// number of bytes occupied by this record. /// number of bytes occupied by this record.
Length: u32, length: u32,
}; };
pub const StringTableHeader = extern struct { pub const StringTableHeader = extern struct {
/// PDBStringTableSignature /// PDBStringTableSignature
Signature: u32, signature: u32,
/// 1 or 2 /// 1 or 2
HashVersion: u32, hash_version: u32,
/// Number of bytes of names buffer. /// Number of bytes of names buffer.
ByteSize: u32, byte_size: u32,
}; };
// https://llvm.org/docs/PDB/MsfFile.html#the-superblock // https://llvm.org/docs/PDB/MsfFile.html#the-superblock
pub const SuperBlock = extern struct { pub const SuperBlock = extern struct {
/// The LLVM docs list a space between C / C++ but empirically this is not the case. /// The LLVM docs list a space between C / C++ but empirically this is not the case.
pub const file_magic = "Microsoft C/C++ MSF 7.00\r\n\x1a\x44\x53\x00\x00\x00"; pub const expect_magic = "Microsoft C/C++ MSF 7.00\r\n\x1a\x44\x53\x00\x00\x00";
FileMagic: [file_magic.len]u8, file_magic: [expect_magic.len]u8,
/// The block size of the internal file system. Valid values are 512, 1024, /// The block size of the internal file system. Valid values are 512, 1024,
/// 2048, and 4096 bytes. Certain aspects of the MSF file layout vary depending /// 2048, and 4096 bytes. Certain aspects of the MSF file layout vary depending
/// on the block sizes. For the purposes of LLVM, we handle only block sizes of /// on the block sizes. For the purposes of LLVM, we handle only block sizes of
/// 4KiB, and all further discussion assumes a block size of 4KiB. /// 4KiB, and all further discussion assumes a block size of 4KiB.
BlockSize: u32, block_size: u32,
/// The index of a block within the file, at which begins a bitfield representing /// The index of a block within the file, at which begins a bitfield representing
/// the set of all blocks within the file which are free (i.e. the data within /// the set of all blocks within the file which are free (i.e. the data within
/// that block is not used). See The Free Block Map for more information. Important: /// that block is not used). See The Free Block Map for more information. Important:
/// FreeBlockMapBlock can only be 1 or 2! /// FreeBlockMapBlock can only be 1 or 2!
FreeBlockMapBlock: u32, free_block_map_block: u32,
/// The total number of blocks in the file. NumBlocks * BlockSize should equal the /// The total number of blocks in the file. NumBlocks * BlockSize should equal the
/// size of the file on disk. /// size of the file on disk.
NumBlocks: u32, num_blocks: u32,
/// The size of the stream directory, in bytes. The stream directory contains /// The size of the stream directory, in bytes. The stream directory contains
/// information about each streams size and the set of blocks that it occupies. /// information about each streams size and the set of blocks that it occupies.
/// It will be described in more detail later. /// It will be described in more detail later.
NumDirectoryBytes: u32, num_directory_bytes: u32,
Unknown: u32, unknown: u32,
/// The index of a block within the MSF file. At this block is an array of /// The index of a block within the MSF file. At this block is an array of
/// ulittle32_ts listing the blocks that the stream directory resides on. /// ulittle32_ts listing the blocks that the stream directory resides on.
/// For large MSF files, the stream directory (which describes the block /// For large MSF files, the stream directory (which describes the block
@ -505,5 +507,5 @@ pub const SuperBlock = extern struct {
// This would mean the Stream Directory is bigger than BlockSize / sizeof(u32) // This would mean the Stream Directory is bigger than BlockSize / sizeof(u32)
// blocks. We're not even close to this with a 1GB pdb file, and LLVM didn't // blocks. We're not even close to this with a 1GB pdb file, and LLVM didn't
// implement it so we're kind of safe making this assumption for now. // implement it so we're kind of safe making this assumption for now.
BlockMapAddr: u32, block_map_addr: u32,
}; };