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() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 35 | if (!config->gcSections) |
Sam Clegg | ffd0aaf | 2018-06-22 15:13:10 +0000 | [diff] [blame] | 36 | return; |
| 37 | |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 38 | LLVM_DEBUG(dbgs() << "markLive\n"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 39 | SmallVector<InputChunk *, 256> q; |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 40 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 41 | std::function<void(Symbol*)> enqueue = [&](Symbol *sym) { |
| 42 | if (!sym || sym->isLive()) |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 43 | return; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 44 | LLVM_DEBUG(dbgs() << "markLive: " << sym->getName() << "\n"); |
| 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 | |
Fangrui Song | 33fdf82 | 2019-07-16 08:08:17 +0000 | [diff] [blame] | 49 | // The ctor functions are all referenced by the synthetic callCtors |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 50 | // function. However, this function does not contain relocations so we |
Fangrui Song | 33fdf82 | 2019-07-16 08:08:17 +0000 | [diff] [blame] | 51 | // have to manually mark the ctors as live if callCtors itself is live. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 52 | if (sym == WasmSym::callCtors) { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 53 | if (config->isPic) |
| 54 | enqueue(WasmSym::applyRelocs); |
| 55 | for (const ObjFile *obj : symtab->objectFiles) { |
| 56 | const WasmLinkingData &l = obj->getWasmObj()->linkingData(); |
| 57 | for (const WasmInitFunc &f : l.InitFunctions) { |
| 58 | auto* initSym = obj->getFunctionSymbol(f.Symbol); |
| 59 | if (!initSym->isDiscarded()) |
| 60 | enqueue(initSym); |
Sam Clegg | fd54fa5 | 2019-06-07 06:00:46 +0000 | [diff] [blame] | 61 | } |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // Add GC root symbols. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 67 | if (!config->entry.empty()) |
| 68 | enqueue(symtab->find(config->entry)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 69 | |
Dan Gohman | 7cb9c8a | 2019-08-29 22:41:05 +0000 | [diff] [blame] | 70 | // We need to preserve any no-strip or exported symbol |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 71 | for (Symbol *sym : symtab->getSymbols()) |
Dan Gohman | 7cb9c8a | 2019-08-29 22:41:05 +0000 | [diff] [blame] | 72 | if (sym->isNoStrip() || sym->isExported()) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 73 | enqueue(sym); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 74 | |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 75 | // For relocatable output, we need to preserve all the ctor functions |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 76 | if (config->relocatable) { |
| 77 | for (const ObjFile *obj : symtab->objectFiles) { |
| 78 | const WasmLinkingData &l = obj->getWasmObj()->linkingData(); |
| 79 | for (const WasmInitFunc &f : l.InitFunctions) |
| 80 | enqueue(obj->getFunctionSymbol(f.Symbol)); |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 81 | } |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 84 | if (config->isPic) |
| 85 | enqueue(WasmSym::callCtors); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 86 | |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 87 | if (config->sharedMemory && !config->shared) |
| 88 | enqueue(WasmSym::initMemory); |
| 89 | |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 +0000 | [diff] [blame] | 90 | // Follow relocations to mark all reachable chunks. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 91 | while (!q.empty()) { |
| 92 | InputChunk *c = q.pop_back_val(); |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 +0000 | [diff] [blame] | 93 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 94 | for (const WasmRelocation reloc : c->getRelocations()) { |
| 95 | if (reloc.Type == R_WASM_TYPE_INDEX_LEB) |
Nicholas Wilson | 2e55ee7 | 2018-03-09 17:06:38 +0000 | [diff] [blame] | 96 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 97 | Symbol *sym = c->file->getSymbol(reloc.Index); |
Nicholas Wilson | 2e55ee7 | 2018-03-09 17:06:38 +0000 | [diff] [blame] | 98 | |
| 99 | // If the function has been assigned the special index zero in the table, |
| 100 | // the relocation doesn't pull in the function body, since the function |
| 101 | // won't actually go in the table (the runtime will trap attempts to call |
| 102 | // that index, since we don't use it). A function with a table index of |
| 103 | // zero is only reachable via "call", not via "call_indirect". The stub |
| 104 | // functions used for weak-undefined symbols have this behaviour (compare |
| 105 | // equal to null pointer, only reachable via direct call). |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 106 | if (reloc.Type == R_WASM_TABLE_INDEX_SLEB || |
| 107 | reloc.Type == R_WASM_TABLE_INDEX_I32) { |
| 108 | auto *funcSym = cast<FunctionSymbol>(sym); |
| 109 | if (funcSym->hasTableIndex() && funcSym->getTableIndex() == 0) |
Nicholas Wilson | 2e55ee7 | 2018-03-09 17:06:38 +0000 | [diff] [blame] | 110 | continue; |
| 111 | } |
| 112 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 113 | enqueue(sym); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 114 | } |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 +0000 | [diff] [blame] | 115 | } |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 116 | |
| 117 | // Report garbage-collected sections. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 118 | if (config->printGcSections) { |
| 119 | for (const ObjFile *obj : symtab->objectFiles) { |
| 120 | for (InputChunk *c : obj->functions) |
| 121 | if (!c->live) |
| 122 | message("removing unused section " + toString(c)); |
| 123 | for (InputChunk *c : obj->segments) |
| 124 | if (!c->live) |
| 125 | message("removing unused section " + toString(c)); |
| 126 | for (InputGlobal *g : obj->globals) |
| 127 | if (!g->live) |
| 128 | message("removing unused section " + toString(g)); |
| 129 | for (InputEvent *e : obj->events) |
| 130 | if (!e->live) |
| 131 | message("removing unused section " + toString(e)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 132 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 133 | for (InputChunk *c : symtab->syntheticFunctions) |
| 134 | if (!c->live) |
| 135 | message("removing unused section " + toString(c)); |
| 136 | for (InputGlobal *g : symtab->syntheticGlobals) |
| 137 | if (!g->live) |
| 138 | message("removing unused section " + toString(g)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 139 | } |
| 140 | } |