2013-02-04 25 views
3

首先,我想要做的是攔截iOS應用程序的任意標準C函數(如fopen,read,write,malloc,...)。使用由LC_LOAD_DYLIB加載的動態庫來設置C函數

我有這個代碼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的實現,並打印「vg_fopen」每當調用fopen。但是,我不明白,所以介入可能失敗了。

我想知道可能是什麼原因。這僅適用於內部開發,僅供學習之用,所以請不要提及其影響或警告我不適當的使用。

在此先感謝。

+1

如果你有答案,你應該回答你自己的問題:) –

+1

@Krypton,你爲什麼不張貼這個答案,並接受它自己。這應該有助於未來的觀衆。 – iDev

+0

@Krypton如果您已經找到了在iOS中設置符號的方法,請發佈它。 –

回答

3

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插入庫有其插入應用。