git: 4f2e5793d753 - stable/13 - Fix build of stand/ when building world with ASAN

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 24 Jan 2023 22:09:52 UTC
The branch stable/13 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=4f2e5793d753cdd2e5625fc292a9d8e1ab4dfa39

commit 4f2e5793d753cdd2e5625fc292a9d8e1ab4dfa39
Author:     Alex Richardson <arichardson@FreeBSD.org>
AuthorDate: 2021-08-02 08:50:16 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-24 21:49:17 +0000

    Fix build of stand/ when building world with ASAN
    
    The userboot/test  program links against the default userspace libraries
    (e.g. shared libgcc_s.so) that will be instrumented if WITH_ASAN is set.
    All other programs link against libsa instead of libc and therefore can't
    use the sanitizer runtime library. To fix the stand/ build with
    sanitizers, we disable MK_ASAN/MK_UBSAN if -nostdlib is found in the
    LDFLAGS (i.e. we are using libsa instead of libc).
    
    Reviewed By:    imp, tsoome
    Differential Revision: https://reviews.freebsd.org/D31047
    
    (cherry picked from commit 5e9226f063bc43574e2168b4aafad8232f08002a)
---
 stand/defs.mk                    | 6 ++++++
 stand/userboot/userboot/Makefile | 5 +++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/stand/defs.mk b/stand/defs.mk
index bbebf270c7ef..765fd046a879 100644
--- a/stand/defs.mk
+++ b/stand/defs.mk
@@ -25,6 +25,12 @@ INTERNALLIB=
 # enough to make that hassle worth chasing.
 _CPUCFLAGS=
 
+.if ${LDFLAGS:M-nostdlib}
+# Sanitizers won't work unless we link against libc (e.g. in userboot/test).
+MK_ASAN:=	no
+MK_UBSAN:=	no
+.endif
+
 .include <src.opts.mk>
 .include <bsd.linker.mk>
 
diff --git a/stand/userboot/userboot/Makefile b/stand/userboot/userboot/Makefile
index 767840101200..60fcf57d9697 100644
--- a/stand/userboot/userboot/Makefile
+++ b/stand/userboot/userboot/Makefile
@@ -6,6 +6,9 @@ LOADER_CD9660_SUPPORT?=	no
 LOADER_EXT2FS_SUPPORT?=	no
 PIC=yes
 
+# Note: -nostdlib needs to be added to LDFLAGS before including defs.mk
+LDFLAGS+=	-nostdlib -Wl,-Bsymbolic
+
 .include <bsd.init.mk>
 
 SHLIB_NAME=	userboot_${LOADER_INTERP}.so
@@ -39,8 +42,6 @@ CFLAGS.main.c+=	-I${SYSDIR}/contrib/openzfs/include/os/freebsd/zfs
 CFLAGS.gfx_fb.c+= -I$(SRCTOP)/sys/teken -I${SRCTOP}/contrib/pnglite
 CWARNFLAGS.main.c += -Wno-implicit-function-declaration
 
-LDFLAGS+=	-nostdlib -Wl,-Bsymbolic
-
 NEWVERSWHAT=	"User boot ${LOADER_INTERP}" ${MACHINE_CPUARCH}
 VERSION_FILE=	${.CURDIR}/../userboot/version