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