Coverage Report

Created: 2018-08-19 21:11

/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
0
void SymbolTable::addFile(InputFile *File) {
29
0
  log("Processing: " + toString(File));
30
0
  File->parse();
31
0
32
0
  // LLVM bitcode file
33
0
  if (auto *F = dyn_cast<BitcodeFile>(File))
34
0
    BitcodeFiles.push_back(F);
35
0
  else if (auto *F = dyn_cast<ObjFile>(File))
36
0
    ObjectFiles.push_back(F);
37
0
}
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
0
void SymbolTable::addCombinedLTOObject() {
47
0
  if (BitcodeFiles.empty())
48
0
    return;
49
0
50
0
  // Compile bitcode files and replace bitcode symbols.
51
0
  LTO.reset(new BitcodeCompiler);
52
0
  for (BitcodeFile *F : BitcodeFiles)
53
0
    LTO->add(*F);
54
0
55
0
  for (StringRef Filename : LTO->compile()) {
56
0
    auto *Obj = make<ObjFile>(MemoryBufferRef(Filename, "lto.tmp"));
57
0
    Obj->parse();
58
0
    ObjectFiles.push_back(Obj);
59
0
  }
60
0
}
61
62
0
void SymbolTable::reportRemainingUndefines() {
63
0
  for (Symbol *Sym : SymVector) {
64
0
    if (!Sym->isUndefined() || Sym->isWeak())
65
0
      continue;
66
0
    if (Config->AllowUndefinedSymbols.count(Sym->getName()) != 0)
67
0
      continue;
68
0
    if (!Sym->IsUsedInRegularObj)
69
0
      continue;
70
0
    error(toString(Sym->getFile()) + ": undefined symbol: " + toString(*Sym));
71
0
  }
72
0
}
73
74
0
Symbol *SymbolTable::find(StringRef Name) {
75
0
  return SymMap.lookup(CachedHashStringRef(Name));
76
0
}
77
78
0
std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, InputFile *File) {
79
0
  bool Inserted = false;
80
0
  Symbol *&Sym = SymMap[CachedHashStringRef(Name)];
81
0
  if (!Sym) {
82
0
    Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
83
0
    Sym->IsUsedInRegularObj = false;
84
0
    SymVector.emplace_back(Sym);
85
0
    Inserted = true;
86
0
  }
87
0
  if (!File || File->kind() == InputFile::ObjectKind)
88
0
    Sym->IsUsedInRegularObj = true;
89
0
  return {Sym, Inserted};
90
0
}
91
92
static void reportTypeError(const Symbol *Existing, const InputFile *File,
93
0
                            llvm::wasm::WasmSymbolType Type) {
94
0
  error("symbol type mismatch: " + toString(*Existing) + "\n>>> defined as " +
95
0
        toString(Existing->getWasmType()) + " in " +
96
0
        toString(Existing->getFile()) + "\n>>> defined as " + toString(Type) +
97
0
        " in " + toString(File));
98
0
}
99
100
static void checkFunctionType(Symbol *Existing, const InputFile *File,
101
0
                              const WasmSignature *NewSig) {
102
0
  auto ExistingFunction = dyn_cast<FunctionSymbol>(Existing);
103
0
  if (!ExistingFunction) {
104
0
    reportTypeError(Existing, File, WASM_SYMBOL_TYPE_FUNCTION);
105
0
    return;
106
0
  }
107
0
108
0
  if (!NewSig)
109
0
    return;
110
0
111
0
  const WasmSignature *OldSig = ExistingFunction->FunctionType;
112
0
  if (!OldSig) {
113
0
    ExistingFunction->FunctionType = NewSig;
114
0
    return;
115
0
  }
116
0
117
0
  if (*NewSig != *OldSig)
118
0
    warn("function signature mismatch: " + Existing->getName() +
119
0
         "\n>>> defined as " + toString(*OldSig) + " in " +
120
0
         toString(Existing->getFile()) + "\n>>> defined as " +
121
0
         toString(*NewSig) + " in " + toString(File));
122
0
}
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
0
                            const WasmGlobalType *NewType) {
128
0
  if (!isa<GlobalSymbol>(Existing)) {
129
0
    reportTypeError(Existing, File, WASM_SYMBOL_TYPE_GLOBAL);
130
0
    return;
131
0
  }
132
0
133
0
  const WasmGlobalType *OldType = cast<GlobalSymbol>(Existing)->getGlobalType();
134
0
  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
0
}
140
141
0
static void checkDataType(const Symbol *Existing, const InputFile *File) {
142
0
  if (!isa<DataSymbol>(Existing))
143
0
    reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA);
144
0
}
145
146
DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name,
147
                                                   uint32_t Flags,
148
0
                                                   InputFunction *Function) {
149
0
  LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n");
150
0
  assert(!find(Name));
151
0
  SyntheticFunctions.emplace_back(Function);
152
0
  return replaceSymbol<DefinedFunction>(insert(Name, nullptr).first, Name,
153
0
                                        Flags, nullptr, Function);
154
0
}
155
156
DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name,
157
0
                                                 uint32_t Flags) {
158
0
  LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n");
159
0
  assert(!find(Name));
160
0
  return replaceSymbol<DefinedData>(insert(Name, nullptr).first, Name, Flags);
161
0
}
162
163
DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags,
164
0
                                               InputGlobal *Global) {
165
0
  LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global
166
0
                    << "\n");
167
0
  assert(!find(Name));
168
0
  SyntheticGlobals.emplace_back(Global);
169
0
  return replaceSymbol<DefinedGlobal>(insert(Name, nullptr).first, Name, Flags,
170
0
                                      nullptr, Global);
