Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- DON'T TELL I DID IT PUBLIC LICENCE
- Everyone is permitted to copy and distribute verbatim copies of this licence document,
- and changing it is allowed as long as the name is changed.
- DON'T TELL I DID IT PUBLIC LICENCE
- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- 0. You may copy, modify and distribute this program under source form and binary form
- as long as you DO NOT include the name of the original author with it.
- 1. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. THE
- PROGRAM IS PROVIDED βAS ISβ WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
- IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
- SERVICING, REPAIR OR CORRECTION.
- */
- #include <dlfcn.h>
- template<typename T>
- const void* voidify(T method)
- {
- void* pointer;
- asm ("movq %rdi, -8(%rbp)");
- return pointer;
- }
- template<typename T, typename M>
- const char* getMethodName(const T* object, M method)
- {
- Dl_info info;
- const void* methodPointer;
- if (sizeof method == 8) methodPointer = voidify(method); // actual method pointer
- else // vtable entry
- {
- struct VTableEntry
- {
- unsigned long long __pfn;
- unsigned long long __delta;
- };
- const VTableEntry* entry = static_cast<const VTableEntry*>(voidify(&method));
- const void* const* vtable = *(const void* const* const* const)object;
- methodPointer = vtable[(entry->__pfn - 1) / sizeof(void*)];
- }
- if (dladdr(voidify(methodPointer), &info))
- return info.dli_sname;
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement