Skip to main content

Module link

Module link 

Source
Expand description

Finding a linker and telling it what to link.

Design: spec/04-driver-and-cli.md section 4.9. There is no linker of our own before 1.0, so this finds one on the machine and builds the command line it wants.

The linker is invoked directly rather than through the system compiler driver. Going through cc would be shorter to write and would borrow that compiler’s idea of where everything is, and it would also mean this compiler cannot link on a machine that has no other compiler on it, which is most of the machines a compiler ends up on. It would also make -### output a line that does not say what happens, since the interesting half would be inside the program being spawned.

§What is not decided here

The startup files and the library directories are looked for rather than configured, for the same reason library looks for the headers: gcc settles this when it is built because a gcc is built for the machine it will run on, and this is one binary that runs wherever it is copied. So the shape of the answer is a list of candidates per platform of which the ones that exist are taken, and a cross build says where the rest is with --sysroot.

§The compiler’s own runtime

crtbegin, crtend and the runtime libraries are found the same way, on the machine rather than by configuration. Ours is librucc_builtins.a, looked for beside the compiler, and the machine’s libgcc goes on after it for the parts we have not written, which today is the unwinder and its personality routine. The C library goes in front of both, so that on a target that has one its memcpy is the one that answers rather than ours. -fno-builtins-lib leaves ours off, for somebody who wants libgcc to answer for everything.

On a static link the three archives go inside --start-group, because libc.a refers to the unwinder and the unwinder refers back to libc.a, and a linker walking a list once resolves whichever of the two it reaches first and leaves the other undefined. That circularity is the whole reason -static failed before this, and it is issue #277.

§What is not here yet

Darwin and Windows. ld64 wants a different line, a platform version load command and a different set of default libraries, and link.exe wants another one again. Each arrives with the target that needs it.

Structs§

LinkOptions
What the command line said about linking.
Linker
A linker, found.

Enums§

Error
Why a link could not be run.
Item
One item on the link line, in the order it was written, because link order is semantic.

Functions§

builtins_archive
Our own runtime library for this target, if it was built.
candidates
Where the library’s own files might be, in search order.
find
The linker to use, looked for where a linker is.
line
What the linker is told, in order, not counting the linker itself.
order
The names to look for, in the order section 4.9 gives.
render
The whole invocation as one line, quoted the way -### prints it.
run
Runs the linker and waits for it.
runtime_dirs
Where a gcc on this machine keeps crtbegin.o, crtend.o and libgcc.a, newest first.