171
0
}
172
173
static bool shouldReplace(const Symbol *Existing, InputFile *NewFile,
174
0
                          uint32_t NewFlags) {
175
0
  // If existing symbol is undefined, replace it.
176
0
  if (!Existing->isDefined()) {
177
0
    LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
178
0
                      << Existing->getName() << "\n");
179
0
    return true;
180
0
  }
181
0
182
0
  // Now we have two defined symbols. If the new one is weak, we can ignore it.
183
0
  if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
184
0
    LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n");
185
0
    return false;
186
0
  }
187
0
188
0
  // If the existing symbol is weak, we should replace it.
189
0
  if (Existing->isWeak()) {
190
0
    LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");
191
0
    return true;
192
0
  }
193
0
194
0
  // Neither symbol is week. They conflict.
195
0
  error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " +
196
0
        toString(Existing->getFile()) + "\n>>> defined in " +
197
0
        toString(NewFile));
198
0
  return true;
199
0
}
200
201
Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags,
202
                                        InputFile *File,
203
0
                                        InputFunction *Function) {
204
0
  LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << "\n");
205
0
  Symbol *S;
206
0
  bool WasInserted;
207
0
  std::tie(S, WasInserted) = insert(Name, File);
208
0
209
0
  if (WasInserted || S->isLazy()) {
210
0
    replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function);
211
0
    return S;
212
0
  }
213
0
214
0
  if (Function)
215
0
    checkFunctionType(S, File, &Function->Signature);
216
0
217
0
  if (shouldReplace(S, File, Flags))
218
0
    replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function);
219
0
  return S;
220
0
}
221
222
Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags,
223
                                    InputFile *File, InputSegment *Segment,
224
0
                                    uint32_t Address, uint32_t Size) {
225
0
  LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address
226
0
                    << "\n");
227
0
  Symbol *S;
228
0
  bool WasInserted;
229
0
  std::tie(S, WasInserted) = insert(Name, File);
230
0
231
0
  if (WasInserted || S->isLazy()) {
232
0
    replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size);
233
0
    return S;
234
0
  }
235
0
236
0
  checkDataType(S, File);
237
0
238
0
  if (shouldReplace(S, File, Flags))
239
0
    replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size);
240
0
  return S;
241
0
}
242
243
Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags,
244
0
                                      InputFile *File, InputGlobal *Global) {
245
0
  LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n");
246
0
247
0
  Symbol *S;
248
0
  bool WasInserted;
249
0
  std::tie(S, WasInserted) = insert(Name, File);
250
0
251
0
  if (WasInserted || S->isLazy()) {
252
0
    replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global);
253
0
    return S;
254
0
  }
255
0
256
0
  checkGlobalType(S, File, &Global->getType());
257
0
258
0
  if (shouldReplace(S, File, Flags))
259
0
    replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global);
260
0
  return S;
261
0
}
262
263
Symbol *SymbolTable::addUndefinedFunction(StringRef Name, uint32_t Flags,
264
                                          InputFile *File,
265
0
                                          const WasmSignature *Sig) {
266
0
  LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name << "\n");
267
0
268
0
  Symbol *S;
269
0
  bool WasInserted;
270
0
  std::tie(S, WasInserted) = insert(Name, File);
271
0
272
0
  if (WasInserted)
273
0
    replaceSymbol<UndefinedFunction>(S, Name, Flags, File, Sig);
274
0
  else if (auto *Lazy = dyn_cast<LazySymbol>(S))
275
0
    Lazy->fetch();
276
0
  else
277
0
    checkFunctionType(S, File, Sig);
278
0
279
0
  return S;
280
0
}
281
282
Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags,
283
0
                                      InputFile *File) {
284
0
  LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n");
285
0
286
0
  Symbol *S;
287
0
  bool WasInserted;
288
0
  std::tie(S, WasInserted) = insert(Name, File);
289
0
290
0
  if (WasInserted)
291
0
    replaceSymbol<UndefinedData>(S, Name, Flags, File);
292
0
  else if (auto *Lazy = dyn_cast<LazySymbol>(S))
293
0
    Lazy->fetch();
294
0
  else if (S->isDefined())
295
0
    checkDataType(S, File);
296
0
  return S;
297
0
}
298
299
Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, uint32_t Flags,
300
                                        InputFile *File,
301
0
                                        const WasmGlobalType *Type) {
302
0
  LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n");
303
0
304
0
  Symbol *S;
305
0
  bool WasInserted;
306
0
  std::tie(S, WasInserted) = insert(Name, File);
307
0
308
0
  if (WasInserted)
309
0
    replaceSymbol<UndefinedGlobal>(S, Name, Flags, File, Type);
310
0
  else if (auto *Lazy = dyn_cast<LazySymbol>(S))
311
0
    Lazy->fetch();
312
0
  else if (S->isDefined())
313
0
    checkGlobalType(S, File, Type);
314
0
  return S;
315
0
}
316
317
0
void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) {
318
0
  LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n");
319
0
  StringRef Name = Sym->getName();
320
0
321
0
  Symbol *S;
322
0
  bool WasInserted;
323
0
  std::tie(S, WasInserted) = insert(Name, nullptr);
324
0
325
0
  if (WasInserted) {
326
0
    replaceSymbol<LazySymbol>(S, Name, File, *Sym);
327
0
    return;
328
0
  }
329
0
330
0
  // If there is an existing undefined symbol, load a new one from the archive.
331
0
  if (S->isUndefined()) {
332
0
    LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
333
0
    File->addMember(Sym);
334
0
  }
335
0
}
336
337
0
bool SymbolTable::addComdat(StringRef Name) {
338
0
  return Comdats.insert(CachedHashStringRef(Name)).second;
339
0
}