blob: 2b30f6a2ffdb33bfce591e40c75918b5c5b6426e [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"
25#include "SymbolTable.h"
26#include "Symbols.h"
27
28#define DEBUG_TYPE "lld"
29
30using namespace llvm;
31using namespace llvm::wasm;
32using namespace lld;
33using namespace lld::wasm;
34
35void lld::wasm::markLive() {
36 if (!Config->GcSections)
37 return;
38
39 DEBUG(dbgs() << "markLive\n");
40 SmallVector<InputChunk *, 256> Q;
41
42 auto Enqueue = [&](Symbol *Sym) {
Nicholas Wilsona1e299f2018-04-20 17:18:06 +000043 if (!Sym || Sym->isLive())
Sam Clegg03626332018-01-31 01:45:47 +000044 return;
Nicholas Wilsona1e299f2018-04-20 17:18:06 +000045 Sym->markLive();
46 if (InputChunk *Chunk = Sym->getChunk())
47 Q.push_back(Chunk);
Sam Clegg03626332018-01-31 01:45:47 +000048 };
49
50 // Add GC root symbols.
51 if (!Config->Entry.empty())
52 Enqueue(Symtab->find(Config->Entry));
Sam Cleggf0d433d2018-02-02 22:59:56 +000053 Enqueue(WasmSym::CallCtors);
Sam Clegg03626332018-01-31 01:45:47 +000054
55 // By default we export all non-hidden, so they are gc roots too
56 for (Symbol *Sym : Symtab->getSymbols())
57 if (!Sym->isHidden())
58 Enqueue(Sym);
59
Sam Clegg3d1f4b92018-02-16 18:37:32 +000060 // The ctor functions are all used in the synthetic __wasm_call_ctors
61 // function, but since this function is created in-place it doesn't contain
Rui Ueyama34133b232018-02-19 22:34:47 +000062 // relocations which mean we have to manually mark the ctors.
Sam Clegg03626332018-01-31 01:45:47 +000063 for (const ObjFile *Obj : Symtab->ObjectFiles) {
64 const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
65 for (const WasmInitFunc &F : L.InitFunctions)
Sam Clegg93102972018-02-23 05:08:53 +000066 Enqueue(Obj->getFunctionSymbol(F.Symbol));
Sam Clegg03626332018-01-31 01:45:47 +000067 }
68
Rui Ueyama34133b232018-02-19 22:34:47 +000069 // Follow relocations to mark all reachable chunks.
70 while (!Q.empty()) {
71 InputChunk *C = Q.pop_back_val();
72
73 for (const WasmRelocation Reloc : C->getRelocations()) {
Nicholas Wilson2e55ee72018-03-09 17:06:38 +000074 if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB)
75 continue;
76 Symbol *Sym = C->File->getSymbol(Reloc.Index);
77
78 // If the function has been assigned the special index zero in the table,
79 // the relocation doesn't pull in the function body, since the function
80 // won't actually go in the table (the runtime will trap attempts to call
81 // that index, since we don't use it). A function with a table index of
82 // zero is only reachable via "call", not via "call_indirect". The stub
83 // functions used for weak-undefined symbols have this behaviour (compare
84 // equal to null pointer, only reachable via direct call).
85 if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB ||
86 Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32) {
87 FunctionSymbol *FuncSym = cast<FunctionSymbol>(Sym);
88 if (FuncSym->hasTableIndex() && FuncSym->getTableIndex() == 0)
89 continue;
90 }
91
92 Enqueue(Sym);
Sam Clegg03626332018-01-31 01:45:47 +000093 }
Rui Ueyama34133b232018-02-19 22:34:47 +000094 }
Sam Clegg03626332018-01-31 01:45:47 +000095
96 // Report garbage-collected sections.
97 if (Config->PrintGcSections) {
Sam Clegg03626332018-01-31 01:45:47 +000098 for (const ObjFile *Obj : Symtab->ObjectFiles) {
99 for (InputChunk *C : Obj->Functions)
Rui Ueyama81bee042018-02-19 22:29:48 +0000100 if (!C->Live)
101 message("removing unused section " + toString(C));
Sam Clegg03626332018-01-31 01:45:47 +0000102 for (InputChunk *C : Obj->Segments)
Rui Ueyama81bee042018-02-19 22:29:48 +0000103 if (!C->Live)
104 message("removing unused section " + toString(C));
Sam Clegg03626332018-01-31 01:45:47 +0000105 }
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000106 for (InputChunk *C : Symtab->SyntheticFunctions)
107 if (!C->Live)
108 message("removing unused section " + toString(C));
Sam Clegg03626332018-01-31 01:45:47 +0000109 }
110}