//! 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 }, // One zstd: the build's, a far replica's stream (src/replica.zig). .{ .what = "a second zstd", .symbols = &.{ "ZSTD_compressStream2", "ZSTD_decompressStream", "ZSTD_createCCtx_advanced" }, .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; }