Windows? Linux? Browser? Same Executable

We’ve been aware of projects like Cosmopolitan that allow you to crank out a single executable that will run on different operating systems. [Kamila] noticed that the idea was sound, but that the executables were large and there were some limitations. So she produced a 13K file that will run under Windows, Linux, or even in a Web browser. The program itself is a simple snake game.

There seems to be little sharing between the three versions. Instead, each version is compressed and stitched together so that each platform sees what it wants to see. To accommodate Windows, the file has to start with a PE header. However, there is enough flexibility in the header that part of the stub forms a valid shell script that skips over the Windows code when running under Linux.

So, essentially, Windows skips the “garbage” in the header, which is the part that makes Linux skip the “garbage” in the front of the file.

That leaves the browser. Browsers will throw away everything before an <HTML> tag, so that’s the easy part.

Should you do this? Probably not. But if you needed to make this happen, this is a clear template for how to do it. If you want to go back to [Kamila’s] inspiration, we’ve covered Cosmopolitan and its APE format before.

14 thoughts on “Windows? Linux? Browser? Same Executable

    1. Given the size of llamm files, It would take fewer resources to simply bundle a dozen different binaries together. You may like the idea but it’s not good in any practical sense.

      1. Hi, sure! Normally, Windows executables have an MZ stub which displays “This program requires MS Windows!” on DOS, so it wouldn’t crash the PC if accidently run on plain DOS.
        Both Win16 (NE) and Win32 (PE) applications had that MZ stub.
        On 16-Bit Windows there also were hybrid applications, such as setup.exe (Windows Setup) inside Windows directory.
        Instead of a short message these programs contained both full Windows 3.x and MS-DOS applications.

        On 16-Bit OS/2, there also were MZ/NE hybrid applications. The socalled “Family API” applications.
        Here, the MZ part contained an OS/2 runtime for DOS that would run the OS/2 application in the NE part of same EXE file.
        That way, same application ran on an 8088 based PC/XT in Real-Mode and on an 80286/80386 based PC/AT in 16-Bit Protected-Mode, including the advantage of multitasking, virtual memory and memory protection.
        The draw-back was that those hybrid applications were fatter and more memory hungry.
        And the OS/2 runtime was more limited than real OS/2, of course.

  1. 1980s are calling and they want their invention back : – ]

    I personally knew people who worked on universal/uniform file format that would work with MacOS (back then it was McIntosh that was the highest popular ride, so it was something like System 2 or System 3 on McIntosh) and IBM clones (and probably Xerox OS, too, but that part I don’t know at all). The year was 1986 and it was really in the middle of nowhere, so as far as I could tell the project mostly went back to the middle of nowhere – though I suspect all programmers found better-paying jobs in the middle of somewhere (as opposed to the original nowhere).

    In the hindsight, programming OSs back then was quite simpler, so potentially this wasn’t insurmountable obstacle that couldn’t be tackled by few dozens dedicated experts. Id also speculate that with some mental grease same files could (probably) run on CP/M or Atari, given they are not too esoteric for those machines.

    I am still unclear what went wrong with compiled Java that promised binaries for any OS/platform. It kinda sorta did happen (JVM), though certainly not on the scale initially promised – and odd coincidence has it, browser solutions are now struggling to reinvent this exact concept – though, in quite awkward and unproductive way.

Leave a Reply to JoshuaCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.