1# $NetBSD: bsd.kmodule.mk,v 1.59 2017/01/11 12:19:43 joerg 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.endif 15 16.include <bsd.sys.mk> 17 18##### Basic targets 19realinstall: kmodinstall 20 21KERN= $S/kern 22MKLDSCRIPT?= no 23 24CFLAGS+= -ffreestanding ${COPTS} 25CPPFLAGS+= -nostdinc -I. -I${.CURDIR} -isystem $S -isystem $S/arch 26CPPFLAGS+= -isystem ${S}/../common/include 27CPPFLAGS+= -D_KERNEL -D_LKM -D_MODULE -DSYSCTL_INCLUDE_DESCR 28 29CWARNFLAGS.clang+= -Wno-error=address-of-packed-member -Wno-error=constant-conversion 30 31# XXX until the kernel is fixed again... 32CFLAGS+= -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" 41CFLAGS+= -fno-common -fno-unwind-tables 42.elif ${MACHINE_CPU} == "hppa" 43CFLAGS+= -mlong-calls 44.elif ${MACHINE_CPU} == "powerpc" 45CFLAGS+= ${${ACTIVE_CC} == "gcc":? -mlongcall :} 46.elif ${MACHINE_CPU} == "vax" 47CFLAGS+= -fno-pic 48.elif ${MACHINE_CPU} == "riscv" 49CFLAGS+= -fPIC -Wa,-fno-pic 50.elif ${MACHINE_ARCH} == "mips64eb" && !defined(BSD_MK_COMPAT_FILE) 51CFLAGS+= -mabi=64 52LDFLAGS+= -Wl,-m,elf64btsmip 53.elif ${MACHINE_ARCH} == "mips64el" && !defined(BSD_MK_COMPAT_FILE) 54CFLAGS+= -mabi=64 55LDFLAGS+= -Wl,-m,elf64ltsmip 56.endif 57 58.if ${MACHINE_CPU} == "sparc64" 59# force same memory model as rest of the kernel 60CFLAGS+= ${${ACTIVE_CC} == "gcc":? -mcmodel=medlow :} 61CFLAGS+= ${${ACTIVE_CC} == "clang":? -mcmodel=small :} 62.endif 63 64# evbppc needs some special help 65.if ${MACHINE} == "evbppc" 66 67. ifndef PPC_INTR_IMPL 68PPC_INTR_IMPL=\"powerpc/intr.h\" 69. endif 70. ifndef PPC_PCI_MACHDEP_IMPL 71PPC_PCI_MACHDEP_IMPL=\"powerpc/pci_machdep.h\" 72. endif 73CPPFLAGS+= -DPPC_INTR_IMPL=${PPC_INTR_IMPL} 74CPPFLAGS+= -DPPC_PCI_MACHDEP_IMPL=${DPPC_PCI_MACHDEP_IMPL} 75 76. ifdef PPC_IBM4XX 77CPPFLAGS+= -DPPC_IBM4XX 78. elifdef PPC_BOOKE 79CPPFLAGS+= -DPPC_BOOKE 80. else 81CPPFLAGS+= -DPPC_OEA 82. endif 83 84.endif 85 86 87_YKMSRCS= ${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}} 88DPSRCS+= ${_YKMSRCS} 89CLEANFILES+= ${_YKMSRCS} 90 91.if exists($S/../sys/modules/xldscripts/kmodule) 92KMODSCRIPTSRC= $S/../sys/modules/xldscripts/kmodule 93.else 94KMODSCRIPTSRC= ${DESTDIR}/usr/libdata/ldscripts/kmodule 95.endif 96.if ${MKLDSCRIPT} == "yes" 97KMODSCRIPT= kldscript 98MKLDSCRIPTSH= 99.else 100KMODSCRIPT= ${KMODSCRIPTSRC} 101.endif 102 103PROG?= ${KMOD}.kmod 104 105##### Build rules 106realall: ${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/})) 115XOBJS+= ${f:R:S/$/.o/} 116.else 117XSRCS+= ${f} 118NODPSRCS+= ${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 141OBJS+= ${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. 156ARCHDIR= $S/modules/arch/${MACHINE_CPU} 157ASM_H= $S/arch/${MACHINE_CPU}/include/asm.h 158CLEANFILES+= 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: 204KMODULEARCHDIR?= ${MACHINE} 205_INST_DIRS= ${DESTDIR}/stand/${KMODULEARCHDIR} 206_INST_DIRS+= ${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE} 207_INST_DIRS+= ${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules 208KMODULEDIR= ${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 232kmodinstall:: ${_PROG} 233.PHONY: kmodinstall 234.PRECIOUS: ${_PROG} # keep if install fails 235 236.undef _PROG 237.endif # !target(kmodinstall) 238 239##### Clean rules 240CLEANFILES+= a.out [Ee]rrs mklog core *.core ${PROG} ${OBJS} ${LOBJS} 241CLEANFILES+= ${PROG}.map 242.if ${MKLDSCRIPT} == "yes" 243CLEANFILES+= kldscript 244.endif 245 246##### Custom rules 247lint: ${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 253LINKSOWN?= ${KMODULEOWN} 254LINKSGRP?= ${KMODULEGRP} 255LINKSMODE?= ${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