Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer 1

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.