Assembled libc
This commit is contained in:
parent
83ae049f71
commit
0b6674afdb
|
@ -1,7 +1,93 @@
|
|||
install-headers:
|
||||
DEFAULT_HOST!=../default-host.sh
|
||||
HOST?=DEFAULT_HOST
|
||||
HOSTARCH!=../target-triplet-to-arch.sh $(HOST)
|
||||
|
||||
install:
|
||||
CFLAGS?=-O2 -g
|
||||
CPPFLAGS?=
|
||||
LDFLAGS?=
|
||||
LIBS?=
|
||||
|
||||
DESTDIR?=
|
||||
PREFIX?=/usr/local
|
||||
EXEC_PREFIX?=$(PREFIX)
|
||||
INCLUDEDIR?=$(PREFIX)/include
|
||||
LIBDIR?=$(EXEC_PREFIX)/lib
|
||||
|
||||
CFLAGS:=$(CFLAGS) -ffreestanding -Wall -Wextra
|
||||
CPPFLAGS:=$(CPPFLAGS) -D__is_libc -Iinclude
|
||||
LIBK_CFLAGS:=$(CFLAGS)
|
||||
LIBK_CPPFLAGS:=$(CPPFLAGS) -D__is_libk
|
||||
|
||||
ARCHDIR=arch/$(HOSTARCH)
|
||||
|
||||
include $(ARCHDIR)/make.config
|
||||
|
||||
CFLAGS:=$(CFLAGS) $(ARCH_CFLAGS)
|
||||
CPPFLAGS:=$(CPPFLAGS) $(ARCH_CPPFLAGS)
|
||||
LIBK_CFLAGS:=$(LIBK_CFLAGS) $(KERNEL_ARCH_CFLAGS)
|
||||
LIBK_CPPFLAGS:=$(LIBK_CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS)
|
||||
|
||||
FREEOBJS=\
|
||||
$(ARCH_FREEOBJS) \
|
||||
stdio/printf.o \
|
||||
stdio/putchar.o \
|
||||
stdio/puts.o \
|
||||
stdlib/abort.o \
|
||||
string/memcmp.o \
|
||||
string/memcpy.o \
|
||||
string/memmove.o \
|
||||
string/memset.o \
|
||||
string/strlen.o \
|
||||
|
||||
HOSTEDOBJS=\
|
||||
$(ARCH_HOSTEDOBJS) \
|
||||
|
||||
OBJS=\
|
||||
$(FREEOBJS) \
|
||||
$(HOSTEDOBJS) \
|
||||
|
||||
LIBK_OBJS=$(FREEOBJS:.o=.libk.o)
|
||||
|
||||
#BINARIES=libc.a libk.a # Not ready for libc yet.
|
||||
BINARIES=libk.a
|
||||
|
||||
.PHONY: all clean install install-headers install-libs
|
||||
.SUFFIXES: .o .libk.o .c .S
|
||||
|
||||
all: $(BINARIES)
|
||||
|
||||
libc.a: $(OBJS)
|
||||
$(AR) rcs $@ $(OBJS)
|
||||
|
||||
libk.a: $(LIBK_OBJS)
|
||||
$(AR) rcs $@ $(LIBK_OBJS)
|
||||
|
||||
.c.o:
|
||||
$(CC) -MD -c $< -o $@ -std=gnu11 $(CFLAGS) $(CPPFLAGS)
|
||||
|
||||
.c.S:
|
||||
$(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
||||
|
||||
.c.libk.o:
|
||||
$(CC) -MD -c $< -o $@ -std=gnu11 $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
||||
|
||||
.S.libk.o:
|
||||
$(CC) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(BINARIES) *.a
|
||||
rm -f $(OBJS) $(LIBK_OBJS) *.o */*.o */*/*.o
|
||||
rm -f $(OBJS:.o=.d) $(LIBK_OBJS:.o=.d) *.d */*.d */*/*.d
|
||||
|
||||
install: install-headers install-libs
|
||||
|
||||
install-headers:
|
||||
mkdir -p $(DESTDIR)$(INCLUDEDIR)
|
||||
cp -R --preserve=timestamps include/. $(DESTDIR)$(INCLUDEDIR)/.
|
||||
|
||||
install-libs: $(BINARIES)
|
||||
mkdir -p $(DESTDIR)$(LIBDIR)
|
||||
cp $(BINARIES) $(DESTDIR)$(LIBDIR)
|
||||
|
||||
-include $(OBJS:.o=.d)
|
||||
-include $(LIBK_OBJS:.o=.d)
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
ARCH_CFLAGS=
|
||||
ARCH_CPPFLAGS=
|
||||
KERNEL_ARCH_CFLAGS=
|
||||
KERNEL_ARCH_CPPFLAGS=
|
||||
|
||||
ARCH_FREEOBJS=\
|
||||
|
||||
ARCH_HOSTEDOBJS=\
|
|
@ -0,0 +1,20 @@
|
|||
#ifndef _STDIO_H
|
||||
#define _STDIO_H 1
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
#define EOF (-1)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int printf(const char* __restrict, ...);
|
||||
int putchar(int);
|
||||
int puts(const char*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef _STDLIB_H
|
||||
#define _STDLIB_H 1
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
__attribute__((__noreturn__))
|
||||
void abort(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef _STRING_H
|
||||
#define _STRING_H 1
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int memcmp(const void*, const void*, size_t);
|
||||
void* memcpy(void* __restrict, const void* __restrict, size_t);
|
||||
void* memmove(void*, const void*, size_t);
|
||||
void* memset(void*, int, size_t);
|
||||
size_t strlen(const char*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef _SYS_CDEFS_H
|
||||
#define _SYS_CDEFS_H 1
|
||||
|
||||
#define __myos_libc 1
|
||||
|
||||
#endif
|
|
@ -0,0 +1,110 @@
|
|||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static bool print(const char* data, size_t length) {
|
||||
|
||||
const unsigned char* bytes = (const unsigned char*) data;
|
||||
|
||||
for (size_t i = 0; i < length; i++)
|
||||
if (putchar(bytes[i]) == EOF)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
int printf(const char* restrict format, ...) {
|
||||
|
||||
va_list parameters;
|
||||
va_start(parameters, format);
|
||||
|
||||
int written = 0;
|
||||
|
||||
while (*format != '\0') {
|
||||
|
||||
size_t maxrem = INT_MAX - written;
|
||||
|
||||
if (format[0] != '%' || format[1] == '%') {
|
||||
|
||||
if (format[0] == '%')
|
||||
format++;
|
||||
|
||||
size_t amount = 1;
|
||||
|
||||
while (format[amount] && format[amount] != '%')
|
||||
amount++;
|
||||
|
||||
if (maxrem < amount) {
|
||||
// TODO: Set errno to EOVERFLOW.
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!print(format, amount))
|
||||
return -1;
|
||||
|
||||
format += amount;
|
||||
written += amount;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
const char* format_begun_at = format++;
|
||||
|
||||
if (*format == 'c') {
|
||||
|
||||
format++;
|
||||
char c = (char) va_arg(parameters, int /* char promotes to int */);
|
||||
|
||||
if (!maxrem) {
|
||||
// TODO: Set errno to EOVERFLOW.
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!print(&c, sizeof(c)))
|
||||
return -1;
|
||||
|
||||
written++;
|
||||
|
||||
} else if (*format == 's') {
|
||||
|
||||
format++;
|
||||
const char* str = va_arg(parameters, const char*);
|
||||
size_t len = strlen(str);
|
||||
|
||||
if (maxrem < len) {
|
||||
// TODO: Set errno to EOVERFLOW.
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!print(str, len))
|
||||
return -1;
|
||||
|
||||
written += len;
|
||||
|
||||
} else {
|
||||
|
||||
format = format_begun_at;
|
||||
size_t len = strlen(format);
|
||||
|
||||
if (maxrem < len) {
|
||||
// TODO: Set errno to EOVERFLOW.
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!print(format, len))
|
||||
return -1;
|
||||
|
||||
written += len;
|
||||
format += len;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
va_end(parameters);
|
||||
return written;
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#if defined(__is_libk)
|
||||
#include <kernel/screen.h>
|
||||
#endif
|
||||
|
||||
int putchar(int ic) {
|
||||
|
||||
#if defined(__is_libk)
|
||||
|
||||
char c = (char) ic;
|
||||
screen_write(&c, sizeof(c));
|
||||
|
||||
#else
|
||||
|
||||
// TODO: Implement stdio and the write system call.
|
||||
|
||||
#endif
|
||||
|
||||
return ic;
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int puts(const char* string) {
|
||||
return printf("%s\n", string);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
__attribute__((__noreturn__))
|
||||
void abort(void) {
|
||||
|
||||
#if defined(__is_libk)
|
||||
|
||||
// TODO: Add proper kernel panic.
|
||||
printf("kernel: panic: abort()\n");
|
||||
|
||||
#else
|
||||
|
||||
// TODO: Abnormally terminate the process as if by SIGABRT.
|
||||
printf("abort()\n");
|
||||
|
||||
#endif
|
||||
|
||||
while (1) { }
|
||||
__builtin_unreachable();
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#include <string.h>
|
||||
|
||||
int memcmp(const void* aptr, const void* bptr, size_t size) {
|
||||
|
||||
const unsigned char* a = (const unsigned char*) aptr;
|
||||
const unsigned char* b = (const unsigned char*) bptr;
|
||||
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
|
||||
if (a[i] < b[i])
|
||||
return -1;
|
||||
else if (b[i] < a[i])
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
#include <string.h>
|
||||
|
||||
void* memcpy(void* restrict dstptr, const void* restrict srcptr, size_t size) {
|
||||
|
||||
unsigned char* dst = (unsigned char*) dstptr;
|
||||
const unsigned char* src = (const unsigned char*) srcptr;
|
||||
|
||||
for (size_t i = 0; i < size; i++)
|
||||
dst[i] = src[i];
|
||||
|
||||
return dstptr;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#include <string.h>
|
||||
|
||||
void* memmove(void* dstptr, const void* srcptr, size_t size) {
|
||||
|
||||
unsigned char* dst = (unsigned char*) dstptr;
|
||||
const unsigned char* src = (const unsigned char*) srcptr;
|
||||
|
||||
if (dst < src) {
|
||||
|
||||
for (size_t i = 0; i < size; i++)
|
||||
dst[i] = src[i];
|
||||
|
||||
} else {
|
||||
|
||||
for (size_t i = size; i != 0; i--)
|
||||
dst[i-1] = src[i-1];
|
||||
|
||||
}
|
||||
|
||||
return dstptr;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
#include <string.h>
|
||||
|
||||
void* memset(void* bufptr, int value, size_t size) {
|
||||
|
||||
unsigned char* buf = (unsigned char*) bufptr;
|
||||
|
||||
for (size_t i = 0; i < size; i++)
|
||||
buf[i] = (unsigned char) value;
|
||||
|
||||
return bufptr;
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
#include <string.h>
|
||||
|
||||
size_t strlen(const char* str) {
|
||||
|
||||
size_t len = 0;
|
||||
|
||||
while (str[len])
|
||||
len++;
|
||||
|
||||
return len;
|
||||
|
||||
}
|
Loading…
Reference in New Issue