From: Kirill A. Korinsky Subject: fix building and linking kernel on sparc64 by clang To: OpenBSD tech Date: Tue, 23 Jun 2026 18:33:05 +0200 tech@, I'd like to commit this diff as separated pieces, but sending it as single one seems the right way to test it. So, here small and nessesary changes to build kernel by clang and link it by ld.lld on sparc64. To make it works you actually need switch sparc64 to clang and lld arch in bsd.own.mk by this: Index: share/mk/bsd.own.mk =================================================================== RCS file: /cvs/src/share/mk/bsd.own.mk,v diff -u -p -r1.216 bsd.own.mk --- share/mk/bsd.own.mk 17 Nov 2025 16:06:09 -0000 1.216 +++ share/mk/bsd.own.mk 23 Jun 2026 12:48:19 -0000 @@ -16,8 +16,8 @@ SKEY?= yes YP?= yes CLANG_ARCH=aarch64 amd64 arm i386 mips64 mips64el powerpc powerpc64 riscv64 sparc64 -GCC4_ARCH=alpha hppa m88k sh sparc64 -LLD_ARCH=aarch64 amd64 arm i386 powerpc powerpc64 riscv64 +GCC4_ARCH=alpha hppa m88k sh +LLD_ARCH=aarch64 amd64 arm i386 powerpc powerpc64 riscv64 sparc64 LLDB_ARCH=aarch64 amd64 clzdi2 is licensed under public domain like other files nearby, but it is quite trivial. Tested by me as clang and gcc based build of kernel, and by tb@ with rebuilding llvm and linking and booting kernel which is built by clang + lld. Ok? Index: gnu/llvm/lld/ELF/Arch/SPARCV9.cpp =================================================================== RCS file: /cvs/src/gnu/llvm/lld/ELF/Arch/SPARCV9.cpp,v diff -u -p -r1.1.1.5 SPARCV9.cpp --- gnu/llvm/lld/ELF/Arch/SPARCV9.cpp 29 May 2026 11:05:55 -0000 1.1.1.5 +++ gnu/llvm/lld/ELF/Arch/SPARCV9.cpp 23 Jun 2026 12:46:21 -0000 @@ -63,6 +63,9 @@ RelExpr SPARCV9::getRelExpr(RelType type case R_SPARC_PC10: case R_SPARC_PC22: case R_SPARC_DISP32: + case R_SPARC_WDISP16: + case R_SPARC_WDISP19: + case R_SPARC_WDISP22: case R_SPARC_WDISP30: return R_PC; case R_SPARC_GOT10: @@ -103,6 +106,11 @@ void SPARCV9::relocate(uint8_t *loc, con checkInt(ctx, loc, val, 32, rel); write32be(loc, (read32be(loc) & ~0x3fffffff) | ((val >> 2) & 0x3fffffff)); break; + case R_SPARC_WDISP22: + // V-disp22 + checkInt(ctx, loc, val, 24, rel); + write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 2) & 0x003fffff)); + break; case R_SPARC_22: // V-imm22 checkUInt(ctx, loc, val, 22, rel); @@ -124,6 +132,15 @@ void SPARCV9::relocate(uint8_t *loc, con checkInt(ctx, loc, val, 21, rel); write32be(loc, (read32be(loc) & ~0x0007ffff) | ((val >> 2) & 0x0007ffff)); break; + case R_SPARC_WDISP16: { + // V-disp16 + checkInt(ctx, loc, val, 18, rel); + uint32_t insn = read32be(loc); + uint32_t d16hi = (val >> 16) & 0x3; + uint32_t d16lo = (val >> 2) & 0x3fff; + write32be(loc, (insn & ~0x00303fff) | (d16hi << 20) | d16lo); + break; + } case R_SPARC_GOT10: case R_SPARC_PC10: // T-simm10 # Can't use ${CLANG_ARCH} ${GCC4_ARCH} below because of sparc64 Index: sys/arch/sparc64/conf/Makefile.sparc64 =================================================================== RCS file: /cvs/src/sys/arch/sparc64/conf/Makefile.sparc64,v diff -u -p -r1.113 Makefile.sparc64 --- sys/arch/sparc64/conf/Makefile.sparc64 5 May 2025 20:43:32 -0000 1.113 +++ sys/arch/sparc64/conf/Makefile.sparc64 23 Jun 2026 12:48:22 -0000 @@ -122,12 +122,16 @@ ioconf.o: ioconf.c ld.script: ${_machdir}/conf/ld.script cp ${_machdir}/conf/ld.script $@ +gapdummy.o: + echo '__asm(".section .rodata,\"a\"");' > gapdummy.c + ${CC} -c ${CFLAGS} ${CPPFLAGS} gapdummy.c -o $@ + makegap.sh: cp $S/conf/makegap.sh $@ -MAKE_GAP = LD="${LD}" sh makegap.sh 0x00000000 +MAKE_GAP = LD="${LD}" sh makegap.sh 0x00000000 gapdummy.o -gap.o: Makefile makegap.sh vers.o +gap.o: Makefile makegap.sh gapdummy.o vers.o ${MAKE_GAP} vers.o: ${SYSTEM_DEP:Ngap.o} @@ -136,7 +140,7 @@ vers.o: ${SYSTEM_DEP:Ngap.o} clean: rm -f *bsd *bsd.gdb *.[dio] [a-z]*.s assym.* \ - gap.link ld.script lorder makegap.sh param.c + gap.link gapdummy.c ld.script lorder makegap.sh param.c cleandir: clean rm -f Makefile *.h ioconf.c options machine ${_mach} vers.c Index: sys/arch/sparc64/conf/files.sparc64 =================================================================== RCS file: /cvs/src/sys/arch/sparc64/conf/files.sparc64,v diff -u -p -r1.156 files.sparc64 --- sys/arch/sparc64/conf/files.sparc64 29 Mar 2024 21:11:31 -0000 1.156 +++ sys/arch/sparc64/conf/files.sparc64 23 Jun 2026 12:48:22 -0000 @@ -340,6 +340,7 @@ file arch/sparc64/sparc64/db_interface.c file arch/sparc64/sparc64/db_trace.c ddb file arch/sparc64/sparc64/db_disasm.c ddb +file lib/libkern/clzdi2.c file lib/libkern/ctzdi2.c # Index: sys/lib/libkern/clzdi2.c =================================================================== RCS file: sys/lib/libkern/clzdi2.c diff -N sys/lib/libkern/clzdi2.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ sys/lib/libkern/clzdi2.c 23 Jun 2026 12:48:30 -0000 @@ -0,0 +1,15 @@ +/* $OpenBSD$ */ + +/* + * Public domain. + */ + +#include + +int +__clzdi2(long mask) +{ + if (mask == 0) + return 0; + return (sizeof(long) * 8 - flsl(mask)); +} -- wbr, Kirill