summaryrefslogtreecommitdiff
path: root/Libraries/LibC/cxxabi.cpp
diff options
context:
space:
mode:
authorAndrew Kaster <andrewdkaster@gmail.com>2020-05-15 21:38:03 -0600
committerAndreas Kling <kling@serenityos.org>2020-05-20 08:37:50 +0200
commitcdbbe14062ea49f9a9d9b0e5627aba9efd07659a (patch)
treecc65775010ad3b4fdefdbd44ac88ee7e922a6afb /Libraries/LibC/cxxabi.cpp
parent4361a502255e409f04c9325ef73f3cd10f9cafdb (diff)
downloadserenity-cdbbe14062ea49f9a9d9b0e5627aba9efd07659a.zip
LibC: Implement Itanium C++ ABI for static variable guards
This is __cxa_guard_acquire, __cxa_guard_release, and __cxa_guard_abort. We put these symbols in a 'fake' libstdc++ to trick gcc into thinking it has libstdc++. These symbols are necessary for C++ programs and not C programs, so, seems file. There's no way to tell gcc that, for example, the standard lib it should use is libc++ or libc. So, this is what we have for now. When threaded code enters a block that is trying to call the constructor for a block-scope static, the compiler will emit calls to these methods to handle the "call_once" nature of block-scope statics. The compiler creates a 64-bit guard variable, which it checks the first byte of to determine if the variable should be intialized or not. If the compiler-generated code reads that byte as a 0, it will call __cxa_guard_acquire to try and be the thread to call the constructor for the static variable. If the first byte is 1, it will assume that the variable's constructor was called, and go on to access it. __cxa_guard_acquire uses one of the 7 implementation defined bytes of the guard variable as an atomic 8 bit variable. To control a state machine that lets each entering thread know if they gained 'initialization rights', someone is working on the varaible, someone is working on the varaible and there's at least one thread waiting for it to be intialized, or if the variable was initialized and it's time to access it. We only store a 1 to the byte the compiler looks at in __cxa_guard_release, and use a futex to handle waiting.
Diffstat (limited to 'Libraries/LibC/cxxabi.cpp')
-rw-r--r--Libraries/LibC/cxxabi.cpp86
1 files changed, 0 insertions, 86 deletions
diff --git a/Libraries/LibC/cxxabi.cpp b/Libraries/LibC/cxxabi.cpp
deleted file mode 100644
index 68c9e33f3e..0000000000
--- a/Libraries/LibC/cxxabi.cpp
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
- * 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.
- */
-
-#include <AK/Types.h>
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-//#define GLOBAL_DTORS_DEBUG
-
-extern "C" {
-
-typedef void (*AtExitFunction)(void*);
-
-struct __exit_entry {
- AtExitFunction method;
- void* parameter;
- void* dso_handle;
- bool has_been_called;
-};
-
-static __exit_entry __exit_entries[1024] {};
-static int __exit_entry_count = 0;
-
-int __cxa_atexit(AtExitFunction exit_function, void* parameter, void* dso_handle)
-{
- if (__exit_entry_count >= 1024)
- return -1;
-
- __exit_entries[__exit_entry_count++] = { exit_function, parameter, dso_handle, false };
-
- return 0;
-}
-
-void __cxa_finalize(void* dso_handle)
-{
- // From the itanium abi, https://itanium-cxx-abi.github.io/cxx-abi/abi.html#dso-dtor-runtime-api
- //
- // When __cxa_finalize(d) is called, it should walk the termination function list, calling each in turn
- // if d matches __dso_handle for the termination function entry. If d == NULL, it should call all of them.
- // Multiple calls to __cxa_finalize shall not result in calling termination function entries multiple times;
- // the implementation may either remove entries or mark them finished.
-
- int entry_index = __exit_entry_count;
-
-#ifdef GLOBAL_DTORS_DEBUG
- dbgprintf("__cxa_finalize: %d entries in the finalizer list\n", entry_index);
-#endif
-
- while (--entry_index >= 0) {
- auto& exit_entry = __exit_entries[entry_index];
- bool needs_calling = !exit_entry.has_been_called && (!dso_handle || dso_handle == exit_entry.dso_handle);
- if (needs_calling) {
-#ifdef GLOBAL_DTORS_DEBUG
- dbgprintf("__cxa_finalize: calling entry[%d] %p(%p) dso: %p\n", entry_index, exit_entry.method, exit_entry.parameter, exit_entry.dso_handle);
-#endif
- exit_entry.method(exit_entry.parameter);
- exit_entry.has_been_called = true;
- }
- }
-}
-
-} // extern "C"