我正在寻找一个c库/框架,它允许我替换内存中的函数并将它们重定向到我自己的实现,同时仍然允许我的实现调用原始实现。
这在linux-y系统上似乎是一个相当罕见的需求,可能是因为ld_preload涵盖了运行时函数替换thingies的大部分方面。

最佳答案

下面的方法似乎适用于我的应用程序。我不喜欢我的机器上有专利的斑点,所以我不知道它是否和蒸汽一起工作。不过,我很想知道,我看不出为什么不应该。
以下方法使用_dl_vsym()在执行dlsym()之前从dlvsym()中查找正确版本的libdl.somain()。在应用程序执行期间,插入的dlsym()dlvsym()调用它们的原始版本(而不是_dl_vsym());我认为这应该避免任何特定于应用程序的问题。
如果其他动态库在此之前初始化,则使用这些函数的非常小心的初始版本。它们使用_dl_vsym()获取对libdldlsym()dlvsym()函数的引用;任何后续调用都将使用libdldlsym()dlvsym()。这将脆弱的时间限制在库初始化期间的第一次调用,但是优先级101希望首先初始化这个库。

#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <string.h>
#include <GL/glx.h>
#include <EGL/egl.h>

#define UNUSED __attribute__((unused))

#define LIBDL_VERSION "GLIBC_2.2.5"
#define LIBDL_PATH    "libdl.so"

extern void *_dl_vsym(void *, const char *, const char *, void *);

static const struct {
    const char *const symbol;
    const char *const version;
    void *const       function;
} interposed[] = {
    { "dlsym",          LIBDL_VERSION,   dlsym  },
    { "dlvsym",         LIBDL_VERSION,   dlvsym },
    { "glXSwapBuffers", (const char *)0, glXSwapBuffers },
    { "eglSwapBuffers", (const char *)0, eglSwapBuffers },
    { (const char *)0,  (const char *)0, (void *)0 }
};

static void *       initial_dlsym(void *, const char *);
static void *       initial_dlvsym(void *, const char *, const char *);
static void         initial_glXSwapBuffers(Display *, GLXDrawable);
static EGLBoolean   initial_eglSwapBuffers(EGLDisplay, EGLSurface);

static void *     (*actual_dlsym)(void *, const char *)                = initial_dlsym;
static void *     (*actual_dlvsym)(void *, const char *, const char *) = initial_dlvsym;
static void       (*actual_glXSwapBuffers)(Display *, GLXDrawable)     = initial_glXSwapBuffers;
static EGLBoolean (*actual_eglSwapBuffers)(EGLDisplay, EGLSurface)     = initial_eglSwapBuffers;

static void initial_glXSwapBuffers(Display *display UNUSED, GLXDrawable drawable UNUSED)
{
    return;
}

static EGLBoolean initial_eglSwapBuffers(EGLDisplay display UNUSED, EGLSurface surface UNUSED)
{
    return 0;
}

static void *initial_dlsym(void *handle, const char *const symbol)
{
    void *(*call_dlsym)(void *, const char *);

    if (symbol) {
        size_t i;
        for (i = 0; interposed[i].symbol; i++)
            if (!strcmp(symbol, interposed[i].symbol))
                return interposed[i].function;
    }

    *(void **)(&call_dlsym) = __atomic_load_n((void **)(&actual_dlsym), __ATOMIC_SEQ_CST);
    if (!call_dlsym || call_dlsym == initial_dlsym) {
        const int saved_errno = errno;
        void     *handle;

        handle = dlopen(LIBDL_PATH, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND | RTLD_NODELETE);
        call_dlsym = _dl_vsym(handle, "dlsym", LIBDL_VERSION, dlsym);
        dlclose(handle);

        if (!call_dlsym || call_dlsym == initial_dlsym || call_dlsym == dlsym) {
            errno = saved_errno;
            return (void *)0;
        }

        __atomic_store_n((void **)(&actual_dlsym), call_dlsym, __ATOMIC_SEQ_CST);
        errno = saved_errno;
    }

    return call_dlsym(handle, symbol);
}

