#! @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): 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; #! The strings, by ID. var at: vector; #! 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::::new(), at = vector::::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; }