Anatomy of a Binary Executable
oswalt.dev
oswalt.dev
Some nitpicks from someone who spends most of their professional day looking at machine code:
* "Execution section" is a weird phrase -- we'd normally talk about a specific section containing machine code (e.g., the `.text` section), or a collection of executable segments (i.e., regions that will be mapped with executable permissions at program load time).
* It's a stretch to call x86 "hardware independent." It has two major vendors (with a few peripheral ones), and is both complex and deeply generational (in terms of features being frequently added) as far as ISAs go. JVM bytecode is closer to hardware independent; LLVM IR can be hardware independent but frequently isn't in the real world.
* Rust and LLVM might not have done this because you ran a debug build, but I actually would have expected your `sum` function to have no stack allocations at all -- it's both small enough to fit within the red zone[1] and is also trivially promotable to registers. Sure enough, with `opt-level=1` or above, the compiler gets very clever and actually uses a `lea` to perform the sum[2].
Used for explicitly requesting that the stack is executable (note in the output above, this bit is not set)
...and 42(!) section headers in a binary definitely seems overkill; a normal Windows PE file has less than half a dozen.
Nonetheless, the ELF format can be trimmed down considerably:
https://www.muppetlabs.com/~breadbox/software/tiny/teensy.ht...
And of course many of those 42 ELF sections are for debug info. On Windows debug information is usually stored in a separate file (.pdb), which also makes the comparison unfair. I have to admit though that Windows' handling of debug symbols is much more useful (especially when you have a symbol server).
The whole article confuses the question of "what will a 32-bit x86 Linux kernel load?" with "what is the smallest ELF file I can make?"
As eatingCake already mentioned this is not optimized for release, but for debugging.
This is not a discussion of rust vs .NET. this is a discussion of Linux vs windows.
So I don't see how your comment is at all relevant.
If you compile the exact same rust code in the post on a windows machine, using cargo like in the post, then is the resulting windows executable file size really any different compared to the linux one?
See http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm... for a example of going very far (maybe even too far, once they start overlapping things, but YMMV) in the opposite direction.
If you don't understand how debug information works, or where the bits that provide your language, library, and OS runtime live, then you might well be surprised that they exist at all. It is, after all, sufficiently advanced technology.
Where else is there to go anyway, Forth running on a microcontroller?