- This was intended to be a series describing trends in programming language
- technology and how they relate to an increase in the average size of
- executable files. It never materialized beyond this initial post.
-
- And, as of 2022, I find it unlikely that I will come back to complete the
- series. We have seen a renewed interest in linkers and loaders
- (cf. mold), but I never
- intended to discuss linkers and loaders in this series beyond the purpose of
- ELF metadata. It warrants treatment, but I would like to approach it without
- the baggage of this first post.
-
- As for the idea that trends in compiler technology are affecting binary
- size: it is not as interesting as one may think. The bottom-line is that
- modern technologies tend to bring a runtime along with them, often more
- accommodating than the C runtime. The binary necessarily contains this
- runtime and is therefore larger. I don't think it's as much of a problem as
- folks make it out to be, and my past self seems to be somewhat cognizant of
- that.
-
-#+END_EXPORT
-
-It seems that static linking is back in style, or at least popular among all the
-hip new programming languages of today. I don't have anything against statically
-linked binaries, nor do I have a problem with larger executables, but I've
-noticed that the acceptable size for an executable is a lot larger now than it
-was a few years ago; that is, the new kids on the block have significantly more
-leeway than their predecessors. For example - a C program that spits out "hello
-world" is 7 KB when statically linked to musl. It's 12 KB when dynamically
-linked to glibc. The same program in D, where the reference compiler doesn't
-allow dynamic linking to the standard library, is 896 KB. A blog post I read
-recently about certificate chain verification in Go made a point of praising the
-toolchain for being able to spit out a binary that was "less than 6 MB!" I'm
-being more facetious than with my D example, as this was statically linked to an
-SSL-capable web server, but 6 MB is a little over half the size of a
-[[https://en.wikipedia.org/wiki/Tiny_Core_Linux][fully-functioning operating system]]. I'm not so interested in why we settle
-binaries the size of a few videos, but instead I'd like to look at why they're
-that large to begin with. To peer in and see what wealth of information is
-stored inside, and how certain programming languages make use of that
-information.
-
-Perhaps we should first take a step back. What is a binary, anyway? It's a
-structured format, not much different than your typical PNG or Ogg file,
-containing some machine code instructions and directives for how the program
-should be loaded into memory. The task of parsing the binary and actually
-loading it is done by a *loader*, though that's a pretty broad term. My favorite
-book on this subject, /Linkers and Loaders/ by John R. Levine, defines a loader
-as a program to "copy a program from secondary storage (which since about 1968
-invariably means a disk) into main memory so it's ready to be run. In some cases
-loading just involves copying the data from disk to memory, in others it
-involves allocating storage, setting protection bits, or arranging for virtual
-memory to map virtual addresses to disk pages."
-
-Loaders are everywhere, as you can probably imagine. Maybe you've heard of a
-boot loader; those are for getting a kernel into memory from the strange and
-unfamiliar land of x86 real mode. Whenever you run a program on Linux, it's
-loaded by the kernel's ELF loader, of which you can find the source code for at
-[[https://github.com/torvalds/linux/blob/master/fs/binfmt_elf.c][=fs/binfmt_elf.c=]] of the kernel source tree. On a higher level, something like
-Java has a class loader for getting bytecode into memory so that the JVM can run
-it.
-
-As our first step into the world of loaders, we'll write our own. A very basic
-one, at that. I think that because we're taking a look at how much information
-can be stored inside of a binary, we should begin with the absolute minimum. It
-won't use a structured format, and won't set up memory beyond the stack and a
-page for executable code, but not at a specified address of any sort. Where that
-code exists in memory isn't known to the program, and it only really knows where
-the stack is from the =%rsp= register. We'll simply load some machine code from
-a file, and execute it. I'll spare you the per-line explanation I usually give,
-since it's reasonably simple and the only part you might not understand already
-is explained through comments.
-
-#+BEGIN_SRC c
-#include