Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LD_PRELOAD not working for printf

i am using LD_PRELOAD to capture write() system call in linux . I am successfully able to do this for write system call and make it work.

But when i call printf() that time it does not work. If we observe printf stack trace using strace i found that, at the end printf calls write() system call to write to console, but at that time my write() system call is not called before actually calling the the write() system call.

Anybody have any idea why is this happening ?

like image 542
app Avatar asked Mar 22 '11 18:03

app


1 Answers

Function calls made from one library to another or from the executable to a dynamically loaded library go through the PLT (Procedure Linkage Table) and are able to be redirected by the use of LD_PRELOAD. However, function calls within a library can be resolved at compile time and do not go through the PLT. Therefore they are unable to be redirected by LD_PRELOAD. Since printf and write are both compiled into libc.so.6, the call to write from printf never goes through the PLT to look for a possible redirection, but when you call write directly from your application (or from another shared library) it does.

like image 94
bdk Avatar answered Sep 27 '22 18:09

bdk