feat(libc): init c++ exception runtime for x8664 linux (wip)

This commit is contained in:
boykopovar
2026-09-05 19:34:15 +03:00
parent 9ac0798c09
commit 6fcbf1f829
8 changed files with 1153 additions and 164 deletions
+5 -140
View File
@@ -1,3 +1,7 @@
#include "exception/Unwind.cpp"
#include "exception/Runtime.cpp"
#include "exception/Personality.cpp"
#include "exception/StandardExceptions.cpp"
#include <cstddef>
#include <cxxabi.h>
#include <cstdint>
@@ -15,51 +19,6 @@
extern "C" {
void* __dynamic_cast_nid_postfix(
const void* source,
const __cxxabiv1::__class_type_info* sourceType,
const __cxxabiv1::__class_type_info* destinationType,
std::ptrdiff_t sourceToDestinationOffset
) {
(void)source;
(void)sourceType;
(void)destinationType;
(void)sourceToDestinationOffset;
return nullptr;
}
void* __cxa_allocate_exception_nid_postfix(std::size_t thrownSize) {
return std::malloc(thrownSize + 128);
}
void __cxa_free_exception_nid_postfix(void* thrownException) {
std::free(thrownException);
}
[[noreturn]] void __cxa_throw_nid_postfix(void* thrownException, std::type_info* tinfo, void (*dest)(void*)) {
(void)thrownException; (void)tinfo; (void)dest;
std::terminate();
}
void* __cxa_begin_catch_nid_postfix(void* exceptionObject) {
(void)exceptionObject;
return nullptr;
}
void __cxa_end_catch_nid_postfix() {}
void __cxa_rethrow_nid_postfix() {}
[[noreturn]] void __cxa_bad_cast_nid_postfix() { throw std::bad_cast(); }
[[noreturn]] void __cxa_bad_typeid_nid_postfix() { throw std::bad_typeid(); }
void* __cxa_get_exception_ptr_nid_postfix(void* exceptionObject) {
(void)exceptionObject;
return nullptr;
}
std::type_info* __cxa_current_exception_type_nid_postfix() { return nullptr; }
int __cxa_guard_acquire_nid_postfix(std::uint64_t* guardObject) {
(void)guardObject;
return 0;
@@ -111,9 +70,6 @@ void __cxa_vec_cctor_nid_postfix(void* dst, void* src, std::size_t n, std::size_
(void)dst; (void)src; (void)n; (void)s; (void)cctor; (void)dtor;
}
void __cxa_pure_virtual_nid_postfix() { std::terminate(); }
void __cxa_deleted_virtual_nid_postfix() { std::terminate(); }
void* __cxa_demangle_nid_postfix(const char* mangled, char* buf, std::size_t* len, int* status) {
(void)mangled; (void)buf; (void)len;
if (status) *status = -2;
@@ -125,98 +81,7 @@ int __cxa_thread_atexit_impl_nid_postfix(void (*func)(void*), void* arg, void* d
return 0;
}
void* __cxa_current_primary_exception_nid_postfix() { return nullptr; }
void __cxa_decrement_exception_refcount_nid_postfix(void* p) { (void)p; }
void __cxa_increment_exception_refcount_nid_postfix(void* p) { (void)p; }
void __cxa_rethrow_primary_exception_nid_postfix(void* p) { (void)p; }
bool __cxa_uncaught_exception_nid_postfix() { return false; }
unsigned int __cxa_uncaught_exceptions_nid_postfix() { return 0; }
void* __cxa_allocate_dependent_exception_nid_postfix() { return std::malloc(256); }
void __cxa_free_dependent_exception_nid_postfix(void* p) { std::free(p); }
[[noreturn]] void __cxa_throw_bad_array_new_length_nid_postfix() { throw std::bad_array_new_length(); }
_Unwind_Reason_Code _Unwind_RaiseException_nid_postfix(_Unwind_Exception* e) {
(void)e;
return _URC_END_OF_STACK;
}
[[noreturn]] void _Unwind_Resume_nid_postfix(_Unwind_Exception* e) {
(void)e;
std::terminate();
}
void _Unwind_DeleteException_nid_postfix(_Unwind_Exception* e) { (void)e; }
std::uintptr_t _Unwind_GetGR_nid_postfix(_Unwind_Context* ctx, int idx) { (void)ctx; (void)idx; return 0; }
void _Unwind_SetGR_nid_postfix(_Unwind_Context* ctx, int idx, std::uintptr_t v) { (void)ctx; (void)idx; (void)v; }
std::uintptr_t _Unwind_GetIP_nid_postfix(_Unwind_Context* ctx) { (void)ctx; return 0; }
void _Unwind_SetIP_nid_postfix(_Unwind_Context* ctx, std::uintptr_t v) { (void)ctx; (void)v; }
std::uintptr_t _Unwind_GetIPInfo_nid_postfix(_Unwind_Context* ctx, int* ipBefore) {
(void)ctx;
if (ipBefore) *ipBefore = 0;
return 0;
}
std::uintptr_t _Unwind_GetCFA_nid_postfix(_Unwind_Context* ctx) { (void)ctx; return 0; }
std::uintptr_t _Unwind_GetLanguageSpecificData_nid_postfix(_Unwind_Context* ctx) { (void)ctx; return 0; }
std::uintptr_t _Unwind_GetRegionStart_nid_postfix(_Unwind_Context* ctx) { (void)ctx; return 0; }
std::uintptr_t _Unwind_GetDataRelBase_nid_postfix(_Unwind_Context* ctx) { (void)ctx; return 0; }
std::uintptr_t _Unwind_GetTextRelBase_nid_postfix(_Unwind_Context* ctx) { (void)ctx; return 0; }
_Unwind_Reason_Code _Unwind_ForcedUnwind_nid_postfix(_Unwind_Exception* e, _Unwind_Stop_Fn stop, void* arg) {
(void)e; (void)stop; (void)arg;
return _URC_END_OF_STACK;
}
_Unwind_Reason_Code _Unwind_Resume_or_Rethrow_nid_postfix(_Unwind_Exception* e) {
(void)e;
return _URC_END_OF_STACK;
}
_Unwind_Reason_Code _Unwind_Backtrace_nid_postfix(_Unwind_Trace_Fn trace, void* arg) {
(void)trace; (void)arg;
return _URC_END_OF_STACK;
}
int dl_iterate_phdr_nid_postfix(int (*cb)(void*, std::size_t, void*), void* data) {
(void)cb; (void)data;
return 0;
}
void _ZNSt9exceptionD1Ev_nid_postfix() {}
void _ZNSt9exceptionD2Ev_nid_postfix() {}
void _ZNSt8bad_castD1Ev_nid_postfix() {}
void _ZNSt12out_of_rangeD1Ev_nid_postfix() {}
void _ZNSt12domain_errorD1Ev_nid_postfix() {}
void _ZNSt16invalid_argumentD1Ev_nid_postfix() {}
void _ZNSt13runtime_errorD2Ev_nid_postfix() {}
void _ZNSt8ios_base7failureD1Ev_nid_postfix() {}
void _ZNSt8ios_baseD2Ev_nid_postfix() {}
const char* _ZNKSt9exception4whatEv_nid_postfix() { return ""; }
[[noreturn]] void _ZSt9terminatev_nid_postfix() { std::terminate(); }
bool _ZSt18uncaught_exceptionv_nid_postfix() { return false; }
void _ZNSt8ios_baseD2Ev_nid_postfix(std::ios_base* self) { self->~ios_base(); }
const std::error_category* _ZSt17iostream_categoryv_nid_postfix() { return &std::iostream_category(); }
std::uintptr_t _ZTVSt13runtime_error_nid_postfix[5] {};
std::uintptr_t _ZTVSt12system_error_nid_postfix[5] {};
std::uintptr_t _ZTVSt9exception_nid_postfix[5] {};
std::uintptr_t _ZTVSt16invalid_argument_nid_postfix[5] {};
std::uintptr_t _ZTVSt12out_of_range_nid_postfix[5] {};
std::uintptr_t _ZTVSt12domain_error_nid_postfix[5] {};
std::uintptr_t _ZTVSt8bad_cast_nid_postfix[5] {};
std::uintptr_t _ZTVSt11logic_error_nid_postfix[5] {};
void* _ZTVNSt8ios_base7failureE_nid_postfix = nullptr;
void* _ZTISt8bad_cast_nid_postfix = nullptr;
void* _ZTISt12out_of_range_nid_postfix = nullptr;
void* _ZTISt12domain_error_nid_postfix = nullptr;
void* _ZTISt16invalid_argument_nid_postfix = nullptr;
void* _ZTINSt8ios_base7failureE_nid_postfix = nullptr;
}
-24
View File
@@ -49,30 +49,6 @@ unsigned int _Atomic_fetch_sub_4_nid_postfix(volatile unsigned int* target, unsi
return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST);
}
[[noreturn]] void _ZSt11_Xbad_allocv_nid_postfix() {
throw std::bad_alloc();
}
[[noreturn]] void _ZSt14_Xout_of_rangePKc_nid_postfix(const char* message) {
throw std::out_of_range(message);
}
[[noreturn]] void _ZSt14_Xlength_errorPKc_nid_postfix(const char* message) {
throw std::length_error(message);
}
[[noreturn]] void _ZSt18_Xinvalid_argumentPKc_nid_postfix(const char* message) {
throw std::invalid_argument(message);
}
[[noreturn]] void _ZSt19_Xbad_function_callv_nid_postfix() {
throw std::bad_function_call();
}
[[noreturn]] void _ZSt13_Xregex_errorNSt15regex_constants10error_typeE_nid_postfix(std::regex_constants::error_type code) {
throw std::regex_error(code);
}
unsigned long _Stoul_nid_postfix(const char* str, char** endptr, int base) {
return std::strtoul(str, endptr, base);
}
@@ -0,0 +1,72 @@
#include "Runtime.hpp"
extern "C" _Unwind_Reason_Code __gxx_personality_v0_nid_postfix(
int version, _Unwind_Action actions, std::uint64_t exceptionClass,
_Unwind_Exception* exception, _Unwind_Context* context
) {
using namespace LibcUnwind;
using namespace LibcException;
if (version != 1) return _URC_FATAL_PHASE1_ERROR;
if (!context->lsda) return _URC_CONTINUE_UNWIND;
const Byte* p = reinterpret_cast<const Byte*>(context->lsda);
Byte landingEncoding = *p++;
Word landingBase = landingEncoding == 255 ? context->region : Encoded(p, landingEncoding, context->dataBase, context->region, context->textBase);
Byte typeEncoding = *p++;
const Byte* typeTable = nullptr;
if (typeEncoding != 255) { Word offset = Uleb(p); typeTable = p + offset; }
Byte callEncoding = *p++;
Word length = Uleb(p);
const Byte* callEnd = p + length;
const Byte* actionsBegin = callEnd;
Word ip = context->registers[16] - !context->signalFrame;
Header* header = Native(exceptionClass) ? FromUnwind(exception) : nullptr;
while (p < callEnd) {
Word start = Encoded(p, callEncoding);
Word size = Encoded(p, callEncoding);
Word landing = Encoded(p, callEncoding);
Word action = Uleb(p);
if (ip < context->region + start || ip - context->region - start >= size) continue;
if (!landing) return _URC_CONTINUE_UNWIND;
bool cleanup = !action, matched = false;
std::intptr_t selector = 0;
void* adjusted = header ? static_cast<void*>(Primary(header) + 1) : nullptr;
if (header && std::strcmp(Kind(Primary(header)->type), "N10__cxxabiv119__pointer_type_infoE") == 0)
std::memcpy(&adjusted, adjusted, sizeof(adjusted));
if (action) {
const Byte* record = actionsBegin + action - 1;
for (unsigned steps = 0; steps < 4096; ++steps) {
auto filter = Sleb(record);
const Byte* nextField = record;
auto next = Sleb(record);
if (filter == 0) cleanup = true;
else if (filter > 0 && !(actions & _UA_FORCE_UNWIND) && typeTable) {
const Byte* entry = typeTable - filter * EncodingSize(typeEncoding);
auto* caught = reinterpret_cast<const std::type_info*>(Encoded(entry, typeEncoding, context->dataBase, context->region, context->textBase));
void* candidate = adjusted;
if (!caught || (header && Match(caught, Primary(header)->type, candidate))) {
matched = true; selector = filter; adjusted = candidate; break;
}
} else if (filter < 0) return (actions & _UA_SEARCH_PHASE) ? _URC_FATAL_PHASE1_ERROR : _URC_FATAL_PHASE2_ERROR;
if (!next) break;
record = nextField + next;
}
}
if (actions & _UA_SEARCH_PHASE) {
if (!matched) return _URC_CONTINUE_UNWIND;
if (header) { header->adjusted = adjusted; header->selector = static_cast<int>(selector); }
return _URC_HANDLER_FOUND;
}
if (actions & _UA_HANDLER_FRAME) {
if (!matched) return _URC_FATAL_PHASE2_ERROR;
if (header) header->adjusted = adjusted;
} else {
if (!cleanup) return _URC_CONTINUE_UNWIND;
selector = 0;
}
context->registers[0] = reinterpret_cast<Word>(exception);
context->registers[1] = selector;
context->registers[16] = landingBase + landing;
return _URC_INSTALL_CONTEXT;
}
return _URC_CONTINUE_UNWIND;
}
@@ -0,0 +1,238 @@
#include "Runtime.hpp"
#include <limits>
namespace LibcException {
inline std::atomic<void(*)()> terminateHandler {std::abort};
[[noreturn]] void Terminate() {
auto handler = terminateHandler.load(std::memory_order_acquire);
if (handler) handler();
std::abort();
}
void Release(void* object) {
if (!object) return;
auto* header = FromObject(object);
auto* allocation = AllocationOf(header);
if (allocation->references.fetch_sub(1, std::memory_order_acq_rel) == 1) {
if (header->destructor) header->destructor(object);
allocation->~Allocation();
std::free(allocation);
}
}
void Cleanup(_Unwind_Reason_Code, _Unwind_Exception* exception) {
auto* header = FromUnwind(exception);
if (exception->exception_class == DependentClass) {
Release(reinterpret_cast<void*>(header->type));
header->~Header();
std::free(header);
} else Release(header + 1);
}
bool Equal(const std::type_info* a, const std::type_info* b) {
return a == b || (a && b && std::strcmp(a->name(), b->name()) == 0);
}
const char* Kind(const std::type_info* type) {
auto table = *reinterpret_cast<const std::type_info* const* const*>(type);
return table[-1]->name();
}
struct Search {
const std::type_info* wanted;
void* found {};
unsigned count {};
};
void Bases(const std::type_info* type, void* object, Search& search, unsigned depth = 0) {
if (depth > 128 || !type) return;
if (Equal(type, search.wanted)) {
if (!search.count) { search.found = object; search.count = 1; }
else if (search.found != object) search.count = 2;
return;
}
const char* kind = Kind(type);
if (std::strcmp(kind, "N10__cxxabiv120__si_class_type_infoE") == 0) {
auto* si = static_cast<const __cxxabiv1::__si_class_type_info*>(type);
Bases(si->__base_type, object, search, depth + 1);
} else if (std::strcmp(kind, "N10__cxxabiv121__vmi_class_type_infoE") == 0) {
auto* vmi = static_cast<const __cxxabiv1::__vmi_class_type_info*>(type);
for (unsigned i = 0; i < vmi->__base_count; ++i) {
const auto& base = vmi->__base_info[i];
if (!(base.__offset_flags & 2)) continue;
auto offset = base.__offset_flags >> 8;
if (base.__offset_flags & 1) {
if (!object) continue;
auto* vtable = *static_cast<const unsigned char* const*>(object);
std::memcpy(&offset, vtable + offset, sizeof(offset));
}
void* adjusted = object ? static_cast<unsigned char*>(object) + offset : nullptr;
Bases(base.__base_type, adjusted, search, depth + 1);
}
}
}
bool Match(const std::type_info* caught, const std::type_info* thrown, void*& object) {
if (!caught || Equal(caught, thrown)) return true;
if (!thrown) return false;
const char* caughtKind = Kind(caught);
const char* thrownKind = Kind(thrown);
if (std::strcmp(caughtKind, "N10__cxxabiv119__pointer_type_infoE") == 0 &&
std::strcmp(thrownKind, "N10__cxxabiv119__pointer_type_infoE") == 0) {
auto* c = static_cast<const __cxxabiv1::__pointer_type_info*>(caught);
auto* t = static_cast<const __cxxabiv1::__pointer_type_info*>(thrown);
if ((t->__flags & 7) & ~(c->__flags & 7)) return false;
if (std::strcmp(c->__pointee->name(), "v") == 0 && std::strcmp(Kind(t->__pointee), "N10__cxxabiv120__function_type_infoE") != 0) return true;
Search search {c->__pointee};
Bases(t->__pointee, object, search);
if (search.count == 1) { object = search.found; return true; }
return false;
}
Search search {caught};
Bases(thrown, object, search);
if (search.count == 1) { object = search.found; return true; }
return false;
}
}
extern "C" {
void* __cxa_allocate_exception_nid_postfix(std::size_t size) {
using namespace LibcException;
if (size > std::numeric_limits<std::size_t>::max() - sizeof(Allocation)) Terminate();
void* storage = std::malloc(sizeof(Allocation) + size);
if (!storage) Terminate();
auto* allocation = new (storage) Allocation;
static_assert(offsetof(Allocation, header) + sizeof(Header) == sizeof(Allocation));
return &allocation->header + 1;
}
void __cxa_free_exception_nid_postfix(void* object) {
if (!object) return;
auto* allocation = LibcException::AllocationOf(LibcException::FromObject(object));
allocation->~Allocation();
std::free(allocation);
}
[[noreturn]] void __cxa_throw_nid_postfix(void* object, std::type_info* type, void (*destructor)(void*)) {
using namespace LibcException;
auto* header = FromObject(object);
header->type = type;
header->destructor = destructor;
header->terminate = terminateHandler.load(std::memory_order_acquire);
header->adjusted = object;
header->unwind.exception_class = PrimaryClass;
header->unwind.exception_cleanup = Cleanup;
++globals.uncaught;
_Unwind_RaiseException_nid_postfix(&header->unwind);
Terminate();
}
void* __cxa_begin_catch_nid_postfix(void* exception) {
using namespace LibcException;
auto* unwind = static_cast<_Unwind_Exception*>(exception);
if (!unwind || !Native(unwind->exception_class)) Terminate();
auto* header = FromUnwind(unwind);
header->handlers = header->handlers < 0 ? -header->handlers + 1 : header->handlers + 1;
if (globals.uncaught) --globals.uncaught;
if (globals.caught != header) { header->next = globals.caught; globals.caught = header; }
return header->adjusted;
}
void __cxa_end_catch_nid_postfix() {
using namespace LibcException;
auto* header = globals.caught;
if (!header) return;
if (header->handlers < 0) {
if (++header->handlers == 0) globals.caught = header->next;
} else if (--header->handlers == 0) {
globals.caught = header->next;
_Unwind_DeleteException_nid_postfix(&header->unwind);
}
}
[[noreturn]] void __cxa_rethrow_nid_postfix() {
using namespace LibcException;
auto* header = globals.caught;
if (!header) Terminate();
header->handlers = -header->handlers;
++globals.uncaught;
_Unwind_RaiseException_nid_postfix(&header->unwind);
Terminate();
}
void* __cxa_get_exception_ptr_nid_postfix(void* exception) {
return LibcException::FromUnwind(static_cast<_Unwind_Exception*>(exception))->adjusted;
}
std::type_info* __cxa_current_exception_type_nid_postfix() {
using namespace LibcException;
return globals.caught ? Primary(globals.caught)->type : nullptr;
}
void* __cxa_get_globals_nid_postfix() { return &LibcException::globals; }
void* __cxa_get_globals_fast_nid_postfix() { return &LibcException::globals; }
bool __cxa_uncaught_exception_nid_postfix() { return LibcException::globals.uncaught != 0; }
unsigned __cxa_uncaught_exceptions_nid_postfix() { return LibcException::globals.uncaught; }
bool _ZSt18uncaught_exceptionv_nid_postfix() { return __cxa_uncaught_exception_nid_postfix(); }
int _ZSt19uncaught_exceptionsv_nid_postfix() { return static_cast<int>(__cxa_uncaught_exceptions_nid_postfix()); }
[[noreturn]] void _ZSt9terminatev_nid_postfix() { LibcException::Terminate(); }
using LibcTerminateHandler = void(*)();
LibcTerminateHandler _ZSt13set_terminatePFvvE_nid_postfix(LibcTerminateHandler handler) {
return LibcException::terminateHandler.exchange(handler ? handler : std::abort);
}
LibcTerminateHandler _ZSt13get_terminatev_nid_postfix() { return LibcException::terminateHandler.load(); }
void __cxa_increment_exception_refcount_nid_postfix(void* object) {
if (object) LibcException::AllocationOf(LibcException::FromObject(object))->references.fetch_add(1, std::memory_order_relaxed);
}
void __cxa_decrement_exception_refcount_nid_postfix(void* object) { LibcException::Release(object); }
void* __cxa_current_primary_exception_nid_postfix() {
using namespace LibcException;
if (!globals.caught) return nullptr;
void* object = Primary(globals.caught) + 1;
__cxa_increment_exception_refcount_nid_postfix(object);
return object;
}
void* __cxa_allocate_dependent_exception_nid_postfix() {
void* storage = std::malloc(sizeof(LibcException::Header));
if (!storage) LibcException::Terminate();
return new (storage) LibcException::Header;
}
void __cxa_free_dependent_exception_nid_postfix(void* storage) {
if (!storage) return;
static_cast<LibcException::Header*>(storage)->~Header();
std::free(storage);
}
void __cxa_rethrow_primary_exception_nid_postfix(void* object) {
using namespace LibcException;
if (!object) return;
auto* header = static_cast<Header*>(__cxa_allocate_dependent_exception_nid_postfix());
header->type = reinterpret_cast<std::type_info*>(object);
header->adjusted = object;
header->terminate = terminateHandler.load();
header->unwind.exception_class = DependentClass;
header->unwind.exception_cleanup = Cleanup;
__cxa_increment_exception_refcount_nid_postfix(object);
++globals.uncaught;
_Unwind_RaiseException_nid_postfix(&header->unwind);
Terminate();
}
void* __dynamic_cast_nid_postfix(const void* source, const __cxxabiv1::__class_type_info* sourceType,
const __cxxabiv1::__class_type_info* destinationType, std::ptrdiff_t) {
using namespace LibcException;
if (!source) return nullptr;
auto* vtable = *static_cast<const std::uintptr_t* const*>(source);
auto* complete = const_cast<unsigned char*>(static_cast<const unsigned char*>(source)) + static_cast<std::intptr_t>(vtable[-2]);
auto* dynamicType = reinterpret_cast<const std::type_info*>(vtable[-1]);
Search target {destinationType};
Bases(dynamicType, complete, target);
if (target.count != 1) return nullptr;
Search downcast {sourceType};
Bases(destinationType, target.found, downcast);
if (downcast.count == 1 && downcast.found == source) return target.found;
Search publicSource {sourceType};
Bases(dynamicType, complete, publicSource);
return publicSource.count == 1 && publicSource.found == source ? target.found : nullptr;
}
[[noreturn]] void __cxa_pure_virtual_nid_postfix() { LibcException::Terminate(); }
[[noreturn]] void __cxa_deleted_virtual_nid_postfix() { LibcException::Terminate(); }
}
@@ -0,0 +1,55 @@
#pragma once
#include "Unwind.hpp"
#include <atomic>
#include <typeinfo>
#include <cxxabi.h>
namespace LibcException {
struct Header {
std::type_info* type {};
void (*destructor)(void*) {};
void (*unexpected)() {};
void (*terminate)() {};
Header* next {};
int handlers {};
int selector {};
const unsigned char* action {};
const unsigned char* lsda {};
std::uintptr_t landing {};
void* adjusted {};
_Unwind_Exception unwind {};
};
struct alignas(16) Allocation {
std::atomic<std::size_t> references {1};
Header header {};
};
struct Globals { Header* caught {}; unsigned uncaught {}; };
inline thread_local Globals globals;
inline constexpr std::uint64_t PrimaryClass = 0x414e5950432b2b00;
inline constexpr std::uint64_t DependentClass = PrimaryClass | 1;
inline Header* FromUnwind(_Unwind_Exception* e) {
return reinterpret_cast<Header*>(reinterpret_cast<unsigned char*>(e) - offsetof(Header, unwind));
}
inline Header* FromObject(void* p) { return static_cast<Header*>(p) - 1; }
inline Allocation* AllocationOf(Header* header) {
return reinterpret_cast<Allocation*>(reinterpret_cast<unsigned char*>(header) - offsetof(Allocation, header));
}
inline bool Native(std::uint64_t value) { return (value & ~std::uint64_t(1)) == PrimaryClass; }
inline Header* Primary(Header* header) {
return header->unwind.exception_class == DependentClass ? FromObject(reinterpret_cast<void*>(header->type)) : header;
}
const char* Kind(const std::type_info*);
bool Match(const std::type_info* caught, const std::type_info* thrown, void*& object);
void Release(void*);
[[noreturn]] void Terminate();
}
extern "C" {
void* __cxa_allocate_exception_nid_postfix(std::size_t);
void __cxa_free_exception_nid_postfix(void*);
[[noreturn]] void __cxa_throw_nid_postfix(void*, std::type_info*, void (*)(void*));
[[noreturn]] void __cxa_rethrow_nid_postfix();
void* __cxa_begin_catch_nid_postfix(void*);
void __cxa_end_catch_nid_postfix();
}
@@ -0,0 +1,280 @@
#include "Runtime.hpp"
#include <limits>
#include <regex>
namespace LibcException {
struct TypeRecord { const void* const* vtable; const char* name; const TypeRecord* base; };
struct ExceptionObject { const void* vtable; const char* message; };
struct ExceptionVtable {
std::ptrdiff_t offset;
const TypeRecord* type;
void (*destroy)(ExceptionObject*);
void (*deleteObject)(ExceptionObject*);
const char* (*what)(const ExceptionObject*);
};
struct Message {
std::size_t length;
std::size_t capacity;
std::atomic<std::ptrdiff_t> references;
};
void DestroyType(TypeRecord*) {}
void DeleteType(TypeRecord* type) { std::free(type); }
bool NotPointer(const TypeRecord*) { return false; }
bool TypeCatch(const TypeRecord* self, const TypeRecord* thrown, void** object, unsigned) {
return Match(reinterpret_cast<const std::type_info*>(self), reinterpret_cast<const std::type_info*>(thrown), *object);
}
bool TypeUpcast(const TypeRecord* self, const TypeRecord* base, void** object) {
Search search {reinterpret_cast<const std::type_info*>(base)};
Bases(reinterpret_cast<const std::type_info*>(self), *object, search);
if (search.count != 1) return false;
*object = search.found; return true;
}
const TypeRecord ClassCategory {nullptr, "N10__cxxabiv117__class_type_infoE", nullptr};
const TypeRecord SingleCategory {nullptr, "N10__cxxabiv120__si_class_type_infoE", nullptr};
const void* const ClassTypeVtable[] {
nullptr, &ClassCategory,
reinterpret_cast<const void*>(DestroyType), reinterpret_cast<const void*>(DeleteType),
reinterpret_cast<const void*>(NotPointer), reinterpret_cast<const void*>(NotPointer),
reinterpret_cast<const void*>(TypeCatch), reinterpret_cast<const void*>(TypeUpcast)
};
const void* const SingleTypeVtable[] {
nullptr, &SingleCategory,
reinterpret_cast<const void*>(DestroyType), reinterpret_cast<const void*>(DeleteType),
reinterpret_cast<const void*>(NotPointer), reinterpret_cast<const void*>(NotPointer),
reinterpret_cast<const void*>(TypeCatch), reinterpret_cast<const void*>(TypeUpcast)
};
const char* CopyMessage(const char* message) {
if (!message) message = "";
auto size = std::strlen(message);
if (size > std::numeric_limits<std::size_t>::max() - sizeof(Message) - 1) Terminate();
void* storage = std::malloc(sizeof(Message) + size + 1);
if (!storage) Terminate();
auto* header = new (storage) Message {size, size, 0};
char* result = reinterpret_cast<char*>(header + 1);
std::memcpy(result, message, size + 1);
return result;
}
void RetainMessage(const char* message) {
if (message) (reinterpret_cast<Message*>(const_cast<char*>(message)) - 1)->references.fetch_add(1, std::memory_order_relaxed);
}
void ReleaseMessage(const char* message) {
if (!message) return;
auto* header = reinterpret_cast<Message*>(const_cast<char*>(message)) - 1;
if (header->references.fetch_sub(1, std::memory_order_acq_rel) == 0) {
header->~Message(); std::free(header);
}
}
void DestroyPlain(ExceptionObject*) {}
void DeletePlain(ExceptionObject* object) { std::free(object); }
void DestroyMessage(ExceptionObject* object) { ReleaseMessage(object->message); object->message = nullptr; }
void DeleteMessage(ExceptionObject* object) { DestroyMessage(object); std::free(object); }
const char* PlainWhat(const ExceptionObject* object) {
auto* table = static_cast<const ExceptionVtable*>(object->vtable) - 0;
auto* complete = reinterpret_cast<const ExceptionVtable*>(reinterpret_cast<const unsigned char*>(table) - offsetof(ExceptionVtable, destroy));
return complete->type->name;
}
const char* MessageWhat(const ExceptionObject* object) { return object->message ? object->message : ""; }
void Construct(ExceptionObject* object, const ExceptionVtable& table, const char* message) {
object->vtable = &table.destroy;
object->message = CopyMessage(message);
}
void Copy(ExceptionObject* object, const ExceptionObject* source, const ExceptionVtable& table) {
object->vtable = &table.destroy;
object->message = source->message;
RetainMessage(object->message);
}
ExceptionObject* Assign(ExceptionObject* object, const ExceptionObject* source) {
if (object != source) { RetainMessage(source->message); ReleaseMessage(object->message); object->message = source->message; }
return object;
}
[[noreturn]] void ThrowMessage(const ExceptionVtable& table, const char* message) {
auto* object = static_cast<ExceptionObject*>(__cxa_allocate_exception_nid_postfix(sizeof(ExceptionObject)));
Construct(object, table, message);
__cxa_throw_nid_postfix(object, reinterpret_cast<std::type_info*>(const_cast<TypeRecord*>(table.type)), [](void* p) { DestroyMessage(static_cast<ExceptionObject*>(p)); });
}
[[noreturn]] void ThrowPlain(const ExceptionVtable& table) {
auto* object = static_cast<ExceptionObject*>(__cxa_allocate_exception_nid_postfix(sizeof(void*)));
object->vtable = &table.destroy;
__cxa_throw_nid_postfix(object, reinterpret_cast<std::type_info*>(const_cast<TypeRecord*>(table.type)), [](void* p) { DestroyPlain(static_cast<ExceptionObject*>(p)); });
}
}
extern "C" {
LibcException::TypeRecord _ZTISt9exception_nid_postfix {LibcException::ClassTypeVtable + 2, "St9exception", nullptr};
LibcException::ExceptionVtable _ZTVSt9exception_nid_postfix {0, &_ZTISt9exception_nid_postfix, LibcException::DestroyPlain, LibcException::DeletePlain, LibcException::PlainWhat};
void _ZNSt9exceptionD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt9exceptionD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt9exceptionD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeletePlain(self); }
const char* _ZNKSt9exception4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::PlainWhat(self); }
void _ZNSt9exceptionC1Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt9exception_nid_postfix.destroy; }
void _ZNSt9exceptionC2Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt9exception_nid_postfix.destroy; }
LibcException::TypeRecord _ZTISt8bad_cast_nid_postfix {LibcException::SingleTypeVtable + 2, "St8bad_cast", &_ZTISt9exception_nid_postfix};
LibcException::ExceptionVtable _ZTVSt8bad_cast_nid_postfix {0, &_ZTISt8bad_cast_nid_postfix, LibcException::DestroyPlain, LibcException::DeletePlain, LibcException::PlainWhat};
void _ZNSt8bad_castD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt8bad_castD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt8bad_castD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeletePlain(self); }
const char* _ZNKSt8bad_cast4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::PlainWhat(self); }
void _ZNSt8bad_castC1Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt8bad_cast_nid_postfix.destroy; }
void _ZNSt8bad_castC2Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt8bad_cast_nid_postfix.destroy; }
LibcException::TypeRecord _ZTISt10bad_typeid_nid_postfix {LibcException::SingleTypeVtable + 2, "St10bad_typeid", &_ZTISt9exception_nid_postfix};
LibcException::ExceptionVtable _ZTVSt10bad_typeid_nid_postfix {0, &_ZTISt10bad_typeid_nid_postfix, LibcException::DestroyPlain, LibcException::DeletePlain, LibcException::PlainWhat};
void _ZNSt10bad_typeidD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt10bad_typeidD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt10bad_typeidD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeletePlain(self); }
const char* _ZNKSt10bad_typeid4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::PlainWhat(self); }
void _ZNSt10bad_typeidC1Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt10bad_typeid_nid_postfix.destroy; }
void _ZNSt10bad_typeidC2Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt10bad_typeid_nid_postfix.destroy; }
LibcException::TypeRecord _ZTISt9bad_alloc_nid_postfix {LibcException::SingleTypeVtable + 2, "St9bad_alloc", &_ZTISt9exception_nid_postfix};
LibcException::ExceptionVtable _ZTVSt9bad_alloc_nid_postfix {0, &_ZTISt9bad_alloc_nid_postfix, LibcException::DestroyPlain, LibcException::DeletePlain, LibcException::PlainWhat};
void _ZNSt9bad_allocD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt9bad_allocD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt9bad_allocD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeletePlain(self); }
const char* _ZNKSt9bad_alloc4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::PlainWhat(self); }
void _ZNSt9bad_allocC1Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt9bad_alloc_nid_postfix.destroy; }
void _ZNSt9bad_allocC2Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt9bad_alloc_nid_postfix.destroy; }
LibcException::TypeRecord _ZTISt20bad_array_new_length_nid_postfix {LibcException::SingleTypeVtable + 2, "St20bad_array_new_length", &_ZTISt9bad_alloc_nid_postfix};
LibcException::ExceptionVtable _ZTVSt20bad_array_new_length_nid_postfix {0, &_ZTISt20bad_array_new_length_nid_postfix, LibcException::DestroyPlain, LibcException::DeletePlain, LibcException::PlainWhat};
void _ZNSt20bad_array_new_lengthD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt20bad_array_new_lengthD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt20bad_array_new_lengthD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeletePlain(self); }
const char* _ZNKSt20bad_array_new_length4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::PlainWhat(self); }
void _ZNSt20bad_array_new_lengthC1Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt20bad_array_new_length_nid_postfix.destroy; }
void _ZNSt20bad_array_new_lengthC2Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt20bad_array_new_length_nid_postfix.destroy; }
LibcException::TypeRecord _ZTISt17bad_function_call_nid_postfix {LibcException::SingleTypeVtable + 2, "St17bad_function_call", &_ZTISt9exception_nid_postfix};
LibcException::ExceptionVtable _ZTVSt17bad_function_call_nid_postfix {0, &_ZTISt17bad_function_call_nid_postfix, LibcException::DestroyPlain, LibcException::DeletePlain, LibcException::PlainWhat};
void _ZNSt17bad_function_callD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt17bad_function_callD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyPlain(self); }
void _ZNSt17bad_function_callD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeletePlain(self); }
const char* _ZNKSt17bad_function_call4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::PlainWhat(self); }
void _ZNSt17bad_function_callC1Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt17bad_function_call_nid_postfix.destroy; }
void _ZNSt17bad_function_callC2Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt17bad_function_call_nid_postfix.destroy; }
LibcException::TypeRecord _ZTISt11logic_error_nid_postfix {LibcException::SingleTypeVtable + 2, "St11logic_error", &_ZTISt9exception_nid_postfix};
LibcException::ExceptionVtable _ZTVSt11logic_error_nid_postfix {0, &_ZTISt11logic_error_nid_postfix, LibcException::DestroyMessage, LibcException::DeleteMessage, LibcException::MessageWhat};
void _ZNSt11logic_errorD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt11logic_errorD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt11logic_errorD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeleteMessage(self); }
const char* _ZNKSt11logic_error4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::MessageWhat(self); }
void _ZNSt11logic_errorC1EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt11logic_error_nid_postfix, message); }
void _ZNSt11logic_errorC2EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt11logic_error_nid_postfix, message); }
void _ZNSt11logic_errorC1ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt11logic_error_nid_postfix); }
void _ZNSt11logic_errorC2ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt11logic_error_nid_postfix); }
LibcException::ExceptionObject* _ZNSt11logic_erroraSERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { return LibcException::Assign(self, source); }
LibcException::TypeRecord _ZTISt13runtime_error_nid_postfix {LibcException::SingleTypeVtable + 2, "St13runtime_error", &_ZTISt9exception_nid_postfix};
LibcException::ExceptionVtable _ZTVSt13runtime_error_nid_postfix {0, &_ZTISt13runtime_error_nid_postfix, LibcException::DestroyMessage, LibcException::DeleteMessage, LibcException::MessageWhat};
void _ZNSt13runtime_errorD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt13runtime_errorD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt13runtime_errorD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeleteMessage(self); }
const char* _ZNKSt13runtime_error4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::MessageWhat(self); }
void _ZNSt13runtime_errorC1EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt13runtime_error_nid_postfix, message); }
void _ZNSt13runtime_errorC2EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt13runtime_error_nid_postfix, message); }
void _ZNSt13runtime_errorC1ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt13runtime_error_nid_postfix); }
void _ZNSt13runtime_errorC2ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt13runtime_error_nid_postfix); }
LibcException::ExceptionObject* _ZNSt13runtime_erroraSERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { return LibcException::Assign(self, source); }
LibcException::TypeRecord _ZTISt16invalid_argument_nid_postfix {LibcException::SingleTypeVtable + 2, "St16invalid_argument", &_ZTISt11logic_error_nid_postfix};
LibcException::ExceptionVtable _ZTVSt16invalid_argument_nid_postfix {0, &_ZTISt16invalid_argument_nid_postfix, LibcException::DestroyMessage, LibcException::DeleteMessage, LibcException::MessageWhat};
void _ZNSt16invalid_argumentD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt16invalid_argumentD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt16invalid_argumentD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeleteMessage(self); }
const char* _ZNKSt16invalid_argument4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::MessageWhat(self); }
void _ZNSt16invalid_argumentC1EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt16invalid_argument_nid_postfix, message); }
void _ZNSt16invalid_argumentC2EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt16invalid_argument_nid_postfix, message); }
void _ZNSt16invalid_argumentC1ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt16invalid_argument_nid_postfix); }
void _ZNSt16invalid_argumentC2ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt16invalid_argument_nid_postfix); }
LibcException::ExceptionObject* _ZNSt16invalid_argumentaSERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { return LibcException::Assign(self, source); }
LibcException::TypeRecord _ZTISt12out_of_range_nid_postfix {LibcException::SingleTypeVtable + 2, "St12out_of_range", &_ZTISt11logic_error_nid_postfix};
LibcException::ExceptionVtable _ZTVSt12out_of_range_nid_postfix {0, &_ZTISt12out_of_range_nid_postfix, LibcException::DestroyMessage, LibcException::DeleteMessage, LibcException::MessageWhat};
void _ZNSt12out_of_rangeD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt12out_of_rangeD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt12out_of_rangeD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeleteMessage(self); }
const char* _ZNKSt12out_of_range4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::MessageWhat(self); }
void _ZNSt12out_of_rangeC1EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt12out_of_range_nid_postfix, message); }
void _ZNSt12out_of_rangeC2EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt12out_of_range_nid_postfix, message); }
void _ZNSt12out_of_rangeC1ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt12out_of_range_nid_postfix); }
void _ZNSt12out_of_rangeC2ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt12out_of_range_nid_postfix); }
LibcException::ExceptionObject* _ZNSt12out_of_rangeaSERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { return LibcException::Assign(self, source); }
LibcException::TypeRecord _ZTISt12domain_error_nid_postfix {LibcException::SingleTypeVtable + 2, "St12domain_error", &_ZTISt11logic_error_nid_postfix};
LibcException::ExceptionVtable _ZTVSt12domain_error_nid_postfix {0, &_ZTISt12domain_error_nid_postfix, LibcException::DestroyMessage, LibcException::DeleteMessage, LibcException::MessageWhat};
void _ZNSt12domain_errorD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt12domain_errorD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt12domain_errorD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeleteMessage(self); }
const char* _ZNKSt12domain_error4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::MessageWhat(self); }
void _ZNSt12domain_errorC1EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt12domain_error_nid_postfix, message); }
void _ZNSt12domain_errorC2EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt12domain_error_nid_postfix, message); }
void _ZNSt12domain_errorC1ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt12domain_error_nid_postfix); }
void _ZNSt12domain_errorC2ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt12domain_error_nid_postfix); }
LibcException::ExceptionObject* _ZNSt12domain_erroraSERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { return LibcException::Assign(self, source); }
LibcException::TypeRecord _ZTISt12length_error_nid_postfix {LibcException::SingleTypeVtable + 2, "St12length_error", &_ZTISt11logic_error_nid_postfix};
LibcException::ExceptionVtable _ZTVSt12length_error_nid_postfix {0, &_ZTISt12length_error_nid_postfix, LibcException::DestroyMessage, LibcException::DeleteMessage, LibcException::MessageWhat};
void _ZNSt12length_errorD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt12length_errorD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt12length_errorD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeleteMessage(self); }
const char* _ZNKSt12length_error4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::MessageWhat(self); }
void _ZNSt12length_errorC1EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt12length_error_nid_postfix, message); }
void _ZNSt12length_errorC2EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt12length_error_nid_postfix, message); }
void _ZNSt12length_errorC1ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt12length_error_nid_postfix); }
void _ZNSt12length_errorC2ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt12length_error_nid_postfix); }
LibcException::ExceptionObject* _ZNSt12length_erroraSERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { return LibcException::Assign(self, source); }
LibcException::TypeRecord _ZTISt12system_error_nid_postfix {LibcException::SingleTypeVtable + 2, "St12system_error", &_ZTISt13runtime_error_nid_postfix};
LibcException::ExceptionVtable _ZTVSt12system_error_nid_postfix {0, &_ZTISt12system_error_nid_postfix, LibcException::DestroyMessage, LibcException::DeleteMessage, LibcException::MessageWhat};
void _ZNSt12system_errorD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt12system_errorD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt12system_errorD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeleteMessage(self); }
const char* _ZNKSt12system_error4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::MessageWhat(self); }
void _ZNSt12system_errorC1EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt12system_error_nid_postfix, message); }
void _ZNSt12system_errorC2EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt12system_error_nid_postfix, message); }
void _ZNSt12system_errorC1ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt12system_error_nid_postfix); }
void _ZNSt12system_errorC2ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt12system_error_nid_postfix); }
LibcException::ExceptionObject* _ZNSt12system_erroraSERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { return LibcException::Assign(self, source); }
LibcException::TypeRecord _ZTISt11regex_error_nid_postfix {LibcException::SingleTypeVtable + 2, "St11regex_error", &_ZTISt13runtime_error_nid_postfix};
LibcException::ExceptionVtable _ZTVSt11regex_error_nid_postfix {0, &_ZTISt11regex_error_nid_postfix, LibcException::DestroyMessage, LibcException::DeleteMessage, LibcException::MessageWhat};
void _ZNSt11regex_errorD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt11regex_errorD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt11regex_errorD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeleteMessage(self); }
const char* _ZNKSt11regex_error4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::MessageWhat(self); }
void _ZNSt11regex_errorC1EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt11regex_error_nid_postfix, message); }
void _ZNSt11regex_errorC2EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVSt11regex_error_nid_postfix, message); }
void _ZNSt11regex_errorC1ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt11regex_error_nid_postfix); }
void _ZNSt11regex_errorC2ERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, _ZTVSt11regex_error_nid_postfix); }
LibcException::ExceptionObject* _ZNSt11regex_erroraSERKS__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { return LibcException::Assign(self, source); }
LibcException::TypeRecord _ZTINSt8ios_base7failureE_nid_postfix {LibcException::SingleTypeVtable + 2, "NSt8ios_base7failureE", &_ZTISt12system_error_nid_postfix};
LibcException::ExceptionVtable _ZTVNSt8ios_base7failureE_nid_postfix {0, &_ZTINSt8ios_base7failureE_nid_postfix, LibcException::DestroyMessage, LibcException::DeleteMessage, LibcException::MessageWhat};
void _ZNSt8ios_base7failureD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt8ios_base7failureD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); }
void _ZNSt8ios_base7failureD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeleteMessage(self); }
const char* _ZNKSt8ios_base7failure4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::MessageWhat(self); }
void _ZNSt8ios_base7failureC1EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVNSt8ios_base7failureE_nid_postfix, message); }
void _ZNSt8ios_base7failureC2EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, _ZTVNSt8ios_base7failureE_nid_postfix, message); }
[[noreturn]] void __cxa_bad_cast_nid_postfix() { LibcException::ThrowPlain(_ZTVSt8bad_cast_nid_postfix); }
[[noreturn]] void __cxa_bad_typeid_nid_postfix() { LibcException::ThrowPlain(_ZTVSt10bad_typeid_nid_postfix); }
[[noreturn]] void __cxa_throw_bad_array_new_length_nid_postfix() { LibcException::ThrowPlain(_ZTVSt20bad_array_new_length_nid_postfix); }
[[noreturn]] void _ZSt11_Xbad_allocv_nid_postfix() { LibcException::ThrowPlain(_ZTVSt9bad_alloc_nid_postfix); }
[[noreturn]] void _ZSt14_Xout_of_rangePKc_nid_postfix(const char* message) { LibcException::ThrowMessage(_ZTVSt12out_of_range_nid_postfix, message); }
[[noreturn]] void _ZSt14_Xlength_errorPKc_nid_postfix(const char* message) { LibcException::ThrowMessage(_ZTVSt12length_error_nid_postfix, message); }
[[noreturn]] void _ZSt18_Xinvalid_argumentPKc_nid_postfix(const char* message) { LibcException::ThrowMessage(_ZTVSt16invalid_argument_nid_postfix, message); }
[[noreturn]] void _ZSt19_Xbad_function_callv_nid_postfix() { LibcException::ThrowPlain(_ZTVSt17bad_function_call_nid_postfix); }
[[noreturn]] void _ZSt13_Xregex_errorNSt15regex_constants10error_typeE_nid_postfix(std::regex_constants::error_type code) {
struct RegexObject { LibcException::ExceptionObject base; std::regex_constants::error_type code; };
auto* object = static_cast<RegexObject*>(__cxa_allocate_exception_nid_postfix(sizeof(RegexObject)));
LibcException::Construct(&object->base, _ZTVSt11regex_error_nid_postfix, "regular expression error");
object->code = code;
__cxa_throw_nid_postfix(object, reinterpret_cast<std::type_info*>(&_ZTISt11regex_error_nid_postfix), [](void* p) { LibcException::DestroyMessage(static_cast<LibcException::ExceptionObject*>(p)); });
}
}
+403
View File
@@ -0,0 +1,403 @@
#include "Unwind.hpp"
#include <link.h>
#if !defined(__linux__) || !defined(__x86_64__)
#error Unsupported unwind platform
#endif
extern "C" void LibcCaptureRegisters(std::uintptr_t*) __attribute__((visibility("hidden")));
extern "C" [[noreturn]] void LibcRestoreRegisters(const std::uintptr_t*) __attribute__((visibility("hidden")));
asm(
".text\n"
".hidden LibcCaptureRegisters\n"
".type LibcCaptureRegisters,@function\n"
"LibcCaptureRegisters:\n"
"movq %rax,0(%rdi)\nmovq %rdx,8(%rdi)\nmovq %rcx,16(%rdi)\nmovq %rbx,24(%rdi)\n"
"movq %rsi,32(%rdi)\nmovq %rdi,40(%rdi)\nmovq %rbp,48(%rdi)\n"
"leaq 8(%rsp),%rax\nmovq %rax,56(%rdi)\n"
"movq %r8,64(%rdi)\nmovq %r9,72(%rdi)\nmovq %r10,80(%rdi)\nmovq %r11,88(%rdi)\n"
"movq %r12,96(%rdi)\nmovq %r13,104(%rdi)\nmovq %r14,112(%rdi)\nmovq %r15,120(%rdi)\n"
"movq (%rsp),%rax\nmovq %rax,128(%rdi)\nret\n"
".size LibcCaptureRegisters,.-LibcCaptureRegisters\n"
".hidden LibcRestoreRegisters\n.type LibcRestoreRegisters,@function\n"
"LibcRestoreRegisters:\n"
"movq %rdi,%r10\nmovq 128(%r10),%r11\n"
"movq 0(%r10),%rax\nmovq 8(%r10),%rdx\nmovq 16(%r10),%rcx\nmovq 24(%r10),%rbx\n"
"movq 32(%r10),%rsi\nmovq 40(%r10),%rdi\nmovq 48(%r10),%rbp\nmovq 56(%r10),%rsp\n"
"movq 64(%r10),%r8\nmovq 72(%r10),%r9\nmovq 96(%r10),%r12\n"
"movq 104(%r10),%r13\nmovq 112(%r10),%r14\nmovq 120(%r10),%r15\njmp *%r11\n"
".size LibcRestoreRegisters,.-LibcRestoreRegisters\n"
);
namespace LibcUnwind {
struct Lookup { Word pc; const Byte* fde {}; Word text {}; Word data {}; };
int FindFrame(dl_phdr_info* info, std::size_t, void* argument) {
auto& query = *static_cast<Lookup*>(argument);
const Byte* header = nullptr;
bool contains = false;
for (unsigned i = 0; i < info->dlpi_phnum; ++i) {
const auto& ph = info->dlpi_phdr[i];
Word start = info->dlpi_addr + ph.p_vaddr;
if (ph.p_type == PT_LOAD && query.pc >= start && query.pc - start < ph.p_memsz) contains = true;
if (ph.p_type == PT_GNU_EH_FRAME) header = reinterpret_cast<const Byte*>(start);
if (ph.p_type == PT_LOAD && (ph.p_flags & PF_X)) query.text = start;
if (ph.p_type == PT_LOAD && (ph.p_flags & PF_W)) query.data = start;
}
if (!contains || !header || header[0] != 1) return 0;
const Byte* p = header + 4;
Encoded(p, header[1], Word(header));
Word count = Encoded(p, header[2]);
if (header[3] == 255) return 1;
unsigned width = EncodingSize(header[3]);
const Byte* table = p;
Word lo = 0, hi = count;
while (lo < hi) {
Word mid = lo + (hi - lo) / 2;
p = table + mid * width * 2;
Word begin = Encoded(p, header[3], Word(header));
if (begin <= query.pc) lo = mid + 1; else hi = mid;
}
if (lo) {
p = table + (lo - 1) * width * 2 + width;
query.fde = reinterpret_cast<const Byte*>(Encoded(p, header[3], Word(header)));
}
return 1;
}
struct Frame {
const Byte* cieBegin {};
const Byte* cieEnd {};
const Byte* begin {};
const Byte* end {};
Word start {}, length {}, personality {}, lsda {};
Word codeAlign {};
std::intptr_t dataAlign {};
unsigned returnRegister {};
bool signal {};
};
bool DecodeFrame(_Unwind_Context& context, Frame& frame) {
Lookup query {context.registers[16] - !context.signalFrame};
dl_iterate_phdr(FindFrame, &query);
if (!query.fde) return false;
const Byte* p = query.fde;
auto length = Read<std::uint32_t>(p);
if (!length || length == 0xffffffff) return false;
frame.end = p + length;
const Byte* ciePointer = p;
auto cieOffset = Read<std::uint32_t>(p);
const Byte* cie = ciePointer - cieOffset;
const Byte* c = cie;
auto cieLength = Read<std::uint32_t>(c);
frame.cieEnd = c + cieLength;
if (Read<std::uint32_t>(c) != 0) return false;
Byte version = *c++;
if (version != 1 && version != 3 && version != 4) return false;
const char* augmentation = reinterpret_cast<const char*>(c);
while (*c++) {}
if (version == 4 && (*c++ != sizeof(Word) || *c++ != 0)) return false;
frame.codeAlign = Uleb(c);
frame.dataAlign = Sleb(c);
frame.returnRegister = version == 1 ? *c++ : Uleb(c);
if (frame.returnRegister >= 17) return false;
Byte pointerEncoding = 0, lsdaEncoding = 255;
if (*augmentation == 'z') {
Word size = Uleb(c);
const Byte* end = c + size;
for (const char* a = augmentation + 1; *a; ++a) {
switch (*a) {
case 'R': pointerEncoding = *c++; break;
case 'L': lsdaEncoding = *c++; break;
case 'P': { Byte encoding = *c++; frame.personality = Encoded(c, encoding, query.data, 0, query.text); break; }
case 'S': frame.signal = true; break;
default: return false;
}
}
c = end;
} else if (*augmentation) return false;
frame.cieBegin = c;
frame.start = Encoded(p, pointerEncoding, query.data, 0, query.text);
frame.length = Encoded(p, pointerEncoding & 15);
if (query.pc < frame.start || query.pc - frame.start >= frame.length) return false;
if (*augmentation == 'z') {
Word size = Uleb(p);
const Byte* end = p + size;
if (lsdaEncoding != 255) frame.lsda = Encoded(p, lsdaEncoding, query.data, frame.start, query.text);
p = end;
}
frame.begin = p;
context.region = frame.start;
context.lsda = frame.lsda;
context.personality = frame.personality;
context.textBase = query.text;
context.dataBase = query.data;
return true;
}
struct Rule { unsigned kind {}; std::intptr_t value {}; const Byte* expression {}; };
struct Rules { Rule registers[17] {}; unsigned cfaRegister {7}; std::intptr_t cfaOffset {}; const Byte* cfaExpression {}; };
bool Instructions(const Byte* p, const Byte* end, const Frame& frame, Word target, Rules& state, const Rules& initial) {
Word location = frame.start;
Rules saved[16];
unsigned depth = 0;
while (p < end && location <= target) {
Byte opcode = *p++;
unsigned reg;
if ((opcode & 192) == 64) { location += (opcode & 63) * frame.codeAlign; continue; }
if ((opcode & 192) == 128) {
reg = opcode & 63;
auto offset = Uleb(p) * frame.dataAlign;
if (reg < 17) state.registers[reg] = {1, static_cast<std::intptr_t>(offset)};
continue;
}
if ((opcode & 192) == 192) {
reg = opcode & 63;
if (reg < 17) state.registers[reg] = initial.registers[reg];
continue;
}
switch (opcode) {
case 0: break;
case 1: location = Read<Word>(p); break;
case 2: location += Read<Byte>(p) * frame.codeAlign; break;
case 3: location += Read<std::uint16_t>(p) * frame.codeAlign; break;
case 4: location += Read<std::uint32_t>(p) * frame.codeAlign; break;
case 5: case 17: case 20: case 21: {
reg = Uleb(p);
auto offset = (opcode == 17 || opcode == 21 ? Sleb(p) : std::intptr_t(Uleb(p))) * frame.dataAlign;
if (reg < 17) state.registers[reg] = {unsigned(opcode >= 20 ? 4 : 1), offset};
break;
}
case 6: reg = Uleb(p); if (reg < 17) state.registers[reg] = initial.registers[reg]; break;
case 7: case 8: reg = Uleb(p); if (reg < 17) state.registers[reg] = {unsigned(opcode == 7 ? 5 : 0)}; break;
case 9: { reg = Uleb(p); auto other = Uleb(p); if (reg < 17) state.registers[reg] = {2, std::intptr_t(other)}; break; }
case 10: if (depth == 16) return false; saved[depth++] = state; break;
case 11: if (!depth) return false; state = saved[--depth]; break;
case 12: case 18:
state.cfaExpression = nullptr; state.cfaRegister = Uleb(p);
state.cfaOffset = opcode == 18 ? Sleb(p) * frame.dataAlign : Uleb(p); break;
case 13: state.cfaRegister = Uleb(p); break;
case 14: case 19: state.cfaOffset = opcode == 19 ? Sleb(p) * frame.dataAlign : Uleb(p); break;
case 15: { state.cfaExpression = p; auto size = Uleb(p); p += size; break; }
case 16: case 22: {
reg = Uleb(p); const Byte* expr = p; auto size = Uleb(p); p += size;
if (reg < 17) state.registers[reg] = {unsigned(opcode == 16 ? 3 : 6), 0, expr};
break;
}
case 46: Uleb(p); break;
default: return false;
}
}
return true;
}
bool Expression(const Byte* p, const _Unwind_Context& context, Word cfa, Word& result) {
Word length = Uleb(p);
const Byte* end = p + length;
Word stack[64]; unsigned size = 0;
unsigned steps = 0;
while (p < end && ++steps < 4096) {
Byte op = *p++;
if (size >= 62) return false;
if (op >= 48 && op <= 79) { stack[size++] = op - 48; continue; }
if (op >= 80 && op <= 96) { stack[size++] = context.registers[op - 80]; continue; }
if (op >= 112 && op <= 128) { stack[size++] = context.registers[op - 112] + Sleb(p); continue; }
switch (op) {
case 3: stack[size++] = Read<Word>(p); break;
case 6: if (!size) return false; std::memcpy(&stack[size-1], reinterpret_cast<void*>(stack[size-1]), 8); break;
case 8: stack[size++] = Read<Byte>(p); break;
case 9: stack[size++] = Read<std::int8_t>(p); break;
case 10: stack[size++] = Read<std::uint16_t>(p); break;
case 11: stack[size++] = Read<std::int16_t>(p); break;
case 12: stack[size++] = Read<std::uint32_t>(p); break;
case 13: stack[size++] = Read<std::int32_t>(p); break;
case 14: case 15: stack[size++] = Read<Word>(p); break;
case 16: stack[size++] = Uleb(p); break;
case 17: stack[size++] = Sleb(p); break;
case 18: if (!size) return false; stack[size] = stack[size-1]; ++size; break;
case 19: if (!size) return false; --size; break;
case 20: if (size < 2) return false; stack[size] = stack[size-2]; ++size; break;
case 22: if (size < 2) return false; { Word x=stack[size-1]; stack[size-1]=stack[size-2]; stack[size-2]=x; } break;
case 26: case 28: case 30: case 33: case 34: case 36: case 37: case 39: case 41: case 42: case 43: case 44: case 45: case 46: {
if (size < 2) return false;
Word b = stack[--size], &a = stack[size-1];
switch (op) {
case 26: a &= b; break; case 28: a -= b; break; case 30: a *= b; break;
case 33: a |= b; break; case 34: a += b; break; case 36: a = b < 64 ? a << b : 0; break;
case 37: a = b < 64 ? a >> b : 0; break; case 39: a ^= b; break;
case 41: a = a == b; break; case 42: a = std::intptr_t(a) >= std::intptr_t(b); break;
case 43: a = std::intptr_t(a) > std::intptr_t(b); break; case 44: a = std::intptr_t(a) <= std::intptr_t(b); break;
case 45: a = std::intptr_t(a) < std::intptr_t(b); break; case 46: a = a != b; break;
}
break;
}
case 35: if (!size) return false; stack[size-1] += Uleb(p); break;
case 146: { auto reg = Uleb(p); if (reg >= 17) return false; stack[size++] = context.registers[reg] + Sleb(p); break; }
case 150: break;
case 156: stack[size++] = cfa; break;
case 159: break;
default: return false;
}
}
if (p != end || size != 1) return false;
result = stack[0]; return true;
}
bool GetRules(_Unwind_Context& context, Frame& frame, Rules& rules) {
if (!DecodeFrame(context, frame)) return false;
Rules initial;
if (!Instructions(frame.cieBegin, frame.cieEnd, frame, ~Word(0), initial, {})) return false;
rules = initial;
if (!Instructions(frame.begin, frame.end, frame, context.registers[16] - !context.signalFrame, rules, initial)) return false;
if (rules.cfaExpression) return Expression(rules.cfaExpression, context, 0, context.cfa);
if (rules.cfaRegister >= 17) return false;
context.cfa = context.registers[rules.cfaRegister] + rules.cfaOffset;
return true;
}
bool Step(_Unwind_Context& context) {
Frame frame; Rules rules;
if (!GetRules(context, frame, rules)) return false;
_Unwind_Context next = context;
for (unsigned i = 0; i < 17; ++i) {
const auto& rule = rules.registers[i];
Word value;
switch (rule.kind) {
case 0: break;
case 1: std::memcpy(&next.registers[i], reinterpret_cast<void*>(context.cfa + rule.value), 8); break;
case 2: if (rule.value < 0 || rule.value >= 17) return false; next.registers[i] = context.registers[rule.value]; break;
case 3: case 6:
if (!Expression(rule.expression, context, context.cfa, value)) return false;
if (rule.kind == 3) std::memcpy(&value, reinterpret_cast<void*>(value), 8);
next.registers[i] = value; break;
case 4: next.registers[i] = context.cfa + rule.value; break;
case 5: next.registers[i] = 0; break;
}
}
next.registers[7] = context.cfa;
next.registers[16] = next.registers[frame.returnRegister];
next.signalFrame = frame.signal;
if (!next.registers[16] || (next.registers[7] == context.registers[7] && next.registers[16] == context.registers[16])) return false;
context = next;
return true;
}
_Unwind_Reason_Code PhaseTwo(_Unwind_Context context, _Unwind_Exception* exception, bool resume) {
bool skip = resume;
for (unsigned depth = 0; depth < 65536; ++depth) {
Frame frame; Rules rules;
if (!GetRules(context, frame, rules)) {
if (exception->private_1) {
auto stop = reinterpret_cast<_Unwind_Stop_Fn>(exception->private_1);
return stop(1, _Unwind_Action(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK), exception->exception_class, exception, &context, reinterpret_cast<void*>(exception->private_2));
}
return _URC_END_OF_STACK;
}
if (skip) {
if (context.cfa == exception->private_2 && !exception->private_1) skip = false;
else if (exception->private_1 && context.cfa == context.registers[0]) skip = false;
} else {
auto actions = _UA_CLEANUP_PHASE;
if (exception->private_1) {
actions = _Unwind_Action(actions | _UA_FORCE_UNWIND);
auto stop = reinterpret_cast<_Unwind_Stop_Fn>(exception->private_1);
auto result = stop(1, actions, exception->exception_class, exception, &context, reinterpret_cast<void*>(exception->private_2));
if (result != _URC_NO_REASON) return result;
} else if (context.cfa == exception->private_2) actions = _Unwind_Action(actions | _UA_HANDLER_FRAME);
if (frame.personality) {
auto result = __gxx_personality_v0_nid_postfix(1, actions, exception->exception_class, exception, &context);
if (result == _URC_INSTALL_CONTEXT) LibcRestoreRegisters(context.registers);
if (result != _URC_CONTINUE_UNWIND) return _URC_FATAL_PHASE2_ERROR;
}
}
if (!Step(context)) return _URC_END_OF_STACK;
}
return _URC_FATAL_PHASE2_ERROR;
}
}
extern "C" {
_Unwind_Reason_Code _Unwind_RaiseException_nid_postfix(_Unwind_Exception* exception) {
_Unwind_Context start;
LibcCaptureRegisters(start.registers);
if (!LibcUnwind::Step(start)) return _URC_FATAL_PHASE1_ERROR;
auto context = start;
exception->private_1 = 0;
for (unsigned depth = 0; depth < 65536; ++depth) {
LibcUnwind::Frame frame; LibcUnwind::Rules rules;
if (!LibcUnwind::GetRules(context, frame, rules)) return _URC_END_OF_STACK;
if (frame.personality) {
auto result = __gxx_personality_v0_nid_postfix(1, _UA_SEARCH_PHASE, exception->exception_class, exception, &context);
if (result == _URC_HANDLER_FOUND) {
exception->private_2 = context.cfa;
return LibcUnwind::PhaseTwo(start, exception, false);
}
if (result != _URC_CONTINUE_UNWIND) return _URC_FATAL_PHASE1_ERROR;
}
if (!LibcUnwind::Step(context)) return _URC_END_OF_STACK;
}
return _URC_FATAL_PHASE1_ERROR;
}
[[noreturn]] void _Unwind_Resume_nid_postfix(_Unwind_Exception* exception) {
_Unwind_Context context;
LibcCaptureRegisters(context.registers);
if (!LibcUnwind::Step(context) || !LibcUnwind::Step(context)) std::abort();
LibcUnwind::PhaseTwo(context, exception, false);
std::abort();
}
_Unwind_Reason_Code _Unwind_Resume_or_Rethrow_nid_postfix(_Unwind_Exception* exception) {
if (exception->private_1) _Unwind_Resume_nid_postfix(exception);
return _Unwind_RaiseException_nid_postfix(exception);
}
void _Unwind_DeleteException_nid_postfix(_Unwind_Exception* exception) {
if (exception && exception->exception_cleanup) exception->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, exception);
}
_Unwind_Word _Unwind_GetGR_nid_postfix(_Unwind_Context* context, int index) {
if (index < 0 || index >= 17) std::abort();
return context->registers[index];
}
void _Unwind_SetGR_nid_postfix(_Unwind_Context* context, int index, _Unwind_Word value) {
if (index < 0 || index >= 17) std::abort();
context->registers[index] = value;
}
_Unwind_Ptr _Unwind_GetIP_nid_postfix(_Unwind_Context* context) { return context->registers[16]; }
void _Unwind_SetIP_nid_postfix(_Unwind_Context* context, _Unwind_Ptr value) { context->registers[16] = value; }
_Unwind_Ptr _Unwind_GetIPInfo_nid_postfix(_Unwind_Context* context, int* before) { if (before) *before = context->signalFrame; return context->registers[16]; }
_Unwind_Word _Unwind_GetCFA_nid_postfix(_Unwind_Context* context) { return context->cfa; }
_Unwind_Ptr _Unwind_GetLanguageSpecificData_nid_postfix(_Unwind_Context* context) { return context->lsda; }
_Unwind_Ptr _Unwind_GetRegionStart_nid_postfix(_Unwind_Context* context) { return context->region; }
_Unwind_Ptr _Unwind_GetDataRelBase_nid_postfix(_Unwind_Context* context) { return context->dataBase; }
_Unwind_Ptr _Unwind_GetTextRelBase_nid_postfix(_Unwind_Context* context) { return context->textBase; }
_Unwind_Reason_Code _Unwind_ForcedUnwind_nid_postfix(_Unwind_Exception* exception, _Unwind_Stop_Fn stop, void* argument) {
if (!stop) return _URC_FATAL_PHASE2_ERROR;
_Unwind_Context context;
LibcCaptureRegisters(context.registers);
if (!LibcUnwind::Step(context)) return _URC_FATAL_PHASE2_ERROR;
exception->private_1 = reinterpret_cast<std::uintptr_t>(stop);
exception->private_2 = reinterpret_cast<std::uintptr_t>(argument);
return LibcUnwind::PhaseTwo(context, exception, false);
}
_Unwind_Reason_Code _Unwind_Backtrace_nid_postfix(_Unwind_Trace_Fn trace, void* argument) {
_Unwind_Context context;
LibcCaptureRegisters(context.registers);
if (!LibcUnwind::Step(context)) return _URC_END_OF_STACK;
for (unsigned depth = 0; depth < 65536; ++depth) {
LibcUnwind::Frame frame; LibcUnwind::Rules rules;
if (!LibcUnwind::GetRules(context, frame, rules)) return _URC_END_OF_STACK;
auto result = trace(&context, argument);
if (result != _URC_NO_REASON) return result;
if (!LibcUnwind::Step(context)) return _URC_END_OF_STACK;
}
return _URC_FATAL_PHASE1_ERROR;
}
int dl_iterate_phdr_nid_postfix(int (*callback)(dl_phdr_info*, std::size_t, void*), void* data) { return dl_iterate_phdr(callback, data); }
}
+100
View File
@@ -0,0 +1,100 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <unwind.h>
struct _Unwind_Context {
std::uintptr_t registers[17] {};
std::uintptr_t cfa {};
std::uintptr_t region {};
std::uintptr_t lsda {};
std::uintptr_t textBase {};
std::uintptr_t dataBase {};
std::uintptr_t personality {};
bool signalFrame {};
};
namespace LibcUnwind {
using Byte = unsigned char;
using Word = std::uintptr_t;
template<class T> T Read(const Byte*& p) {
T value;
std::memcpy(&value, p, sizeof(value));
p += sizeof(value);
return value;
}
inline Word Uleb(const Byte*& p) {
Word result = 0;
for (unsigned shift = 0; shift < 64; shift += 7) {
Byte b = *p++;
result |= Word(b & 127) << shift;
if (!(b & 128)) return result;
}
std::abort();
}
inline std::intptr_t Sleb(const Byte*& p) {
Word result = 0;
unsigned shift = 0;
Byte b;
do {
if (shift >= 64) std::abort();
b = *p++;
result |= Word(b & 127) << shift;
shift += 7;
} while (b & 128);
if (shift < 64 && (b & 64)) result |= (~Word(0)) << shift;
return static_cast<std::intptr_t>(result);
}
inline Word Encoded(const Byte*& p, Byte encoding, Word data = 0, Word function = 0, Word text = 0) {
if (encoding == 255) return 0;
if ((encoding & 112) == 80) p = reinterpret_cast<const Byte*>((Word(p) + 7) & ~Word(7));
Word location = Word(p), value;
switch (encoding & 15) {
case 0: value = Read<Word>(p); break;
case 1: value = Uleb(p); break;
case 2: value = Read<std::uint16_t>(p); break;
case 3: value = Read<std::uint32_t>(p); break;
case 4: value = Read<std::uint64_t>(p); break;
case 9: value = Sleb(p); break;
case 10: value = Read<std::int16_t>(p); break;
case 11: value = Read<std::int32_t>(p); break;
case 12: value = Read<std::int64_t>(p); break;
default: std::abort();
}
if (!value) return 0;
switch (encoding & 112) {
case 0: case 80: break;
case 16: value += location; break;
case 32: value += text; break;
case 48: value += data; break;
case 64: value += function; break;
default: std::abort();
}
if (encoding & 128) std::memcpy(&value, reinterpret_cast<void*>(value), sizeof(value));
return value;
}
inline unsigned EncodingSize(Byte encoding) {
switch (encoding & 15) {
case 0: case 4: case 12: return 8;
case 2: case 10: return 2;
case 3: case 11: return 4;
default: std::abort();
}
}
}
extern "C" {
_Unwind_Reason_Code __gxx_personality_v0_nid_postfix(int, _Unwind_Action, std::uint64_t, _Unwind_Exception*, _Unwind_Context*);
_Unwind_Reason_Code _Unwind_RaiseException_nid_postfix(_Unwind_Exception*);
[[noreturn]] void _Unwind_Resume_nid_postfix(_Unwind_Exception*);
_Unwind_Reason_Code _Unwind_Resume_or_Rethrow_nid_postfix(_Unwind_Exception*);
void _Unwind_DeleteException_nid_postfix(_Unwind_Exception*);
}