Просмотр исходного кода

LibC: Avoid ninja-imports of system functions

This adds a new header <sys/internals.h>, 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.
Ben Wiederhake 5 лет назад
Родитель
Сommit
9d2d97a059

+ 3 - 5
Demos/DynamicLink/LinkLib/DynamicLib.cpp

@@ -27,19 +27,17 @@
 #include <AK/String.h>
 #include <assert.h>
 #include <stdio.h>
+#include <sys/internals.h>
 
 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();

+ 2 - 5
Libraries/LibC/crt0.cpp

@@ -28,16 +28,13 @@
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/internals.h>
+#include <unistd.h>
 
 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);
 

+ 3 - 4
Libraries/LibC/libcinit.cpp

@@ -26,6 +26,8 @@
 
 #include <AK/Types.h>
 #include <assert.h>
+#include <sys/internals.h>
+#include <unistd.h>
 
 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();
 }
-
 }

+ 1 - 0
Libraries/LibC/malloc.cpp

@@ -35,6 +35,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/internals.h>
 #include <sys/mman.h>
 
 // FIXME: Thread safety.

+ 1 - 0
Libraries/LibC/stdio.cpp

@@ -38,6 +38,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/internals.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>

+ 39 - 0
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 <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+extern void __libc_init();
+extern void __malloc_init();
+extern void __stdio_init();
+extern void _init();
+extern bool __environ_is_malloced;
+
+__END_DECLS