News for hackers. Stuff that matters.
Re:Actually Portable Executables by mikewarot on Sunday February 28, 2021 @07:23AM (#26292884)
I'm an old Delphi/Pascal application programmer here, trying to get the lay of the land in the C/C++ world.

Why is there a forest of files in Cosmopolitan Libc? I tried looking at it on Github to see how things were done, and there were a lot of .h and .S files, I couldn't actually find the C source, though I'm sure somewhere in there is a .c file.

Doesn't fragmenting the source into thousands of files make compilation far slower than it needs to be?

Also, I wonder if/how functions that aren't called in a program get trimmed away by the linker, and thus don't make the executable larger.

Re:Actually Portable Executables by jart on Sunday February 28, 2021 @07:44AM (#26293009)
It takes about 18 seconds to build the entire Cosmopolitan repo and run all its unit tests on my PC. During that time the make command builds 14,376 .o files, 66 .a archives, and 421 .com executables. It's an exceedingly fast build config.

Having lots of objects is a good thing because it helps static linking work better. When the Unix linker loads a symbol from a static archive, it pulls in the whole module that defines that symbol. For example, if you define memcpy() and memset() in the same .c file and then your app only needs one of them, they'll both go towards bloating your binary. Workarounds exist like -ffunction-sections and -Wl,--gc-sections but a C library should make assumptions about the fewest flags feasible.

What's the end result? We're able to build executables that are 12kb in size which run on seven different operating systems. The big codebase is what made tiny binaries possible: https://justine.lol/cosmopolitan/howfat.html

Re:Actually Portable Executables by matheusmoreira on Sunday February 28, 2021 @08:22AM (#26293241)
Do you build on tmpfs? I started doing that and it makes builds ridiculously fast. $XDG_RUNTIME_DIR is perfect for this, I just make a directory there and symlink the build directory to it. Even added some makefile logic to ensure the link target exists:

  build_directory_link := $(shell readlink $(build_directory))
  $(if $(build_directory_link),$(shell mkdir -p $(build_directory_link)))
Re:Actually Portable Executables by ed25519FUUU on Sunday February 28, 2021 @09:56AM (#26293786)
Good optimization, thanks for the pointer!
Re:Actually Portable Executables by matheusmoreira on Sunday February 28, 2021 @09:30PM (#26299101)
It also reduces wear on storage devices. Why write temporary build artifacts to permanent memory after all? Gotta use those 32 GB of RAM for something.

Linux will automatically cache all source files in RAM because of the page table cache. Writes to slow storage devices are eliminated through tmpfs. Amazing really.

Re:Actually Portable Executables by wyldfire on Sunday February 28, 2021 @12:02PM (#26294757)
Also, if you are using BFD ld, you should consider gold or lld. Significantly faster if the link is the slow step.

Also much lower peak memory usage for lld, which is great if you have multiple concurrent link steps.

Re:Actually Portable Executables by krab on Sunday February 28, 2021 @08:14AM (#26293193)
May I ask - what was the reason for creating APE/Cosmopolitan?

I read your post about actually portable executable format but I wonder if it's something that you found immediately helpful for some project or if you work on it just out of curiosity.

Re:Actually Portable Executables by moonchild on Sunday February 28, 2021 @04:04PM (#26297046)
> Doesn't fragmenting the source into thousands of files make compilation far slower than it needs to be?

This allows incremental compilation to work much better than it otherwise would. Your first clean build may be slightly slower, but after that, you only have to recompile the files you change.

Re:Actually Portable Executables by simias on Sunday February 28, 2021 @10:51AM (#26294183)
Compiling ASM and even C is very fast these days. No generics, small compilation units etc... Even with optims it's usually a non issue in my experience.

For a datapoint it takes me ~2 minutes to build a reasonably fully featured ARM linux kernel with a cold cache on a 5 year old i5. wc counts 1399 CC, 65 AS and 411 LD calls. And of course incremental rebuilds are only a fraction of that.