1
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!

it works fine. but when i ldd the exec file, it doesn't dynamic link to libhello.so why??

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)

how can I write a LD_PRELOAD hook to hijack function(here is go.hello.Hello?), because name of function in C cannot include dot

#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!
user3682618
  • 87
  • 1
  • 8
  • It might use dlopen/dlsym to access shared libraries in run time. Regarding your second question, keyword `__asm__` is the haxors' friends: https://gcc.gnu.org/onlinedocs/gcc/Asm-Labels.html – Zsigmond Lőrinczy Feb 10 '17 at 16:07

0 Answers0