diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-28 21:23:53 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-28 21:46:53 -0300 |
| commit | 3ad8a8117435124d5df8c1d70de62c32dd97e113 (patch) | |
| tree | 1a62696a04308a64f5122a17a8f0fabeae393677 /tools | |
| parent | b250eb6135475699a945324c005890e8e1b4dd0b (diff) | |
| download | pardes-3ad8a8117435124d5df8c1d70de62c32dd97e113.tar.gz pardes-3ad8a8117435124d5df8c1d70de62c32dd97e113.zip | |
one copy of each codec: PNG and JPEG through wuffs (ghostty's, already linked by ghostty-vt), stb_image without them, its writer and its inflate, SDL without its private stb_image; a symbol-table guard; docs/shared-libs.md
ReleaseFast before, both binaries carried stb_image's PNG, JPEG and inflate beside wuffs's (whose function tables keep its decoders linked whatever pardes calls), stb_image_write and its deflate (unused), and the GUI a second stb_image of SDL's (SDL_stb.c, for SDL_LoadPNG and camera MJPEG). Now:
- image.zig decodes PNG and JPEG with wuffs (`wuffs_images`, wherever ghostty-vt is in the build), sized from the IHDR or the first SOF before decoding, as stb's info call sized them; other formats and the builds without ghostty-vt keep stb_image.
- build.zig compiles zstbi with -DSTBI_NO_PNG -DSTBI_NO_JPEG and patches its stb_image_write include out (one count-checked anchor; a no-op stbi_flip_vertically_on_write stays for zstbi.deinit), and SDL with SDL_DISABLE_STB.
Text: tty -74,488 bytes, gui -154,392.
`zig build shared-libs-check` (in unit-test, ELF) reads the linked symbol table and fails on a family the build chose against: stb's PNG/JPEG/inflate beside wuffs, a second stb_image, stb_image_write's deflate, a second zlib or FreeType. Checked against the old binaries: it names every one.
docs/shared-libs.md: the inventory (implementation, users, copies and bytes before and after, allocator hook) and where one copy was not possible: zlib and wuffs both inflate (FreeType's and MuPDF's zlib API vs ghostty's kitty o=z), MuPDF's libjpeg beside wuffs (filter-dct.c is libjpeg's API), MuPDF's unreached fitz image loaders, base64 in Zig std/simdutf/MuPDF, per-library Unicode code, std.debug's flate.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/shared_libs_check.zig | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/tools/shared_libs_check.zig b/tools/shared_libs_check.zig new file mode 100644 index 00000000..d18c6850 --- /dev/null +++ b/tools/shared_libs_check.zig @@ -0,0 +1,127 @@ +//! One copy of each codec in a pardes binary (docs/shared-libs.md). +//! +//! zig build shared-libs-check -Dplatform=tty|gui +//! +//! Reads the linked executable's ELF symbol table and fails when a family of +//! symbols the build has chosen against is in it: a second PNG, JPEG or +//! inflate implementation beside the one docs/shared-libs.md names for the +//! job. The families are named by functions each copy defines (static ones +//! too: the symbol table keeps locals), so a dependency that brings its own +//! copy back shows here, whatever its symbols are called in the end. +const std = @import("std"); + +const Family = struct { + /// What the family is, and what the build uses instead. + what: []const u8, + /// Functions only that copy defines; an optimized build inlines some + /// of them, so a copy counts as linked when any is there, as many + /// times as the most of them are. + symbols: []const []const u8, + /// How many copies may be linked (0: none). + most: usize = 0, +}; + +const stb_png = [_][]const u8{ "stbi__png_load", "stbi__parse_png_file", "stbi__create_png_image_raw" }; +const stb_jpeg = [_][]const u8{ "stbi__jpeg_load", "stbi__decode_jpeg_header", "stbi__jpeg_decode_block" }; +const stb_inflate = [_][]const u8{ "stbi__parse_zlib", "stbi__do_zlib", "stbi__zbuild_huffman", "stbi__zhuffman_decode" }; +const stb_deflate = [_][]const u8{ "stbi_zlib_compress", "stbiw__zlib_bitrev", "stbi_write_png_to_mem" }; + +/// Banned in every binary that links ghostty-vt (wuffs decodes PNG and +/// JPEG; zlib and wuffs are the two inflates, see docs/shared-libs.md). +const with_wuffs = [_]Family{ + .{ .what = "stb_image's PNG decoder (wuffs decodes PNG: STBI_NO_PNG)", .symbols = &stb_png }, + .{ .what = "stb_image's JPEG decoder (wuffs decodes JPEG: STBI_NO_JPEG)", .symbols = &stb_jpeg }, + .{ .what = "stb_image's own inflate (zlib and wuffs inflate)", .symbols = &stb_inflate }, +}; + +/// Banned everywhere. +const always = [_]Family{ + // SDL's SDL_stb.c (SDL_LoadPNG, camera MJPEG) is off: SDL_DISABLE_STB. + .{ .what = "a second stb_image PNG decoder (SDL_stb.c)", .symbols = &stb_png, .most = 1 }, + .{ .what = "a second stb_image JPEG decoder (SDL_stb.c)", .symbols = &stb_jpeg, .most = 1 }, + // zstbi's stb_image_write, patched out: its own deflate beside zlib's. + .{ .what = "stb_image_write and its deflate (zlib deflates)", .symbols = &stb_deflate }, + // One zlib: FreeType's, which MuPDF links (mupdf.zig). + .{ .what = "a second zlib", .symbols = &.{ "inflate_table", "inflate_fast", "deflate_slow" }, .most = 1 }, + // One FreeType: the build's, which MuPDF is compiled against. + .{ .what = "a second FreeType", .symbols = &.{ "FT_Init_FreeType", "FT_New_Library" }, .most = 1 }, +}; + +pub fn main(init: std.process.Init) !void { + var arena_state: std.heap.ArenaAllocator = .init(std.heap.page_allocator); + defer arena_state.deinit(); + const arena = arena_state.allocator(); + const args = try init.minimal.args.toSlice(arena); + if (args.len != 2) return error.BadArgs; + const bytes = try readFile(arena, try arena.dupeSentinel(u8, args[1], 0)); + + var counts = std.StringHashMap(usize).init(arena); + try symbols(bytes, &counts); + if (counts.count() == 0) { + std.debug.print("shared-libs-check: {s} has no symbol table (stripped?): nothing to check\n", .{args[1]}); + return; + } + const wuffs = counts.get("wuffs_zlib__decoder__transform_io") != null or + counts.get("wuffs_base__pixel_swizzler__prepare") != null; + var failed = false; + for ([2][]const Family{ &always, if (wuffs) &with_wuffs else &.{} }) |set| for (set) |family| { + var n: usize = 0; + var seen: []const u8 = family.symbols[0]; + for (family.symbols) |symbol| { + const here = counts.get(symbol) orelse 0; + if (here > n) { + n = here; + seen = symbol; + } + } + if (n > family.most) { + failed = true; + std.debug.print("shared-libs-check: {s}: `{s}` is linked {d} times (at most {d})\n", .{ family.what, seen, n, family.most }); + } + }; + if (failed) return error.DuplicateLibrary; + std.debug.print("shared-libs-check: one copy of each ({d} symbols{s})\n", .{ counts.count(), if (wuffs) ", wuffs present" else "" }); +} + +/// Counts every name in the ELF64 little-endian symbol tables of `bytes`. +fn symbols(bytes: []const u8, counts: *std.StringHashMap(usize)) !void { + if (bytes.len < @sizeOf(std.elf.Elf64_Ehdr) or !std.mem.eql(u8, bytes[0..4], "\x7fELF")) return error.NotElf; + if (bytes[std.elf.EI.CLASS] != std.elf.ELFCLASS64 or bytes[std.elf.EI.DATA] != std.elf.ELFDATA2LSB) return error.UnsupportedElf; + const eh = std.mem.bytesAsValue(std.elf.Elf64_Ehdr, bytes[0..@sizeOf(std.elf.Elf64_Ehdr)]); + const shoff: usize = @intCast(eh.e_shoff); + const shnum: usize = eh.e_shnum; + const shsize: usize = eh.e_shentsize; + if (shsize != @sizeOf(std.elf.Elf64_Shdr) or shoff + shnum * shsize > bytes.len) return error.BadSections; + const sections = std.mem.bytesAsSlice(std.elf.Elf64_Shdr, @as([]align(1) const u8, bytes[shoff..][0 .. shnum * shsize])); + for (sections) |section| { + if (section.sh_type != std.elf.SHT_SYMTAB) continue; + const strtab = sections[section.sh_link]; + const strings = bytes[@intCast(strtab.sh_offset)..][0..@intCast(strtab.sh_size)]; + const table = bytes[@intCast(section.sh_offset)..][0..@intCast(section.sh_size)]; + const syms = std.mem.bytesAsSlice(std.elf.Elf64_Sym, @as([]align(1) const u8, table)); + for (syms) |sym| { + // Functions only: a family is the code it brings. + if (sym.st_type() != std.elf.STT_FUNC or sym.st_shndx == 0) continue; + const name = std.mem.sliceTo(strings[sym.st_name..], 0); + const gop = try counts.getOrPut(name); + gop.value_ptr.* = if (gop.found_existing) gop.value_ptr.* + 1 else 1; + } + } +} + +fn readFile(arena: std.mem.Allocator, path: [:0]const u8) ![]u8 { + const c = std.c; + const fd = c.open(path, .{ .ACCMODE = .RDONLY }, @as(c.mode_t, 0)); + if (fd < 0) return error.OpenFailed; + defer _ = c.close(fd); + const end = c.lseek(fd, 0, c.SEEK.END); + if (end < 0 or c.lseek(fd, 0, c.SEEK.SET) < 0) return error.ReadFailed; + const out = try arena.alloc(u8, @intCast(end)); + var filled: usize = 0; + while (filled < out.len) { + const n = c.read(fd, out[filled..].ptr, out.len - filled); + if (n <= 0) return error.ReadFailed; + filled += @intCast(n); + } + return out; +} |
