From 2e3046757ac6f591a97d417d4c47c32cc01ec34c Mon Sep 17 00:00:00 2001 From: Anton Kuijsten Date: Mon, 17 Jun 2013 14:51:49 +0000 Subject: [PATCH] LLVM bitcode for almost the whole source tree Change-Id: Ibc6f89de112e0649c3a3bfe49c2baa2ed0746212 --- distrib/sets/lists/minix/mi | 1 + drivers/memory/Makefile | 2 + drivers/ramdisk/Makefile | 2 + etc/mtree/NetBSD.dist.base | 1 + kernel/Makefile | 2 + lib/libc/Makefile | 2 +- libexec/ld.elf_so/Makefile | 2 + share/mk/bsd.dep.mk | 15 +++++- share/mk/bsd.lib.mk | 46 +++++++++++++++++- share/mk/bsd.own.mk | 6 ++- share/mk/bsd.prog.mk | 54 +++++++++++++++++++++- share/mk/sys.mk | 2 +- sys/arch/i386/stand/boot/Makefile.boot | 1 + sys/arch/i386/stand/bootxx/Makefile.bootxx | 1 + sys/arch/i386/stand/lib/Makefile.inc | 2 + sys/sys/cdefs_elf.h | 2 + test/Makefile | 8 +++- 17 files changed, 139 insertions(+), 10 deletions(-) diff --git a/distrib/sets/lists/minix/mi b/distrib/sets/lists/minix/mi index 549c80ea6..bd4a436e3 100644 --- a/distrib/sets/lists/minix/mi +++ b/distrib/sets/lists/minix/mi @@ -987,6 +987,7 @@ ./usr/include/zconf.h minix-sys ./usr/include/zlib.h minix-sys ./usr/lib minix-sys +./usr/lib/bc minix-sys ./usr/lib/cawf minix-sys ./usr/lib/cawf/common minix-sys ./usr/lib/cawf/device.cf minix-sys diff --git a/drivers/memory/Makefile b/drivers/memory/Makefile index e8a6f5a27..4ca573381 100644 --- a/drivers/memory/Makefile +++ b/drivers/memory/Makefile @@ -2,6 +2,8 @@ .include +USE_BITCODE:=no + PROG= memory SRCS= memory.c imgrd.mfs OBJS= ${SRCS:N*.h:R:S/$/.o/g} diff --git a/drivers/ramdisk/Makefile b/drivers/ramdisk/Makefile index 280c77043..3c2e1759b 100644 --- a/drivers/ramdisk/Makefile +++ b/drivers/ramdisk/Makefile @@ -2,6 +2,8 @@ .include +USE_BITCODE:=no + install: all: diff --git a/etc/mtree/NetBSD.dist.base b/etc/mtree/NetBSD.dist.base index 675798897..ff4712b85 100644 --- a/etc/mtree/NetBSD.dist.base +++ b/etc/mtree/NetBSD.dist.base @@ -68,6 +68,7 @@ ./usr/include/ufs/mfs ./usr/include/ufs/ufs ./usr/lib +./usr/lib/bc ./usr/lib/cawf ./usr/lib/dict ./usr/lib/flex diff --git a/kernel/Makefile b/kernel/Makefile index f7623664f..c16eadcc9 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,6 +1,8 @@ # Makefile for kernel .include +USE_BITCODE:=no + BINDIR= /usr/sbin MAN= PROG= kernel diff --git a/lib/libc/Makefile b/lib/libc/Makefile index 6c5bbf8ea..3d4258749 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -81,7 +81,7 @@ SUBDIR+= pkgconfig .include "${.CURDIR}/net/Makefile.inc" .include "${.CURDIR}/nameser/Makefile.inc" .include "${.CURDIR}/nls/Makefile.inc" -.if (${MACHINE_ARCH} != "alpha") && (${ARCHSUBDIR} != "sparc64") +.if (${MACHINE_ARCH} != "alpha") && (${ARCHSUBDIR} != "sparc64") && ${USE_BITCODE:Uno} != "yes" .include "${.CURDIR}/quad/Makefile.inc" .endif .if (${USE_LIBTRE} == "yes") diff --git a/libexec/ld.elf_so/Makefile b/libexec/ld.elf_so/Makefile index 9c3551b62..8a2a16077 100644 --- a/libexec/ld.elf_so/Makefile +++ b/libexec/ld.elf_so/Makefile @@ -3,6 +3,8 @@ # NOTE: when changing ld.so, ensure that ldd still compiles. # +USE_BITCODE=no + WARNS?=4 # Minix Clang binaries references /libexec/ld.elf_so diff --git a/share/mk/bsd.dep.mk b/share/mk/bsd.dep.mk index 71de88ecf..5101b1b85 100644 --- a/share/mk/bsd.dep.mk +++ b/share/mk/bsd.dep.mk @@ -30,6 +30,11 @@ __DPSRCS.all+= ${SRCS:C/\.(c|m|s|S|C|cc|cpp|cxx)$/.d/} \ __DPSRCS.d= ${__DPSRCS.all:O:u:M*.d} __DPSRCS.notd= ${__DPSRCS.all:O:u:N*.d} +.if defined(__MINIX) +__DPSRCS.bcd= ${"${USE_BITCODE:Uno}" == "yes":? ${SRCS:M*.c:O:u:.c=.bcd}:} +__DPSRCS.d+= ${__DPSRCS.bcd} +.endif # defined(__MINIX) + .NOPATH: .depend ${__DPSRCS.d} .if !empty(__DPSRCS.d) # { @@ -41,7 +46,15 @@ ${__DPSRCS.d}: ${__DPSRCS.notd} ${DPSRCS} rm -f .depend ${MKDEP} -d -f ${.TARGET} -s ${MKDEP_SUFFIXES:Q} ${__DPSRCS.d} -.SUFFIXES: .d .s .S .c .C .cc .cpp .cxx .m +.SUFFIXES: .bcd .d .s .S .c .C .cc .cpp .cxx .m + +.if ${USE_BITCODE:Uno} == "yes" +.c.bcd: + ${_MKTARGET_CREATE} + ${MKDEP} -f ${.TARGET} -s .bc -- ${MKDEPFLAGS} \ + ${CFLAGS:C/-([IDU])[ ]*/-\1/Wg:M-[IDU]*} \ + ${CPPFLAGS} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} +.endif .c.d: ${_MKTARGET_CREATE} diff --git a/share/mk/bsd.lib.mk b/share/mk/bsd.lib.mk index c951456f1..1dc2433d5 100644 --- a/share/mk/bsd.lib.mk +++ b/share/mk/bsd.lib.mk @@ -210,6 +210,16 @@ CTFFLAGS+= -g .endif .endif +.if ${USE_BITCODE:Uno} == "yes" +.S.bc: ${.TARGET:.bc=.o} + rm -f ${.TARGET} + ln ${.TARGET:.bc=.o} ${.TARGET} +.c.bc: + ${_MKTARGET_COMPILE} + ${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} -o - -Qunused-arguments -E -D"__weak_alias(X, Y)=__weak_alias2( __WEAK__ ## X, __WEAK__ ## Y)" \ + | sed 's/__WEAK__//g' \ + | ${COMPILE.c} -flto ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} -x c - -o ${.TARGET} -D"STRINGIFY(a)=#a" -D"__weak_alias2(X, Y)=_Pragma(STRINGIFY(weak X = Y))" +.endif # ${USE_BITCODE:Uno} == "yes" .c.o: ${_MKTARGET_COMPILE} ${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} -o ${.TARGET} @@ -491,6 +501,10 @@ SOBJS= _YLSRCS= ${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}} +.if ${USE_BITCODE:Uno} == "yes" +_LIBS+=lib${LIB}_bc.a +.endif + .NOPATH: ${ALLOBJS} ${_LIBS} ${_YLSRCS} realall: ${SRCS} ${ALLOBJS:O} ${_LIBS} ${_LIB.debug} @@ -532,6 +546,14 @@ CLEANFILES+= ${_YLSRCS} ${STOBJS} ${POBJS} ${GOBJS} ${SOBJS} ${LOBJS}: ${DPSRCS} +.if ${USE_BITCODE:Uno} == "yes" +BCOBJS+=${OBJS:.o=.bc} +lib${LIB}_bc.a:: ${BCOBJS} + ${_MKTARGET_BUILD} + rm -f ${.TARGET} + ${AR} ${_ARFL} ${.TARGET} `NM=${NM} ${LORDER} ${.ALLSRC:M*bc} | ${TSORT}` +.endif + lib${LIB}.a:: ${STOBJS} __archivebuild lib${LIB}_p.a:: ${POBJS} __archivebuild @@ -588,7 +610,8 @@ lib${LIB}.so.${SHLIB_FULLVERSION}: ${SOLIB} ${DPADD} ${DPLIBC} \ rm -f lib${LIB}.so.${SHLIB_FULLVERSION} ${LIBCC} ${LDLIBC} -Wl,-x -shared ${SHLIB_SHFLAGS} ${_LDFLAGS.lib${LIB}} \ -o ${.TARGET} ${_LIBLDOPTS} \ - -Wl,--whole-archive ${SOLIB} -Wl,--no-whole-archive ${_LDADD.lib${LIB}} + -Wl,--whole-archive ${SOLIB} -Wl,--no-whole-archive ${_LDADD.lib${LIB}} \ + ${${USE_BITCODE:Uno} == "yes":? -Wl,-plugin=/usr/pkg/lib/bfd-plugins/LLVMgold.so -Wl,-plugin-opt=-disable-opt:} # We don't use INSTALL_SYMLINK here because this is just # happening inside the build directory/objdir. XXX Why does # this spend so much effort on libraries that aren't live??? XXX @@ -637,16 +660,18 @@ lint: ${LOBJS} # use it here mimics the way it's used by the clean target in # bsd.clean.mk. # -clean: libclean1 libclean2 libclean3 libclean4 libclean5 +clean: libclean1 libclean2 libclean3 libclean4 libclean5 libclean6 libclean1: .PHONY .MADE __cleanuse LIBCLEANFILES1 libclean2: .PHONY .MADE __cleanuse LIBCLEANFILES2 libclean3: .PHONY .MADE __cleanuse LIBCLEANFILES3 libclean4: .PHONY .MADE __cleanuse LIBCLEANFILES4 libclean5: .PHONY .MADE __cleanuse LIBCLEANFILES5 +libclean6: .PHONY .MADE __cleanuse LIBCLEANFILES6 CLEANFILES+= a.out [Ee]rrs mklog *.core # core conflicts with core/ in lib/liblwip .if !defined(__MINIX) CLEANFILES+=core +LIBCLEANFILES6+= lib${LIB}_bc.a ${BCOBJS} ${BCOBJS:=.tmp} .endif LIBCLEANFILES1+= lib${LIB}.a ${STOBJS} ${STOBJS:=.tmp} LIBCLEANFILES2+= lib${LIB}_p.a ${POBJS} ${POBJS:=.tmp} @@ -659,6 +684,23 @@ LIBCLEANFILES5+= llib-l${LIB}.ln ${LOBJS} # Make sure it gets defined, in case MKPIC==no && MKLINKLIB==no libinstall:: +.if ${USE_BITCODE:Uno} == "yes" +libinstall:: ${DESTDIR}${LIBDIR}/bc/lib${LIB}.a +.PRECIOUS: ${DESTDIR}${LIBDIR}/bc/lib${LIB}.a + +.if ${MKUPDATE} == "no" +.if !defined(BUILD) && !make(all) && !make(lib${LIB}_bc.a) +${DESTDIR}${LIBDIR}/bc/lib${LIB}.a! .MADE +.endif +${DESTDIR}${LIBDIR}/bc/lib${LIB}.a! lib${LIB}_bc.a __archiveinstall +.else +.if !defined(BUILD) && !make(all) && !make(lib${LIB}_bc.a) +${DESTDIR}${LIBDIR}/bc/lib${LIB}.a: .MADE +.endif +${DESTDIR}${LIBDIR}/bc/lib${LIB}.a: lib${LIB}_bc.a __archiveinstall +.endif +.endif # ${USE_BITCODE:Uno} == "yes" + .if ${MKLINKLIB} != "no" && ${MKSTATICLIB} != "no" libinstall:: ${DESTDIR}${LIBDIR}/lib${LIB}.a .PRECIOUS: ${DESTDIR}${LIBDIR}/lib${LIB}.a diff --git a/share/mk/bsd.own.mk b/share/mk/bsd.own.mk index 0e6dc81ed..b5a46c88b 100644 --- a/share/mk/bsd.own.mk +++ b/share/mk/bsd.own.mk @@ -56,6 +56,8 @@ USETOOLS?= never HAVE_GOLD?= ${_HAVE_GOLD} CFLAGS+= -DHAVE_GOLD=${_HAVE_GOLD} AFLAGS+= -DHAVE_GOLD=${_HAVE_GOLD} +. else + MKBITCODE:=no . endif . endif # ${_HAVE_GOLD:U} == "" . endif # ${USETOOLS:Uno} != "yes" @@ -984,7 +986,7 @@ _MKVARS.no= \ #MINIX-specific vars _MKVARS.no+= \ - MKIMAGEONLY MKSMALL + MKIMAGEONLY MKSMALL MKBITCODE .if (${MACHINE_ARCH} == "earm") _MKVARS.no+= \ MKWATCHDOG MKACPI MKAPIC MKDEBUGREG MKINSTALLBOOT MKPCI @@ -1141,7 +1143,7 @@ ${var}?= yes #MINIX-specific vars .for var in \ USE_WATCHDOG USE_ACPI USE_APIC USE_MCONTEXT USE_DEBUGREG USE_SYSDEBUG \ - USE_LIVEUPDATE USE_STATECTL USE_TRACE USE_PCI + USE_LIVEUPDATE USE_STATECTL USE_TRACE USE_PCI USE_BITCODE .if (${${var:S/USE_/MK/}} == "no") ${var}:= no .else diff --git a/share/mk/bsd.prog.mk b/share/mk/bsd.prog.mk index 237b9b856..6587b1da1 100644 --- a/share/mk/bsd.prog.mk +++ b/share/mk/bsd.prog.mk @@ -315,9 +315,15 @@ _CCLINK.${_P}= ${CXX} ${_CCLINKFLAGS} .endfor # Language-independent definitions. +.if defined(__MINIX) && ${USE_BITCODE:Uno} == "yes" +CFLAGS+= -flto +.endif + .for _P in ${PROGS} ${PROGS_CXX} # { .if defined(__MINIX) && ${HAVE_GOLD:U} != "" + + . if ${LD_STATIC:U} != "-static" GOLDLINKERSCRIPT?= ${LDS_DYNAMIC_BIN} . else @@ -418,13 +424,57 @@ CLEANFILES+= ${_P}.d ${OBJS.${_P}} ${LOBJS.${_P}}: ${DPSRCS} -${_P}: .gdbinit ${LIBCRT0} ${OBJS.${_P}} ${LIBC} ${LIBCRTBEGIN} ${LIBCRTEND} ${_DPADD.${_P}} +.if defined(__MINIX) +CLEANFILES+= ${_P}.opt.bcl ${_P}.bcl ${_P}.bcl.o +.endif # defined(__MINIX) + .if !commands(${_P}) +.if ${USE_BITCODE:Uno} == "yes" + +.if ${LLVM_PASS:UNO_PASS} != "NO_PASS" +_LLVM_PASS_ARGS= -load ${LLVM_PASS} ${LLVM_PASS_ARGS} +_TARGET_BCL= ${_P}.opt.bcl +.else +_TARGET_BCL= ${_P}.bcl +.endif # ${LLVM_PASS:UNO_PASS} != "NO_PASS" + +${_P}.bcl: .gdbinit ${LIBCRT0} ${OBJS.${_P}} ${LIBC} ${LIBCRTBEGIN} ${LIBCRTEND} ${_DPADD.${_P}} + ${_MKTARGET_LINK} + ${_CCLINK.${_P}} \ + -o ${.TARGET} \ + -nostartfiles \ + -L/usr/lib/bc \ + ${OBJS.${_P}} ${LLVM_LINK_ARGS} ${_LDADD.${_P}:N-shared} \ + ${_LDSTATIC.${_P}} \ + -Wl,-r \ + -Wl,-plugin=/usr/pkg/lib/bfd-plugins/LLVMgold.so,-plugin-opt=-disable-opt,-plugin-opt=-disable-inlining,-plugin-opt=emit-llvm + +${_P}.opt.bcl: ${_P}.bcl ${LLVM_PASS} + ${_MKTARGET_LINK} + opt -disable-opt ${_LLVM_PASS_ARGS} -o ${.TARGET} ${_P}.bcl + +${_P}.bcl.o: ${_TARGET_BCL} + ${_MKTARGET_LINK} + llc -O1 -filetype=obj -o ${.TARGET} ${.ALLSRC} + +${_P}: ${_P}.bcl.o + ${_MKTARGET_LINK} + ${_CCLINK.${_P}} \ + -o ${.TARGET} \ + -L/usr/lib/bc \ + ${.TARGET}.bcl.o ${_LDADD.${_P}} \ + ${_LDSTATIC.${_P}} \ + -Wl,--script,${GOLDLINKERSCRIPT.${_P}} \ + -Wl,-plugin=/usr/pkg/lib/bfd-plugins/LLVMgold.so,-plugin-opt=-disable-opt,-plugin-opt=-disable-inlining + +.else +${_P}: .gdbinit ${LIBCRT0} ${OBJS.${_P}} ${LIBC} ${LIBCRTBEGIN} ${LIBCRTEND} ${_DPADD.${_P}} ${_MKTARGET_LINK} ${_CCLINK.${_P}} \ ${_LDFLAGS.${_P}} ${_LDSTATIC.${_P}} -o ${.TARGET} \ ${OBJS.${_P}} ${_LDADD.${_P}} \ ${_PROGLDOPTS} +.endif # ${USE_BITCODE:Uno} == "yes" .if defined(CTFMERGE) ${CTFMERGE} ${CTFMFLAGS} -o ${.TARGET} ${OBJS.${_P}} .endif @@ -434,6 +484,8 @@ ${_P}: .gdbinit ${LIBCRT0} ${OBJS.${_P}} ${LIBC} ${LIBCRTBEGIN} ${LIBCRTEND} ${_ .if ${MKSTRIPIDENT} != "no" ${OBJCOPY} -R .ident ${.TARGET} .endif +.else +${_P}: .gdbinit ${LIBCRT0} ${OBJS.${_P}} ${LIBC} ${LIBCRTBEGIN} ${LIBCRTEND} ${_DPADD.${_P}} .endif # !commands(${_P}) .endif # USE_COMBINE diff --git a/share/mk/sys.mk b/share/mk/sys.mk index 88a9ec20d..8f1a4419e 100644 --- a/share/mk/sys.mk +++ b/share/mk/sys.mk @@ -16,7 +16,7 @@ CPP?= /usr/lib/cpp .endif # defined(__MINIX) unix?= We run NetBSD. -.SUFFIXES: .a .o .ln .s .S .c .cc .cpp .cxx .C .f .F .r .p .l .y .sh +.SUFFIXES: .a .o .bc .ln .s .S .c .cc .cpp .cxx .C .f .F .r .p .l .y .sh .LIBS: .a diff --git a/sys/arch/i386/stand/boot/Makefile.boot b/sys/arch/i386/stand/boot/Makefile.boot index 5390ed184..ecf055221 100644 --- a/sys/arch/i386/stand/boot/Makefile.boot +++ b/sys/arch/i386/stand/boot/Makefile.boot @@ -124,6 +124,7 @@ LIBKERN= ${KERNLIB} # When using gold linker, specify the linker script GOLDLINKERSCRIPT?= ${LDS_N} +USE_BITCODE=no # use MINIX minc LIBKERN= ${DESTDIR}/usr/lib/libminc.a diff --git a/sys/arch/i386/stand/bootxx/Makefile.bootxx b/sys/arch/i386/stand/bootxx/Makefile.bootxx index e5eaee559..3bf1916cb 100644 --- a/sys/arch/i386/stand/bootxx/Makefile.bootxx +++ b/sys/arch/i386/stand/bootxx/Makefile.bootxx @@ -129,6 +129,7 @@ LIBKERN= ${KERNLIB} # When using gold linker, specify the linker script GOLDLINKERSCRIPT?= ${LDS_N} +USE_BITCODE=no # use MINIX minc LIBKERN= ${DESTDIR}/usr/lib/libminc.a diff --git a/sys/arch/i386/stand/lib/Makefile.inc b/sys/arch/i386/stand/lib/Makefile.inc index 754a38525..b4a408244 100644 --- a/sys/arch/i386/stand/lib/Makefile.inc +++ b/sys/arch/i386/stand/lib/Makefile.inc @@ -13,6 +13,8 @@ # building. See library's Makefile for more details about # supported flags and their default values. +USE_BITCODE=no + # Default values: I386DST?= ${.OBJDIR}/lib/i386 diff --git a/sys/sys/cdefs_elf.h b/sys/sys/cdefs_elf.h index 96cec8e24..46e8a2532 100644 --- a/sys/sys/cdefs_elf.h +++ b/sys/sys/cdefs_elf.h @@ -56,9 +56,11 @@ __asm(".global " _C_LABEL_STRING(#alias) "\n" \ _C_LABEL_STRING(#alias) " = " _C_LABEL_STRING(#sym)); +#if defined(__minix) && !defined(__weak_alias) /* For bitcode support */ #define __weak_alias(alias,sym) \ __asm(".weak " _C_LABEL_STRING(#alias) "\n" \ _C_LABEL_STRING(#alias) " = " _C_LABEL_STRING(#sym)); +#endif /* Do not use __weak_extern, use __weak_reference instead */ #define __weak_extern(sym) \ diff --git a/test/Makefile b/test/Makefile index 191c5acd2..58a1f7add 100644 --- a/test/Makefile +++ b/test/Makefile @@ -39,7 +39,8 @@ LDADD.test72+= -lminixfs PROGS += testvm OBJS.testvm+= testcache.o -LDADD.testvm+= -lsys -ltimers -lminlib -static +LDFLAGS.testvm+= -static +LDADD.testvm+= -lsys -ltimers -lminlib FILES += testvm.conf @@ -79,7 +80,10 @@ LDSTATIC= -dynamic # use the shared linkerscript for mod when using the gold linker GOLDLINKERSCRIPT.mod?= ${${HAVE_GOLD:Uno} != "no":? ${LDS_SHARED_LIB} :} -GOLDLINKERSCRIPT= +LDADD.mod+= ${${USE_BITCODE:Uno} != "no":? -shared:} +# test57loop.S is not linked into the .bcl file. +# This way, we can link it in when linking the final binary +LDADD.test57+= ${${USE_BITCODE:Uno} != "no":? test57loop.o -Wl,-allow-multiple-definition:} # Add test that must be linked dynamically, and its dynamically loaded # module