Using Ld_Preload Mixed 64Bit/32Bit Environment in Linux

Using LD_PRELOAD mixed 64bit/32bit environment in Linux

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

$ LD_PRELOAD=lib_init.so ./hello32

will search for lib_init.so in /lib, while

$ LD_PRELOAD=lib_init.so ./hello64

will search in /lib64

How to apply LD_PRELOAD only to target program?

It looks like you can set QEMU_SET_ENV=LD_PRELOAD=./malloc.so in the environment to affect the process.

I'll note that you can similarly unset environment variables for the target by setting QEMU_UNSET_ENV=FOO,BAR.

How does LD_PRELOAD='/something/$LIB/libmyx.so' myprogram expand under different conditions?

$LIB documentation from ld.so man:
https://man7.org/linux/man-pages/man8/ld.so.8.html

   $LIB (or equivalently ${LIB})
This expands to lib or lib64 depending on the architecture
(e.g., on x86-64, it expands to lib64 and on x86-32, it
expands to lib).

This is not completely true and actual values can be different. E.g. you are running a nativesdk binary from OpenEmbedded SDK. As you've noticed the SDK uses its own ld.so. That ld.so is not configured for multilib support and it expects to have only one lib directory with 64-bit libraries. That's why it resolves $LIB to lib.

You can see default library directory definition (BASELIB) here:
http://git.openembedded.org/openembedded-core/tree/meta/conf/bitbake.conf#n12

Using 32bit .lib library in a 64bit application

You don't have any other alternative; you cannot load a 32-bit assembly in a 64-bit program, so you'll need to create a 32 bit application that your 64 bit application can talk to somehow. There are lots of ways to do IPC, so find one that works for you.

How is linux simultaneously 32bit and 64bit? Or is that something handled in glibc?

The cpu can execute both 64 and 32bit instructions and the kernel can switch between modes. The only limitation is that you cannot link 32bit programs against 64bit libraries so you must have both 32 and 64bit versions of libc, etc. installed.



Related Topics



Leave a reply



Submit