Another weird LLVM mystery solved!

So, I was porting LDC to FreeBSD/aarch64, wondering why global constructors (you know, the before-main() code you can make in C using an __attribute__ thingy) aren't running… but only when the executable is linked with LLD. Turns out:

  • .init_array is the only supported way to do constructors on AArch64
  • and everything in general is moving towards .init_array — but the default in LLVM is still to emit .ctors
  • clang has code to enable .init_array when appropriate, ldc did not
  • and the reason it all worked fine with bfd and gold is that these linkers SILENTLY convert .ctors to .init_array. For PERFORMANCE REASONS.