Skip to content

Commit

Permalink
A bunch of LibC boilerplate stuff added while trying to get figlet to…
Browse files Browse the repository at this point in the history
… build.
  • Loading branch information
awesomekling committed Oct 31, 2018
1 parent 511ed4c commit bb90c8e
Show file tree
Hide file tree
Showing 23 changed files with 117 additions and 45 deletions.
2 changes: 1 addition & 1 deletion LibC/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ OBJS = $(AK_OBJS) $(LIBC_OBJS)

LIBRARY = LibC.a
ARCH_FLAGS =
STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib
STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib -nostdinc
LIBC_FLAGS = -ffreestanding -fno-stack-protector -fno-ident
WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings
FLAVOR_FLAGS = -fomit-frame-pointer -mregparm=3 -march=i386 -m32 -fno-exceptions -fno-rtti -ffunction-sections -fdata-sections -fmerge-all-constants -fno-unroll-loops -falign-functions=1 -falign-jumps=1 -falign-loops=1 -fno-pie -fno-pic
Expand Down
6 changes: 4 additions & 2 deletions LibC/assert.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

extern "C" {
#include <sys/cdefs.h>

__BEGIN_DECLS

void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func);

Expand All @@ -10,5 +12,5 @@ void __assertion_failed(const char* msg, const char* file, unsigned line, const
#define RELEASE_ASSERT assert
#define ASSERT_NOT_REACHED() assert(false)

}
__END_DECLS

4 changes: 4 additions & 0 deletions LibC/ctype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#define isascii(c) (((c) & ~0x7f) == 0)

7 changes: 4 additions & 3 deletions LibC/dirent.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include "types.h"
#include <sys/cdefs.h>
#include <sys/types.h>

