← Runtimes
Runtimes

Linking, loading, and modules

Separately built pieces, joined by name and placed at an address.

Linking connects separately built pieces; loading prepares their code and dependencies for execution.

Outcome

Predict which stage reports a missing or duplicate definition, compute symbol addresses and patched values for a layout, and tell which references a loader must adjust when the image moves.

Terminology (7)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Symbol
A name a unit defines or refers to, such as a function or a global.
Linker
The tool that combines units, assigns addresses and patches references between them.
Relocation
A place in a unit whose final value depends on where a symbol ends up.
Loader
The part of the operating system or runtime that places a program in memory and maps its shared libraries.
Static and dynamic linking
Copying library code into the executable, or recording the library to load when the program starts.
Position-independent code
Code designed to execute independently of a fixed load address. Same-image PC-relative addressing is one technique; native formats can also use indirection and relocations.
Ambient declaration
In TypeScript, declare: a promise to the checker that a value exists, which emits no code.

From declarations to a running image

A declaration describes something a caller may use; a definition supplies its implementation or storage. For a native executable, the linker connects references to definitions across object files and libraries. Static linking incorporates needed library code; dynamic linking leaves shared-library dependencies for the execution environment.

Loading places the executable and its dependencies into memory. A relocation describes a reference that must be adjusted for the final layout or address. Following a release therefore combines three questions: what the caller can compile against, which implementation is supplied, and what the destination environment must provide. JavaScript module and name resolution follow their runtime contracts rather than a native object-file link.

01Resolution

Who notices a function nobody defined?

The invoice client’s pricing module was left out of a release build. Its caller has a declaration, so compilation and integration can fail at different stages. After restoring the module, the release engineer still needs to know which libraries the artifact expects on the destination machine and which references change when its image moves.

The caller expects price to come from a separate component. A declaration can let a tool compile the call without supplying that implementation. The four probes show how this particular missing component is reported: the Go build stops at compilation, the Rust and C++ executable links fail, and the TypeScript declaration disappears before JavaScript reaches the call.

Each program declares price, expecting another unit to define it. No unit does.

declare function tells tsc a value exists without providing it. Node resolves names when code runs.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// price is declared here and expected to come from another unit, which was never added.

declare function price(units: number): number;

console.log("starting");
console.log(`three units cost ${price(3)}`);
tsc accepts the declaration. What happens when node runs the program?
TypeScript · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --strict && node main.js

Predict the shown command before revealing the recorded build notes. Dependency names, byte sizes and diagnostics belong to those toolchains and targets. otool -L describes the recorded macOS binaries; Linux ldd and nm can answer related questions but need not produce the same libraries or output.

02Libraries

Which library code is inside the binary?

Once price is present, the release can link and still depend on libraries installed elsewhere. Static linking incorporates needed library code into the artifact. Dynamic dependencies identify shared libraries for the platform loader; some binding and optional loading may occur after startup.

Use the square-root probe to audit the resulting artifact. The recorded otool -L output lists shared-library dependencies; nm -u lists undefined symbols. A missing sqrt import establishes that this listing has no such imported symbol. It does not by itself prove a particular instruction, inlining decision or execution path.

node runs the JavaScript tsc emitted. In the recorded Node build, V8 supplies Math.sqrt.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// Calls one library function and one function from this file.

function twice(x: number): number {
  return x * 2;
}

const input = Number(process.argv[2] ?? "2");

console.log(`sqrt: ${Math.sqrt(twice(input))}`);
In the recorded Node build, where does Math.sqrt come from when this file runs?

03Loading

What changes when the image is loaded elsewhere?

To inspect movement separately from native file formats, use a flat image linked at base 0. In this authored model, every unit moves by the same base, an abs entry stores an address, and a rel entry stores the signed distance from its own site to a target inside that image. Moving both ends equally preserves the distance. The question starts with the values already stored.

In the lab’s one-image model, an image linked at 0 moves to base 4096. An absolute reference holds 36; a same-image distance reference holds 24. What do they hold after rebasing?

04Practice

Finish the model linker

The release engineer uses a small model to inspect unit layout and bad symbol lists before examining movement. Lay out named units, reject duplicate or missing definitions, then compute absolute and signed-distance references at different bases. This is bookkeeping over authored records; it reads no native object file and loads no executable.

The model linker

Lay out the units, refuse a bad link, then load the image at another base.

  1. easy · R-LINK-V4-1 Lay out the units
  2. medium · R-LINK-V4-2 Refuse a bad link
  3. hard · R-LINK-V4-3 Load at another base
In the frontend A declaration does not install the pricing plugin Optional

Review a server route from the invoice client’s web companion. Assume no runtime provider installs pricingPlugin. The ambient declaration makes the TypeScript name available to the checker. Predict what happens when GET is called.

This diagnostic separates a type promise from a module implementation. A native linker is not involved in this JavaScript name lookup.

app/api/price/route.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
declare const pricingPlugin: { price(units: number): number };

export function GET(): Response {
  return Response.json({ total: pricingPlugin.price(3) });
}

The declaration emits no binding. With no provider for pricingPlugin, calling GET throws ReferenceError before Response.json can return. A declaration file alone does not bundle a plugin implementation into the route.

Sources: TypeScript: declaration reference.

Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.