I have a 32-bit binary-only C .so (provided by a vendor) built for Linux (using gcc), that I need to link to a 64-bit C/C++ application (which I am developing). Is there any way to access this library from my application?
I know there will need to be care taken to handle arguments and pointers to functions inside the .so, and I know that long ago it was deemed basically impossible (like this post). I'm hoping the situation has changed.
It might be possible to hack together some assembler glue code to translate the function parameters and return values from one module to the other, and flip the processor mode. It might be possible to hack the linker so it doesn't just reject it out of hand (or hack the .so file so the linker doesn't know). And you'd have to teach the 64-bit dynamic linker how to do 32-bit relocations, and so on, and on, and on.
But, what happens when you hit a pointer outside the 32-bit range? Come to that, what would happen if the 32-bit code got loaded at a 64-bit address? Now the kernel would have to be taught to load this 64-bit program in low memory addresses, which rather defeats the point of making it 64-bit in the first place.
Then you'd have to arrange for that library to link against the 32-bit libc, but having two libc in one project is probably broken. Maybe the 32-bit library could use the 64-bit libc? But, now that's not just function parameters you have to worry about: the system types are all the wrong size. Hmmm, and the kernel probably responds differently to syscalls from 32-bit and 64-bit processes. Not good.
(And, I'm not even sure it's even possible to switch between the 32-bit and 64-bit ISA without assistance from the kernel?)
No, I'm pretty sure this is close enough to impossible that nobody wants to do the work. And, with the world going increasingly 64-bit and 32-bit getting less, and less interesting, the situation is getting less likely to change, not more so.
The IPC option doesn't sound like much fun. If I were you I'd be seriously questioning why my new project has to be 64-bit.
来源:https://stackoverflow.com/questions/19391501/mixing-32-and-64-bit-libraries-in-linux-gcc