Help Using LD_PRELOAD

I want to create a library with a modified version of printf and then call LD_PRELOAD, so when my program calls printf it uses my version. Can someone explain to me how to use LD_PRELOAD and if there is something special in my code or my library?

+1


source to share


3 answers


You just set the environment variable LD_PRELOAD

to the full path of the library you are replacing. Since all programs you run after this attempt will try to use this library, you can create a shell script that installs LD_PRELOAD

and then calls the program you want to run.



+5


source


  • As far as I know, in the first place the program could not change the evitive uid or gid (the so called suid or guid of the program).
  • It should only be used for specific purposes like debugging. As far as I remember, you can shadow functions in C (in elf?). However, both LD_PRELOAD

    shading and shading methods must deal with careful care. I remember g_malloc

    running into a shadowing error in gpgme (or other gpg-related code) where GLib's internal objects changed.


The simple answer is don't do it. The harder it is - do it if and only if you need to - and usually you don't (unless you are writing some kind of debugging software).

+2


source


This sounds like a bad idea. Why not call your version printf

something else?

-3


source







All Articles