Coverage Report

Created: 2018-10-20 12:32

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/SymbolTable.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- SymbolTable.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
#include "SymbolTable.h"
11
#include "Config.h"
12
#include "InputChunks.h"
13
#include "InputGlobal.h"
14
#include "WriterUtils.h"
15
#include "lld/Common/ErrorHandler.h"
16
#include "lld/Common/Memory.h"
17
#include "llvm/ADT/SetVector.h"
18
19
#define DEBUG_TYPE "lld"
20
21
using namespace llvm;
22
using namespace llvm::wasm;
23
using namespace lld;
24
using namespace lld::wasm;
25
26
SymbolTable *lld::wasm::Symtab;
27
28
201
void SymbolTable::addFile(InputFile *File) {
29
201
  log("Processing: " + toString(File));
30
201
  File->parse();
31
201
32
201
  // LLVM bitcode file
33
201
  if (auto *F = dyn_cast<BitcodeFile>(File))
34
38
    BitcodeFiles.push_back(F);
35
163
  else if (auto *F = dyn_cast<ObjFile>(File))
36
148
    ObjectFiles.push_back(F);
37
201
}
38
39
// This function is where all the optimizations of link-time
40
// optimization happens. When LTO is in use, some input files are
41
// not in native object file format but in the LLVM bitcode format.
42
// This function compiles bitcode files into a few big native files
43
// using LLVM functions and replaces bitcode symbols with the results.
44
// Because all bitcode files that the program consists of are passed
45
// to the compiler at once, it can do whole-program optimization.
46
101
void SymbolTable::addCombinedLTOObject() {
47
101
  if (BitcodeFiles.empty())
48
76
    return;
49
25
50
25
  // Compile bitcode files and replace bitcode symbols.
51
25
  LTO.reset(new BitcodeCompiler);
52
25
  for (BitcodeFile *F : BitcodeFiles)
53
36
    LTO->add(*F);
54
25
55
42
  for (StringRef Filename : LTO->compile()) {
56
42
    auto *Obj = make<ObjFile>(MemoryBufferRef(Filename, "lto.tmp"));
57
42
    Obj->parse();
58
42
    ObjectFiles.push_back(Obj);
59
42
  }
60
25
}
61
62
84
void SymbolTable::reportRemainingUndefines() {
63
671
  for (Symbol *Sym : SymVector) {
64
671
    if (!Sym->isUndefined() || 
Sym->isWeak()8
)
65
664
      continue;
66
7
    if (Config->AllowUndefinedSymbols.count(Sym->getName()) != 0)
67
3
      continue;
68
4
    if (!Sym->IsUsedInRegularObj)
69
0
      continue;
70
4
    error(toString(Sym->getFile()) + ": undefined symbol: " + toString(*Sym));
71
4
  }
72
84
}
73
74
167
Symbol *SymbolTable::find(StringRef Name) {
75
167
  return SymMap.lookup(CachedHashStringRef(Name));
76
167
}
77
78
1.18k
std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, InputFile *File) {
79
1.18k
  bool Inserted = false;
80
1.18k
  Symbol *&Sym = SymMap[CachedHashStringRef(Name)];
81
1.18k
  if (!Sym) {
82
1.04k
    Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
83
1.04k
    Sym->IsUsedInRegularObj = false;
84
1.04k
    SymVector.emplace_back(Sym);
85
1.04k
    Inserted = true;
86
1.04k
  }
87
1.18k
  if (!File || 
File->kind() == InputFile::ObjectKind617
)
88
1.13k
    Sym->IsUsedInRegularObj = true;
89
1.18k
  return {Sym, Inserted};
90
1.18k
}
91
92
static void reportTypeError(const Symbol *Existing, const InputFile *File,
93
1
                            llvm::wasm::WasmSymbolType Type) {
94
1
  error("symbol type mismatch: " + toString(*Existing) + "\n>>> defined as " +
95
1
        toString(Existing->getWasmType()) + " in " +
96
1
        toString(Existing->getFile()) + "\n>>> defined as " + toString(Type) +
97
1
        " in " + toString(File));
98
1
}
99
100
static void checkFunctionType(Symbol *Existing, const InputFile *File,
101
76
                              const WasmSignature *NewSig) {
102
76
  auto ExistingFunction = dyn_cast<FunctionSymbol>(Existing);
103
76
  if (!ExistingFunction) {
104
1
    reportTypeError(Existing, File, WASM_SYMBOL_TYPE_FUNCTION);
105
1
    return;
106
1
  }
107
75
108
75
  if (!NewSig)
109
0
    return;
110
75
111
75
  const WasmSignature *OldSig = ExistingFunction->FunctionType;
112
75
  if (!OldSig) {
113
36
    ExistingFunction->FunctionType = NewSig;
114
36
    return;
115
36
  }
116
39
117
39
  if (*NewSig != *OldSig)
118
7
    warn("function signature mismatch: " + Existing->getName() +
119
7
         "\n>>> defined as " + toString(*OldSig) + " in " +
120
7
         toString(Existing->getFile()) + "\n>>> defined as " +
121
7
         toString(*NewSig) + " in " + toString(File));
122
39
}
123
124
// Check the type of new symbol matches that of the symbol is replacing.
125
// For functions this can also involve verifying that the signatures match.
126
static void checkGlobalType(const Symbol *Existing, const InputFile *File,
127
3
                            const WasmGlobalType *NewType) {
128
3
  if (!isa<GlobalSymbol>(Existing)) {
129
0
    reportTypeError(Existing, File, WASM_SYMBOL_TYPE_GLOBAL);
130
0
    return;
131
0
  }
132
3
133
3
  const WasmGlobalType *OldType = cast<GlobalSymbol>(Existing)->getGlobalType();
134
3
  if (*NewType != *OldType) {
135
0
    error("Global type mismatch: " + Existing->getName() + "\n>>> defined as " +
136
0
          toString(*OldType) + " in " + toString(Existing->getFile()) +
137
0
          "\n>>> defined as " + toString(*NewType) + " in " + toString(File));
138
0
  }
139
3
}
140
141
9
static void checkDataType(const Symbol *Existing, const InputFile *File) {
142
9
  if (!isa<DataSymbol>(Existing))
143
0
    reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA);
