Mitsos101
Mitsos101

Reputation: 571

Is it possible to run an executable built for one operating system on another?

I know about Wine/Darling and other compatibility layers, but I was wondering if it was possible (for example) this way:
1. Build a simple hello_world.c program on OS X.
2. Using objcopy/objconv/some other tool, copy the machine code into a Linux executable, and replace the call to the OSX libc with a call to glibc. If it's another executable dynamically linked to other OSX libraries, copy the libraries.
3. Run the program on Linux, assuming it works.
Example hello_world.c:

#include <stdio.h>
int main(void) {
    printf("Hello World");
    return 0;
}

Upvotes: 1

Views: 60

Answers (1)

Marco van de Voort
Marco van de Voort

Reputation: 26358

  1. First, the dynamic library is not the only OS specific part, the startup code is statically linked and OS X/Linux specific.
  2. The OS X binary is macho, Linux requires ELF.
  3. Even if it were ELF, there is an ELF identifier that needs to be patched to make the dyn loader even consider it.
  4. syscalls might be inlined. (man 2 usage), same with FD set macros and many other macros in OS X libc headers.
  5. the calling conventions might be different (
    • register usage
    • stack alignment
    • passing of small structures in registers.

etc,etc. In this case e.g. OS X uses AIX ABI, and Linux SysV. )

In general, POSIX is a source compatibility concept, not a binary one. So no, this is not possible, or only under unworkable constrained requirements. Exceptions are things like the Linuxator on BSD systems.

Upvotes: 2

Related Questions