From fd817f02f0144fc73e6a3fc0742ab6ec1eefc266 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Fri, 26 Jun 2015 20:49:43 +0200 Subject: [PATCH] add newlib as C library for HermitCore apps --- hermit/Makefile | 41 ++++++- hermit/newlib/Makefile | 37 ++++++ hermit/newlib/examples/Makefile | 57 +++++++++ hermit/newlib/examples/hello.c | 47 ++++++++ hermit/newlib/examples/jacobi.c | 205 ++++++++++++++++++++++++++++++++ hermit/tools/Makefile | 3 + 6 files changed, 385 insertions(+), 5 deletions(-) create mode 100644 hermit/newlib/Makefile create mode 100644 hermit/newlib/examples/Makefile create mode 100644 hermit/newlib/examples/hello.c create mode 100644 hermit/newlib/examples/jacobi.c diff --git a/hermit/Makefile b/hermit/Makefile index be77d5039..dd770c432 100644 --- a/hermit/Makefile +++ b/hermit/Makefile @@ -40,9 +40,9 @@ QEMUSERIALFLAGS = -device pci-serial,chardev=tS0 \ INCLUDE = -I$(TOPDIR)/include -I$(TOPDIR)/arch/$(ARCH)/include # Compiler options for final code -CFLAGS = -g -m64 -Wall -O2 -mno-red-zone -fstrength-reduce -fomit-frame-pointer -finline-functions -ffreestanding -nostdinc -fno-stack-protector $(INCLUDE) +CFLAGS = -D_HERMIT -g -m64 -Wall -O2 -mno-red-zone -fstrength-reduce -fomit-frame-pointer -finline-functions -ffreestanding -nostdinc -fno-stack-protector $(INCLUDE) # Compiler options for debugging -debug debug-eclipse : CFLAGS = -g -O0 -m64 -Wall -fno-builtin -DWITH_FRAME_POINTER -nostdinc -mno-red-zone -fno-stack-protector $(INCLUDE) +debug debug-eclipse : CFLAGS = -D_HERMIT -g -O0 -m64 -Wall -fno-builtin -DWITH_FRAME_POINTER -nostdinc -mno-red-zone -fno-stack-protector $(INCLUDE) AR = ar ARFLAGS = rsv RM = rm -rf @@ -51,6 +51,12 @@ STRIP_DEBUG = --strip-debug KEEP_DEBUG = --only-keep-debug OUTPUT_FORMAT = -O elf32-i386 +CFLAGS_FOR_NEWLIB = $(STACKPROT) +LDFLAGS_FOR_NEWLIB = +NASMFLAGS_FOR_NEWLIB = -felf64 +CFLAGS_FOR_TOOLS = -O2 -Wall +LDFLAGS_FOR_TOOLS = + # Prettify output V = 0 ifeq ($V,0) @@ -58,9 +64,30 @@ ifeq ($V,0) P = > /dev/null endif -default: $(NAME).elf +default: all -all: $(NAME).elf +all: newlib $(NAME).elf + +newlib: + $Q$(MAKE) ARCH=$(ARCH) \ + LDFLAGS="$(LDFLAGS_FOR_NEWLIB)" \ + CFLAGS="$(CFLAGS_FOR_NEWLIB)" \ + NASMFLAGS="$(NASMFLAGS_FOR_NEWLIB)" \ + CC_FOR_TARGET=$(CC_FOR_TARGET) \ + CXX_FOR_TARGET=$(CXX_FOR_TARGET) \ + GCC_FOR_TARGET=$(GCC_FOR_TARGET) \ + AR_FOR_TARGET=$(AR_FOR_TARGET) \ + AS_FOR_TARGET=$(AS_FOR_TARGET) \ + LD_FOR_TARGET=$(LD_FOR_TARGET) \ + NM_FOR_TARGET=$(NM_FOR_TARGET) \ + OBJDUMP_FOR_TARGET=$(OBJDUMP_FOR_TARGET) \ + OBJCOPY_FOR_TARGET=$(OBJCOPY_FOR_TARGET) \ + RANLIB_FOR_TARGET=$(RANLIB_FOR_TARGET) \ + STRIP_FOR_TARGET=$(STRIP_FOR_TARGET) \ + READELF_FOR_TARGET=$(READELF_FOR_TARGET) -C newlib + +tools: + $Q$(MAKE) ARCH=$(ARCH) CFLAGS="$(CFLAGS_FOR_TOOLS)" LDFLAGS="$(LDFLAGS_FOR_TOOLS)" -C tools $(NAME).elf: $Q$(LD_FOR_TARGET) $(LDFLAGS) -o $(NAME).elf $^ @@ -75,10 +102,14 @@ $(NAME).elf32: $(NAME).elf clean: $Q$(RM) $(NAME).elf $(NAME).sym $(NAME).bin *~ + $Q$(MAKE) -C tools clean + $Q$(MAKE) -C newlib clean @echo Cleaned. veryclean: clean $Q$(RM) qemu-vlan0.pcap include/hermit/config.inc + $Q$(MAKE) -C tools veryclean + $Q$(MAKE) -C newlib veryclean @echo Very cleaned qemu: $(NAME).elf32 @@ -114,6 +145,6 @@ include/hermit/config.inc: include/hermit/config.h @echo [GCC-ASM] $@ $Q$(CC_FOR_TARGET) $(CFLAGS) -c -o $@ $< -.PHONY: default all clean emu gdb +.PHONY: default all clean emu gdb newlib include $(addsuffix /Makefile,$(SUBDIRS)) diff --git a/hermit/newlib/Makefile b/hermit/newlib/Makefile new file mode 100644 index 000000000..2d4f5d532 --- /dev/null +++ b/hermit/newlib/Makefile @@ -0,0 +1,37 @@ +TOPDIR = $(shell pwd) +ARCH = x86 +TARGET=x86_64-hermit-elf64 + +NEWLIB = $(TOPDIR)/$(ARCH)/$(TARGET) +RM = rm -rf +CD = cd +MKDIR = mkdir +TMP = $(TOPDIR)/tmp +OPT = --disable-shared --disable-multilib --disable-newlib-multithread --disable-newlib-reent-small + +# Prettify output +V = 0 +ifeq ($V,0) + Q = @ + P = > /dev/null +endif + +default: $(ARCH) + $Q$(MAKE) ARCH=$(ARCH) TARGET=$(TARGET) CFLAGS+="-ffreestanding -Wall -I$(NEWLIB)/include -I../../include -I../../arch/$(ARCH)/include" LDFLAGS+="-nostdlib -L$(NEWLIB)/lib" -C examples depend + $Q$(MAKE) ARCH=$(ARCH) TARGET=$(TARGET) CFLAGS+="-ffreestanding -Wall -I$(NEWLIB)/include -I../../include -I../../arch/$(ARCH)/include" LDFLAGS+="-nostdlib -L$(NEWLIB)/lib" -C examples + +$(ARCH): + @echo Build newlib + $Q$(RM) $(TMP) + $Q$(MKDIR) $(TMP) + $Q$(CD) $(TMP); $(TOPDIR)/src/configure --target=$(TARGET) --prefix=$(TOPDIR)/$(ARCH) $(OPT) && $(MAKE) && $(MAKE) install + +clean: + @echo Cleaning newlib + $Q$(MAKE) -C examples clean + $Q$(RM) $(TMP) + +veryclean: + @echo Propper cleaning newlib + $Q$(MAKE) -C examples veryclean + $Q$(RM) $(TOPDIR)/$(ARCH) diff --git a/hermit/newlib/examples/Makefile b/hermit/newlib/examples/Makefile new file mode 100644 index 000000000..2d3c1e33a --- /dev/null +++ b/hermit/newlib/examples/Makefile @@ -0,0 +1,57 @@ +ARCH = x86 +NEWLIB = ../x86/x86_64-hermit-elf64 +MAKE = make +STRIP_DEBUG = --strip-debug +KEEP_DEBUG = --only-keep-debug + +override LDFLAGS += -T link.ld + +# Default section offsets in x86-64 ELF files are aligned to the page-size. +# For x86-64 the pagesize is huge (2 MB) with the consquence of large sparse +# ELF files (which lead to a huge initrd). To solve this, we manually set the page-size to 4 KB. +# Furthermore, we use a large memory model to deal with real 64bit application. +override LDFLAGS += -Wl,-n,-z,max-page-size=0x1000 + +# Prettify output +V = 0 +ifeq ($V,0) + Q = @ + P = > /dev/null +endif + +# other implicit rules +%.o : %.c + @echo [CC] $@ + $Q$(CC_FOR_TARGET) -c $(CFLAGS) -o $@ $< + +default: all + +all: hello jacobi + +hello: hello.o + @echo [LD] $@ + $Q$(CC_FOR_TARGET) $(LDFLAGS) $(CFLAGS) -o $@ $< + $Q$(OBJCOPY_FOR_TARGET) $(KEEP_DEBUG) $@ $@.sym + $Q$(OBJCOPY_FOR_TARGET) $(STRIP_DEBUG) $@ + $Qchmod a-x $@.sym + +jacobi: jacobi.o + @echo [LD] $@ + $Q$(CC_FOR_TARGET) $(LDFLAGS) $(CFLAGS) -o $@ $< -lm + $Q$(OBJCOPY_FOR_TARGET) $(KEEP_DEBUG) $@ $@.sym + $Q$(OBJCOPY_FOR_TARGET) $(STRIP_DEBUG) $@ + $Qchmod a-x $@.sym + +clean: + @echo Cleaning examples + $Q$(RM) hello jacobi *.sym *.o *~ + +veryclean: + @echo Propper cleaning examples + $Q$(RM) hello jacobi *.sym *.o *~ + +depend: + $Q$(CC_FOR_TARGET) -MM $(CFLAGS) *.c > Makefile.dep + +-include Makefile.dep +# DO NOT DELETE diff --git a/hermit/newlib/examples/hello.c b/hermit/newlib/examples/hello.c new file mode 100644 index 000000000..43d93af4d --- /dev/null +++ b/hermit/newlib/examples/hello.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2010, Stefan Lankes, RWTH Aachen University + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 REGENTS OR CONTRIBUTORS 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. + */ + +#include +#include +#include +#include +#include +#undef errno +extern int errno; + +int main(int argc, char** argv) +{ + int i; + + printf("Hello World!!!\n"); + //for(i=0; environ[i]; i++) + // printf("environ[%d] = %s\n", i, environ[i]); + //for(i=0; i +#include +#include +#include +#include +#include +#include + +#define MATRIX_SIZE 128 +#define MAXVALUE 1337 +#define PAGE_SIZE 4096 +#define CACHE_SIZE (256*1024) +#define ALIGN(x,a) (((x)+(a)-1)&~((a)-1)) + +static int generate_empty_matrix(double*** A , unsigned int N) { + unsigned int iCnt; + int i,j; + + *A = (double**) malloc((N+1)*sizeof(double*)); + + if (*A == NULL) + return -2; /* Error */ + + (*A)[0] = (double*) malloc((N+1)*N*sizeof(double)); + + if (**A == NULL) + return -2; /* Error */ + + for(iCnt=1; iCnt Sum |A[i][j]| with (i != j) + */ + + (*A)[i][i] = sum + 2.0; + (*A)[i][N] += sum + 2.0; + } + + return 0; +} + +int main(int argc, char **argv) +{ + double* temp; + unsigned int i, j, iter_start, iter_end; + unsigned int iterations = 0; + double error, norm, max = 0.0; + double** A=0; + double* X; + double* X_old, xi; + //clock_t start, end; + + if (generate_empty_matrix(&A,MATRIX_SIZE) < 0) + { + printf("generate_empty_matrix() failed...\n"); + exit(-1); + + } + + printf("generate_empty_matrix() done...\n"); + + X = (double*) malloc(MATRIX_SIZE*sizeof(double)); + X_old = (double*) malloc(MATRIX_SIZE*sizeof(double)); + if(X == NULL || X_old == NULL) + { + printf("X or X_old is NULL...\n"); + exit(-1); + } + + for(i=0; i 0.01f) { + printf("Result is on position %d wrong (%f != 1.0)\n", i, X[i]); + exit(1); + } + } + printf("maximal error is %f\n", max); + + printf("\nmatrix size: %d x %d\n", MATRIX_SIZE, MATRIX_SIZE); + printf("number of iterations: %d\n", iterations); + //printf("calculation time: %f s\n", (float) (end-start) / (float) CLOCKS_PER_SEC); + + free((void*) X_old); + free((void*) X); + + return 0; +} diff --git a/hermit/tools/Makefile b/hermit/tools/Makefile index 1834bee18..a4aad5f52 100644 --- a/hermit/tools/Makefile +++ b/hermit/tools/Makefile @@ -15,6 +15,9 @@ init.hex: init.bin clean: $(RM) -rf *.o *~ *.bin *.obj *.hex +veryclean: clean + + depend: $(CC) -MM $(CFLAGS) *.c > Makefile.dep