xref: /netbsd-src/share/mk/bsd.kmodule.mk (revision d8c8e7d1cb875df9084271702dffbdbf856d254b)
1#	$NetBSD: bsd.kmodule.mk,v 1.86 2024/06/16 21:50:23 pgoyette Exp $
2
3# We are not building this with PIE
4MKPIE=no
5
6.include <bsd.init.mk>
7.include <bsd.klinks.mk>
8
9.if ${MKCTF:Uno} == "yes"
10CFLAGS+=	-g
11# Only need symbols for ctf, strip them after converting to CTF
12CTFFLAGS=	-L VERSION
13CTFMFLAGS=	-t -L VERSION
14# Keep symbols if built with "-g"
15.if !empty(COPTS:M*-g*) || ${MKDEBUG:Uno} == "yes"
16CTFFLAGS+=     -g
17CTFMFLAGS+=    -g
18.endif
19.endif
20
21.include <bsd.sys.mk>
22
23##### Basic targets
24realinstall:	kmodinstall
25
26KERN=		$S/kern
27MKLDSCRIPT?=	no
28
29CFLAGS+=	-ffreestanding ${COPTS}
30CPPFLAGS+=	-nostdinc -I. -I${.CURDIR} -isystem $S -isystem $S/arch
31CPPFLAGS+=	-isystem ${S}/../common/include
32CPPFLAGS+=	-D_KERNEL -D_MODULE -DSYSCTL_INCLUDE_DESCR
33CPPFLAGS+=	${${MKDTRACE:Uno} != "no" :? -DKDTRACE_HOOKS :}
34AFLAGS+=	-D_LOCORE -Wa,--fatal-warnings
35
36CWARNFLAGS.clang+=	-Wno-error=constant-conversion
37
38# XXX until the kernel is fixed again...
39CFLAGS+=	-fno-strict-aliasing
40CWARNFLAGS+=	-Wno-pointer-sign -Wno-attributes
41CWARNFLAGS+=	-Wno-type-limits
42CWARNFLAGS+=	${CC_WNO_ADDRESS_OF_PACKED_MEMBER}
43
44# XXX This is a workaround for platforms that have relative relocations
45# that, when relocated by the module loader, result in addresses that
46# overflow the size of the relocation (e.g. R_PPC_REL24 in powerpc).
47# The real solution to this involves generating trampolines for those
48# relocations inside the loader and removing this workaround, as the
49# resulting code would be much faster.
50.if ${MACHINE_CPU} == "aarch64"
51CFLAGS+=	-march=armv8-a+nofp+nosimd
52.elif ${MACHINE_CPU} == "arm"
53CFLAGS+=	-fno-common -fno-unwind-tables
54.elif ${MACHINE_CPU} == "hppa"
55CFLAGS+=	-mlong-calls -mno-space-regs -mfast-indirect-calls
56.elif ${MACHINE_CPU} == "powerpc"
57CFLAGS+=	${${ACTIVE_CC} == "gcc":? -mlongcall :}
58CFLAGS+=	${${ACTIVE_CC} == "gcc" && ${HAVE_GCC:U0} >= 9:? -mno-pltseq :}
59.elif ${MACHINE_CPU} == "vax"
60CFLAGS+=	-fno-pic
61.elif ${MACHINE_CPU} == "riscv"
62CFLAGS+=	-mcmodel=medany
63.elif ${MACHINE_ARCH} == "mips64eb" && !defined(BSD_MK_COMPAT_FILE)
64CFLAGS+=	-mabi=64
65AFLAGS+=	-mabi=64
66LDFLAGS+=	-Wl,-m,elf64btsmip
67.elif ${MACHINE_ARCH} == "mips64el" && !defined(BSD_MK_COMPAT_FILE)
68CFLAGS+=	-mabi=64
69AFLAGS+=	-mabi=64
70LDFLAGS+=	-Wl,-m,elf64ltsmip
71.endif
72
73.if ${MACHINE_CPU} == "mips"
74# We can't use -msym32 with -mlong-calls as -msym32 forces all addresses
75# to be 32-bit which defeats the whole purpose of long calls.
76CFLAGS+=	-mlong-calls
77.endif
78
79.if ${MACHINE_CPU} == "sparc64"
80# force same memory model as rest of the kernel
81CFLAGS+=	${${ACTIVE_CC} == "gcc":? -mcmodel=medlow :}
82CFLAGS+=	${${ACTIVE_CC} == "clang":? -mcmodel=small :}
83.endif
84
85# evbppc needs some special help
86.if ${MACHINE} == "evbppc"
87
88. ifndef PPC_INTR_IMPL
89PPC_INTR_IMPL=\"powerpc/intr.h\"
90. endif
91. ifndef PPC_PCI_MACHDEP_IMPL
92PPC_PCI_MACHDEP_IMPL=\"powerpc/pci_machdep.h\"
93. endif
94CPPFLAGS+=	-DPPC_INTR_IMPL=${PPC_INTR_IMPL}
95CPPFLAGS+=	-DPPC_PCI_MACHDEP_IMPL=${DPPC_PCI_MACHDEP_IMPL}
96
97. ifdef PPC_IBM4XX
98CPPFLAGS+=	-DPPC_IBM4XX
99. elifdef PPC_BOOKE
100CPPFLAGS+=	-DPPC_BOOKE
101. elif ${MACHINE_ARCH} == "powerpc64"
102CPPFLAGS+=	-DPPC_OEA64
103. else
104CPPFLAGS+=	-DPPC_OEA
105. endif
106
107.endif
108
109
110_YKMSRCS=	${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}}
111DPSRCS+=	${_YKMSRCS}
112CLEANFILES+=	${_YKMSRCS}
113
114.if exists($S/../sys/modules/xldscripts/kmodule)
115KMODSCRIPTSRC=	$S/../sys/modules/xldscripts/kmodule
116.else
117KMODSCRIPTSRC=	${DESTDIR}/usr/libdata/ldscripts/kmodule
118.endif
119.if ${MKLDSCRIPT} == "yes"
120KMODSCRIPT=	kldscript
121MKLDSCRIPTSH=
122.else
123KMODSCRIPT=	${KMODSCRIPTSRC}
124.endif
125
126PROG?=		${KMOD}.kmod
127.if ${MKDEBUG:Uno} != "no" && !defined(NODEBUG) && !commands(${PROG}) && \
128    empty(SRCS:M*.sh)
129PROGDEBUG:=      ${PROG}.debug
130.endif
131
132##### Build rules
133realall:	${PROG} ${PROGDEBUG}
134
135OBJS+=		${SRCS:N*.h:N*.sh:R:S/$/.o/g}
136
137${OBJS} ${LOBJS}: ${DPSRCS}
138
139.if ${MKLDSCRIPT} == "yes"
140${KMODSCRIPT}: ${KMODSCRIPTSRC} ${OBJS} $S/conf/mkldscript.sh
141	@rm -f ${.TARGET}
142	@OBJDUMP=${OBJDUMP} ${HOST_SH} $S/conf/mkldscript.sh \
143	    -t ${KMODSCRIPTSRC} ${OBJS} > ${.TARGET}
144.endif
145
146.if ${MACHINE_CPU} == "arm"
147# The solution to limited branch space involves generating trampolines for
148# those relocations while creating the module, as the resulting code will
149# be much faster and simplifies the loader.
150ARCHDIR=	$S/modules/arch/${MACHINE_CPU}
151ASM_H=		$S/arch/${MACHINE_CPU}/include/asm.h
152CLEANFILES+=	tmp.o tmp.S ${KMOD}_tmp.o ${KMOD}_tramp.o ${KMOD}_tramp.S
153${KMOD}_tmp.o: ${OBJS} ${DPADD}
154	${_MKTARGET_LINK}
155	${LD} -r -o tmp.o ${OBJS}
156	${LD} -r \
157		$$(${OBJDUMP} --syms --reloc tmp.o | \
158			${TOOL_AWK} -f ${ARCHDIR}/kmodwrap.awk) \
159		 -o ${.TARGET} tmp.o
160
161${KMOD}_tramp.S: ${KMOD}_tmp.o ${ARCHDIR}/kmodtramp.awk ${ASM_H}
162	${_MKTARGET_CREATE}
163	${OBJDUMP} --syms --reloc ${KMOD}_tmp.o | \
164		 ${TOOL_AWK} -f ${ARCHDIR}/kmodtramp.awk \
165		 > tmp.S && \
166	${MV} tmp.S ${.TARGET}
167
168${PROG}: ${KMOD}_tmp.o ${KMOD}_tramp.o
169	${_MKTARGET_LINK}
170.if exists(${ARCHDIR}/kmodhide.awk)
171	${LD} -r -Map=${.TARGET}.map \
172	    -o tmp.o ${KMOD}_tmp.o ${KMOD}_tramp.o
173	${OBJCOPY} \
174		$$(${NM} tmp.o | ${TOOL_AWK} -f ${ARCHDIR}/kmodhide.awk) \
175		tmp.o ${.TARGET} && \
176	rm tmp.o
177.else
178	${LD} -r -Map=${.TARGET}.map \
179	    -o ${.TARGET} ${KMOD}_tmp.o ${KMOD}_tramp.o
180.endif
181.else
182${PROG}: ${OBJS} ${DPADD} ${KMODSCRIPT}
183	${_MKTARGET_LINK}
184	${CC} ${LDFLAGS} -nostdlib -r -Wl,-T,${KMODSCRIPT},-d \
185		-Wl,-Map=${.TARGET}.map \
186		-o ${.TARGET} ${OBJS}
187.endif
188.if defined(CTFMERGE)
189	${CTFMERGE} ${CTFMFLAGS} -o ${.TARGET} ${OBJS}
190.endif
191
192.if defined(PROGDEBUG)
193${PROGDEBUG}: ${PROG}
194	${_MKTARGET_CREATE}
195	( ${OBJCOPY} --only-keep-debug --compress-debug-sections \
196	    ${PROG} ${PROGDEBUG} \
197	&& ${OBJCOPY} --strip-debug -p -R .gnu_debuglink \
198		--add-gnu-debuglink=${PROGDEBUG} ${PROG} \
199	) || (rm -f ${PROGDEBUG}; false)
200.endif
201
202##### Install rules
203.if !target(kmodinstall)
204.if !defined(KMODULEDIR)
205.if ${KERNEL_DIR:Uno} == "yes"
206_INST_DIRS=	/netbsd
207_INST_DIRS+=	/netbsd/modules
208KMODULEDIR=	/netbsd/modules/${KMOD}
209.else
210# Ensure these are recorded properly in METALOG on unprived installes:
211_OSRELEASE!=	${HOST_SH} $S/conf/osrelease.sh -k
212KMODULEARCHDIR?= ${MACHINE}
213_INST_DIRS=	/stand/${KMODULEARCHDIR}
214_INST_DIRS+=	/stand/${KMODULEARCHDIR}/${_OSRELEASE}
215_INST_DIRS+=	/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules
216KMODULEDIR=	/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules/${KMOD}
217.endif
218.endif
219
220_INST_DIRS+=	${KMODULEDIR}
221_PROG:=		${DESTDIR}${KMODULEDIR}/${PROG} # installed path
222
223.if defined(PROGDEBUG)
224.for i in ${_INST_DIRS}
225_DEBUG_INST_DIRS += ${DEBUGDIR}${i}
226.endfor
227_INST_DIRS += ${_DEBUG_INST_DIRS}
228_PROGDEBUG:=	${DESTDIR}${DEBUGDIR}${KMODULEDIR}/${PROG}.debug
229.endif
230
231.for _P P in ${_PROG} ${PROG} ${_PROGDEBUG} ${PROGDEBUG}
232.if ${MKUPDATE} == "no"
233${_P}! ${P}					# install rule
234.if !defined(BUILD) && !make(all) && !make(${P})
235${_P}!	.MADE					# no build at install
236.endif
237.else
238${_P}: ${P}					# install rule
239.if !defined(BUILD) && !make(all) && !make(${P})
240${_P}:	.MADE					# no build at install
241.endif
242.endif
243	${_MKTARGET_INSTALL}
244	dirs=${_INST_DIRS:Q}; \
245	for d in $$dirs; do \
246		${INSTALL_DIR} ${DESTDIR}$$d; \
247	done
248	${INSTALL_FILE} -o ${KMODULEOWN} -g ${KMODULEGRP} -m ${KMODULEMODE} \
249		${.ALLSRC} ${.TARGET}
250
251kmodinstall::	${_P}
252.PHONY:		kmodinstall
253.PRECIOUS:	${_P}				# keep if install fails
254.endfor
255
256.undef _PPROG
257.undef _PPROGDEBUG
258.endif # !target(kmodinstall)
259
260##### Clean rules
261CLEANFILES+= a.out [Ee]rrs mklog core *.core ${PROG} ${OBJS} ${LOBJS}
262CLEANFILES+= ${PROGDEBUG}
263CLEANFILES+= ${PROG}.map
264.if ${MKLDSCRIPT} == "yes"
265CLEANFILES+= kldscript
266.endif
267
268##### Custom rules
269lint: ${LOBJS}
270.if defined(LOBJS) && !empty(LOBJS)
271	${LINT} ${LINTFLAGS} ${LDFLAGS:C/-L[  ]*/-L/Wg:M-L*} ${LOBJS} ${LDADD}
272.endif
273
274##### Pull in related .mk logic
275LINKSOWN?= ${KMODULEOWN}
276LINKSGRP?= ${KMODULEGRP}
277LINKSMODE?= ${KMODULEMODE}
278.include <bsd.man.mk>
279.include <bsd.links.mk>
280.include <bsd.dep.mk>
281.include <bsd.clean.mk>
282
283.-include "$S/arch/${MACHINE_CPU}/include/Makefile.inc"
284.-include "$S/arch/${MACHINE}/include/Makefile.inc"
285