I am studying how to hook a self-defined function through dynamical linking using LD_PRELOAD
.
However, most of the tutorials try to hook a libc function, e.g. puts(). Is it possible to hook a self-defined function, too?
I am trying the following code
//test.c
#include <stdio.h>
int ptest(){
printf("test!\n");
return 0;
}
int main(void){
printf("%d\n", ptest());
return 0;
}
//hook.c
//compile: "gcc hook.c -shared -fPIC -Wall -o hook.so"
#include <stdio.h>
int ptest(){
printf("fake!\n");
return 1;
}
run
LD_PRELOAD=./hook.so ./test
Result (It means nothing changed by hook.so
)
test!
0
What I expect
fake!
1