Sam Clegg | 0362633 | 2018-01-31 01:45:47 | [diff] [blame] | 1 | //===- MarkLive.cpp -------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [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 | [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" |
Andy Wingo | a56e574 | 2021-02-11 11:15:24 | [diff] [blame] | 24 | #include "InputElement.h" |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 | [diff] [blame] | 25 | #include "SymbolTable.h" |
| 26 | #include "Symbols.h" |
| 27 | |
| 28 | #define DEBUG_TYPE "lld" |
| 29 | |
| 30 | using namespace llvm; |
| 31 | using namespace llvm::wasm; |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 | [diff] [blame] | 32 | |
Sam Clegg | d32f71a | 2023-03-06 17:55:00 | [diff] [blame] | 33 | namespace lld::wasm { |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 34 | |
| 35 | namespace { |
| 36 | |
| 37 | class MarkLive { |
| 38 | public: |
| 39 | void run(); |
| 40 | |
| 41 | private: |
| 42 | void enqueue(Symbol *sym); |
Yuta Saito | ba3c1f9 | 2024-02-20 18:35:36 | [diff] [blame] | 43 | void enqueue(InputChunk *chunk); |
Dan Gohman | 950ae43 | 2020-10-01 03:00:04 | [diff] [blame] | 44 | void enqueueInitFunctions(const ObjFile *sym); |
Yuta Saito | ba3c1f9 | 2024-02-20 18:35:36 | [diff] [blame] | 45 | void enqueueRetainedSegments(const ObjFile *file); |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 46 | void mark(); |
Dan Gohman | 6cd8511 | 2020-10-01 00:21:57 | [diff] [blame] | 47 | bool isCallCtorsLive(); |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 48 | |
| 49 | // A list of chunks to visit. |
| 50 | SmallVector<InputChunk *, 256> queue; |
| 51 | }; |
| 52 | |
| 53 | } // namespace |
| 54 | |
| 55 | void MarkLive::enqueue(Symbol *sym) { |
| 56 | if (!sym || sym->isLive()) |
Sam Clegg | ffd0aaf | 2018-06-22 15:13:10 | [diff] [blame] | 57 | return; |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 58 | LLVM_DEBUG(dbgs() << "markLive: " << sym->getName() << "\n"); |
Dan Gohman | 950ae43 | 2020-10-01 03:00:04 | [diff] [blame] | 59 | |
| 60 | InputFile *file = sym->getFile(); |
Yuta Saito | ba3c1f9 | 2024-02-20 18:35:36 | [diff] [blame] | 61 | bool markImplicitDeps = file && !file->isLive() && sym->isDefined(); |
Dan Gohman | 950ae43 | 2020-10-01 03:00:04 | [diff] [blame] | 62 | |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 63 | sym->markLive(); |
Dan Gohman | 950ae43 | 2020-10-01 03:00:04 | [diff] [blame] | 64 | |
Yuta Saito | ba3c1f9 | 2024-02-20 18:35:36 | [diff] [blame] | 65 | if (markImplicitDeps) { |
Sam Clegg | 22b7b84 | 2024-07-12 20:26:52 | [diff] [blame] | 66 | if (auto obj = dyn_cast<ObjFile>(file)) { |
| 67 | // Mark as live the ctor functions in the object that defines this symbol. |
| 68 | // The ctor functions are all referenced by the synthetic callCtors |
| 69 | // function. However, this function does not contain relocations so we |
| 70 | // have to manually mark the ctors as live. |
| 71 | enqueueInitFunctions(obj); |
| 72 | // Mark retained segments in the object that defines this symbol live. |
| 73 | enqueueRetainedSegments(obj); |
| 74 | } |
Yuta Saito | ba3c1f9 | 2024-02-20 18:35:36 | [diff] [blame] | 75 | } |
Dan Gohman | 950ae43 | 2020-10-01 03:00:04 | [diff] [blame] | 76 | |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 77 | if (InputChunk *chunk = sym->getChunk()) |
| 78 | queue.push_back(chunk); |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 79 | } |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 | [diff] [blame] | 80 | |
Yuta Saito | ba3c1f9 | 2024-02-20 18:35:36 | [diff] [blame] | 81 | void MarkLive::enqueue(InputChunk *chunk) { |
| 82 | LLVM_DEBUG(dbgs() << "markLive: " << toString(chunk) << "\n"); |
| 83 | chunk->live = true; |
| 84 | queue.push_back(chunk); |
| 85 | } |
| 86 | |
Dan Gohman | 950ae43 | 2020-10-01 03:00:04 | [diff] [blame] | 87 | // The ctor functions are all referenced by the synthetic callCtors |
| 88 | // function. However, this function does not contain relocations so we |
| 89 | // have to manually mark the ctors as live. |
| 90 | void MarkLive::enqueueInitFunctions(const ObjFile *obj) { |
| 91 | const WasmLinkingData &l = obj->getWasmObj()->linkingData(); |
| 92 | for (const WasmInitFunc &f : l.InitFunctions) { |
| 93 | auto *initSym = obj->getFunctionSymbol(f.Symbol); |
| 94 | if (!initSym->isDiscarded()) |
| 95 | enqueue(initSym); |
| 96 | } |
| 97 | } |
| 98 | |
Yuta Saito | ba3c1f9 | 2024-02-20 18:35:36 | [diff] [blame] | 99 | // Mark segments flagged by segment-level no-strip. Segment-level no-strip is |
| 100 | // usually used to retain segments without having symbol table entry. |
| 101 | void MarkLive::enqueueRetainedSegments(const ObjFile *file) { |
| 102 | for (InputChunk *chunk : file->segments) |
| 103 | if (chunk->isRetained()) |
| 104 | enqueue(chunk); |
| 105 | } |
| 106 | |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 107 | void MarkLive::run() { |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 | [diff] [blame] | 108 | // Add GC root symbols. |
Fangrui Song | 3792b36 | 2025-01-03 01:08:18 | [diff] [blame] | 109 | if (!ctx.arg.entry.empty()) |
| 110 | enqueue(symtab->find(ctx.arg.entry)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 | [diff] [blame] | 111 | |
Dan Gohman | 7cb9c8a | 2019-08-29 22:41:05 | [diff] [blame] | 112 | // We need to preserve any no-strip or exported symbol |
Sam Clegg | 113b568 | 2022-08-05 19:54:29 | [diff] [blame] | 113 | for (Symbol *sym : symtab->symbols()) |
Dan Gohman | 7cb9c8a | 2019-08-29 22:41:05 | [diff] [blame] | 114 | if (sym->isNoStrip() || sym->isExported()) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 115 | enqueue(sym); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 | [diff] [blame] | 116 | |
Anutosh Bhat | 9cbbb74 | 2025-04-25 14:35:00 | [diff] [blame] | 117 | if (ctx.sym.callDtors) |
| 118 | enqueue(ctx.sym.callDtors); |
Dan Gohman | 6cd8511 | 2020-10-01 00:21:57 | [diff] [blame] | 119 | |
Sam Clegg | 3c58457 | 2024-01-18 23:53:13 | [diff] [blame] | 120 | for (const ObjFile *obj : ctx.objectFiles) |
Yuta Saito | ba3c1f9 | 2024-02-20 18:35:36 | [diff] [blame] | 121 | if (obj->isLive()) { |
| 122 | // Enqueue constructors in objects explicitly live from the command-line. |
Fangrui Song | 025b309 | 2022-06-04 05:18:06 | [diff] [blame] | 123 | enqueueInitFunctions(obj); |
Yuta Saito | ba3c1f9 | 2024-02-20 18:35:36 | [diff] [blame] | 124 | // Enqueue retained segments in objects explicitly live from the |
| 125 | // command-line. |
| 126 | enqueueRetainedSegments(obj); |
| 127 | } |
Dan Gohman | 950ae43 | 2020-10-01 03:00:04 | [diff] [blame] | 128 | |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 129 | mark(); |
Dan Gohman | 950ae43 | 2020-10-01 03:00:04 | [diff] [blame] | 130 | |
| 131 | // If we have any non-discarded init functions, mark `__wasm_call_ctors` as |
| 132 | // live so that we assign it an index and call it. |
| 133 | if (isCallCtorsLive()) |
Anutosh Bhat | 9cbbb74 | 2025-04-25 14:35:00 | [diff] [blame] | 134 | ctx.sym.callCtors->markLive(); |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void MarkLive::mark() { |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 | [diff] [blame] | 138 | // Follow relocations to mark all reachable chunks. |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 139 | while (!queue.empty()) { |
| 140 | InputChunk *c = queue.pop_back_val(); |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 | [diff] [blame] | 141 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 142 | for (const WasmRelocation reloc : c->getRelocations()) { |
| 143 | if (reloc.Type == R_WASM_TYPE_INDEX_LEB) |
Nicholas Wilson | 2e55ee7 | 2018-03-09 17:06:38 | [diff] [blame] | 144 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 145 | Symbol *sym = c->file->getSymbol(reloc.Index); |
Nicholas Wilson | 2e55ee7 | 2018-03-09 17:06:38 | [diff] [blame] | 146 | |
| 147 | // If the function has been assigned the special index zero in the table, |
| 148 | // the relocation doesn't pull in the function body, since the function |
| 149 | // won't actually go in the table (the runtime will trap attempts to call |
| 150 | // that index, since we don't use it). A function with a table index of |
| 151 | // zero is only reachable via "call", not via "call_indirect". The stub |
| 152 | // functions used for weak-undefined symbols have this behaviour (compare |
| 153 | // equal to null pointer, only reachable via direct call). |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 154 | if (reloc.Type == R_WASM_TABLE_INDEX_SLEB || |
Wouter van Oortmerssen | cc1b9b6 | 2020-07-10 23:51:01 | [diff] [blame] | 155 | reloc.Type == R_WASM_TABLE_INDEX_SLEB64 || |
| 156 | reloc.Type == R_WASM_TABLE_INDEX_I32 || |
| 157 | reloc.Type == R_WASM_TABLE_INDEX_I64) { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 158 | auto *funcSym = cast<FunctionSymbol>(sym); |
Sam Clegg | 48ddf5e1 | 2020-11-23 23:41:07 | [diff] [blame] | 159 | if (funcSym->isStub) |
Nicholas Wilson | 2e55ee7 | 2018-03-09 17:06:38 | [diff] [blame] | 160 | continue; |
| 161 | } |
| 162 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 163 | enqueue(sym); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 | [diff] [blame] | 164 | } |
Rui Ueyama | 34133b23 | 2018-02-19 22:34:47 | [diff] [blame] | 165 | } |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void markLive() { |
Fangrui Song | 3792b36 | 2025-01-03 01:08:18 | [diff] [blame] | 169 | if (!ctx.arg.gcSections) |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 170 | return; |
| 171 | |
| 172 | LLVM_DEBUG(dbgs() << "markLive\n"); |
| 173 | |
| 174 | MarkLive marker; |
| 175 | marker.run(); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 | [diff] [blame] | 176 | |
| 177 | // Report garbage-collected sections. |
Fangrui Song | 3792b36 | 2025-01-03 01:08:18 | [diff] [blame] | 178 | if (ctx.arg.printGcSections) { |
Sam Clegg | 3c58457 | 2024-01-18 23:53:13 | [diff] [blame] | 179 | for (const ObjFile *obj : ctx.objectFiles) { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 180 | for (InputChunk *c : obj->functions) |
| 181 | if (!c->live) |
| 182 | message("removing unused section " + toString(c)); |
| 183 | for (InputChunk *c : obj->segments) |
| 184 | if (!c->live) |
| 185 | message("removing unused section " + toString(c)); |
| 186 | for (InputGlobal *g : obj->globals) |
| 187 | if (!g->live) |
| 188 | message("removing unused section " + toString(g)); |
Heejin Ahn | 1d891d4 | 2021-06-15 08:49:43 | [diff] [blame] | 189 | for (InputTag *t : obj->tags) |
| 190 | if (!t->live) |
| 191 | message("removing unused section " + toString(t)); |
Andy Wingo | 53e3b81 | 2021-01-05 11:08:58 | [diff] [blame] | 192 | for (InputTable *t : obj->tables) |
| 193 | if (!t->live) |
| 194 | message("removing unused section " + toString(t)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 | [diff] [blame] | 195 | } |
Sam Clegg | 3c58457 | 2024-01-18 23:53:13 | [diff] [blame] | 196 | for (InputChunk *c : ctx.syntheticFunctions) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 197 | if (!c->live) |
| 198 | message("removing unused section " + toString(c)); |
Sam Clegg | 3c58457 | 2024-01-18 23:53:13 | [diff] [blame] | 199 | for (InputGlobal *g : ctx.syntheticGlobals) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 200 | if (!g->live) |
| 201 | message("removing unused section " + toString(g)); |
Sam Clegg | 3c58457 | 2024-01-18 23:53:13 | [diff] [blame] | 202 | for (InputTable *t : ctx.syntheticTables) |
Andy Wingo | 6339382 | 2021-01-14 09:15:56 | [diff] [blame] | 203 | if (!t->live) |
| 204 | message("removing unused section " + toString(t)); |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 | [diff] [blame] | 205 | } |
| 206 | } |
Sam Clegg | ad2e12a | 2019-10-10 03:23:06 | [diff] [blame] | 207 | |
Dan Gohman | 6cd8511 | 2020-10-01 00:21:57 | [diff] [blame] | 208 | bool MarkLive::isCallCtorsLive() { |
| 209 | // In a reloctable link, we don't call `__wasm_call_ctors`. |
Fangrui Song | 3792b36 | 2025-01-03 01:08:18 | [diff] [blame] | 210 | if (ctx.arg.relocatable) |
Dan Gohman | 6cd8511 | 2020-10-01 00:21:57 | [diff] [blame] | 211 | return false; |
| 212 | |
| 213 | // In Emscripten-style PIC, we call `__wasm_call_ctors` which calls |
Sam Clegg | 5c01648 | 2021-01-28 16:20:42 | [diff] [blame] | 214 | // `__wasm_apply_data_relocs`. |
Sam Clegg | 184c22d | 2024-01-18 23:01:21 | [diff] [blame] | 215 | if (ctx.isPic) |
Dan Gohman | 6cd8511 | 2020-10-01 00:21:57 | [diff] [blame] | 216 | return true; |
| 217 | |
| 218 | // If there are any init functions, mark `__wasm_call_ctors` live so that |
| 219 | // it can call them. |
Sam Clegg | 3c58457 | 2024-01-18 23:53:13 | [diff] [blame] | 220 | for (const ObjFile *file : ctx.objectFiles) { |
Dan Gohman | 6cd8511 | 2020-10-01 00:21:57 | [diff] [blame] | 221 | const WasmLinkingData &l = file->getWasmObj()->linkingData(); |
Dan Gohman | 950ae43 | 2020-10-01 03:00:04 | [diff] [blame] | 222 | for (const WasmInitFunc &f : l.InitFunctions) { |
| 223 | auto *sym = file->getFunctionSymbol(f.Symbol); |
| 224 | if (!sym->isDiscarded() && sym->isLive()) |
Dan Gohman | 6cd8511 | 2020-10-01 00:21:57 | [diff] [blame] | 225 | return true; |
Dan Gohman | 950ae43 | 2020-10-01 03:00:04 | [diff] [blame] | 226 | } |
Dan Gohman | 6cd8511 | 2020-10-01 00:21:57 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return false; |
| 230 | } |
| 231 | |
Sam Clegg | d32f71a | 2023-03-06 17:55:00 | [diff] [blame] | 232 | } // namespace lld::wasm |