Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-26 10:02:47 -05:00
commit 253eca39ed
33 files changed
+7506

No files matched your search

+110
View File
@@ -0,0 +1,110 @@
#! @file digest.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains MD5, as RFC 1321 gives it, whose digest of a PDB file names it, so
#! the same build names its file the same way.
module binary::pdb;
#! The MD5 digest of bytes.
#! @param data The bytes.
#! @returns The digest, its first byte first.
pub def md5_of(data: span<u8>): u8[16] {
var state: u32[4] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
var block: u8[64];
val total := data.length;
var at := 0u;
while at + 64 <= total {
md5_block(&mut state, data.data + at);
at += 64;
}
# The last bytes, then `0x80`, then zeros, then the length in bits, low
# byte first, filling one block or two.
val left := total - at;
for k in 0..64 {
block[k] = 0;
}
for k in 0..left {
block[k] = data[at + k];
}
block[left] = 0x80;
if left >= 56 {
md5_block(&mut state, (&block) as *u8);
for k in 0..64 {
block[k] = 0;
}
}
val bits := total * 8;
for k in 0..8 {
block[56 + k] = ((bits >> (8 * k as u64)) & 0xff) as u8;
}
md5_block(&mut state, (&block) as *u8);
var made: u8[16];
for w in 0..4u {
for k in 0..4u {
made[4 * w + k] = ((state[w] >> (8 * k as u32)) & 0xff) as u8;
}
}
return made;
}
# The amounts each round of MD5 turns its words left by.
val md5_turns: u32[16] = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21];
# Mixes a block of 64 bytes into MD5's state.
def md5_block(state: &mut u32[4], at: *u8): void {
var words: u32[16];
for k in 0..16 {
val from := at + 4 * k;
words[k] = from[0] as u32 | (from[1] as u32 << 8) | (from[2] as u32 << 16) | (from[3] as u32 << 24);
}
var a := state[0];
var b := state[1];
var c := state[2];
var d := state[3];
for i in 0u..64u {
var mixed: u32 = 0;
var index: u64 = 0;
if i < 16 {
mixed = (b & c) | (~b & d);
index = i;
} else if i < 32 {
mixed = (d & b) | (~d & c);
index = (5 * i + 1) % 16;
} else if i < 48 {
mixed = b ^ c ^ d;
index = (3 * i + 5) % 16;
} else {
mixed = c ^ (b | ~d);
index = (7 * i) % 16;
}
val sum := a + mixed + md5_constants[i] + words[index];
a = d;
d = c;
c = b;
val by := md5_turns[(i / 16) * 4 + i % 4];
b = b + ((sum << by) | (sum >> (32 - by)));
}
state[0] = state[0] + a;
state[1] = state[1] + b;
state[2] = state[2] + c;
state[3] = state[3] + d;
}
# MD5's constant of each step: the whole part of 2^32 times the sine of the
# step's number, counted from 1.
val md5_constants: u32[64] = [
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
];
+250
View File
@@ -0,0 +1,250 @@
#! @file lines.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the writer of a module's lines, as the subsections after its
#! symbols hold them: the files the module's code is from, named in the PDB
#! file's table of names, the lines of each procedure's code, and the file and
#! line each function inlined somewhere begins at. And the annotations an
#! inline site says which of its procedure's code it is with, and the lines of
#! each part.
import std::io;
module binary::pdb;
#! Adds a subsection: its kind, its bytes, then what it holds, padded to four
#! bytes.
#! @param out Where.
#! @param kind Its kind, as `debug_s_lines`.
#! @param data What it holds.
pub def subsection(out: &mut byte_writer, kind: u32, data: span<u8>): void {
out.u32(kind);
out.u32(data.length);
out.put(data);
out.align_to(4);
}
#! The files a module's code is from, as its subsection of checksums names
#! them, each with no checksum.
pub struct file_checksums {
pub var bytes: byte_writer; #! The subsection's contents.
}
#! Makes a subsection naming no files.
#! @returns The subsection.
pub def file_checksums::new(): file_checksums {
return file_checksums { bytes = byte_writer::new() };
}
#! Names a file, giving where its entry is, which lines name the file by.
#! @param this The subsection.
#! @param name Where the file's path is in the PDB file's table of names.
#! @returns Where its entry is.
pub def file_checksums::file(&mut this, name: u32): u32 {
val at := this.bytes.length as u32;
this.bytes.u32(name);
this.bytes.byte(0); # No checksum,
this.bytes.byte(0); # of no kind.
this.bytes.align_to(4);
return at;
}
#! A line of a procedure's code.
struct line_entry {
var offset: u32; #! Where its code begins, from where the procedure's does.
var line: u32;
var column: u16;
}
#! The lines of a procedure's code, as a subsection of lines holds them: each
#! run of lines of one file in a block of its own.
pub struct function_lines {
pub var bytes: byte_writer; #! The subsection's contents.
var file: u32; #! The file of the block being written.
var pending: vector<line_entry>; #! Its lines.
}
#! Begins the lines of a procedure's code.
#! @param section The section holding its code.
#! @param offset Where its code begins in it.
#! @param size The bytes of its code.
#! @returns The lines.
pub def function_lines::new(section: u16, offset: u32, size: u32): function_lines {
var made := function_lines { bytes = byte_writer::new(), file = 0, pending = vector::<line_entry>::new() };
made.bytes.u32(offset);
made.bytes.u16(section);
made.bytes.u16(1); # With columns.
made.bytes.u32(size);
return made;
}
#! Adds a line: the code from an offset is from a line and column of a file.
#! @param this The lines.
#! @param file Where the file's entry is among the checksums.
#! @param offset Where the code begins, from where the procedure's does.
#! @param line The line, from 1.
#! @param column The column, from 1, or 0 for none.
pub def function_lines::line(&mut this, file: u32, offset: u32, line: u32, column: u16): void {
if this.pending.length != 0 && file != this.file {
this.flush();
}
this.file = file;
this.pending.append(line_entry { offset = offset, line = line, column = column });
}
#! Ends the lines, writing the last block.
#! @param this The lines.
pub def function_lines::finish(&mut this): void {
if this.pending.length != 0 {
this.flush();
}
}
# Writes the block of the lines kept, each a statement, then their columns.
def function_lines::flush(&mut this): void {
val count := this.pending.length;
this.bytes.u32(this.file);
this.bytes.u32(count);
this.bytes.u32(12 + 12 * count);
for i in 0..count {
val each: line_entry = this.pending[i];
this.bytes.u32(each.offset);
this.bytes.u32((each.line & 0xFFFFFF) | 0x80000000);
}
for i in 0..count {
val each: line_entry = this.pending[i];
this.bytes.u16(each.column);
this.bytes.u16(0);
}
this.pending.clear();
}
#! The file and line each function inlined somewhere begins at, as the
#! subsection of inlinees' lines holds them.
pub struct inlinee_lines {
pub var bytes: byte_writer; #! The subsection's contents.
pub var count: u64; #! How many functions it names.
}
#! Makes a subsection naming no functions.
#! @returns The subsection.
pub def inlinee_lines::new(): inlinee_lines {
var made := inlinee_lines { bytes = byte_writer::new(), count = 0 };
made.bytes.u32(0); # Each entry naming one file.
return made;
}
#! Names where a function inlined somewhere begins.
#! @param this The subsection.
#! @param inlinee The function's ID.
#! @param file Where the file's entry is among the checksums.
#! @param line The line it is declared at.
pub def inlinee_lines::add(&mut this, inlinee: u32, file: u32, line: u32): void {
this.bytes.u32(inlinee);
this.bytes.u32(file);
this.bytes.u32(line);
this.count++;
}
# Annotations.
# What each annotation does.
const ba_change_code_offset: u32 = 3;
const ba_change_code_length: u32 = 4;
const ba_change_file: u32 = 5;
const ba_change_line_offset: u32 = 6;
const ba_change_code_offset_and_line_offset: u32 = 11;
#! The annotations of an inline site, written as its code is met: each part of
#! the procedure's code the site is, from where the procedure's code begins,
#! and the line of each, from the line the function inlined begins at. Parts
#! of the same line are one.
pub struct annotations {
pub var bytes: byte_writer; #! The annotations.
var file: u32; #! The file of the last part, as its entry among the checksums.
var line: u32; #! Its line.
var at: u32; #! Where the last annotation left the code.
var open: bool; #! Whether the last part is not yet ended.
var begun: bool; #! Whether a part has begun.
}
#! Begins the annotations of a site, from where the function inlined begins.
#! @param file The file it is declared in, as its entry among the checksums.
#! @param line The line it is declared at.
#! @returns The annotations.
pub def annotations::new(file: u32, line: u32): annotations {
return annotations { bytes = byte_writer::new(), file = file, line = line, at = 0, open = false, begun = false };
}
# Adds an annotation and its operand, each in as few bytes as hold it.
def annotations::put(&mut this, value: u32): void {
if value < 0x80 {
this.bytes.byte(value as u8);
} else if value < 0x4000 {
this.bytes.byte(((value >> 8) | 0x80) as u8);
this.bytes.byte((value & 0xFF) as u8);
} else {
this.bytes.byte(((value >> 24) | 0xC0) as u8);
this.bytes.byte(((value >> 16) & 0xFF) as u8);
this.bytes.byte(((value >> 8) & 0xFF) as u8);
this.bytes.byte((value & 0xFF) as u8);
}
}
#! The site's code from an offset is from a line of a file. Where the part
#! before it is from the same line, it goes on.
#! @param this The annotations.
#! @param offset Where the code begins, from where the procedure's does.
#! @param file Its file, as its entry among the checksums.
#! @param line Its line.
pub def annotations::at_line(&mut this, offset: u32, file: u32, line: u32): void {
return if this.open && file == this.file && line == this.line;
this.open = true;
if file != this.file {
this.put(ba_change_file);
this.put(file);
}
val delta := line as i64 - this.line as i64;
val encoded: u32 = delta < 0 ? ((-delta) as u32 << 1) | 1 : (delta as u32) << 1;
val code := offset - this.at;
if encoded < 8 && code <= 0xF {
this.put(ba_change_code_offset_and_line_offset);
this.put((encoded << 4) | code);
} else {
# The first part's line is said though it is where the function begins,
# as some readers take a part's line only from a change to it.
if delta != 0 || !this.begun {
this.put(ba_change_line_offset);
this.put(encoded);
}
this.put(ba_change_code_offset);
this.put(code);
}
this.begun = true;
this.at = offset;
this.file = file;
this.line = line;
}
#! The code from an offset is not the site's, so the part before it ends.
#! @param this The annotations.
#! @param offset Where that code begins.
pub def annotations::away(&mut this, offset: u32): void {
if this.open {
this.put(ba_change_code_length);
this.put(offset - this.at);
this.at = offset;
}
this.open = false;
}
#! Ends the annotations where the site's last part ends.
#! @param this The annotations.
#! @param offset Where it ends.
pub def annotations::finish(&mut this, offset: u32): void {
this.put(ba_change_code_length);
this.put(offset - this.at);
}
+224
View File
@@ -0,0 +1,224 @@
#! @file msf.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the writer of the file a PDB file's streams are held in, an MSF
#! file of version 7: blocks of 4096 bytes, the first describing the file, the
#! second and third, and those 4096 blocks after them, the map of which blocks
#! are free, then each stream's blocks in turn, then the directory naming each
#! stream's size and blocks, and a block naming the directory's. And the table
#! of strings a PDB file names files and other text by.
import std::containers;
import std::io;
module binary::pdb;
#! The bytes of a block.
const block_bytes: u64 = 4096;
#! Whether a block holds a map of free blocks, as every block 1 and 2 past a
#! multiple of 4096 does.
#! @param block The block.
#! @returns Whether it does.
def holds_free_map(block: u64): bool {
val within := block % block_bytes;
return within == 1 || within == 2;
}
#! Lays streams out in an MSF file, giving the file, and where the first byte
#! of each stream's first block is, or 0 for one holding nothing.
#! @param streams The streams, by number.
#! @param firsts Where the first byte of each is, filled.
#! @returns The file.
pub def msf_file(streams: &vector<byte_writer>, firsts: &mut vector<u64>): byte_writer {
# Blocks are given out in order, past the maps of free blocks.
var next: u64 = 3;
var blocks := vector::<u64>::new(); # Each stream's blocks, one after another.
var first_block := vector::<u64>::new(); # Where each stream's are in `blocks`.
firsts.clear();
for &each in streams.view() {
first_block.append(blocks.length);
val count := (each.length + block_bytes - 1) / block_bytes;
firsts.append(0);
for _ in 0..count {
while holds_free_map(next) {
next++;
}
blocks.append(next);
next++;
}
}
first_block.append(blocks.length);
# The directory: how many streams, each one's size, then each one's blocks.
var directory := byte_writer::new();
directory.u32(streams.length);
for &each in streams.view() {
directory.u32(each.length);
}
for &each in blocks.view() {
directory.u32(each);
}
var directory_blocks := vector::<u64>::new();
val directory_count := (directory.length + block_bytes - 1) / block_bytes;
for _ in 0..directory_count {
while holds_free_map(next) {
next++;
}
directory_blocks.append(next);
next++;
}
while holds_free_map(next) {
next++;
}
val map_block := next;
next++;
val total := next;
var file := byte_writer::new();
file.zeros(total * block_bytes);
# The file's description: its signature, the size of its blocks, the map of
# free blocks in use, how many blocks there are, the directory's bytes, and
# the block naming the directory's blocks.
val magic: strview = "Microsoft C/C++ MSF 7.00\r\n";
for i in 0..magic.length {
file.set_byte(i, magic[i]);
}
file.set_byte(26, 0x1A);
file.set_byte(27, 'D' as u8);
file.set_byte(28, 'S' as u8);
file.set_u32(32, block_bytes);
file.set_u32(36, 1);
file.set_u32(40, total);
file.set_u32(44, directory.length);
file.set_u32(48, 0);
file.set_u32(52, map_block);
# Every map of free blocks says every block is free, but those the file
# holds, which the map in use, the one in block 1 and those 4096 after it,
# says are taken, a bit of each, the first block's lowest.
var b: u64 = 1;
while b < total {
if holds_free_map(b) {
for k in 0..block_bytes {
file.set_byte(b * block_bytes + k, 0xFF);
}
}
b++;
}
for block in 0..total {
val map := 1 + (block / (8 * block_bytes)) * block_bytes;
val byte := map * block_bytes + (block % (8 * block_bytes)) / 8;
val bit := block % 8;
file.set_byte(byte, file.get_byte(byte) & ~((1u << bit) as u8));
}
# Each stream's bytes, in its blocks.
for s in 0..streams.length {
val data := streams[s].view();
var at: u64 = 0;
for k in first_block[s]..first_block[s + 1] {
val block := blocks[k];
if at == 0 {
firsts[s] = block * block_bytes;
}
val take := data.length - at < block_bytes ? data.length - at : block_bytes;
memcpy(file.slice().data + block * block_bytes, data.data + at, take);
at += take;
}
}
var at: u64 = 0;
val directory_data := directory.view();
for &block in directory_blocks.view() {
val take := directory_data.length - at < block_bytes ? directory_data.length - at : block_bytes;
memcpy(file.slice().data + block * block_bytes, directory_data.data + at, take);
at += take;
}
for k in 0..directory_blocks.length {
file.set_u32(map_block * block_bytes + 4 * k, directory_blocks[k]);
}
return file;
}
# The table of names.
#! A table of strings, as the stream `/names` holds them: each string once,
#! ending with a zero, after the empty one, and a table of their hashes a
#! reader finds one by.
pub struct name_table {
var bytes: byte_writer; #! The strings.
var held: interner<string>; #! The strings, by ID.
var at: vector<u32>; #! Where each is in the table, by its ID.
}
#! Makes a table holding only the empty string.
#! @returns The table.
pub def name_table::new(): name_table {
var made := name_table { bytes = byte_writer::new(), held = interner::<string>::new(), at = vector::<u32>::new() };
made.bytes.byte(0);
return made;
}
#! Where a string is in the table, adding it where it is new.
#! @param this The table.
#! @param text The string.
#! @returns Its offset.
pub def name_table::offset_of(&mut this, text: strview): u32 {
return 0 if text.length == 0;
val id := this.held.intern_view(text);
if id < this.at.length {
return this.at[id];
}
val at := this.bytes.length as u32;
this.bytes.put_terminated(text);
this.at.append(at);
return at;
}
#! The table as a stream holds it: its signature, the version of its hash, the
#! strings' bytes, then the strings' places by their hashes, each in the first
#! free bucket from its hash's on, then how many strings there are. The
#! buckets grow as Microsoft's tools grow them, so three of four at most hold a
#! string.
#! @param this The table.
#! @returns The stream.
pub def name_table::stream(&this): byte_writer {
val count := this.at.length;
var buckets: u64 = 1;
for k in 1..count + 1 {
if buckets * 3 / 4 < k {
buckets = buckets * 3 / 2 + 1;
}
}
var slots := vector::<u32>::new();
slots.resize(buckets);
val all := this.bytes.view();
for &offset in this.at.view() {
var end := offset as u64;
while all[end] != 0 {
end++;
}
val text := span::<u8>::over(all.data + offset as u64, end - offset as u64);
var slot := hash_v1(text) as u64 % buckets;
while slots[slot] != 0 {
slot = (slot + 1) % buckets;
}
slots[slot] = offset;
}
var made := byte_writer::new();
made.u32(0xEFFEEFFE);
made.u32(1);
made.u32(this.bytes.length);
made.put(all);
made.u32(buckets);
for &each in slots.view() {
made.u32(each);
}
made.u32(count);
return made;
}
+212
View File
@@ -0,0 +1,212 @@
#! @file pdb.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the numbers of PDB files and of CodeView, the debugging information
#! they hold, as Microsoft's tools number them: the simple types, the kinds of
#! type records and of symbols, the subsections of a module's lines, the
#! languages, and the registers of x86-64. And the hashes a PDB file's tables
#! find names and types by, as Microsoft's tools compute them, so a debugger
#! finds what this writes.
module binary::pdb;
# Simple types, whose index is their number, held by no record.
pub const t_notype: u32 = 0x0000; #! No type.
pub const t_void: u32 = 0x0003;
pub const t_bool08: u32 = 0x0030;
pub const t_real32: u32 = 0x0040;
pub const t_real64: u32 = 0x0041;
pub const t_rchar: u32 = 0x0070; #! A character of a byte.
pub const t_char32: u32 = 0x007B; #! A character of 32 bits.
pub const t_int1: u32 = 0x0068;
pub const t_uint1: u32 = 0x0069;
pub const t_int2: u32 = 0x0072;
pub const t_uint2: u32 = 0x0073;
pub const t_int4: u32 = 0x0074;
pub const t_uint4: u32 = 0x0075;
pub const t_int8: u32 = 0x0076;
pub const t_uint8: u32 = 0x0077;
#! Added to a simple type, a 64-bit pointer to it, as `t_64p | t_void` is a
#! pointer to nothing in particular.
pub const t_64p: u32 = 0x0600;
#! The index of the first type a record holds, past every simple type's.
pub const first_type: u32 = 0x1000;
# Kinds of type records, which CodeView calls leaves.
pub const lf_modifier: u16 = 0x1001;
pub const lf_pointer: u16 = 0x1002;
pub const lf_procedure: u16 = 0x1008;
pub const lf_arglist: u16 = 0x1201;
pub const lf_fieldlist: u16 = 0x1203;
pub const lf_index: u16 = 0x1404; #! A field list going on in another record.
pub const lf_enumerate: u16 = 0x1502;
pub const lf_array: u16 = 0x1503;
pub const lf_structure: u16 = 0x1505;
pub const lf_union: u16 = 0x1506;
pub const lf_enum: u16 = 0x1507;
pub const lf_member: u16 = 0x150D;
pub const lf_func_id: u16 = 0x1601;
pub const lf_string_id: u16 = 0x1605;
# The leaves a number past what 15 bits hold is written as, after its kind.
pub const lf_char: u16 = 0x8000;
pub const lf_short: u16 = 0x8001;
pub const lf_ushort: u16 = 0x8002;
pub const lf_long: u16 = 0x8003;
pub const lf_ulong: u16 = 0x8004;
pub const lf_quadword: u16 = 0x8009;
pub const lf_uquadword: u16 = 0x800A;
#! The properties of a struct, a union, or an enum.
pub const property_forward: u16 = 0x0080; #! Only declared, its definition named elsewhere.
pub const property_unique: u16 = 0x0200; #! Named by a unique name, after its name.
#! The access a member is given: public.
pub const access_public: u16 = 3;
# Kinds of symbols.
pub const s_end: u16 = 0x0006;
pub const s_frameproc: u16 = 0x1012;
pub const s_objname: u16 = 0x1101;
pub const s_block32: u16 = 0x1103;
pub const s_udt: u16 = 0x1108;
pub const s_gdata32: u16 = 0x110D;
pub const s_pub32: u16 = 0x110E;
pub const s_lproc32: u16 = 0x110F;
pub const s_gproc32: u16 = 0x1110;
pub const s_regrel32: u16 = 0x1111;
pub const s_procref: u16 = 0x1125;
pub const s_lprocref: u16 = 0x1127;
pub const s_section: u16 = 0x1136;
pub const s_compile3: u16 = 0x113C;
pub const s_local: u16 = 0x113E;
pub const s_defrange_register: u16 = 0x1141;
pub const s_defrange_framepointer_rel: u16 = 0x1142;
pub const s_defrange_subfield_register: u16 = 0x1143;
pub const s_defrange_framepointer_rel_full_scope: u16 = 0x1144;
pub const s_defrange_register_rel: u16 = 0x1145;
pub const s_inlinesite: u16 = 0x114D;
pub const s_inlinesite_end: u16 = 0x114E;
#! What a procedure is: it keeps a frame pointer.
pub const proc_has_frame_pointer: u8 = 0x01;
#! What a public symbol names: code, and a function.
pub const public_code: u32 = 0x01;
pub const public_function: u32 = 0x02;
#! What a local is: a parameter.
pub const local_parameter: u16 = 0x0001;
#! The register a procedure's locals and parameters are found from, as its
#! frame's record encodes it: `rbp`, for each.
pub const frame_locals_rbp: u32 = 2 << 14;
pub const frame_params_rbp: u32 = 2 << 16;
# The subsections a module's lines are held in.
pub const debug_s_lines: u32 = 0xF2;
pub const debug_s_file_checksums: u32 = 0xF4;
pub const debug_s_inlinee_lines: u32 = 0xF6;
# Languages and machines.
pub const language_c: u8 = 0x00;
pub const language_cxx: u8 = 0x01;
pub const language_link: u8 = 0x07; #! The linker's own module.
pub const cv_machine_x64: u16 = 0xD0;
pub const image_machine_x64: u16 = 0x8664;
# The registers of x86-64, as CodeView numbers them.
pub const amd64_rbp: u16 = 334;
pub const amd64_rsp: u16 = 335;
#! A general register's number, as CodeView numbers the part of a width.
#! @param number The register, as x86-64 encodes it: `rax` is 0, `r8` is 8.
#! @param width The bytes of it: 1, 2, 4, or 8.
#! @returns Its number.
pub def amd64_register(number: u8, width: u64): u16 {
val n := (number & 15) as u16;
if width == 1 {
# `al`, `cl`, `dl`, and `bl`, then `spl`, `bpl`, `sil`, and `dil`.
val low: u16[8] = [1, 2, 3, 4, 327, 326, 324, 325];
return n < 8 ? low[n as u64] : 344 + n - 8;
}
if width == 2 {
return n < 8 ? 9 + n : 352 + n - 8;
}
if width == 4 {
return n < 8 ? 17 + n : 360 + n - 8;
}
# `rax`, `rcx`, `rdx`, `rbx`, `rsp`, `rbp`, `rsi`, and `rdi`, as numbered.
val whole: u16[8] = [328, 330, 331, 329, 335, 334, 332, 333];
return n < 8 ? whole[n as u64] : 336 + n - 8;
}
#! An SSE register's number.
#! @param number The register: `xmm0` is 0.
#! @returns Its number.
pub def amd64_xmm(number: u8): u16 {
val n := (number & 15) as u16;
return n < 8 ? 154 + n : 252 + n - 8;
}
# Hashes.
#! The hash a PDB file finds a name by, in its tables of strings, of globals,
#! and of types: the words of the text xored together, then the half word and
#! the byte left over, its letters' cases made alike.
#! @param text The name.
#! @returns The hash.
pub def hash_v1(text: span<u8>): u32 {
var made: u32 = 0;
val words := text.length / 4;
for i in 0..words {
val at := 4 * i;
made = made ^ (text[at] as u32 | (text[at + 1] as u32 << 8) | (text[at + 2] as u32 << 16) | (text[at + 3] as u32 << 24));
}
var at := 4 * words;
var left := text.length % 4;
if left >= 2 {
made = made ^ (text[at] as u32 | (text[at + 1] as u32 << 8));
at += 2;
left -= 2;
}
if left == 1 {
made = made ^ text[at] as u32;
}
made = made | 0x20202020;
made = made ^ (made >> 11);
return made ^ (made >> 16);
}
#! The hash a PDB file finds a type record by, of its bytes: CRC-32 begun at
#! zero and never inverted, as JamCRC is.
#! @param bytes The record.
#! @returns The hash.
pub def hash_record(bytes: span<u8>): u32 {
var made: u32 = 0;
for &each in bytes {
var c := (made ^ each as u32) & 0xFF;
for _ in 0..8u {
c = (c & 1) != 0 ? 0xEDB88320 ^ (c >> 1) : c >> 1;
}
made = c ^ (made >> 8);
}
return made;
}
+338
View File
@@ -0,0 +1,338 @@
#! @file symbols.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the writer of symbols, as a module's stream holds them: the
#! module's name and compiler, each procedure, the scopes within it, as blocks
#! and the sites a call was inlined at, and the locals of each with where they
#! are over which code. A scope's record names the one holding it and the
#! record ending it, by where each is in the stream, which this writes once the
#! scope ends. The records the stream of globals and publics hold are written
#! here too.
import std::io;
module binary::pdb;
#! A run of code in a section, which a local is somewhere over.
pub struct code_range {
pub var section: u16; #! The section, numbered from 1.
pub var offset: u32; #! Where the code begins in it.
pub var length: u16; #! How many bytes.
}
#! A module's symbols being written.
pub struct symbol_stream {
pub var bytes: byte_writer; #! The stream, which begins with its signature.
var scopes: vector<u64>; #! Where each scope still open begins, the innermost last.
var sites: vector<bool>; #! Whether each is an inline site, which ends with a record of its own.
}
#! Makes a stream holding no symbols, beginning with the signature of
#! CodeView's C13 format.
#! @returns The stream.
pub def symbol_stream::new(): symbol_stream {
var made := symbol_stream { bytes = byte_writer::new(), scopes = vector::<u64>::new(), sites = vector::<bool>::new() };
made.bytes.u32(4);
return made;
}
# Where the innermost open scope begins, or 0 where none is open.
def symbol_stream::parent(&this): u64 {
return this.scopes.length == 0 ? 0 : this.scopes.last();
}
# Opens a scope at a record just begun, writing where the one holding it is,
# and a place for where it ends.
def symbol_stream::open(&mut this, at: u64, site: bool): void {
this.bytes.u32(this.parent());
this.bytes.u32(0);
this.scopes.append(at);
this.sites.append(site);
}
#! Adds the module's name, as the object it was compiled to.
#! @param this The stream.
#! @param name The name.
pub def symbol_stream::object_name(&mut this, name: strview): void {
val at := begin_record(&mut this.bytes, s_objname);
this.bytes.u32(0);
put_name(&mut this.bytes, name);
end_symbol_record(&mut this.bytes, at);
}
#! Adds what compiled the module: its language, for the machine, and its
#! version, as `S_COMPILE3` says it.
#! @param this The stream.
#! @param language The language, as `language_cxx`.
#! @param machine The machine, as `cv_machine_x64`.
#! @param version The compiler's major, minor, and build numbers.
#! @param name The compiler's name and version, as text.
pub def symbol_stream::compiler(&mut this, language: u8, machine: u16, version: u16[3], name: strview): void {
val at := begin_record(&mut this.bytes, s_compile3);
this.bytes.u32(language as u64);
this.bytes.u16(machine);
# The front end's version, then the back end's, each with no fix.
for _ in 0..2u {
for k in 0..3u {
this.bytes.u16(version[k]);
}
this.bytes.u16(0);
}
put_name(&mut this.bytes, name);
end_symbol_record(&mut this.bytes, at);
}
#! Begins a procedure, whose frame, locals, and scopes follow, then `end`.
#! @param this The stream.
#! @param global Whether it is seen outside its module.
#! @param name Its name.
#! @param type_index Its type.
#! @param section The section holding its code.
#! @param offset Where its code begins in it.
#! @param size The bytes of its code.
#! @param body Where its prologue ends, from where it begins, or 0.
#! @param leaving Where its epilogue begins, from where it begins, or 0.
#! @param flags What it is, as `proc_has_frame_pointer`.
#! @returns Where its record is in the stream, as the globals name it by.
pub def symbol_stream::procedure(
&mut this,
global: bool,
name: strview,
type_index: u32,
section: u16,
offset: u32,
size: u32,
body: u32,
leaving: u32,
flags: u8
): u64 {
val at := begin_record(&mut this.bytes, global ? s_gproc32 : s_lproc32);
this.open(at, false);
this.bytes.u32(0); # No procedure follows it within a scope.
this.bytes.u32(size);
this.bytes.u32(body);
this.bytes.u32(leaving);
this.bytes.u32(type_index);
this.bytes.u32(offset);
this.bytes.u16(section);
this.bytes.byte(flags);
put_name(&mut this.bytes, name);
end_symbol_record(&mut this.bytes, at);
return at;
}
#! Adds what a procedure's frame holds, and the register its locals and its
#! parameters are found from.
#! @param this The stream.
#! @param bytes The bytes of the frame.
#! @param saved The bytes of the registers it saves.
#! @param registers The registers, as `frame_locals_rbp | frame_params_rbp`.
pub def symbol_stream::frame(&mut this, bytes: u32, saved: u32, registers: u32): void {
val at := begin_record(&mut this.bytes, s_frameproc);
this.bytes.u32(bytes);
this.bytes.u32(0); # No padding,
this.bytes.u32(0);
this.bytes.u32(saved);
this.bytes.u32(0); # and no handler of exceptions.
this.bytes.u16(0);
this.bytes.u32(registers);
end_symbol_record(&mut this.bytes, at);
}
#! Begins a block of a procedure's code, whose locals and scopes follow, then
#! `end`.
#! @param this The stream.
#! @param section The section holding its code.
#! @param offset Where its code begins in it.
#! @param size The bytes of its code.
pub def symbol_stream::block(&mut this, section: u16, offset: u32, size: u32): void {
val at := begin_record(&mut this.bytes, s_block32);
this.open(at, false);
this.bytes.u32(size);
this.bytes.u32(offset);
this.bytes.u16(section);
put_name(&mut this.bytes, "");
end_symbol_record(&mut this.bytes, at);
}
#! Begins the site a function was inlined at, whose locals and scopes follow,
#! then `end`. The annotations say which of the procedure's code the site is,
#! and the lines of each part.
#! @param this The stream.
#! @param inlinee The ID of the function inlined.
#! @param annotations The site's annotations.
pub def symbol_stream::inline_site(&mut this, inlinee: u32, annotations: span<u8>): void {
val at := begin_record(&mut this.bytes, s_inlinesite);
this.open(at, true);
this.bytes.u32(inlinee);
this.bytes.put(annotations);
end_symbol_record(&mut this.bytes, at);
}
#! Ends the innermost scope, naming where it ends in the record that began it.
#! @param this The stream.
pub def symbol_stream::end(&mut this): void {
val begun := this.scopes.last();
val site := this.sites.last();
this.scopes.truncate();
this.sites.truncate();
val at := begin_record(&mut this.bytes, site ? s_inlinesite_end : s_end);
end_symbol_record(&mut this.bytes, at);
this.bytes.set_u32(begun + 8, at);
}
#! Adds a local, where it is following in records of the ranges it is in.
#! @param this The stream.
#! @param type_index Its type.
#! @param flags What it is, as `local_parameter`.
#! @param name Its name.
pub def symbol_stream::local(&mut this, type_index: u32, flags: u16, name: strview): void {
val at := begin_record(&mut this.bytes, s_local);
this.bytes.u32(type_index);
this.bytes.u16(flags);
put_name(&mut this.bytes, name);
end_symbol_record(&mut this.bytes, at);
}
# Adds a range, with no gaps.
def symbol_stream::range(&mut this, over: code_range): void {
this.bytes.u32(over.offset);
this.bytes.u16(over.section);
this.bytes.u16(over.length);
}
#! The local before is in a register over a range.
#! @param this The stream.
#! @param register The register, as CodeView numbers it.
#! @param over The range.
pub def symbol_stream::in_register(&mut this, register: u16, over: code_range): void {
val at := begin_record(&mut this.bytes, s_defrange_register);
this.bytes.u16(register);
this.bytes.u16(0);
this.range(over);
end_symbol_record(&mut this.bytes, at);
}
#! The local before is at an offset from the frame pointer over a range.
#! @param this The stream.
#! @param offset The offset.
#! @param over The range.
pub def symbol_stream::in_frame(&mut this, offset: i32, over: code_range): void {
val at := begin_record(&mut this.bytes, s_defrange_framepointer_rel);
this.bytes.u32(offset as u64);
this.range(over);
end_symbol_record(&mut this.bytes, at);
}
#! The local before is at an offset from the frame pointer wherever its scope
#! is, as a parameter is.
#! @param this The stream.
#! @param offset The offset.
pub def symbol_stream::in_frame_throughout(&mut this, offset: i32): void {
val at := begin_record(&mut this.bytes, s_defrange_framepointer_rel_full_scope);
this.bytes.u32(offset as u64);
end_symbol_record(&mut this.bytes, at);
}
#! A part of the local before, from an offset within it, is in a register over
#! a range.
#! @param this The stream.
#! @param register The register.
#! @param part Where the part begins in the local, below 4096.
#! @param over The range.
pub def symbol_stream::part_in_register(&mut this, register: u16, part: u32, over: code_range): void {
val at := begin_record(&mut this.bytes, s_defrange_subfield_register);
this.bytes.u16(register);
this.bytes.u16(0);
this.bytes.u32(part & 0xFFF);
this.range(over);
end_symbol_record(&mut this.bytes, at);
}
#! The local before is in memory at an offset from what a register holds over
#! a range.
#! @param this The stream.
#! @param register The register.
#! @param offset The offset.
#! @param over The range.
pub def symbol_stream::through_register(&mut this, register: u16, offset: i32, over: code_range): void {
val at := begin_record(&mut this.bytes, s_defrange_register_rel);
this.bytes.u16(register);
this.bytes.u16(0);
this.bytes.u32(offset as u64);
this.range(over);
end_symbol_record(&mut this.bytes, at);
}
#! Adds a local at an offset from a register throughout its scope, as
#! `S_REGREL32` says it.
#! @param this The stream.
#! @param offset The offset.
#! @param type_index Its type.
#! @param register The register.
#! @param name Its name.
pub def symbol_stream::relative(&mut this, offset: i32, type_index: u32, register: u16, name: strview): void {
val at := begin_record(&mut this.bytes, s_regrel32);
this.bytes.u32(offset as u64);
this.bytes.u32(type_index);
this.bytes.u16(register);
put_name(&mut this.bytes, name);
end_symbol_record(&mut this.bytes, at);
}
#! Adds each section of the image, as the linker's module names them, from
#! their headers as the image holds them.
#! @param this The stream.
#! @param headers The headers, 40 bytes each.
pub def symbol_stream::sections(&mut this, headers: &byte_writer): void {
for k in 0..headers.length / 40 {
val header := 40 * k;
val at := begin_record(&mut this.bytes, s_section);
this.bytes.u16(k + 1);
this.bytes.byte(12); # Aligned to a page,
this.bytes.byte(0);
this.bytes.u32(headers.get_u32(header + 12));
this.bytes.u32(headers.get_u32(header + 8));
this.bytes.u32(headers.get_u32(header + 36));
for i in 0..8u {
val letter := headers.get_byte(header + i);
break if letter == 0;
this.bytes.byte(letter);
}
this.bytes.byte(0);
end_symbol_record(&mut this.bytes, at);
}
}
#! Writes a public symbol's record: a name, and the place it names.
#! @param out Where.
#! @param flags What it names, as `public_function | public_code`.
#! @param section The section.
#! @param offset Where in it.
#! @param name The name.
pub def public_record(out: &mut byte_writer, flags: u32, section: u16, offset: u32, name: strview): void {
val at := begin_record(out, s_pub32);
out.u32(flags);
out.u32(offset);
out.u16(section);
put_name(out, name);
end_symbol_record(out, at);
}
#! Writes the record naming a procedure among the globals: its name, and where
#! its record is in its module's stream.
#! @param out Where.
#! @param global Whether the procedure is global.
#! @param owner Its module, numbered from 1.
#! @param offset Where its record is in the module's stream.
#! @param name Its name.
pub def procedure_reference(out: &mut byte_writer, global: bool, owner: u16, offset: u32, name: strview): void {
val at := begin_record(out, global ? s_procref : s_lprocref);
out.u32(0);
out.u32(offset);
out.u16(owner);
put_name(out, name);
end_symbol_record(out, at);
}
+504
View File
@@ -0,0 +1,504 @@
#! @file types.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the writer of type records, as a PDB file's stream of types and its
#! stream of IDs hold them: each record's length, its kind, and what it holds,
#! padded to four bytes, numbered from `first_type` in the order they are
#! added, a record only naming those before it. A record added twice is held
#! once. Each is hashed as Microsoft's tools hash it, so a debugger finds a
#! struct's definition by its name.
import std::containers;
import std::io;
module binary::pdb;
#! How many buckets the hashes of a stream of types are taken modulo.
const type_buckets: u32 = 0x3FFFF;
#! Begins a record: its length, written once it ends, then its kind.
#! @param out Where.
#! @param kind Its kind.
#! @returns Where it begins.
pub def begin_record(out: &mut byte_writer, kind: u16): u64 {
val at := out.length;
out.u16(0);
out.u16(kind);
return at;
}
#! Ends a type record, padding it to four bytes with the bytes that say how
#! many are left, as `LF_PAD3`, `LF_PAD2`, and `LF_PAD1` do, then writing its
#! length, which leaves out its own two bytes.
#! @param out Where.
#! @param at Where it begins.
pub def end_type_record(out: &mut byte_writer, at: u64): void {
pad_type(out);
out.set_u16(at, out.length - at - 2);
}
#! Pads a type record, or a member of a field list, to four bytes, as
#! `end_type_record` does.
#! @param out Where.
def pad_type(out: &mut byte_writer): void {
while out.length % 4 != 0 {
out.byte((0xF0 + 4 - out.length % 4) as u8);
}
}
#! Ends a symbol record, padding it to four bytes with zeros, then writing its
#! length, which leaves out its own two bytes.
#! @param out Where.
#! @param at Where it begins.
pub def end_symbol_record(out: &mut byte_writer, at: u64): void {
while (out.length - at) % 4 != 0 {
out.byte(0);
}
out.set_u16(at, out.length - at - 2);
}
#! Adds a name, ending with a zero.
#! @param out Where.
#! @param text The name.
pub def put_name(out: &mut byte_writer, text: strview): void {
out.put_terminated(text);
}
#! Adds a number, as CodeView's numeric leaves write one: in two bytes where it
#! is below 0x8000, else after the kind of leaf its size takes.
#! @param out Where.
#! @param value The number.
pub def put_unsigned(out: &mut byte_writer, value: u64): void {
if value < 0x8000 {
out.u16(value);
} else if value <= 0xFFFF {
out.u16(lf_ushort as u64);
out.u16(value);
} else if value <= 0xFFFFFFFF {
out.u16(lf_ulong as u64);
out.u32(value);
} else {
out.u16(lf_uquadword as u64);
out.u64(value);
}
}
#! Adds a signed number, as `put_unsigned` does one not below zero, and a
#! negative one after the kind of leaf its size takes.
#! @param out Where.
#! @param value The number.
pub def put_signed(out: &mut byte_writer, value: i64): void {
if value >= 0 {
put_unsigned(out, value as u64);
} else if value >= -128 {
out.u16(lf_char as u64);
out.byte(value as u8);
} else if value >= -32768 {
out.u16(lf_short as u64);
out.u16(value as u64);
} else if value >= -2147483648 {
out.u16(lf_long as u64);
out.u32(value as u64);
} else {
out.u16(lf_quadword as u64);
out.u64(value as u64);
}
}
#! The records of a stream of types, or of IDs, which are written as types are.
pub struct type_table {
pub var records: byte_writer; #! Each record, one after another.
var offsets: vector<u32>; #! Where each record begins.
var hashes: vector<u32>; #! Each record's hash, modulo the buckets.
var held: interner<string>; #! Each record's bytes, by its index less the first.
}
#! Makes a table holding no records.
#! @returns The table.
pub def type_table::new(): type_table {
return type_table {
records = byte_writer::new(),
offsets = vector::<u32>::new(),
hashes = vector::<u32>::new(),
held = interner::<string>::new()
};
}
#! How many records the table holds.
#! @param this The table.
#! @returns How many.
pub val type_table::count: u64 {
get => this.offsets.length;
}
#! Adds a record, or finds it where the table holds it already, hashed by its
#! bytes.
#! @param this The table.
#! @param record The record, its length and kind first.
#! @returns Its index.
pub def type_table::add(&mut this, record: span<u8>): u32 {
return this.add_hashed(record, hash_record(record));
}
#! Adds a record, or finds it, with its hash given, as a struct's is its name's.
#! @param this The table.
#! @param record The record.
#! @param hash Its hash, before the buckets.
#! @returns Its index.
pub def type_table::add_hashed(&mut this, record: span<u8>, hash: u32): u32 {
val id := this.held.intern_view(record);
if id < this.offsets.length {
return first_type + id;
}
this.offsets.append(this.records.length as u32);
this.hashes.append(hash % type_buckets);
this.records.put(record);
return first_type + id;
}
#! A pointer of 64 bits, or a reference, to a type.
#! @param this The table.
#! @param referent The type.
#! @param reference Whether it is a reference.
#! @returns Its index.
pub def type_table::pointer(&mut this, referent: u32, reference: bool): u32 {
var made := byte_writer::new();
val at := begin_record(&mut made, lf_pointer);
made.u32(referent);
# A pointer of 64 bits, a pointer or a reference to a value, of 8 bytes.
made.u32(0x0C | ((reference ? 1u : 0u) << 5) | (8u << 13));
end_type_record(&mut made, at);
return this.add(made.view());
}
#! An array of a type, indexed by 64-bit numbers.
#! @param this The table.
#! @param element The type of each element.
#! @param bytes The bytes of the whole array.
#! @returns Its index.
pub def type_table::array(&mut this, element: u32, bytes: u64): u32 {
var made := byte_writer::new();
val at := begin_record(&mut made, lf_array);
made.u32(element);
made.u32(0x23); # `unsigned __int64`.
put_unsigned(&mut made, bytes);
put_name(&mut made, "");
end_type_record(&mut made, at);
return this.add(made.view());
}
#! The list of the types of a function's parameters.
#! @param this The table.
#! @param types The types.
#! @returns Its index.
pub def type_table::arguments(&mut this, types: span<u32>): u32 {
var made := byte_writer::new();
val at := begin_record(&mut made, lf_arglist);
made.u32(types.length);
for &each in types {
made.u32(each);
}
end_type_record(&mut made, at);
return this.add(made.view());
}
#! A function's type, called as C is.
#! @param this The table.
#! @param returns The type it returns.
#! @param arguments The list of the types of its parameters.
#! @param count How many it takes.
#! @returns Its index.
pub def type_table::procedure(&mut this, returns: u32, arguments: u32, count: u16): u32 {
var made := byte_writer::new();
val at := begin_record(&mut made, lf_procedure);
made.u32(returns);
made.byte(0); # Called as C is, near,
made.byte(0); # with no options.
made.u16(count);
made.u32(arguments);
end_type_record(&mut made, at);
return this.add(made.view());
}
#! The members of a struct, a union, or an enum, as a field list holds them,
#! each padded to four bytes.
pub struct field_list {
pub var count: u16; #! How many members it holds.
var bytes: byte_writer; #! The members, one after another.
var starts: vector<u64>; #! Where each member begins.
}
#! Makes a list of no members.
#! @returns The list.
pub def field_list::new(): field_list {
return field_list { count = 0, bytes = byte_writer::new(), starts = vector::<u64>::new() };
}
#! Adds a member of a struct or union.
#! @param this The list.
#! @param type_index Its type.
#! @param offset Where it begins.
#! @param name Its name.
pub def field_list::member(&mut this, type_index: u32, offset: u64, name: strview): void {
this.starts.append(this.bytes.length);
this.bytes.u16(lf_member as u64);
this.bytes.u16(access_public as u64);
this.bytes.u32(type_index);
put_unsigned(&mut this.bytes, offset);
put_name(&mut this.bytes, name);
pad_type(&mut this.bytes);
this.count++;
}
#! Adds a member of an enum.
#! @param this The list.
#! @param value Its value.
#! @param name Its name.
pub def field_list::enumerate(&mut this, value: i64, name: strview): void {
this.starts.append(this.bytes.length);
this.bytes.u16(lf_enumerate as u64);
this.bytes.u16(access_public as u64);
put_signed(&mut this.bytes, value);
put_name(&mut this.bytes, name);
pad_type(&mut this.bytes);
this.count++;
}
#! The most a record of a field list holds, past its length and kind, leaving
#! room for the member naming the record it goes on in.
const fields_most: u64 = 0xFF00 - 4 - 8;
#! Adds a field list's records, giving the index of the first. A list too long
#! for one record goes on in another, added first, which the one before it
#! names with `LF_INDEX` as its last member.
#! @param this The table.
#! @param list The list.
#! @returns The index of its first record.
pub def type_table::fields(&mut this, list: &field_list): u32 {
# Where each record's members begin, as many to a record as it holds.
var firsts := vector::<u64>::new();
firsts.append(0);
var size: u64 = 0;
for m in 0..list.starts.length {
val end := m + 1 < list.starts.length ? list.starts[m + 1] : list.bytes.length;
val bytes := end - list.starts[m];
if size + bytes > fields_most && size != 0 {
firsts.append(m);
size = 0;
}
size += bytes;
}
# The last record first, so each names one added before it.
val all := list.bytes.view();
var next: u32 = 0;
var r := firsts.length;
while r > 0 {
r--;
val first := firsts[r];
val from := first < list.starts.length ? list.starts[first] : list.bytes.length;
val to := r + 1 < firsts.length ? list.starts[firsts[r + 1]] : list.bytes.length;
var made := byte_writer::new();
val at := begin_record(&mut made, lf_fieldlist);
if to > from {
made.put(span::<u8>::over(all.data + from, to - from));
}
if next != 0 {
made.u16(lf_index as u64);
made.u16(0);
made.u32(next);
}
end_type_record(&mut made, at);
next = this.add(made.view());
}
return next;
}
#! The properties of a struct, a union, or an enum, by whether it is only
#! declared and whether it has a unique name.
def properties(forward: bool, unique: strview): u16 {
var made: u16 = forward ? property_forward : 0;
if unique.length != 0 {
made = made | property_unique;
}
return made;
}
#! Adds a struct's record, or the record declaring it, hashed so a debugger
#! finds the definition the declaration names by its name.
#! @param this The table.
#! @param name Its name.
#! @param unique Its unique name, which tells it apart from another of its
#! name, or empty for none.
#! @param fields Its field list, or 0 where it is only declared.
#! @param count How many members the list holds.
#! @param size Its bytes.
#! @param forward Whether it is only declared, its definition elsewhere.
#! @returns Its index.
pub def type_table::structure(&mut this, name: strview, unique: strview, fields: u32, count: u16, size: u64, forward: bool): u32 {
var made := byte_writer::new();
val at := begin_record(&mut made, lf_structure);
made.u16(count);
made.u16(properties(forward, unique));
made.u32(fields);
made.u32(0); # Derived from nothing,
made.u32(0); # with no virtual functions.
put_unsigned(&mut made, size);
put_name(&mut made, name);
if unique.length != 0 {
put_name(&mut made, unique);
}
end_type_record(&mut made, at);
return this.add_tag(made.view(), name, forward);
}
#! Adds a union's record, or the record declaring it, as `structure` does a
#! struct's.
#! @param this The table.
#! @param name Its name.
#! @param unique Its unique name, or empty for none.
#! @param fields Its field list, or 0 where it is only declared.
#! @param count How many members the list holds.
#! @param size Its bytes.
#! @param forward Whether it is only declared.
#! @returns Its index.
pub def type_table::union_of(&mut this, name: strview, unique: strview, fields: u32, count: u16, size: u64, forward: bool): u32 {
var made := byte_writer::new();
val at := begin_record(&mut made, lf_union);
made.u16(count);
made.u16(properties(forward, unique));
made.u32(fields);
put_unsigned(&mut made, size);
put_name(&mut made, name);
if unique.length != 0 {
put_name(&mut made, unique);
}
end_type_record(&mut made, at);
return this.add_tag(made.view(), name, forward);
}
#! Adds an enum's record, as `structure` does a struct's.
#! @param this The table.
#! @param name Its name.
#! @param unique Its unique name, or empty for none.
#! @param underlying The integer it is held as.
#! @param fields Its field list, or 0 where it is only declared.
#! @param count How many members the list holds.
#! @param forward Whether it is only declared.
#! @returns Its index.
pub def type_table::enumeration(&mut this, name: strview, unique: strview, underlying: u32, fields: u32, count: u16, forward: bool): u32 {
var made := byte_writer::new();
val at := begin_record(&mut made, lf_enum);
made.u16(count);
made.u16(properties(forward, unique));
made.u32(underlying);
made.u32(fields);
put_name(&mut made, name);
if unique.length != 0 {
put_name(&mut made, unique);
}
end_type_record(&mut made, at);
return this.add_tag(made.view(), name, forward);
}
# A struct's, union's, or enum's definition is hashed by its name, which a
# declaration is found to it by, and a declaration by its bytes.
def type_table::add_tag(&mut this, record: span<u8>, name: strview, forward: bool): u32 {
return this.add_hashed(record, forward ? hash_record(record) : hash_v1(name));
}
#! Adds the ID of a function, as the stream of IDs holds it, which an inline
#! site names what it inlined by, and the inlinees' lines say where it begins.
#! Each is a function of its own, though another has its name and type, as two
#! of one name in different modules have, so none is found as another's.
#! @param this The table.
#! @param scope The ID of the scope it is declared in, or 0.
#! @param type_index Its type, in the stream of types.
#! @param name Its name.
#! @returns Its index.
pub def type_table::function_id(&mut this, scope: u32, type_index: u32, name: strview): u32 {
var made := byte_writer::new();
val at := begin_record(&mut made, lf_func_id);
made.u32(scope);
made.u32(type_index);
put_name(&mut made, name);
end_type_record(&mut made, at);
return this.add_distinct(made.view());
}
#! Adds a record, though the table holds one alike, hashed by its bytes.
#! @param this The table.
#! @param record The record.
#! @returns Its index.
pub def type_table::add_distinct(&mut this, record: span<u8>): u32 {
# It is held under its bytes and its index, which no other record's are.
var key := byte_writer::new();
key.put(record);
key.u64(this.offsets.length);
_ = this.held.intern_view(key.view());
val index := this.offsets.length as u32;
this.offsets.append(this.records.length as u32);
this.hashes.append(hash_record(record) % type_buckets);
this.records.put(record);
return first_type + index;
}
#! The stream holding the records: its header, then the records.
#! @param this The table.
#! @param hash_stream The stream holding the records' hashes.
#! @returns The stream.
pub def type_table::stream(&this, hash_stream: u16): byte_writer {
val count := this.offsets.length;
val marks := this.index_offsets();
var made := byte_writer::new();
made.u32(20040203); # The version,
made.u32(56); # the header's bytes,
made.u32(first_type); # the first index,
made.u32(first_type + count); # and the one past the last,
made.u32(this.records.length); # the records' bytes,
made.u16(hash_stream); # the stream of hashes,
made.u16(0xFFFF); # and no second one,
made.u32(4); # each hash of four bytes,
made.u32(type_buckets);
made.u32(0); # the hashes,
made.u32(4 * count);
made.u32(4 * count); # where some records begin,
made.u32(4 * marks.length);
made.u32(4 * count + 4 * marks.length); # and no adjustments.
made.u32(0);
made.put(this.records.view());
return made;
}
#! The stream holding the records' hashes, then the index and offset of the
#! first record and of each one an 8 KB mark falls within, which a reader
#! finds a record from.
#! @param this The table.
#! @returns The stream.
pub def type_table::hash_stream(&this): byte_writer {
var made := byte_writer::new();
for &each in this.hashes {
made.u32(each);
}
for &each in this.index_offsets() {
made.u32(each);
}
return made;
}
# The index and offset of each record a reader begins from.
def type_table::index_offsets(&this): vector<u32> {
var made := vector::<u32>::new();
val count := this.offsets.length;
for i in 0..count {
val begins := this.offsets[i] as u64;
val ends := i + 1 < count ? this.offsets[i + 1] as u64 : this.records.length;
if i == 0 || ends / 8192 > begins / 8192 {
made.append(first_type + i as u32);
made.append(begins as u32);
}
}
return made;
}
+550
View File
@@ -0,0 +1,550 @@
#! @file writer.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the writer of PDB files, as Microsoft's linker writes them for an
#! executable of x86-64: the stream naming the file, which the executable
#! names it by, the streams of types and of IDs, the stream of modules, each
#! with a stream of its symbols and lines, the sections each module's code is
#! in, the files each is from, and how the image's sections are laid out, then
#! the globals and publics, which a debugger finds a name by, and the table of
#! names. The file is named by a GUID made from what it holds, so the same
#! build writes the same file.
import std::containers;
import std::io;
module binary::pdb;
# The streams every PDB file has at its number, and those this one gives the
# rest at.
const stream_info: u16 = 1;
const stream_types: u16 = 2;
const stream_modules: u16 = 3;
const stream_ids: u16 = 4;
const stream_names: u16 = 5;
const stream_type_hashes: u16 = 6;
const stream_id_hashes: u16 = 7;
const stream_globals: u16 = 8;
const stream_publics: u16 = 9;
const stream_records: u16 = 10;
const stream_sections: u16 = 11;
const stream_first_module: u16 = 12;
#! How many buckets the hashes of globals and publics are taken modulo.
const name_buckets: u64 = 4096;
#! A part of a section, which a module's code or data is.
struct contribution {
var section: u16;
var offset: u32;
var size: u32;
var characteristics: u32;
var owner: u16;
}
#! A module: a part of the program compiled together, with its symbols and
#! lines.
struct module_info {
var name: strview; #! Its name.
var object: strview; #! The object it was compiled to, or empty for none.
var symbols: byte_writer; #! Its stream of symbols, which begins with its signature.
var lines: byte_writer; #! Its subsections of lines.
var files: vector<strview>; #! The paths of the files its code is from.
}
#! A name the stream of globals or of publics holds, with its record.
struct named_record {
var name: strview;
var offset: u32; #! Where its record is among the symbols' records.
var section: u16; #! For a public, where it names.
var place: u32;
}
#! A PDB file being written.
pub struct writer {
pub var types: type_table; #! The types.
pub var ids: type_table; #! The IDs, of functions inlined.
pub var names: name_table; #! The table of names, which lines name files in.
pub var age: u32 = 1; #! How many times the file was written for its executable.
pub var guid: u8[16]; #! What names the file, made from it once it is written.
pub var signature: u32; #! A word of it, as the executable's debug directory gives its time.
pub var sections: byte_writer; #! The headers of the image's sections, as the executable holds them.
var modules: vector<module_info>;
var contributions: vector<contribution>;
var records: byte_writer; #! The records of the globals and publics.
var globals: vector<named_record>;
var publics: vector<named_record>;
}
#! Makes a PDB file with nothing in it yet.
#! @returns The file.
pub def writer::new(): writer {
return writer {
types = type_table::new(),
ids = type_table::new(),
names = name_table::new(),
sections = byte_writer::new(),
modules = vector::<module_info>::new(),
contributions = vector::<contribution>::new(),
records = byte_writer::new(),
globals = vector::<named_record>::new(),
publics = vector::<named_record>::new()
};
}
#! Adds a module.
#! @param this The file.
#! @param name Its name, as the object it was compiled to is named, or
#! `* Linker *` for the linker's.
#! @param object The object it was compiled to, or empty for none.
#! @param symbols Its symbols, taken.
#! @param lines Its subsections of lines, taken.
#! @param files The paths of the files its code is from.
#! @returns Its number, from 1, as globals name it by.
pub def writer::add_module(&mut this, name: strview, object: strview, symbols: &mut byte_writer, lines: &mut byte_writer, files: span<strview>): u16 {
var paths := vector::<strview>::new();
for &each in files {
paths.append(each);
}
this.modules.append(module_info {
name = name, object = object, symbols = byte_writer::move_of(symbols), lines = byte_writer::move_of(lines), files = move(&mut paths)
});
return this.modules.length as u16;
}
#! Says a part of a section is a module's.
#! @param this The file.
#! @param owner The module, from 1.
#! @param section The section, from 1.
#! @param offset Where the part begins in it.
#! @param size Its bytes.
#! @param characteristics The section's characteristics.
pub def writer::contribute(&mut this, owner: u16, section: u16, offset: u32, size: u32, characteristics: u32): void {
this.contributions.append(contribution {
section = section, offset = offset, size = size, characteristics = characteristics, owner = owner - 1
});
}
#! Adds a public symbol, a name a debugger finds a place by.
#! @param this The file.
#! @param name The name.
#! @param section The section, from 1.
#! @param offset Where in it.
#! @param flags What it names, as `public_function | public_code`.
pub def writer::public(&mut this, name: strview, section: u16, offset: u32, flags: u32): void {
val at := this.records.length as u32;
public_record(&mut this.records, flags, section, offset, name);
this.publics.append(named_record { name = name, offset = at, section = section, place = offset });
}
#! Adds a global naming a procedure a module's stream holds.
#! @param this The file.
#! @param name Its name.
#! @param owner The module, from 1.
#! @param offset Where its record is in the module's stream.
#! @param global Whether it is seen outside its module.
pub def writer::procedure(&mut this, name: strview, owner: u16, offset: u64, global: bool): void {
val at := this.records.length as u32;
procedure_reference(&mut this.records, global, owner, offset as u32, name);
this.globals.append(named_record { name = name, offset = at });
}
# The streams.
#! Writes the file, naming it by a GUID made from what it holds, which `guid`
#! and `signature` then give.
#! @param this The file.
#! @returns The file's bytes.
pub def writer::file(&mut this): byte_writer {
var streams := vector::<byte_writer>::new();
val count := stream_first_module as u64 + this.modules.length;
for _ in 0..count {
streams.append(byte_writer::new());
}
streams[stream_info as u64] = this.info_stream();
streams[stream_types as u64] = this.types.stream(stream_type_hashes);
streams[stream_type_hashes as u64] = this.types.hash_stream();
streams[stream_ids as u64] = this.ids.stream(stream_id_hashes);
streams[stream_id_hashes as u64] = this.ids.hash_stream();
streams[stream_names as u64] = this.names.stream();
streams[stream_globals as u64] = hashed_names(&this.globals);
streams[stream_publics as u64] = this.publics_stream();
var records := byte_writer::new();
records.put(this.records.view());
streams[stream_records as u64] = move(&mut records);
var sections := byte_writer::new();
sections.put(this.sections.view());
streams[stream_sections as u64] = move(&mut sections);
for m in 0..this.modules.length {
var stream := byte_writer::new();
stream.put(this.modules[m].symbols.view());
stream.put(this.modules[m].lines.view());
stream.u32(0); # No references to globals.
streams[stream_first_module as u64 + m] = move(&mut stream);
}
streams[stream_modules as u64] = this.modules_stream();
# The GUID is the digest of the file with none, and its first word the
# signature, each written where the stream naming the file holds it.
var firsts := vector::<u64>::new();
var file := msf_file(&streams, &mut firsts);
val info := firsts[stream_info as u64];
val digest := md5_of(file.view());
for k in 0..16u {
this.guid[k] = digest[k];
file.set_byte(info + 12 + k, digest[k]);
}
this.signature = digest[0] as u32 | (digest[1] as u32 << 8) | (digest[2] as u32 << 16) | (digest[3] as u32 << 24);
file.set_u32(info + 4, this.signature);
return file;
}
#! The stream naming the file: its version, its signature, its age, and its
#! GUID, the last two of which the executable names it by, then the map of the
#! streams named, `/names` alone, then what the file has, the types of version
#! 14's linker. The signature and GUID are written once the file is.
#! @param this The file.
#! @returns The stream.
def writer::info_stream(&this): byte_writer {
var made := byte_writer::new();
made.u32(20000404);
made.u32(0);
made.u32(this.age);
made.zeros(16);
# The map of names to streams: the names, then a table of eight buckets,
# its one entry in the bucket of the name's hash, in 16 bits.
val name: strview = "/names";
made.u32(name.length + 1);
made.put_terminated(name);
val bucket := (hash_v1(name) & 0xFFFF) % 8;
made.u32(1); # One entry,
made.u32(8); # in eight buckets,
made.u32(1); # those holding one,
made.u32(1u << bucket);
made.u32(0); # none deleted.
made.u32(0); # The name's offset,
made.u32(stream_names);
made.u32(20140508); # VC140.
return made;
}
#! The stream of globals or the hash part of that of publics: a table of
#! hashes of each name, each record named by where it is among the records,
#! in buckets of its name's hash, sorted within each as Microsoft's tools sort
#! them, then which buckets hold any, and where each of those begins.
#! @param names The names.
#! @returns The table.
def hashed_names(names: &vector<named_record>): byte_writer {
val count := names.length;
var bucket_of := vector::<u64>::new();
var sizes := vector::<u64>::new();
sizes.resize(name_buckets);
for &each in names.view() {
val bucket := hash_v1(each.name) as u64 % name_buckets;
bucket_of.append(bucket);
sizes[bucket] = sizes[bucket] + 1;
}
var starts := vector::<u64>::new();
var sum: u64 = 0;
for b in 0..name_buckets {
starts.append(sum);
sum += sizes[b];
}
var cursors := vector::<u64>::new();
cursors.extend(starts.view());
var order := vector::<u64>::new();
order.resize(count);
for i in 0..count {
val b := bucket_of[i];
order[cursors[b]] = i;
cursors[b] = cursors[b] + 1;
}
# Within a bucket, shorter names first, then case set aside, then by place.
for b in 0..name_buckets {
val from := starts[b];
val to := cursors[b];
var i := from + 1;
while i < to {
var j := i;
while j > from && name_before(names, order[j], order[j - 1]) {
val swapped := order[j];
order[j] = order[j - 1];
order[j - 1] = swapped;
j--;
}
i++;
}
}
var made := byte_writer::new();
made.u32(0xFFFFFFFF); # The signature,
made.u32(0xF12F091A); # the version,
made.u32(8 * count); # the records' bytes,
val buckets_at := made.length;
made.u32(0); # and the buckets', written below.
for &each in order.view() {
made.u32(names[each].offset as u64 + 1);
made.u32(1);
}
val start := made.length;
for word in 0..(name_buckets + 32) / 32 {
var bits: u32 = 0;
for k in 0..32u {
val b := word * 32 + k;
if b < name_buckets && sizes[b] != 0 {
bits = bits | (1u << k) as u32;
}
}
made.u32(bits);
}
for b in 0..name_buckets {
if sizes[b] != 0 {
made.u32(starts[b] * 12);
}
}
made.set_u32(buckets_at, made.length - start);
return made;
}
# Whether one name comes before another in their bucket: a shorter one first,
# then letters as though of one case, then the one whose record comes first.
def name_before(names: &vector<named_record>, a: u64, b: u64): bool {
val left := names[a].name;
val right := names[b].name;
if left.length != right.length {
return left.length < right.length;
}
for i in 0..left.length {
var x := left[i];
var y := right[i];
if x >= 'A' as u8 && x <= 'Z' as u8 { x = x + 32; }
if y >= 'A' as u8 && y <= 'Z' as u8 { y = y + 32; }
if x != y {
return x < y;
}
}
return names[a].offset < names[b].offset;
}
#! The stream of publics: its header, the table of their names' hashes, then
#! where each record is, in the order of the places they name.
#! @param this The file.
#! @returns The stream.
def writer::publics_stream(&this): byte_writer {
val table := hashed_names(&this.publics);
var order := vector::<u64>::new();
for i in 0..this.publics.length {
order.append(i);
}
var i: u64 = 1;
while i < order.length {
var j := i;
while j > 0 && this.placed_before(order[j], order[j - 1]) {
val swapped := order[j];
order[j] = order[j - 1];
order[j - 1] = swapped;
j--;
}
i++;
}
var made := byte_writer::new();
made.u32(table.length);
made.u32(4 * order.length);
for _ in 0..5u {
made.u32(0); # No thunks.
}
made.put(table.view());
for &each in order.view() {
made.u32(this.publics[each].offset);
}
return made;
}
# Whether one public names a place before another's, or the same place by a
# name before the other's.
def writer::placed_before(&this, a: u64, b: u64): bool {
val left: named_record = this.publics[a];
val right: named_record = this.publics[b];
return left.section < right.section if left.section != right.section;
return left.place < right.place if left.place != right.place;
return left.name.compare(right.name) < 0;
}
#! The stream of modules: its header, each module, the parts of sections each
#! is, the map of the image's sections, the files each module's code is from,
#! a table of names of its own, empty, and the number of the stream holding
#! the image's section headers.
#! @param this The file.
#! @returns The stream.
def writer::modules_stream(&mut this): byte_writer {
# The modules.
var modules := byte_writer::new();
for m in 0..this.modules.length {
val each := &this.modules[m];
modules.u32(0);
var first := contribution { section = 0xFFFF, owner = m as u16 };
for &part in this.contributions.view() {
if part.owner == m as u16 {
first = part;
break;
}
}
put_contribution(&mut modules, &first);
modules.u16(0); # Its flags,
modules.u16(stream_first_module as u64 + m); # its stream,
modules.u32(each.symbols.length); # its symbols' bytes,
modules.u32(0); # no lines of C11,
modules.u32(each.lines.length); # its lines' bytes,
modules.u16(each.files.length); # how many files,
modules.u16(0);
modules.u32(0);
modules.u32(0);
modules.u32(0);
modules.put_terminated(each.name);
modules.put_terminated(each.object);
modules.align_to(4);
}
# The parts of sections, in order of their places.
var parts := byte_writer::new();
parts.u32(0xF12EBA2D);
var order := vector::<u64>::new();
for i in 0..this.contributions.length {
order.append(i);
}
var i: u64 = 1;
while i < order.length {
var j := i;
while j > 0 && contribution_before(&this.contributions[order[j]], &this.contributions[order[j - 1]]) {
val swapped := order[j];
order[j] = order[j - 1];
order[j - 1] = swapped;
j--;
}
i++;
}
for &each in order.view() {
put_contribution(&mut parts, &this.contributions[each]);
}
# The map of the sections, from their headers, then one of absolute places.
val headers := this.sections.view();
val section_count := headers.length / 40;
var map := byte_writer::new();
map.u16(section_count + 1);
map.u16(section_count + 1);
for k in 0..section_count {
val characteristics := this.sections.get_u32(40 * k + 36) as u64;
var flags: u64 = 0x0108; # Its address of 32 bits, a selector,
if (characteristics & 0x40000000) != 0 { flags = flags | 1; }
if (characteristics & 0x80000000) != 0 { flags = flags | 2; }
if (characteristics & 0x20000000) != 0 { flags = flags | 4; }
map.u16(flags);
map.u16(0);
map.u16(0);
map.u16(k + 1);
map.u16(0xFFFF);
map.u16(0xFFFF);
map.u32(0);
map.u32(this.sections.get_u32(40 * k + 8));
}
map.u16(0x0208);
map.u16(0);
map.u16(0);
map.u16(section_count + 1);
map.u16(0xFFFF);
map.u16(0xFFFF);
map.u32(0);
map.u32(0xFFFFFFFF);
# The files of each module, as offsets into the names after them.
var files := byte_writer::new();
var total: u64 = 0;
for &each in this.modules.view() {
total += each.files.length;
}
files.u16(this.modules.length);
files.u16(total & 0xFFFF);
var first: u64 = 0;
for &each in this.modules.view() {
files.u16(first);
first += each.files.length;
}
for &each in this.modules.view() {
files.u16(each.files.length);
}
var file_names := byte_writer::new();
var file_at := interner::<string>::new();
var file_offsets := vector::<u32>::new();
for &each in this.modules.view() {
for &path in each.files.view() {
val id := file_at.intern_view(path);
if id == file_offsets.length {
file_offsets.append(file_names.length as u32);
file_names.put_terminated(path);
}
files.u32(file_offsets[id]);
}
}
files.put(file_names.view());
files.align_to(4);
# The table of names of its own, holding none.
val empty := name_table::new();
val names := empty.stream();
var made := byte_writer::new();
made.u32(0xFFFFFFFF); # The signature,
made.u32(19990903); # the version,
made.u32(this.age);
made.u16(stream_globals);
made.u16(0x8E00 | 0x0B); # As version 14.11's linker writes it,
made.u16(stream_publics);
made.u16(0);
made.u16(stream_records);
made.u16(0);
made.u32(modules.length);
made.u32(parts.length);
made.u32(map.length);
made.u32(files.length);
made.u32(0); # No type servers,
made.u32(0);
made.u32(22); # the streams of debugging headers,
made.u32(names.length);
made.u16(0);
made.u16(image_machine_x64);
made.u32(0);
made.put(modules.view());
made.put(parts.view());
made.put(map.view());
made.put(files.view());
made.put(names.view());
# The streams of headers from the executable: only its sections'.
for k in 0..11u {
made.u16(k == 5 ? stream_sections : 0xFFFF);
}
return made;
}
# Adds a part of a section, as the stream of modules writes one.
def put_contribution(out: &mut byte_writer, part: &contribution): void {
out.u16(part.section);
out.u16(0);
out.u32(part.offset);
out.u32(part.size);
out.u32(part.characteristics);
out.u16(part.owner);
out.u16(0);
out.u32(0);
out.u32(0);
}
# Whether one part of a section comes before another.
def contribution_before(a: &contribution, b: &contribution): bool {
return a.section < b.section if a.section != b.section;
return a.offset < b.offset;
}