Be able to compile OCaml with our kernel

This commit is contained in:
Calascibetta Romain 2025-01-12 20:06:40 +01:00
parent 6f7e4bf16a
commit b9e6d34daf
11 changed files with 140 additions and 204 deletions

View file

@ -4,8 +4,8 @@ AS= $(CONFIG_TARGET_CC)
# NOTE(dinosaure): we must use [aarch64-none-elf-gcc] to compile assembly code
# to be able to include some files and define some macros.
LD= $(CONFIG_TARGET_LD)
AR= aarch64-linux-gnu-ar # TODO
OBJDUMP= aarch64-linux-gnu-objdump # TODO
AR= $(CONFIG_TARGET_AR)
OBJDUMP= $(CONFIG_TARGET_OBJDUMP)
OBJCOPY= $(CONFIG_TARGET_OBJCOPY)
export TOPDIR := $(abspath .)
@ -27,17 +27,34 @@ DEFINE += -DSSP_GUARD_SYMBOL=__stack_chk_guard \
-DSSP_FAIL_SYMBOL=__stack_chk_fail
INCLUDE += -I $(TOPDIR)/include -I $(TOPDIR)/include/$(CONFIG_TARGET_TRIPLE) \
-I $(TOPDIR)/nolibc/include
-I $(TOPDIR)/nolibc/include -I $(TOPDIR)/openlibm/include -I $(TOPDIR)/openlibm/src
AFLAGS += $(ARCH) $(DEFINE) $(INCLUDE)
CFLAGS += -std=c11 -ffreestanding -fstack-protector-strong -nostdlib \
# CFLAGS & LDFLAGS are used with $(CC). Link arguments must be passed with -Wl
# Also, CONFIG_TARGET_LD_LDFLAGS are specific when we use CONFIG_TARGET_LD
CFLAGS += -ffreestanding -fstack-protector-strong -nostdlib \
-nostartfiles -mstrict-align -Wall \
$(ARCH) $(DEFINE) $(INCLUDE) $(CONFIG_TARGET_CC_CFLAGS)
LDFLAGS += -nostdlib -static -Wl,--no-warn-rwx-segments \
-Wl,--section-start=.init=0x80000 -Wl,--build-id=none \
-Wl,--start-group -L lib/$(CONFIG_TARGET_TRIPLE) -lgilbraltar.rpi5 -L nolibc -lnolibc -L openlibm -lopenlibm -lgcc -Wl,--end-group
comma := ,
empty:=
space := $(empty) $(empty)
LDFLAGS += -static -Wl,--no-warn-rwx-segments \
-Wl,--section-start=.init=0x80000 \
-Wl,--start-group \
-L lib/$(CONFIG_TARGET_TRIPLE) -lgilbraltar -lopenlibm -lnolibc -lgcc \
-Wl,--end-group \
-Wl,$(subst $(space),$(comma),${CONFIG_TARGET_CC_LDFLAGS})
LD_LDFLAGS += -static --no-warn-rwx-segments \
--section-start=.init=0x80000 \
--start-group \
-L lib/$(CONFIG_TARGET_TRIPLE) -lgilbraltar -lopenlibm -lnolibc -lgcc \
--end-group \
${CONFIG_TARGET_CC_LDFLAGS}
SRCS= kernel.c timer.c led.c interrupt_handler.c exception_handler.c \
mbox.c clock.c crt.c serial.c log.c power.c tag.c coherent_page.c dtb.c \
@ -50,23 +67,37 @@ ASMS:=$(addprefix kernel/,$(ASMS))
OBJS= $(SRCS:c=o) $(ASMS:S=o)
bin:
ALLTOOLS := gcc cc ar as ld nm objcopy objdump ranlib readelf strip
ALLTOOLS := $(foreach tool,$(ALLTOOLS),\
$(CONFIG_TARGET_TRIPLE)-$(tool))
TOOLCHAIN_FOR_BUILD := $(addprefix bin/build/,$(ALLTOOLS))
bin/build:
@test ! -d $@
@mkdir -p $@
bin/$(CONFIG_TARGET_TRIPLE)-%: toolchain/%.in | bin
bin/build/$(CONFIG_TARGET_TRIPLE)-%: toolchain/%.in | bin/build
@echo "SUBST $@"
@sed -e 's!@@CONFIG_TARGET_ARCH@@!$(CONFIG_TARGET_ARCH)!g' \
-e 's!@@CONFIG_TARGET_TRIPLE@@!$(CONFIG_TARGET_TRIPLE)!g' \
-e 's!@@CONFIG_TARGET_CC@@!$(CONFIG_TARGET_CC)!g' \
-e 's!@@CONFIG_TARGET_CC_CFLAGS@@!$(CONFIG_TARGET_CC_CFLAGS)!g' \
-e 's!@@CONFIG_TARGET_CC_LDFLAGS@@!$(CONFIG_TARGET_CC_LDFLAGS)!g' \
-e 's!@@CONFIG_TARGET_CC_CFLAGS@@!$(CFLAGS)!g' \
-e 's!@@CONFIG_TARGET_CC_LDFLAGS@@!$(LDFLAGS)!g' \
-e 's!@@CONFIG_TARGET_LD@@!$(CONFIG_TARGET_LD)!g' \
-e 's!@@CONFIG_TARGET_LD_LDFLAGS@@!$(CONFIG_TARGET_LD_LDFLAGS)!g' \
-e 's!@@CONFIG_TARGET_LD_LDFLAGS@@!$(LD_LDFLAGS)!g' \
-e 's!@@CONFIG_TARGET_OBJCOPY@@!$(CONFIG_TARGET_OBJCOPY)!g' \
-e 's!@@CONFIG_HOST_CC@@!$(CONFIG_HOST_CC)!g' \
-e 's!@@CONFIG_TARGET_NM@@!$(CONFIG_TARGET_NM)!g' \
-e 's!@@CONFIG_TARGET_OBJDUMP@@!$(CONFIG_TARGET_OBJDUMP)!g' \
-e 's!@@CONFIG_TARGET_AR@@!$(CONFIG_TARGET_AR)!g' \
-e 's!@@CONFIG_TARGET_RANLIB@@!$(CONFIG_TARGET_RANLIB)!g' \
-e 's!@@CONFIG_TARGET_STRIP@@!$(CONFIG_TARGET_STRIP)!g' \
$< >$@
@chmod +x $@
.PHONY: toolchains
toolchains: bin/build
%.o: %.S
@echo "AS $@"
@$(AS) $(AFLAGS) -c -o $@ $<
@ -79,13 +110,18 @@ lib/$(CONFIG_TARGET_TRIPLE):
@test ! -d $@
@mkdir -p $@
lib/$(CONFIG_TARGET_TRIPLE)/libgilbraltar.rpi5.a: $(OBJS) | lib/$(CONFIG_TARGET_TRIPLE)
lib/$(CONFIG_TARGET_TRIPLE)/libgilbraltar.a: $(OBJS) | lib/$(CONFIG_TARGET_TRIPLE)
@echo "AR $@"
@rm -f $@
@$(AR) cr $@ $^
lib/$(CONFIG_TARGET_TRIPLE)/libgilbraltar.stub.a: | lib/$(CONFIG_TARGET_TRIPLE)
@$(AR) cr $@
lib/$(CONFIG_TARGET_TRIPLE)/libnolibc.a: nolibc/libnolibc.a | lib/$(CONFIG_TARGET_TRIPLE)
@echo "COPY $@"
@cp $^ $@
lib/$(CONFIG_TARGET_TRIPLE)/libopenlibm.a: openlibm/libopenlibm.a | lib/$(CONFIG_TARGET_TRIPLE)
@echo "COPY $@"
@cp $^ $@
.PHONY: phony-openlibm
phony-openlibm: include/$(CONFIG_TARGET_TRIPLE)
@ -102,11 +138,10 @@ ocaml:
git apply --directory=$@ "patches/$$VERSION"/*; \
fi
ocaml/Makefile.config: | ocaml
PATH="$$PWD/bin/:$$PATH" ; \
ocaml/Makefile.config: $(TOOLCHAIN_FOR_BUILD) | ocaml
PATH="$$PWD/bin/build:$$PATH" ; \
cd ocaml && \
./configure \
--host="$(CONFIG_HOST_ARCH)-unknown-none" \
--target="$(CONFIG_TARGET_TRIPLE)" \
--prefix="$(CONFIG_SYSROOT)" \
--disable-shared \
@ -119,14 +154,20 @@ ocaml/Makefile.config: | ocaml
--without-zstd \
$(MAKECONF_OCAML_CONFIGURE_OPTIONS)
OCAML_IS_BUILT := _build/ocaml_is_built
$(OCAML_IS_BUILT): ocaml/Makefile.config | _build
PATH="$$PWD/$(TOOLDIR_FOR_BUILD):$$PATH" $(MAKE) -C ocaml cross.opt
build:
mkdir -p $@
OCAML_IS_BUILT := build/ocaml_is_built
$(OCAML_IS_BUILT): ocaml/Makefile.config | build
PATH="$$PWD/bin/build/$(TOOLDIR_FOR_BUILD):$$PATH" $(MAKE) -C ocaml cross.opt
cd ocaml && ocamlrun tools/stripdebug ocamlc ocamlc.tmp
cd ocaml && ocamlrun tools/stripdebug ocalmopt ocamlopt.tmp
cd ocaml && ocamlrun tools/stripdebug ocamlopt ocamlopt.tmp
touch $@
NOLIBC_CFLAGS= $(CFLAGS) -I $(TOPDIR)/nolibc/include -I $(TOPDIR)/openlibm/src -I $(TOPDIR)/openlibm/include
NOLIBC_CFLAGS= $(CFLAGS) \
-I $(TOPDIR)/nolibc/include \
-I $(TOPDIR)/openlibm/src \
-I $(TOPDIR)/openlibm/include
.PHONY: phony-nolibc
phony-nolibc: include/$(CONFIG_TARGET_TRIPLE)
@ -136,7 +177,7 @@ phony-nolibc: include/$(CONFIG_TARGET_TRIPLE)
nolibc/libnolibc.a: phony-nolibc
.PHONY: all
all: kernel/libgilbraltar.rpi5.a
all: kernel/libgilbraltar.a
.PHONY: clean
clean:
@ -159,7 +200,7 @@ test/%.o: test/%.c
@echo "CC $@"
@$(CC) $(CFLAGS) -c -o $@ $<
kernel_2712.img: lib/$(CONFIG_TARGET_TRIPLE)/libgilbraltar.rpi5.a nolibc/libnolibc.a openlibm/libopenlibm.a test/test.o
kernel_2712.img: lib/$(CONFIG_TARGET_TRIPLE)/libgilbraltar.a lib/$(CONFIG_TARGET_TRIPLE)/libnolibc.a lib/$(CONFIG_TARGET_TRIPLE)/libopenlibm.a test/test.o
@echo "LD ${@:img=elf}"
@$(LD) test/test.o -o ${@:img=elf} $(LDFLAGS) -Wl,-Map ${@:img=map} -Wl,-T gilbraltar.rpi5.ld
@$(CC) test/test.o -o ${@:img=elf} $(CFLAGS) $(LDFLAGS) -Wl,-Map,${@:img=map} -T gilbraltar.ld
@$(OBJCOPY) ${@:img=elf} -O binary $@

View file

@ -19,8 +19,14 @@
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
TARGET_CC="${TARGET_CC:-aarch64-linux-gnu-gcc}"
TARGET_LD="${TARGET_LD:-aarch64-linux-gnu-gcc}"
TARGET_LD="${TARGET_LD:-aarch64-linux-gnu-ld}"
TARGET_AR="${TARGET_AR:-aarch64-linux-gnu-ar}"
TARGET_NM="${TARGET_NM:-aarch64-linux-gnu-nm}"
TARGET_RANLIB="${TARGET_NM:-aarch64-linux-gnu-ranlib}"
TARGET_OBJCOPY="${TARGET_OBJCOPY:-aarch64-linux-gnu-objcopy}"
TARGET_OBJDUMP="${TARGET_OBJDUMP:-aarch64-linux-gnu-objdump}"
TARGET_READELF="${TARGET_OBJDUMP:-aarch64-linux-gnu-readelf}"
TARGET_STRIP="${TARGET_STRIP:-aarch64-linux-gnu-strip}"
prog_NAME="$(basename $0)"
@ -375,7 +381,7 @@ case ${CONFIG_HOST} in
# is subsequently discarded.
TARGET_LD="${TARGET_LD:-ld}"
TARGET_OBJCOPY="${TARGET_OBJCOPY:-objcopy}"
TARGET_CC_LDFLAGS="-Wl,--build-id=none"
TARGET_CC_LDFLAGS="--build-id=none"
;;
FreeBSD)
TARGET_LD="${TARGET_LD:-ld.lld}"
@ -396,7 +402,7 @@ case ${CONFIG_HOST} in
fi
# [LLD] OpenBSD's LLD needs to be explicitly told not to produce PIE
# executables.
TARGET_CC_LDFLAGS="-Wl,-nopie"
TARGET_CC_LDFLAGS="-nopie"
TARGET_LD_LDFLAGS="-nopie"
;;
*)
@ -421,10 +427,15 @@ if ! CC="${TARGET_CC}" OBJCOPY="${TARGET_OBJCOPY}" check_objcopy; then
die "Could not find a working target objcopy"
fi
if CC="${TARGET_CC}" cc_is_gcc; then
LIBGCC=$(${TARGET_CC} -print-libgcc-file-name)
TARGET_CC_LDFLAGS="${TARGET_CC_LDFLAGS} -L $(dirname ${LIBGCC})"
fi
echo "${prog_NAME}: Using ${TARGET_LD} for target linker"
echo "${prog_NAME}: Using ${TARGET_OBJCOPY} for target objcopy"
TARGET_TRIPLE="${TARGET_ARCH}-gilbraltar-none"
TARGET_TRIPLE="${TARGET_ARCH}-gilbraltar-ocaml"
echo "${prog_NAME}: Target toolchain triple is ${TARGET_TRIPLE}"
#
@ -445,7 +456,13 @@ CONFIG_TARGET_CC_IS_OPENBSD=${TARGET_CC_IS_OPENBSD}
CONFIG_TARGET_LD=${TARGET_LD}
CONFIG_TARGET_LD_LDFLAGS=${TARGET_LD_LDFLAGS}
CONFIG_TARGET_LD_MAX_PAGE_SIZE=${TARGET_LD_MAX_PAGE_SIZE}
CONFIG_TARGET_NM=${TARGET_NM}
CONFIG_TARGET_AR=${TARGET_AR}
CONFIG_TARGET_RANLIB=${TARGET_RANLIB}
CONFIG_TARGET_OBJCOPY=${TARGET_OBJCOPY}
CONFIG_TARGET_OBJDUMP=${TARGET_OBJDUMP}
CONFIG_TARGET_READELF=${TARGET_READELF}
CONFIG_TARGET_STRIP=${TARGET_STRIP}
EOM
#

View file

@ -1,133 +0,0 @@
#!/bin/sh
# Generate a wrapper for Gilbraltar (G)CC, ld, objcopy and any other binutil
# Expected argument: the tool to generate
# Expected environment variables:
# ARCH: the target architecture (x86_64 or aarch64)
# TOOL_CFLAGS and TOOL_LDFLAGS: extra flags
# GILBRALTAR_TOOLCHAIN: the target for the wrapped Gilbraltar toolchain
# OTHERTOOLPREFIX: the prefix for tools not in the Gilbraltar toolchain
# TARGET_X: overrides the command for binutil X
gen_cc() {
# Note that -nostdlib is not required, as it is injected by Solo5' cc, ld
CFLAGS="$TOOL_CFLAGS"
LDFLAGS="$TOOL_LDFLAGS"
EXTRALIBS=""
case "$ARCH" in
aarch64)
EXTRALIBS="-lgcc"
;;
esac
# Add the -Wno-unused-command-line-argument option for clang, as we always
# give it compiling options, even if it will be only linking
# Reuse the test from Solo5 to detect clang
if "$SOLO5_TOOLCHAIN-cc" -dM -E - </dev/null | grep -Eq '^#define __clang__ 1$'
then CFLAGS="-Wno-unused-command-line-argument $CFLAGS"
fi
cat << EOF
#!/bin/sh
# Just like the Solo5 cc, we assume that we are linking, unless we find an
# argument suggesting we are compiling but we call Solo5' cc regardless
compiling=
for arg in "\$@"; do
case "\$arg" in
-[cSE])
compiling="\$arg"
break
;;
esac
done
set -- \\
$CFLAGS \\
-include _solo5/overrides.h \\
-D__BSD_VISIBLE=0 \\
-D__XSI_VISIBLE=0 \\
"\$@"
if [ -z "\$compiling" ]; then
# linking options
set -- \\
"\$@" \\
$LDFLAGS \\
-Wl,--start-group \\
-lnolibc \\
-lopenlibm \\
$EXTRALIBS \\
-Wl,--end-group
fi
[ -n "\${__V}" ] && set -x
exec "$SOLO5_TOOLCHAIN-cc" "\$@"
EOF
}
gen_tool() {
TOOL="$1"
case "$TOOL" in
ar)
TARGET_TOOL="$TARGET_AR"
;;
as)
TARGET_TOOL="$TARGET_AS"
;;
ld)
TARGET_TOOL="$TARGET_LD"
;;
nm)
TARGET_TOOL="$TARGET_NM"
;;
objcopy)
TARGET_TOOL="$TARGET_OBJCOPY"
;;
objdump)
TARGET_TOOL="$TARGET_OBJDUMP"
;;
ranlib)
TARGET_TOOL="$TARGET_RANLIB"
;;
readelf)
TARGET_TOOL="$TARGET_READELF"
;;
strip)
TARGET_TOOL="$TARGET_STRIP"
;;
esac
if test "$TARGET_TOOL" ; then
TOOL="$TARGET_TOOL"
elif command -v -- "$SOLO5_TOOLCHAIN-$TOOL" > /dev/null; then
TOOL="$SOLO5_TOOLCHAIN-$TOOL"
else
case "$TOOL" in
as)
TOOL="$SOLO5_TOOLCHAIN-cc -c"
;;
*)
if command -v -- "$OTHERTOOLPREFIX$TOOL" > /dev/null; then
TOOL="$OTHERTOOLPREFIX$TOOL"
fi
;;
esac
fi
cat << EOF
#!/bin/sh
exec $TOOL "\$@"
EOF
}
case "$1" in
cc|gcc)
gen_cc
;;
*)
gen_tool "$1"
;;
esac

View file

@ -1 +0,0 @@

View file

@ -9,5 +9,6 @@
void gilbraltar_delay_hot_loop(uint32_t);
void gilbraltar_delay_us(uint32_t);
void gilbraltar_delay_ms(uint32_t);
uint32_t gilbraltar_system_clock(void);
#endif

View file

@ -11,7 +11,7 @@
#include <sysconfig.h>
#include <tag.h>
extern int main(int, char *const *);
extern int main();
extern char __bss_start;
extern char _etext[];

View file

@ -23,3 +23,7 @@ void gilbraltar_delay_ms(uint32_t ms) {
if (ms > 0)
gilbraltar_delay_us(ms * 1000);
}
uint32_t gilbraltar_system_clock(void) {
return (read32(ARM_SYSTIMER_CLO)); // TODO(dinosaure): CLOCKHZ?
}

View file

@ -3,11 +3,19 @@
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/times.h>
int errno;
#include <timer.h>
#include <serial.h>
/*
extern void gilbraltar_serial_write(const char *, size_t);
extern void gilbraltar_serial_puts(const char *);
extern uint32_t gilbraltar_system_clock(void);
*/
static size_t console_write(FILE *f __attribute__((unused)), const char *str, size_t len) {
gilbraltar_serial_write(str, len);
@ -45,3 +53,23 @@ int __getauxval(int unused) {
return (0);
}
#endif
clock_t times(struct tms *buf) {
memset(buf, 0, sizeof(*buf));
return ((clock_t) gilbraltar_system_clock());
}
#define NSEC_PER_SEC 1000000000ULL
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
if (tv != NULL) {
uint32_t now = gilbraltar_system_clock();
tv->tv_sec = now / NSEC_PER_SEC;
tv->tv_usec = (now % NSEC_PER_SEC) / 1000ULL;
}
if (tz != NULL) {
memset(tz, 0, sizeof(*tz));
}
return 0;
}

