tags:

views:

572

answers:

3

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 a something special I need to do in my code or my library?

A: 

That seems like a bad idea. Why not name your version of printf something else?

KernelM
+4  A: 

You just set the environment variable LD_PRELOAD to the full path to the replacement library. Since all programs you launch after that point will attempt to use this library, you may want to make a wrapper script that sets LD_PRELOAD then calls the program you want to run.

Steve Baker
Or my favourite way (using sh or bash):LD_PRELOAD=/path/to/lib ./myProg
Paul Tomblin
+1  A: 
  1. As far as I know first of all the program cannot have changed evective uid or gid (so called suid or guid programs).
  2. It should be used only for specific purposes such as debugging. As far as I recall you may shadow functions in C (in elf?). However both techniques - LD_PRELOAD and shadowing should be deal with extream care. I remember discovering bug in shadowing g_malloc in gpgme code (or other related to gpg) as the GLib internals changed.

The simple answer is - don't do it. The more complicated - do it if and only if you have to - and usually you don't (unless you write some sort of debugging software).

Maciej Piechotka