Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can 32 bit and 64 bit work together?

Tags:

Can 64 bit library work in a 32 bit application? For example, my application GUI uses 32 bit Qt. And my business core is a 64 bit library. The OS is 64 bit. Can they work together and how? Thanks.

like image 877
user1899020 Avatar asked Apr 12 '13 12:04

user1899020


People also ask

Can I run 32-bit and 64-bit on the same computer?

You can't run it side-by-side. This is an upgrade copy that can only be used in a single instance. You can go to 64-bit if you want, but you have to do it with a clean install following your first 32-bit upgrade.

Do I need both 32-bit and 64-bit?

Here's why it matters. Simply put, a 64-bit processor is more capable than a 32-bit processor because it can handle more data at once. A 64-bit processor can store more computational values, including memory addresses, which means it can access over 4 billion times the physical memory of a 32-bit processor.


2 Answers

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.

like image 177
Mats Petersson Avatar answered Oct 12 '22 01:10

Mats Petersson


Yes, but it is a major nuisance.

First, a kernel is different from a library. Typically, a library is made visible in your process’ virtual address space; it shares the address space with your own code. Calling a library routine is simply a subroutine call.

In contrast, to request services from the kernel, your process executes a special instruction to generate a trap. This trap causes the processor to do some special things that include saving your process’ registers and other state in memory (or in special processor registers that you cannot normally access), changing various modes in the processor to make them suitable for the kernel, and changing the program counter to point to instructions for the kernel. Then the kernel is running. At this point, the kernel might be running in 64-bit mode while your process was running in 32-bit mode. However, the kernel is designed to be aware of these differences. When your kernel inspects your process to see what you are requesting, it looks for information and data structures knowing that your process was running in a 32-bit mode. A kernel can support both 32-bit and 64-bit processes, it just treats each type of process differently.

This presumes, of course, that the 64-bit kernel you are using supports 32-bit processes.

Normally, when you call a library, you want it to be the same mode as your code, because a normal library call is just a subroutine call; it does not generate a trap and does not change processor modes. If there were a critical need to call routines in a 64-bit library from a 32-bit process, then you could create a helper 64-bit process. Your 32-bit process would package up a request for a library call and send that request to the 64-bit helper process by some form of interprocess communication. That helper process would call the library routine and send the results back.

Naturally, this adds significant overhead to each library call, so it is something you want to do only if there is a great need and no better alternative.

like image 31
Eric Postpischil Avatar answered Oct 12 '22 00:10

Eric Postpischil