Coverage Report

Created: 2019-01-18 03:29

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