Help with using LD_PRELOAD

2019-08-11 04:34发布

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?

标签: c linux
3条回答
可以哭但决不认输i
2楼-- · 2019-08-11 04:56

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

查看更多
相关推荐>>
3楼-- · 2019-08-11 04:57

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.

查看更多
Summer. ? 凉城
4楼-- · 2019-08-11 05:10
  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).

查看更多
登录 后发表回答