TIL

Quoting skitter:

Wow, Collect2 sounds kinda hacky based on the linked documentation:

  • There is user code that needs to run before main. With C/C++ allowing separate compilation, the compiler invocation that creates the entrypoint doesn't know about all of these static constructors.
  • Therefore collect2 pretends to be ld; when invoked, it tries to call the real ld and examines its output
  • It creates a c file with a table containing any constructor symbols it finds, then compiles that
  • Finally it links the program again, together with the new object file

Another commenter says that the documentation lies, but the source code [4] doesn't.

Sources: