xref: /freebsd/sys/conf/kern.mk (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1# $FreeBSD$
2
3#
4# Warning flags for compiling the kernel and components of the kernel:
5#
6CWARNFLAGS?=	-Wall -Wstrict-prototypes \
7		-Wmissing-prototypes -Wpointer-arith -Wcast-qual \
8		-Wundef -Wno-pointer-sign ${FORMAT_EXTENSIONS} \
9		-Wmissing-include-dirs -fdiagnostics-show-option \
10		-Wno-unknown-pragmas \
11		${CWARNEXTRA}
12#
13# The following flags are next up for working on:
14#	-Wextra
15
16# Disable a few warnings for clang, since there are several places in the
17# kernel where fixing them is more trouble than it is worth, or where there is
18# a false positive.
19.if ${COMPILER_TYPE} == "clang"
20NO_WCONSTANT_CONVERSION=	-Wno-error=constant-conversion
21NO_WSHIFT_COUNT_NEGATIVE=	-Wno-shift-count-negative
22NO_WSHIFT_COUNT_OVERFLOW=	-Wno-shift-count-overflow
23NO_WSELF_ASSIGN=		-Wno-self-assign
24NO_WUNNEEDED_INTERNAL_DECL=	-Wno-error=unneeded-internal-declaration
25NO_WSOMETIMES_UNINITIALIZED=	-Wno-error=sometimes-uninitialized
26NO_WCAST_QUAL=			-Wno-error=cast-qual
27NO_WTAUTOLOGICAL_POINTER_COMPARE= -Wno-tautological-pointer-compare
28.if ${COMPILER_VERSION} >= 100000
29NO_WMISLEADING_INDENTATION=	-Wno-misleading-indentation
30.endif
31.if ${COMPILER_VERSION} >= 130000
32NO_WUNUSED_BUT_SET_VARIABLE=	-Wno-unused-but-set-variable
33.endif
34.if ${COMPILER_VERSION} >= 140000
35NO_WBITWISE_INSTEAD_OF_LOGICAL=	-Wno-bitwise-instead-of-logical
36.endif
37.if ${COMPILER_VERSION} >= 150000
38NO_WSTRICT_PROTOTYPES=		-Wno-strict-prototypes
39NO_WDEPRECATED_NON_PROTOTYPE=	-Wno-deprecated-non-prototype
40.endif
41# Several other warnings which might be useful in some cases, but not severe
42# enough to error out the whole kernel build.  Display them anyway, so there is
43# some incentive to fix them eventually.
44CWARNEXTRA?=	-Wno-error=tautological-compare -Wno-error=empty-body \
45		-Wno-error=parentheses-equality -Wno-error=unused-function \
46		-Wno-error=pointer-sign
47CWARNEXTRA+=	-Wno-error=shift-negative-value
48CWARNEXTRA+=	-Wno-address-of-packed-member
49.endif	# clang
50
51.if ${COMPILER_TYPE} == "gcc"
52# Catch-all for all the things that are in our tree, but for which we're
53# not yet ready for this compiler.
54NO_WUNUSED_BUT_SET_VARIABLE=-Wno-unused-but-set-variable
55CWARNEXTRA?=	-Wno-error=address				\
56		-Wno-error=aggressive-loop-optimizations	\
57		-Wno-error=array-bounds				\
58		-Wno-error=attributes				\
59		-Wno-error=cast-qual				\
60		-Wno-error=enum-compare				\
61		-Wno-error=maybe-uninitialized			\
62		-Wno-error=misleading-indentation		\
63		-Wno-error=nonnull-compare			\
64		-Wno-error=overflow				\
65		-Wno-error=sequence-point			\
66		-Wno-error=shift-overflow			\
67		-Wno-error=tautological-compare			\
68		-Wno-error=unused-function
69.if ${COMPILER_VERSION} >= 70100
70CWARNEXTRA+=	-Wno-error=stringop-overflow
71.endif
72.if ${COMPILER_VERSION} >= 70200
73CWARNEXTRA+=	-Wno-error=memset-elt-size
74.endif
75.if ${COMPILER_VERSION} >= 80000
76CWARNEXTRA+=	-Wno-error=packed-not-aligned
77.endif
78.if ${COMPILER_VERSION} >= 90100
79CWARNEXTRA+=	-Wno-address-of-packed-member			\
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
144INLINE_LIMIT?=	8000
145.endif
146
147#
148# For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
149# point registers within the kernel. However, we include the F and D extensions
150# in -march so we can have limited floating point support in context switching
151# code. This is different than userland where we use a hard-float ABI (lp64d).
152#
153# We also specify the "medium" code model, which generates code suitable for a
154# 2GiB addressing range located at any offset, allowing modules to be located
155# anywhere in the 64-bit address space.  Note that clang and GCC refer to this
156# code model as "medium" and "medany" respectively.
157#
158.if ${MACHINE_CPUARCH} == "riscv"
159CFLAGS+=	-march=rv64imafdc
160CFLAGS+=	-mabi=lp64
161CFLAGS.clang+=	-mcmodel=medium
162CFLAGS.gcc+=	-mcmodel=medany
163INLINE_LIMIT?=	8000
164
165.if ${LINKER_FEATURES:Mriscv-relaxations} == ""
166CFLAGS+=	-mno-relax
167.endif
168.endif
169
170#
171# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
172# operations inside the kernel itself.  These operations are exclusively
173# reserved for user applications.
174#
175# gcc:
176# Setting -mno-mmx implies -mno-3dnow
177# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
178#
179# clang:
180# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
181# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
182# (-mfpmath= is not supported)
183#
184.if ${MACHINE_CPUARCH} == "amd64"
185CFLAGS.clang+=	-mno-aes -mno-avx
186CFLAGS+=	-mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
187		-fno-asynchronous-unwind-tables
188INLINE_LIMIT?=	8000
189.endif
190
191#
192# For PowerPC we tell gcc to use floating point emulation.  This avoids using
193# floating point registers for integer operations which it has a tendency to do.
194# Also explicitly disable Altivec instructions inside the kernel.
195#
196.if ${MACHINE_CPUARCH} == "powerpc"
197CFLAGS+=	-mno-altivec -msoft-float
198INLINE_LIMIT?=	15000
199.endif
200
201.if ${MACHINE_ARCH} == "powerpcspe"
202CFLAGS.gcc+=	-mno-spe
203.endif
204
205#
206# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
207# DDB happy. ELFv2, if available, has some other efficiency benefits.
208#
209.if ${MACHINE_ARCH:Mpowerpc64*} != "" && \
210    ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
211CFLAGS+=	-mabi=elfv2
212.endif
213
214#
215# GCC 3.0 and above like to do certain optimizations based on the
216# assumption that the program is linked against libc.  Stop this.
217#
218CFLAGS+=	-ffreestanding
219
220#
221# The C standard leaves signed integer overflow behavior undefined.
222# gcc and clang opimizers take advantage of this.  The kernel makes
223# use of signed integer wraparound mechanics so we need the compiler
224# to treat it as a wraparound and not take shortcuts.
225#
226CFLAGS+=	-fwrapv
227
228#
229# GCC SSP support
230#
231.if ${MK_SSP} != "no"
232CFLAGS+=	-fstack-protector
233.endif
234
235#
236# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
237#
238.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
239    ${MK_KERNEL_RETPOLINE} != "no"
240CFLAGS+=	-mretpoline
241.endif
242
243#
244# Initialize stack variables on function entry
245#
246.if ${MK_INIT_ALL_ZERO} == "yes"
247.if ${COMPILER_FEATURES:Minit-all}
248CFLAGS+= -ftrivial-auto-var-init=zero
249.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
250CFLAGS+= -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
251.endif
252.else
253.warning InitAll (zeros) requested but not supported by compiler
254.endif
255.elif ${MK_INIT_ALL_PATTERN} == "yes"
256.if ${COMPILER_FEATURES:Minit-all}
257CFLAGS+= -ftrivial-auto-var-init=pattern
258.else
259.warning InitAll (pattern) requested but not support by compiler
260.endif
261.endif
262
263CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
264CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
265CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
266
267# Tell bmake not to mistake standard targets for things to be searched for
268# or expect to ever be up-to-date.
269PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
270		beforelinking build build-tools buildfiles buildincludes \
271		checkdpadd clean cleandepend cleandir cleanobj configure \
272		depend distclean distribute exe \
273		html includes install installfiles installincludes \
274		obj objlink objs objwarn \
275		realinstall regress \
276		tags whereobj
277
278.PHONY: ${PHONY_NOTMAIN}
279.NOTMAIN: ${PHONY_NOTMAIN}
280
281CSTD=		gnu99
282
283.if ${CSTD} == "k&r"
284CFLAGS+=        -traditional
285.elif ${CSTD} == "c89" || ${CSTD} == "c90"
286CFLAGS+=        -std=iso9899:1990
287.elif ${CSTD} == "c94" || ${CSTD} == "c95"
288CFLAGS+=        -std=iso9899:199409
289.elif ${CSTD} == "c99"
290CFLAGS+=        -std=iso9899:1999
291.else # CSTD
292CFLAGS+=        -std=${CSTD}
293.endif # CSTD
294
295# Please keep this if in sync with bsd.sys.mk
296.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
297# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
298.if ${COMPILER_TYPE} == "clang"
299# Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
300.if ${COMPILER_VERSION} >= 120000
301CCLDFLAGS+=	--ld-path=${LD:[1]:S/^ld.//1W}
302.else
303CCLDFLAGS+=	-fuse-ld=${LD:[1]:S/^ld.//1W}
304.endif
305.else
306# GCC does not support an absolute path for -fuse-ld so we just print this
307# warning instead and let the user add the required symlinks.
308# However, we can avoid this warning if -B is set appropriately (e.g. for
309# CROSS_TOOLCHAIN=...-gcc).
310.if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
311.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
312.endif
313.endif
314.endif
315
316# Set target-specific linker emulation name.
317LD_EMULATION_aarch64=aarch64elf
318LD_EMULATION_amd64=elf_x86_64_fbsd
319LD_EMULATION_arm=armelf_fbsd
320LD_EMULATION_armv6=armelf_fbsd
321LD_EMULATION_armv7=armelf_fbsd
322LD_EMULATION_i386=elf_i386_fbsd
323LD_EMULATION_powerpc= elf32ppc_fbsd
324LD_EMULATION_powerpcspe= elf32ppc_fbsd
325LD_EMULATION_powerpc64= elf64ppc_fbsd
326LD_EMULATION_powerpc64le= elf64lppc_fbsd
327LD_EMULATION_riscv64= elf64lriscv
328LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}
329