1# $NetBSD: bsd.kmodule.mk,v 1.81 2022/08/07 23:42:09 riastradh 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.gcc+= ${GCC_NO_ADDR_OF_PACKED_MEMBER} 42CWARNFLAGS.clang+= -Wno-error=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+= -fPIC -Wa,-fno-pic 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 ${PROG} ${PROGDEBUG} \ 196 && ${OBJCOPY} --strip-debug -p -R .gnu_debuglink \ 197 --add-gnu-debuglink=${PROGDEBUG} ${PROG} \ 198 ) || (rm -f ${PROGDEBUG}; false) 199.endif 200 201##### Install rules 202.if !target(kmodinstall) 203.if !defined(KMODULEDIR) 204.if ${KERNEL_DIR:Uno} == "yes" 205_INST_DIRS= /netbsd 206_INST_DIRS+= /netbsd/modules 207KMODULEDIR= /netbsd/modules/${KMOD} 208.else 209# Ensure these are recorded properly in METALOG on unprived installes: 210_OSRELEASE!= ${HOST_SH} $S/conf/osrelease.sh -k 211KMODULEARCHDIR?= ${MACHINE} 212_INST_DIRS= /stand/${KMODULEARCHDIR} 213_INST_DIRS+= /stand/${KMODULEARCHDIR}/${_OSRELEASE} 214_INST_DIRS+= /stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules 215KMODULEDIR= /stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules/${KMOD} 216.endif 217.endif 218 219_INST_DIRS+= ${KMODULEDIR} 220_PROG:= ${DESTDIR}${KMODULEDIR}/${PROG} # installed path 221 222.if defined(PROGDEBUG) 223.for i in ${_INST_DIRS} 224_DEBUG_INST_DIRS += ${DEBUGDIR}${i} 225.endfor 226_INST_DIRS += ${_DEBUG_INST_DIRS} 227_PROGDEBUG:= ${DESTDIR}${DEBUGDIR}${KMODULEDIR}/${PROG}.debug 228.endif 229 230.for _P P in ${_PROG} ${PROG} ${_PROGDEBUG} ${PROGDEBUG} 231.if ${MKUPDATE} == "no" 232${_P}! ${P} # install rule 233.if !defined(BUILD) && !make(all) && !make(${P}) 234${_P}! .MADE # no build at install 235.endif 236.else 237${_P}: ${P} # install rule 238.if !defined(BUILD) && !make(all) && !make(${P}) 239${_P}: .MADE # no build at install 240.endif 241.endif 242 ${_MKTARGET_INSTALL} 243 dirs=${_INST_DIRS:Q}; \ 244 for d in $$dirs; do \ 245 ${INSTALL_DIR} ${DESTDIR}$$d; \ 246 done 247 ${INSTALL_FILE} -o ${KMODULEOWN} -g ${KMODULEGRP} -m ${KMODULEMODE} \ 248 ${.ALLSRC} ${.TARGET} 249 250kmodinstall:: ${_P} 251.PHONY: kmodinstall 252.PRECIOUS: ${_P} # keep if install fails 253.endfor 254 255.undef _PPROG 256.undef _PPROGDEBUG 257.endif # !target(kmodinstall) 258 259##### Clean rules 260CLEANFILES+= a.out [Ee]rrs mklog core *.core ${PROG} ${OBJS} ${LOBJS} 261CLEANFILES+= ${PROGDEBUG} 262CLEANFILES+= ${PROG}.map 263.if ${MKLDSCRIPT} == "yes" 264CLEANFILES+= kldscript 265.endif 266 267##### Custom rules 268lint: ${LOBJS} 269.if defined(LOBJS) && !empty(LOBJS) 270 ${LINT} ${LINTFLAGS} ${LDFLAGS:C/-L[ ]*/-L/Wg:M-L*} ${LOBJS} ${LDADD} 271.endif 272 273##### Pull in related .mk logic 274LINKSOWN?= ${KMODULEOWN} 275LINKSGRP?= ${KMODULEGRP} 276LINKSMODE?= ${KMODULEMODE} 277.include <bsd.man.mk> 278.include <bsd.links.mk> 279.include <bsd.dep.mk> 280.include <bsd.clean.mk> 281 282.-include "$S/arch/${MACHINE_CPU}/include/Makefile.inc" 283.-include "$S/arch/${MACHINE}/include/Makefile.inc" 284