12

I mean could a single binary file run in both Win32 and Linux i386 ?

est
  • 11,429
  • 14
  • 70
  • 118

3 Answers3

26

This is not possible, because the two types have conflicting formats:

  • The initial two characters of a PE file must be 'M' 'Z';
  • The initial four characters of an ELF file must be '\x7f' 'E' 'L' 'F'.

Clearly, you can't create one file that satisifies both formats.


In response to the comment about a polyglot binary valid as both a 16 bit COM file and a Linux ELF file, that's possible (although really a COM file is a DOS program, not Windows - and certainly not Win32).

Here's one I knocked together - compile it with NASM. It works because the first two bytes of an ELF file ('\x7f' 'E') happen to also be valid 8086 machine code (a 45 byte relative jump-if-greater-than instruction). Minimal ELF headers cribbed from Brian Raiter.

BITS 32
ORG 0x08048000

  ehdr:                                                 ; Elf32_Ehdr
                db      0x7F, "ELF", 1, 1, 1, 0         ;   e_ident
        times 8 db      0
                dw      2                               ;   e_type
                dw      3                               ;   e_machine
                dd      1                               ;   e_version
                dd      _start                          ;   e_entry
                dd      phdr - $$                       ;   e_phoff
                dd      0                               ;   e_shoff
                dd      0                               ;   e_flags
                dw      ehdrsize                        ;   e_ehsize
                dw      phdrsize                        ;   e_phentsize
                dw      1                               ;   e_phnum
                dw      0                               ;   e_shentsize
                dw      0                               ;   e_shnum
                dw      0                               ;   e_shstrndx
  ehdrsize      equ     $ - ehdr

times 0x47-($-$$) db    0

; DOS COM File code
BITS 16
    mov dx, msg1 - $$ + 0x100
    mov ah, 0x09
    int 0x21
    mov ah, 0x00
    int 0x21
  msg1:         db      `Hello World (DOS).\r\n$`

BITS 32
  phdr:                                                 ; Elf32_Phdr
                dd      1                               ;   p_type
                dd      0                               ;   p_offset
                dd      $$                              ;   p_vaddr
                dd      $$                              ;   p_paddr
                dd      filesize                        ;   p_filesz
                dd      filesize                        ;   p_memsz
                dd      5                               ;   p_flags
                dd      0x1000                          ;   p_align
  phdrsize      equ     $ - phdr

; Linux ELF code
  _start:
    mov eax, 4      ; SYS_write
    mov ebx, 1      ; stdout
    mov ecx, msg2
    mov edx, msg2_len
    int 0x80
    mov eax, 1      ; SYS_exit
    mov ebx, 0
    int 0x80
  msg2:         db      `Hello World (Linux).\n`
  msg2_len      equ     $ - msg2

  filesize      equ     $ - $$
caf
  • 233,326
  • 40
  • 323
  • 462
  • 1
    cool, but how about 16-bit .com for Windows and make it somehow ELF compatible? – est Jan 18 '10 at 15:37
  • 1
    So can the COM program launch a Windows .exe? That would be awesome. – Mark Ransom Sep 16 '11 at 03:20
  • 1
    Perhaps the COM program could write out a full-fledged Win32 binary into the users' temp directory and then launch that? (Or even somehow do so in memory). BTW, for those wondering: No, 16-bit programs won't run in Win7+. – rkagerer Jul 29 '12 at 17:28
  • “Not possible” => actually it is possible in that it’s PE + shell + ELF rather than PE+ELF. “Actually portable executable” – Vitali Oct 31 '22 at 13:13
  • @Vitali: It's a clever hack, but I wouldn't call that an ELF file. – caf Nov 01 '22 at 01:34
2

The two formats are sufficiently different that a hybrid is unlikely.

However, Linux supports loading different executable formats by "interpreter". This way compiled .exe files containing CIL (compiled C# or other .NET languages) can be executed directly under Linux, for example.

Jakob Borg
  • 23,685
  • 6
  • 47
  • 47
  • How does that exactly happen? You know we can execute a .txt under Windows. – est Jan 18 '10 at 15:38
  • 2
    @est Well yes, it's a little bit similar actually, except to "execute" a text file you would need to specify the "interpreter" on the command line: "open foo.txt" to use Explorer (which will probably spawn textedit) for example. What I'm talking about is recognizing the type of executable and loading the appropriate format directly using a system called binfmt_misc: "Binfmt_misc provides the ability to register additional binary formats to the Kernel [...]." (http://www.tat.physik.uni-tuebingen.de/~rguenth/linux/binfmt_misc.html) It's just a convenience feature, not magic. – Jakob Borg Jan 18 '10 at 17:37
-2

Sure. Use Java.

Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
  • 1
    Sorry about the curt answer, but it was cleverly crafted to be exactly 15 characters. The real answer is ELF vs COFF format is almost the least of your worries. You also have completely different system calls, shared libraries/dlls, etc. – Richard Pennington Jan 17 '10 at 19:52