blob: 08264925a7fc175f46792acb4b32133a0bdf6a7d [file] [log] [blame]
Sam Clegg03626332018-01-31 01:45:47 +00001//===- MarkLive.cpp -------------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sam Clegg03626332018-01-31 01:45:47 +00006//
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"
Heejin Ahne915a712018-12-08 06:17:43 +000024#include "InputEvent.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;
Sam Clegg03626332018-01-31 01:45:47 +000033
34void lld::wasm::markLive() {
Sam Cleggffd0aaf2018-06-22 15:13:10 +000035 if (!Config->GcSections)
36 return;
37
Nicola Zaghene7245b42018-05-15 13:36:20 +000038 LLVM_DEBUG(dbgs() << "markLive\n");
Sam Clegg03626332018-01-31 01:45:47 +000039 SmallVector<InputChunk *, 256> Q;
40
Sam Clegg0e6b42f2019-03-01 22:35:47 +000041 std::function<void(Symbol*)> Enqueue = [&](Symbol *Sym) {
Nicholas Wilsona1e299f2018-04-20 17:18:06 +000042 if (!Sym || Sym->isLive())
Sam Clegg03626332018-01-31 01:45:47 +000043 return;
Sam Clegg084d3602018-06-21 15:00:00 +000044 LLVM_DEBUG(dbgs() << "markLive: " << Sym->getName() << "\n");
Nicholas Wilsona1e299f2018-04-20 17:18:06 +000045 Sym->markLive();
46 if (InputChunk *Chunk = Sym->getChunk())
47 Q.push_back(Chunk);
Sam Clegg0e6b42f2019-03-01 22:35:47 +000048
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) {
Thomas Lively6004d9a2019-07-03 22:04:54 +000053 if (Config->PassiveSegments)
54 Enqueue(WasmSym::InitMemory);
55 if (Config->Pic)
56 Enqueue(WasmSym::ApplyRelocs);
Sam Clegg0e6b42f2019-03-01 22:35:47 +000057 for (const ObjFile *Obj : Symtab->ObjectFiles) {
58 const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
Sam Cleggfd54fa52019-06-07 06:00:46 +000059 for (const WasmInitFunc &F : L.InitFunctions) {
60 auto* InitSym = Obj->getFunctionSymbol(F.Symbol);
61 if (!InitSym->isDiscarded())
62 Enqueue(InitSym);
63 }
Sam Clegg0e6b42f2019-03-01 22:35:47 +000064 }
65 }
Sam Clegg03626332018-01-31 01:45:47 +000066 };
67
68 // Add GC root symbols.
69 if (!Config->Entry.empty())
70 Enqueue(Symtab->find(Config->Entry));
Sam Clegg03626332018-01-31 01:45:47 +000071
Sam Cleggce004bf2018-06-28 17:04:58 +000072 // We need to preserve any exported symbol
Sam Clegg03626332018-01-31 01:45:47 +000073 for (Symbol *Sym : Symtab->getSymbols())
Sam Cleggce004bf2018-06-28 17:04:58 +000074 if (Sym->isExported())
Sam Clegg03626332018-01-31 01:45:47 +000075 Enqueue(Sym);
76
Sam Clegg0e6b42f2019-03-01 22:35:47 +000077 // For relocatable output, we need to preserve all the ctor functions
78 if (Config->Relocatable) {
79 for (const ObjFile *Obj : Symtab->ObjectFiles) {
80 const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
81 for (const WasmInitFunc &F : L.InitFunctions)
82 Enqueue(Obj->getFunctionSymbol(F.Symbol));
83 }
Sam Clegg03626332018-01-31 01:45:47 +000084 }
85
Thomas Lively6004d9a2019-07-03 22:04:54 +000086 if (Config->Pic)
Sam Clegg09137be2019-04-04 18:40:51 +000087 Enqueue(WasmSym::CallCtors);
Sam Clegg09137be2019-04-04 18:40:51 +000088
Rui Ueyama34133b232018-02-19 22:34:47 +000089 // Follow relocations to mark all reachable chunks.
90 while (!Q.empty()) {
91 InputChunk *C = Q.pop_back_val();
92
93 for (const WasmRelocation Reloc : C->getRelocations()) {
Sam Clegg79e33172019-02-04 17:49:33 +000094 if (Reloc.Type == R_WASM_TYPE_INDEX_LEB)
Nicholas Wilson2e55ee72018-03-09 17:06:38 +000095 continue;
96 Symbol *Sym = C->File->getSymbol(Reloc.Index);
97
98 // If the function has been assigned the special index zero in the table,
99 // the relocation doesn't pull in the function body, since the function
100 // won't actually go in the table (the runtime will trap attempts to call
101 // that index, since we don't use it). A function with a table index of
102 // zero is only reachable via "call", not via "call_indirect". The stub
103 // functions used for weak-undefined symbols have this behaviour (compare
104 // equal to null pointer, only reachable via direct call).
Sam Clegg79e33172019-02-04 17:49:33 +0000105 if (Reloc.Type == R_WASM_TABLE_INDEX_SLEB ||
106 Reloc.Type == R_WASM_TABLE_INDEX_I32) {
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000107 auto *FuncSym = cast<FunctionSymbol>(Sym);
Nicholas Wilson2e55ee72018-03-09 17:06:38 +0000108 if (FuncSym->hasTableIndex() && FuncSym->getTableIndex() == 0)
109 continue;
110 }
111
112 Enqueue(Sym);
Sam Clegg03626332018-01-31 01:45:47 +0000113 }
Rui Ueyama34133b232018-02-19 22:34:47 +0000114 }
Sam Clegg03626332018-01-31 01:45:47 +0000115
116 // Report garbage-collected sections.
117 if (Config->PrintGcSections) {
Sam Clegg03626332018-01-31 01:45:47 +0000118 for (const ObjFile *Obj : Symtab->ObjectFiles) {
119 for (InputChunk *C : Obj->Functions)
Rui Ueyama81bee042018-02-19 22:29:48 +0000120 if (!C->Live)
121 message("removing unused section " + toString(C));
Sam Clegg03626332018-01-31 01:45:47 +0000122 for (InputChunk *C : Obj->Segments)
Rui Ueyama81bee042018-02-19 22:29:48 +0000123 if (!C->Live)
124 message("removing unused section " + toString(C));
Nicholas Wilson358af382018-04-20 17:28:12 +0000125 for (InputGlobal *G : Obj->Globals)
126 if (!G->Live)
127 message("removing unused section " + toString(G));
Heejin Ahne915a712018-12-08 06:17:43 +0000128 for (InputEvent *E : Obj->Events)
129 if (!E->Live)
130 message("removing unused section " + toString(E));
Sam Clegg03626332018-01-31 01:45:47 +0000131 }
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000132 for (InputChunk *C : Symtab->SyntheticFunctions)
133 if (!C->Live)
134 message("removing unused section " + toString(C));
Nicholas Wilson358af382018-04-20 17:28:12 +0000135 for (InputGlobal *G : Symtab->SyntheticGlobals)
136 if (!G->Live)
137 message("removing unused section " + toString(G));
Sam Clegg03626332018-01-31 01:45:47 +0000138 }
139}