Compare commits

..

4 Commits

13 changed files with 256 additions and 84 deletions

1
.gitignore vendored
View File

@ -19,3 +19,4 @@ base/etc/skel/LICENSE
kernel/config.cmake kernel/config.cmake
ports/out/ ports/out/
ports/temp/ ports/temp/
ports/dev/

View File

@ -95,8 +95,6 @@ void handle_cpu_exception(int signo, const char* err, Registers* regs)
kerrorln("R12: %.16lx R13: %.16lx R14: %.16lx R15: %.16lx", regs->r12, regs->r13, regs->r14, regs->r15); kerrorln("R12: %.16lx R13: %.16lx R14: %.16lx R15: %.16lx", regs->r12, regs->r13, regs->r14, regs->r15);
kerrorln("RIP: %.16lx CS: %.16lx SS: %.16lx FLAGS: %.16lx", regs->rip, regs->cs, regs->ss, regs->rflags); kerrorln("RIP: %.16lx CS: %.16lx SS: %.16lx FLAGS: %.16lx", regs->rip, regs->cs, regs->ss, regs->rflags);
CPU::print_stack_trace_at(regs);
if (!is_in_kernel(regs)) if (!is_in_kernel(regs))
{ {
auto* current = Scheduler::current(); auto* current = Scheduler::current();

View File

@ -187,7 +187,10 @@ void Thread::process_pending_signals(Registers* current_regs)
switch (action) switch (action)
{ {
case DefaultSignalAction::Ignore: return; case DefaultSignalAction::Ignore: return;
case DefaultSignalAction::Terminate: exit_and_signal_parent(signo | _SIGBIT); case DefaultSignalAction::Terminate:
kwarnln("Terminating thread %d with signal %d", id, signo);
CPU::print_stack_trace_at(current_regs);
exit_and_signal_parent(signo | _SIGBIT);
case DefaultSignalAction::Stop: stop(); case DefaultSignalAction::Stop: stop();
default: return; default: return;
} }

View File

@ -3,6 +3,8 @@
.global _start .global _start
.extern exit .extern exit
.extern libc_init .extern libc_init
.extern _init
.extern _fini
_start: _start:
# Set up end of the stack frame linked list. # Set up end of the stack frame linked list.
movq $0, %rbp movq $0, %rbp
@ -26,6 +28,11 @@ _start:
# Run main # Run main
call main call main
# Store the exit code before calling _fini.
push %rax
call _fini
# Terminate the process with the exit code. # Terminate the process with the exit code.
movl %eax, %edi pop %rdi
call exit call exit

View File

@ -7,6 +7,14 @@ extern char** environ;
extern "C" FILE* _fdopen_impl(int, const char*, int); extern "C" FILE* _fdopen_impl(int, const char*, int);
extern "C" void _init_stdio(); extern "C" void _init_stdio();
typedef void initfunc_t(void);
extern initfunc_t *__init_array_start[], *__init_array_end[];
static void handle_init_array(void)
{
for (initfunc_t** p = __init_array_start; p != __init_array_end; p++) (*p)();
}
extern "C" extern "C"
{ {
void libc_init(int argc, char** argv, int envc, char** envp) void libc_init(int argc, char** argv, int envc, char** envp)
@ -19,5 +27,7 @@ extern "C"
stdin = _fdopen_impl(STDIN_FILENO, "r", _IOLBF); stdin = _fdopen_impl(STDIN_FILENO, "r", _IOLBF);
stdout = _fdopen_impl(STDOUT_FILENO, "w", _IOLBF); stdout = _fdopen_impl(STDOUT_FILENO, "w", _IOLBF);
stderr = _fdopen_impl(STDERR_FILENO, "w", _IONBF); stderr = _fdopen_impl(STDERR_FILENO, "w", _IONBF);
handle_init_array();
} }
} }

View File

@ -19,5 +19,5 @@ do_patch()
do_configure() do_configure()
{ {
$srcdir/configure --host=$LUNA_ARCH-luna --with-build-sysroot=$LUNA_BASE --disable-nls --disable-werror --enable-warn-rwx-segments=no --prefix=/usr --enable-gold=no --enable-ld=yes --enable-gprofng=no $srcdir/configure --host=$LUNA_ARCH-luna --with-build-sysroot=$LUNA_BASE --disable-nls --disable-werror --enable-warn-rwx-segments=no --prefix=/usr --enable-gold=no --enable-ld=yes --enable-gprofng=no --enable-initfini-array
} }

