Skip to content

Instantly share code, notes, and snippets.

View mlugg's full-sized avatar

Matthew Lugg mlugg

View GitHub Profile
@mlugg
mlugg / debug.zig
Created October 9, 2025 12:22
well-commented basic freestanding SelfInfo implementation
//! This whole file is meant to be exposed as `@import("root").debug`. Everything `pub` here is an
//! override which the standard library will use.
/// This is the allocator which `std.debug` will use to allocate debug info. On freestanding you
/// usually just want to use a `std.heap.FixedBufferAllocator` for this---unless you have a proper
/// memory manager working of course!
pub fn getDebugInfoAllocator() Allocator {
const Global = struct {
/// If you see OOM errors in stack traces, try increasing the size of this buffer. This is
/// 16 MiB, which is about 4x what ClashOS has been observed to use. The amount of memory is
@mlugg
mlugg / inject_debug_info.zig
Created October 9, 2025 12:48
linker script and binary-modifying tool for a freestanding SelfInfo impl
//! This is the bespoke script which finds the DWARF sections and copies them all to where we want
//! them in the `.rodata` section. It's quite primitive, but works fine for ClashOS at least! I'm
//! not going to explain this toooo in-depth; if you want to understand it you'll need to know a bit
//! about how ELF files are structured, feel free to ask me.
// zig fmt: off
/// You might need to modify this, since RISC-V binaries might have some slightly different fields
/// here compared to AArch64 binaries.
const expect_e_ident: *const [16]u8 = &.{
0x7F, 'E', 'L', 'F',