ELF runnable on multiple OSes

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 dual produces 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   
Comments:

So, you own a AMD (32bit) CPU. This won't work on IA-32 CPU, since there is no "syscall" instruction on IA-32 (at least not on Pentium 4 and earliers).

Posted by Ashish Shukla on June 13, 2007 at 04:34 PM MSD #

Yes, you're right. Solaris supports several alternative technologies of doing syscalls. I corrected my code with int $0x91 which should work everywhere.

Posted by nike on June 13, 2007 at 06:33 PM MSD #

Actually int $0x91 works only for Solaris 11 and later updates of S10, but lcall I used should work everywhere.

Posted by nike on June 14, 2007 at 02:07 AM MSD #

Post a Comment:
  • HTML Syntax: NOT allowed
About

nike

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today