Robert McLay Robert McLay - 1 year ago 154
Linux Question

Using LD_PRELOAD mixed 64bit/32bit environment in Linux

I would like to set LD_PRELOAD to point to a shared library where I might run either a 64bit or 32bit application. It is obvious that the shared library and the executable have to match in bit-ness.

$ LD_PRELOAD=/lib64/ ./hello32
ERROR: object '/lib64/' from LD_PRELOAD cannot be preloaded (wrong ELF class: ELFCLASS64): ignored

where hello32 is a 32 bit application. There are some pages out in the world that say that I should be able to do:

$ LD_PRELOAD='/$LIB/' ./hello32
ERROR: object '/$LIB/' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored

Where $LIB will automatically switch between lib and lib64 depending on whether the application is 32 or 64bit. But obviously this doesn't work.

Is there some trick to make this work? LD_PRELOAD_32, LD_PRELOAD_64?

Answer Source

By specifying full path to the library, you don't let dynamic linker to adjust it's search path accosting to binaries architecture. Define only library name and let linker to pick the correct library for you. E.g.:

$ ./hello32

will search for in /lib, while

$ ./hello64

will search in /lib64