View File

@ -17,7 +17,7 @@ do_patch()
do_configure() do_configure()
{ {
$srcdir/configure --prefix=/usr --target=$LUNA_ARCH-luna --host=$LUNA_ARCH-luna --disable-nls --with-build-sysroot=$LUNA_BASE --enable-languages=c,c++ --enable-checking $srcdir/configure --prefix=/usr --target=$LUNA_ARCH-luna --host=$LUNA_ARCH-luna --disable-nls --with-build-sysroot=$LUNA_BASE --enable-languages=c,c++ --enable-checking --enable-initfini-array
} }
do_build() do_build()

View File

@ -23,6 +23,7 @@ luna_test(libluna/TestSharedPtr.cpp TestSharedPtr)
luna_test(libc/TestScanf.cpp TestScanf) luna_test(libc/TestScanf.cpp TestScanf)
luna_test(libc/TestString.cpp TestString) luna_test(libc/TestString.cpp TestString)
luna_test(libc/TestEnv.cpp TestEnv) luna_test(libc/TestEnv.cpp TestEnv)
luna_test(libc/TestGlobalCtors.cpp TestGlobalCtors)
luna_app(run-tests.cpp run-tests) luna_app(run-tests.cpp run-tests)
endif() endif()

View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <string.h>
#include <test.h>
#include <time.h>
struct TestStruct
{
TestStruct(const char* name) : m_name(name)
{
// Just to make sure this constructor doesn't get optimized out
time_t t = time(NULL);
ctime(&t);
}
const char* name()
{
return m_name;
}
private:
const char* m_name;
};
TestStruct g_struct("Example Struct");
TestResult test_successful_global_constructor()
{
validate(g_struct.name());
validate(!strcmp(g_struct.name(), "Example Struct"));
test_success;
}
Result<void> test_main()
{
test_prelude;
run_test(test_successful_global_constructor);
return {};
}

View File

