blob: 6906f86f7150af2ec9aca201a06e9ce47131035e [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
Sam Cleggad2e12a2019-10-10 03:23:06 +000034namespace lld {
35namespace wasm {
36
37namespace {
38
39class MarkLive {
40public:
41 void run();
42
43private:
44 void enqueue(Symbol *sym);
45 void markSymbol(Symbol *sym);
46 void mark();
47
48 // A list of chunks to visit.
49 SmallVector<InputChunk *, 256> queue;
50};
51
52} // namespace
53
54void MarkLive::enqueue(Symbol *sym) {
55 if (!sym || sym->isLive())
Sam Cleggffd0aaf2018-06-22 15:13:10 +000056 return;
Sam Cleggad2e12a2019-10-10 03:23:06 +000057 LLVM_DEBUG(dbgs() << "markLive: " << sym->getName() << "\n");
58 sym->markLive();
59 if (InputChunk *chunk = sym->getChunk())
60 queue.push_back(chunk);
Sam Cleggffd0aaf2018-06-22 15:13:10 +000061
Sam Cleggad2e12a2019-10-10 03:23:06 +000062 // The ctor functions are all referenced by the synthetic callCtors
63 // function. However, this function does not contain relocations so we
64 // have to manually mark the ctors as live if callCtors itself is live.
65 if (sym == WasmSym::callCtors) {
66 if (config->isPic)
67 enqueue(WasmSym::applyRelocs);
68 for (const ObjFile *obj : symtab->objectFiles) {
69 const WasmLinkingData &l = obj->getWasmObj()->linkingData();
70 for (const WasmInitFunc &f : l.InitFunctions) {
71 auto* initSym = obj->getFunctionSymbol(f.Symbol);
72 if (!initSym->isDiscarded())
73 enqueue(initSym);
Sam Clegg0e6b42f2019-03-01 22:35:47 +000074 }
75 }
Sam Cleggad2e12a2019-10-10 03:23:06 +000076 }
77}
Sam Clegg03626332018-01-31 01:45:47 +000078
Sam Cleggad2e12a2019-10-10 03:23:06 +000079void MarkLive::run() {
Sam Clegg03626332018-01-31 01:45:47 +000080 // Add GC root symbols.
Rui Ueyama136d27a2019-07-11 05:40:30 +000081 if (!config->entry.empty())
82 enqueue(symtab->find(config->entry));
Sam Clegg03626332018-01-31 01:45:47 +000083
Dan Gohman7cb9c8a2019-08-29 22:41:05 +000084 // We need to preserve any no-strip or exported symbol
Rui Ueyama136d27a2019-07-11 05:40:30 +000085 for (Symbol *sym : symtab->getSymbols())
Dan Gohman7cb9c8a2019-08-29 22:41:05 +000086 if (sym->isNoStrip() || sym->isExported())
Rui Ueyama136d27a2019-07-11 05:40:30 +000087 enqueue(sym);
Sam Clegg03626332018-01-31 01:45:47 +000088
Sam Clegg0e6b42f2019-03-01 22:35:47 +000089 // For relocatable output, we need to preserve all the ctor functions
Rui Ueyama136d27a2019-07-11 05:40:30 +000090 if (config->relocatable) {
91 for (const ObjFile *obj : symtab->objectFiles) {
92 const WasmLinkingData &l = obj->getWasmObj()->linkingData();
93 for (const WasmInitFunc &f : l.InitFunctions)
94 enqueue(obj->getFunctionSymbol(f.Symbol));
Sam Clegg0e6b42f2019-03-01 22:35:47 +000095 }
Sam Clegg03626332018-01-31 01:45:47 +000096 }
97
Rui Ueyama136d27a2019-07-11 05:40:30 +000098 if (config->isPic)
99 enqueue(WasmSym::callCtors);
Sam Clegg09137be2019-04-04 18:40:51 +0000100
Thomas Lively09768c52019-09-04 19:50:39 +0000101 if (config->sharedMemory && !config->shared)
102 enqueue(WasmSym::initMemory);
103
Sam Cleggad2e12a2019-10-10 03:23:06 +0000104 mark();
105}
106
107void MarkLive::mark() {
Rui Ueyama34133b232018-02-19 22:34:47 +0000108 // Follow relocations to mark all reachable chunks.
Sam Cleggad2e12a2019-10-10 03:23:06 +0000109 while (!queue.empty()) {
110 InputChunk *c = queue.pop_back_val();
Rui Ueyama34133b232018-02-19 22:34:47 +0000111
Rui Ueyama136d27a2019-07-11 05:40:30 +0000112 for (const WasmRelocation reloc : c->getRelocations()) {
113 if (reloc.Type == R_WASM_TYPE_INDEX_LEB)
Nicholas Wilson2e55ee72018-03-09 17:06:38 +0000114 continue;
Rui Ueyama136d27a2019-07-11 05:40:30 +0000115 Symbol *sym = c->file->getSymbol(reloc.Index);
Nicholas Wilson2e55ee72018-03-09 17:06:38 +0000116
117 // If the function has been assigned the special index zero in the table,
118 // the relocation doesn't pull in the function body, since the function
119 // won't actually go in the table (the runtime will trap attempts to call
120 // that index, since we don't use it). A function with a table index of
121 // zero is only reachable via "call", not via "call_indirect". The stub
122 // functions used for weak-undefined symbols have this behaviour (compare
123 // equal to null pointer, only reachable via direct call).
Rui Ueyama136d27a2019-07-11 05:40:30 +0000124 if (reloc.Type == R_WASM_TABLE_INDEX_SLEB ||
125 reloc.Type == R_WASM_TABLE_INDEX_I32) {
126 auto *funcSym = cast<FunctionSymbol>(sym);
127 if (funcSym->hasTableIndex() && funcSym->getTableIndex() == 0)
Nicholas Wilson2e55ee72018-03-09 17:06:38 +0000128 continue;
129 }
130
Rui Ueyama136d27a2019-07-11 05:40:30 +0000131 enqueue(sym);
Sam Clegg03626332018-01-31 01:45:47 +0000132 }
Rui Ueyama34133b232018-02-19 22:34:47 +0000133 }
Sam Cleggad2e12a2019-10-10 03:23:06 +0000134}
135
136void markLive() {
137 if (!config->gcSections)
138 return;
139
140 LLVM_DEBUG(dbgs() << "markLive\n");
141
142 MarkLive marker;
143 marker.run();
Sam Clegg03626332018-01-31 01:45:47 +0000144
145 // Report garbage-collected sections.
Rui Ueyama136d27a2019-07-11 05:40:30 +0000146 if (config->printGcSections) {
147 for (const ObjFile *obj : symtab->objectFiles) {
148 for (InputChunk *c : obj->functions)
149 if (!c->live)
150 message("removing unused section " + toString(c));
151 for (InputChunk *c : obj->segments)
152 if (!c->live)
153 message("removing unused section " + toString(c));
154 for (InputGlobal *g : obj->globals)
155 if (!g->live)
156 message("removing unused section " + toString(g));
157 for (InputEvent *e : obj->events)
158 if (!e->live)
159 message("removing unused section " + toString(e));
Sam Clegg03626332018-01-31 01:45:47 +0000160 }
Rui Ueyama136d27a2019-07-11 05:40:30 +0000161 for (InputChunk *c : symtab->syntheticFunctions)
162 if (!c->live)
163 message("removing unused section " + toString(c));
164 for (InputGlobal *g : symtab->syntheticGlobals)
165 if (!g->live)
166 message("removing unused section " + toString(g));
Sam Clegg03626332018-01-31 01:45:47 +0000167 }
168}
Sam Cleggad2e12a2019-10-10 03:23:06 +0000169
170} // namespace wasm
171} // namespace lld