1 //===- MarkLive.cpp -------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements --gc-sections, which is a feature to remove unused
11 // chunks from the output. Unused chunks are those that are not reachable from
12 // known root symbols or chunks. This feature is implemented as a mark-sweep
15 // Here's how it works. Each InputChunk has a "Live" bit. The bit is off by
16 // default. Starting with the GC-roots, visit all reachable chunks and set their
17 // Live bits. The Writer will then ignore chunks whose Live bits are off, so
18 // that such chunk are not appear in the output.
20 //===----------------------------------------------------------------------===//
24 #include "InputChunks.h"
25 #include "InputGlobal.h"
26 #include "SymbolTable.h"
29 #define DEBUG_TYPE "lld"
32 using namespace llvm::wasm;
34 using namespace lld::wasm;
36 void lld::wasm::markLive() {
37 if (!Config->GcSections)
40 LLVM_DEBUG(dbgs() << "markLive\n");
41 SmallVector<InputChunk *, 256> Q;
43 auto Enqueue = [&](Symbol *Sym) {
44 if (!Sym || Sym->isLive())
46 LLVM_DEBUG(dbgs() << "markLive: " << Sym->getName() << "\n");
48 if (InputChunk *Chunk = Sym->getChunk())
52 // Add GC root symbols.
53 if (!Config->Entry.empty())
54 Enqueue(Symtab->find(Config->Entry));
55 Enqueue(WasmSym::CallCtors);
57 // We need to preserve any exported symbol
58 for (Symbol *Sym : Symtab->getSymbols())
59 if (Sym->isExported())
62 // The ctor functions are all used in the synthetic __wasm_call_ctors
63 // function, but since this function is created in-place it doesn't contain
64 // relocations which mean we have to manually mark the ctors.
65 for (const ObjFile *Obj : Symtab->ObjectFiles) {
66 const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
67 for (const WasmInitFunc &F : L.InitFunctions)
68 Enqueue(Obj->getFunctionSymbol(F.Symbol));
71 // Follow relocations to mark all reachable chunks.
73 InputChunk *C = Q.pop_back_val();
75 for (const WasmRelocation Reloc : C->getRelocations()) {
76 if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB)
78 Symbol *Sym = C->File->getSymbol(Reloc.Index);
80 // If the function has been assigned the special index zero in the table,
81 // the relocation doesn't pull in the function body, since the function
82 // won't actually go in the table (the runtime will trap attempts to call
83 // that index, since we don't use it). A function with a table index of
84 // zero is only reachable via "call", not via "call_indirect". The stub
85 // functions used for weak-undefined symbols have this behaviour (compare
86 // equal to null pointer, only reachable via direct call).
87 if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB ||
88 Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32) {
89 FunctionSymbol *FuncSym = cast<FunctionSymbol>(Sym);
90 if (FuncSym->hasTableIndex() && FuncSym->getTableIndex() == 0)
98 // Report garbage-collected sections.
99 if (Config->PrintGcSections) {
100 for (const ObjFile *Obj : Symtab->ObjectFiles) {
101 for (InputChunk *C : Obj->Functions)
103 message("removing unused section " + toString(C));
104 for (InputChunk *C : Obj->Segments)
106 message("removing unused section " + toString(C));
107 for (InputGlobal *G : Obj->Globals)
109 message("removing unused section " + toString(G));
111 for (InputChunk *C : Symtab->SyntheticFunctions)
113 message("removing unused section " + toString(C));
114 for (InputGlobal *G : Symtab->SyntheticGlobals)
116 message("removing unused section " + toString(G));