#! @file pdb.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the numbers of PDB files and of CodeView, the debugging information #! they hold, as Microsoft's tools number them: the simple types, the kinds of #! type records and of symbols, the subsections of a module's lines, the #! languages, and the registers of x86-64. And the hashes a PDB file's tables #! find names and types by, as Microsoft's tools compute them, so a debugger #! finds what this writes. import std; module binary::pdb; # Simple types, whose index is their number, held by no record. pub const t_notype: u32 = 0x0000; #! No type. pub const t_void: u32 = 0x0003; pub const t_bool08: u32 = 0x0030; pub const t_real32: u32 = 0x0040; pub const t_real64: u32 = 0x0041; pub const t_rchar: u32 = 0x0070; #! A character of a byte. pub const t_char32: u32 = 0x007B; #! A character of 32 bits. pub const t_int1: u32 = 0x0068; pub const t_uint1: u32 = 0x0069; pub const t_int2: u32 = 0x0072; pub const t_uint2: u32 = 0x0073; pub const t_int4: u32 = 0x0074; pub const t_uint4: u32 = 0x0075; pub const t_int8: u32 = 0x0076; pub const t_uint8: u32 = 0x0077; #! Added to a simple type, a 64-bit pointer to it, as `t_64p | t_void` is a #! pointer to nothing in particular. pub const t_64p: u32 = 0x0600; #! The index of the first type a record holds, past every simple type's. pub const first_type: u32 = 0x1000; # Kinds of type records, which CodeView calls leaves. pub const lf_modifier: u16 = 0x1001; pub const lf_pointer: u16 = 0x1002; pub const lf_procedure: u16 = 0x1008; pub const lf_arglist: u16 = 0x1201; pub const lf_fieldlist: u16 = 0x1203; pub const lf_index: u16 = 0x1404; #! A field list going on in another record. pub const lf_enumerate: u16 = 0x1502; pub const lf_array: u16 = 0x1503; pub const lf_structure: u16 = 0x1505; pub const lf_union: u16 = 0x1506; pub const lf_enum: u16 = 0x1507; pub const lf_member: u16 = 0x150D; pub const lf_func_id: u16 = 0x1601; pub const lf_string_id: u16 = 0x1605; # The leaves a number past what 15 bits hold is written as, after its kind. pub const lf_char: u16 = 0x8000; pub const lf_short: u16 = 0x8001; pub const lf_ushort: u16 = 0x8002; pub const lf_long: u16 = 0x8003; pub const lf_ulong: u16 = 0x8004; pub const lf_quadword: u16 = 0x8009; pub const lf_uquadword: u16 = 0x800A; #! The properties of a struct, a union, or an enum. pub const property_forward: u16 = 0x0080; #! Only declared, its definition named elsewhere. pub const property_unique: u16 = 0x0200; #! Named by a unique name, after its name. #! The access a member is given: public. pub const access_public: u16 = 3; # Kinds of symbols. pub const s_end: u16 = 0x0006; pub const s_frameproc: u16 = 0x1012; pub const s_objname: u16 = 0x1101; pub const s_block32: u16 = 0x1103; pub const s_udt: u16 = 0x1108; pub const s_gdata32: u16 = 0x110D; pub const s_pub32: u16 = 0x110E; pub const s_lproc32: u16 = 0x110F; pub const s_gproc32: u16 = 0x1110; pub const s_regrel32: u16 = 0x1111; pub const s_procref: u16 = 0x1125; pub const s_lprocref: u16 = 0x1127; pub const s_section: u16 = 0x1136; pub const s_compile3: u16 = 0x113C; pub const s_local: u16 = 0x113E; pub const s_defrange_register: u16 = 0x1141; pub const s_defrange_framepointer_rel: u16 = 0x1142; pub const s_defrange_subfield_register: u16 = 0x1143; pub const s_defrange_framepointer_rel_full_scope: u16 = 0x1144; pub const s_defrange_register_rel: u16 = 0x1145; pub const s_inlinesite: u16 = 0x114D; pub const s_inlinesite_end: u16 = 0x114E; #! What a procedure is: it keeps a frame pointer. pub const proc_has_frame_pointer: u8 = 0x01; #! What a public symbol names: code, and a function. pub const public_code: u32 = 0x01; pub const public_function: u32 = 0x02; #! What a local is: a parameter. pub const local_parameter: u16 = 0x0001; #! The register a procedure's locals and parameters are found from, as its #! frame's record encodes it: `rbp`, for each. pub const frame_locals_rbp: u32 = 2 << 14; pub const frame_params_rbp: u32 = 2 << 16; # The subsections a module's lines are held in. pub const debug_s_lines: u32 = 0xF2; pub const debug_s_file_checksums: u32 = 0xF4; pub const debug_s_inlinee_lines: u32 = 0xF6; # Languages and machines. pub const language_c: u8 = 0x00; pub const language_cxx: u8 = 0x01; pub const language_link: u8 = 0x07; #! The linker's own module. pub const cv_machine_x64: u16 = 0xD0; pub const image_machine_x64: u16 = 0x8664; # The registers of x86-64, as CodeView numbers them. pub const amd64_rbp: u16 = 334; pub const amd64_rsp: u16 = 335; #! A general register's number, as CodeView numbers the part of a width. #! @param number The register, as x86-64 encodes it: `rax` is 0, `r8` is 8. #! @param width The bytes of it: 1, 2, 4, or 8. #! @returns Its number. pub def amd64_register(number: u8, width: u64): u16 { val n := (number & 15) as u16; if width == 1 { # `al`, `cl`, `dl`, and `bl`, then `spl`, `bpl`, `sil`, and `dil`. val low: u16[8] = [1, 2, 3, 4, 327, 326, 324, 325]; return n < 8 ? low[n as u64] : 344 + n - 8; } if width == 2 { return n < 8 ? 9 + n : 352 + n - 8; } if width == 4 { return n < 8 ? 17 + n : 360 + n - 8; } # `rax`, `rcx`, `rdx`, `rbx`, `rsp`, `rbp`, `rsi`, and `rdi`, as numbered. val whole: u16[8] = [328, 330, 331, 329, 335, 334, 332, 333]; return n < 8 ? whole[n as u64] : 336 + n - 8; } #! An SSE register's number. #! @param number The register: `xmm0` is 0. #! @returns Its number. pub def amd64_xmm(number: u8): u16 { val n := (number & 15) as u16; return n < 8 ? 154 + n : 252 + n - 8; } # Hashes. #! The hash a PDB file finds a name by, in its tables of strings, of globals, #! and of types: the words of the text xored together, then the half word and #! the byte left over, its letters' cases made alike. #! @param text The name. #! @returns The hash. pub def hash_v1(text: span): u32 { var made: u32 = 0; val words := text.length / 4; for i in 0..words { val at := 4 * i; made = made ^ (text[at] as u32 | (text[at + 1] as u32 << 8) | (text[at + 2] as u32 << 16) | (text[at + 3] as u32 << 24)); } var at := 4 * words; var left := text.length % 4; if left >= 2 { made = made ^ (text[at] as u32 | (text[at + 1] as u32 << 8)); at += 2; left -= 2; } if left == 1 { made = made ^ text[at] as u32; } made = made | 0x20202020; made = made ^ (made >> 11); return made ^ (made >> 16); } #! The hash a PDB file finds a type record by, of its bytes: CRC-32 begun at #! zero and never inverted, as JamCRC is. #! @param bytes The record. #! @returns The hash. pub def hash_record(bytes: span): u32 { var made: u32 = 0; for &each in bytes { var c := (made ^ each as u32) & 0xFF; for _ in 0..8u { c = (c & 1) != 0 ? 0xEDB88320 ^ (c >> 1) : c >> 1; } made = c ^ (made >> 8); } return made; }