From 9d2d97a05922599e714f2243b3d12248a386ac6a Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Wed, 12 Aug 2020 01:58:50 +0200 Subject: [PATCH] LibC: Avoid ninja-imports of system functions This adds a new header , which provides access to LibC internals. This is in the interest of type-checking LibC itself, as well as enabling less-hacky access for uses like LinkDemo. And, of course, this progresses LibC towards building cleanly with -Wmissing-declarations. --- Demos/DynamicLink/LinkLib/DynamicLib.cpp | 8 ++--- Libraries/LibC/crt0.cpp | 7 ++--- Libraries/LibC/libcinit.cpp | 7 ++--- Libraries/LibC/malloc.cpp | 1 + Libraries/LibC/stdio.cpp | 1 + Libraries/LibC/sys/internals.h | 39 ++++++++++++++++++++++++ 6 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 Libraries/LibC/sys/internals.h diff --git a/Demos/DynamicLink/LinkLib/DynamicLib.cpp b/Demos/DynamicLink/LinkLib/DynamicLib.cpp index 547a206468c..8e1a36dcb02 100644 --- a/Demos/DynamicLink/LinkLib/DynamicLib.cpp +++ b/Demos/DynamicLink/LinkLib/DynamicLib.cpp @@ -27,19 +27,17 @@ #include #include #include +#include char* __static_environ[] = { nullptr }; // We don't get the environment without some libc workarounds.. -// FIXME: Because we need to call printf, and we don't have access to the stout file descriptor -// from the main executable. We need to call __libc_init.... -extern "C" void __libc_init(); -extern "C" bool __environ_is_malloced; - class Global { public: Global(int i) : m_i(i) { + // FIXME: Because we need to call printf, and we don't have access to the stdout + // file descriptor from the main executable, we need to initialize LibC ourself. __environ_is_malloced = false; environ = __static_environ; __libc_init(); diff --git a/Libraries/LibC/crt0.cpp b/Libraries/LibC/crt0.cpp index 60a96cb0668..513df9c49fd 100644 --- a/Libraries/LibC/crt0.cpp +++ b/Libraries/LibC/crt0.cpp @@ -28,16 +28,13 @@ #include #include #include +#include +#include extern "C" { int main(int, char**, char**); -extern void __libc_init(); -extern void _init(); -extern char** environ; -extern bool __environ_is_malloced; - // Tell the compiler that this may be called from somewhere else. int _start(int argc, char** argv, char** env); diff --git a/Libraries/LibC/libcinit.cpp b/Libraries/LibC/libcinit.cpp index 8aba7a62b51..8f97a51737f 100644 --- a/Libraries/LibC/libcinit.cpp +++ b/Libraries/LibC/libcinit.cpp @@ -26,6 +26,8 @@ #include #include +#include +#include extern "C" { @@ -35,10 +37,7 @@ bool __environ_is_malloced; void __libc_init() { - void __malloc_init(); __malloc_init(); - - void __stdio_init(); __stdio_init(); } @@ -52,9 +51,9 @@ void __libc_init() extern u32 __stack_chk_guard; u32 __stack_chk_guard = (u32)0xc6c7c8c9; +[[noreturn]] void __stack_chk_fail(); [[noreturn]] void __stack_chk_fail() { ASSERT_NOT_REACHED(); } - } diff --git a/Libraries/LibC/malloc.cpp b/Libraries/LibC/malloc.cpp index 6971ead3ff9..17ddbbb1b4d 100644 --- a/Libraries/LibC/malloc.cpp +++ b/Libraries/LibC/malloc.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include // FIXME: Thread safety. diff --git a/Libraries/LibC/stdio.cpp b/Libraries/LibC/stdio.cpp index f0922acde42..53ee187fa6d 100644 --- a/Libraries/LibC/stdio.cpp +++ b/Libraries/LibC/stdio.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include diff --git a/Libraries/LibC/sys/internals.h b/Libraries/LibC/sys/internals.h new file mode 100644 index 00000000000..d86f616b5c5 --- /dev/null +++ b/Libraries/LibC/sys/internals.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include + +__BEGIN_DECLS + +extern void __libc_init(); +extern void __malloc_init(); +extern void __stdio_init(); +extern void _init(); +extern bool __environ_is_malloced; + +__END_DECLS