2010-09-29 42 views
1

我已成功使用dyld的-macosx-夾着標準C函數給第三方應用程序,獲取有關它的解決方法的重要信息類的功能。但我真正需要的是取代某個班級的某個功能。使用dyld的插入夾着

我要重寫的功能是:: QString的追加(...,...,...),所以每一個字符串追加到另一個-the整個應用程序時使用qstring-,我找出來。

有沒有辦法?這是我已有的代碼。

// libinterposers.c 
#include <stdio.h> 
#include <stdint.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <stdarg.h> 
#include <dlfcn.h> 
#include <stdlib.h> 

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

int my_open(const char *, int, mode_t); 
int my_close(int); 
void* my_malloc(size_t); 

static const interpose_t interposers[] \ 
    __attribute__ ((section("__DATA, __interpose"))) = { 
     { (void *)my_open, (void *)open }, 
     { (void *)my_close, (void *)close }, 
     { (void *)my_malloc, (void *)malloc }, 
    }; 

int 
my_open(const char *path, int flags, mode_t mode) 
{ 
    int ret = open(path, flags, mode); 
    printf("--> %d = open(%s, %x, %x)\n", ret, path, flags, mode); 
    return ret; 
} 

int 
my_close(int d) 
{ 
    int ret = close(d); 
    printf("--> %d = close(%d)\n", ret, d); 
    return ret; 
} 

void* 
my_malloc(size_t size) 
{ 
    void *ret = malloc(size); 
    //fprintf(stderr, "Reserva de memoria"); 
    return ret; 
} 

非常感謝您

回答

3

C++做name mangling。這意味着鏈接器的成員函數QString::mid()看起來像__ZNK7QString3midEii。在要插入的庫上運行nm(1)命令以查看符號。

+1

+1準確無誤。使用* nm *查找要替換的方法的正確符號,然後在您正在構建的共享庫上使用該確切名稱創建一個C函數。 – karlphillip 2010-09-29 17:35:48

+0

我會盡快回家。該函數位於應用程序目錄中的一個包中。這是一個* .bundle文件。如果符號存在於主應用程序的.bundle和and中,應該如何處理?Ã – flaab 2010-09-29 18:22:05

+0

謝謝您的回答。 Karlphillip:一旦知道確切的符號,我是否應該使用上面的代碼來干涉他們? – flaab 2010-09-29 18:22:36

0

這將是這容易得多。 QString使用memcpy連接並使用Strings,我可以輕鬆地覆蓋memcpy,並將正則表達式應用於結果,只記錄我想要的字符串。小菜一碟。無需魔法voodo-hoodo :)