From ebf308d413640c2bc405ac7bf5930f2aa4e7e1d8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 9 Nov 2018 12:20:44 +0100 Subject: [PATCH] Make kernel build with clang. It's a bit faster than g++ and seems to generate perfectly fine code. The kernel is also roughly 10% smaller(!) --- AK/StdLib.h | 24 ++++++++++++------------ AK/StringImpl.cpp | 10 +++++++--- AK/StringImpl.h | 1 - ELFLoader/ELFLoader.cpp | 2 +- Kernel/Makefile | 8 ++++---- Kernel/MemoryManager.h | 6 ++++-- Kernel/StdLib.cpp | 8 ++++++-- Kernel/StdLib.h | 4 ++++ Kernel/Syscall.cpp | 2 +- VirtualFileSystem/Ext2FileSystem.cpp | 2 +- 10 files changed, 40 insertions(+), 27 deletions(-) diff --git a/AK/StdLib.h b/AK/StdLib.h index 6d1d398dc2e..64892b041dd 100644 --- a/AK/StdLib.h +++ b/AK/StdLib.h @@ -114,31 +114,31 @@ struct IsPointer : __IsPointerHelper::Type> { }; template struct IsFunction : FalseType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsFunction : TrueType { }; -template struct IsFunction : TrueType { }; +template struct IsFunction : TrueType { }; template struct IsRvalueReference : FalseType { }; template struct IsRvalueReference : TrueType { }; diff --git a/AK/StringImpl.cpp b/AK/StringImpl.cpp index 6a3b9767a5e..8fbf11f6928 100644 --- a/AK/StringImpl.cpp +++ b/AK/StringImpl.cpp @@ -29,9 +29,7 @@ static inline size_t allocationSizeForStringImpl(size_t length) RetainPtr StringImpl::createUninitialized(size_t length, char*& buffer) { - if (!length) - return theEmptyStringImpl(); - + ASSERT(length); void* slot = kmalloc(allocationSizeForStringImpl(length)); if (!slot) return nullptr; @@ -52,6 +50,8 @@ RetainPtr StringImpl::create(const char* cstring, size_t length, Sho char* buffer; auto newStringImpl = createUninitialized(length, buffer); + if (!newStringImpl) + return nullptr; memcpy(buffer, cstring, length * sizeof(char)); if (shouldChomp && buffer[length - 1] == '\n') { @@ -108,6 +108,8 @@ RetainPtr StringImpl::toLowercase() const slowPath: char* buffer; auto lowercased = createUninitialized(m_length, buffer); + if (!lowercased) + return nullptr; for (size_t i = 0; i < m_length; ++i) buffer[i] = toASCIILowercase(m_characters[i]); @@ -128,6 +130,8 @@ RetainPtr StringImpl::toUppercase() const slowPath: char* buffer; auto uppercased = createUninitialized(m_length, buffer); + if (!uppercased) + return nullptr; for (size_t i = 0; i < m_length; ++i) buffer[i] = toASCIIUppercase(m_characters[i]); diff --git a/AK/StringImpl.h b/AK/StringImpl.h index c5402a21211..6d3419e8af8 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -42,7 +42,6 @@ private: void computeHash() const; size_t m_length { 0 }; - bool m_ownsBuffer { true }; mutable bool m_hasHash { false }; const char* m_characters { nullptr }; mutable unsigned m_hash { 0 }; diff --git a/ELFLoader/ELFLoader.cpp b/ELFLoader/ELFLoader.cpp index 58826efafdf..b7b206a0668 100644 --- a/ELFLoader/ELFLoader.cpp +++ b/ELFLoader/ELFLoader.cpp @@ -49,7 +49,7 @@ bool ELFLoader::layout() } }); - m_image.for_each_section_of_type(SHT_PROGBITS, [this, &failed] (const ELFImage::Section& section) { + m_image.for_each_section_of_type(SHT_PROGBITS, [this] (const ELFImage::Section& section) { #ifdef ELFLOADER_DEBUG kprintf("ELFLoader: Copying progbits section: %s\n", section.name()); #endif diff --git a/Kernel/Makefile b/Kernel/Makefile index 71eae660db5..8f99da09c82 100644 --- a/Kernel/Makefile +++ b/Kernel/Makefile @@ -55,19 +55,19 @@ BOOTLOADER = Boot/boot.bin IMAGE = .floppy-image ARCH_FLAGS = STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib #-nostdinc -KERNEL_FLAGS = -ffreestanding -fno-stack-protector -fno-ident +KERNEL_FLAGS = -ffreestanding -fno-stack-protector -fno-ident -fno-builtin WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings FLAVOR_FLAGS = -mregparm=3 -march=i386 -m32 -fno-exceptions -fno-rtti -fmerge-all-constants -fno-unroll-loops -fno-pie -fno-pic -OPTIMIZATION_FLAGS = -Os -fno-asynchronous-unwind-tables +OPTIMIZATION_FLAGS = -Oz -fno-asynchronous-unwind-tables INCLUDE_FLAGS = -I.. -I. -SUGGEST_FLAGS = -Wsuggest-final-types -Wsuggest-final-methods -Wsuggest-override #-Wsuggest-attribute=noreturn +#SUGGEST_FLAGS = -Wsuggest-final-types -Wsuggest-final-methods -Wsuggest-override #-Wsuggest-attribute=noreturn DEFINES = -DSERENITY -DKERNEL -DSANITIZE_PTRS CXXFLAGS = $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(KERNEL_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(SUGGEST_FLAGS) $(INCLUDE_FLAGS) $(DEFINES) #CXX = /usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-g++ #LD = /usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-ld -CXX = g++-8 +CXX = clang LD = ld LDFLAGS = -T linker.ld --strip-debug -melf_i386 --gc-sections --build-id=none -z norelro -z now diff --git a/Kernel/MemoryManager.h b/Kernel/MemoryManager.h index 6ec6c5f95c9..0cc028d8faf 100644 --- a/Kernel/MemoryManager.h +++ b/Kernel/MemoryManager.h @@ -52,7 +52,8 @@ private: PhysicalAddress m_paddr; }; -struct PageDirectory { +class PageDirectory { +public: dword entries[1024]; RetainPtr physical_pages[1024]; @@ -91,7 +92,8 @@ private: Vector> m_physical_pages; }; -struct Region : public Retainable { +class Region : public Retainable { +public: Region(LinearAddress, size_t, String&&, bool r, bool w, bool cow = false); Region(LinearAddress, size_t, RetainPtr&&, size_t offset_in_vmo, String&&, bool r, bool w, bool cow = false); Region(LinearAddress, size_t, RetainPtr&&, String&&, bool r, bool w); diff --git a/Kernel/StdLib.cpp b/Kernel/StdLib.cpp index be71de1f1d6..eb55e695bc8 100644 --- a/Kernel/StdLib.cpp +++ b/Kernel/StdLib.cpp @@ -3,6 +3,8 @@ #include "kmalloc.h" #include +extern "C" { + void memcpy(void *dest, const void *src, DWORD n) { BYTE* bdest = (BYTE*)dest; @@ -71,8 +73,10 @@ int memcmp(const void* v1, const void* v2, size_t n) return 0; } -extern "C" void __cxa_pure_virtual() NORETURN; -extern "C" void __cxa_pure_virtual() +void __cxa_pure_virtual() NORETURN; +void __cxa_pure_virtual() { ASSERT_NOT_REACHED(); } + +} diff --git a/Kernel/StdLib.h b/Kernel/StdLib.h index 8b58218418a..32349a8d449 100644 --- a/Kernel/StdLib.h +++ b/Kernel/StdLib.h @@ -2,6 +2,8 @@ #include "types.h" +extern "C" { + void memcpy(void*, const void*, DWORD); void strcpy(char*, const char*); int strcmp(char const*, const char*); @@ -10,3 +12,5 @@ void *memset(void*, BYTE, DWORD); char *strdup(const char*); int memcmp(const void*, const void*, size_t); char* strrchr(const char* str, int ch); + +} diff --git a/Kernel/Syscall.cpp b/Kernel/Syscall.cpp index 639e3ad4076..8dea51dd7ba 100644 --- a/Kernel/Syscall.cpp +++ b/Kernel/Syscall.cpp @@ -8,7 +8,7 @@ extern "C" void syscall_entry(RegisterDump&); extern "C" void syscall_ISR(); extern volatile RegisterDump* syscallRegDump; -asm volatile( +asm( ".globl syscall_ISR \n" "syscall_ISR:\n" " pusha\n" diff --git a/VirtualFileSystem/Ext2FileSystem.cpp b/VirtualFileSystem/Ext2FileSystem.cpp index 55515d598c1..1a9dfc86228 100644 --- a/VirtualFileSystem/Ext2FileSystem.cpp +++ b/VirtualFileSystem/Ext2FileSystem.cpp @@ -824,7 +824,7 @@ bool Ext2FileSystem::setBlockAllocationState(GroupIndex group, BlockIndex bi, bo ASSERT(block); auto bitmap = Bitmap::wrap(block.pointer(), block.size()); bool currentState = bitmap.get(bitIndex); - kprintf("ext2fs: setBlockAllocationState(%u) %u -> %u\n", block, currentState, newState); + kprintf("ext2fs: setBlockAllocationState(%u) %u -> %u\n", bi, currentState, newState); if (currentState == newState) return true;