Fix libc header dependencies

Turns out we had a bunch of silly dependencies on libc headers that are
not included with freestanding compilers. Fix all this and change the
CFLAGS to exclude libc headers and only include the built-in compiler
path.

Add our own versions of assert.h, errno.h, limits.h, and move malloc.h
and string.h together into a new path used as -isystem, so these headers
can be included using #include <>.

Remove a bunch of other dependencies in third-party code.

Add a strnlen function.

Disable building the libfdt overlay code for now, as it needs a strtoul
implementation. We can throw that in if/when we decide to use overlays.

Signed-off-by: Hector Martin <marcan@marcan.st>
This commit is contained in:
Hector Martin 2021-03-08 01:31:14 +09:00
parent 3c514e826c
commit efa4452a47
14 changed files with 77 additions and 34 deletions

View file

@ -1,9 +1,22 @@
ARCH := aarch64-linux-gnu-
ifeq ($(USE_CLANG),1)
CC := clang --target=$(ARCH)
AS := clang --target=$(ARCH)
LD := ld.lld
OBJCOPY := llvm-objcopy
else
CC := $(ARCH)gcc
AS := $(ARCH)gcc
LD := $(ARCH)ld
OBJCOPY := $(ARCH)objcopy
endif
CFLAGS := -O2 -Wall -Wundef -Werror=strict-prototypes -fno-common -fno-PIE \
-Werror=implicit-function-declaration -Werror=implicit-int \
-Wsign-compare -Wunused-parameter -Wno-multichar \
-ffreestanding -fpic -ffunction-sections -fdata-sections \
-nostdinc -isystem $(shell $(CC) -print-file-name=include) -isystem sysinc \
-fno-stack-protector -mgeneral-regs-only -mstrict-align -march=armv8.2-a
LDFLAGS := -T m1n1.ld -EL -maarch64elf --no-undefined -X -Bsymbolic \
@ -19,7 +32,7 @@ TINF_OBJECTS := $(patsubst %,tinf/%, \
DLMALLOC_OBJECTS := dlmalloc/malloc.o
LIBFDT_OBJECTS := $(patsubst %,libfdt/%, \
fdt_addresses.o fdt_empty_tree.o fdt_overlay.o fdt_ro.o fdt_rw.o fdt_strerror.o fdt_sw.o \
fdt_addresses.o fdt_empty_tree.o fdt_ro.o fdt_rw.o fdt_strerror.o fdt_sw.o \
fdt_wip.o fdt.o)
OBJECTS := adt.o bootlogo_128.o bootlogo_256.o chickens.o exception.o exception_asm.o fb.o \
@ -37,24 +50,12 @@ TARGET := m1n1.macho
DEPDIR := build/.deps
ifeq ($(USE_CLANG),1)
CC := clang --target=$(ARCH)
AS := clang --target=$(ARCH)
LD := ld.lld
OBJCOPY := llvm-objcopy
else
CC := $(ARCH)gcc
AS := $(ARCH)gcc
LD := $(ARCH)ld
OBJCOPY := $(ARCH)objcopy
endif
.PHONY: all clean format
all: build/$(TARGET) $(DTBS)
clean:
rm -rf build/*
format:
clang-format -i src/*.c src/*.h
clang-format -i src/*.c src/*.h sysinc/*.h
build/dtb/%.dtb: dts/%.dts
@echo " DTC $@"

View file

@ -1,5 +1,8 @@
/* SPDX-License-Identifier: MIT */
#include <string.h>
#include "../heapblock.h"
#include "../string.h"
#include "../utils.h"
#define HAVE_MORECORE 1
@ -13,15 +16,16 @@
#define NO_MALLINFO 1
#define NO_MALLOC_STATS 1
#define malloc_getpagesize 16384
#define LACKS_UNISTD_H 1
#define LACKS_FCNTL_H 1
#define LACKS_SYS_PARAM_H 1
#define LACKS_SYS_MMAN_H 1
#define LACKS_SYS_PARAM_H 1
#define LACKS_SYS_TYPES_H 1
#define LACKS_STRINGS_H 1
#define LACKS_STRING_H 1
#define LACKS_STDLIB_H 1
#define LACKS_SCHED_H 1
#define LACKS_TIME_H 1
#define LACKS_UNISTD_H 1
#define MALLOC_FAILURE_ACTION panic("dlmalloc: out of memory\n");
static void *sbrk(intptr_t inc)

View file

@ -188,7 +188,6 @@ void exc_sync(u64 *regs)
default:
printf("Unknown HVC: 0x%x\n", imm);
break;
}
} else {
if (!(exc_guard & GUARD_SILENT))

View file

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: MIT */
#include <assert.h>
#include "assert.h"
#include "heapblock.h"
#include "types.h"
#include "utils.h"

View file

@ -1,8 +1,7 @@
/* SPDX-License-Identifier: MIT */
#include <assert.h>
#include "adt.h"
#include "assert.h"
#include "exception.h"
#include "kboot.h"
#include "malloc.h"

View file

@ -10,7 +10,6 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

View file

@ -1,8 +1,7 @@
/* SPDX-License-Identifier: MIT */
#include <assert.h>
#include "payload.h"
#include "assert.h"
#include "heapblock.h"
#include "kboot.h"
#include "smp.h"

View file

@ -147,6 +147,17 @@ size_t strlen(const char *s)
return rc;
}
size_t strnlen(const char *s, size_t n)
{
size_t rc = 0;
while (rc < n && s[rc]) {
++rc;
}
return rc;
}
char *strchr(const char *s, int c)
{
do {

View file

@ -3,6 +3,7 @@
#ifndef TYPES_H
#define TYPES_H
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
@ -31,15 +32,6 @@ typedef s64 ptrdiff_t;
type *name = \
(type *)(((u32)(_al__##name)) + ((alignment) - (((u32)(_al__##name)) & ((alignment)-1))))
#define INT_MAX ((int)0x7fffffff)
#define UINT_MAX ((unsigned int)0xffffffff)
#define LONG_MAX ((long)0x7fffffffffffffffl)
#define ULONG_MAX ((unsigned long)0xfffffffffffffffful)
#define LLONG_MAX LONG_MAX
#define ULLONG_MAX ULLONG_MAX
#define HAVE_PTRDIFF_T 1
#define HAVE_UINTPTR_T 1
#define UPTRDIFF_T uintptr_t

15
sysinc/assert.h Normal file
View file

@ -0,0 +1,15 @@
/* SPDX-License-Identifier: MIT */
#ifndef ASSERT_H
#define ASSERT_H
void __assert_fail(const char *assertion, const char *file, unsigned int line,
const char *function);
#define assert(expression) \
((expression) ? (void)0 : __assert_fail(#expression, __FILE__, __LINE__, __func__))
/* Requires C11 */
#define static_assert _Static_assert
#endif

9
sysinc/errno.h Normal file
View file

@ -0,0 +1,9 @@
/* SPDX-License-Identifier: MIT */
#ifndef ERRNO_H
#define ERRNO_H
#define ENOMEM 12
#define EINVAL 22
#endif

15
sysinc/limits.h Normal file
View file

@ -0,0 +1,15 @@
/* SPDX-License-Identifier: MIT */
#ifndef LIMITS_H
#define LIMITS_H
#define INT_MAX (0x7fffffff)
#define UINT_MAX (0xffffffffu)
#define LONG_MAX (0x7fffffffffffffffl)
#define ULONG_MAX (0xfffffffffffffffful)
#define LLONG_MAX LONG_MAX
#define ULLONG_MAX ULLONG_MAX
#endif

View file

@ -15,6 +15,7 @@ char *strncpy(char *s1, const char *s2, size_t n);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
size_t strlen(const char *s);
size_t strnlen(const char *s, size_t n);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);