# Tests of DWARF's encodings: numbers, abbreviations, tables of strings, the # headers of units and of lists, line programs and the header naming their # files, and descriptions of frames and their instructions. import std::io; import binary::dwarf; import std::testing; # The bytes as hex. def hex_of(bytes: span): string { val digits: strview = "0123456789abcdef"; var made := string::new(nothing {}); for &each in bytes { made.append(digits.substr((each >> 4) as u64, 1)); made.append(digits.substr((each & 15) as u64, 1)); } return made; } def uleb_hex(value: u64): string { var made := byte_writer::new(); dwarf::uleb(&mut made, value); return hex_of(made.span); } def sleb_hex(value: i64): string { var made := byte_writer::new(); dwarf::sleb(&mut made, value); return hex_of(made.span); } @test def numbers_take_as_few_bytes_of_seven_bits_as_hold_them(t: &mut tester): void { t.expects(uleb_hex(0).span == "00"); t.expects(uleb_hex(127).span == "7f"); t.expects(uleb_hex(128).span == "8001"); t.expects(uleb_hex(624485).span == "e58e26"); t.expects(sleb_hex(0).span == "00"); t.expects(sleb_hex(-1).span == "7f"); t.expects(sleb_hex(63).span == "3f"); t.expects(sleb_hex(64).span == "c000"); t.expects(sleb_hex(-128).span == "807f"); t.expects(sleb_hex(-123456).span == "c0bb78"); } @test def abbreviations_strings_and_headers_are_as_dwarf_writes_them(t: &mut tester): void { var abbrev := byte_writer::new(); val pairs: u64[4] = [dwarf::dw_at_producer, dwarf::dw_form_strp, dwarf::dw_at_language, dwarf::dw_form_data2]; dwarf::abbreviation(&mut abbrev, 1, dwarf::dw_tag_compile_unit, true, pairs[:]); t.expects(hex_of(abbrev.span).span == "011101250e13050000"); var strings := dwarf::string_table::new(); t.expects(strings.offset_of("a") == 0 && strings.offset_of("bc") == 2 && strings.offset_of("a") == 0); t.expects(hex_of(strings.bytes.span).span == "6100626300"); var info := byte_writer::new(); val unit := dwarf::begin_unit(&mut info, dwarf::dw_ut_compile, 8, 0); info.byte(0); info.end_length(unit); t.expects(hex_of(info.span).span == "09000000050001080000000000"); var lists := byte_writer::new(); val list := dwarf::begin_lists(&mut lists); lists.end_length(list); t.expects(hex_of(lists.span).span == "080000000500080000000000"); } @test def a_line_program_writes_what_changes_from_row_to_row(t: &mut tester): void { var program := dwarf::line_program::new(); program.begin(0x1000); program.row(0, 1, 1, 0, false); program.row(4, 1, 3, 5, true); program.row(10, 2, 3, 5, false); program.end(16); var expected := string::new(nothing {}); expected.append("0009020010000000000000"); # The sequence's address, expected.append("01"); # the first row, as a program begins, expected.append("0505030202040a01"); # a column, two lines, four bytes on, the prologue's end, expected.append("0402020601"); # another file, six bytes on, expected.append("0206000101"); # and the end, six bytes on. t.expects(hex_of(program.bytes.span).span == expected.span, hex_of(program.bytes.span).span); } @test def the_line_tables_header_names_directories_and_files(t: &mut tester): void { var strings := dwarf::string_table::new(); var table := byte_writer::new(); val paths: strview[2] = ["main.sakura", "/lib/x.sakura"]; val program: u8[1] = [0]; dwarf::write_line_table(&mut table, "/build", paths[:], &mut strings, program[:]); # The build's directory, then the absolute path's, each file by its last part. t.expects(hex_of(strings.bytes.span).span == "2f6275696c64002f6c6962006d61696e2e73616b75726100782e73616b75726100", hex_of(strings.bytes.span).span); val all := table.span; t.expects(all.length == 4 + table.get_u32(0) as u64); t.expects(all[4] == 5 && all[5] == 0 && all[6] == 8 && all[7] == 0); # Three files: the main one numbered 0 and 1, each with its directory. val tail := all.length; t.expects(all[tail - 1] == 0); t.expects(all[tail - 2] == 1 && all[tail - 7] == 0 && all[tail - 12] == 0); } @test def frames_are_described_from_a_common_entry(t: &mut tester): void { var frame := byte_writer::new(); val cie := dwarf::begin_cie(&mut frame, 1, -8, dwarf::amd64_return_address); dwarf::cfa_def_cfa(&mut frame, dwarf::amd64_rsp, 8); dwarf::cfa_offset(&mut frame, dwarf::amd64_return_address as u8, 1); dwarf::end_entry(&mut frame, cie); t.expects(hex_of(frame.span).span == "14000000ffffffff040008000178100c0708900100000000", hex_of(frame.span).span); var described := byte_writer::new(); val fde := dwarf::begin_fde(&mut described, 0, 0x401000, 0x20); var at: u64 = 0; dwarf::cfa_advance(&mut described, &mut at, 1); dwarf::cfa_def_cfa_offset(&mut described, 16); dwarf::end_entry(&mut described, fde); t.expects(hex_of(described.span).span == "1c0000000000000000104000000000002000000000000000410e100000000000", hex_of(described.span).span); # Moves past what a byte of the instruction holds take bytes of their own. var moves := byte_writer::new(); var from: u64 = 0; dwarf::cfa_advance(&mut moves, &mut from, 0x40); dwarf::cfa_advance(&mut moves, &mut from, 0x140); dwarf::cfa_advance(&mut moves, &mut from, 0x10140); t.expects(from == 0x10140 && hex_of(moves.span).span == "02400300010400000100", hex_of(moves.span).span); }