Files

338 lines
12 KiB
Plaintext
Raw Permalink Normal View History

2026-10-26 06:02:30 -05:00
#! @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);
}