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 | |
| 41 | auto 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 | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | // Add GC root symbols. |
| 51 | if (!Config->Entry.empty()) |
| 52 | Enqueue(Symtab->find(Config->Entry)); |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 53 | Enqueue(WasmSym::CallCtors); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 54 | |
Sam Clegg | ce004bf | 2018-06-28 17:04:58 +0000 | [diff] [blame] | 55 | // We need to preserve any exported symbol |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 56 | for (Symbol *Sym : Symtab->getSymbols()) |
Sam Clegg | ce004bf | 2018-06-28 17:04:58 +0000 | [diff] [blame] | 57 | if (Sym->isExported()) |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 58 | Enqueue(Sym); |
| 59 | |
Sam Clegg | 3d1f4b9 | 2018-02-16 18:37:32 +0000 | [diff] [blame] | 60 | // The ctor functions are all used in the synthetic __wasm_call_ctors |
| 61 | // 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] | 62 | // relocations which mean we have to manually mark the ctors. |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 63 | for (const ObjFile *Obj : Symtab->ObjectFiles) { |
| 64 | const WasmLinkingData &L = Obj->getWasmObj()->linkingData(); |
| 65 | for (const WasmInitFunc &F : L.InitFunctions) |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 66 | Enqueue(Obj->getFunctionSymbol(F.Symbol)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 +0000 | [diff] [blame] | 69 | // Follow relocations to mark all reachable chunks. |
| 70 | while (!Q.empty()) { |
| 71 | InputChunk *C = Q.pop_back_val(); |
| 72 | |
| 73 | for (const WasmRelocation Reloc : C->getRelocations()) { |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 74 | if (Reloc.Type == R_WASM_TYPE_INDEX_LEB) |
Nicholas Wilson | 2e55ee7 | 2018-03-09 17:06:38 +0000 | [diff] [blame] | 75 | continue; |
| 76 | Symbol *Sym = C->File->getSymbol(Reloc.Index); |
| 77 | |
| 78 | // If the function has been assigned the special index zero in the table, |
| 79 | // the relocation doesn't pull in the function body, since the function |
| 80 | // won't actually go in the table (the runtime will trap attempts to call |
| 81 | // that index, since we don't use it). A function with a table index of |
| 82 | // zero is only reachable via "call", not via "call_indirect". The stub |
| 83 | // functions used for weak-undefined symbols have this behaviour (compare |
| 84 | // equal to null pointer, only reachable via direct call). |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 85 | if (Reloc.Type == R_WASM_TABLE_INDEX_SLEB || |
| 86 | Reloc.Type == R_WASM_TABLE_INDEX_I32) { |
Heejin Ahn | a1cc4ea | 2019-02-04 19:13:46 +0000 | [diff] [blame^] | 87 | auto *FuncSym = cast<FunctionSymbol>(Sym); |
Nicholas Wilson | 2e55ee7 | 2018-03-09 17:06:38 +0000 | [diff] [blame] | 88 | if (FuncSym->hasTableIndex() && FuncSym->getTableIndex() == 0) |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | Enqueue(Sym); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 93 | } |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 +0000 | [diff] [blame] | 94 | } |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 95 | |
| 96 | // Report garbage-collected sections. |
| 97 | if (Config->PrintGcSections) { |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 98 | for (const ObjFile *Obj : Symtab->ObjectFiles) { |
| 99 | for (InputChunk *C : Obj->Functions) |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 100 | if (!C->Live) |
| 101 | message("removing unused section " + toString(C)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 102 | for (InputChunk *C : Obj->Segments) |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 103 | if (!C->Live) |
| 104 | message("removing unused section " + toString(C)); |
Nicholas Wilson | 358af38 | 2018-04-20 17:28:12 +0000 | [diff] [blame] | 105 | for (InputGlobal *G : Obj->Globals) |
| 106 | if (!G->Live) |
| 107 | message("removing unused section " + toString(G)); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 108 | for (InputEvent *E : Obj->Events) |
| 109 | if (!E->Live) |
| 110 | message("removing unused section " + toString(E)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 111 | } |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 112 | for (InputChunk *C : Symtab->SyntheticFunctions) |
| 113 | if (!C->Live) |
| 114 | message("removing unused section " + toString(C)); |
Nicholas Wilson | 358af38 | 2018-04-20 17:28:12 +0000 | [diff] [blame] | 115 | for (InputGlobal *G : Symtab->SyntheticGlobals) |
| 116 | if (!G->Live) |
| 117 | message("removing unused section " + toString(G)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 118 | } |
| 119 | } |