17f3dea24SPeter Wemm# $FreeBSD$ 26b30bfd3SBruce Evans 36b30bfd3SBruce Evans# 412dd58a3SRebecca Cran# Warning flags for compiling the kernel and components of the kernel: 56b30bfd3SBruce Evans# 6ea8f3ee3SPeter WemmCWARNFLAGS?= -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes \ 7df66feb8SMatt Macy -Wmissing-prototypes -Wpointer-arith -Wcast-qual \ 854fc0ecaSBrooks Davis -Wundef -Wno-pointer-sign ${FORMAT_EXTENSIONS} \ 92cd8464eSDimitry Andric -Wmissing-include-dirs -fdiagnostics-show-option \ 10f75ecd9bSWarner Losh -Wno-unknown-pragmas \ 112cd8464eSDimitry Andric ${CWARNEXTRA} 129a6c0bd6SBruce Evans# 139a6c0bd6SBruce Evans# The following flags are next up for working on: 145b44317bSRebecca Cran# -Wextra 150bcef6efSMatthew Dillon 16a380d07bSDimitry Andric# Disable a few warnings for clang, since there are several places in the 17a380d07bSDimitry Andric# kernel where fixing them is more trouble than it is worth, or where there is 18a380d07bSDimitry Andric# a false positive. 199215d178SBrooks Davis.if ${COMPILER_TYPE} == "clang" 201c7c2b26SDimitry AndricNO_WCONSTANT_CONVERSION= -Wno-error-constant-conversion 21cccc969bSMatt MacyNO_WSHIFT_COUNT_NEGATIVE= -Wno-shift-count-negative 22cccc969bSMatt MacyNO_WSHIFT_COUNT_OVERFLOW= -Wno-shift-count-overflow 23cccc969bSMatt MacyNO_WSELF_ASSIGN= -Wno-self-assign 241c7c2b26SDimitry AndricNO_WUNNEEDED_INTERNAL_DECL= -Wno-error-unneeded-internal-declaration 257ae0e2c9SDimitry AndricNO_WSOMETIMES_UNINITIALIZED= -Wno-error-sometimes-uninitialized 261c7c2b26SDimitry AndricNO_WCAST_QUAL= -Wno-error-cast-qual 27cccc969bSMatt MacyNO_WTAUTOLOGICAL_POINTER_COMPARE= -Wno-tautological-pointer-compare 280ed78d64SDimitry Andric# Several other warnings which might be useful in some cases, but not severe 290ed78d64SDimitry Andric# enough to error out the whole kernel build. Display them anyway, so there is 300ed78d64SDimitry Andric# some incentive to fix them eventually. 318cd925d9SDimitry AndricCWARNEXTRA?= -Wno-error-tautological-compare -Wno-error-empty-body \ 32faa5d16dSWarner Losh -Wno-error-parentheses-equality -Wno-error-unused-function \ 3374079bdaSWarner Losh -Wno-error-pointer-sign 3445587483SDimitry Andric.if ${COMPILER_VERSION} >= 30700 3545587483SDimitry AndricCWARNEXTRA+= -Wno-error-shift-negative-value 3645587483SDimitry Andric.endif 378177b837SDimitry Andric.if ${COMPILER_VERSION} >= 40000 38cccc969bSMatt MacyCWARNEXTRA+= -Wno-address-of-packed-member 398177b837SDimitry Andric.endif 40a5b2b29fSBrooks Davis.endif 41a5b2b29fSBrooks Davis 42bbae23a7SIan Lepore.if ${COMPILER_TYPE} == "gcc" 4312701462SBryan Drewery.if ${COMPILER_VERSION} >= 40800 44f5bb5524SWarner Losh# Catch-all for all the things that are in our tree, but for which we're 4512701462SBryan Drewery# not yet ready for this compiler. 46e5ae3af7SMatt MacyNO_WUNUSED_BUT_SET_VARIABLE = -Wno-unused-but-set-variable 4796c072fcSRuslan BukinCWARNEXTRA?= -Wno-error=address \ 4896c072fcSRuslan Bukin -Wno-error=aggressive-loop-optimizations \ 4996c072fcSRuslan Bukin -Wno-error=array-bounds \ 5096c072fcSRuslan Bukin -Wno-error=attributes \ 5196c072fcSRuslan Bukin -Wno-error=cast-qual \ 5296c072fcSRuslan Bukin -Wno-error=enum-compare \ 5396c072fcSRuslan Bukin -Wno-error=inline \ 5496c072fcSRuslan Bukin -Wno-error=maybe-uninitialized \ 5596c072fcSRuslan Bukin -Wno-error=overflow \ 5696c072fcSRuslan Bukin -Wno-error=sequence-point \ 575426539cSMatt Macy -Wno-unused-but-set-variable 58c4dc5cdbSRuslan Bukin.if ${COMPILER_VERSION} >= 60100 5996c072fcSRuslan BukinCWARNEXTRA+= -Wno-error=misleading-indentation \ 6096c072fcSRuslan Bukin -Wno-error=nonnull-compare \ 6196c072fcSRuslan Bukin -Wno-error=shift-overflow \ 62be3ab187SConrad Meyer -Wno-error=tautological-compare 63c4dc5cdbSRuslan Bukin.endif 6488eb44d7SRyan Libby.if ${COMPILER_VERSION} >= 70100 6588eb44d7SRyan LibbyCWARNEXTRA+= -Wno-error=stringop-overflow 6688eb44d7SRyan Libby.endif 678cd6c09eSRuslan Bukin.if ${COMPILER_VERSION} >= 70200 688cd6c09eSRuslan BukinCWARNEXTRA+= -Wno-error=memset-elt-size 698cd6c09eSRuslan Bukin.endif 70d39c2658SMatt Macy.if ${COMPILER_VERSION} >= 80000 71d39c2658SMatt MacyCWARNEXTRA+= -Wno-error=packed-not-aligned 72d39c2658SMatt Macy.endif 73bbae23a7SIan Lepore.else 74bbae23a7SIan Lepore# For gcc 4.2, eliminate the too-often-wrong warnings about uninitialized vars. 75bbae23a7SIan LeporeCWARNEXTRA?= -Wno-uninitialized 760a646b97SConrad Meyer# GCC 4.2 doesn't have -Wno-error=cast-qual, so just disable the warning for 770a646b97SConrad Meyer# the few files that are already known to generate cast-qual warnings. 780a646b97SConrad MeyerNO_WCAST_QUAL= -Wno-cast-qual 7982334850SJohn BaldwinNO_WNONNULL= -Wno-nonnull 80bbae23a7SIan Lepore.endif 8120256b0bSWarner Losh.endif 8220256b0bSWarner Losh 83be3ab187SConrad Meyer# This warning is utter nonsense 84be3ab187SConrad MeyerCWARNFLAGS+= -Wno-format-zero-length 85be3ab187SConrad Meyer 86a5b2b29fSBrooks Davis# External compilers may not support our format extensions. Allow them 87a5b2b29fSBrooks Davis# to be disabled. WARNING: format checking is disabled in this case. 88a5b2b29fSBrooks Davis.if ${MK_FORMAT_EXTENSIONS} == "no" 89e94a64deSWarner LoshFORMAT_EXTENSIONS= -Wno-format 90accc510aSDimitry Andric.elif ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600 91accc510aSDimitry AndricFORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__ 9288f05751SEnji Cooper.else 9354fc0ecaSBrooks DavisFORMAT_EXTENSIONS= -fformat-extensions 94a380d07bSDimitry Andric.endif 95a380d07bSDimitry Andric 9665be2aacSDoug Rabson# 975b44317bSRebecca Cran# On i386, do not align the stack to 16-byte boundaries. Otherwise GCC 2.95 985b44317bSRebecca Cran# and above adds code to the entry and exit point of every function to align the 9954d477a4SDavid E. O'Brien# stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack 10054d477a4SDavid E. O'Brien# per function call. While the 16-byte alignment may benefit micro benchmarks, 101b453864cSMike Pritchard# it is probably an overall loss as it makes the code bigger (less efficient 10254d477a4SDavid E. O'Brien# use of code cache tag lines) and uses more stack (less efficient use of data 1035b44317bSRebecca Cran# cache tag lines). Explicitly prohibit the use of FPU, SSE and other SIMD 1048d2046faSPeter Wemm# operations inside the kernel itself. These operations are exclusively 1058d2046faSPeter Wemm# reserved for user applications. 10654d477a4SDavid E. O'Brien# 107d1b9bdd1SRebecca Cran# gcc: 108d1b9bdd1SRebecca Cran# Setting -mno-mmx implies -mno-3dnow 109d1b9bdd1SRebecca Cran# Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3 110d1b9bdd1SRebecca Cran# 111d1b9bdd1SRebecca Cran# clang: 11243917540SDimitry Andric# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa 11343917540SDimitry Andric# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42 114d1b9bdd1SRebecca Cran# 11558ff0f42SDimitry Andric.if ${MACHINE_CPUARCH} == "i386" 11622ba0b2fSWarner LoshCFLAGS.gcc+= -mno-align-long-strings -mpreferred-stack-boundary=2 11722ba0b2fSWarner LoshCFLAGS.clang+= -mno-aes -mno-avx 11843917540SDimitry AndricCFLAGS+= -mno-mmx -mno-sse -msoft-float 1194338438bSDavid E. O'BrienINLINE_LIMIT?= 8000 12054d477a4SDavid E. O'Brien.endif 12154d477a4SDavid E. O'Brien 122c614f4e2SWarner Losh.if ${MACHINE_CPUARCH} == "arm" 123d6301dcdSOlivier HouchardINLINE_LIMIT?= 8000 124d6301dcdSOlivier Houchard.endif 1255b44317bSRebecca Cran 1268598392cSKonstantin Belousov.if ${MACHINE_CPUARCH} == "aarch64" 1278598392cSKonstantin Belousov# We generally don't want fpu instructions in the kernel. 1288598392cSKonstantin BelousovCFLAGS += -mgeneral-regs-only 1298598392cSKonstantin Belousov# Reserve x18 for pcpu data 1308598392cSKonstantin BelousovCFLAGS += -ffixed-x18 131cbc6e751SAndrew TurnerINLINE_LIMIT?= 8000 1328598392cSKonstantin Belousov.endif 1338598392cSKonstantin Belousov 13445b535faSMitchell Horne# 13545b535faSMitchell Horne# For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating 13645b535faSMitchell Horne# point registers within the kernel. We also specify the "medium" code model, 13745b535faSMitchell Horne# which generates code suitable for a 2GiB addressing range located at any 13845b535faSMitchell Horne# offset, allowing modules to be located anywhere in the 64-bit address space. 13945b535faSMitchell Horne# Note that clang and GCC refer to this code model as "medium" and "medany" 14045b535faSMitchell Horne# respectively. 14145b535faSMitchell Horne# 14228029b68SRuslan Bukin.if ${MACHINE_CPUARCH} == "riscv" 14345b535faSMitchell HorneCFLAGS+= -march=rv64imafdc -mabi=lp64 14445b535faSMitchell HorneCFLAGS.clang+= -mcmodel=medium 14545b535faSMitchell HorneCFLAGS.gcc+= -mcmodel=medany 14628029b68SRuslan BukinINLINE_LIMIT?= 8000 147*0869dcebSJohn Baldwin 148*0869dcebSJohn Baldwin.if ${LINKER_FEATURES:Mriscv-relaxations} == "" 149*0869dcebSJohn BaldwinCFLAGS+= -mno-relax 150*0869dcebSJohn Baldwin.endif 15128029b68SRuslan Bukin.endif 15228029b68SRuslan Bukin 1530d409bfdSDoug Rabson# 1541496f657SMarius Strobl# For sparc64 we want the medany code model so modules may be located 1551496f657SMarius Strobl# anywhere in the 64-bit address space. We also tell GCC to use floating 15653faa77bSJake Burkholder# point emulation. This avoids using floating point registers for integer 15753faa77bSJake Burkholder# operations which it has a tendency to do. 15853faa77bSJake Burkholder# 159c614f4e2SWarner Losh.if ${MACHINE_CPUARCH} == "sparc64" 16022ba0b2fSWarner LoshCFLAGS.clang+= -mcmodel=large -fno-dwarf2-cfi-asm 16122ba0b2fSWarner LoshCFLAGS.gcc+= -mcmodel=medany -msoft-float 1625064105eSPeter WemmINLINE_LIMIT?= 15000 16353faa77bSJake Burkholder.endif 16453faa77bSJake Burkholder 16553faa77bSJake Burkholder# 1668d2046faSPeter Wemm# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD 1678d2046faSPeter Wemm# operations inside the kernel itself. These operations are exclusively 1688d2046faSPeter Wemm# reserved for user applications. 169a15febf9SPeter Wemm# 170d1b9bdd1SRebecca Cran# gcc: 171d1b9bdd1SRebecca Cran# Setting -mno-mmx implies -mno-3dnow 172d1b9bdd1SRebecca Cran# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387 173d1b9bdd1SRebecca Cran# 174d1b9bdd1SRebecca Cran# clang: 17543917540SDimitry Andric# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa 17643917540SDimitry Andric# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42 177d1b9bdd1SRebecca Cran# (-mfpmath= is not supported) 178d1b9bdd1SRebecca Cran# 179c614f4e2SWarner Losh.if ${MACHINE_CPUARCH} == "amd64" 18022ba0b2fSWarner LoshCFLAGS.clang+= -mno-aes -mno-avx 18143917540SDimitry AndricCFLAGS+= -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \ 182d1b9bdd1SRebecca Cran -fno-asynchronous-unwind-tables 1834e37bb8fSPeter WemmINLINE_LIMIT?= 8000 184a15febf9SPeter Wemm.endif 185a15febf9SPeter Wemm 186a15febf9SPeter Wemm# 187813dd172SDavid E. O'Brien# For PowerPC we tell gcc to use floating point emulation. This avoids using 188813dd172SDavid E. O'Brien# floating point registers for integer operations which it has a tendency to do. 1895b4975b1SNathan Whitehorn# Also explicitly disable Altivec instructions inside the kernel. 190813dd172SDavid E. O'Brien# 191c614f4e2SWarner Losh.if ${MACHINE_CPUARCH} == "powerpc" 192d9dbd70bSJustin HibbitsCFLAGS+= -mno-altivec -msoft-float 193813dd172SDavid E. O'BrienINLINE_LIMIT?= 15000 194813dd172SDavid E. O'Brien.endif 195813dd172SDavid E. O'Brien 196dc9b124dSJustin Hibbits.if ${MACHINE_ARCH} == "powerpcspe" 197d9dbd70bSJustin HibbitsCFLAGS.gcc+= -mno-spe 198dc9b124dSJustin Hibbits.endif 199dc9b124dSJustin Hibbits 200813dd172SDavid E. O'Brien# 2015d851700SNathan Whitehorn# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make 2025d851700SNathan Whitehorn# DDB happy. ELFv2, if available, has some other efficiency benefits. 20379c77d72SNathan Whitehorn# 20479c77d72SNathan Whitehorn.if ${MACHINE_ARCH} == "powerpc64" 2055d851700SNathan Whitehorn.if ${COMPILER_VERSION} >= 40900 2065d851700SNathan WhitehornCFLAGS.gcc+= -mabi=elfv2 2075d851700SNathan Whitehorn.else 208e52a2dc8SNathan WhitehornCFLAGS.gcc+= -mcall-aixdesc 20979c77d72SNathan Whitehorn.endif 2105d851700SNathan WhitehornCFLAGS.clang+= -mabi=elfv2 2115d851700SNathan Whitehorn.endif 21279c77d72SNathan Whitehorn 21379c77d72SNathan Whitehorn# 2149fc13460SWarner Losh# For MIPS we also tell gcc to use floating point emulation 215578328c9SOleksandr Tymoshenko# 216c614f4e2SWarner Losh.if ${MACHINE_CPUARCH} == "mips" 2179fc13460SWarner LoshCFLAGS+= -msoft-float 218e0a04e74SWarner LoshINLINE_LIMIT?= 8000 219578328c9SOleksandr Tymoshenko.endif 220578328c9SOleksandr Tymoshenko 221578328c9SOleksandr Tymoshenko# 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# 2365b44317bSRebecca Cran# GCC SSP support 237042df2e2SRuslan Ermilov# 238e7d939bdSMarcel Moolenaar.if ${MK_SSP} != "no" && \ 23958ff0f42SDimitry Andric ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips" 240042df2e2SRuslan ErmilovCFLAGS+= -fstack-protector 241e9093b66SEd Maste.endif 242e9093b66SEd Maste 243e9093b66SEd Maste# 244e9093b66SEd Maste# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715) 245e9093b66SEd Maste# 246e9093b66SEd Maste.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \ 247e9093b66SEd Maste ${MK_KERNEL_RETPOLINE} != "no" 248e9093b66SEd MasteCFLAGS+= -mretpoline 249042df2e2SRuslan Ermilov.endif 2503ad1a091SWarner Losh 2513ad1a091SWarner Losh# 252647a9d04SWarner Losh# Add -gdwarf-2 when compiling -g. The default starting in clang v3.4 253647a9d04SWarner Losh# and gcc 4.8 is to generate DWARF version 4. However, our tools don't 254647a9d04SWarner Losh# cope well with DWARF 4, so force it to genereate DWARF2, which they 255647a9d04SWarner Losh# understand. Do this unconditionally as it is harmless when not needed, 256647a9d04SWarner Losh# but critical for these newer versions. 2573ad1a091SWarner Losh# 258647a9d04SWarner Losh.if ${CFLAGS:M-g} != "" && ${CFLAGS:M-gdwarf*} == "" 2593ad1a091SWarner LoshCFLAGS+= -gdwarf-2 2603ad1a091SWarner Losh.endif 26122ba0b2fSWarner Losh 2629346408dSRuslan BukinCFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}} 263b16a3c9dSKyle EvansCFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}} 264814e92e9SWarner LoshCFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}} 2657ed11c5eSWarner Losh 2667ed11c5eSWarner Losh# Tell bmake not to mistake standard targets for things to be searched for 2677ed11c5eSWarner Losh# or expect to ever be up-to-date. 2687ed11c5eSWarner LoshPHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \ 2697ed11c5eSWarner Losh beforelinking build build-tools buildfiles buildincludes \ 2707ed11c5eSWarner Losh checkdpadd clean cleandepend cleandir cleanobj configure \ 2714e47b95cSBryan Drewery depend distclean distribute exe \ 2721cbb5888SWarner Losh html includes install installfiles installincludes \ 2734e47b95cSBryan Drewery obj objlink objs objwarn \ 2744e47b95cSBryan Drewery realinstall regress \ 2757ed11c5eSWarner Losh tags whereobj 2767ed11c5eSWarner Losh 2777ed11c5eSWarner Losh.PHONY: ${PHONY_NOTMAIN} 2787ed11c5eSWarner Losh.NOTMAIN: ${PHONY_NOTMAIN} 279faa5d16dSWarner Losh 280faa5d16dSWarner LoshCSTD= c99 281faa5d16dSWarner Losh 282faa5d16dSWarner Losh.if ${CSTD} == "k&r" 283faa5d16dSWarner LoshCFLAGS+= -traditional 284faa5d16dSWarner Losh.elif ${CSTD} == "c89" || ${CSTD} == "c90" 285faa5d16dSWarner LoshCFLAGS+= -std=iso9899:1990 286faa5d16dSWarner Losh.elif ${CSTD} == "c94" || ${CSTD} == "c95" 287faa5d16dSWarner LoshCFLAGS+= -std=iso9899:199409 288faa5d16dSWarner Losh.elif ${CSTD} == "c99" 289faa5d16dSWarner LoshCFLAGS+= -std=iso9899:1999 290faa5d16dSWarner Losh.else # CSTD 291faa5d16dSWarner LoshCFLAGS+= -std=${CSTD} 292faa5d16dSWarner Losh.endif # CSTD 2931dd50172SEd Maste 294dc653882SJohn Baldwin# Set target-specific linker emulation name. 2951dd50172SEd MasteLD_EMULATION_aarch64=aarch64elf 2961dd50172SEd MasteLD_EMULATION_amd64=elf_x86_64_fbsd 2971dd50172SEd MasteLD_EMULATION_arm=armelf_fbsd 2981dd50172SEd MasteLD_EMULATION_armv6=armelf_fbsd 2990b972ac9SWarner LoshLD_EMULATION_armv7=armelf_fbsd 3001dd50172SEd MasteLD_EMULATION_i386=elf_i386_fbsd 3011dd50172SEd MasteLD_EMULATION_mips= elf32btsmip_fbsd 302e31b69ecSRuslan BukinLD_EMULATION_mipshf= elf32btsmip_fbsd 3031dd50172SEd MasteLD_EMULATION_mips64= elf64btsmip_fbsd 304e31b69ecSRuslan BukinLD_EMULATION_mips64hf= elf64btsmip_fbsd 3051dd50172SEd MasteLD_EMULATION_mipsel= elf32ltsmip_fbsd 306e31b69ecSRuslan BukinLD_EMULATION_mipselhf= elf32ltsmip_fbsd 3071dd50172SEd MasteLD_EMULATION_mips64el= elf64ltsmip_fbsd 308e31b69ecSRuslan BukinLD_EMULATION_mips64elhf= elf64ltsmip_fbsd 3091dd50172SEd MasteLD_EMULATION_mipsn32= elf32btsmipn32_fbsd 3101dd50172SEd MasteLD_EMULATION_mipsn32el= elf32btsmipn32_fbsd # I don't think this is a thing that works 3111dd50172SEd MasteLD_EMULATION_powerpc= elf32ppc_fbsd 312dc9b124dSJustin HibbitsLD_EMULATION_powerpcspe= elf32ppc_fbsd 3131dd50172SEd MasteLD_EMULATION_powerpc64= elf64ppc_fbsd 314fd46810aSJohn BaldwinLD_EMULATION_riscv64= elf64lriscv 3151dd50172SEd MasteLD_EMULATION_sparc64= elf64_sparc_fbsd 3161dd50172SEd MasteLD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}} 317