Help LD_PRELOAD

I am 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 the original.cpp file:

g++ original.cpp

And hacked.cpp:

g++ -shared -fPIC hacked.cpp

I'm trying to:

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

The string "Hello hacked myPuts" appears, and "Hello myPuts" appears. (If I try to “overwrite” the puts function, it works correctly)

What am I missing?

+3
source share
2 answers

You must have:

main.cpp

int main() {  
    myPuts();  
    return 0;  
}

original.cpp

void myPuts() {  
    puts ("Hello myPuts");  
}  

hacked.cpp

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

Compilation of all:

g++ -shared -fPIC original.cpp -o liboriginal.so
g++ -shared -fPIC hacked.cpp -o libhacked.so
g++ main.cpp -loriginal -o main.out

And using:

LD_PRELOAD=./libhacked.so ./main.out
+3
source

From man ld.so

LD_PRELOAD

A space-separated list, optional, user-defined, common ELF libraries to load before everyone else. This can be used to selectively override functions in other shared libraries .

myPuts , , , , myPuts .

+6

Source: https://habr.com/ru/post/1789707/


All Articles