@retrovm/nobj
v0.1.5
Published
Generate native linkable object files (COFF, Mach-O, ELF64) from TypeScript/Bun — zero native dependencies
Maintainers
Readme
@retrovm/nobj
Generate native linkable object files — COFF, Mach-O and ELF64 — containing exported read-only data symbols, directly from TypeScript/Bun.
No native dependencies. No spawn. No temp files. Just a Buffer out.
The problem
Embedding binary assets in a C/C++ project is usually done as a static array:
// generated by xxd or similar
static const unsigned char font_data[] = {
0x00, 0x01, 0x02, 0x03, /* ... 512 KB of this ... */
};This works, but it doesn't scale. Clang has to parse, lex and codegen every single byte as a numeric literal. A 1 MB asset becomes a translation unit with a million tokens — compilation slows to a crawl and RAM usage spikes. With several large assets the build becomes the bottleneck.
The right fix is to put the data in a proper object file and let the linker handle placement. This library generates that object file in pure TypeScript, with no external tools required.
Install
bun add @retrovm/nobjUsage
import { encodeSymbols } from '@retrovm/nobj'
const buf = encodeSymbols([
{ name: 'font_data', obj: Buffer.from(await Bun.file('font.bin').bytes()) },
{ name: 'shader_src', obj: '#version 330\nvoid main() {}' },
{ name: 'sample_rate', obj: 44100.0 },
])
await Bun.write('assets.o', buf)Link normally:
# Any Os
clang main.c assets.o -o appReference from C with no runtime overhead:
extern const unsigned char font_data[];
extern const char shader_src[];
extern const double sample_rate;API
encodeObject(name, obj, arch?, platform?)
Encodes a single symbol.
const buf = encodeObject('palette', paletteBuffer)encodeSymbols(symbols[], arch?, platform?)
Encodes multiple symbols into one object file. Preferred for production — one file, one link step.
const buf = encodeSymbols([
{ name: 'icon_png', obj: iconBuffer },
{ name: 'main_glsl', obj: shaderSource },
])Symbol values
| TypeScript type | C declaration | Notes |
|-----------------|--------------------------|----------------------------------------|
| Uint8Array | const uint8_t[] | Raw bytes, no transformation |
| string | const char[] | ASCII, null-terminated (except macOS) |
| number | const double | IEEE 754, 8 bytes |
Cross-compilation
By default both platform and architecture are inferred from the running process. Override to cross-compile:
encodeSymbols(symbols, 'arm64', 'linux') // Linux/arm64
encodeSymbols(symbols, 'x64', 'win32') // Windows/x64
encodeSymbols(symbols, 'arm64', 'macos') // macOS/arm64| TargetPlatform | Format | Section |
|------------------|--------|-----------|
| 'win32' | COFF | .rdata |
| 'macos' | Mach-O | __const |
| 'linux' | ELF64 | .rodata |
| TargetArch | COFF machine | Mach-O cputype |
|--------------|--------------|----------------|
| 'x64' | 0x8664 | 0x1000007 |
| 'arm64' | 0xAA64 | 0x100000C |
Format notes
The generated files are minimal but correct — accepted by clang, lld and ld without warnings, and readable by llvm-readobj, dumpbin and objdump.
- COFF — includes a
.drectvesection with/EXPORTdirectives and@feat.00to satisfy MSVC/clang-cl safe-SEH requirements. - Mach-O — emits
LC_BUILD_VERSIONtargeting macOS 10.9 so modernldaccepts the file without deprecation warnings. Symbol values are section-relative offsets, as required for relocatable objects. - ELF64 — uses a single
.strtabas bothsh_strtaband symbol string table. Includes an empty.note.GNU-stackto mark the stack as non-executable.
License
Copyright (c) 2026 Juan Carlos González Amestoy
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.