@ -13,7 +13,7 @@ then
exit 1 exit 1
fi fi
export WORKDIR=$LUNA_ROOT/ports/temp/ export WORKDIR=${WORKDIR:-$LUNA_ROOT/ports/temp}
show_error() show_error()
{ {
@ -28,11 +28,13 @@ source ports/$PORT_NAME/PACKAGE
[ -z ${format+x} ] && show_error "error: The port's PACKAGE file does not have a 'format' key." [ -z ${format+x} ] && show_error "error: The port's PACKAGE file does not have a 'format' key."
[ -z ${url+x} ] && show_error "error: The port's PACKAGE file does not have a 'url' key." [ -z ${url+x} ] && show_error "error: The port's PACKAGE file does not have a 'url' key."
if ! [ -z ${dependencies+x} ]; then if [ -x ${skip_dependencies+x} ]; then
echo "Installing dependencies of $name: (${dependencies[@]})" if ! [ -z ${dependencies+x} ]; then
for dep in ${dependencies[@]}; do echo "Installing dependencies of $name: (${dependencies[@]})"
tools/install-package.sh $dep for dep in ${dependencies[@]}; do
done tools/install-package.sh $dep
done
fi
fi fi
export portdir=$LUNA_ROOT/ports/$name/ export portdir=$LUNA_ROOT/ports/$name/
@ -43,37 +45,39 @@ export installdir=$WORKDIR/pkgroot-$name
mkdir -p $WORKDIR mkdir -p $WORKDIR
cd $WORKDIR cd $WORKDIR
# Download package sources if [ -z ${skip_download_sources+x} ]; then
case $format in # Download package sources
tar) case $format in
[ -z ${output+x} ] && show_error "error: The 'tar' download format needs an 'output' key." tar)
[ -z ${sha256sum+x} ] && show_error "error: The 'tar' download format needs a 'sha256sum' key." [ -z ${output+x} ] && show_error "error: The 'tar' download format needs an 'output' key."
if ! [ -f $output ]; then [ -z ${sha256sum+x} ] && show_error "error: The 'tar' download format needs a 'sha256sum' key."
echo "Downloading tarball for $name..." if ! [ -f $output ]; then
wget $url --quiet echo "Downloading tarball for $name..."
else wget $url --quiet
echo "Using cached tarball for $name." else
fi echo "Using cached tarball for $name."
echo "$sha256sum $output" | sha256sum -c fi
tar xf $output echo "$sha256sum $output" | sha256sum -c
;; tar xf $output
git) ;;
if ! [ -d $srcdir ]; then git)
echo "Cloning repository for $name..." if ! [ -d $srcdir ]; then
git clone $url $srcdir echo "Cloning repository for $name..."
else git clone $url $srcdir
echo "Using local repository for $name." else
fi echo "Using local repository for $name."
if ! [ -z ${tag+x} ]; then fi
cd $srcdir if ! [ -z ${tag+x} ]; then
git checkout $tag cd $srcdir
cd - git checkout $tag
fi cd -
;; fi
*) ;;
show_error "error: Unrecognized 'format' key in PACKAGE file: '$format'" *)
;; show_error "error: Unrecognized 'format' key in PACKAGE file: '$format'"
esac ;;
esac
fi
if ! [ "$set_cc_variables" = "no" ]; then if ! [ "$set_cc_variables" = "no" ]; then
export CC=x86_64-luna-gcc export CC=x86_64-luna-gcc
@ -89,54 +93,65 @@ fi
mkdir -p $builddir mkdir -p $builddir
cd $builddir cd $builddir
declare -F do_patch &>/dev/null && do_patch if [ -z ${skip_patch+x} ]; then
declare -F do_patch &>/dev/null && do_patch
echo "Configuring $name..."
if [ "$use_default_configure" = "true" ]; then
$srcdir/configure --host=$LUNA_ARCH-luna --prefix=/usr
elif [ "$use_cmake_configure" = "true" ]; then
cmake -S $srcdir -B . -DCMAKE_C_COMPILER=$LUNA_ARCH-luna-gcc -DCMAKE_CXX_COMPILER=$LUNA_ARCH-luna-g++ -G "Unix Makefiles"
else
declare -F do_configure &>/dev/null || show_error "error: If not using a default configure line, the PACKAGE file must provide a 'do_configure' function."
do_configure
fi fi
echo "Building $name..." if [ -z ${skip_configure+x} ]; then
echo "Configuring $name..."
export MAKEJOBS=$(nproc) if [ "$use_default_configure" = "true" ]; then
$srcdir/configure --host=$LUNA_ARCH-luna --prefix=/usr
if [ "$default_build_make" = "true" ]; then elif [ "$use_cmake_configure" = "true" ]; then
make -j$MAKEJOBS cmake -S $srcdir -B . -DCMAKE_C_COMPILER=$LUNA_ARCH-luna-gcc -DCMAKE_CXX_COMPILER=$LUNA_ARCH-luna-g++ -G "Unix Makefiles"
elif [ "$default_build_cmake" = "true" ]; then else
cmake --build . declare -F do_configure &>/dev/null || show_error "error: If not using a default configure line, the PACKAGE file must provide a 'do_configure' function."
else do_configure
declare -F do_build &>/dev/null || show_error "error: If not using a default build preset, the PACKAGE file must provide a 'do_build' function." fi
do_build
fi fi
echo "Installing $name..." if [ -z ${skip_build+x} ]; then
echo "Building $name..."
mkdir -p $installdir export MAKEJOBS=$(nproc)
export DESTDIR=$installdir
if [ "$default_install_make" = "true" ]; then if [ "$default_build_make" = "true" ]; then
make install make -j$MAKEJOBS
elif [ "$default_install_cmake" = "true" ]; then elif [ "$default_build_cmake" = "true" ]; then
cmake --install $builddir --prefix /usr cmake --build .
else else
declare -F do_install &>/dev/null || show_error "error: If not using a default install preset, the PACKAGE file must provide a 'do_install' function." declare -F do_build &>/dev/null || show_error "error: If not using a default build preset, the PACKAGE file must provide a 'do_build' function."
do_install do_build
fi
fi fi
rm -f $installdir/usr/lib/*.la # remove all libtool .la files, as they use host stuff which we don't want at all if [ -z ${skip_install+x} ]; then
set +e echo "Installing $name..."
$LUNA_ARCH-luna-strip $installdir/usr/bin/* # strip all binaries
set -e
cd $installdir rm -rf $installdir # clean it if it already existed
mkdir -p $installdir
export DESTDIR=$installdir
tar czf $LUNA_ROOT/ports/out/$name-$version.tar.gz * if [ "$default_install_make" = "true" ]; then
make install
elif [ "$default_install_cmake" = "true" ]; then
cmake --install $builddir --prefix /usr
else
declare -F do_install &>/dev/null || show_error "error: If not using a default install preset, the PACKAGE file must provide a 'do_install' function."
do_install
fi
cd $LUNA_ROOT rm -f $installdir/usr/lib/*.la # remove all libtool .la files, as they use host stuff which we don't want at all
rm -rf $WORKDIR set +e
$LUNA_ARCH-luna-strip $installdir/usr/bin/* # strip all binaries
set -e
cd $installdir
tar czf $LUNA_ROOT/ports/out/$name-$version.tar.gz *
fi
if [ -z ${preserve_workdir+x} ]; then
cd $LUNA_ROOT
rm -rf $WORKDIR
fi

95
tools/package-helper.sh Executable file
View File

@ -0,0 +1,95 @@
#!/usr/bin/env bash
set -e
source $(dirname $0)/env.sh
cd $LUNA_ROOT
SUBCOMMAND=$1
PORT_NAME=$2
if [ "$SUBCOMMAND" = "help" ]; then
echo "TODO: add help"
exit 1
fi
if ! [ -f ports/$PORT_NAME/PACKAGE ]
then
echo "Package $PORT_NAME does not exist. Make sure there is a valid PACKAGE file in the appropriate directory!"
exit 1
fi
source ports/$PORT_NAME/PACKAGE
export preserve_workdir=1
export WORKDIR=$LUNA_ROOT/ports/dev
case $SUBCOMMAND in
download)
export skip_configure=1
export skip_build=1
export skip_install=1
export skip_dependencies=1
export skip_patch=1
tools/make-package.sh $PORT_NAME
if [ "$format" = "tar" ]; then
cd ${srcdir:-$WORKDIR/$name-$version}
git init
git add .
git commit -m "Temporary development git repository for diffs."
cd $LUNA_ROOT
fi
echo "Created dev source tree in ${srcdir:-$WORKDIR/$name-$version}."
;;
setup-dependencies)
export skip_download_sources=1
export skip_build=1
export skip_install=1
export skip_configure=1
export skip_patch=1
tools/make-package.sh $PORT_NAME
;;
patch)
export skip_download_sources=1
export skip_build=1
export skip_install=1
export skip_configure=1
export skip_dependencies=1
tools/make-package.sh $PORT_NAME
;;
configure)
export skip_download_sources=1
export skip_build=1
export skip_install=1
export skip_dependencies=1
export skip_patch=1
tools/make-package.sh $PORT_NAME
;;
compile)
export skip_download_sources=1
export skip_configure=1
export skip_dependencies=1
export skip_patch=1
tools/make-package.sh $PORT_NAME
echo "Packaged dev build as $LUNA_ROOT/ports/out/$name-$version.tar.gz. Install it using tools/install-package.sh."
;;
generate-diff)
cd ${srcdir:-$WORKDIR/$name-$version}
git diff > $LUNA_ROOT/ports/$name/$name.patch
echo "Created $LUNA_ROOT/ports/$name/$name.patch. Remember to add patching code to the PACKAGE file!"
;;
cleanup)
rm -rfv $WORKDIR/*$name*
;;
*)
echo "No such subcommand. Run 'tools/package-helper.sh help $name' for help."
;;
esac

View File

@ -39,7 +39,7 @@ unset CXX
unset LD unset LD
unset AR unset AR
../binutils-$LUNA_BINUTILS_VERSION_REQUIRED/configure --prefix="$BUILD_PREFIX" --target=$BUILD_TARGET --disable-nls --with-sysroot=$BUILD_SYSROOT --disable-werror --enable-warn-rwx-segments=no ../binutils-$LUNA_BINUTILS_VERSION_REQUIRED/configure --prefix="$BUILD_PREFIX" --target=$BUILD_TARGET --disable-nls --with-sysroot=$BUILD_SYSROOT --disable-werror --enable-warn-rwx-segments=no --enable-initfini-array
echo Building Binutils... echo Building Binutils...

View File

@ -51,7 +51,7 @@ unset CXX
unset LD unset LD
unset AR unset AR
../gcc-$LUNA_GCC_VERSION_REQUIRED/configure --prefix="$BUILD_PREFIX" --target=$BUILD_TARGET --disable-nls --with-sysroot=$BUILD_SYSROOT --enable-languages=c,c++ ../gcc-$LUNA_GCC_VERSION_REQUIRED/configure --prefix="$BUILD_PREFIX" --target=$BUILD_TARGET --disable-nls --with-sysroot=$BUILD_SYSROOT --enable-languages=c,c++ --enable-initfini-array
echo Building GCC... echo Building GCC...