From 8e3394a51e08affcf23cc254b92f1564b9b96972 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Thu, 15 Apr 2021 16:55:09 +0100 Subject: [PATCH] Drop dependency on gcc-multilib inttypes.h in particular isn't a freestanding header, and certain distros have problems providing suitable freestanding headers anyway. This also gets more complicated as we start supporting other architectures. Take the plunge and switch to entirely local headers only. Signed-off-by: Andrew Cooper --- CODING_STYLE | 13 +------------ build/common.mk | 2 +- docs/mainpage.dox | 21 +++++++++------------ include/inttypes.h | 37 +++++++++++++++++++++++++++++++++++++ include/limits.h | 21 +++++++++++++++++++++ include/stdarg.h | 25 +++++++++++++++++++++++++ include/stdbool.h | 23 +++++++++++++++++++++++ include/stddef.h | 26 ++++++++++++++++++++++++++ include/stdint.h | 32 ++++++++++++++++++++++++++++++++ 9 files changed, 175 insertions(+), 25 deletions(-) create mode 100644 include/inttypes.h create mode 100644 include/limits.h create mode 100644 include/stdarg.h create mode 100644 include/stdbool.h create mode 100644 include/stddef.h create mode 100644 include/stdint.h diff --git a/CODING_STYLE b/CODING_STYLE index 761de46..866da96 100644 --- a/CODING_STYLE +++ b/CODING_STYLE @@ -112,15 +112,4 @@ Header files ------------ A microkernel will not make use of system libraries, and provide local -implementations of all required functionality. C99 however provides -standard headers which are explicitly safe for embedded use. These -should be used instead of reimplementing equivalent functionality -locally. - -An incomplete list includes: - - * inttypes.h (PRIx64, ...) - * limits.h (INT_MAX, ...) - * stdarg.h (va_list, ...) - * stdbool.h (bool, true, false) - * stdint.h (uint??_t ...) +implementations of all required functionality. diff --git a/build/common.mk b/build/common.mk index 6480a54..2a146c6 100644 --- a/build/common.mk +++ b/build/common.mk @@ -25,7 +25,7 @@ COMMON_AFLAGS := $(COMMON_FLAGS) -D__ASSEMBLY__ COMMON_CFLAGS := $(COMMON_FLAGS) $(COMMON_CFLAGS-y) COMMON_CFLAGS += -Wall -Wextra -Werror -std=gnu99 -Wstrict-prototypes -O3 -g COMMON_CFLAGS += -fno-common -fno-asynchronous-unwind-tables -fno-strict-aliasing -COMMON_CFLAGS += -fno-stack-protector -fno-pic -ffreestanding +COMMON_CFLAGS += -fno-stack-protector -fno-pic -ffreestanding -nostdinc COMMON_CFLAGS += -mno-red-zone -mno-sse COMMON_CFLAGS += -Wno-unused-parameter -Winline diff --git a/docs/mainpage.dox b/docs/mainpage.dox index 4b09ce1..d9558d6 100644 --- a/docs/mainpage.dox +++ b/docs/mainpage.dox @@ -34,20 +34,17 @@ Environment | Guest | Width | Paging Requirements: - GNU Make >= 3.81 -- GNU compatible 32 and 64-bit toolchain, capable of `-std=gnu99`, `-m64`, and - `-m32` - - For Debian-based systems, the `build-essential` package is - sufficient. For RHEL-based systems, the `glibc-devel.i686` package is - generally needed beyond the default toolchain packages. - - Clang may be used, via `CC="clang"` - Python 2.6 or later -Optionally: -- A toolchain with x32 support. - - `hvm64` tests would prefer to use the `elf32-x86-64` format, so they - both load and disassemble correctly. In the absence of x32 support, - `elf32-i386` will be used which will load correctly, but disassemble - incorrectly. +For x86: + - GNU compatible 32 and 64-bit toolchain, capable of `-std=gnu99`, `-m64`, + and `-m32`. + - Clang may be used, via `CC="clang"`. + - Optionally, a toolchain with x32 support. + - `hvm64` tests would prefer to use the `elf32-x86-64` format, so they + both load and disassemble correctly. In the absence of x32 support, + `elf32-i386` will be used which will load correctly, but disassemble + incorrectly. To obtain and build: diff --git a/include/inttypes.h b/include/inttypes.h new file mode 100644 index 0000000..565fe9e --- /dev/null +++ b/include/inttypes.h @@ -0,0 +1,37 @@ +/** + * @file include/inttypes.h + * + * Local subset of C's inttypes.h + */ +#ifndef INTTYPES_H +#define INTTYPES_H + +#if __SIZEOF_LONG__ == 8 +# define __PRI64 "l" +# define __PRIPTR "l" +#else +# define __PRI64 "ll" +# define __PRIPTR +#endif + +#define PRId64 __PRI64 "d" +#define PRIx64 __PRI64 "x" +#define PRIo64 __PRI64 "o" +#define PRIu64 __PRI64 "u" + +#define PRIdPTR __PRIPTR "d" +#define PRIoPTR __PRIPTR "o" +#define PRIuPTR __PRIPTR "u" +#define PRIxPTR __PRIPTR "x" + +#endif /* INTTYPES_H */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/include/limits.h b/include/limits.h new file mode 100644 index 0000000..ae8b2d5 --- /dev/null +++ b/include/limits.h @@ -0,0 +1,21 @@ +/** + * @file include/limits.h + * + * Local subset of C's limits.h + */ +#ifndef LIMITS_H +#define LIMITS_H + +#define CHAR_BIT __CHAR_BIT__ + +#endif /* LIMITS_H */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/include/stdarg.h b/include/stdarg.h new file mode 100644 index 0000000..819ea55 --- /dev/null +++ b/include/stdarg.h @@ -0,0 +1,25 @@ +/** + * @file include/stdarg.h + * + * Local subset of C's stdarg.h + */ +#ifndef STDARG_H +#define STDARG_H + +typedef __builtin_va_list va_list; +#define va_start(v, l) __builtin_va_start(v, l) +#define va_end(v) __builtin_va_end(v) +#define va_arg(v, l) __builtin_va_arg(v, l) +#define va_copy(d, s) __builtin_va_copy(d, s) + +#endif /* STDARG_H */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/include/stdbool.h b/include/stdbool.h new file mode 100644 index 0000000..d4000b6 --- /dev/null +++ b/include/stdbool.h @@ -0,0 +1,23 @@ +/** + * @file include/stdbool.h + * + * Local subset of C's stdbool.h + */ +#ifndef STDBOOL_H +#define STDBOOL_H + +typedef _Bool bool; +#define true 1 +#define false 0 + +#endif /* STDBOOL_H */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/include/stddef.h b/include/stddef.h new file mode 100644 index 0000000..7bd0722 --- /dev/null +++ b/include/stddef.h @@ -0,0 +1,26 @@ +/** + * @file include/stddef.h + * + * Local subset of C's stddef.h + */ +#ifndef STDDEF_H +#define STDDEF_H + +typedef __SIZE_TYPE__ size_t; +typedef __PTRDIFF_TYPE__ ptrdiff_t; + +#define NULL ((void *)0) + +#define offsetof(t, m) __builtin_offsetof(t, m) + +#endif /* STDDEF_H */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/include/stdint.h b/include/stdint.h new file mode 100644 index 0000000..8fcb1b3 --- /dev/null +++ b/include/stdint.h @@ -0,0 +1,32 @@ +/** + * @file include/stdint.h + * + * Local subset of C's stdint.h + */ +#ifndef STDINT_H +#define STDINT_H + +typedef __INT8_TYPE__ int8_t; +typedef __INT16_TYPE__ int16_t; +typedef __INT32_TYPE__ int32_t; +typedef __INT64_TYPE__ int64_t; + +typedef __UINT8_TYPE__ uint8_t; +typedef __UINT16_TYPE__ uint16_t; +typedef __UINT32_TYPE__ uint32_t; +typedef __UINT64_TYPE__ uint64_t; + +typedef __INTPTR_TYPE__ intptr_t; +typedef __UINTPTR_TYPE__ uintptr_t; + +#endif /* STDINT_H */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ -- 2.39.5