diff options
| -rw-r--r-- | src/common/CMakeLists.txt | 7 | ||||
| -rw-r--r-- | src/common/signal_chain.cpp | 42 | ||||
| -rw-r--r-- | src/common/signal_chain.h | 19 |
3 files changed, 68 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index e216eb3de..7107f4f78 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -166,6 +166,13 @@ if (WIN32) | |||
| 166 | target_link_libraries(common PRIVATE ntdll) | 166 | target_link_libraries(common PRIVATE ntdll) |
| 167 | endif() | 167 | endif() |
| 168 | 168 | ||
| 169 | if (NOT WIN32) | ||
| 170 | target_sources(common PRIVATE | ||
| 171 | signal_chain.cpp | ||
| 172 | signal_chain.h | ||
| 173 | ) | ||
| 174 | endif() | ||
| 175 | |||
| 169 | if(ANDROID) | 176 | if(ANDROID) |
| 170 | target_sources(common | 177 | target_sources(common |
| 171 | PRIVATE | 178 | PRIVATE |
diff --git a/src/common/signal_chain.cpp b/src/common/signal_chain.cpp new file mode 100644 index 000000000..e0c6b9d4e --- /dev/null +++ b/src/common/signal_chain.cpp | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <dlfcn.h> | ||
| 5 | |||
| 6 | #include "common/assert.h" | ||
| 7 | #include "common/dynamic_library.h" | ||
| 8 | #include "common/scope_exit.h" | ||
| 9 | #include "common/signal_chain.h" | ||
| 10 | |||
| 11 | namespace Common { | ||
| 12 | |||
| 13 | template <typename T> | ||
| 14 | T* LookupLibcSymbol(const char* name) { | ||
| 15 | #if defined(__BIONIC__) | ||
| 16 | Common::DynamicLibrary provider("libc.so"); | ||
| 17 | if (!provider.IsOpen()) { | ||
| 18 | UNREACHABLE_MSG("Failed to open libc!"); | ||
| 19 | } | ||
| 20 | #else | ||
| 21 | // For other operating environments, we assume the symbol is not overriden. | ||
| 22 | const char* base = nullptr; | ||
| 23 | Common::DynamicLibrary provider(base); | ||
| 24 | #endif | ||
| 25 | |||
| 26 | void* sym = provider.GetSymbolAddress(name); | ||
| 27 | if (sym == nullptr) { | ||
| 28 | sym = dlsym(RTLD_DEFAULT, name); | ||
| 29 | } | ||
| 30 | if (sym == nullptr) { | ||
| 31 | UNREACHABLE_MSG("Unable to find symbol {}!", name); | ||
| 32 | } | ||
| 33 | |||
| 34 | return reinterpret_cast<T*>(sym); | ||
| 35 | } | ||
| 36 | |||
| 37 | int SigAction(int signum, const struct sigaction* act, struct sigaction* oldact) { | ||
| 38 | static auto libc_sigaction = LookupLibcSymbol<decltype(sigaction)>("sigaction"); | ||
| 39 | return libc_sigaction(signum, act, oldact); | ||
| 40 | } | ||
| 41 | |||
| 42 | } // namespace Common | ||
diff --git a/src/common/signal_chain.h b/src/common/signal_chain.h new file mode 100644 index 000000000..e3bfe6882 --- /dev/null +++ b/src/common/signal_chain.h | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #ifndef _WIN32 | ||
| 7 | |||
| 8 | #include <signal.h> | ||
| 9 | |||
| 10 | namespace Common { | ||
| 11 | |||
| 12 | // Android's ART overrides sigaction with its own wrapper. This is problematic for SIGSEGV | ||
| 13 | // in particular, because ARTs handler access TPIDR_EL0, so this extracts the libc version | ||
| 14 | // and calls it directly. | ||
| 15 | int SigAction(int signum, const struct sigaction* act, struct sigaction* oldact); | ||
| 16 | |||
| 17 | } // namespace Common | ||
| 18 | |||
| 19 | #endif | ||