# Packs

A pack is a `.borer` text file: what borer should know about a family of
programs, kept out of borer's code. borer ships no packs. Anyone can write
one, and every rule in borer works without any.

### The format

A pack is read line by line:

- A line starting with `#` is a comment. Blank lines are skipped.
- `pack <name> <version>` comes first, before any section. The name is one
  word.
- `about <text>` says what the pack is for. It is optional.
- `[section]` starts a section. Every line after it, up to the next section,
  belongs to it.
- Every other line is a key, a space, and its value.

A key borer does not read is kept and reported as a warning, so a pack written
for a newer borer still loads. A line outside every section, or a section
before the `pack` line, is an error.

### A complete example

This pack names SQLite's Windows file layer. It loads by itself for any
program that holds SQLite's `os_win.c` error format and exports
`sqlite3_open_v2`. Two functions are named by their first bytes, and two
scripts name the rest (they are the [logger](/docs/plugins/your-first-script.md) and
[table](/docs/plugins/recipes.md#label-a-handler-table) scripts).

```text
packs/
  sqlite-win.borer
  sqlite-win/
    logger_names.rhai
    label_table.rhai
```

```borer
# SQLite for Windows: names the os_win.c VFS functions.
#
# Loads by itself, from a pack directory, for a program both [match]
# lines fit. --pack or BORER_PACKS loads it for any program.

pack sqlite-win 1
about SQLite built for Windows: the os_win.c VFS functions by name

[match]
# The VFS's error format, and an export only SQLite has.
string os_win.c:%d: (%lu) %s(%s) - %s
export sqlite3_open_v2

[names]
# From `borer export-pack`: each function by its first bytes.
fn 48895C241048896C241856574155415641574883EC50 winRead
fn 488BC4488958084889681048897020 winWrite

[scripts]
# Name what the VFS functions log, then the io-methods table's slots.
run sqlite-win/logger_names.rhai
run sqlite-win/label_table.rhai
```

`borer pack` reads a pack and reports what it holds, with a warning for each
key borer does not read:

```text
$ borer pack packs\sqlite-win.borer
# pack sqlite-win 1: 6 lines in 4 keys, 0 warnings
# SQLite built for Windows: the os_win.c VFS functions by name
section	key	lines
match	export	1
match	string	1
names	fn	2
scripts	run	2
```

`borer packs` shows every pack borer can see, whether each loads for a binary,
and what it did:

```text
$ $env:BORER_PACK_DIRS = "packs"
$ borer packs sqlite3.dll
# 0 named, 1 in the pack directories (packs)
pack	version	file	loads	match
sqlite-win	1	packs\sqlite-win.borer	yes	string os_win.c:%d: (%lu) %s(%s) - %s, export sqlite3_open_v2
# pack names: 2 of 2 named a function, 0 matched one the program names itself (0 by the same name), 0 matched no function entry, 0 matched several, 0 do not read
# library names: 0 of 6217 signatures (0 from packs, 6217 shipped) named a function, 0 matched one the program names itself (0 by the same name), 0 do not read
# pack scripts: 2 ran, 0 failed, 20 functions named
pack	script	named	kept	comments	operations	result
sqlite-win	packs\sqlite-win\logger_names.rhai	9	0	0	11473	skipped winClose: 2 functions log it
sqlite-win	packs\sqlite-win\label_table.rhai	11	2	2	2510	skipped winUnlock: 2 functions fill it

$ borer functions sqlite3.dll winShm
borer: pack sqlite-win 1 from packs\sqlite-win.borer fits: string os_win.c:%d: (%lu) %s(%s) - %s, export sqlite3_open_v2
# showing 4 of 4 functions; 104 import thunks left out, filter=thunk lists them; 12 exception funclets left out, filter=funclet lists them
# entry size blocks instrs callers callees src name
0x18000f600 299 26 72 0 1 pack winShmUnmap
0x18000f730 574 43 154 0 0 pack winShmLock
0x18000f970 51 5 13 0 0 pack winShmBarrier
0x18000f9b0 774 51 204 0 5 pack winShmMap
```

For notepad the same pack does not load: `no  fails string os_win.c:%d: ...`.

### \[match]: which programs a pack is for

Every line must hold for the pack to load by itself. A pack with no `[match]`
lines never loads by itself; name it with `--pack` or `BORER_PACKS`.

| Key                                               | Holds when                                  |
| ------------------------------------------------- | ------------------------------------------- |
| `string <text>`                                   | the file holds the text, as ASCII or UTF-16 |
| `import <module>` or `import <module>!<function>` | the program imports it; case is ignored     |
| `export <name>`                                   | the program exports the name                |
| `section <name>`                                  | the program has a section of that name      |

Match on what only your family has: an assertion banner, a version string, a
unique export. Write the module as the import table spells it: notepad imports
`CreateFileW` from `api-ms-win-core-file-l1-1-0.dll`, not from `kernel32.dll`.
`borer symbols <binary> <name>` shows the module.

### \[names]: functions by their bytes

| Key                                               | Value                                                                                                                                                                                                 |
| ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `fn <signature> <name>`                           | The one function whose entry matches the signature takes the name. The signature is the function's first bytes in hex, `??` for a byte that moves between builds.                                     |
| `lib <signature> <symbol> [<offset>=<symbol>...]` | A library function as its object file has it, with the bytes relocations fill as `??` and what each relocation names. `borer sigmake` writes these from `.lib`, `.obj`, `.a`, `.o` and `.rlib` files. |

A name goes only on a function borer left unnamed or named by vtable slot. A
signature that matches no entry, or several, names nothing, and `borer packs`
counts both. You rarely write `fn` lines by hand: `export-pack` writes them.

### \[scripts]: rules that run on every load

| Key          | Value                                                                 |
| ------------ | --------------------------------------------------------------------- |
| `run <file>` | A Rhai script, its path relative to the pack's own file (`/` or `\`). |

Each script runs at the end of every analysis the pack takes part in:

- It sees the analysis with the pack's `[names]` and `[lib]` names in place,
  but not the names another script proposes in the same run. The order of the
  scripts does not change what any of them sees.
- Its names go on functions borer left unnamed, named by vtable slot, or named
  from an algorithm's constants. The first script's name wins. A name for a
  function that already has one is counted as `kept`.
- `args()` is empty, and `query` and `rows` are not available.
- Its comments are counted and dropped: an analysis holds no comments.
- A script that fails names nothing; `borer packs` shows its error.

### \[naming]: what an engine calls its statics

Each key is single: the last pack's value wins, else the default.

| Key                   | Default          | Names                                                        |
| --------------------- | ---------------- | ------------------------------------------------------------ |
| `singleton`           | `instance`       | the static holding a class's one instance: `Class::<name>`   |
| `class_id`            | `class_id`       | the static a registered class's id accessor hands out        |
| `class_id_accessor`   | `get_class_id`   | a registered class's id accessor                             |
| `class_info`          | `class_info`     | a class's type descriptor, the object its registration names |
| `class_info_accessor` | `get_class_info` | a function whose whole body hands out a class's descriptor   |

### \[types]: serializer streams

| Key      | Default                                                                | Value                                                                         |
| -------- | ---------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
| `stream` | `Stream`, `Writer`, `Archive`, `Serializer`                            | text a serializer's stream class contains (`Net::`, `BitStream`)              |
| `wire`   | `Net`, `Socket`, `Packet`, `BitStream`, `Wire`                         | text a stream class contains when it is the network, not a file               |
| `kind`   | `Guid guid`, `Blob blob`, `Atom atom`, `String string`, `Array< array` | `<word> <kind>`: a word in a reader helper's text and the value kind it hints |

A pack's `stream` or `wire` lines replace the defaults. Its `kind` lines come
before the defaults, which still apply.

### \[roles]: what classes are for

| Key                           | Value                                                                                                                                                                  |
| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `class <base class> <role>`   | classes deriving from the base play the role                                                                                                                           |
| `prefix <name prefix> <role>` | classes whose name starts with the prefix play the role                                                                                                                |
| `word <word> <role>`          | a class whose name, or an ancestor's, ends with the word plays the role; the defaults (`Manager manager`, `Widget widget`, `Handler handler` and ten more) still apply |

The roles group classes in `borer map` and `borer://map`.

### \[attributes] and \[messages]: engine catalogs

| Key                             | Value                                                                                                                                            |
| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `[attributes] registrar <name>` | the qualified-name suffix of the function every typed attribute registrar calls (`AttrDefBase::Register`); `borer attrs` starts from its callers |
| `[messages] accept <name>`      | a helper a message port's declaring virtual calls with each message id it accepts; `borer ports` starts from its callers                         |

Both have no default: without a pack, those catalogs are empty.

`borer pack-keys` prints every key with its kind, default and meaning, from
the engine itself:

```text
$ borer pack-keys
# 20 keys a pack can set; a list's defaults apply when no pack sets it, a list+'s always
section	key	kind	default	meaning
naming	singleton	single	instance	the name of the static holding a class's one instance (a thread-local slot the analysis ties to the class): Class::<name>
...
match	section	list	-	the name of a section the program has
```

### Several packs at once

Packs load together. A list key gathers every pack's lines in load order. A
single key takes the last pack's value. Packs from the pack directories load
first, then the ones you name, so a named pack's single values win. A
directory pack is skipped when you name a pack of the same name.

### Loading packs

| How                                                        | Loads for                                                |
| ---------------------------------------------------------- | -------------------------------------------------------- |
| `--pack <file>`, repeatable                                | every program, on the command line                       |
| `BORER_PACKS`, a path list (`;` on Windows, `:` elsewhere) | every program, on the command line and in the MCP server |
| a pack directory                                           | only the programs every one of its `[match]` lines fits  |

The pack directories are the ones `BORER_PACK_DIRS` lists. When it is not set,
they are `%APPDATA%\borer\packs` on Windows (`$XDG_CONFIG_HOME/borer/packs`, or
`~/.config/borer/packs`, elsewhere), and a `packs` folder beside the borer
executable or two levels above it. borer reads the `.borer` files directly in
each directory, not in its subfolders, so a pack's scripts can sit in a folder
of their own. `--no-auto-packs`, or `BORER_AUTO_PACKS=0`, turns the
directories off.

A pack named with `--pack` or `BORER_PACKS` loads even when its `[match]` lines
fail:

```text
$ $env:BORER_PACKS = "packs\sqlite-win.borer"
$ borer packs notepad.exe
# 1 named, ...
pack	version	file	loads	match
sqlite-win	1	packs\sqlite-win.borer	yes: named	string os_win.c:%d: (%lu) %s(%s) - %s, export sqlite3_open_v2
```

The MCP server reads `BORER_PACKS` when it starts, and the pack directories
each time it loads a binary. `borer://packs` answers the same table as
`borer packs`.

### export-pack: names that survive a rebuild

`export-pack` turns names into `[names] fn` lines. Each signature is a
function's first bytes, at least 12 and as many as it takes to tell it from
every other function's entry. Call and branch displacements, RIP-relative
displacements and addresses inside the image are `??`, since they move between
builds:

```text
$ borer export-pack sqlite3.dll winvfs names.json > winvfs.borer
```

```borer
# Exported by borer from sqlite3.dll: 11 of 11 names by their functions' first bytes.
# Add a [match] section (a string only this program holds) to load it by itself.

pack winvfs 1
about names exported from sqlite3.dll

[names]
fn 48895C241048896C241856574155415641574883EC50 winRead
fn 488BC4488958084889681048897020 winWrite
...
```

- `borer export-pack <binary> <name> [project] [all]` writes the names a
  project file gives. With `all` it adds the names borer recovered itself, but
  not symbols and not names built from an address.
- Over MCP, `borer://exportpack/<name>` (or `<name>,all`) writes the session's
  names the same way.
- A name no signature tells apart from another function is listed as a
  comment at the end, not as a line.

A signature finds the function again in another build only while its first
bytes stay the same. A recompile with other options, or a change to the
function, can move it; `borer packs` then counts the line as `matched no
function entry`.
