From 253eca39ed50ca937cb43f833a23a0441ccdc8d8 Mon Sep 17 00:00:00 2001 From: SoraKatadzuma Date: Mon, 26 Oct 2026 06:02:30 -0500 Subject: [PATCH] Initial Commit --- .editorconfig | 9 + .gitignore | 1 + CHANGELOG.md | 48 ++ LICENSE | 18 + README.md | 330 +++++++++++ amd64/assembly.sakura | 1069 +++++++++++++++++++++++++++++++++++ amd64/vector.sakura | 281 +++++++++ blossomfile | 73 +++ dwarf/dwarf.sakura | 141 +++++ dwarf/encoding.sakura | 127 +++++ dwarf/frames.sakura | 138 +++++ dwarf/lines.sakura | 168 ++++++ elf/elf.sakura | 481 ++++++++++++++++ pdb/digest.sakura | 110 ++++ pdb/lines.sakura | 250 ++++++++ pdb/msf.sakura | 224 ++++++++ pdb/pdb.sakura | 212 +++++++ pdb/symbols.sakura | 338 +++++++++++ pdb/types.sakura | 504 +++++++++++++++++ pdb/writer.sakura | 550 ++++++++++++++++++ pe/debug.sakura | 123 ++++ pe/pe.sakura | 310 ++++++++++ pe/unwind.sakura | 212 +++++++ tests/amd64/encoding.sakura | 927 ++++++++++++++++++++++++++++++ tests/dwarf/dwarf.sakura | 122 ++++ tests/elf/elf.sakura | 104 ++++ tests/main.sakura | 14 + tests/pdb/file.sakura | 138 +++++ tests/pdb/hashing.sakura | 51 ++ tests/pdb/lines.sakura | 54 ++ tests/pdb/records.sakura | 129 +++++ tests/pdb/support.sakura | 88 +++ tests/pe/pe.sakura | 162 ++++++ 33 files changed, 7506 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 amd64/assembly.sakura create mode 100644 amd64/vector.sakura create mode 100644 blossomfile create mode 100644 dwarf/dwarf.sakura create mode 100644 dwarf/encoding.sakura create mode 100644 dwarf/frames.sakura create mode 100644 dwarf/lines.sakura create mode 100644 elf/elf.sakura create mode 100644 pdb/digest.sakura create mode 100644 pdb/lines.sakura create mode 100644 pdb/msf.sakura create mode 100644 pdb/pdb.sakura create mode 100644 pdb/symbols.sakura create mode 100644 pdb/types.sakura create mode 100644 pdb/writer.sakura create mode 100644 pe/debug.sakura create mode 100644 pe/pe.sakura create mode 100644 pe/unwind.sakura create mode 100644 tests/amd64/encoding.sakura create mode 100644 tests/dwarf/dwarf.sakura create mode 100644 tests/elf/elf.sakura create mode 100644 tests/main.sakura create mode 100644 tests/pdb/file.sakura create mode 100644 tests/pdb/hashing.sakura create mode 100644 tests/pdb/lines.sakura create mode 100644 tests/pdb/records.sakura create mode 100644 tests/pdb/support.sakura create mode 100644 tests/pe/pe.sakura diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..c6c8b36 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cfecb95 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.blossom diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..cf36da4 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,48 @@ +# Changelog + +Each version's changes, newest first. Versions follow [semantic +versioning](https://semver.org). + +## Unreleased + +- The standard library is required as the package `sakura/std`, and each + library names the modules it reads: `std::io`, where `byte_writer` is now, + and, for `dwarf`, `pe`, and `pdb`, `std::containers`, where `interner` is. + +## 1.0.0 - Unreleased + +- The five libraries Sakura writes executables with, until now each a + repository of its own, gathered into one, each still a library of its own: + `binary::amd64`, `binary::elf`, `binary::dwarf`, `binary::pe`, and + `binary::pdb`, which were `malunal::amd64` and the rest. Their folders are + named for them alone, as `amd64/`, and their tests are in `tests/amd64/` and + the rest, sharing one `main`. A project requires the repository once and + names among its `deps` only the libraries it reads. +- What each held at 1.0.0 in its own repository, of 2026-10-24: + - **`amd64`**: the encoder of the Sakura compiler, moved out of its `native` + folder, with its tests. `assembly::rex` is public, so code beside the + library writes prefixes as it does, and the fields of `mem` and `assembly` + the compiler reads are public. + - **`elf`**: the ELF writer of the Sakura compiler, moved out of its `native` + folder, with tests. Its types lost their `elf_` prefixes, as `elf::image` + and `elf::segment`, a section's kind is an enum, and the machine an image + is for may be given. Writing the file is the caller's: the library gives + its bytes. + - **`dwarf`**: DWARF's numbers and encodings, moved out of the Sakura + compiler's DWARF writer, with tests: numbers, abbreviations, tables of + strings, the headers of units and lists, line programs and their table, and + the descriptions of frames. What the compiler says of its programs stays + the compiler's. + - **`pe`**: the PE writer of the Sakura compiler, moved out of its `native` + folder, with tests. Its types lost their `pe_` prefixes, as `pe::image`, + the subsystem may be given, and the exception and debug directories are + named besides the imports and their addresses. How functions unwind: + `unwind_info`, `unwind_table`, and `function_table`. The debug directory + naming a PDB file: `debug_entry`, `codeview_record`, and `stamp_codeview`, + and `section_headers`, which a PDB file keeps. Writing the file is the + caller's: the library gives its bytes. + - **`pdb`**: the first version, written for the Sakura compiler's Windows + executables: the MSF file and the streams naming the file, of types and + IDs, of modules, of names, and of globals and publics; CodeView's type + records and symbols; each module's lines, inlinees' lines, and inline + sites' annotations; and the GUID naming the file, made from what it holds. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b8bae39 --- /dev/null +++ b/LICENSE @@ -0,0 +1,18 @@ +MIT License + +Copyright (c) 2026 Malunal Studios, LLC. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..88cae13 --- /dev/null +++ b/README.md @@ -0,0 +1,330 @@ +# binary + +The libraries Sakura writes executables with: the instructions of a machine, +the files an operating system loads, and the debugging information debuggers +read. Each is a library of its own, reading only `std`: + +| Library | Writes | Folder | +|---|---|---| +| [`binary::amd64`](#binaryamd64) | AMD64's instructions, x86-64's | `amd64/` | +| [`binary::elf`](#binaryelf) | ELF files, Linux's executables | `elf/` | +| [`binary::dwarf`](#binarydwarf) | DWARF 5, Linux's debugging information | `dwarf/` | +| [`binary::pe`](#binarype) | PE files, Windows' executables | `pe/` | +| [`binary::pdb`](#binarypdb) | PDB files, Windows' debugging information | `pdb/` | + +The Sakura compiler writes its Linux executables with `amd64`, `elf`, and +`dwarf`, and its Windows executables with `amd64`, `pe`, and `pdb`. + +## Using it + +Require the repository once in your blossomfile, and name among a target's +`deps` only the libraries it reads: + +```yaml +requires: +- https://git.malunal.studio/sakura/std@v1.0.0 +- https://git.malunal.studio/sakura/binary@v1.0.0 + +targets: +- name: app + type: program + deps: [binary.amd64, binary.elf, std.io] +``` + +Each is imported by its own name, as `import binary::elf;`, and written by its +last, as `elf::image`. + +## `binary::amd64` + +Encodes AMD64's instructions. `assembly` writes each instruction's bytes one +after another, as an assembler does, and aims the jumps and calls it writes +once their targets are known. + +```sakura +import binary::amd64; + +# A function returning 42 where its argument in `rdi` is zero, else 7. +def answer(): assembly { + var code := assembly::new(); + code.push(reg::rbp); + code.mov(reg::rbp, reg::rsp); + code.mov_imm(reg::rax, 7); + code.test(reg::rdi, reg::rdi); + val skip := code.jcc(cc::not_equal); + code.mov_imm(reg::rax, 42); + code.land(skip); + code.leave(); + code.ret(); + return code; # Its bytes are `code.bytes`. +} +``` + +- **Registers and operations**, numbered as the hardware numbers them: `reg` + (`rax` to `r15`), the conditions `cc`, two-operand arithmetic `alu`, `shift`, + `unary`, and scalar SSE arithmetic `sse`. SSE registers are numbers of their + own, `xmm1` being 1. +- **Operands in memory**: `at(base, disp)`, `indexed(base, index, scale, + disp)`, `absolute(address)`, and, through `gs`, `own(offset)` and + `own_at(base)`. +- **`assembly`**: moves, loads, and stores of each width; arithmetic, + comparisons, shifts, multiplication, and division; atomics (`lock`, `xchg`, + `lock_xadd`, `lock_cmpxchg`, `mfence`); calls and jumps through registers and + memory; string instructions; SSE's scalar instructions; and vector + instructions, each a word naming its opcode, map, and prefix (`vec_paddb` and + the rest), written with SSE's encodings or, where `avx` is set, VEX's. +- **Jumps and calls**: `jmp`, `jcc`, and `call` give where their displacement + is; `land` aims one at the code written next and `patch` at any offset. + `fixup_list` keeps the displacements of calls to code placed later. + +`std::io` declares an `absolute` too, so a file importing both writes +`amd64::absolute`. + +## `binary::elf` + +Writes ELF files: an executable of segments loaded at fixed addresses, x86-64's +unless another machine is given, each described by a section of its name, with +its functions named in a symbol table so debuggers and disassemblers show them, +sections it holds but doesn't load, as notes and debugging information, and the +note naming its build. It also writes the file of an executable's debugging +information, which describes the executable's segments without holding them, +and gives the checksum a `.gnu_debuglink` names that file with. + +```sakura +import std::io; +import binary::elf; + +# An executable of the code given, loaded with the file's headers at 4 MiB. +def executable(code: span): byte_writer { + val headers := elf::headers_size(1, false); + var image := elf::image::new(0x400000 + headers); + image.segment(".text", 0x400000, 0, code, code.length, elf::readable | elf::executable, true); + image.symbols.append(elf::symbol { name = "_start", address = 0x400000 + headers, size = code.length }); + image.build_id(); + return image.file(); +} +``` + +- **`image`**: its entry, `machine` (62, x86-64, unless given), `segments`, + `symbols`, the `sections` it holds but doesn't load, and, where it is + dynamically linked (`linked`), where the loader's records are (`linking`). + `segment` adds a segment, `build_id` adds the note naming the build by hashes + of what it loads, and `file` gives the file's bytes. An image that is + `described` only describes its segments, as a file of debugging information + does, holding none of them. +- **`segment`**: a name, an address, its offset in the file, its bytes, and its + permissions, `readable`, `writable`, and `executable`. The first may begin with + the file's headers, `headers_size` of them. +- **`section`**: a name, its kind (`section_kind::progbits`, `note`, or + `nobits`), its bytes, alignment, flags (`merge`, `strings`), and the bytes of + each entry where it holds entries of a size. +- **`crc32`**: the checksum `.gnu_debuglink` names a file with, as zlib + computes it. + +## `binary::dwarf` + +Writes DWARF 5. It holds DWARF's numbers, as its standard names them, and the +encodings its sections are made of: numbers in bytes of seven bits, +abbreviations, tables of strings, the headers of units and of lists, line +programs and the table naming their files, and the descriptions of how frames +unwind. What a compiler says of its program, its types, functions, and +variables, is its own. + +```sakura +import std::io; +import binary::dwarf; + +# A line program of one function's code at an address, its prologue three +# bytes, and the table naming its file. +def lines(address: u64, size: u64): byte_writer { + var program := dwarf::line_program::new(); + program.begin(address); + program.row(0, 1, 2, 1, false); + program.row(3, 1, 3, 5, true); + program.end(size); + var strings := dwarf::string_table::new(); + var table := byte_writer::new(); + val paths: strview[1] = ["main.sakura"]; + dwarf::write_line_table(&mut table, "/src", paths[:], &mut strings, program.bytes.view()); + return table; # `.debug_line`, whose names are `strings.bytes`, `.debug_line_str`. +} +``` + +- **Numbers**: the tags of entries (`dw_tag_*`), their attributes (`dw_at_*`) + and forms (`dw_form_*`), the encodings of base types (`dw_ate_*`), the + operations of expressions (`dw_op_*`), the entries of lists (`dw_lle_*`, + `dw_rle_*`), the instructions of frames (`dw_cfa_*`) and of line programs + (`dw_lns_*`, `dw_lne_*`), and x86-64's registers as its System V ABI numbers + them (`amd64_registers`, `amd64_xmm0`, `amd64_rbp`, `amd64_rsp`, + `amd64_return_address`). +- **Encodings**: `uleb` and `sleb`; `abbreviation`; `string_table`, each string + once, by its offset; `begin_unit` and `begin_lists`, whose lengths + `byte_writer::end_length` writes. +- **Lines**: `line_program`, a sequence at a time, its rows written as the + changes from one to the next, and `write_line_table`, its header naming the + directories and files. +- **Frames**: `begin_cie`, `begin_fde`, and `end_entry`, and the instructions: + `cfa_advance`, `cfa_def_cfa`, `cfa_def_cfa_offset`, `cfa_def_cfa_register`, + `cfa_offset`, `cfa_same_value`, `cfa_remember_state`, and + `cfa_restore_state`. + +## `binary::pe` + +Writes PE files: an x86-64 executable run in a console unless another subsystem +is given, loaded at the address it was built for with nothing to relocate, its +sections each a page past the last, the tables the loader and debuggers read +named by the data directories, and its functions named in a COFF symbol table. +It also writes how each function unwinds, which Windows reads to unwind a +thread's stack, and the debug directory naming a PDB file, stamped with that +file's GUID once it is written. + +```sakura +import std::io; +import binary::pe; + +# An executable of the code given at 4 MiB plus a page, which pushes `rbp` and +# points it at its frame, so it unwinds through `rbp` however its stack moves. +def executable(code: span): byte_writer { + var image := pe::image::new(0x400000, 0x401000); + image.stack_reserve = 0x100000; + image.stack_commit = 0x1000; + image.section(".text", 0x401000, code, code.length, pe::readable | pe::executable); + + var info := pe::unwind_info::new(); + info.pushed(1, 5); # push rbp + info.framed(4, 5, 0); # mov rbp, rsp + var records := pe::unwind_table::new(); + val record := records.add(&info); + image.section(".xdata", 0x402000, records.bytes.view(), records.bytes.length, pe::readable); + var functions := pe::function_table::new(); + functions.add(0x1000, (0x1000 + code.length) as u32, 0x2000 + record); + val table := functions.bytes(); + image.section(".pdata", 0x403000, table.view(), table.length, pe::readable); + image.exceptions = pe::directory { address = 0x403000, size = table.length }; + return image.file(); +} +``` + +- **`image`**: its base and entry, `subsystem` (3, a console, unless given), + the bytes of the first thread's stack, its `sections`, its `symbols`, and the + data directories the loader and debuggers read: `imports`, `exceptions`, + `debug`, and `iat`. `section` adds a section and `file` gives the file's + bytes. A section holding nothing is left out, and one whose name is longer + than eight bytes is named in the table of strings. +- **`section`**: a name, an address, its bytes, and its permissions: + `readable`, `writable`, `executable`, and `discardable`, as debugging + information is. +- **Unwinding**: `unwind_info` lists what a prologue did, each at the offset + past its instruction: `pushed`, `allocated`, `framed`, and `saved`, written + the latest first, as Windows reads them. `unwind_table` holds the records, + each once, as `.xdata` does, and `function_table` each function's code and + record, sorted, as `.pdata` does. +- **The PDB file**: `debug_entry` adds an entry to the debug directory, whose + place in the file `image::file` writes, and `codeview_record` the record + naming the file, `RSDS`. Once the PDB file is written, `stamp_codeview` + writes its GUID into the executable's file, and `section_headers` gives the + headers a PDB file keeps. + +## `binary::pdb` + +Writes PDB files as Microsoft's linker writes them for an executable of +x86-64: CodeView's types and symbols, each module's lines and the functions +inlined into its code, the globals and publics a debugger finds a name by, all +held in an MSF file's streams. The file is named by a GUID made from what it +holds, so the same build writes the same file. + +```sakura +import std::io; +import binary::pdb; + +# A PDB file naming one function, `main`, 16 bytes of code at the start of the +# image's second section, whose headers are given, from line 3 of its file. +def describe(sections: span): pdb::writer { + var file := pdb::writer::new(); + file.sections.put(sections); + val arguments := file.types.arguments(span::::empty()); + val procedure := file.types.procedure(pdb::t_void, arguments, 0); + + var symbols := pdb::symbol_stream::new(); + symbols.object_name("main.obj"); + val at := symbols.procedure(true, "main", procedure, 2, 0, 16, 4, 15, pdb::proc_has_frame_pointer); + symbols.end(); + + var checksums := pdb::file_checksums::new(); + val source := checksums.file(file.names.offset_of("/src/main.c")); + var lines := pdb::function_lines::new(2, 0, 16); + lines.line(source, 0, 3, 1); + lines.finish(); + var subsections := byte_writer::new(); + pdb::subsection(&mut subsections, pdb::debug_s_lines, lines.bytes.view()); + pdb::subsection(&mut subsections, pdb::debug_s_file_checksums, checksums.bytes.view()); + + val files: strview[1] = ["/src/main.c"]; + val ours := file.add_module("main.obj", "main.obj", &mut symbols.bytes, &mut subsections, files[:]); + file.contribute(ours, 2, 0, 16, 0x60000020); + file.public("main", 2, 0, pdb::public_function | pdb::public_code); + file.procedure("main", ours, at, true); + return file; # `file.file()` gives its bytes, then `file.guid` names it. +} +``` + +The executable names the file by its path, GUID, and age in its debug +directory, which `binary::pe`'s `codeview_record` and `stamp_codeview` write. + +- **`writer`**: the file. Its `types` and `ids`, each a `type_table`; its + `names`, the table of strings lines name files in; the `sections` of the + image, as their headers are; its modules (`add_module`), the parts of + sections each is (`contribute`), its publics (`public`), and its globals + naming procedures (`procedure`). `file` lays the streams out in an MSF file + and names it by the MD5 of what it holds, which `guid` and `signature` then + give. +- **Types**: `type_table` holds records, each once but functions' IDs, hashed as + Microsoft's tools hash them, so a struct's declaration is found to its + definition: `pointer`, `array`, `arguments`, `procedure`, `structure`, + `union_of`, `enumeration`, `fields` of a `field_list` (going on in another + record where one can't hold it), and `function_id`. The simple types are + `t_void`, `t_int4`, and the rest, and `t_64p` makes a pointer of one. +- **Symbols**: `symbol_stream` writes a module's symbols: its name and + compiler, procedures, their frames, blocks, inline sites, and locals with + where each is over which code: `in_register`, `in_frame`, + `in_frame_throughout`, `part_in_register`, and `through_register`. A scope's + record names where it ends once `end` ends it. +- **Lines**: `file_checksums`, `function_lines`, and `inlinee_lines`, each the + contents of a subsection `subsection` writes, and `annotations`, the parts of + a procedure's code an inline site is and the line of each. +- **Numbers**: the kinds of records and symbols (`lf_*`, `s_*`), and + x86-64's registers as CodeView numbers them (`amd64_register`, `amd64_xmm`). + +## Writing the file + +Each library gives bytes and leaves writing them to a file to you, as a +compiler writing over an executable that is running has its own way to. + +## Testing + +From the repository's root, with `blossom test`, which runs each library's +tests as a program of its own, or one library's, as `amd64`'s: + +```sh +sakurac run --lib binary::amd64=amd64 tests/main.sakura tests/amd64/*.sakura +``` + +- `amd64`: each instruction's expected bytes were written by GNU as from the + Intel syntax beside it. +- `pdb`: the files the Sakura compiler writes with it were checked with LLVM's + `llvm-pdbutil`, which reads every stream of them, and `llvm-symbolizer`, + whose functions, files, lines, and inlined frames from the PDB file match + those from the executable's DWARF. + +## Adding a library + +An architecture or a format, as `aarch64` or Mach-O, is a folder of its own +beside the others, with: +- its files, each `module binary::;`; +- a target in the blossomfile, `binary.`, whose `root` is its folder; +- its tests in `tests//`, run as a test of their own with + `tests/main.sakura`; +- a line among the blossomfile's `exports`, and a row in the table above. + +## License + +MIT. See [LICENSE](LICENSE). diff --git a/amd64/assembly.sakura b/amd64/assembly.sakura new file mode 100644 index 0000000..9ac9715 --- /dev/null +++ b/amd64/assembly.sakura @@ -0,0 +1,1069 @@ +#! @file assembly.sakura +#! @author John Christman (sorakatadzuma@gmail.com) +#! @copyright Malunal Studios, LLC. +#! @license MIT +#! +#! Contains the encoder of AMD64's instructions, x86-64's: the registers, the +#! operands in memory, and `assembly`, which writes each instruction's bytes one +#! after another, with the jumps and calls it writes patched once their targets +#! are known. Every integer operation is on 64 bits unless it takes a width. +#! Registers are numbered as the hardware numbers them, so `reg::r8` is 8, and +#! SSE registers are numbers of their own in the same range, so `xmm1` is 1. + +import std::io; +module binary::amd64; + + +#! The general registers, as the hardware numbers them. +pub enum reg : u8 { + rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi, + r8, r9, r10, r11, r12, r13, r14, r15, + none, #! No base or index in a memory operand. +} + +#! The conditions of `jcc`, `setcc`, and `cmovcc`, as the hardware numbers +#! them. A condition's opposite is the one its lowest bit flipped gives. +pub enum cc : u8 { + overflow, no_overflow, below, above_equal, equal, not_equal, below_equal, above, + sign, no_sign, parity, no_parity, less, greater_equal, less_equal, greater, +} + +#! Arithmetic of two operands, as the hardware numbers it. +pub enum alu : u8 { add, bor, adc, sbb, band, sub, bxor, cmp } + +#! Shifts, as the hardware numbers them. +pub enum shift : u8 { rol = 0, ror = 1, shl = 4, shr = 5, sar = 7 } + +#! Operations of one operand, where multiplying and dividing use `rdx:rax`. +pub enum unary : u8 { bnot = 2, neg = 3, mul = 4, div = 6, idiv = 7 } + +#! Scalar double arithmetic, by the second byte of its opcode. +pub enum sse : u8 { sqrtsd = 0x51, addsd = 0x58, mulsd = 0x59, subsd = 0x5C, divsd = 0x5E } + +#! A memory operand, `[base + index * scale + disp]`, where either register +#! may be `none`. Without either, the displacement is an absolute address. +pub struct mem { + pub var base: reg; #! The base, or `none`. + pub var index: reg; #! The index, or `none`. + pub var scale: u8; #! What the index is multiplied by: 1, 2, 4, or 8. + pub var disp: i32; #! The displacement. + pub var own: bool = false; #! Whether it is within the running thread's own record, through `gs`. +} + +#! The memory at an offset from a register. +#! @param base The register. +#! @param disp The offset. +#! @returns The operand. +pub def at(base: reg, disp: i32): mem { + return mem { base = base, index = reg::none, scale = 1, disp = disp }; +} + +#! The memory at an offset from a register, plus another register times a scale. +#! @param base The register. +#! @param index The other register. +#! @param scale What it is multiplied by: 1, 2, 4, or 8. +#! @param disp The offset. +#! @returns The operand. +pub def indexed(base: reg, index: reg, scale: u8, disp: i32): mem { + return mem { base = base, index = index, scale = scale, disp = disp }; +} + +#! The memory at an address, which fits in 31 bits as every address of an +#! executable's image does. +#! @param address The address. +#! @returns The operand. +pub def absolute(address: u64): mem { + return mem { base = reg::none, index = reg::none, scale = 1, disp = address as i32 }; +} + +#! The bytes at an offset within the running thread's own record, which `gs` +#! points at, as each thread's base of `gs` is its own. +#! @param offset The offset. +#! @returns The memory. +pub def own(offset: u64): mem { + return mem { base = reg::none, index = reg::none, scale = 1, disp = offset as i32, own = true }; +} + +#! The bytes at the offset a register holds from where `gs` points, as Windows +#! reaches a slot of its record of the running thread. +#! @param base The register. +#! @returns The memory. +pub def own_at(base: reg): mem { + return mem { base = base, index = reg::none, scale = 1, disp = 0, own = true }; +} + +#! Whether a word is what a sign-extended 32-bit immediate holds. +#! @param value The word. +#! @returns Whether it is. +pub def fits_i32(value: u64): bool { + val signed := value as i64; + return signed >= -2147483648 && signed <= 2147483647; +} + +#! Machine code being made, a byte at a time, which a byte writer holds. +pub struct assembly : owning { + pub var bytes: byte_writer; #! The code. + pub var fixups: vector; #! Where `patch` wrote a displacement, then where it goes to, for each while `logging`. + pub var logging: bool; #! Whether `patch` notes what it writes in `fixups`. + pub var avx: bool; #! Whether SSE's instructions are written with VEX, as code for processors with AVX writes them, so none meets the upper half of a register holding 32 bytes. +} + +#! Makes empty code. +#! @returns The code. +pub def assembly::new(): assembly { + return assembly { bytes = byte_writer::new(), fixups = vector::::new() }; +} + +#! How many bytes the code holds, which is where the next begins. +#! @param this The code. +#! @returns How many. +pub val assembly::length: u64 { + get => this.bytes.length; +} + +#! Gives back the memory the code holds. +#! @param this The code. +pub def assembly::close(&mut this): void { + this.bytes.close(); + this.fixups.close(); +} + +#! Adds a byte. +#! @param this The code. +#! @param value The byte. +pub def assembly::byte(&mut this, value: u8): void { + this.bytes.byte(value); +} + +#! Adds a 16-bit word, least significant byte first, as x86-64 stores it. +#! @param this The code. +#! @param value The word. +pub def assembly::u16(&mut this, value: u64): void { + this.bytes.u16(value); +} + +#! Adds a 32-bit word, least significant byte first. +#! @param this The code. +#! @param value The word. +pub def assembly::u32(&mut this, value: u64): void { + this.bytes.u32(value); +} + +#! Adds a 64-bit word, least significant byte first. +#! @param this The code. +#! @param value The word. +pub def assembly::u64(&mut this, value: u64): void { + this.bytes.u64(value); +} + +#! Writes a 32-bit word over the four bytes at a place. +#! @param this The code. +#! @param at The place, whose bytes are held. +#! @param value The word. +pub def assembly::set_u32(&mut this, at: u64, value: u64): void { + this.bytes.set_u32(at, value); +} + +#! Writes a 64-bit word over the eight bytes at a place. +#! @param this The code. +#! @param at The place, whose bytes are held. +#! @param value The word. +pub def assembly::set_u64(&mut this, at: u64, value: u64): void { + this.bytes.set_u64(at, value); +} + +#! Reads the 64-bit word at a place. +#! @param this The code. +#! @param at The place, whose bytes are held. +#! @returns The word. +pub def assembly::get_u64(&this, at: u64): u64 { + return this.bytes.get_u64(at); +} + +#! Adds bytes. +#! @param this The code. +#! @param data The bytes. +pub def assembly::put(&mut this, data: span): void { + this.bytes.put(data); +} + +#! Adds zeros until the length is a multiple of an alignment. +#! @param this The code. +#! @param align The alignment. +#! @returns The length after. +pub def assembly::align_to(&mut this, align: u64): u64 { + return this.bytes.align_to(align); +} + +# Operands. + +#! Adds a REX prefix for the registers where one is needed, or where `force` +#! makes the byte registers `spl`, `bpl`, `sil`, and `dil` reachable. +#! @param this The code. +#! @param wide Whether the operation is on 64 bits. +#! @param r ModRM's `reg`: a register, or an opcode's extension. +#! @param index The index register, or `reg::none`. +#! @param base The base register, or `reg::none`. +#! @param force Whether it is written though no register needs it. +pub def assembly::rex(&mut this, wide: bool, r: u32, index: reg, base: reg, force: bool): void { + var rex: u32 = 0x40; + if wide { rex = rex | 0x08; } + if (r & 8) != 0 { rex = rex | 0x04; } + if index != reg::none && (index as u32 & 8) != 0 { rex = rex | 0x02; } + if base != reg::none && (base as u32 & 8) != 0 { rex = rex | 0x01; } + if rex != 0x40 || force { + this.byte(rex as u8); + } +} + +# The bits of ModRM's or SIB's scale. +def scale_bits(scale: u8): u32 { + return scale == 8 ? 3 : scale == 4 ? 2 : scale == 2 ? 1 : 0; +} + +# Encodes a memory operand: a base of `rsp` or `r12`, or any index, takes a SIB +# byte, and a base of `rbp` or `r13` always takes a displacement. +def assembly::modrm_mem(&mut this, r: u32, m: mem): void { + val index: u32 = m.index == reg::none ? 4 : (m.index as u32 & 7); + if m.base == reg::none { + this.byte((0x04 | ((r & 7) << 3)) as u8); + this.byte(((scale_bits(m.scale) << 6) | (index << 3) | 5) as u8); + this.u32(m.disp as u64); + return; + } + + val base := m.base as u32 & 7; + var mode: u32 = 2; + if m.disp == 0 && base != 5 { + mode = 0; + } else if m.disp >= -128 && m.disp <= 127 { + mode = 1; + } + if base == 4 || m.index != reg::none { + this.byte(((mode << 6) | ((r & 7) << 3) | 4) as u8); + this.byte(((scale_bits(m.scale) << 6) | (index << 3) | base) as u8); + } else { + this.byte(((mode << 6) | ((r & 7) << 3) | base) as u8); + } + if mode == 1 { + this.byte((m.disp as i64 & 0xFF) as u8); + } else if mode == 2 { + this.u32(m.disp as u64); + } +} + +# An instruction of a register, or an opcode extension, and a memory operand: +# prefix, REX, the opcode's bytes, and ModRM. A prefix of 0 is none. +def assembly::mem_op(&mut this, prefix: u8, wide: bool, op1: u8, op2: u8, count: u32, r: u32, m: mem, force: bool): void { + if m.own { + this.byte(0x65); + } + if prefix != 0 { + this.byte(prefix); + } + this.rex(wide, r, m.index, m.base, force); + this.byte(op1); + if count > 1 { this.byte(op2); } + this.modrm_mem(r, m); +} + +# The ModRM of two registers, or of an opcode extension and a register: `r` in +# its reg and `rm` in its rm. +def modrm_reg(r: u32, rm: u32): u8 { + return (0xC0 | ((r & 7) << 3) | (rm & 7)) as u8; +} + +# An instruction of two register operands, where `r` goes in ModRM's reg and +# `rm` in its rm. +def assembly::reg_op(&mut this, prefix: u8, wide: bool, op1: u8, op2: u8, count: u32, r: u32, rm: u32, force: bool): void { + if prefix != 0 { + this.byte(prefix); + } + this.rex(wide, r, reg::none, (rm & 15) as reg, force); + this.byte(op1); + if count > 1 { this.byte(op2); } + this.byte(modrm_reg(r, rm)); +} + +# Whether a register's low byte needs a REX prefix to be named, as `sil` does. +def byte_needs_rex(r: reg): bool { + return r == reg::rsp || r == reg::rbp || r == reg::rsi || r == reg::rdi; +} + + +# Moves. + +#! `mov dst, src` of 64 bits. +#! @param this The code. +#! @param dst The register written. +#! @param src The register read. +pub def assembly::mov(&mut this, dst: reg, src: reg): void { + this.reg_op(0, true, 0x89, 0, 1, src as u32, dst as u32, false); +} + +#! Loads a constant into a register, as briefly as it can be written. +#! @param this The code. +#! @param dst The register. +#! @param value The constant. +pub def assembly::mov_imm(&mut this, dst: reg, value: u64): void { + if value <= 0xFFFFFFFF { + # Writing 32 bits zeroes the upper half. + this.rex(false, 0, reg::none, dst, false); + this.byte((0xB8 + (dst as u32 & 7)) as u8); + this.u32(value); + return; + } + if (value as i64) >= -2147483648 && (value as i64) < 0 { + this.rex(true, 0, reg::none, dst, false); + this.byte(0xC7); + this.byte(modrm_reg(0, dst as u32)); + this.u32(value); + return; + } + this.rex(true, 0, reg::none, dst, false); + this.byte((0xB8 + (dst as u32 & 7)) as u8); + this.u64(value); +} + +#! Loads 1, 2, 4, or 8 bytes, extending them by their sign or with zeros. +#! @param this The code. +#! @param width How many bytes. +#! @param sign Whether they extend by their sign. +#! @param dst The register written. +#! @param src The memory read. +pub def assembly::load(&mut this, width: u64, sign: bool, dst: reg, src: mem): void { + val r := dst as u32; + if width == 1 { + this.mem_op(0, sign, 0x0F, sign ? 0xBE : 0xB6, 2, r, src, false); + } else if width == 2 { + this.mem_op(0, sign, 0x0F, sign ? 0xBF : 0xB7, 2, r, src, false); + } else if width == 4 { + # Reading 32 bits zeroes the upper half. + this.mem_op(0, sign, sign ? 0x63 : 0x8B, 0, 1, r, src, false); + } else { + this.mem_op(0, true, 0x8B, 0, 1, r, src, false); + } +} + +#! Stores the low 1, 2, 4, or 8 bytes of a register. +#! @param this The code. +#! @param width How many bytes. +#! @param dst The memory written. +#! @param src The register read. +pub def assembly::store(&mut this, width: u64, dst: mem, src: reg): void { + val r := src as u32; + if width == 1 { + this.mem_op(0, false, 0x88, 0, 1, r, dst, byte_needs_rex(src)); + } else if width == 2 { + this.mem_op(0x66, false, 0x89, 0, 1, r, dst, false); + } else if width == 4 { + this.mem_op(0, false, 0x89, 0, 1, r, dst, false); + } else { + this.mem_op(0, true, 0x89, 0, 1, r, dst, false); + } +} + +#! Stores a sign-extended 32-bit constant into 8 bytes. +#! @param this The code. +#! @param dst The memory written. +#! @param value The constant. +pub def assembly::store_imm(&mut this, dst: mem, value: i32): void { + this.mem_op(0, true, 0xC7, 0, 1, 0, dst, false); + this.u32(value as u64); +} + +#! Stores a constant into 1, 2, or 4 bytes, or a sign-extended 32-bit one +#! into 8. +#! @param this The code. +#! @param width How many bytes. +#! @param dst The memory written. +#! @param value The constant, of which the low `width` bytes are written. +pub def assembly::store_imm_width(&mut this, width: u64, dst: mem, value: u64): void { + if width == 1 { + this.mem_op(0, false, 0xC6, 0, 1, 0, dst, false); + this.byte((value & 0xFF) as u8); + } else if width == 2 { + this.mem_op(0x66, false, 0xC7, 0, 1, 0, dst, false); + this.u16(value); + } else if width == 4 { + this.mem_op(0, false, 0xC7, 0, 1, 0, dst, false); + this.u32(value); + } else { + this.store_imm(dst, value as i32); + } +} + +#! `lea dst, src`: the address of memory. +#! @param this The code. +#! @param dst The register written. +#! @param src The memory. +pub def assembly::lea(&mut this, dst: reg, src: mem): void { + this.mem_op(0, true, 0x8D, 0, 1, dst as u32, src, false); +} + +#! `push r`. +#! @param this The code. +#! @param r The register. +pub def assembly::push(&mut this, r: reg): void { + this.rex(false, 0, reg::none, r, false); + this.byte((0x50 + (r as u32 & 7)) as u8); +} + +#! `pop r`. +#! @param this The code. +#! @param r The register. +pub def assembly::pop(&mut this, r: reg): void { + this.rex(false, 0, reg::none, r, false); + this.byte((0x58 + (r as u32 & 7)) as u8); +} + +#! `cmovcc dst, src`: moves where the condition holds. +#! @param this The code. +#! @param condition The condition. +#! @param dst The register written. +#! @param src The register read. +pub def assembly::cmov(&mut this, condition: cc, dst: reg, src: reg): void { + this.reg_op(0, true, 0x0F, (0x40 + condition as u32) as u8, 2, dst as u32, src as u32, false); +} + + +# Arithmetic. + +#! `op dst, src` of two registers. +#! @param this The code. +#! @param op The operation. +#! @param dst The register written, and read first. +#! @param src The register read second. +pub def assembly::alu(&mut this, op: alu, dst: reg, src: reg): void { + this.reg_op(0, true, (op as u32 * 8 + 1) as u8, 0, 1, src as u32, dst as u32, false); +} + +#! Writes a constant an instruction takes: a byte where it is small, and else +#! 32 bits. +#! @param this The code. +#! @param value The constant. +#! @param small Whether it is written as a byte. +def assembly::immediate(&mut this, value: i32, small: bool): void { + if small { + this.byte((value as i64 & 0xFF) as u8); + } else { + this.u32(value as u64); + } +} + +#! Whether a constant fits a sign-extended byte. +#! @param value The constant. +#! @returns Whether it does. +def fits_byte(value: i32): bool { + return value >= -128 && value <= 127; +} + +#! `op dst, value` of a register and a sign-extended 32-bit constant. +#! @param this The code. +#! @param op The operation. +#! @param dst The register. +#! @param value The constant. +pub def assembly::alu_imm(&mut this, op: alu, dst: reg, value: i32): void { + val small := fits_byte(value); + this.reg_op(0, true, small ? 0x83 : 0x81, 0, 1, op as u32, dst as u32, false); + this.immediate(value, small); +} + +#! `op dst, [src]` of a register and the 8 bytes in memory. +#! @param this The code. +#! @param op The operation. +#! @param dst The register, written but by `cmp`. +#! @param src The memory. +pub def assembly::alu_load(&mut this, op: alu, dst: reg, src: mem): void { + this.mem_op(0, true, (op as u32 * 8 + 3) as u8, 0, 1, dst as u32, src, false); +} + +#! `op [dst], value` of the 8 bytes in memory and a sign-extended 32-bit +#! constant. +#! @param this The code. +#! @param op The operation. +#! @param dst The memory, written but by `cmp`. +#! @param value The constant. +pub def assembly::alu_mem_imm(&mut this, op: alu, dst: mem, value: i32): void { + val small := fits_byte(value); + this.mem_op(0, true, small ? 0x83 : 0x81, 0, 1, op as u32, dst, false); + this.immediate(value, small); +} + +#! `op [dst], src` of the 8 bytes in memory and a register. +#! @param this The code. +#! @param op The operation. +#! @param dst The memory, written but by `cmp`. +#! @param src The register. +pub def assembly::alu_store(&mut this, op: alu, dst: mem, src: reg): void { + this.mem_op(0, true, (op as u32 * 8 + 1) as u8, 0, 1, src as u32, dst, false); +} + +#! Makes the instruction after it, which writes memory, one step no other +#! processor sees halfway. +#! @param this The code. +pub def assembly::lock(&mut this): void { + this.byte(0xF0); +} + +#! Swaps the low 1, 2, 4, or 8 bytes of a register with memory, in one step no +#! other processor sees halfway, as `xchg` always is. Of fewer than 8, the rest +#! of the register is left as it was. +#! @param this The code. +#! @param width How many bytes. +#! @param m The memory. +#! @param r The register. +pub def assembly::xchg(&mut this, width: u64, m: mem, r: reg): void { + this.sized(width, false, false, 0x86, 0x87, r, m); +} + +#! `lock xadd [m], r`: adds the low 1, 2, 4, or 8 bytes of a register to memory, +#! in one step, leaving in them what the memory held. +#! @param this The code. +#! @param width How many bytes. +#! @param m The memory. +#! @param r The register. +pub def assembly::lock_xadd(&mut this, width: u64, m: mem, r: reg): void { + this.sized(width, true, true, 0xC0, 0xC1, r, m); +} + +#! `lock cmpxchg [m], r`: where the memory holds what the low 1, 2, 4, or 8 +#! bytes of `rax` do, writes those of a register there and sets the zero flag, +#! and otherwise loads what it holds into them, in one step. +#! @param this The code. +#! @param width How many bytes. +#! @param m The memory. +#! @param r The register. +pub def assembly::lock_cmpxchg(&mut this, width: u64, m: mem, r: reg): void { + this.sized(width, true, true, 0xB0, 0xB1, r, m); +} + +#! `mfence`: no read or write after it happens before every one before it has, +#! as every processor sees them. +#! @param this The code. +pub def assembly::mfence(&mut this): void { + this.byte(0x0F); + this.byte(0xAE); + this.byte(0xF0); +} + +# An instruction of a register and memory of 1, 2, 4, or 8 bytes, its opcode +# `op8` for a byte and `op` otherwise, after 0x0F where it is extended, written +# as GNU as writes them: the operand size before `lock`. +def assembly::sized(&mut this, width: u64, locked: bool, extended: bool, op8: u8, op: u8, r: reg, m: mem): void { + if width == 2 { + this.byte(0x66); + } + if locked { + this.lock(); + } + val code := width == 1 ? op8 : op; + val force := width == 1 && byte_needs_rex(r); + if extended { + this.mem_op(0, width == 8, 0x0F, code, 2, r as u32, m, force); + } else { + this.mem_op(0, width == 8, code, 0, 1, r as u32, m, force); + } +} + +#! `test a, b`. +#! @param this The code. +#! @param a One register. +#! @param b The other. +pub def assembly::test(&mut this, a: reg, b: reg): void { + this.reg_op(0, true, 0x85, 0, 1, b as u32, a as u32, false); +} + +#! `test [m], r` of the 8 bytes in memory and a register. +#! @param this The code. +#! @param m The memory. +#! @param r The register. +pub def assembly::test_mem(&mut this, m: mem, r: reg): void { + this.mem_op(0, true, 0x85, 0, 1, r as u32, m, false); +} + +#! `imul dst, src`. +#! @param this The code. +#! @param dst The register written, and read first. +#! @param src The register read second. +pub def assembly::imul(&mut this, dst: reg, src: reg): void { + this.reg_op(0, true, 0x0F, 0xAF, 2, dst as u32, src as u32, false); +} + +#! `imul dst, [src]`. +#! @param this The code. +#! @param dst The register written, and read first. +#! @param src The memory read second. +pub def assembly::imul_load(&mut this, dst: reg, src: mem): void { + this.mem_op(0, true, 0x0F, 0xAF, 2, dst as u32, src, false); +} + +#! `imul dst, src, value`. +#! @param this The code. +#! @param dst The register written. +#! @param src The register read. +#! @param value The constant it is multiplied by. +pub def assembly::imul_imm(&mut this, dst: reg, src: reg, value: i32): void { + val small := fits_byte(value); + this.reg_op(0, true, small ? 0x6B : 0x69, 0, 1, dst as u32, src as u32, false); + this.immediate(value, small); +} + +#! Copies the low 1, 2, or 4 bytes of a register into another, extending them +#! by their sign or with zeros, or all 8 bytes for a width of 8. +#! @param this The code. +#! @param width How many bytes. +#! @param sign Whether they extend by their sign. +#! @param dst The register written. +#! @param src The register read. +pub def assembly::extend(&mut this, width: u64, sign: bool, dst: reg, src: reg): void { + if width == 1 { + this.reg_op(0, sign, 0x0F, sign ? 0xBE : 0xB6, 2, dst as u32, src as u32, byte_needs_rex(src)); + } else if width == 2 { + this.reg_op(0, sign, 0x0F, sign ? 0xBF : 0xB7, 2, dst as u32, src as u32, false); + } else if width == 4 { + # Writing 32 bits zeroes the upper half. + this.reg_op(0, sign, sign ? 0x63 : 0x8B, 0, 1, dst as u32, src as u32, false); + } else if dst != src { + this.mov(dst, src); + } +} + +#! An operation of one register, as `neg`, `not`, `mul`, `div`, and `idiv`. +#! @param this The code. +#! @param op The operation. +#! @param r The register. +pub def assembly::unary(&mut this, op: unary, r: reg): void { + this.reg_op(0, true, 0xF7, 0, 1, op as u32, r as u32, false); +} + +#! `bsr dst, src`: the index of the highest bit of `src` that is 1. Where `src` is +#! zero, it sets the zero flag and leaves `dst` as it was. +#! @param this The code. +#! @param dst The register written. +#! @param src The register read. +pub def assembly::bsr(&mut this, dst: reg, src: reg): void { + this.reg_op(0, true, 0x0F, 0xBD, 2, dst as u32, src as u32, false); +} + +#! `bsf dst, src`: the index of the lowest bit of `src` that is 1, as `bsr` says +#! nothing of zero. +#! @param this The code. +#! @param dst The register written. +#! @param src The register read. +pub def assembly::bsf(&mut this, dst: reg, src: reg): void { + this.reg_op(0, true, 0x0F, 0xBC, 2, dst as u32, src as u32, false); +} + +#! `bswap r`: the register's bytes in the reverse order. +#! @param this The code. +#! @param r The register. +pub def assembly::bswap(&mut this, r: reg): void { + this.rex(true, 0, reg::none, r, false); + this.byte(0x0F); + this.byte((0xC8 | (r as u32 & 7)) as u8); +} + +#! Replaces the double at `[rsp]` with what is left dividing it by the double at +#! `[rsp + 8]`, as C's `fmod` does, through the x87's `fprem`, which is exact. +#! Clobbers `rax`. +#! @param this The code. +pub def assembly::fmod_stack(&mut this): void { + val code: u8[21] = [ + 0xDD, 0x44, 0x24, 0x08, # fld qword [rsp + 8] + 0xDD, 0x04, 0x24, # fld qword [rsp] + 0xD9, 0xF8, # fprem + 0xDF, 0xE0, # fnstsw ax + 0xF6, 0xC4, 0x04, # test ah, 4 + 0x75, 0xF7, # jnz back to fprem + 0xDD, 0x1C, 0x24, # fstp qword [rsp] + 0xDD, 0xD8, # fstp st(0) + ]; + this.put(code[:]); +} + +#! `cqo`: extends `rax` by its sign into `rdx`. +#! @param this The code. +pub def assembly::cqo(&mut this): void { + this.byte(0x48); + this.byte(0x99); +} + +#! Shifts a register by `cl`. +#! @param this The code. +#! @param op The shift. +#! @param r The register. +pub def assembly::shift_cl(&mut this, op: shift, r: reg): void { + this.reg_op(0, true, 0xD3, 0, 1, op as u32, r as u32, false); +} + +#! Shifts a register by a constant. +#! @param this The code. +#! @param op The shift. +#! @param r The register. +#! @param count How far. +pub def assembly::shift_imm(&mut this, op: shift, r: reg, count: u8): void { + this.reg_op(0, true, 0xC1, 0, 1, op as u32, r as u32, false); + this.byte(count); +} + +#! Sets a register to whether a condition holds: its low byte, then extended +#! with zeros. +#! @param this The code. +#! @param condition The condition. +#! @param r The register. +pub def assembly::setcc(&mut this, condition: cc, r: reg): void { + val force := byte_needs_rex(r); + this.reg_op(0, false, 0x0F, (0x90 + condition as u32) as u8, 2, 0, r as u32, force); + this.reg_op(0, false, 0x0F, 0xB6, 2, r as u32, r as u32, force); +} + + +# Control. + +#! `ret`. +#! @param this The code. +pub def assembly::ret(&mut this): void { + this.byte(0xC3); +} + +#! `leave`: puts back the stack and frame pointers of the caller. +#! @param this The code. +pub def assembly::leave(&mut this): void { + this.byte(0xC9); +} + +#! `syscall`. +#! @param this The code. +pub def assembly::syscall(&mut this): void { + this.byte(0x0F); + this.byte(0x05); +} + +#! `rep movsb`: copies `rcx` bytes from `rsi` to `rdi`. +#! @param this The code. +pub def assembly::rep_movsb(&mut this): void { + this.byte(0xF3); + this.byte(0xA4); +} + +#! `rep stosb`: stores `al` into `rcx` bytes from `rdi`. +#! @param this The code. +pub def assembly::rep_stosb(&mut this): void { + this.byte(0xF3); + this.byte(0xAA); +} + +#! `rep movsq`: copies `rcx` words from `rsi` to `rdi`. +#! @param this The code. +pub def assembly::rep_movsq(&mut this): void { + this.byte(0xF3); + this.byte(0x48); + this.byte(0xA5); +} + +#! `rep stosq`: stores `rax` into `rcx` words from `rdi`. +#! @param this The code. +pub def assembly::rep_stosq(&mut this): void { + this.byte(0xF3); + this.byte(0x48); + this.byte(0xAB); +} + +#! `ud2`, which stops the program. +#! @param this The code. +pub def assembly::ud2(&mut this): void { + this.byte(0x0F); + this.byte(0x0B); +} + +#! `call r`. +#! @param this The code. +#! @param r The register holding the address called. +pub def assembly::call_reg(&mut this, r: reg): void { + this.reg_op(0, false, 0xFF, 0, 1, 2, r as u32, false); +} + +#! `call [m]`. +#! @param this The code. +#! @param m The memory holding the address called. +pub def assembly::call_mem(&mut this, m: mem): void { + this.mem_op(0, false, 0xFF, 0, 1, 2, m, false); +} + +#! `jmp r`. +#! @param this The code. +#! @param r The register holding the address jumped to. +pub def assembly::jmp_reg(&mut this, r: reg): void { + this.reg_op(0, false, 0xFF, 0, 1, 4, r as u32, false); +} + +#! `jmp` to the address a word of memory holds, as an entry of a table. +#! @param this The code. +#! @param m The memory. +pub def assembly::jmp_mem(&mut this, m: mem): void { + this.mem_op(0, false, 0xFF, 0, 1, 4, m, false); +} + +#! `call` of a place not yet known, patched once it is. +#! @param this The code. +#! @returns Where its 32-bit displacement is, for `patch`. +pub def assembly::call(&mut this): u64 { + this.byte(0xE8); + return this.displacement(); +} + +#! `jmp` to a place not yet known, patched once it is. +#! @param this The code. +#! @returns Where its 32-bit displacement is, for `patch`. +pub def assembly::jmp(&mut this): u64 { + this.byte(0xE9); + return this.displacement(); +} + +#! `jcc` to a place not yet known, patched once it is. +#! @param this The code. +#! @param condition Where it jumps. +#! @returns Where its 32-bit displacement is, for `patch`. +pub def assembly::jcc(&mut this, condition: cc): u64 { + this.byte(0x0F); + this.byte((0x80 + condition as u32) as u8); + return this.displacement(); +} + +#! A 32-bit displacement written as zero, to be patched once where it goes is +#! known. +#! @param this The code. +#! @returns Where it is, for `patch`. +def assembly::displacement(&mut this): u64 { + val at := this.bytes.length; + this.u32(0); + return at; +} + +#! Points the displacement at a place to a place of the same code. +#! @param this The code. +#! @param at Where the displacement is. +#! @param target The place it goes to. +pub def assembly::patch(&mut this, at: u64, target: u64): void { + val relative := (target as i64 - (at as i64 + 4)) as u64; + this.set_u32(at, relative & 0xFFFFFFFF); + if this.logging { + this.fixups.append(at); + this.fixups.append(target); + } +} + +#! Points the displacement at a place to where the code ends now, which is +#! where what is written next lands. +#! @param this The code. +#! @param at Where the displacement is. +pub def assembly::land(&mut this, at: u64): void { + this.patch(at, this.bytes.length); +} + +#! Places that name what is not placed yet: where each is, and what it names, +#! by a number, each written once every one is placed. +pub struct fixup_list { + pub var at: vector; #! Where each place is, which moves with what holds it. + var target: vector; #! What each names. +} + +#! Provides read only access to how many places a list holds. +#! @param this The list. +#! @returns How many. +pub val fixup_list::length: u64 { + get => this.at.length; +} + +#! Makes a list of no places. +#! @param alloc The optional allocator for the memory backing of the list. +#! @returns The list. +pub def fixup_list::new(alloc: maybe<&mut allocator>): fixup_list { + return fixup_list { at = vector::::new(alloc), target = vector::::new(alloc) }; +} + +#! Adds a place naming what is not placed yet. +#! @param this The list. +#! @param at Where the place is. +#! @param target What it names. +pub def fixup_list::add(&mut this, at: u64, target: u32): void { + this.at.append(at); + this.target.append(target); +} + +#! Points each displacement of code at where what it names was placed. +#! @param this The list. +#! @param text The code. +#! @param placed Where each thing named was placed in the code. +pub def fixup_list::resolve(&this, text: &mut assembly, placed: span): void { + for k in 0..this.at.length { + text.patch(this.at[k], placed[this.target[k]]); + } +} + +#! Writes over each 32-bit place where what it names was placed. +#! @param this The list. +#! @param out The bytes holding the places. +#! @param placed Where each thing named was placed. +pub def fixup_list::resolve_words(&this, out: &mut byte_writer, placed: span): void { + for k in 0..this.at.length { + out.set_u32(this.at[k], placed[this.target[k]] as u64); + } +} + + +# Scalar floating point. + +# An SSE instruction of two registers, `op r, rm`: with its mandatory prefix +# and the opcode after `0F`, or with VEX where the code is written so, naming +# `r` again as the operand the instruction merges where `merges`. +def assembly::sse_reg(&mut this, prefix: u8, wide: bool, opcode: u8, r: u32, rm: u32, merges: bool): void { + if !this.avx { + this.reg_op(prefix, wide, 0x0F, opcode, 2, r, rm, false); + return; + } + this.vex_opcode(sse_word(prefix, wide, opcode), false, r, merges ? r : 0, 0, rm); + this.byte(modrm_reg(r, rm)); +} + +# An SSE instruction of a register and memory, `op r, m`, as `sse_reg`. +def assembly::sse_mem(&mut this, prefix: u8, wide: bool, opcode: u8, r: u32, m: mem, merges: bool): void { + if !this.avx { + this.mem_op(prefix, wide, 0x0F, opcode, 2, r, m, false); + return; + } + val x: u32 = m.index != reg::none ? m.index as u32 : 0; + val b: u32 = m.base != reg::none ? m.base as u32 : 0; + this.vex_opcode(sse_word(prefix, wide, opcode), false, r, merges ? r : 0, x, b); + this.modrm_mem(r, m); +} + +# The word of an SSE instruction, as `vector.sakura` writes them, from its +# mandatory prefix, whether it is 64 bits wide, and its opcode after `0F`. +def sse_word(prefix: u8, wide: bool, opcode: u8): u32 { + val pp: u32 = prefix == 0x66 ? 1 : prefix == 0xF3 ? 2 : prefix == 0xF2 ? 3 : 0; + return (wide ? vec_wide : 0) | (pp << 16) | 0x100 | opcode as u32; +} + +#! `movq xmm, r`: the bits of a register into an SSE register. +#! @param this The code. +#! @param xmm The SSE register. +#! @param r The general register. +pub def assembly::movq_to_xmm(&mut this, xmm: u8, r: reg): void { + this.sse_reg(0x66, true, 0x6E, xmm as u32, r as u32, false); +} + +#! `movq r, xmm`: the bits of an SSE register into a register. +#! @param this The code. +#! @param r The general register. +#! @param xmm The SSE register. +pub def assembly::movq_from_xmm(&mut this, r: reg, xmm: u8): void { + this.sse_reg(0x66, true, 0x7E, xmm as u32, r as u32, false); +} + +#! `movaps dst, src`: copies an SSE register. +#! @param this The code. +#! @param dst The register written. +#! @param src The register read. +pub def assembly::movaps(&mut this, dst: u8, src: u8): void { + this.sse_reg(0, false, 0x28, dst as u32, src as u32, false); +} + +#! `movsd xmm, [m]`: loads a double. +#! @param this The code. +#! @param xmm The register written. +#! @param src The memory read. +pub def assembly::movsd_load(&mut this, xmm: u8, src: mem): void { + this.sse_mem(0xF2, false, 0x10, xmm as u32, src, false); +} + +#! `movsd [m], xmm`: stores a double. +#! @param this The code. +#! @param dst The memory written. +#! @param xmm The register read. +pub def assembly::movsd_store(&mut this, dst: mem, xmm: u8): void { + this.sse_mem(0xF2, false, 0x11, xmm as u32, dst, false); +} + +#! An arithmetic of doubles: `op dst, src`. +#! @param this The code. +#! @param op The operation. +#! @param dst The register written, and read first. +#! @param src The register read second. +pub def assembly::sse(&mut this, op: sse, dst: u8, src: u8): void { + this.sse_reg(0xF2, false, op as u8, dst as u32, src as u32, true); +} + +#! An arithmetic of doubles with one in memory: `op dst, [src]`. +#! @param this The code. +#! @param op The operation. +#! @param dst The register written, and read first. +#! @param src The memory read second. +pub def assembly::sse_load(&mut this, op: sse, dst: u8, src: mem): void { + this.sse_mem(0xF2, false, op as u8, dst as u32, src, true); +} + +#! `xorpd dst, src`. +#! @param this The code. +#! @param dst The register written, and read first. +#! @param src The register read second. +pub def assembly::xorpd(&mut this, dst: u8, src: u8): void { + this.sse_reg(0x66, false, 0x57, dst as u32, src as u32, true); +} + +#! `ucomisd a, b`: compares doubles, unordered where either is not a number. +#! @param this The code. +#! @param a One register. +#! @param b The other. +pub def assembly::ucomisd(&mut this, a: u8, b: u8): void { + this.sse_reg(0x66, false, 0x2E, a as u32, b as u32, false); +} + +#! `cvtsi2sd xmm, r`: the double a signed word is. +#! @param this The code. +#! @param xmm The SSE register written. +#! @param r The general register read. +pub def assembly::cvtsi2sd(&mut this, xmm: u8, r: reg): void { + this.sse_reg(0xF2, true, 0x2A, xmm as u32, r as u32, true); +} + +#! `cvttsd2si r, xmm`: the signed word a double is, toward zero. +#! @param this The code. +#! @param r The general register written. +#! @param xmm The SSE register read. +pub def assembly::cvttsd2si(&mut this, r: reg, xmm: u8): void { + this.sse_reg(0xF2, true, 0x2C, r as u32, xmm as u32, false); +} + +#! `cvtsd2ss dst, src`: a double rounded to a float. +#! @param this The code. +#! @param dst The register written. +#! @param src The register read. +pub def assembly::cvtsd2ss(&mut this, dst: u8, src: u8): void { + this.sse_reg(0xF2, false, 0x5A, dst as u32, src as u32, true); +} + +#! `cvtss2sd dst, src`: a float widened to a double. +#! @param this The code. +#! @param dst The register written. +#! @param src The register read. +pub def assembly::cvtss2sd(&mut this, dst: u8, src: u8): void { + this.sse_reg(0xF3, false, 0x5A, dst as u32, src as u32, true); +} + +#! `movss xmm, [m]`: loads a float. +#! @param this The code. +#! @param xmm The register written. +#! @param src The memory read. +pub def assembly::movss_load(&mut this, xmm: u8, src: mem): void { + this.sse_mem(0xF3, false, 0x10, xmm as u32, src, false); +} + +#! `movss [m], xmm`: stores a float. +#! @param this The code. +#! @param dst The memory written. +#! @param xmm The register read. +pub def assembly::movss_store(&mut this, dst: mem, xmm: u8): void { + this.sse_mem(0xF3, false, 0x11, xmm as u32, dst, false); +} diff --git a/amd64/vector.sakura b/amd64/vector.sakura new file mode 100644 index 0000000..5434dc6 --- /dev/null +++ b/amd64/vector.sakura @@ -0,0 +1,281 @@ +#! @file vector.sakura +#! @author John Christman (sorakatadzuma@gmail.com) +#! @copyright Malunal Studios, LLC. +#! @license MIT +#! +#! Contains the encodings of vector instructions: SSE's, on 16 bytes, written +#! with a mandatory prefix and an opcode map before the opcode, and AVX's, of +#! the same opcodes, written with a VEX prefix, which names a third register and +#! may work on 32 bytes. Asking the processor which of them it has, `cpuid` and +#! `xgetbv`, and `vzeroupper`, which ends working on 32 bytes, are here too. +#! +#! An instruction is a word: its opcode in the low byte, its map above it (1 for +#! `0F`, 2 for `0F 38`, and 3 for `0F 3A`), its mandatory prefix above that (0 +#! for none, 1 for `66`, 2 for `F3`, and 3 for `F2`), as VEX numbers both, and +#! VEX's `W` at bit 24. + +module binary::amd64; + + +#! Moving 16 or 32 bytes. +pub const vec_movdqu_load: u32 = 0x02016F; #! `movdqu x, m`. +pub const vec_movdqu_store: u32 = 0x02017F; #! `movdqu m, x`. +pub const vec_movdqa: u32 = 0x01016F; #! `movdqa x, x`. +pub const vec_movd_load: u32 = 0x01016E; #! `movd x, m32`. + +#! Integer arithmetic, by the width of each lane. +pub const vec_paddb: u32 = 0x0101FC; +pub const vec_paddw: u32 = 0x0101FD; +pub const vec_paddd: u32 = 0x0101FE; +pub const vec_paddq: u32 = 0x0101D4; +pub const vec_psubb: u32 = 0x0101F8; +pub const vec_psubw: u32 = 0x0101F9; +pub const vec_psubd: u32 = 0x0101FA; +pub const vec_psubq: u32 = 0x0101FB; +pub const vec_pmullw: u32 = 0x0101D5; +pub const vec_pmulld: u32 = 0x010240; #! SSE4.1. +pub const vec_pand: u32 = 0x0101DB; +pub const vec_pandn: u32 = 0x0101DF; +pub const vec_por: u32 = 0x0101EB; +pub const vec_pxor: u32 = 0x0101EF; +pub const vec_pcmpeqb: u32 = 0x010174; +pub const vec_pcmpeqw: u32 = 0x010175; +pub const vec_pcmpeqd: u32 = 0x010176; +pub const vec_pcmpeqq: u32 = 0x010229; #! SSE4.1. +pub const vec_pcmpgtb: u32 = 0x010164; +pub const vec_pcmpgtw: u32 = 0x010165; +pub const vec_pcmpgtd: u32 = 0x010166; +pub const vec_pcmpgtq: u32 = 0x010237; #! SSE4.2. +pub const vec_pminub: u32 = 0x0101DA; +pub const vec_pmaxub: u32 = 0x0101DE; +pub const vec_pminsw: u32 = 0x0101EA; +pub const vec_pmaxsw: u32 = 0x0101EE; +pub const vec_pminsb: u32 = 0x010238; #! SSE4.1, as the rest of the least and greatest. +pub const vec_pmaxsb: u32 = 0x01023C; +pub const vec_pminuw: u32 = 0x01023A; +pub const vec_pmaxuw: u32 = 0x01023E; +pub const vec_pminsd: u32 = 0x010239; +pub const vec_pmaxsd: u32 = 0x01023D; +pub const vec_pminud: u32 = 0x01023B; +pub const vec_pmaxud: u32 = 0x01023F; +pub const vec_pabsb: u32 = 0x01021C; #! SSSE3, as the rest of the absolute values. +pub const vec_pabsw: u32 = 0x01021D; +pub const vec_pabsd: u32 = 0x01021E; +pub const vec_psllw: u32 = 0x0101F1; #! Shifts by the count in the low word of a register. +pub const vec_pslld: u32 = 0x0101F2; +pub const vec_psllq: u32 = 0x0101F3; +pub const vec_psrlw: u32 = 0x0101D1; +pub const vec_psrld: u32 = 0x0101D2; +pub const vec_psrlq: u32 = 0x0101D3; +pub const vec_psraw: u32 = 0x0101E1; +pub const vec_psrad: u32 = 0x0101E2; +pub const vec_packsswb: u32 = 0x010163; +pub const vec_pmovmskb: u32 = 0x0101D7; #! A general register's bits of each byte's top bit. +pub const vec_punpcklqdq: u32 = 0x01016C; + +#! Real arithmetic: of `f32`s, and of `f64`s with the prefix `66`, which these +#! add. +pub const vec_addps: u32 = 0x000158; +pub const vec_subps: u32 = 0x00015C; +pub const vec_mulps: u32 = 0x000159; +pub const vec_divps: u32 = 0x00015E; +pub const vec_minps: u32 = 0x00015D; +pub const vec_maxps: u32 = 0x00015F; +pub const vec_sqrtps: u32 = 0x000151; +pub const vec_andps: u32 = 0x000154; +pub const vec_andnps: u32 = 0x000155; +pub const vec_orps: u32 = 0x000156; +pub const vec_xorps: u32 = 0x000157; +pub const vec_cmpps: u32 = 0x0001C2; #! With a predicate. +pub const vec_movmskps: u32 = 0x000150; #! A general register's bits of each lane's sign. +pub const vec_roundps: u32 = 0x010308; #! SSE4.1, with how it rounds; `roundpd` is `09`. +pub const vec_roundpd: u32 = 0x010309; +pub const vec_doubles: u32 = 0x010000; #! The prefix making an `f32` operation of `f64`s. + +#! AVX's alone. +pub const vec_vfmadd231ps: u32 = 0x0102B8; #! FMA3: `x = a * b + x`; `pd` is the same with `W`. +pub const vec_vinsertf128: u32 = 0x010318; #! The upper half of a register from another's lower, or memory. +pub const vec_wide: u32 = 0x1000000; #! VEX's `W`. + +# The byte of each mandatory prefix. +const pp_bytes: u8[4] = [0, 0x66, 0xF3, 0xF2]; + +# Writes a mandatory prefix, REX where the registers need one, the escape and +# map of an instruction, and its opcode. +def assembly::packed_opcode(&mut this, op: u32, r: u32, index: reg, base: reg): void { + val pp := (op >> 16) & 3; + if pp != 0 { + this.byte(pp_bytes[pp as u64]); + } + this.rex((op & vec_wide) != 0, r, index, base, false); + this.byte(0x0F); + val map := (op >> 8) & 3; + if map == 2 { + this.byte(0x38); + } else if map == 3 { + this.byte(0x3A); + } + this.byte((op & 0xFF) as u8); +} + +#! An SSE instruction of two registers, `op dst, src`, on 16 bytes. A general +#! register is named by its number, as `pmovmskb` writes one. Where the code is +#! written with VEX, it is VEX's form, which names `dst` again as the operand +#! read first, but for an instruction of one operand. +#! @param this The code. +#! @param op The instruction. +#! @param dst The register in ModRM's reg. +#! @param src The register in its rm. +pub def assembly::packed(&mut this, op: u32, dst: u32, src: u32): void { + if this.avx { + this.vex(op, false, dst, vec_unary(op) ? 0 : dst, src); + return; + } + this.packed_opcode(op, dst, reg::none, (src & 15) as reg); + this.byte(modrm_reg(dst, src)); +} + +#! An SSE instruction of a register and memory, `op r, m`, as a load or store, +#! with VEX where the code is written so. +#! @param this The code. +#! @param op The instruction. +#! @param r The register. +#! @param m The memory. +pub def assembly::packed_mem(&mut this, op: u32, r: u32, m: mem): void { + if this.avx { + this.vex_mem(op, false, r, m); + return; + } + this.packed_opcode(op, r, m.index, m.base); + this.modrm_mem(r, m); +} + +#! An SSE instruction of two registers and a byte, `op dst, src, imm`. +#! @param this The code. +#! @param op The instruction. +#! @param dst The register in ModRM's reg. +#! @param src The register in its rm. +#! @param imm The byte. +pub def assembly::packed_imm(&mut this, op: u32, dst: u32, src: u32, imm: u8): void { + this.packed(op, dst, src); + this.byte(imm); +} + +# Whether an instruction reads only the operand in ModRM's rm, as a move, a +# square root, rounding, an absolute value, or gathering a mask's bits, so VEX +# names no other register. +def vec_unary(op: u32): bool { + val code := op & 0xFF; + val map := (op >> 8) & 3; + if map == 1 { + return code == 0x6F || code == 0x7F || code == 0x6E || code == 0x51 || code == 0xD7 || code == 0x50 || code == 0x28; + } + if map == 2 { + return code == 0x1C || code == 0x1D || code == 0x1E; + } + return code == 0x08 || code == 0x09; +} + +# Writes a VEX prefix and an instruction's opcode: the two-byte form where the +# map is `0F`, `W` is clear, and neither the index nor the base needs REX's bits, +# and the three-byte form otherwise. `v` is the register VEX names, or 0 where +# the instruction names none, which VEX writes as all ones. +def assembly::vex_opcode(&mut this, op: u32, long: bool, r: u32, v: u32, x: u32, b: u32): void { + val pp := (op >> 16) & 3; + val map := (op >> 8) & 3; + val w: u32 = (op & vec_wide) != 0 ? 1 : 0; + val length: u32 = long ? 4 : 0; + val last := ((~v & 15) << 3) | length | pp; + val not_r := ((r >> 3) & 1) ^ 1; + if map == 1 && w == 0 && (x & 8) == 0 && (b & 8) == 0 { + this.byte(0xC5); + this.byte(((not_r << 7) | last) as u8); + } else { + this.byte(0xC4); + this.byte(((not_r << 7) | ((((x >> 3) & 1) ^ 1) << 6) | ((((b >> 3) & 1) ^ 1) << 5) | map) as u8); + this.byte(((w << 7) | last) as u8); + } + this.byte((op & 0xFF) as u8); +} + +#! An AVX instruction of three registers, `op dst, v, src`, on 32 bytes where +#! `long` and 16 otherwise. One of two names no `v`, which is given as 0. +#! @param this The code. +#! @param op The instruction. +#! @param long Whether it works on 32 bytes. +#! @param dst The register in ModRM's reg. +#! @param v The register VEX names. +#! @param src The register in ModRM's rm. +pub def assembly::vex(&mut this, op: u32, long: bool, dst: u32, v: u32, src: u32): void { + this.vex_opcode(op, long, dst, v, 0, src); + this.byte(modrm_reg(dst, src)); +} + +#! An AVX instruction of three registers and a byte, `op dst, v, src, imm`. +#! @param this The code. +#! @param op The instruction. +#! @param long Whether it works on 32 bytes. +#! @param dst The register in ModRM's reg. +#! @param v The register VEX names, or 0. +#! @param src The register in ModRM's rm. +#! @param imm The byte. +pub def assembly::vex_imm(&mut this, op: u32, long: bool, dst: u32, v: u32, src: u32, imm: u8): void { + this.vex(op, long, dst, v, src); + this.byte(imm); +} + +#! An AVX instruction of a register and memory, `op r, m`, as a load or store. +#! @param this The code. +#! @param op The instruction. +#! @param long Whether it works on 32 bytes. +#! @param r The register. +#! @param m The memory. +pub def assembly::vex_mem(&mut this, op: u32, long: bool, r: u32, m: mem): void { + val x: u32 = m.index != reg::none ? m.index as u32 : 0; + val b: u32 = m.base != reg::none ? m.base as u32 : 0; + this.vex_opcode(op, long, r, 0, x, b); + this.modrm_mem(r, m); +} + +#! An AVX instruction of a register, memory, and a byte, naming a third +#! register too, `op r, v, m, imm`, as `vinsertf128`. +#! @param this The code. +#! @param op The instruction. +#! @param long Whether it works on 32 bytes. +#! @param r The register in ModRM's reg. +#! @param v The register VEX names, or 0. +#! @param m The memory. +#! @param imm The byte. +pub def assembly::vex_mem_imm(&mut this, op: u32, long: bool, r: u32, v: u32, m: mem, imm: u8): void { + val x: u32 = m.index != reg::none ? m.index as u32 : 0; + val b: u32 = m.base != reg::none ? m.base as u32 : 0; + this.vex_opcode(op, long, r, v, x, b); + this.modrm_mem(r, m); + this.byte(imm); +} + +#! `cpuid`: what the processor is, as the leaf in `eax` asks, into `eax`, +#! `ebx`, `ecx`, and `edx`. +#! @param this The code. +pub def assembly::cpuid(&mut this): void { + this.byte(0x0F); + this.byte(0xA2); +} + +#! `xgetbv`: the extended state the system keeps, as `ecx` names it, into +#! `edx:eax`. +#! @param this The code. +pub def assembly::xgetbv(&mut this): void { + this.byte(0x0F); + this.byte(0x01); + this.byte(0xD0); +} + +#! `vzeroupper`: clears the upper halves of the vector registers, which the code +#! after working on 32 bytes asks, so SSE's instructions after it run at speed. +#! @param this The code. +pub def assembly::vzeroupper(&mut this): void { + this.byte(0xC5); + this.byte(0xF8); + this.byte(0x77); +} diff --git a/blossomfile b/blossomfile new file mode 100644 index 0000000..694c49d --- /dev/null +++ b/blossomfile @@ -0,0 +1,73 @@ +name: binary +gpid: malunal +semv: 1.0.0 + +requires: +- https://git.malunal.studio/sakura/std@v1.0.0 + +# Each library is one of its own, in the folder named for it, and reads only +# std's modules it lists. A project names among its deps only those it reads. +targets: +- name: binary.amd64 + type: library + root: ./amd64 + deps: [std.io] + +- name: binary.elf + type: library + root: ./elf + deps: [std.io] + +- name: binary.dwarf + type: library + root: ./dwarf + deps: [std.containers, std.io] + +- name: binary.pe + type: library + root: ./pe + deps: [std.containers, std.io] + +- name: binary.pdb + type: library + root: ./pdb + deps: [std.containers, std.io] + +# Each library's tests are a program of their own, reading only it. +tests: +- name: binary.amd64.tests + type: program + runs: machine + deps: [binary.amd64, std.io, std.testing] + srcs: [tests/main.sakura, tests/amd64/*.sakura] + +- name: binary.elf.tests + type: program + runs: machine + deps: [binary.elf, std.io, std.testing] + srcs: [tests/main.sakura, tests/elf/*.sakura] + +- name: binary.dwarf.tests + type: program + runs: machine + deps: [binary.dwarf, std.io, std.testing] + srcs: [tests/main.sakura, tests/dwarf/*.sakura] + +- name: binary.pe.tests + type: program + runs: machine + deps: [binary.pe, std.io, std.testing] + srcs: [tests/main.sakura, tests/pe/*.sakura] + +- name: binary.pdb.tests + type: program + runs: machine + deps: [binary.pdb, std.io, std.testing] + srcs: [tests/main.sakura, tests/pdb/*.sakura] + +exports: +- binary.amd64 +- binary.elf +- binary.dwarf +- binary.pe +- binary.pdb diff --git a/dwarf/dwarf.sakura b/dwarf/dwarf.sakura new file mode 100644 index 0000000..e55a776 --- /dev/null +++ b/dwarf/dwarf.sakura @@ -0,0 +1,141 @@ +#! @file dwarf.sakura +#! @author John Christman (sorakatadzuma@gmail.com) +#! @copyright Malunal Studios, LLC. +#! @license MIT +#! +#! Contains the numbers of DWARF 5, as its standard names them: the tags of +#! entries, their attributes and the forms those are written in, the encodings +#! of base types, the operations of expressions, the entries of lists of +#! locations and ranges, the instructions of frames and of line programs, and +#! how x86-64's registers are numbered, as its System V ABI numbers them. + +module binary::dwarf; + + +# Tags. +pub const dw_tag_array_type: u64 = 0x01; +pub const dw_tag_enumeration_type: u64 = 0x04; +pub const dw_tag_formal_parameter: u64 = 0x05; +pub const dw_tag_lexical_block: u64 = 0x0b; +pub const dw_tag_member: u64 = 0x0d; +pub const dw_tag_pointer_type: u64 = 0x0f; +pub const dw_tag_reference_type: u64 = 0x10; +pub const dw_tag_compile_unit: u64 = 0x11; +pub const dw_tag_structure_type: u64 = 0x13; +pub const dw_tag_union_type: u64 = 0x17; +pub const dw_tag_inlined_subroutine: u64 = 0x1d; +pub const dw_tag_subrange_type: u64 = 0x21; +pub const dw_tag_base_type: u64 = 0x24; +pub const dw_tag_enumerator: u64 = 0x28; +pub const dw_tag_subprogram: u64 = 0x2e; +pub const dw_tag_variable: u64 = 0x34; +pub const dw_tag_unspecified_type: u64 = 0x3b; + +# Attributes. +pub const dw_at_location: u64 = 0x02; +pub const dw_at_name: u64 = 0x03; +pub const dw_at_byte_size: u64 = 0x0b; +pub const dw_at_stmt_list: u64 = 0x10; +pub const dw_at_low_pc: u64 = 0x11; +pub const dw_at_high_pc: u64 = 0x12; +pub const dw_at_language: u64 = 0x13; +pub const dw_at_comp_dir: u64 = 0x1b; +pub const dw_at_const_value: u64 = 0x1c; +pub const dw_at_inline: u64 = 0x20; +pub const dw_at_producer: u64 = 0x25; +pub const dw_at_abstract_origin: u64 = 0x31; +pub const dw_at_calling_convention: u64 = 0x36; +pub const dw_at_count: u64 = 0x37; +pub const dw_at_data_member_location: u64 = 0x38; +pub const dw_at_decl_file: u64 = 0x3a; +pub const dw_at_decl_line: u64 = 0x3b; +pub const dw_at_encoding: u64 = 0x3e; +pub const dw_at_external: u64 = 0x3f; +pub const dw_at_frame_base: u64 = 0x40; +pub const dw_at_type: u64 = 0x49; +pub const dw_at_ranges: u64 = 0x55; +pub const dw_at_call_column: u64 = 0x57; +pub const dw_at_call_file: u64 = 0x58; +pub const dw_at_call_line: u64 = 0x59; + +# Forms. +pub const dw_form_addr: u64 = 0x01; +pub const dw_form_data2: u64 = 0x05; +pub const dw_form_data8: u64 = 0x07; +pub const dw_form_data1: u64 = 0x0b; +pub const dw_form_sdata: u64 = 0x0d; +pub const dw_form_strp: u64 = 0x0e; +pub const dw_form_udata: u64 = 0x0f; +pub const dw_form_ref4: u64 = 0x13; +pub const dw_form_sec_offset: u64 = 0x17; +pub const dw_form_exprloc: u64 = 0x18; +pub const dw_form_flag_present: u64 = 0x19; +pub const dw_form_line_strp: u64 = 0x1f; + +# Encodings of base types. +pub const dw_ate_boolean: u8 = 0x02; +pub const dw_ate_float: u8 = 0x04; +pub const dw_ate_signed: u8 = 0x05; +pub const dw_ate_unsigned: u8 = 0x07; + +# Operations of expressions. +pub const dw_op_deref: u8 = 0x06; +pub const dw_op_constu: u8 = 0x10; +pub const dw_op_reg0: u8 = 0x50; +pub const dw_op_breg0: u8 = 0x70; +pub const dw_op_fbreg: u8 = 0x91; +pub const dw_op_piece: u8 = 0x93; +pub const dw_op_call_frame_cfa: u8 = 0x9c; +pub const dw_op_implicit_value: u8 = 0x9e; +pub const dw_op_stack_value: u8 = 0x9f; + +# Entries of lists of locations. +pub const dw_lle_end_of_list: u8 = 0x00; +pub const dw_lle_start_length: u8 = 0x08; + +# Instructions of frames. +pub const dw_cfa_advance_loc: u8 = 0x40; +pub const dw_cfa_offset: u8 = 0x80; +pub const dw_cfa_advance_loc1: u8 = 0x02; +pub const dw_cfa_advance_loc2: u8 = 0x03; +pub const dw_cfa_advance_loc4: u8 = 0x04; +pub const dw_cfa_same_value: u8 = 0x08; +pub const dw_cfa_remember_state: u8 = 0x0a; +pub const dw_cfa_restore_state: u8 = 0x0b; +pub const dw_cfa_def_cfa: u8 = 0x0c; +pub const dw_cfa_def_cfa_register: u8 = 0x0d; +pub const dw_cfa_def_cfa_offset: u8 = 0x0e; + +# Instructions of line programs. +pub const dw_lns_copy: u8 = 0x01; +pub const dw_lns_advance_pc: u8 = 0x02; +pub const dw_lns_advance_line: u8 = 0x03; +pub const dw_lns_set_file: u8 = 0x04; +pub const dw_lns_set_column: u8 = 0x05; +pub const dw_lns_set_prologue_end: u8 = 0x0a; +pub const dw_lne_end_sequence: u8 = 0x01; +pub const dw_lne_set_address: u8 = 0x02; + +# Entries of lists of ranges. +pub const dw_rle_end_of_list: u8 = 0x00; +pub const dw_rle_start_length: u8 = 0x07; + +# Languages, calling conventions, inlining, and units. +pub const dw_lang_c_plus_plus_14: u64 = 0x21; +pub const dw_cc_nocall: u8 = 0x03; +pub const dw_inl_inlined: u8 = 0x01; +pub const dw_ut_compile: u8 = 0x01; + +# Registers, as x86-64's System V ABI numbers them for DWARF. + +#! The number of each general register, by how x86-64 encodes it: `rax`, `rcx`, +#! `rdx`, `rbx`, `rsp`, `rbp`, `rsi`, `rdi`, then `r8` to `r15`. +pub const amd64_registers: u8[16] = [0, 2, 1, 3, 7, 6, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15]; + +#! The number of `xmm0`, which the other SSE registers follow. +pub const amd64_xmm0: u8 = 17; + +#! The numbers of `rbp` and `rsp`, and of the column of the return address. +pub const amd64_rbp: u64 = 6; +pub const amd64_rsp: u64 = 7; +pub const amd64_return_address: u64 = 16; diff --git a/dwarf/encoding.sakura b/dwarf/encoding.sakura new file mode 100644 index 0000000..09b7b7c --- /dev/null +++ b/dwarf/encoding.sakura @@ -0,0 +1,127 @@ +#! @file encoding.sakura +#! @author John Christman (sorakatadzuma@gmail.com) +#! @copyright Malunal Studios, LLC. +#! @license MIT +#! +#! Contains how DWARF writes what its sections hold: numbers in as few bytes of +#! seven bits as hold them, the abbreviations entries are read by, the tables of +#! strings entries name, and the headers units and lists begin with. Every +#! length is of 32 bits, as DWARF's 32-bit format writes them. + +import std::containers; +import std::io; +module binary::dwarf; + + +#! Adds a number in as few bytes of seven bits as hold it, the lowest first. +#! @param out Where. +#! @param value The number. +pub def uleb(out: &mut byte_writer, value: u64): void { + var left := value; + while true { + val low := (left & 0x7F) as u8; + left = left >> 7; + if left == 0 { + out.byte(low); + return; + } + out.byte(low | 0x80); + } +} + +#! Adds a signed number in as few bytes of seven bits as hold it, the lowest +#! first. +#! @param out Where. +#! @param value The number. +pub def sleb(out: &mut byte_writer, value: i64): void { + var left := value; + while true { + val low := (left & 0x7F) as u8; + left = left >> 7; + val negative := (low & 0x40) != 0; + if (left == 0 && !negative) || (left == -1 && negative) { + out.byte(low); + return; + } + out.byte(low | 0x80); + } +} + +#! Writes an abbreviation: its code, its tag, whether it has children, then +#! each attribute and its form, then two zeros. A table of them ends with a +#! zero after the last. +#! @param out The section. +#! @param code Its code, which entries name it by. +#! @param tag Its tag. +#! @param children Whether its entries have children. +#! @param pairs Each attribute, then its form. +pub def abbreviation(out: &mut byte_writer, code: u64, tag: u64, children: bool, pairs: span): void { + uleb(out, code); + uleb(out, tag); + out.byte(children ? 1 : 0); + for &each in pairs { + uleb(out, each); + } + out.byte(0); + out.byte(0); +} + +#! A table of strings, as `.debug_str` and `.debug_line_str` are: each string +#! once, ending with a zero, where entries name it by its offset. +pub struct string_table { + pub var bytes: byte_writer; #! The table, as its section holds it. + var held: interner; #! The strings, by ID. + var at: vector; #! Where each is in the table, by its ID. +} + +#! Makes a table holding no strings. +#! @returns The table. +pub def string_table::new(): string_table { + return string_table { bytes = byte_writer::new(), held = interner::::new(), at = vector::::new() }; +} + +#! Where a string is in the table, adding it where it is new. +#! @param this The table. +#! @param text The string. +#! @returns Its offset. +pub def string_table::offset_of(&mut this, text: strview): u32 { + # A string new to the table is given the next ID, which has no place yet. + val id := this.held.intern_view(text); + if id < this.at.length { + return this.at[id]; + } + val at := this.bytes.length as u32; + this.bytes.put_terminated(text); + this.at.append(at); + return at; +} + +#! Begins a unit of `.debug_info`: its length, written once it ends, DWARF's +#! version, 5, the unit's type, the size of an address, and where its +#! abbreviations begin. +#! @param out The section. +#! @param kind The unit's type, as `dw_ut_compile`. +#! @param address_size The bytes of an address, 8 for x86-64. +#! @param abbreviations Where its abbreviations begin in `.debug_abbrev`. +#! @returns Where its length is, which `byte_writer::end_length` writes. +pub def begin_unit(out: &mut byte_writer, kind: u8, address_size: u8, abbreviations: u64): u64 { + val length := out.begin_length(); + out.u16(5); + out.byte(kind); + out.byte(address_size); + out.u32(abbreviations); + return length; +} + +#! Begins a table of lists of ranges or of locations, as `.debug_rnglists` and +#! `.debug_loclists` hold, with addresses of 8 bytes and no table of offsets. +#! @param out Where. +#! @returns Where its length is, which `byte_writer::end_length` writes. +pub def begin_lists(out: &mut byte_writer): u64 { + val length := out.begin_length(); + out.u16(5); + out.byte(8); + out.byte(0); + out.u32(0); + return length; +} diff --git a/dwarf/frames.sakura b/dwarf/frames.sakura new file mode 100644 index 0000000..536718d --- /dev/null +++ b/dwarf/frames.sakura @@ -0,0 +1,138 @@ +#! @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); +} diff --git a/dwarf/lines.sakura b/dwarf/lines.sakura new file mode 100644 index 0000000..6b04721 --- /dev/null +++ b/dwarf/lines.sakura @@ -0,0 +1,168 @@ +#! @file lines.sakura +#! @author John Christman (sorakatadzuma@gmail.com) +#! @copyright Malunal Studios, LLC. +#! @license MIT +#! +#! Contains the writer of line tables: a line program, a sequence of rows each +#! naming where in the source the code from an offset is from, written as the +#! changes from one row to the next, and the header naming the directories and +#! files the rows number. + +import std::io; +module binary::dwarf; + + +#! A line program being written, a sequence at a time. +pub struct line_program { + pub var bytes: byte_writer; #! The program. + var file: u64; #! The file of the last row, numbered from 1. + var line: i64; #! Its line. + var column: u64; #! Its column. + var at: u64; #! Its offset from where the sequence begins. +} + +#! Makes a program with no sequences. +#! @returns The program. +pub def line_program::new(): line_program { + return line_program { bytes = byte_writer::new(), file = 1, line = 1 }; +} + +#! Begins a sequence, of the code at an address: its rows follow, then `end`. +#! @param this The program. +#! @param address Where the code begins. +pub def line_program::begin(&mut this, address: u64): void { + this.bytes.byte(0); + uleb(&mut this.bytes, 9); + this.bytes.byte(dw_lne_set_address); + this.bytes.u64(address); + this.file = 1; + this.line = 1; + this.column = 0; + this.at = 0; +} + +#! Adds a row: the code from an offset is from a place in a file, and where the +#! function's prologue ends, where it is the first past it. +#! @param this The program. +#! @param offset Where the code is, from where the sequence begins. +#! @param file The file, numbered from 1. +#! @param line Its line, from 1. +#! @param column Its column, from 1, or 0 for none. +#! @param prologue_end Whether the row is the first past the prologue. +pub def line_program::row(&mut this, offset: u64, file: u64, line: u64, column: u64, prologue_end: bool): void { + if file != this.file { + this.bytes.byte(dw_lns_set_file); + uleb(&mut this.bytes, file); + this.file = file; + } + if column != this.column { + this.bytes.byte(dw_lns_set_column); + uleb(&mut this.bytes, column); + this.column = column; + } + if line as i64 != this.line { + this.bytes.byte(dw_lns_advance_line); + sleb(&mut this.bytes, line as i64 - this.line); + this.line = line as i64; + } + if offset > this.at { + this.bytes.byte(dw_lns_advance_pc); + uleb(&mut this.bytes, offset - this.at); + this.at = offset; + } + if prologue_end { + this.bytes.byte(dw_lns_set_prologue_end); + } + this.bytes.byte(dw_lns_copy); +} + +#! Ends a sequence, where its code ends. +#! @param this The program. +#! @param size The bytes of the code. +pub def line_program::end(&mut this, size: u64): void { + if size > this.at { + this.bytes.byte(dw_lns_advance_pc); + uleb(&mut this.bytes, size - this.at); + } + this.bytes.byte(0); + uleb(&mut this.bytes, 1); + this.bytes.byte(dw_lne_end_sequence); +} + +#! Writes a line table: its header, naming the directories and the files, then +#! the program. The directories are the one the build was in, which relative +#! paths are within, then the folder of each absolute path, which names its +#! file by its last part. The file numbered 0 is the one numbered 1, as DWARF 5 +#! numbers the unit's main file both ways. Each name is in the line table's +#! strings. +#! @param out The section. +#! @param directory The directory the build was in. +#! @param paths The path of each file, as the program numbers them from 1. +#! @param strings The table of the line table's strings, `.debug_line_str`. +#! @param program The program. +pub def write_line_table(out: &mut byte_writer, directory: strview, paths: span, strings: &mut string_table, program: span): void { + val unit := out.begin_length(); + out.u16(5); + out.byte(8); + out.byte(0); + val length := out.begin_length(); + out.byte(1); # The least bytes an instruction takes, + out.byte(1); # the operations of one, + out.byte(1); # and every row begins a statement. + out.byte(0xFB); # The least line advanced by a special opcode, -5, + out.byte(14); # how many lines they advance, + out.byte(13); # and the first of them. + val lengths: u8[12] = [0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1]; + out.put(lengths[:]); + + var directories := vector::::new(); + var held_in := vector::::new(); + directories.append(directory); + for &text in paths { + held_in.append(0); + continue if text.length == 0 || text[0] != '/' as u8; + var slash := text.length - 1; + while text[slash] != '/' as u8 { + slash--; + } + val parent := text.substr(0, slash > 0 ? slash : 1); + var d: u64 = 1; + while d < directories.length && directories[d] != parent { + d++; + } + if d == directories.length { + directories.append(parent); + } + held_in[held_in.length - 1] = d; + } + out.byte(1); + uleb(out, 1); + uleb(out, dw_form_line_strp); + uleb(out, directories.length); + for &each in directories { + out.u32(strings.offset_of(each) as u64); + } + + out.byte(2); + uleb(out, 1); + uleb(out, dw_form_line_strp); + uleb(out, 2); + uleb(out, dw_form_udata); + uleb(out, paths.length + 1); + for i in 0..paths.length + 1 { + val k := i == 0 ? 0 : i - 1; + val text := paths[k]; + var name := text; + val d := held_in[k]; + if d != 0 { + val skip := directories[d].length + (directories[d].length > 1 ? 1 : 0); + name = text.substr(skip, text.length - skip); + } + out.u32(strings.offset_of(name) as u64); + uleb(out, d); + } + out.end_length(length); + + out.put(program); + out.end_length(unit); +} diff --git a/elf/elf.sakura b/elf/elf.sakura new file mode 100644 index 0000000..29dfb5f --- /dev/null +++ b/elf/elf.sakura @@ -0,0 +1,481 @@ +#! @file elf.sakura +#! @author John Christman (sorakatadzuma@gmail.com) +#! @copyright Malunal Studios, LLC. +#! @license MIT +#! +#! Contains the writer of ELF files: an executable of segments loaded at fixed +#! addresses, x86-64's unless another machine is given, with a section +#! describing each, the functions named in a symbol table so debuggers and +#! disassemblers show them, sections held but not loaded, as notes and +#! debugging information are, and the note naming its build. It also writes the +#! file of an executable's debugging information, which describes the +#! executable's segments without holding them, and gives the checksum a link +#! to that file is written with. + +import std::io; +module binary::elf; + + +#! The permissions of a segment. +pub const readable: u32 = 4; +pub const writable: u32 = 2; +pub const executable: u32 = 1; + +# The sizes of the file header, a program header, a section header, and a +# symbol. +const header_bytes: u64 = 64; +const program_bytes: u64 = 56; +const section_bytes: u64 = 64; +const symbol_bytes: u64 = 24; + +#! The types of sections: those a file holds but does not load, as notes, and +#! those a file describing an executable's segments places where they are. +pub enum section_kind : u32 { + progbits = 1, #! Bytes of the program's own meaning. + note = 7, #! Notes, as the one naming a build. + nobits = 8, #! Bytes the file does not hold. +} + +#! The flags of a section of strings, each ending with a zero, which a linker +#! may merge. +pub const merge: u64 = 0x10; +pub const strings: u64 = 0x20; + +#! The bytes of a note naming a build, by a hash of what it loads. +pub const build_id_bytes: u64 = 20; + +#! A loaded segment, which is also described by a section of its name. +pub struct segment { + pub var name: strview; #! Of its section, as `.text`. + pub var address: u64; + pub var offset: u64; #! In the file, which is the address less a whole number of pages. + pub var data: span; #! What it holds. + pub var size: u64; #! Of what the file holds of it. + pub var flags: u32; #! As `readable`, `writable`, and `executable`. + pub var headers: bool; #! Whether it begins with the file's headers, before its data. +} + +#! A function, which debuggers and disassemblers show by name. +pub struct symbol { + pub var name: strview; + pub var address: u64; + pub var size: u64; +} + +#! A section the file holds but does not load, as a note. +pub struct section { + pub var name: strview; + pub var kind: section_kind; + pub var data: byte_writer; + pub var align: u64; + pub var flags: u64; #! As `merge` and `strings`. + pub var entry: u64; #! The bytes of each entry, where it holds entries of a size. +} + +#! A section's header. +struct header { + var name: u64; #! Where its name begins in the table of section names. + var kind: u64; + var flags: u64; + var address: u64; + var offset: u64; #! In the file. + var size: u64; + var link: u64; + var info: u64; + var align: u64; + var entry: u64; #! The bytes of each entry, where it holds entries of a size. +} + +#! What makes an executable dynamically linked: the path of the loader the +#! system runs it with, and the dynamic section telling the loader what to load +#! and where to write the addresses it finds, each at an address within its +#! first segment. +pub struct linking { + pub var interpreter: u64; + pub var interpreter_size: u64; + pub var section: u64; + pub var section_size: u64; + pub var strings: u64; #! The names the loader reads, which the symbols name into. + pub var strings_size: u64; + pub var symbols: u64; + pub var symbols_size: u64; +} + +#! An executable to write. +pub struct image { + pub var entry: u64; + pub var machine: u16 = 62; #! As ELF numbers it: x86-64 unless given. + pub var segments: vector; + pub var symbols: vector; + pub var sections: vector
; #! Those held but not loaded. + pub var linked: bool; #! Whether it is dynamically linked, by `dynamic`. + pub var dynamic: linking; + pub var described: bool; #! Whether the file only describes the segments, as debugging information does, holding none of them. +} + +# The program headers a dynamically linked executable has beside its segments: +# those of the headers themselves, the loader, and the dynamic section. +const dynamic_headers: u64 = 3; + +#! The bytes of an executable's headers, which its first segment begins with. +#! @param segments How many segments it has. +#! @param dynamic Whether it is dynamically linked. +#! @returns How many. +pub def headers_size(segments: u64, dynamic: bool): u64 { + return header_bytes + (segments + (dynamic ? dynamic_headers : 0)) * program_bytes; +} + +#! Makes an executable starting at an address, with nothing in it yet. +#! @param entry The address. +#! @returns The executable. +pub def image::new(entry: u64): image { + return image { + entry = entry, + segments = vector::::new(), + symbols = vector::::new(), + sections = vector::
::new(), + dynamic = linking {} + }; +} + +#! Adds a segment. +#! @param this The executable. +#! @param name The name of its section. +#! @param address Where it is loaded. +#! @param offset Where it is in the file. +#! @param data What it holds. +#! @param size How many bytes. +#! @param flags Its permissions. +#! @param headers Whether it begins with the file's headers. +pub def image::segment( + &mut this, + name: strview, + address: u64, + offset: u64, + data: span, + size: u64, + flags: u32, + headers: bool +): void { + this.segments.append(segment { + name = name, address = address, offset = offset, data = data, size = size, flags = flags, headers = headers + }); +} + +#! Adds the note naming the build by hashes of each segment's address, size, +#! and bytes, which differ wherever the code or what it loads does. +#! @param this The executable. +pub def image::build_id(&mut this): void { + val seeds: u64[3] = [14695981039346656037, 0x9E3779B97F4A7C15, 0xC2B2AE3D27D4EB4F]; + # A word at a time, the last few bytes as a word of their own. + var hashes: u64[3] = seeds; + for &each in this.segments { + val words: u64[3] = [each.address, each.size, each.size]; + for k in 0..3u { + for h in 0..3u { + hashes[h] = (hashes[h] ^ words[k]) * 1099511628211; + } + } + val data := each.data.data; + val whole := each.size / 8; + for i in 0..whole { + val word: u64 = *((data + 8 * i) as *u64); + hashes[0] = (hashes[0] ^ word) * 1099511628211; + hashes[1] = (hashes[1] ^ word) * 1099511628213; + hashes[2] = (hashes[2] ^ word) * 1099511628215; + } + var rest: u64 = 0; + for i in 8 * whole..each.size { + rest = rest | (data[i] as u64 << (8 * (i - 8 * whole))); + } + for h in 0..3u { + hashes[h] = (hashes[h] ^ rest) * 1099511628211; + } + } + for h in 0..3u { + hashes[h] = hashes[h] ^ (hashes[h] >> 29); + } + + # The hashes are the description, its first 20 bytes. + var note := byte_writer::new(); + note.u32(4); # The size of the name, + note.u32(build_id_bytes); # of the description, + note.u32(3); # and the type, a GNU build ID. + note.put_terminated("GNU"); + note.u64(hashes[0]); + note.u64(hashes[1]); + note.u32(hashes[2]); + this.sections.append(section { + name = ".note.gnu.build-id", kind = section_kind::note, data = byte_writer::move_of(&mut note), align = 4, flags = 0, entry = 0 + }); +} + +#! A place rounded up to a multiple of an alignment. +#! @param value The place. +#! @param align The alignment, a power of two. +#! @returns The place. +def aligned(value: u64, align: u64): u64 { + return (value + align - 1) & ~(align - 1); +} + +#! 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); + } +} + +#! Adds a name to a table of names, giving where it begins. +#! @param table The table. +#! @param name The name. +#! @returns Where. +def add_name(table: &mut byte_writer, name: strview): u64 { + val at := table.length; + table.put_terminated(name); + return at; +} + +#! Adds a program header of a part of a loaded segment, at the offset in the +#! file its address is at. +#! @param this The executable. +#! @param file The file's bytes. +#! @param kind Its type. +#! @param address Where the part is loaded. +#! @param size How many bytes it takes. +#! @param headers The bytes of the file's headers. +def image::part(&this, file: &mut byte_writer, kind: u64, address: u64, size: u64, headers: u64): void { + var offset: u64 = 0; + for &each in this.segments { + val reach := each.size + (each.headers ? headers : 0); + if address >= each.address && address < each.address + reach { + offset = each.offset + (address - each.address); + } + } + put_program(file, kind, readable as u64, offset, address, size, size, 8); +} + +#! Adds a program header. +#! @param file The file's bytes. +#! @param kind Its type. +#! @param flags How what it describes may be used. +#! @param offset Where what it describes is in the file. +#! @param address Where it is loaded. +#! @param stored How many bytes of it the file holds. +#! @param size How many bytes it takes once loaded. +#! @param align What its address and offset are aligned to. +def put_program(file: &mut byte_writer, kind: u64, flags: u64, offset: u64, address: u64, stored: u64, size: u64, align: u64): void { + file.u32(kind); + file.u32(flags); + file.u64(offset); + file.u64(address); # Where it is loaded, virtually, + file.u64(address); # and physically. + file.u64(stored); + file.u64(size); + file.u64(align); +} + +#! Adds a section header. +#! @param file The file's bytes. +#! @param made The header. +def put_section(file: &mut byte_writer, made: header): void { + file.u32(made.name); + file.u32(made.kind); + file.u64(made.flags); + file.u64(made.address); + file.u64(made.offset); + file.u64(made.size); + file.u32(made.link); + file.u32(made.info); + file.u64(made.align); + file.u64(made.entry); +} + +#! The bytes of the file of the executable, in the order the file holds them: +#! the headers, the segments, the symbols, the sections held, and the section +#! headers, which the file header says where they are once they are placed. +#! Where it only describes the segments, it places the sections describing them +#! where the executable's are, holds none of their bytes, and describes the +#! segments as the executable's headers do: debuggers match the two files by +#! them. +#! @param this The executable. +#! @returns The bytes. +pub def image::file(&this): byte_writer { + val segment_count := this.segments.length; + val described := this.described; + val headers := headers_size(segment_count, this.linked); + val linked := this.linked && !described; + val programs := segment_count + (linked ? dynamic_headers : 0); + # A dynamically linked executable's loaded parts are named too, for readelf. + val extra: u64 = linked ? 4 : 0; + val count := segment_count + extra + this.sections.length + 4; + var file := byte_writer::new(); + + # The file header, of an executable of its machine. + val ident: u8[8] = [0x7F, 'E' as u8, 'L' as u8, 'F' as u8, 2, 1, 1, 0]; + file.put(ident[:]); + file.zeros(8); + file.u16(2); + file.u16(this.machine); + file.u32(1); + file.u64(this.entry); + file.u64(header_bytes); + val sections_at := file.length; + file.u64(0); + file.u32(0); + file.u16(header_bytes); + file.u16(program_bytes); + file.u16(programs); + file.u16(section_bytes); + file.u16(count); + file.u16(count - 1); + + # The program headers. A dynamically linked executable names where its + # headers are and the loader before any segment is loaded, and its dynamic + # section after. + if linked { + this.part(&mut file, 6, this.segments[0].address + header_bytes, programs * program_bytes, headers); + this.part(&mut file, 3, this.dynamic.interpreter, this.dynamic.interpreter_size, headers); + } + for &each in this.segments { + val size := each.size + (each.headers ? headers : 0); + put_program(&mut file, 1, each.flags as u64, each.offset, each.address, described ? 0 : size, size, 0x1000); + } + if linked { + this.part(&mut file, 2, this.dynamic.section, this.dynamic.section_size, headers); + } + + # The segments, in order of their offsets, the one holding the headers placing + # its data after them. + var end := file.length; + for &each in this.segments { + continue if described; + val begins := each.offset + (each.headers ? headers : 0); + pad_to(&mut file, begins); + file.put(each.data); + if begins + each.size > end { + end = begins + each.size; + } + } + + # The symbol table, beginning with the null symbol, then its names. + var strings := byte_writer::new(); + add_name(&mut strings, ""); + val symtab := aligned(end, 8); + var text_section: u64 = 0; + for s in 0..segment_count { + if (this.segments[s].flags & executable) != 0 { + text_section = s + 1; + } + } + pad_to(&mut file, symtab); + file.zeros(symbol_bytes); + for &each in this.symbols { + file.u32(add_name(&mut strings, each.name)); + file.byte(0x12); # A global function, + file.byte(0); + file.u16(text_section); # within the code. + file.u64(each.address); + file.u64(each.size); + } + val symtab_size := (this.symbols.length + 1) * symbol_bytes; + val strtab := file.length; + file.put(strings.view()); + + # The sections held but not loaded, each where its alignment allows. + var placed := vector::::new(); + for &each in this.sections { + placed.append(file.align_to(each.align > 0 ? each.align : 1)); + file.put(each.data.view()); + } + + # Section names, then the sections: the null one, one per segment, those + # held, and the tables. + var names := byte_writer::new(); + add_name(&mut names, ""); + var segment_names := vector::::new(); + for &each in this.segments { + segment_names.append(add_name(&mut names, each.name)); + } + val part_names: strview[4] = [".interp", ".dynstr", ".dynsym", ".dynamic"]; + var extra_names := vector::::new(); + for i in 0..extra { + extra_names.append(add_name(&mut names, part_names[i])); + } + var held_names := vector::::new(); + for &each in this.sections { + held_names.append(add_name(&mut names, each.name)); + } + val symtab_name := add_name(&mut names, ".symtab"); + val strtab_name := add_name(&mut names, ".strtab"); + val shstrtab_name := add_name(&mut names, ".shstrtab"); + val shstrtab := file.length; + file.put(names.view()); + + val sections := file.align_to(8); + file.set_u64(sections_at, sections); + file.zeros(section_bytes); + for s in 0..segment_count { + val each: segment = this.segments[s]; + val skip := each.headers ? headers : 0; + var flags: u64 = 2; + if (each.flags & writable) != 0 { flags = flags | 1; } + if (each.flags & executable) != 0 { flags = flags | 4; } + put_section(&mut file, header { + name = segment_names[s], kind = described ? 8 : 1, flags = flags, # NOBITS, or PROGBITS. + address = each.address + skip, offset = described ? header_bytes : each.offset + skip, + size = each.size, align = 16 + }); + } + for i in 0..extra { + val addresses: u64[4] = [this.dynamic.interpreter, this.dynamic.strings, this.dynamic.symbols, this.dynamic.section]; + val sizes: u64[4] = [this.dynamic.interpreter_size, this.dynamic.strings_size, this.dynamic.symbols_size, this.dynamic.section_size]; + val types: u64[4] = [1, 3, 11, 6]; + val entries: u64[4] = [0, 0, 24, 16]; + put_section(&mut file, header { + name = extra_names[i], kind = types[i], flags = 2, address = addresses[i], + offset = addresses[i] - this.segments[0].address, size = sizes[i], + link = i >= 2 ? segment_count + 2 : 0, info = i == 2 ? 1 : 0, align = 8, entry = entries[i] + }); + } + for h in 0..this.sections.length { + val each: §ion = &this.sections[h]; + put_section(&mut file, header { + name = held_names[h], kind = each.kind as u64, flags = each.flags, offset = placed[h], + size = each.data.length, align = each.align, entry = each.entry + }); + } + put_section(&mut file, header { + name = symtab_name, kind = 2, offset = symtab, size = symtab_size, + link = segment_count + extra + this.sections.length + 2, info = 1, align = 8, entry = symbol_bytes + }); + put_section(&mut file, header { + name = strtab_name, kind = 3, offset = strtab, size = strings.length, align = 1 + }); + put_section(&mut file, header { + name = shstrtab_name, kind = 3, offset = shstrtab, size = names.length, align = 1 + }); + return file; +} + +#! The checksum `.gnu_debuglink` gives with the name of a file of debugging +#! information: CRC-32, as zlib computes it. +#! @param bytes The file's bytes. +#! @returns The checksum. +pub def crc32(bytes: span): u32 { + var table: u32[256]; + for n in 0..256u { + var c := n as u32; + for _ in 0..8u { + c = (c & 1) != 0 ? 0xEDB88320 ^ (c >> 1) : c >> 1; + } + table[n] = c; + } + var sum: u32 = 0xFFFFFFFF; + for &each in bytes { + sum = table[((sum ^ each as u32) & 0xFF) as u64] ^ (sum >> 8); + } + return sum ^ 0xFFFFFFFF; +} diff --git a/pdb/digest.sakura b/pdb/digest.sakura new file mode 100644 index 0000000..2f213de --- /dev/null +++ b/pdb/digest.sakura @@ -0,0 +1,110 @@ +#! @file digest.sakura +#! @author John Christman (sorakatadzuma@gmail.com) +#! @copyright Malunal Studios, LLC. +#! @license MIT +#! +#! Contains MD5, as RFC 1321 gives it, whose digest of a PDB file names it, so +#! the same build names its file the same way. + +module binary::pdb; + + +#! The MD5 digest of bytes. +#! @param data The bytes. +#! @returns The digest, its first byte first. +pub def md5_of(data: span): u8[16] { + var state: u32[4] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]; + var block: u8[64]; + val total := data.length; + var at := 0u; + while at + 64 <= total { + md5_block(&mut state, data.data + at); + at += 64; + } + + # The last bytes, then `0x80`, then zeros, then the length in bits, low + # byte first, filling one block or two. + val left := total - at; + for k in 0..64 { + block[k] = 0; + } + for k in 0..left { + block[k] = data[at + k]; + } + block[left] = 0x80; + if left >= 56 { + md5_block(&mut state, (&block) as *u8); + for k in 0..64 { + block[k] = 0; + } + } + val bits := total * 8; + for k in 0..8 { + block[56 + k] = ((bits >> (8 * k as u64)) & 0xff) as u8; + } + md5_block(&mut state, (&block) as *u8); + + var made: u8[16]; + for w in 0..4u { + for k in 0..4u { + made[4 * w + k] = ((state[w] >> (8 * k as u32)) & 0xff) as u8; + } + } + return made; +} + +# The amounts each round of MD5 turns its words left by. +val md5_turns: u32[16] = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21]; + +# Mixes a block of 64 bytes into MD5's state. +def md5_block(state: &mut u32[4], at: *u8): void { + var words: u32[16]; + for k in 0..16 { + val from := at + 4 * k; + words[k] = from[0] as u32 | (from[1] as u32 << 8) | (from[2] as u32 << 16) | (from[3] as u32 << 24); + } + var a := state[0]; + var b := state[1]; + var c := state[2]; + var d := state[3]; + for i in 0u..64u { + var mixed: u32 = 0; + var index: u64 = 0; + if i < 16 { + mixed = (b & c) | (~b & d); + index = i; + } else if i < 32 { + mixed = (d & b) | (~d & c); + index = (5 * i + 1) % 16; + } else if i < 48 { + mixed = b ^ c ^ d; + index = (3 * i + 5) % 16; + } else { + mixed = c ^ (b | ~d); + index = (7 * i) % 16; + } + val sum := a + mixed + md5_constants[i] + words[index]; + a = d; + d = c; + c = b; + val by := md5_turns[(i / 16) * 4 + i % 4]; + b = b + ((sum << by) | (sum >> (32 - by))); + } + state[0] = state[0] + a; + state[1] = state[1] + b; + state[2] = state[2] + c; + state[3] = state[3] + d; +} + +# MD5's constant of each step: the whole part of 2^32 times the sine of the +# step's number, counted from 1. +val md5_constants: u32[64] = [ + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391, +]; diff --git a/pdb/lines.sakura b/pdb/lines.sakura new file mode 100644 index 0000000..e6d8dfc --- /dev/null +++ b/pdb/lines.sakura @@ -0,0 +1,250 @@ +#! @file lines.sakura +#! @author John Christman (sorakatadzuma@gmail.com) +#! @copyright Malunal Studios, LLC. +#! @license MIT +#! +#! Contains the writer of a module's lines, as the subsections after its +#! symbols hold them: the files the module's code is from, named in the PDB +#! file's table of names, the lines of each procedure's code, and the file and +#! line each function inlined somewhere begins at. And the annotations an +#! inline site says which of its procedure's code it is with, and the lines of +#! each part. + +import std::io; +module binary::pdb; + + +#! Adds a subsection: its kind, its bytes, then what it holds, padded to four +#! bytes. +#! @param out Where. +#! @param kind Its kind, as `debug_s_lines`. +#! @param data What it holds. +pub def subsection(out: &mut byte_writer, kind: u32, data: span): void { + out.u32(kind); + out.u32(data.length); + out.put(data); + out.align_to(4); +} + +#! The files a module's code is from, as its subsection of checksums names +#! them, each with no checksum. +pub struct file_checksums { + pub var bytes: byte_writer; #! The subsection's contents. +} + +#! Makes a subsection naming no files. +#! @returns The subsection. +pub def file_checksums::new(): file_checksums { + return file_checksums { bytes = byte_writer::new() }; +} + +#! Names a file, giving where its entry is, which lines name the file by. +#! @param this The subsection. +#! @param name Where the file's path is in the PDB file's table of names. +#! @returns Where its entry is. +pub def file_checksums::file(&mut this, name: u32): u32 { + val at := this.bytes.length as u32; + this.bytes.u32(name); + this.bytes.byte(0); # No checksum, + this.bytes.byte(0); # of no kind. + this.bytes.align_to(4); + return at; +} + +#! A line of a procedure's code. +struct line_entry { + var offset: u32; #! Where its code begins, from where the procedure's does. + var line: u32; + var column: u16; +} + +#! The lines of a procedure's code, as a subsection of lines holds them: each +#! run of lines of one file in a block of its own. +pub struct function_lines { + pub var bytes: byte_writer; #! The subsection's contents. + var file: u32; #! The file of the block being written. + var pending: vector; #! Its lines. +} + +#! Begins the lines of a procedure's code. +#! @param section The section holding its code. +#! @param offset Where its code begins in it. +#! @param size The bytes of its code. +#! @returns The lines. +pub def function_lines::new(section: u16, offset: u32, size: u32): function_lines { + var made := function_lines { bytes = byte_writer::new(), file = 0, pending = vector::::new() }; + made.bytes.u32(offset); + made.bytes.u16(section); + made.bytes.u16(1); # With columns. + made.bytes.u32(size); + return made; +} + +#! Adds a line: the code from an offset is from a line and column of a file. +#! @param this The lines. +#! @param file Where the file's entry is among the checksums. +#! @param offset Where the code begins, from where the procedure's does. +#! @param line The line, from 1. +#! @param column The column, from 1, or 0 for none. +pub def function_lines::line(&mut this, file: u32, offset: u32, line: u32, column: u16): void { + if this.pending.length != 0 && file != this.file { + this.flush(); + } + this.file = file; + this.pending.append(line_entry { offset = offset, line = line, column = column }); +} + +#! Ends the lines, writing the last block. +#! @param this The lines. +pub def function_lines::finish(&mut this): void { + if this.pending.length != 0 { + this.flush(); + } +} + +# Writes the block of the lines kept, each a statement, then their columns. +def function_lines::flush(&mut this): void { + val count := this.pending.length; + this.bytes.u32(this.file); + this.bytes.u32(count); + this.bytes.u32(12 + 12 * count); + for i in 0..count { + val each: line_entry = this.pending[i]; + this.bytes.u32(each.offset); + this.bytes.u32((each.line & 0xFFFFFF) | 0x80000000); + } + for i in 0..count { + val each: line_entry = this.pending[i]; + this.bytes.u16(each.column); + this.bytes.u16(0); + } + this.pending.clear(); +} + +#! The file and line each function inlined somewhere begins at, as the +#! subsection of inlinees' lines holds them. +pub struct inlinee_lines { + pub var bytes: byte_writer; #! The subsection's contents. + pub var count: u64; #! How many functions it names. +} + +#! Makes a subsection naming no functions. +#! @returns The subsection. +pub def inlinee_lines::new(): inlinee_lines { + var made := inlinee_lines { bytes = byte_writer::new(), count = 0 }; + made.bytes.u32(0); # Each entry naming one file. + return made; +} + +#! Names where a function inlined somewhere begins. +#! @param this The subsection. +#! @param inlinee The function's ID. +#! @param file Where the file's entry is among the checksums. +#! @param line The line it is declared at. +pub def inlinee_lines::add(&mut this, inlinee: u32, file: u32, line: u32): void { + this.bytes.u32(inlinee); + this.bytes.u32(file); + this.bytes.u32(line); + this.count++; +} + + +# Annotations. + +# What each annotation does. +const ba_change_code_offset: u32 = 3; +const ba_change_code_length: u32 = 4; +const ba_change_file: u32 = 5; +const ba_change_line_offset: u32 = 6; +const ba_change_code_offset_and_line_offset: u32 = 11; + +#! The annotations of an inline site, written as its code is met: each part of +#! the procedure's code the site is, from where the procedure's code begins, +#! and the line of each, from the line the function inlined begins at. Parts +#! of the same line are one. +pub struct annotations { + pub var bytes: byte_writer; #! The annotations. + var file: u32; #! The file of the last part, as its entry among the checksums. + var line: u32; #! Its line. + var at: u32; #! Where the last annotation left the code. + var open: bool; #! Whether the last part is not yet ended. + var begun: bool; #! Whether a part has begun. +} + +#! Begins the annotations of a site, from where the function inlined begins. +#! @param file The file it is declared in, as its entry among the checksums. +#! @param line The line it is declared at. +#! @returns The annotations. +pub def annotations::new(file: u32, line: u32): annotations { + return annotations { bytes = byte_writer::new(), file = file, line = line, at = 0, open = false, begun = false }; +} + +# Adds an annotation and its operand, each in as few bytes as hold it. +def annotations::put(&mut this, value: u32): void { + if value < 0x80 { + this.bytes.byte(value as u8); + } else if value < 0x4000 { + this.bytes.byte(((value >> 8) | 0x80) as u8); + this.bytes.byte((value & 0xFF) as u8); + } else { + this.bytes.byte(((value >> 24) | 0xC0) as u8); + this.bytes.byte(((value >> 16) & 0xFF) as u8); + this.bytes.byte(((value >> 8) & 0xFF) as u8); + this.bytes.byte((value & 0xFF) as u8); + } +} + +#! The site's code from an offset is from a line of a file. Where the part +#! before it is from the same line, it goes on. +#! @param this The annotations. +#! @param offset Where the code begins, from where the procedure's does. +#! @param file Its file, as its entry among the checksums. +#! @param line Its line. +pub def annotations::at_line(&mut this, offset: u32, file: u32, line: u32): void { + return if this.open && file == this.file && line == this.line; + this.open = true; + if file != this.file { + this.put(ba_change_file); + this.put(file); + } + val delta := line as i64 - this.line as i64; + val encoded: u32 = delta < 0 ? ((-delta) as u32 << 1) | 1 : (delta as u32) << 1; + val code := offset - this.at; + if encoded < 8 && code <= 0xF { + this.put(ba_change_code_offset_and_line_offset); + this.put((encoded << 4) | code); + } else { + # The first part's line is said though it is where the function begins, + # as some readers take a part's line only from a change to it. + if delta != 0 || !this.begun { + this.put(ba_change_line_offset); + this.put(encoded); + } + this.put(ba_change_code_offset); + this.put(code); + } + this.begun = true; + this.at = offset; + this.file = file; + this.line = line; +} + +#! The code from an offset is not the site's, so the part before it ends. +#! @param this The annotations. +#! @param offset Where that code begins. +pub def annotations::away(&mut this, offset: u32): void { + if this.open { + this.put(ba_change_code_length); + this.put(offset - this.at); + this.at = offset; + } + this.open = false; +} + +#! Ends the annotations where the site's last part ends. +#! @param this The annotations. +#! @param offset Where it ends. +pub def annotations::finish(&mut this, offset: u32): void { + this.put(ba_change_code_length); + this.put(offset - this.at); +} diff --git a/pdb/msf.sakura b/pdb/msf.sakura new file mode 100644 index 0000000..7e91ea5 --- /dev/null +++ b/pdb/msf.sakura @@ -0,0 +1,224 @@ +#! @file msf.sakura +#! @author John Christman (sorakatadzuma@gmail.com) +#! @copyright Malunal Studios, LLC. +#! @license MIT +#! +#! Contains the writer of the file a PDB file's streams are held in, an MSF +#! file of version 7: blocks of 4096 bytes, the first describing the file, the +#! second and third, and those 4096 blocks after them, the map of which blocks +#! are free, then each stream's blocks in turn, then the directory naming each +#! stream's size and blocks, and a block naming the directory's. And the table +#! of strings a PDB file names files and other text by. + +import std::containers; +import std::io; +module binary::pdb; + + +#! The bytes of a block. +const block_bytes: u64 = 4096; + +#! Whether a block holds a map of free blocks, as every block 1 and 2 past a +#! multiple of 4096 does. +#! @param block The block. +#! @returns Whether it does. +def holds_free_map(block: u64): bool { + val within := block % block_bytes; + return within == 1 || within == 2; +} + +#! Lays streams out in an MSF file, giving the file, and where the first byte +#! of each stream's first block is, or 0 for one holding nothing. +#! @param streams The streams, by number. +#! @param firsts Where the first byte of each is, filled. +#! @returns The file. +pub def msf_file(streams: &vector, firsts: &mut vector): byte_writer { + # Blocks are given out in order, past the maps of free blocks. + var next: u64 = 3; + var blocks := vector::::new(); # Each stream's blocks, one after another. + var first_block := vector::::new(); # Where each stream's are in `blocks`. + firsts.clear(); + for &each in streams.view() { + first_block.append(blocks.length); + val count := (each.length + block_bytes - 1) / block_bytes; + firsts.append(0); + for _ in 0..count { + while holds_free_map(next) { + next++; + } + blocks.append(next); + next++; + } + } + first_block.append(blocks.length); + + # The directory: how many streams, each one's size, then each one's blocks. + var directory := byte_writer::new(); + directory.u32(streams.length); + for &each in streams.view() { + directory.u32(each.length); + } + for &each in blocks.view() { + directory.u32(each); + } + var directory_blocks := vector::::new(); + val directory_count := (directory.length + block_bytes - 1) / block_bytes; + for _ in 0..directory_count { + while holds_free_map(next) { + next++; + } + directory_blocks.append(next); + next++; + } + while holds_free_map(next) { + next++; + } + val map_block := next; + next++; + val total := next; + + var file := byte_writer::new(); + file.zeros(total * block_bytes); + + # The file's description: its signature, the size of its blocks, the map of + # free blocks in use, how many blocks there are, the directory's bytes, and + # the block naming the directory's blocks. + val magic: strview = "Microsoft C/C++ MSF 7.00\r\n"; + for i in 0..magic.length { + file.set_byte(i, magic[i]); + } + file.set_byte(26, 0x1A); + file.set_byte(27, 'D' as u8); + file.set_byte(28, 'S' as u8); + file.set_u32(32, block_bytes); + file.set_u32(36, 1); + file.set_u32(40, total); + file.set_u32(44, directory.length); + file.set_u32(48, 0); + file.set_u32(52, map_block); + + # Every map of free blocks says every block is free, but those the file + # holds, which the map in use, the one in block 1 and those 4096 after it, + # says are taken, a bit of each, the first block's lowest. + var b: u64 = 1; + while b < total { + if holds_free_map(b) { + for k in 0..block_bytes { + file.set_byte(b * block_bytes + k, 0xFF); + } + } + b++; + } + for block in 0..total { + val map := 1 + (block / (8 * block_bytes)) * block_bytes; + val byte := map * block_bytes + (block % (8 * block_bytes)) / 8; + val bit := block % 8; + file.set_byte(byte, file.get_byte(byte) & ~((1u << bit) as u8)); + } + + # Each stream's bytes, in its blocks. + for s in 0..streams.length { + val data := streams[s].view(); + var at: u64 = 0; + for k in first_block[s]..first_block[s + 1] { + val block := blocks[k]; + if at == 0 { + firsts[s] = block * block_bytes; + } + val take := data.length - at < block_bytes ? data.length - at : block_bytes; + memcpy(file.slice().data + block * block_bytes, data.data + at, take); + at += take; + } + } + var at: u64 = 0; + val directory_data := directory.view(); + for &block in directory_blocks.view() { + val take := directory_data.length - at < block_bytes ? directory_data.length - at : block_bytes; + memcpy(file.slice().data + block * block_bytes, directory_data.data + at, take); + at += take; + } + for k in 0..directory_blocks.length { + file.set_u32(map_block * block_bytes + 4 * k, directory_blocks[k]); + } + return file; +} + + +# The table of names. + +#! A table of strings, as the stream `/names` holds them: each string once, +#! ending with a zero, after the empty one, and a table of their hashes a +#! reader finds one by. +pub struct name_table { + var bytes: byte_writer; #! The strings. + var held: interner; #! The strings, by ID. + var at: vector; #! Where each is in the table, by its ID. +} + +#! Makes a table holding only the empty string. +#! @returns The table. +pub def name_table::new(): name_table { + var made := name_table { bytes = byte_writer::new(), held = interner::::new(), at = vector::::new() }; + made.bytes.byte(0); + return made; +} + +#! Where a string is in the table, adding it where it is new. +#! @param this The table. +#! @param text The string. +#! @returns Its offset. +pub def name_table::offset_of(&mut this, text: strview): u32 { + return 0 if text.length == 0; + val id := this.held.intern_view(text); + if id < this.at.length { + return this.at[id]; + } + val at := this.bytes.length as u32; + this.bytes.put_terminated(text); + this.at.append(at); + return at; +} + +#! The table as a stream holds it: its signature, the version of its hash, the +#! strings' bytes, then the strings' places by their hashes, each in the first +#! free bucket from its hash's on, then how many strings there are. The +#! buckets grow as Microsoft's tools grow them, so three of four at most hold a +#! string. +#! @param this The table. +#! @returns The stream. +pub def name_table::stream(&this): byte_writer { + val count := this.at.length; + var buckets: u64 = 1; + for k in 1..count + 1 { + if buckets * 3 / 4 < k { + buckets = buckets * 3 / 2 + 1; + } + } + var slots := vector::::new(); + slots.resize(buckets); + val all := this.bytes.view(); + for &offset in this.at.view() { + var end := offset as u64; + while all[end] != 0 { + end++; + } + val text := span::::over(all.data + offset as u64, end - offset as u64); + var slot := hash_v1(text) as u64 % buckets; + while slots[slot] != 0 { + slot = (slot + 1) % buckets; + } + slots[slot] = offset; + } + + var made := byte_writer::new(); + made.u32(0xEFFEEFFE); + made.u32(1); + made.u32(this.bytes.length); + made.put(all); + made.u32(buckets); + for &each in slots.view() { + made.u32(each); + } + made.u32(count); + return made; +} diff --git a/pdb/pdb.sakura b/pdb/pdb.sakura new file mode 100644 index 0000000..a788717 --- /dev/null +++ b/pdb/pdb.sakura @@ -0,0 +1,212 @@ +#! @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. + +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; +} diff --git a/pdb/symbols.sakura b/pdb/symbols.sakura new file mode 100644 index 0000000..e00a2f8 --- /dev/null +++ b/pdb/symbols.sakura @@ -0,0 +1,338 @@ +#! @file symbols.sakura +#! @author John Christman (sorakatadzuma@gmail.com) +#! @copyright Malunal Studios, LLC. +#! @license MIT +#! +#! Contains the writer of symbols, as a module's stream holds them: the +#! module's name and compiler, each procedure, the scopes within it, as blocks +#! and the sites a call was inlined at, and the locals of each with where they +#! are over which code. A scope's record names the one holding it and the +#! record ending it, by where each is in the stream, which this writes once the +#! scope ends. The records the stream of globals and publics hold are written +#! here too. + +import std::io; +module binary::pdb; + + +#! A run of code in a section, which a local is somewhere over. +pub struct code_range { + pub var section: u16; #! The section, numbered from 1. + pub var offset: u32; #! Where the code begins in it. + pub var length: u16; #! How many bytes. +} + +#! A module's symbols being written. +pub struct symbol_stream { + pub var bytes: byte_writer; #! The stream, which begins with its signature. + var scopes: vector; #! Where each scope still open begins, the innermost last. + var sites: vector; #! Whether each is an inline site, which ends with a record of its own. +} + +#! Makes a stream holding no symbols, beginning with the signature of +#! CodeView's C13 format. +#! @returns The stream. +pub def symbol_stream::new(): symbol_stream { + var made := symbol_stream { bytes = byte_writer::new(), scopes = vector::::new(), sites = vector::::new() }; + made.bytes.u32(4); + return made; +} + +# Where the innermost open scope begins, or 0 where none is open. +def symbol_stream::parent(&this): u64 { + return this.scopes.length == 0 ? 0 : this.scopes.last(); +} + +# Opens a scope at a record just begun, writing where the one holding it is, +# and a place for where it ends. +def symbol_stream::open(&mut this, at: u64, site: bool): void { + this.bytes.u32(this.parent()); + this.bytes.u32(0); + this.scopes.append(at); + this.sites.append(site); +} + +#! Adds the module's name, as the object it was compiled to. +#! @param this The stream. +#! @param name The name. +pub def symbol_stream::object_name(&mut this, name: strview): void { + val at := begin_record(&mut this.bytes, s_objname); + this.bytes.u32(0); + put_name(&mut this.bytes, name); + end_symbol_record(&mut this.bytes, at); +} + +#! Adds what compiled the module: its language, for the machine, and its +#! version, as `S_COMPILE3` says it. +#! @param this The stream. +#! @param language The language, as `language_cxx`. +#! @param machine The machine, as `cv_machine_x64`. +#! @param version The compiler's major, minor, and build numbers. +#! @param name The compiler's name and version, as text. +pub def symbol_stream::compiler(&mut this, language: u8, machine: u16, version: u16[3], name: strview): void { + val at := begin_record(&mut this.bytes, s_compile3); + this.bytes.u32(language as u64); + this.bytes.u16(machine); + # The front end's version, then the back end's, each with no fix. + for _ in 0..2u { + for k in 0..3u { + this.bytes.u16(version[k]); + } + this.bytes.u16(0); + } + put_name(&mut this.bytes, name); + end_symbol_record(&mut this.bytes, at); +} + +#! Begins a procedure, whose frame, locals, and scopes follow, then `end`. +#! @param this The stream. +#! @param global Whether it is seen outside its module. +#! @param name Its name. +#! @param type_index Its type. +#! @param section The section holding its code. +#! @param offset Where its code begins in it. +#! @param size The bytes of its code. +#! @param body Where its prologue ends, from where it begins, or 0. +#! @param leaving Where its epilogue begins, from where it begins, or 0. +#! @param flags What it is, as `proc_has_frame_pointer`. +#! @returns Where its record is in the stream, as the globals name it by. +pub def symbol_stream::procedure( + &mut this, + global: bool, + name: strview, + type_index: u32, + section: u16, + offset: u32, + size: u32, + body: u32, + leaving: u32, + flags: u8 +): u64 { + val at := begin_record(&mut this.bytes, global ? s_gproc32 : s_lproc32); + this.open(at, false); + this.bytes.u32(0); # No procedure follows it within a scope. + this.bytes.u32(size); + this.bytes.u32(body); + this.bytes.u32(leaving); + this.bytes.u32(type_index); + this.bytes.u32(offset); + this.bytes.u16(section); + this.bytes.byte(flags); + put_name(&mut this.bytes, name); + end_symbol_record(&mut this.bytes, at); + return at; +} + +#! Adds what a procedure's frame holds, and the register its locals and its +#! parameters are found from. +#! @param this The stream. +#! @param bytes The bytes of the frame. +#! @param saved The bytes of the registers it saves. +#! @param registers The registers, as `frame_locals_rbp | frame_params_rbp`. +pub def symbol_stream::frame(&mut this, bytes: u32, saved: u32, registers: u32): void { + val at := begin_record(&mut this.bytes, s_frameproc); + this.bytes.u32(bytes); + this.bytes.u32(0); # No padding, + this.bytes.u32(0); + this.bytes.u32(saved); + this.bytes.u32(0); # and no handler of exceptions. + this.bytes.u16(0); + this.bytes.u32(registers); + end_symbol_record(&mut this.bytes, at); +} + +#! Begins a block of a procedure's code, whose locals and scopes follow, then +#! `end`. +#! @param this The stream. +#! @param section The section holding its code. +#! @param offset Where its code begins in it. +#! @param size The bytes of its code. +pub def symbol_stream::block(&mut this, section: u16, offset: u32, size: u32): void { + val at := begin_record(&mut this.bytes, s_block32); + this.open(at, false); + this.bytes.u32(size); + this.bytes.u32(offset); + this.bytes.u16(section); + put_name(&mut this.bytes, ""); + end_symbol_record(&mut this.bytes, at); +} + +#! Begins the site a function was inlined at, whose locals and scopes follow, +#! then `end`. The annotations say which of the procedure's code the site is, +#! and the lines of each part. +#! @param this The stream. +#! @param inlinee The ID of the function inlined. +#! @param annotations The site's annotations. +pub def symbol_stream::inline_site(&mut this, inlinee: u32, annotations: span): void { + val at := begin_record(&mut this.bytes, s_inlinesite); + this.open(at, true); + this.bytes.u32(inlinee); + this.bytes.put(annotations); + end_symbol_record(&mut this.bytes, at); +} + +#! Ends the innermost scope, naming where it ends in the record that began it. +#! @param this The stream. +pub def symbol_stream::end(&mut this): void { + val begun := this.scopes.last(); + val site := this.sites.last(); + this.scopes.truncate(); + this.sites.truncate(); + val at := begin_record(&mut this.bytes, site ? s_inlinesite_end : s_end); + end_symbol_record(&mut this.bytes, at); + this.bytes.set_u32(begun + 8, at); +} + +#! Adds a local, where it is following in records of the ranges it is in. +#! @param this The stream. +#! @param type_index Its type. +#! @param flags What it is, as `local_parameter`. +#! @param name Its name. +pub def symbol_stream::local(&mut this, type_index: u32, flags: u16, name: strview): void { + val at := begin_record(&mut this.bytes, s_local); + this.bytes.u32(type_index); + this.bytes.u16(flags); + put_name(&mut this.bytes, name); + end_symbol_record(&mut this.bytes, at); +} + +# Adds a range, with no gaps. +def symbol_stream::range(&mut this, over: code_range): void { + this.bytes.u32(over.offset); + this.bytes.u16(over.section); + this.bytes.u16(over.length); +} + +#! The local before is in a register over a range. +#! @param this The stream. +#! @param register The register, as CodeView numbers it. +#! @param over The range. +pub def symbol_stream::in_register(&mut this, register: u16, over: code_range): void { + val at := begin_record(&mut this.bytes, s_defrange_register); + this.bytes.u16(register); + this.bytes.u16(0); + this.range(over); + end_symbol_record(&mut this.bytes, at); +} + +#! The local before is at an offset from the frame pointer over a range. +#! @param this The stream. +#! @param offset The offset. +#! @param over The range. +pub def symbol_stream::in_frame(&mut this, offset: i32, over: code_range): void { + val at := begin_record(&mut this.bytes, s_defrange_framepointer_rel); + this.bytes.u32(offset as u64); + this.range(over); + end_symbol_record(&mut this.bytes, at); +} + +#! The local before is at an offset from the frame pointer wherever its scope +#! is, as a parameter is. +#! @param this The stream. +#! @param offset The offset. +pub def symbol_stream::in_frame_throughout(&mut this, offset: i32): void { + val at := begin_record(&mut this.bytes, s_defrange_framepointer_rel_full_scope); + this.bytes.u32(offset as u64); + end_symbol_record(&mut this.bytes, at); +} + +#! A part of the local before, from an offset within it, is in a register over +#! a range. +#! @param this The stream. +#! @param register The register. +#! @param part Where the part begins in the local, below 4096. +#! @param over The range. +pub def symbol_stream::part_in_register(&mut this, register: u16, part: u32, over: code_range): void { + val at := begin_record(&mut this.bytes, s_defrange_subfield_register); + this.bytes.u16(register); + this.bytes.u16(0); + this.bytes.u32(part & 0xFFF); + this.range(over); + end_symbol_record(&mut this.bytes, at); +} + +#! The local before is in memory at an offset from what a register holds over +#! a range. +#! @param this The stream. +#! @param register The register. +#! @param offset The offset. +#! @param over The range. +pub def symbol_stream::through_register(&mut this, register: u16, offset: i32, over: code_range): void { + val at := begin_record(&mut this.bytes, s_defrange_register_rel); + this.bytes.u16(register); + this.bytes.u16(0); + this.bytes.u32(offset as u64); + this.range(over); + end_symbol_record(&mut this.bytes, at); +} + +#! Adds a local at an offset from a register throughout its scope, as +#! `S_REGREL32` says it. +#! @param this The stream. +#! @param offset The offset. +#! @param type_index Its type. +#! @param register The register. +#! @param name Its name. +pub def symbol_stream::relative(&mut this, offset: i32, type_index: u32, register: u16, name: strview): void { + val at := begin_record(&mut this.bytes, s_regrel32); + this.bytes.u32(offset as u64); + this.bytes.u32(type_index); + this.bytes.u16(register); + put_name(&mut this.bytes, name); + end_symbol_record(&mut this.bytes, at); +} + +#! Adds each section of the image, as the linker's module names them, from +#! their headers as the image holds them. +#! @param this The stream. +#! @param headers The headers, 40 bytes each. +pub def symbol_stream::sections(&mut this, headers: &byte_writer): void { + for k in 0..headers.length / 40 { + val header := 40 * k; + val at := begin_record(&mut this.bytes, s_section); + this.bytes.u16(k + 1); + this.bytes.byte(12); # Aligned to a page, + this.bytes.byte(0); + this.bytes.u32(headers.get_u32(header + 12)); + this.bytes.u32(headers.get_u32(header + 8)); + this.bytes.u32(headers.get_u32(header + 36)); + for i in 0..8u { + val letter := headers.get_byte(header + i); + break if letter == 0; + this.bytes.byte(letter); + } + this.bytes.byte(0); + end_symbol_record(&mut this.bytes, at); + } +} + +#! Writes a public symbol's record: a name, and the place it names. +#! @param out Where. +#! @param flags What it names, as `public_function | public_code`. +#! @param section The section. +#! @param offset Where in it. +#! @param name The name. +pub def public_record(out: &mut byte_writer, flags: u32, section: u16, offset: u32, name: strview): void { + val at := begin_record(out, s_pub32); + out.u32(flags); + out.u32(offset); + out.u16(section); + put_name(out, name); + end_symbol_record(out, at); +} + +#! Writes the record naming a procedure among the globals: its name, and where +#! its record is in its module's stream. +#! @param out Where. +#! @param global Whether the procedure is global. +#! @param owner Its module, numbered from 1. +#! @param offset Where its record is in the module's stream. +#! @param name Its name. +pub def procedure_reference(out: &mut byte_writer, global: bool, owner: u16, offset: u32, name: strview): void { + val at := begin_record(out, global ? s_procref : s_lprocref); + out.u32(0); + out.u32(offset); + out.u16(owner); + put_name(out, name); + end_symbol_record(out, at); +} diff --git a/pdb/types.sakura b/pdb/types.sakura new file mode 100644 index 0000000..752b1bb --- /dev/null +++ b/pdb/types.sakura @@ -0,0 +1,504 @@ +#! @file types.sakura +#! @author John Christman (sorakatadzuma@gmail.com) +#! @copyright Malunal Studios, LLC. +#! @license MIT +#! +#! Contains the writer of type records, as a PDB file's stream of types and its +#! stream of IDs hold them: each record's length, its kind, and what it holds, +#! padded to four bytes, numbered from `first_type` in the order they are +#! added, a record only naming those before it. A record added twice is held +#! once. Each is hashed as Microsoft's tools hash it, so a debugger finds a +#! struct's definition by its name. + +import std::containers; +import std::io; +module binary::pdb; + + +#! How many buckets the hashes of a stream of types are taken modulo. +const type_buckets: u32 = 0x3FFFF; + +#! Begins a record: its length, written once it ends, then its kind. +#! @param out Where. +#! @param kind Its kind. +#! @returns Where it begins. +pub def begin_record(out: &mut byte_writer, kind: u16): u64 { + val at := out.length; + out.u16(0); + out.u16(kind); + return at; +} + +#! Ends a type record, padding it to four bytes with the bytes that say how +#! many are left, as `LF_PAD3`, `LF_PAD2`, and `LF_PAD1` do, then writing its +#! length, which leaves out its own two bytes. +#! @param out Where. +#! @param at Where it begins. +pub def end_type_record(out: &mut byte_writer, at: u64): void { + pad_type(out); + out.set_u16(at, out.length - at - 2); +} + +#! Pads a type record, or a member of a field list, to four bytes, as +#! `end_type_record` does. +#! @param out Where. +def pad_type(out: &mut byte_writer): void { + while out.length % 4 != 0 { + out.byte((0xF0 + 4 - out.length % 4) as u8); + } +} + +#! Ends a symbol record, padding it to four bytes with zeros, then writing its +#! length, which leaves out its own two bytes. +#! @param out Where. +#! @param at Where it begins. +pub def end_symbol_record(out: &mut byte_writer, at: u64): void { + while (out.length - at) % 4 != 0 { + out.byte(0); + } + out.set_u16(at, out.length - at - 2); +} + +#! Adds a name, ending with a zero. +#! @param out Where. +#! @param text The name. +pub def put_name(out: &mut byte_writer, text: strview): void { + out.put_terminated(text); +} + +#! Adds a number, as CodeView's numeric leaves write one: in two bytes where it +#! is below 0x8000, else after the kind of leaf its size takes. +#! @param out Where. +#! @param value The number. +pub def put_unsigned(out: &mut byte_writer, value: u64): void { + if value < 0x8000 { + out.u16(value); + } else if value <= 0xFFFF { + out.u16(lf_ushort as u64); + out.u16(value); + } else if value <= 0xFFFFFFFF { + out.u16(lf_ulong as u64); + out.u32(value); + } else { + out.u16(lf_uquadword as u64); + out.u64(value); + } +} + +#! Adds a signed number, as `put_unsigned` does one not below zero, and a +#! negative one after the kind of leaf its size takes. +#! @param out Where. +#! @param value The number. +pub def put_signed(out: &mut byte_writer, value: i64): void { + if value >= 0 { + put_unsigned(out, value as u64); + } else if value >= -128 { + out.u16(lf_char as u64); + out.byte(value as u8); + } else if value >= -32768 { + out.u16(lf_short as u64); + out.u16(value as u64); + } else if value >= -2147483648 { + out.u16(lf_long as u64); + out.u32(value as u64); + } else { + out.u16(lf_quadword as u64); + out.u64(value as u64); + } +} + +#! The records of a stream of types, or of IDs, which are written as types are. +pub struct type_table { + pub var records: byte_writer; #! Each record, one after another. + var offsets: vector; #! Where each record begins. + var hashes: vector; #! Each record's hash, modulo the buckets. + var held: interner; #! Each record's bytes, by its index less the first. +} + +#! Makes a table holding no records. +#! @returns The table. +pub def type_table::new(): type_table { + return type_table { + records = byte_writer::new(), + offsets = vector::::new(), + hashes = vector::::new(), + held = interner::::new() + }; +} + +#! How many records the table holds. +#! @param this The table. +#! @returns How many. +pub val type_table::count: u64 { + get => this.offsets.length; +} + +#! Adds a record, or finds it where the table holds it already, hashed by its +#! bytes. +#! @param this The table. +#! @param record The record, its length and kind first. +#! @returns Its index. +pub def type_table::add(&mut this, record: span): u32 { + return this.add_hashed(record, hash_record(record)); +} + +#! Adds a record, or finds it, with its hash given, as a struct's is its name's. +#! @param this The table. +#! @param record The record. +#! @param hash Its hash, before the buckets. +#! @returns Its index. +pub def type_table::add_hashed(&mut this, record: span, hash: u32): u32 { + val id := this.held.intern_view(record); + if id < this.offsets.length { + return first_type + id; + } + this.offsets.append(this.records.length as u32); + this.hashes.append(hash % type_buckets); + this.records.put(record); + return first_type + id; +} + +#! A pointer of 64 bits, or a reference, to a type. +#! @param this The table. +#! @param referent The type. +#! @param reference Whether it is a reference. +#! @returns Its index. +pub def type_table::pointer(&mut this, referent: u32, reference: bool): u32 { + var made := byte_writer::new(); + val at := begin_record(&mut made, lf_pointer); + made.u32(referent); + # A pointer of 64 bits, a pointer or a reference to a value, of 8 bytes. + made.u32(0x0C | ((reference ? 1u : 0u) << 5) | (8u << 13)); + end_type_record(&mut made, at); + return this.add(made.view()); +} + +#! An array of a type, indexed by 64-bit numbers. +#! @param this The table. +#! @param element The type of each element. +#! @param bytes The bytes of the whole array. +#! @returns Its index. +pub def type_table::array(&mut this, element: u32, bytes: u64): u32 { + var made := byte_writer::new(); + val at := begin_record(&mut made, lf_array); + made.u32(element); + made.u32(0x23); # `unsigned __int64`. + put_unsigned(&mut made, bytes); + put_name(&mut made, ""); + end_type_record(&mut made, at); + return this.add(made.view()); +} + +#! The list of the types of a function's parameters. +#! @param this The table. +#! @param types The types. +#! @returns Its index. +pub def type_table::arguments(&mut this, types: span): u32 { + var made := byte_writer::new(); + val at := begin_record(&mut made, lf_arglist); + made.u32(types.length); + for &each in types { + made.u32(each); + } + end_type_record(&mut made, at); + return this.add(made.view()); +} + +#! A function's type, called as C is. +#! @param this The table. +#! @param returns The type it returns. +#! @param arguments The list of the types of its parameters. +#! @param count How many it takes. +#! @returns Its index. +pub def type_table::procedure(&mut this, returns: u32, arguments: u32, count: u16): u32 { + var made := byte_writer::new(); + val at := begin_record(&mut made, lf_procedure); + made.u32(returns); + made.byte(0); # Called as C is, near, + made.byte(0); # with no options. + made.u16(count); + made.u32(arguments); + end_type_record(&mut made, at); + return this.add(made.view()); +} + +#! The members of a struct, a union, or an enum, as a field list holds them, +#! each padded to four bytes. +pub struct field_list { + pub var count: u16; #! How many members it holds. + var bytes: byte_writer; #! The members, one after another. + var starts: vector; #! Where each member begins. +} + +#! Makes a list of no members. +#! @returns The list. +pub def field_list::new(): field_list { + return field_list { count = 0, bytes = byte_writer::new(), starts = vector::::new() }; +} + +#! Adds a member of a struct or union. +#! @param this The list. +#! @param type_index Its type. +#! @param offset Where it begins. +#! @param name Its name. +pub def field_list::member(&mut this, type_index: u32, offset: u64, name: strview): void { + this.starts.append(this.bytes.length); + this.bytes.u16(lf_member as u64); + this.bytes.u16(access_public as u64); + this.bytes.u32(type_index); + put_unsigned(&mut this.bytes, offset); + put_name(&mut this.bytes, name); + pad_type(&mut this.bytes); + this.count++; +} + +#! Adds a member of an enum. +#! @param this The list. +#! @param value Its value. +#! @param name Its name. +pub def field_list::enumerate(&mut this, value: i64, name: strview): void { + this.starts.append(this.bytes.length); + this.bytes.u16(lf_enumerate as u64); + this.bytes.u16(access_public as u64); + put_signed(&mut this.bytes, value); + put_name(&mut this.bytes, name); + pad_type(&mut this.bytes); + this.count++; +} + +#! The most a record of a field list holds, past its length and kind, leaving +#! room for the member naming the record it goes on in. +const fields_most: u64 = 0xFF00 - 4 - 8; + +#! Adds a field list's records, giving the index of the first. A list too long +#! for one record goes on in another, added first, which the one before it +#! names with `LF_INDEX` as its last member. +#! @param this The table. +#! @param list The list. +#! @returns The index of its first record. +pub def type_table::fields(&mut this, list: &field_list): u32 { + # Where each record's members begin, as many to a record as it holds. + var firsts := vector::::new(); + firsts.append(0); + var size: u64 = 0; + for m in 0..list.starts.length { + val end := m + 1 < list.starts.length ? list.starts[m + 1] : list.bytes.length; + val bytes := end - list.starts[m]; + if size + bytes > fields_most && size != 0 { + firsts.append(m); + size = 0; + } + size += bytes; + } + + # The last record first, so each names one added before it. + val all := list.bytes.view(); + var next: u32 = 0; + var r := firsts.length; + while r > 0 { + r--; + val first := firsts[r]; + val from := first < list.starts.length ? list.starts[first] : list.bytes.length; + val to := r + 1 < firsts.length ? list.starts[firsts[r + 1]] : list.bytes.length; + var made := byte_writer::new(); + val at := begin_record(&mut made, lf_fieldlist); + if to > from { + made.put(span::::over(all.data + from, to - from)); + } + if next != 0 { + made.u16(lf_index as u64); + made.u16(0); + made.u32(next); + } + end_type_record(&mut made, at); + next = this.add(made.view()); + } + return next; +} + +#! The properties of a struct, a union, or an enum, by whether it is only +#! declared and whether it has a unique name. +def properties(forward: bool, unique: strview): u16 { + var made: u16 = forward ? property_forward : 0; + if unique.length != 0 { + made = made | property_unique; + } + return made; +} + +#! Adds a struct's record, or the record declaring it, hashed so a debugger +#! finds the definition the declaration names by its name. +#! @param this The table. +#! @param name Its name. +#! @param unique Its unique name, which tells it apart from another of its +#! name, or empty for none. +#! @param fields Its field list, or 0 where it is only declared. +#! @param count How many members the list holds. +#! @param size Its bytes. +#! @param forward Whether it is only declared, its definition elsewhere. +#! @returns Its index. +pub def type_table::structure(&mut this, name: strview, unique: strview, fields: u32, count: u16, size: u64, forward: bool): u32 { + var made := byte_writer::new(); + val at := begin_record(&mut made, lf_structure); + made.u16(count); + made.u16(properties(forward, unique)); + made.u32(fields); + made.u32(0); # Derived from nothing, + made.u32(0); # with no virtual functions. + put_unsigned(&mut made, size); + put_name(&mut made, name); + if unique.length != 0 { + put_name(&mut made, unique); + } + end_type_record(&mut made, at); + return this.add_tag(made.view(), name, forward); +} + +#! Adds a union's record, or the record declaring it, as `structure` does a +#! struct's. +#! @param this The table. +#! @param name Its name. +#! @param unique Its unique name, or empty for none. +#! @param fields Its field list, or 0 where it is only declared. +#! @param count How many members the list holds. +#! @param size Its bytes. +#! @param forward Whether it is only declared. +#! @returns Its index. +pub def type_table::union_of(&mut this, name: strview, unique: strview, fields: u32, count: u16, size: u64, forward: bool): u32 { + var made := byte_writer::new(); + val at := begin_record(&mut made, lf_union); + made.u16(count); + made.u16(properties(forward, unique)); + made.u32(fields); + put_unsigned(&mut made, size); + put_name(&mut made, name); + if unique.length != 0 { + put_name(&mut made, unique); + } + end_type_record(&mut made, at); + return this.add_tag(made.view(), name, forward); +} + +#! Adds an enum's record, as `structure` does a struct's. +#! @param this The table. +#! @param name Its name. +#! @param unique Its unique name, or empty for none. +#! @param underlying The integer it is held as. +#! @param fields Its field list, or 0 where it is only declared. +#! @param count How many members the list holds. +#! @param forward Whether it is only declared. +#! @returns Its index. +pub def type_table::enumeration(&mut this, name: strview, unique: strview, underlying: u32, fields: u32, count: u16, forward: bool): u32 { + var made := byte_writer::new(); + val at := begin_record(&mut made, lf_enum); + made.u16(count); + made.u16(properties(forward, unique)); + made.u32(underlying); + made.u32(fields); + put_name(&mut made, name); + if unique.length != 0 { + put_name(&mut made, unique); + } + end_type_record(&mut made, at); + return this.add_tag(made.view(), name, forward); +} + +# A struct's, union's, or enum's definition is hashed by its name, which a +# declaration is found to it by, and a declaration by its bytes. +def type_table::add_tag(&mut this, record: span, name: strview, forward: bool): u32 { + return this.add_hashed(record, forward ? hash_record(record) : hash_v1(name)); +} + +#! Adds the ID of a function, as the stream of IDs holds it, which an inline +#! site names what it inlined by, and the inlinees' lines say where it begins. +#! Each is a function of its own, though another has its name and type, as two +#! of one name in different modules have, so none is found as another's. +#! @param this The table. +#! @param scope The ID of the scope it is declared in, or 0. +#! @param type_index Its type, in the stream of types. +#! @param name Its name. +#! @returns Its index. +pub def type_table::function_id(&mut this, scope: u32, type_index: u32, name: strview): u32 { + var made := byte_writer::new(); + val at := begin_record(&mut made, lf_func_id); + made.u32(scope); + made.u32(type_index); + put_name(&mut made, name); + end_type_record(&mut made, at); + return this.add_distinct(made.view()); +} + +#! Adds a record, though the table holds one alike, hashed by its bytes. +#! @param this The table. +#! @param record The record. +#! @returns Its index. +pub def type_table::add_distinct(&mut this, record: span): u32 { + # It is held under its bytes and its index, which no other record's are. + var key := byte_writer::new(); + key.put(record); + key.u64(this.offsets.length); + _ = this.held.intern_view(key.view()); + val index := this.offsets.length as u32; + this.offsets.append(this.records.length as u32); + this.hashes.append(hash_record(record) % type_buckets); + this.records.put(record); + return first_type + index; +} + +#! The stream holding the records: its header, then the records. +#! @param this The table. +#! @param hash_stream The stream holding the records' hashes. +#! @returns The stream. +pub def type_table::stream(&this, hash_stream: u16): byte_writer { + val count := this.offsets.length; + val marks := this.index_offsets(); + var made := byte_writer::new(); + made.u32(20040203); # The version, + made.u32(56); # the header's bytes, + made.u32(first_type); # the first index, + made.u32(first_type + count); # and the one past the last, + made.u32(this.records.length); # the records' bytes, + made.u16(hash_stream); # the stream of hashes, + made.u16(0xFFFF); # and no second one, + made.u32(4); # each hash of four bytes, + made.u32(type_buckets); + made.u32(0); # the hashes, + made.u32(4 * count); + made.u32(4 * count); # where some records begin, + made.u32(4 * marks.length); + made.u32(4 * count + 4 * marks.length); # and no adjustments. + made.u32(0); + made.put(this.records.view()); + return made; +} + +#! The stream holding the records' hashes, then the index and offset of the +#! first record and of each one an 8 KB mark falls within, which a reader +#! finds a record from. +#! @param this The table. +#! @returns The stream. +pub def type_table::hash_stream(&this): byte_writer { + var made := byte_writer::new(); + for &each in this.hashes { + made.u32(each); + } + for &each in this.index_offsets() { + made.u32(each); + } + return made; +} + +# The index and offset of each record a reader begins from. +def type_table::index_offsets(&this): vector { + var made := vector::::new(); + val count := this.offsets.length; + for i in 0..count { + val begins := this.offsets[i] as u64; + val ends := i + 1 < count ? this.offsets[i + 1] as u64 : this.records.length; + if i == 0 || ends / 8192 > begins / 8192 { + made.append(first_type + i as u32); + made.append(begins as u32); + } + } + return made; +} diff --git a/pdb/writer.sakura b/pdb/writer.sakura new file mode 100644 index 0000000..f56010f --- /dev/null +++ b/pdb/writer.sakura @@ -0,0 +1,550 @@ +#! @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.view()); + streams[stream_records as u64] = move(&mut records); + var sections := byte_writer::new(); + sections.put(this.sections.view()); + 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.view()); + stream.put(this.modules[m].lines.view()); + 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.view()); + 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.view() { + 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.view()); + 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.view() { + 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.view()); + for &each in order.view() { + 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.view() { + 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.view() { + put_contribution(&mut parts, &this.contributions[each]); + } + + # The map of the sections, from their headers, then one of absolute places. + val headers := this.sections.view(); + 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.view() { + total += each.files.length; + } + files.u16(this.modules.length); + files.u16(total & 0xFFFF); + var first: u64 = 0; + for &each in this.modules.view() { + files.u16(first); + first += each.files.length; + } + for &each in this.modules.view() { + 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.view() { + for &path in each.files.view() { + 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.view()); + 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.view()); + made.put(parts.view()); + made.put(map.view()); + made.put(files.view()); + made.put(names.view()); + # 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; +} diff --git a/pe/debug.sakura b/pe/debug.sakura new file mode 100644 index 0000000..fdbc430 --- /dev/null +++ b/pe/debug.sakura @@ -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, 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, 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): 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::::over(file.data + table, 40 * count)); + return made; +} diff --git a/pe/pe.sakura b/pe/pe.sakura new file mode 100644 index 0000000..fb945de --- /dev/null +++ b/pe/pe.sakura @@ -0,0 +1,310 @@ +#! @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, 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 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; #! 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 = 3; #! 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
; #! In order of their addresses. + pub var imports: directory; #! The record of each library imported, then a record of zeros. + 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; #! 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::
::new(), + imports = directory {}, + exceptions = directory {}, + debug = directory {}, + iat = directory {}, + symbols = vector::::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, 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::::new(); + var raw := vector::::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.view()); + } + + # 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. + # 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[4] = [this.imports, this.exceptions, this.debug, this.iat]; + val indexes: u64[4] = [1, 3, 6, 12]; + for k in 0..4u { + 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.view(), 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.view(), file.get_u32(entry + 20) as u64); + file.set_u32(entry + 24, record); + } + } + return file; +} diff --git a/pe/unwind.sakura b/pe/unwind.sakura new file mode 100644 index 0000000..4c76686 --- /dev/null +++ b/pe/unwind.sakura @@ -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; #! Each code's slots, in the order the prologue did them. + var starts: vector; #! 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::::new(), starts = vector::::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; #! Each record, by its ID. + var at: vector; #! 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::::new(), at = vector::::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.view()); + 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.view()); + 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; +} + +#! Makes a table naming no functions. +#! @returns The table. +pub def function_table::new(): function_table { + return function_table { entries = vector::::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; +} diff --git a/tests/amd64/encoding.sakura b/tests/amd64/encoding.sakura new file mode 100644 index 0000000..a07e4f9 --- /dev/null +++ b/tests/amd64/encoding.sakura @@ -0,0 +1,927 @@ +# Tests of encoding instructions: each instruction's bytes, as GNU as writes them +# from the Intel syntax beside it, at every width and with every register a REX +# prefix or VEX reaches, then jumps and calls patched to their targets. +import std::io; +import binary::amd64; +import std::testing; + + +#! Whether code holds the bytes a reference assembler writes, given as hex, +#! emptying it for the next. +#! @param t The tester. +#! @param made The code. +#! @param expected The bytes, as hex. +def encodes(t: &mut tester, made: &mut assembly, expected: strview): void { + val digits := "0123456789abcdef"; + var text := string::new(nothing {}); + for &each in made.bytes.view() { + text.append(digits.substr((each >> 4) as u64, 1)); + text.append(digits.substr((each & 15) as u64, 1)); + } + if text.view() != expected { + println($"encoded as {text}, expected {expected}"); + } + t.expects(text.view() == expected); + made.bytes.clear(); +} + +# Each instruction's expected bytes were written by GNU as from the Intel syntax +# after it, so the encoder matches a reference assembler byte for byte. +@test def instructions_encode_as_a_reference_assembler_writes_them(t: &mut tester): void { + var a := assembly::new(); + a.mov(reg::rax, reg::rcx); encodes(t, &mut a, "4889c8"); # mov rax, rcx + a.mov(reg::rax, reg::rbp); encodes(t, &mut a, "4889e8"); # mov rax, rbp + a.mov(reg::rax, reg::r8); encodes(t, &mut a, "4c89c0"); # mov rax, r8 + a.mov(reg::rax, reg::r14); encodes(t, &mut a, "4c89f0"); # mov rax, r14 + a.mov(reg::rbx, reg::rcx); encodes(t, &mut a, "4889cb"); # mov rbx, rcx + a.mov(reg::rbx, reg::rbp); encodes(t, &mut a, "4889eb"); # mov rbx, rbp + a.mov(reg::rbx, reg::r8); encodes(t, &mut a, "4c89c3"); # mov rbx, r8 + a.mov(reg::rbx, reg::r14); encodes(t, &mut a, "4c89f3"); # mov rbx, r14 + a.mov(reg::rsp, reg::rcx); encodes(t, &mut a, "4889cc"); # mov rsp, rcx + a.mov(reg::rsp, reg::rbp); encodes(t, &mut a, "4889ec"); # mov rsp, rbp + a.mov(reg::rsp, reg::r8); encodes(t, &mut a, "4c89c4"); # mov rsp, r8 + a.mov(reg::rsp, reg::r14); encodes(t, &mut a, "4c89f4"); # mov rsp, r14 + a.mov(reg::r12, reg::rcx); encodes(t, &mut a, "4989cc"); # mov r12, rcx + a.mov(reg::r12, reg::rbp); encodes(t, &mut a, "4989ec"); # mov r12, rbp + a.mov(reg::r12, reg::r8); encodes(t, &mut a, "4d89c4"); # mov r12, r8 + a.mov(reg::r12, reg::r14); encodes(t, &mut a, "4d89f4"); # mov r12, r14 + a.mov(reg::r13, reg::rcx); encodes(t, &mut a, "4989cd"); # mov r13, rcx + a.mov(reg::r13, reg::rbp); encodes(t, &mut a, "4989ed"); # mov r13, rbp + a.mov(reg::r13, reg::r8); encodes(t, &mut a, "4d89c5"); # mov r13, r8 + a.mov(reg::r13, reg::r14); encodes(t, &mut a, "4d89f5"); # mov r13, r14 + a.mov(reg::r15, reg::rcx); encodes(t, &mut a, "4989cf"); # mov r15, rcx + a.mov(reg::r15, reg::rbp); encodes(t, &mut a, "4989ef"); # mov r15, rbp + a.mov(reg::r15, reg::r8); encodes(t, &mut a, "4d89c7"); # mov r15, r8 + a.mov(reg::r15, reg::r14); encodes(t, &mut a, "4d89f7"); # mov r15, r14 + a.mov_imm(reg::rax, 0x0); encodes(t, &mut a, "b800000000"); # mov eax, 0 + a.mov_imm(reg::r9, 0x0); encodes(t, &mut a, "41b900000000"); # mov r9d, 0 + a.mov_imm(reg::rax, 0x1); encodes(t, &mut a, "b801000000"); # mov eax, 1 + a.mov_imm(reg::r9, 0x1); encodes(t, &mut a, "41b901000000"); # mov r9d, 1 + a.mov_imm(reg::rax, 0x7FFFFFFF); encodes(t, &mut a, "b8ffffff7f"); # mov eax, 2147483647 + a.mov_imm(reg::r9, 0x7FFFFFFF); encodes(t, &mut a, "41b9ffffff7f"); # mov r9d, 2147483647 + a.mov_imm(reg::rax, 0xFFFFFFFF); encodes(t, &mut a, "b8ffffffff"); # mov eax, 4294967295 + a.mov_imm(reg::r9, 0xFFFFFFFF); encodes(t, &mut a, "41b9ffffffff"); # mov r9d, 4294967295 + a.mov_imm(reg::rax, 0x100000000); encodes(t, &mut a, "48b80000000001000000"); # movabs rax, 4294967296 + a.mov_imm(reg::r9, 0x100000000); encodes(t, &mut a, "49b90000000001000000"); # movabs r9, 4294967296 + a.mov_imm(reg::rax, 0xFFFFFFFFFFFFFFFF); encodes(t, &mut a, "48c7c0ffffffff"); # mov rax, -1 + a.mov_imm(reg::r9, 0xFFFFFFFFFFFFFFFF); encodes(t, &mut a, "49c7c1ffffffff"); # mov r9, -1 + a.mov_imm(reg::rax, 0xFFFFFFFF80000000); encodes(t, &mut a, "48c7c000000080"); # mov rax, -2147483648 + a.mov_imm(reg::r9, 0xFFFFFFFF80000000); encodes(t, &mut a, "49c7c100000080"); # mov r9, -2147483648 + a.mov_imm(reg::rax, 0x123456789ABCDEF0); encodes(t, &mut a, "48b8f0debc9a78563412"); # movabs rax, 1311768467463790320 + a.mov_imm(reg::r9, 0x123456789ABCDEF0); encodes(t, &mut a, "49b9f0debc9a78563412"); # movabs r9, 1311768467463790320 + a.load(1, false, reg::rax, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "0fb603"); # movzx eax, byte ptr [rbx] + a.load(1, false, reg::r11, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "440fb61b"); # movzx r11d, byte ptr [rbx] + a.load(1, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "0fb613"); # movzx edx, byte ptr [rbx] + a.load(1, true, reg::rax, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "480fbe03"); # movsx rax, byte ptr [rbx] + a.load(1, true, reg::r11, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "4c0fbe1b"); # movsx r11, byte ptr [rbx] + a.load(1, true, reg::rdx, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "480fbe13"); # movsx rdx, byte ptr [rbx] + a.store(1, indexed(reg::rbx, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "8803"); # mov byte ptr [rbx], al + a.store(1, indexed(reg::rbx, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "408833"); # mov byte ptr [rbx], sil + a.store(1, indexed(reg::rbx, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "448813"); # mov byte ptr [rbx], r10b + a.load(2, false, reg::rax, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "0fb703"); # movzx eax, word ptr [rbx] + a.load(2, false, reg::r11, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "440fb71b"); # movzx r11d, word ptr [rbx] + a.load(2, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "0fb713"); # movzx edx, word ptr [rbx] + a.load(2, true, reg::rax, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "480fbf03"); # movsx rax, word ptr [rbx] + a.load(2, true, reg::r11, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "4c0fbf1b"); # movsx r11, word ptr [rbx] + a.load(2, true, reg::rdx, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "480fbf13"); # movsx rdx, word ptr [rbx] + a.store(2, indexed(reg::rbx, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "668903"); # mov word ptr [rbx], ax + a.store(2, indexed(reg::rbx, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "668933"); # mov word ptr [rbx], si + a.store(2, indexed(reg::rbx, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "66448913"); # mov word ptr [rbx], r10w + a.load(4, false, reg::rax, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "8b03"); # mov eax, dword ptr [rbx] + a.load(4, false, reg::r11, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "448b1b"); # mov r11d, dword ptr [rbx] + a.load(4, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "8b13"); # mov edx, dword ptr [rbx] + a.load(4, true, reg::rax, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "486303"); # movsxd rax, dword ptr [rbx] + a.load(4, true, reg::r11, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "4c631b"); # movsxd r11, dword ptr [rbx] + a.load(4, true, reg::rdx, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "486313"); # movsxd rdx, dword ptr [rbx] + a.store(4, indexed(reg::rbx, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "8903"); # mov dword ptr [rbx], eax + a.store(4, indexed(reg::rbx, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "8933"); # mov dword ptr [rbx], esi + a.store(4, indexed(reg::rbx, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "448913"); # mov dword ptr [rbx], r10d + a.load(8, false, reg::rax, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "488b03"); # mov rax, qword ptr [rbx] + a.load(8, false, reg::r11, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "4c8b1b"); # mov r11, qword ptr [rbx] + a.load(8, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "488b13"); # mov rdx, qword ptr [rbx] + a.store(8, indexed(reg::rbx, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "488903"); # mov qword ptr [rbx], rax + a.store(8, indexed(reg::rbx, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "488933"); # mov qword ptr [rbx], rsi + a.store(8, indexed(reg::rbx, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "4c8913"); # mov qword ptr [rbx], r10 + a.store_imm(indexed(reg::rbx, reg::none, 1, 0), -5); encodes(t, &mut a, "48c703fbffffff"); # mov qword ptr [rbx], -5 + a.lea(reg::rdi, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "488d3b"); # lea rdi, [rbx] + a.alu_load(alu::add, reg::r9, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "4c030b"); # add r9, qword ptr [rbx] + a.alu_mem_imm(alu::cmp, indexed(reg::rbx, reg::none, 1, 0), 0); encodes(t, &mut a, "48833b00"); # cmp qword ptr [rbx], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::rbx, reg::none, 1, 0), 1000); encodes(t, &mut a, "48813be8030000"); # cmp qword ptr [rbx], 1000 + a.movss_load(1, indexed(reg::rbx, reg::none, 1, 0)); encodes(t, &mut a, "f30f100b"); # movss xmm1, dword ptr [rbx] + a.movss_store(indexed(reg::rbx, reg::none, 1, 0), 9); encodes(t, &mut a, "f3440f110b"); # movss dword ptr [rbx], xmm9 + a.load(1, false, reg::rax, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "0fb64310"); # movzx eax, byte ptr [rbx+16] + a.load(1, false, reg::r11, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "440fb65b10"); # movzx r11d, byte ptr [rbx+16] + a.load(1, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "0fb65310"); # movzx edx, byte ptr [rbx+16] + a.load(1, true, reg::rax, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "480fbe4310"); # movsx rax, byte ptr [rbx+16] + a.load(1, true, reg::r11, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "4c0fbe5b10"); # movsx r11, byte ptr [rbx+16] + a.load(1, true, reg::rdx, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "480fbe5310"); # movsx rdx, byte ptr [rbx+16] + a.store(1, indexed(reg::rbx, reg::none, 1, 16), reg::rax); encodes(t, &mut a, "884310"); # mov byte ptr [rbx+16], al + a.store(1, indexed(reg::rbx, reg::none, 1, 16), reg::rsi); encodes(t, &mut a, "40887310"); # mov byte ptr [rbx+16], sil + a.store(1, indexed(reg::rbx, reg::none, 1, 16), reg::r10); encodes(t, &mut a, "44885310"); # mov byte ptr [rbx+16], r10b + a.load(2, false, reg::rax, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "0fb74310"); # movzx eax, word ptr [rbx+16] + a.load(2, false, reg::r11, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "440fb75b10"); # movzx r11d, word ptr [rbx+16] + a.load(2, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "0fb75310"); # movzx edx, word ptr [rbx+16] + a.load(2, true, reg::rax, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "480fbf4310"); # movsx rax, word ptr [rbx+16] + a.load(2, true, reg::r11, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "4c0fbf5b10"); # movsx r11, word ptr [rbx+16] + a.load(2, true, reg::rdx, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "480fbf5310"); # movsx rdx, word ptr [rbx+16] + a.store(2, indexed(reg::rbx, reg::none, 1, 16), reg::rax); encodes(t, &mut a, "66894310"); # mov word ptr [rbx+16], ax + a.store(2, indexed(reg::rbx, reg::none, 1, 16), reg::rsi); encodes(t, &mut a, "66897310"); # mov word ptr [rbx+16], si + a.store(2, indexed(reg::rbx, reg::none, 1, 16), reg::r10); encodes(t, &mut a, "6644895310"); # mov word ptr [rbx+16], r10w + a.load(4, false, reg::rax, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "8b4310"); # mov eax, dword ptr [rbx+16] + a.load(4, false, reg::r11, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "448b5b10"); # mov r11d, dword ptr [rbx+16] + a.load(4, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "8b5310"); # mov edx, dword ptr [rbx+16] + a.load(4, true, reg::rax, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "48634310"); # movsxd rax, dword ptr [rbx+16] + a.load(4, true, reg::r11, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "4c635b10"); # movsxd r11, dword ptr [rbx+16] + a.load(4, true, reg::rdx, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "48635310"); # movsxd rdx, dword ptr [rbx+16] + a.store(4, indexed(reg::rbx, reg::none, 1, 16), reg::rax); encodes(t, &mut a, "894310"); # mov dword ptr [rbx+16], eax + a.store(4, indexed(reg::rbx, reg::none, 1, 16), reg::rsi); encodes(t, &mut a, "897310"); # mov dword ptr [rbx+16], esi + a.store(4, indexed(reg::rbx, reg::none, 1, 16), reg::r10); encodes(t, &mut a, "44895310"); # mov dword ptr [rbx+16], r10d + a.load(8, false, reg::rax, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "488b4310"); # mov rax, qword ptr [rbx+16] + a.load(8, false, reg::r11, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "4c8b5b10"); # mov r11, qword ptr [rbx+16] + a.load(8, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "488b5310"); # mov rdx, qword ptr [rbx+16] + a.store(8, indexed(reg::rbx, reg::none, 1, 16), reg::rax); encodes(t, &mut a, "48894310"); # mov qword ptr [rbx+16], rax + a.store(8, indexed(reg::rbx, reg::none, 1, 16), reg::rsi); encodes(t, &mut a, "48897310"); # mov qword ptr [rbx+16], rsi + a.store(8, indexed(reg::rbx, reg::none, 1, 16), reg::r10); encodes(t, &mut a, "4c895310"); # mov qword ptr [rbx+16], r10 + a.store_imm(indexed(reg::rbx, reg::none, 1, 16), -5); encodes(t, &mut a, "48c74310fbffffff"); # mov qword ptr [rbx+16], -5 + a.lea(reg::rdi, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "488d7b10"); # lea rdi, [rbx+16] + a.alu_load(alu::add, reg::r9, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "4c034b10"); # add r9, qword ptr [rbx+16] + a.alu_mem_imm(alu::cmp, indexed(reg::rbx, reg::none, 1, 16), 0); encodes(t, &mut a, "48837b1000"); # cmp qword ptr [rbx+16], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::rbx, reg::none, 1, 16), 1000); encodes(t, &mut a, "48817b10e8030000"); # cmp qword ptr [rbx+16], 1000 + a.movss_load(1, indexed(reg::rbx, reg::none, 1, 16)); encodes(t, &mut a, "f30f104b10"); # movss xmm1, dword ptr [rbx+16] + a.movss_store(indexed(reg::rbx, reg::none, 1, 16), 9); encodes(t, &mut a, "f3440f114b10"); # movss dword ptr [rbx+16], xmm9 + a.load(1, false, reg::rax, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "0fb643f8"); # movzx eax, byte ptr [rbx-8] + a.load(1, false, reg::r11, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "440fb65bf8"); # movzx r11d, byte ptr [rbx-8] + a.load(1, false, reg::rdx, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "0fb653f8"); # movzx edx, byte ptr [rbx-8] + a.load(1, true, reg::rax, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "480fbe43f8"); # movsx rax, byte ptr [rbx-8] + a.load(1, true, reg::r11, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "4c0fbe5bf8"); # movsx r11, byte ptr [rbx-8] + a.load(1, true, reg::rdx, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "480fbe53f8"); # movsx rdx, byte ptr [rbx-8] + a.store(1, indexed(reg::rbx, reg::none, 1, -8), reg::rax); encodes(t, &mut a, "8843f8"); # mov byte ptr [rbx-8], al + a.store(1, indexed(reg::rbx, reg::none, 1, -8), reg::rsi); encodes(t, &mut a, "408873f8"); # mov byte ptr [rbx-8], sil + a.store(1, indexed(reg::rbx, reg::none, 1, -8), reg::r10); encodes(t, &mut a, "448853f8"); # mov byte ptr [rbx-8], r10b + a.load(2, false, reg::rax, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "0fb743f8"); # movzx eax, word ptr [rbx-8] + a.load(2, false, reg::r11, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "440fb75bf8"); # movzx r11d, word ptr [rbx-8] + a.load(2, false, reg::rdx, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "0fb753f8"); # movzx edx, word ptr [rbx-8] + a.load(2, true, reg::rax, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "480fbf43f8"); # movsx rax, word ptr [rbx-8] + a.load(2, true, reg::r11, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "4c0fbf5bf8"); # movsx r11, word ptr [rbx-8] + a.load(2, true, reg::rdx, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "480fbf53f8"); # movsx rdx, word ptr [rbx-8] + a.store(2, indexed(reg::rbx, reg::none, 1, -8), reg::rax); encodes(t, &mut a, "668943f8"); # mov word ptr [rbx-8], ax + a.store(2, indexed(reg::rbx, reg::none, 1, -8), reg::rsi); encodes(t, &mut a, "668973f8"); # mov word ptr [rbx-8], si + a.store(2, indexed(reg::rbx, reg::none, 1, -8), reg::r10); encodes(t, &mut a, "66448953f8"); # mov word ptr [rbx-8], r10w + a.load(4, false, reg::rax, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "8b43f8"); # mov eax, dword ptr [rbx-8] + a.load(4, false, reg::r11, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "448b5bf8"); # mov r11d, dword ptr [rbx-8] + a.load(4, false, reg::rdx, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "8b53f8"); # mov edx, dword ptr [rbx-8] + a.load(4, true, reg::rax, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "486343f8"); # movsxd rax, dword ptr [rbx-8] + a.load(4, true, reg::r11, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "4c635bf8"); # movsxd r11, dword ptr [rbx-8] + a.load(4, true, reg::rdx, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "486353f8"); # movsxd rdx, dword ptr [rbx-8] + a.store(4, indexed(reg::rbx, reg::none, 1, -8), reg::rax); encodes(t, &mut a, "8943f8"); # mov dword ptr [rbx-8], eax + a.store(4, indexed(reg::rbx, reg::none, 1, -8), reg::rsi); encodes(t, &mut a, "8973f8"); # mov dword ptr [rbx-8], esi + a.store(4, indexed(reg::rbx, reg::none, 1, -8), reg::r10); encodes(t, &mut a, "448953f8"); # mov dword ptr [rbx-8], r10d + a.load(8, false, reg::rax, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "488b43f8"); # mov rax, qword ptr [rbx-8] + a.load(8, false, reg::r11, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "4c8b5bf8"); # mov r11, qword ptr [rbx-8] + a.load(8, false, reg::rdx, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "488b53f8"); # mov rdx, qword ptr [rbx-8] + a.store(8, indexed(reg::rbx, reg::none, 1, -8), reg::rax); encodes(t, &mut a, "488943f8"); # mov qword ptr [rbx-8], rax + a.store(8, indexed(reg::rbx, reg::none, 1, -8), reg::rsi); encodes(t, &mut a, "488973f8"); # mov qword ptr [rbx-8], rsi + a.store(8, indexed(reg::rbx, reg::none, 1, -8), reg::r10); encodes(t, &mut a, "4c8953f8"); # mov qword ptr [rbx-8], r10 + a.store_imm(indexed(reg::rbx, reg::none, 1, -8), -5); encodes(t, &mut a, "48c743f8fbffffff"); # mov qword ptr [rbx-8], -5 + a.lea(reg::rdi, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "488d7bf8"); # lea rdi, [rbx-8] + a.alu_load(alu::add, reg::r9, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "4c034bf8"); # add r9, qword ptr [rbx-8] + a.alu_mem_imm(alu::cmp, indexed(reg::rbx, reg::none, 1, -8), 0); encodes(t, &mut a, "48837bf800"); # cmp qword ptr [rbx-8], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::rbx, reg::none, 1, -8), 1000); encodes(t, &mut a, "48817bf8e8030000"); # cmp qword ptr [rbx-8], 1000 + a.movss_load(1, indexed(reg::rbx, reg::none, 1, -8)); encodes(t, &mut a, "f30f104bf8"); # movss xmm1, dword ptr [rbx-8] + a.movss_store(indexed(reg::rbx, reg::none, 1, -8), 9); encodes(t, &mut a, "f3440f114bf8"); # movss dword ptr [rbx-8], xmm9 + a.load(1, false, reg::rax, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "0fb68300100000"); # movzx eax, byte ptr [rbx+4096] + a.load(1, false, reg::r11, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "440fb69b00100000"); # movzx r11d, byte ptr [rbx+4096] + a.load(1, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "0fb69300100000"); # movzx edx, byte ptr [rbx+4096] + a.load(1, true, reg::rax, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "480fbe8300100000"); # movsx rax, byte ptr [rbx+4096] + a.load(1, true, reg::r11, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "4c0fbe9b00100000"); # movsx r11, byte ptr [rbx+4096] + a.load(1, true, reg::rdx, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "480fbe9300100000"); # movsx rdx, byte ptr [rbx+4096] + a.store(1, indexed(reg::rbx, reg::none, 1, 4096), reg::rax); encodes(t, &mut a, "888300100000"); # mov byte ptr [rbx+4096], al + a.store(1, indexed(reg::rbx, reg::none, 1, 4096), reg::rsi); encodes(t, &mut a, "4088b300100000"); # mov byte ptr [rbx+4096], sil + a.store(1, indexed(reg::rbx, reg::none, 1, 4096), reg::r10); encodes(t, &mut a, "44889300100000"); # mov byte ptr [rbx+4096], r10b + a.load(2, false, reg::rax, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "0fb78300100000"); # movzx eax, word ptr [rbx+4096] + a.load(2, false, reg::r11, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "440fb79b00100000"); # movzx r11d, word ptr [rbx+4096] + a.load(2, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "0fb79300100000"); # movzx edx, word ptr [rbx+4096] + a.load(2, true, reg::rax, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "480fbf8300100000"); # movsx rax, word ptr [rbx+4096] + a.load(2, true, reg::r11, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "4c0fbf9b00100000"); # movsx r11, word ptr [rbx+4096] + a.load(2, true, reg::rdx, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "480fbf9300100000"); # movsx rdx, word ptr [rbx+4096] + a.store(2, indexed(reg::rbx, reg::none, 1, 4096), reg::rax); encodes(t, &mut a, "66898300100000"); # mov word ptr [rbx+4096], ax + a.store(2, indexed(reg::rbx, reg::none, 1, 4096), reg::rsi); encodes(t, &mut a, "6689b300100000"); # mov word ptr [rbx+4096], si + a.store(2, indexed(reg::rbx, reg::none, 1, 4096), reg::r10); encodes(t, &mut a, "6644899300100000"); # mov word ptr [rbx+4096], r10w + a.load(4, false, reg::rax, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "8b8300100000"); # mov eax, dword ptr [rbx+4096] + a.load(4, false, reg::r11, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "448b9b00100000"); # mov r11d, dword ptr [rbx+4096] + a.load(4, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "8b9300100000"); # mov edx, dword ptr [rbx+4096] + a.load(4, true, reg::rax, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "48638300100000"); # movsxd rax, dword ptr [rbx+4096] + a.load(4, true, reg::r11, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "4c639b00100000"); # movsxd r11, dword ptr [rbx+4096] + a.load(4, true, reg::rdx, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "48639300100000"); # movsxd rdx, dword ptr [rbx+4096] + a.store(4, indexed(reg::rbx, reg::none, 1, 4096), reg::rax); encodes(t, &mut a, "898300100000"); # mov dword ptr [rbx+4096], eax + a.store(4, indexed(reg::rbx, reg::none, 1, 4096), reg::rsi); encodes(t, &mut a, "89b300100000"); # mov dword ptr [rbx+4096], esi + a.store(4, indexed(reg::rbx, reg::none, 1, 4096), reg::r10); encodes(t, &mut a, "44899300100000"); # mov dword ptr [rbx+4096], r10d + a.load(8, false, reg::rax, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "488b8300100000"); # mov rax, qword ptr [rbx+4096] + a.load(8, false, reg::r11, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "4c8b9b00100000"); # mov r11, qword ptr [rbx+4096] + a.load(8, false, reg::rdx, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "488b9300100000"); # mov rdx, qword ptr [rbx+4096] + a.store(8, indexed(reg::rbx, reg::none, 1, 4096), reg::rax); encodes(t, &mut a, "48898300100000"); # mov qword ptr [rbx+4096], rax + a.store(8, indexed(reg::rbx, reg::none, 1, 4096), reg::rsi); encodes(t, &mut a, "4889b300100000"); # mov qword ptr [rbx+4096], rsi + a.store(8, indexed(reg::rbx, reg::none, 1, 4096), reg::r10); encodes(t, &mut a, "4c899300100000"); # mov qword ptr [rbx+4096], r10 + a.store_imm(indexed(reg::rbx, reg::none, 1, 4096), -5); encodes(t, &mut a, "48c78300100000fbffffff"); # mov qword ptr [rbx+4096], -5 + a.lea(reg::rdi, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "488dbb00100000"); # lea rdi, [rbx+4096] + a.alu_load(alu::add, reg::r9, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "4c038b00100000"); # add r9, qword ptr [rbx+4096] + a.alu_mem_imm(alu::cmp, indexed(reg::rbx, reg::none, 1, 4096), 0); encodes(t, &mut a, "4883bb0010000000"); # cmp qword ptr [rbx+4096], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::rbx, reg::none, 1, 4096), 1000); encodes(t, &mut a, "4881bb00100000e8030000"); # cmp qword ptr [rbx+4096], 1000 + a.movss_load(1, indexed(reg::rbx, reg::none, 1, 4096)); encodes(t, &mut a, "f30f108b00100000"); # movss xmm1, dword ptr [rbx+4096] + a.movss_store(indexed(reg::rbx, reg::none, 1, 4096), 9); encodes(t, &mut a, "f3440f118b00100000"); # movss dword ptr [rbx+4096], xmm9 + a.load(1, false, reg::rax, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "0fb60424"); # movzx eax, byte ptr [rsp] + a.load(1, false, reg::r11, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "440fb61c24"); # movzx r11d, byte ptr [rsp] + a.load(1, false, reg::rdx, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "0fb61424"); # movzx edx, byte ptr [rsp] + a.load(1, true, reg::rax, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "480fbe0424"); # movsx rax, byte ptr [rsp] + a.load(1, true, reg::r11, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "4c0fbe1c24"); # movsx r11, byte ptr [rsp] + a.load(1, true, reg::rdx, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "480fbe1424"); # movsx rdx, byte ptr [rsp] + a.store(1, indexed(reg::rsp, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "880424"); # mov byte ptr [rsp], al + a.store(1, indexed(reg::rsp, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "40883424"); # mov byte ptr [rsp], sil + a.store(1, indexed(reg::rsp, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "44881424"); # mov byte ptr [rsp], r10b + a.load(2, false, reg::rax, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "0fb70424"); # movzx eax, word ptr [rsp] + a.load(2, false, reg::r11, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "440fb71c24"); # movzx r11d, word ptr [rsp] + a.load(2, false, reg::rdx, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "0fb71424"); # movzx edx, word ptr [rsp] + a.load(2, true, reg::rax, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "480fbf0424"); # movsx rax, word ptr [rsp] + a.load(2, true, reg::r11, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "4c0fbf1c24"); # movsx r11, word ptr [rsp] + a.load(2, true, reg::rdx, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "480fbf1424"); # movsx rdx, word ptr [rsp] + a.store(2, indexed(reg::rsp, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "66890424"); # mov word ptr [rsp], ax + a.store(2, indexed(reg::rsp, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "66893424"); # mov word ptr [rsp], si + a.store(2, indexed(reg::rsp, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "6644891424"); # mov word ptr [rsp], r10w + a.load(4, false, reg::rax, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "8b0424"); # mov eax, dword ptr [rsp] + a.load(4, false, reg::r11, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "448b1c24"); # mov r11d, dword ptr [rsp] + a.load(4, false, reg::rdx, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "8b1424"); # mov edx, dword ptr [rsp] + a.load(4, true, reg::rax, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "48630424"); # movsxd rax, dword ptr [rsp] + a.load(4, true, reg::r11, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "4c631c24"); # movsxd r11, dword ptr [rsp] + a.load(4, true, reg::rdx, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "48631424"); # movsxd rdx, dword ptr [rsp] + a.store(4, indexed(reg::rsp, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "890424"); # mov dword ptr [rsp], eax + a.store(4, indexed(reg::rsp, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "893424"); # mov dword ptr [rsp], esi + a.store(4, indexed(reg::rsp, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "44891424"); # mov dword ptr [rsp], r10d + a.load(8, false, reg::rax, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "488b0424"); # mov rax, qword ptr [rsp] + a.load(8, false, reg::r11, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "4c8b1c24"); # mov r11, qword ptr [rsp] + a.load(8, false, reg::rdx, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "488b1424"); # mov rdx, qword ptr [rsp] + a.store(8, indexed(reg::rsp, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "48890424"); # mov qword ptr [rsp], rax + a.store(8, indexed(reg::rsp, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "48893424"); # mov qword ptr [rsp], rsi + a.store(8, indexed(reg::rsp, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "4c891424"); # mov qword ptr [rsp], r10 + a.store_imm(indexed(reg::rsp, reg::none, 1, 0), -5); encodes(t, &mut a, "48c70424fbffffff"); # mov qword ptr [rsp], -5 + a.lea(reg::rdi, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "488d3c24"); # lea rdi, [rsp] + a.alu_load(alu::add, reg::r9, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "4c030c24"); # add r9, qword ptr [rsp] + a.alu_mem_imm(alu::cmp, indexed(reg::rsp, reg::none, 1, 0), 0); encodes(t, &mut a, "48833c2400"); # cmp qword ptr [rsp], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::rsp, reg::none, 1, 0), 1000); encodes(t, &mut a, "48813c24e8030000"); # cmp qword ptr [rsp], 1000 + a.movss_load(1, indexed(reg::rsp, reg::none, 1, 0)); encodes(t, &mut a, "f30f100c24"); # movss xmm1, dword ptr [rsp] + a.movss_store(indexed(reg::rsp, reg::none, 1, 0), 9); encodes(t, &mut a, "f3440f110c24"); # movss dword ptr [rsp], xmm9 + a.load(1, false, reg::rax, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "0fb6442408"); # movzx eax, byte ptr [rsp+8] + a.load(1, false, reg::r11, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "440fb65c2408"); # movzx r11d, byte ptr [rsp+8] + a.load(1, false, reg::rdx, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "0fb6542408"); # movzx edx, byte ptr [rsp+8] + a.load(1, true, reg::rax, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "480fbe442408"); # movsx rax, byte ptr [rsp+8] + a.load(1, true, reg::r11, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "4c0fbe5c2408"); # movsx r11, byte ptr [rsp+8] + a.load(1, true, reg::rdx, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "480fbe542408"); # movsx rdx, byte ptr [rsp+8] + a.store(1, indexed(reg::rsp, reg::none, 1, 8), reg::rax); encodes(t, &mut a, "88442408"); # mov byte ptr [rsp+8], al + a.store(1, indexed(reg::rsp, reg::none, 1, 8), reg::rsi); encodes(t, &mut a, "4088742408"); # mov byte ptr [rsp+8], sil + a.store(1, indexed(reg::rsp, reg::none, 1, 8), reg::r10); encodes(t, &mut a, "4488542408"); # mov byte ptr [rsp+8], r10b + a.load(2, false, reg::rax, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "0fb7442408"); # movzx eax, word ptr [rsp+8] + a.load(2, false, reg::r11, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "440fb75c2408"); # movzx r11d, word ptr [rsp+8] + a.load(2, false, reg::rdx, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "0fb7542408"); # movzx edx, word ptr [rsp+8] + a.load(2, true, reg::rax, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "480fbf442408"); # movsx rax, word ptr [rsp+8] + a.load(2, true, reg::r11, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "4c0fbf5c2408"); # movsx r11, word ptr [rsp+8] + a.load(2, true, reg::rdx, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "480fbf542408"); # movsx rdx, word ptr [rsp+8] + a.store(2, indexed(reg::rsp, reg::none, 1, 8), reg::rax); encodes(t, &mut a, "6689442408"); # mov word ptr [rsp+8], ax + a.store(2, indexed(reg::rsp, reg::none, 1, 8), reg::rsi); encodes(t, &mut a, "6689742408"); # mov word ptr [rsp+8], si + a.store(2, indexed(reg::rsp, reg::none, 1, 8), reg::r10); encodes(t, &mut a, "664489542408"); # mov word ptr [rsp+8], r10w + a.load(4, false, reg::rax, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "8b442408"); # mov eax, dword ptr [rsp+8] + a.load(4, false, reg::r11, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "448b5c2408"); # mov r11d, dword ptr [rsp+8] + a.load(4, false, reg::rdx, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "8b542408"); # mov edx, dword ptr [rsp+8] + a.load(4, true, reg::rax, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "4863442408"); # movsxd rax, dword ptr [rsp+8] + a.load(4, true, reg::r11, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "4c635c2408"); # movsxd r11, dword ptr [rsp+8] + a.load(4, true, reg::rdx, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "4863542408"); # movsxd rdx, dword ptr [rsp+8] + a.store(4, indexed(reg::rsp, reg::none, 1, 8), reg::rax); encodes(t, &mut a, "89442408"); # mov dword ptr [rsp+8], eax + a.store(4, indexed(reg::rsp, reg::none, 1, 8), reg::rsi); encodes(t, &mut a, "89742408"); # mov dword ptr [rsp+8], esi + a.store(4, indexed(reg::rsp, reg::none, 1, 8), reg::r10); encodes(t, &mut a, "4489542408"); # mov dword ptr [rsp+8], r10d + a.load(8, false, reg::rax, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "488b442408"); # mov rax, qword ptr [rsp+8] + a.load(8, false, reg::r11, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "4c8b5c2408"); # mov r11, qword ptr [rsp+8] + a.load(8, false, reg::rdx, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "488b542408"); # mov rdx, qword ptr [rsp+8] + a.store(8, indexed(reg::rsp, reg::none, 1, 8), reg::rax); encodes(t, &mut a, "4889442408"); # mov qword ptr [rsp+8], rax + a.store(8, indexed(reg::rsp, reg::none, 1, 8), reg::rsi); encodes(t, &mut a, "4889742408"); # mov qword ptr [rsp+8], rsi + a.store(8, indexed(reg::rsp, reg::none, 1, 8), reg::r10); encodes(t, &mut a, "4c89542408"); # mov qword ptr [rsp+8], r10 + a.store_imm(indexed(reg::rsp, reg::none, 1, 8), -5); encodes(t, &mut a, "48c7442408fbffffff"); # mov qword ptr [rsp+8], -5 + a.lea(reg::rdi, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "488d7c2408"); # lea rdi, [rsp+8] + a.alu_load(alu::add, reg::r9, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "4c034c2408"); # add r9, qword ptr [rsp+8] + a.alu_mem_imm(alu::cmp, indexed(reg::rsp, reg::none, 1, 8), 0); encodes(t, &mut a, "48837c240800"); # cmp qword ptr [rsp+8], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::rsp, reg::none, 1, 8), 1000); encodes(t, &mut a, "48817c2408e8030000"); # cmp qword ptr [rsp+8], 1000 + a.movss_load(1, indexed(reg::rsp, reg::none, 1, 8)); encodes(t, &mut a, "f30f104c2408"); # movss xmm1, dword ptr [rsp+8] + a.movss_store(indexed(reg::rsp, reg::none, 1, 8), 9); encodes(t, &mut a, "f3440f114c2408"); # movss dword ptr [rsp+8], xmm9 + a.load(1, false, reg::rax, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "0fb64500"); # movzx eax, byte ptr [rbp] + a.load(1, false, reg::r11, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "440fb65d00"); # movzx r11d, byte ptr [rbp] + a.load(1, false, reg::rdx, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "0fb65500"); # movzx edx, byte ptr [rbp] + a.load(1, true, reg::rax, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "480fbe4500"); # movsx rax, byte ptr [rbp] + a.load(1, true, reg::r11, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "4c0fbe5d00"); # movsx r11, byte ptr [rbp] + a.load(1, true, reg::rdx, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "480fbe5500"); # movsx rdx, byte ptr [rbp] + a.store(1, indexed(reg::rbp, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "884500"); # mov byte ptr [rbp], al + a.store(1, indexed(reg::rbp, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "40887500"); # mov byte ptr [rbp], sil + a.store(1, indexed(reg::rbp, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "44885500"); # mov byte ptr [rbp], r10b + a.load(2, false, reg::rax, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "0fb74500"); # movzx eax, word ptr [rbp] + a.load(2, false, reg::r11, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "440fb75d00"); # movzx r11d, word ptr [rbp] + a.load(2, false, reg::rdx, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "0fb75500"); # movzx edx, word ptr [rbp] + a.load(2, true, reg::rax, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "480fbf4500"); # movsx rax, word ptr [rbp] + a.load(2, true, reg::r11, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "4c0fbf5d00"); # movsx r11, word ptr [rbp] + a.load(2, true, reg::rdx, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "480fbf5500"); # movsx rdx, word ptr [rbp] + a.store(2, indexed(reg::rbp, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "66894500"); # mov word ptr [rbp], ax + a.store(2, indexed(reg::rbp, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "66897500"); # mov word ptr [rbp], si + a.store(2, indexed(reg::rbp, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "6644895500"); # mov word ptr [rbp], r10w + a.load(4, false, reg::rax, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "8b4500"); # mov eax, dword ptr [rbp] + a.load(4, false, reg::r11, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "448b5d00"); # mov r11d, dword ptr [rbp] + a.load(4, false, reg::rdx, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "8b5500"); # mov edx, dword ptr [rbp] + a.load(4, true, reg::rax, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "48634500"); # movsxd rax, dword ptr [rbp] + a.load(4, true, reg::r11, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "4c635d00"); # movsxd r11, dword ptr [rbp] + a.load(4, true, reg::rdx, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "48635500"); # movsxd rdx, dword ptr [rbp] + a.store(4, indexed(reg::rbp, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "894500"); # mov dword ptr [rbp], eax + a.store(4, indexed(reg::rbp, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "897500"); # mov dword ptr [rbp], esi + a.store(4, indexed(reg::rbp, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "44895500"); # mov dword ptr [rbp], r10d + a.load(8, false, reg::rax, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "488b4500"); # mov rax, qword ptr [rbp] + a.load(8, false, reg::r11, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "4c8b5d00"); # mov r11, qword ptr [rbp] + a.load(8, false, reg::rdx, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "488b5500"); # mov rdx, qword ptr [rbp] + a.store(8, indexed(reg::rbp, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "48894500"); # mov qword ptr [rbp], rax + a.store(8, indexed(reg::rbp, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "48897500"); # mov qword ptr [rbp], rsi + a.store(8, indexed(reg::rbp, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "4c895500"); # mov qword ptr [rbp], r10 + a.store_imm(indexed(reg::rbp, reg::none, 1, 0), -5); encodes(t, &mut a, "48c74500fbffffff"); # mov qword ptr [rbp], -5 + a.lea(reg::rdi, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "488d7d00"); # lea rdi, [rbp] + a.alu_load(alu::add, reg::r9, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "4c034d00"); # add r9, qword ptr [rbp] + a.alu_mem_imm(alu::cmp, indexed(reg::rbp, reg::none, 1, 0), 0); encodes(t, &mut a, "48837d0000"); # cmp qword ptr [rbp], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::rbp, reg::none, 1, 0), 1000); encodes(t, &mut a, "48817d00e8030000"); # cmp qword ptr [rbp], 1000 + a.movss_load(1, indexed(reg::rbp, reg::none, 1, 0)); encodes(t, &mut a, "f30f104d00"); # movss xmm1, dword ptr [rbp] + a.movss_store(indexed(reg::rbp, reg::none, 1, 0), 9); encodes(t, &mut a, "f3440f114d00"); # movss dword ptr [rbp], xmm9 + a.load(1, false, reg::rax, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "0fb64518"); # movzx eax, byte ptr [rbp+24] + a.load(1, false, reg::r11, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "440fb65d18"); # movzx r11d, byte ptr [rbp+24] + a.load(1, false, reg::rdx, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "0fb65518"); # movzx edx, byte ptr [rbp+24] + a.load(1, true, reg::rax, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "480fbe4518"); # movsx rax, byte ptr [rbp+24] + a.load(1, true, reg::r11, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "4c0fbe5d18"); # movsx r11, byte ptr [rbp+24] + a.load(1, true, reg::rdx, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "480fbe5518"); # movsx rdx, byte ptr [rbp+24] + a.store(1, indexed(reg::rbp, reg::none, 1, 24), reg::rax); encodes(t, &mut a, "884518"); # mov byte ptr [rbp+24], al + a.store(1, indexed(reg::rbp, reg::none, 1, 24), reg::rsi); encodes(t, &mut a, "40887518"); # mov byte ptr [rbp+24], sil + a.store(1, indexed(reg::rbp, reg::none, 1, 24), reg::r10); encodes(t, &mut a, "44885518"); # mov byte ptr [rbp+24], r10b + a.load(2, false, reg::rax, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "0fb74518"); # movzx eax, word ptr [rbp+24] + a.load(2, false, reg::r11, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "440fb75d18"); # movzx r11d, word ptr [rbp+24] + a.load(2, false, reg::rdx, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "0fb75518"); # movzx edx, word ptr [rbp+24] + a.load(2, true, reg::rax, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "480fbf4518"); # movsx rax, word ptr [rbp+24] + a.load(2, true, reg::r11, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "4c0fbf5d18"); # movsx r11, word ptr [rbp+24] + a.load(2, true, reg::rdx, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "480fbf5518"); # movsx rdx, word ptr [rbp+24] + a.store(2, indexed(reg::rbp, reg::none, 1, 24), reg::rax); encodes(t, &mut a, "66894518"); # mov word ptr [rbp+24], ax + a.store(2, indexed(reg::rbp, reg::none, 1, 24), reg::rsi); encodes(t, &mut a, "66897518"); # mov word ptr [rbp+24], si + a.store(2, indexed(reg::rbp, reg::none, 1, 24), reg::r10); encodes(t, &mut a, "6644895518"); # mov word ptr [rbp+24], r10w + a.load(4, false, reg::rax, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "8b4518"); # mov eax, dword ptr [rbp+24] + a.load(4, false, reg::r11, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "448b5d18"); # mov r11d, dword ptr [rbp+24] + a.load(4, false, reg::rdx, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "8b5518"); # mov edx, dword ptr [rbp+24] + a.load(4, true, reg::rax, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "48634518"); # movsxd rax, dword ptr [rbp+24] + a.load(4, true, reg::r11, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "4c635d18"); # movsxd r11, dword ptr [rbp+24] + a.load(4, true, reg::rdx, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "48635518"); # movsxd rdx, dword ptr [rbp+24] + a.store(4, indexed(reg::rbp, reg::none, 1, 24), reg::rax); encodes(t, &mut a, "894518"); # mov dword ptr [rbp+24], eax + a.store(4, indexed(reg::rbp, reg::none, 1, 24), reg::rsi); encodes(t, &mut a, "897518"); # mov dword ptr [rbp+24], esi + a.store(4, indexed(reg::rbp, reg::none, 1, 24), reg::r10); encodes(t, &mut a, "44895518"); # mov dword ptr [rbp+24], r10d + a.load(8, false, reg::rax, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "488b4518"); # mov rax, qword ptr [rbp+24] + a.load(8, false, reg::r11, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "4c8b5d18"); # mov r11, qword ptr [rbp+24] + a.load(8, false, reg::rdx, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "488b5518"); # mov rdx, qword ptr [rbp+24] + a.store(8, indexed(reg::rbp, reg::none, 1, 24), reg::rax); encodes(t, &mut a, "48894518"); # mov qword ptr [rbp+24], rax + a.store(8, indexed(reg::rbp, reg::none, 1, 24), reg::rsi); encodes(t, &mut a, "48897518"); # mov qword ptr [rbp+24], rsi + a.store(8, indexed(reg::rbp, reg::none, 1, 24), reg::r10); encodes(t, &mut a, "4c895518"); # mov qword ptr [rbp+24], r10 + a.store_imm(indexed(reg::rbp, reg::none, 1, 24), -5); encodes(t, &mut a, "48c74518fbffffff"); # mov qword ptr [rbp+24], -5 + a.lea(reg::rdi, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "488d7d18"); # lea rdi, [rbp+24] + a.alu_load(alu::add, reg::r9, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "4c034d18"); # add r9, qword ptr [rbp+24] + a.alu_mem_imm(alu::cmp, indexed(reg::rbp, reg::none, 1, 24), 0); encodes(t, &mut a, "48837d1800"); # cmp qword ptr [rbp+24], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::rbp, reg::none, 1, 24), 1000); encodes(t, &mut a, "48817d18e8030000"); # cmp qword ptr [rbp+24], 1000 + a.movss_load(1, indexed(reg::rbp, reg::none, 1, 24)); encodes(t, &mut a, "f30f104d18"); # movss xmm1, dword ptr [rbp+24] + a.movss_store(indexed(reg::rbp, reg::none, 1, 24), 9); encodes(t, &mut a, "f3440f114d18"); # movss dword ptr [rbp+24], xmm9 + a.load(1, false, reg::rax, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "410fb60424"); # movzx eax, byte ptr [r12] + a.load(1, false, reg::r11, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "450fb61c24"); # movzx r11d, byte ptr [r12] + a.load(1, false, reg::rdx, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "410fb61424"); # movzx edx, byte ptr [r12] + a.load(1, true, reg::rax, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "490fbe0424"); # movsx rax, byte ptr [r12] + a.load(1, true, reg::r11, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "4d0fbe1c24"); # movsx r11, byte ptr [r12] + a.load(1, true, reg::rdx, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "490fbe1424"); # movsx rdx, byte ptr [r12] + a.store(1, indexed(reg::r12, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "41880424"); # mov byte ptr [r12], al + a.store(1, indexed(reg::r12, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "41883424"); # mov byte ptr [r12], sil + a.store(1, indexed(reg::r12, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "45881424"); # mov byte ptr [r12], r10b + a.load(2, false, reg::rax, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "410fb70424"); # movzx eax, word ptr [r12] + a.load(2, false, reg::r11, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "450fb71c24"); # movzx r11d, word ptr [r12] + a.load(2, false, reg::rdx, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "410fb71424"); # movzx edx, word ptr [r12] + a.load(2, true, reg::rax, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "490fbf0424"); # movsx rax, word ptr [r12] + a.load(2, true, reg::r11, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "4d0fbf1c24"); # movsx r11, word ptr [r12] + a.load(2, true, reg::rdx, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "490fbf1424"); # movsx rdx, word ptr [r12] + a.store(2, indexed(reg::r12, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "6641890424"); # mov word ptr [r12], ax + a.store(2, indexed(reg::r12, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "6641893424"); # mov word ptr [r12], si + a.store(2, indexed(reg::r12, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "6645891424"); # mov word ptr [r12], r10w + a.load(4, false, reg::rax, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "418b0424"); # mov eax, dword ptr [r12] + a.load(4, false, reg::r11, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "458b1c24"); # mov r11d, dword ptr [r12] + a.load(4, false, reg::rdx, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "418b1424"); # mov edx, dword ptr [r12] + a.load(4, true, reg::rax, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "49630424"); # movsxd rax, dword ptr [r12] + a.load(4, true, reg::r11, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "4d631c24"); # movsxd r11, dword ptr [r12] + a.load(4, true, reg::rdx, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "49631424"); # movsxd rdx, dword ptr [r12] + a.store(4, indexed(reg::r12, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "41890424"); # mov dword ptr [r12], eax + a.store(4, indexed(reg::r12, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "41893424"); # mov dword ptr [r12], esi + a.store(4, indexed(reg::r12, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "45891424"); # mov dword ptr [r12], r10d + a.load(8, false, reg::rax, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "498b0424"); # mov rax, qword ptr [r12] + a.load(8, false, reg::r11, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "4d8b1c24"); # mov r11, qword ptr [r12] + a.load(8, false, reg::rdx, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "498b1424"); # mov rdx, qword ptr [r12] + a.store(8, indexed(reg::r12, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "49890424"); # mov qword ptr [r12], rax + a.store(8, indexed(reg::r12, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "49893424"); # mov qword ptr [r12], rsi + a.store(8, indexed(reg::r12, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "4d891424"); # mov qword ptr [r12], r10 + a.store_imm(indexed(reg::r12, reg::none, 1, 0), -5); encodes(t, &mut a, "49c70424fbffffff"); # mov qword ptr [r12], -5 + a.lea(reg::rdi, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "498d3c24"); # lea rdi, [r12] + a.alu_load(alu::add, reg::r9, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "4d030c24"); # add r9, qword ptr [r12] + a.alu_mem_imm(alu::cmp, indexed(reg::r12, reg::none, 1, 0), 0); encodes(t, &mut a, "49833c2400"); # cmp qword ptr [r12], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::r12, reg::none, 1, 0), 1000); encodes(t, &mut a, "49813c24e8030000"); # cmp qword ptr [r12], 1000 + a.movss_load(1, indexed(reg::r12, reg::none, 1, 0)); encodes(t, &mut a, "f3410f100c24"); # movss xmm1, dword ptr [r12] + a.movss_store(indexed(reg::r12, reg::none, 1, 0), 9); encodes(t, &mut a, "f3450f110c24"); # movss dword ptr [r12], xmm9 + a.load(1, false, reg::rax, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "410fb6442428"); # movzx eax, byte ptr [r12+40] + a.load(1, false, reg::r11, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "450fb65c2428"); # movzx r11d, byte ptr [r12+40] + a.load(1, false, reg::rdx, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "410fb6542428"); # movzx edx, byte ptr [r12+40] + a.load(1, true, reg::rax, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "490fbe442428"); # movsx rax, byte ptr [r12+40] + a.load(1, true, reg::r11, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "4d0fbe5c2428"); # movsx r11, byte ptr [r12+40] + a.load(1, true, reg::rdx, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "490fbe542428"); # movsx rdx, byte ptr [r12+40] + a.store(1, indexed(reg::r12, reg::none, 1, 40), reg::rax); encodes(t, &mut a, "4188442428"); # mov byte ptr [r12+40], al + a.store(1, indexed(reg::r12, reg::none, 1, 40), reg::rsi); encodes(t, &mut a, "4188742428"); # mov byte ptr [r12+40], sil + a.store(1, indexed(reg::r12, reg::none, 1, 40), reg::r10); encodes(t, &mut a, "4588542428"); # mov byte ptr [r12+40], r10b + a.load(2, false, reg::rax, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "410fb7442428"); # movzx eax, word ptr [r12+40] + a.load(2, false, reg::r11, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "450fb75c2428"); # movzx r11d, word ptr [r12+40] + a.load(2, false, reg::rdx, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "410fb7542428"); # movzx edx, word ptr [r12+40] + a.load(2, true, reg::rax, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "490fbf442428"); # movsx rax, word ptr [r12+40] + a.load(2, true, reg::r11, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "4d0fbf5c2428"); # movsx r11, word ptr [r12+40] + a.load(2, true, reg::rdx, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "490fbf542428"); # movsx rdx, word ptr [r12+40] + a.store(2, indexed(reg::r12, reg::none, 1, 40), reg::rax); encodes(t, &mut a, "664189442428"); # mov word ptr [r12+40], ax + a.store(2, indexed(reg::r12, reg::none, 1, 40), reg::rsi); encodes(t, &mut a, "664189742428"); # mov word ptr [r12+40], si + a.store(2, indexed(reg::r12, reg::none, 1, 40), reg::r10); encodes(t, &mut a, "664589542428"); # mov word ptr [r12+40], r10w + a.load(4, false, reg::rax, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "418b442428"); # mov eax, dword ptr [r12+40] + a.load(4, false, reg::r11, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "458b5c2428"); # mov r11d, dword ptr [r12+40] + a.load(4, false, reg::rdx, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "418b542428"); # mov edx, dword ptr [r12+40] + a.load(4, true, reg::rax, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "4963442428"); # movsxd rax, dword ptr [r12+40] + a.load(4, true, reg::r11, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "4d635c2428"); # movsxd r11, dword ptr [r12+40] + a.load(4, true, reg::rdx, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "4963542428"); # movsxd rdx, dword ptr [r12+40] + a.store(4, indexed(reg::r12, reg::none, 1, 40), reg::rax); encodes(t, &mut a, "4189442428"); # mov dword ptr [r12+40], eax + a.store(4, indexed(reg::r12, reg::none, 1, 40), reg::rsi); encodes(t, &mut a, "4189742428"); # mov dword ptr [r12+40], esi + a.store(4, indexed(reg::r12, reg::none, 1, 40), reg::r10); encodes(t, &mut a, "4589542428"); # mov dword ptr [r12+40], r10d + a.load(8, false, reg::rax, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "498b442428"); # mov rax, qword ptr [r12+40] + a.load(8, false, reg::r11, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "4d8b5c2428"); # mov r11, qword ptr [r12+40] + a.load(8, false, reg::rdx, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "498b542428"); # mov rdx, qword ptr [r12+40] + a.store(8, indexed(reg::r12, reg::none, 1, 40), reg::rax); encodes(t, &mut a, "4989442428"); # mov qword ptr [r12+40], rax + a.store(8, indexed(reg::r12, reg::none, 1, 40), reg::rsi); encodes(t, &mut a, "4989742428"); # mov qword ptr [r12+40], rsi + a.store(8, indexed(reg::r12, reg::none, 1, 40), reg::r10); encodes(t, &mut a, "4d89542428"); # mov qword ptr [r12+40], r10 + a.store_imm(indexed(reg::r12, reg::none, 1, 40), -5); encodes(t, &mut a, "49c7442428fbffffff"); # mov qword ptr [r12+40], -5 + a.lea(reg::rdi, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "498d7c2428"); # lea rdi, [r12+40] + a.alu_load(alu::add, reg::r9, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "4d034c2428"); # add r9, qword ptr [r12+40] + a.alu_mem_imm(alu::cmp, indexed(reg::r12, reg::none, 1, 40), 0); encodes(t, &mut a, "49837c242800"); # cmp qword ptr [r12+40], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::r12, reg::none, 1, 40), 1000); encodes(t, &mut a, "49817c2428e8030000"); # cmp qword ptr [r12+40], 1000 + a.movss_load(1, indexed(reg::r12, reg::none, 1, 40)); encodes(t, &mut a, "f3410f104c2428"); # movss xmm1, dword ptr [r12+40] + a.movss_store(indexed(reg::r12, reg::none, 1, 40), 9); encodes(t, &mut a, "f3450f114c2428"); # movss dword ptr [r12+40], xmm9 + a.load(1, false, reg::rax, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "410fb64500"); # movzx eax, byte ptr [r13] + a.load(1, false, reg::r11, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "450fb65d00"); # movzx r11d, byte ptr [r13] + a.load(1, false, reg::rdx, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "410fb65500"); # movzx edx, byte ptr [r13] + a.load(1, true, reg::rax, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "490fbe4500"); # movsx rax, byte ptr [r13] + a.load(1, true, reg::r11, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "4d0fbe5d00"); # movsx r11, byte ptr [r13] + a.load(1, true, reg::rdx, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "490fbe5500"); # movsx rdx, byte ptr [r13] + a.store(1, indexed(reg::r13, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "41884500"); # mov byte ptr [r13], al + a.store(1, indexed(reg::r13, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "41887500"); # mov byte ptr [r13], sil + a.store(1, indexed(reg::r13, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "45885500"); # mov byte ptr [r13], r10b + a.load(2, false, reg::rax, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "410fb74500"); # movzx eax, word ptr [r13] + a.load(2, false, reg::r11, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "450fb75d00"); # movzx r11d, word ptr [r13] + a.load(2, false, reg::rdx, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "410fb75500"); # movzx edx, word ptr [r13] + a.load(2, true, reg::rax, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "490fbf4500"); # movsx rax, word ptr [r13] + a.load(2, true, reg::r11, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "4d0fbf5d00"); # movsx r11, word ptr [r13] + a.load(2, true, reg::rdx, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "490fbf5500"); # movsx rdx, word ptr [r13] + a.store(2, indexed(reg::r13, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "6641894500"); # mov word ptr [r13], ax + a.store(2, indexed(reg::r13, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "6641897500"); # mov word ptr [r13], si + a.store(2, indexed(reg::r13, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "6645895500"); # mov word ptr [r13], r10w + a.load(4, false, reg::rax, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "418b4500"); # mov eax, dword ptr [r13] + a.load(4, false, reg::r11, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "458b5d00"); # mov r11d, dword ptr [r13] + a.load(4, false, reg::rdx, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "418b5500"); # mov edx, dword ptr [r13] + a.load(4, true, reg::rax, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "49634500"); # movsxd rax, dword ptr [r13] + a.load(4, true, reg::r11, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "4d635d00"); # movsxd r11, dword ptr [r13] + a.load(4, true, reg::rdx, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "49635500"); # movsxd rdx, dword ptr [r13] + a.store(4, indexed(reg::r13, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "41894500"); # mov dword ptr [r13], eax + a.store(4, indexed(reg::r13, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "41897500"); # mov dword ptr [r13], esi + a.store(4, indexed(reg::r13, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "45895500"); # mov dword ptr [r13], r10d + a.load(8, false, reg::rax, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "498b4500"); # mov rax, qword ptr [r13] + a.load(8, false, reg::r11, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "4d8b5d00"); # mov r11, qword ptr [r13] + a.load(8, false, reg::rdx, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "498b5500"); # mov rdx, qword ptr [r13] + a.store(8, indexed(reg::r13, reg::none, 1, 0), reg::rax); encodes(t, &mut a, "49894500"); # mov qword ptr [r13], rax + a.store(8, indexed(reg::r13, reg::none, 1, 0), reg::rsi); encodes(t, &mut a, "49897500"); # mov qword ptr [r13], rsi + a.store(8, indexed(reg::r13, reg::none, 1, 0), reg::r10); encodes(t, &mut a, "4d895500"); # mov qword ptr [r13], r10 + a.store_imm(indexed(reg::r13, reg::none, 1, 0), -5); encodes(t, &mut a, "49c74500fbffffff"); # mov qword ptr [r13], -5 + a.lea(reg::rdi, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "498d7d00"); # lea rdi, [r13] + a.alu_load(alu::add, reg::r9, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "4d034d00"); # add r9, qword ptr [r13] + a.alu_mem_imm(alu::cmp, indexed(reg::r13, reg::none, 1, 0), 0); encodes(t, &mut a, "49837d0000"); # cmp qword ptr [r13], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::r13, reg::none, 1, 0), 1000); encodes(t, &mut a, "49817d00e8030000"); # cmp qword ptr [r13], 1000 + a.movss_load(1, indexed(reg::r13, reg::none, 1, 0)); encodes(t, &mut a, "f3410f104d00"); # movss xmm1, dword ptr [r13] + a.movss_store(indexed(reg::r13, reg::none, 1, 0), 9); encodes(t, &mut a, "f3450f114d00"); # movss dword ptr [r13], xmm9 + a.load(1, false, reg::rax, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "410fb685c8000000"); # movzx eax, byte ptr [r13+200] + a.load(1, false, reg::r11, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "450fb69dc8000000"); # movzx r11d, byte ptr [r13+200] + a.load(1, false, reg::rdx, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "410fb695c8000000"); # movzx edx, byte ptr [r13+200] + a.load(1, true, reg::rax, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "490fbe85c8000000"); # movsx rax, byte ptr [r13+200] + a.load(1, true, reg::r11, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "4d0fbe9dc8000000"); # movsx r11, byte ptr [r13+200] + a.load(1, true, reg::rdx, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "490fbe95c8000000"); # movsx rdx, byte ptr [r13+200] + a.store(1, indexed(reg::r13, reg::none, 1, 200), reg::rax); encodes(t, &mut a, "418885c8000000"); # mov byte ptr [r13+200], al + a.store(1, indexed(reg::r13, reg::none, 1, 200), reg::rsi); encodes(t, &mut a, "4188b5c8000000"); # mov byte ptr [r13+200], sil + a.store(1, indexed(reg::r13, reg::none, 1, 200), reg::r10); encodes(t, &mut a, "458895c8000000"); # mov byte ptr [r13+200], r10b + a.load(2, false, reg::rax, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "410fb785c8000000"); # movzx eax, word ptr [r13+200] + a.load(2, false, reg::r11, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "450fb79dc8000000"); # movzx r11d, word ptr [r13+200] + a.load(2, false, reg::rdx, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "410fb795c8000000"); # movzx edx, word ptr [r13+200] + a.load(2, true, reg::rax, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "490fbf85c8000000"); # movsx rax, word ptr [r13+200] + a.load(2, true, reg::r11, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "4d0fbf9dc8000000"); # movsx r11, word ptr [r13+200] + a.load(2, true, reg::rdx, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "490fbf95c8000000"); # movsx rdx, word ptr [r13+200] + a.store(2, indexed(reg::r13, reg::none, 1, 200), reg::rax); encodes(t, &mut a, "66418985c8000000"); # mov word ptr [r13+200], ax + a.store(2, indexed(reg::r13, reg::none, 1, 200), reg::rsi); encodes(t, &mut a, "664189b5c8000000"); # mov word ptr [r13+200], si + a.store(2, indexed(reg::r13, reg::none, 1, 200), reg::r10); encodes(t, &mut a, "66458995c8000000"); # mov word ptr [r13+200], r10w + a.load(4, false, reg::rax, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "418b85c8000000"); # mov eax, dword ptr [r13+200] + a.load(4, false, reg::r11, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "458b9dc8000000"); # mov r11d, dword ptr [r13+200] + a.load(4, false, reg::rdx, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "418b95c8000000"); # mov edx, dword ptr [r13+200] + a.load(4, true, reg::rax, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "496385c8000000"); # movsxd rax, dword ptr [r13+200] + a.load(4, true, reg::r11, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "4d639dc8000000"); # movsxd r11, dword ptr [r13+200] + a.load(4, true, reg::rdx, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "496395c8000000"); # movsxd rdx, dword ptr [r13+200] + a.store(4, indexed(reg::r13, reg::none, 1, 200), reg::rax); encodes(t, &mut a, "418985c8000000"); # mov dword ptr [r13+200], eax + a.store(4, indexed(reg::r13, reg::none, 1, 200), reg::rsi); encodes(t, &mut a, "4189b5c8000000"); # mov dword ptr [r13+200], esi + a.store(4, indexed(reg::r13, reg::none, 1, 200), reg::r10); encodes(t, &mut a, "458995c8000000"); # mov dword ptr [r13+200], r10d + a.load(8, false, reg::rax, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "498b85c8000000"); # mov rax, qword ptr [r13+200] + a.load(8, false, reg::r11, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "4d8b9dc8000000"); # mov r11, qword ptr [r13+200] + a.load(8, false, reg::rdx, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "498b95c8000000"); # mov rdx, qword ptr [r13+200] + a.store(8, indexed(reg::r13, reg::none, 1, 200), reg::rax); encodes(t, &mut a, "498985c8000000"); # mov qword ptr [r13+200], rax + a.store(8, indexed(reg::r13, reg::none, 1, 200), reg::rsi); encodes(t, &mut a, "4989b5c8000000"); # mov qword ptr [r13+200], rsi + a.store(8, indexed(reg::r13, reg::none, 1, 200), reg::r10); encodes(t, &mut a, "4d8995c8000000"); # mov qword ptr [r13+200], r10 + a.store_imm(indexed(reg::r13, reg::none, 1, 200), -5); encodes(t, &mut a, "49c785c8000000fbffffff"); # mov qword ptr [r13+200], -5 + a.lea(reg::rdi, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "498dbdc8000000"); # lea rdi, [r13+200] + a.alu_load(alu::add, reg::r9, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "4d038dc8000000"); # add r9, qword ptr [r13+200] + a.alu_mem_imm(alu::cmp, indexed(reg::r13, reg::none, 1, 200), 0); encodes(t, &mut a, "4983bdc800000000"); # cmp qword ptr [r13+200], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::r13, reg::none, 1, 200), 1000); encodes(t, &mut a, "4981bdc8000000e8030000"); # cmp qword ptr [r13+200], 1000 + a.movss_load(1, indexed(reg::r13, reg::none, 1, 200)); encodes(t, &mut a, "f3410f108dc8000000"); # movss xmm1, dword ptr [r13+200] + a.movss_store(indexed(reg::r13, reg::none, 1, 200), 9); encodes(t, &mut a, "f3450f118dc8000000"); # movss dword ptr [r13+200], xmm9 + a.load(1, false, reg::rax, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "410fb687d4feffff"); # movzx eax, byte ptr [r15-300] + a.load(1, false, reg::r11, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "450fb69fd4feffff"); # movzx r11d, byte ptr [r15-300] + a.load(1, false, reg::rdx, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "410fb697d4feffff"); # movzx edx, byte ptr [r15-300] + a.load(1, true, reg::rax, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "490fbe87d4feffff"); # movsx rax, byte ptr [r15-300] + a.load(1, true, reg::r11, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "4d0fbe9fd4feffff"); # movsx r11, byte ptr [r15-300] + a.load(1, true, reg::rdx, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "490fbe97d4feffff"); # movsx rdx, byte ptr [r15-300] + a.store(1, indexed(reg::r15, reg::none, 1, -300), reg::rax); encodes(t, &mut a, "418887d4feffff"); # mov byte ptr [r15-300], al + a.store(1, indexed(reg::r15, reg::none, 1, -300), reg::rsi); encodes(t, &mut a, "4188b7d4feffff"); # mov byte ptr [r15-300], sil + a.store(1, indexed(reg::r15, reg::none, 1, -300), reg::r10); encodes(t, &mut a, "458897d4feffff"); # mov byte ptr [r15-300], r10b + a.load(2, false, reg::rax, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "410fb787d4feffff"); # movzx eax, word ptr [r15-300] + a.load(2, false, reg::r11, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "450fb79fd4feffff"); # movzx r11d, word ptr [r15-300] + a.load(2, false, reg::rdx, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "410fb797d4feffff"); # movzx edx, word ptr [r15-300] + a.load(2, true, reg::rax, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "490fbf87d4feffff"); # movsx rax, word ptr [r15-300] + a.load(2, true, reg::r11, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "4d0fbf9fd4feffff"); # movsx r11, word ptr [r15-300] + a.load(2, true, reg::rdx, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "490fbf97d4feffff"); # movsx rdx, word ptr [r15-300] + a.store(2, indexed(reg::r15, reg::none, 1, -300), reg::rax); encodes(t, &mut a, "66418987d4feffff"); # mov word ptr [r15-300], ax + a.store(2, indexed(reg::r15, reg::none, 1, -300), reg::rsi); encodes(t, &mut a, "664189b7d4feffff"); # mov word ptr [r15-300], si + a.store(2, indexed(reg::r15, reg::none, 1, -300), reg::r10); encodes(t, &mut a, "66458997d4feffff"); # mov word ptr [r15-300], r10w + a.load(4, false, reg::rax, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "418b87d4feffff"); # mov eax, dword ptr [r15-300] + a.load(4, false, reg::r11, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "458b9fd4feffff"); # mov r11d, dword ptr [r15-300] + a.load(4, false, reg::rdx, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "418b97d4feffff"); # mov edx, dword ptr [r15-300] + a.load(4, true, reg::rax, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "496387d4feffff"); # movsxd rax, dword ptr [r15-300] + a.load(4, true, reg::r11, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "4d639fd4feffff"); # movsxd r11, dword ptr [r15-300] + a.load(4, true, reg::rdx, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "496397d4feffff"); # movsxd rdx, dword ptr [r15-300] + a.store(4, indexed(reg::r15, reg::none, 1, -300), reg::rax); encodes(t, &mut a, "418987d4feffff"); # mov dword ptr [r15-300], eax + a.store(4, indexed(reg::r15, reg::none, 1, -300), reg::rsi); encodes(t, &mut a, "4189b7d4feffff"); # mov dword ptr [r15-300], esi + a.store(4, indexed(reg::r15, reg::none, 1, -300), reg::r10); encodes(t, &mut a, "458997d4feffff"); # mov dword ptr [r15-300], r10d + a.load(8, false, reg::rax, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "498b87d4feffff"); # mov rax, qword ptr [r15-300] + a.load(8, false, reg::r11, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "4d8b9fd4feffff"); # mov r11, qword ptr [r15-300] + a.load(8, false, reg::rdx, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "498b97d4feffff"); # mov rdx, qword ptr [r15-300] + a.store(8, indexed(reg::r15, reg::none, 1, -300), reg::rax); encodes(t, &mut a, "498987d4feffff"); # mov qword ptr [r15-300], rax + a.store(8, indexed(reg::r15, reg::none, 1, -300), reg::rsi); encodes(t, &mut a, "4989b7d4feffff"); # mov qword ptr [r15-300], rsi + a.store(8, indexed(reg::r15, reg::none, 1, -300), reg::r10); encodes(t, &mut a, "4d8997d4feffff"); # mov qword ptr [r15-300], r10 + a.store_imm(indexed(reg::r15, reg::none, 1, -300), -5); encodes(t, &mut a, "49c787d4fefffffbffffff"); # mov qword ptr [r15-300], -5 + a.lea(reg::rdi, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "498dbfd4feffff"); # lea rdi, [r15-300] + a.alu_load(alu::add, reg::r9, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "4d038fd4feffff"); # add r9, qword ptr [r15-300] + a.alu_mem_imm(alu::cmp, indexed(reg::r15, reg::none, 1, -300), 0); encodes(t, &mut a, "4983bfd4feffff00"); # cmp qword ptr [r15-300], 0 + a.alu_mem_imm(alu::cmp, indexed(reg::r15, reg::none, 1, -300), 1000); encodes(t, &mut a, "4981bfd4feffffe8030000"); # cmp qword ptr [r15-300], 1000 + a.movss_load(1, indexed(reg::r15, reg::none, 1, -300)); encodes(t, &mut a, "f3410f108fd4feffff"); # movss xmm1, dword ptr [r15-300] + a.movss_store(indexed(reg::r15, reg::none, 1, -300), 9); encodes(t, &mut a, "f3450f118fd4feffff"); # movss dword ptr [r15-300], xmm9 + a.lea(reg::rax, indexed(reg::rax, reg::rcx, 8, 0)); encodes(t, &mut a, "488d04c8"); # lea rax, [rax+rcx*8] + a.load(8, false, reg::r8, indexed(reg::rax, reg::rcx, 8, 0)); encodes(t, &mut a, "4c8b04c8"); # mov r8, qword ptr [rax+rcx*8] + a.lea(reg::rax, indexed(reg::rbx, reg::r12, 4, 16)); encodes(t, &mut a, "4a8d44a310"); # lea rax, [rbx+r12*4+16] + a.load(8, false, reg::r8, indexed(reg::rbx, reg::r12, 4, 16)); encodes(t, &mut a, "4e8b44a310"); # mov r8, qword ptr [rbx+r12*4+16] + a.lea(reg::rax, indexed(reg::r13, reg::rax, 1, 0)); encodes(t, &mut a, "498d440500"); # lea rax, [r13+rax*1] + a.load(8, false, reg::r8, indexed(reg::r13, reg::rax, 1, 0)); encodes(t, &mut a, "4d8b440500"); # mov r8, qword ptr [r13+rax*1] + a.lea(reg::rax, indexed(reg::rsp, reg::rbp, 2, -4)); encodes(t, &mut a, "488d446cfc"); # lea rax, [rsp+rbp*2-4] + a.load(8, false, reg::r8, indexed(reg::rsp, reg::rbp, 2, -4)); encodes(t, &mut a, "4c8b446cfc"); # mov r8, qword ptr [rsp+rbp*2-4] + a.lea(reg::rax, indexed(reg::none, reg::rcx, 8, 4194304)); encodes(t, &mut a, "488d04cd00004000"); # lea rax, [rcx*8+4194304] + a.load(8, false, reg::r8, indexed(reg::none, reg::rcx, 8, 4194304)); encodes(t, &mut a, "4c8b04cd00004000"); # mov r8, qword ptr [rcx*8+4194304] + a.load(8, false, reg::rax, indexed(reg::none, reg::none, 1, 4198400)); encodes(t, &mut a, "488b042500104000"); # mov rax, qword ptr ds:0x401000 + a.alu(alu::add, reg::rax, reg::r11); encodes(t, &mut a, "4c01d8"); # add rax, r11 + a.alu(alu::add, reg::r12, reg::rdx); encodes(t, &mut a, "4901d4"); # add r12, rdx + a.alu_imm(alu::add, reg::rcx, 5); encodes(t, &mut a, "4883c105"); # add rcx, 5 + a.alu_imm(alu::add, reg::r10, -100000); encodes(t, &mut a, "4981c26079feff"); # add r10, -100000 + a.alu(alu::bor, reg::rax, reg::r11); encodes(t, &mut a, "4c09d8"); # or rax, r11 + a.alu(alu::bor, reg::r12, reg::rdx); encodes(t, &mut a, "4909d4"); # or r12, rdx + a.alu_imm(alu::bor, reg::rcx, 5); encodes(t, &mut a, "4883c905"); # or rcx, 5 + a.alu_imm(alu::bor, reg::r10, -100000); encodes(t, &mut a, "4981ca6079feff"); # or r10, -100000 + a.alu(alu::band, reg::rax, reg::r11); encodes(t, &mut a, "4c21d8"); # and rax, r11 + a.alu(alu::band, reg::r12, reg::rdx); encodes(t, &mut a, "4921d4"); # and r12, rdx + a.alu_imm(alu::band, reg::rcx, 5); encodes(t, &mut a, "4883e105"); # and rcx, 5 + a.alu_imm(alu::band, reg::r10, -100000); encodes(t, &mut a, "4981e26079feff"); # and r10, -100000 + a.alu(alu::sub, reg::rax, reg::r11); encodes(t, &mut a, "4c29d8"); # sub rax, r11 + a.alu(alu::sub, reg::r12, reg::rdx); encodes(t, &mut a, "4929d4"); # sub r12, rdx + a.alu_imm(alu::sub, reg::rcx, 5); encodes(t, &mut a, "4883e905"); # sub rcx, 5 + a.alu_imm(alu::sub, reg::r10, -100000); encodes(t, &mut a, "4981ea6079feff"); # sub r10, -100000 + a.alu(alu::bxor, reg::rax, reg::r11); encodes(t, &mut a, "4c31d8"); # xor rax, r11 + a.alu(alu::bxor, reg::r12, reg::rdx); encodes(t, &mut a, "4931d4"); # xor r12, rdx + a.alu_imm(alu::bxor, reg::rcx, 5); encodes(t, &mut a, "4883f105"); # xor rcx, 5 + a.alu_imm(alu::bxor, reg::r10, -100000); encodes(t, &mut a, "4981f26079feff"); # xor r10, -100000 + a.alu(alu::cmp, reg::rax, reg::r11); encodes(t, &mut a, "4c39d8"); # cmp rax, r11 + a.alu(alu::cmp, reg::r12, reg::rdx); encodes(t, &mut a, "4939d4"); # cmp r12, rdx + a.alu_imm(alu::cmp, reg::rcx, 5); encodes(t, &mut a, "4883f905"); # cmp rcx, 5 + a.alu_imm(alu::cmp, reg::r10, -100000); encodes(t, &mut a, "4981fa6079feff"); # cmp r10, -100000 + a.alu(alu::adc, reg::rax, reg::r11); encodes(t, &mut a, "4c11d8"); # adc rax, r11 + a.alu(alu::adc, reg::r12, reg::rdx); encodes(t, &mut a, "4911d4"); # adc r12, rdx + a.alu_imm(alu::adc, reg::rcx, 5); encodes(t, &mut a, "4883d105"); # adc rcx, 5 + a.alu_imm(alu::adc, reg::r10, -100000); encodes(t, &mut a, "4981d26079feff"); # adc r10, -100000 + a.alu(alu::sbb, reg::rax, reg::r11); encodes(t, &mut a, "4c19d8"); # sbb rax, r11 + a.alu(alu::sbb, reg::r12, reg::rdx); encodes(t, &mut a, "4919d4"); # sbb r12, rdx + a.alu_imm(alu::sbb, reg::rcx, 5); encodes(t, &mut a, "4883d905"); # sbb rcx, 5 + a.alu_imm(alu::sbb, reg::r10, -100000); encodes(t, &mut a, "4981da6079feff"); # sbb r10, -100000 + a.test(reg::rax, reg::rax); encodes(t, &mut a, "4885c0"); # test rax, rax + a.test(reg::r9, reg::rcx); encodes(t, &mut a, "4985c9"); # test r9, rcx + a.imul(reg::rax, reg::rcx); encodes(t, &mut a, "480fafc1"); # imul rax, rcx + a.imul(reg::r14, reg::r8); encodes(t, &mut a, "4d0faff0"); # imul r14, r8 + a.unary(unary::bnot, reg::rcx); encodes(t, &mut a, "48f7d1"); # not rcx + a.unary(unary::bnot, reg::r13); encodes(t, &mut a, "49f7d5"); # not r13 + a.unary(unary::neg, reg::rcx); encodes(t, &mut a, "48f7d9"); # neg rcx + a.unary(unary::neg, reg::r13); encodes(t, &mut a, "49f7dd"); # neg r13 + a.unary(unary::mul, reg::rcx); encodes(t, &mut a, "48f7e1"); # mul rcx + a.unary(unary::mul, reg::r13); encodes(t, &mut a, "49f7e5"); # mul r13 + a.unary(unary::div, reg::rcx); encodes(t, &mut a, "48f7f1"); # div rcx + a.unary(unary::div, reg::r13); encodes(t, &mut a, "49f7f5"); # div r13 + a.unary(unary::idiv, reg::rcx); encodes(t, &mut a, "48f7f9"); # idiv rcx + a.unary(unary::idiv, reg::r13); encodes(t, &mut a, "49f7fd"); # idiv r13 + a.bsr(reg::rax, reg::rcx); encodes(t, &mut a, "480fbdc1"); # bsr rax, rcx + a.bsr(reg::r13, reg::r9); encodes(t, &mut a, "4d0fbde9"); # bsr r13, r9 + a.bsf(reg::rax, reg::rcx); encodes(t, &mut a, "480fbcc1"); # bsf rax, rcx + a.bsf(reg::r13, reg::r9); encodes(t, &mut a, "4d0fbce9"); # bsf r13, r9 + a.bswap(reg::rax); encodes(t, &mut a, "480fc8"); # bswap rax + a.bswap(reg::r13); encodes(t, &mut a, "490fcd"); # bswap r13 + a.cqo(); encodes(t, &mut a, "4899"); # cqo + a.shift_cl(shift::shl, reg::rax); encodes(t, &mut a, "48d3e0"); # shl rax, cl + a.shift_imm(shift::shl, reg::r9, 13); encodes(t, &mut a, "49c1e10d"); # shl r9, 13 + a.shift_cl(shift::shr, reg::rax); encodes(t, &mut a, "48d3e8"); # shr rax, cl + a.shift_imm(shift::shr, reg::r9, 13); encodes(t, &mut a, "49c1e90d"); # shr r9, 13 + a.shift_cl(shift::sar, reg::rax); encodes(t, &mut a, "48d3f8"); # sar rax, cl + a.shift_imm(shift::sar, reg::r9, 13); encodes(t, &mut a, "49c1f90d"); # sar r9, 13 + a.setcc(cc::equal, reg::rax); encodes(t, &mut a, "0f94c00fb6c0"); # sete al; movzx eax, al + a.setcc(cc::equal, reg::rsi); encodes(t, &mut a, "400f94c6400fb6f6"); # sete sil; movzx esi, sil + a.setcc(cc::equal, reg::r10); encodes(t, &mut a, "410f94c2450fb6d2"); # sete r10b; movzx r10d, r10b + a.setcc(cc::not_equal, reg::rax); encodes(t, &mut a, "0f95c00fb6c0"); # setne al; movzx eax, al + a.setcc(cc::not_equal, reg::rsi); encodes(t, &mut a, "400f95c6400fb6f6"); # setne sil; movzx esi, sil + a.setcc(cc::not_equal, reg::r10); encodes(t, &mut a, "410f95c2450fb6d2"); # setne r10b; movzx r10d, r10b + a.setcc(cc::below, reg::rax); encodes(t, &mut a, "0f92c00fb6c0"); # setb al; movzx eax, al + a.setcc(cc::below, reg::rsi); encodes(t, &mut a, "400f92c6400fb6f6"); # setb sil; movzx esi, sil + a.setcc(cc::below, reg::r10); encodes(t, &mut a, "410f92c2450fb6d2"); # setb r10b; movzx r10d, r10b + a.setcc(cc::above, reg::rax); encodes(t, &mut a, "0f97c00fb6c0"); # seta al; movzx eax, al + a.setcc(cc::above, reg::rsi); encodes(t, &mut a, "400f97c6400fb6f6"); # seta sil; movzx esi, sil + a.setcc(cc::above, reg::r10); encodes(t, &mut a, "410f97c2450fb6d2"); # seta r10b; movzx r10d, r10b + a.setcc(cc::less, reg::rax); encodes(t, &mut a, "0f9cc00fb6c0"); # setl al; movzx eax, al + a.setcc(cc::less, reg::rsi); encodes(t, &mut a, "400f9cc6400fb6f6"); # setl sil; movzx esi, sil + a.setcc(cc::less, reg::r10); encodes(t, &mut a, "410f9cc2450fb6d2"); # setl r10b; movzx r10d, r10b + a.setcc(cc::greater_equal, reg::rax); encodes(t, &mut a, "0f9dc00fb6c0"); # setge al; movzx eax, al + a.setcc(cc::greater_equal, reg::rsi); encodes(t, &mut a, "400f9dc6400fb6f6"); # setge sil; movzx esi, sil + a.setcc(cc::greater_equal, reg::r10); encodes(t, &mut a, "410f9dc2450fb6d2"); # setge r10b; movzx r10d, r10b + a.setcc(cc::parity, reg::rax); encodes(t, &mut a, "0f9ac00fb6c0"); # setp al; movzx eax, al + a.setcc(cc::parity, reg::rsi); encodes(t, &mut a, "400f9ac6400fb6f6"); # setp sil; movzx esi, sil + a.setcc(cc::parity, reg::r10); encodes(t, &mut a, "410f9ac2450fb6d2"); # setp r10b; movzx r10d, r10b + a.setcc(cc::no_parity, reg::rax); encodes(t, &mut a, "0f9bc00fb6c0"); # setnp al; movzx eax, al + a.setcc(cc::no_parity, reg::rsi); encodes(t, &mut a, "400f9bc6400fb6f6"); # setnp sil; movzx esi, sil + a.setcc(cc::no_parity, reg::r10); encodes(t, &mut a, "410f9bc2450fb6d2"); # setnp r10b; movzx r10d, r10b + a.push(reg::rax); encodes(t, &mut a, "50"); # push rax + a.pop(reg::rax); encodes(t, &mut a, "58"); # pop rax + a.call_reg(reg::rax); encodes(t, &mut a, "ffd0"); # call rax + a.push(reg::rbp); encodes(t, &mut a, "55"); # push rbp + a.pop(reg::rbp); encodes(t, &mut a, "5d"); # pop rbp + a.call_reg(reg::rbp); encodes(t, &mut a, "ffd5"); # call rbp + a.push(reg::r12); encodes(t, &mut a, "4154"); # push r12 + a.pop(reg::r12); encodes(t, &mut a, "415c"); # pop r12 + a.call_reg(reg::r12); encodes(t, &mut a, "41ffd4"); # call r12 + a.push(reg::r15); encodes(t, &mut a, "4157"); # push r15 + a.pop(reg::r15); encodes(t, &mut a, "415f"); # pop r15 + a.call_reg(reg::r15); encodes(t, &mut a, "41ffd7"); # call r15 + a.ret(); encodes(t, &mut a, "c3"); # ret + a.leave(); encodes(t, &mut a, "c9"); # leave + a.syscall(); encodes(t, &mut a, "0f05"); # syscall + a.rep_movsb(); encodes(t, &mut a, "f3a4"); # rep movsb + a.rep_stosb(); encodes(t, &mut a, "f3aa"); # rep stosb + a.imul_imm(reg::rax, reg::rcx, 5); encodes(t, &mut a, "486bc105"); # imul rax, rcx, 5 + a.imul_imm(reg::r9, reg::r15, 1000); encodes(t, &mut a, "4d69cfe8030000"); # imul r9, r15, 1000 + a.imul_imm(reg::rsi, reg::r13, -3); encodes(t, &mut a, "496bf5fd"); # imul rsi, r13, -3 + a.extend(1, false, reg::rax, reg::rsi); encodes(t, &mut a, "400fb6c6"); # movzx eax, sil + a.extend(1, false, reg::r14, reg::r9); encodes(t, &mut a, "450fb6f1"); # movzx r14d, r9b + a.extend(1, true, reg::rcx, reg::rdi); encodes(t, &mut a, "480fbecf"); # movsx rcx, dil + a.extend(1, true, reg::r8, reg::rax); encodes(t, &mut a, "4c0fbec0"); # movsx r8, al + a.extend(2, false, reg::rdx, reg::r10); encodes(t, &mut a, "410fb7d2"); # movzx edx, r10w + a.extend(2, true, reg::r11, reg::rcx); encodes(t, &mut a, "4c0fbfd9"); # movsx r11, cx + a.extend(4, false, reg::rsi, reg::r13); encodes(t, &mut a, "418bf5"); # mov esi, r13d + a.extend(4, false, reg::rax, reg::rax); encodes(t, &mut a, "8bc0"); # mov eax, eax + a.extend(4, true, reg::rdi, reg::r9); encodes(t, &mut a, "4963f9"); # movsxd rdi, r9d + a.rep_movsq(); encodes(t, &mut a, "f348a5"); # rep movsq + a.rep_stosq(); encodes(t, &mut a, "f348ab"); # rep stosq + a.ud2(); encodes(t, &mut a, "0f0b"); # ud2 + a.movq_to_xmm(0, reg::rax); encodes(t, &mut a, "66480f6ec0"); # movq xmm0, rax + a.movq_from_xmm(reg::rax, 0); encodes(t, &mut a, "66480f7ec0"); # movq rax, xmm0 + a.cvtsi2sd(0, reg::rax); encodes(t, &mut a, "f2480f2ac0"); # cvtsi2sd xmm0, rax + a.cvttsd2si(reg::rax, 0); encodes(t, &mut a, "f2480f2cc0"); # cvttsd2si rax, xmm0 + a.movq_to_xmm(9, reg::rcx); encodes(t, &mut a, "664c0f6ec9"); # movq xmm9, rcx + a.movq_from_xmm(reg::rcx, 9); encodes(t, &mut a, "664c0f7ec9"); # movq rcx, xmm9 + a.cvtsi2sd(9, reg::rcx); encodes(t, &mut a, "f24c0f2ac9"); # cvtsi2sd xmm9, rcx + a.cvttsd2si(reg::rcx, 9); encodes(t, &mut a, "f2490f2cc9"); # cvttsd2si rcx, xmm9 + a.movq_to_xmm(2, reg::r13); encodes(t, &mut a, "66490f6ed5"); # movq xmm2, r13 + a.movq_from_xmm(reg::r13, 2); encodes(t, &mut a, "66490f7ed5"); # movq r13, xmm2 + a.cvtsi2sd(2, reg::r13); encodes(t, &mut a, "f2490f2ad5"); # cvtsi2sd xmm2, r13 + a.cvttsd2si(reg::r13, 2); encodes(t, &mut a, "f24c0f2cea"); # cvttsd2si r13, xmm2 + a.sse(sse::sqrtsd, 0, 1); encodes(t, &mut a, "f20f51c1"); # sqrtsd xmm0, xmm1 + a.sse(sse::sqrtsd, 12, 3); encodes(t, &mut a, "f2440f51e3"); # sqrtsd xmm12, xmm3 + a.sse(sse::addsd, 0, 1); encodes(t, &mut a, "f20f58c1"); # addsd xmm0, xmm1 + a.sse(sse::addsd, 12, 3); encodes(t, &mut a, "f2440f58e3"); # addsd xmm12, xmm3 + a.sse(sse::subsd, 0, 1); encodes(t, &mut a, "f20f5cc1"); # subsd xmm0, xmm1 + a.sse(sse::subsd, 12, 3); encodes(t, &mut a, "f2440f5ce3"); # subsd xmm12, xmm3 + a.sse(sse::mulsd, 0, 1); encodes(t, &mut a, "f20f59c1"); # mulsd xmm0, xmm1 + a.sse(sse::mulsd, 12, 3); encodes(t, &mut a, "f2440f59e3"); # mulsd xmm12, xmm3 + a.sse(sse::divsd, 0, 1); encodes(t, &mut a, "f20f5ec1"); # divsd xmm0, xmm1 + a.sse(sse::divsd, 12, 3); encodes(t, &mut a, "f2440f5ee3"); # divsd xmm12, xmm3 + a.ucomisd(0, 1); encodes(t, &mut a, "660f2ec1"); # ucomisd xmm0, xmm1 + a.ucomisd(10, 15); encodes(t, &mut a, "66450f2ed7"); # ucomisd xmm10, xmm15 + a.cvtsd2ss(0, 0); encodes(t, &mut a, "f20f5ac0"); # cvtsd2ss xmm0, xmm0 + a.cvtss2sd(1, 8); encodes(t, &mut a, "f3410f5ac8"); # cvtss2sd xmm1, xmm8 + a.alu_store(alu::bor, at(reg::rbx, 0), reg::rax); encodes(t, &mut a, "480903"); # or qword ptr [rbx], rax + a.alu_store(alu::bor, at(reg::rbx, 16), reg::r9); encodes(t, &mut a, "4c094b10"); # or qword ptr [rbx+16], r9 + a.alu_store(alu::bor, at(reg::r12, -8), reg::rcx); encodes(t, &mut a, "49094c24f8"); # or qword ptr [r12-8], rcx + a.alu_store(alu::band, at(reg::r13, 0), reg::rdx); encodes(t, &mut a, "49215500"); # and qword ptr [r13], rdx + a.alu_store(alu::bor, amd64::absolute(0x400058), reg::rax); encodes(t, &mut a, "4809042558004000"); # or qword ptr [0x400058], rax + a.lock(); a.alu_store(alu::bor, amd64::absolute(0x400058), reg::rax); encodes(t, &mut a, "f04809042558004000"); # lock or qword ptr [0x400058], rax + a.lock(); a.alu_store(alu::bor, at(reg::r12, 4096), reg::r9); encodes(t, &mut a, "f04d098c2400100000"); # lock or qword ptr [r12+4096], r9 + a.xchg(8, at(reg::rbx, 0), reg::rax); encodes(t, &mut a, "488703"); # xchg qword ptr [rbx], rax + a.xchg(8, amd64::absolute(0x400058), reg::rax); encodes(t, &mut a, "4887042558004000"); # xchg qword ptr [0x400058], rax + a.xchg(8, at(reg::r12, 16), reg::r9); encodes(t, &mut a, "4d874c2410"); # xchg qword ptr [r12+16], r9 + a.xchg(8, at(reg::rsp, 8), reg::r15); encodes(t, &mut a, "4c877c2408"); # xchg qword ptr [rsp+8], r15 + a.fmod_stack(); encodes(t, &mut a, "dd442408dd0424d9f8dfe0f6c40475f7dd1c24ddd8"); # fld, fld, fprem until done, fstp, fstp +} + +# Atomics' instructions, at each width, with registers needing a REX prefix and +# the bytes of `sil` and `dil`, which only one names, as GNU as writes them. +@test def atomic_instructions_encode_at_every_width(t: &mut tester): void { + var a := assembly::new(); + a.xchg(1, at(reg::rbx, 0), reg::rax); encodes(t, &mut a, "8603"); # xchg byte ptr [rbx], al + a.xchg(1, at(reg::rdi, 8), reg::rsi); encodes(t, &mut a, "40867708"); # xchg byte ptr [rdi+8], sil + a.xchg(2, at(reg::r12, 0), reg::rcx); encodes(t, &mut a, "6641870c24"); # xchg word ptr [r12], cx + a.xchg(4, at(reg::rbp, -16), reg::r9); encodes(t, &mut a, "44874df0"); # xchg dword ptr [rbp-16], r9d + a.lock_xadd(1, at(reg::rdx, 0), reg::rcx); encodes(t, &mut a, "f00fc00a"); # lock xadd byte ptr [rdx], cl + a.lock_xadd(1, at(reg::r13, 4), reg::rdi); encodes(t, &mut a, "f0410fc07d04"); # lock xadd byte ptr [r13+4], dil + a.lock_xadd(2, at(reg::rax, 0), reg::r10); encodes(t, &mut a, "66f0440fc110"); # lock xadd word ptr [rax], r10w + a.lock_xadd(4, at(reg::rsp, 8), reg::rdx); encodes(t, &mut a, "f00fc1542408"); # lock xadd dword ptr [rsp+8], edx + a.lock_xadd(8, at(reg::r15, 0), reg::rax); encodes(t, &mut a, "f0490fc107"); # lock xadd qword ptr [r15], rax + a.lock_cmpxchg(1, at(reg::rdx, 0), reg::rcx); encodes(t, &mut a, "f00fb00a"); # lock cmpxchg byte ptr [rdx], cl + a.lock_cmpxchg(1, at(reg::rcx, 0), reg::r8); encodes(t, &mut a, "f0440fb001"); # lock cmpxchg byte ptr [rcx], r8b + a.lock_cmpxchg(2, at(reg::rbx, 2), reg::rsi); encodes(t, &mut a, "66f00fb17302"); # lock cmpxchg word ptr [rbx+2], si + a.lock_cmpxchg(4, at(reg::rdi, 0), reg::rcx); encodes(t, &mut a, "f00fb10f"); # lock cmpxchg dword ptr [rdi], ecx + a.lock_cmpxchg(8, at(reg::r11, -8), reg::r14); encodes(t, &mut a, "f04d0fb173f8"); # lock cmpxchg qword ptr [r11-8], r14 + a.lock_cmpxchg(8, amd64::absolute(0x400058), reg::rcx); encodes(t, &mut a, "f0480fb10c2558004000"); # lock cmpxchg qword ptr [0x400058], rcx + a.mfence(); encodes(t, &mut a, "0faef0"); # mfence + a.cmov(cc::less, reg::rcx, reg::rax); encodes(t, &mut a, "480f4cc8"); # cmovl rcx, rax + a.cmov(cc::above, reg::r9, reg::rdx); encodes(t, &mut a, "4c0f47ca"); # cmova r9, rdx +} + +# A thread's own record, through `gs`, as GNU as writes it. +@test def a_threads_own_record_is_reached_through_gs(t: &mut tester): void { + var a := assembly::new(); + a.alu_load(alu::cmp, reg::rax, own(8)); encodes(t, &mut a, "65483b042508000000"); # cmp rax, qword ptr gs:[8] + a.store(8, own(0x78), reg::rsp); encodes(t, &mut a, "654889242578000000"); # mov qword ptr gs:[0x78], rsp + a.load(8, false, reg::rax, own(0x18)); encodes(t, &mut a, "65488b042518000000"); # mov rax, qword ptr gs:[0x18] + a.store(8, own(0x40), reg::rbp); encodes(t, &mut a, "6548892c2540000000"); # mov qword ptr gs:[0x40], rbp + a.store_imm(own(0x78), 0); encodes(t, &mut a, "6548c704257800000000000000"); # mov qword ptr gs:[0x78], 0 + a.alu_mem_imm(alu::cmp, own(0x58), 0); encodes(t, &mut a, "6548833c255800000000"); # cmp qword ptr gs:[0x58], 0 + a.load(8, false, reg::r9, own(0x10)); encodes(t, &mut a, "654c8b0c2510000000"); # mov r9, qword ptr gs:[0x10] +} + +@test def instructions_the_c_compiler_never_wrote_encode_as_well(t: &mut tester): void { + var a := assembly::new(); + a.movsd_load(0, at(reg::rbp, -8)); encodes(t, &mut a, "f20f1045f8"); # movsd xmm0, qword ptr [rbp-8] + a.movsd_load(12, at(reg::rsp, 16)); encodes(t, &mut a, "f2440f10642410"); # movsd xmm12, qword ptr [rsp+16] + a.movsd_store(at(reg::rbp, -8), 3); encodes(t, &mut a, "f20f115df8"); # movsd qword ptr [rbp-8], xmm3 + a.movsd_store(at(reg::r13, 0), 9); encodes(t, &mut a, "f2450f114d00"); # movsd qword ptr [r13+0], xmm9 + a.movaps(2, 3); encodes(t, &mut a, "0f28d3"); # movaps xmm2, xmm3 + a.movaps(10, 1); encodes(t, &mut a, "440f28d1"); # movaps xmm10, xmm1 + a.movaps(4, 15); encodes(t, &mut a, "410f28e7"); # movaps xmm4, xmm15 + a.xorpd(2, 0); encodes(t, &mut a, "660f57d0"); # xorpd xmm2, xmm0 + a.xorpd(9, 0); encodes(t, &mut a, "66440f57c8"); # xorpd xmm9, xmm0 + a.call_mem(at(reg::rbp, -24)); encodes(t, &mut a, "ff55e8"); # call qword ptr [rbp-24] + a.call_mem(amd64::absolute(0x400100)); encodes(t, &mut a, "ff142500014000"); # call qword ptr [0x400100] + a.call_mem(indexed(reg::rax, reg::r9, 8, 16)); encodes(t, &mut a, "42ff54c810"); # call qword ptr [rax+r9*8+16] + a.jmp_reg(reg::rax); encodes(t, &mut a, "ffe0"); # jmp rax + a.jmp_reg(reg::r11); encodes(t, &mut a, "41ffe3"); # jmp r11 + a.jmp_mem(indexed(reg::none, reg::rdx, 8, 0x401000)); encodes(t, &mut a, "ff24d500104000"); # jmp qword [rdx*8+0x401000] + a.jmp_mem(indexed(reg::none, reg::r9, 8, 0x7f)); encodes(t, &mut a, "42ff24cd7f000000"); # jmp qword [r9*8+0x7f] + a.jmp_mem(at(reg::rax, 0)); encodes(t, &mut a, "ff20"); # jmp qword [rax] + a.store_imm_width(1, at(reg::rbp, -1), 7); encodes(t, &mut a, "c645ff07"); # mov byte ptr [rbp-1], 7 + a.store_imm_width(2, at(reg::rbx, 2), 0x1234); encodes(t, &mut a, "66c743023412"); # mov word ptr [rbx+2], 0x1234 + a.store_imm_width(4, at(reg::r12, 4), 0x12345678); encodes(t, &mut a, "41c744240478563412"); # mov dword ptr [r12+4], 0x12345678 + a.store_imm_width(8, at(reg::rbp, -16), 0xFFFFFFFFFFFFFFFF); encodes(t, &mut a, "48c745f0ffffffff"); # mov qword ptr [rbp-16], -1 + a.cmov(cc::equal, reg::rax, reg::rcx); encodes(t, &mut a, "480f44c1"); # cmove rax, rcx + a.cmov(cc::less, reg::r11, reg::r9); encodes(t, &mut a, "4d0f4cd9"); # cmovl r11, r9 + a.cmov(cc::above_equal, reg::rsi, reg::r14); encodes(t, &mut a, "490f43f6"); # cmovae rsi, r14 + a.imul_load(reg::rax, at(reg::rbp, -8)); encodes(t, &mut a, "480faf45f8"); # imul rax, qword ptr [rbp-8] + a.imul_load(reg::r10, at(reg::r13, 8)); encodes(t, &mut a, "4d0faf5508"); # imul r10, qword ptr [r13+8] + a.test_mem(at(reg::rbp, -8), reg::rax); encodes(t, &mut a, "488545f8"); # test qword ptr [rbp-8], rax + a.test_mem(at(reg::r12, 0), reg::r9); encodes(t, &mut a, "4d850c24"); # test qword ptr [r12], r9 + a.sse_load(sse::addsd, 2, at(reg::rbp, -8)); encodes(t, &mut a, "f20f5855f8"); # addsd xmm2, qword ptr [rbp-8] + a.sse_load(sse::mulsd, 11, at(reg::rsp, 8)); encodes(t, &mut a, "f2440f595c2408"); # mulsd xmm11, qword ptr [rsp+8] + a.alu_load(alu::cmp, reg::rax, at(reg::rbp, -8)); encodes(t, &mut a, "483b45f8"); # cmp rax, qword ptr [rbp-8] + a.alu_store(alu::cmp, at(reg::rbp, -16), reg::r10); encodes(t, &mut a, "4c3955f0"); # cmp qword ptr [rbp-16], r10 + a.load(8, false, reg::rax, indexed(reg::rax, reg::rcx, 8, 0x400000)); encodes(t, &mut a, "488b84c800004000"); # mov rax, qword ptr [rax+rcx*8+0x400000] + a.lea(reg::rdx, indexed(reg::rcx, reg::rdx, 1, 0)); encodes(t, &mut a, "488d1411"); # lea rdx, [rcx+rdx*1] + a.lea(reg::r11, indexed(reg::r12, reg::r13, 2, 8)); encodes(t, &mut a, "4f8d5c6c08"); # lea r11, [r12+r13*2+8] +} + +# Vector instructions: SSE's, with their mandatory prefixes and opcode maps, AVX's, +# with VEX prefixes of both lengths, and asking the processor what it has. +@test def vector_instructions_encode_as_a_reference_assembler_writes_them(t: &mut tester): void { + var a := assembly::new(); + a.packed(vec_paddd, 9, 2); encodes(t, &mut a, "66440ffeca"); # paddd xmm9, xmm2 + a.packed(vec_pminsb, 0, 12); encodes(t, &mut a, "66410f3838c4"); # pminsb xmm0, xmm12 + a.packed_imm(vec_roundps, 1, 2, 9); encodes(t, &mut a, "660f3a08ca09"); # roundps xmm1, xmm2, 9 + a.packed_imm(vec_cmpps, 3, 4, 2); encodes(t, &mut a, "0fc2dc02"); # cmpleps xmm3, xmm4 + a.packed(vec_movmskps, reg::rax as u32, 1); encodes(t, &mut a, "0f50c1"); # movmskps eax, xmm1 + a.packed(vec_pmovmskb, reg::r9 as u32, 10); encodes(t, &mut a, "66450fd7ca"); # pmovmskb r9d, xmm10 + a.packed_mem(vec_movdqu_load, 8, at(reg::rax, 16)); encodes(t, &mut a, "f3440f6f4010"); # movdqu xmm8, [rax+16] + a.packed_mem(vec_movdqu_store, 3, at(reg::r12, 0)); encodes(t, &mut a, "f3410f7f1c24"); # movdqu [r12], xmm3 + a.packed_mem(vec_movd_load, 1, at(reg::rcx, 0)); encodes(t, &mut a, "660f6e09"); # movd xmm1, [rcx] + a.packed(vec_punpcklqdq, 2, 2); encodes(t, &mut a, "660f6cd2"); # punpcklqdq xmm2, xmm2 + a.packed(vec_xorps | vec_doubles, 0, 1); encodes(t, &mut a, "660f57c1"); # xorpd xmm0, xmm1 + a.packed(vec_pcmpgtq, 1, 9); encodes(t, &mut a, "66410f3837c9"); # pcmpgtq xmm1, xmm9 + a.packed(vec_movdqa, 4, 11); encodes(t, &mut a, "66410f6fe3"); # movdqa xmm4, xmm11 + a.vex(vec_paddd, true, 0, 1, 2); encodes(t, &mut a, "c5f5fec2"); # vpaddd ymm0, ymm1, ymm2 + a.vex(vec_paddd, true, 8, 9, 10); encodes(t, &mut a, "c44135fec2"); # vpaddd ymm8, ymm9, ymm10 + a.vex(vec_vfmadd231ps, true, 0, 1, 2); encodes(t, &mut a, "c4e275b8c2"); # vfmadd231ps ymm0, ymm1, ymm2 + a.vex(vec_vfmadd231ps | vec_wide, false, 3, 4, 5); encodes(t, &mut a, "c4e2d9b8dd"); # vfmadd231pd xmm3, xmm4, xmm5 + a.vex_mem(vec_movdqu_load, true, 0, at(reg::rax, 0)); encodes(t, &mut a, "c5fe6f00"); # vmovdqu ymm0, [rax] + a.vex_mem(vec_movdqu_store, true, 9, at(reg::r13, 8)); encodes(t, &mut a, "c4417e7f4d08"); # vmovdqu [r13+8], ymm9 + a.vex_imm(vec_roundps, true, 0, 0, 0, 9); encodes(t, &mut a, "c4e37d08c009"); # vroundps ymm0, ymm0, 9 + a.vex_imm(vec_cmpps, true, 3, 3, 1, 1); encodes(t, &mut a, "c5e4c2d901"); # vcmpltps ymm3, ymm3, ymm1 + a.vex_imm(vec_vinsertf128, true, 2, 2, 2, 1); encodes(t, &mut a, "c4e36d18d201"); # vinsertf128 ymm2, ymm2, xmm2, 1 + a.vex_mem_imm(vec_vinsertf128, true, 3, 3, at(reg::rbp, -48), 1); encodes(t, &mut a, "c4e365185dd001"); # vinsertf128 ymm3, ymm3, [rbp-48], 1 + a.vex_mem_imm(vec_vinsertf128, true, 10, 10, at(reg::r9, 16), 1); encodes(t, &mut a, "c4432d18511001"); # vinsertf128 ymm10, ymm10, [r9+16], 1 + a.vex_mem(vec_movdqu_load, false, 10, at(reg::r9, 0)); encodes(t, &mut a, "c4417a6f11"); # vmovdqu xmm10, [r9] + a.vex(vec_movmskps, true, reg::rax as u32, 0, 0); encodes(t, &mut a, "c5fc50c0"); # vmovmskps eax, ymm0 + a.vex(vec_pmovmskb, true, reg::rax as u32, 0, 0); encodes(t, &mut a, "c5fdd7c0"); # vpmovmskb eax, ymm0 + a.vex(vec_psllw, true, 0, 0, 1); encodes(t, &mut a, "c5fdf1c1"); # vpsllw ymm0, ymm0, xmm1 + a.vex(vec_sqrtps | vec_doubles, true, 0, 0, 1); encodes(t, &mut a, "c5fd51c1"); # vsqrtpd ymm0, ymm1 + a.vex(vec_pabsd, true, 1, 0, 2); encodes(t, &mut a, "c4e27d1eca"); # vpabsd ymm1, ymm2 + a.vex(vec_pminud, true, 12, 12, 3); encodes(t, &mut a, "c4621d3be3"); # vpminud ymm12, ymm12, ymm3 + a.cpuid(); encodes(t, &mut a, "0fa2"); # cpuid + a.xgetbv(); encodes(t, &mut a, "0f01d0"); # xgetbv + a.vzeroupper(); encodes(t, &mut a, "c5f877"); # vzeroupper +} + +# Code for processors with AVX writes SSE's instructions with VEX, the operand +# an instruction merges into named again. Each was written by GNU as, but +# `vmovaps xmm5, xmm12` and `vmovdqa xmm7, xmm13`, which it writes as stores; +# the loads are as good, and objdump reads them back. +@test def sse_instructions_encode_with_vex_in_code_for_avx(t: &mut tester): void { + var a := assembly::new(); + a.avx = true; + a.sse(sse::addsd, 5, 6); encodes(t, &mut a, "c5d358ee"); # vaddsd xmm5, xmm5, xmm6 + a.sse(sse::sqrtsd, 5, 9); encodes(t, &mut a, "c4c15351e9"); # vsqrtsd xmm5, xmm5, xmm9 + a.sse_load(sse::addsd, 10, at(reg::rbp, -16)); encodes(t, &mut a, "c52b5855f0"); # vaddsd xmm10, xmm10, [rbp-16] + a.movsd_load(5, at(reg::rbp, -8)); encodes(t, &mut a, "c5fb106df8"); # vmovsd xmm5, [rbp-8] + a.movsd_store(at(reg::rax, 8), 12); encodes(t, &mut a, "c57b116008"); # vmovsd [rax+8], xmm12 + a.movq_to_xmm(5, reg::rax); encodes(t, &mut a, "c4e1f96ee8"); # vmovq xmm5, rax + a.movq_to_xmm(11, reg::r9); encodes(t, &mut a, "c441f96ed9"); # vmovq xmm11, r9 + a.movq_from_xmm(reg::rax, 5); encodes(t, &mut a, "c4e1f97ee8"); # vmovq rax, xmm5 + a.movq_from_xmm(reg::r10, 12); encodes(t, &mut a, "c441f97ee2"); # vmovq r10, xmm12 + a.movaps(5, 12); encodes(t, &mut a, "c4c17828ec"); # vmovaps xmm5, xmm12 + a.xorpd(5, 5); encodes(t, &mut a, "c5d157ed"); # vxorpd xmm5, xmm5, xmm5 + a.ucomisd(5, 6); encodes(t, &mut a, "c5f92eee"); # vucomisd xmm5, xmm6 + a.cvtsi2sd(5, reg::rax); encodes(t, &mut a, "c4e1d32ae8"); # vcvtsi2sd xmm5, xmm5, rax + a.cvtsi2sd(12, reg::r9); encodes(t, &mut a, "c4419b2ae1"); # vcvtsi2sd xmm12, xmm12, r9 + a.cvttsd2si(reg::rax, 5); encodes(t, &mut a, "c4e1fb2cc5"); # vcvttsd2si rax, xmm5 + a.cvttsd2si(reg::r11, 13); encodes(t, &mut a, "c441fb2cdd"); # vcvttsd2si r11, xmm13 + a.cvtsd2ss(0, 5); encodes(t, &mut a, "c5fb5ac5"); # vcvtsd2ss xmm0, xmm0, xmm5 + a.cvtss2sd(5, 5); encodes(t, &mut a, "c5d25aed"); # vcvtss2sd xmm5, xmm5, xmm5 + a.movss_load(5, at(reg::rax, 0)); encodes(t, &mut a, "c5fa1028"); # vmovss xmm5, [rax] + a.movss_store(at(reg::rbx, 4), 0); encodes(t, &mut a, "c5fa114304"); # vmovss [rbx+4], xmm0 + a.packed(vec_paddb, 5, 6); encodes(t, &mut a, "c5d1fcee"); # vpaddb xmm5, xmm5, xmm6 + a.packed_mem(vec_movdqu_load, 5, at(reg::rax, 0)); encodes(t, &mut a, "c5fa6f28"); # vmovdqu xmm5, [rax] + a.packed_mem(vec_movdqu_store, 9, at(reg::r12, 16)); encodes(t, &mut a, "c4417a7f4c2410"); # vmovdqu [r12+16], xmm9 + a.packed(vec_pmovmskb, reg::rax as u32, 5); encodes(t, &mut a, "c5f9d7c5"); # vpmovmskb eax, xmm5 + a.packed(vec_sqrtps, 5, 6); encodes(t, &mut a, "c5f851ee"); # vsqrtps xmm5, xmm6 + a.packed_imm(vec_roundps, 5, 6, 9); encodes(t, &mut a, "c4e37908ee09"); # vroundps xmm5, xmm6, 9 + a.packed_imm(vec_cmpps, 5, 6, 1); encodes(t, &mut a, "c5d0c2ee01"); # vcmpltps xmm5, xmm5, xmm6 + a.packed(vec_pabsb, 5, 6); encodes(t, &mut a, "c4e2791cee"); # vpabsb xmm5, xmm6 + a.packed(vec_movdqa, 7, 13); encodes(t, &mut a, "c4c1796ffd"); # vmovdqa xmm7, xmm13 +} + +@test def relative_branches_are_patched_to_their_targets(t: &mut tester): void { + var a := assembly::new(); + val jump := a.jcc(cc::equal); + a.ret(); + val target := a.length; + val call := a.call(); + a.patch(jump, target); + a.patch(call, 0); + + # `je +1` over the `ret`, then `call` back to the start. + val expected: u8[12] = [0x0F, 0x84, 0x01, 0x00, 0x00, 0x00, 0xC3, 0xE8, 0xF4, 0xFF, 0xFF, 0xFF]; + var same := a.bytes.length == 12; + for i in 0..12u { + same = same && i < a.bytes.length && a.bytes.get_byte(i) == expected[i]; + } + t.expects(same); +} diff --git a/tests/dwarf/dwarf.sakura b/tests/dwarf/dwarf.sakura new file mode 100644 index 0000000..2e1438a --- /dev/null +++ b/tests/dwarf/dwarf.sakura @@ -0,0 +1,122 @@ +# Tests of DWARF's encodings: numbers, abbreviations, tables of strings, the +# headers of units and of lists, line programs and the header naming their +# files, and descriptions of frames and their instructions. +import std::io; +import binary::dwarf; +import std::testing; + + +# The bytes as hex. +def hex_of(bytes: span): string { + val digits: strview = "0123456789abcdef"; + var made := string::new(nothing {}); + for &each in bytes { + made.append(digits.substr((each >> 4) as u64, 1)); + made.append(digits.substr((each & 15) as u64, 1)); + } + return made; +} + +def uleb_hex(value: u64): string { + var made := byte_writer::new(); + dwarf::uleb(&mut made, value); + return hex_of(made.view()); +} + +def sleb_hex(value: i64): string { + var made := byte_writer::new(); + dwarf::sleb(&mut made, value); + return hex_of(made.view()); +} + +@test def numbers_take_as_few_bytes_of_seven_bits_as_hold_them(t: &mut tester): void { + t.expects(uleb_hex(0).view() == "00"); + t.expects(uleb_hex(127).view() == "7f"); + t.expects(uleb_hex(128).view() == "8001"); + t.expects(uleb_hex(624485).view() == "e58e26"); + t.expects(sleb_hex(0).view() == "00"); + t.expects(sleb_hex(-1).view() == "7f"); + t.expects(sleb_hex(63).view() == "3f"); + t.expects(sleb_hex(64).view() == "c000"); + t.expects(sleb_hex(-128).view() == "807f"); + t.expects(sleb_hex(-123456).view() == "c0bb78"); +} + +@test def abbreviations_strings_and_headers_are_as_dwarf_writes_them(t: &mut tester): void { + var abbrev := byte_writer::new(); + val pairs: u64[4] = [dwarf::dw_at_producer, dwarf::dw_form_strp, dwarf::dw_at_language, dwarf::dw_form_data2]; + dwarf::abbreviation(&mut abbrev, 1, dwarf::dw_tag_compile_unit, true, pairs[:]); + t.expects(hex_of(abbrev.view()).view() == "011101250e13050000"); + + var strings := dwarf::string_table::new(); + t.expects(strings.offset_of("a") == 0 && strings.offset_of("bc") == 2 && strings.offset_of("a") == 0); + t.expects(hex_of(strings.bytes.view()).view() == "6100626300"); + + var info := byte_writer::new(); + val unit := dwarf::begin_unit(&mut info, dwarf::dw_ut_compile, 8, 0); + info.byte(0); + info.end_length(unit); + t.expects(hex_of(info.view()).view() == "09000000050001080000000000"); + var lists := byte_writer::new(); + val list := dwarf::begin_lists(&mut lists); + lists.end_length(list); + t.expects(hex_of(lists.view()).view() == "080000000500080000000000"); +} + +@test def a_line_program_writes_what_changes_from_row_to_row(t: &mut tester): void { + var program := dwarf::line_program::new(); + program.begin(0x1000); + program.row(0, 1, 1, 0, false); + program.row(4, 1, 3, 5, true); + program.row(10, 2, 3, 5, false); + program.end(16); + var expected := string::new(nothing {}); + expected.append("0009020010000000000000"); # The sequence's address, + expected.append("01"); # the first row, as a program begins, + expected.append("0505030202040a01"); # a column, two lines, four bytes on, the prologue's end, + expected.append("0402020601"); # another file, six bytes on, + expected.append("0206000101"); # and the end, six bytes on. + t.expects(hex_of(program.bytes.view()).view() == expected.view(), hex_of(program.bytes.view()).view()); +} + +@test def the_line_tables_header_names_directories_and_files(t: &mut tester): void { + var strings := dwarf::string_table::new(); + var table := byte_writer::new(); + val paths: strview[2] = ["main.sakura", "/lib/x.sakura"]; + val program: u8[1] = [0]; + dwarf::write_line_table(&mut table, "/build", paths[:], &mut strings, program[:]); + # The build's directory, then the absolute path's, each file by its last part. + t.expects(hex_of(strings.bytes.view()).view() == "2f6275696c64002f6c6962006d61696e2e73616b75726100782e73616b75726100", hex_of(strings.bytes.view()).view()); + val all := table.view(); + t.expects(all.length == 4 + table.get_u32(0) as u64); + t.expects(all[4] == 5 && all[5] == 0 && all[6] == 8 && all[7] == 0); + # Three files: the main one numbered 0 and 1, each with its directory. + val tail := all.length; + t.expects(all[tail - 1] == 0); + t.expects(all[tail - 2] == 1 && all[tail - 7] == 0 && all[tail - 12] == 0); +} + +@test def frames_are_described_from_a_common_entry(t: &mut tester): void { + var frame := byte_writer::new(); + val cie := dwarf::begin_cie(&mut frame, 1, -8, dwarf::amd64_return_address); + dwarf::cfa_def_cfa(&mut frame, dwarf::amd64_rsp, 8); + dwarf::cfa_offset(&mut frame, dwarf::amd64_return_address as u8, 1); + dwarf::end_entry(&mut frame, cie); + t.expects(hex_of(frame.view()).view() == "14000000ffffffff040008000178100c0708900100000000", hex_of(frame.view()).view()); + + var described := byte_writer::new(); + val fde := dwarf::begin_fde(&mut described, 0, 0x401000, 0x20); + var at: u64 = 0; + dwarf::cfa_advance(&mut described, &mut at, 1); + dwarf::cfa_def_cfa_offset(&mut described, 16); + dwarf::end_entry(&mut described, fde); + t.expects(hex_of(described.view()).view() == "1c0000000000000000104000000000002000000000000000410e100000000000", hex_of(described.view()).view()); + + # Moves past what a byte of the instruction holds take bytes of their own. + var moves := byte_writer::new(); + var from: u64 = 0; + dwarf::cfa_advance(&mut moves, &mut from, 0x40); + dwarf::cfa_advance(&mut moves, &mut from, 0x140); + dwarf::cfa_advance(&mut moves, &mut from, 0x10140); + t.expects(from == 0x10140 && hex_of(moves.view()).view() == "02400300010400000100", hex_of(moves.view()).view()); +} diff --git a/tests/elf/elf.sakura b/tests/elf/elf.sakura new file mode 100644 index 0000000..f5b5254 --- /dev/null +++ b/tests/elf/elf.sakura @@ -0,0 +1,104 @@ +# Tests of ELF files: an executable's headers, segments, and sections, its +# symbols, the machine it is for, the note naming its build, the file +# describing its segments without holding them, and the checksum of a link. +import binary::elf; +import std::testing; + + +# A word of bytes, least significant byte first. +def word(bytes: span, at: u64, size: u64): u64 { + var made: u64 = 0; + for k in 0..size { + made = made | (bytes[at + k] as u64 << (8 * k)); + } + return made; +} + +# Whether a section, by its number, is named so in the table of section names. +def section_named(file: span, k: u64, name: strview): bool { + val headers := word(file, 0x28, 8); + val names := headers + 64 * word(file, 0x3E, 2); + val at := word(file, names + 24, 8) + word(file, headers + 64 * k, 4); + for i in 0..name.length { + return false if file[at + i] != name[i]; + } + return file[at + name.length] == 0; +} + +# An executable of code and data, the code loaded with the headers. +def small_image(code: span): elf::image { + val headers := elf::headers_size(2, false); + var made := elf::image::new(0x400000 + headers); + made.segment(".text", 0x400000, 0, code, code.length, elf::readable | elf::executable, true); + made.segment(".data", 0x401000, 0x1000, "data", 4, elf::readable | elf::writable, false); + made.symbols.append(elf::symbol { name = "_start", address = 0x400000 + headers, size = code.length }); + return made; +} + +@test def an_executable_holds_its_segments_and_names_them(t: &mut tester): void { + val code: u8[3] = [0x31, 0xC0, 0xC3]; + var image := small_image(code[:]); + image.build_id(); + val bytes := image.file(); + val all := bytes.view(); + t.expects(all[0] == 0x7F && all[1] == 'E' as u8 && all[2] == 'L' as u8 && all[3] == 'F' as u8); + t.expects(all[4] == 2 && all[5] == 1 && word(all, 16, 2) == 2 && word(all, 18, 2) == 62); + t.expects(word(all, 24, 8) == 0x400000 + 64 + 2 * 56); + t.expects(word(all, 0x38, 2) == 2); + + # The first segment holds the headers, then the code just past them. + val headers := elf::headers_size(2, false); + t.expects(headers == 64 + 2 * 56); + t.expects(word(all, 64, 4) == 1 && word(all, 64 + 4, 4) == 5 && word(all, 64 + 32, 8) == headers + 3); + t.expects(all[headers] == 0x31 && all[headers + 2] == 0xC3); + t.expects(word(all, 64 + 56 + 8, 8) == 0x1000 && all[0x1000] == 'd' as u8); + + # The sections: none, each segment's, the note, then the tables. + val count := word(all, 0x3C, 2); + t.expects(count == 7 && word(all, 0x3E, 2) == 6); + val names: strview[7] = ["", ".text", ".data", ".note.gnu.build-id", ".symtab", ".strtab", ".shstrtab"]; + for k in 0..7u { + t.expects(section_named(all, k, names[k]), names[k]); + } +} + +@test def the_machine_is_the_one_given(t: &mut tester): void { + val code: u8[1] = [0xC3]; + var image := small_image(code[:]); + image.machine = 183; + val bytes := image.file(); + t.expects(word(bytes.view(), 18, 2) == 183); +} + +@test def the_build_is_named_by_what_it_loads(t: &mut tester): void { + val code: u8[3] = [0x31, 0xC0, 0xC3]; + var first := small_image(code[:]); + first.build_id(); + var again := small_image(code[:]); + again.build_id(); + val other_code: u8[3] = [0x31, 0xC0, 0xC2]; + var other := small_image(other_code[:]); + other.build_id(); + val note := first.sections[0].data.view(); + t.expects(note.length == 36 && word(note, 0, 4) == 4 && word(note, 4, 4) == elf::build_id_bytes && word(note, 8, 4) == 3); + t.expects(note == again.sections[0].data.view()); + t.expects(note != other.sections[0].data.view()); +} + +@test def a_described_file_holds_no_segment(t: &mut tester): void { + val code: u8[3] = [0x31, 0xC0, 0xC3]; + var image := small_image(code[:]); + image.described = true; + val bytes := image.file(); + val all := bytes.view(); + # Its segments are described, holding nothing in the file, as are their sections. + t.expects(word(all, 64 + 32, 8) == 0 && word(all, 64 + 40, 8) == elf::headers_size(2, false) + 3); + val headers := word(all, 0x28, 8); + t.expects(word(all, headers + 64 + 4, 4) == 8); + t.expects(all.length < 0x1000); +} + +@test def links_are_checked_as_zlib_checks_them(t: &mut tester): void { + t.expects(elf::crc32("123456789") == 0xCBF43926); + t.expects(elf::crc32("") == 0); +} diff --git a/tests/main.sakura b/tests/main.sakura new file mode 100644 index 0000000..12a73b1 --- /dev/null +++ b/tests/main.sakura @@ -0,0 +1,14 @@ +# Runs the tests of one library, those in its folder beside this file. From the +# root of the repository, those of `binary::amd64`: +# +# sakurac run --lib binary::amd64=amd64 tests/main.sakura tests/amd64/*.sakura +# +# Add `-- --only TEXT` to run those whose name holds the text, and `-- --here` to +# run them in one program, as a debugger wants. Built with `sakurac build`, they +# run as an executable the same way. +import system; +import std::testing; + +def main(args: span): void { + exit(tester::run(args)); +} diff --git a/tests/pdb/file.sakura b/tests/pdb/file.sakura new file mode 100644 index 0000000..b2a391d --- /dev/null +++ b/tests/pdb/file.sakura @@ -0,0 +1,138 @@ +# Tests of a whole PDB file, read back as a debugger reads one: the MSF file's +# description and map of free blocks, the stream naming the file and its map +# of named streams, the stream of modules and its numbers of the others, the +# table of names, and the GUID made from what the file holds. +import std::io; +import binary::pdb; +import std::testing; + + +# A small file: a type, a module with a procedure and its lines, the linker's, +# a public, the global naming the procedure, and two sections. +def small_file(public_name: strview): pdb::writer { + var made := pdb::writer::new(); + val arguments := made.types.arguments(span::::empty()); + val procedure := made.types.procedure(pdb::t_void, arguments, 0); + var symbols := pdb::symbol_stream::new(); + symbols.object_name("main.sakura"); + val version: u16[3] = [1, 0, 0]; + symbols.compiler(pdb::language_cxx, pdb::cv_machine_x64, version, "sakura"); + val at := symbols.procedure(true, "main", procedure, 2, 0, 16, 4, 15, pdb::proc_has_frame_pointer); + symbols.end(); + var sums := pdb::file_checksums::new(); + val file := sums.file(made.names.offset_of("/src/main.sakura")); + var lines := pdb::function_lines::new(2, 0, 16); + lines.line(file, 0, 1, 1); + lines.finish(); + var subsections := byte_writer::new(); + pdb::subsection(&mut subsections, pdb::debug_s_lines, lines.bytes.view()); + pdb::subsection(&mut subsections, pdb::debug_s_file_checksums, sums.bytes.view()); + val files: strview[1] = ["/src/main.sakura"]; + val ours := made.add_module("main.sakura", "main.sakura", &mut symbols.bytes, &mut subsections, files[:]); + var linker := pdb::symbol_stream::new(); + linker.object_name("* Linker *"); + var none := byte_writer::new(); + _ = made.add_module("* Linker *", "", &mut linker.bytes, &mut none, span::::empty()); + + # Two sections' headers: data, then code. + val names: strview[2] = [".data", ".text"]; + val characteristics: u32[2] = [0xC0000040, 0x60000020]; + for k in 0..2u { + for i in 0..8u { + made.sections.byte(i < names[k].length ? names[k][i] : 0); + } + made.sections.u32(16); # Its virtual size, + made.sections.u32(0x1000 * (k + 1)); # address, + made.sections.u32(0x1000); # bytes in the file, + made.sections.u32(0x1000 * (k + 1)); # and where. + made.sections.zeros(12); + made.sections.u32(characteristics[k]); + } + made.contribute(ours, 2, 0, 16, 0x60000020); + made.public(public_name, 2, 0, pdb::public_function | pdb::public_code); + made.procedure("main", ours, at, true); + return made; +} + +@test def the_file_is_an_msf_file_of_blocks(t: &mut tester): void { + var made := small_file("main"); + val bytes := made.file(); + val all := bytes.view(); + val magic: strview = "Microsoft C/C++ MSF 7.00\r\n"; + t.expects(holds_at(all, 0, magic)); + t.expects(all[26] == 0x1A && all[27] == 'D' as u8 && all[28] == 'S' as u8); + t.expects(word(all, 32, 4) == 4096 && word(all, 36, 4) == 1); + t.expects(word(all, 40, 4) * 4096 == all.length); + t.expects(stream_count(all) == 14); + # The map of free blocks says each block the file holds is taken, and those + # past them free. + val blocks := word(all, 40, 4); + for b in 0..blocks { + t.expects((all[4096 + b / 8] & (1u << (b % 8)) as u8) == 0); + } + t.expects((all[4096 + blocks / 8] & (1u << (blocks % 8)) as u8) != 0); +} + +@test def the_file_is_named_by_its_guid_and_age(t: &mut tester): void { + var made := small_file("main"); + val bytes := made.file(); + val info := stream_of(bytes.view(), 1); + val held := info.view(); + t.expects(word(held, 0, 4) == 20000404); + t.expects(word(held, 4, 4) == made.signature as u64 && word(held, 8, 4) == 1); + var named := false; + for k in 0..16u { + t.expects(held[12 + k] == made.guid[k]); + named = named || made.guid[k] != 0; + } + t.expects(named); + # The map of named streams names `/names`, in its hash's bucket of eight; the + # table follows the name's seven bytes. + t.expects(word(held, 28, 4) == 7 && holds_at(held, 32, "/names")); + t.expects(word(held, 39, 4) == 1 && word(held, 43, 4) == 8 && word(held, 47, 4) == 1); + t.expects(word(held, 51, 4) == (1u << ((pdb::hash_v1("/names") & 0xFFFF) % 8)) as u64); + t.expects(word(held, 55, 4) == 0 && word(held, 59, 4) == 0 && word(held, 63, 4) == 5); + t.expects(word(held, 67, 4) == 20140508 && held.length == 71); + + # The same build names its file the same way, and another another. + var again := small_file("main"); + val same := again.file(); + t.expects(same.view() == bytes.view()); + var other := small_file("start"); + _ = other.file(); + t.expects(other.signature != made.signature); +} + +@test def the_stream_of_modules_numbers_the_others(t: &mut tester): void { + var made := small_file("main"); + val bytes := made.file(); + val dbi := stream_of(bytes.view(), 3); + val held := dbi.view(); + t.expects(word(held, 0, 4) == 0xFFFFFFFF && word(held, 4, 4) == 19990903 && word(held, 8, 4) == 1); + t.expects(word(held, 12, 2) == 8 && word(held, 16, 2) == 9 && word(held, 20, 2) == 10); + t.expects(word(held, 58, 2) == 0x8664); + # The headers of the executable's sections are the stream the debugging + # headers name. + val end := held.length; + t.expects(word(held, end - 22 + 10, 2) == 11); + val sections := stream_of(bytes.view(), 11); + t.expects(sections.view() == made.sections.view()); + # The first module's stream holds its symbols then its lines. + val first := stream_of(bytes.view(), 12); + t.expects(word(first.view(), 0, 4) == 4 && word(first.view(), 6, 2) == pdb::s_objname as u64); +} + +@test def names_are_found_by_their_hash(t: &mut tester): void { + var made := small_file("main"); + val bytes := made.file(); + val names := stream_of(bytes.view(), 5); + val held := names.view(); + t.expects(word(held, 0, 4) == 0xEFFEEFFE && word(held, 4, 4) == 1); + val size := word(held, 8, 4); + val buckets := word(held, 12 + size, 4); + val wanted: strview = "/src/main.sakura"; + var slot := pdb::hash_v1(wanted) as u64 % buckets; + val offset := word(held, 16 + size + 4 * slot, 4); + t.expects(offset != 0 && holds_at(held, 12 + offset, wanted)); + t.expects(word(held, 16 + size + 4 * buckets, 4) == 1); +} diff --git a/tests/pdb/hashing.sakura b/tests/pdb/hashing.sakura new file mode 100644 index 0000000..2bbf370 --- /dev/null +++ b/tests/pdb/hashing.sakura @@ -0,0 +1,51 @@ +# Tests of the hashes a PDB file finds names and records by, against the values +# Microsoft's algorithms give, and of the digest naming a file. +import binary::pdb; +import std::testing; + + +@test def names_hash_as_microsofts_tools_hash_them(t: &mut tester): void { + # Words, then a half word, then a byte, cases made alike. + t.expects(pdb::hash_v1("") == 0x20240400); + t.expects(pdb::hash_v1("a") == 0x20240441); + t.expects(pdb::hash_v1("ab") == 0x20244649); + t.expects(pdb::hash_v1("abc") == 0x2024460A); + t.expects(pdb::hash_v1("abcd") == 0x646F8A62); + t.expects(pdb::hash_v1("abcde") == 0x646F8A27); + t.expects(pdb::hash_v1("/names") == 0x6D6CFC21); + t.expects(pdb::hash_v1("main") == 0x6E64C225); + t.expects(pdb::hash_v1("MAIN") == pdb::hash_v1("main")); +} + +@test def records_hash_by_crc_begun_at_zero(t: &mut tester): void { + t.expects(pdb::hash_record("123456789") == 0x2DFD2D88); + t.expects(pdb::hash_record("") == 0); +} + +@test def files_are_named_by_their_md5(t: &mut tester): void { + val digest := pdb::md5_of("abc"); + val expected: u8[16] = [0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72]; + var same := true; + for k in 0..16u { + same = same && digest[k] == expected[k]; + } + t.expects(same); + val empty := pdb::md5_of(""); + t.expects(empty[0] == 0xd4 && empty[15] == 0x7e); # d41d8cd98f00b204e9800998ecf8427e +} + +@test def registers_are_numbered_as_codeview_numbers_them(t: &mut tester): void { + t.expects(pdb::amd64_register(0, 8) == 328); # rax + t.expects(pdb::amd64_register(3, 8) == 329); # rbx + t.expects(pdb::amd64_register(5, 8) == 334); # rbp + t.expects(pdb::amd64_register(6, 8) == 332); # rsi + t.expects(pdb::amd64_register(15, 8) == 343); # r15 + t.expects(pdb::amd64_register(1, 4) == 18); # ecx + t.expects(pdb::amd64_register(9, 4) == 361); # r9d + t.expects(pdb::amd64_register(2, 2) == 11); # dx + t.expects(pdb::amd64_register(7, 1) == 325); # dil + t.expects(pdb::amd64_register(12, 1) == 348); # r12b + t.expects(pdb::amd64_xmm(0) == 154); + t.expects(pdb::amd64_xmm(7) == 161); + t.expects(pdb::amd64_xmm(8) == 252); +} diff --git a/tests/pdb/lines.sakura b/tests/pdb/lines.sakura new file mode 100644 index 0000000..6aef3b9 --- /dev/null +++ b/tests/pdb/lines.sakura @@ -0,0 +1,54 @@ +# Tests of a module's lines: the blocks of each file's lines with their +# columns, the subsections holding them, and the annotations of inline sites. +import std::io; +import binary::pdb; +import std::testing; + + +@test def lines_are_written_in_a_block_for_each_file(t: &mut tester): void { + var lines := pdb::function_lines::new(1, 0x10, 0x20); + lines.line(0, 0, 3, 5); + lines.line(0, 8, 4, 1); + lines.line(8, 12, 9, 2); + lines.finish(); + var expected := string::new(nothing {}); + expected.append("100000000100010020000000"); # Where the code is, with columns, + expected.append("000000000200000024000000000000000300008008000000040000800500000001000000"); # the first file's, + expected.append("0800000001000000180000000c0000000900008002000000"); # and the second's. + t.expects(hex_of(lines.bytes.view()).view() == expected.view(), hex_of(lines.bytes.view()).view()); + + var section := byte_writer::new(); + pdb::subsection(&mut section, pdb::debug_s_lines, "abc"); + t.expects(hex_of(section.view()).view() == "f20000000300000061626300"); +} + +@test def files_are_named_by_where_their_entries_are(t: &mut tester): void { + var sums := pdb::file_checksums::new(); + t.expects(sums.file(1) == 0 && sums.file(9) == 8); + t.expects(hex_of(sums.bytes.view()).view() == "01000000000000000900000000000000"); + var inlinees := pdb::inlinee_lines::new(); + inlinees.add(0x1000, 8, 42); + t.expects(inlinees.count == 1 && hex_of(inlinees.bytes.view()).view() == "0000000000100000080000002a000000"); +} + +@test def annotations_say_each_part_of_a_site_and_its_line(t: &mut tester): void { + var notes := pdb::annotations::new(0, 10); + # The first part, of the line the function begins at, says it is. + notes.at_line(0x20, 0, 10); + t.expects(hex_of(notes.bytes.view()).view() == "06000320"); + # A near part a near line on is one annotation. + notes.at_line(0x24, 0, 12); + # A part of the same line goes on. + notes.at_line(0x26, 0, 12); + # Code not the site's ends the part, then a part of another file follows. + notes.away(0x30); + notes.at_line(0x40, 8, 11); + notes.finish(0x48); + t.expects(hex_of(notes.bytes.view()).view() == "060003200b44040c0508060303100408"); + + # Lengths and offsets past seven bits take two bytes. + var far := pdb::annotations::new(0, 1); + far.at_line(0x1234, 0, 1); + far.finish(0x1240); + t.expects(hex_of(far.bytes.view()).view() == "0600039234040c"); +} diff --git a/tests/pdb/records.sakura b/tests/pdb/records.sakura new file mode 100644 index 0000000..11bd0d9 --- /dev/null +++ b/tests/pdb/records.sakura @@ -0,0 +1,129 @@ +# Tests of type records and symbols: numbers as numeric leaves, records padded +# to four bytes, records added twice held once but functions' IDs, field lists +# too long for one record going on in another, the header of a stream of types, +# and scopes naming where they end. +import std::io; +import binary::pdb; +import std::testing; + + +# The bytes a number is written as. +def unsigned_hex(value: u64): string { + var made := byte_writer::new(); + pdb::put_unsigned(&mut made, value); + return hex_of(made.view()); +} + +def signed_hex(value: i64): string { + var made := byte_writer::new(); + pdb::put_signed(&mut made, value); + return hex_of(made.view()); +} + +@test def numbers_are_written_as_numeric_leaves(t: &mut tester): void { + t.expects(unsigned_hex(0x7FFF).view() == "ff7f"); + t.expects(unsigned_hex(0x8000).view() == "02800080"); + t.expects(unsigned_hex(0x12345).view() == "048045230100"); + t.expects(unsigned_hex(0x123456789).view() == "0a808967452301000000"); + t.expects(signed_hex(5).view() == "0500"); + t.expects(signed_hex(-1).view() == "0080ff"); + t.expects(signed_hex(-200).view() == "018038ff"); + t.expects(signed_hex(-100000).view() == "03806079feff"); +} + +@test def type_records_are_padded_and_held_once(t: &mut tester): void { + var table := pdb::type_table::new(); + val pointer := table.pointer(pdb::t_int4, false); + t.expects(pointer == pdb::first_type); + t.expects(hex_of(table.records.view()).view() == "0a000210740000000c000100"); + val again := table.pointer(pdb::t_int4, false); + t.expects(again == pointer && table.count == 1); + + # A reference, and an array, whose name's zero is padded by `LF_PAD1`. + val reference := table.pointer(pdb::t_int4, true); + t.expects(reference == pdb::first_type + 1); + val array := table.array(pdb::t_int4, 16); + t.expects(array == pdb::first_type + 2); + val bytes := table.records.view(); + t.expects(hex_of(span::::over(bytes.data + 24, 16)).view() == "0e0003157400000023000000100000f1"); + + # Each function's ID is its own, though two are alike. + var ids := pdb::type_table::new(); + val first := ids.function_id(0, 0x1001, "write_all"); + val second := ids.function_id(0, 0x1001, "write_all"); + t.expects(first != second && ids.count == 2); +} + +@test def a_long_field_list_goes_on_in_a_record_before_it(t: &mut tester): void { + var table := pdb::type_table::new(); + var list := pdb::field_list::new(); + val name: strview = "a_member_whose_name_is_long_enough_to_fill_the_record"; + for k in 0..1200u { + list.member(pdb::t_int8, 8 * k, name); + } + t.expects(list.count == 1200); + val fields := table.fields(&list); + t.expects(table.count == 2 && fields == pdb::first_type + 1); + # The second record, added last, is the list's first, and ends naming the + # one it goes on in. + val all := table.records.view(); + val second := word(all, 0, 2) + 2; + val length := word(all, second, 2); + t.expects(length <= 0xFF00); + val end := second + 2 + length; + t.expects(word(all, end - 8, 2) == pdb::lf_index as u64 && word(all, end - 4, 4) == pdb::first_type as u64); +} + +@test def a_stream_of_types_says_what_it_holds(t: &mut tester): void { + var table := pdb::type_table::new(); + _ = table.pointer(pdb::t_void, false); + val named := table.structure("point", "sakura@1", 0, 0, 0, true); + val stream := table.stream(6); + val bytes := stream.view(); + t.expects(word(bytes, 0, 4) == 20040203 && word(bytes, 4, 4) == 56); + t.expects(word(bytes, 8, 4) == 0x1000 && word(bytes, 12, 4) == 0x1002); + t.expects(word(bytes, 16, 4) == table.records.length && word(bytes, 20, 2) == 6); + t.expects(word(bytes, 28, 4) == 0x3FFFF); + t.expects(bytes.length == 56 + table.records.length); + + # A declaration is hashed by its bytes, a definition by its name, so the one + # finds the other. + val complete := table.structure("point", "sakura@1", 0, 0, 16, false); + val hashes := table.hash_stream(); + val held := hashes.view(); + t.expects(named == 0x1001 && complete == 0x1002); + t.expects(word(held, 8, 4) == (pdb::hash_v1("point") % 0x3FFFF) as u64); + t.expects(word(held, 4, 4) != word(held, 8, 4)); + # The first record's index and offset, which a reader begins from. + t.expects(word(held, 12, 4) == 0x1000 && word(held, 16, 4) == 0); +} + +@test def scopes_name_where_they_begin_and_end(t: &mut tester): void { + var symbols := pdb::symbol_stream::new(); + val proc := symbols.procedure(true, "main", 0x1001, 3, 0x40, 0x20, 4, 0x1F, pdb::proc_has_frame_pointer); + t.expects(proc == 4); + symbols.block(3, 0x48, 8); + val block := proc + word(symbols.bytes.view(), proc, 2) + 2; + symbols.local(pdb::t_int8, 0, "x"); + symbols.in_frame(-8, pdb::code_range { section = 3, offset = 0x48, length = 8 }); + symbols.end(); + val block_end := symbols.bytes.length - 4; + val notes: u8[6] = [6, 0, 3, 4, 4, 2]; + symbols.inline_site(0x1000, notes[:]); + val site := block_end + 4; + symbols.end(); + val site_end := symbols.bytes.length - 4; + symbols.end(); + val proc_end := symbols.bytes.length - 4; + val all := symbols.bytes.view(); + + t.expects(word(all, 0, 4) == 4); + t.expects(word(all, proc + 2, 2) == pdb::s_gproc32 as u64 && word(all, proc + 4, 4) == 0 && word(all, proc + 8, 4) == proc_end); + t.expects(word(all, block + 2, 2) == pdb::s_block32 as u64 && word(all, block + 4, 4) == proc && word(all, block + 8, 4) == block_end); + t.expects(word(all, site + 4, 4) == proc && word(all, site + 8, 4) == site_end); + t.expects(word(all, block_end + 2, 2) == pdb::s_end as u64); + t.expects(word(all, site_end + 2, 2) == pdb::s_inlinesite_end as u64); + t.expects(word(all, proc_end + 2, 2) == pdb::s_end as u64); + # Every record is padded to four bytes. + t.expects(all.length % 4 == 0); +} diff --git a/tests/pdb/support.sakura b/tests/pdb/support.sakura new file mode 100644 index 0000000..1dc242f --- /dev/null +++ b/tests/pdb/support.sakura @@ -0,0 +1,88 @@ +# What the tests share: reading words of bytes, and reading a stream back out +# of an MSF file, as a debugger does. +import std::io; +import binary::pdb; +import std::testing; + + +# A word of bytes, least significant byte first. +def word(bytes: span, at: u64, size: u64): u64 { + var made: u64 = 0; + for k in 0..size { + made = made | (bytes[at + k] as u64 << (8 * k)); + } + return made; +} + +# Whether bytes hold text at a place. +def holds_at(bytes: span, at: u64, text: strview): bool { + return false if at + text.length > bytes.length; + for k in 0..text.length { + return false if bytes[at + k] != text[k]; + } + return true; +} + +# The bytes as hex, as the tests write what they expect. +def hex_of(bytes: span): string { + val digits: strview = "0123456789abcdef"; + var made := string::new(nothing {}); + for &each in bytes { + made.append(digits.substr((each >> 4) as u64, 1)); + made.append(digits.substr((each & 15) as u64, 1)); + } + return made; +} + +# How many streams an MSF file holds. +def stream_count(file: span): u64 { + val directory := directory_of(file); + return word(directory.view(), 0, 4); +} + +# The directory of an MSF file, gathered from the blocks the block map names. +def directory_of(file: span): byte_writer { + val block := word(file, 32, 4); + val size := word(file, 44, 4); + val map := word(file, 52, 4); + var made := byte_writer::new(); + var left := size; + var k: u64 = 0; + while left > 0 { + val at := word(file, map * block + 4 * k, 4) * block; + val take := left < block ? left : block; + for i in 0..take { + made.byte(file[at + i]); + } + left -= take; + k++; + } + return made; +} + +# A stream of an MSF file, gathered from its blocks. +def stream_of(file: span, number: u64): byte_writer { + val block := word(file, 32, 4); + val directory := directory_of(file); + val names := directory.view(); + val count := word(names, 0, 4); + var at := 4 + 4 * count; + for s in 0..number { + val size := word(names, 4 + 4 * s, 4); + at += 4 * ((size + block - 1) / block); + } + val size := word(names, 4 + 4 * number, 4); + var made := byte_writer::new(); + var left := size; + var k: u64 = 0; + while left > 0 { + val from := word(names, at + 4 * k, 4) * block; + val take := left < block ? left : block; + for i in 0..take { + made.byte(file[from + i]); + } + left -= take; + k++; + } + return made; +} diff --git a/tests/pe/pe.sakura b/tests/pe/pe.sakura new file mode 100644 index 0000000..d321275 --- /dev/null +++ b/tests/pe/pe.sakura @@ -0,0 +1,162 @@ +# Tests of PE files: the headers and sections of an executable, a long name in +# the table of strings, the data directories, how functions unwind, and the +# debug directory naming a PDB file, stamped with its GUID once it is written. +import std::io; +import binary::pe; +import std::testing; + + +# A word of bytes, least significant byte first. +def word(bytes: span, at: u64, size: u64): u64 { + var made: u64 = 0; + for k in 0..size { + made = made | (bytes[at + k] as u64 << (8 * k)); + } + return made; +} + +# The bytes as hex. +def hex_of(bytes: span): string { + val digits: strview = "0123456789abcdef"; + var made := string::new(nothing {}); + for &each in bytes { + made.append(digits.substr((each >> 4) as u64, 1)); + made.append(digits.substr((each & 15) as u64, 1)); + } + return made; +} + +# Where a section's header is, by its number from 0. +def header_of(file: span, k: u64): u64 { + val pe := word(file, 0x3C, 4); + return pe + 24 + word(file, pe + 20, 2) + 40 * k; +} + +@test def an_executable_holds_its_sections_where_they_load(t: &mut tester): void { + val code: u8[4] = [0x31, 0xC0, 0xC3, 0x90]; + val data: u8[3] = [1, 2, 3]; + var image := pe::image::new(0x400000, 0x402000); + image.stack_reserve = 0x100000; + image.stack_commit = 0x1000; + image.section(".data", 0x401000, data[:], 3, pe::readable | pe::writable); + image.section(".text", 0x402000, code[:], 4, pe::readable | pe::executable); + image.section(".empty", 0x403000, span::::empty(), 0, pe::readable); + image.section(".debug_info", 0x403000, data[:], 3, pe::readable | pe::discardable); + image.symbols.append(pe::symbol { name = "start", address = 0x402000 }); + image.imports = pe::directory { address = 0x401000, size = 3 }; + val bytes := image.file(); + val all := bytes.view(); + + t.expects(all[0] == 'M' as u8 && all[1] == 'Z' as u8); + val pe := word(all, 0x3C, 4); + t.expects(word(all, pe, 4) == 0x4550 && word(all, pe + 4, 2) == 0x8664); + # The empty section is left out. + t.expects(word(all, pe + 6, 2) == 3); + val o := pe + 24; + t.expects(word(all, o, 2) == 0x20B && word(all, o + 16, 4) == 0x2000 && word(all, o + 24, 8) == 0x400000); + t.expects(word(all, o + 68, 2) == 3 && word(all, o + 56, 4) == 0x4000); + t.expects(word(all, o + 112 + 8, 4) == 0x1000 && word(all, o + 116 + 8, 4) == 3); + + # Each section is where its address says, page for page, with its permissions. + val text := header_of(all, 1); + t.expects(word(all, text + 12, 4) == 0x2000 && word(all, text + 8, 4) == 4); + t.expects(word(all, text + 36, 4) == 0x60000020); + val at := word(all, text + 20, 4); + t.expects(all[at] == 0x31 && all[at + 2] == 0xC3); + t.expects(word(all, header_of(all, 0) + 36, 4) == 0xC0000040); + t.expects(word(all, header_of(all, 2) + 36, 4) == 0x42000040); + + # A name of more than eight bytes is where `/` and its offset say, in the + # table of strings after the symbols. + val named := header_of(all, 2); + t.expects(all[named] == '/' as u8); + val symbols := word(all, pe + 12, 4); + t.expects(word(all, pe + 16, 4) == 1); + val strings := symbols + 18 * 1; + var offset: u64 = 0; + var k: u64 = 1; + while all[named + k] >= '0' as u8 && all[named + k] <= '9' as u8 { + offset = offset * 10 + (all[named + k] - '0' as u8) as u64; + k++; + } + val name: strview = ".debug_info"; + var same := true; + for i in 0..name.length { + same = same && all[strings + offset + i] == name[i]; + } + t.expects(same); + # The symbol names its function within the code, by its offset there. + t.expects(word(all, symbols + 8, 4) == 0 && word(all, symbols + 12, 2) == 2); +} + +@test def a_prologue_is_described_latest_first(t: &mut tester): void { + # push rbp; mov rbp, rsp; push rbx: the frame found from `rbp`, sixteen past + # its base, `rbx` saved at its base plus eight. + var info := pe::unwind_info::new(); + info.pushed(1, 5); + info.allocated(4, 16); + info.framed(4, 5, 16); + info.saved(5, 3, 8); + val bytes := info.bytes(); + t.expects(hex_of(bytes.view()).view() == "01050515053401000403041201500000", hex_of(bytes.view()).view()); + + # Allocations of each size take the slots their sizes need. + var big := pe::unwind_info::new(); + big.allocated(4, 136); + big.allocated(9, 0x100000); + t.expects(hex_of(big.bytes().view()).view() == "01090500091100001000040111000000", hex_of(big.bytes().view()).view()); +} + +@test def records_are_shared_and_the_table_sorted(t: &mut tester): void { + var records := pe::unwind_table::new(); + var info := pe::unwind_info::new(); + info.allocated(4, 40); + val first := records.add(&info); + val again := records.add(&info); + var other := pe::unwind_info::new(); + other.pushed(1, 5); + val second := records.add(&other); + t.expects(first == 0 && again == 0 && second == 8); + + var functions := pe::function_table::new(); + functions.add(0x2040, 0x2080, second); + functions.add(0x2000, 0x2040, first); + val table := functions.bytes(); + t.expects(hex_of(table.view()).view() == "002000004020000000000000402000008020000008000000"); +} + +@test def the_debug_directory_names_a_pdb_file_stamped_once_written(t: &mut tester): void { + val code: u8[1] = [0xC3]; + var named := byte_writer::new(); + val record := pe::codeview_record(1, "app.pdb"); + pe::debug_entry(&mut named, pe::debug_codeview, 0x2000 + pe::debug_entry_bytes as u32, record.length as u32); + named.put(record.view()); + var image := pe::image::new(0x400000, 0x401000); + image.section(".text", 0x401000, code[:], 1, pe::readable | pe::executable); + image.section(".buildid", 0x402000, named.view(), named.length, pe::readable); + image.debug = pe::directory { address = 0x402000, size = pe::debug_entry_bytes }; + var bytes := image.file(); + + # The entry says where its record is in the file. + val entry := word(bytes.view(), header_of(bytes.view(), 1) + 20, 4); + val raw := word(bytes.view(), entry + 24, 4); + t.expects(word(bytes.view(), entry + 12, 4) == 2 && word(bytes.view(), entry + 20, 4) == 0x201C); + t.expects(bytes.view()[raw] == 'R' as u8 && bytes.view()[raw + 3] == 'S' as u8); + t.expects(word(bytes.view(), raw + 20, 4) == 1 && bytes.view()[raw + 24] == 'a' as u8); + + var guid: u8[16]; + for k in 0..16u { + guid[k] = (k * 17) as u8; + } + t.expects(pe::stamp_codeview(&mut bytes, guid, 0x12345678)); + t.expects(word(bytes.view(), entry + 4, 4) == 0x12345678); + var stamped := true; + for k in 0..16u { + stamped = stamped && bytes.view()[raw + 4 + k] == guid[k]; + } + t.expects(stamped); + + # The headers of the sections, as a PDB file keeps them. + val headers := pe::section_headers(bytes.view()); + t.expects(headers.length == 80 && word(headers.view(), 40 + 12, 4) == 0x2000); +}