From 4c2f1e01a7911f9f1d793156459d6df6c76edf68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sat, 10 May 2025 17:52:35 +0200 Subject: [PATCH] freebsd: Create strong references to __progname and environ in stub libc.so. These symbols are defined in the statically-linked startup code. The real libc.so.7 contains strong references to them, so they need to be put into the dynamic symbol table. --- src/libs/freebsd.zig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/libs/freebsd.zig b/src/libs/freebsd.zig index 9aa4066649..c7365ede4a 100644 --- a/src/libs/freebsd.zig +++ b/src/libs/freebsd.zig @@ -668,6 +668,25 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye try stubs_writer.writeAll(".data\n"); + // FreeBSD's `libc.so.7` contains strong references to `__progname` and `environ` which are + // defined in the statically-linked startup code. Those references cause the linker to put + // the symbols in the dynamic symbol table. We need to create dummy references to them here + // to get the same effect. + if (std.mem.eql(u8, lib.name, "c")) { + try stubs_writer.print( + \\.balign {d} + \\.globl __progname + \\.globl environ + \\{s} __progname + \\{s} environ + \\ + , .{ + target.ptrBitWidth() / 8, + wordDirective(target), + wordDirective(target), + }); + } + const obj_inclusions_len = try inc_reader.readInt(u16, .little); var sizes = try arena.alloc(u16, metadata.all_versions.len);