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