|
|
Subscribe / Log in / New account

Truly portable C applications

By Daroc Alden
November 13, 2024
Note: This topic was chosen based on the technical merit of the project before we were aware of its author's political views and controversies. Our coverage of technical projects is never an endorsement of the developers' political views. The moderation of comments here is not meant to defend, or defame, anybody, but is in keeping with our longstanding policy against personal attacks. We could certainly have handled both topic selection and moderation better, and will endeavor to do so going forward.


Programming language polyglots are files that are valid programs in multiple languages, and do different things in each. While polyglots are normally nothing more than a curiosity, the Cosmopolitan Libc project has been trying to put them to a novel use: producing native, multi-platform binaries that run directly on several operating systems and architectures. There are still some rough edges with the project's approach, but it is generally possible to build C programs into a polyglot format with with minimal tweaking.

Actually portable executables

Justine Tunney, the creator of the project, calls the polyglot format she put together "αcτµαlly pδrταblε εxεcµταblεs" (APEs). Every program compiled to this format starts with a header that can simultaneously be interpreted as a shell script, a BIOS boot sector, and macOS or Windows executables. This lets APEs run across Linux, macOS, FreeBSD, OpenBSD, NetBSD, Windows, and bare metal on both x86_64 and Arm64 chips. When interpreted as a shell script, the program detects which architecture and operating system it is running on, and (by default) overwrites the first few bytes of the program on disk with an ELF header pointing to the code for that architecture and system, before re-executing itself. This does mean that the binary is no longer portable, but it can be restored by overwriting the ELF header again.

By building separate versions of the program for different architectures and then combining them with this polyglot trick, the project promises portability between different architectures without the overhead of an emulator or bytecode virtual machine. This approach does have downsides — APEs are larger than normal C binaries, although still smaller than many produced by other languages — but for projects where the portability is a benefit, the tradeoff may be worth it.

Still, at the cost of increased compile time and binary size, APEs nearly hold up their promise of running across all major operating systems. There are some systems where they don't run correctly. The biggest source of problems is, ironically, one of Linux's tools for handling foreign binaries: binfmt_misc. In typical configurations, the Linux kernel knows how to run two kinds of binaries: ELF files and shell scripts. The binfmt_misc mechanism lets the user register additional types of program to be run with special helpers. When correctly configured, binfmt_misc can let users transparently run programs for other architectures under QEMU, or run Windows programs under Wine — which is precisely the problem. On Linux systems configured like that, APE programs get run under Wine, instead of running natively.

Luckily, the workaround is quite simple: just add a more specific binfmt_misc rule for APE binaries. This also has the advantage of saving the extra startup latency of first invoking the program as a shell script and then again as a binary. Another problem for APE programs is that some older shells (such as Zsh before version 5.9, released in 2022) don't handle binary data being part of shell scripts well. Because of these problems, the documentation actually recommends installing a binfmt_misc handler, even though it is not strictly necessary on most systems.

The tooling

Producing programs in APE format is relatively straightforward. Stock GCC and Clang can both be configured to do so, with the appropriate headers and linker scripts. But there are a number of configuration flags to get right, so the Cosmopolitan Libc project has also created cosmocc, a wrapper around both GCC and Clang that takes care of building for multiple architectures and linking them together into an APE.

Cosmocc is, itself, an APE. So interested readers can download it and experiment with compiling their own software. Like all APEs, it is statically linked, and so can just be unpacked and run in place. Most C programs should build relatively painlessly under cosmocc. One potential stumbling block is that any dependencies also need to be built for multiple architectures, so relying on distribution packages is unlikely to work.

The Cosmopolitan Libc project does include several common dependencies, such as ncurses, zstd, zlib, SQLite, and others in its repository. So if a project only uses common third-party dependencies, it is usually fairly straightforward to build the necessary libraries. For projects that use GNU Autotools, the superconfigure project provides tools for integrating Cosmopolitan Libc into the build process.