View file

@ -31,9 +31,9 @@
# presence of symbols.
prog="$(basename $0)"
I="$(dirname $0)/../include"
I="$(dirname $0)/../../include"
[ ! -d "${I}" ] && echo "$prog: Could not determine include path" 1>&2 && exit 1
L="$(dirname $0)/../lib/@@CONFIG_TARGET_TRIPLE@@"
L="$(dirname $0)/../../lib/@@CONFIG_TARGET_TRIPLE@@"
[ ! -d "${L}" ] && echo "$prog: Could not determine library path" 1>&2 && exit 1
# we can't really tell if 'cc' is called with no input, but work around the
# most obvious cases and stop them from "succeeding" and producing an "a.out"
@ -42,8 +42,7 @@ L="$(dirname $0)/../lib/@@CONFIG_TARGET_TRIPLE@@"
[ "$#" -eq 1 -a "$1" = "-v" ] && exec @@CONFIG_TARGET_CC@@ "$@"
# default to cc as linker, unless we see args to the contrary
M=link
# cc as linker with no -z gilbraltar-abi= defaults to stub
B=stub
B=rpi5
Z=
for arg do
shift
@ -75,56 +74,36 @@ for arg do
esac
set -- "$@" "$arg"
done
case ${M} in
compile)
[ -n "${__V}" ] && set -x
echo exec @@CONFIG_TARGET_CC@@ \
@@CONFIG_TARGET_CC_CFLAGS@@ \
-isystem ${I}/@@CONFIG_TARGET_TRIPLE@@ \
-I ${I} \
-D__BSD_VISIBLE=0 \
-D__XSI_VISIBLE=0 \
-std=c11 \
-ffreestanding \
-fstack-protector-strong \
-nostdlib \
-nostartfiles \
-mstrict-align \
-Wall \
-DAARCH=64 \
-mcpu=cortex-a76 \
"$@" ;
exec @@CONFIG_TARGET_CC@@ \
@@CONFIG_TARGET_CC_CFLAGS@@ \
-isystem ${I}/@@CONFIG_TARGET_TRIPLE@@ \
-I ${I} \
-D__BSD_VISIBLE=0 \
-D__XSI_VISIBLE=0 \
-std=c11 \
-ffreestanding \
-fstack-protector-strong \
-nostdlib \
-nostartfiles \
-mstrict-align \
-Wall \
-DAARCH=64 \
-mcpu=cortex-a76 \
"$@"
;;
link)
[ -n "${B}" ] && B="-Wl,-T,gilbraltar.${B}.ld -lgilbraltar.${B}"
[ -n "${B}" ] && B="-Wl,-T,gilbraltar.ld"
[ -n "${LDFLAGS}" ] && LDFLAGS=$(echo "-Wl @@CONFIG_TARGET_LD_LDFLAGS@@" | tr ' ' ',')
[ -n "${__V}" ] && set -x
exec @@CONFIG_TARGET_CC@@ \
-L ${L} \
@@CONFIG_TARGET_CC_CFLAGS@@ \
-isystem ${I}/@@CONFIG_TARGET_TRIPLE@@ \
-I ${I} \
-ffreestanding \
-fstack-protector-strong \
@@CONFIG_TARGET_CC_LDFLAGS@@ \
-nostdlib \
-L ${L} \
"$@" \
@@CONFIG_TARGET_CC_LDFLAGS@@
-Wl,${LDFLAGS} \
${B} \
-static \
"$@"
;;
esac

View file

@ -29,7 +29,7 @@
# if linking. No default for ABI is provided, as it is expected that a caller
# directly using 'ld' knows what they are doing.
L="$(dirname $0)/../lib/@@CONFIG_TARGET_TRIPLE@@"
L="$(dirname $0)/../../lib/@@CONFIG_TARGET_TRIPLE@@"
[ ! -d "${L}" ] && echo "$0: Could not determine library path" 1>&2 && exit 1
# ld accepts -z gilbraltar-abi=ABI, but does not provide a default ABI
# this is intentional
@ -62,7 +62,7 @@ for arg do
esac
set -- "$@" "$arg"
done
[ -n "${B}" ] && B="-T $gilbraltar.{B}.ld -lgilbraltar.${B}"
[ -n "${B}" ] && B="-T $gilbraltar.ld -lgilbraltar"
[ -n "${__V}" ] && set -x
exec @@CONFIG_TARGET_LD@@ \
@@CONFIG_TARGET_LD_LDFLAGS@@ \