#! @file dwarf.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the writer of debugging information, as DWARF 5: the line table of #! the code, its functions and the calls inlined into them, how a debugger #! unwinds their frames, and the variables of code built with its locals kept #! in cells, with their types. It goes to a file of its own beside the #! executable, OUTPUT.petal, which describes the executable's segments without #! holding them. The executable names that file, with its checksum, and both #! hold the note naming the build, which debuggers match the two by. #! #! Every function makes its frame of `rbp` as it begins, saving the registers #! a call keeps that it uses just below it, and leaves it with `leave` just #! before each `ret`, so a debugger unwinds each the same way, and a variable is #! where it is from the frame's canonical address. import std::io; import sakura::compiler; import binary::amd64; import binary::dwarf; import binary::elf; module sakura::native; # Where every function's code has made its frame: past `push rbp`, then past # `mov rbp, rsp`. const framed_at: u64 = 1; const based_at: u64 = 4; # The kinds of entries, each with the attributes it has, in order. const ab_unit: u64 = 1; const ab_subprogram: u64 = 2; const ab_abstract: u64 = 3; const ab_concrete: u64 = 4; const ab_inlined: u64 = 5; const ab_scattered: u64 = 6; const ab_parameter: u64 = 7; const ab_variable: u64 = 8; const ab_block: u64 = 9; const ab_base: u64 = 10; const ab_struct: u64 = 11; const ab_member: u64 = 12; const ab_anonymous: u64 = 13; const ab_pointer: u64 = 14; const ab_address: u64 = 15; const ab_reference: u64 = 16; const ab_array: u64 = 17; const ab_subrange: u64 = 18; const ab_enum: u64 = 19; const ab_enumerator: u64 = 20; const ab_union: u64 = 21; const ab_unspecified: u64 = 22; const ab_listed_parameter: u64 = 23; #! A parameter held in values, where its location list says. const ab_listed_variable: u64 = 24; #! A variable held in values, where its location list says. #! What debugging information needs of each function's code, noted as the code #! is selected. pub struct debugging { var bodies: vector; #! Where each function's prologue ends, within its code. var returns: vector; #! Past each `leave`, within its function's code, each function's after the one before. var return_bytes: vector; #! The bytes of the instruction after each `leave`, which leaves the function. var return_first: vector; #! Where each function's are in `returns`. var saves: vector; #! The registers each function saves below its frame pointer, one bit each by number, in the order `kept_gprs` lists them from the highest. var saved_at: vector; #! Where each function's code has saved them. var frames: vector; #! The bytes of each function's frame below its frame pointer, those saved among them. var variables: vector; #! The variables of each function, each function's after the one before. var variable_first: vector; #! Where each function's are in `variables`. var ranges: vector; #! The ranges of the variables held in values. } #! A variable of a function's code: where its frame holds it, or, where it is #! held in values, the ranges of code it is somewhere in. pub struct placed_variable { var name: u32; #! The ID of its name. var at: u64; #! Where it is declared: one more than the file, then the byte. var layout: u32; #! Its layout. var offset: i64; #! Where its memory begins, from the frame's canonical address. var param: bool; #! Whether it is a parameter. var boxed: bool; #! Whether its memory, or its value, holds the address of the object of the heap holding it. var held: bool; #! Whether it is held in values, where its ranges say. var narrow: bool; #! Of an `f32` held in values, whether it is described as one, as most of its values are held, rather than as the `f64` they are held as. var site: u32; #! One more than the inline site it was declared in, or 0. var from: u32; #! Where the code its scope covers begins, within the function's code, var to: u32; #! and where it ends. var ranges: u32; #! Of one held in values, its first range in the ranges of debugging information, var count: u32; #! and how many. } #! Where a variable held in values is over a range of code: each of its words. pub struct located_range { var from: u32; #! Where the range begins, within the function's code, var to: u32; #! and ends. var kinds: u8[3]; #! Where each word is, as a `word_spot`. var spots: u64[3]; #! The register, the offset from the frame's canonical address, or the bits. } #! Where a word of a variable is. pub enum word_spot : u8 { nowhere, #! Nowhere known. gpr, #! In a general register. xmm, #! In an SSE register. frame, #! In the frame, at an offset from its canonical address. word, #! A word known. real, #! A real known, by its bits. } #! Makes what debugging information needs, with nothing noted yet. #! @returns What it needs. pub def debugging::new(): debugging { return debugging { bodies = vector::::new(), returns = vector::::new(), return_bytes = vector::::new(), return_first = vector::::new(), saves = vector::::new(), saved_at = vector::::new(), frames = vector::::new(), variables = vector::::new(), variable_first = vector::::new(), ranges = vector::::new() }; } #! Makes room for what debugging information needs of each function. #! @param this What it needs. #! @param count How many functions there are. pub def debugging::resize(&mut this, count: u64): void { this.bodies.resize(count); this.return_first.resize(count + 1); this.saves.resize(count); this.saved_at.resize(count); this.frames.resize(count); this.variable_first.resize(count + 1); } #! Writes the abbreviations: of each kind of entry, its tag, whether it has #! children, and each attribute it has with its form, in order. #! @param out The section. def abbreviations(out: &mut byte_writer): void { val unit_pairs: u64[14] = [ dw_at_producer, dw_form_strp, dw_at_language, dw_form_data2, dw_at_name, dw_form_line_strp, dw_at_comp_dir, dw_form_line_strp, dw_at_low_pc, dw_form_addr, dw_at_high_pc, dw_form_data8, dw_at_stmt_list, dw_form_sec_offset ]; abbreviation(out, ab_unit, dw_tag_compile_unit, true, unit_pairs[:]); val subprogram_pairs: u64[16] = [ dw_at_name, dw_form_strp, dw_at_decl_file, dw_form_udata, dw_at_decl_line, dw_form_udata, dw_at_external, dw_form_flag_present, dw_at_calling_convention, dw_form_data1, dw_at_low_pc, dw_form_addr, dw_at_high_pc, dw_form_data8, dw_at_frame_base, dw_form_exprloc ]; abbreviation(out, ab_subprogram, dw_tag_subprogram, true, subprogram_pairs[:]); val abstract_pairs: u64[12] = [ dw_at_name, dw_form_strp, dw_at_decl_file, dw_form_udata, dw_at_decl_line, dw_form_udata, dw_at_external, dw_form_flag_present, dw_at_calling_convention, dw_form_data1, dw_at_inline, dw_form_data1 ]; abbreviation(out, ab_abstract, dw_tag_subprogram, false, abstract_pairs[:]); val concrete_pairs: u64[8] = [ dw_at_abstract_origin, dw_form_ref4, dw_at_low_pc, dw_form_addr, dw_at_high_pc, dw_form_data8, dw_at_frame_base, dw_form_exprloc ]; abbreviation(out, ab_concrete, dw_tag_subprogram, true, concrete_pairs[:]); val inlined_pairs: u64[12] = [ dw_at_abstract_origin, dw_form_ref4, dw_at_low_pc, dw_form_addr, dw_at_high_pc, dw_form_data8, dw_at_call_file, dw_form_udata, dw_at_call_line, dw_form_udata, dw_at_call_column, dw_form_udata ]; abbreviation(out, ab_inlined, dw_tag_inlined_subroutine, true, inlined_pairs[:]); val scattered_pairs: u64[10] = [ dw_at_abstract_origin, dw_form_ref4, dw_at_ranges, dw_form_sec_offset, dw_at_call_file, dw_form_udata, dw_at_call_line, dw_form_udata, dw_at_call_column, dw_form_udata ]; abbreviation(out, ab_scattered, dw_tag_inlined_subroutine, true, scattered_pairs[:]); val local_pairs: u64[10] = [ dw_at_name, dw_form_strp, dw_at_decl_file, dw_form_udata, dw_at_decl_line, dw_form_udata, dw_at_type, dw_form_ref4, dw_at_location, dw_form_exprloc ]; abbreviation(out, ab_parameter, dw_tag_formal_parameter, false, local_pairs[:]); abbreviation(out, ab_variable, dw_tag_variable, false, local_pairs[:]); val block_pairs: u64[4] = [ dw_at_low_pc, dw_form_addr, dw_at_high_pc, dw_form_data8 ]; abbreviation(out, ab_block, dw_tag_lexical_block, true, block_pairs[:]); val base_pairs: u64[6] = [ dw_at_name, dw_form_strp, dw_at_encoding, dw_form_data1, dw_at_byte_size, dw_form_udata ]; abbreviation(out, ab_base, dw_tag_base_type, false, base_pairs[:]); val named_pairs: u64[4] = [ dw_at_name, dw_form_strp, dw_at_byte_size, dw_form_udata ]; abbreviation(out, ab_struct, dw_tag_structure_type, true, named_pairs[:]); val member_pairs: u64[6] = [ dw_at_name, dw_form_strp, dw_at_type, dw_form_ref4, dw_at_data_member_location, dw_form_udata ]; abbreviation(out, ab_member, dw_tag_member, false, member_pairs[:]); val anonymous_pairs: u64[4] = [ dw_at_type, dw_form_ref4, dw_at_data_member_location, dw_form_udata ]; abbreviation(out, ab_anonymous, dw_tag_member, false, anonymous_pairs[:]); val pointer_pairs: u64[4] = [ dw_at_type, dw_form_ref4, dw_at_byte_size, dw_form_udata ]; abbreviation(out, ab_pointer, dw_tag_pointer_type, false, pointer_pairs[:]); val sized_pairs: u64[2] = [ dw_at_byte_size, dw_form_udata ]; abbreviation(out, ab_address, dw_tag_pointer_type, false, sized_pairs[:]); abbreviation(out, ab_reference, dw_tag_reference_type, false, pointer_pairs[:]); val array_pairs: u64[2] = [ dw_at_type, dw_form_ref4 ]; abbreviation(out, ab_array, dw_tag_array_type, true, array_pairs[:]); val subrange_pairs: u64[2] = [ dw_at_count, dw_form_udata ]; abbreviation(out, ab_subrange, dw_tag_subrange_type, false, subrange_pairs[:]); abbreviation(out, ab_enum, dw_tag_enumeration_type, true, named_pairs[:]); val enumerator_pairs: u64[4] = [ dw_at_name, dw_form_strp, dw_at_const_value, dw_form_sdata ]; abbreviation(out, ab_enumerator, dw_tag_enumerator, false, enumerator_pairs[:]); abbreviation(out, ab_union, dw_tag_union_type, true, sized_pairs[:]); val unspecified_pairs: u64[2] = [ dw_at_name, dw_form_strp ]; abbreviation(out, ab_unspecified, dw_tag_unspecified_type, false, unspecified_pairs[:]); val listed_pairs: u64[10] = [ dw_at_name, dw_form_strp, dw_at_decl_file, dw_form_udata, dw_at_decl_line, dw_form_udata, dw_at_type, dw_form_ref4, dw_at_location, dw_form_sec_offset ]; abbreviation(out, ab_listed_parameter, dw_tag_formal_parameter, false, listed_pairs[:]); abbreviation(out, ab_listed_variable, dw_tag_variable, false, listed_pairs[:]); out.byte(0); } # The writer. #! What writing debugging information keeps. struct dwarf_writer { var b: &building; var info: byte_writer; var abbrev: byte_writer; var lines: byte_writer; var program: line_program; #! The line program, which follows the table of the files it names. var strings: string_table; var line_strings: string_table; var frame: byte_writer; var ranges: byte_writer; var locations: byte_writer; #! The location lists of the variables held in values. var files: vector; #! The source of each file numbered, from 1. var numbers: vector; #! The number of each source, or 0. var types: vector; #! Where the entry of each type is in `info`, by key, or 0. var queued: vector; #! Whether the entry of each type is to be written. var queue: vector; #! The types whose entries are to be written, by key. var fixups: fixup_list; #! Where each reference to a type written before the type is in `info`, and the type's key. var abstracts: vector; #! Where the abstract entry of each function inlined is in `info`, or 0. } #! The key of the type of a word, which no layout is, and that of an address #! of nothing the program says: one past the layouts, and two past. #! @param this The writer. #! @returns The key. def dwarf_writer::word_key(&this): u64 { return this.b.c.program.layouts.length; } #! @copydoc dwarf_writer::word_key def dwarf_writer::address_key(&this): u64 { return this.b.c.program.layouts.length + 1; } #! The key of the type of an `f32` held as a real, as optimized code holds one #! in a register or a slot: the eight bytes of a double. #! @param this The writer. #! @returns The key. def dwarf_writer::widened_key(&this): u64 { return this.b.c.program.layouts.length + 2; } #! Where a string is in a table of strings, adding it once. #! @param this The writer. #! @param line Whether the table is that of the line table's strings. #! @param text The string. #! @returns Where. def dwarf_writer::string_of(&mut this, line: bool, text: strview): u32 { return line ? this.line_strings.offset_of(text) : this.strings.offset_of(text); } #! Writes a reference to a string. #! @param this The writer. #! @param text The string. def dwarf_writer::name(&mut this, text: strview): void { val at := this.string_of(false, text); this.info.u32(at as u64); } #! The number of a source among the files the line table names, from 1. #! @param this The writer. #! @param source The source. #! @returns Its number. def dwarf_writer::file(&mut this, source: u32): u64 { if this.numbers[source] == 0 { this.files.append(source); this.numbers[source] = this.files.length as u32; } return this.numbers[source] as u64; } #! Writes where something is declared: its file's number, then its line. #! @param this The writer. #! @param source The file. #! @param byte Where in it. def dwarf_writer::declared(&mut this, source: u32, byte: u32): void { val number := this.file(source); val line := this.b.c.checker.units[source].source.lines.place_of(byte).line; uleb(&mut this.info, number); uleb(&mut this.info, line as u64); } # Types. #! Writes a reference to the entry of a type, which is written later where it #! is not yet. #! @param this The writer. #! @param key The type's key: its layout, or one past the layouts or two. def dwarf_writer::type_ref(&mut this, key: u64): void { if this.types[key] != 0 { this.info.u32(this.types[key] as u64); return; } this.fixups.add(this.info.length, key as u32); this.info.u32(0); return if this.queued[key]; this.queued[key] = true; this.queue.append(key as u32); } #! Writes the name of a layout's type, as problems write it. #! @param this The writer. #! @param shape The layout. def dwarf_writer::type_name(&mut this, shape: &layout): void { if shape.type_id != 0 { val text := this.b.c.checker.describe(shape.type_id); if text.length != 0 { this.name(text.view()); return; } } if shape.name != 0 { this.name(this.b.c.checker.names.text(shape.name)); return; } this.name("?"); } #! Writes a member of a struct. #! @param this The writer. #! @param name Its name. #! @param key The key of its type. #! @param offset Where it begins. def dwarf_writer::member(&mut this, name: strview, key: u64, offset: u64): void { uleb(&mut this.info, ab_member); this.name(name); this.type_ref(key); uleb(&mut this.info, offset); } #! Writes the members of a struct or union: each field but those holding #! nothing, at its offset less one. #! @param this The writer. #! @param shape The layout. #! @param less What each offset is less. def dwarf_writer::fields(&mut this, shape: &layout, less: u64): void { val program := &this.b.c.program; for f in 0..shape.count { val field: slot_field = program.fields[shape.fields as u64 + f as u64]; continue if field.layout == never || program.layouts[field.layout].kind == lk::empty; this.member(this.b.c.checker.names.text(field.name), field.layout as u64, field.offset - less); } } #! Writes the entry of a type. #! @param this The writer. #! @param key The type's key. def dwarf_writer::write_type(&mut this, key: u64): void { this.types[key] = this.info.length as u32; if key == this.word_key() { uleb(&mut this.info, ab_base); this.name("u64"); this.info.byte(dw_ate_unsigned); uleb(&mut this.info, 8); return; } if key == this.address_key() { uleb(&mut this.info, ab_address); uleb(&mut this.info, 8); return; } if key == this.widened_key() { uleb(&mut this.info, ab_base); this.name("f32"); this.info.byte(dw_ate_float); uleb(&mut this.info, 8); return; } val shape: layout = this.b.c.program.layouts[key]; match shape.kind { # An enum is laid out as its integer, with a field for each member whose # offset is the member's value. lk::signed, lk::unsigned => { if shape.name != 0 && shape.count != 0 { uleb(&mut this.info, ab_enum); this.type_name(&shape); uleb(&mut this.info, shape.size); for f in 0..shape.count { val field: slot_field = this.b.c.program.fields[shape.fields as u64 + f as u64]; uleb(&mut this.info, ab_enumerator); this.name(this.b.c.checker.names.text(field.name)); sleb(&mut this.info, field.offset as i64); } this.info.byte(0); return; } uleb(&mut this.info, ab_base); this.type_name(&shape); this.info.byte(shape.kind == lk::signed ? dw_ate_signed : dw_ate_unsigned); uleb(&mut this.info, shape.size); } lk::floating, lk::boolean => { uleb(&mut this.info, ab_base); this.type_name(&shape); this.info.byte(shape.kind == lk::floating ? dw_ate_float : dw_ate_boolean); uleb(&mut this.info, shape.size); } # A reference is the address of what it refers to. lk::pointer, lk::reference => { if shape.element == 0 || this.b.c.program.layouts[shape.element - 1].kind == lk::empty { uleb(&mut this.info, ab_address); uleb(&mut this.info, 8); return; } uleb(&mut this.info, shape.kind == lk::pointer ? ab_pointer : ab_reference); this.type_ref(shape.element as u64 - 1); uleb(&mut this.info, 8); } # A reference to a trait is a reference, then the index of the vtable. lk::trait_reference => { uleb(&mut this.info, ab_struct); this.type_name(&shape); uleb(&mut this.info, shape.size); this.member("address", this.address_key(), 0); this.member("vtable", this.word_key(), 8); this.info.byte(0); } # A function value is its function's index plus one, then its environment. lk::function => { uleb(&mut this.info, ab_struct); this.type_name(&shape); uleb(&mut this.info, shape.size); this.member("function", this.word_key(), 0); this.member("environment", this.address_key(), 8); this.info.byte(0); } lk::fixed => { uleb(&mut this.info, ab_array); this.type_ref(shape.element as u64 - 1); uleb(&mut this.info, ab_subrange); uleb(&mut this.info, shape.count as u64); this.info.byte(0); } lk::record => { uleb(&mut this.info, ab_struct); this.type_name(&shape); uleb(&mut this.info, shape.size); this.fields(&shape, 0); this.info.byte(0); } # A union laid out without a tag is its fields over one another, the empty one # held where the first word is null. lk::variant => { if shape.niche != 0 { uleb(&mut this.info, ab_struct); this.type_name(&shape); uleb(&mut this.info, shape.size); this.fields(&shape, 0); this.info.byte(0); return; } uleb(&mut this.info, ab_struct); this.type_name(&shape); uleb(&mut this.info, shape.size); val tag := this.info.length; uleb(&mut this.info, ab_enum); this.name("tag"); uleb(&mut this.info, 8); for f in 0..shape.count { val field: slot_field = this.b.c.program.fields[shape.fields as u64 + f as u64]; uleb(&mut this.info, ab_enumerator); this.name(this.b.c.checker.names.text(field.name)); sleb(&mut this.info, f as i64); } this.info.byte(0); uleb(&mut this.info, ab_member); this.name("tag"); this.info.u32(tag); uleb(&mut this.info, 0); val payload := this.info.length; uleb(&mut this.info, ab_union); uleb(&mut this.info, shape.size > 8 ? shape.size - 8 : 0); this.fields(&shape, 8); this.info.byte(0); uleb(&mut this.info, ab_anonymous); this.info.u32(payload); uleb(&mut this.info, 8); this.info.byte(0); } else => { uleb(&mut this.info, ab_unspecified); this.type_name(&shape); } } } # Functions. #! Whether code of an inline site is within another, or is that site. #! @param sites The function's inline sites. #! @param site One more than the site, or 0 for the function's own code. #! @param outer One more than the other. #! @returns Whether it is. def within(sites: &vector, site: u32, outer: u32): bool { var at := site; var depth: u64 = 0; while at != 0 && depth <= sites.length { return true if at == outer; at = sites[at as u64 - 1].parent; depth++; } return false; } #! Writes the sites inlined within a site of a function, or within its own #! code, each with the code of the rows within it, as one range or several. #! @param this The writer. #! @param index The function. #! @param parent One more than the site, or 0. def dwarf_writer::sites(&mut this, index: u32, parent: u32): void { val sites := &this.b.c.program.functions[index].body.sites; val first := this.b.row_first[index] as u64; val last := this.b.row_first[index + 1] as u64; val address := this.b.text_base + this.b.starts[index]; val size := this.b.ends[index] - this.b.starts[index]; for k in 0..sites.length { val site: inline_site = sites[k]; continue if site.parent != parent; var pieces: u64 = 0; var low: u64 = 0; var high: u64 = 0; val list := this.ranges.length; var r := first; while r < last { if !within(sites, this.b.rows[r].site, k as u32 + 1) { r++; continue; } val from := this.b.rows[r].offset; var next := r + 1; while next < last && within(sites, this.b.rows[next].site, k as u32 + 1) { next++; } val to := next < last ? this.b.rows[next].offset : size; r = next; continue if to <= from; if pieces == 0 { low = from; } high = to; this.ranges.byte(dw_rle_start_length); this.ranges.u64(address + from); uleb(&mut this.ranges, to - from); pieces++; } this.ranges.byte(dw_rle_end_of_list); continue if pieces == 0 || this.abstracts[site.function] == 0; uleb(&mut this.info, pieces == 1 ? ab_inlined : ab_scattered); this.info.u32(this.abstracts[site.function] as u64); if pieces == 1 { this.info.u64(address + low); this.info.u64(high - low); } else { this.info.u32(list); } var source: u32 = 0; val located := this.b.source_of(index, site.at, &mut source); uleb(&mut this.info, this.file(source)); uleb(&mut this.info, located.line as u64); uleb(&mut this.info, located.column as u64); this.site_variables(index, k as u32 + 1); this.sites(index, k as u32 + 1); this.info.byte(0); } } #! Writes the variables of the function a site inlined. #! @param this The writer. #! @param index The function it was inlined into. #! @param site One more than the site. def dwarf_writer::site_variables(&mut this, index: u32, site: u32): void { val debug := &this.b.debug; for v in debug.variable_first[index]..debug.variable_first[index + 1] { val each: placed_variable = debug.variables[v as u64]; continue if each.site != site || this.b.c.program.layouts[each.layout].kind == lk::empty; if each.held { this.listed_variable(index, &each); } else { this.variable(&each); } } } #! Writes a variable: its name, where it is declared, its type, and where its #! frame holds it. #! @param this The writer. #! @param each The variable. def dwarf_writer::variable(&mut this, each: &placed_variable): void { uleb(&mut this.info, each.param ? ab_parameter : ab_variable); this.name(this.b.c.checker.names.text(each.name)); this.declared(pair_high(each.at) - 1, pair_low(each.at)); this.type_ref(each.layout as u64); var expression := byte_writer::new(); expression.byte(dw_op_fbreg); sleb(&mut expression, each.offset); if each.boxed { expression.byte(dw_op_deref); } uleb(&mut this.info, expression.length); this.info.put(expression.view()); } #! Writes where a word of a variable held in values is, over a range, as an #! expression: a register, the frame, or a value known. One of an object of the #! heap is that object, at the address the word is. #! @param out Where. #! @param kind Where the word is, as a `word_spot`. #! @param spot The register, the offset from the frame's canonical address, or the bits. #! @param boxed Whether the word is the address of the object of the heap the variable is in. def word_location(out: &mut byte_writer, kind: u8, spot: u64, boxed: bool): void { if kind == word_spot::gpr as u8 { val number := amd64_registers[spot & 15]; if boxed { out.byte(dw_op_breg0 + number); sleb(out, 0); } else { out.byte(dw_op_reg0 + number); } } else if kind == word_spot::xmm as u8 { out.byte(dw_op_reg0 + amd64_xmm0 + (spot & 15) as u8); } else if kind == word_spot::frame as u8 { out.byte(dw_op_fbreg); sleb(out, spot as i64); if boxed { out.byte(dw_op_deref); } } else if kind == word_spot::word as u8 { out.byte(dw_op_constu); uleb(out, spot); if !boxed { out.byte(dw_op_stack_value); } } else if kind == word_spot::real as u8 { out.byte(dw_op_implicit_value); uleb(out, 8); out.u64(spot); } } #! Writes a variable held in values: its name, where it is declared, its type, #! and the list of where it is over each range of code it is somewhere in. #! @param this The writer. #! @param index The function. #! @param each The variable. def dwarf_writer::listed_variable(&mut this, index: u32, each: &placed_variable): void { uleb(&mut this.info, each.param ? ab_listed_parameter : ab_listed_variable); this.name(this.b.c.checker.names.text(each.name)); this.declared(pair_high(each.at) - 1, pair_low(each.at)); val shape: layout = this.b.c.program.layouts[each.layout]; val widened := !each.boxed && !each.narrow && shape.kind == lk::floating && shape.size == 4; this.type_ref(widened ? this.widened_key() : each.layout as u64); this.info.u32(this.locations.length); # Each word of a variable of several is a piece of it. val words := each.boxed ? 1 : word_count(&shape); val address := this.b.text_base + this.b.starts[index]; var expression := byte_writer::new(); for r in each.ranges..each.ranges + each.count { val range: located_range = this.b.debug.ranges[r as u64]; expression.clear(); for w in 0..words { word_location(&mut expression, range.kinds[w], range.spots[w], each.boxed); if words > 1 { expression.byte(dw_op_piece); uleb(&mut expression, 8); } } continue if expression.length == 0; this.locations.byte(dw_lle_start_length); this.locations.u64(address + range.from as u64); uleb(&mut this.locations, (range.to - range.from) as u64); uleb(&mut this.locations, expression.length); this.locations.put(expression.view()); } this.locations.byte(dw_lle_end_of_list); } #! Whether one variable's scope comes before another's: it begins first, or #! ends later where they begin together, so a scope comes before those within #! it. #! @param a The one. #! @param b The other. #! @returns Whether it does. def scope_before(a: &placed_variable, b: &placed_variable): bool { return a.from < b.from || (a.from == b.from && a.to > b.to); } #! Writes the variables of a function: its parameters, then its locals, each in #! a block of the code its scope covers. Scopes nest, so the blocks do. #! @param this The writer. #! @param index The function. def dwarf_writer::variables(&mut this, index: u32): void { val debug := &this.b.debug; val first := debug.variable_first[index] as u64; val last := debug.variable_first[index + 1] as u64; var locals := vector::::new(); for v in first..last { val each: placed_variable = debug.variables[v]; continue if this.b.c.program.layouts[each.layout].kind == lk::empty || each.site != 0; if each.held { this.listed_variable(index, &each); continue; } if each.param { this.variable(&each); continue; } continue if each.to <= each.from; locals.append(each); } # Put in order of their scopes, those of one scope in the order they came. locals.sort_by(scope_before); # The blocks open, the innermost last. val address := this.b.text_base + this.b.starts[index]; var open := vector::::new(); for &each in locals { while open.length != 0 && (each.from < open.last().from || each.to > open.last().to) { this.info.byte(0); open.truncate(); } if open.length == 0 || each.from != open.last().from || each.to != open.last().to { uleb(&mut this.info, ab_block); this.info.u64(address + each.from as u64); this.info.u64((each.to - each.from) as u64); open.append(each); } this.variable(&each); } for _ in 0..open.length { this.info.byte(0); } } #! Writes the entry of a function's code: its name, or the abstract entry of #! it where it was inlined somewhere too, then its variables and the calls #! inlined into it. #! @param this The writer. #! @param index The function. def dwarf_writer::function(&mut this, index: u32): void { val function := &this.b.c.program.functions[index]; if this.abstracts[index] != 0 { uleb(&mut this.info, ab_concrete); this.info.u32(this.abstracts[index] as u64); } else { uleb(&mut this.info, ab_subprogram); this.name(function.name.view()); this.declared(function.file, function.offset); this.info.byte(dw_cc_nocall); } this.info.u64(this.b.text_base + this.b.starts[index]); this.info.u64(this.b.ends[index] - this.b.starts[index]); uleb(&mut this.info, 1); this.info.byte(dw_op_call_frame_cfa); this.variables(index); this.sites(index, 0); this.info.byte(0); } # Lines. #! Writes the line program of a function's code, as a sequence of its own. #! @param this The writer. #! @param index The function. def dwarf_writer::line_program(&mut this, index: u32): void { val size := this.b.ends[index] - this.b.starts[index]; val body := this.b.debug.bodies[index] as u64; this.program.begin(this.b.text_base + this.b.starts[index]); var begun := false; for r in this.b.row_first[index]..this.b.row_first[index + 1] { val row: line_row = this.b.rows[r as u64]; var source: u32 = 0; val located := this.b.source_of(index, row.at, &mut source); val number := this.file(source); val prologue_end := !begun && body != 0 && row.offset >= body; begun = begun || prologue_end; this.program.row(row.offset, number, located.line as u64, located.column as u64, prologue_end); } this.program.end(size); } #! Writes the line table: its header, naming the directories and the files, #! then the program the functions wrote. #! @param this The writer. #! @param directory The directory the build was in, which relative paths are #! within. def dwarf_writer::line_table(&mut this, directory: strview): void { var paths := vector::::new(); for &each in this.files { paths.append(this.b.c.checker.units[each].source.path); } write_line_table(&mut this.lines, directory, paths.view(), &mut this.line_strings, this.program.bytes.view()); } # Frames. #! Writes the rules of code that has made a frame of `rbp` by an offset: the #! caller's stack is past `rbp`'s saved value and the return address, then #! where `rbp` points. #! @param out Where. #! @param at The offset the rules apply from, moved. #! @param framed Where `rbp` is pushed. #! @param based Where the frame is made. def frame_rules(out: &mut byte_writer, at: &mut u64, framed: u64, based: u64): void { cfa_advance(out, at, framed); cfa_def_cfa_offset(out, 16); cfa_offset(out, amd64_rbp as u8, 2); cfa_advance(out, at, based); cfa_def_cfa_register(out, amd64_rbp); } #! Writes the common entry each description of frames begins from, as a call #! leaves them: the caller's stack just past the return address. #! @param this The writer. def dwarf_writer::cie(&mut this): void { # Code is advanced by bytes, and offsets are of words. val start := begin_cie(&mut this.frame, 1, -8, amd64_return_address); cfa_def_cfa(&mut this.frame, amd64_rsp, 8); cfa_offset(&mut this.frame, amd64_return_address as u8, 1); end_entry(&mut this.frame, start); } #! Writes how a function's frames unwind: through `rbp` from its prologue on, #! with each register it saves below the frame pointer once it has, and #! through `rsp` past each `leave`, until the instruction leaving the function, #! where each register is as the caller had it. #! @param this The writer. #! @param index The function. def dwarf_writer::function_frame(&mut this, index: u32): void { val start := begin_fde(&mut this.frame, 0, this.b.text_base + this.b.starts[index], this.b.ends[index] - this.b.starts[index]); var at: u64 = 0; frame_rules(&mut this.frame, &mut at, framed_at, based_at); val saves := this.b.debug.saves[index] as u64; if saves != 0 { # The first is past the saved `rbp` and the return address, the third word # below the canonical address. cfa_advance(&mut this.frame, &mut at, this.b.debug.saved_at[index] as u64); var word: u64 = 3; for &r in kept_gprs { continue if (saves & (1u << r as u64)) == 0; cfa_offset(&mut this.frame, amd64_registers[r as u64], word); word++; } } for r in this.b.debug.return_first[index]..this.b.debug.return_first[index + 1] { val left := this.b.debug.returns[r as u64] as u64; cfa_advance(&mut this.frame, &mut at, left); cfa_remember_state(&mut this.frame); cfa_def_cfa(&mut this.frame, amd64_rsp, 8); cfa_same_value(&mut this.frame, amd64_rbp); for &kept in kept_gprs { continue if (saves & (1u << kept as u64)) == 0; cfa_same_value(&mut this.frame, amd64_registers[kept as u64] as u64); } cfa_advance(&mut this.frame, &mut at, left + this.b.debug.return_bytes[r as u64] as u64); cfa_restore_state(&mut this.frame); } end_entry(&mut this.frame, start); } #! Writes how the trap stub's frame unwinds, which it makes as a function #! does, so a debugger stopped in the report unwinds to where the trap was. #! @param this The writer. def dwarf_writer::trap_frame(&mut this): void { val stub := this.b.trap_stub; val start := begin_fde(&mut this.frame, 0, this.b.text_base + stub, this.b.faults - stub); var at: u64 = 0; frame_rules(&mut this.frame, &mut at, this.b.trap_framed - stub, this.b.trap_based - stub); end_entry(&mut this.frame, start); } # The program. #! Writes the debugging information of an executable's code. #! @param b The building. #! @param directory The directory the build was in. #! @returns The writer, holding each section. #! The directory the build was in, which relative paths are within, or `.` #! where the system does not give it. #! @returns The directory. def build_directory(): string { var at := working_directory() else (_) { return string::from(".", nothing {}); }; return string::from(at.view(), nothing {}); } def write_dwarf(b: &building, directory: strview): dwarf_writer { val program := &b.c.program; val keys := program.layouts.length + 3; var w := dwarf_writer { b = b, info = byte_writer::new(), abbrev = byte_writer::new(), lines = byte_writer::new(), program = line_program::new(), strings = string_table::new(), line_strings = string_table::new(), frame = byte_writer::new(), ranges = byte_writer::new(), locations = byte_writer::new(), files = vector::::new(), numbers = vector::::new(), types = vector::::new(), queued = vector::::new(), queue = vector::::new(), fixups = fixup_list::new(nothing {}), abstracts = vector::::new() }; w.numbers.resize(b.c.checker.units.length); w.types.resize(keys); w.queued.resize(keys); w.abstracts.resize(program.functions.length); abbreviations(&mut w.abbrev); val ranges_length := begin_lists(&mut w.ranges); val locations_length := begin_lists(&mut w.locations); # The main file is numbered 1, and names the unit. val main_file := program.functions[program.main].file; w.file(main_file); val main_path := b.c.checker.units[main_file].source.path; # The unit, which every function and type is within. val unit := begin_unit(&mut w.info, dw_ut_compile, 8, 0); uleb(&mut w.info, ab_unit); w.name("sakura"); # The language debuggers read the program as, whose printing of structs, # references, and names joined by `::` is Sakura's own; DWARF has no code for # Sakura. Its functions can't be called by a debugger, whose calls follow C's # conventions, so each says it takes no calls. w.info.u16(dw_lang_c_plus_plus_14); val name_at := w.string_of(true, main_path); w.info.u32(name_at as u64); val directory_at := w.string_of(true, directory); w.info.u32(directory_at as u64); w.info.u64(b.text_base); w.info.u64(b.text.length); w.info.u32(0); # What each function inlined somewhere is, which the code it was inlined as # names. for i in 0..program.functions.length { continue if b.starts[i] == b.missing; for &site in program.functions[i].body.sites { val callee := site.function; continue if w.abstracts[callee] != 0; val inlined := &program.functions[callee]; w.abstracts[callee] = w.info.length as u32; uleb(&mut w.info, ab_abstract); w.name(inlined.name.view()); w.declared(inlined.file, inlined.offset); w.info.byte(dw_cc_nocall); w.info.byte(dw_inl_inlined); } } for i in 0..program.functions.length { continue if b.starts[i] == b.missing || b.ends[i] <= b.starts[i]; w.function(i as u32); w.line_program(i as u32); } # The types the variables named, and those their types name in turn. var q: u64 = 0; while q < w.queue.length { w.write_type(w.queue[q] as u64); q++; } w.fixups.resolve_words(&mut w.info, w.types.view()); w.info.byte(0); w.info.end_length(unit); w.line_table(directory); w.cie(); for i in 0..program.functions.length { continue if b.starts[i] == b.missing || b.ends[i] <= b.starts[i]; w.function_frame(i as u32); } w.trap_frame(); w.ranges.end_length(ranges_length); w.locations.end_length(locations_length); return w; } #! The sections of debugging information, by name, in the order an executable #! holds them. const debug_names: strview[8] = [ ".debug_info", ".debug_abbrev", ".debug_line", ".debug_str", ".debug_line_str", ".debug_frame", ".debug_rnglists", ".debug_loclists" ]; #! A section of debugging information a writer holds. #! @param this The writer. #! @param k Its place among `debug_names`. #! @returns The section. def dwarf_writer::section(&mut this, k: u64): &mut byte_writer { if k == 0 { return &mut this.info; } if k == 1 { return &mut this.abbrev; } if k == 2 { return &mut this.lines; } if k == 3 { return &mut this.strings.bytes; } if k == 4 { return &mut this.line_strings.bytes; } if k == 5 { return &mut this.frame; } if k == 6 { return &mut this.ranges; } return &mut this.locations; } #! A section of debugging information, held but not loaded. #! @param name Its name. #! @param data What it holds, taken. #! @param strings Whether it is strings, each ending with a zero. #! @returns The section. def debug_section(name: strview, data: &mut byte_writer, strings: bool): elf::section { return elf::section { name = name, kind = elf::section_kind::progbits, data = byte_writer::move_of(data), align = 1, flags = strings ? elf::merge | elf::strings : 0, entry = strings ? 1 : 0 }; } #! Writes the debugging information of an executable to its own file, its path #! with `.petal` added, and names that file in the executable, with its #! checksum. #! @param b The building. #! @param image The executable, which holds the note naming its build. #! @returns Whether the file was written. pub def describe_build(b: &building, image: &mut elf::image): bool { var directory := working_directory() else (_) { return false; }; var w := write_dwarf(b, directory.view()); # The file describes the executable, holding its build's note to be matched # by, and the sections. var described := elf::image::new(image.entry); described.described = true; described.linked = image.linked; described.segments.extend(image.segments.view()); described.symbols.extend(image.symbols.view()); for &each in image.sections { var data := byte_writer::new(); data.put(each.data.view()); described.sections.append(elf::section { name = each.name, kind = each.kind, data = byte_writer::move_of(&mut data), align = each.align, flags = each.flags, entry = each.entry }); } # The tables of strings, each ending with a zero, may be merged. for k in 0..debug_names.length { described.sections.append(debug_section(debug_names[k], w.section(k), k == 3 || k == 4)); } val path := $"{b.options.output}.petal"; val bytes := described.file(); return false if !write_bytes(path.view(), bytes.view(), 0o644); # The executable names the file, its zeros padded to a multiple of four, # then the file's checksum. val whole := path.view(); var slash := whole.length; while slash > 0 && whole[slash - 1] != '/' as u8 { slash--; } var link := byte_writer::new(); link.put_terminated(whole.substr(slash, whole.length - slash)); link.align_to(4); link.u32(elf::crc32(bytes.view())); image.sections.append(elf::section { name = ".gnu_debuglink", kind = elf::section_kind::progbits, data = byte_writer::move_of(&mut link), align = 4, flags = 0, entry = 0 }); return true; }