Initial Commit
This commit is contained in:
35 files changed
+8351
No files matched your search
@@ -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.span);
|
||||
}
|
||||
|
||||
#! 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.span);
|
||||
}
|
||||
|
||||
#! 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.span);
|
||||
}
|
||||
|
||||
#! 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.span);
|
||||
}
|
||||
|
||||
#! 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.span;
|
||||
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.span);
|
||||
}
|
||||
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.span, 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.span, 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.span, 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.span);
|
||||
}
|
||||
|
||||
#! 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.span);
|
||||
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.span);
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user