How can intercept libc function in C++ using LD_PRELOAD. I want to write code only in C++ file and all the logic of symbol fishing (dlsym) must be in C++ file. Please give an example.

  • Hi and welcome to RE.SE. How about using extern "C"? This effectively lets you write C functions in C++ by assigning C linkage.
    – 0xC0000022L
    Nov 22, 2018 at 10:42

1 Answer 1


If you want to intercept only library calls, you can insert your hooks in the GOT of the executable or at the dynamic loader resolver pointer (GOT[2]). I don't have sample code at hand, but this aricle is good for background.

If you want to intercept all syscalls (including those in the binary itself, e.g. if it's linked statically), you can use ptrace (PTRACE_SYSCALL) to be notified of all syscalls happening in the process. There are some examples on StackOverflow but they usually assume you're tracing another process. If you want to trace the same process you're in, you can use the ptrace(PTRACE_TRACEME) call.

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .