#! @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::io; 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); }