3

首先,我想做的是拦截iOS应用程序的任意标准C函数(如fopen、read、write、malloc等)。

我有一个带有以下代码的 libtest.dylib:

typedef struct interpose_s {
    void *new_func;
    void *orig_func;
} interpose_t;


FILE *vg_fopen(const char * __restrict, const char * __restrict);

static const interpose_t interposing_functions[] \
__attribute__ ((section("__DATA, __interpose"))) = {
    { (void *)vg_fopen, (void *)fopen },
};

FILE *vg_fopen(const char * __restrict path, const char * __restrict mode) {
    printf("vg_fopen");
    return fopen(path, mode);
}

编译完 dylib 后,我转到主机 iOS 应用程序的二进制文件并将 LC_LOAD_DYLIB 添加到 LC_LOAD_COMMANDS 列表的末尾并将其指向 @executable_path/libtest.dylib

我期望它会覆盖 fopen 的实现,并在调用 fopen 时打印“vg_fopen”。但是,我不明白,所以插入可能失败了。

我想知道可能是什么原因。这仅用于内部开发,仅用于学习目的,因此请不要提及影响或警告我不当使用。

提前致谢。

4

1 回答 1

3

dyld来源

// link any inserted libraries
// do this after linking main executable so that any dylibs pulled in by inserted 
// dylibs (e.g. libSystem) will not be in front of dylibs the program uses
if ( sInsertedDylibCount > 0 ) {
    for(unsigned int i=0; i < sInsertedDylibCount; ++i) {
        ImageLoader* image = sAllImages[i+1];
        link(image, sEnv.DYLD_BIND_AT_LAUNCH, ImageLoader::RPathChain(NULL, NULL));
        // only INSERTED libraries can interpose
        image->registerInterposing();
    }
}

所以不,只有通过插入的库才DYLD_INSERT_LIBRARIES应用了它们的插入。

于 2013-04-10T08:15:51.210 回答