One obvious question when building programs that should run across multiple operating systems is how to translate system calls between platforms. Unfortunately, a complete solution is simply not possible — some operating systems support operations that just aren't available on others — but Cosmopolitan Libc does a good job of covering the common operations. The documentation lists exactly which functions are available on which platforms. Generally, most common POSIX operations are usable without issues. Any more recent, specialized, or obscure APIs may require the use of the C preprocessor to select the right implementation. That is, of course, the normal state of affairs for portable software. But at least with Cosmopolitan's polyfills, programs can focus on only doing that for the small portion of functions where it is truly necessary. These functions are where Cosmopolitan Libc gets the "Libc" portion of its name from; while the tooling does support linking programs with the GNU C library, musl libc, or any other local C library, the usual case is to link to Cosmopolitan's Libc.

I attempted to build several pieces of software (GNU Bash, TCC, and some software of my own) with the Cosmopolitan toolchain in the course of writing this article. Overall, it was remarkably straightforward. I ran into a few confusing compiler errors until I found the correct set of configuration flags, but after configuring the projects to expect static linking and to use cosmocc and its associated linker, everything did work as promised.

Additional facilities

The fact that Cosmopolitan has to be involved at every step of the building and linking process in order to produce APE files allows it to pull a few other tricks, as well. In addition to being an executable and a shell script, every APE file is also a Zip archive. This is due to the fact that Zip archives store their directory information at the end of the file, so the information can be appended to the APE binary without interfering with the already-overloaded first few bytes of the format.

A few of Tunney's other projects take advantage of this, including redbean, llamafile, and others. Redbean is a web server that serves dynamic web sites made using Lua and SQLite from its own Zip archive; the project lets programmers distribute these web sites as self-contained portable executables, suitable to be run by end users. Llamafile, which LWN covered earlier this year, is a project that packages LLM weights alongside the code required to run them, for ease of distribution and archiving.

Other projects have used the ability to treat APE executables as Zip archives to bundle data files like a copy of the timezone database as a sort of super-static-linking that ensures the program always has the resources that it needs to run. Data files aren't the only thing required to be distributed alongside programs, however — many licenses also require distributing copies of the license, or copyright attributions.

Cosmopolitan Libc's tooling automates compliance with those licenses by making sure that licenses and attributions for all of the Libc code or (supported) third-party libraries that are linked into the final APE file are embedded in a special linker section. Cosmopolitan Libc itself is licensed under the ISC license (a simplification of the MIT/BSD licenses), but some of the third-party components that Cosmopolitan either uses directly or makes available to compiled programs are licensed under the BSD, MIT, or Apache 2.0 licenses. The Cosmopolitan compiler itself includes GPL-licensed code from GCC, but all of the code linked into finished binaries is licensed under a permissive license (unless one chooses to statically link the GNU C library), so the final result is not covered under the GPL.

Cosmopolitan Libc is a fairly active project; it has regular, small releases. The most recent is v3.9.6. As might be expected for any project with such ambitious compatibility goals, there are usually a number of small bugs affecting different platforms open at any given time. The project is fairly stable, however, with the basic ability to compile programs across multiple systems unchanged since the initial release.

So, while the project does still have its rough edges, it's a promising tool for people who want to write highly portable software. The automated license compliance, static compilation, and easy bundling of non-software dependencies are nice additions to the already compelling idea of running the same binaries on multiple platforms.



to post comments

What about Zig?

