123 lines
4.3 KiB
Plaintext
123 lines
4.3 KiB
Plaintext
#! @file debug.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains how an executable names its debugging information: the debug
|
||
|
|
#! directory's entries, each naming a record within the image, and the record
|
||
|
|
#! naming a PDB file, `RSDS`, by its GUID, its age, and its path, which a
|
||
|
|
#! debugger finds the file by and matches it with. A record names a file whose
|
||
|
|
#! GUID is only known once both are written, so the file is stamped with it
|
||
|
|
#! last. And the headers of an executable's sections, as a PDB file keeps them.
|
||
|
|
|
||
|
|
import std::io;
|
||
|
|
module binary::pe;
|
||
|
|
|
||
|
|
|
||
|
|
#! The kind of entry naming a PDB file, as CodeView's.
|
||
|
|
pub const debug_codeview: u32 = 2;
|
||
|
|
|
||
|
|
#! The bytes of an entry of the debug directory.
|
||
|
|
pub const debug_entry_bytes: u64 = 28;
|
||
|
|
|
||
|
|
#! Adds an entry to a debug directory, naming a record within the image. Where
|
||
|
|
#! the record is in the file is written as the file is.
|
||
|
|
#! @param out The directory.
|
||
|
|
#! @param kind The kind of record, as `debug_codeview`.
|
||
|
|
#! @param address Where the record is, past the image's base.
|
||
|
|
#! @param size The record's bytes.
|
||
|
|
pub def debug_entry(out: &mut byte_writer, kind: u32, address: u32, size: u32): void {
|
||
|
|
out.u32(0); # Its characteristics,
|
||
|
|
out.u32(0); # its time, as `stamp_codeview` writes it,
|
||
|
|
out.u16(0); # and no version.
|
||
|
|
out.u16(0);
|
||
|
|
out.u32(kind);
|
||
|
|
out.u32(size);
|
||
|
|
out.u32(address);
|
||
|
|
out.u32(0); # Where it is in the file.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The record naming a PDB file: `RSDS`, its GUID, as `stamp_codeview` writes
|
||
|
|
#! it, its age, and its path, ending with a zero.
|
||
|
|
#! @param age How many times the file was written for the executable.
|
||
|
|
#! @param path The file's path.
|
||
|
|
#! @returns The record.
|
||
|
|
pub def codeview_record(age: u32, path: strview): byte_writer {
|
||
|
|
var made := byte_writer::new();
|
||
|
|
made.put("RSDS");
|
||
|
|
made.zeros(16);
|
||
|
|
made.u32(age);
|
||
|
|
made.put_terminated(path);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Where in an executable's file an address past its base is, by the section
|
||
|
|
#! holding it, or 0 where none does.
|
||
|
|
#! @param file The file.
|
||
|
|
#! @param address The address.
|
||
|
|
#! @returns Where it is.
|
||
|
|
def file_offset_of(file: span<u8>, address: u64): u64 {
|
||
|
|
val pe := word_at(file, 0x3C, 4);
|
||
|
|
val count := word_at(file, pe + 6, 2);
|
||
|
|
val table := pe + 24 + word_at(file, pe + 20, 2);
|
||
|
|
for k in 0..count {
|
||
|
|
val at := table + 40 * k;
|
||
|
|
val begins := word_at(file, at + 12, 4);
|
||
|
|
val size := word_at(file, at + 8, 4);
|
||
|
|
if address >= begins && address < begins + size {
|
||
|
|
return word_at(file, at + 20, 4) + address - begins;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A word of a file, least significant byte first.
|
||
|
|
def word_at(file: span<u8>, at: u64, bytes: u64): u64 {
|
||
|
|
var made: u64 = 0;
|
||
|
|
for k in 0..bytes {
|
||
|
|
made = made | (file[at + k] as u64 << (8 * k));
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Stamps an executable's file with the GUID of the PDB file it names, and a
|
||
|
|
#! word of it as the time of each entry naming one, once that file is written.
|
||
|
|
#! @param file The executable's file.
|
||
|
|
#! @param guid The PDB file's GUID.
|
||
|
|
#! @param stamp The word.
|
||
|
|
#! @returns Whether the file names a PDB file.
|
||
|
|
pub def stamp_codeview(file: &mut byte_writer, guid: u8[16], stamp: u32): bool {
|
||
|
|
val bytes := file.view();
|
||
|
|
val pe := word_at(bytes, 0x3C, 4);
|
||
|
|
val directory := pe + 24 + 112 + 8 * 6;
|
||
|
|
val address := word_at(bytes, directory, 4);
|
||
|
|
val size := word_at(bytes, directory + 4, 4);
|
||
|
|
return false if address == 0;
|
||
|
|
val first := file_offset_of(bytes, address);
|
||
|
|
return false if first == 0;
|
||
|
|
var named := false;
|
||
|
|
for k in 0..size / debug_entry_bytes {
|
||
|
|
val entry := first + debug_entry_bytes * k;
|
||
|
|
continue if word_at(file.view(), entry + 12, 4) != debug_codeview as u64;
|
||
|
|
file.set_u32(entry + 4, stamp);
|
||
|
|
val record := word_at(file.view(), entry + 24, 4);
|
||
|
|
for i in 0..16u {
|
||
|
|
file.set_byte(record + 4 + i, guid[i]);
|
||
|
|
}
|
||
|
|
named = true;
|
||
|
|
}
|
||
|
|
return named;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The headers of an executable's sections, as its file holds them, which a
|
||
|
|
#! PDB file keeps to place what it names.
|
||
|
|
#! @param file The file.
|
||
|
|
#! @returns The headers.
|
||
|
|
pub def section_headers(file: span<u8>): byte_writer {
|
||
|
|
val pe := word_at(file, 0x3C, 4);
|
||
|
|
val count := word_at(file, pe + 6, 2);
|
||
|
|
val table := pe + 24 + word_at(file, pe + 20, 2);
|
||
|
|
var made := byte_writer::new();
|
||
|
|
made.put(span::<u8>::over(file.data + table, 40 * count));
|
||
|
|
return made;
|
||
|
|
}
|