Initial Commit
This commit is contained in:
35 files changed
+8351
No files matched your search
+123
@@ -0,0 +1,123 @@
|
||||
#! @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.span;
|
||||
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.span, entry + 12, 4) != debug_codeview as u64;
|
||||
file.set_u32(entry + 4, stamp);
|
||||
val record := word_at(file.span, 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;
|
||||
}
|
||||
+318
@@ -0,0 +1,318 @@
|
||||
#! @file pe.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the writer of PE files: an x86-64 Windows executable, run in a
|
||||
#! console unless another subsystem is given, as `subsystem_windows` runs one
|
||||
#! in windows of its own with no console, loaded at the address it was
|
||||
#! built for with nothing to relocate, so every section is at an address known
|
||||
#! while building and the file holds each at the same offset from its start as
|
||||
#! the section is from the image's. The tables the loader and debuggers read,
|
||||
#! as the imports, are named by the data directories, each at an address within
|
||||
#! a section. The functions are named in a COFF symbol table so debuggers and
|
||||
#! disassemblers show them, and a section whose name is longer than eight
|
||||
#! bytes, as debugging information names them, is named by where the name is
|
||||
#! in the table of strings after the symbols.
|
||||
|
||||
import std::io;
|
||||
module binary::pe;
|
||||
|
||||
|
||||
#! The permissions of a section, numbered as ELF numbers a segment's, and
|
||||
#! whether only debuggers read it, so the system may discard it once loaded.
|
||||
pub const readable: u32 = 4;
|
||||
pub const writable: u32 = 2;
|
||||
pub const executable: u32 = 1;
|
||||
pub const discardable: u32 = 8;
|
||||
|
||||
#! The subsystems an executable runs in: in windows of its own, with no console,
|
||||
#! or in a console, which Windows opens for it where it's started without one.
|
||||
pub const subsystem_windows: u16 = 2;
|
||||
pub const subsystem_console: u16 = 3;
|
||||
|
||||
#! The bytes of the file headers, which the first section follows.
|
||||
pub const headers_size: u64 = 0x1000;
|
||||
|
||||
# Sections are aligned to a page in memory and in the file alike, so each is at
|
||||
# the same offset in both. The header of the image follows that of MS-DOS, then
|
||||
# the optional header, then the section headers.
|
||||
const page: u64 = 0x1000;
|
||||
const signature_at: u64 = 0x40;
|
||||
const file_header: u64 = 0x44;
|
||||
const optional_header: u64 = 0x58;
|
||||
const section_table: u64 = 0x148;
|
||||
const section_bytes: u64 = 40;
|
||||
const name_bytes: u64 = 8;
|
||||
|
||||
#! A section, loaded where it is placed, a page past the one before it.
|
||||
pub struct section {
|
||||
pub var name: strview; #! As `.text`, or longer, as debugging information names them.
|
||||
pub var address: u64; #! Aligned to a page.
|
||||
pub var data: span<u8>; #! What it holds.
|
||||
pub var size: u64; #! How many bytes.
|
||||
pub var flags: u32; #! As `readable`, `writable`, `executable`, and `discardable`.
|
||||
}
|
||||
|
||||
#! Where a table the loader reads is, at an address within a section, or
|
||||
#! nowhere where its address is 0.
|
||||
pub struct directory {
|
||||
pub var address: u64;
|
||||
pub var size: u64;
|
||||
}
|
||||
|
||||
#! A function, which debuggers and disassemblers show by name.
|
||||
pub struct symbol {
|
||||
pub var name: strview;
|
||||
pub var address: u64;
|
||||
}
|
||||
|
||||
#! An executable to write.
|
||||
pub struct image {
|
||||
pub var base: u64; #! Where it is loaded, below 2 GiB, as every address its code holds is.
|
||||
pub var entry: u64;
|
||||
pub var subsystem: u16 = subsystem_console; #! A console's, unless given.
|
||||
pub var stack_reserve: u64; #! The bytes of the first thread's stack.
|
||||
pub var stack_commit: u64;
|
||||
pub var sections: vector<section>; #! In order of their addresses.
|
||||
pub var imports: directory; #! The record of each library imported, then a record of zeros.
|
||||
pub var resources: directory; #! The tree of its resources, as `resources::bytes` writes it.
|
||||
pub var exceptions: directory; #! The table of functions and how each unwinds.
|
||||
pub var debug: directory; #! The records naming the executable's debugging information.
|
||||
pub var iat: directory; #! The words the loader writes the addresses imported to.
|
||||
pub var symbols: vector<symbol>; #! The functions, each within a section.
|
||||
}
|
||||
|
||||
#! Makes an executable loaded at an address and starting at another, with
|
||||
#! nothing in it yet.
|
||||
#! @param base Where it is loaded.
|
||||
#! @param entry Where it starts.
|
||||
#! @returns The executable.
|
||||
pub def image::new(base: u64, entry: u64): image {
|
||||
return image {
|
||||
base = base,
|
||||
entry = entry,
|
||||
sections = vector::<section>::new(),
|
||||
imports = directory {},
|
||||
resources = directory {},
|
||||
exceptions = directory {},
|
||||
debug = directory {},
|
||||
iat = directory {},
|
||||
symbols = vector::<symbol>::new()
|
||||
};
|
||||
}
|
||||
|
||||
#! Adds a section.
|
||||
#! @param this The executable.
|
||||
#! @param name Its name.
|
||||
#! @param address Where it is loaded.
|
||||
#! @param data What it holds.
|
||||
#! @param size How many bytes.
|
||||
#! @param flags Its permissions.
|
||||
pub def image::section(&mut this, name: strview, address: u64, data: span<u8>, size: u64, flags: u32): void {
|
||||
this.sections.append(section { name = name, address = address, data = data, size = size, flags = flags });
|
||||
}
|
||||
|
||||
#! The end of a section, aligned to a page.
|
||||
#! @param value Where it ends.
|
||||
#! @returns The end.
|
||||
def page_aligned(value: u64): u64 {
|
||||
return (value + page - 1) / page * page;
|
||||
}
|
||||
|
||||
#! Adds zeros up to a place, where the bytes end before it, so what is added
|
||||
#! next begins there.
|
||||
#! @param file The bytes.
|
||||
#! @param at The place.
|
||||
def pad_to(file: &mut byte_writer, at: u64): void {
|
||||
if file.length < at {
|
||||
file.zeros(at - file.length);
|
||||
}
|
||||
}
|
||||
|
||||
#! The eight bytes a name is held in, as a word, least significant byte first:
|
||||
#! the name, padded with zeros, or, where it is longer, where it is in the table
|
||||
#! of strings, which it is added to: a section's as `/` and the offset in
|
||||
#! decimal, and a symbol's as four zero bytes and the offset.
|
||||
#! @param strings The table of strings, whose first four bytes hold its size.
|
||||
#! @param name The name.
|
||||
#! @param of_symbol Whether it is a symbol's, rather than a section's.
|
||||
#! @returns The bytes.
|
||||
def name_word(strings: &mut byte_writer, name: strview, of_symbol: bool): u64 {
|
||||
var word: u64 = 0;
|
||||
if name.length <= name_bytes {
|
||||
for i in 0..name.length {
|
||||
word = word | (name[i] as u64 << (8 * i));
|
||||
}
|
||||
return word;
|
||||
}
|
||||
val offset := strings.length;
|
||||
strings.put_terminated(name);
|
||||
return offset << 32 if of_symbol;
|
||||
var digits: u8[10];
|
||||
var count: u64 = 0;
|
||||
var left := offset;
|
||||
while true {
|
||||
digits[count] = ('0' as u64 + left % 10) as u8;
|
||||
count++;
|
||||
left = left / 10;
|
||||
break if left == 0;
|
||||
}
|
||||
# Digits past the eighth byte are not held.
|
||||
word = '/' as u64;
|
||||
for i in 0..count {
|
||||
break if i + 1 >= name_bytes;
|
||||
word = word | (digits[count - 1 - i] as u64 << (8 * (i + 1)));
|
||||
}
|
||||
return word;
|
||||
}
|
||||
|
||||
#! The characteristics of a section: code, or data, read, written, or only read
|
||||
#! by debuggers.
|
||||
#! @param flags Its permissions.
|
||||
#! @returns The characteristics.
|
||||
def characteristics(flags: u32): u64 {
|
||||
return 0x60000020 if (flags & executable) != 0; # Code, executed and read.
|
||||
var made: u64 = 0x40000040; # Data, read.
|
||||
if (flags & writable) != 0 { made = made | 0x80000000; }
|
||||
if (flags & discardable) != 0 { made = made | 0x02000000; }
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The bytes of the file of the executable: the headers, each section's data
|
||||
#! padded to a page, then the symbols and the table of strings, which the
|
||||
#! loader does not read. The headers take the first page, written once what
|
||||
#! they say is known.
|
||||
#! @param this The executable.
|
||||
#! @returns The bytes.
|
||||
pub def image::file(&this): byte_writer {
|
||||
var file := byte_writer::new();
|
||||
file.zeros(headers_size);
|
||||
var strings := byte_writer::new();
|
||||
strings.u32(0);
|
||||
|
||||
# The sections holding nothing are left out, so no two share an address.
|
||||
var numbers := vector::<u64>::new();
|
||||
var raw := vector::<u64>::new();
|
||||
var count: u64 = 0;
|
||||
var offset := headers_size;
|
||||
var end := this.base + headers_size;
|
||||
var code: u64 = 0;
|
||||
var code_size: u64 = 0;
|
||||
var data_size: u64 = 0;
|
||||
for &each in this.sections {
|
||||
if each.size == 0 {
|
||||
numbers.append(0);
|
||||
raw.append(0);
|
||||
continue;
|
||||
}
|
||||
val at := section_table + count * section_bytes;
|
||||
val held := page_aligned(each.size);
|
||||
count++;
|
||||
numbers.append(count);
|
||||
raw.append(offset);
|
||||
file.set_u64(at, name_word(&mut strings, each.name, false));
|
||||
file.set_u32(at + 8, each.size);
|
||||
file.set_u32(at + 12, each.address - this.base);
|
||||
file.set_u32(at + 16, held);
|
||||
file.set_u32(at + 20, offset);
|
||||
file.set_u32(at + 36, characteristics(each.flags));
|
||||
pad_to(&mut file, offset);
|
||||
file.put(each.data);
|
||||
offset += held;
|
||||
end = page_aligned(each.address + each.size);
|
||||
if (each.flags & executable) != 0 {
|
||||
code = code == 0 ? each.address : code;
|
||||
code_size += held;
|
||||
} else {
|
||||
data_size += held;
|
||||
}
|
||||
}
|
||||
pad_to(&mut file, offset);
|
||||
|
||||
# Each function is named within the section holding it, by its offset there.
|
||||
val symbols := offset;
|
||||
var symbol_count: u64 = 0;
|
||||
for &named in this.symbols {
|
||||
for i in 0..this.sections.length {
|
||||
val each: section = this.sections[i];
|
||||
continue if numbers[i] == 0 || named.address < each.address || named.address >= each.address + each.size;
|
||||
file.u64(name_word(&mut strings, named.name, true));
|
||||
file.u32(named.address - each.address);
|
||||
file.u16(numbers[i]);
|
||||
file.u16(0x20); # A function,
|
||||
file.byte(2); # named outside the file.
|
||||
file.byte(0);
|
||||
symbol_count++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
# The strings follow the symbols, where the file's header says the symbols
|
||||
# are, even where there are none, as the names of sections may be among them.
|
||||
val tabled := symbol_count != 0 || strings.length > 4;
|
||||
strings.set_u32(0, strings.length);
|
||||
if tabled {
|
||||
file.put(strings.span);
|
||||
}
|
||||
|
||||
# The header of MS-DOS, which only says where the header of the image is.
|
||||
file.set_byte(0, 'M' as u8);
|
||||
file.set_byte(1, 'Z' as u8);
|
||||
file.set_u32(0x3C, signature_at);
|
||||
file.set_u32(signature_at, ('P' as u64) | (('E' as u64) << 8));
|
||||
|
||||
val f := file_header;
|
||||
file.set_u16(f, 0x8664); # x86-64,
|
||||
file.set_u16(f + 2, count);
|
||||
file.set_u32(f + 8, tabled ? symbols : 0);
|
||||
file.set_u32(f + 12, symbol_count);
|
||||
file.set_u16(f + 16, 0xF0);
|
||||
# with nothing to relocate, an executable, and an address space larger than
|
||||
# 2 GiB, which the runtime's memory reserves.
|
||||
file.set_u16(f + 18, 0x0023);
|
||||
|
||||
val o := optional_header;
|
||||
file.set_u16(o, 0x20B); # PE32+
|
||||
file.set_u32(o + 4, code_size);
|
||||
file.set_u32(o + 8, data_size);
|
||||
file.set_u32(o + 16, this.entry - this.base);
|
||||
file.set_u32(o + 20, code != 0 ? code - this.base : 0);
|
||||
file.set_u64(o + 24, this.base);
|
||||
file.set_u32(o + 32, page);
|
||||
file.set_u32(o + 36, page);
|
||||
file.set_u16(o + 40, 6); # Windows Vista and later.
|
||||
file.set_u16(o + 48, 6);
|
||||
file.set_u32(o + 56, end - this.base);
|
||||
file.set_u32(o + 60, headers_size);
|
||||
file.set_u16(o + 68, this.subsystem); # 3 runs it in a console, 2 in windows.
|
||||
# Its memory is executed only where it is code, and it is loaded where it was
|
||||
# built for, which every address its code holds assumes: NX_COMPAT and
|
||||
# TERMINAL_SERVER_AWARE, but not DYNAMIC_BASE.
|
||||
file.set_u16(o + 70, 0x8100);
|
||||
file.set_u64(o + 72, this.stack_reserve);
|
||||
file.set_u64(o + 80, this.stack_commit);
|
||||
file.set_u64(o + 88, 0x100000);
|
||||
file.set_u64(o + 96, 0x1000);
|
||||
file.set_u32(o + 108, 16);
|
||||
# The data directories, by their numbers.
|
||||
val directories: directory[5] = [this.imports, this.resources, this.exceptions, this.debug, this.iat];
|
||||
val indexes: u64[5] = [1, 2, 3, 6, 12];
|
||||
for k in 0..5u {
|
||||
val each: directory = directories[k];
|
||||
continue if each.address == 0;
|
||||
file.set_u32(o + 112 + 8 * indexes[k], each.address - this.base);
|
||||
file.set_u32(o + 116 + 8 * indexes[k], each.size);
|
||||
}
|
||||
|
||||
# Each entry of the debug directory says where in the file its record is,
|
||||
# which only the file's layout knows.
|
||||
if this.debug.address != 0 {
|
||||
val first := file_offset_of(file.span, this.debug.address - this.base);
|
||||
for k in 0..this.debug.size / debug_entry_bytes {
|
||||
val entry := first + debug_entry_bytes * k;
|
||||
val record := file_offset_of(file.span, file.get_u32(entry + 20) as u64);
|
||||
file.set_u32(entry + 24, record);
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
@@ -0,0 +1,596 @@
|
||||
#! @file resources.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the writer of an executable's resources, what the system and its
|
||||
#! shell read of a program without running it: `resources`, each resource by
|
||||
#! its type, its number, and its language, as `.rsrc` holds them and the
|
||||
#! resource directory names them; `icon_group`, an icon's images, of PNG files
|
||||
#! or icon files, as Explorer shows the program and `LoadImageW` finds its icon
|
||||
#! by the number of the group; and `version_resource`, what the file's
|
||||
#! properties say of it: its name, its version, and its file's name. And
|
||||
#! `find_resource`, which finds a resource in an executable's file again.
|
||||
#!
|
||||
#! The section is a tree: a directory of the types, each naming a directory of
|
||||
#! that type's numbers, each naming a directory of its languages, each naming
|
||||
#! an entry, which says where the resource's bytes are, by their address past
|
||||
#! the image's base, and how many there are. Every resource is named by a
|
||||
#! number, not a name.
|
||||
|
||||
import std::io;
|
||||
module binary::pe;
|
||||
|
||||
|
||||
#! The types of resources, as Windows numbers them.
|
||||
pub const resource_icon: u32 = 3; #! One image of an icon.
|
||||
pub const resource_icon_group: u32 = 14; #! An icon: its images, by their numbers.
|
||||
pub const resource_version: u32 = 16; #! What the file's properties say of the program.
|
||||
pub const resource_manifest: u32 = 24; #! The manifest the loader reads.
|
||||
|
||||
#! The language of a resource written in none.
|
||||
pub const language_neutral: u32 = 0;
|
||||
#! English as the United States speaks it, which version information is
|
||||
#! written in.
|
||||
pub const language_english: u32 = 0x409;
|
||||
|
||||
#! The most pixels an icon's image is wide or tall.
|
||||
pub const icon_most: u32 = 256;
|
||||
|
||||
# The bit of a directory's entry naming another directory, rather than a
|
||||
# resource's entry.
|
||||
const subdirectory: u64 = 0x80000000;
|
||||
|
||||
# A resource, its bytes where they are among the others'.
|
||||
struct resource_entry {
|
||||
var kind: u32;
|
||||
var id: u32;
|
||||
var language: u32;
|
||||
var at: u64;
|
||||
var size: u64;
|
||||
}
|
||||
|
||||
#! An executable's resources, each by its type, its number, and its language,
|
||||
#! as `.rsrc` holds them.
|
||||
pub struct resources {
|
||||
var entries: vector<resource_entry>;
|
||||
var data: byte_writer; # Each resource's bytes, each aligned to eight.
|
||||
}
|
||||
|
||||
#! Makes resources holding none.
|
||||
#! @returns The resources.
|
||||
pub def resources::new(): resources {
|
||||
return resources { entries = vector::<resource_entry>::new(), data = byte_writer::new() };
|
||||
}
|
||||
|
||||
#! How many resources there are.
|
||||
pub val resources::length: u64 {
|
||||
get => this.entries.length;
|
||||
}
|
||||
|
||||
#! Whether there are none, so the executable needs no section of them.
|
||||
pub val resources::empty: bool {
|
||||
get => this.entries.length == 0;
|
||||
}
|
||||
|
||||
#! Adds a resource, its bytes copied, in place of one of the same type, number,
|
||||
#! and language.
|
||||
#! @param this The resources.
|
||||
#! @param kind Its type, as `resource_icon`.
|
||||
#! @param id Its number.
|
||||
#! @param language Its language, as `language_neutral`.
|
||||
#! @param bytes What it holds.
|
||||
pub def resources::add(&mut this, kind: u32, id: u32, language: u32, bytes: span<u8>): void {
|
||||
var same := this.entries.length;
|
||||
for i in 0..this.entries.length {
|
||||
val each := &this.entries[i];
|
||||
if each.kind == kind && each.id == id && each.language == language {
|
||||
same = i;
|
||||
}
|
||||
}
|
||||
if same < this.entries.length {
|
||||
this.entries.remove_at(same);
|
||||
}
|
||||
this.data.align_to(8);
|
||||
val at := this.data.length;
|
||||
this.data.put(bytes);
|
||||
this.entries.append(resource_entry { kind = kind, id = id, language = language, at = at, size = bytes.length });
|
||||
}
|
||||
|
||||
#! The number past every resource of a type's, which the next of them takes.
|
||||
#! @param this The resources.
|
||||
#! @param kind The type.
|
||||
#! @returns The number, 1 where the type has none.
|
||||
pub def resources::next_id(&this, kind: u32): u32 {
|
||||
var most: u32 = 0;
|
||||
for &each in this.entries {
|
||||
if each.kind == kind && each.id > most {
|
||||
most = each.id;
|
||||
}
|
||||
}
|
||||
return most + 1;
|
||||
}
|
||||
|
||||
#! Adds an icon: each of its images a resource of `resource_icon`, numbered
|
||||
#! past those there are, and the group naming them, which `LoadImageW` finds the
|
||||
#! icon by, and Explorer shows the program by where it is the first group.
|
||||
#! @param this The resources.
|
||||
#! @param group The icon's images.
|
||||
#! @param id The group's number, 1 for the program's own icon.
|
||||
#! @param language Its language.
|
||||
pub def resources::add_icon(&mut this, group: &icon_group, id: u32, language: u32): void {
|
||||
var directory := byte_writer::new();
|
||||
directory.u16(0);
|
||||
directory.u16(1);
|
||||
directory.u16(group.images.length);
|
||||
for &each in group.images {
|
||||
val number := this.next_id(resource_icon);
|
||||
this.add(resource_icon, number, language, span::<u8>::over(group.data.span.data + each.at, each.size));
|
||||
directory.byte((each.width % 256) as u8);
|
||||
directory.byte((each.height % 256) as u8);
|
||||
directory.byte(each.colors);
|
||||
directory.byte(0);
|
||||
directory.u16(each.planes);
|
||||
directory.u16(each.bits);
|
||||
directory.u32(each.size);
|
||||
directory.u16(number);
|
||||
}
|
||||
this.add(resource_icon_group, id, language, directory.span);
|
||||
}
|
||||
|
||||
#! The resources as `.rsrc` holds them, at an address: the directory of their
|
||||
#! types, then each type's directory of its numbers, then each number's of its
|
||||
#! languages, each in order, then the entry of each resource, naming its bytes
|
||||
#! by their address, then the bytes.
|
||||
#! @param this The resources.
|
||||
#! @param address Where the section is, past the image's base.
|
||||
#! @returns The section's bytes, which the resource directory names whole.
|
||||
pub def resources::bytes(&this, address: u64): byte_writer {
|
||||
val order := this.sorted();
|
||||
# Where in `order` each type's resources begin, and each number's.
|
||||
var kinds := vector::<u64>::new();
|
||||
var names := vector::<u64>::new();
|
||||
for k in 0..order.length {
|
||||
val each := &this.entries[order[k]];
|
||||
if k == 0 || this.entries[order[k - 1]].kind != each.kind {
|
||||
kinds.append(k);
|
||||
names.append(k);
|
||||
} else if this.entries[order[k - 1]].id != each.id {
|
||||
names.append(k);
|
||||
}
|
||||
}
|
||||
|
||||
# Where each directory is, from where the section begins: the types', then
|
||||
# each type's, then each number's, then the resources' entries.
|
||||
var at := 16 + 8 * kinds.length;
|
||||
var kind_at := vector::<u64>::new();
|
||||
for i in 0..kinds.length {
|
||||
kind_at.append(at);
|
||||
at += 16 + 8 * names_within(&kinds, &names, i, order.length);
|
||||
}
|
||||
var name_at := vector::<u64>::new();
|
||||
for j in 0..names.length {
|
||||
name_at.append(at);
|
||||
at += 16 + 8 * (run_end(&names, j, order.length) - names[j]);
|
||||
}
|
||||
val entries_at := at;
|
||||
at += 16 * order.length;
|
||||
val data_at := (at + 7) / 8 * 8;
|
||||
|
||||
var made := byte_writer::new();
|
||||
directory_header(&mut made, kinds.length);
|
||||
for i in 0..kinds.length {
|
||||
made.u32(this.entries[order[kinds[i]]].kind);
|
||||
made.u32(kind_at[i] | subdirectory);
|
||||
}
|
||||
for i in 0..kinds.length {
|
||||
val end := run_end(&kinds, i, order.length);
|
||||
directory_header(&mut made, names_within(&kinds, &names, i, order.length));
|
||||
for j in 0..names.length {
|
||||
continue if names[j] < kinds[i] || names[j] >= end;
|
||||
made.u32(this.entries[order[names[j]]].id);
|
||||
made.u32(name_at[j] | subdirectory);
|
||||
}
|
||||
}
|
||||
for j in 0..names.length {
|
||||
val end := run_end(&names, j, order.length);
|
||||
directory_header(&mut made, end - names[j]);
|
||||
for k in names[j]..end {
|
||||
made.u32(this.entries[order[k]].language);
|
||||
made.u32(entries_at + 16 * k);
|
||||
}
|
||||
}
|
||||
for k in 0..order.length {
|
||||
val each := &this.entries[order[k]];
|
||||
made.u32(address + data_at + each.at);
|
||||
made.u32(each.size);
|
||||
made.u32(0); # Its code page,
|
||||
made.u32(0); # and nothing.
|
||||
}
|
||||
made.zeros(data_at - made.length);
|
||||
made.put(this.data.span);
|
||||
return made;
|
||||
}
|
||||
|
||||
# The places of the resources, in order of their types, then their numbers,
|
||||
# then their languages.
|
||||
def resources::sorted(&this): vector<u64> {
|
||||
var order := vector::<u64>::new();
|
||||
for k in 0..this.entries.length {
|
||||
var at := order.length;
|
||||
while at > 0 && comes_after(&this.entries[order[at - 1]], &this.entries[k]) {
|
||||
at--;
|
||||
}
|
||||
order.insert_at(at, k);
|
||||
}
|
||||
return order;
|
||||
}
|
||||
|
||||
# Whether a resource comes after another in the tree.
|
||||
def comes_after(a: &resource_entry, b: &resource_entry): bool {
|
||||
return a.kind > b.kind if a.kind != b.kind;
|
||||
return a.id > b.id if a.id != b.id;
|
||||
return a.language > b.language;
|
||||
}
|
||||
|
||||
# Where a run of places ends: where the next begins, or at the end.
|
||||
def run_end(starts: &vector<u64>, i: u64, count: u64): u64 {
|
||||
return i + 1 < starts.length ? starts[i + 1] : count;
|
||||
}
|
||||
|
||||
# How many numbers a type has, of those beginning within its run.
|
||||
def names_within(kinds: &vector<u64>, names: &vector<u64>, i: u64, count: u64): u64 {
|
||||
val end := run_end(kinds, i, count);
|
||||
var found: u64 = 0;
|
||||
for &each in names {
|
||||
if each >= kinds[i] && each < end {
|
||||
found++;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
# A directory's header: no characteristics, time, or version, no entries named
|
||||
# by text, and how many are named by numbers.
|
||||
def directory_header(out: &mut byte_writer, numbered: u64): void {
|
||||
out.u32(0);
|
||||
out.u32(0);
|
||||
out.u16(0);
|
||||
out.u16(0);
|
||||
out.u16(0);
|
||||
out.u16(numbered);
|
||||
}
|
||||
|
||||
|
||||
# Icons.
|
||||
|
||||
#! What a file given as an icon is, as `icon_group::add` finds it.
|
||||
pub enum icon_read: u8 {
|
||||
added, #! A PNG or an icon file, whose images were added.
|
||||
too_large, #! A PNG wider or taller than an icon's image may be, `icon_most`, left out.
|
||||
unknown, #! Neither a PNG nor an icon file.
|
||||
broken, #! A PNG cut short, or an icon file whose images aren't where it says.
|
||||
}
|
||||
|
||||
# An image of an icon: its size, as an icon's directory says it, and where its
|
||||
# bytes are among the group's.
|
||||
struct icon_image {
|
||||
var width: u32;
|
||||
var height: u32;
|
||||
var colors: u8;
|
||||
var planes: u16;
|
||||
var bits: u16;
|
||||
var at: u64;
|
||||
var size: u64;
|
||||
}
|
||||
|
||||
#! The images of an icon, each a PNG, or a bitmap as an icon file holds one,
|
||||
#! which `resources::add_icon` adds.
|
||||
pub struct icon_group {
|
||||
var images: vector<icon_image>;
|
||||
var data: byte_writer;
|
||||
}
|
||||
|
||||
#! Makes an icon of no images.
|
||||
#! @returns The icon.
|
||||
pub def icon_group::new(): icon_group {
|
||||
return icon_group { images = vector::<icon_image>::new(), data = byte_writer::new() };
|
||||
}
|
||||
|
||||
#! How many images it has.
|
||||
pub val icon_group::length: u64 {
|
||||
get => this.images.length;
|
||||
}
|
||||
|
||||
#! Whether it has none.
|
||||
pub val icon_group::empty: bool {
|
||||
get => this.images.length == 0;
|
||||
}
|
||||
|
||||
#! Adds the images of a file: a PNG, whole, as Windows has read an icon's
|
||||
#! images since Vista, its size as its header says it; or each image of an icon
|
||||
#! file, a `.ico`.
|
||||
#! @param this The icon.
|
||||
#! @param file The file's bytes.
|
||||
#! @returns What the file was, and whether its images were added.
|
||||
pub def icon_group::add(&mut this, file: span<u8>): icon_read {
|
||||
if is_png(file) {
|
||||
return icon_read::broken if file.length < 24 || !same_text(file, 12, "IHDR");
|
||||
val width := big_word(file, 16);
|
||||
val height := big_word(file, 20);
|
||||
return icon_read::broken if width == 0 || height == 0;
|
||||
return icon_read::too_large if width > icon_most as u64 || height > icon_most as u64;
|
||||
this.add_image(width as u32, height as u32, 0, 1, 32, file);
|
||||
return icon_read::added;
|
||||
}
|
||||
return icon_read::unknown if file.length < 6 || word_at(file, 0, 2) != 0 || word_at(file, 2, 2) != 1;
|
||||
val count := word_at(file, 4, 2);
|
||||
return icon_read::broken if count == 0 || file.length < 6 + 16 * count;
|
||||
for k in 0..count {
|
||||
val entry := 6 + 16 * k;
|
||||
val size := word_at(file, entry + 8, 4);
|
||||
val at := word_at(file, entry + 12, 4);
|
||||
return icon_read::broken if size == 0 || at + size > file.length;
|
||||
}
|
||||
for k in 0..count {
|
||||
val entry := 6 + 16 * k;
|
||||
val width := file[entry] as u32;
|
||||
val height := file[entry + 1] as u32;
|
||||
val size := word_at(file, entry + 8, 4);
|
||||
val at := word_at(file, entry + 12, 4);
|
||||
this.add_image(
|
||||
width == 0 ? icon_most : width, height == 0 ? icon_most : height, file[entry + 2],
|
||||
word_at(file, entry + 4, 2) as u16, word_at(file, entry + 6, 2) as u16,
|
||||
span::<u8>::over(file.data + at, size)
|
||||
);
|
||||
}
|
||||
return icon_read::added;
|
||||
}
|
||||
|
||||
# Adds an image, its bytes copied.
|
||||
def icon_group::add_image(&mut this, width: u32, height: u32, colors: u8, planes: u16, bits: u16, bytes: span<u8>): void {
|
||||
this.data.align_to(8);
|
||||
val at := this.data.length;
|
||||
this.data.put(bytes);
|
||||
this.images.append(icon_image {
|
||||
width = width, height = height, colors = colors, planes = planes, bits = bits, at = at, size = bytes.length
|
||||
});
|
||||
}
|
||||
|
||||
#! Whether a file is a PNG, by its first eight bytes.
|
||||
#! @param file The file.
|
||||
#! @returns Whether it is.
|
||||
pub def is_png(file: span<u8>): bool {
|
||||
val signature: u8[8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
|
||||
return false if file.length < 8;
|
||||
for k in 0..8u {
|
||||
return false if file[k] != signature[k];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The size of a PNG, as its header says it.
|
||||
#! @param file The file.
|
||||
#! @returns Its width and height, or zeros where it is no PNG or is cut short.
|
||||
pub def png_size(file: span<u8>): u32[2] {
|
||||
var made: u32[2];
|
||||
return made if !is_png(file) || file.length < 24 || !same_text(file, 12, "IHDR");
|
||||
made[0] = big_word(file, 16) as u32;
|
||||
made[1] = big_word(file, 20) as u32;
|
||||
return made;
|
||||
}
|
||||
|
||||
# A word of four bytes, the most significant first, as PNG writes them.
|
||||
def big_word(file: span<u8>, at: u64): u64 {
|
||||
return (file[at] as u64 << 24) | (file[at + 1] as u64 << 16) | (file[at + 2] as u64 << 8) | file[at + 3] as u64;
|
||||
}
|
||||
|
||||
# Whether bytes of a file at a place are text.
|
||||
def same_text(file: span<u8>, at: u64, text: strview): bool {
|
||||
return false if at + text.length > file.length;
|
||||
for k in 0..text.length {
|
||||
return false if file[at + k] != text[k];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
# What the file's properties say.
|
||||
|
||||
#! What the file's properties say of an executable, as `VS_VERSIONINFO` holds
|
||||
#! it: its version, as four numbers, the first three of `version` and 0, marked
|
||||
#! a prerelease where `version` has a `-`; then, in English, its description and
|
||||
#! its product, `title`, or its file's name without its ending where there's
|
||||
#! none, its version as written, where there's one, its file's name, and that
|
||||
#! name without its ending, as its internal name.
|
||||
#! @param title What it's called, as `Hearth`.
|
||||
#! @param version Its version, as `1.2.0` or `1.2.0-alpha`, or empty.
|
||||
#! @param file_name Its file's name, as `hearth.exe`.
|
||||
#! @returns The resource's bytes, which `resources::add` takes as
|
||||
#! `resource_version` number 1, in `language_english`.
|
||||
pub def version_resource(title: strview, version: strview, file_name: strview): byte_writer {
|
||||
var stem := file_name;
|
||||
for k in 0..file_name.length {
|
||||
if file_name[file_name.length - 1 - k] == '.' {
|
||||
stem = file_name.substr(0, file_name.length - 1 - k);
|
||||
break;
|
||||
}
|
||||
}
|
||||
val named := title.length != 0 ? title : stem;
|
||||
val numbers := version_numbers(version);
|
||||
var made := byte_writer::new();
|
||||
val root := begin_block(&mut made, "VS_VERSION_INFO", 52, false);
|
||||
made.u32(0xFEEF04BD); # Its signature,
|
||||
made.u32(0x00010000); # the struct's version,
|
||||
made.u32((numbers[0] << 16) | numbers[1]); # the file's version,
|
||||
made.u32(numbers[2] << 16);
|
||||
made.u32((numbers[0] << 16) | numbers[1]); # the product's,
|
||||
made.u32(numbers[2] << 16);
|
||||
made.u32(0x3F); # the flags that mean anything,
|
||||
made.u32(version.find("-") is some ? 2u : 0u); # a prerelease's,
|
||||
made.u32(0x00040004); # for Windows NT,
|
||||
made.u32(1); # a program,
|
||||
made.u32(0);
|
||||
made.u32(0); # and no date.
|
||||
made.u32(0);
|
||||
made.align_to(4);
|
||||
|
||||
val strings := begin_block(&mut made, "StringFileInfo", 0, true);
|
||||
val table := begin_block(&mut made, "040904B0", 0, true);
|
||||
string_value(&mut made, "FileDescription", named);
|
||||
if version.length != 0 {
|
||||
string_value(&mut made, "FileVersion", version);
|
||||
}
|
||||
string_value(&mut made, "InternalName", stem);
|
||||
string_value(&mut made, "OriginalFilename", file_name);
|
||||
string_value(&mut made, "ProductName", named);
|
||||
if version.length != 0 {
|
||||
string_value(&mut made, "ProductVersion", version);
|
||||
}
|
||||
end_block(&mut made, table);
|
||||
end_block(&mut made, strings);
|
||||
|
||||
made.align_to(4);
|
||||
val vars := begin_block(&mut made, "VarFileInfo", 0, true);
|
||||
val translation := begin_block(&mut made, "Translation", 4, false);
|
||||
made.u16(0x0409); # English,
|
||||
made.u16(0x04B0); # in UTF-16.
|
||||
end_block(&mut made, translation);
|
||||
end_block(&mut made, vars);
|
||||
end_block(&mut made, root);
|
||||
return made;
|
||||
}
|
||||
|
||||
# The first three numbers of a version, each at most 65535, and 0 for each it
|
||||
# lacks.
|
||||
def version_numbers(version: strview): u64[3] {
|
||||
var made: u64[3];
|
||||
var at: u64 = 0;
|
||||
for part in 0..3u {
|
||||
if part > 0 {
|
||||
break if at >= version.length || version[at] != '.';
|
||||
at++;
|
||||
}
|
||||
var value: u64 = 0;
|
||||
while at < version.length && version[at] >= '0' as u8 && version[at] <= '9' as u8 {
|
||||
value = value * 10 + (version[at] - '0' as u8) as u64;
|
||||
if value > 0xFFFF {
|
||||
value = 0xFFFF;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
made[part] = value;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# Begins a block of version information: its length, written as it ends, the
|
||||
# length of its value, whether that value is text, and its key, then zeros to
|
||||
# four bytes. Gives where it begins.
|
||||
def begin_block(out: &mut byte_writer, key: strview, value_length: u64, text: bool): u64 {
|
||||
val at := out.length;
|
||||
out.u16(0);
|
||||
out.u16(value_length);
|
||||
out.u16(text ? 1u : 0u);
|
||||
put_utf16(out, key);
|
||||
out.align_to(4);
|
||||
return at;
|
||||
}
|
||||
|
||||
# Ends a block, writing its length, which holds every block within it.
|
||||
def end_block(out: &mut byte_writer, at: u64): void {
|
||||
out.set_u16(at, out.length - at);
|
||||
}
|
||||
|
||||
# A block of text, its key and its value, the value's length in characters of
|
||||
# UTF-16 with its zero.
|
||||
def string_value(out: &mut byte_writer, key: strview, value: strview): void {
|
||||
out.align_to(4);
|
||||
var units := byte_writer::new();
|
||||
put_utf16(&mut units, value);
|
||||
val at := begin_block(out, key, units.length / 2, true);
|
||||
out.put(units.span);
|
||||
end_block(out, at);
|
||||
}
|
||||
|
||||
#! Writes text as UTF-16, the least significant byte first, as Windows holds
|
||||
#! text, each character of its UTF-8, then a zero. A byte that begins no
|
||||
#! character is written as U+FFFD.
|
||||
#! @param out Where it's written.
|
||||
#! @param text The text.
|
||||
pub def put_utf16(out: &mut byte_writer, text: strview): void {
|
||||
var at: u64 = 0;
|
||||
while at < text.length {
|
||||
val first := text[at] as u64;
|
||||
var code: u64 = 0xFFFD;
|
||||
var length: u64 = 1;
|
||||
if first < 0x80 {
|
||||
code = first;
|
||||
} else if first >= 0xF0 && first < 0xF8 && at + 3 < text.length {
|
||||
code = ((first & 7) << 18) | ((text[at + 1] as u64 & 0x3F) << 12) | ((text[at + 2] as u64 & 0x3F) << 6) | (text[at + 3] as u64 & 0x3F);
|
||||
length = 4;
|
||||
} else if first >= 0xE0 && first < 0xF0 && at + 2 < text.length {
|
||||
code = ((first & 0xF) << 12) | ((text[at + 1] as u64 & 0x3F) << 6) | (text[at + 2] as u64 & 0x3F);
|
||||
length = 3;
|
||||
} else if first >= 0xC0 && first < 0xE0 && at + 1 < text.length {
|
||||
code = ((first & 0x1F) << 6) | (text[at + 1] as u64 & 0x3F);
|
||||
length = 2;
|
||||
}
|
||||
if code >= 0x10000 {
|
||||
val past := code - 0x10000;
|
||||
out.u16(0xD800 | (past >> 10));
|
||||
out.u16(0xDC00 | (past & 0x3FF));
|
||||
} else {
|
||||
out.u16(code);
|
||||
}
|
||||
at += length;
|
||||
}
|
||||
out.u16(0);
|
||||
}
|
||||
|
||||
|
||||
# Reading.
|
||||
|
||||
#! Finds a resource in an executable's file: by its type and number, in the
|
||||
#! first of its languages.
|
||||
#! @param file The executable's file.
|
||||
#! @param kind The type, as `resource_icon_group`.
|
||||
#! @param id The number.
|
||||
#! @returns Its bytes, within the file, or nothing where the file has none.
|
||||
pub def find_resource(file: span<u8>, kind: u32, id: u32): maybe<span<u8>> {
|
||||
return nothing {} if file.length < 0x40;
|
||||
val pe := word_at(file, 0x3C, 4);
|
||||
val directory := pe + 24 + 112 + 8 * 2;
|
||||
return nothing {} if directory + 8 > file.length;
|
||||
val address := word_at(file, directory, 4);
|
||||
return nothing {} if address == 0;
|
||||
val base := file_offset_of(file, address);
|
||||
return nothing {} if base == 0;
|
||||
val types := entry_named(file, base, base, kind as u64) else { return nothing {}; };
|
||||
val names := entry_named(file, base, types, id as u64) else { return nothing {}; };
|
||||
return nothing {} if names + 24 > file.length || word_at(file, names + 14, 2) == 0;
|
||||
val entry := word_at(file, names + 20, 4);
|
||||
return nothing {} if (entry & subdirectory) != 0;
|
||||
val at := base + entry;
|
||||
return nothing {} if at + 16 > file.length;
|
||||
val data := file_offset_of(file, word_at(file, at, 4));
|
||||
val size := word_at(file, at + 4, 4);
|
||||
return nothing {} if data == 0 || data + size > file.length;
|
||||
return span::<u8>::over(file.data + data, size);
|
||||
}
|
||||
|
||||
# Where, in a file, the directory a directory's entry of a number names is, or
|
||||
# nothing where it has no such entry.
|
||||
def entry_named(file: span<u8>, base: u64, at: u64, id: u64): maybe<u64> {
|
||||
return nothing {} if at + 16 > file.length;
|
||||
val count := word_at(file, at + 12, 2) + word_at(file, at + 14, 2);
|
||||
for k in 0..count {
|
||||
val entry := at + 16 + 8 * k;
|
||||
return nothing {} if entry + 8 > file.length;
|
||||
continue if word_at(file, entry, 4) != id;
|
||||
val offset := word_at(file, entry + 4, 4);
|
||||
return nothing {} if (offset & subdirectory) == 0;
|
||||
return base + (offset & ~subdirectory);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
#! @file unwind.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the writer of how x86-64 code on Windows unwinds, which the system
|
||||
#! reads to unwind a thread's stack past each function, as exceptions and
|
||||
#! debuggers do: each function's record of how its prologue made its frame, as
|
||||
#! `.xdata` holds them, and the table of each function's code and its record,
|
||||
#! sorted by address, as `.pdata` holds it and the exception directory names.
|
||||
#!
|
||||
#! A record lists what the prologue did, each at the offset just past its
|
||||
#! instruction, the latest first: registers pushed, bytes taken from the stack,
|
||||
#! the frame register set, and registers saved in the frame. Unwinding past
|
||||
#! the prologue undoes each in turn; within it, only those done yet.
|
||||
|
||||
import std::containers;
|
||||
import std::io;
|
||||
module binary::pe;
|
||||
|
||||
|
||||
# What each code of a record does, in its low four bits.
|
||||
const push_nonvolatile: u16 = 0;
|
||||
const allocate_large: u16 = 1;
|
||||
const allocate_small: u16 = 2;
|
||||
const set_frame: u16 = 3;
|
||||
const save_nonvolatile: u16 = 4;
|
||||
const save_far: u16 = 5;
|
||||
|
||||
#! What a function's prologue did, as its record of unwinding says it.
|
||||
pub struct unwind_info {
|
||||
var codes: vector<u16>; #! Each code's slots, in the order the prologue did them.
|
||||
var starts: vector<u64>; #! Where each code's slots begin.
|
||||
var prologue: u8; #! The bytes of the prologue.
|
||||
var register: u8; #! The frame register, or 0 for none.
|
||||
var offset: u8; #! What the frame register points past the frame's base, in 16 bytes.
|
||||
}
|
||||
|
||||
#! Makes the record of a prologue that did nothing, as a function that leaves
|
||||
#! the stack as its caller left it has.
|
||||
#! @returns The record.
|
||||
pub def unwind_info::new(): unwind_info {
|
||||
return unwind_info { codes = vector::<u16>::new(), starts = vector::<u64>::new(), prologue = 0, register = 0, offset = 0 };
|
||||
}
|
||||
|
||||
# Adds a code done just before an offset, and its operand's slots after.
|
||||
def unwind_info::code(&mut this, at: u8, op: u16, info: u16): void {
|
||||
this.starts.append(this.codes.length);
|
||||
this.codes.append(at as u16 | (op << 8) | (info << 12));
|
||||
if at > this.prologue {
|
||||
this.prologue = at;
|
||||
}
|
||||
}
|
||||
|
||||
#! The prologue pushed a register, just before an offset.
|
||||
#! @param this The record.
|
||||
#! @param at Where the push ends, from where the function begins.
|
||||
#! @param register The register, as x86-64 numbers it: `rbp` is 5.
|
||||
pub def unwind_info::pushed(&mut this, at: u8, register: u8): void {
|
||||
this.code(at, push_nonvolatile, register as u16);
|
||||
}
|
||||
|
||||
#! The prologue took bytes from the stack, just before an offset.
|
||||
#! @param this The record.
|
||||
#! @param at Where the instruction taking them ends.
|
||||
#! @param bytes How many, a multiple of 8.
|
||||
pub def unwind_info::allocated(&mut this, at: u8, bytes: u64): void {
|
||||
if bytes <= 128 {
|
||||
this.code(at, allocate_small, (bytes / 8 - 1) as u16);
|
||||
} else if bytes <= 512 * 1024 - 8 {
|
||||
this.code(at, allocate_large, 0);
|
||||
this.codes.append((bytes / 8) as u16);
|
||||
} else {
|
||||
this.code(at, allocate_large, 1);
|
||||
this.codes.append((bytes & 0xFFFF) as u16);
|
||||
this.codes.append((bytes >> 16) as u16);
|
||||
}
|
||||
}
|
||||
|
||||
#! The prologue pointed a register at the frame, past its base by an offset,
|
||||
#! just before an offset of the code. The frame is found from that register
|
||||
#! from then on, however the stack moves.
|
||||
#! @param this The record.
|
||||
#! @param at Where the instruction ends.
|
||||
#! @param register The register.
|
||||
#! @param offset How far past the frame's base it points, a multiple of 16
|
||||
#! up to 240.
|
||||
pub def unwind_info::framed(&mut this, at: u8, register: u8, offset: u64): void {
|
||||
this.code(at, set_frame, 0);
|
||||
this.register = register;
|
||||
this.offset = (offset / 16) as u8;
|
||||
}
|
||||
|
||||
#! The prologue saved a register in the frame, just before an offset.
|
||||
#! @param this The record.
|
||||
#! @param at Where the instruction saving it ends.
|
||||
#! @param register The register.
|
||||
#! @param offset Where it is, past the frame's base, a multiple of 8.
|
||||
pub def unwind_info::saved(&mut this, at: u8, register: u8, offset: u64): void {
|
||||
if offset / 8 <= 0xFFFF {
|
||||
this.code(at, save_nonvolatile, register as u16);
|
||||
this.codes.append((offset / 8) as u16);
|
||||
} else {
|
||||
this.code(at, save_far, register as u16);
|
||||
this.codes.append((offset & 0xFFFF) as u16);
|
||||
this.codes.append((offset >> 16) as u16);
|
||||
}
|
||||
}
|
||||
|
||||
#! The record as `.xdata` holds it: version 1, the prologue's bytes, how many
|
||||
#! slots its codes take, the frame register and its offset, then the codes,
|
||||
#! the latest first, padded to an even number of slots.
|
||||
#! @param this The record.
|
||||
#! @returns The record's bytes.
|
||||
pub def unwind_info::bytes(&this): byte_writer {
|
||||
var made := byte_writer::new();
|
||||
made.byte(1);
|
||||
made.byte(this.prologue);
|
||||
made.byte(this.codes.length as u8);
|
||||
made.byte(this.register | (this.offset << 4));
|
||||
var k := this.starts.length;
|
||||
while k > 0 {
|
||||
k--;
|
||||
val end := k + 1 < this.starts.length ? this.starts[k + 1] : this.codes.length;
|
||||
for s in this.starts[k]..end {
|
||||
made.u16(this.codes[s]);
|
||||
}
|
||||
}
|
||||
if this.codes.length % 2 != 0 {
|
||||
made.u16(0);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The records of how functions unwind, as `.xdata` holds them, each once and
|
||||
#! aligned to four bytes, however many functions share it.
|
||||
pub struct unwind_table {
|
||||
pub var bytes: byte_writer; #! The section's bytes.
|
||||
var held: interner<string>; #! Each record, by its ID.
|
||||
var at: vector<u32>; #! Where each is, by its ID.
|
||||
}
|
||||
|
||||
#! Makes a table holding no records.
|
||||
#! @returns The table.
|
||||
pub def unwind_table::new(): unwind_table {
|
||||
return unwind_table { bytes = byte_writer::new(), held = interner::<string>::new(), at = vector::<u32>::new() };
|
||||
}
|
||||
|
||||
#! Where a record is in the table, adding it where it is new.
|
||||
#! @param this The table.
|
||||
#! @param info The record.
|
||||
#! @returns Where it is, from where the table begins.
|
||||
pub def unwind_table::add(&mut this, info: &unwind_info): u32 {
|
||||
val made := info.bytes();
|
||||
val id := this.held.intern_view(made.span);
|
||||
if id < this.at.length {
|
||||
return this.at[id];
|
||||
}
|
||||
this.bytes.align_to(4);
|
||||
val at := this.bytes.length as u32;
|
||||
this.bytes.put(made.span);
|
||||
this.at.append(at);
|
||||
return at;
|
||||
}
|
||||
|
||||
#! A function's code and its record of unwinding, each as an address past the
|
||||
#! image's base.
|
||||
struct runtime_function {
|
||||
var begin: u32;
|
||||
var end: u32;
|
||||
var unwind: u32;
|
||||
}
|
||||
|
||||
#! The table of each function's code and its record of unwinding, as `.pdata`
|
||||
#! holds it.
|
||||
pub struct function_table {
|
||||
var entries: vector<runtime_function>;
|
||||
}
|
||||
|
||||
#! Makes a table naming no functions.
|
||||
#! @returns The table.
|
||||
pub def function_table::new(): function_table {
|
||||
return function_table { entries = vector::<runtime_function>::new() };
|
||||
}
|
||||
|
||||
#! Names a function's code, which no other's overlaps, and its record.
|
||||
#! @param this The table.
|
||||
#! @param begin Where its code begins, past the image's base.
|
||||
#! @param end Where it ends.
|
||||
#! @param unwind Where its record is, past the image's base.
|
||||
pub def function_table::add(&mut this, begin: u32, end: u32, unwind: u32): void {
|
||||
this.entries.append(runtime_function { begin = begin, end = end, unwind = unwind });
|
||||
}
|
||||
|
||||
# Whether one function's code begins before another's.
|
||||
def begins_before(a: &runtime_function, b: &runtime_function): bool {
|
||||
return a.begin < b.begin;
|
||||
}
|
||||
|
||||
#! The table as `.pdata` holds it, sorted by where each function begins.
|
||||
#! @param this The table.
|
||||
#! @returns Its bytes.
|
||||
pub def function_table::bytes(&mut this): byte_writer {
|
||||
this.entries.sort_by(begins_before);
|
||||
var made := byte_writer::new();
|
||||
for &each in this.entries {
|
||||
made.u32(each.begin);
|
||||
made.u32(each.end);
|
||||
made.u32(each.unwind);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
Reference in new issue
Block a user