From d824b54d823a2d10ddcb99801cbac243e7a80ee7 Mon Sep 17 00:00:00 2001 From: Arun Thomas Date: Fri, 10 Dec 2010 22:20:12 +0000 Subject: [PATCH] csu/build support for ELF --- Makefile | 29 ++++++--- boot/Makefile | 2 +- lib/Makefile | 12 +++- lib/csu/Makefile | 17 ++---- lib/csu/i386-aout/Makefile | 12 ++++ lib/csu/{i386 => i386-aout}/crtso.S | 0 lib/csu/i386-elf/Makefile | 29 +++++++++ lib/csu/i386-elf/crt1_c.c | 94 +++++++++++++++++++++++++++++ lib/csu/i386-elf/crt1_s.S | 51 ++++++++++++++++ lib/csu/i386-elf/crti.S | 41 +++++++++++++ lib/csu/i386-elf/crtn.S | 35 +++++++++++ lib/elf_build.sh | 8 +++ lib/libc/arch/i386/rts/brksize.S | 8 +++ share/mk/bsd.own.mk | 13 +++- share/mk/sys.mk | 5 ++ 15 files changed, 334 insertions(+), 22 deletions(-) create mode 100644 lib/csu/i386-aout/Makefile rename lib/csu/{i386 => i386-aout}/crtso.S (100%) create mode 100644 lib/csu/i386-elf/Makefile create mode 100644 lib/csu/i386-elf/crt1_c.c create mode 100644 lib/csu/i386-elf/crt1_s.S create mode 100644 lib/csu/i386-elf/crti.S create mode 100644 lib/csu/i386-elf/crtn.S create mode 100755 lib/elf_build.sh diff --git a/Makefile b/Makefile index b9856cb58..ab4b1e01a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ # Master Makefile to compile everything in /usr/src except the system. +.include + MAKE=make usage: @@ -33,7 +35,11 @@ usage: .if ${COMPILER_TYPE} == "ack" world: mkfiles includes depend libraries install etcforce .elif ${COMPILER_TYPE} == "gnu" +.if ${OBJECT_FMT} == "a.out" world: mkfiles includes depend gnu-libraries install etcforce +.elif ${OBJECT_FMT} == "ELF" +world: mkfiles elf-includes depend elf-libraries install etcforce +.endif .endif mkfiles: @@ -60,43 +66,52 @@ gnu-libraries: gnu-includes clang-libraries: includes $(MAKE) -C lib build_clang +MKHEADERS443_ELF=/usr/gnu_cross/libexec/gcc/i386-pc-minix3/4.4.3/install-tools/mkheaders +elf-includes: includes + cp -r /usr/include/* /usr/gnu_cross/i386-pc-minix3/sys-include + SHELL=/bin/sh; if [ -f $(MKHEADERS443_ELF) ] ; then sh -e $(MKHEADERS443_ELF) ; fi + +elf-libraries: elf-includes + $(MAKE) -C lib build_elf + commands: includes libraries $(MAKE) -C commands all -depend:: +depend: $(MAKE) -C boot depend $(MAKE) -C commands depend $(MAKE) -C kernel depend $(MAKE) -C servers depend $(MAKE) -C drivers depend -etcfiles:: +etcfiles: $(MAKE) -C etc install -etcforce:: +etcforce: $(MAKE) -C etc installforce -all:: +all: $(MAKE) -C boot all $(MAKE) -C commands all $(MAKE) -C tools all -install:: +install: $(MAKE) -C boot install $(MAKE) -C man install makedb $(MAKE) -C commands install $(MAKE) -C share install $(MAKE) -C tools install -clean:: +clean: $(MAKE) -C boot clean $(MAKE) -C commands clean $(MAKE) -C tools clean $(MAKE) -C lib clean_gnu $(MAKE) -C lib clean_ack + $(MAKE) -C lib clean_elf $(MAKE) -C test clean -cleandepend:: +cleandepend: $(MAKE) -C boot cleandepend $(MAKE) -C commands cleandepend $(MAKE) -C tools cleandepend diff --git a/boot/Makefile b/boot/Makefile index b2b9292ed..07571cbe3 100644 --- a/boot/Makefile +++ b/boot/Makefile @@ -1,7 +1,7 @@ # Makefile for the boot monitor package. # XXX: Can only be built with ACK currently -CC:=${CC:C/^gcc/cc/} +CC:=${CC:C/^.*gcc/cc/} COMPILER_TYPE:=ack PROGS= bootblock cdbootblock boot masterboot \ diff --git a/lib/Makefile b/lib/Makefile index 7fc948460..80e1baa05 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,6 +1,6 @@ .include -SUBDIR= csu libc libcurses libdriver libnetdriver libend libedit libm libsys \ +SUBDIR= csu libc libcurses libdriver libnetdriver libedit libm libsys \ libtimers libutil libbz2 libl libhgfs libz libfetch libarchive \ libvtreefs libaudiodriver libmthread libexec @@ -8,6 +8,10 @@ SUBDIR= csu libc libcurses libdriver libnetdriver libend libedit libm libsys \ SUBDIR+= ack/libd ack/libe ack/libfp ack/liby .endif +.if ${OBJECT_FMT} == "a.out" +SUBDIR+= libend +.endif + .include build_ack: @@ -19,8 +23,14 @@ build_gnu: build_clang: sh clang_build.sh obj depend all install +build_elf: + sh elf_build.sh obj depend all install + clean_ack: sh ack_build.sh clean clean_gnu: sh gnu_build.sh clean + +clean_elf: + sh elf_build.sh clean diff --git a/lib/csu/Makefile b/lib/csu/Makefile index 78c737fb3..a34bd119d 100644 --- a/lib/csu/Makefile +++ b/lib/csu/Makefile @@ -1,14 +1,9 @@ .include -.PATH: ${.CURDIR}/${ARCH} +.if ${OBJECT_FMT} == "a.out" +SUBDIR=${ARCH}-aout +.elif ${OBJECT_FMT} == "ELF" +SUBDIR=${ARCH}-elf +.endif -SRCS= crtso.S -OBJS= crtso.o - -realall: ${OBJS} - -FILES=${OBJS} -FILESDIR=${LIBDIR} -CLEANFILES=${OBJS} - -.include +.include diff --git a/lib/csu/i386-aout/Makefile b/lib/csu/i386-aout/Makefile new file mode 100644 index 000000000..558fd6673 --- /dev/null +++ b/lib/csu/i386-aout/Makefile @@ -0,0 +1,12 @@ +.include + +SRCS= crtso.S +OBJS= crtso.o + +realall: ${OBJS} + +FILES=${OBJS} +FILESDIR=${LIBDIR} +CLEANFILES=${OBJS} + +.include diff --git a/lib/csu/i386/crtso.S b/lib/csu/i386-aout/crtso.S similarity index 100% rename from lib/csu/i386/crtso.S rename to lib/csu/i386-aout/crtso.S diff --git a/lib/csu/i386-elf/Makefile b/lib/csu/i386-elf/Makefile new file mode 100644 index 000000000..54a645dc4 --- /dev/null +++ b/lib/csu/i386-elf/Makefile @@ -0,0 +1,29 @@ +.include + +SRCS= crti.S crtn.S +OBJS= ${SRCS:N*.h:R:S/$/.o/g} gcrt1.o crt1.o Scrt1.o + +realall: ${OBJS} + +FILES=${OBJS} +FILESDIR=${LIBDIR} +CLEANFILES=${OBJS} crt1_c.o crt1_s.o gcrt1_c.o Scrt1_c.o + +gcrt1_c.o: crt1_c.c + ${CC} ${CFLAGS} -DGCRT -c -o gcrt1_c.o ${.CURDIR}/crt1_c.c + +gcrt1.o: gcrt1_c.o crt1_s.o + ${LD} ${LDFLAGS} -o gcrt1.o -r crt1_s.o gcrt1_c.o + +crt1.o: crt1_c.o crt1_s.o + ${LD} ${LDFLAGS} -o crt1.o -r crt1_s.o crt1_c.o + ${OBJCOPY} --localize-symbol _start1 crt1.o + +Scrt1_c.o: crt1_c.c + ${CC} ${CFLAGS} -fPIC -DPIC -c -o Scrt1_c.o ${.CURDIR}/crt1_c.c + +Scrt1.o: Scrt1_c.o crt1_s.o + ${LD} ${LDFLAGS} -o Scrt1.o -r crt1_s.o Scrt1_c.o + ${OBJCOPY} --localize-symbol _start1 Scrt1.o + +.include diff --git a/lib/csu/i386-elf/crt1_c.c b/lib/csu/i386-elf/crt1_c.c new file mode 100644 index 000000000..25dc5acb1 --- /dev/null +++ b/lib/csu/i386-elf/crt1_c.c @@ -0,0 +1,94 @@ +/* LINTLIBRARY */ +/*- + * Copyright 1996-1998 John D. Polstra. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef lint +#ifndef __GNUC__ +#error "GCC is needed to compile this file" +#endif +#endif /* lint */ + +#include + +extern int _DYNAMIC; +#pragma weak _DYNAMIC + +typedef void (*fptr)(void); + +extern void _fini(void); +extern void _init(void); +extern int main(int, char **, char **); +extern void _start(char *, ...); + +#ifdef GCRT +extern void _mcleanup(void); +extern void monstartup(void *, void *); +extern int eprol; +extern int etext; +#endif + +char **environ; +const char *__progname = ""; + +__dead void _start1(fptr, int, char *[]); + +/* The entry function, C part. */ +__dead void +_start1(fptr cleanup, int argc, char *argv[]) +{ + char **env; + const char *s; + + env = argv + argc + 1; + environ = env; + if (argc > 0 && argv[0] != NULL) { + __progname = argv[0]; + for (s = __progname; *s != '\0'; s++) + if (*s == '/') + __progname = s + 1; + } + +#if 0 + if (&_DYNAMIC != NULL) + atexit(cleanup); + else + _init_tls(); +#endif + +#ifdef GCRT + atexit(_mcleanup); +#endif + atexit(_fini); +#ifdef GCRT + monstartup(&eprol, &etext); +__asm__("eprol:"); +#endif + _init(); + exit( main(argc, argv, env) ); +} + +__asm(".hidden _start1"); diff --git a/lib/csu/i386-elf/crt1_s.S b/lib/csu/i386-elf/crt1_s.S new file mode 100644 index 000000000..e101330e2 --- /dev/null +++ b/lib/csu/i386-elf/crt1_s.S @@ -0,0 +1,51 @@ +/*- + * Copyright 2009 Konstantin Belousov. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + + + .text + .align 4 + .globl _start + .type _start, @function +_start: + .cfi_startproc + xorl %ebp,%ebp + pushl %ebp + .cfi_def_cfa_offset 4 + movl %esp,%ebp + .cfi_offset %ebp,-8 + .cfi_def_cfa_register %ebp + andl $0xfffffff0,%esp # align stack + leal 8(%ebp),%eax + pushl %eax # argv + pushl 4(%ebp) # argc + pushl %edx # rtld cleanup + call _start1 + int3 + .cfi_endproc + .size _start, . - _start + + .ident "$MINIX$" diff --git a/lib/csu/i386-elf/crti.S b/lib/csu/i386-elf/crti.S new file mode 100644 index 000000000..f06517c88 --- /dev/null +++ b/lib/csu/i386-elf/crti.S @@ -0,0 +1,41 @@ +/*- + * Copyright 1996, 1997, 1998, 2000 John D. Polstra. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + .section .init,"ax",@progbits + .align 4 + .globl _init + .type _init,@function +_init: + sub $12,%esp /* re-align stack pointer */ + + .section .fini,"ax",@progbits + .align 4 + .globl _fini + .type _fini,@function +_fini: + sub $12,%esp /* re-align stack pointer */ + + .section .rodata +.ascii "$MINIX$\0" diff --git a/lib/csu/i386-elf/crtn.S b/lib/csu/i386-elf/crtn.S new file mode 100644 index 000000000..ae0754726 --- /dev/null +++ b/lib/csu/i386-elf/crtn.S @@ -0,0 +1,35 @@ +/*- + * Copyright 1996, 1997, 1998, 2000 John D. Polstra. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + .section .init,"ax",@progbits + add $12,%esp + ret + + .section .fini,"ax",@progbits + add $12,%esp + ret + + .section .rodata +.ascii "$MINIX$\0" diff --git a/lib/elf_build.sh b/lib/elf_build.sh new file mode 100755 index 000000000..2d4d5fe3f --- /dev/null +++ b/lib/elf_build.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +export CC=i386-pc-minix3-gcc +export COMPILER_TYPE=gnu +export MAKEOBJDIR=obj-elf +export PATH=$PATH:/usr/gnu_cross/bin + +make $@ diff --git a/lib/libc/arch/i386/rts/brksize.S b/lib/libc/arch/i386/rts/brksize.S index 43b3033b2..64ec062d1 100644 --- a/lib/libc/arch/i386/rts/brksize.S +++ b/lib/libc/arch/i386/rts/brksize.S @@ -1,5 +1,13 @@ +#if defined(__ELF__) +.globl _end +.globl _brksize + +.data +_brksize: .long _end +#else .globl __brksize .data .extern endbss, __brksize __brksize: .long endbss +#endif diff --git a/share/mk/bsd.own.mk b/share/mk/bsd.own.mk index f3c64e1bc..f81646817 100644 --- a/share/mk/bsd.own.mk +++ b/share/mk/bsd.own.mk @@ -483,8 +483,6 @@ INFOMODE?= ${NONBINMODE} #LIBDIR?= /usr/lib .if ${COMPILER_TYPE} == "ack" LIBDIR?= /usr/lib/i386 -.elif ${COMPILER_TYPE} == "gnu" -LIBDIR?= /usr/lib .endif LINTLIBDIR?= /usr/libdata/lint @@ -533,7 +531,18 @@ DEBUGMODE?= ${NONBINMODE} # All platforms are ELF. # #OBJECT_FMT= ELF +.if !empty(CC:Mi386-pc-minix3-gcc) +OBJECT_FMT= ELF +.else OBJECT_FMT= a.out +.endif +.if ${COMPILER_TYPE} == "gnu" +.if ${OBJECT_FMT} == "a.out" +LIBDIR?= /usr/lib +.elif ${OBJECT_FMT} == "ELF" +LIBDIR?= /usr/gnu_cross/i386-pc-minix3/lib +.endif +.endif # # If this platform's toolchain is missing, we obviously cannot build it. diff --git a/share/mk/sys.mk b/share/mk/sys.mk index f44905974..1488fb2cb 100644 --- a/share/mk/sys.mk +++ b/share/mk/sys.mk @@ -233,4 +233,9 @@ AR=ar .elif !empty(CC:Mclang) COMPILER_TYPE=gnu AR=ar +.elif !empty(CC:Mi386-pc-minix3-gcc) +COMPILER_TYPE=gnu +AR=i386-pc-minix3-ar +LD=i386-pc-minix3-ld +OBJCOPY=i386-pc-minix3-objcopy .endif -- 2.44.0