Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LD_PRELOAD help

I'm trying to use LD_PRELOAD.

original.cpp

void myPuts() {  
    puts ("Hello myPuts");  
}  
int main() {  
    myPuts();  
    return 0;  
}

hacked.cpp

void myPuts() {  
    std::cout >> "Hello hacked myPuts";  
}

I compile original.cpp:

g++ original.cpp

And hacked.cpp:

g++ -shared -fPIC hacked.cpp

I try:

LD_PRELOAD=./hacked.so ./original.out

The string "Hello hacked myPuts" should be seen, by "Hello myPuts" appears. (If I try to "overwrite" the puts function, it works correctly)

What am I missing?

like image 347
krisy Avatar asked Mar 06 '26 16:03

krisy


1 Answers

From man ld.so

LD_PRELOAD

A whitespace-separated list of additional, user-specified, ELF shared libraries to be loaded before all others. This can be used to selectively override functions in other shared libraries.

If myPuts was in shared library linked to main application it would work, but not when myPuts exists in the application and does not resolved in an external library.

like image 120
Artyom Avatar answered Mar 08 '26 04:03

Artyom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!