在OSX上挂钩C++方法?

RLT*_*RLT 7 c c++ macos hook code-injection

我正在某些应用程序中注入一个dylib以获得某些期望的行为.

我能够正确地连接平面C API.一旦我注入了dylib,我会在符号表中查看并使用我的函数地址更新其条目,然后调用原始函数.

因此,符号名称对我来说很重要.

我的问题是C++名称修改.我们如何挂钩名称已被破坏的C++函数.我读了堆栈溢出的一些地方,可以用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);
}
Run Code Online (Sandbox Code Playgroud)

现在,我想要修补此方法.

gWindowDataGetCGContextProcPtr  = (WindowData_GetCGContextProcPtr)Patch((const void*)&WindowData::GetCGContext, (const void*)&try_WindowDataGetCGContextProcPtr);
Run Code Online (Sandbox Code Playgroud)

此修补程序提供编译错误.

error: 'WindowData' has not been declared
error: 'GetCGContext' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

我是怎么解决的?

par*_*mar 4

您可以使用c++filt解码损坏并进行注入。

但是优化器内联的方法不适用于此方法