기능

2013-10-31 3 views
1

나는 그들이기능

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

해당 페이지

#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: 

[email protected](~)$ gcc -dynamiclib l.c -o libMTrace.dylib -Wall // compile to dylib 
[email protected](~)$ 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 버전 또는 여기에 작성된 코드? 그것은 아무 것도 가로 채지 않는 것처럼 보였습니다.

답변

1

이것은 매버릭스의 기능이 아니며, clang의 기능입니다. 같은 웹 사이트에서 jtool을 사용하면 생성 된 dylib에 _ DATA가 없음을 알 수 있습니다. _interpose : DYLD가 삽입 마술을 작동시키는 데 필요합니다.

덧붙여서이 질문은 해당 도서의 자체 포럼에서 확인하는 것이 가장 좋습니다. 그게 아마도 거기에있는 것이다.

3

추가 interposing_functions 정의하기 전에 속성 ((사용되는)), 그것은 다음과 같이 작동합니다 :

// Note: Compile with GCC, not cc (important) 
 
// 
 
// 
 
// This is the expected interpose structure 
 
typedef struct interpose_s { void *new_func; 
 
\t \t \t  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) { 
 
....