Posted Nov 13, 2024 17:56 UTC (Wed) by prittner (subscriber, #110534) [Link] (1 responses)

I'm not a fan of the Zig language at all, but there have been similar discussions about using it as a cross platform C tool chain: https://ruoyusun.com/2022/02/27/zig-cc.html

I wonder how this approach has evolved in the last two years and how it compares to Cosmopolitan?

What about Zig?

Posted Nov 13, 2024 18:02 UTC (Wed) by mathstuf (subscriber, #69389) [Link]

I think `zigcc` can generate platform-specific binaries from anywhere (like a cross-compiler). `cosmocc` is, rather, making a "target" that executes on any of up-to-12 platforms as a single binary.

Overwriting an executable?

Posted Nov 13, 2024 18:18 UTC (Wed) by jorgegv (subscriber, #60484) [Link] (4 responses)

I can imagine all sorts of warnings and alerts coming from security software, antimalware, audit systems, etc. the very first moment an executable overwrites itself...

Also, I suppose this does not work either in inmutable storage (e.g. container images), or storage that is measured and/or authenticated (e.g. dmverity).

I appreciate the effort, but I'm not sure this way of achieving multiplataformas execution is in line with current developments... Can this "self overwrite" thing be deactivated, even at the cost of a slightly higher startup latency? And have them work like old MacOS universal binaries, which had both Intel and PowerPC executables on the same file....

Overwriting an executable?

Posted Nov 13, 2024 18:39 UTC (Wed) by daroc (editor, #160859) [Link] (1 responses)

If the binary can't overwrite itself for whatever reason (or has been configured not to at build time), it can instead write a modified copy of itself to a temporary location and invoke that.

Overwriting an executable?

Posted Nov 23, 2024 18:54 UTC (Sat) by ssokolow (guest, #94568) [Link]

If the binary can't overwrite itself for whatever reason (or has been configured not to at build time), it can instead write a modified copy of itself to a temporary location and invoke that.
That assumes the target system hasn't been configured so that all user-modifiable locations are mounted noexec as part of a larger security policy.

Overwriting an executable?

Posted Nov 13, 2024 20:01 UTC (Wed) by mathstuf (subscriber, #69389) [Link]

> And have them work like old MacOS universal binaries, which had both Intel and PowerPC executables on the same file....

The Mach-O format explicitly supports having multiple architectures in the binary. There is/was an effort for a "FatELF" format to do this, but it has, AFAIK, never made much of an impact.

Overwriting an executable?

Posted Nov 16, 2024 7:00 UTC (Sat) by Lennie (subscriber, #49641) [Link]

I think it's a good thing that it needs to overwrite itself, because that means it can't be used as a virus if the admins configure the system directly and only allow programs that are installed by the admin. Because if you have an installer on Windows or a package manager on Linux/Unix then they could do that 1 step to install it correctly.

Linux kernel patch for handling APEs natively

Posted Nov 13, 2024 19:22 UTC (Wed) by Lionel_Debroux (subscriber, #30014) [Link]

Justine Tunney has also produced a Linux kernel patch for directly loading the ELF part of APEs: https://justine.lol/ape.patch .

jart must have had fun

Posted Nov 13, 2024 20:18 UTC (Wed) by pbonzini (subscriber, #60935) [Link] (2 responses)

The sources to the header of the APE format are completely crazy:

https://raw.githubusercontent.com/jart/cosmopolitan/1.0/a...

jart must have had fun

Posted Nov 13, 2024 23:09 UTC (Wed) by Cyberax (✭ supporter ✭, #52523) [Link] (1 responses)

I can't decide whether it's great or horrifying.

jart must have had fun

Posted Nov 14, 2024 3:20 UTC (Thu) by mirabilos (subscriber, #84359) [Link]

I discovered cosmo some months ago, and I am (still) both at the same time.

It’s something I would do, just even more… (and on more contemporary platforms than I do… I do i386+sparc32 and things like that).

a question of manners

Posted Nov 14, 2024 15:47 UTC (Thu) by jubal (subscriber, #67202) [Link] (22 responses)

did ms. tunney finally stop using antisemitic or racist dogwhistles for her projects now that she's pretending she's not a techno-fascist anymore?

stop this right here

Posted Nov 14, 2024 16:01 UTC (Thu) by jake (editor, #205) [Link]

This kind of comment has no place here. Please refrain from this kind of thing. I have placed you into moderated mode to help ensure that you stay "polite, respectful, and informative".

jake

a question of manners

Posted Nov 14, 2024 19:25 UTC (Thu) by dskoll (subscriber, #1630) [Link] (18 responses)

Parent comment was a bit shocking, but prompted me to do some online searches and... huh... it's not very pretty.

Not relevant

Posted Nov 14, 2024 19:40 UTC (Thu) by jake (editor, #205) [Link] (17 responses)

Justine's views in unrelated areas are not relevant to this article.

thanks,

jake

Not relevant

Posted Nov 15, 2024 19:59 UTC (Fri) by lunaryorn (subscriber, #111088) [Link] (16 responses)

They absolutely are relevant.

You can't just "split" the public appearance of a person into the nice things you'd like to report about, and the "oh we'd rather not talk about these" things. It's still the same person saying those things.

If you talk about light you better talk about the shadows, too.

You choose what you want to report on, you choose whom you'd like to give a platform. If you don't want to talk about shadows, don't start writing about light. But if you write about light and then aggressively pretend shadows don't exist, that's just dishonest.

It's not as if we're talking about a person with just a few oddities and some mildly annoying views...

Not relevant

Posted Nov 15, 2024 20:39 UTC (Fri) by kleptog (subscriber, #1183) [Link] (4 responses)

Sure you can. The technology presented in the article doesn't become more or less useful or interesting based on whatever views someone holds elsewhere.

I think it's perfectly reasonable for a technology site to stick to the technology. This is not someone anyone is likely to meet or interact with, so whatever else they do is rather irrelevant to know.

Not relevant

Posted Nov 15, 2024 21:44 UTC (Fri) by atnot (subscriber, #124910) [Link]

Right, it's fine as long as the trains are on time

Not relevant

Posted Nov 15, 2024 22:16 UTC (Fri) by lunaryorn (subscriber, #111088) [Link] (2 responses)

I respectfully disagree. Sure, "let's stick to technology" and all that, but this just isn't a person who's just holding some mildly annoying views or does some slightly weird things.

This is someone for whom even a dead simple google search immediately brings up views and statements that I can only describe as borderline fascistic. Where I'm from most of that stuff would be absolutely unacceptable and intolerable and probably even prosecutable. And it's someone who's done political activism, who themselves haven't been silent about their opinions at all, in technology contexts. It's someone who talks loud and wants to be heard.

That's just an wholly different level of escalation, and I think this is one of the cases where I definitely consider the state of affairs long past "let's just talk technology". Some things are more important than technology, and sometimes a site about technology should definitely talk about the wider context.

Not relevant

Posted Nov 16, 2024 8:57 UTC (Sat) by atnot (subscriber, #124910) [Link] (1 responses)

I think this brings up a good point, which is that "sticking to technology" only works if it's mutual. You know who isn't "sticking to technology"? People like Justine Tunney.[1]

I certainly don't go around personally vetting the beliefs of every maintainer of software I use against my own. But when you "stick to technology" and uncritically promote the work of a controversial figure without context, and that person then turns around and uses that legitimization and notariety to promote fascism, the net effect of that can only be promoting fascism. And you shouldn't be surprised when people take issue with that.

[1] Even within just the context of this article, it should be noted that "cosmopolitan" has been historically used by antisemites as stand-in for "jewish" and given her history I would expect this played at least some factor in the naming choice (see e.g. https://en.m.wikipedia.org/wiki/Rootless_cosmopolitan). In fact the resemblance between this antisemtic trope and the design of her libc is pretty uncanny.

Not relevant

Posted Nov 16, 2024 14:18 UTC (Sat) by jake (editor, #205) [Link]

> I certainly don't go around personally vetting the beliefs of every maintainer of software I use against my own.

Right. And we don't either.

That's why this line of comments is not relevant to this article. As asked before, please stop here. Not just you, but the whole sub-thread.

thanks,

jake

Not relevant

Posted Nov 16, 2024 13:45 UTC (Sat) by dankamongmen (subscriber, #35141) [Link] (10 responses)

for whatever it's worth, i worked with justine at google-ny, and she was one of the finest people (and technologists) i met in my years there. i consider myself lucky to call her a friend.

Not relevant

Posted Nov 16, 2024 15:53 UTC (Sat) by atnot (subscriber, #124910) [Link] (9 responses)

Hi Nick, since you were lending your credibility to Justine and I wasn't familiar with what that credibility was, I thought I'd give it a quick check.

When you made some recent comments on reddit, for example saying that you couldn't imagine someone thinking "black america" was admirable or something to look up to[1] and that you didn't want North America to look like South Africa[2], what did you mean by that?

It seems like your comment stating, in reference to Americans' role in Israel's occupation of palestine, that "if I could, I would annex other planets"[3] was considered too far by the moderators so I won't question you on that.

You also seem to have an odd penchant for bringing up the "Turner Diaries", a book which describes a purely hypothetical (wink wink) future purge of jews, non-whites and "liberals" in the US, described by some as the most hateful book ever written. I also gather from context that you personally own a copy of it. I would rather not speculate as to why that is.

I would also usually look past your use of the neo-nazi stock phrase "this is what they took from us", as it is sometimes used mockingly, but given the previous context I do find it somewhat concerning. As are the streaks of covid denialism and also the virulent transphobia, but I guess that just comes with the territory.

I'll stop there but given this brief look, I'm not surprised you think very highly of her.

[1] https://www.reddit.com/r/self/comments/1gmnrhf/comment/lw...
[2] https://www.reddit.com/r/AskHistory/comments/1fh96qp/comm...
[3] https://www.reveddit.com/v/worldnews/comments/1fafc9b/ame...

Not relevant

Posted Nov 16, 2024 16:38 UTC (Sat) by jake (editor, #205) [Link] (8 responses)

This is not the place for this. I have put atnot into moderated mode, which is something we really do not like to do.

Everyone else, please drop this. It is not relevant.

jake

Not relevant

Posted Nov 16, 2024 17:48 UTC (Sat) by mgb (guest, #3226) [Link] (7 responses)

Punishing criticism of racism is never a good thing.

Not relevant

Posted Nov 16, 2024 18:06 UTC (Sat) by corbet (editor, #1) [Link] (4 responses)

This is not a site for people to snipe at what others have posted on reddit. It is not a site for personal attacks in general.

There is a lot to criticize in the world right now. Trust me, I live in the US, I'm well aware of it. But if we let that discussion take over LWN, we won't have LWN anymore. There are plenty of other places where these discussions can be had.

As a postscript...

Posted Nov 16, 2024 18:11 UTC (Sat) by corbet (editor, #1) [Link] (3 responses)

We get it: people wish that we had not highlighted work by this particular author. Had we known more about the person in question, we might have shied away from the topic. But the article is out now, it describes a bit of interesting technology, people have had their say, please let's leave it at that.

As a postscript...

Posted Nov 16, 2024 22:07 UTC (Sat) by mgb (guest, #3226) [Link]

LWN is one of the least crazy forums on the Intertubes. Thank you.

But off-topic stuff has been posted here occasionally. And it was rightly criticized by your moderators.

What I have never seen before is two people punished in this way. It is not a good look for LWN that in both cases the people were punished for criticizing racism.

My personal preference would be for the article, the punishments, and all associated comments to be expunged.

As a postscript...

Posted Nov 17, 2024 14:32 UTC (Sun) by jart (guest, #171513) [Link] (1 responses)

I'm also an LWN subscriber. I was so happy when I saw that Daroc took the time to put together a second, very well researched article that covered many subtle facets of Cosmopolitan Libc that typically go unnoticed. In the four years I've been working on this project in public, more effort went into this piece than any I've seen so far. However this comment section is is not the kind of attention I want. While I can't thank you enough for the support you've provided, not moderating these kinds of comments probably does more to hurt my career, since this will likely be the part that folks remember.

I understand there are concerns here about the name of my project. For what it's worth, I named it "cosmopolitan" in the sense of cosmopolitan liberalism, which I define as wanting to feel at home in every nation, and the reason why I did that is because my project builds executables that aim to feel at home on (almost) every operating system. I created tools that help you build programs that overcome artificial barriers so you can have impact helping more communities, while respecting the rules each platform put in place. If anyone has actionable feedback on how I can do that better, then I'm more than happy to listen.

Note:

Posted Nov 17, 2024 14:34 UTC (Sun) by corbet (editor, #1) [Link]

I approved the above comment because, given that the poster has been targeted for extensive criticism here, to do otherwise seemed unfair. This is not an invitation to restart the discussion, though; it needs to stop here.

Not relevant

Posted Nov 16, 2024 22:06 UTC (Sat) by atnot (subscriber, #124910) [Link] (1 responses)

Yeah so, for the record, I do fully accept I overstepped my bounds here and I'm sorry about that.

The point I meant to illustrate was that a large contingent of the people who claim to support Justine for her technical achievements are in fact just racists and antisemites and are generally very bad at hiding it. But I could and should have just made that point directly instead of launching a barrage of personal attacks at some random person who just happened to make a tantalizingly good case in point. I also did not mean to make such insinuations about any LWN staff, to be clear.

I do agree with you in the general case that this website tends to punish e.g. calling out racism far harder proclaiming your support for racists or making racist statements. And I would still encourage anyone to ponder the the long-term implications of such a policy. But in this case I accept that the moderator action against me specifically was warranted.

I also consider Corbet's position ("we didn't know and might have handled things differently") much more satisfactory than the previous one ("we don't care please shut up"). It is perhaps the latter that led me to put a finer point on it than I should have, and with the further clarification I do consider that resolved, not that that excuses it.

Not relevant

Posted Nov 16, 2024 22:16 UTC (Sat) by corbet (editor, #1) [Link]

The reason you don't see people called out for making racist statements here is ... because that doesn't really happen much here. For something that overtly discriminatory, you may never see it at all; that is exceedingly rare, but there was one episode recently.

The people we addressed did not get that attention for "calling out" or "criticizing" racism. It was because they pointedly ignored our "no personal attacks" policy. In your case after multiple requests to stop. We have been consistent about that policy for many years; please do not try to make it into something that it is not.

It is time for this discussion to end, let's do that here, please. If you wish to discuss our moderation policy with us, send us a note at lwn@lwn.net and we can talk further that way.

a question of manners

Posted Nov 15, 2024 9:05 UTC (Fri) by atnot (subscriber, #124910) [Link] (1 responses)

I was not aware of this and that's good to know, thanks for mentioning it

a question of manners

Posted Nov 15, 2024 10:35 UTC (Fri) by paulj (subscriber, #341) [Link]

I too would prefer to know than not know. Thanks.

"Tech" isn't the only consideration

Posted Nov 30, 2024 1:56 UTC (Sat) by faramir (subscriber, #2327) [Link]

TLDR: Interesting technology, probably not one I would want to depend on long term without some non-tech research.

As someone who has been around the FOSS world for a while, I often see interesting new technologies that don't become an integral part of the software ecosystem that I can depend on. There can be many reasons for that to happen that have nothing to do with the quality of the technology. For example, many projects have a 'bus factor' of close to one. ReiserFS is an example of something that went away for that reason. I haven't determined the veracity of the concerns raised in this case, but I appreciate those who pointed them out. Because (if true), they could threaten the long term viability of the software completely independent of any personal moral issues that I might have.


Copyright © 2024, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds