ELF runnable on multiple OSes
By nike on Jun 13, 2007
Once I asked myself, if it's possible to create an ELF file, which will run on multiple OSes with same CPU. ELF only does checks of target hardware, not an operating systems, but OSes differs in calling conventions and way arguments passed. So I wrote following piece of ASM code for x86, if compiled with
gcc -nodefaultlibs -nostdlib -static dual.s -o dualproduces ELF file 627 bytes long, which works for both Linux and Solaris x86. Enjoy!
.macro SC_sol num, a1, a2, a3 movl \\num, %eax pushl \\a3 pushl \\a2 pushl \\a1 pushl $0 lcall $0x07, $0x0 # lcall $0x27, $0x0 will do as far add $16, %esp .endm .macro SC_lin num, a1, a2, a3 movl \\num, %eax movl \\a1, %ebx movl \\a2, %ecx movl \\a3, %edx int $0x80 .endm .globl _start .type _start, @function _start: subl $4, %esp fnstcw (%esp) popl %eax cmpl $0x037f, %eax je 1f ### solaris pushl $0x0a0a4948 movl %esp, %ecx SC_sol $0x4 $1 %ecx $3 SC_sol $0x1 $239 $0 $0 ### linux 1: pushl $0x0a0a4948 movl %esp, %ecx SC_lin $0x4 $1 %ecx $3 SC_lin $0x1 $239 $0 $0