Support dynamically linked binaries on OpenBSD. (#350814)

This commit is contained in:
John Ericson 2024-10-24 09:03:12 -04:00 committed by GitHub
commit f2cf76575c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 106 additions and 7 deletions

View File

@ -134,6 +134,7 @@ let
else if targetPlatform.isLoongArch64 then "${sharedLibraryLoader}/lib/ld-linux-loongarch*.so.1"
else if targetPlatform.isDarwin then "/usr/lib/dyld"
else if targetPlatform.isFreeBSD then "${sharedLibraryLoader}/libexec/ld-elf.so.1"
else if targetPlatform.isOpenBSD then "${sharedLibraryLoader}/libexec/ld.so"
else if hasSuffix "pc-gnu" targetPlatform.config then "ld.so.1"
else "";

View File

@ -1,11 +1,13 @@
{
lib,
stdenvNoLibc,
symlinkJoin,
libcMinimal,
librthread,
libm,
librpcsvc,
libutil,
rtld,
version,
}:
@ -27,13 +29,16 @@ symlinkJoin rec {
(lib.getLib p)
(lib.getMan p)
])
[
libcMinimal
libm
librthread
librpcsvc
libutil
];
(
[
libcMinimal
libm
librthread
librpcsvc
libutil
]
++ (lib.optional (!stdenvNoLibc.hostPlatform.isStatic) rtld)
);
postBuild = ''
rm -r "$out/nix-support"

View File

@ -1,6 +1,11 @@
addOpenBSDMakeFlags() {
prependToVar makeFlags "INCSDIR=${!outputDev}/include"
prependToVar makeFlags "MANDIR=${!outputMan}/share/man"
# Variables are used to declare dependencies, but we handle them with cc-wrapper
prependToVar makeFlags "CRTBEGIN="
prependToVar makeFlags "CRTEND="
prependToVar makeFlags "LIBCRT0="
prependToVar makeFlags "LIBC="
}
fixOpenBSDInstallDirs() {

View File

@ -0,0 +1,57 @@
diff --git a/libexec/ld.so/Makefile b/libexec/ld.so/Makefile
index 7f8f6ef2961..469c34bb6de 100644
--- a/libexec/ld.so/Makefile
+++ b/libexec/ld.so/Makefile
@@ -1,6 +1,5 @@
# $OpenBSD: Makefile,v 1.88 2024/04/05 13:51:47 deraadt Exp $
-SUBDIR=ldconfig ldd
MAN= ld.so.1
.include <bsd.own.mk>
@@ -70,21 +69,15 @@ ELF_LDFLAGS+=--shared -Bsymbolic --no-undefined
.ifdef RELATIVE_RELOC
CHECK_LDSO=c() { \
- ! readelf -Wr $$1 | \
+ ! $(READELF) -Wr $$1 | \
egrep -qv '^($$|[ R])| (${RELATIVE_RELOC}) '; \
}; c
.endif
-test_prog= test-$(PROG)
-CLEANFILES+= test-$(PROG)
candidate= $(PROG).test
CLEANFILES+= ${candidate}
-$(test_prog):
- printf '#include <stdio.h>\n#include <pthread.h>\nint main(int argc, char **argv){ pthread_attr_t attr; printf("%%s: ", argv[0]); pthread_attr_init(&attr); printf("%%s!\\n", argv[1] ? argv[1] : "foo"); }\n' | \
- $(CC) -P -x c - -Wl,-dynamic-linker,./$(candidate) -o $@ -lpthread
-
-$(PROG): $(test_prog) ${VERSION_SCRIPT} $(OBJS) ${LD_SCRIPT}
+$(PROG): ${VERSION_SCRIPT} $(OBJS) ${LD_SCRIPT}
.if defined(SYSPATCH_PATH)
$(LD) -e _dl_start $(ELF_LDFLAGS) -o $(candidate) \
`readelf -Ws ${SYSPATCH_PATH}/usr/libexec/${.TARGET} | \
@@ -96,9 +89,6 @@ $(PROG): $(test_prog) ${VERSION_SCRIPT} $(OBJS) ${LD_SCRIPT}
.endif
.ifdef CHECK_LDSO
${CHECK_LDSO} $(candidate)
-.endif
-.ifndef CROSSDIR
- ulimit -c 0; [ "`${.OBJDIR}/$(test_prog) ok`" = "${.OBJDIR}/$(test_prog): ok!" ]
.endif
cp $(candidate) $@
.endif
@@ -113,10 +103,4 @@ CLEANFILES+= ld.so.a
all: ld.so.a
ld.so.a: ${OBJS} ${.CURDIR}/Symbols.map ${test_prog} ${LD_SCRIPT}
- ar cqD $@ $?
-
-afterinstall: ld.so.a
- install -d -o root -g wheel -m 755 \
- ${DESTDIR}/usr/share/relink/usr/libexec
- install -o ${BINOWN} -g ${BINGRP} -m ${NONBINMODE} \
- ld.so.a ${DESTDIR}/usr/share/relink/usr/libexec/ld.so.a
+ $(AR) cqD $@ $?

View File

@ -0,0 +1,31 @@
{
lib,
mkDerivation,
}:
mkDerivation {
path = "libexec/ld.so";
extraPaths = [
"lib/libc/string"
"lib/csu/os-note-elf.h"
];
patches = [
./ldso-fix-makefile.patch
];
libcMinimal = true;
NIX_CFLAGS_COMPILE = "-Wno-error";
# DESTDIR is overridden in bsdSetupHook, just fixup afterwards
postInstall = ''
mv $out/bin $out/libexec
'';
outputs = [
"out"
"man"
];
meta.platforms = lib.platforms.openbsd;
}