From f808a0fbde62245f01a551cc19c4802b25d50174 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Wed, 7 Feb 2018 12:24:42 +0000 Subject: [PATCH] devirtualize: Devirtualize Itanium ABI MFPs at runtime --- src/backend_x64/devirtualize.h | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/backend_x64/devirtualize.h b/src/backend_x64/devirtualize.h index e7714f11..4636884a 100644 --- a/src/backend_x64/devirtualize.h +++ b/src/backend_x64/devirtualize.h @@ -6,9 +6,12 @@ #pragma once +#include #include #include "backend_x64/callback.h" +#include "common/assert.h" +#include "common/cast_util.h" #include "common/common_types.h" #include "common/mp.h" @@ -30,11 +33,37 @@ struct ThunkBuilder { } // namespace impl template -ArgCallback Devirtualize(mp::class_type_t* this_) { +ArgCallback DevirtualizeGeneric(mp::class_type_t* this_) { return ArgCallback{&impl::ThunkBuilder::Thunk, reinterpret_cast(this_)}; } -#define DEVIRT(this_, mfp) Dynarmic::BackendX64::Devirtualize(this_) +template +ArgCallback DevirtualizeItanium(mp::class_type_t* this_) { + struct MemberFunctionPointer { + /// For a non-virtual function, this is a simple function pointer. + /// For a virtual function, it is (1 + virtual table offset in bytes). + u64 ptr; + /// The required adjustment to `this`, prior to the call. + u64 adj; + } mfp_struct = Common::BitCast(mfp); + + static_assert(sizeof(MemberFunctionPointer) == 16); + static_assert(sizeof(MemberFunctionPointer) == sizeof(mfp)); + + u64 fn_ptr = mfp_struct.ptr; + u64 this_ptr = reinterpret_cast(this_) + mfp_struct.adj; + if (mfp_struct.ptr & 1) { + u64 vtable = Common::BitCastPointee(this_ptr); + fn_ptr = Common::BitCastPointee(vtable + fn_ptr - 1); + } + return ArgCallback{fn_ptr, this_ptr}; +} + +#if defined(__APPLE__) || defined(linux) || defined(__linux) || defined(__linux__) +#define DEVIRT(this_, mfp) Dynarmic::BackendX64::DevirtualizeItanium(this_) +#else +#define DEVIRT(this_, mfp) Dynarmic::BackendX64::DevirtualizeGeneric(this_) +#endif } // namespace BackendX64 } // namespace Dynarmic