|
|
我自己创建了一个libmy.so.1.0.1共享连接库,主要功能是重定义了strlen这个函数
使用export LD_PRELOAD=./libmy.so.1.0.1后,用测试程序测试strlen发现没有调用我的strlen
可是用shell下输入ls后,又表现出调用了我的strlen程序,请问这是怎么回事,怎样才能让我的strlen在测试程序中生效
root:~# cat mystrlib.c
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
int strlen(const char * s)
{
void * handle;
char * dl_error;
int orig_return;
typedef int (*func_t) (const char *);
func_t my_strlen;
char *my_name="strlen";
printf("using strlen here!\n");
handle = dlopen("/lib/libc.so.6",RTLD_LAZY);
if(!handle)exit(0);
my_strlen=dlsym(handle,my_name);
if((dl_error = dlerror()) != NULL)exit(0);
orig_return = my_strlen(s);
return orig_return;
}
root:~# gcc -fPIC -rdynamic -g -c -Wall mystrlib.c
root:~# gcc -shared -WI,-soname,libmy.so.1 -o libmy.so.1.0.1 mystrlib.o -lc -ldl
root:~# cat test_lib.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *a="isndfiasdf";
int len;
len = strlen(a);
printf("The word '%s' len = %d\n",a,len);
return 1;
}
root:~# gcc -g test_lib.c -o test_lib
root:~# ./test_lib
The word 'isndfiasdf' len = 10
root:~# export LD_PRELOAD=./libmy.so.1.0.1
root:~# ./test_lib
The word 'isndfiasdf' len = 10
root:~# ls
using strlen here!
using strlen here!
libmy.so.1.0.1 using strlen here!
mystrlib.c using strlen here!
mystrlib.o test_lib using strlen here!
test_lib.c
root:~# |
|