Compare commits

...

3 Commits

Author SHA1 Message Date
Bananymous d1c8273826 Toolchain: Allow toolchain compilation for i386 targets
Fix GCC flags that were x86_64 specific
2024-03-21 21:59:09 +02:00
Bananymous 7d1b7436d4 LibC: Dummy ctr* files for i386
This allows compilation of libc for i386 targets
2024-03-21 15:20:20 +02:00
Bananymous 65750586b6 LibC: Use GCC builtins for math functions 2024-03-21 15:19:44 +02:00
7 changed files with 141 additions and 38 deletions

View File

@ -28,7 +28,7 @@ set(LIBC_SOURCES
termios.cpp
time.cpp
unistd.cpp
math.S
math.cpp
icxxabi.cpp
../BAN/BAN/Assert.cpp

33
libc/arch/i386/crt0.S Normal file
View File

@ -0,0 +1,33 @@
.section .text
.global _start
_start:
# zero out stack frame
pushl $0
pushl $0
movl %esp, %ebp
# FIXME: handle stack alignment
ud2
# push argc, argv, environ for call to main
pushl %edx
pushl %esi
pushl %edi
# initialize libc
pushl %edx
call _init_libc
addl $4, %esp
# call global constructos
call _init
# call main, arguments are already on stack
call main
# cleanly exit the process
pushl %eax
call exit
.size _start, . - _start

16
libc/arch/i386/crti.S Normal file
View File

@ -0,0 +1,16 @@
/* i386 crti.s */
.section .init
.global _init
.type _init, @function
_init:
pushl %ebp
movl %esp, %ebp
/* gcc will nicely put the contents of crtbegin.o's .init section here. */
.section .fini
.global _fini
.type _fini, @function
_fini:
pushl %ebp
movl %esp, %ebp
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */

10
libc/arch/i386/crtn.S Normal file
View File

@ -0,0 +1,10 @@
/* i386 crtn.s */
.section .init
/* gcc will nicely put the contents of crtend.o's .init section here. */
popl %ebp
ret
.section .fini
/* gcc will nicely put the contents of crtend.o's .fini section here. */
popl %ebp
ret

View File

@ -1,26 +0,0 @@
.global floorl;
floorl:
fldt 8(%rsp)
fnstenv -28(%rsp) /* store fpu environment */
/* We use here %edx although only the low 1 bits are defined.
But none of the operations should care and they are faster
than the 16 bit operations. */
movl $0x400,%edx /* round towards -oo */
orl -28(%rsp),%edx
andl $0xf7ff,%edx
movl %edx,-32(%rsp)
fldcw -32(%rsp) /* load modified control word */
frndint /* round */
/* Preserve "invalid" exceptions from sNaN input. */
fnstsw
andl $0x1, %eax
orl %eax, -24(%rsp)
fldenv -28(%rsp) /* restore original environment */
ret

63
libc/math.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <math.h>
#define BUILTINS1(func) \
float func##f(float a) { return __builtin_##func##f(a); } \
double func(double a) { return __builtin_##func(a); } \
long double func##l(long double a) { return __builtin_##func##l(a); }
#define BUILTINS2(func) \
float func##f(float a, float b) { return __builtin_##func##f(a, b); } \
double func(double a, double b) { return __builtin_##func(a, b); } \
long double func##l(long double a, long double b) { return __builtin_##func##l(a, b); } \
__BEGIN_DECLS
BUILTINS1(acos)
BUILTINS1(acosh)
BUILTINS1(asin)
BUILTINS1(asinh)
BUILTINS1(atan)
BUILTINS2(atan2)
BUILTINS1(atanh)
BUILTINS1(cbrt)
BUILTINS1(ceil)
BUILTINS2(copysign)
BUILTINS1(cos)
BUILTINS1(cosh)
BUILTINS1(erf)
BUILTINS1(erfc)
BUILTINS1(exp)
BUILTINS1(exp2)
BUILTINS1(expm1)
BUILTINS1(fabs)
BUILTINS2(fdim)
BUILTINS1(floor)
BUILTINS2(fmax)
BUILTINS2(fmin)
BUILTINS2(fmod)
BUILTINS2(hypot)
BUILTINS1(j0)
BUILTINS1(j1)
BUILTINS1(lgamma)
BUILTINS1(log)
BUILTINS1(log10)
BUILTINS1(log1p)
BUILTINS1(log2)
BUILTINS1(logb)
BUILTINS1(nearbyint)
BUILTINS2(nextafter)
BUILTINS2(pow)
BUILTINS2(remainder)
BUILTINS1(rint)
BUILTINS1(round)
BUILTINS1(sin)
BUILTINS1(sinh)
BUILTINS1(sqrt)
BUILTINS1(tan)
BUILTINS1(tanh)
BUILTINS1(tgamma)
BUILTINS1(trunc)
BUILTINS1(y0)
BUILTINS1(y1)
__END_DECLS

View File

@ -55,10 +55,16 @@ if [[ -z ${MAKE_JOBS:x} ]]; then
MAKE_JOBS="-j$(nproc)"
fi
if [ $BANAN_ARCH = "x86_64" ]; then
XCFLAGS="-g -O2 -mcmodel=large -mno-red-zone"
else
XCFLAGS="-g -O2"
fi
enter_clean_build () {
rm -rf build
mkdir build
cd build
rm -rf build-$BANAN_ARCH
mkdir build-$BANAN_ARCH
cd build-$BANAN_ARCH
}
build_binutils () {
@ -110,7 +116,7 @@ build_gcc () {
--enable-languages=c,c++
make $MAKE_JOBS all-gcc
make $MAKE_JOBS all-target-libgcc CFLAGS_FOR_TARGET='-g -O2 -mcmodel=large -mno-red-zone'
make $MAKE_JOBS all-target-libgcc CFLAGS_FOR_TARGET="$XCFLAGS"
make install-gcc
make install-target-libgcc
}
@ -142,13 +148,13 @@ build_grub () {
}
build_libstdcpp () {
if ! [[ -d $BANAN_BUILD_DIR/toolchain/$GCC_VERSION/build ]]; then
if ! [[ -d $BANAN_BUILD_DIR/toolchain/$GCC_VERSION/build-$BANAN_ARCH ]]; then
echo "You have to build gcc first"
exit 1
fi
cd $BANAN_BUILD_DIR/toolchain/$GCC_VERSION/build
make $MAKE_JOBS all-target-libstdc++-v3 CFLAGS_FOR_TARGET='-g -O2 -mcmodel=large -mno-red-zone'
cd $BANAN_BUILD_DIR/toolchain/$GCC_VERSION/build-$BANAN_ARCH
make $MAKE_JOBS all-target-libstdc++-v3 CFLAGS_FOR_TARGET="$XCFLAGS"
make install-target-libstdc++-v3
}
@ -162,13 +168,14 @@ echo "Creating dummy sysroot"
mkdir -p $BANAN_SYSROOT/usr
cp -r $BANAN_ROOT_DIR/libc/include $BANAN_SYSROOT/usr/include
# Cleanup all old files from toolchain prefix
rm -rf $BANAN_TOOLCHAIN_PREFIX
mkdir -p $BANAN_BUILD_DIR/toolchain
build_binutils
build_gcc
build_grub
# Grub is only needed for UEFI (x86_64)
if [ $BANAN_ARCH = "x86_64" ]; then
build_grub
fi
# delete sysroot and install libc
rm -r $BANAN_SYSROOT