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