1 //===- MarkLive.cpp -------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements --gc-sections, which is a feature to remove unused 10 // chunks from the output. Unused chunks are those that are not reachable from 11 // known root symbols or chunks. This feature is implemented as a mark-sweep 12 // garbage collector. 13 // 14 // Here's how it works. Each InputChunk has a "Live" bit. The bit is off by 15 // default. Starting with the GC-roots, visit all reachable chunks and set their 16 // Live bits. The Writer will then ignore chunks whose Live bits are off, so 17 // that such chunk are not appear in the output. 18 // 19 //===----------------------------------------------------------------------===// 20 21 #include "MarkLive.h" 22 #include "Config.h" 23 #include "InputChunks.h" 24 #include "InputEvent.h" 25 #include "InputGlobal.h" 26 #include "SymbolTable.h" 27 #include "Symbols.h" 28 29 #define DEBUG_TYPE "lld" 30 31 using namespace llvm; 32 using namespace llvm::wasm; 33 34 void lld::wasm::markLive() { 35 if (!config->gcSections) 36 return; 37 38 LLVM_DEBUG(dbgs() << "markLive\n"); 39 SmallVector<InputChunk *, 256> q; 40 41 std::function<void(Symbol*)> enqueue = [&](Symbol *sym) { 42 if (!sym || sym->isLive()) 43 return; 44 LLVM_DEBUG(dbgs() << "markLive: " << sym->getName() << "\n"); 45 sym->markLive(); 46 if (InputChunk *chunk = sym->getChunk()) 47 q.push_back(chunk); 48 49 // The ctor functions are all referenced by the synthetic callCtors 50 // function. However, this function does not contain relocations so we 51 // have to manually mark the ctors as live if callCtors itself is live. 52 if (sym == WasmSym::callCtors) { 53 if (config->passiveSegments) 54 enqueue(WasmSym::initMemory); 55 if (config->isPic) 56 enqueue(WasmSym::applyRelocs); 57 for (const ObjFile *obj : symtab->objectFiles) { 58 const WasmLinkingData &l = obj->getWasmObj()->linkingData(); 59 for (const WasmInitFunc &f : l.InitFunctions) { 60 auto* initSym = obj->getFunctionSymbol(f.Symbol); 61 if (!initSym->isDiscarded()) 62 enqueue(initSym); 63 } 64 } 65 } 66 }; 67 68 // Add GC root symbols. 69 if (!config->entry.empty()) 70 enqueue(symtab->find(config->entry)); 71 72 // We need to preserve any exported symbol 73 for (Symbol *sym : symtab->getSymbols()) 74 if (sym->isExported()) 75 enqueue(sym); 76 77 // For relocatable output, we need to preserve all the ctor functions 78 if (config->relocatable) { 79 for (const ObjFile *obj : symtab->objectFiles) { 80 const WasmLinkingData &l = obj->getWasmObj()->linkingData(); 81 for (const WasmInitFunc &f : l.InitFunctions) 82 enqueue(obj->getFunctionSymbol(f.Symbol)); 83 } 84 } 85 86 if (config->isPic) 87 enqueue(WasmSym::callCtors); 88 89 // Follow relocations to mark all reachable chunks. 90 while (!q.empty()) { 91 InputChunk *c = q.pop_back_val(); 92 93 for (const WasmRelocation reloc : c->getRelocations()) { 94 if (reloc.Type == R_WASM_TYPE_INDEX_LEB) 95 continue; 96 Symbol *sym = c->file->getSymbol(reloc.Index); 97 98 // If the function has been assigned the special index zero in the table, 99 // the relocation doesn't pull in the function body, since the function 100 // won't actually go in the table (the runtime will trap attempts to call 101 // that index, since we don't use it). A function with a table index of 102 // zero is only reachable via "call", not via "call_indirect". The stub 103 // functions used for weak-undefined symbols have this behaviour (compare 104 // equal to null pointer, only reachable via direct call). 105 if (reloc.Type == R_WASM_TABLE_INDEX_SLEB || 106 reloc.Type == R_WASM_TABLE_INDEX_I32) { 107 auto *funcSym = cast<FunctionSymbol>(sym); 108 if (funcSym->hasTableIndex() && funcSym->getTableIndex() == 0) 109 continue; 110 } 111 112 enqueue(sym); 113 } 114 } 115 116 // Report garbage-collected sections. 117 if (config->printGcSections) { 118 for (const ObjFile *obj : symtab->objectFiles) { 119 for (InputChunk *c : obj->functions) 120 if (!c->live) 121 message("removing unused section " + toString(c)); 122 for (InputChunk *c : obj->segments) 123 if (!c->live) 124 message("removing unused section " + toString(c)); 125 for (InputGlobal *g : obj->globals) 126 if (!g->live) 127 message("removing unused section " + toString(g)); 128 for (InputEvent *e : obj->events) 129 if (!e->live) 130 message("removing unused section " + toString(e)); 131 } 132 for (InputChunk *c : symtab->syntheticFunctions) 133 if (!c->live) 134 message("removing unused section " + toString(c)); 135 for (InputGlobal *g : symtab->syntheticGlobals) 136 if (!g->live) 137 message("removing unused section " + toString(g)); 138 } 139 } 140