Coverage Report

Created: 2019-05-19 14:56

/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
// 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
6
//
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"
24
#include "InputEvent.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
34
162
void lld::wasm::markLive() {
35
162
  if (!Config->GcSections)
36
38
    return;
37
124
38
124
  LLVM_DEBUG(dbgs() << "markLive\n");
39
124
  SmallVector<InputChunk *, 256> Q;
40
124
41
695
  std::function<void(Symbol*)> Enqueue = [&](Symbol *Sym) {
42
695
    if (!Sym || Sym->isLive())
43
178
      return;
44
517
    LLVM_DEBUG(dbgs() << "markLive: " << Sym->getName() << "\n");
45
517
    Sym->markLive();
46
517
    if (InputChunk *Chunk = Sym->getChunk())
47
257
      Q.push_back(Chunk);
48
517
49
517
    // The ctor functions are all referenced by the synthetic CallCtors
50
517
    // function.  However, this function does not contain relocations so we
51
517
    // have to manually mark the ctors as live if CallCtors itself is live.
52
517
    if (Sym == WasmSym::CallCtors) {
53
9
      for (const ObjFile *Obj : Symtab->ObjectFiles) {
54
9
        const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
55
9
        for (const WasmInitFunc &F : L.InitFunctions)
56
15
          Enqueue(Obj->getFunctionSymbol(F.Symbol));
57
9
      }
58
8
    }
59
517
  };
60
124
61
124
  // Add GC root symbols.
62
124
  if (!Config->Entry.empty())
63
88
    Enqueue(Symtab->find(Config->Entry));
64
124
65
124
  // We need to preserve any exported symbol
66
124
  for (Symbol *Sym : Symtab->getSymbols())
67
904
    if (Sym->isExported())
68
410
      Enqueue(Sym);
69
124
70
124
  // For relocatable output, we need to preserve all the ctor functions
71
124
  if (Config->Relocatable) {
72
0
    for (const ObjFile *Obj : Symtab->ObjectFiles) {
73
0
      const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
74
0
      for (const WasmInitFunc &F : L.InitFunctions)
75
0
        Enqueue(Obj->getFunctionSymbol(F.Symbol));
76
0
    }
77
0
  }
78
124
79
124
  if (Config->Pic) {
80
4
    Enqueue(WasmSym::CallCtors);
81
4
    Enqueue(WasmSym::ApplyRelocs);
82
4
  }
83
124
84
124
  // Follow relocations to mark all reachable chunks.
85
381
  while (!Q.empty()) {
86
257
    InputChunk *C = Q.pop_back_val();
87
257
88
257
    for (const WasmRelocation Reloc : C->getRelocations()) {
89
189
      if (Reloc.Type == R_WASM_TYPE_INDEX_LEB)
90
12
        continue;
91
177
      Symbol *Sym = C->File->getSymbol(Reloc.Index);
92
177
93
177
      // If the function has been assigned the special index zero in the table,
94
177
      // the relocation doesn't pull in the function body, since the function
95
177
      // won't actually go in the table (the runtime will trap attempts to call
96
177
      // that index, since we don't use it).  A function with a table index of
97
177
      // zero is only reachable via "call", not via "call_indirect".  The stub
98
177
      // functions used for weak-undefined symbols have this behaviour (compare
99
177
      // equal to null pointer, only reachable via direct call).
100
177
      if (Reloc.Type == R_WASM_TABLE_INDEX_SLEB ||
101
177
          
Reloc.Type == R_WASM_TABLE_INDEX_I32153
) {
102
37
        auto *FuncSym = cast<FunctionSymbol>(Sym);
103
37
        if (FuncSym->hasTableIndex() && 
FuncSym->getTableIndex() == 03
)
104
3
          continue;
105
174
      }
106
174
107
174
      Enqueue(Sym);
108
174
    }
109
257
  }
110
124
111
124
  // Report garbage-collected sections.
112
124
  if (Config->PrintGcSections) {
113
3
    for (const ObjFile *Obj : Symtab->ObjectFiles) {
114
3
      for (InputChunk *C : Obj->Functions)
115
5
        if (!C->Live)
116
1
          message("removing unused section " + toString(C));
117
3
      for (InputChunk *C : Obj->Segments)
118
2
        if (!C->Live)
119
1
          message("removing unused section " + toString(C));
120
3
      for (InputGlobal *G : Obj->Globals)
121
2
        if (!G->Live)
122
1
          message("removing unused section " + toString(G));
123
3
      for (InputEvent *E : Obj->Events)
124
0
        if (!E->Live)
125
0
          message("removing unused section " + toString(E));
126
3
    }
127
2
    for (InputChunk *C : Symtab->SyntheticFunctions)
128
6
      if (!C->Live)
129
3
        message("removing unused section " + toString(C));
130
2
    for (InputGlobal *G : Symtab->SyntheticGlobals)
131
2
      if (!G->Live)
132
0
        message("removing unused section " + toString(G));
133
2
  }
134
124
}