Patching GCC to Build Actually Portable Executables

2023/07/14
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

In this article, the author discusses a patch they wrote for GCC to simplify building Actually Portable Executables with Cosmopolitan Libc. This patch allows developers to build popular software such as bash, curl, git, ninja, and even GCC itself with Cosmopolitan Libc without having to change the source code. The built executables can run on various operating systems including Linux, FreeBSD, MacOS, OpenBSD, NetBSD, and in some cases, even Windows. The author provides instructions on how to port your own software to Cosmopolitan Libc using the patch. They also explain the motivation behind the patch and how it fits into the conventional build systems like ./configure and cmake. The article concludes by discussing the process of porting software to Cosmopolitan Libc and the benefits of the GCC patch in simplifying this process.

Introduction

The article begins by mentioning that Lua was the first programming language to be ported to Cosmopolitan Libc, followed by Wren, Janet, and Fabrice Bellard's quickjs. The author highlights a common change that had to be made across these ports, which involved rewriting switch statements with system values like SIGTERM or EINVAL as if statements. They raised an issue on Github about this and received a response explaining the need for this change. The author then shares their experience of writing ports and realizing that the switch(errno) pattern is common and automating the bending of rules would speed up the software porting process.

Switch to If

The article dives into the technical details of the switch(errno) pattern and how it can be a challenge when porting software. According to the C standard, case labels need to be compile-time constants. If EINVAL is not a compile-time constant, an error would occur when compiling the code snippet with GCC. To overcome this, the author introduces their solution of automatically bending the rules by converting switch statements to if statements. They provide a code snippet to illustrate this approach.

Conclusion

In this article, the author presents a GCC patch that simplifies building Actually Portable Executables with Cosmopolitan Libc. They explain the process of porting software to Cosmopolitan Libc and how the patch fits into conventional build systems. The author also discusses the challenges faced when dealing with switch statements and their solution of automatically converting them to if statements. This patch is a significant step towards seamlessly building a wide range of software with Cosmopolitan Libc, making it easier for developers to leverage its benefits.