How Do Shared Libraries Work in a Mixed 64Bit/32Bit System

Can 32 bit and 64 bit work together?

In short: You can't link a 32-bit app to a 64-bit library.

You can run a 32-bit application, using 32-bit shared libraries on a 64-bit OS (at least all the popular 32-/64-bit processors such as AMD, Intel and Sparc). But that doesn't involve any libraries.

Longer answer: I was involved (on the outskirts) of some of the teams that designed the 64-bit Linux kernel for x86. There were briefly (compared to the whole project, the discussions lasted quite a few hours) some discussion as to how you could technically make this work. The short summary of this is that in 64-bit there are registers that aren't available in 32-bit. There is also the problem of memory addresses and the extra 32-bits in registers. All of these CAN be solved supposing the library itself "knows" that it's a 32-bit compatible library. But then we basically have a 64-bit library that is written to be a 32-bit library, and we've kind of lost the point.

The "more registers" may not apply to some processors, but the bigger address/bit-range of registers definitely apply to ALL 32- and 64-bit compatible processors. And I'm not aware of any single processor that allows a 32-bit code calling a 64-bit shared library or static library. It just doesn't work unless the code is specifically written to cope with that, which defeats the purpose of having a generic 64-bit library to support 32-bit apps.

Edit:

The above discusses linking one executable unit, e.g. an executable file, a shared library or a static library. That has to be all "one bitness", either 32 or 64 - no mixing.

When a process that talks to another process (e.g. a GUI app which displays status from a non-GUI process that), as long as the two processes use the same protocol [and typically, IPC doesn't allow passing of pointers anyway, so 32-/64-bit conversion isn't as big an issue], you can have one process that is 32-bit and another that is 64-bit.

Mono interop: Loading 32bit shared library does not work on my 64bit system

The problem is that you have a 64bit version of Mono installed on your system which can only P/Invoke into 64bit native libraries, it cannot P/Invoke into 32bit native libraries.

The -platform:x86 flag is meant for the C# compiler, not the runtime, and does not hint to the runtime to use a 32bit memory space.

You need to install the 32bit version of Mono on your Ubuntu system if you want to P/Invoke into 32bit native libraries.

Can 32-bit and 64-bit code be mixed?

Basically, no, you can't link, statically or dynamically, 32bit and 64bit code, not on x86 anyway.

What you can do is write a wrapper that runs in a separate process and uses RPC to "link" the library to your code. nspluginwrapper on Linux does something like that for Flash.

Load 32-bit shared library from 64-bit application?

No, you cannot load a 32-bit library in a 64-bit application through conventional means.

There are some clever hacks out there such as having a 32-bit application which loads the library and exports the functions through an IPC interface, but if you have the option to compile the library as 64-bit, then that is by far the best choice.

64 bit and 32 bit tools run in one environment

In the script, before calling 64-bit tools set LD_LIBRARY_PATH to 64-bit *.so.

As per my understanding executable will take the correct *.so, if both 32-bit and 64-bit *.so are there in the LD_LIBRARY_PATH as loaders know to ignore libraries of the wrong architecture.

For more info see this How do shared libraries work in a mixed 64bit/32bit system?

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.



Related Topics



Leave a reply



Submit