- 論壇徽章:
- 0
|
10可用積分
我測(cè)試的代碼如下:
cat test.c
#include<stdio.h>
#include<stdlib.h>
void hi(void)
{
printf("hi");
}
int main(void)
{
char *p;
setenv("LD_PRELOAD","./libmylib.so.1.0",1);
p=getenv("LD_PRELOAD");
printf("LD_PRELOAD:%s\n",p);
//hi();
printf("hello\n");
}
cat prog.c
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
/*
void _init()
{
unsetenv("LD_PRELOAD");
execl("./abc.sh","abc.sh","init",NULL);
}
*/
int printf(const char *s,...)
{
unsetenv("LD_PRELOAD");
execl("./abc.sh","abc.sh",s,NULL);
}
gcc test.c -o test
gcc -o prog.o -c prog.c -fPIC
gcc -shared -Wl,-soname,libmylib.so.1 -o libmylib.so.1.0 prog.o -nostartfiles
我的測(cè)試的結(jié)果:
當(dāng)前環(huán)境變量里沒(méi)有LD_PRELOAD,執(zhí)行./test 發(fā)現(xiàn)printf沒(méi)有替換。
然后 我export LD_PRELOAD="./libmylib.so.1.0" 之后,再次執(zhí)行./test,printf就被替換成我的共享庫(kù)里的printf了
為什么test.c里的 setenv("LD_PRELOAD","./libmylib.so.1.0",1); 沒(méi)起作用呢?
還有一個(gè)問(wèn)題,對(duì)于共享庫(kù)中_init 用法,我不是太明白,當(dāng)我把注釋取消的時(shí)候,什么時(shí)候_init 起作用? 好像只要執(zhí)行函數(shù)調(diào)用,這個(gè)_init 就會(huì)起作用,為什么。磕菈K知識(shí)是介紹這塊東西的?謝謝各位給執(zhí)教一下
還有,test.c中,有兩個(gè)printf,export LD_PRELOAD="./libmylib.so.1.0" 之后,只看到t.txt 中有LD_PRELOAD:%s,沒(méi)有看到hello,這個(gè)是為什么?
謝謝指點(diǎn)一下! |
|