Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 1 | //===- MarkLive.cpp -------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 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" |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 24 | #include "InputEvent.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; |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 33 | |
| 34 | void lld::wasm::markLive() { |
Sam Clegg | ffd0aaf | 2018-06-22 15:13:10 +0000 | [diff] [blame] | 35 | if (!Config->GcSections) |
| 36 | return; |
| 37 | |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 38 | LLVM_DEBUG(dbgs() << "markLive\n"); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 39 | SmallVector<InputChunk *, 256> Q; |
| 40 | |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 41 | std::function<void(Symbol*)> Enqueue = [&](Symbol *Sym) { |
Nicholas Wilson | a1e299f | 2018-04-20 17:18:06 +0000 | [diff] [blame] | 42 | if (!Sym || Sym->isLive()) |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 43 | return; |
Sam Clegg | 084d360 | 2018-06-21 15:00:00 +0000 | [diff] [blame] | 44 | LLVM_DEBUG(dbgs() << "markLive: " << Sym->getName() << "\n"); |
Nicholas Wilson | a1e299f | 2018-04-20 17:18:06 +0000 | [diff] [blame] | 45 | Sym->markLive(); |
| 46 | if (InputChunk *Chunk = Sym->getChunk()) |
| 47 | Q.push_back(Chunk); |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 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 | for (const ObjFile *Obj : Symtab->ObjectFiles) { |
| 54 | const WasmLinkingData &L = Obj->getWasmObj()->linkingData(); |
| 55 | for (const WasmInitFunc &F : L.InitFunctions) |
| 56 | Enqueue(Obj->getFunctionSymbol(F.Symbol)); |
| 57 | } |
| 58 | } |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | // Add GC root symbols. |
| 62 | if (!Config->Entry.empty()) |
| 63 | Enqueue(Symtab->find(Config->Entry)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 64 | |
Sam Clegg | ce004bf | 2018-06-28 17:04:58 +0000 | [diff] [blame] | 65 | // We need to preserve any exported symbol |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 66 | for (Symbol *Sym : Symtab->getSymbols()) |
Sam Clegg | ce004bf | 2018-06-28 17:04:58 +0000 | [diff] [blame] | 67 | if (Sym->isExported()) |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 68 | Enqueue(Sym); |
| 69 | |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 70 | // For relocatable output, we need to preserve all the ctor functions |
| 71 | if (Config->Relocatable) { |
| 72 | for (const ObjFile *Obj : Symtab->ObjectFiles) { |
| 73 | const WasmLinkingData &L = Obj->getWasmObj()->linkingData(); |
| 74 | for (const WasmInitFunc &F : L.InitFunctions) |
| 75 | Enqueue(Obj->getFunctionSymbol(F.Symbol)); |
| 76 | } |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame^] | 79 | if (Config->Pic) { |
| 80 | Enqueue(WasmSym::CallCtors); |
| 81 | Enqueue(WasmSym::ApplyRelocs); |
| 82 | } |
| 83 | |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 +0000 | [diff] [blame] | 84 | // Follow relocations to mark all reachable chunks. |
| 85 | while (!Q.empty()) { |
| 86 | InputChunk *C = Q.pop_back_val(); |
| 87 | |
| 88 | for (const WasmRelocation Reloc : C->getRelocations()) { |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 89 | if (Reloc.Type == R_WASM_TYPE_INDEX_LEB) |
Nicholas Wilson | 2e55ee7 | 2018-03-09 17:06:38 +0000 | [diff] [blame] | 90 | continue; |
| 91 | Symbol *Sym = C->File->getSymbol(Reloc.Index); |
| 92 | |
| 93 | // If the function has been assigned the special index zero in the table, |
| 94 | // the relocation doesn't pull in the function body, since the function |
| 95 | // won't actually go in the table (the runtime will trap attempts to call |
| 96 | // that index, since we don't use it). A function with a table index of |
| 97 | // zero is only reachable via "call", not via "call_indirect". The stub |
| 98 | // functions used for weak-undefined symbols have this behaviour (compare |
| 99 | // equal to null pointer, only reachable via direct call). |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 100 | if (Reloc.Type == R_WASM_TABLE_INDEX_SLEB || |
| 101 | Reloc.Type == R_WASM_TABLE_INDEX_I32) { |
Heejin Ahn | a1cc4ea | 2019-02-04 19:13:46 +0000 | [diff] [blame] | 102 | auto *FuncSym = cast<FunctionSymbol>(Sym); |
Nicholas Wilson | 2e55ee7 | 2018-03-09 17:06:38 +0000 | [diff] [blame] | 103 | if (FuncSym->hasTableIndex() && FuncSym->getTableIndex() == 0) |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | Enqueue(Sym); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 108 | } |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 +0000 | [diff] [blame] | 109 | } |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 110 | |
| 111 | // Report garbage-collected sections. |
| 112 | if (Config->PrintGcSections) { |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 113 | for (const ObjFile *Obj : Symtab->ObjectFiles) { |
| 114 | for (InputChunk *C : Obj->Functions) |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 115 | if (!C->Live) |
| 116 | message("removing unused section " + toString(C)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 117 | for (InputChunk *C : Obj->Segments) |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 118 | if (!C->Live) |
| 119 | message("removing unused section " + toString(C)); |
Nicholas Wilson | 358af38 | 2018-04-20 17:28:12 +0000 | [diff] [blame] | 120 | for (InputGlobal *G : Obj->Globals) |
| 121 | if (!G->Live) |
| 122 | message("removing unused section " + toString(G)); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 123 | for (InputEvent *E : Obj->Events) |
| 124 | if (!E->Live) |
| 125 | message("removing unused section " + toString(E)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 126 | } |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 127 | for (InputChunk *C : Symtab->SyntheticFunctions) |
| 128 | if (!C->Live) |
| 129 | message("removing unused section " + toString(C)); |
Nicholas Wilson | 358af38 | 2018-04-20 17:28:12 +0000 | [diff] [blame] | 130 | for (InputGlobal *G : Symtab->SyntheticGlobals) |
| 131 | if (!G->Live) |
| 132 | message("removing unused section " + toString(G)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 133 | } |
| 134 | } |