summaryrefslogtreecommitdiff
path: root/lib/mlibc/options/internal/include
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-03-07 17:28:00 -0500
committerIan Moffett <ian@osmora.org>2024-03-07 17:28:32 -0500
commitbd5969fc876a10b18613302db7087ef3c40f18e1 (patch)
tree7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/options/internal/include
parenta95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff)
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/options/internal/include')
-rw-r--r--lib/mlibc/options/internal/include/bits/cpu_set.h13
-rw-r--r--lib/mlibc/options/internal/include/bits/ensure.h45
-rw-r--r--lib/mlibc/options/internal/include/bits/ether_addr.h10
-rw-r--r--lib/mlibc/options/internal/include/bits/inline-definition.h19
-rw-r--r--lib/mlibc/options/internal/include/bits/machine.h86
-rw-r--r--lib/mlibc/options/internal/include/bits/mbstate.h12
-rw-r--r--lib/mlibc/options/internal/include/bits/nl_item.h82
-rw-r--r--lib/mlibc/options/internal/include/bits/null.h16
-rw-r--r--lib/mlibc/options/internal/include/bits/off_t.h8
-rw-r--r--lib/mlibc/options/internal/include/bits/sigset_t.h25
-rw-r--r--lib/mlibc/options/internal/include/bits/size_t.h6
-rw-r--r--lib/mlibc/options/internal/include/bits/ssize_t.h15
-rw-r--r--lib/mlibc/options/internal/include/bits/threads.h79
-rw-r--r--lib/mlibc/options/internal/include/bits/types.h319
-rw-r--r--lib/mlibc/options/internal/include/bits/wchar.h9
-rw-r--r--lib/mlibc/options/internal/include/bits/wchar_t.h12
-rw-r--r--lib/mlibc/options/internal/include/bits/winsize.h13
-rw-r--r--lib/mlibc/options/internal/include/bits/wint_t.h6
-rw-r--r--lib/mlibc/options/internal/include/mlibc/all-sysdeps.hpp33
-rw-r--r--lib/mlibc/options/internal/include/mlibc/allocator.hpp37
-rw-r--r--lib/mlibc/options/internal/include/mlibc/bitutil.hpp34
-rw-r--r--lib/mlibc/options/internal/include/mlibc/charcode.hpp124
-rw-r--r--lib/mlibc/options/internal/include/mlibc/charset.hpp40
-rw-r--r--lib/mlibc/options/internal/include/mlibc/debug.hpp27
-rw-r--r--lib/mlibc/options/internal/include/mlibc/file-window.hpp64
-rw-r--r--lib/mlibc/options/internal/include/mlibc/fsfd_target.hpp15
-rw-r--r--lib/mlibc/options/internal/include/mlibc/global-config.hpp19
-rw-r--r--lib/mlibc/options/internal/include/mlibc/internal-sysdeps.hpp41
-rw-r--r--lib/mlibc/options/internal/include/mlibc/locale.hpp12
-rw-r--r--lib/mlibc/options/internal/include/mlibc/lock.hpp124
-rw-r--r--lib/mlibc/options/internal/include/mlibc/stack_protector.hpp10
-rw-r--r--lib/mlibc/options/internal/include/mlibc/strings.hpp12
-rw-r--r--lib/mlibc/options/internal/include/mlibc/strtofp.hpp165
-rw-r--r--lib/mlibc/options/internal/include/mlibc/strtol.hpp159
-rw-r--r--lib/mlibc/options/internal/include/mlibc/tcb.hpp180
-rw-r--r--lib/mlibc/options/internal/include/mlibc/threads.hpp27
-rw-r--r--lib/mlibc/options/internal/include/mlibc/tid.hpp18
-rw-r--r--lib/mlibc/options/internal/include/stdint.h150
38 files changed, 2066 insertions, 0 deletions
diff --git a/lib/mlibc/options/internal/include/bits/cpu_set.h b/lib/mlibc/options/internal/include/bits/cpu_set.h
new file mode 100644
index 0000000..69f6923
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/cpu_set.h
@@ -0,0 +1,13 @@
+#ifndef _MLIBC_INTERNAL_CPU_SET_H
+#define _MLIBC_INTERNAL_CPU_SET_H
+
+typedef unsigned long __cpu_mask;
+
+#define CPU_SETSIZE 1024
+#define __NCPUBITS (8 * sizeof(__cpu_mask))
+
+typedef struct {
+ __cpu_mask __bits[CPU_SETSIZE / __NCPUBITS];
+} cpu_set_t;
+
+#endif /* _MLIBC_INTERNAL_CPU_SET_H */
diff --git a/lib/mlibc/options/internal/include/bits/ensure.h b/lib/mlibc/options/internal/include/bits/ensure.h
new file mode 100644
index 0000000..f75a2e9
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/ensure.h
@@ -0,0 +1,45 @@
+
+#ifndef MLIBC_ENSURE_H
+#define MLIBC_ENSURE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef __MLIBC_ABI_ONLY
+
+void __ensure_fail(const char *assertion, const char *file, unsigned int line,
+ const char *function);
+
+void __ensure_warn(const char *assertion, const char *file, unsigned int line,
+ const char *function);
+
+#endif /* !__MLIBC_ABI_ONLY */
+
+#define __ensure(assertion) do { if(!(assertion)) \
+ __ensure_fail(#assertion, __FILE__, __LINE__, __func__); } while(0)
+
+#define MLIBC_UNIMPLEMENTED() __ensure_fail("Functionality is not implemented", \
+ __FILE__, __LINE__, __func__)
+
+#define MLIBC_MISSING_SYSDEP() __ensure_warn("Library function fails due to missing sysdep", \
+ __FILE__, __LINE__, __func__)
+
+#define MLIBC_CHECK_OR_ENOSYS(sysdep, ret) ({ \
+ if (!(sysdep)) { \
+ __ensure_warn("Library function fails due to missing sysdep", \
+ __FILE__, __LINE__, __func__); \
+ errno = ENOSYS; \
+ return (ret); \
+ } \
+ sysdep; \
+ })
+
+#define MLIBC_STUB_BODY { MLIBC_UNIMPLEMENTED(); __builtin_unreachable(); }
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // MLIBC_ENSURE_H
+
diff --git a/lib/mlibc/options/internal/include/bits/ether_addr.h b/lib/mlibc/options/internal/include/bits/ether_addr.h
new file mode 100644
index 0000000..1631e98
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/ether_addr.h
@@ -0,0 +1,10 @@
+#ifndef MLIBC_ETHER_ADDR_H
+#define MLIBC_ETHER_ADDR_H
+
+#include <stdint.h>
+
+struct ether_addr {
+ uint8_t ether_addr_octet[6];
+} __attribute__((__packed__));
+
+#endif // MLIBC_ETHER_ADDR_H
diff --git a/lib/mlibc/options/internal/include/bits/inline-definition.h b/lib/mlibc/options/internal/include/bits/inline-definition.h
new file mode 100644
index 0000000..ec4c4da
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/inline-definition.h
@@ -0,0 +1,19 @@
+#ifndef MLIBC_INLINE_DEFINITION_H
+#define MLIBC_INLINE_DEFINITION_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __MLIBC_EMIT_INLINE_DEFINITIONS
+#define __MLIBC_INLINE_DEFINITION
+#else
+#define __MLIBC_INLINE_DEFINITION __attribute__((__gnu_inline__)) extern __inline__
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // MLIBC_INLINE_DEFINITION_H
+
diff --git a/lib/mlibc/options/internal/include/bits/machine.h b/lib/mlibc/options/internal/include/bits/machine.h
new file mode 100644
index 0000000..371a94b
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/machine.h
@@ -0,0 +1,86 @@
+
+#ifndef MLIBC_MACHINE_H
+#define MLIBC_MACHINE_H
+
+#include <stdint.h>
+
+#if defined (__i386__)
+struct __mlibc_jmpbuf_register_state {
+ uint32_t ebx;
+ uint32_t ebp;
+ uint32_t esi;
+ uint32_t edi;
+ uint32_t esp;
+ uint32_t eip;
+};
+#elif defined (__x86_64__)
+struct __mlibc_jmpbuf_register_state {
+ uint64_t rbx;
+ uint64_t rbp;
+ uint64_t r12;
+ uint64_t r13;
+ uint64_t r14;
+ uint64_t r15;
+ uint64_t rsp;
+ uint64_t rip;
+};
+#elif defined (__aarch64__)
+struct __mlibc_jmpbuf_register_state {
+ uint64_t x19;
+ uint64_t x20;
+ uint64_t x21;
+ uint64_t x22;
+ uint64_t x23;
+ uint64_t x24;
+ uint64_t x25;
+ uint64_t x26;
+ uint64_t x27;
+ uint64_t x28;
+ uint64_t x29;
+ uint64_t x30;
+ uint64_t sp;
+ uint64_t pad;
+ uint64_t d8;
+ uint64_t d9;
+ uint64_t d10;
+ uint64_t d11;
+ uint64_t d12;
+ uint64_t d13;
+ uint64_t d14;
+ uint64_t d15;
+};
+#elif defined (__riscv) && __riscv_xlen == 64
+struct __mlibc_jmpbuf_register_state {
+ uint64_t ra;
+ uint64_t s0;
+ uint64_t s1;
+ uint64_t s2;
+ uint64_t s3;
+ uint64_t s4;
+ uint64_t s5;
+ uint64_t s6;
+ uint64_t s7;
+ uint64_t s8;
+ uint64_t s9;
+ uint64_t s10;
+ uint64_t s11;
+ uint64_t sp;
+ double fs0;
+ double fs1;
+ double fs2;
+ double fs3;
+ double fs4;
+ double fs5;
+ double fs6;
+ double fs7;
+ double fs8;
+ double fs9;
+ double fs10;
+ double fs11;
+};
+#else
+# error "Missing architecture specific code"
+#endif
+
+#endif // MLIBC_MACHINE_H
+
diff --git a/lib/mlibc/options/internal/include/bits/mbstate.h b/lib/mlibc/options/internal/include/bits/mbstate.h
new file mode 100644
index 0000000..2bfb3eb
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/mbstate.h
@@ -0,0 +1,12 @@
+#ifndef MLIBC_MBSTATE_H
+#define MLIBC_MBSTATE_H
+
+struct __mlibc_mbstate {
+ short __progress;
+ short __shift;
+ unsigned int __cpoint;
+};
+
+#define __MLIBC_MBSTATE_INITIALIZER {0, 0, 0}
+
+#endif // MLIBC_MBSTATE_H
diff --git a/lib/mlibc/options/internal/include/bits/nl_item.h b/lib/mlibc/options/internal/include/bits/nl_item.h
new file mode 100644
index 0000000..dc882dc
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/nl_item.h
@@ -0,0 +1,82 @@
+
+#ifndef _NL_ITEM_H
+#define _NL_ITEM_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef int nl_item;
+
+#define ABDAY_1 0x60000
+#define ABDAY_2 0x60001
+#define ABDAY_3 0x60002
+#define ABDAY_4 0x60003
+#define ABDAY_5 0x60004
+#define ABDAY_6 0x60005
+#define ABDAY_7 0x60006
+
+#define DAY_1 0x60007
+#define DAY_2 0x60008
+#define DAY_3 0x60009
+#define DAY_4 0x6000A
+#define DAY_5 0x6000B
+#define DAY_6 0x6000C
+#define DAY_7 0x6000D
+
+#define ABMON_1 0x6000E
+#define ABMON_2 0x6000F
+#define ABMON_3 0x60010
+#define ABMON_4 0x60011
+#define ABMON_5 0x60012
+#define ABMON_6 0x60013
+#define ABMON_7 0x60014
+#define ABMON_8 0x60015
+#define ABMON_9 0x60016
+#define ABMON_10 0x60017
+#define ABMON_11 0x60018
+#define ABMON_12 0x60019
+
+#define MON_1 0x6001A
+#define MON_2 0x6001B
+#define MON_3 0x6001C
+#define MON_4 0x6001D
+#define MON_5 0x6001E
+#define MON_6 0x6001F
+#define MON_7 0x60020
+#define MON_8 0x60021
+#define MON_9 0x60022
+#define MON_10 0x60023
+#define MON_11 0x60024
+#define MON_12 0x60025
+
+#define AM_STR 0x60026
+#define PM_STR 0x60027
+
+#define D_T_FMT 0x60028
+#define D_FMT 0x60029
+#define T_FMT 0x6002A
+#define T_FMT_AMPM 0x6002B
+
+#define ERA 0x6002C
+#define ERA_D_FMT 0x6002D
+#define ALT_DIGITS 0x6002E
+#define ERA_D_T_FMT 0x6002F
+#define ERA_T_FMT 0x60030
+
+#define CODESET 0x30000
+
+#define CRNCYSTR 0x40000
+
+#define RADIXCHAR 0x50000
+#define THOUSEP 0x50001
+
+#define YESEXPR 0x70000
+#define NOEXPR 0x70001
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // _NL_ITEM_H
+
diff --git a/lib/mlibc/options/internal/include/bits/null.h b/lib/mlibc/options/internal/include/bits/null.h
new file mode 100644
index 0000000..7d3fa7b
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/null.h
@@ -0,0 +1,16 @@
+
+#ifndef MLIBC_NULL_H
+#define MLIBC_NULL_H
+
+#ifdef NULL
+#undef NULL
+#endif
+
+#ifndef __cplusplus
+# define NULL ((void *)0)
+#else
+# define NULL 0
+#endif
+
+#endif // MLIBC_NULL_H
+
diff --git a/lib/mlibc/options/internal/include/bits/off_t.h b/lib/mlibc/options/internal/include/bits/off_t.h
new file mode 100644
index 0000000..929fae9
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/off_t.h
@@ -0,0 +1,8 @@
+#ifndef MLIBC_OFF_T_H
+#define MLIBC_OFF_T_H
+
+// TODO: use something like int64_t instead?
+typedef long off_t;
+typedef long off64_t;
+
+#endif // MLIBC_OFF_T_H
diff --git a/lib/mlibc/options/internal/include/bits/sigset_t.h b/lib/mlibc/options/internal/include/bits/sigset_t.h
new file mode 100644
index 0000000..bb86848
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/sigset_t.h
@@ -0,0 +1,25 @@
+#ifndef MLIBC_BITS_SIGSET_T_H
+#define MLIBC_BITS_SIGSET_T_H
+
+#include <abi-bits/signal.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef __MLIBC_ABI_ONLY
+
+// functions to manage sigset_t
+int sigemptyset(sigset_t *);
+int sigfillset(sigset_t *);
+int sigaddset(sigset_t *, int);
+int sigdelset(sigset_t *, int);
+int sigismember(const sigset_t *set, int sig);
+
+#endif /* !__MLIBC_ABI_ONLY */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //MLIBC_BITS_SIGSET_T_H
diff --git a/lib/mlibc/options/internal/include/bits/size_t.h b/lib/mlibc/options/internal/include/bits/size_t.h
new file mode 100644
index 0000000..46d7486
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/size_t.h
@@ -0,0 +1,6 @@
+#ifndef MLIBC_SIZE_T_H
+#define MLIBC_SIZE_T_H
+
+typedef __SIZE_TYPE__ size_t;
+
+#endif // MLIBC_SIZE_T_H
diff --git a/lib/mlibc/options/internal/include/bits/ssize_t.h b/lib/mlibc/options/internal/include/bits/ssize_t.h
new file mode 100644
index 0000000..c450233
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/ssize_t.h
@@ -0,0 +1,15 @@
+
+#ifndef MLIBC_SSIZE_T_H
+#define MLIBC_SSIZE_T_H
+
+// TODO: use ptrdiff_t instead?
+#if __UINTPTR_MAX__ == __UINT64_MAX__
+typedef long ssize_t;
+#elif __UINTPTR_MAX__ == __UINT32_MAX__
+typedef int ssize_t;
+#else
+#error "unsupported architecture"
+#endif
+
+#endif // MLIBC_SSIZE_T_H
+
diff --git a/lib/mlibc/options/internal/include/bits/threads.h b/lib/mlibc/options/internal/include/bits/threads.h
new file mode 100644
index 0000000..3feb4c3
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/threads.h
@@ -0,0 +1,79 @@
+#ifndef _INTERNAL_THREADS_H
+#define _INTERNAL_THREADS_H
+
+#include <abi-bits/clockid_t.h>
+#include <bits/size_t.h>
+#include <bits/cpu_set.h>
+#include <bits/sigset_t.h>
+
+// values for pthread_attr_{get,set}detachstate().
+#define __MLIBC_THREAD_CREATE_JOINABLE 0
+#define __MLIBC_THREAD_CREATE_DETACHED 1
+
+// values for pthread_mutexattr_{get,set}type().
+#define __MLIBC_THREAD_MUTEX_DEFAULT 0
+#define __MLIBC_THREAD_MUTEX_NORMAL 0
+#define __MLIBC_THREAD_MUTEX_ERRORCHECK 1
+#define __MLIBC_THREAD_MUTEX_RECURSIVE 2
+
+// values for pthread_mutexattr_{get,set}pshared().
+#define __MLIBC_THREAD_PROCESS_PRIVATE 0
+#define __MLIBC_THREAD_PROCESS_SHARED 1
+
+// values for pthread_mutexattr_{get,set}robust().
+#define __MLIBC_THREAD_MUTEX_STALLED 0
+#define __MLIBC_THREAD_MUTEX_ROBUST 1
+
+// Values for pthread_mutexattr_{get,set}protocol()
+#define __MLIBC_THREAD_PRIO_NONE 0
+#define __MLIBC_THREAD_PRIO_INHERIT 1
+#define __MLIBC_THREAD_PRIO_PROTECT 2
+
+struct sched_param {
+ int sched_priority;
+};
+
+struct __mlibc_thread_data;
+
+struct __mlibc_threadattr {
+ size_t __mlibc_guardsize;
+ size_t __mlibc_stacksize;
+ void *__mlibc_stackaddr;
+ int __mlibc_detachstate;
+ int __mlibc_scope;
+ int __mlibc_inheritsched;
+ struct sched_param __mlibc_schedparam;
+ int __mlibc_schedpolicy;
+ cpu_set_t *__mlibc_cpuset;
+ size_t __mlibc_cpusetsize;
+ sigset_t __mlibc_sigmask;
+ int __mlibc_sigmaskset;
+};
+
+struct __mlibc_mutex {
+ unsigned int __mlibc_state;
+ unsigned int __mlibc_recursion;
+ unsigned int __mlibc_flags;
+ int __mlibc_prioceiling;
+};
+
+struct __mlibc_mutexattr {
+ int __mlibc_type;
+ int __mlibc_robust;
+ int __mlibc_protocol;
+ int __mlibc_pshared;
+ int __mlibc_prioceiling;
+};
+
+struct __mlibc_cond {
+ unsigned int __mlibc_seq;
+ unsigned int __mlibc_flags;
+ clockid_t __mlibc_clock;
+};
+
+struct __mlibc_condattr {
+ int __mlibc_pshared;
+ clockid_t __mlibc_clock;
+};
+
+#endif /* _INTERNAL_THREADS_H */
diff --git a/lib/mlibc/options/internal/include/bits/types.h b/lib/mlibc/options/internal/include/bits/types.h
new file mode 100644
index 0000000..935c5e0
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/types.h
@@ -0,0 +1,319 @@
+#ifndef _MLIBC_INTERNAL_TYPES_H
+#define _MLIBC_INTERNAL_TYPES_H
+
+typedef __UINT8_TYPE__ __mlibc_uint8;
+typedef __UINT16_TYPE__ __mlibc_uint16;
+typedef __UINT32_TYPE__ __mlibc_uint32;
+typedef __UINT64_TYPE__ __mlibc_uint64;
+
+typedef __INT8_TYPE__ __mlibc_int8;
+typedef __INT16_TYPE__ __mlibc_int16;
+typedef __INT32_TYPE__ __mlibc_int32;
+typedef __INT64_TYPE__ __mlibc_int64;
+
+// Clang and GCC have different mechanisms for INT32_C and friends.
+#ifdef __clang__
+# define __MLIBC_C_EXPAND_JOIN(x, suffix) x ## suffix
+# define __MLIBC_C_JOIN(x, suffix) __MLIBC_C_EXPAND_JOIN(x, suffix)
+
+# define __MLIBC_INT8_C(x) __MLIBC_C_JOIN(x, __INT8_C_SUFFIX__)
+# define __MLIBC_INT16_C(x) __MLIBC_C_JOIN(x, __INT16_C_SUFFIX__)
+# define __MLIBC_INT32_C(x) __MLIBC_C_JOIN(x, __INT32_C_SUFFIX__)
+# define __MLIBC_INT64_C(x) __MLIBC_C_JOIN(x, __INT64_C_SUFFIX__)
+
+# define __MLIBC_UINT8_C(x) __MLIBC_C_JOIN(x, __UINT8_C_SUFFIX__)
+# define __MLIBC_UINT16_C(x) __MLIBC_C_JOIN(x, __UINT16_C_SUFFIX__)
+# define __MLIBC_UINT32_C(x) __MLIBC_C_JOIN(x, __UINT32_C_SUFFIX__)
+# define __MLIBC_UINT64_C(x) __MLIBC_C_JOIN(x, __UINT64_C_SUFFIX__)
+
+# define __MLIBC_INTMAX_C(x) __MLIBC_C_JOIN(x, __INTMAX_C_SUFFIX__)
+# define __MLIBC_UINTMAX_C(x) __MLIBC_C_JOIN(x, __UINTMAX_C_SUFFIX__)
+#else
+# define __MLIBC_INT8_C(x) __INT8_C(x)
+# define __MLIBC_INT16_C(x) __INT16_C(x)
+# define __MLIBC_INT32_C(x) __INT32_C(x)
+# define __MLIBC_INT64_C(x) __INT64_C(x)
+
+# define __MLIBC_UINT8_C(x) __UINT8_C(x)
+# define __MLIBC_UINT16_C(x) __UINT16_C(x)
+# define __MLIBC_UINT32_C(x) __UINT32_C(x)
+# define __MLIBC_UINT64_C(x) __UINT64_C(x)
+
+# define __MLIBC_INTMAX_C(x) __INTMAX_C(x)
+# define __MLIBC_UINTMAX_C(x) __UINTMAX_C(x)
+#endif
+
+#define __MLIBC_INT8_MAX __INT8_MAX__
+#define __MLIBC_INT16_MAX __INT16_MAX__
+#define __MLIBC_INT32_MAX __INT32_MAX__
+#define __MLIBC_INT64_MAX __INT64_MAX__
+
+#define __MLIBC_INT8_MIN (-__MLIBC_INT8_MAX - 1)
+#define __MLIBC_INT16_MIN (-__MLIBC_INT16_MAX - 1)
+#define __MLIBC_INT32_MIN (-__MLIBC_INT32_MAX - 1)
+#define __MLIBC_INT64_MIN (-__MLIBC_INT64_MAX - 1)
+
+#define __MLIBC_UINT8_MAX __UINT8_MAX__
+#define __MLIBC_UINT16_MAX __UINT16_MAX__
+#define __MLIBC_UINT32_MAX __UINT32_MAX__
+#define __MLIBC_UINT64_MAX __UINT64_MAX__
+
+// Fast types (signed).
+
+#if defined (__i386__)
+
+typedef __mlibc_int8 __mlibc_int_fast8;
+#define __MLIBC_INT_FAST8_C(x) __MLIBC_INT8_C(x)
+#define __MLIBC_INT_FAST8_MAX __MLIBC_INT8_MAX
+#define __MLIBC_INT_FAST8_MIN __MLIBC_INT8_MIN
+
+typedef __mlibc_int32 __mlibc_int_fast16;
+#define __MLIBC_INT_FAST16_C(x) __MLIBC_INT32_C(x)
+#define __MLIBC_INT_FAST16_MAX __MLIBC_INT32_MAX
+#define __MLIBC_INT_FAST16_MIN __MLIBC_INT32_MIN
+
+typedef __mlibc_int32 __mlibc_int_fast32;
+#define __MLIBC_INT_FAST32_C(x) __MLIBC_INT32_C(x)
+#define __MLIBC_INT_FAST32_MAX __MLIBC_INT32_MAX
+#define __MLIBC_INT_FAST32_MIN __MLIBC_INT32_MIN
+
+typedef __mlibc_int64 __mlibc_int_fast64;
+#define __MLIBC_INT_FAST64_C(x) __MLIBC_INT64_C(x)
+#define __MLIBC_INT_FAST64_MAX __MLIBC_INT64_MAX
+#define __MLIBC_INT_FAST64_MIN __MLIBC_INT64_MIN
+
+#elif defined (__x86_64__)
+
+typedef __mlibc_int8 __mlibc_int_fast8;
+#define __MLIBC_INT_FAST8_C(x) __MLIBC_INT8_C(x)
+#define __MLIBC_INT_FAST8_MAX __MLIBC_INT8_MAX
+#define __MLIBC_INT_FAST8_MIN __MLIBC_INT8_MIN
+
+typedef __mlibc_int64 __mlibc_int_fast16;
+#define __MLIBC_INT_FAST16_C(x) __MLIBC_INT64_C(x)
+#define __MLIBC_INT_FAST16_MAX __MLIBC_INT64_MAX
+#define __MLIBC_INT_FAST16_MIN __MLIBC_INT64_MIN
+
+typedef __mlibc_int64 __mlibc_int_fast32;
+#define __MLIBC_INT_FAST32_C(x) __MLIBC_INT64_C(x)
+#define __MLIBC_INT_FAST32_MAX __MLIBC_INT64_MAX
+#define __MLIBC_INT_FAST32_MIN __MLIBC_INT64_MIN
+
+typedef __mlibc_int64 __mlibc_int_fast64;
+#define __MLIBC_INT_FAST64_C(x) __MLIBC_INT64_C(x)
+#define __MLIBC_INT_FAST64_MAX __MLIBC_INT64_MAX
+#define __MLIBC_INT_FAST64_MIN __MLIBC_INT64_MIN
+
+#elif defined (__aarch64__)
+
+typedef __mlibc_int8 __mlibc_int_fast8;
+#define __MLIBC_INT_FAST8_C(x) __MLIBC_INT8_C(x)
+#define __MLIBC_INT_FAST8_MAX __MLIBC_INT8_MAX
+#define __MLIBC_INT_FAST8_MIN __MLIBC_INT8_MIN
+
+typedef __mlibc_int64 __mlibc_int_fast16;
+#define __MLIBC_INT_FAST16_C(x) __MLIBC_INT64_C(x)
+#define __MLIBC_INT_FAST16_MAX __MLIBC_INT64_MAX
+#define __MLIBC_INT_FAST16_MIN __MLIBC_INT64_MIN
+
+typedef __mlibc_int64 __mlibc_int_fast32;
+#define __MLIBC_INT_FAST32_C(x) __MLIBC_INT64_C(x)
+#define __MLIBC_INT_FAST32_MAX __MLIBC_INT64_MAX
+#define __MLIBC_INT_FAST32_MIN __MLIBC_INT64_MIN
+
+typedef __mlibc_int64 __mlibc_int_fast64;
+#define __MLIBC_INT_FAST64_C(x) __MLIBC_INT64_C(x)
+#define __MLIBC_INT_FAST64_MAX __MLIBC_INT64_MAX
+#define __MLIBC_INT_FAST64_MIN __MLIBC_INT64_MIN
+
+#elif defined (__riscv) && __riscv_xlen == 64
+
+typedef __mlibc_int8 __mlibc_int_fast8;
+#define __MLIBC_INT_FAST8_C(x) __MLIBC_INT8_C(x)
+#define __MLIBC_INT_FAST8_MAX __MLIBC_INT8_MAX
+#define __MLIBC_INT_FAST8_MIN __MLIBC_INT8_MIN
+
+typedef __mlibc_int64 __mlibc_int_fast16;
+#define __MLIBC_INT_FAST16_C(x) __MLIBC_INT64_C(x)
+#define __MLIBC_INT_FAST16_MAX __MLIBC_INT64_MAX
+#define __MLIBC_INT_FAST16_MIN __MLIBC_INT64_MIN
+
+typedef __mlibc_int64 __mlibc_int_fast32;
+#define __MLIBC_INT_FAST32_C(x) __MLIBC_INT64_C(x)
+#define __MLIBC_INT_FAST32_MAX __MLIBC_INT64_MAX
+#define __MLIBC_INT_FAST32_MIN __MLIBC_INT64_MIN
+
+typedef __mlibc_int64 __mlibc_int_fast64;
+#define __MLIBC_INT_FAST64_C(x) __MLIBC_INT64_C(x)
+#define __MLIBC_INT_FAST64_MAX __MLIBC_INT64_MAX
+#define __MLIBC_INT_FAST64_MIN __MLIBC_INT64_MIN
+
+#else
+# error "Missing architecture specific code"
+#endif
+
+// Fast types (unsigned).
+
+#if defined (__i386__)
+
+typedef __mlibc_uint8 __mlibc_uint_fast8;
+#define __MLIBC_UINT_FAST8_C(x) __MLIBC_UINT8_C(x)
+#define __MLIBC_UINT_FAST8_MAX __MLIBC_UINT8_MAX
+#define __MLIBC_UINT_FAST8_MIN __MLIBC_UINT8_MIN
+
+typedef __mlibc_uint32 __mlibc_uint_fast16;
+#define __MLIBC_UINT_FAST16_C(x) __MLIBC_UINT32_C(x)
+#define __MLIBC_UINT_FAST16_MAX __MLIBC_UINT32_MAX
+#define __MLIBC_UINT_FAST16_MIN __MLIBC_UINT32_MIN
+
+typedef __mlibc_uint32 __mlibc_uint_fast32;
+#define __MLIBC_UINT_FAST32_C(x) __MLIBC_UINT32_C(x)
+#define __MLIBC_UINT_FAST32_MAX __MLIBC_UINT32_MAX
+#define __MLIBC_UINT_FAST32_MIN __MLIBC_UINT32_MIN
+
+typedef __mlibc_uint64 __mlibc_uint_fast64;
+#define __MLIBC_UINT_FAST64_C(x) __MLIBC_UINT64_C(x)
+#define __MLIBC_UINT_FAST64_MAX __MLIBC_UINT64_MAX
+#define __MLIBC_UINT_FAST64_MIN __MLIBC_UINT64_MIN
+
+#elif defined (__x86_64__)
+
+typedef __mlibc_uint8 __mlibc_uint_fast8;
+#define __MLIBC_UINT_FAST8_C(x) __MLIBC_UINT8_C(x)
+#define __MLIBC_UINT_FAST8_MAX __MLIBC_UINT8_MAX
+#define __MLIBC_UINT_FAST8_MIN __MLIBC_UINT8_MIN
+
+typedef __mlibc_uint64 __mlibc_uint_fast16;
+#define __MLIBC_UINT_FAST16_C(x) __MLIBC_UINT64_C(x)
+#define __MLIBC_UINT_FAST16_MAX __MLIBC_UINT64_MAX
+#define __MLIBC_UINT_FAST16_MIN __MLIBC_UINT64_MIN
+
+typedef __mlibc_uint64 __mlibc_uint_fast32;
+#define __MLIBC_UINT_FAST32_C(x) __MLIBC_UINT64_C(x)
+#define __MLIBC_UINT_FAST32_MAX __MLIBC_UINT64_MAX
+#define __MLIBC_UINT_FAST32_MIN __MLIBC_UINT64_MIN
+
+typedef __mlibc_uint64 __mlibc_uint_fast64;
+#define __MLIBC_UINT_FAST64_C(x) __MLIBC_UINT64_C(x)
+#define __MLIBC_UINT_FAST64_MAX __MLIBC_UINT64_MAX
+#define __MLIBC_UINT_FAST64_MIN __MLIBC_UINT64_MIN
+
+#elif defined (__aarch64__)
+
+typedef __mlibc_uint8 __mlibc_uint_fast8;
+#define __MLIBC_UINT_FAST8_C(x) __MLIBC_UINT8_C(x)
+#define __MLIBC_UINT_FAST8_MAX __MLIBC_UINT8_MAX
+#define __MLIBC_UINT_FAST8_MIN __MLIBC_UINT8_MIN
+
+typedef __mlibc_uint64 __mlibc_uint_fast16;
+#define __MLIBC_UINT_FAST16_C(x) __MLIBC_UINT64_C(x)
+#define __MLIBC_UINT_FAST16_MAX __MLIBC_UINT64_MAX
+#define __MLIBC_UINT_FAST16_MIN __MLIBC_UINT64_MIN
+
+typedef __mlibc_uint64 __mlibc_uint_fast32;
+#define __MLIBC_UINT_FAST32_C(x) __MLIBC_UINT64_C(x)
+#define __MLIBC_UINT_FAST32_MAX __MLIBC_UINT64_MAX
+#define __MLIBC_UINT_FAST32_MIN __MLIBC_UINT64_MIN
+
+typedef __mlibc_uint64 __mlibc_uint_fast64;
+#define __MLIBC_UINT_FAST64_C(x) __MLIBC_UINT64_C(x)
+#define __MLIBC_UINT_FAST64_MAX __MLIBC_UINT64_MAX
+#define __MLIBC_UINT_FAST64_MIN __MLIBC_UINT64_MIN
+
+#elif defined (__riscv) && __riscv_xlen == 64
+
+typedef __mlibc_uint8 __mlibc_uint_fast8;
+#define __MLIBC_UINT_FAST8_C(x) __MLIBC_UINT8_C(x)
+#define __MLIBC_UINT_FAST8_MAX __MLIBC_UINT8_MAX
+#define __MLIBC_UINT_FAST8_MIN __MLIBC_UINT8_MIN
+
+typedef __mlibc_uint64 __mlibc_uint_fast16;
+#define __MLIBC_UINT_FAST16_C(x) __MLIBC_UINT64_C(x)
+#define __MLIBC_UINT_FAST16_MAX __MLIBC_UINT64_MAX
+#define __MLIBC_UINT_FAST16_MIN __MLIBC_UINT64_MIN
+
+typedef __mlibc_uint64 __mlibc_uint_fast32;
+#define __MLIBC_UINT_FAST32_C(x) __MLIBC_UINT64_C(x)
+#define __MLIBC_UINT_FAST32_MAX __MLIBC_UINT64_MAX
+#define __MLIBC_UINT_FAST32_MIN __MLIBC_UINT64_MIN
+
+typedef __mlibc_uint64 __mlibc_uint_fast64;
+#define __MLIBC_UINT_FAST64_C(x) __MLIBC_UINT64_C(x)
+#define __MLIBC_UINT_FAST64_MAX __MLIBC_UINT64_MAX
+#define __MLIBC_UINT_FAST64_MIN __MLIBC_UINT64_MIN
+
+#else
+# error "Missing architecture specific code"
+#endif
+
+// Special types.
+
+typedef __INTMAX_TYPE__ __mlibc_intmax;
+typedef __INTPTR_TYPE__ __mlibc_intptr;
+typedef __PTRDIFF_TYPE__ __mlibc_ptrdiff;
+#define __MLIBC_INTMAX_MAX __INTMAX_MAX__
+#define __MLIBC_INTMAX_MIN (-__INTMAX_MAX__ - 1)
+#define __MLIBC_INTPTR_MAX __INTPTR_MAX__
+#define __MLIBC_INTPTR_MIN (-__INTPTR_MAX__ - 1)
+#define __MLIBC_PTRDIFF_MAX __PTRDIFF_MAX__
+#define __MLIBC_PTRDIFF_MIN (-__PTRDIFF_MAX__ - 1)
+
+typedef __UINTMAX_TYPE__ __mlibc_uintmax;
+typedef __UINTPTR_TYPE__ __mlibc_uintptr;
+typedef __SIZE_TYPE__ __mlibc_size;
+#define __MLIBC_UINTMAX_MAX __UINTMAX_MAX__
+#define __MLIBC_UINTPTR_MAX __UINTPTR_MAX__
+#define __MLIBC_SIZE_MAX __SIZE_MAX__
+
+// Other limits.
+
+#define __MLIBC_WCHAR_MAX __WCHAR_MAX__
+#define __MLIBC_WCHAR_MIN __WCHAR_MIN__
+
+#define __MLIBC_WINT_MAX __WINT_MAX__
+#define __MLIBC_WINT_MIN __WINT_MIN__
+
+#define __MLIBC_SIG_ATOMIC_MAX __SIG_ATOMIC_MAX__
+#define __MLIBC_SIG_ATOMIC_MIN __SIG_ATOMIC_MIN__
+
+// ----------------------------------------------------------------------------
+// Sanity checking. Make sure that we agree with the compiler's ABI.
+// ----------------------------------------------------------------------------
+
+#if defined(__cpp_static_assert)
+# define __MLIBC_STATIC_ASSERT(c, text) static_assert(c, text)
+#elif !defined(__cplusplus)
+# define __MLIBC_STATIC_ASSERT(c, text) _Static_assert(c, text)
+#else
+# define __MLIBC_STATIC_ASSERT(c, text)
+#endif
+
+#define __MLIBC_CHECK_TYPE(T1, T2) __MLIBC_STATIC_ASSERT(sizeof(T1) == sizeof(T2),\
+ #T1 " != " #T2);
+
+// Least-width.
+__MLIBC_CHECK_TYPE(__mlibc_int8, __INT_LEAST8_TYPE__);
+__MLIBC_CHECK_TYPE(__mlibc_int16, __INT_LEAST16_TYPE__);
+__MLIBC_CHECK_TYPE(__mlibc_int32, __INT_LEAST32_TYPE__);
+__MLIBC_CHECK_TYPE(__mlibc_int64, __INT_LEAST64_TYPE__);
+
+__MLIBC_CHECK_TYPE(__mlibc_uint8, __UINT_LEAST8_TYPE__);
+__MLIBC_CHECK_TYPE(__mlibc_uint16, __UINT_LEAST16_TYPE__);
+__MLIBC_CHECK_TYPE(__mlibc_uint32, __UINT_LEAST32_TYPE__);
+__MLIBC_CHECK_TYPE(__mlibc_uint64, __UINT_LEAST64_TYPE__);
+
+// Fast-width.
+// Unfortunately, GCC and Clang disagree about fast types.
+#ifndef __clang__
+ __MLIBC_CHECK_TYPE(__mlibc_int_fast8, __INT_FAST8_TYPE__);
+ __MLIBC_CHECK_TYPE(__mlibc_int_fast16, __INT_FAST16_TYPE__);
+ __MLIBC_CHECK_TYPE(__mlibc_int_fast32, __INT_FAST32_TYPE__);
+ __MLIBC_CHECK_TYPE(__mlibc_int_fast64, __INT_FAST64_TYPE__);
+
+ __MLIBC_CHECK_TYPE(__mlibc_uint_fast8, __UINT_FAST8_TYPE__);
+ __MLIBC_CHECK_TYPE(__mlibc_uint_fast16, __UINT_FAST16_TYPE__);
+ __MLIBC_CHECK_TYPE(__mlibc_uint_fast32, __UINT_FAST32_TYPE__);
+ __MLIBC_CHECK_TYPE(__mlibc_uint_fast64, __UINT_FAST64_TYPE__);
+#endif
+
+#endif // _MLIBC_INTERNAL_TYPES_H
diff --git a/lib/mlibc/options/internal/include/bits/wchar.h b/lib/mlibc/options/internal/include/bits/wchar.h
new file mode 100644
index 0000000..a422ef7
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/wchar.h
@@ -0,0 +1,9 @@
+#ifndef MLIBC_WCHAR_H
+#define MLIBC_WCHAR_H
+
+#include <bits/types.h>
+
+#define WCHAR_MAX __MLIBC_WCHAR_MAX
+#define WCHAR_MIN __MLIBC_WCHAR_MIN
+
+#endif // MLIBC_WCHAR_H
diff --git a/lib/mlibc/options/internal/include/bits/wchar_t.h b/lib/mlibc/options/internal/include/bits/wchar_t.h
new file mode 100644
index 0000000..4eb4e9c
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/wchar_t.h
@@ -0,0 +1,12 @@
+
+#ifndef MLIBC_WCHAR_T_H
+#define MLIBC_WCHAR_T_H
+
+#ifndef __cplusplus
+
+typedef __WCHAR_TYPE__ wchar_t;
+
+#endif
+
+#endif // MLIBC_WCHAR_T_H
+
diff --git a/lib/mlibc/options/internal/include/bits/winsize.h b/lib/mlibc/options/internal/include/bits/winsize.h
new file mode 100644
index 0000000..7b3006a
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/winsize.h
@@ -0,0 +1,13 @@
+
+#ifndef MLIBC_WINSIZE_H
+#define MLIBC_WINSIZE_H
+
+struct winsize {
+ unsigned short ws_row;
+ unsigned short ws_col;
+ unsigned short ws_xpixel;
+ unsigned short ws_ypixel;
+};
+
+#endif // MLIBC_WINSIZE_H
+
diff --git a/lib/mlibc/options/internal/include/bits/wint_t.h b/lib/mlibc/options/internal/include/bits/wint_t.h
new file mode 100644
index 0000000..b4f57bf
--- /dev/null
+++ b/lib/mlibc/options/internal/include/bits/wint_t.h
@@ -0,0 +1,6 @@
+#ifndef MLIBC_WINT_T_H
+#define MLIBC_WINT_T_H
+
+typedef __WINT_TYPE__ wint_t;
+
+#endif // MLIBC_WINT_T_H
diff --git a/lib/mlibc/options/internal/include/mlibc/all-sysdeps.hpp b/lib/mlibc/options/internal/include/mlibc/all-sysdeps.hpp
new file mode 100644
index 0000000..7189286
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/all-sysdeps.hpp
@@ -0,0 +1,33 @@
+#ifndef MLIBC_ALL_SYSDEPS
+#define MLIBC_ALL_SYSDEPS
+
+#include <mlibc-config.h>
+#include <internal-config.h>
+
+#if __MLIBC_ANSI_OPTION
+# include <mlibc/ansi-sysdeps.hpp>
+#endif /* __MLIBC_ANSI_OPTION */
+
+#if __MLIBC_POSIX_OPTION
+# include <mlibc/posix-sysdeps.hpp>
+#endif /* __MLIBC_POSIX_OPTION */
+
+#if __MLIBC_LINUX_OPTION
+# include <mlibc/linux-sysdeps.hpp>
+#endif /* __MLIBC_LINUX_OPTION */
+
+#if __MLIBC_GLIBC_OPTION
+# include <mlibc/glibc-sysdeps.hpp>
+#endif /* __MLIBC_GLIBC_OPTION */
+
+#if __MLIBC_BSD_OPTION
+# include <mlibc/bsd-sysdeps.hpp>
+#endif /* __MLIBC_BSD_OPTION */
+
+#if MLIBC_BUILDING_RTDL
+# include <mlibc/rtdl-sysdeps.hpp>
+#endif /* MLIBC_BUILDING_RTDL */
+
+#include <mlibc/internal-sysdeps.hpp>
+
+#endif /* MLIBC_ALL_SYSDEPS */
diff --git a/lib/mlibc/options/internal/include/mlibc/allocator.hpp b/lib/mlibc/options/internal/include/mlibc/allocator.hpp
new file mode 100644
index 0000000..5f9617e
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/allocator.hpp
@@ -0,0 +1,37 @@
+#ifndef MLIBC_FRIGG_ALLOC
+#define MLIBC_FRIGG_ALLOC
+
+#include <mlibc/lock.hpp>
+#include <bits/ensure.h>
+#include <frg/slab.hpp>
+#include <internal-config.h>
+
+#if !MLIBC_DEBUG_ALLOCATOR
+
+struct VirtualAllocator {
+public:
+ uintptr_t map(size_t length);
+
+ void unmap(uintptr_t address, size_t length);
+};
+
+typedef frg::slab_pool<VirtualAllocator, FutexLock> MemoryPool;
+
+typedef frg::slab_allocator<VirtualAllocator, FutexLock> MemoryAllocator;
+
+MemoryAllocator &getAllocator();
+
+#else
+
+struct MemoryAllocator {
+ void *allocate(size_t size);
+ void free(void *ptr);
+ void deallocate(void *ptr, size_t size);
+ void *reallocate(void *ptr, size_t size);
+};
+
+MemoryAllocator &getAllocator();
+
+#endif // !MLIBC_DEBUG_ALLOCATOR
+
+#endif // MLIBC_FRIGG_ALLOC
diff --git a/lib/mlibc/options/internal/include/mlibc/bitutil.hpp b/lib/mlibc/options/internal/include/mlibc/bitutil.hpp
new file mode 100644
index 0000000..6d2b25e
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/bitutil.hpp
@@ -0,0 +1,34 @@
+#ifndef MLIBC_BITUTIL
+#define MLIBC_BITUTIL
+
+#include <stdint.h>
+
+namespace mlibc {
+
+template<typename T>
+struct bit_util;
+
+template<>
+struct bit_util<uint64_t> {
+ static uint64_t byteswap(uint64_t x) {
+ return __builtin_bswap64(x);
+ }
+};
+
+template<>
+struct bit_util<uint32_t> {
+ static uint32_t byteswap(uint32_t x) {
+ return __builtin_bswap32(x);
+ }
+};
+
+template<>
+struct bit_util<uint16_t> {
+ static uint16_t byteswap(uint16_t x) {
+ return __builtin_bswap16(x);
+ }
+};
+
+} // namespace mlibc
+
+#endif // MLIBC_BITUTIL
diff --git a/lib/mlibc/options/internal/include/mlibc/charcode.hpp b/lib/mlibc/options/internal/include/mlibc/charcode.hpp
new file mode 100644
index 0000000..67bd03d
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/charcode.hpp
@@ -0,0 +1,124 @@
+#ifndef MLIBC_CHARCODE_HPP
+#define MLIBC_CHARCODE_HPP
+
+#include <stddef.h>
+#include <stdint.h>
+#include <bits/ensure.h>
+#include <bits/mbstate.h>
+#include <mlibc/debug.hpp>
+
+namespace mlibc {
+
+enum class charcode_error {
+ null,
+ dirty,
+ illegal_input,
+ input_underflow,
+ output_overflow
+};
+
+template<typename C>
+struct code_seq {
+ C *it;
+ const C *end;
+
+ explicit operator bool () {
+ return it != end;
+ }
+};
+
+// Some encodings (e.g. the one defined in RFC 1843) have "shift states",
+// i.e. escape sequences that switch between different encodings (e.g. between single-byte ASCII
+// and 2-byte encoding of Chinese characters).
+// TODO: Implement that using the __shift member of __mlibc_mbstate.
+
+typedef uint32_t codepoint;
+
+// The following class deals with decoding/encoding "code units" (of type char)
+// to "code points" that are defined by unicode (of type codepoint).
+// It also offers convenience functions to transcode to wchar_t, char16_t and char32_t.
+// We assume that the encoding of wchar_t (and char16_t, char32_t) is fixed.
+// char is allowed to have an arbitrary encoding.
+// TODO: char16_t and char32_t variants are missing.
+// TODO: For iconv(), first decode and then encode to the destination encoding.
+struct polymorphic_charcode {
+ virtual ~polymorphic_charcode();
+
+ // Helper function to decode a single char.
+ charcode_error promote(char nc, codepoint &wc) {
+ auto uc = static_cast<unsigned char>(nc);
+ if(uc <= 0x7F && preserves_7bit_units) {
+ wc = uc;
+ return charcode_error::null;
+ }
+
+ code_seq<const char> nseq{&nc, &nc + 1};
+ code_seq<codepoint> wseq{&wc, &wc + 1};
+ __mlibc_mbstate st = __MLIBC_MBSTATE_INITIALIZER;
+
+ if(auto e = decode(nseq, wseq, st); e != charcode_error::null)
+ return e;
+ // This should have read/written exactly one code unit/code point.
+ __ensure(nseq.it == nseq.end);
+ __ensure(wseq.it == wseq.end);
+ return charcode_error::null;
+ }
+
+ // Helper function to decode a single char.
+ charcode_error promote_wtranscode(char nc, wchar_t &wc) {
+ auto uc = static_cast<unsigned char>(nc);
+ if(uc <= 0x7F && preserves_7bit_units) { // TODO: Use "wtranscode_preserves_7bit_units".
+ wc = uc;
+ return charcode_error::null;
+ }
+
+ code_seq<const char> nseq{&nc, &nc + 1};
+ code_seq<wchar_t> wseq{&wc, &wc + 1};
+ __mlibc_mbstate st = __MLIBC_MBSTATE_INITIALIZER;
+
+ if(auto e = decode_wtranscode(nseq, wseq, st); e != charcode_error::null)
+ return e;
+ // This should have read/written exactly one code unit/code point.
+ __ensure(nseq.it == nseq.end);
+ __ensure(wseq.it == wseq.end);
+ return charcode_error::null;
+ }
+
+ polymorphic_charcode(bool preserves_7bit_units_, bool has_shift_states_)
+ : preserves_7bit_units{preserves_7bit_units_}, has_shift_states{has_shift_states_} { }
+
+ virtual charcode_error decode(code_seq<const char> &nseq, code_seq<codepoint> &wseq,
+ __mlibc_mbstate &st) = 0;
+
+ virtual charcode_error decode_wtranscode(code_seq<const char> &nseq, code_seq<wchar_t> &wseq,
+ __mlibc_mbstate &st) = 0;
+
+ virtual charcode_error decode_wtranscode_length(code_seq<const char> &nseq, size_t *n,
+ __mlibc_mbstate &st) = 0;
+
+ virtual charcode_error encode_wtranscode(code_seq<char> &nseq, code_seq<const wchar_t> &wseq,
+ __mlibc_mbstate &st) = 0;
+
+ virtual charcode_error encode_wtranscode_length(code_seq<const wchar_t> &wseq, size_t *n,
+ __mlibc_mbstate &st) = 0;
+
+ // True if promotion only zero-extends units below 0x7F.
+ const bool preserves_7bit_units;
+
+ // Whether the encoding has shift states.
+ const bool has_shift_states;
+};
+
+polymorphic_charcode *current_charcode();
+
+// Similar to polymorphic_charcode but for wchar_t. Note that this encoding is fixed per-platform;
+// thus, it does not need to be polymorphic.
+struct wide_charcode {
+ charcode_error promote(wchar_t nc, codepoint &wc);
+};
+
+wide_charcode *platform_wide_charcode();
+
+} // namespace mlibc
+
+#endif // MLIBC_CHARCODE_HPP
diff --git a/lib/mlibc/options/internal/include/mlibc/charset.hpp b/lib/mlibc/options/internal/include/mlibc/charset.hpp
new file mode 100644
index 0000000..a068f05
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/charset.hpp
@@ -0,0 +1,40 @@
+#ifndef MLIBC_CHARSET_HPP
+#define MLIBC_CHARSET_HPP
+
+#include <mlibc/charcode.hpp>
+
+namespace mlibc {
+
+// Represents the charset of a certain locale. We define the charset as
+// a set of characters, together with their properties and conversion rules
+// *but not* their encoding (e.g. to UTF-8 or UTF-16).
+struct charset {
+ // Returns true iif the meaning of the first 0x7F characters matches ASCII.
+ bool is_ascii_superset();
+
+ bool is_alpha(codepoint c);
+ bool is_digit(codepoint c);
+ bool is_xdigit(codepoint c);
+ bool is_alnum(codepoint c);
+ bool is_punct(codepoint c);
+ bool is_graph(codepoint c);
+ bool is_blank(codepoint c);
+ bool is_space(codepoint c);
+ bool is_print(codepoint c);
+
+ bool is_lower(codepoint c);
+ bool is_upper(codepoint c);
+ codepoint to_lower(codepoint c);
+ codepoint to_upper(codepoint c);
+};
+
+charset *current_charset();
+
+// The property if a character is a control character is locale-independent.
+inline bool generic_is_control(codepoint c) {
+ return (c <= 0x1F) || (c == 0x7F) || (c >= 0x80 && c <= 0x9F);
+}
+
+} // namespace mlibc
+
+#endif // MLIBC_CHARSET_HPP
diff --git a/lib/mlibc/options/internal/include/mlibc/debug.hpp b/lib/mlibc/options/internal/include/mlibc/debug.hpp
new file mode 100644
index 0000000..7067039
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/debug.hpp
@@ -0,0 +1,27 @@
+#ifndef MLIBC_DEBUG_HPP
+#define MLIBC_DEBUG_HPP
+
+#include <frg/logging.hpp>
+
+namespace mlibc {
+
+struct InfoSink {
+ // constexpr so that this can be initialized statically.
+ constexpr InfoSink() = default;
+
+ void operator() (const char *message);
+};
+
+struct PanicSink {
+ // constexpr so that this can be initialized statically.
+ constexpr PanicSink() = default;
+
+ void operator() (const char *message);
+};
+
+extern frg::stack_buffer_logger<InfoSink, 512> infoLogger;
+extern frg::stack_buffer_logger<PanicSink, 512> panicLogger;
+
+} // namespace mlibc
+
+#endif // MLIBC_DEBUG_HPP
diff --git a/lib/mlibc/options/internal/include/mlibc/file-window.hpp b/lib/mlibc/options/internal/include/mlibc/file-window.hpp
new file mode 100644
index 0000000..68c3ebf
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/file-window.hpp
@@ -0,0 +1,64 @@
+#ifndef MLIBC_FILE_WINDOW
+#define MLIBC_FILE_WINDOW
+
+#include <abi-bits/fcntl.h>
+#include <mlibc/allocator.hpp>
+#include <mlibc/debug.hpp>
+#include <mlibc/internal-sysdeps.hpp>
+#include <internal-config.h>
+
+struct file_window {
+ file_window(const char *path) {
+ int fd;
+ if(mlibc::sys_open("/etc/localtime", O_RDONLY, 0, &fd))
+ mlibc::panicLogger() << "mlibc: Error opening file_window to "
+ << path << frg::endlog;
+
+ if(!mlibc::sys_stat) {
+ MLIBC_MISSING_SYSDEP();
+ __ensure(!"cannot proceed without sys_stat");
+ }
+ struct stat info;
+ if(mlibc::sys_stat(mlibc::fsfd_target::fd, fd, "", 0, &info))
+ mlibc::panicLogger() << "mlibc: Error getting TZinfo stats" << frg::endlog;
+
+#if MLIBC_MAP_FILE_WINDOWS
+ if(mlibc::sys_vm_map(nullptr, (size_t)info.st_size, PROT_READ, MAP_PRIVATE,
+ fd, 0, &_ptr))
+ mlibc::panicLogger() << "mlibc: Error mapping TZinfo" << frg::endlog;
+#else
+ _ptr = getAllocator().allocate(info.st_size);
+ __ensure(_ptr);
+
+ size_t progress = 0;
+ size_t st_size = static_cast<size_t>(info.st_size);
+ while(progress < st_size) {
+ ssize_t chunk;
+ if(int e = mlibc::sys_read(fd, reinterpret_cast<char *>(_ptr) + progress,
+ st_size - progress, &chunk); e)
+ mlibc::panicLogger() << "mlibc: Read from file_window failed" << frg::endlog;
+ if(!chunk)
+ break;
+ progress += chunk;
+ }
+ if(progress != st_size)
+ mlibc::panicLogger() << "stat reports " << info.st_size << " but we only read "
+ << progress << " bytes" << frg::endlog;
+#endif
+
+ if(mlibc::sys_close(fd))
+ mlibc::panicLogger() << "mlibc: Error closing TZinfo" << frg::endlog;
+ }
+
+ // TODO: Write destructor to deallocate/unmap memory.
+
+ void *get() {
+ return _ptr;
+ }
+
+private:
+ void *_ptr;
+};
+
+#endif // MLIBC_FILE_WINDOW
+
diff --git a/lib/mlibc/options/internal/include/mlibc/fsfd_target.hpp b/lib/mlibc/options/internal/include/mlibc/fsfd_target.hpp
new file mode 100644
index 0000000..b577325
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/fsfd_target.hpp
@@ -0,0 +1,15 @@
+#ifndef MLIBC_FSFD_TARGET
+#define MLIBC_FSFD_TARGET
+
+namespace mlibc {
+
+enum class fsfd_target {
+ none,
+ path,
+ fd,
+ fd_path
+};
+
+} // namespace mlibc
+
+#endif // MLIBC_FSFD_TARGET
diff --git a/lib/mlibc/options/internal/include/mlibc/global-config.hpp b/lib/mlibc/options/internal/include/mlibc/global-config.hpp
new file mode 100644
index 0000000..7eaed3c
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/global-config.hpp
@@ -0,0 +1,19 @@
+#ifndef MLIBC_GLOBAL_CONFIG
+#define MLIBC_GLOBAL_CONFIG
+
+namespace mlibc {
+
+struct GlobalConfig {
+ GlobalConfig();
+
+ bool debugMalloc;
+};
+
+inline const GlobalConfig &globalConfig() {
+ static GlobalConfig cached;
+ return cached;
+}
+
+}
+
+#endif // MLIBC_GLOBAL_CONFIG
diff --git a/lib/mlibc/options/internal/include/mlibc/internal-sysdeps.hpp b/lib/mlibc/options/internal/include/mlibc/internal-sysdeps.hpp
new file mode 100644
index 0000000..02df713
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/internal-sysdeps.hpp
@@ -0,0 +1,41 @@
+#ifndef MLIBC_INTERNAL_SYSDEPS
+#define MLIBC_INTERNAL_SYSDEPS
+
+#include <stddef.h>
+
+#include <abi-bits/seek-whence.h>
+#include <abi-bits/vm-flags.h>
+#include <bits/off_t.h>
+#include <bits/ssize_t.h>
+#include <abi-bits/stat.h>
+#include <mlibc/fsfd_target.hpp>
+
+namespace [[gnu::visibility("hidden")]] mlibc {
+
+void sys_libc_log(const char *message);
+[[noreturn]] void sys_libc_panic();
+
+int sys_tcb_set(void *pointer);
+
+[[gnu::weak]] int sys_futex_tid();
+int sys_futex_wait(int *pointer, int expected, const struct timespec *time);
+int sys_futex_wake(int *pointer);
+
+int sys_anon_allocate(size_t size, void **pointer);
+int sys_anon_free(void *pointer, size_t size);
+
+int sys_open(const char *pathname, int flags, mode_t mode, int *fd);
+int sys_read(int fd, void *buf, size_t count, ssize_t *bytes_read);
+int sys_seek(int fd, off_t offset, int whence, off_t *new_offset);
+int sys_close(int fd);
+
+[[gnu::weak]] int sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags,
+ struct stat *statbuf);
+// mlibc assumes that anonymous memory returned by sys_vm_map() is zeroed by the kernel / whatever is behind the sysdeps
+int sys_vm_map(void *hint, size_t size, int prot, int flags, int fd, off_t offset, void **window);
+int sys_vm_unmap(void *pointer, size_t size);
+[[gnu::weak]] int sys_vm_protect(void *pointer, size_t size, int prot);
+
+} //namespace mlibc
+
+#endif // MLIBC_INTERNAL_SYSDEPS
diff --git a/lib/mlibc/options/internal/include/mlibc/locale.hpp b/lib/mlibc/options/internal/include/mlibc/locale.hpp
new file mode 100644
index 0000000..a46a2c3
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/locale.hpp
@@ -0,0 +1,12 @@
+#ifndef MLIBC_LOCALE
+#define MLIBC_LOCALE
+
+#include <bits/nl_item.h>
+
+namespace mlibc {
+
+char *nl_langinfo(nl_item item);
+
+} // namespace mlibc
+
+#endif // MLIBC_LOCALE
diff --git a/lib/mlibc/options/internal/include/mlibc/lock.hpp b/lib/mlibc/options/internal/include/mlibc/lock.hpp
new file mode 100644
index 0000000..aa05079
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/lock.hpp
@@ -0,0 +1,124 @@
+#ifndef MLIBC_LOCK_HPP
+#define MLIBC_LOCK_HPP
+
+#include <errno.h>
+#include <stdint.h>
+#include <mlibc/internal-sysdeps.hpp>
+#include <mlibc/debug.hpp>
+#include <mlibc/tid.hpp>
+#include <bits/ensure.h>
+
+template<bool Recursive>
+struct FutexLockImpl {
+ FutexLockImpl() : _state{0}, _recursion{0} { }
+
+ FutexLockImpl(const FutexLockImpl &) = delete;
+
+ FutexLockImpl &operator= (const FutexLockImpl &) = delete;
+
+ static constexpr uint32_t waitersBit = (1 << 31);
+ static constexpr uint32_t ownerMask = (static_cast<uint32_t>(1) << 30) - 1;
+
+ void lock() {
+ unsigned int this_tid = mlibc::this_tid();
+ unsigned int expected = 0;
+
+ while(true) {
+ if(!expected) {
+ // Try to take the mutex here.
+ if(__atomic_compare_exchange_n(&_state,
+ &expected, this_tid, false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE)) {
+ if constexpr (Recursive) {
+ __ensure(!_recursion);
+ _recursion = 1;
+ }
+ return;
+ }
+ }else{
+ // If this (recursive) mutex is already owned by us, increment the recursion level.
+ if((expected & ownerMask) == this_tid) {
+ if constexpr (Recursive)
+ ++_recursion;
+ else
+ mlibc::panicLogger() << "mlibc: FutexLock deadlock detected!" << frg::endlog;
+ return;
+ }
+
+ // Wait on the futex if the waiters flag is set.
+ if(expected & waitersBit) {
+ int e = mlibc::sys_futex_wait((int *)&_state, expected, nullptr);
+
+ // If the wait returns EAGAIN, that means that the waitersBit was just unset by
+ // some other thread. In this case, we should loop back around.
+ if (e && e != EAGAIN)
+ mlibc::panicLogger() << "sys_futex_wait() failed with error code " << e << frg::endlog;
+
+ // Opportunistically try to take the lock after we wake up.
+ expected = 0;
+ }else{
+ // Otherwise we have to set the waiters flag first.
+ unsigned int desired = expected | waitersBit;
+ if(__atomic_compare_exchange_n((int *)&_state,
+ reinterpret_cast<int*>(&expected), desired, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
+ expected = desired;
+ }
+ }
+ }
+ }
+
+ bool try_lock() {
+ unsigned int this_tid = mlibc::this_tid();
+ unsigned int expected = __atomic_load_n(&_state, __ATOMIC_RELAXED);
+
+ if(!expected) {
+ // Try to take the mutex here.
+ if(__atomic_compare_exchange_n(&_state,
+ &expected, this_tid, false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE)) {
+ if constexpr (Recursive)
+ _recursion = 1;
+ return true;
+ }
+ } else {
+ // If this (recursive) mutex is already owned by us, increment the recursion level.
+ if((expected & ownerMask) == this_tid) {
+ if constexpr (Recursive) {
+ __ensure(!_recursion);
+ ++_recursion;
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ void unlock() {
+ // Decrement the recursion level and unlock if we hit zero.
+ if constexpr (Recursive) {
+ __ensure(_recursion);
+ if(--_recursion)
+ return;
+ }
+
+ // Reset the mutex to the unlocked state.
+ auto state = __atomic_exchange_n(&_state, 0, __ATOMIC_RELEASE);
+ __ensure((state & ownerMask) == mlibc::this_tid());
+
+ if(state & waitersBit) {
+ // Wake the futex if there were waiters. Since the mutex might not exist at this location
+ // anymore, we must conservatively ignore EACCES and EINVAL which may occur as a result.
+ int e = mlibc::sys_futex_wake((int *)&_state);
+ __ensure(e >= 0 || e == EACCES || e == EINVAL);
+ }
+ }
+private:
+ uint32_t _state;
+ uint32_t _recursion;
+};
+
+using FutexLock = FutexLockImpl<false>;
+using RecursiveFutexLock = FutexLockImpl<true>;
+
+#endif
diff --git a/lib/mlibc/options/internal/include/mlibc/stack_protector.hpp b/lib/mlibc/options/internal/include/mlibc/stack_protector.hpp
new file mode 100644
index 0000000..47290fc
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/stack_protector.hpp
@@ -0,0 +1,10 @@
+#ifndef MLIBC_STACK_PROTECTOR_HPP
+#define MLIBC_STACK_PROTECTOR_HPP
+
+namespace mlibc {
+
+void initStackGuard(void *);
+
+} // namespace mlibc
+
+#endif // MLIBC_STACK_PROTECTOR_HPP
diff --git a/lib/mlibc/options/internal/include/mlibc/strings.hpp b/lib/mlibc/options/internal/include/mlibc/strings.hpp
new file mode 100644
index 0000000..5a93c7c
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/strings.hpp
@@ -0,0 +1,12 @@
+#ifndef MLIBC_STRINGS
+#define MLIBC_STRINGS
+
+#include <bits/size_t.h>
+
+namespace mlibc {
+
+int strncasecmp(const char *a, const char *b, size_t size);
+
+} // namespace mlibc
+
+#endif // MLIBC_STRINGS
diff --git a/lib/mlibc/options/internal/include/mlibc/strtofp.hpp b/lib/mlibc/options/internal/include/mlibc/strtofp.hpp
new file mode 100644
index 0000000..f9c5e20
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/strtofp.hpp
@@ -0,0 +1,165 @@
+#ifndef MLIBC_STRTOFP_HPP
+#define MLIBC_STRTOFP_HPP
+
+#include <string.h>
+#include <bits/ensure.h>
+#include <type_traits>
+
+namespace mlibc {
+
+template<typename T>
+T strtofp(const char *str, char **endptr) {
+ if (strcmp(str, "INF") == 0 || strcmp(str, "inf") == 0) {
+ if (endptr)
+ *endptr = (char *)str + 3;
+ if constexpr (std::is_same_v<T, float>)
+ return __builtin_inff();
+ else if constexpr (std::is_same_v<T, double>)
+ return __builtin_inf();
+ else
+ return __builtin_infl();
+ } else if (strcmp(str, "INFINITY") == 0 || strcmp(str, "infinity") == 0) {
+ if (endptr)
+ *endptr = (char *)str + 8;
+ if constexpr (std::is_same_v<T, float>)
+ return __builtin_inff();
+ else if constexpr (std::is_same_v<T, double>)
+ return __builtin_inf();
+ else
+ return __builtin_infl();
+ } else if (strncmp(str, "NAN", 3) == 0 || strncmp(str, "nan", 3) == 0) {
+ if (endptr)
+ *endptr = (char *)str + 3;
+ if constexpr (std::is_same_v<T, float>)
+ return __builtin_nanf("");
+ else if constexpr (std::is_same_v<T, double>)
+ return __builtin_nan("");
+ else
+ return __builtin_nanl("");
+ }
+
+ bool negative = *str == '-';
+ if (*str == '+' || *str == '-')
+ str++;
+
+ bool hex = false;
+ if (*str == '0' && (*(str + 1) == 'x' || *(str + 1) == 'X')) {
+ str += 2;
+ hex = true;
+ }
+
+ T result = static_cast<T>(0);
+
+ const char *tmp = str;
+
+ if (!hex) {
+ while (true) {
+ if (!isdigit(*tmp))
+ break;
+ result *= static_cast<T>(10);
+ result += static_cast<T>(*tmp - '0');
+ tmp++;
+ }
+ } else {
+ while (true) {
+ if (!isxdigit(*tmp))
+ break;
+ result *= static_cast<T>(16);
+ result += static_cast<T>(*tmp <= '9' ? (*tmp - '0') : (tolower(*tmp) - 'a' + 10));
+ tmp++;
+ }
+ }
+
+ if (*tmp == '.') {
+ tmp++;
+
+ if (!hex) {
+ T d = static_cast<T>(10);
+
+ while (true) {
+ if (!isdigit(*tmp))
+ break;
+ result += static_cast<T>(*tmp - '0') / d;
+ d *= static_cast<T>(10);
+ tmp++;
+ }
+ } else {
+ T d = static_cast<T>(16);
+
+ while (true) {
+ if (!isxdigit(*tmp))
+ break;
+ result += static_cast<T>(*tmp <= '9' ? (*tmp - '0') : (tolower(*tmp) - 'a' + 10)) / d;
+ d *= static_cast<T>(16);
+ tmp++;
+ }
+ }
+ }
+
+ if (!hex) {
+ if (*tmp == 'e' || *tmp == 'E') {
+ tmp++;
+
+ bool exp_negative = *tmp == '-';
+ if (*tmp == '+' || *tmp == '-')
+ tmp++;
+
+ int exp = 0;
+ while (true) {
+ if (!isdigit(*tmp))
+ break;
+ exp *= 10;
+ exp += *tmp - '0';
+ tmp++;
+ }
+
+ if (!exp_negative) {
+ for (int i = 0; i < exp; ++i) {
+ result *= static_cast<T>(10);
+ }
+ } else {
+ for (int i = 0; i < exp; ++i) {
+ result /= static_cast<T>(10);
+ }
+ }
+ }
+ } else {
+ if (*tmp == 'p' || *tmp == 'P') {
+ tmp++;
+
+ bool exp_negative = *tmp == '-';
+ if (*tmp == '+' || *tmp == '-')
+ tmp++;
+
+ int exp = 0;
+ while (true) {
+ if (!isdigit(*tmp))
+ break;
+ exp *= 10;
+ exp += *tmp - '0';
+ tmp++;
+ }
+
+ if (!exp_negative) {
+ for (int i = 0; i < exp; ++i) {
+ result *= static_cast<T>(2);
+ }
+ } else {
+ for (int i = 0; i < exp; ++i) {
+ result /= static_cast<T>(2);
+ }
+ }
+ }
+ }
+
+ if (endptr)
+ *endptr = const_cast<char *>(tmp);
+ if (negative)
+ result = -result;
+
+ return result;
+}
+
+}
+
+#endif // MLIBC_STRTOFP_HPP
diff --git a/lib/mlibc/options/internal/include/mlibc/strtol.hpp b/lib/mlibc/options/internal/include/mlibc/strtol.hpp
new file mode 100644
index 0000000..3b8fca9
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/strtol.hpp
@@ -0,0 +1,159 @@
+#ifndef MLIBC_STRTOL_HPP
+#define MLIBC_STRTOL_HPP
+
+#include <type_traits>
+#include <ctype.h>
+#include <wctype.h>
+#include <limits.h>
+
+namespace mlibc {
+
+template<typename T> struct int_limits {};
+
+template<>
+struct int_limits<long> {
+ static long max() { return LONG_MAX; }
+ static long min() { return LONG_MIN; }
+};
+
+template<>
+struct int_limits<unsigned long> {
+ static unsigned long max() { return ULONG_MAX; }
+ static unsigned long min() { return 0; }
+};
+
+template<>
+struct int_limits<long long> {
+ static long long max() { return LLONG_MAX; }
+ static long long min() { return LLONG_MIN; }
+};
+
+template<>
+struct int_limits<unsigned long long> {
+ static unsigned long long max() { return ULLONG_MAX; }
+ static unsigned long long min() { return 0; }
+};
+
+template<typename T> struct char_detail {};
+
+template<>
+struct char_detail<char> {
+ static bool isSpace(char c) { return isspace(c); }
+ static bool isDigit(char c) { return isdigit(c); }
+ static bool isHexDigit(char c) { return isxdigit(c); }
+ static bool isLower(char c) { return islower(c); }
+ static bool isUpper(char c) { return isupper(c); }
+};
+
+template<>
+struct char_detail<wchar_t> {
+ static bool isSpace(wchar_t c) { return iswspace(c); }
+ static bool isDigit(wchar_t c) { return iswdigit(c); }
+ static bool isHexDigit(wchar_t c) { return iswxdigit(c); }
+ static bool isLower(wchar_t c) { return iswlower(c); }
+ static bool isUpper(wchar_t c) { return iswupper(c); }
+};
+
+template<typename Char> Char widen(char c) { return static_cast<Char>(c); }
+
+template<typename Return, typename Char>
+Return stringToInteger(const Char *__restrict nptr, Char **__restrict endptr, int baseInt) {
+ using UnsignedReturn = std::make_unsigned_t<Return>;
+
+ auto base = static_cast<Return>(baseInt);
+ auto s = nptr;
+
+ if (base < 0 || base == 1) {
+ if (endptr)
+ *endptr = const_cast<Char *>(nptr);
+ return 0;
+ }
+
+ while (char_detail<Char>::isSpace(*s))
+ s++;
+
+ bool negative = false;
+ if (*s == widen<Char>('-')) {
+ negative = true;
+ s++;
+ } else if (*s == widen<Char>('+')) {
+ s++;
+ }
+
+
+ bool hasOctalPrefix = s[0] == widen<Char>('0');
+ bool hasHexPrefix = hasOctalPrefix && (s[1] == widen<Char>('x') || s[1] == widen<Char>('X'));
+
+ // There's two tricky cases we need to keep in mind here:
+ // 1. We should interpret "0x5" as hex 5 rather than octal 0.
+ // 2. We should interpret "0x" as octal 0 (and set endptr correctly).
+ // To deal with 2, we check the charcacter following the hex prefix.
+ if ((base == 0 || base == 16) && hasHexPrefix && char_detail<Char>::isHexDigit(s[2])) {
+ s += 2;
+ base = 16;
+ } else if ((base == 0 || base == 8) && hasOctalPrefix) {
+ base = 8;
+ } else if (base == 0) {
+ base = 10;
+ }
+
+ // Compute the range of acceptable values.
+ UnsignedReturn cutoff, cutlim;
+ if (std::is_unsigned_v<Return>) {
+ cutoff = int_limits<Return>::max() / base;
+ cutlim = int_limits<Return>::max() % base;
+ } else {
+ Return co = negative ? int_limits<Return>::min() : int_limits<Return>::max();
+ cutlim = negative ? -(co % base) : co % base;
+ co /= negative ? -base : base;
+ cutoff = co;
+ }
+
+ UnsignedReturn totalValue = 0;
+ bool convertedAny = false;
+ bool outOfRange = false;
+ for (Char c = *s; c != widen<Char>('\0'); c = *++s) {
+ UnsignedReturn digitValue;
+ if (char_detail<Char>::isDigit(c))
+ digitValue = c - widen<Char>('0');
+ else if (char_detail<Char>::isUpper(c))
+ digitValue = c - widen<Char>('A') + 10;
+ else if (char_detail<Char>::isLower(c))
+ digitValue = c - widen<Char>('a') + 10;
+ else
+ break;
+
+ if (digitValue >= static_cast<UnsignedReturn>(base))
+ break;
+
+ if (outOfRange) {
+ // The value is already known to be out of range, but we need to keep
+ // consuming characters until we can't (to set endptr correctly).
+ } else if (totalValue > cutoff || (totalValue == cutoff && digitValue > cutlim)) {
+ // The value will be out of range if we accumulate digitValue.
+ outOfRange = true;
+ } else {
+ totalValue = (totalValue * base) + digitValue;
+ convertedAny = true;
+ }
+ }
+
+ if (endptr)
+ *endptr = const_cast<Char *>(convertedAny ? s : nptr);
+
+ if (outOfRange) {
+ errno = ERANGE;
+
+ if (std::is_unsigned_v<Return>) {
+ return int_limits<Return>::max();
+ } else {
+ return negative ? int_limits<Return>::min() : int_limits<Return>::max();
+ }
+ }
+
+ return negative ? -totalValue : totalValue;
+}
+
+}
+
+#endif // MLIBC_STRTOL_HPP
diff --git a/lib/mlibc/options/internal/include/mlibc/tcb.hpp b/lib/mlibc/options/internal/include/mlibc/tcb.hpp
new file mode 100644
index 0000000..92aad7a
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/tcb.hpp
@@ -0,0 +1,180 @@
+#pragma once
+
+#include <stdint.h>
+#include <limits.h>
+#include <bits/size_t.h>
+#include <frg/array.hpp>
+
+#include "elf.hpp"
+
+/*
+ * Explanation of cancellation bits:
+ *
+ * tcbCancelEnableBit and tcbCancelAsyncBit should be self-explanatory,
+ * they are set if cancellation is enabled, or asynchronous, respectively.
+ *
+ * tcbCancelTriggerBit is set whenever a cancellation is triggered, which is
+ * in pthread_cancel() or in the signal handler. This bit is used by
+ * pthread_testcancel() to check whether a cancellation has been requested,
+ * and also by cancellable syscalls.
+ *
+ * tcbCancelingBit is set when a cancellation is currently being handled. This
+ * is to avoid a situation in which a cancellation handler gets interrupted by
+ * a SIGCANCEL and a second cancellation handler gets executed on top of the
+ * previous one. Right now this cannot happen, since we stay in signal handler
+ * context when canceling/exiting. In the future this might be done outside
+ * of a signal handler, in which case we shouldn't restart the cancellation process.
+ *
+ * tcbExitingBit is set when the thread starts the exit procedure. Currently
+ * this is just an exit, but in the future this will be a stack unwinding
+ * procedure, which shouldn't be reentered. Not currently set anywhere,
+ * may be done so in the future.
+ *
+ * TODO(geert): update this comment when we do unwinding in the exit procedure.
+ */
+
+namespace {
+ // Set when the cancellation is enabled
+ constexpr unsigned int tcbCancelEnableBit = 1 << 0;
+ // 1 - cancellation is asynchronous, 0 - cancellation is deferred
+ constexpr unsigned int tcbCancelAsyncBit = 1 << 1;
+ // Set when the thread has been cancelled
+ constexpr unsigned int tcbCancelTriggerBit = 1 << 2;
+ // Set when the thread is in the process of being cancelled.
+ constexpr unsigned int tcbCancelingBit = 1 << 3;
+ // Set when the thread is exiting.
+ constexpr unsigned int tcbExitingBit = 1 << 4;
+}
+
+namespace mlibc {
+ // Returns true when bitmask indicates thread has been asynchronously
+ // cancelled.
+ static constexpr bool tcb_async_cancelled(int value) {
+ return (value & (tcbCancelEnableBit | tcbCancelAsyncBit
+ | tcbCancelTriggerBit)) == (tcbCancelEnableBit
+ | tcbCancelAsyncBit | tcbCancelTriggerBit);
+ }
+
+ // Returns true when bitmask indicates async cancellation is enabled.
+ static constexpr bool tcb_async_cancel(int value) {
+ return (value & (tcbCancelEnableBit | tcbCancelAsyncBit))
+ == (tcbCancelEnableBit | tcbCancelAsyncBit);
+ }
+
+ // Returns true when bitmask indicates cancellation is enabled.
+ static constexpr bool tcb_cancel_enabled(int value) {
+ return (value & tcbCancelEnableBit);
+ }
+
+ // Returns true when bitmask indicates threas has been cancelled.
+ static constexpr bool tcb_cancelled(int value) {
+ return (value & (tcbCancelEnableBit | tcbCancelTriggerBit))
+ == (tcbCancelEnableBit | tcbCancelTriggerBit);
+ }
+
+#if !MLIBC_STATIC_BUILD && !MLIBC_BUILDING_RTDL
+ // In non-static builds, libc.so always has a TCB available.
+ constexpr bool tcb_available_flag = true;
+#else
+ // Otherwise this will be set to true after RTDL has initialized the TCB.
+ extern bool tcb_available_flag;
+#endif
+}
+
+enum class TcbThreadReturnValue {
+ Pointer,
+ Integer,
+};
+
+struct Tcb {
+ Tcb *selfPointer;
+ size_t dtvSize;
+ void **dtvPointers;
+ int tid;
+ int didExit;
+#if defined(__x86_64__)
+ uint8_t padding[8];
+#endif
+ uintptr_t stackCanary;
+ int cancelBits;
+
+ union {
+ void *voidPtr;
+ int intVal;
+ } returnValue;
+ TcbThreadReturnValue returnValueType;
+
+ struct AtforkHandler {
+ void (*prepare)(void);
+ void (*parent)(void);
+ void (*child)(void);
+
+ AtforkHandler *next;
+ AtforkHandler *prev;
+ };
+
+ AtforkHandler *atforkBegin;
+ AtforkHandler *atforkEnd;
+
+ struct CleanupHandler {
+ void (*func)(void *);
+ void *arg;
+
+ CleanupHandler *next;
+ CleanupHandler *prev;
+ };
+
+ CleanupHandler *cleanupBegin;
+ CleanupHandler *cleanupEnd;
+ int isJoinable;
+
+ struct LocalKey {
+ void *value;
+ uint64_t generation;
+ };
+ frg::array<LocalKey, PTHREAD_KEYS_MAX> *localKeys;
+
+ size_t stackSize;
+ void *stackAddr;
+ size_t guardSize;
+
+ inline void invokeThreadFunc(void *entry, void *user_arg) {
+ if(returnValueType == TcbThreadReturnValue::Pointer) {
+ auto func = reinterpret_cast<void *(*)(void *)>(entry);
+ returnValue.voidPtr = func(user_arg);
+ } else {
+ auto func = reinterpret_cast<int (*)(void *)>(entry);
+ returnValue.intVal = func(user_arg);
+ }
+ }
+};
+
+// There are a few places where we assume the layout of the TCB:
+#if defined(__x86_64__)
+// GCC expects the stack canary to be at fs:0x28.
+static_assert(offsetof(Tcb, stackCanary) == 0x28);
+// sysdeps/linux/x86_64/cp_syscall.S uses the offset of cancelBits.
+static_assert(offsetof(Tcb, cancelBits) == 0x30);
+#elif defined(__i386__)
+// GCC expects the stack canary to be at gs:0x14.
+// The offset differs from x86_64 due to the change in the pointer size
+// and removed padding before the stack canary.
+static_assert(offsetof(Tcb, stackCanary) == 0x14);
+// sysdeps/linux/x86/cp_syscall.S uses the offset of cancelBits.
+// It differs from x86_64 for the same reasons as the stack canary.
+static_assert(offsetof(Tcb, cancelBits) == 0x18);
+#elif defined(__aarch64__)
+// The thread pointer on AArch64 points to 16 bytes before the end of the TCB.
+// options/linker/aarch64/runtime.S uses the offset of dtvPointers.
+static_assert(sizeof(Tcb) - offsetof(Tcb, dtvPointers) - TP_TCB_OFFSET == 104);
+// sysdeps/linux/aarch64/cp_syscall.S uses the offset of cancelBits.
+static_assert(sizeof(Tcb) - offsetof(Tcb, cancelBits) - TP_TCB_OFFSET == 80);
+#elif defined(__riscv) && __riscv_xlen == 64
+// The thread pointer on RISC-V points to *after* the TCB, and since
+// we need to access specific fields that means that the value in
+// sysdeps/linux/riscv64/cp_syscall.S needs to be updated whenever
+// the struct is expanded.
+static_assert(sizeof(Tcb) - offsetof(Tcb, cancelBits) == 96);
+#else
+#error "Missing architecture specific code."
+#endif
diff --git a/lib/mlibc/options/internal/include/mlibc/threads.hpp b/lib/mlibc/options/internal/include/mlibc/threads.hpp
new file mode 100644
index 0000000..989a8e5
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/threads.hpp
@@ -0,0 +1,27 @@
+#pragma once
+
+#include <bits/ansi/timespec.h>
+#include <bits/threads.h>
+
+namespace mlibc {
+
+int thread_create(struct __mlibc_thread_data **__restrict thread, const struct __mlibc_threadattr *__restrict attrp, void *entry, void *__restrict user_arg, bool returns_int);
+int thread_attr_init(struct __mlibc_threadattr *attr);
+int thread_join(struct __mlibc_thread_data *thread, void *res);
+
+int thread_mutex_init(struct __mlibc_mutex *__restrict mutex, const struct __mlibc_mutexattr *__restrict attr);
+int thread_mutex_destroy(struct __mlibc_mutex *mutex);
+int thread_mutex_lock(struct __mlibc_mutex *mutex);
+int thread_mutex_unlock(struct __mlibc_mutex *mutex);
+
+int thread_mutexattr_init(struct __mlibc_mutexattr *attr);
+int thread_mutexattr_destroy(struct __mlibc_mutexattr *attr);
+int thread_mutexattr_gettype(const struct __mlibc_mutexattr *__restrict attr, int *__restrict type);
+int thread_mutexattr_settype(struct __mlibc_mutexattr *attr, int type);
+
+int thread_cond_init(struct __mlibc_cond *__restrict cond, const struct __mlibc_condattr *__restrict attr);
+int thread_cond_destroy(struct __mlibc_cond *cond);
+int thread_cond_broadcast(struct __mlibc_cond *cond);
+int thread_cond_timedwait(struct __mlibc_cond *__restrict cond, __mlibc_mutex *__restrict mutex, const struct timespec *__restrict abstime);
+
+}
diff --git a/lib/mlibc/options/internal/include/mlibc/tid.hpp b/lib/mlibc/options/internal/include/mlibc/tid.hpp
new file mode 100644
index 0000000..e85c19f
--- /dev/null
+++ b/lib/mlibc/options/internal/include/mlibc/tid.hpp
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <mlibc/thread.hpp>
+#include <mlibc/internal-sysdeps.hpp>
+
+namespace mlibc {
+ inline unsigned int this_tid() {
+ // During RTDL initialization, we don't have a TCB.
+ if (mlibc::tcb_available_flag) {
+ auto tcb = get_current_tcb();
+ return tcb->tid;
+ } else if (mlibc::sys_futex_tid) {
+ return mlibc::sys_futex_tid();
+ } else {
+ return 1;
+ }
+ }
+}
diff --git a/lib/mlibc/options/internal/include/stdint.h b/lib/mlibc/options/internal/include/stdint.h
new file mode 100644
index 0000000..4d8df66
--- /dev/null
+++ b/lib/mlibc/options/internal/include/stdint.h
@@ -0,0 +1,150 @@
+#ifndef _MLIBC_STDINT_H
+#define _MLIBC_STDINT_H
+
+#include <bits/types.h>
+#include <bits/wchar.h>
+
+// ----------------------------------------------------------------------------
+// Type definitions.
+// ----------------------------------------------------------------------------
+
+// Fixed-width (signed).
+typedef __mlibc_int8 int8_t;
+typedef __mlibc_int16 int16_t;
+typedef __mlibc_int32 int32_t;
+typedef __mlibc_int64 int64_t;
+
+// Fixed-width (unsigned).
+typedef __mlibc_uint8 uint8_t;
+typedef __mlibc_uint16 uint16_t;
+typedef __mlibc_uint32 uint32_t;
+typedef __mlibc_uint64 uint64_t;
+
+// Least-width (signed).
+typedef __mlibc_int8 int_least8_t;
+typedef __mlibc_int16 int_least16_t;
+typedef __mlibc_int32 int_least32_t;
+typedef __mlibc_int64 int_least64_t;
+
+// Least-width (unsigned).
+typedef __mlibc_uint8 uint_least8_t;
+typedef __mlibc_uint16 uint_least16_t;
+typedef __mlibc_uint32 uint_least32_t;
+typedef __mlibc_uint64 uint_least64_t;
+
+// Fast-width (signed).
+typedef __mlibc_int_fast8 int_fast8_t;
+typedef __mlibc_int_fast16 int_fast16_t;
+typedef __mlibc_int_fast32 int_fast32_t;
+typedef __mlibc_int_fast64 int_fast64_t;
+
+// Fast-width (unsigned).
+typedef __mlibc_uint_fast8 uint_fast8_t;
+typedef __mlibc_uint_fast16 uint_fast16_t;
+typedef __mlibc_uint_fast32 uint_fast32_t;
+typedef __mlibc_uint_fast64 uint_fast64_t;
+
+// Miscellaneous (signed).
+typedef __mlibc_intmax intmax_t;
+typedef __mlibc_intptr intptr_t;
+
+// Miscellaneous (unsigned).
+typedef __mlibc_uintmax uintmax_t;
+typedef __mlibc_uintptr uintptr_t;
+
+// ----------------------------------------------------------------------------
+// Constants.
+// ----------------------------------------------------------------------------
+
+// Fixed-width (signed).
+#define INT8_C(x) __MLIBC_INT8_C(x)
+#define INT16_C(x) __MLIBC_INT16_C(x)
+#define INT32_C(x) __MLIBC_INT32_C(x)
+#define INT64_C(x) __MLIBC_INT64_C(x)
+#define INTMAX_C(x) __MLIBC_INTMAX_C(x)
+
+// Fixed-width (unsigned).
+#define UINT8_C(x) __MLIBC_UINT8_C(x)
+#define UINT16_C(x) __MLIBC_UINT16_C(x)
+#define UINT32_C(x) __MLIBC_UINT32_C(x)
+#define UINT64_C(x) __MLIBC_UINT64_C(x)
+#define UINTMAX_C(x) __MLIBC_UINTMAX_C(x)
+
+// ----------------------------------------------------------------------------
+// Limits.
+// ----------------------------------------------------------------------------
+
+// Fixed-width (signed).
+#define INT8_MAX __MLIBC_INT8_MAX
+#define INT16_MAX __MLIBC_INT16_MAX
+#define INT32_MAX __MLIBC_INT32_MAX
+#define INT64_MAX __MLIBC_INT64_MAX
+
+#define INT8_MIN __MLIBC_INT8_MIN
+#define INT16_MIN __MLIBC_INT16_MIN
+#define INT32_MIN __MLIBC_INT32_MIN
+#define INT64_MIN __MLIBC_INT64_MIN
+
+// Fixed-width (unsigned).
+#define UINT8_MAX __MLIBC_UINT8_MAX
+#define UINT16_MAX __MLIBC_UINT16_MAX
+#define UINT32_MAX __MLIBC_UINT32_MAX
+#define UINT64_MAX __MLIBC_UINT64_MAX
+
+// Least-width (signed).
+#define INT_LEAST8_MAX __MLIBC_INT8_MAX
+#define INT_LEAST16_MAX __MLIBC_INT16_MAX
+#define INT_LEAST32_MAX __MLIBC_INT32_MAX
+#define INT_LEAST64_MAX __MLIBC_INT64_MAX
+
+#define INT_LEAST8_MIN __MLIBC_INT8_MIN
+#define INT_LEAST16_MIN __MLIBC_INT16_MIN
+#define INT_LEAST32_MIN __MLIBC_INT32_MIN
+#define INT_LEAST64_MIN __MLIBC_INT64_MIN
+
+// Least-width (unsigned).
+#define UINT_LEAST8_MAX __MLIBC_UINT8_MAX
+#define UINT_LEAST16_MAX __MLIBC_UINT16_MAX
+#define UINT_LEAST32_MAX __MLIBC_UINT32_MAX
+#define UINT_LEAST64_MAX __MLIBC_UINT64_MAX
+
+// Fast-width (signed).
+#define INT_FAST8_MAX __MLIBC_INT_FAST8_MAX
+#define INT_FAST16_MAX __MLIBC_INT_FAST16_MAX
+#define INT_FAST32_MAX __MLIBC_INT_FAST32_MAX
+#define INT_FAST64_MAX __MLIBC_INT_FAST64_MAX
+
+#define INT_FAST8_MIN __MLIBC_INT_FAST8_MIN
+#define INT_FAST16_MIN __MLIBC_INT_FAST16_MIN
+#define INT_FAST32_MIN __MLIBC_INT_FAST32_MIN
+#define INT_FAST64_MIN __MLIBC_INT_FAST64_MIN
+
+// Fast-width (unsigned).
+#define UINT_FAST8_MAX __MLIBC_UINT_FAST8_MAX
+#define UINT_FAST16_MAX __MLIBC_UINT_FAST16_MAX
+#define UINT_FAST32_MAX __MLIBC_UINT_FAST32_MAX
+#define UINT_FAST64_MAX __MLIBC_UINT_FAST64_MAX
+
+// Miscellaneous (signed).
+#define INTMAX_MAX __MLIBC_INTMAX_MAX
+#define INTPTR_MAX __MLIBC_INTPTR_MAX
+
+#define INTMAX_MIN __MLIBC_INTMAX_MIN
+#define INTPTR_MIN __MLIBC_INTPTR_MIN
+
+// Miscellaneous (unsigned).
+#define UINTMAX_MAX __MLIBC_UINTMAX_MAX
+#define UINTPTR_MAX __MLIBC_UINTPTR_MAX
+
+// Other limits (signed).
+#define PTRDIFF_MAX __MLIBC_PTRDIFF_MAX
+#define PTRDIFF_MIN __MLIBC_PTRDIFF_MIN
+#define SIG_ATOMIC_MAX __MLIBC_SIG_ATOMIC_MAX
+#define SIG_ATOMIC_MIN __MLIBC_SIG_ATOMIC_MIN
+#define WINT_MAX __MLIBC_WINT_MAX
+#define WINT_MIN __MLIBC_WINT_MIN
+
+// Other limits (unsigned).
+#define SIZE_MAX __MLIBC_SIZE_MAX
+
+#endif // _MLIBC_STDINT_H