bsd.kmodule.mk revision 1.59.12.1 1 # $NetBSD: bsd.kmodule.mk,v 1.59.12.1 2019/01/26 21:59:58 pgoyette 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 .endif
15
16 .include <bsd.sys.mk>
17
18 ##### Basic targets
19 realinstall: kmodinstall
20
21 KERN= $S/kern
22 MKLDSCRIPT?= no
23
24 CFLAGS+= -ffreestanding ${COPTS}
25 CPPFLAGS+= -nostdinc -I. -I${.CURDIR} -isystem $S -isystem $S/arch
26 CPPFLAGS+= -isystem ${S}/../common/include
27 CPPFLAGS+= -D_KERNEL -D_LKM -D_MODULE -DSYSCTL_INCLUDE_DESCR
28
29 CWARNFLAGS.clang+= -Wno-error=address-of-packed-member -Wno-error=constant-conversion
30
31 # XXX until the kernel is fixed again...
32 CFLAGS+= -fno-strict-aliasing -Wno-pointer-sign
33
34 # XXX This is a workaround for platforms that have relative relocations
35 # that, when relocated by the module loader, result in addresses that
36 # overflow the size of the relocation (e.g. R_PPC_REL24 in powerpc).
37 # The real solution to this involves generating trampolines for those
38 # relocations inside the loader and removing this workaround, as the
39 # resulting code would be much faster.
40 .if ${MACHINE_CPU} == "arm"
41 CFLAGS+= -fno-common -fno-unwind-tables
42 .elif ${MACHINE_CPU} == "hppa"
43 CFLAGS+= -mlong-calls
44 .elif ${MACHINE_CPU} == "powerpc"
45 CFLAGS+= ${${ACTIVE_CC} == "gcc":? -mlongcall :}
46 .elif ${MACHINE_CPU} == "vax"
47 CFLAGS+= -fno-pic
48 .elif ${MACHINE_CPU} == "riscv"
49 CFLAGS+= -fPIC -Wa,-fno-pic
50 .elif ${MACHINE_ARCH} == "mips64eb" && !defined(BSD_MK_COMPAT_FILE)
51 CFLAGS+= -mabi=64
52 LDFLAGS+= -Wl,-m,elf64btsmip
53 .elif ${MACHINE_ARCH} == "mips64el" && !defined(BSD_MK_COMPAT_FILE)
54 CFLAGS+= -mabi=64
55 LDFLAGS+= -Wl,-m,elf64ltsmip
56 .endif
57
58 .if ${MACHINE_CPU} == "sparc64"
59 # force same memory model as rest of the kernel
60 CFLAGS+= ${${ACTIVE_CC} == "gcc":? -mcmodel=medlow :}
61 CFLAGS+= ${${ACTIVE_CC} == "clang":? -mcmodel=small :}
62 .endif
63
64 # evbppc needs some special help
65 .if ${MACHINE} == "evbppc"
66
67 . ifndef PPC_INTR_IMPL
68 PPC_INTR_IMPL=\"powerpc/intr.h\"
69 . endif
70 . ifndef PPC_PCI_MACHDEP_IMPL
71 PPC_PCI_MACHDEP_IMPL=\"powerpc/pci_machdep.h\"
72 . endif
73 CPPFLAGS+= -DPPC_INTR_IMPL=${PPC_INTR_IMPL}
74 CPPFLAGS+= -DPPC_PCI_MACHDEP_IMPL=${DPPC_PCI_MACHDEP_IMPL}
75
76 . ifdef PPC_IBM4XX
77 CPPFLAGS+= -DPPC_IBM4XX
78 . elifdef PPC_BOOKE
79 CPPFLAGS+= -DPPC_BOOKE
80 . else
81 CPPFLAGS+= -DPPC_OEA
82 . endif
83
84 .endif
85
86
87 _YKMSRCS= ${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}}
88 DPSRCS+= ${_YKMSRCS}
89 CLEANFILES+= ${_YKMSRCS}
90
91 .if exists($S/../sys/modules/xldscripts/kmodule)
92 KMODSCRIPTSRC= $S/../sys/modules/xldscripts/kmodule
93 .else
94 KMODSCRIPTSRC= ${DESTDIR}/usr/libdata/ldscripts/kmodule
95 .endif
96 .if ${MKLDSCRIPT} == "yes"
97 KMODSCRIPT= kldscript
98 MKLDSCRIPTSH=
99 .else
100 KMODSCRIPT= ${KMODSCRIPTSRC}
101 .endif
102
103 PROG?= ${KMOD}.kmod
104
105 ##### Build rules
106 realall: ${PROG}
107
108 .if (defined(USE_COMBINE) && ${USE_COMBINE} != "no" && !commands(${_P}) \
109 && !defined(NOCOMBINE.${_P}) && !defined(NOCOMBINE))
110 .for f in ${SRCS:N*.h:N*.sh:N*.fth:C/\.[yl]$/.c/g}
111 .if (${CPPFLAGS.$f:D1} == "1" || ${CPUFLAGS.$f:D2} == "2" \
112 || ${COPTS.$f:D3} == "3" || ${OBJCOPTS.$f:D4} == "4" \
113 || ${CXXFLAGS.$f:D5} == "5") \
114 || ("${f:M*.[cyl]}" == "" || commands(${f:R:S/$/.o/}))
115 XOBJS+= ${f:R:S/$/.o/}
116 .else
117 XSRCS+= ${f}
118 NODPSRCS+= ${f}
119 .endif
120 .endfor
121
122 .if !empty(XOBJS)
123 ${XOBJS}: ${DPSRCS}
124 .endif
125
126 .if ${MKLDSCRIPT} == "yes"
127 ${KMODSCRIPT}: ${KMODSCRIPTSRC} ${XOBJS} $S/conf/mkldscript.sh
128 @rm -f ${.TARGET}
129 @OBJDUMP=${OBJDUMP} ${HOST_SH} $S/conf/mkldscript.sh \
130 -t ${KMODSCRIPTSRC} ${XOBJS} > ${.TARGET}
131 .endif
132
133 ${PROG}: ${XOBJS} ${XSRCS} ${DPSRCS} ${DPADD} ${KMODSCRIPT}
134 ${CC} ${LDFLAGS} -nostdlib -MD -combine -r -Wl,-T,${KMODSCRIPT},-d \
135 -Wl,-Map=${.TARGET}.map \
136 -o ${.TARGET} ${CFLAGS} ${CPPFLAGS} ${XOBJS} \
137 ${XSRCS:@.SRC.@${.ALLSRC:M*.c:M*${.SRC.}}@:O:u} && \
138 echo '.-include "${KMOD}.d"' > .depend
139
140 .else
141 OBJS+= ${SRCS:N*.h:N*.sh:R:S/$/.o/g}
142
143 ${OBJS} ${LOBJS}: ${DPSRCS}
144
145 .if ${MKLDSCRIPT} == "yes"
146 ${KMODSCRIPT}: ${KMODSCRIPTSRC} ${OBJS} $S/conf/mkldscript.sh
147 @rm -f ${.TARGET}
148 @OBJDUMP=${OBJDUMP} ${HOST_SH} $S/conf/mkldscript.sh \
149 -t ${KMODSCRIPTSRC} ${OBJS} > ${.TARGET}
150 .endif
151
152 .if ${MACHINE_CPU} == "arm"
153 # The solution to limited branch space involves generating trampolines for
154 # those relocations while creating the module, as the resulting code will
155 # be much faster and simplifies the loader.
156 ARCHDIR= $S/modules/arch/${MACHINE_CPU}
157 ASM_H= $S/arch/${MACHINE_CPU}/include/asm.h
158 CLEANFILES+= tmp.o tmp.S ${KMOD}_tmp.o ${KMOD}_tramp.o ${KMOD}_tramp.S
159 ${KMOD}_tmp.o: ${OBJS} ${DPADD}
160 ${_MKTARGET_LINK}
161 ${LD} -r -o tmp.o ${OBJS}
162 ${LD} -r \
163 `${OBJDUMP} --syms --reloc tmp.o | \
164 ${TOOL_AWK} -f ${ARCHDIR}/kmodwrap.awk` \
165 -o ${.TARGET} tmp.o
166
167 ${KMOD}_tramp.S: ${KMOD}_tmp.o ${ARCHDIR}/kmodtramp.awk ${ASM_H}
168 ${_MKTARGET_CREATE}
169 ${OBJDUMP} --syms --reloc ${KMOD}_tmp.o | \
170 ${TOOL_AWK} -f ${ARCHDIR}/kmodtramp.awk \
171 > tmp.S && \
172 ${MV} tmp.S ${.TARGET}
173
174 ${PROG}: ${KMOD}_tmp.o ${KMOD}_tramp.o
175 ${_MKTARGET_LINK}
176 .if exists(${ARCHDIR}/kmodhide.awk)
177 ${LD} -r -Map=${.TARGET}.map \
178 -o tmp.o ${KMOD}_tmp.o ${KMOD}_tramp.o
179 ${OBJCOPY} \
180 `${NM} tmp.o | ${TOOL_AWK} -f ${ARCHDIR}/kmodhide.awk` \
181 tmp.o ${.TARGET} && \
182 rm tmp.o
183 .else
184 ${LD} -r -Map=${.TARGET}.map \
185 -o ${.TARGET} ${KMOD}_tmp.o ${KMOD}_tramp.o
186 .endif
187 .else
188 ${PROG}: ${OBJS} ${DPADD} ${KMODSCRIPT}
189 ${_MKTARGET_LINK}
190 ${CC} ${LDFLAGS} -nostdlib -r -Wl,-T,${KMODSCRIPT},-d \
191 -Wl,-Map=${.TARGET}.map \
192 -o ${.TARGET} ${OBJS}
193 .endif
194 .endif
195 .if defined(CTFMERGE)
196 ${CTFMERGE} ${CTFMFLAGS} -o ${.TARGET} ${OBJS}
197 .endif
198
199 ##### Install rules
200 .if !target(kmodinstall)
201 .if !defined(KMODULEDIR)
202 _OSRELEASE!= ${HOST_SH} $S/conf/osrelease.sh -k
203 # Ensure these are recorded properly in METALOG on unprived installes:
204 KMODULEARCHDIR?= ${MACHINE}
205 _INST_DIRS= ${DESTDIR}/stand/${KMODULEARCHDIR}
206 _INST_DIRS+= ${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}
207 _INST_DIRS+= ${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules
208 KMODULEDIR= ${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules/${KMOD}
209 .endif
210 _PROG:= ${KMODULEDIR}/${PROG} # installed path
211
212 .if ${MKUPDATE} == "no"
213 ${_PROG}! ${PROG} # install rule
214 .if !defined(BUILD) && !make(all) && !make(${PROG})
215 ${_PROG}! .MADE # no build at install
216 .endif
217 .else
218 ${_PROG}: ${PROG} # install rule
219 .if !defined(BUILD) && !make(all) && !make(${PROG})
220 ${_PROG}: .MADE # no build at install
221 .endif
222 .endif
223 ${_MKTARGET_INSTALL}
224 dirs=${_INST_DIRS:Q}; \
225 for d in $$dirs; do \
226 ${INSTALL_DIR} $$d; \
227 done
228 ${INSTALL_DIR} ${KMODULEDIR}
229 ${INSTALL_FILE} -o ${KMODULEOWN} -g ${KMODULEGRP} -m ${KMODULEMODE} \
230 ${.ALLSRC} ${.TARGET}
231
232 kmodinstall:: ${_PROG}
233 .PHONY: kmodinstall
234 .PRECIOUS: ${_PROG} # keep if install fails
235
236 .undef _PROG
237 .endif # !target(kmodinstall)
238
239 ##### Clean rules
240 CLEANFILES+= a.out [Ee]rrs mklog core *.core ${PROG} ${OBJS} ${LOBJS}
241 CLEANFILES+= ${PROG}.map
242 .if ${MKLDSCRIPT} == "yes"
243 CLEANFILES+= kldscript
244 .endif
245
246 ##### Custom rules
247 lint: ${LOBJS}
248 .if defined(LOBJS) && !empty(LOBJS)
249 ${LINT} ${LINTFLAGS} ${LDFLAGS:C/-L[ ]*/-L/Wg:M-L*} ${LOBJS} ${LDADD}
250 .endif
251
252 ##### Pull in related .mk logic
253 LINKSOWN?= ${KMODULEOWN}
254 LINKSGRP?= ${KMODULEGRP}
255 LINKSMODE?= ${KMODULEMODE}
256 .include <bsd.man.mk>
257 .include <bsd.links.mk>
258 .include <bsd.dep.mk>
259 .include <bsd.clean.mk>
260
261 .-include "$S/arch/${MACHINE_CPU}/include/Makefile.inc"
262 .-include "$S/arch/${MACHINE}/include/Makefile.inc"
263