package hello
import "fmt"
func Hello() {
fmt.Println("hello, world!")
}
gccgo -c hello.go -o libhello.so -shared
nm libhello.so
...
0000000000000000 T go.hello.Hello
0000000000000000 R go.hello.Hello$descriptor
package main
import "./hello"
func main() {
hello.Hello()
}
gccgo -g main.go -L. -lhello -ldl -o main
./main
hello, world!
它工作正常。但是当我查看exec文件时,它没有动态链接到libhello.so为什么??
ldd main
linux-vdso.so.1 => (0x00007ffd7bb88000)
libgo.so.4 => /lib64/libgo.so.4 (0x00007f134bca3000)
libm.so.6 => /lib64/libm.so.6 (0x00007f134b9a0000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f134b78a000)
libc.so.6 => /lib64/libc.so.6 (0x00007f134b3c8000)
/lib64/ld-linux-x86-64.so.2 (0x00007f134c9ce000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f134b1ab000)
如何编写LD_PRELOAD挂钩到hijack函数(这里是go.hello.Hello?),因为C中函数的名称不能包含点
#include <stdio.h>
#include <string.h>
#define __USE_GNU
#include <dlfcn.h>
static void (*hello) () = 0;
__attribute__ ((constructor))
void hello_init (void)
{
hello = dlsym (RTLD_NEXT, "go.hello.Hello");
}
void Hello()
{
int fd;
if (!hello)
return;
printf("pre hello\n");
hello();
printf("post hello\n");
}
LD_PRELOAD=./libxxx.so ./main
hello, world!