1

Take for example a program downloaded from some website, the different options to pick from are the usual operating systems (Linux, Mac, Windows) but what about CPU architecture? The program is a binary executable. Does it just assume amd64? Or is the program compiled into all of the supported architectures and packaged together with a script on top that chooses the right one?

I'm only interested in C and would like to know how this is accomplished.


On further investigation, thanks to the lovely information provided by the individuals below, I came across Fat Binaries with support on both Mac and Linux. It doesn't seem as though windows supports it.

Community
  • 1
  • 1
  • You can generally create a multi-architecture executable, and have a small "lowest-common-denominator" boostrap section that detects the operating environment and starts up the architecture-specific code. But you can't have a universal executable that'd work on all platforms. Every OS has different executable file formats and none of them are compatible with the other OSes, e.g. Linux uses ELF, Windows has PE, OSX has Mach-O. – Marc B Jan 07 '15 at 14:26
  • e.g. as a nice example, grab any of the Sysinternals utilities off the MS website. It's a single small .exe and contains both 32bit and 64bit versions, held together with a small boostrap section. – Marc B Jan 07 '15 at 14:27
  • What google searches have you done, that all failed? I tried one search: 'how does a compiler work' and got [this](http://programmers.stackexchange.com/questions/118586/how-does-a-compiler-work), [this](http://en.wikipedia.org/wiki/Compiler) and many others. – meaning-matters Jan 07 '15 at 14:28
  • Thank you for your answer Marc, exactly what I needed to know. Now I actually know what to research :p – Gorgey Gorge Jan 07 '15 at 14:32
  • @MarcB I'm pretty sure you can't switch between 32 and 64 bit mode without kernel intervention. The best you can do is to spawn another process with required arch, but that's basically a self-extracting executable, more than a bootstrap. – ElderBug Jan 07 '15 at 14:45

1 Answers1

0

The Mac OS X binary format includes a mechanism for providing code for multiple architectures in the same file, and so a single Mac application can support 32 and 64 bit x86; in the recent past PowerPC support was possible too, although those are now obsolete. But for Windows and Linux, you generally need separate binaries for each CPU architecture (as comments have pointed out, it's possible to jury-rig something similar, although it's far from standard practice.) The default, and by far the most common, is amd64, but sometimes you'll still see separate downloads for 32-bit machines. The world used to be more interesting in this respect, but nowadays things are more standardized than ever.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186