mirror of
https://github.com/hermitcore/libhermit.git
synced 2025-03-09 00:00:03 +01:00
add newlib as C library for HermitCore apps
This commit is contained in:
parent
b1225d3800
commit
fd817f02f0
6 changed files with 385 additions and 5 deletions
|
@ -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))
|
||||
|
|
37
hermit/newlib/Makefile
Normal file
37
hermit/newlib/Makefile
Normal file
|
@ -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)
|
57
hermit/newlib/examples/Makefile
Normal file
57
hermit/newlib/examples/Makefile
Normal file
|
@ -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
|
47
hermit/newlib/examples/hello.c
Normal file
47
hermit/newlib/examples/hello.c
Normal file
|
@ -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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#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<argc; i++)
|
||||
// printf("argv[%d] = %s\n", i, argv[i]);
|
||||
|
||||
return errno;
|
||||
}
|
205
hermit/newlib/examples/jacobi.c
Normal file
205
hermit/newlib/examples/jacobi.c
Normal file
|
@ -0,0 +1,205 @@
|
|||
/*
|
||||
* Copyright (c) 2010-2011, 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#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<N; iCnt++) { /* Assign pointers in the first "real index"; Value from 1 to N (0 yet set, value N means N+1) */
|
||||
(*A)[iCnt] = &((*A)[0][iCnt*(N+1)]);
|
||||
}
|
||||
|
||||
memset(**A, 0, (N+1)*N*sizeof(double)); /* Fill matrix values with 0 */
|
||||
|
||||
srand( 42 /*(unsigned) time(NULL)*/ ) ; /* init random number generator */
|
||||
|
||||
/*
|
||||
* initialize the system of linear equations
|
||||
* the result vector is one
|
||||
*/
|
||||
for (i = 0; i < N; i++)
|
||||
{
|
||||
double sum = 0.0;
|
||||
|
||||
for (j = 0; j < N; j++)
|
||||
{
|
||||
if (i != j)
|
||||
{
|
||||
double c = ((double)rand()) / ((double)RAND_MAX) * MAXVALUE;
|
||||
|
||||
sum += fabs(c);
|
||||
(*A)[i][j] = c;
|
||||
(*A)[i][N] += c;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The Jacobi method will always converge if the matrix A is strictly or irreducibly diagonally dominant.
|
||||
* Strict row diagonal dominance means that for each row, the absolute value of the diagonal term is
|
||||
* greater than the sum of absolute values of other terms: |A[i][i]| > 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<MATRIX_SIZE; i++)
|
||||
{
|
||||
X[i] = ((double)rand()) / ((double)RAND_MAX) * 10.0;
|
||||
X_old[i] = 0.0;
|
||||
}
|
||||
|
||||
printf("start calculation...\n");
|
||||
|
||||
iter_start = 0;
|
||||
iter_end = MATRIX_SIZE;
|
||||
|
||||
//start = clock();
|
||||
|
||||
while(1)
|
||||
{
|
||||
iterations++;
|
||||
|
||||
temp = X_old;
|
||||
X_old = X;
|
||||
X = temp;
|
||||
|
||||
for (i=iter_start; i<iter_end; i++)
|
||||
{
|
||||
for(j=0, xi=0.0; j<i; j++)
|
||||
xi += A[i][j] * X_old[j];
|
||||
|
||||
for(j=i+1; j<MATRIX_SIZE; j++)
|
||||
xi += A[i][j] * X_old[j];
|
||||
X[i] = (A[i][MATRIX_SIZE] - xi) / A[i][i];
|
||||
}
|
||||
|
||||
if (iterations % 5000 == 0 ) {/* calculate the Euclidean norm between X_old and X*/
|
||||
norm = 0.0;
|
||||
for (i=iter_start; i<iter_end; i++)
|
||||
norm += (X_old[i] - X[i]) * (X_old[i] - X[i]);
|
||||
|
||||
/* check the break condition */
|
||||
norm /= (double) MATRIX_SIZE;
|
||||
if (norm < 0.0000001)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//end = clock();
|
||||
|
||||
if (MATRIX_SIZE < 16) {
|
||||
printf("Print the solution...\n");
|
||||
/* print solution */
|
||||
for(i=0; i<MATRIX_SIZE; i++) {
|
||||
for(j=0; j<MATRIX_SIZE; j++)
|
||||
printf("%8.2f\t", A[i][j]);
|
||||
printf("*\t%8.2f\t=\t%8.2f\n", X[i], A[i][MATRIX_SIZE]);
|
||||
}
|
||||
}
|
||||
printf("Check the result...\n");
|
||||
|
||||
/*
|
||||
* check the result
|
||||
* X[i] have to be 1
|
||||
*/
|
||||
for(i=0; i<MATRIX_SIZE; i++) {
|
||||
error = fabs(X[i] - 1.0f);
|
||||
|
||||
if (max < error)
|
||||
max = error;
|
||||
if (error > 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;
|
||||
}
|
|
@ -15,6 +15,9 @@ init.hex: init.bin
|
|||
clean:
|
||||
$(RM) -rf *.o *~ *.bin *.obj *.hex
|
||||
|
||||
veryclean: clean
|
||||
|
||||
|
||||
depend:
|
||||
$(CC) -MM $(CFLAGS) *.c > Makefile.dep
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue