|
下面的代码,编译连接都通过,运行时说时断错误,segmentation fault
????
源代码:
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
static void *(*libc_malloc) (size_t)=0;
static void (*libc_free) (void *)=0;
static void load_libc ()
{
void *handle;
const char *err;
char *libcname;
libcname = "/lib/libc.so.6";
handle = dlopen (libcname, RTLD_NOW);
if ((err = dlerror ()))
{
/*
printf ("*** wrapper can not open `");
printf (libcname);
printf ("'!\n");
printf ("*** dlerror() reports: ");
printf (err);
printf ("\n"); */
exit (1);
}
libc_malloc = (void *(*)(size_t)) dlsym (handle, "malloc");
if ((err = dlerror ()))
{
printf ("*** does not find malloc in `libc.so'!\n");
exit(1);
}
libc_free = (void (*)(void *)) dlsym (handle, "free");
if ((err = dlerror ()))
{
printf ("*** does not find free in `libc.so'!\n");
exit (1);
}
}
void * malloc (size_t n)
{
return (*libc_malloc)(n);
}
void free (void* p)
{
return (*libc_free)(p);
}
int main()
{
load_libc();
char * m =(char*) malloc(2);
.....
free(m);
......
} |
|