summaryrefslogtreecommitdiff
path: root/LibC
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2018-10-28 09:36:21 +0100
committerAndreas Kling <awesomekling@gmail.com>2018-10-28 09:36:21 +0100
commite904f193c1d49ccb18c75e6e1b1ef0a8e2dc1db7 (patch)
treef4af2e35301c888bbaa227f35180fa54c2a3325e /LibC
parent88ad59bfb18feead399c5250b426fc3dc3512b95 (diff)
downloadserenity-e904f193c1d49ccb18c75e6e1b1ef0a8e2dc1db7.zip
Canonicalize the path used by sh.
With a bunch of LibC work to support the feature. LibC now initializes AK::StringImpl by default. It's now fine to use AK in LibC/Userland! :^)
Diffstat (limited to 'LibC')
-rw-r--r--LibC/Makefile14
-rw-r--r--LibC/assert.cpp14
-rw-r--r--LibC/assert.h14
-rw-r--r--LibC/entry.cpp3
-rw-r--r--LibC/stdlib.cpp25
-rw-r--r--LibC/stdlib.h5
-rw-r--r--LibC/string.cpp11
-rw-r--r--LibC/string.h1
8 files changed, 85 insertions, 2 deletions
diff --git a/LibC/Makefile b/LibC/Makefile
index d0ab3ef533..c376122965 100644
--- a/LibC/Makefile
+++ b/LibC/Makefile
@@ -1,4 +1,11 @@
-OBJS = \
+AK_OBJS = \
+ ../AK/StringImpl.o \
+ ../AK/String.o \
+ ../AK/StringBuilder.o \
+ ../AK/FileSystemPath.o \
+ ../AK/kmalloc.o
+
+LIBC_OBJS = \
stdio.o \
unistd.o \
string.o \
@@ -8,8 +15,11 @@ OBJS = \
stdlib.o \
time.o \
utsname.o \
+ assert.o \
entry.o
+OBJS = $(AK_OBJS) $(LIBC_OBJS)
+
LIBRARY = LibC.a
ARCH_FLAGS =
STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib
@@ -19,7 +29,7 @@ FLAVOR_FLAGS = -fomit-frame-pointer -mregparm=3 -march=i386 -m32 -fno-exceptions
OPTIMIZATION_FLAGS = -Os -fno-asynchronous-unwind-tables
INCLUDE_FLAGS = -I.. -I.
-DEFINES = -DSERENITY -DSANITIZE_PTRS
+DEFINES = -DSERENITY -DUSERLAND -DSANITIZE_PTRS
CXXFLAGS = $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(LIBC_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = g++-8
diff --git a/LibC/assert.cpp b/LibC/assert.cpp
new file mode 100644
index 0000000000..4aa774c3fd
--- /dev/null
+++ b/LibC/assert.cpp
@@ -0,0 +1,14 @@
+#include "assert.h"
+#include "stdlib.h"
+#include "stdio.h"
+
+extern "C" {
+
+extern void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func)
+{
+ printf("ASSERTION FAILED: %s\n%s:%u in %s\n", msg, file, line, func);
+ abort();
+}
+
+}
+
diff --git a/LibC/assert.h b/LibC/assert.h
new file mode 100644
index 0000000000..1f5610dd2e
--- /dev/null
+++ b/LibC/assert.h
@@ -0,0 +1,14 @@
+#pragma once
+
+extern "C" {
+
+void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func);
+
+#define assert(expr) (static_cast<bool>(expr) ? (void)0 : __assertion_failed(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__))
+#define CRASH() do { asm volatile("ud2"); } while(0)
+#define ASSERT assert
+#define RELEASE_ASSERT assert
+#define ASSERT_NOT_REACHED() assert(false)
+
+}
+
diff --git a/LibC/entry.cpp b/LibC/entry.cpp
index 5d5eb27078..c3657b704b 100644
--- a/LibC/entry.cpp
+++ b/LibC/entry.cpp
@@ -1,4 +1,5 @@
#include <Kernel/Syscall.h>
+#include <AK/StringImpl.h>
extern "C" int main(int, char**);
@@ -8,6 +9,8 @@ extern "C" int _start()
{
errno = 0;
+ StringImpl::initializeGlobals();
+
int argc;
char** argv;
int rc = Syscall::invoke(Syscall::GetArguments, (dword)&argc, (dword)&argv);
diff --git a/LibC/stdlib.cpp b/LibC/stdlib.cpp
index 7d0f1b5e8f..5feceaec5f 100644
--- a/LibC/stdlib.cpp
+++ b/LibC/stdlib.cpp
@@ -1,5 +1,7 @@
#include "stdlib.h"
#include "mman.h"
+#include <Kernel/Syscall.h>
+#include <AK/Assertions.h>
extern "C" {
@@ -20,5 +22,28 @@ void free(void* ptr)
munmap(ptr, 4096);
}
+void* calloc(size_t nmemb, size_t)
+{
+ ASSERT_NOT_REACHED();
+ return nullptr;
+}
+
+void* realloc(void *ptr, size_t)
+{
+ ASSERT_NOT_REACHED();
+ return nullptr;
+}
+
+void exit(int status)
+{
+ Syscall::invoke(Syscall::PosixExit, (dword)status);
+}
+
+void abort()
+{
+ // FIXME: Implement proper abort().
+ exit(253);
+}
+
}
diff --git a/LibC/stdlib.h b/LibC/stdlib.h
index 55cd47c9f7..4ebff011da 100644
--- a/LibC/stdlib.h
+++ b/LibC/stdlib.h
@@ -6,6 +6,11 @@ extern "C" {
void* malloc(size_t);
void free(void*);
+void* calloc(size_t nmemb, size_t);
+void* realloc(void *ptr, size_t);
+
+void exit(int status);
+void abort();
}
diff --git a/LibC/string.cpp b/LibC/string.cpp
index a72bbb7cda..a48e1c2a4f 100644
--- a/LibC/string.cpp
+++ b/LibC/string.cpp
@@ -21,6 +21,17 @@ int strcmp(const char* s1, const char* s2)
return *(const unsigned char*)s1 < *(const unsigned char*)s2 ? -1 : 1;
}
+int memcmp(const void* v1, const void* v2, size_t n)
+{
+ auto* s1 = (const byte*)v1;
+ auto* s2 = (const byte*)v2;
+ while (n-- > 0) {
+ if (*s1++ != *s2++)
+ return s1[-1] < s2[-1] ? -1 : 1;
+ }
+ return 0;
+}
+
void memcpy(void* dest, const void* src, size_t n)
{
auto* bdest = (unsigned char*)dest;
diff --git a/LibC/string.h b/LibC/string.h
index 32444fd90d..6ef56e075b 100644
--- a/LibC/string.h
+++ b/LibC/string.h
@@ -6,6 +6,7 @@ extern "C" {
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);