#! @file writer.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the writer of PDB files, as Microsoft's linker writes them for an #! executable of x86-64: the stream naming the file, which the executable #! names it by, the streams of types and of IDs, the stream of modules, each #! with a stream of its symbols and lines, the sections each module's code is #! in, the files each is from, and how the image's sections are laid out, then #! the globals and publics, which a debugger finds a name by, and the table of #! names. The file is named by a GUID made from what it holds, so the same #! build writes the same file. import std::containers; import std::io; module binary::pdb; # The streams every PDB file has at its number, and those this one gives the # rest at. const stream_info: u16 = 1; const stream_types: u16 = 2; const stream_modules: u16 = 3; const stream_ids: u16 = 4; const stream_names: u16 = 5; const stream_type_hashes: u16 = 6; const stream_id_hashes: u16 = 7; const stream_globals: u16 = 8; const stream_publics: u16 = 9; const stream_records: u16 = 10; const stream_sections: u16 = 11; const stream_first_module: u16 = 12; #! How many buckets the hashes of globals and publics are taken modulo. const name_buckets: u64 = 4096; #! A part of a section, which a module's code or data is. struct contribution { var section: u16; var offset: u32; var size: u32; var characteristics: u32; var owner: u16; } #! A module: a part of the program compiled together, with its symbols and #! lines. struct module_info { var name: strview; #! Its name. var object: strview; #! The object it was compiled to, or empty for none. var symbols: byte_writer; #! Its stream of symbols, which begins with its signature. var lines: byte_writer; #! Its subsections of lines. var files: vector; #! The paths of the files its code is from. } #! A name the stream of globals or of publics holds, with its record. struct named_record { var name: strview; var offset: u32; #! Where its record is among the symbols' records. var section: u16; #! For a public, where it names. var place: u32; } #! A PDB file being written. pub struct writer { pub var types: type_table; #! The types. pub var ids: type_table; #! The IDs, of functions inlined. pub var names: name_table; #! The table of names, which lines name files in. pub var age: u32 = 1; #! How many times the file was written for its executable. pub var guid: u8[16]; #! What names the file, made from it once it is written. pub var signature: u32; #! A word of it, as the executable's debug directory gives its time. pub var sections: byte_writer; #! The headers of the image's sections, as the executable holds them. var modules: vector; var contributions: vector; var records: byte_writer; #! The records of the globals and publics. var globals: vector; var publics: vector; } #! Makes a PDB file with nothing in it yet. #! @returns The file. pub def writer::new(): writer { return writer { types = type_table::new(), ids = type_table::new(), names = name_table::new(), sections = byte_writer::new(), modules = vector::::new(), contributions = vector::::new(), records = byte_writer::new(), globals = vector::::new(), publics = vector::::new() }; } #! Adds a module. #! @param this The file. #! @param name Its name, as the object it was compiled to is named, or #! `* Linker *` for the linker's. #! @param object The object it was compiled to, or empty for none. #! @param symbols Its symbols, taken. #! @param lines Its subsections of lines, taken. #! @param files The paths of the files its code is from. #! @returns Its number, from 1, as globals name it by. pub def writer::add_module(&mut this, name: strview, object: strview, symbols: &mut byte_writer, lines: &mut byte_writer, files: span): u16 { var paths := vector::::new(); for &each in files { paths.append(each); } this.modules.append(module_info { name = name, object = object, symbols = byte_writer::move_of(symbols), lines = byte_writer::move_of(lines), files = move(&mut paths) }); return this.modules.length as u16; } #! Says a part of a section is a module's. #! @param this The file. #! @param owner The module, from 1. #! @param section The section, from 1. #! @param offset Where the part begins in it. #! @param size Its bytes. #! @param characteristics The section's characteristics. pub def writer::contribute(&mut this, owner: u16, section: u16, offset: u32, size: u32, characteristics: u32): void { this.contributions.append(contribution { section = section, offset = offset, size = size, characteristics = characteristics, owner = owner - 1 }); } #! Adds a public symbol, a name a debugger finds a place by. #! @param this The file. #! @param name The name. #! @param section The section, from 1. #! @param offset Where in it. #! @param flags What it names, as `public_function | public_code`. pub def writer::public(&mut this, name: strview, section: u16, offset: u32, flags: u32): void { val at := this.records.length as u32; public_record(&mut this.records, flags, section, offset, name); this.publics.append(named_record { name = name, offset = at, section = section, place = offset }); } #! Adds a global naming a procedure a module's stream holds. #! @param this The file. #! @param name Its name. #! @param owner The module, from 1. #! @param offset Where its record is in the module's stream. #! @param global Whether it is seen outside its module. pub def writer::procedure(&mut this, name: strview, owner: u16, offset: u64, global: bool): void { val at := this.records.length as u32; procedure_reference(&mut this.records, global, owner, offset as u32, name); this.globals.append(named_record { name = name, offset = at }); } # The streams. #! Writes the file, naming it by a GUID made from what it holds, which `guid` #! and `signature` then give. #! @param this The file. #! @returns The file's bytes. pub def writer::file(&mut this): byte_writer { var streams := vector::::new(); val count := stream_first_module as u64 + this.modules.length; for _ in 0..count { streams.append(byte_writer::new()); } streams[stream_info as u64] = this.info_stream(); streams[stream_types as u64] = this.types.stream(stream_type_hashes); streams[stream_type_hashes as u64] = this.types.hash_stream(); streams[stream_ids as u64] = this.ids.stream(stream_id_hashes); streams[stream_id_hashes as u64] = this.ids.hash_stream(); streams[stream_names as u64] = this.names.stream(); streams[stream_globals as u64] = hashed_names(&this.globals); streams[stream_publics as u64] = this.publics_stream(); var records := byte_writer::new(); records.put(this.records.span); streams[stream_records as u64] = move(&mut records); var sections := byte_writer::new(); sections.put(this.sections.span); streams[stream_sections as u64] = move(&mut sections); for m in 0..this.modules.length { var stream := byte_writer::new(); stream.put(this.modules[m].symbols.span); stream.put(this.modules[m].lines.span); stream.u32(0); # No references to globals. streams[stream_first_module as u64 + m] = move(&mut stream); } streams[stream_modules as u64] = this.modules_stream(); # The GUID is the digest of the file with none, and its first word the # signature, each written where the stream naming the file holds it. var firsts := vector::::new(); var file := msf_file(&streams, &mut firsts); val info := firsts[stream_info as u64]; val digest := md5_of(file.span); for k in 0..16u { this.guid[k] = digest[k]; file.set_byte(info + 12 + k, digest[k]); } this.signature = digest[0] as u32 | (digest[1] as u32 << 8) | (digest[2] as u32 << 16) | (digest[3] as u32 << 24); file.set_u32(info + 4, this.signature); return file; } #! The stream naming the file: its version, its signature, its age, and its #! GUID, the last two of which the executable names it by, then the map of the #! streams named, `/names` alone, then what the file has, the types of version #! 14's linker. The signature and GUID are written once the file is. #! @param this The file. #! @returns The stream. def writer::info_stream(&this): byte_writer { var made := byte_writer::new(); made.u32(20000404); made.u32(0); made.u32(this.age); made.zeros(16); # The map of names to streams: the names, then a table of eight buckets, # its one entry in the bucket of the name's hash, in 16 bits. val name: strview = "/names"; made.u32(name.length + 1); made.put_terminated(name); val bucket := (hash_v1(name) & 0xFFFF) % 8; made.u32(1); # One entry, made.u32(8); # in eight buckets, made.u32(1); # those holding one, made.u32(1u << bucket); made.u32(0); # none deleted. made.u32(0); # The name's offset, made.u32(stream_names); made.u32(20140508); # VC140. return made; } #! The stream of globals or the hash part of that of publics: a table of #! hashes of each name, each record named by where it is among the records, #! in buckets of its name's hash, sorted within each as Microsoft's tools sort #! them, then which buckets hold any, and where each of those begins. #! @param names The names. #! @returns The table. def hashed_names(names: &vector): byte_writer { val count := names.length; var bucket_of := vector::::new(); var sizes := vector::::new(); sizes.resize(name_buckets); for &each in names.span { val bucket := hash_v1(each.name) as u64 % name_buckets; bucket_of.append(bucket); sizes[bucket] = sizes[bucket] + 1; } var starts := vector::::new(); var sum: u64 = 0; for b in 0..name_buckets { starts.append(sum); sum += sizes[b]; } var cursors := vector::::new(); cursors.extend(starts.span); var order := vector::::new(); order.resize(count); for i in 0..count { val b := bucket_of[i]; order[cursors[b]] = i; cursors[b] = cursors[b] + 1; } # Within a bucket, shorter names first, then case set aside, then by place. for b in 0..name_buckets { val from := starts[b]; val to := cursors[b]; var i := from + 1; while i < to { var j := i; while j > from && name_before(names, order[j], order[j - 1]) { val swapped := order[j]; order[j] = order[j - 1]; order[j - 1] = swapped; j--; } i++; } } var made := byte_writer::new(); made.u32(0xFFFFFFFF); # The signature, made.u32(0xF12F091A); # the version, made.u32(8 * count); # the records' bytes, val buckets_at := made.length; made.u32(0); # and the buckets', written below. for &each in order.span { made.u32(names[each].offset as u64 + 1); made.u32(1); } val start := made.length; for word in 0..(name_buckets + 32) / 32 { var bits: u32 = 0; for k in 0..32u { val b := word * 32 + k; if b < name_buckets && sizes[b] != 0 { bits = bits | (1u << k) as u32; } } made.u32(bits); } for b in 0..name_buckets { if sizes[b] != 0 { made.u32(starts[b] * 12); } } made.set_u32(buckets_at, made.length - start); return made; } # Whether one name comes before another in their bucket: a shorter one first, # then letters as though of one case, then the one whose record comes first. def name_before(names: &vector, a: u64, b: u64): bool { val left := names[a].name; val right := names[b].name; if left.length != right.length { return left.length < right.length; } for i in 0..left.length { var x := left[i]; var y := right[i]; if x >= 'A' as u8 && x <= 'Z' as u8 { x = x + 32; } if y >= 'A' as u8 && y <= 'Z' as u8 { y = y + 32; } if x != y { return x < y; } } return names[a].offset < names[b].offset; } #! The stream of publics: its header, the table of their names' hashes, then #! where each record is, in the order of the places they name. #! @param this The file. #! @returns The stream. def writer::publics_stream(&this): byte_writer { val table := hashed_names(&this.publics); var order := vector::::new(); for i in 0..this.publics.length { order.append(i); } var i: u64 = 1; while i < order.length { var j := i; while j > 0 && this.placed_before(order[j], order[j - 1]) { val swapped := order[j]; order[j] = order[j - 1]; order[j - 1] = swapped; j--; } i++; } var made := byte_writer::new(); made.u32(table.length); made.u32(4 * order.length); for _ in 0..5u { made.u32(0); # No thunks. } made.put(table.span); for &each in order.span { made.u32(this.publics[each].offset); } return made; } # Whether one public names a place before another's, or the same place by a # name before the other's. def writer::placed_before(&this, a: u64, b: u64): bool { val left: named_record = this.publics[a]; val right: named_record = this.publics[b]; return left.section < right.section if left.section != right.section; return left.place < right.place if left.place != right.place; return left.name.compare(right.name) < 0; } #! The stream of modules: its header, each module, the parts of sections each #! is, the map of the image's sections, the files each module's code is from, #! a table of names of its own, empty, and the number of the stream holding #! the image's section headers. #! @param this The file. #! @returns The stream. def writer::modules_stream(&mut this): byte_writer { # The modules. var modules := byte_writer::new(); for m in 0..this.modules.length { val each := &this.modules[m]; modules.u32(0); var first := contribution { section = 0xFFFF, owner = m as u16 }; for &part in this.contributions.span { if part.owner == m as u16 { first = part; break; } } put_contribution(&mut modules, &first); modules.u16(0); # Its flags, modules.u16(stream_first_module as u64 + m); # its stream, modules.u32(each.symbols.length); # its symbols' bytes, modules.u32(0); # no lines of C11, modules.u32(each.lines.length); # its lines' bytes, modules.u16(each.files.length); # how many files, modules.u16(0); modules.u32(0); modules.u32(0); modules.u32(0); modules.put_terminated(each.name); modules.put_terminated(each.object); modules.align_to(4); } # The parts of sections, in order of their places. var parts := byte_writer::new(); parts.u32(0xF12EBA2D); var order := vector::::new(); for i in 0..this.contributions.length { order.append(i); } var i: u64 = 1; while i < order.length { var j := i; while j > 0 && contribution_before(&this.contributions[order[j]], &this.contributions[order[j - 1]]) { val swapped := order[j]; order[j] = order[j - 1]; order[j - 1] = swapped; j--; } i++; } for &each in order.span { put_contribution(&mut parts, &this.contributions[each]); } # The map of the sections, from their headers, then one of absolute places. val headers := this.sections.span; val section_count := headers.length / 40; var map := byte_writer::new(); map.u16(section_count + 1); map.u16(section_count + 1); for k in 0..section_count { val characteristics := this.sections.get_u32(40 * k + 36) as u64; var flags: u64 = 0x0108; # Its address of 32 bits, a selector, if (characteristics & 0x40000000) != 0 { flags = flags | 1; } if (characteristics & 0x80000000) != 0 { flags = flags | 2; } if (characteristics & 0x20000000) != 0 { flags = flags | 4; } map.u16(flags); map.u16(0); map.u16(0); map.u16(k + 1); map.u16(0xFFFF); map.u16(0xFFFF); map.u32(0); map.u32(this.sections.get_u32(40 * k + 8)); } map.u16(0x0208); map.u16(0); map.u16(0); map.u16(section_count + 1); map.u16(0xFFFF); map.u16(0xFFFF); map.u32(0); map.u32(0xFFFFFFFF); # The files of each module, as offsets into the names after them. var files := byte_writer::new(); var total: u64 = 0; for &each in this.modules.span { total += each.files.length; } files.u16(this.modules.length); files.u16(total & 0xFFFF); var first: u64 = 0; for &each in this.modules.span { files.u16(first); first += each.files.length; } for &each in this.modules.span { files.u16(each.files.length); } var file_names := byte_writer::new(); var file_at := interner::::new(); var file_offsets := vector::::new(); for &each in this.modules.span { for &path in each.files.span { val id := file_at.intern_view(path); if id == file_offsets.length { file_offsets.append(file_names.length as u32); file_names.put_terminated(path); } files.u32(file_offsets[id]); } } files.put(file_names.span); files.align_to(4); # The table of names of its own, holding none. val empty := name_table::new(); val names := empty.stream(); var made := byte_writer::new(); made.u32(0xFFFFFFFF); # The signature, made.u32(19990903); # the version, made.u32(this.age); made.u16(stream_globals); made.u16(0x8E00 | 0x0B); # As version 14.11's linker writes it, made.u16(stream_publics); made.u16(0); made.u16(stream_records); made.u16(0); made.u32(modules.length); made.u32(parts.length); made.u32(map.length); made.u32(files.length); made.u32(0); # No type servers, made.u32(0); made.u32(22); # the streams of debugging headers, made.u32(names.length); made.u16(0); made.u16(image_machine_x64); made.u32(0); made.put(modules.span); made.put(parts.span); made.put(map.span); made.put(files.span); made.put(names.span); # The streams of headers from the executable: only its sections'. for k in 0..11u { made.u16(k == 5 ? stream_sections : 0xFFFF); } return made; } # Adds a part of a section, as the stream of modules writes one. def put_contribution(out: &mut byte_writer, part: &contribution): void { out.u16(part.section); out.u16(0); out.u32(part.offset); out.u32(part.size); out.u32(part.characteristics); out.u16(part.owner); out.u16(0); out.u32(0); out.u32(0); } # Whether one part of a section comes before another. def contribution_before(a: &contribution, b: &contribution): bool { return a.section < b.section if a.section != b.section; return a.offset < b.offset; }