Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 1 | //===- MarkLive.cpp -------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 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 |
| 13 | // garbage collector. |
| 14 | // |
| 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. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "MarkLive.h" |
| 23 | #include "Config.h" |
| 24 | #include "InputChunks.h" |
Nicholas Wilson | 358af38 | 2018-04-20 17:28:12 +0000 | [diff] [blame^] | 25 | #include "InputGlobal.h" |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 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 | using namespace lld; |
| 34 | using namespace lld::wasm; |
| 35 | |
| 36 | void lld::wasm::markLive() { |
| 37 | if (!Config->GcSections) |
| 38 | return; |
| 39 | |
| 40 | DEBUG(dbgs() << "markLive\n"); |
| 41 | SmallVector<InputChunk *, 256> Q; |
| 42 | |
| 43 | auto Enqueue = [&](Symbol *Sym) { |
Nicholas Wilson | a1e299f | 2018-04-20 17:18:06 +0000 | [diff] [blame] | 44 | if (!Sym || Sym->isLive()) |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 45 | return; |
Nicholas Wilson | a1e299f | 2018-04-20 17:18:06 +0000 | [diff] [blame] | 46 | Sym->markLive(); |
| 47 | if (InputChunk *Chunk = Sym->getChunk()) |
| 48 | Q.push_back(Chunk); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | // Add GC root symbols. |
| 52 | if (!Config->Entry.empty()) |
| 53 | Enqueue(Symtab->find(Config->Entry)); |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 54 | Enqueue(WasmSym::CallCtors); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 55 | |
| 56 | // By default we export all non-hidden, so they are gc roots too |
| 57 | for (Symbol *Sym : Symtab->getSymbols()) |
| 58 | if (!Sym->isHidden()) |
| 59 | Enqueue(Sym); |
| 60 | |
Sam Clegg | 3d1f4b9 | 2018-02-16 18:37:32 +0000 | [diff] [blame] | 61 | // The ctor functions are all used in the synthetic __wasm_call_ctors |
| 62 | // function, but since this function is created in-place it doesn't contain |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 +0000 | [diff] [blame] | 63 | // relocations which mean we have to manually mark the ctors. |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 64 | for (const ObjFile *Obj : Symtab->ObjectFiles) { |
| 65 | const WasmLinkingData &L = Obj->getWasmObj()->linkingData(); |
| 66 | for (const WasmInitFunc &F : L.InitFunctions) |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 67 | Enqueue(Obj->getFunctionSymbol(F.Symbol)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 +0000 | [diff] [blame] | 70 | // Follow relocations to mark all reachable chunks. |
| 71 | while (!Q.empty()) { |
| 72 | InputChunk *C = Q.pop_back_val(); |
| 73 | |
| 74 | for (const WasmRelocation Reloc : C->getRelocations()) { |
Nicholas Wilson | 2e55ee7 | 2018-03-09 17:06:38 +0000 | [diff] [blame] | 75 | if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB) |
| 76 | continue; |
| 77 | Symbol *Sym = C->File->getSymbol(Reloc.Index); |
| 78 | |
| 79 | // If the function has been assigned the special index zero in the table, |
| 80 | // the relocation doesn't pull in the function body, since the function |
| 81 | // won't actually go in the table (the runtime will trap attempts to call |
| 82 | // that index, since we don't use it). A function with a table index of |
| 83 | // zero is only reachable via "call", not via "call_indirect". The stub |
| 84 | // functions used for weak-undefined symbols have this behaviour (compare |
| 85 | // equal to null pointer, only reachable via direct call). |
| 86 | if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB || |
| 87 | Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32) { |
| 88 | FunctionSymbol *FuncSym = cast<FunctionSymbol>(Sym); |
| 89 | if (FuncSym->hasTableIndex() && FuncSym->getTableIndex() == 0) |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | Enqueue(Sym); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 94 | } |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 +0000 | [diff] [blame] | 95 | } |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 96 | |
| 97 | // Report garbage-collected sections. |
| 98 | if (Config->PrintGcSections) { |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 99 | for (const ObjFile *Obj : Symtab->ObjectFiles) { |
| 100 | for (InputChunk *C : Obj->Functions) |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 101 | if (!C->Live) |
| 102 | message("removing unused section " + toString(C)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 103 | for (InputChunk *C : Obj->Segments) |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 104 | if (!C->Live) |
| 105 | message("removing unused section " + toString(C)); |
Nicholas Wilson | 358af38 | 2018-04-20 17:28:12 +0000 | [diff] [blame^] | 106 | for (InputGlobal *G : Obj->Globals) |
| 107 | if (!G->Live) |
| 108 | message("removing unused section " + toString(G)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 109 | } |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 110 | for (InputChunk *C : Symtab->SyntheticFunctions) |
| 111 | if (!C->Live) |
| 112 | message("removing unused section " + toString(C)); |
Nicholas Wilson | 358af38 | 2018-04-20 17:28:12 +0000 | [diff] [blame^] | 113 | for (InputGlobal *G : Symtab->SyntheticGlobals) |
| 114 | if (!G->Live) |
| 115 | message("removing unused section " + toString(G)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 116 | } |
| 117 | } |