feat: hosted libstdc++ for x86_64-montauk - full C++ on MontaukOS

The cross toolchain now builds real hosted C++: std::string, vector,
unique_ptr, iostreams and exceptions all work in a plain
x86_64-montauk-g++ invocation with no special flags. This is the
library that native cc1plus will link against for the GCC self-host.

crt1 now runs .preinit_array/.init_array before main and hooks
.fini_array through atexit (main returns via exit(), so destructors
run on both exit paths). Global constructors and libgcc's eh_frame
registration both ride this. GCC is reconfigured with
--enable-initfini-array (modern arrays instead of .ctors),
--with-newlib (satisfies libstdc++'s crossconfig for unknown hosts),
--disable-wchar_t and --disable-libstdcxx-pch; the montauk gcc patch
grows a hunk keeping os/generic ctype under --with-newlib (the
Montauk libc is not newlib).

libc additions harvested by the libstdc++ build: mbtowc/wctomb,
strxfrm, strtok/strtok_r, a real vfscanf/fscanf/scanf (character-
driven with widths and l/ll; also unblocks gprof later), fgetpos/
fsetpos/setbuf/fpos_t, modf plus the C99 float math variants
(acosf..tanhf, hypot/hypotf as wrappers), FP_* classification macros,
the full POSIX errno vocabulary, complete DT_* dirent types, and
mbstate_t.

The SDK ships the real libstdc++.a (was an empty stand-in), the C++
headers at 0:/sdk/include/c++, and cxx-test.elf (global ctor, string,
vector+sort, unique_ptr, throw/catch; exits 0 on 5/5). Ramdisk sits
at 1571 files, within the 2048 table.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
2026-07-16 23:06:50 +02:00
co-authored by Claude Fable 5
parent eadc79708f
commit 03d1a1bc2f
19 changed files with 480 additions and 8 deletions
+16 -5
View File
@@ -128,10 +128,15 @@ cp "${REPO_DIR}/programs/lib/libc/crt1.o" \
"${REPO_DIR}/programs/lib/libc/crti.o" \
"${REPO_DIR}/programs/lib/libc/crtn.o" "${SYSROOT}/usr/lib/"
cp "${REPO_DIR}/programs/lib/libc/liblibc.a" "${SYSROOT}/usr/lib/libc.a"
# Empty stand-ins: math functions live inside libc.a, and there is no
# libstdc++ yet, but the g++ driver links -lstdc++ -lm unconditionally.
# libm is an empty stand-in (math lives inside libc.a; the g++ driver
# links -lm unconditionally). libstdc++ is copied from the toolchain
# once built - the empty stand-in only bridges the first bootstrap.
ar rcs "${SYSROOT}/usr/lib/libm.a"
ar rcs "${SYSROOT}/usr/lib/libstdc++.a"
if [[ -f "${PREFIX}/x86_64-montauk/lib/libstdc++.a" ]]; then
cp "${PREFIX}/x86_64-montauk/lib/libstdc++.a" "${SYSROOT}/usr/lib/libstdc++.a"
else
ar rcs "${SYSROOT}/usr/lib/libstdc++.a"
fi
green "Sysroot ready."
# ── Build Binutils ────────────────────────────────────────────────────────────
@@ -177,13 +182,19 @@ else
--disable-multilib \
--disable-gcov \
--with-gnu-as \
--with-gnu-ld
--with-gnu-ld \
--with-newlib \
--enable-initfini-array \
--disable-wchar_t \
--disable-libstdcxx-pch
make -j"${JOBS}" all-gcc
make -j"${JOBS}" all-target-libgcc
make install-gcc
make install-target-libgcc
make -j"${JOBS}" all-target-libstdc++-v3
make install-target-libstdc++-v3
)
green "GCC installed."
green "GCC and libstdc++ installed."
fi
# ── Done ──────────────────────────────────────────────────────────────────────
+35
View File
@@ -0,0 +1,35 @@
#include <cstdio>
#include <string>
#include <vector>
#include <stdexcept>
#include <memory>
#include <algorithm>
static int ctor_ran = 0;
struct Global { Global() { ctor_ran = 42; } };
static Global g;
int main() {
int score = 0;
if (ctor_ran == 42) { puts("global ctor: OK"); score++; }
std::string s = "montauk";
s += "-cxx";
if (s == "montauk-cxx" && s.size() == 11) { puts("std::string: OK"); score++; }
std::vector<int> v{5, 3, 1, 4, 2};
std::sort(v.begin(), v.end());
if (v.front() == 1 && v.back() == 5) { puts("std::vector+sort: OK"); score++; }
auto p = std::make_unique<std::string>("heap");
if (*p == "heap") { puts("unique_ptr/new: OK"); score++; }
try {
throw std::runtime_error("caught!");
} catch (const std::exception& e) {
if (std::string(e.what()) == "caught!") { puts("exceptions: OK"); score++; }
}
printf("%d/5 passed\n", score);
return (score == 5) ? 0 : 1;
}
@@ -47,3 +47,15 @@
*-musl* )
# IF there is no include fixing,
# THEN create a no-op fixer and exit
--- a/libstdc++-v3/configure
+++ b/libstdc++-v3/configure
@@ -27340,6 +27340,9 @@
# on a hosted environment.
if test "x${with_newlib}" = "xyes"; then
os_include_dir="os/newlib"
+ case "${host}" in
+ *-montauk*) os_include_dir="os/generic" ;; # Montauk libc is not newlib
+ esac
$as_echo "#define HAVE_HYPOT 1" >>confdefs.h