xref: /llvm-project-15.0.7/lld/wasm/MarkLive.cpp (revision ccbe567f)
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 void lld::wasm::markLive() {
35   if (!Config->GcSections)
36     return;
37 
38   LLVM_DEBUG(dbgs() << "markLive\n");
39   SmallVector<InputChunk *, 256> Q;
40 
41   std::function<void(Symbol*)> Enqueue = [&](Symbol *Sym) {
42     if (!Sym || Sym->isLive())
43       return;
44     LLVM_DEBUG(dbgs() << "markLive: " << Sym->getName() << "\n");
45     Sym->markLive();
46     if (InputChunk *Chunk = Sym->getChunk())
47       Q.push_back(Chunk);
48 
49     // The ctor functions are all referenced by the synthetic CallCtors
50     // function.  However, this function does not contain relocations so we
51     // have to manually mark the ctors as live if CallCtors itself is live.
52     if (Sym == WasmSym::CallCtors) {
53       for (const ObjFile *Obj : Symtab->ObjectFiles) {
54         const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
55         for (const WasmInitFunc &F : L.InitFunctions)
56           Enqueue(Obj->getFunctionSymbol(F.Symbol));
57       }
58     }
59   };
60 
61   // Add GC root symbols.
62   if (!Config->Entry.empty())
63     Enqueue(Symtab->find(Config->Entry));
64 
65   // We need to preserve any exported symbol
66   for (Symbol *Sym : Symtab->getSymbols())
67     if (Sym->isExported())
68       Enqueue(Sym);
69 
70   // For relocatable output, we need to preserve all the ctor functions
71   if (Config->Relocatable) {
72     for (const ObjFile *Obj : Symtab->ObjectFiles) {
73       const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
74       for (const WasmInitFunc &F : L.InitFunctions)
75         Enqueue(Obj->getFunctionSymbol(F.Symbol));
76     }
77   }
78 
79   if (Config->Pic) {
80     Enqueue(WasmSym::CallCtors);
81     Enqueue(WasmSym::ApplyRelocs);
82   }
83 
84   // Follow relocations to mark all reachable chunks.
85   while (!Q.empty()) {
86     InputChunk *C = Q.pop_back_val();
87 
88     for (const WasmRelocation Reloc : C->getRelocations()) {
89       if (Reloc.Type == R_WASM_TYPE_INDEX_LEB)
90         continue;
91       Symbol *Sym = C->File->getSymbol(Reloc.Index);
92 
93       // If the function has been assigned the special index zero in the table,
94       // the relocation doesn't pull in the function body, since the function
95       // won't actually go in the table (the runtime will trap attempts to call
96       // that index, since we don't use it).  A function with a table index of
97       // zero is only reachable via "call", not via "call_indirect".  The stub
98       // functions used for weak-undefined symbols have this behaviour (compare
99       // equal to null pointer, only reachable via direct call).
100       if (Reloc.Type == R_WASM_TABLE_INDEX_SLEB ||
101           Reloc.Type == R_WASM_TABLE_INDEX_I32) {
102         auto *FuncSym = cast<FunctionSymbol>(Sym);
103         if (FuncSym->hasTableIndex() && FuncSym->getTableIndex() == 0)
104           continue;
105       }
106 
107       Enqueue(Sym);
108     }
109   }
110 
111   // Report garbage-collected sections.
112   if (Config->PrintGcSections) {
113     for (const ObjFile *Obj : Symtab->ObjectFiles) {
114       for (InputChunk *C : Obj->Functions)
115         if (!C->Live)
116           message("removing unused section " + toString(C));
117       for (InputChunk *C : Obj->Segments)
118         if (!C->Live)
119           message("removing unused section " + toString(C));
120       for (InputGlobal *G : Obj->Globals)
121         if (!G->Live)
122           message("removing unused section " + toString(G));
123       for (InputEvent *E : Obj->Events)
124         if (!E->Live)
125           message("removing unused section " + toString(E));
126     }
127     for (InputChunk *C : Symtab->SyntheticFunctions)
128       if (!C->Live)
129         message("removing unused section " + toString(C));
130     for (InputGlobal *G : Symtab->SyntheticGlobals)
131       if (!G->Live)
132         message("removing unused section " + toString(G));
133   }
134 }
135