首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OSX小牛的函数插入

OSX小牛的函数插入
EN

Stack Overflow用户
提问于 2013-10-31 01:36:01
回答 2查看 1.4K关注 0票数 1

我完全按照这个网站上的指示行事。

http://www.newosxbook.com/src.jl?tree=listings&file=4-5-interpose.c

这是那一页的代码

代码语言:javascript
复制
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <malloc/malloc.h> // for malloc_printf()

// Note: Compile with GCC, not cc (important)
//
//
// This is the expected interpose structure
 typedef struct interpose_s { void *new_func;
                   void *orig_func; } interpose_t;
// Our prototypes - requires since we are putting them in 
//  the interposing_functions, below

void *my_malloc(int size); // matches real malloc()
void my_free (void *); // matches real free()

static const interpose_t interposing_functions[] \ 
    __attribute__ ((section("__DATA, __interpose"))) = {

 { (void *)my_free, (void *)free },
 { (void *)my_malloc, (void *)malloc } 

};

void *
my_malloc (int size) {
 // In our function we have access to the real malloc() -
 // and since we don’t want to mess with the heap ourselves,
 // just call it
 //
void *returned = malloc(size);
// call malloc_printf() because the real printf() calls malloc()
// // internally - and would end up calling us, recursing ad infinitum

  malloc_printf ( "+ %p %d\n", returned, size); return (returned);
}
void
my_free (void *freed) {
// Free - just print the address, then call the real free()


  malloc_printf ( "- %p\n", freed); free(freed);
}



#if 0
  From output 4-11:

 morpheus@Ergo(~)$ gcc -dynamiclib l.c -o libMTrace.dylib -Wall  // compile to dylib
 morpheus@Ergo(~)$ DYLD_INSERT_LIBRARIES=libMTrace.dylib ls     // force insert into ls
 ls(24346) malloc: + 0x100100020 88
 ls(24346) malloc: + 0x100800000 4096
 ls(24346) malloc: + 0x100801000 2160 
 ls(24346) malloc: - 0x100800000 
 ls(24346) malloc: + 0x100801a00 3312 ... // etc.

#endif

OSX的最新版本或这里编写的代码有什么不同吗?它似乎没有拦截任何东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-01 19:53:57

这不是小牛的特征,它是小牛的特征。如果您在同一个网站上使用jtool,您将看到生成的dylib没有_DATA._interpose,这是DYLD使用插入魔术所必需的。

顺便提一句,这个问题最好在那本书的论坛上提出。可能就是为了这个。

票数 1
EN

Stack Overflow用户

发布于 2015-01-05 15:38:28

attribute((used))定义之前添加interposing_functions,它将起作用,如下所示:

代码语言:javascript
复制
// Note: Compile with GCC, not cc (important)
//
//
// This is the expected interpose structure
 typedef struct interpose_s { void *new_func;
			       void *orig_func; } interpose_t;
// Our prototypes - requires since we are putting them in 
//  the interposing_functions, below

void *my_malloc(int size); // matches real malloc()
void my_free (void *); // matches real free()

__attribute__((used)) static const interpose_t interposing_functions[] \
    __attribute__ ((section("__DATA, __interpose"))) = {

 { (void *)my_free, (void *)free },
 { (void *)my_malloc, (void *)malloc } 

};

void *
my_malloc (int size) {
....

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

https://stackoverflow.com/questions/19696940

复制
相关文章

相似问题

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