Master the fundamental concepts of linkers & loaders through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksLink-Time Optimization (LTO) compiles to LLVM bitcode in .o files, then runs the full optimization pipeline at link. Clang -flto enables cross-module inlining and devirtualization invisible to separate compilation.
Each translation unit emits IR bitcode in a special section. The linker invokes the compiler backend as a plugin to merge modules, optimize globally, then codegen once.
bashLoading…
Production compilers embed this step inside a longer pipeline. GCC flows through cpp, cc1, assembly, and ld; Clang uses the driver, Sema, LLVM IR passes, and a target backend. LLVM bitcode, JVM bytecode, and WASM are other familiar IRs at the same layer. The exercise isolates one pass so you can test it alone before chaining it to the next stage.
You will understand and apply link-time optimization concepts. This exercise asks you to explain how LTO defers codegen and what cross-module wins it enables over separate compilation.
Show what link-time optimisation buys. Model a program split across source files, then optimise it twice (once file by file (a normal build, where the compiler sees one translation unit at a time) and once with the whole program visible (LTO)) and compare code size.
cLoading…
A call resolves to a static function of the same name in the caller's file if there is one, otherwise to the non-static function of that name.
size + for each call site: 1 if the call stays a call, or the callee's final size if it is inlined.size (the original, not the final size) is ≤ B and the callee cannot reach the caller through calls (no inlining into recursion). Without LTO the callee must also be in the caller's file; with LTO it can be anywhere.static function is kept only if a kept function still calls it.main is kept initially; anything a kept function still calls is kept.For each mode (without LTO first):
cLoading…
Then LTO saves D units (P%), where P is D * 100 / total-without-LTO rounded down.
Input:
cLoading…
Output:
cLoading…
static visibility.Hidden tests cover a function kept only because an inlined callee calls it, mutual recursion (never inlined), static functions with the same name in two files, and a budget of 0.