144
9
}
145
146
DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name,
147
                                                   uint32_t Flags,
148
104
                                                   InputFunction *Function) {
149
104
  LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n");
150
104
  assert(!find(Name));
151
104
  SyntheticFunctions.emplace_back(Function);
152
104
  return replaceSymbol<DefinedFunction>(insert(Name, nullptr).first, Name,
153
104
                                        Flags, nullptr, Function);
154
104
}
155
156
DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name,
157
312
                                                 uint32_t Flags) {
158
312
  LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n");
159
312
  assert(!find(Name));
160
312
  return replaceSymbol<DefinedData>(insert(Name, nullptr).first, Name, Flags);
161
312
}
162
163
DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags,
164
104
                                               InputGlobal *Global) {
165
104
  LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global
166
104
                    << "\n");
167
104
  assert(!find(Name));
168
104
  SyntheticGlobals.emplace_back(Global);
169
104
  return replaceSymbol<DefinedGlobal>(insert(Name, nullptr).first, Name, Flags,
170
104
                                      nullptr, Global);
171
104
}
172
173
static bool shouldReplace(const Symbol *Existing, InputFile *NewFile,
174
83
                          uint32_t NewFlags) {
175
83
  // If existing symbol is undefined, replace it.
176
83
  if (!Existing->isDefined()) {
177
76
    LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
178
76
                      << Existing->getName() << "\n");
179
76
    return true;
180
76
  }
181
7
182
7
  // Now we have two defined symbols. If the new one is weak, we can ignore it.
183
7
  if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
184
5
    LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n");
185
5
    return false;
186
5
  }
187
2
188
2
  // If the existing symbol is weak, we should replace it.
189
2
  if (Existing->isWeak()) {
190
0
    LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");
191
0
    return true;
192
0
  }
193
2
194
2
  // Neither symbol is week. They conflict.
195
2
  error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " +
196
2
        toString(Existing->getFile()) + "\n>>> defined in " +
197
2
        toString(NewFile));
198
2
  return true;
199
2
}
200
201
Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags,
202
                                        InputFile *File,
203
446
                                        InputFunction *Function) {
204
446
  LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << " ["
205
446
                    << (Function ? toString(Function->Signature) : "none")
206
446
                    << "]\n");
207
446
  Symbol *S;
208
446
  bool WasInserted;
209
446
  std::tie(S, WasInserted) = insert(Name, File);
210
446
211
446
  if (WasInserted || 
S->isLazy()94
) {
212
369
    replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function);
213
369
    return S;
214
369
  }
215
77
216
77
  if (Function)
217
65
    checkFunctionType(S, File, &Function->Signature);
218
77
219
77
  if (shouldReplace(S, File, Flags)) {
220
73
    // If the new defined function doesn't have signture (i.e. bitcode
221
73
    // functions) but the old symbols does then preserve the old signature
222
73
    const WasmSignature *OldSig = nullptr;
223
73
    if (auto* F = dyn_cast<FunctionSymbol>(S))
224
72
      OldSig = F->FunctionType;
225
73
    auto NewSym = replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function);
