首先,我想要做的是截取任意的标准C函数(如fopen、read、write、malloc等)。iOS应用程序的。
我有一个包含以下代码的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“。但是,我不明白,所以插入可能失败了。
我想知道可能是什么原因。这只是为了学习目的的内部开发,所以请不要提到影响或警告我不适当的使用。
提前谢谢。
发布于 2013-04-10 16:15:51
从dyld source
// 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插入的库才会应用它们的插入。
https://stackoverflow.com/questions/14686074
复制相似问题