Zablokuj standardową funkcję w bibliotece i wywołaj funkcję natywnej biblioteki

Próbuję najpierw włamać się do funkcji malloc, aby wywołać moją funkcję malloc. Kiedy moja funkcja malloc zostanie wykonana w tym miejscu, chcę wywołać standardowy malloc. Ale otrzymuję rekursję, ponieważ ładuje tylko mój zdefiniowany malloc. Jak mogę naprawić poniższy kod?

#include <dlfcn.h>
#include "stdio.h"
//#include "stdlib.h"


void *handle;

void *handle_malloc;

int (*loadprg)(void);

void * (*malloc_sysm)(size_t);


void init()
{
    handle = dlopen ("/export/home/joshis1/Foxtel/temp/libloadmap.so", RTLD_LAZY);
    if( handle == NULL)
     {
       puts(dlerror());
     }


   handle_malloc = dlopen ("/lib/libc.so.6", RTLD_LAZY);
    if( handle_malloc == NULL)
     {
       puts("handle malloc error\r\n");
       puts(dlerror());
     }


}


#include "stdio.h"


void *malloc(int size)
{
   printf("called..my malloc\r\n");

   malloc_sysm = dlsym(handle_malloc,"malloc");

   if ( dlerror() != NULL)
    {
       puts("malloc symbol not found..");
       exit(1);
    }


    printf("This should call actual malloc now..\r\n");
    return  malloc_sysm(size);




}


int main()
{
  int *t;
  init();
  printf("call load program now\r\n");

  loadprg = dlsym(handle, "loadprg");

  if( dlerror()!= NULL)
   {
      puts("Symbol load errror");
   }

  (*loadprg)();  

  printf("Close the handle now..\r\n");

  dlclose(handle);


  t = (int *) malloc (100);

  dlclose(handle_malloc);



  return 0;

}

Wyjście jest rekurencją do mojego zdefiniowanego malloc (). jak to naprawić?

questionAnswers(3)

yourAnswerToTheQuestion