xref: /freebsd/sys/conf/kern.mk (revision 222c850503d1cbd5293c04987181a74b9dbe3fcf)
16b30bfd3SBruce Evans
26b30bfd3SBruce Evans#
312dd58a3SRebecca Cran# Warning flags for compiling the kernel and components of the kernel:
46b30bfd3SBruce Evans#
54ffeb3b8SJohn BaldwinCWARNFLAGS?=	-Wall -Wstrict-prototypes \
6df66feb8SMatt Macy		-Wmissing-prototypes -Wpointer-arith -Wcast-qual \
754fc0ecaSBrooks Davis		-Wundef -Wno-pointer-sign ${FORMAT_EXTENSIONS} \
82cd8464eSDimitry Andric		-Wmissing-include-dirs -fdiagnostics-show-option \
956b40c28SRichard Scheffenegger		-Wno-unknown-pragmas -Wswitch \
102cd8464eSDimitry Andric		${CWARNEXTRA}
119a6c0bd6SBruce Evans#
129a6c0bd6SBruce Evans# The following flags are next up for working on:
135b44317bSRebecca Cran#	-Wextra
140bcef6efSMatthew Dillon
15a380d07bSDimitry Andric# Disable a few warnings for clang, since there are several places in the
16a380d07bSDimitry Andric# kernel where fixing them is more trouble than it is worth, or where there is
17a380d07bSDimitry Andric# a false positive.
189215d178SBrooks Davis.if ${COMPILER_TYPE} == "clang"
1915565e0aSGreg VNO_WCONSTANT_CONVERSION=	-Wno-error=constant-conversion
20cccc969bSMatt MacyNO_WSHIFT_COUNT_NEGATIVE=	-Wno-shift-count-negative
21cccc969bSMatt MacyNO_WSHIFT_COUNT_OVERFLOW=	-Wno-shift-count-overflow
22cccc969bSMatt MacyNO_WSELF_ASSIGN=		-Wno-self-assign
2315565e0aSGreg VNO_WUNNEEDED_INTERNAL_DECL=	-Wno-error=unneeded-internal-declaration
2415565e0aSGreg VNO_WSOMETIMES_UNINITIALIZED=	-Wno-error=sometimes-uninitialized
2515565e0aSGreg VNO_WCAST_QUAL=			-Wno-error=cast-qual
26cccc969bSMatt MacyNO_WTAUTOLOGICAL_POINTER_COMPARE= -Wno-tautological-pointer-compare
27395d46caSDimitry Andric.if ${COMPILER_VERSION} >= 100000
28395d46caSDimitry AndricNO_WMISLEADING_INDENTATION=	-Wno-misleading-indentation
29395d46caSDimitry Andric.endif
302814ba8eSJohn Baldwin.if ${COMPILER_VERSION} >= 130000
312814ba8eSJohn BaldwinNO_WUNUSED_BUT_SET_VARIABLE=	-Wno-unused-but-set-variable
322814ba8eSJohn Baldwin.endif
335f2aca83SDimitry Andric.if ${COMPILER_VERSION} >= 140000
345f2aca83SDimitry AndricNO_WBITWISE_INSTEAD_OF_LOGICAL=	-Wno-bitwise-instead-of-logical
355f2aca83SDimitry Andric.endif
36e83ffec3SDimitry Andric.if ${COMPILER_VERSION} >= 150000
37e83ffec3SDimitry AndricNO_WSTRICT_PROTOTYPES=		-Wno-strict-prototypes
381a1f7b7dSWarner LoshNO_WDEPRECATED_NON_PROTOTYPE=	-Wno-deprecated-non-prototype
39e83ffec3SDimitry Andric.endif
400ed78d64SDimitry Andric# Several other warnings which might be useful in some cases, but not severe
410ed78d64SDimitry Andric# enough to error out the whole kernel build.  Display them anyway, so there is
420ed78d64SDimitry Andric# some incentive to fix them eventually.
4315565e0aSGreg VCWARNEXTRA?=	-Wno-error=tautological-compare -Wno-error=empty-body \
4415565e0aSGreg V		-Wno-error=parentheses-equality -Wno-error=unused-function \
4515565e0aSGreg V		-Wno-error=pointer-sign
4615565e0aSGreg VCWARNEXTRA+=	-Wno-error=shift-negative-value
47cccc969bSMatt MacyCWARNEXTRA+=	-Wno-address-of-packed-member
48fac6dee9SEric van Gyzen.endif	# clang
49a5b2b29fSBrooks Davis
50bbae23a7SIan Lepore.if ${COMPILER_TYPE} == "gcc"
51f5bb5524SWarner Losh# Catch-all for all the things that are in our tree, but for which we're
5212701462SBryan Drewery# not yet ready for this compiler.
53e5ae3af7SMatt MacyNO_WUNUSED_BUT_SET_VARIABLE=-Wno-unused-but-set-variable
5496c072fcSRuslan BukinCWARNEXTRA?=	-Wno-error=address				\
5596c072fcSRuslan Bukin		-Wno-error=aggressive-loop-optimizations	\
5696c072fcSRuslan Bukin		-Wno-error=array-bounds				\
5796c072fcSRuslan Bukin		-Wno-error=attributes				\
5896c072fcSRuslan Bukin		-Wno-error=cast-qual				\
5996c072fcSRuslan Bukin		-Wno-error=enum-compare				\
6096c072fcSRuslan Bukin		-Wno-error=maybe-uninitialized			\
61fac6dee9SEric van Gyzen		-Wno-error=misleading-indentation		\
62fac6dee9SEric van Gyzen		-Wno-error=nonnull-compare			\
6396c072fcSRuslan Bukin		-Wno-error=overflow				\
6496c072fcSRuslan Bukin		-Wno-error=sequence-point			\
6596c072fcSRuslan Bukin		-Wno-error=shift-overflow			\
66fac6dee9SEric van Gyzen		-Wno-error=tautological-compare			\
673b3762c3SJohn Baldwin		-Wno-error=unused-function
6888eb44d7SRyan Libby.if ${COMPILER_VERSION} >= 70100
6988eb44d7SRyan LibbyCWARNEXTRA+=	-Wno-error=stringop-overflow
7088eb44d7SRyan Libby.endif
718cd6c09eSRuslan Bukin.if ${COMPILER_VERSION} >= 70200
728cd6c09eSRuslan BukinCWARNEXTRA+=	-Wno-error=memset-elt-size
738cd6c09eSRuslan Bukin.endif
74d39c2658SMatt Macy.if ${COMPILER_VERSION} >= 80000
75d39c2658SMatt MacyCWARNEXTRA+=	-Wno-error=packed-not-aligned
76d39c2658SMatt Macy.endif
7777acc3cfSRyan Libby.if ${COMPILER_VERSION} >= 90100
78e72c7e27SJohn BaldwinCWARNEXTRA+=	-Wno-address-of-packed-member			\
79ede077bfSJohn Baldwin		-Wno-alloc-size-larger-than			\
80e72c7e27SJohn Baldwin		-Wno-error=alloca-larger-than=
81488c498bSJohn Baldwin.if ${COMPILER_VERSION} >= 120100
822637ed55SJohn BaldwinCWARNEXTRA+=	-Wno-error=nonnull				\
831aa6d44dSJohn Baldwin		-Wno-dangling-pointer				\
841aa6d44dSJohn Baldwin		-Wno-zero-length-bounds
8532661319SJohn BaldwinNO_WINFINITE_RECURSION=	-Wno-infinite-recursion
862e08e4b7SJohn BaldwinNO_WSTRINGOP_OVERREAD=	-Wno-stringop-overread
87488c498bSJohn Baldwin.endif
8877acc3cfSRyan Libby.endif
892f6a8424SJohn Baldwin
902f6a8424SJohn Baldwin# GCC produces false positives for functions that switch on an
912f6a8424SJohn Baldwin# enum (GCC bug 87950)
922f6a8424SJohn BaldwinCWARNFLAGS+=	-Wno-return-type
93fac6dee9SEric van Gyzen.endif	# gcc
9420256b0bSWarner Losh
95be3ab187SConrad Meyer# This warning is utter nonsense
96be3ab187SConrad MeyerCWARNFLAGS+=	-Wno-format-zero-length
97be3ab187SConrad Meyer
98a5b2b29fSBrooks Davis# External compilers may not support our format extensions.  Allow them
99a5b2b29fSBrooks Davis# to be disabled.  WARNING: format checking is disabled in this case.
100a5b2b29fSBrooks Davis.if ${MK_FORMAT_EXTENSIONS} == "no"
101e94a64deSWarner LoshFORMAT_EXTENSIONS=	-Wno-format
1025115511eSJohn Baldwin.elif ${COMPILER_TYPE} == "clang" || \
1035115511eSJohn Baldwin    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100)
104accc510aSDimitry AndricFORMAT_EXTENSIONS=	-D__printf__=__freebsd_kprintf__
10588f05751SEnji Cooper.else
10654fc0ecaSBrooks DavisFORMAT_EXTENSIONS=	-fformat-extensions
107a380d07bSDimitry Andric.endif
108a380d07bSDimitry Andric
10965be2aacSDoug Rabson#
1105b44317bSRebecca Cran# On i386, do not align the stack to 16-byte boundaries.  Otherwise GCC 2.95
1115b44317bSRebecca Cran# and above adds code to the entry and exit point of every function to align the
11254d477a4SDavid E. O'Brien# stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
11354d477a4SDavid E. O'Brien# per function call.  While the 16-byte alignment may benefit micro benchmarks,
114b453864cSMike Pritchard# it is probably an overall loss as it makes the code bigger (less efficient
11554d477a4SDavid E. O'Brien# use of code cache tag lines) and uses more stack (less efficient use of data
1165b44317bSRebecca Cran# cache tag lines).  Explicitly prohibit the use of FPU, SSE and other SIMD
1178d2046faSPeter Wemm# operations inside the kernel itself.  These operations are exclusively
1188d2046faSPeter Wemm# reserved for user applications.
11954d477a4SDavid E. O'Brien#
120d1b9bdd1SRebecca Cran# gcc:
121d1b9bdd1SRebecca Cran# Setting -mno-mmx implies -mno-3dnow
122d1b9bdd1SRebecca Cran# Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
123d1b9bdd1SRebecca Cran#
124d1b9bdd1SRebecca Cran# clang:
12543917540SDimitry Andric# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
12643917540SDimitry Andric# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
127d1b9bdd1SRebecca Cran#
12858ff0f42SDimitry Andric.if ${MACHINE_CPUARCH} == "i386"
129629c4aeaSRyan LibbyCFLAGS.gcc+=	-mpreferred-stack-boundary=2
13022ba0b2fSWarner LoshCFLAGS.clang+=	-mno-aes -mno-avx
13143917540SDimitry AndricCFLAGS+=	-mno-mmx -mno-sse -msoft-float
1324338438bSDavid E. O'BrienINLINE_LIMIT?=	8000
13354d477a4SDavid E. O'Brien.endif
13454d477a4SDavid E. O'Brien
135c614f4e2SWarner Losh.if ${MACHINE_CPUARCH} == "arm"
136d6301dcdSOlivier HouchardINLINE_LIMIT?=	8000
137d6301dcdSOlivier Houchard.endif
1385b44317bSRebecca Cran
1398598392cSKonstantin Belousov.if ${MACHINE_CPUARCH} == "aarch64"
1408598392cSKonstantin Belousov# We generally don't want fpu instructions in the kernel.
1418598392cSKonstantin BelousovCFLAGS += -mgeneral-regs-only
1428598392cSKonstantin Belousov# Reserve x18 for pcpu data
1438598392cSKonstantin BelousovCFLAGS += -ffixed-x18
144d09a64e1SAndrew Turner# Build with BTI+PAC
145d09a64e1SAndrew TurnerCFLAGS += -mbranch-protection=standard
146973bbdabSJose Luis Duran.if ${LINKER_FEATURES:Mbti-report}
14743e8849bSAndrew TurnerLDFLAGS += -Wl,-zbti-report=error
14843e8849bSAndrew Turner.endif
149361da405SAndrew Turner# TODO: support outline atomics
150361da405SAndrew TurnerCFLAGS += -mno-outline-atomics
151cbc6e751SAndrew TurnerINLINE_LIMIT?=	8000
1528598392cSKonstantin Belousov.endif
1538598392cSKonstantin Belousov
15445b535faSMitchell Horne#
15545b535faSMitchell Horne# For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
156c32b6c74SMitchell Horne# point registers within the kernel. However, we include the F and D extensions
157c32b6c74SMitchell Horne# in -march so we can have limited floating point support in context switching
158c32b6c74SMitchell Horne# code. This is different than userland where we use a hard-float ABI (lp64d).
1596b72948dSWarner Losh#
1606b72948dSWarner Losh# We also specify the "medium" code model, which generates code suitable for a
1616b72948dSWarner Losh# 2GiB addressing range located at any offset, allowing modules to be located
1626b72948dSWarner Losh# anywhere in the 64-bit address space.  Note that clang and GCC refer to this
1636b72948dSWarner Losh# code model as "medium" and "medany" respectively.
16445b535faSMitchell Horne#
16528029b68SRuslan Bukin.if ${MACHINE_CPUARCH} == "riscv"
166d3916eacSRuslan BukinCFLAGS+=	-march=rv64imafdch
1676b72948dSWarner LoshCFLAGS+=	-mabi=lp64
16845b535faSMitchell HorneCFLAGS.clang+=	-mcmodel=medium
16945b535faSMitchell HorneCFLAGS.gcc+=	-mcmodel=medany
17028029b68SRuslan BukinINLINE_LIMIT?=	8000
1710869dcebSJohn Baldwin
1720869dcebSJohn Baldwin.if ${LINKER_FEATURES:Mriscv-relaxations} == ""
1730869dcebSJohn BaldwinCFLAGS+=	-mno-relax
1740869dcebSJohn Baldwin.endif
17528029b68SRuslan Bukin.endif
17628029b68SRuslan Bukin
1770d409bfdSDoug Rabson#
1788d2046faSPeter Wemm# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
1798d2046faSPeter Wemm# operations inside the kernel itself.  These operations are exclusively
1808d2046faSPeter Wemm# reserved for user applications.
181a15febf9SPeter Wemm#
182d1b9bdd1SRebecca Cran# gcc:
183d1b9bdd1SRebecca Cran# Setting -mno-mmx implies -mno-3dnow
184d1b9bdd1SRebecca Cran# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
185d1b9bdd1SRebecca Cran#
186d1b9bdd1SRebecca Cran# clang:
18743917540SDimitry Andric# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
18843917540SDimitry Andric# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
189d1b9bdd1SRebecca Cran# (-mfpmath= is not supported)
190d1b9bdd1SRebecca Cran#
191c614f4e2SWarner Losh.if ${MACHINE_CPUARCH} == "amd64"
19222ba0b2fSWarner LoshCFLAGS.clang+=	-mno-aes -mno-avx
19343917540SDimitry AndricCFLAGS+=	-mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
194d1b9bdd1SRebecca Cran		-fno-asynchronous-unwind-tables
1954e37bb8fSPeter WemmINLINE_LIMIT?=	8000
196a15febf9SPeter Wemm.endif
197a15febf9SPeter Wemm
198a15febf9SPeter Wemm#
199813dd172SDavid E. O'Brien# For PowerPC we tell gcc to use floating point emulation.  This avoids using
200813dd172SDavid E. O'Brien# floating point registers for integer operations which it has a tendency to do.
2015b4975b1SNathan Whitehorn# Also explicitly disable Altivec instructions inside the kernel.
202813dd172SDavid E. O'Brien#
203c614f4e2SWarner Losh.if ${MACHINE_CPUARCH} == "powerpc"
204d9dbd70bSJustin HibbitsCFLAGS+=	-mno-altivec -msoft-float
205813dd172SDavid E. O'BrienINLINE_LIMIT?=	15000
206813dd172SDavid E. O'Brien.endif
207813dd172SDavid E. O'Brien
208dc9b124dSJustin Hibbits.if ${MACHINE_ARCH} == "powerpcspe"
209d9dbd70bSJustin HibbitsCFLAGS.gcc+=	-mno-spe
210dc9b124dSJustin Hibbits.endif
211dc9b124dSJustin Hibbits
212813dd172SDavid E. O'Brien#
2135d851700SNathan Whitehorn# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
2145d851700SNathan Whitehorn# DDB happy. ELFv2, if available, has some other efficiency benefits.
21579c77d72SNathan Whitehorn#
21642140052SDimitry Andric.if ${MACHINE_ARCH:Mpowerpc64*} != "" && \
21742140052SDimitry Andric    ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
218fac6dee9SEric van GyzenCFLAGS+=	-mabi=elfv2
2195d851700SNathan Whitehorn.endif
22079c77d72SNathan Whitehorn
22179c77d72SNathan Whitehorn#
222ea8f3ee3SPeter Wemm# GCC 3.0 and above like to do certain optimizations based on the
223ea8f3ee3SPeter Wemm# assumption that the program is linked against libc.  Stop this.
224ea8f3ee3SPeter Wemm#
225ea8f3ee3SPeter WemmCFLAGS+=	-ffreestanding
226042df2e2SRuslan Ermilov
227042df2e2SRuslan Ermilov#
2285d6adf25SPeter Wemm# The C standard leaves signed integer overflow behavior undefined.
2295d6adf25SPeter Wemm# gcc and clang opimizers take advantage of this.  The kernel makes
2305d6adf25SPeter Wemm# use of signed integer wraparound mechanics so we need the compiler
2315d6adf25SPeter Wemm# to treat it as a wraparound and not take shortcuts.
2325d6adf25SPeter Wemm#
2335d6adf25SPeter WemmCFLAGS+=	-fwrapv
2345d6adf25SPeter Wemm
2355d6adf25SPeter Wemm#
236*222c8505SEd Maste# Stack Smashing Protection (SSP) support
237042df2e2SRuslan Ermilov#
2381b8db4b4SMitchell Horne.if ${MK_SSP} != "no"
239042df2e2SRuslan ErmilovCFLAGS+=	-fstack-protector
240e9093b66SEd Maste.endif
241e9093b66SEd Maste
242e9093b66SEd Maste#
243e9093b66SEd Maste# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
244e9093b66SEd Maste#
245e9093b66SEd Maste.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
246e9093b66SEd Maste    ${MK_KERNEL_RETPOLINE} != "no"
247e9093b66SEd MasteCFLAGS+=	-mretpoline
248042df2e2SRuslan Ermilov.endif
2493ad1a091SWarner Losh
2503ad1a091SWarner Losh#
2516e3875ebSKa Ho Ng# Kernel Address SANitizer support
2526e3875ebSKa Ho Ng#
2536e3875ebSKa Ho Ng.if !empty(KASAN_ENABLED)
2546e3875ebSKa Ho NgSAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kasan \
2556e3875ebSKa Ho Ng		-fsanitize=kernel-address
2566e3875ebSKa Ho Ng.if ${COMPILER_TYPE} == "clang"
2576e3875ebSKa Ho NgSAN_CFLAGS+=	-mllvm -asan-stack=true \
2586e3875ebSKa Ho Ng		-mllvm -asan-instrument-dynamic-allocas=true \
2596e3875ebSKa Ho Ng		-mllvm -asan-globals=true \
2606e3875ebSKa Ho Ng		-mllvm -asan-use-after-scope=true \
2616e3875ebSKa Ho Ng		-mllvm -asan-instrumentation-with-call-threshold=0 \
2626e3875ebSKa Ho Ng		-mllvm -asan-instrument-byval=false
2636e3875ebSKa Ho Ng.endif
2646e3875ebSKa Ho Ng
2656e3875ebSKa Ho Ng.if ${MACHINE_CPUARCH} == "aarch64"
2666e3875ebSKa Ho Ng# KASAN/ARM64 TODO: -asan-mapping-offset is calculated from:
2676e3875ebSKa Ho Ng#	   (VM_KERNEL_MIN_ADDRESS >> KASAN_SHADOW_SCALE_SHIFT) + $offset = KASAN_MIN_ADDRESS
2686e3875ebSKa Ho Ng#
2696e3875ebSKa Ho Ng#	This is different than amd64, where we have a different
2706e3875ebSKa Ho Ng#	KASAN_MIN_ADDRESS, and this offset value should eventually be
2716e3875ebSKa Ho Ng#	upstreamed similar to: https://reviews.llvm.org/D98285
2726e3875ebSKa Ho Ng#
2736e3875ebSKa Ho Ng.if ${COMPILER_TYPE} == "clang"
2746e3875ebSKa Ho NgSAN_CFLAGS+=	-mllvm -asan-mapping-offset=0xdfff208000000000
2756e3875ebSKa Ho Ng.else
2766e3875ebSKa Ho NgSAN_CFLAGS+=	-fasan-shadow-offset=0xdfff208000000000
2776e3875ebSKa Ho Ng.endif
2786e3875ebSKa Ho Ng.elif ${MACHINE_CPUARCH} == "amd64" && \
2796e3875ebSKa Ho Ng      ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 180000
2806e3875ebSKa Ho Ng# Work around https://github.com/llvm/llvm-project/issues/87923, which leads to
2816e3875ebSKa Ho Ng# an assertion failure compiling dtrace.c with asan enabled.
2826e3875ebSKa Ho NgSAN_CFLAGS+=	-mllvm -asan-use-stack-safety=0
2836e3875ebSKa Ho Ng.endif
2846e3875ebSKa Ho Ng.endif # !empty(KASAN_ENABLED)
2856e3875ebSKa Ho Ng
2866e3875ebSKa Ho Ng#
2876e3875ebSKa Ho Ng# Kernel Concurrency SANitizer support
2886e3875ebSKa Ho Ng#
2896e3875ebSKa Ho Ng.if !empty(KCSAN_ENABLED)
2906e3875ebSKa Ho NgSAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kcsan \
2916e3875ebSKa Ho Ng		-fsanitize=thread
2926e3875ebSKa Ho Ng.endif
2936e3875ebSKa Ho Ng
2946e3875ebSKa Ho Ng#
2956e3875ebSKa Ho Ng# Kernel Memory SANitizer support
2966e3875ebSKa Ho Ng#
2976e3875ebSKa Ho Ng.if !empty(KMSAN_ENABLED)
2986e3875ebSKa Ho Ng# Disable -fno-sanitize-memory-param-retval until interceptors have been
2996e3875ebSKa Ho Ng# updated to work properly with it.
3006e3875ebSKa Ho NgMSAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kmsan \
3016e3875ebSKa Ho Ng		-fsanitize=kernel-memory
3026e3875ebSKa Ho Ng.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 160000
3036e3875ebSKa Ho NgMSAN_CFLAGS+=	-fno-sanitize-memory-param-retval
3046e3875ebSKa Ho Ng.endif
3056e3875ebSKa Ho NgSAN_CFLAGS+=	${MSAN_CFLAGS}
3066e3875ebSKa Ho Ng.endif # !empty(KMSAN_ENABLED)
3076e3875ebSKa Ho Ng
3086e3875ebSKa Ho Ng#
3096e3875ebSKa Ho Ng# Kernel Undefined Behavior SANitizer support
3106e3875ebSKa Ho Ng#
3116e3875ebSKa Ho Ng.if !empty(KUBSAN_ENABLED)
3126e3875ebSKa Ho NgSAN_CFLAGS+=	-fsanitize=undefined
3136e3875ebSKa Ho Ng.endif
3146e3875ebSKa Ho Ng
3156e3875ebSKa Ho Ng#
3166e3875ebSKa Ho Ng# Generic Kernel Coverage support
3176e3875ebSKa Ho Ng#
3186e3875ebSKa Ho Ng.if !empty(COVERAGE_ENABLED)
3196e3875ebSKa Ho Ng.if ${COMPILER_TYPE} == "clang" || \
3206e3875ebSKa Ho Ng    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 80100)
3216e3875ebSKa Ho NgSAN_CFLAGS+=	-fsanitize-coverage=trace-pc,trace-cmp
3226e3875ebSKa Ho Ng.else
3236e3875ebSKa Ho NgSAN_CFLAGS+=	-fsanitize-coverage=trace-pc
3246e3875ebSKa Ho Ng.endif
3256e3875ebSKa Ho Ng.endif # !empty(COVERAGE_ENABLED)
3266e3875ebSKa Ho Ng
3276e3875ebSKa Ho Ng# Add the sanitizer C flags
3286e3875ebSKa Ho NgCFLAGS+=	${SAN_CFLAGS}
3296e3875ebSKa Ho Ng
3306e3875ebSKa Ho Ng#
331e268fd0aSBrooks Davis# Initialize stack variables on function entry
332e268fd0aSBrooks Davis#
3332befa269SBrooks Davis.if ${OPT_INIT_ALL} != "none"
33426676c47SWarner Losh.if ${COMPILER_FEATURES:Minit-all}
3352befa269SBrooks DavisCFLAGS+= -ftrivial-auto-var-init=${OPT_INIT_ALL}
3362befa269SBrooks Davis.if ${OPT_INIT_ALL} == "zero" && ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
3373006f6dfSDimitry AndricCFLAGS+= -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
3383006f6dfSDimitry Andric.endif
339e268fd0aSBrooks Davis.else
3402befa269SBrooks Davis.warning INIT_ALL (${OPT_INIT_ALL}) requested but not supported by compiler
341e268fd0aSBrooks Davis.endif
342e268fd0aSBrooks Davis.endif
343e268fd0aSBrooks Davis
34453fba3b9SMark Johnston#
34553fba3b9SMark Johnston# Some newer toolchains default to DWARF 5, which isn't supported by some build
34653fba3b9SMark Johnston# tools yet.
34753fba3b9SMark Johnston#
34853fba3b9SMark Johnston.if (${CFLAGS:M-g} != "" || ${CFLAGS:M-g[0-3]} != "") && ${CFLAGS:M-gdwarf*} == ""
34953fba3b9SMark JohnstonCFLAGS+=	-gdwarf-4
35053fba3b9SMark Johnston.endif
35153fba3b9SMark Johnston
3529346408dSRuslan BukinCFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
353b16a3c9dSKyle EvansCFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
354814e92e9SWarner LoshCFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
3557ed11c5eSWarner Losh
3567ed11c5eSWarner Losh# Tell bmake not to mistake standard targets for things to be searched for
3577ed11c5eSWarner Losh# or expect to ever be up-to-date.
3587ed11c5eSWarner LoshPHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
3597ed11c5eSWarner Losh		beforelinking build build-tools buildfiles buildincludes \
3607ed11c5eSWarner Losh		checkdpadd clean cleandepend cleandir cleanobj configure \
3614e47b95cSBryan Drewery		depend distclean distribute exe \
3621cbb5888SWarner Losh		html includes install installfiles installincludes \
3634e47b95cSBryan Drewery		obj objlink objs objwarn \
3644e47b95cSBryan Drewery		realinstall regress \
3657ed11c5eSWarner Losh		tags whereobj
3667ed11c5eSWarner Losh
3677ed11c5eSWarner Losh.PHONY: ${PHONY_NOTMAIN}
3687ed11c5eSWarner Losh.NOTMAIN: ${PHONY_NOTMAIN}
369faa5d16dSWarner Losh
3703a98d570SMinsoo ChooCSTD?=		gnu17
371faa5d16dSWarner Losh
372079d67b1SMinsoo Choo# c99/gnu99 is the minimum C standard version supported for kernel build
373079d67b1SMinsoo Choo.if ${CSTD} == "k&r" || ${CSTD} == "c89" || ${CSTD} == "c90" || \
374079d67b1SMinsoo Choo    ${CSTD} == "c94" || ${CSTD} == "c95"
375079d67b1SMinsoo Choo.error "Only c99/gnu99 or later is supported"
376faa5d16dSWarner Losh.else # CSTD
377faa5d16dSWarner LoshCFLAGS+=        -std=${CSTD}
378faa5d16dSWarner Losh.endif # CSTD
3791dd50172SEd Maste
38012a6257aSAndrew TurnerNOSAN_CFLAGS= ${CFLAGS:N-fsanitize*:N-fno-sanitize*:N-fasan-shadow-offset*}
38112a6257aSAndrew Turner
3822b6ee34cSAlex Richardson# Please keep this if in sync with bsd.sys.mk
3832b6ee34cSAlex Richardson.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
3842b6ee34cSAlex Richardson# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
3852b6ee34cSAlex Richardson.if ${COMPILER_TYPE} == "clang"
386172a624fSAlex Richardson# Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
387172a624fSAlex Richardson.if ${COMPILER_VERSION} >= 120000
38801fe4cacSAlex RichardsonCCLDFLAGS+=	--ld-path=${LD:[1]:S/^ld.//1W}
389172a624fSAlex Richardson.else
39001fe4cacSAlex RichardsonCCLDFLAGS+=	-fuse-ld=${LD:[1]:S/^ld.//1W}
391172a624fSAlex Richardson.endif
3922b6ee34cSAlex Richardson.else
3932b6ee34cSAlex Richardson# GCC does not support an absolute path for -fuse-ld so we just print this
3942b6ee34cSAlex Richardson# warning instead and let the user add the required symlinks.
395172a624fSAlex Richardson# However, we can avoid this warning if -B is set appropriately (e.g. for
396172a624fSAlex Richardson# CROSS_TOOLCHAIN=...-gcc).
397172a624fSAlex Richardson.if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
3982b6ee34cSAlex Richardson.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
3992b6ee34cSAlex Richardson.endif
4002b6ee34cSAlex Richardson.endif
401172a624fSAlex Richardson.endif
4022b6ee34cSAlex Richardson
403dc653882SJohn Baldwin# Set target-specific linker emulation name.
4041dd50172SEd MasteLD_EMULATION_aarch64=aarch64elf
4051dd50172SEd MasteLD_EMULATION_amd64=elf_x86_64_fbsd
4061dd50172SEd MasteLD_EMULATION_arm=armelf_fbsd
4070b972ac9SWarner LoshLD_EMULATION_armv7=armelf_fbsd
4081dd50172SEd MasteLD_EMULATION_i386=elf_i386_fbsd
4091dd50172SEd MasteLD_EMULATION_powerpc= elf32ppc_fbsd
410dc9b124dSJustin HibbitsLD_EMULATION_powerpcspe= elf32ppc_fbsd
4111dd50172SEd MasteLD_EMULATION_powerpc64= elf64ppc_fbsd
412b75abea4SBrandon BergrenLD_EMULATION_powerpc64le= elf64lppc_fbsd
413fd46810aSJohn BaldwinLD_EMULATION_riscv64= elf64lriscv
4141dd50172SEd MasteLD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}
415