static void *initial_dlvsym(void *handle, const char *const symbol, const char *const version)
{
    void *(*call_dlvsym)(void *, const char *, const char *);

    if (symbol) {
        size_t i;
        for (i = 0; interposed[i].symbol; i++)
            if (!strcmp(symbol, interposed[i].symbol))
                if (!interposed[i].version || !version || !strcmp(version, interposed[i].version))
                    return interposed[i].function;
    }

    *(void **)(&call_dlvsym) = __atomic_load_n((void **)(&actual_dlvsym), __ATOMIC_SEQ_CST);
    if (!call_dlvsym || call_dlvsym == initial_dlvsym) {
        const int saved_errno = errno;
        void     *handle;

        handle = dlopen(LIBDL_PATH, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND | RTLD_NODELETE);
        call_dlvsym = _dl_vsym(handle, "dlvsym", LIBDL_VERSION, dlvsym);
        dlclose(handle);

        if (!call_dlvsym || call_dlvsym == initial_dlvsym || call_dlvsym == dlvsym) {
            errno = saved_errno;
            return (void *)0;
        }

        __atomic_store_n((void **)(&actual_dlvsym), call_dlvsym, __ATOMIC_SEQ_CST);
        errno = saved_errno;
    }

    return call_dlvsym(handle, symbol, version);
}

void *dlsym(void *handle, const char *const symbol)
{
    if (symbol) {
        size_t i;
        for (i = 0; interposed[i].symbol; i++)
            if (!strcmp(symbol, interposed[i].symbol))
                return interposed[i].function;
    }
    return actual_dlsym(handle, symbol);
}

void *dlvsym(void *handle, const char *const symbol, const char *version)
{
    if (symbol) {
        size_t i;
        for (i = 0; interposed[i].symbol; i++)
            if (!strcmp(symbol, interposed[i].symbol))
                if (!interposed[i].version || !version || !strcmp(version, interposed[i].version))
                    return interposed[i].function;
    }
    return actual_dlvsym(handle, symbol, version);
}

static void init(void) __attribute__((constructor (101)));
static void init(void)
{
    int    saved_errno;
    void  *handle;

    saved_errno = errno;

    handle = dlopen(LIBDL_PATH, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND | RTLD_NODELETE);
    __atomic_store_n((void **)(&actual_dlsym),  _dl_vsym(handle, "dlsym",  LIBDL_VERSION, dlsym),  __ATOMIC_SEQ_CST);
    __atomic_store_n((void **)(&actual_dlvsym), _dl_vsym(handle, "dlvsym", LIBDL_VERSION, dlvsym), __ATOMIC_SEQ_CST);
    dlclose(handle);

    __atomic_store_n((void **)(&actual_glXSwapBuffers), actual_dlsym(RTLD_NEXT, "glXSwapBuffers"), __ATOMIC_SEQ_CST);
    __atomic_store_n((void **)(&actual_eglSwapBuffers), actual_dlsym(RTLD_NEXT, "eglSwapBuffers"), __ATOMIC_SEQ_CST);

    errno = saved_errno;
}

void glXSwapBuffers(Display *dpy, GLXDrawable drawable)
{
    /* TODO: Custom stuff before glXSwapBuffers() */
    actual_glXSwapBuffers(dpy, drawable);
    /* TODO: Custom stuff after glXSwapBuffers() */
}

EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
{
    EGLBoolean result;
    /* TODO: Custom stuff before eglSwapBuffers() */
    result = actual_eglSwapBuffers(dpy, surface);
    /* TODO: Custom stuff after eglSwapBuffers() */
    return result;
}

如果将上面的代码保存为example.c,则可以使用
gcc -Wall -fPIC -shared `pkg-config --cflags gl egl` example.c -ldl -Wl,-soname,libexample.so `pkg-config --libs gl egl` -o libexample.so

在某些情况下,您需要修改libexample.so。使用
find /lib* /usr/ -name 'libdl.*' | while read FILE ; do echo "$FILE:" ; readelf -s "$FILE" | sed -ne '/ dlsym@/ s|^.*@@*|\t|p' ; done

检查libdl使用的api版本。(我看到过LIBDL_VERSIONGLIBC_2.0;它并不反映库的实际版本,而是GLIBC_2.2.5dlsym()调用的api版本。)
dlvsym()数组包含插入函数的修改结果。
我已经验证了上面的示例不会与我尝试过的任何应用程序(包括我编写的一个简单的interposed[]dlsym()压力测试)崩溃,并且它也正确地插入了dlvsym()(在glXSwapBuffers()glxgears中)。
问题?评论?

关于c - Hook 框架(类似于Detours),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26897350/

10-11 17:33