xref: /llvm-project-15.0.7/lld/wasm/MarkLive.cpp (revision fe2bd543)
1 //===- MarkLive.cpp -------------------------------------------------------===//
2 //
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
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"
24 #include "InputEvent.h"
25 #include "InputGlobal.h"
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 
34 namespace lld {
35 namespace wasm {
36 
37 namespace {
38 
39 class MarkLive {
40 public:
41   void run();
42 
43 private:
44   void enqueue(Symbol *sym);
45   void markSymbol(Symbol *sym);
46   void mark();
47   bool isCallCtorsLive();
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())
57     return;
58   LLVM_DEBUG(dbgs() << "markLive: " << sym->getName() << "\n");
59   sym->markLive();
60   if (InputChunk *chunk = sym->getChunk())
61     queue.push_back(chunk);
62 }
63 
64 void MarkLive::run() {
65   // Add GC root symbols.
66   if (!config->entry.empty())
67     enqueue(symtab->find(config->entry));
68 
69   // We need to preserve any no-strip or exported symbol
70   for (Symbol *sym : symtab->getSymbols())
71     if (sym->isNoStrip() || sym->isExported())
72       enqueue(sym);
73 
74   // If we'll be calling the user's `__wasm_call_dtors` function, mark it live.
75   if (Symbol *callDtors = WasmSym::callDtors)
76     enqueue(callDtors);
77 
78   // The ctor functions are all referenced by the synthetic callCtors
79   // function.  However, this function does not contain relocations so we
80   // have to manually mark the ctors as live.
81   for (const ObjFile *obj : symtab->objectFiles) {
82     const WasmLinkingData &l = obj->getWasmObj()->linkingData();
83     for (const WasmInitFunc &f : l.InitFunctions) {
84       auto *initSym = obj->getFunctionSymbol(f.Symbol);
85       if (!initSym->isDiscarded())
86         enqueue(initSym);
87     }
88   }
89 
90   // In Emscripten-style PIC, `__wasm_call_ctors` calls `__wasm_apply_relocs`.
91   if (config->isPic)
92     enqueue(WasmSym::applyRelocs);
93 
94   // If we have any non-discarded init functions, mark `__wasm_call_ctors` as
95   // live so that we assign it an index and call it.
96   if (isCallCtorsLive())
97     enqueue(WasmSym::callCtors);
98 
99   if (config->sharedMemory && !config->shared)
100     enqueue(WasmSym::initMemory);
101 
102   mark();
103 }
104 
105 void MarkLive::mark() {
106   // Follow relocations to mark all reachable chunks.
107   while (!queue.empty()) {
108     InputChunk *c = queue.pop_back_val();
109 
110     for (const WasmRelocation reloc : c->getRelocations()) {
111       if (reloc.Type == R_WASM_TYPE_INDEX_LEB)
112         continue;
113       Symbol *sym = c->file->getSymbol(reloc.Index);
114 
115       // If the function has been assigned the special index zero in the table,
116       // the relocation doesn't pull in the function body, since the function
117       // won't actually go in the table (the runtime will trap attempts to call
118       // that index, since we don't use it).  A function with a table index of
119       // zero is only reachable via "call", not via "call_indirect".  The stub
120       // functions used for weak-undefined symbols have this behaviour (compare
121       // equal to null pointer, only reachable via direct call).
122       if (reloc.Type == R_WASM_TABLE_INDEX_SLEB ||
123           reloc.Type == R_WASM_TABLE_INDEX_SLEB64 ||
124           reloc.Type == R_WASM_TABLE_INDEX_I32 ||
125           reloc.Type == R_WASM_TABLE_INDEX_I64) {
126         auto *funcSym = cast<FunctionSymbol>(sym);
127         if (funcSym->hasTableIndex() && funcSym->getTableIndex() == 0)
128           continue;
129       }
130 
131       enqueue(sym);
132     }
133   }
134 }
135 
136 void markLive() {
137   if (!config->gcSections)
138     return;
139 
140   LLVM_DEBUG(dbgs() << "markLive\n");
141 
142   MarkLive marker;
143   marker.run();
144 
145   // Report garbage-collected sections.
146   if (config->printGcSections) {
147     for (const ObjFile *obj : symtab->objectFiles) {
148       for (InputChunk *c : obj->functions)
149         if (!c->live)
150           message("removing unused section " + toString(c));
151       for (InputChunk *c : obj->segments)
152         if (!c->live)
153           message("removing unused section " + toString(c));
154       for (InputGlobal *g : obj->globals)
155         if (!g->live)
156           message("removing unused section " + toString(g));
157       for (InputEvent *e : obj->events)
158         if (!e->live)
159           message("removing unused section " + toString(e));
160     }
161     for (InputChunk *c : symtab->syntheticFunctions)
162       if (!c->live)
163         message("removing unused section " + toString(c));
164     for (InputGlobal *g : symtab->syntheticGlobals)
165       if (!g->live)
166         message("removing unused section " + toString(g));
167   }
168 }
169 
170 bool MarkLive::isCallCtorsLive() {
171   // In a reloctable link, we don't call `__wasm_call_ctors`.
172   if (config->relocatable)
173     return false;
174 
175   // In Emscripten-style PIC, we call `__wasm_call_ctors` which calls
176   // `__wasm_apply_relocs`.
177   if (config->isPic)
178     return true;
179 
180   // If there are any init functions, mark `__wasm_call_ctors` live so that
181   // it can call them.
182   for (const ObjFile *file : symtab->objectFiles) {
183     const WasmLinkingData &l = file->getWasmObj()->linkingData();
184     for (const WasmInitFunc &f : l.InitFunctions)
185       if (!file->getFunctionSymbol(f.Symbol)->isDiscarded())
186         return true;
187   }
188 
189   return false;
190 }
191 
192 } // namespace wasm
193 } // namespace lld
194