Comment by hprx
5 years ago
I just spent 30 minutes reading about this. I'm so shocked that I'm logging in to comment after 5 years of lurking.
Justine has built a c library that allows you compile a binary once and have it run it on any os or baremetal. The SAME binary. Quite frankly, that sentence doesn't even make sense to me.
Check out https://storage.googleapis.com/justine/cosmopolitan/index.ht...
As far as I'm concerned, this is literal magic. Look at the magic numbers: https://github.com/jart/cosmopolitan/blob/37a4c70c3634862d8d...
I could go on, but there's no binary portability comparison with any other language. And she has made some pretty neat optimizations.
Back in the day, I saw some pretty neat stuff with the ELF format, but this takes the cake.
Wow.
Edit: I'm editing because this is just so bloody absurd.
https://storage.googleapis.com/justine/printimage.html
$ ./printimage.com someimage.jpg
Like wow. And also video.
https://storage.googleapis.com/justine/printvideo.html
I'm struggling to put my shock into words. I've been around.
There's engineering. There's academia.
But this falls into straight-up wizardry.
Agreed, this is the best programming-related thing I've seen on the internet in a long time. It reminds me of when I was a teenager, excited to become a computer programmer. Then I grew up and joined the real world workforce and it was all far less exciting than I imagined.
The coolest thing about this Actually Portable Executable is that once there's a compiler and linker built with it, I can play around with writing C on Windows without having to faff about with WSL or MinGW or learning what makes MSVC different from the C I learned in university and coded in my first job before I became a Java/JavaScript/Python/etc programmer. When I discovered Go I thought I had discovered the better C that just worked everywhere, but the idea of having plain old C that also just works everywhere is very appealing.
I like Go for the same reason, but I have an irrational fear that someday I might be in a life or death situation and should program my way out using a bare bones computer without Internet; So I keep tabs on C occasionally.
αcτµαlly pδrταblε εxεcµταblε has now piqued my interest, So I might actually indulge in C programming for fun again after all. Thanks Justine.
I think it's really cool project; but could you explain why it's important that a binary works everywhere for you? I don't really get the enthusiasm for this sort of thing.
Maybe I have a blind spot or something or I've just been in Unix-land for too long.
I think the UNIX thing is the blind spot. People who run Linux or BSD don't seem so bothered to recompile for their own platform, safe in the knowledge that it will almost certainly just work.
But on Windows, trying to get open source software written in C to compile has been a pain for decades. There have been some attempts to make it work (notably Cygwin, and now MSYS2), but it can still be challenging, especially with programs that were never designed to be compiled for non-UNIX (or non-Linux!) targets.
Having a C library that is designed from the ground up to target multiple operating systems, and an executable format that means the first compile is the only compile, that takes away a lot of toil that programmers normally have when trying to build their own abstractions around each platform's C library. I guess from UNIX land you could see the benefit as removing the need for autoconf or platform ifdefs.
Agreed. This single sentence at the end of αcτµαlly pδrταblε εxεcµταblε page:
> I believe the best chance we have of doing that [writing software that stands the test of time with minimal toil], is by gluing together the binary interfaces that've already achieved a decades-long consensus, and ignoring the APIs.
...it's a kind of thought I don't think I could ever come up with. Mind blown.
You go to war with the army you have.
I agree, I got to the page on actually portable executable, and had to read it a few times to make sure I was grokking it properly. This is just sheer cleverness, THIS is the stuff that should be on hacker news!
For real.
It's hard to grok cause there are no words to describe it.
When people say that something is cross-platform, they usually mean a) that the software will build on multiple platforms b) there is some sort of vm which runs the executable (jvm, beam, wasm)
This is the SAME binary. Running everywhere. Could be super-useful as an archival format for mission critical code. sqlite comes to mind.
Maybe it's late at night, but I'm struggling to find a suitable word for the project that is better than portable or cross-platform.
Literally, I can't find a better word than the project name - "actually portable executable."
@author - if you're reading this please set up something for a few bucks a month on your github sponsors. I don't think I have any use for the library, but this is so outrageous it deserves something more than imaginary internet points.
Author here. Thanks! I've added a new sponsor tier per your request. https://github.com/sponsors/jart
2 replies →
> THIS is the stuff that should be on hacker news!
If interested, see previous discussions of APE:
https://news.ycombinator.com/item?id=25556286
Thanks for the links. The discussion on the first one is interesting, it wasn't clear to me from the original doc that the UNIX version needs to overwrite itself to get to the ELF header. That's a bit unfortunate, because it means you can't share the program again after you ran it the first time on BSD or Linux. Still very clever, though. I imagine there could be a "repackage for distribution" switch added to put the MZ header back again.
2 replies →
Everything she does has this level of jaw-dropping amazingness. Between her and Fabrice Bellard i don't know many people who consistently get my chin to hit the table.
I hope that Cosmopolitan becomes mainstream, so much more software could have that It Just Works quality.
Mike Pall for LuaJIT
"Jaw-dropping" is definitely a term I'd use to describe some of her more infamous tweets and blog posts, too.
Please don't. Resisting extrinsic provocation is key to thread quality.
2 replies →
Strong agree; someone needs to give her like a million dollars a year salary and unlimited resources to explore whatever she wants for life, just to see what she comes up with.
or don't, because she produced these gems without any such incentives and whilst I only make the connection in a speculative fashion, the most offended individuals I've known to be upset at the suggestion that money is a necessary component of compulsory exchange or even coercive production, have all been women.
one millliiiooonnn dollars.... Hey Dr Evil (Austin powers reference), I think one billion is okay this year and age :)
but yea I agree
It's funny because the high-level introductions to C I would read as a kid before I really knew much about computing would almost always start out by explaining that one of the biggest strengths of C was its portability, and how it allowed you to write the same code that worked on every platform. Of course I came to learn that's true only in a sense that has no connection to practical reality, this looks like an exception.
Could you please explain to someone from the web domain with very little experience with compiled C programs why this is significantly better than, say, distributing a python program? Python runs on all the platforms mentioned. Or alternatively requiring compilation from source?
The python script/program ultimately depends on a Python interpreter which currently has to be distributed as a different binary for every platform.
So in the end, the difference is purely where the platform-differentiated compilation happens for VM & interpreted languages like Java & Python.
This would instead allow you to ship a single Python executable that simply works everywhere. Whether with a bundled-in script/program, or just the interpreter.
While this is absolutely amazing, and I'm still trying to see if I'm even able to grok the how..
But, the paranoid in me is now curious/afraid if this approach can/will lead to multi-OS malware?
That sort of malware already exists. This does simplify making it by a lot, because now it can much more easily ensure its portability without external communication.
I really don't understand internals ibut I've often downloaded cygwin exe file for commands like grep, tail, etc.. Are you telling me that is no longer required?
On linux, I downloaded the printvideo binary and played a friggin crab video on my terminal after just a chmod. I don't have any other OS to confirm, but it sure looks like it if someone compiles/links coreutils against this.
Wow, that cosmopolitan C library is absolutely awesome. The ABI specialization is extremely interesting. Never seen anything like that before. I thought compilers did that. I don't understand why they couldn't properly optimize memcpy.
I wonder how it handles system calls. Let me check the source...
https://github.com/jart/cosmopolitan/blob/master/tool/build/...
https://github.com/jart/cosmopolitan/blob/master/tool/build/...
> void OpSyscall(struct Machine *, uint32_t);
Pretty interesting... Need some time to figure out how it works.
The system call entry points:
https://github.com/jart/cosmopolitan/blob/master/libc/sysv/s...
https://github.com/jart/cosmopolitan/blob/master/libc/sysv/s...
It uses a jump slot technique which is interesting:
https://github.com/jart/cosmopolitan/blob/master/libc/sysv/m...
System call numbers:
https://github.com/jart/cosmopolitan/blob/master/libc/sysv/c...
https://github.com/jart/cosmopolitan/blob/master/libc/sysv/s...
Looks like it packs the system call numbers of all operating systems into a single constant.
For some reason there are some dedicated implementations of Linux system calls using inline assembly rather than the entry point:
https://github.com/jart/cosmopolitan/tree/master/libc/linux
More related files:
https://github.com/jart/cosmopolitan/blob/master/libc/calls/...
https://github.com/jart/cosmopolitan/blob/master/libc/calls/...
Also funny how it calls Linux "GNU/Systemd"!
"...it calls Linux "GNU/Systemd""
Yeah, it would have taken less bits to use GNU/POS instead, because it ain't Linux anymore.
Poettering Operating System.
Will this work if your code uses the C++ standard library ? Or is this just for pure C ?
The are a few options but all require some effort https://github.com/jart/cosmopolitan/issues/27
Impressive.
That malware for OS X that does nothing (so far) from the news earlier this week also has one multi-arch binary for all modern Apple notebook architectures (AMD64 and ARM64)
In MacOS this is really just a feature of using Mach-O’s multi architecture abilities, they call this universal binaries.