226
73
    if (!NewSym->FunctionType)
227
11
      NewSym->FunctionType = OldSig;
228
73
  }
229
77
  return S;
230
77
}
231
232
Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags,
233
                                    InputFile *File, InputSegment *Segment,
234
59
                                    uint32_t Address, uint32_t Size) {
235
59
  LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address
236
59
                    << "\n");
237
59
  Symbol *S;
238
59
  bool WasInserted;
239
59
  std::tie(S, WasInserted) = insert(Name, File);
240
59
241
59
  if (WasInserted || 
S->isLazy()6
) {
242
53
    replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size);
243
53
    return S;
244
53
  }
245
6
246
6
  checkDataType(S, File);
247
6
248
6
  if (shouldReplace(S, File, Flags))
249
5
    replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size);
250
6
  return S;
251
6
}
252
253
Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags,
254
4
                                      InputFile *File, InputGlobal *Global) {
255
4
  LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n");
256
4
257
4
  Symbol *S;
258
4
  bool WasInserted;
259
4
  std::tie(S, WasInserted) = insert(Name, File);
260
4
261
4
  if (WasInserted || 
S->isLazy()0
) {
262
4
    replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global);
263
4
    return S;
264
4
  }
265
0
266
0
  checkGlobalType(S, File, &Global->getType());
267
0
268
0
  if (shouldReplace(S, File, Flags))
269
0
    replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global);
270
0
  return S;
271
0
}
272
273
Symbol *SymbolTable::addUndefinedFunction(StringRef Name, uint32_t Flags,
274
                                          InputFile *File,
275
85
                                          const WasmSignature *Sig) {
276
85
  LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name <<
277
85
             " [" << (Sig ? toString(*Sig) : "none") << "]\n");
278
85
279
85
  Symbol *S;
280
85
  bool WasInserted;
281
85
  std::tie(S, WasInserted) = insert(Name, File);
282
85
283
85
  if (WasInserted)
284
65
    replaceSymbol<UndefinedFunction>(S, Name, Flags, File, Sig);
285
20
  else if (auto *Lazy = dyn_cast<LazySymbol>(S))
286
9
    Lazy->fetch();
287
11
  else
288
11
    checkFunctionType(S, File, Sig);
289
85
290
85
  return S;
291
85
}
292
293
Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags,
294
12
                                      InputFile *File) {
295
12
  LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n");
296
12
297
12
  Symbol *S;
298
12
  bool WasInserted;
299
12
  std::tie(S, WasInserted) = insert(Name, File);
300
12
301
12
  if (WasInserted)
302
8
    replaceSymbol<UndefinedData>(S, Name, Flags, File);
303
4
  else if (auto *Lazy = dyn_cast<LazySymbol>(S))
304
0
    Lazy->fetch();
305
4
  else if (S->isDefined())
306
3
    checkDataType(S, File);
307
12
  return S;
308
12
}
309
310
Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, uint32_t Flags,
311
                                        InputFile *File,
312
11
                                        const WasmGlobalType *Type) {
313
11
  LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n");
314
11
315
11
  Symbol *S;
316
11
  bool WasInserted;
317
11
  std::tie(S, WasInserted) = insert(Name, File);
318
11
319
11
  if (WasInserted)
320
8
    replaceSymbol<UndefinedGlobal>(S, Name, Flags, File, Type);
321
3
  else if (auto *Lazy = dyn_cast<LazySymbol>(S))
322
0
    Lazy->fetch();
323
3
  else if (S->isDefined())
324
3
    checkGlobalType(S, File, Type);
325
11
  return S;
326
11
}
327
328
51
void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) {
329
51
  LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n");
330
51
  StringRef Name = Sym->getName();
331
51
332
51
  Symbol *S;
333
51
  bool WasInserted;
334
51
  std::tie(S, WasInserted) = insert(Name, nullptr);
335
51
336
51
  if (WasInserted) {
337
35
    replaceSymbol<LazySymbol>(S, Name, File, *Sym);
338
35
    return;
339
35
  }
340
16
341
16
  // If there is an existing undefined symbol, load a new one from the archive.
342
16
  if (S->isUndefined()) {
343
3
    LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
344
3
    File->addMember(Sym);
345
3
  }
346
16
}
347
348
3
bool SymbolTable::addComdat(StringRef Name) {
349
3
  return Comdats.insert(CachedHashStringRef(Name)).second;
350
3
}