首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用LC_LOAD_DYLIB加载的动态库实现C语言函数

利用LC_LOAD_DYLIB加载的动态库实现C语言函数
EN

Stack Overflow用户
提问于 2013-02-04 19:44:21
回答 1查看 1.8K关注 0票数 3

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

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

代码语言:javascript
复制
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“。但是,我不明白,所以插入可能失败了。

我想知道可能是什么原因。这只是为了学习目的的内部开发,所以请不要提到影响或警告我不适当的使用。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-10 16:15:51

dyld source

代码语言:javascript
复制
// 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插入的库才会应用它们的插入。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14686074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档