Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any way to create a binary that will run on many executable file formats (for example, Mach-O and ELF)? Even if it's just a file that contains both and runs the right one, I'd like to make an executable that works on both Linux and Mac.

share|improve this question
this is exactly what interpreted languages are for ;p – Journeyman Geek Dec 26 '12 at 6:28
@JourneymanGeek there's a C interpreter? :P – tkbx Dec 26 '12 at 22:08
up vote 1 down vote accepted

Short answer: no, because each binary format starts with a specific sequence ("magic number") that identifies the format. ELF binaries start with 0x7F 45 4C 46, Mach-O single-arch binaries with 0xFE ED FA CE or 0xCE FA ED FE (depending on byte order), and Mach-O fat binaries with 0xCA FE BA BE. Since the binary can't start with more than one of these, it can't be valid in multiple formats.

Now, it might be possible to do something trickier, like having a wrapper shell script that somehow convinces the loaders to execute part of itself... But that's getting beyond my expertise (and will depend not only on the binary formats you're trying to load, but the OSes you're trying to load them on as well...)

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .