views:

105

answers:

3

As title said, I have C program complied and created a binary in 64-bit machine.Will this binary work under 32-bit ?

+4  A: 

The compiling machine does not matter. What matters is: is the code generated 32 bits (answer: yes) or 64 bits (answer: no).

Ritsaert Hornstra
I can't understand it.Can you explain it more?When I ran file command I got this :giis-ext4: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not strippedDoes this mean it can run only on 64-bit? IF so how to create 32-bit executable from 64-bit machine[
lakshmipathi
@lakshmipathi: The code generated by GCC is x86-64 (default under a 64 bit OS) or x86 using the -m32 switch. See the answer of JesperE .
Ritsaert Hornstra
Thanks Ritsaert, Now I understand it.
lakshmipathi
+5  A: 

64-bit binaries cannot run on a 32-bit OS. If file reports ELF 64-bit, you have a 64-bit binary.

In order to build 32-bit binaries on a 64-bit Linux, you need pass -m32 to gcc. You also need to have 32-bit libraries installed (sudo apt-get install libc6-dev-i386).

JesperE
thank you guyz ,now I got it .
lakshmipathi
64 bits processes run just fine on 32 bit OS with a 64 bit cpu (Mac OS X does this all the time...). Windows or Linux have no "native" way for doing this (it won't load 64 bit binaries) but it could be easily forced using some clever magick, switching CPU modes manually, etc.But the general answer to your question is still "no".
Aram Hăvărneanu
Mac OS X has a "transparent" 32-bit mode, just as Windows and Linux does; there is no "manual magic" necessary. Windows has a 32-bit subsystem which kicks in automatically, and Linux has a similar mechanism. This is possible since modern x86-64 processors are backwards compatible with the old 32-bit x86 architecture (and basically include a 32-bit x86 on the chip).
JesperE
What I mean with "64-bit binaries cannot run on a 32-bit OS" is that modern OS which allow this basically includes a 32-bit compatibility subsystem to run 32-bit binaries, but this subsystem is usually completely isolated from the 64-bit OS and can communicate only using IPC mechanisms etc.
JesperE
Oh, you misunderstood what I was saying :). Mac OS X is a *32 bit* operating system! (until 10.6 at least, and there is 32 bit by default too). This 32 bit operating system can run 64 bit binaries just fine.On 32 bit Windows or Linux, with a 64 bit processor, you can do the same thing, although the OS won't load by default your binary. However, if you switch from 32 to 64 bit in code it is possible.
Aram Hăvărneanu
That's cool. But then it would probably be better labelled a "mixed" 32/64-bit OS.
JesperE
A: 

Will it work on a 32-bit machine? One with a CPU which doesn't support 64-bit mode? No.

Will it work on a 64-bit VM on a 32-bit native OS? Yes, in my experience.

A surprising result is that 64-bit VMs CAN be run under a 32-bit host OS, provided the CPU is capable and you don't want to allocate too much ram (>2G or thereabouts) to the guest.

MarkR
To clarify, a 64-bit guest OS can run under a 32-bit host OS but ONLY if the CPU is 64-bit.
Matt Olenik
64 bits processes run just fine on 32 bit OS with 64 bit cpu (Mac OS X does this all the time...). Windows has no "native" way for doing this (it won't load 64 bit binaries) but it could be easily forced using some clever magick, switching CPU modes manually, etc.
Aram Hăvărneanu