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