Coverage Report

Created: 2018-10-23 15:26

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