源碼:
#define _GNU_SOURCE //使用RTLD_DEFAULT和RTLD_NEXT宏需定義
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
#include <errno.h>
void* dlopen(const char* libfile,int flag)
{
FILE *fp = fopen("/tmp/test.log","a+");
fprintf(fp, "%s\n",libfile);
static void * (*realdlopen)(const char* libfile,int flag) = NULL;
realdlopen = dlsym(RTLD_NEXT, "dlopen");
void * rt = realdlopen(libfile, flag);
if (rt == NULL) {
int errcode = errno;
char *err = dlerror();
fprintf(fp, "errno code: %d\n",errcode);
fprintf(fp, "error str: %s\n",err);
} else {
fprintf(fp, "success dl: %p\n", rt);
}
fclose(fp);
return rt;
}
編譯:
gcc --shared test.c -o test.so -ldl -fPIC
使用:
export LD_PRELOAD=./test.so
./yourelf
cat /tmp/test/log