LD_PRELOAD和ld --wrap

Foreword

LD_PRELOAD and ld --wrap can be achieved without modifying the original code, replacing achieve the specified function. We typically use these methods to replace such as malloc) () / free (), read () / write () like function, and do some recorded in alternate functions, in order to analyze memory allocated during program execution and IO situation. These functions are usually called wrapper function.

LD_PRELAD

The linker checks the LD_PRELOAD environment variable, if not empty, then the priority use of symbols LD_PRELOAD specified dynamic library to malloc () / free (), for example.

Listing main.c

int main()
{
    malloc(10);
    return 0;
}

Listing my.c

void* malloc(size_t size)
{
    void (*__real_malloc)(int) = NULL;
    // 获取真实的malloc()地址
    __real_malloc = dlsym(RTLD_NEXT, "malloc");
    void* ptr =  __real_malloc(size);
    printf("[malloc] addr=%p, size=%u\n", ret, size);
    return ptr;
}

void free(void* ptr)
{
    printf("[free] addr=%p", ptr);
    void (*__real_free)(void*) = NULL;
    __real_free = dlsym(RTLD_NEXT, "free");
    return __real_free(ptr);
}

gcc main.c -o testThe main.c compiled test, gcc -fPIC -shared my.c -o libmy.somy.c compiled libmy.so.

export LD_PRELOAD=./libmy.soAfter execution testyou will find main()performed malloc()actually libmy.sois malloc(), which outputs the memory address and size distribution. Because there is no free output, so we can see that there is a memory leak test. This is a very simple example.

export LD_PRELOAD so that all procedures are performed by the entry into force of the terminal, do not use will also be required unset LD_PRELOAD, we can LD_PRELOAD=./libmy.so ./testmake it take effect only on the test.

LD_PRELOAD nature is to load the first use. As we found at compile time if the two have the same name as the library function, then what the former library, which library to function with.

ld --wrap

ld --wrap parameter is a compilation of links, the principle is specified at compile time --wrap function name to be replaced, replace it with the rule is to replace the rule is applied before wrapping function __wrap_, plus former real function __real_, for example gcc main.c -o test -Wl,--wrap=malloc, the compiler We will call malloc()places replaced __wrap_malloc(), and the __real_malloc()symbol name is mapped to the real malloc () address.

At this point my.c implementation is not the same, we need to provide __wrap_malloc(), and really need to call malloc()to use local __real_malloc().

void* __wrap_malloc(size_t size)
{
    void* ptr =  __real_malloc(size);
    printf("[malloc] addr=%p, size=%u\n", ret, size);
    return ptr;
}

void __wrap_free(void* ptr)
{
    printf("[free] addr=%p", ptr);
    return __real_free(ptr);
}

If there are more --wrap, you can put it in a file, assuming the file name ld-opt, which reads as follows

--wrap=malloc
--wrap=free

Use gcc main.c -o test -Wl,@ld-optwill be able to replace the malloc and free.

Ld --wrap essence is to replace the symbolic name (malloc -> __ wrap_malloc) specified by the rule and executing code associated with a particular symbol (__real_malloc-> maloc code).

Guess you like

Origin www.cnblogs.com/yizui/p/11368082.html