beginnerLinking

What does a linker do?

Whether the candidate has a model of separate compilation at all. The discriminating detail is relocation: candidates who have only read about linking say "it joins the files"; candidates who have debugged a link error know addresses had to be patched.

What a strong answer covers

  • It takes the object files and libraries produced by separate compilations and produces one executable or library. Concretely: it lays out the sections from each input into the output, builds a symbol table, resolves every undefined symbol against a definition somewhere in the inputs, and then performs relocations — patching the placeholder values the compiler emitted where it could not know the final address.
  • Relocation is the part that makes the rest make sense. When a compilation unit calls a function defined elsewhere, the compiler emits a call with a blank target and a relocation entry saying "put the address of foo here, in this encoding". The linker fills those in once it has decided where everything lives.
  • It also decides what to include: pulling only the needed members out of a static archive, discarding unreferenced sections, merging identical constants, and — with link-time optimization — running actual optimization passes across module boundaries that no single compilation could see.
  • A dynamic linker splits this in half. The static link leaves undefined symbols to be resolved when the program loads, records which shared objects to look in, and produces the tables — a global offset table, a procedure linkage table — that the loader and the runtime linker fill at load or first call. That is why the same symbol can resolve differently depending on link order, interposition or preloading.
✓ Green flags
  • Names relocation and explains it as address patching, not as a synonym for linking.
  • Distinguishes static from dynamic linking by *when* resolution happens.
  • Mentions symbol resolution order or archive semantics, which is where real link errors come from.
  • Knows the linker can do more than concatenate — dead section stripping, identical code folding, LTO.
  • Can describe what an undefined-symbol and a duplicate-symbol error each mean.
✗ Red flags
  • "It combines the object files into one file." True and empty — it does not explain why link errors exist or why order matters.
  • "The linker compiles the code into machine code." Machine code was already emitted; the linker patches and arranges it.
  • "An undefined symbol means the header was missing." The header satisfied the compiler; the definition is what is missing, which is why the error is at link time and not compile time.
  • "Static and dynamic linking differ only in file size." They differ in when symbols are resolved, which is what makes interposition, versioning and plugin loading possible at all.

Follow-up

The same object files link successfully in one order and fail with an undefined symbol in another. Why?

Implementation challenge

What to ask them to write or trace on a whiteboard.

Two object files, one calls a function the other defines. Write out what the caller's object file contains at the call site before linking, and what the linker changes.

The lessons behind it