aboutsummaryrefslogtreecommitdiff
path: root/programs/libc
diff options
context:
space:
mode:
Diffstat (limited to 'programs/libc')
-rw-r--r--programs/libc/CMakeLists.txt27
-rw-r--r--programs/libc/fcntl/CMakeLists.txt19
-rw-r--r--programs/libc/fcntl/open.c41
-rw-r--r--programs/libc/malloc_init.c29
-rw-r--r--programs/libc/start.c72
-rw-r--r--programs/libc/sys/CMakeLists.txt18
-rw-r--r--programs/libc/sys/mount/CMakeLists.txt19
-rw-r--r--programs/libc/sys/mount/mount.c32
-rw-r--r--programs/libc/sys/stat/CMakeLists.txt20
-rw-r--r--programs/libc/sys/stat/mkdir.c30
-rw-r--r--programs/libc/sys/stat/umask.c29
-rw-r--r--programs/libc/unistd/CMakeLists.txt21
-rw-r--r--programs/libc/unistd/close.c30
-rw-r--r--programs/libc/unistd/sbrk.c56
-rw-r--r--programs/libc/unistd/write.c32
15 files changed, 475 insertions, 0 deletions
diff --git a/programs/libc/CMakeLists.txt b/programs/libc/CMakeLists.txt
new file mode 100644
index 0000000..a0c6d97
--- /dev/null
+++ b/programs/libc/CMakeLists.txt
@@ -0,0 +1,27 @@
+# wnix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+set(LIBC_FREESTANDING ON)
+set(libc_dir ${CMAKE_CURRENT_LIST_DIR}/../../src/libc)
+add_subdirectory(${libc_dir} ${CMAKE_CURRENT_BINARY_DIR}/libc)
+add_subdirectory(fcntl)
+add_subdirectory(sys)
+add_subdirectory(unistd)
+target_compile_definitions(c PRIVATE PRINTF_DISABLE_SUPPORT_FLOAT)
+target_sources(c PRIVATE
+ malloc_init.c
+ start.c
+)
diff --git a/programs/libc/fcntl/CMakeLists.txt b/programs/libc/fcntl/CMakeLists.txt
new file mode 100644
index 0000000..c0ca1d1
--- /dev/null
+++ b/programs/libc/fcntl/CMakeLists.txt
@@ -0,0 +1,19 @@
+# wnix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+target_sources(c PRIVATE
+ open.c
+)
diff --git a/programs/libc/fcntl/open.c b/programs/libc/fcntl/open.c
new file mode 100644
index 0000000..71511a7
--- /dev/null
+++ b/programs/libc/fcntl/open.c
@@ -0,0 +1,41 @@
+/*
+ * wnix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <fcntl.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <stdarg.h>
+
+int open(const char *const path, const int flags, ...)
+{
+ int __wnix_open(const char *, int, mode_t, int *) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("open")
+ ));
+
+ mode_t mode = 0;
+ va_list ap;
+
+ va_start(ap, flags);
+
+ if (flags & O_CREAT)
+ mode = va_arg(ap, mode_t);
+
+ va_end(ap);
+ return __wnix_open(path, flags, mode, &errno);
+}
diff --git a/programs/libc/malloc_init.c b/programs/libc/malloc_init.c
new file mode 100644
index 0000000..969f523
--- /dev/null
+++ b/programs/libc/malloc_init.c
@@ -0,0 +1,29 @@
+/*
+ * wnix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <libc/malloc.h>
+#include <tinyalloc.h>
+#include <stdint.h>
+
+void __malloc_init(void)
+{
+ /* README.md states 16 is a "good default value". */
+ if (!ta_init(64, 16, sizeof (intmax_t)))
+ abort();
+}
diff --git a/programs/libc/start.c b/programs/libc/start.c
new file mode 100644
index 0000000..5e56239
--- /dev/null
+++ b/programs/libc/start.c
@@ -0,0 +1,72 @@
+/*
+ * wnix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+void _start(void)
+{
+ /* clang mangles main if argc/argv are present:
+ * https://reviews.llvm.org/D127888 */
+ int __main_argc_argv(int, char *[]);
+ int __wnix_argc(void) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("argc")
+ ));
+ _Noreturn void __wnix_exit(int) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("exit")
+ ));
+
+ if (!(__stdin = fopen("/dev/stdin", "rb"))
+ || !(__stdout = fopen("/dev/stdout", "wb"))
+ || !(__stderr = fopen("/dev/stderr", "wb")))
+ __wnix_exit(EXIT_FAILURE);
+
+ const int argc = __wnix_argc();
+ char **argv = malloc((argc + 1) * sizeof *argv);
+
+ if (!argv)
+ __wnix_exit(EXIT_FAILURE);
+
+ for (int i = 0; i < argc; i++)
+ {
+ size_t __wnix_arglen(int) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("arglen")
+ ));
+
+ int __wnix_argcopy(int, char *, int *) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("argcopy")
+ ));
+
+ char *const arg = malloc(__wnix_arglen(i));
+
+ if (!arg)
+ __wnix_exit(EXIT_FAILURE);
+
+ __wnix_argcopy(i, arg, &errno);
+ argv[i] = arg;
+ }
+
+ argv[argc] = NULL;
+ __wnix_exit(__main_argc_argv(argc, argv));
+}
diff --git a/programs/libc/sys/CMakeLists.txt b/programs/libc/sys/CMakeLists.txt
new file mode 100644
index 0000000..ea57403
--- /dev/null
+++ b/programs/libc/sys/CMakeLists.txt
@@ -0,0 +1,18 @@
+# wnix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+add_subdirectory(mount)
+add_subdirectory(stat)
diff --git a/programs/libc/sys/mount/CMakeLists.txt b/programs/libc/sys/mount/CMakeLists.txt
new file mode 100644
index 0000000..222e506
--- /dev/null
+++ b/programs/libc/sys/mount/CMakeLists.txt
@@ -0,0 +1,19 @@
+# wnix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+target_sources(c PRIVATE
+ mount.c
+)
diff --git a/programs/libc/sys/mount/mount.c b/programs/libc/sys/mount/mount.c
new file mode 100644
index 0000000..2512f73
--- /dev/null
+++ b/programs/libc/sys/mount/mount.c
@@ -0,0 +1,32 @@
+/*
+ * wnix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <sys/mount.h>
+#include <errno.h>
+
+int mount(const char *const src, const char *const tgt, const char *const type,
+ const unsigned long flags, const void *const args)
+{
+ int __wnix_mount(const char *, const char *, const char *,
+ unsigned long, const void *, int *) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("mount")
+ ));
+
+ return __wnix_mount(src, tgt, type, flags, args, &errno);
+}
diff --git a/programs/libc/sys/stat/CMakeLists.txt b/programs/libc/sys/stat/CMakeLists.txt
new file mode 100644
index 0000000..60a8d6c
--- /dev/null
+++ b/programs/libc/sys/stat/CMakeLists.txt
@@ -0,0 +1,20 @@
+# wnix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+target_sources(c PRIVATE
+ mkdir.c
+ umask.c
+)
diff --git a/programs/libc/sys/stat/mkdir.c b/programs/libc/sys/stat/mkdir.c
new file mode 100644
index 0000000..5547616
--- /dev/null
+++ b/programs/libc/sys/stat/mkdir.c
@@ -0,0 +1,30 @@
+/*
+ * wnix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <sys/stat.h>
+#include <errno.h>
+
+int mkdir(const char *const pathname, mode_t flags)
+{
+ int __wnix_mkdir(const char *, mode_t, int *) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("mkdir")
+ ));
+
+ return __wnix_mkdir(pathname, flags, &errno);
+}
diff --git a/programs/libc/sys/stat/umask.c b/programs/libc/sys/stat/umask.c
new file mode 100644
index 0000000..f42a6e5
--- /dev/null
+++ b/programs/libc/sys/stat/umask.c
@@ -0,0 +1,29 @@
+/*
+ * wnix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <sys/stat.h>
+
+mode_t umask(const mode_t mask)
+{
+ int __wnix_umask(mode_t) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("umask")
+ ));
+
+ return __wnix_umask(mask);
+}
diff --git a/programs/libc/unistd/CMakeLists.txt b/programs/libc/unistd/CMakeLists.txt
new file mode 100644
index 0000000..e3481bc
--- /dev/null
+++ b/programs/libc/unistd/CMakeLists.txt
@@ -0,0 +1,21 @@
+# wnix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+target_sources(c PRIVATE
+ close.c
+ sbrk.c
+ write.c
+)
diff --git a/programs/libc/unistd/close.c b/programs/libc/unistd/close.c
new file mode 100644
index 0000000..94ca6fc
--- /dev/null
+++ b/programs/libc/unistd/close.c
@@ -0,0 +1,30 @@
+/*
+ * wnix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <unistd.h>
+#include <errno.h>
+
+int close(const int fd)
+{
+ int __wnix_close(int, int *) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("close")
+ ));
+
+ return __wnix_close(fd, &errno);
+}
diff --git a/programs/libc/unistd/sbrk.c b/programs/libc/unistd/sbrk.c
new file mode 100644
index 0000000..7e32118
--- /dev/null
+++ b/programs/libc/unistd/sbrk.c
@@ -0,0 +1,56 @@
+/*
+ * wnix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <errno.h>
+#include <unistd.h>
+#include <stddef.h>
+#include <stdint.h>
+
+void *sbrk(const intptr_t increment)
+{
+ const unsigned pagesize = 64 << 10;
+ extern char __heap_base[];
+ static char *cur = __heap_base, *end;
+
+ if (increment < 0)
+ {
+ errno = EINVAL;
+ return NULL;
+ }
+ else if (!end)
+ end = cur + __builtin_wasm_memory_size(0) * pagesize;
+
+ if (increment >= (intptr_t)end || cur >= end - increment)
+ {
+ const unsigned long pages = (uintptr_t)increment / pagesize,
+ new_end = __builtin_wasm_memory_grow(0, pages);
+
+ if (!new_end)
+ {
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ end = (void *)new_end;
+ }
+
+ void *ret = cur;
+
+ cur += increment;
+ return ret;
+}
diff --git a/programs/libc/unistd/write.c b/programs/libc/unistd/write.c
new file mode 100644
index 0000000..9151e61
--- /dev/null
+++ b/programs/libc/unistd/write.c
@@ -0,0 +1,32 @@
+/*
+ * wnix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <stddef.h>
+
+ssize_t write(const int fd, const void *const p, const size_t sz)
+{
+ int __wnix_write(int, const void *, size_t, int *) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("write")
+ ));
+
+ return __wnix_write(fd, p, sz, &errno);
+}