Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-26 06:02:30 -05:00
commit af3d3ae402
33 files changed
+7500

No files matched your search

+142
View File
@@ -0,0 +1,142 @@
#! @file dwarf.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the numbers of DWARF 5, as its standard names them: the tags of
#! entries, their attributes and the forms those are written in, the encodings
#! of base types, the operations of expressions, the entries of lists of
#! locations and ranges, the instructions of frames and of line programs, and
#! how x86-64's registers are numbered, as its System V ABI numbers them.
import std;
module binary::dwarf;
# Tags.
pub const dw_tag_array_type: u64 = 0x01;
pub const dw_tag_enumeration_type: u64 = 0x04;
pub const dw_tag_formal_parameter: u64 = 0x05;
pub const dw_tag_lexical_block: u64 = 0x0b;
pub const dw_tag_member: u64 = 0x0d;
pub const dw_tag_pointer_type: u64 = 0x0f;
pub const dw_tag_reference_type: u64 = 0x10;
pub const dw_tag_compile_unit: u64 = 0x11;
pub const dw_tag_structure_type: u64 = 0x13;
pub const dw_tag_union_type: u64 = 0x17;
pub const dw_tag_inlined_subroutine: u64 = 0x1d;
pub const dw_tag_subrange_type: u64 = 0x21;
pub const dw_tag_base_type: u64 = 0x24;
pub const dw_tag_enumerator: u64 = 0x28;
pub const dw_tag_subprogram: u64 = 0x2e;
pub const dw_tag_variable: u64 = 0x34;
pub const dw_tag_unspecified_type: u64 = 0x3b;
# Attributes.
pub const dw_at_location: u64 = 0x02;
pub const dw_at_name: u64 = 0x03;
pub const dw_at_byte_size: u64 = 0x0b;
pub const dw_at_stmt_list: u64 = 0x10;
pub const dw_at_low_pc: u64 = 0x11;
pub const dw_at_high_pc: u64 = 0x12;
pub const dw_at_language: u64 = 0x13;
pub const dw_at_comp_dir: u64 = 0x1b;
pub const dw_at_const_value: u64 = 0x1c;
pub const dw_at_inline: u64 = 0x20;
pub const dw_at_producer: u64 = 0x25;
pub const dw_at_abstract_origin: u64 = 0x31;
pub const dw_at_calling_convention: u64 = 0x36;
pub const dw_at_count: u64 = 0x37;
pub const dw_at_data_member_location: u64 = 0x38;
pub const dw_at_decl_file: u64 = 0x3a;
pub const dw_at_decl_line: u64 = 0x3b;
pub const dw_at_encoding: u64 = 0x3e;
pub const dw_at_external: u64 = 0x3f;
pub const dw_at_frame_base: u64 = 0x40;
pub const dw_at_type: u64 = 0x49;
pub const dw_at_ranges: u64 = 0x55;
pub const dw_at_call_column: u64 = 0x57;
pub const dw_at_call_file: u64 = 0x58;
pub const dw_at_call_line: u64 = 0x59;
# Forms.
pub const dw_form_addr: u64 = 0x01;
pub const dw_form_data2: u64 = 0x05;
pub const dw_form_data8: u64 = 0x07;
pub const dw_form_data1: u64 = 0x0b;
pub const dw_form_sdata: u64 = 0x0d;
pub const dw_form_strp: u64 = 0x0e;
pub const dw_form_udata: u64 = 0x0f;
pub const dw_form_ref4: u64 = 0x13;
pub const dw_form_sec_offset: u64 = 0x17;
pub const dw_form_exprloc: u64 = 0x18;
pub const dw_form_flag_present: u64 = 0x19;
pub const dw_form_line_strp: u64 = 0x1f;
# Encodings of base types.
pub const dw_ate_boolean: u8 = 0x02;
pub const dw_ate_float: u8 = 0x04;
pub const dw_ate_signed: u8 = 0x05;
pub const dw_ate_unsigned: u8 = 0x07;
# Operations of expressions.
pub const dw_op_deref: u8 = 0x06;
pub const dw_op_constu: u8 = 0x10;
pub const dw_op_reg0: u8 = 0x50;
pub const dw_op_breg0: u8 = 0x70;
pub const dw_op_fbreg: u8 = 0x91;
pub const dw_op_piece: u8 = 0x93;
pub const dw_op_call_frame_cfa: u8 = 0x9c;
pub const dw_op_implicit_value: u8 = 0x9e;
pub const dw_op_stack_value: u8 = 0x9f;
# Entries of lists of locations.
pub const dw_lle_end_of_list: u8 = 0x00;
pub const dw_lle_start_length: u8 = 0x08;
# Instructions of frames.
pub const dw_cfa_advance_loc: u8 = 0x40;
pub const dw_cfa_offset: u8 = 0x80;
pub const dw_cfa_advance_loc1: u8 = 0x02;
pub const dw_cfa_advance_loc2: u8 = 0x03;
pub const dw_cfa_advance_loc4: u8 = 0x04;
pub const dw_cfa_same_value: u8 = 0x08;
pub const dw_cfa_remember_state: u8 = 0x0a;
pub const dw_cfa_restore_state: u8 = 0x0b;
pub const dw_cfa_def_cfa: u8 = 0x0c;
pub const dw_cfa_def_cfa_register: u8 = 0x0d;
pub const dw_cfa_def_cfa_offset: u8 = 0x0e;
# Instructions of line programs.
pub const dw_lns_copy: u8 = 0x01;
pub const dw_lns_advance_pc: u8 = 0x02;
pub const dw_lns_advance_line: u8 = 0x03;
pub const dw_lns_set_file: u8 = 0x04;
pub const dw_lns_set_column: u8 = 0x05;
pub const dw_lns_set_prologue_end: u8 = 0x0a;
pub const dw_lne_end_sequence: u8 = 0x01;
pub const dw_lne_set_address: u8 = 0x02;
# Entries of lists of ranges.
pub const dw_rle_end_of_list: u8 = 0x00;
pub const dw_rle_start_length: u8 = 0x07;
# Languages, calling conventions, inlining, and units.
pub const dw_lang_c_plus_plus_14: u64 = 0x21;
pub const dw_cc_nocall: u8 = 0x03;
pub const dw_inl_inlined: u8 = 0x01;
pub const dw_ut_compile: u8 = 0x01;
# Registers, as x86-64's System V ABI numbers them for DWARF.
#! The number of each general register, by how x86-64 encodes it: `rax`, `rcx`,
#! `rdx`, `rbx`, `rsp`, `rbp`, `rsi`, `rdi`, then `r8` to `r15`.
pub const amd64_registers: u8[16] = [0, 2, 1, 3, 7, 6, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15];
#! The number of `xmm0`, which the other SSE registers follow.
pub const amd64_xmm0: u8 = 17;
#! The numbers of `rbp` and `rsp`, and of the column of the return address.
pub const amd64_rbp: u64 = 6;
pub const amd64_rsp: u64 = 7;
pub const amd64_return_address: u64 = 16;
+126
View File
@@ -0,0 +1,126 @@
#! @file encoding.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains how DWARF writes what its sections hold: numbers in as few bytes of
#! seven bits as hold them, the abbreviations entries are read by, the tables of
#! strings entries name, and the headers units and lists begin with. Every
#! length is of 32 bits, as DWARF's 32-bit format writes them.
import std;
module binary::dwarf;
#! Adds a number in as few bytes of seven bits as hold it, the lowest first.
#! @param out Where.
#! @param value The number.
pub def uleb(out: &mut byte_writer, value: u64): void {
var left := value;
while true {
val low := (left & 0x7F) as u8;
left = left >> 7;
if left == 0 {
out.byte(low);
return;
}
out.byte(low | 0x80);
}
}
#! Adds a signed number in as few bytes of seven bits as hold it, the lowest
#! first.
#! @param out Where.
#! @param value The number.
pub def sleb(out: &mut byte_writer, value: i64): void {
var left := value;
while true {
val low := (left & 0x7F) as u8;
left = left >> 7;
val negative := (low & 0x40) != 0;
if (left == 0 && !negative) || (left == -1 && negative) {
out.byte(low);
return;
}
out.byte(low | 0x80);
}
}
#! Writes an abbreviation: its code, its tag, whether it has children, then
#! each attribute and its form, then two zeros. A table of them ends with a
#! zero after the last.
#! @param out The section.
#! @param code Its code, which entries name it by.
#! @param tag Its tag.
#! @param children Whether its entries have children.
#! @param pairs Each attribute, then its form.
pub def abbreviation(out: &mut byte_writer, code: u64, tag: u64, children: bool, pairs: span<u64>): void {
uleb(out, code);
uleb(out, tag);
out.byte(children ? 1 : 0);
for &each in pairs {
uleb(out, each);
}
out.byte(0);
out.byte(0);
}
#! A table of strings, as `.debug_str` and `.debug_line_str` are: each string
#! once, ending with a zero, where entries name it by its offset.
pub struct string_table {
pub var bytes: byte_writer; #! The table, as its section holds it.
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 no strings.
#! @returns The table.
pub def string_table::new(): string_table {
return string_table { bytes = byte_writer::new(), held = interner::<string>::new(), at = vector::<u32>::new() };
}
#! 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 string_table::offset_of(&mut this, text: strview): u32 {
# A string new to the table is given the next ID, which has no place yet.
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;
}
#! Begins a unit of `.debug_info`: its length, written once it ends, DWARF's
#! version, 5, the unit's type, the size of an address, and where its
#! abbreviations begin.
#! @param out The section.
#! @param kind The unit's type, as `dw_ut_compile`.
#! @param address_size The bytes of an address, 8 for x86-64.
#! @param abbreviations Where its abbreviations begin in `.debug_abbrev`.
#! @returns Where its length is, which `byte_writer::end_length` writes.
pub def begin_unit(out: &mut byte_writer, kind: u8, address_size: u8, abbreviations: u64): u64 {
val length := out.begin_length();
out.u16(5);
out.byte(kind);
out.byte(address_size);
out.u32(abbreviations);
return length;
}
#! Begins a table of lists of ranges or of locations, as `.debug_rnglists` and
#! `.debug_loclists` hold, with addresses of 8 bytes and no table of offsets.
#! @param out Where.
#! @returns Where its length is, which `byte_writer::end_length` writes.
pub def begin_lists(out: &mut byte_writer): u64 {
val length := out.begin_length();
out.u16(5);
out.byte(8);
out.byte(0);
out.u32(0);
return length;
}
+138
View File
@@ -0,0 +1,138 @@
#! @file frames.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the writer of how a debugger unwinds frames, as `.debug_frame`
#! holds it: the common entry each description of code begins from, the
#! description of each piece of code, and the instructions of each, which say
#! where the caller's frame and registers are from each offset on.
import std;
module binary::dwarf;
#! Begins the common entry descriptions of code begin from, as version 4 of
#! `.debug_frame` writes it, with no augmentation and addresses of 8 bytes.
#! The instructions every description begins with follow, then `end_entry`.
#! @param out The section.
#! @param code_align What offsets in the code are counted in.
#! @param data_align What offsets of registers are counted in.
#! @param return_register The column of the return address.
#! @returns Where the entry begins, which its length is written at.
pub def begin_cie(out: &mut byte_writer, code_align: u64, data_align: i64, return_register: u64): u64 {
val start := out.begin_length();
out.u32(0xFFFFFFFF);
out.byte(4); # The version,
out.byte(0); # no augmentation,
out.byte(8); # the size of an address and of a segment.
out.byte(0);
uleb(out, code_align);
sleb(out, data_align);
uleb(out, return_register);
return start;
}
#! Begins the description of the frames of the code at an address. Its
#! instructions follow, then `end_entry`.
#! @param out The section.
#! @param cie Where the common entry it begins from is in the section.
#! @param address Where the code begins.
#! @param size How many bytes it takes.
#! @returns Where the description begins, which its length is written at.
pub def begin_fde(out: &mut byte_writer, cie: u64, address: u64, size: u64): u64 {
val start := out.begin_length();
out.u32(cie);
out.u64(address);
out.u64(size);
return start;
}
#! Ends a description of frames, or the common entry, padding it to a word.
#! @param out The section.
#! @param start Where it begins.
pub def end_entry(out: &mut byte_writer, start: u64): void {
while (out.length - start) % 8 != 0 {
out.byte(0);
}
out.end_length(start);
}
#! Moves where the rules apply from, from one offset to a later one, in as few
#! bytes as the distance takes.
#! @param out Where.
#! @param at The offset, moved.
#! @param to The later one.
pub def cfa_advance(out: &mut byte_writer, at: &mut u64, to: u64): void {
return if to <= at;
val delta := to - at;
if delta < 0x40 {
out.byte(dw_cfa_advance_loc | delta as u8);
} else if delta <= 0xFF {
out.byte(dw_cfa_advance_loc1);
out.byte(delta as u8);
} else if delta <= 0xFFFF {
out.byte(dw_cfa_advance_loc2);
out.u16(delta);
} else {
out.byte(dw_cfa_advance_loc4);
out.u32(delta);
}
at = to;
}
#! The caller's frame is at an offset from a register.
#! @param out Where.
#! @param register The register.
#! @param offset The offset.
pub def cfa_def_cfa(out: &mut byte_writer, register: u64, offset: u64): void {
out.byte(dw_cfa_def_cfa);
uleb(out, register);
uleb(out, offset);
}
#! The caller's frame is at a new offset from the same register.
#! @param out Where.
#! @param offset The offset.
pub def cfa_def_cfa_offset(out: &mut byte_writer, offset: u64): void {
out.byte(dw_cfa_def_cfa_offset);
uleb(out, offset);
}
#! The caller's frame is at the same offset from a new register.
#! @param out Where.
#! @param register The register.
pub def cfa_def_cfa_register(out: &mut byte_writer, register: u64): void {
out.byte(dw_cfa_def_cfa_register);
uleb(out, register);
}
#! A register is saved below the caller's frame, at a number of the data's
#! alignment.
#! @param out Where.
#! @param register The register, below 64.
#! @param factored The number.
pub def cfa_offset(out: &mut byte_writer, register: u8, factored: u64): void {
out.byte(dw_cfa_offset | register);
uleb(out, factored);
}
#! A register holds what the caller left in it.
#! @param out Where.
#! @param register The register.
pub def cfa_same_value(out: &mut byte_writer, register: u64): void {
out.byte(dw_cfa_same_value);
uleb(out, register);
}
#! Keeps the rules as they are, for `cfa_restore_state` to put back.
#! @param out Where.
pub def cfa_remember_state(out: &mut byte_writer): void {
out.byte(dw_cfa_remember_state);
}
#! Puts back the rules `cfa_remember_state` kept.
#! @param out Where.
pub def cfa_restore_state(out: &mut byte_writer): void {
out.byte(dw_cfa_restore_state);
}
+168
View File
@@ -0,0 +1,168 @@
#! @file lines.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the writer of line tables: a line program, a sequence of rows each
#! naming where in the source the code from an offset is from, written as the
#! changes from one row to the next, and the header naming the directories and
#! files the rows number.
import std;
module binary::dwarf;
#! A line program being written, a sequence at a time.
pub struct line_program {
pub var bytes: byte_writer; #! The program.
var file: u64; #! The file of the last row, numbered from 1.
var line: i64; #! Its line.
var column: u64; #! Its column.
var at: u64; #! Its offset from where the sequence begins.
}
#! Makes a program with no sequences.
#! @returns The program.
pub def line_program::new(): line_program {
return line_program { bytes = byte_writer::new(), file = 1, line = 1 };
}
#! Begins a sequence, of the code at an address: its rows follow, then `end`.
#! @param this The program.
#! @param address Where the code begins.
pub def line_program::begin(&mut this, address: u64): void {
this.bytes.byte(0);
uleb(&mut this.bytes, 9);
this.bytes.byte(dw_lne_set_address);
this.bytes.u64(address);
this.file = 1;
this.line = 1;
this.column = 0;
this.at = 0;
}
#! Adds a row: the code from an offset is from a place in a file, and where the
#! function's prologue ends, where it is the first past it.
#! @param this The program.
#! @param offset Where the code is, from where the sequence begins.
#! @param file The file, numbered from 1.
#! @param line Its line, from 1.
#! @param column Its column, from 1, or 0 for none.
#! @param prologue_end Whether the row is the first past the prologue.
pub def line_program::row(&mut this, offset: u64, file: u64, line: u64, column: u64, prologue_end: bool): void {
if file != this.file {
this.bytes.byte(dw_lns_set_file);
uleb(&mut this.bytes, file);
this.file = file;
}
if column != this.column {
this.bytes.byte(dw_lns_set_column);
uleb(&mut this.bytes, column);
this.column = column;
}
if line as i64 != this.line {
this.bytes.byte(dw_lns_advance_line);
sleb(&mut this.bytes, line as i64 - this.line);
this.line = line as i64;
}
if offset > this.at {
this.bytes.byte(dw_lns_advance_pc);
uleb(&mut this.bytes, offset - this.at);
this.at = offset;
}
if prologue_end {
this.bytes.byte(dw_lns_set_prologue_end);
}
this.bytes.byte(dw_lns_copy);
}
#! Ends a sequence, where its code ends.
#! @param this The program.
#! @param size The bytes of the code.
pub def line_program::end(&mut this, size: u64): void {
if size > this.at {
this.bytes.byte(dw_lns_advance_pc);
uleb(&mut this.bytes, size - this.at);
}
this.bytes.byte(0);
uleb(&mut this.bytes, 1);
this.bytes.byte(dw_lne_end_sequence);
}
#! Writes a line table: its header, naming the directories and the files, then
#! the program. The directories are the one the build was in, which relative
#! paths are within, then the folder of each absolute path, which names its
#! file by its last part. The file numbered 0 is the one numbered 1, as DWARF 5
#! numbers the unit's main file both ways. Each name is in the line table's
#! strings.
#! @param out The section.
#! @param directory The directory the build was in.
#! @param paths The path of each file, as the program numbers them from 1.
#! @param strings The table of the line table's strings, `.debug_line_str`.
#! @param program The program.
pub def write_line_table(out: &mut byte_writer, directory: strview, paths: span<strview>, strings: &mut string_table, program: span<u8>): void {
val unit := out.begin_length();
out.u16(5);
out.byte(8);
out.byte(0);
val length := out.begin_length();
out.byte(1); # The least bytes an instruction takes,
out.byte(1); # the operations of one,
out.byte(1); # and every row begins a statement.
out.byte(0xFB); # The least line advanced by a special opcode, -5,
out.byte(14); # how many lines they advance,
out.byte(13); # and the first of them.
val lengths: u8[12] = [0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1];
out.put(lengths[:]);
var directories := vector::<strview>::new();
var held_in := vector::<u64>::new();
directories.append(directory);
for &text in paths {
held_in.append(0);
continue if text.length == 0 || text[0] != '/' as u8;
var slash := text.length - 1;
while text[slash] != '/' as u8 {
slash--;
}
val parent := text.substr(0, slash > 0 ? slash : 1);
var d: u64 = 1;
while d < directories.length && directories[d] != parent {
d++;
}
if d == directories.length {
directories.append(parent);
}
held_in[held_in.length - 1] = d;
}
out.byte(1);
uleb(out, 1);
uleb(out, dw_form_line_strp);
uleb(out, directories.length);
for &each in directories {
out.u32(strings.offset_of(each) as u64);
}
out.byte(2);
uleb(out, 1);
uleb(out, dw_form_line_strp);
uleb(out, 2);
uleb(out, dw_form_udata);
uleb(out, paths.length + 1);
for i in 0..paths.length + 1 {
val k := i == 0 ? 0 : i - 1;
val text := paths[k];
var name := text;
val d := held_in[k];
if d != 0 {
val skip := directories[d].length + (directories[d].length > 1 ? 1 : 0);
name = text.substr(skip, text.length - skip);
}
out.u32(strings.offset_of(name) as u64);
uleb(out, d);
}
out.end_length(length);
out.put(program);
out.end_length(unit);
}