Home | History | Annotate | Line # | Download | only in mk
bsd.kmodule.mk revision 1.64
      1  1.64  christos #	$NetBSD: bsd.kmodule.mk,v 1.64 2020/04/04 19:50:53 christos Exp $
      2  1.14      elad 
      3  1.14      elad # We are not building this with PIE
      4  1.14      elad MKPIE=no
      5   1.1        ad 
      6   1.1        ad .include <bsd.init.mk>
      7   1.1        ad .include <bsd.klinks.mk>
      8  1.57  christos 
      9  1.57  christos .if ${MKCTF:Uno} == "yes"
     10  1.57  christos CFLAGS+=	-g
     11  1.57  christos # Only need symbols for ctf, strip them after converting to CTF
     12  1.57  christos CTFFLAGS=	-L VERSION
     13  1.58  christos CTFMFLAGS=	-t -L VERSION
     14  1.62   hannken # Keep symbols if built with "-g"
     15  1.62   hannken .if !empty(COPTS:M*-g*)
     16  1.62   hannken CTFFLAGS+=	-g
     17  1.62   hannken CTFMFLAGS+=	-g
     18  1.62   hannken .endif
     19  1.57  christos .endif
     20  1.57  christos 
     21   1.7        ad .include <bsd.sys.mk>
     22   1.1        ad 
     23   1.1        ad ##### Basic targets
     24   1.1        ad realinstall:	kmodinstall
     25   1.1        ad 
     26   1.1        ad KERN=		$S/kern
     27  1.47  christos MKLDSCRIPT?=	no
     28   1.1        ad 
     29   1.6        ad CFLAGS+=	-ffreestanding ${COPTS}
     30   1.1        ad CPPFLAGS+=	-nostdinc -I. -I${.CURDIR} -isystem $S -isystem $S/arch
     31   1.1        ad CPPFLAGS+=	-isystem ${S}/../common/include
     32  1.33   mbalmer CPPFLAGS+=	-D_KERNEL -D_LKM -D_MODULE -DSYSCTL_INCLUDE_DESCR
     33   1.1        ad 
     34  1.59     joerg CWARNFLAGS.clang+=	-Wno-error=address-of-packed-member -Wno-error=constant-conversion
     35  1.59     joerg 
     36   1.1        ad # XXX until the kernel is fixed again...
     37   1.1        ad CFLAGS+=	-fno-strict-aliasing -Wno-pointer-sign
     38   1.1        ad 
     39  1.24      jmmv # XXX This is a workaround for platforms that have relative relocations
     40  1.24      jmmv # that, when relocated by the module loader, result in addresses that
     41  1.24      jmmv # overflow the size of the relocation (e.g. R_PPC_REL24 in powerpc).
     42  1.24      jmmv # The real solution to this involves generating trampolines for those
     43  1.24      jmmv # relocations inside the loader and removing this workaround, as the
     44  1.24      jmmv # resulting code would be much faster.
     45  1.63  jmcneill .if ${MACHINE_CPU} == "aarch64"
     46  1.63  jmcneill CFLAGS+=	-march=armv8-a+nofp+nosimd
     47  1.63  jmcneill .elif ${MACHINE_CPU} == "arm"
     48  1.41  jmcneill CFLAGS+=	-fno-common -fno-unwind-tables
     49  1.38     skrll .elif ${MACHINE_CPU} == "hppa"
     50  1.38     skrll CFLAGS+=	-mlong-calls
     51  1.24      jmmv .elif ${MACHINE_CPU} == "powerpc"
     52  1.44     joerg CFLAGS+=	${${ACTIVE_CC} == "gcc":? -mlongcall :}
     53  1.31      matt .elif ${MACHINE_CPU} == "vax"
     54  1.31      matt CFLAGS+=	-fno-pic
     55  1.45      matt .elif ${MACHINE_CPU} == "riscv"
     56  1.45      matt CFLAGS+=	-fPIC -Wa,-fno-pic
     57  1.54      matt .elif ${MACHINE_ARCH} == "mips64eb" && !defined(BSD_MK_COMPAT_FILE)
     58  1.53      matt CFLAGS+=	-mabi=64
     59  1.53      matt LDFLAGS+=	-Wl,-m,elf64btsmip
     60  1.54      matt .elif ${MACHINE_ARCH} == "mips64el" && !defined(BSD_MK_COMPAT_FILE)
     61  1.53      matt CFLAGS+=	-mabi=64
     62  1.53      matt LDFLAGS+=	-Wl,-m,elf64ltsmip
     63  1.24      jmmv .endif
     64  1.24      jmmv 
     65  1.43  nakayama .if ${MACHINE_CPU} == "sparc64"
     66  1.40    martin # force same memory model as rest of the kernel
     67  1.42     joerg CFLAGS+=	${${ACTIVE_CC} == "gcc":? -mcmodel=medlow :}
     68  1.42     joerg CFLAGS+=	${${ACTIVE_CC} == "clang":? -mcmodel=small :}
     69  1.40    martin .endif
     70  1.40    martin 
     71  1.28       mrg # evbppc needs some special help
     72  1.28       mrg .if ${MACHINE} == "evbppc"
     73  1.28       mrg 
     74  1.28       mrg . ifndef PPC_INTR_IMPL
     75  1.28       mrg PPC_INTR_IMPL=\"powerpc/intr.h\"
     76  1.28       mrg . endif
     77  1.28       mrg . ifndef PPC_PCI_MACHDEP_IMPL
     78  1.28       mrg PPC_PCI_MACHDEP_IMPL=\"powerpc/pci_machdep.h\"
     79  1.28       mrg . endif
     80  1.28       mrg CPPFLAGS+=      -DPPC_INTR_IMPL=${PPC_INTR_IMPL}
     81  1.28       mrg CPPFLAGS+=      -DPPC_PCI_MACHDEP_IMPL=${DPPC_PCI_MACHDEP_IMPL}
     82  1.28       mrg 
     83  1.28       mrg . ifdef PPC_IBM4XX
     84  1.28       mrg CPPFLAGS+=      -DPPC_IBM4XX
     85  1.28       mrg . elifdef PPC_BOOKE
     86  1.28       mrg CPPFLAGS+=      -DPPC_BOOKE
     87  1.28       mrg . else
     88  1.28       mrg CPPFLAGS+=      -DPPC_OEA
     89  1.28       mrg . endif
     90  1.28       mrg 
     91  1.28       mrg .endif
     92  1.28       mrg 
     93  1.28       mrg 
     94   1.1        ad _YKMSRCS=	${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}}
     95   1.1        ad DPSRCS+=	${_YKMSRCS}
     96   1.1        ad CLEANFILES+=	${_YKMSRCS}
     97   1.7        ad 
     98  1.21     skrll .if exists($S/../sys/modules/xldscripts/kmodule)
     99  1.47  christos KMODSCRIPTSRC=	$S/../sys/modules/xldscripts/kmodule
    100   1.7        ad .else
    101  1.47  christos KMODSCRIPTSRC=	${DESTDIR}/usr/libdata/ldscripts/kmodule
    102  1.47  christos .endif
    103  1.47  christos .if ${MKLDSCRIPT} == "yes"
    104  1.47  christos KMODSCRIPT=	kldscript
    105  1.52  christos MKLDSCRIPTSH=	
    106  1.47  christos .else
    107  1.47  christos KMODSCRIPT=	${KMODSCRIPTSRC}
    108   1.7        ad .endif
    109   1.1        ad 
    110   1.1        ad PROG?=		${KMOD}.kmod
    111   1.1        ad 
    112   1.1        ad ##### Build rules
    113   1.1        ad realall:	${PROG}
    114   1.1        ad 
    115  1.35      matt OBJS+=		${SRCS:N*.h:N*.sh:R:S/$/.o/g}
    116  1.35      matt 
    117   1.1        ad ${OBJS} ${LOBJS}: ${DPSRCS}
    118   1.1        ad 
    119  1.52  christos .if ${MKLDSCRIPT} == "yes"
    120  1.52  christos ${KMODSCRIPT}: ${KMODSCRIPTSRC} ${OBJS} $S/conf/mkldscript.sh
    121  1.52  christos 	@rm -f ${.TARGET}
    122  1.52  christos 	@OBJDUMP=${OBJDUMP} ${HOST_SH} $S/conf/mkldscript.sh \
    123  1.52  christos 	    -t ${KMODSCRIPTSRC} ${OBJS} > ${.TARGET}
    124  1.52  christos .endif
    125  1.52  christos 
    126  1.39      matt .if ${MACHINE_CPU} == "arm"
    127  1.39      matt # The solution to limited branch space involves generating trampolines for
    128  1.39      matt # those relocations while creating the module, as the resulting code will
    129  1.39      matt # be much faster and simplifies the loader.
    130  1.39      matt ARCHDIR=	$S/modules/arch/${MACHINE_CPU}
    131  1.39      matt ASM_H=		$S/arch/${MACHINE_CPU}/include/asm.h
    132  1.39      matt CLEANFILES+=	tmp.o tmp.S ${KMOD}_tmp.o ${KMOD}_tramp.o ${KMOD}_tramp.S
    133  1.39      matt ${KMOD}_tmp.o: ${OBJS} ${DPADD}
    134  1.39      matt 	${_MKTARGET_LINK}
    135  1.39      matt 	${LD} -r -o tmp.o ${OBJS}
    136  1.39      matt 	${LD} -r \
    137  1.39      matt 		`${OBJDUMP} --syms --reloc tmp.o | \
    138  1.39      matt 			${TOOL_AWK} -f ${ARCHDIR}/kmodwrap.awk` \
    139  1.39      matt 		 -o ${.TARGET} tmp.o
    140  1.39      matt 
    141  1.39      matt ${KMOD}_tramp.S: ${KMOD}_tmp.o ${ARCHDIR}/kmodtramp.awk ${ASM_H}
    142  1.39      matt 	${_MKTARGET_CREATE}
    143  1.39      matt 	${OBJDUMP} --syms --reloc ${KMOD}_tmp.o | \
    144  1.39      matt 		 ${TOOL_AWK} -f ${ARCHDIR}/kmodtramp.awk \
    145  1.39      matt 		 > tmp.S && \
    146  1.60  christos 	${MV} tmp.S ${.TARGET}
    147  1.39      matt 
    148  1.39      matt ${PROG}: ${KMOD}_tmp.o ${KMOD}_tramp.o
    149  1.39      matt 	${_MKTARGET_LINK}
    150  1.39      matt .if exists(${ARCHDIR}/kmodhide.awk)
    151  1.46  uebayasi 	${LD} -r -Map=${.TARGET}.map \
    152  1.46  uebayasi 	    -o tmp.o ${KMOD}_tmp.o ${KMOD}_tramp.o
    153  1.39      matt 	${OBJCOPY} \
    154  1.39      matt 		`${NM} tmp.o | ${TOOL_AWK} -f ${ARCHDIR}/kmodhide.awk` \
    155  1.39      matt 		tmp.o ${.TARGET} && \
    156  1.39      matt 	rm tmp.o
    157  1.39      matt .else
    158  1.46  uebayasi 	${LD} -r -Map=${.TARGET}.map \
    159  1.46  uebayasi 	    -o ${.TARGET} ${KMOD}_tmp.o ${KMOD}_tramp.o
    160  1.39      matt .endif
    161  1.39      matt .else
    162  1.52  christos ${PROG}: ${OBJS} ${DPADD} ${KMODSCRIPT}
    163  1.55      matt 	${_MKTARGET_LINK}
    164  1.26    martin 	${CC} ${LDFLAGS} -nostdlib -r -Wl,-T,${KMODSCRIPT},-d \
    165  1.46  uebayasi 		-Wl,-Map=${.TARGET}.map \
    166  1.22      matt 		-o ${.TARGET} ${OBJS}
    167  1.35      matt .endif
    168  1.58  christos .if defined(CTFMERGE)
    169  1.58  christos 	${CTFMERGE} ${CTFMFLAGS} -o ${.TARGET} ${OBJS}
    170  1.58  christos .endif
    171   1.1        ad 
    172   1.1        ad ##### Install rules
    173   1.1        ad .if !target(kmodinstall)
    174   1.7        ad .if !defined(KMODULEDIR)
    175  1.64  christos .if ${KERNEL_DIR:Uno} == "yes"
    176  1.64  christos KMODULEDIR=	${DESTDIR}/netbsd/modules/${KMOD}
    177  1.64  christos _INST_DIRS=	${DESTDIR}/netbsd
    178  1.64  christos _INST_DIRS+=	${DESTDIR}/netbsd/modules
    179  1.64  christos _INST_DIRS+=	${DESTDIR}/netbsd/modules/${KMOD}
    180  1.34  christos _OSRELEASE!=	${HOST_SH} $S/conf/osrelease.sh -k
    181  1.64  christos .else
    182   1.9        he # Ensure these are recorded properly in METALOG on unprived installes:
    183  1.28       mrg KMODULEARCHDIR?= ${MACHINE}
    184  1.28       mrg _INST_DIRS=	${DESTDIR}/stand/${KMODULEARCHDIR}
    185  1.28       mrg _INST_DIRS+=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}
    186  1.28       mrg _INST_DIRS+=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules
    187  1.28       mrg KMODULEDIR=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules/${KMOD}
    188  1.10    dyoung .endif
    189  1.64  christos .endif
    190  1.10    dyoung _PROG:=		${KMODULEDIR}/${PROG} # installed path
    191   1.1        ad 
    192   1.1        ad .if ${MKUPDATE} == "no"
    193   1.1        ad ${_PROG}! ${PROG}					# install rule
    194   1.1        ad .if !defined(BUILD) && !make(all) && !make(${PROG})
    195   1.1        ad ${_PROG}!	.MADE					# no build at install
    196   1.1        ad .endif
    197   1.1        ad .else
    198   1.1        ad ${_PROG}: ${PROG}					# install rule
    199   1.1        ad .if !defined(BUILD) && !make(all) && !make(${PROG})
    200   1.1        ad ${_PROG}:	.MADE					# no build at install
    201   1.1        ad .endif
    202   1.1        ad .endif
    203   1.1        ad 	${_MKTARGET_INSTALL}
    204  1.16   hubertf 	dirs=${_INST_DIRS:Q}; \
    205  1.16   hubertf 	for d in $$dirs; do \
    206   1.9        he 		${INSTALL_DIR} $$d; \
    207   1.9        he 	done
    208   1.7        ad 	${INSTALL_DIR} ${KMODULEDIR}
    209  1.15        ad 	${INSTALL_FILE} -o ${KMODULEOWN} -g ${KMODULEGRP} -m ${KMODULEMODE} \
    210   1.1        ad 		${.ALLSRC} ${.TARGET}
    211   1.1        ad 
    212   1.1        ad kmodinstall::	${_PROG}
    213   1.1        ad .PHONY:		kmodinstall
    214   1.1        ad .PRECIOUS:	${_PROG}				# keep if install fails
    215   1.1        ad 
    216   1.1        ad .undef _PROG
    217   1.1        ad .endif # !target(kmodinstall)
    218   1.1        ad 
    219   1.1        ad ##### Clean rules
    220  1.32       apb CLEANFILES+= a.out [Ee]rrs mklog core *.core ${PROG} ${OBJS} ${LOBJS}
    221  1.46  uebayasi CLEANFILES+= ${PROG}.map
    222  1.47  christos .if ${MKLDSCRIPT} == "yes"
    223  1.47  christos CLEANFILES+= kldscript
    224  1.47  christos .endif
    225   1.1        ad 
    226   1.1        ad ##### Custom rules
    227   1.1        ad lint: ${LOBJS}
    228   1.1        ad .if defined(LOBJS) && !empty(LOBJS)
    229   1.1        ad 	${LINT} ${LINTFLAGS} ${LDFLAGS:C/-L[  ]*/-L/Wg:M-L*} ${LOBJS} ${LDADD}
    230   1.1        ad .endif
    231   1.1        ad 
    232   1.1        ad ##### Pull in related .mk logic
    233  1.19       apb LINKSOWN?= ${KMODULEOWN}
    234  1.19       apb LINKSGRP?= ${KMODULEGRP}
    235  1.19       apb LINKSMODE?= ${KMODULEMODE}
    236   1.1        ad .include <bsd.man.mk>
    237   1.1        ad .include <bsd.links.mk>
    238   1.1        ad .include <bsd.dep.mk>
    239  1.32       apb .include <bsd.clean.mk>
    240   1.1        ad 
    241   1.1        ad .-include "$S/arch/${MACHINE_CPU}/include/Makefile.inc"
    242   1.1        ad .-include "$S/arch/${MACHINE}/include/Makefile.inc"
    243