Files
faumaray bba5c8a39a initial commit
Signed-off-by: Aleksey Selihov <faumaray@gmail.com>
2026-05-22 02:40:32 +03:00

205 lines
8.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# `.vlc` container format — v0.1 (draft)
Status: **M1–M3 complete** (Linux ELF + Windows PE synthesis, fat-VLC fuse, dlopen interposition). macOS Mach-O synthesis in progress (M4).
All integers are **little-endian**. All offsets are **byte offsets from the start of the file**. The on-disk layout is `#pragma pack(push, 1)` — no implicit padding. `format/vlc_format.hpp` is the source of truth; this document explains intent.
---
## File extensions
VoidLibC uses three file extensions based on the container's `ContainerFlags`:
| Extension | Name | Flag | Description |
|-----------|------------------|----------------------------|-------------|
| `.vle` | Executable | `Executable` (0x04) | Has a defined entry point. Run via `voidloader` or binfmt. |
| `.vls` | Shared library | *(none of the below)* | Loadable at runtime via `dlopen()` (interposed by `libvoid.a`). |
| `.vla` | Static archive | `StaticLibrary` (0x10) | For build-time linking via `voidlink`. Not loadable at runtime. |
`voidlink` automatically appends the correct extension when `-o` is specified without one. The extension is a convention for humans and file managers; tools identify VoidLibC files by the `VLC\0` magic bytes, not by extension.
---
## `file(1)` integration
A magic definition (`installer/linux/magic/vlc.magic`) is provided for the `file` command. After installation (the installer copies it to `/usr/share/file/magic/` and recompiles the database), `file` recognizes VoidLibC files:
```
$ file hello.vle
hello.vle: VoidLibC binary, v0.1, executable (.vle), x86_64
$ file lib.vls
lib.vls: VoidLibC binary, v0.1, x86_64, dynamic imports
```
---
## Consumer API
Consumers use standard `<dlfcn.h>`. Linking against `VoidLibC::void` (a static archive) interposes `dlopen` so that any path whose first four bytes are `VLC\0` is handled by VoidLibC's synthesis path; all other paths fall through to the OS's real `dlopen`.
```c
#include <dlfcn.h>
void* h = dlopen("mylib.vls", RTLD_NOW); // transparently loaded by libvoid.a
int (*fn)(int, int) = dlsym(h, "add");
printf("%d\n", fn(1, 2));
dlclose(h);
```
`dlsym`, `dlclose`, and `dlerror` are **not** interposed — the handle returned is a native OS handle, so the native implementations work directly.
For executable containers (`.vle`), invoke via `voidloader`:
```sh
voidloader hello.vle arg1 arg2
# or, if binfmt_misc is registered on Linux:
./hello.vle arg1 arg2
```
---
## Shipping a fat VLC (CMake integration)
Link against `VoidLibC::void` and call `voidlibc_enable_auto_vlc()` at the end of your `CMakeLists.txt`. The DEFER hook cross-compiles your sources for Linux (musl), Windows (MinGW), and macOS, then fuses them into a single fat container automatically.
```cmake
find_package(VoidLibC REQUIRED)
add_library(mylib SHARED src.c)
target_link_libraries(mylib PRIVATE VoidLibC::void)
set_target_properties(mylib PROPERTIES EXCLUDE_FROM_ALL TRUE)
# Optional: mark as executable (.vle) instead of library (.vls)
# voidlibc_vlc_executable(mylib main)
# Optional: restrict to Linux only (e.g., when using extern global vars)
# set_target_properties(mylib PROPERTIES VOIDLIBC_VLC_PLATFORMS "linux")
voidlibc_enable_auto_vlc() # call once per CMakeLists.txt
```
After `cmake --build`, `<builddir>/mylib.vls` is a fat container with Linux + Windows + macOS sections. Requires `zig` on `PATH` (preferred) or `clang` with cross-compilation support.
---
## File outline
```
+-------------------------------+ 0
| FileHeader | fixed 84 bytes
+-------------------------------+ section_dir_off
| SectionEntry[section_count] | 64 bytes each
+-------------------------------+ <symtab_off>
| SymbolTableHeader |
| SymbolEntry[...] | 36 bytes each
| name pool (UTF-8) |
+-------------------------------+ <reloc chunks, per section>
| RelocChunk + RelocEntry[] | see reloc-ir.md
+-------------------------------+ <per-OS code, per section>
| raw machine code (PIC) |
+-------------------------------+ <per-OS debug, per section, optional>
| DWARF / PDB / DWARF blob |
+-------------------------------+ <imports_off>
| ImportsHeader + ... | (M1)
+-------------------------------+ <exports_off>
| ExportsHeader + ... | (M1)
+-------------------------------+ file_size
```
The directory + tables describe *where* things are; producers can lay regions out in any order as long as offsets are consistent and `file_size` matches the on-disk total.
---
## FileHeader (84 bytes)
| Field | Type | Notes |
|-------------------|----------|-------|
| `magic` | `u32` | `0x00434C56` = bytes `V L C \0`. Reject anything else. |
| `version_major` | `u16` | Breaking changes bump this. Readers must reject unknown majors. |
| `version_minor` | `u16` | Additive changes bump this. Readers may accept higher minors of a known major. |
| `flags` | `u32` | Bitfield. See `ContainerFlags` in the header. |
| `arch` | `u16` | `1` = x86_64. MVP rejects everything else. |
| `reserved` | `u16` | Must be `0`. |
| `section_count` | `u32` | Number of `SectionEntry`s following `section_dir_off`. |
| `section_dir_off` | `u64` | Byte offset of the section directory. |
| `symtab_off` | `u64` | Byte offset of `SymbolTableHeader`. |
| `symtab_size` | `u64` | Total bytes of the symbol-table region (header + entries + name pool). |
| `imports_off` | `u64` | (M1) |
| `imports_size` | `u64` | (M1) |
| `exports_off` | `u64` | (M1) |
| `exports_size` | `u64` | (M1) |
| `file_size` | `u64` | Total bytes of the file. Must equal the actual on-disk length. |
For an empty container, all `*_off` may equal `sizeof(FileHeader)` with corresponding sizes `0`. This is what `voidlink create-empty` produces.
---
## SectionEntry (64 bytes)
One per (target OS) at MVP — typically 3 (Linux, Windows, macOS). Each entry describes a per-OS native code region plus the portable relocation IR that patches it.
| Field | Type | Notes |
|----------------|----------|-------|
| `target_os` | `u16` | `1` Linux, `2` Windows, `3` macOS. |
| `reserved0` | `u16` | `0`. |
| `reserved1` | `u32` | `0`. |
| `code_off` | `u64` | Offset of the raw code blob. |
| `code_size` | `u64` | Code blob byte length. |
| `reloc_off` | `u64` | Offset of the `RelocChunk` for this section. |
| `reloc_size` | `u64` | RelocChunk byte length. |
| `debug_off` | `u64` | `0` if no debug info. |
| `debug_size` | `u64` | `0` if no debug info. |
| `entry_offset` | `u64` | For executables: offset of the entry point within `code`. `0` for libraries with no entry. |
Sections **must be position-independent** (PIC / `-fPIC`). The synthesis core relies on no absolute addresses being baked into `code`.
---
## SymbolTableHeader + SymbolEntry (36 bytes each)
A flat region: `SymbolTableHeader` + `SymbolEntry[entry_count]` + a UTF-8 name pool of size `name_pool_size`. Symbol references elsewhere are by `u32` index into this entry array.
Each symbol carries **three optional name views** into the pool:
- `name` — canonical (typically C linkage or unmangled) name.
- `itanium_name` — Itanium C++ mangled form (Linux/macOS). `name_off=0` means absent.
- `msvc_name` — MSVC C++ mangled form (Windows). `name_off=0` means absent.
When the synthesis core re-exports a C++ symbol, it picks the mangled form matching the running OS. C symbols use `name` for all three OSes.
`SymbolKind`: `Undefined`/`Function`/`Object`/`TLS`. `SymbolFlags`: bitfield with `Exported`, `Weak`, `Hidden`.
---
## RelocChunk
8-byte header followed by an array of `RelocEntry`. See `format/reloc_ir.hpp` and (forthcoming) `docs/reloc-ir.md` for opcode semantics.
`RelocChunk.target_os` must match the owning `SectionEntry.target_os` — redundant but lets readers validate.
---
## Version policy
- **v0.x** is *unstable*. The format may break between minors during pre-alpha.
- **v1.0** will lock the on-disk layout. After that:
- Major bump = breaking change. Old readers reject the file.
- Minor bump = additive change. Old readers may parse it, ignoring unknown bits in `flags`.
- New `RelocOp` opcodes are an additive change (minor bump). Readers encountering unknown opcodes must fail closed, not silently skip.
---
## Validation checklist for readers
A conforming reader **must** check, in order:
1. File length ≥ `sizeof(FileHeader)`.
2. `magic == 0x00434C56`.
3. `version_major` is supported.
4. `arch == X86_64`.
5. `file_size` equals the on-disk length.
6. All `*_off + *_size` are within `file_size`.
7. `section_dir_off + section_count * sizeof(SectionEntry)` is within `file_size`.
8. Each `SectionEntry`'s `code_off + code_size`, `reloc_off + reloc_size`, `debug_off + debug_size` are within `file_size`.
9. `RelocChunk.target_os` matches its owning `SectionEntry.target_os`.
Readers must treat any failure as a hard error — never best-effort parse a malformed `.vlc`.