67

I know how to override one library with LD_PRELOAD, for example, as follows.

LD_PRELOAD=./getpid.so ./testpid

Now my question is how to override multiple files. Say I want to override both getpid and getid, how would I specify that?

peterh
  • 11,875
  • 18
  • 85
  • 108
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356

2 Answers2

88

According to the ld.so manpage, it is a space separated list. So:

LD_PRELOAD="path1 path2"

ought to work.

peterh
  • 11,875
  • 18
  • 85
  • 108
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 19
    According to the man-page colons are also accepted, e.g.: `LD_PRELOAD=path1:path2` – Isac Casapu Dec 28 '16 at 13:20
  • 4
    Interesting. In the section on LD_LIBRARY_PATH, the page mentions that semi-colons and colons are valid, but makes no mention of spaces. The parsing of $ORIGIN, $LIB, and $PLATFORM are (I believe) new. Spaces were always a bit surprising, and I find it odd that they are valid in LD_PRELOAD but not LD_LIBRARY_PATH. – William Pursell Dec 28 '16 at 13:45
  • 4
    Note that the page dated 2001 (from https://www.kernel.org/pub/linux/docs/man-pages/Archive/man-pages-2.00.tar.bz2) states: " LD_PRELOAD: A whitespace-separated list of additional, user-specified, ELF shared libraries to be loaded before all others." I cannot find a change in binutils, and would be curious to know if this is merely an omission in the documentation or if the behavior has changed recently. – William Pursell Dec 28 '16 at 14:02
  • Since it is spaces, if libraries to preload have a space in their pathname it will break, right? – Golar Ramblar Jul 27 '23 at 18:35
  • @MetallicPriest, I think this solution solves this question, can you check and maybe mark it as the solution? – Golar Ramblar Jul 27 '23 at 18:36
  • @WilliamPursell, the [manpage linked in the answer](http://kernel.org/doc/man-pages/online/pages/man8/ld.so.8.html) states "*separated by spaces or colons*". To complete your answer, I suggest to add also the option with spaces. – Golar Ramblar Jul 27 '23 at 18:39
5

One option is to have the overridden version of both getpid and getid in a single .so which you give to LD_PRELOAD.

codaddict
  • 445,704
  • 82
  • 492
  • 529