liblinux++

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

commit 9d096e3cbb5f365d99363d2c377435309f909765
parent 6bd1cb7de768c20fdda94bb18c8ed462204c8590
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Wed, 24 Dec 2025 21:37:57 +0000

Fix size_t, trial global allocation, disable exceptions

Diffstat:
MTupfile | 2+-
Maarch64.s | 10+++++++---
Mlinux.hpp | 33+++++++++++++++++----------------
Anew.cpp | 26++++++++++++++++++++++++++
Mx86_64.s | 10+++++++---
5 files changed, 58 insertions(+), 23 deletions(-)

diff --git a/Tupfile b/Tupfile @@ -1,4 +1,4 @@ -CXXFLAGS = -g -Os -fno-asynchronous-unwind-tables -fno-builtin -fno-pic -fno-pie -fno-stack-protector -fdiagnostics-color=always +CXXFLAGS = -g -Os -fno-exceptions -fno-asynchronous-unwind-tables -fno-builtin -fno-pic -fno-pie -fno-stack-protector -fdiagnostics-color=always LDFLAGS = --gc-sections : foreach *.cpp |> clang++ --target=aarch64 $(CXXFLAGS) -c -o %o %f |> obj/%B.aarch64.o {objs-aarch64} diff --git a/aarch64.s b/aarch64.s @@ -29,15 +29,15 @@ extern_alias \name .global exit extern getdents64 -extern_alias _Z10getdents644fileP14linux_dirent64y +extern_alias _Z10getdents644fileP14linux_dirent64m syscall 61 extern read -extern_alias _Z4read4filePcy +extern_alias _Z4read4filePcm syscall 63 extern write -extern_alias _Z5write4filePKcy +extern_alias _Z5write4filePKcm extern_alias _Z5write4file4spanIKcE syscall 64 @@ -61,6 +61,10 @@ extern nanosleep extern_alias _Z9nanosleepRK8timespecPS_ syscall 101 +extern munmap +extern_alias _Z6munmapPvm +syscall 215 + extern mmap extern_alias _Z4mmapPvmmm4filem syscall 222 diff --git a/linux.hpp b/linux.hpp @@ -1,7 +1,7 @@ #pragma once -using uint64_t = unsigned long long; -using int64_t = signed long long; +using uint64_t = unsigned long; +using int64_t = signed long; using uint32_t = unsigned int; using int32_t = signed int; using uint16_t = unsigned short; @@ -123,26 +123,26 @@ private: }; [[noreturn]] -extern void exit(int error_code); +extern void exit(int error_code) noexcept; -extern syscall_result<size_t> read(file fd, char data[], size_t count); -extern syscall_result<size_t> read(file fd, span<char> data); -inline syscall_result<size_t> read(file fd, char* begin, char* end) +extern syscall_result<size_t> read(file fd, char data[], size_t count) noexcept; +extern syscall_result<size_t> read(file fd, span<char> data) noexcept; +inline syscall_result<size_t> read(file fd, char* begin, char* end) noexcept { return read(fd, begin, end - begin); } -extern syscall_result<size_t> write(file fd, char const data[], size_t count); -extern syscall_result<size_t> write(file fd, span<char const> data); -extern syscall_result<size_t> write(file fd, span<span<char const> const> data); -inline syscall_result<size_t> write(file fd, char const* begin, char const* end) +extern syscall_result<size_t> write(file fd, char const data[], size_t count) noexcept; +extern syscall_result<size_t> write(file fd, span<char const> data) noexcept; +extern syscall_result<size_t> write(file fd, span<span<char const> const> data) noexcept; +inline syscall_result<size_t> write(file fd, char const* begin, char const* end) noexcept { return write(fd, begin, end - begin); } -extern syscall_result<file> openat(file fd, c_str name, int flags, int mode); +extern syscall_result<file> openat(file fd, c_str name, int flags, int mode) noexcept; -extern syscall_result<void> close(file fd); +extern syscall_result<void> close(file fd) noexcept; struct linux_dirent64 { ino64_t d_ino; /* 64-bit inode number */ @@ -151,16 +151,17 @@ struct linux_dirent64 { unsigned char d_type; /* File type */ char d_name[]; /* Filename (null-terminated) */ }; -extern syscall_result<size_t> getdents64(file dir, linux_dirent64 dirp[], size_t count); +extern syscall_result<size_t> getdents64(file dir, linux_dirent64 dirp[], size_t count) noexcept; -extern syscall_result<void*> mmap(void* addr, unsigned long len, unsigned long prot, unsigned long flags, file fd, unsigned long off); +extern syscall_result<void*> mmap(void* addr, size_t len, unsigned long prot, unsigned long flags, file fd, size_t off) noexcept; +extern syscall_result<void> munmap(void* addr, size_t len) noexcept; struct timespec { time_t tv_sec; /* Seconds */ uint32_t tv_nsec; /* Nanoseconds [0, 999'999'999] */ }; -extern syscall_result<void> nanosleep(const timespec &duration, timespec */*_Nullable*/ rem); +extern syscall_result<void> nanosleep(const timespec &duration, timespec */*_Nullable*/ rem) noexcept; enum class rename_flags : unsigned int { @@ -169,4 +170,4 @@ enum class rename_flags : unsigned int WHITEOUT = 0b100, }; -extern syscall_result<void> renameat2(file olddir, c_str oldpath, file newdir, c_str newpath, rename_flags flags); +extern syscall_result<void> renameat2(file olddir, c_str oldpath, file newdir, c_str newpath, rename_flags flags) noexcept; diff --git a/new.cpp b/new.cpp @@ -0,0 +1,26 @@ +#include "linux.hpp" + +namespace std { + +struct nothrow_t {}; +constexpr nothrow_t nothrow; + +} + +void* operator new(size_t size, std::nothrow_t const&) noexcept +{ + return *mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, {}, 0); +} + +void operator delete(void* p, size_t size) noexcept +{ + *munmap(p, size); +} + +int main() +{ + auto p = new (std::nothrow) int{5}; + int x = *p; + delete p; + return x; +} diff --git a/x86_64.s b/x86_64.s @@ -45,11 +45,11 @@ syscall6: ret extern read -extern_alias _Z4read4filePcy +extern_alias _Z4read4filePcm _syscall 0 syscall3 extern write -extern_alias _Z5write4filePKcy +extern_alias _Z5write4filePKcm extern_alias _Z5write4file4spanIKcE _syscall 1 syscall3 @@ -61,6 +61,10 @@ extern mmap extern_alias _Z4mmapPvmmm4filem _syscall 9 syscall6 +extern munmap +extern_alias _Z6munmapPvm +_syscall 11 syscall3 + extern writev extern_alias _Z5write4file4spanIKS0_IKcEE _syscall 20 syscall3 @@ -74,7 +78,7 @@ extern_alias _Z4exiti _syscall 60 syscall3 extern getdents64 -extern_alias _Z10getdents644fileP14linux_dirent64y +extern_alias _Z10getdents644fileP14linux_dirent64m _syscall 217 syscall3 extern openat