liblinux++

A hosted C++ runtime without any libc.
git clone git://henryandlizzy.uk/liblinux++
Log | Files | Refs

commit 0dda04fa914c3932d10a52f3517ed51650aa8f64
parent 0a27f7f2616afb73e29ca56a05004d715dc17a1c
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Tue,  6 Jan 2026 01:00:07 +0000

Add errno.hpp generation and exec

Diffstat:
MTupfile | 6++++--
Aerrno.sh | 6++++++
Aexec.cpp | 17+++++++++++++++++
Mlinux.hpp | 12+++---------
Msyscalls.h | 4++++
5 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/Tupfile b/Tupfile @@ -1,8 +1,10 @@ CXXFLAGS = -std=c++23 -Werror -g -fno-exceptions -fno-rtti -fno-asynchronous-unwind-tables -fno-builtin -fno-pic -fno-pie -fno-stack-protector -fdiagnostics-color=always LDFLAGS = --gc-sections -: foreach *.cpp |> ^j^ clang++ --target=aarch64 $(CXXFLAGS) -c -o %o %f |> obj/%B.aarch64.o {objs-aarch64} -: foreach *.cpp |> ^j^ clang++ --target=x86_64 $(CXXFLAGS) -c -o %o %f |> obj/%B.x86_64.o {objs-x86_64} +: |> ./errno.sh > %o |> errno.hpp {pre-compile} + +: foreach *.cpp | {pre-compile} |> ^j^ clang++ --target=aarch64 $(CXXFLAGS) -c -o %o %f |> obj/%B.aarch64.o {objs-aarch64} +: foreach *.cpp | {pre-compile} |> ^j^ clang++ --target=x86_64 $(CXXFLAGS) -c -o %o %f |> obj/%B.x86_64.o {objs-x86_64} : foreach *.S |> clang --target=%B -c -o %o %f |> obj/%B.o diff --git a/errno.sh b/errno.sh @@ -0,0 +1,6 @@ +#!/bin/sh +set -eu + +echo -e '#pragma once\n\nenum class errno_t : int16_t {' +sed -En 's/#[[:space:]]*define[[:space:]]+E([[:alnum:]]+)[[:space:]]+([[:digit:]]+)/E\1 = -\2,/p' /usr/include/asm-generic/errno{,-base}.h +echo '};' diff --git a/exec.cpp b/exec.cpp @@ -0,0 +1,17 @@ +#include "linux.hpp" +#include "errno.hpp" + +int main(int argc, char* argv[], char* envp[]) +{ + if (argc < 2) + return 1; + switch (execve(argv[1], argv + 1, envp).err()) + { + case errno_t::ENOENT: + *write(stderr, "ENOENT: No such file or directory\n"); + break; + default: + __builtin_trap(); + } + return 1; +} diff --git a/linux.hpp b/linux.hpp @@ -92,15 +92,7 @@ constexpr file const stdin{0}; constexpr file const stdout{1}; constexpr file const stderr{2}; -enum class errno_t : int16_t -{ - EPERM = -1, /* Operation not permitted */ - ENOENT = -2, /* No such file or directory */ - ESRCH = -3, /* No such process */ - EINTR = -4, /* Interrupted system call */ - - EINVAL = -22, /* Invalid argument */ -}; +enum class errno_t : int16_t; template <typename T> struct [[nodiscard]] syscall_result @@ -197,6 +189,8 @@ enum class unlink_flags : int extern syscall_result<void> unlinkat(file dir, c_str path, unlink_flags flags); +extern syscall_result<void> execve(c_str path, char *const /*_Nullable*/ argv[], char *const /*_Nullable*/ envp[]); + extern "C" { diff --git a/syscalls.h b/syscalls.h @@ -28,6 +28,10 @@ extern close extern_alias _Z5close4file SYS(close) +extern execve +extern_alias _Z6execve5c_strPKPcS2_ +SYS(execve) + extern exit extern_alias _Z4exiti SYS(exit)