extern "C" {
__BEGIN_DECLS

struct dirent {
ino_t d_ino;
Expand All @@ -23,5 +24,5 @@ struct DIR {
DIR* opendir(const char* name);
dirent* readdir(DIR* dirp);

}
__END_DECLS

15 changes: 15 additions & 0 deletions LibC/entry.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
#include <stdio.h>
#include <Kernel/Syscall.h>
#include <AK/StringImpl.h>

extern "C" int main(int, char**);

FILE __default_streams[3];

int errno;
FILE* stdin;
FILE* stdout;
FILE* stderr;

extern "C" int _start()
{
errno = 0;

__default_streams[0].fd = 0;
stdin = &__default_streams[0];

__default_streams[1].fd = 1;
stdout = &__default_streams[1];

__default_streams[2].fd = 2;
stderr = &__default_streams[2];

StringImpl::initializeGlobals();

int argc;
Expand Down
5 changes: 3 additions & 2 deletions LibC/errno.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <sys/cdefs.h>
#include <Kernel/errno.h>

#define __RETURN_WITH_ERRNO(rc, good_ret, bad_ret) \
Expand All @@ -13,9 +14,9 @@
} \
} while(0)

extern "C" {
__BEGIN_DECLS

extern int errno;

};
__END_DECLS

Empty file added LibC/fcntl.h
Empty file.
8 changes: 5 additions & 3 deletions LibC/mman.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

#include "types.h"
#include <sys/cdefs.h>
#include <sys/types.h>

extern "C" {
__BEGIN_DECLS

void* mmap(void*, size_t);
int munmap(void*, size_t);
int set_mmap_name(void*, size_t, const char*);

}
__END_DECLS

6 changes: 4 additions & 2 deletions LibC/process.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

extern "C" {
#include <sys/cdefs.h>

__BEGIN_DECLS

int spawn(const char* path, const char** args);

}
__END_DECLS

6 changes: 3 additions & 3 deletions LibC/signal.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "types.h"
#include <sys/types.h>

extern "C" {
__BEGIN_DECLS

int kill(pid_t, int sig);

Expand All @@ -22,5 +22,5 @@ int kill(pid_t, int sig);
#define SIGALRM 14
#define SIGTERM 15

}
__END_DECLS

6 changes: 4 additions & 2 deletions LibC/stdarg.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

extern "C" {
#include <sys/cdefs.h>

__BEGIN_DECLS

typedef char* va_list;

#define va_start(ap, v) ap = (va_list)&v + sizeof(v)
#define va_arg(ap, t) ((t*)(ap += sizeof(t)))[-1]
#define va_end(ap) ap = nullptr

}
__END_DECLS

12 changes: 6 additions & 6 deletions LibC/stdio.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "stdio.h"
#include "stdarg.h"
#include "types.h"
#include "string.h"
#include "errno.h"
#include "unistd.h"
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <Kernel/Syscall.h>
#include <AK/printf.cpp>

Expand Down
21 changes: 19 additions & 2 deletions LibC/stdio.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
#pragma once

extern "C" {
#include <sys/cdefs.h>

__BEGIN_DECLS

#ifndef EOF
#define EOF (-1)
#endif

struct __STDIO_FILE {
int fd;
};

typedef struct __STDIO_FILE FILE;

extern FILE* stdin;
extern FILE* stdout;
extern FILE* stderr;

int fprintf(FILE*, const char* fmt, ...);
int printf(const char* fmt, ...);
int sprintf(char* buffer, const char* fmt, ...);
int putchar(int ch);
void perror(const char*);

}
__END_DECLS

7 changes: 4 additions & 3 deletions LibC/stdlib.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include "types.h"
#include <sys/cdefs.h>
#include <sys/types.h>

extern "C" {
__BEGIN_DECLS

void* malloc(size_t);
void free(void*);
Expand All @@ -12,5 +13,5 @@ void* realloc(void *ptr, size_t);
void exit(int status);
void abort();

}
__END_DECLS

7 changes: 4 additions & 3 deletions LibC/string.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#pragma once

#include "types.h"
#include <sys/cdefs.h>
#include <sys/types.h>

extern "C" {
__BEGIN_DECLS

size_t strlen(const char*);
int strcmp(const char*, const char*);
int memcmp(const void*, const void*, size_t);
void memcpy(void*, const void*, size_t);
const char* strerror(int errnum);

}
__END_DECLS

10 changes: 10 additions & 0 deletions LibC/sys/cdefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#ifdef __cplusplus
#define __BEGIN_DECLS extern "C" {
#define __END_DECLS }
#else
#define __BEGIN_DECLS
#define __END_DECLS
#endif

Empty file added LibC/sys/ioctl.h
Empty file.
Empty file added LibC/sys/stat.h
Empty file.
12 changes: 10 additions & 2 deletions LibC/types.h → LibC/sys/types.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

extern "C" {
#include <sys/cdefs.h>

__BEGIN_DECLS

typedef unsigned int dword;
typedef unsigned short word;
Expand Down Expand Up @@ -49,5 +51,11 @@ struct stat {
time_t st_ctime; /* time of last status change */
};

}
#ifdef __cplusplus
#define NULL nullptr
#else
#define NULL 0
#endif

__END_DECLS

7 changes: 4 additions & 3 deletions LibC/time.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once

#include "types.h"
#include <sys/cdefs.h>
#include <sys/types.h>

extern "C" {
__BEGIN_DECLS

int gettimeofday(timeval*);
time_t time(time_t*);

}
__END_DECLS

10 changes: 6 additions & 4 deletions LibC/unistd.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include "types.h"
#include <sys/cdefs.h>
#include <sys/types.h>

extern "C" {
__BEGIN_DECLS

uid_t getuid();
gid_t getgid();
Expand All @@ -14,7 +15,7 @@ int close(int fd);
pid_t waitpid(pid_t, int* wstatus, int options);
int chdir(const char* path);
char* getcwd(char* buffer, size_t size);
int lstat(const char* path, stat* statbuf);
int lstat(const char* path, struct stat* statbuf);
int sleep(unsigned seconds);
int gethostname(char*, size_t);
ssize_t readlink(const char* path, char* buffer, size_t);
Expand Down Expand Up @@ -62,4 +63,5 @@ int ttyname_r(int fd, char* buffer, size_t);
#define O_DIRECTORY 00200000
#define O_ 00400000

}
__END_DECLS

7 changes: 5 additions & 2 deletions LibC/utsname.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <sys/cdefs.h>

#define UTSNAME_ENTRY_LEN 65

extern "C" {
__BEGIN_DECLS

struct utsname {
char sysname[UTSNAME_ENTRY_LEN];
Expand All @@ -14,4 +16,5 @@ struct utsname {

int uname(struct utsname*);

}
__END_DECLS

4 changes: 2 additions & 2 deletions Userland/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ APPS = \
kill

ARCH_FLAGS =
STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib
STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib -nostdinc
USERLAND_FLAGS = -ffreestanding -fno-stack-protector -fno-ident
WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings
FLAVOR_FLAGS = -march=i386 -mregparm=3 -m32 -fno-exceptions -fno-rtti -fmerge-all-constants -fno-unroll-loops -fno-pie -fno-pic
OPTIMIZATION_FLAGS = -Os -fno-asynchronous-unwind-tables
INCLUDE_FLAGS = -I.. -I.
INCLUDE_FLAGS = -I.. -I. -I../LibC

DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND

Expand Down

0 comments on commit bb90c8e

Please sign in to comment.