OS x Dynamic Library implements function interception function

Source: Internet
Author: User

In OS x systems, only a small number of processes are required by the kernel loader, and almost all programs are dynamically connected, often using/USR/LIB/DYLD as a dynamic linker.
As a private loader, DYLD provides some unique features, such as function interception. The Dyld_interpose macro definition allows a library to replace its function implementation with another function implementation. The following code, taken from the source code of DYLD, demonstrates this feature.

#if !defined(_DYLD_INTERPOSING_H_)#define _DYLD_INTERPOSING_H_#define DYLD_INTERPOSE(_replacment,_replacee) \ __attribute__((used)) static strut{const void* replacment;const void* replacee;}_interpose_##_replace \ __attribute__((section ("__DATA,__interpose"))) = { (constvoidlong)&_replacement, (constvoidlong)&_replacee};#endif

The function interception function of DYLD provides a new __data area called __interpose, in which the replacement function and the replaced function are listed in sequence, and the other things are referred to Dyld.
Below is an experiment showing how to track the malloc () function through a function intercept mechanism

#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <stdlib.h>#include <malloc/malloc.h>//Standard interpose data structuretypedef structinterpose_s{void*new_func;void*ORIG_FNC;} interpose_t;//Our prototypesvoid*my_malloc (intsize);//corresponds to the real malloc functionvoidMy_free (void*);//corresponds to the real free functionStatic Constinterpose_t interposing_functions[] __attribute__ (section ("__data,__interpose"))) = {{(void*) My_free, (void*) Free},{(void*) My_malloc, (void*)malloc}};void*my_malloc (intSize) {//In our function, to access the real malloc () function, because we do not want to manage the entire heap ourselves, so we call the original malloc ()    void*returned =malloc(size);//Call malloc_printf because malloc () is called in printf, resulting in an infinite recursive call. malloc_printf ("+%p%d\n", returned,size);return(returned);}voidMy_free (void*freed) {malloc_printf ("-%p\n", freed); Free(freed);}intMainintargcConst Char* argv[]) {//Release memory--print out the address and then call the true free ()    printf("Hello, world!\n.");return 0;}

Execute the following code in the terminal to compile to dylib and force insert in LS

-dynamiclib1.-o libMTrace.-WallDYLD_INSERT_LIBRARIES=libMTrace.dylib ls

Eventually it will be found that the place where malloc is called will print such information

ls(243460x10010002088ls(243460x1008000004096ls(243460x1008010002160ls(243460x100800000ls(243460x100801a003312

OS x Dynamic Library implements function interception function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.