在OSX上如何挂钩C++方法?

7
我正在为某些应用程序注入dylib以实现所需的行为。我能够正确地挂接扁平的C API。一旦我注入了dylib,我会查看符号表并使用我的函数地址更新其条目,然后调用原始函数。
因此,符号名称对我很重要。
我的问题是C++名称修饰。我们如何挂接名称已被修改的C++函数?我在 stack overflow 上读到过一些地方,说可以使用 mach_override 挂接 C++ 代码,但没有示例或引用。
有人可以给我一个关于如何实现 C++ 挂钩的示例吗?
编辑: 我使用 $ c++filt -n _ZN10WindowData12GetCGContextEv 作为示例,输出如下 WindowData::GetCGContext() 1. WindowData 是类还是命名空间? 2. 我该如何挂接它?我可能需要一些前向声明和外部声明来正确使用 WindowData::GetCGContext() 进行挂接。
类似这样...
typedef void* (*WindowData_GetCGContextProcPtr)(void* inObj);
static WindowData_GetCGContextProcPtr gWindowDataGetCGContextProcPtr = NULL;
void*  try_WindowDataGetCGContextProcPtr(void* inObj)
{
    printf("try_WindowDataGetCGContextProcPtr \n");

    return gWindowDataGetCGContextProcPtr(inObj);
}

现在,我想修补这个方法。
gWindowDataGetCGContextProcPtr  = (WindowData_GetCGContextProcPtr)Patch((const void*)&WindowData::GetCGContext, (const void*)&try_WindowDataGetCGContextProcPtr);

这个补丁会导致编译错误。

error: 'WindowData' has not been declared
error: 'GetCGContext' was not declared in this scope

我该如何解决这个问题?
2个回答

4
你可以使用c++filt来解码名称修饰并进行注入。
但是,由优化器内联的方法将无法使用此方法。

0
在OS X上,假设您正在使用gcc,则可以使用中的abi::__cxa_demangle函数来解开C++名称。您不需要这个来分配一个指向匹配以下签名的方法的指针:WindowData::GetCGContext。但是,仍需要提供此类的规范。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接