diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 23:25:41 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 341882fc1a655137c7a68cdf45c77d5d53c0e522 (patch) | |
| tree | 9753b572f5d236f9d165d6110fc6d2def7b82720 /src/host | |
| parent | 8ce27cfdbab6d576db36403afbcef9659cb6dbdc (diff) | |
| download | sketchbook-341882fc1a655137c7a68cdf45c77d5d53c0e522.tar.gz sketchbook-341882fc1a655137c7a68cdf45c77d5d53c0e522.zip | |
Load libraries from Zig 0.17.1's incremental linker
`-fincremental` selects Zig's new ELF linker, and 0.17.1's writes DT_VERSYM
without DT_VERDEF or DT_VERNEED when nothing is versioned: glibc then reads
a null version table at the first relocation against an export, and dlopen
faults. When a library has that shape, the host loads a copy of it in an
anonymous memory file with DT_VERSYM dropped (nothing in it is versioned,
so nothing is lost); the build's own file is never written, and every other
library loads as before. The copy is 10 ms for the counter's 28 MB library.
Fixed in Zig after 0.17.1 (6d27011edb): the check then never fires. The
reload test's second build goes through the new linker.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/host')
| -rw-r--r-- | src/host/library.zig | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/src/host/library.zig b/src/host/library.zig index 91b2296..6516704 100644 --- a/src/host/library.zig +++ b/src/host/library.zig @@ -48,8 +48,12 @@ pub const Library = struct { const cwd = std.Io.Dir.cwd(); const file = try cwd.openFile(io, source, .{}); defer file.close(io); + const fixed = try withoutVersym(file.handle); + defer if (fixed) |fd| { + _ = linux.close(fd); + }; var target_buf: [32]u8 = undefined; - const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{file.handle}); + const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{fixed orelse file.handle}); var buf: [std.fs.max_path_bytes]u8 = undefined; const link = try std.mem.printSentinel(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); try cwd.symLink(io, target, link, .{}); @@ -62,6 +66,55 @@ pub const Library = struct { } }; +// ponytail: Zig 0.17.1's new ELF linker (which `-fincremental` selects) +// writes DT_VERSYM without DT_VERDEF or DT_VERNEED when nothing is +// versioned, and glibc's dlopen then reads a null version table and +// faults. Such a library has nothing versioned, so a copy without +// DT_VERSYM, in an anonymous memory file, loads instead (the build's own +// file is never written: the watch would see the write). Never fires on a +// correct library; dead once Zig has commit 6d27011edb (0.18): delete it. +fn withoutVersym(fd: i32) !?i32 { + const elf = std.elf; + var ehdr: elf.Elf64.Ehdr = undefined; + try pread(fd, std.mem.asBytes(&ehdr), 0); + if (!std.mem.eql(u8, &ehdr.ident.magic, elf.MAGIC) or ehdr.ident.class != .@"64" or ehdr.phnum > 32) return null; + var phdrs: [32]elf.Elf64.Phdr = undefined; + try pread(fd, std.mem.sliceAsBytes(phdrs[0..ehdr.phnum]), ehdr.phoff); + for (phdrs[0..ehdr.phnum]) |ph| { + if (ph.type != .DYNAMIC) continue; + var dyn: [64]elf.Elf64_Dyn = undefined; + const len = @min(dyn.len, ph.filesz / @sizeOf(elf.Elf64_Dyn)); + try pread(fd, std.mem.sliceAsBytes(dyn[0..len]), ph.offset); + var versym: ?usize = null; + var end: usize = 0; + while (end < len and dyn[end].d_tag != elf.DT_NULL) : (end += 1) switch (dyn[end].d_tag) { + elf.DT_VERSYM => versym = end, + elf.DT_VERDEF, elf.DT_VERNEED => return null, + else => {}, + }; + const at = versym orelse return null; + if (end == len) return null; + const copy: i32 = @intCast(@as(isize, @bitCast(linux.memfd_create("sketchbook", linux.MFD.CLOEXEC)))); + if (copy < 0) return error.MemfdFailed; + errdefer _ = linux.close(copy); + var offset: i64 = 0; + while (true) { + const rc = linux.sendfile(copy, fd, &offset, 1 << 30); + if (linux.errno(rc) != .SUCCESS) return error.CopyFailed; + if (rc == 0) break; + } + std.mem.copyForwards(elf.Elf64_Dyn, dyn[at..end], dyn[at + 1 .. end + 1]); + const bytes = std.mem.sliceAsBytes(dyn[at..end]); + if (linux.pwrite(copy, bytes.ptr, bytes.len, @intCast(ph.offset + at * @sizeOf(elf.Elf64_Dyn))) != bytes.len) return error.WriteFailed; + return copy; + } + return null; +} + +fn pread(fd: i32, buf: []u8, offset: u64) !void { + if (linux.pread(fd, buf.ptr, buf.len, @intCast(offset)) != buf.len) return error.ShortRead; +} + /// inotify on a library's directory, filtered to its file name. Builds /// replace files by renaming (`MOVED_TO`) or rewrite them (`CLOSE_WRITE`). pub const Watch = struct { |
