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