feat: native GCC - cc1, cc1plus and the gcc driver run on MontaukOS

Cross-build GCC 14.2.0 with --host=x86_64-montauk: the driver, cc1,
cc1plus and collect2 now link as static Montauk ET_EXEC binaries
against the hosted libstdc++, and ship in the SDK. Together with the
native binutils, TCC and the target sysroot, MontaukOS carries a
complete self-contained C and C++ toolchain at 0:/sdk.

libc: anonymous mmap/munmap over SYS_ALLOC (GCC's page allocator
wants mmap; SYS_ALLOC memory is page-aligned by construction),
getpagesize, minimal sysconf, MB_CUR_MAX, isascii/toascii, POSIX id
typedefs (ino_t, dev_t, nlink_t, ...), struct stat fields switched to
proper POSIX types (st_size is a signed off_t now), and a
conventional _STDIO_H guard marker so GMP's FILE detection works.

Build recipe and the five host-build gotchas (bundled config.subs,
gettext removal, stdio guard sniffing, endianness cache preset,
sysroot sdk symlink) are documented in toolchain/README.md. The
devkit stages the driver as gcc.elf/g++.elf/cpp.elf, the backends in
the libexec layout the driver expects, a copy of ld where collect2
searches, and a 0:/tmp scratch dir. Ramdisk: 1604 files, 368 MB.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
2026-07-16 23:51:30 +02:00
co-authored by Claude Fable 5
parent 03d1a1bc2f
commit f234c67a2b
13 changed files with 195 additions and 17 deletions
+20
View File
@@ -405,6 +405,26 @@ ifneq ($(wildcard $(NATIVE_BIN)/as),)
cp -r ../toolchain/local/x86_64-montauk/include/c++ $(BINDIR)/sdk/include/c++
../toolchain/local/bin/x86_64-montauk-gcc -O2 ../toolchain/files/tls-test.c -o $(BINDIR)/sdk/bin/tls-test.elf
../toolchain/local/bin/x86_64-montauk-g++ -O2 ../toolchain/files/cxx-test.cpp -o $(BINDIR)/sdk/bin/cxx-test.elf
ifneq ($(wildcard ../toolchain/native-gcc/sdk/bin/gcc),)
cp ../toolchain/native-gcc/sdk/bin/gcc $(BINDIR)/sdk/bin/gcc.elf
cp ../toolchain/native-gcc/sdk/bin/g++ $(BINDIR)/sdk/bin/g++.elf
cp ../toolchain/native-gcc/sdk/bin/cpp $(BINDIR)/sdk/bin/cpp.elf
mkdir -p $(BINDIR)/sdk/libexec/gcc/x86_64-montauk/14.2.0
cp ../toolchain/native-gcc/sdk/libexec/gcc/x86_64-montauk/14.2.0/cc1 \
../toolchain/native-gcc/sdk/libexec/gcc/x86_64-montauk/14.2.0/cc1plus \
../toolchain/native-gcc/sdk/libexec/gcc/x86_64-montauk/14.2.0/collect2 \
$(BINDIR)/sdk/libexec/gcc/x86_64-montauk/14.2.0/
cp -r ../toolchain/native-gcc/sdk/lib/gcc $(BINDIR)/sdk/lib/gcc
cp ../toolchain/local/lib/gcc/x86_64-montauk/14.2.0/libgcc.a \
../toolchain/local/lib/gcc/x86_64-montauk/14.2.0/crtbegin.o \
../toolchain/local/lib/gcc/x86_64-montauk/14.2.0/crtend.o \
$(BINDIR)/sdk/lib/gcc/x86_64-montauk/14.2.0/
cp ../toolchain/native/sdk/bin/ld $(BINDIR)/sdk/libexec/gcc/x86_64-montauk/14.2.0/ld
mkdir -p $(BINDIR)/tmp
printf 'scratch space for compiler intermediates\n' > $(BINDIR)/tmp/readme.txt
else
@echo "devkit: toolchain/native-gcc/ not built; skipping native GCC"
endif
else
@echo "devkit: toolchain/native/ not built; skipping native dev tools"
endif
+3
View File
@@ -16,6 +16,9 @@ int islower(int c);
int isprint(int c);
int ispunct(int c);
int isxdigit(int c);
#define isascii(c) (((c) & ~0x7F) == 0)
#define toascii(c) ((c) & 0x7F)
int iscntrl(int c);
int isgraph(int c);
int toupper(int c);
+6
View File
@@ -1,6 +1,12 @@
#ifndef _LIBC_STDIO_H
#define _LIBC_STDIO_H
/* Conventional guard marker: packages like GMP sniff the libc's stdio
include-guard name to detect that FILE is available. */
#ifndef _STDIO_H
#define _STDIO_H 1
#endif
#pragma once
#include <stdarg.h>
+2
View File
@@ -62,6 +62,8 @@ int mkstemp(char *template_);
char *mktemp(char *template_);
char *realpath(const char *path, char *resolved);
size_t mbstowcs(wchar_t *dst, const char *src, size_t n);
#define MB_CUR_MAX 1
int mblen(const char *s, size_t n);
int mbtowc(wchar_t *pwc, const char *s, size_t n);
int wctomb(char *s, wchar_t wc);
+41
View File
@@ -0,0 +1,41 @@
#ifndef _LIBC_SYS_MMAN_H
#define _LIBC_SYS_MMAN_H
#pragma once
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Anonymous-memory mmap over SYS_ALLOC. SYS_ALLOC returns
* page-aligned process memory, which is exactly what callers like
* GCC's page allocator need. File-backed mappings are not
* supported and fail with ENODEV.
*/
#define PROT_NONE 0
#define PROT_READ 1
#define PROT_WRITE 2
#define PROT_EXEC 4
#define MAP_SHARED 0x01
#define MAP_PRIVATE 0x02
#define MAP_FIXED 0x10
#define MAP_ANONYMOUS 0x20
#define MAP_ANON MAP_ANONYMOUS
#define MAP_FAILED ((void *)-1)
void *mmap(void *addr, size_t length, int prot, int flags, int fd,
long offset);
int munmap(void *addr, size_t length);
int mprotect(void *addr, size_t length, int prot);
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_SYS_MMAN_H */
+12 -12
View File
@@ -39,18 +39,18 @@ extern "C" {
#define S_IXOTH 0001
struct stat {
unsigned long st_dev;
unsigned long st_ino;
mode_t st_mode;
unsigned long st_nlink;
uid_t st_uid;
gid_t st_gid;
unsigned long st_size;
unsigned long st_blksize;
unsigned long st_blocks;
time_t st_atime;
time_t st_mtime;
time_t st_ctime;
dev_t st_dev;
ino_t st_ino;
mode_t st_mode;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
off_t st_size;
blksize_t st_blksize;
blkcnt_t st_blocks;
time_t st_atime;
time_t st_mtime;
time_t st_ctime;
};
int mkdir(const char *path, unsigned int mode);
+7
View File
@@ -21,6 +21,13 @@ typedef unsigned int mode_t;
typedef unsigned int uid_t;
typedef unsigned int gid_t;
typedef long time_t;
typedef unsigned long ino_t;
typedef unsigned long dev_t;
typedef unsigned long nlink_t;
typedef long blkcnt_t;
typedef long blksize_t;
typedef long suseconds_t;
typedef int clockid_t;
#ifdef __cplusplus
}
+8
View File
@@ -39,6 +39,14 @@ int execve(const char *path, char *const argv[], char *const envp[]);
int execvp(const char *file, char *const argv[]);
int pipe(int fds[2]);
long pathconf(const char *path, int name);
int getpagesize(void);
long sysconf(int name);
/* sysconf() names (Linux numbering). */
#define _SC_OPEN_MAX 4
#define _SC_PAGESIZE 30
#define _SC_PAGE_SIZE 30
#define _SC_NPROCESSORS_ONLN 84
/* pathconf() names (Linux numbering). */
#define _PC_LINK_MAX 0
+56 -4
View File
@@ -22,6 +22,7 @@
#include <sys/stat.h>
#include <utime.h>
#include <spawn.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <unistd.h>
@@ -2286,7 +2287,7 @@ int stat(const char *path, struct stat *buf) {
int h = (int)_zos_syscall1(SYS_OPEN, (long)path);
if (h >= 0) {
buf->st_mode = S_IFREG;
buf->st_size = (unsigned long)_zos_syscall1(SYS_GETSIZE, (long)h);
buf->st_size = (off_t)_zos_syscall1(SYS_GETSIZE, (long)h);
_zos_syscall1(SYS_CLOSE, (long)h);
/* No inodes in the VFS API: derive a stable id from the path
so that distinct paths compare as distinct files. */
@@ -2297,7 +2298,7 @@ int stat(const char *path, struct stat *buf) {
buf->st_ino = ino | 1;
buf->st_nlink = 1;
buf->st_blksize = 4096;
buf->st_blocks = (buf->st_size + 511) / 512;
buf->st_blocks = (blkcnt_t)((buf->st_size + 511) / 512);
return 0;
}
@@ -2318,13 +2319,13 @@ int fstat(int fd, struct stat *buf) {
}
memset(buf, 0, sizeof(*buf));
buf->st_mode = S_IFREG;
buf->st_size = (unsigned long)_zos_syscall1(SYS_GETSIZE, (long)fd);
buf->st_size = (off_t)_zos_syscall1(SYS_GETSIZE, (long)fd);
/* Handles do not map back to paths, so fake a per-handle inode in a
range disjoint from the path hashes used by stat(). */
buf->st_ino = 0x4000000000000000UL + (unsigned long)fd;
buf->st_nlink = 1;
buf->st_blksize = 4096;
buf->st_blocks = (buf->st_size + 511) / 512;
buf->st_blocks = (blkcnt_t)((buf->st_size + 511) / 512);
return 0;
}
@@ -3711,6 +3712,57 @@ int kill(pid_t pid, int sig) {
return 0;
}
int getpagesize(void) {
return 4096;
}
long sysconf(int name) {
switch (name) {
case 4: /* _SC_OPEN_MAX */
return 64;
case 30: /* _SC_PAGESIZE */
return 4096;
case 84: /* _SC_NPROCESSORS_ONLN */
return 1;
default:
errno = EINVAL;
return -1;
}
}
/* Anonymous mappings only: SYS_ALLOC hands back zeroed page-aligned
memory and SYS_FREE releases it. Length is remembered by the kernel
per allocation, so munmap ignores its length argument. */
void *mmap(void *addr, size_t length, int prot, int flags, int fd,
long offset) {
(void)addr; (void)prot; (void)offset;
if (length == 0 || fd != -1 || !(flags & MAP_ANONYMOUS)) {
errno = ENODEV;
return MAP_FAILED;
}
void *p = (void *)_zos_syscall1(SYS_ALLOC, (long)length);
if (p == NULL) {
errno = ENOMEM;
return MAP_FAILED;
}
return p;
}
int munmap(void *addr, size_t length) {
(void)length;
if (addr == NULL || addr == MAP_FAILED) {
errno = EINVAL;
return -1;
}
_zos_syscall1(SYS_FREE, (long)addr);
return 0;
}
int mprotect(void *addr, size_t length, int prot) {
(void)addr; (void)length; (void)prot;
return 0; /* page protections are not adjustable from userspace */
}
long pathconf(const char *path, int name) {
(void)path;
switch (name) {
Binary file not shown.