3 ms·
From the other side of it, there's "How to Write Shared Libraries": https://akkadia.org/drepper/dsohowto.pdf https://akkadia.org/drepper/dsohowto.pdf Edit: LD
by gnufx 4y ago
From the other side of it, there's "How to Write Shared Libraries": https://akkadia.org/drepper/dsohowto.pdf https://akkadia.org/drepper/dsohowto.pdf
Edit: LD_PRELOAD would have been useful to mention in the article. It's a useful mechanism, e.g. for profiling by hooking relevant functions, like the PMPI mechanism in the MPI standard.
- doix 4y agoLD_PRELOAD is also a great way to modify programs you don't have the source for. I used to work for a place that use Perforce as their source control platform. Perforce has/had a CLI tool you could use to open specific GUI tools called p4vc[0]. The first time you ran the tool, it'd spawn a webserver in the background listening on localhost:7999 (this is now actually documented, back then I found out via strace). The problem was that we had many people working on the same machine (in different X11 servers). So whoever was first to run the p4vc on a machine would spawn the background server. Anyone that tried to use p4vc in the future would communicate with a server spawned by someone else. Since the authentication didn't match, it would create a login prompt (on the DISPLAY of the person that owns the webserver, not the person trying to run the command). It was super annoying. I created a tiny .so file that would intercept 'bind' and 'connect', check the port to see if it was 7999, and if it was, use the uid of the user instead. Otherwise just transparently pass along all the calls. Problem fixed! I felt like a wizard, one of the few times in my career that "arcane" knowledge came in handy. [0] https://www.perforce.com/manuals/p4v/Content/P4V/advanced_options.p4vc.html https://www.perforce.com/manuals/p4v/Content/P4V/advanced_op...
- gnufx 4y agoYes, there's a bunch of common implementations which use it, like (Debian packages) eatmydata, fakeroot, datefudge, substitute mallocs. (One implementation of) fakeroot spawns a demon. Oh, and if you have something statically linked, so LD_PRELOAD is useless, the usual subversion tool is dyninst.