1 2# 3# Warning flags for compiling the kernel and components of the kernel: 4# 5CWARNFLAGS?= -Wall -Wstrict-prototypes \ 6 -Wmissing-prototypes -Wpointer-arith -Wcast-qual \ 7 -Wundef -Wno-pointer-sign ${FORMAT_EXTENSIONS} \ 8 -Wmissing-include-dirs -fdiagnostics-show-option \ 9 -Wno-unknown-pragmas -Wswitch \ 10 ${CWARNEXTRA} 11# 12# The following flags are next up for working on: 13# -Wextra 14 15# Disable a few warnings for clang, since there are several places in the 16# kernel where fixing them is more trouble than it is worth, or where there is 17# a false positive. 18.if ${COMPILER_TYPE} == "clang" 19NO_WCONSTANT_CONVERSION= -Wno-error=constant-conversion 20NO_WSHIFT_COUNT_NEGATIVE= -Wno-shift-count-negative 21NO_WSHIFT_COUNT_OVERFLOW= -Wno-shift-count-overflow 22NO_WSELF_ASSIGN= -Wno-self-assign 23NO_WUNNEEDED_INTERNAL_DECL= -Wno-error=unneeded-internal-declaration 24NO_WSOMETIMES_UNINITIALIZED= -Wno-error=sometimes-uninitialized 25NO_WCAST_QUAL= -Wno-error=cast-qual 26NO_WTAUTOLOGICAL_POINTER_COMPARE= -Wno-tautological-pointer-compare 27.if ${COMPILER_VERSION} >= 100000 28NO_WMISLEADING_INDENTATION= -Wno-misleading-indentation 29.endif 30.if ${COMPILER_VERSION} >= 130000 31NO_WUNUSED_BUT_SET_VARIABLE= -Wno-unused-but-set-variable 32.endif 33.if ${COMPILER_VERSION} >= 140000 34NO_WBITWISE_INSTEAD_OF_LOGICAL= -Wno-bitwise-instead-of-logical 35.endif 36.if ${COMPILER_VERSION} >= 150000 37NO_WSTRICT_PROTOTYPES= -Wno-strict-prototypes 38NO_WDEPRECATED_NON_PROTOTYPE= -Wno-deprecated-non-prototype 39.endif 40# Several other warnings which might be useful in some cases, but not severe 41# enough to error out the whole kernel build. Display them anyway, so there is 42# some incentive to fix them eventually. 43CWARNEXTRA?= -Wno-error=tautological-compare -Wno-error=empty-body \ 44 -Wno-error=parentheses-equality -Wno-error=unused-function \ 45 -Wno-error=pointer-sign 46CWARNEXTRA+= -Wno-error=shift-negative-value 47CWARNEXTRA+= -Wno-address-of-packed-member 48.endif # clang 49 50.if ${COMPILER_TYPE} == "gcc" 51# Catch-all for all the things that are in our tree, but for which we're 52# not yet ready for this compiler. 53NO_WUNUSED_BUT_SET_VARIABLE=-Wno-unused-but-set-variable 54CWARNEXTRA?= -Wno-error=address \ 55 -Wno-error=aggressive-loop-optimizations \ 56 -Wno-error=array-bounds \ 57 -Wno-error=attributes \ 58 -Wno-error=cast-qual \ 59 -Wno-error=enum-compare \ 60 -Wno-error=maybe-uninitialized \ 61 -Wno-error=misleading-indentation \ 62 -Wno-error=nonnull-compare \ 63 -Wno-error=overflow \ 64 -Wno-error=sequence-point \ 65 -Wno-error=shift-overflow \ 66 -Wno-error=tautological-compare \ 67 -Wno-error=unused-function 68.if ${COMPILER_VERSION} >= 70100 69CWARNEXTRA+= -Wno-error=stringop-overflow 70.endif 71.if ${COMPILER_VERSION} >= 70200 72CWARNEXTRA+= -Wno-error=memset-elt-size 73.endif 74.if ${COMPILER_VERSION} >= 80000 75CWARNEXTRA+= -Wno-error=packed-not-aligned 76.endif 77.if ${COMPILER_VERSION} >= 90100 78CWARNEXTRA+= -Wno-address-of-packed-member \ 79 -Wno-alloc-size-larger-than \ 80 -Wno-error=alloca-larger-than= 81.if ${COMPILER_VERSION} >= 120100 82CWARNEXTRA+= -Wno-error=nonnull \ 83 -Wno-dangling-pointer \ 84 -Wno-zero-length-bounds 85NO_WINFINITE_RECURSION= -Wno-infinite-recursion 86NO_WSTRINGOP_OVERREAD= -Wno-stringop-overread 87.endif 88.endif 89 90# GCC produces false positives for functions that switch on an 91# enum (GCC bug 87950) 92CWARNFLAGS+= -Wno-return-type 93.endif # gcc 94 95# This warning is utter nonsense 96CWARNFLAGS+= -Wno-format-zero-length 97 98# External compilers may not support our format extensions. Allow them 99# to be disabled. WARNING: format checking is disabled in this case. 100.if ${MK_FORMAT_EXTENSIONS} == "no" 101FORMAT_EXTENSIONS= -Wno-format 102.elif ${COMPILER_TYPE} == "clang" || \ 103 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100) 104FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__ 105.else 106FORMAT_EXTENSIONS= -fformat-extensions 107.endif 108 109# 110# On i386, do not align the stack to 16-byte boundaries. Otherwise GCC 2.95 111# and above adds code to the entry and exit point of every function to align the 112# stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack 113# per function call. While the 16-byte alignment may benefit micro benchmarks, 114# it is probably an overall loss as it makes the code bigger (less efficient 115# use of code cache tag lines) and uses more stack (less efficient use of data 116# cache tag lines). Explicitly prohibit the use of FPU, SSE and other SIMD 117# operations inside the kernel itself. These operations are exclusively 118# reserved for user applications. 119# 120# gcc: 121# Setting -mno-mmx implies -mno-3dnow 122# Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3 123# 124# clang: 125# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa 126# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42 127# 128.if ${MACHINE_CPUARCH} == "i386" 129CFLAGS.gcc+= -mpreferred-stack-boundary=2 130CFLAGS.clang+= -mno-aes -mno-avx 131CFLAGS+= -mno-mmx -mno-sse -msoft-float 132INLINE_LIMIT?= 8000 133.endif 134 135.if ${MACHINE_CPUARCH} == "arm" 136INLINE_LIMIT?= 8000 137.endif 138 139.if ${MACHINE_CPUARCH} == "aarch64" 140# We generally don't want fpu instructions in the kernel. 141CFLAGS += -mgeneral-regs-only 142# Reserve x18 for pcpu data 143CFLAGS += -ffixed-x18 144# Build with BTI+PAC 145CFLAGS += -mbranch-protection=standard 146.if ${LINKER_FEATURES:Mbti-report} 147LDFLAGS += -Wl,-zbti-report=error 148.endif 149# TODO: support outline atomics 150CFLAGS += -mno-outline-atomics 151INLINE_LIMIT?= 8000 152.endif 153 154# 155# For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating 156# point registers within the kernel. However, we include the F and D extensions 157# in -march so we can have limited floating point support in context switching 158# code. This is different than userland where we use a hard-float ABI (lp64d). 159# 160# We also specify the "medium" code model, which generates code suitable for a 161# 2GiB addressing range located at any offset, allowing modules to be located 162# anywhere in the 64-bit address space. Note that clang and GCC refer to this 163# code model as "medium" and "medany" respectively. 164# 165.if ${MACHINE_CPUARCH} == "riscv" 166CFLAGS+= -march=rv64imafdch 167CFLAGS+= -mabi=lp64 168CFLAGS.clang+= -mcmodel=medium 169CFLAGS.gcc+= -mcmodel=medany 170INLINE_LIMIT?= 8000 171 172.if ${LINKER_FEATURES:Mriscv-relaxations} == "" 173CFLAGS+= -mno-relax 174.endif 175.endif 176 177# 178# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD 179# operations inside the kernel itself. These operations are exclusively 180# reserved for user applications. 181# 182# gcc: 183# Setting -mno-mmx implies -mno-3dnow 184# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387 185# 186# clang: 187# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa 188# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42 189# (-mfpmath= is not supported) 190# 191.if ${MACHINE_CPUARCH} == "amd64" 192CFLAGS.clang+= -mno-aes -mno-avx 193CFLAGS+= -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \ 194 -fno-asynchronous-unwind-tables 195INLINE_LIMIT?= 8000 196.endif 197 198# 199# For PowerPC we tell gcc to use floating point emulation. This avoids using 200# floating point registers for integer operations which it has a tendency to do. 201# Also explicitly disable Altivec instructions inside the kernel. 202# 203.if ${MACHINE_CPUARCH} == "powerpc" 204CFLAGS+= -mno-altivec -msoft-float 205INLINE_LIMIT?= 15000 206.endif 207 208.if ${MACHINE_ARCH} == "powerpcspe" 209CFLAGS.gcc+= -mno-spe 210.endif 211 212# 213# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make 214# DDB happy. ELFv2, if available, has some other efficiency benefits. 215# 216.if ${MACHINE_ARCH:Mpowerpc64*} != "" && \ 217 ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000 218CFLAGS+= -mabi=elfv2 219.endif 220 221# 222# GCC 3.0 and above like to do certain optimizations based on the 223# assumption that the program is linked against libc. Stop this. 224# 225CFLAGS+= -ffreestanding 226 227# 228# The C standard leaves signed integer overflow behavior undefined. 229# gcc and clang opimizers take advantage of this. The kernel makes 230# use of signed integer wraparound mechanics so we need the compiler 231# to treat it as a wraparound and not take shortcuts. 232# 233CFLAGS+= -fwrapv 234 235# 236# GCC SSP support 237# 238.if ${MK_SSP} != "no" 239CFLAGS+= -fstack-protector 240.endif 241 242# 243# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715) 244# 245.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \ 246 ${MK_KERNEL_RETPOLINE} != "no" 247CFLAGS+= -mretpoline 248.endif 249 250# 251# Kernel Address SANitizer support 252# 253.if !empty(KASAN_ENABLED) 254SAN_CFLAGS+= -DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kasan \ 255 -fsanitize=kernel-address 256.if ${COMPILER_TYPE} == "clang" 257SAN_CFLAGS+= -mllvm -asan-stack=true \ 258 -mllvm -asan-instrument-dynamic-allocas=true \ 259 -mllvm -asan-globals=true \ 260 -mllvm -asan-use-after-scope=true \ 261 -mllvm -asan-instrumentation-with-call-threshold=0 \ 262 -mllvm -asan-instrument-byval=false 263.endif 264 265.if ${MACHINE_CPUARCH} == "aarch64" 266# KASAN/ARM64 TODO: -asan-mapping-offset is calculated from: 267# (VM_KERNEL_MIN_ADDRESS >> KASAN_SHADOW_SCALE_SHIFT) + $offset = KASAN_MIN_ADDRESS 268# 269# This is different than amd64, where we have a different 270# KASAN_MIN_ADDRESS, and this offset value should eventually be 271# upstreamed similar to: https://reviews.llvm.org/D98285 272# 273.if ${COMPILER_TYPE} == "clang" 274SAN_CFLAGS+= -mllvm -asan-mapping-offset=0xdfff208000000000 275.else 276SAN_CFLAGS+= -fasan-shadow-offset=0xdfff208000000000 277.endif 278.elif ${MACHINE_CPUARCH} == "amd64" && \ 279 ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 180000 280# Work around https://github.com/llvm/llvm-project/issues/87923, which leads to 281# an assertion failure compiling dtrace.c with asan enabled. 282SAN_CFLAGS+= -mllvm -asan-use-stack-safety=0 283.endif 284.endif # !empty(KASAN_ENABLED) 285 286# 287# Kernel Concurrency SANitizer support 288# 289.if !empty(KCSAN_ENABLED) 290SAN_CFLAGS+= -DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kcsan \ 291 -fsanitize=thread 292.endif 293 294# 295# Kernel Memory SANitizer support 296# 297.if !empty(KMSAN_ENABLED) 298# Disable -fno-sanitize-memory-param-retval until interceptors have been 299# updated to work properly with it. 300MSAN_CFLAGS+= -DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kmsan \ 301 -fsanitize=kernel-memory 302.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 160000 303MSAN_CFLAGS+= -fno-sanitize-memory-param-retval 304.endif 305SAN_CFLAGS+= ${MSAN_CFLAGS} 306.endif # !empty(KMSAN_ENABLED) 307 308# 309# Kernel Undefined Behavior SANitizer support 310# 311.if !empty(KUBSAN_ENABLED) 312SAN_CFLAGS+= -fsanitize=undefined 313.endif 314 315# 316# Generic Kernel Coverage support 317# 318.if !empty(COVERAGE_ENABLED) 319.if ${COMPILER_TYPE} == "clang" || \ 320 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 80100) 321SAN_CFLAGS+= -fsanitize-coverage=trace-pc,trace-cmp 322.else 323SAN_CFLAGS+= -fsanitize-coverage=trace-pc 324.endif 325.endif # !empty(COVERAGE_ENABLED) 326 327# Add the sanitizer C flags 328CFLAGS+= ${SAN_CFLAGS} 329 330# 331# Initialize stack variables on function entry 332# 333.if ${OPT_INIT_ALL} != "none" 334.if ${COMPILER_FEATURES:Minit-all} 335CFLAGS+= -ftrivial-auto-var-init=${OPT_INIT_ALL} 336.if ${OPT_INIT_ALL} == "zero" && ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000 337CFLAGS+= -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang 338.endif 339.else 340.warning INIT_ALL (${OPT_INIT_ALL}) requested but not supported by compiler 341.endif 342.endif 343 344# 345# Some newer toolchains default to DWARF 5, which isn't supported by some build 346# tools yet. 347# 348.if (${CFLAGS:M-g} != "" || ${CFLAGS:M-g[0-3]} != "") && ${CFLAGS:M-gdwarf*} == "" 349CFLAGS+= -gdwarf-4 350.endif 351 352CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}} 353CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}} 354CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}} 355 356# Tell bmake not to mistake standard targets for things to be searched for 357# or expect to ever be up-to-date. 358PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \ 359 beforelinking build build-tools buildfiles buildincludes \ 360 checkdpadd clean cleandepend cleandir cleanobj configure \ 361 depend distclean distribute exe \ 362 html includes install installfiles installincludes \ 363 obj objlink objs objwarn \ 364 realinstall regress \ 365 tags whereobj 366 367.PHONY: ${PHONY_NOTMAIN} 368.NOTMAIN: ${PHONY_NOTMAIN} 369 370CSTD?= gnu99 371 372# c99/gnu99 is the minimum C standard version supported for kernel build 373.if ${CSTD} == "k&r" || ${CSTD} == "c89" || ${CSTD} == "c90" || \ 374 ${CSTD} == "c94" || ${CSTD} == "c95" 375.error "Only c99/gnu99 or later is supported" 376.else # CSTD 377CFLAGS+= -std=${CSTD} 378.endif # CSTD 379 380NOSAN_CFLAGS= ${CFLAGS:N-fsanitize*:N-fno-sanitize*:N-fasan-shadow-offset*} 381 382# Please keep this if in sync with bsd.sys.mk 383.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld") 384# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld". 385.if ${COMPILER_TYPE} == "clang" 386# Note: Clang does not like relative paths for ld so we map ld.lld -> lld. 387.if ${COMPILER_VERSION} >= 120000 388CCLDFLAGS+= --ld-path=${LD:[1]:S/^ld.//1W} 389.else 390CCLDFLAGS+= -fuse-ld=${LD:[1]:S/^ld.//1W} 391.endif 392.else 393# GCC does not support an absolute path for -fuse-ld so we just print this 394# warning instead and let the user add the required symlinks. 395# However, we can avoid this warning if -B is set appropriately (e.g. for 396# CROSS_TOOLCHAIN=...-gcc). 397.if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/}) 398.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported 399.endif 400.endif 401.endif 402 403# Set target-specific linker emulation name. 404LD_EMULATION_aarch64=aarch64elf 405LD_EMULATION_amd64=elf_x86_64_fbsd 406LD_EMULATION_arm=armelf_fbsd 407LD_EMULATION_armv7=armelf_fbsd 408LD_EMULATION_i386=elf_i386_fbsd 409LD_EMULATION_powerpc= elf32ppc_fbsd 410LD_EMULATION_powerpcspe= elf32ppc_fbsd 411LD_EMULATION_powerpc64= elf64ppc_fbsd 412LD_EMULATION_powerpc64le= elf64lppc_fbsd 413LD_EMULATION_riscv64= elf64lriscv 414LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}} 415