blob: 018b3255b55b105f256ce1ee9adebc33b8b71d42 [file] [log] [blame]
Sam Clegg03626332018-01-31 01:45:47 +00001//===- MarkLive.cpp -------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements --gc-sections, which is a feature to remove unused
11// chunks from the output. Unused chunks are those that are not reachable from
12// known root symbols or chunks. This feature is implemented as a mark-sweep
13// garbage collector.
14//
15// Here's how it works. Each InputChunk has a "Live" bit. The bit is off by
16// default. Starting with the GC-roots, visit all reachable chunks and set their
17// Live bits. The Writer will then ignore chunks whose Live bits are off, so
18// that such chunk are not appear in the output.
19//
20//===----------------------------------------------------------------------===//
21
22#include "MarkLive.h"
23#include "Config.h"
24#include "InputChunks.h"
Nicholas Wilson358af382018-04-20 17:28:12 +000025#include "InputGlobal.h"
Sam Clegg03626332018-01-31 01:45:47 +000026#include "SymbolTable.h"
27#include "Symbols.h"
28
29#define DEBUG_TYPE "lld"
30
31using namespace llvm;
32using namespace llvm::wasm;
33using namespace lld;
34using namespace lld::wasm;
35
36void lld::wasm::markLive() {
Sam Cleggffd0aaf2018-06-22 15:13:10 +000037 if (!Config->GcSections)
38 return;
39
Nicola Zaghene7245b42018-05-15 13:36:20 +000040 LLVM_DEBUG(dbgs() << "markLive\n");
Sam Clegg03626332018-01-31 01:45:47 +000041 SmallVector<InputChunk *, 256> Q;
42
43 auto Enqueue = [&](Symbol *Sym) {
Nicholas Wilsona1e299f2018-04-20 17:18:06 +000044 if (!Sym || Sym->isLive())
Sam Clegg03626332018-01-31 01:45:47 +000045 return;
Sam Clegg084d3602018-06-21 15:00:00 +000046 LLVM_DEBUG(dbgs() << "markLive: " << Sym->getName() << "\n");
Nicholas Wilsona1e299f2018-04-20 17:18:06 +000047 Sym->markLive();
48 if (InputChunk *Chunk = Sym->getChunk())
49 Q.push_back(Chunk);
Sam Clegg03626332018-01-31 01:45:47 +000050 };
51
52 // Add GC root symbols.
53 if (!Config->Entry.empty())
54 Enqueue(Symtab->find(Config->Entry));
Sam Cleggf0d433d2018-02-02 22:59:56 +000055 Enqueue(WasmSym::CallCtors);
Sam Clegg03626332018-01-31 01:45:47 +000056
Sam Clegg084d3602018-06-21 15:00:00 +000057 // We export all defined, non-hidden symbols so they are all gc roots too
Sam Clegg03626332018-01-31 01:45:47 +000058 for (Symbol *Sym : Symtab->getSymbols())
Sam Clegg084d3602018-06-21 15:00:00 +000059 if (Sym->isDefined() && !Sym->isHidden())
Sam Clegg03626332018-01-31 01:45:47 +000060 Enqueue(Sym);
61
Sam Clegg3d1f4b92018-02-16 18:37:32 +000062 // The ctor functions are all used in the synthetic __wasm_call_ctors
63 // function, but since this function is created in-place it doesn't contain
Rui Ueyama34133b232018-02-19 22:34:47 +000064 // relocations which mean we have to manually mark the ctors.
Sam Clegg03626332018-01-31 01:45:47 +000065 for (const ObjFile *Obj : Symtab->ObjectFiles) {
66 const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
67 for (const WasmInitFunc &F : L.InitFunctions)
Sam Clegg93102972018-02-23 05:08:53 +000068 Enqueue(Obj->getFunctionSymbol(F.Symbol));
Sam Clegg03626332018-01-31 01:45:47 +000069 }
70
Rui Ueyama34133b232018-02-19 22:34:47 +000071 // Follow relocations to mark all reachable chunks.
72 while (!Q.empty()) {
73 InputChunk *C = Q.pop_back_val();
74
75 for (const WasmRelocation Reloc : C->getRelocations()) {
Nicholas Wilson2e55ee72018-03-09 17:06:38 +000076 if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB)
77 continue;
78 Symbol *Sym = C->File->getSymbol(Reloc.Index);
79
80 // If the function has been assigned the special index zero in the table,
81 // the relocation doesn't pull in the function body, since the function
82 // won't actually go in the table (the runtime will trap attempts to call
83 // that index, since we don't use it). A function with a table index of
84 // zero is only reachable via "call", not via "call_indirect". The stub
85 // functions used for weak-undefined symbols have this behaviour (compare
86 // equal to null pointer, only reachable via direct call).
87 if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB ||
88 Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32) {
89 FunctionSymbol *FuncSym = cast<FunctionSymbol>(Sym);
90 if (FuncSym->hasTableIndex() && FuncSym->getTableIndex() == 0)
91 continue;
92 }
93
94 Enqueue(Sym);
Sam Clegg03626332018-01-31 01:45:47 +000095 }
Rui Ueyama34133b232018-02-19 22:34:47 +000096 }
Sam Clegg03626332018-01-31 01:45:47 +000097
98 // Report garbage-collected sections.
99 if (Config->PrintGcSections) {
Sam Clegg03626332018-01-31 01:45:47 +0000100 for (const ObjFile *Obj : Symtab->ObjectFiles) {
101 for (InputChunk *C : Obj->Functions)
Rui Ueyama81bee042018-02-19 22:29:48 +0000102 if (!C->Live)
103 message("removing unused section " + toString(C));
Sam Clegg03626332018-01-31 01:45:47 +0000104 for (InputChunk *C : Obj->Segments)
Rui Ueyama81bee042018-02-19 22:29:48 +0000105 if (!C->Live)
106 message("removing unused section " + toString(C));
Nicholas Wilson358af382018-04-20 17:28:12 +0000107 for (InputGlobal *G : Obj->Globals)
108 if (!G->Live)
109 message("removing unused section " + toString(G));
Sam Clegg03626332018-01-31 01:45:47 +0000110 }
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000111 for (InputChunk *C : Symtab->SyntheticFunctions)
112 if (!C->Live)
113 message("removing unused section " + toString(C));
Nicholas Wilson358af382018-04-20 17:28:12 +0000114 for (InputGlobal *G : Symtab->SyntheticGlobals)
115 if (!G->Live)
116 message("removing unused section " + toString(G));
Sam Clegg03626332018-01-31 01:45:47 +0000117 }
118}