Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The author somehow came up with an executable file format that's compatible with everything. It's simultaneously a valid shell script, Unix ELF, Windows PE file, boot sector code and zip file.

Then she wrote a standard C library that detects which OS you're using at runtime and branches to the appropriate implementation, allowing the same x86_64 code to run on any supported system.

  if (IsWindows())
      DoWindowsThing();
  else
      DoUnixThing();
Traditionally, these system differences are resolved at build time: only the code for the target platform is included. Why would anyone want Windows-specific code in a Linux build? It's dead code... right? The new run-anywhere executable format makes that code useful.


> Why would anyone want Windows-specific code in a Linux build? It's dead code...

You might want to ask the thousands of Electron App users how much they care about dead code ;-)

I mean, you are right. But if you offer your users no alternative they might not care enough to choose another product. In addition, some might even value it not having to choose the right binary for their system.


> In addition, some might even value it not having to choose the right binary for their system.

Yes. The true innovation that made all this possible is the novel Actually Portable Executable (APE) format. This means there's no need to choose: Windows will read it as a valid PE file while Linux, the BSDs and Mac will read it as a valid ELF program.

Having code for all operating systems in the executable has always been possible. It's just that before APE there used to be no point. Windows simply isn't able to load and execute an ELF program even if there is Windows code in it.

ELFs would be incompatible even between Linux, Mac and the BSDs since they would almost always depend on very different dynamic libraries. Cosmopolitan fixes this by providing a standard C library with support for everything. I assume incompatibilities can still be introduced by linking against other libraries.


Are you sure? Other answers are suggesting it isn't a valid ELF file and I don't get why it would need to be a valid boot sector or zip file.

You might be right though. The author might be very good at executable hacks but they're a terrible writer. Their explanation explains nothing.


The author seems to be an excellent writer. They just may not have taken the enormous amount of time to answer tons of questions or write tons of documentation people are seeking. One person did all this!


You really think so? Here's their description of how it works:

> Here's how it works:

>

> MZqFpD='

> BIOS BOOT SECTOR'

> exec 7<> $(command -v $0)

> printf '\177ELF...LINKER-ENCODED-FREEBSD-HEADER' >&7

> exec "$0" "$@"

> exec qemu-x86_64 "$0" "$@"

> exit 1

> REAL MODE...

> ELF SEGMENTS...

> OPENBSD NOTE...

> NETBSD NOTE...

> MACHO HEADERS...

> CODE AND DATA...

> ZIP DIRECTORY...

Ok well that clears it up. MZqFpD='. Of course!


Actually I'm not 100% sure. I can barely make sense of it. All I know is it somehow works as if it was a valid ELF. Some dark magic going on here...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: