Coverage Report

Created: 2018-06-18 20:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/MarkLive.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- 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 "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
using namespace lld;
34
using namespace lld::wasm;
35
36
0
void lld::wasm::markLive() {
37
0
  if (!Config->GcSections)
38
0
    return;
39
0
40
0
  LLVM_DEBUG(dbgs() << "markLive\n");
41
0
  SmallVector<InputChunk *, 256> Q;
42
0
43
0
  auto Enqueue = [&](Symbol *Sym) {
44
0
    if (!Sym || Sym->isLive())
45
0
      return;
46
0
    Sym->markLive();
47
0
    if (InputChunk *Chunk = Sym->getChunk())
48
0
      Q.push_back(Chunk);
49
0
  };
50
0
51
0
  // Add GC root symbols.
52
0
  if (!Config->Entry.empty())
53
0
    Enqueue(Symtab->find(Config->Entry));
54
0
  Enqueue(WasmSym::CallCtors);
55
0
56
0
  // By default we export all non-hidden, so they are gc roots too
57
0
  for (Symbol *Sym : Symtab->getSymbols())
58
0
    if (!Sym->isHidden())
59
0
      Enqueue(Sym);
60
0
61
0
  // The ctor functions are all used in the synthetic __wasm_call_ctors
62
0
  // function, but since this function is created in-place it doesn't contain
63
0
  // relocations which mean we have to manually mark the ctors.
64
0
  for (const ObjFile *Obj : Symtab->ObjectFiles) {
65
0
    const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
66
0
    for (const WasmInitFunc &F : L.InitFunctions)
67
0
      Enqueue(Obj->getFunctionSymbol(F.Symbol));
68
0
  }
69
0
70
0
  // Follow relocations to mark all reachable chunks.
71
0
  while (!Q.empty()) {
72
0
    InputChunk *C = Q.pop_back_val();
73
0
74
0
    for (const WasmRelocation Reloc : C->getRelocations()) {
75
0
      if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB)
76
0
        continue;
77
0
      Symbol *Sym = C->File->getSymbol(Reloc.Index);
78
0
79
0
      // If the function has been assigned the special index zero in the table,
80
0
      // the relocation doesn't pull in the function body, since the function
81
0
      // won't actually go in the table (the runtime will trap attempts to call
82
0
      // that index, since we don't use it).  A function with a table index of
83
0
      // zero is only reachable via "call", not via "call_indirect".  The stub
84
0
      // functions used for weak-undefined symbols have this behaviour (compare
85
0
      // equal to null pointer, only reachable via direct call).
86
0
      if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB ||
87
0
          Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32) {
88
0
        FunctionSymbol *FuncSym = cast<FunctionSymbol>(Sym);
89
0
        if (FuncSym->hasTableIndex() && FuncSym->getTableIndex() == 0)
90
0
          continue;
91
0
      }
92
0
93
0
      Enqueue(Sym);
94
0
    }
95
0
  }
96
0
97
0
  // Report garbage-collected sections.
98
0
  if (Config->PrintGcSections) {
99
0
    for (const ObjFile *Obj : Symtab->ObjectFiles) {
100
0
      for (InputChunk *C : Obj->Functions)
101
0
        if (!C->Live)
102
0
          message("removing unused section " + toString(C));
103
0
      for (InputChunk *C : Obj->Segments)
104
0
        if (!C->Live)
105
0
          message("removing unused section " + toString(C));
106
0
      for (InputGlobal *G : Obj->Globals)
107
0
        if (!G->Live)
108
0
          message("removing unused section " + toString(G));
109
0
    }
110
0
    for (InputChunk *C : Symtab->SyntheticFunctions)
111
0
      if (!C->Live)
112
0
        message("removing unused section " + toString(C));
113
0
    for (InputGlobal *G : Symtab->SyntheticGlobals)
114
0
      if (!G->Live)
115
0
        message("removing unused section " + toString(G));
116
0
  }
117
0
}