Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/COFF/SymbolTable.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- SymbolTable.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
#include "SymbolTable.h"
10
#include "Config.h"
11
#include "Driver.h"
12
#include "LTO.h"
13
#include "PDB.h"
14
#include "Symbols.h"
15
#include "lld/Common/ErrorHandler.h"
16
#include "lld/Common/Memory.h"
17
#include "lld/Common/Timer.h"
18
#include "llvm/IR/LLVMContext.h"
19
#include "llvm/Object/WindowsMachineFlag.h"
20
#include "llvm/Support/Debug.h"
21
#include "llvm/Support/raw_ostream.h"
22
#include <utility>
23
24
using namespace llvm;
25
26
namespace lld {
27
namespace coff {
28
29
static Timer ltoTimer("LTO", Timer::root());
30
31
SymbolTable *symtab;
32
33
1.05k
void SymbolTable::addFile(InputFile *file) {
34
1.05k
  log("Reading " + toString(file));
35
1.05k
  file->parse();
36
1.05k
37
1.05k
  MachineTypes mt = file->getMachineType();
38
1.05k
  if (config->machine == IMAGE_FILE_MACHINE_UNKNOWN) {
39
628
    config->machine = mt;
40
628
  } else 
if (422
mt != IMAGE_FILE_MACHINE_UNKNOWN422
&&
config->machine != mt222
) {
41
2
    error(toString(file) + ": machine type " + machineToStr(mt) +
42
2
          " conflicts with " + machineToStr(config->machine));
43
2
    return;
44
2
  }
45
1.04k
46
1.04k
  if (auto *f = dyn_cast<ObjFile>(file)) {
47
765
    ObjFile::instances.push_back(f);
48
765
  } else 
if (auto *283
f283
= dyn_cast<BitcodeFile>(file)) {
49
77
    BitcodeFile::instances.push_back(f);
50
206
  } else if (auto *f = dyn_cast<ImportFile>(file)) {
51
109
    ImportFile::instances.push_back(f);
52
109
  }
53
1.04k
54
1.04k
  driver->parseDirectives(file);
55
1.04k
}
56
57
73
static void errorOrWarn(const Twine &s) {
58
73
  if (config->forceUnresolved)
59
20
    warn(s);
60
53
  else
61
53
    error(s);
62
73
}
63
64
// Returns the symbol in SC whose value is <= Addr that is closest to Addr.
65
// This is generally the global variable or function whose definition contains
66
// Addr.
67
59
static Symbol *getSymbol(SectionChunk *sc, uint32_t addr) {
68
59
  DefinedRegular *candidate = nullptr;
69
59
70
881
  for (Symbol *s : sc->file->getSymbols()) {
71
881
    auto *d = dyn_cast_or_null<DefinedRegular>(s);
72
881
    if (!d || 
!d->data423
||
d->getChunk() != sc421
||
d->getValue() > addr142
||
73
881
        
(127
candidate127
&&
d->getValue() < candidate->getValue()68
))
74
754
      continue;
75
127
76
127
    candidate = d;
77
127
  }
78
59
79
59
  return candidate;
80
59
}
81
82
// Given a file and the index of a symbol in that file, returns a description
83
// of all references to that symbol from that file. If no debug information is
84
// available, returns just the name of the file, else one string per actual
85
// reference as described in the debug info.
86
41
std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex) {
87
41
  struct Location {
88
41
    Symbol *sym;
89
41
    std::pair<StringRef, uint32_t> fileLine;
90
41
  };
91
41
  std::vector<Location> locations;
92
41
93
151
  for (Chunk *c : file->getChunks()) {
94
151
    auto *sc = dyn_cast<SectionChunk>(c);
95
151
    if (!sc)
96
0
      continue;
97
180
    
for (const coff_relocation &r : sc->getRelocs())151
{
98
180
      if (r.SymbolTableIndex != symIndex)
99
121
        continue;
100
59
      std::pair<StringRef, uint32_t> fileLine =
101
59
          getFileLine(sc, r.VirtualAddress);
102
59
      Symbol *sym = getSymbol(sc, r.VirtualAddress);
103
59
      if (!fileLine.first.empty() || 
sym48
)
104
59
        locations.push_back({sym, fileLine});
105
59
    }
106
151
  }
107
41
108
41
  if (locations.empty())
109
2
    return std::vector<std::string>({"\n>>> referenced by " + toString(file)});
110
39
111
39
  std::vector<std::string> symbolLocations(locations.size());
112
39
  size_t i = 0;
113
59
  for (Location loc : locations) {
114
59
    llvm::raw_string_ostream os(symbolLocations[i++]);
115
59
    os << "\n>>> referenced by ";
116
59
    if (!loc.fileLine.first.empty())
117
11
      os << loc.fileLine.first << ":" << loc.fileLine.second
118
11
         << "\n>>>               ";
119
59
    os << toString(file);
120
59
    if (loc.sym)
121
59
      os << ":(" << toString(*loc.sym) << ')';
122
59
  }
123
39
  return symbolLocations;
124
39
}
125
126
// For an undefined symbol, stores all files referencing it and the index of
127
// the undefined symbol in each file.
128
struct UndefinedDiag {
129
  Symbol *sym;
130
  struct File {
131
    ObjFile *oFile;
132
    uint64_t symIndex;
133
  };
134
  std::vector<File> files;
135
};
136
137
37
static void reportUndefinedSymbol(const UndefinedDiag &undefDiag) {
138
37
  std::string out;
139
37
  llvm::raw_string_ostream os(out);
140
37
  os << "undefined symbol: " << toString(*undefDiag.sym);
141
37
142
37
  const size_t maxUndefReferences = 10;
143
37
  size_t i = 0, numRefs = 0;
144
38
  for (const UndefinedDiag::File &ref : undefDiag.files) {
145
38
    std::vector<std::string> symbolLocations =
146
38
        getSymbolLocations(ref.oFile, ref.symIndex);
147
38
    numRefs += symbolLocations.size();
148
57
    for (const std::string &s : symbolLocations) {
149
57
      if (i >= maxUndefReferences)
150
1
        break;
151
56
      os << s;
152
56
      i++;
153
56
    }
154
38
  }
155
37
  if (i < numRefs)
156
1
    os << "\n>>> referenced " << numRefs - i << " more times";
157
37
  errorOrWarn(os.str());
158
37
}
159
160
47
void SymbolTable::loadMinGWAutomaticImports() {
161
835
  for (auto &i : symMap) {
162
835
    Symbol *sym = i.second;
163
835
    auto *undef = dyn_cast<Undefined>(sym);
164
835
    if (!undef)
165
825
      continue;
166
10
    if (!sym->isUsedInRegularObj)
167
0
      continue;
168
10
169
10
    StringRef name = undef->getName();
170
10
171
10
    if (name.startswith("__imp_"))
172
0
      continue;
173
10
    // If we have an undefined symbol, but we have a Lazy representing a
174
10
    // symbol we could load from file, make sure to load that.
175
10
    Lazy *l = dyn_cast_or_null<Lazy>(find(("__imp_" + name).str()));
176
10
    if (!l || 
l->pendingArchiveLoad7
)
177
3
      continue;
178
7
179
7
    log("Loading lazy " + l->getName() + " from " + l->file->getName() +
180
7
        " for automatic import");
181
7
    l->pendingArchiveLoad = true;
182
7
    l->file->addMember(l->sym);
183
7
  }
184
47
}
185
186
8
bool SymbolTable::handleMinGWAutomaticImport(Symbol *sym, StringRef name) {
187
8
  if (name.startswith("__imp_"))
188
0
    return false;
189
8
  Defined *imp = dyn_cast_or_null<Defined>(find(("__imp_" + name).str()));
190
8
  if (!imp)
191
1
    return false;
192
7
193
7
  // Replace the reference directly to a variable with a reference
194
7
  // to the import address table instead. This obviously isn't right,
195
7
  // but we mark the symbol as isRuntimePseudoReloc, and a later pass
196
7
  // will add runtime pseudo relocations for every relocation against
197
7
  // this Symbol. The runtime pseudo relocation framework expects the
198
7
  // reference itself to point at the IAT entry.
199
7
  size_t impSize = 0;
200
7
  if (isa<DefinedImportData>(imp)) {
201
6
    log("Automatically importing " + name + " from " +
202
6
        cast<DefinedImportData>(imp)->getDLLName());
203
6
    impSize = sizeof(DefinedImportData);
204
6
  } else 
if (1
isa<DefinedRegular>(imp)1
) {
205
1
    log("Automatically importing " + name + " from " +
206
1
        toString(cast<DefinedRegular>(imp)->file));
207
1
    impSize = sizeof(DefinedRegular);
208
1
  } else {
209
0
    warn("unable to automatically import " + name + " from " + imp->getName() +
210
0
         " from " + toString(cast<DefinedRegular>(imp)->file) +
211
0
         "; unexpected symbol type");
212
0
    return false;
213
0
  }
214
7
  sym->replaceKeepingName(imp, impSize);
215
7
  sym->isRuntimePseudoReloc = true;
216
7
217
7
  // There may exist symbols named .refptr.<name> which only consist
218
7
  // of a single pointer to <name>. If it turns out <name> is
219
7
  // automatically imported, we don't need to keep the .refptr.<name>
220
7
  // pointer at all, but redirect all accesses to it to the IAT entry
221
7
  // for __imp_<name> instead, and drop the whole .refptr.<name> chunk.
222
7
  DefinedRegular *refptr =
223
7
      dyn_cast_or_null<DefinedRegular>(find((".refptr." + name).str()));
224
7
  if (refptr && 
refptr->getChunk()->getSize() == config->wordsize1
) {
225
1
    SectionChunk *sc = dyn_cast_or_null<SectionChunk>(refptr->getChunk());
226
1
    if (sc && sc->getRelocs().size() == 1 && *sc->symbols().begin() == sym) {
227
1
      log("Replacing .refptr." + name + " with " + imp->getName());
228
1
      refptr->getChunk()->live = false;
229
1
      refptr->replaceKeepingName(imp, impSize);
230
1
    }
231
1
  }
232
7
  return true;
233
7
}
234
235
607
void SymbolTable::reportRemainingUndefines() {
236
607
  SmallPtrSet<Symbol *, 8> undefs;
237
607
  DenseMap<Symbol *, Symbol *> localImports;
238
607
239
7.82k
  for (auto &i : symMap) {
240
7.82k
    Symbol *sym = i.second;
241
7.82k
    auto *undef = dyn_cast<Undefined>(sym);
242
7.82k
    if (!undef)
243
7.66k
      continue;
244
159
    if (!sym->isUsedInRegularObj)
245
29
      continue;
246
130
247
130
    StringRef name = undef->getName();
248
130
249
130
    // A weak alias may have been resolved, so check for that.
250
130
    if (Defined *d = undef->getWeakAlias()) {
251
36
      // We want to replace Sym with D. However, we can't just blindly
252
36
      // copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
253
36
      // internal symbol, and internal symbols are stored as "unparented"
254
36
      // Symbols. For that reason we need to check which type of symbol we
255
36
      // are dealing with and copy the correct number of bytes.
256
36
      if (isa<DefinedRegular>(d))
257
34
        memcpy(sym, d, sizeof(DefinedRegular));
258
2
      else if (isa<DefinedAbsolute>(d))
259
0
        memcpy(sym, d, sizeof(DefinedAbsolute));
260
2
      else
261
2
        memcpy(sym, d, sizeof(SymbolUnion));
262
36
      continue;
263
36
    }
264
94
265
94
    // If we can resolve a symbol by removing __imp_ prefix, do that.
266
94
    // This odd rule is for compatibility with MSVC linker.
267
94
    if (name.startswith("__imp_")) {
268
12
      Symbol *imp = find(name.substr(strlen("__imp_")));
269
12
      if (imp && 
isa<Defined>(imp)9
) {
270
9
        auto *d = cast<Defined>(imp);
271
9
        replaceSymbol<DefinedLocalImport>(sym, name, d);
272
9
        localImportChunks.push_back(cast<DefinedLocalImport>(sym)->getChunk());
273
9
        localImports[sym] = d;
274
9
        continue;
275
9
      }
276
85
    }
277
85
278
85
    // We don't want to report missing Microsoft precompiled headers symbols.
279
85
    // A proper message will be emitted instead in PDBLinker::aquirePrecompObj
280
85
    if (name.contains("_PchSym_"))
281
5
      continue;
282
80
283
80
    if (config->mingw && 
handleMinGWAutomaticImport(sym, name)8
)
284
7
      continue;
285
73
286
73
    // Remaining undefined symbols are not fatal if /force is specified.
287
73
    // They are replaced with dummy defined symbols.
288
73
    if (config->forceUnresolved)
289
20
      replaceSymbol<DefinedAbsolute>(sym, name, 0);
290
73
    undefs.insert(sym);
291
73
  }
292
607
293
607
  if (undefs.empty() && 
localImports.empty()564
)
294
555
    return;
295
52
296
61
  
for (Symbol *b : config->gcroot)52
{
297
61
    if (undefs.count(b))
298
36
      errorOrWarn("<root>: undefined symbol: " + toString(*b));
299
61
    if (config->warnLocallyDefinedImported)
300
59
      if (Symbol *imp = localImports.lookup(b))
301
0
        warn("<root>: locally defined symbol imported: " + toString(*imp) +
302
0
             " (defined in " + toString(imp->getFile()) + ") [LNK4217]");
303
61
  }
304
52
305
52
  std::vector<UndefinedDiag> undefDiags;
306
52
  DenseMap<Symbol *, int> firstDiag;
307
52
308
64
  for (ObjFile *file : ObjFile::instances) {
309
64
    size_t symIndex = (size_t)-1;
310
647
    for (Symbol *sym : file->getSymbols()) {
311
647
      ++symIndex;
312
647
      if (!sym)
313
289
        continue;
314
358
      if (undefs.count(sym)) {
315
38
        auto it = firstDiag.find(sym);
316
38
        if (it == firstDiag.end()) {
317
37
          firstDiag[sym] = undefDiags.size();
318
37
          undefDiags.push_back({sym, {{file, symIndex}}});
319
37
        } else {
320
1
          undefDiags[it->second].files.push_back({file, symIndex});
321
1
        }
322
38
      }
323
358
      if (config->warnLocallyDefinedImported)
324
347
        if (Symbol *imp = localImports.lookup(sym))
325
8
          warn(toString(file) +
326
8
               ": locally defined symbol imported: " + toString(*imp) +
327
8
               " (defined in " + toString(imp->getFile()) + ") [LNK4217]");
328
358
    }
329
64
  }
330
52
331
52
  for (const UndefinedDiag& undefDiag : undefDiags)
332
37
    reportUndefinedSymbol(undefDiag);
333
52
}
334
335
9.53k
std::pair<Symbol *, bool> SymbolTable::insert(StringRef name) {
336
9.53k
  bool inserted = false;
337
9.53k
  Symbol *&sym = symMap[CachedHashStringRef(name)];
338
9.53k
  if (!sym) {
339
7.98k
    sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
340
7.98k
    sym->isUsedInRegularObj = false;
341
7.98k
    sym->pendingArchiveLoad = false;
342
7.98k
    inserted = true;
343
7.98k
  }
344
9.53k
  return {sym, inserted};
345
9.53k
}
346
347
9.02k
std::pair<Symbol *, bool> SymbolTable::insert(StringRef name, InputFile *file) {
348
9.02k
  std::pair<Symbol *, bool> result = insert(name);
349
9.02k
  if (!file || 
!isa<BitcodeFile>(file)1.95k
)
350
8.87k
    result.first->isUsedInRegularObj = true;
351
9.02k
  return result;
352
9.02k
}
353
354
Symbol *SymbolTable::addUndefined(StringRef name, InputFile *f,
355
1.28k
                                  bool isWeakAlias) {
356
1.28k
  Symbol *s;
357
1.28k
  bool wasInserted;
358
1.28k
  std::tie(s, wasInserted) = insert(name, f);
359
1.28k
  if (wasInserted || 
(816
isa<Lazy>(s)816
&&
isWeakAlias31
)) {
360
467
    replaceSymbol<Undefined>(s, name);
361
467
    return s;
362
467
  }
363
816
  if (auto *l = dyn_cast<Lazy>(s)) {
364
31
    if (!s->pendingArchiveLoad) {
365
31
      s->pendingArchiveLoad = true;
366
31
      l->file->addMember(l->sym);
367
31
    }
368
31
  }
369
816
  return s;
370
816
}
371
372
508
void SymbolTable::addLazy(ArchiveFile *f, const Archive::Symbol &sym) {
373
508
  StringRef name = sym.getName();
374
508
  Symbol *s;
375
508
  bool wasInserted;
376
508
  std::tie(s, wasInserted) = insert(name);
377
508
  if (wasInserted) {
378
373
    replaceSymbol<Lazy>(s, f, sym);
379
373
    return;
380
373
  }
381
135
  auto *u = dyn_cast<Undefined>(s);
382
135
  if (!u || 
u->weakAlias119
||
s->pendingArchiveLoad119
)
383
16
    return;
384
119
  s->pendingArchiveLoad = true;
385
119
  f->addMember(sym);
386
119
}
387
388
20
void SymbolTable::reportDuplicate(Symbol *existing, InputFile *newFile) {
389
20
  std::string msg = "duplicate symbol: " + toString(*existing) + " in " +
390
20
                    toString(existing->getFile()) + " and in " +
391
20
                    toString(newFile);
392
20
393
20
  if (config->forceMultiple)
394
3
    warn(msg);
395
17
  else
396
17
    error(msg);
397
20
}
398
399
8
Symbol *SymbolTable::addAbsolute(StringRef n, COFFSymbolRef sym) {
400
8
  Symbol *s;
401
8
  bool wasInserted;
402
8
  std::tie(s, wasInserted) = insert(n, nullptr);
403
8
  s->isUsedInRegularObj = true;
404
8
  if (wasInserted || 
isa<Undefined>(s)0
||
isa<Lazy>(s)0
)
405
8
    replaceSymbol<DefinedAbsolute>(s, n, sym);
406
0
  else if (!isa<DefinedCOFF>(s))
407
0
    reportDuplicate(s, nullptr);
408
8
  return s;
409
8
}
410
411
5.33k
Symbol *SymbolTable::addAbsolute(StringRef n, uint64_t va) {
412
5.33k
  Symbol *s;
413
5.33k
  bool wasInserted;
414
5.33k
  std::tie(s, wasInserted) = insert(n, nullptr);
415
5.33k
  s->isUsedInRegularObj = true;
416
5.33k
  if (wasInserted || 
isa<Undefined>(s)85
||
isa<Lazy>(s)0
)
417
5.33k
    replaceSymbol<DefinedAbsolute>(s, n, va);
418
0
  else if (!isa<DefinedCOFF>(s))
419
0
    reportDuplicate(s, nullptr);
420
5.33k
  return s;
421
5.33k
}
422
423
618
Symbol *SymbolTable::addSynthetic(StringRef n, Chunk *c) {
424
618
  Symbol *s;
425
618
  bool wasInserted;
426
618
  std::tie(s, wasInserted) = insert(n, nullptr);
427
618
  s->isUsedInRegularObj = true;
428
618
  if (wasInserted || 
isa<Undefined>(s)29
||
isa<Lazy>(s)0
)
429
618
    replaceSymbol<DefinedSynthetic>(s, n, c);
430
0
  else if (!isa<DefinedCOFF>(s))
431
0
    reportDuplicate(s, nullptr);
432
618
  return s;
433
618
}
434
435
Symbol *SymbolTable::addRegular(InputFile *f, StringRef n,
436
                                const coff_symbol_generic *sym,
437
1.16k
                                SectionChunk *c) {
438
1.16k
  Symbol *s;
439
1.16k
  bool wasInserted;
440
1.16k
  std::tie(s, wasInserted) = insert(n, f);
441
1.16k
  if (wasInserted || 
!isa<DefinedRegular>(s)124
)
442
1.15k
    replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ false,
443
1.15k
                                  /*IsExternal*/ true, sym, c);
444
8
  else
445
8
    reportDuplicate(s, f);
446
1.16k
  return s;
447
1.16k
}
448
449
std::pair<DefinedRegular *, bool>
450
SymbolTable::addComdat(InputFile *f, StringRef n,
451
394
                       const coff_symbol_generic *sym) {
452
394
  Symbol *s;
453
394
  bool wasInserted;
454
394
  std::tie(s, wasInserted) = insert(n, f);
455
394
  if (wasInserted || 
!isa<DefinedRegular>(s)150
) {
456
345
    replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ true,
457
345
                                  /*IsExternal*/ true, sym, nullptr);
458
345
    return {cast<DefinedRegular>(s), true};
459
345
  }
460
49
  auto *existingSymbol = cast<DefinedRegular>(s);
461
49
  if (!existingSymbol->isCOMDAT)
462
0
    reportDuplicate(s, f);
463
49
  return {existingSymbol, false};
464
49
}
465
466
Symbol *SymbolTable::addCommon(InputFile *f, StringRef n, uint64_t size,
467
14
                               const coff_symbol_generic *sym, CommonChunk *c) {
468
14
  Symbol *s;
469
14
  bool wasInserted;
470
14
  std::tie(s, wasInserted) = insert(n, f);
471
14
  if (wasInserted || 
!isa<DefinedCOFF>(s)0
)
472
14
    replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
473
0
  else if (auto *dc = dyn_cast<DefinedCommon>(s))
474
0
    if (size > dc->getSize())
475
0
      replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
476
14
  return s;
477
14
}
478
479
110
Symbol *SymbolTable::addImportData(StringRef n, ImportFile *f) {
480
110
  Symbol *s;
481
110
  bool wasInserted;
482
110
  std::tie(s, wasInserted) = insert(n, nullptr);
483
110
  s->isUsedInRegularObj = true;
484
110
  if (wasInserted || isa<Undefined>(s) || 
isa<Lazy>(s)99
) {
485
108
    replaceSymbol<DefinedImportData>(s, n, f);
486
108
    return s;
487
108
  }
488
2
489
2
  reportDuplicate(s, f);
490
2
  return nullptr;
491
2
}
492
493
Symbol *SymbolTable::addImportThunk(StringRef name, DefinedImportData *id,
494
99
                                    uint16_t machine) {
495
99
  Symbol *s;
496
99
  bool wasInserted;
497
99
  std::tie(s, wasInserted) = insert(name, nullptr);
498
99
  s->isUsedInRegularObj = true;
499
99
  if (wasInserted || isa<Undefined>(s) || 
isa<Lazy>(s)20
) {
500
98
    replaceSymbol<DefinedImportThunk>(s, name, id, machine);
501
98
    return s;
502
98
  }
503
1
504
1
  reportDuplicate(s, id->file);
505
1
  return nullptr;
506
1
}
507
508
1.50k
std::vector<Chunk *> SymbolTable::getChunks() {
509
1.50k
  std::vector<Chunk *> res;
510
1.98k
  for (ObjFile *file : ObjFile::instances) {
511
1.98k
    ArrayRef<Chunk *> v = file->getChunks();
512
1.98k
    res.insert(res.end(), v.begin(), v.end());
513
1.98k
  }
514
1.50k
  return res;
515
1.50k
}
516
517
3.31k
Symbol *SymbolTable::find(StringRef name) {
518
3.31k
  return symMap.lookup(CachedHashStringRef(name));
519
3.31k
}
520
521
2.02k
Symbol *SymbolTable::findUnderscore(StringRef name) {
522
2.02k
  if (config->machine == I386)
523
369
    return find(("_" + name).str());
524
1.65k
  return find(name);
525
1.65k
}
526
527
// Return all symbols that start with Prefix, possibly ignoring the first
528
// character of Prefix or the first character symbol.
529
931
std::vector<Symbol *> SymbolTable::getSymsWithPrefix(StringRef prefix) {
530
931
  std::vector<Symbol *> syms;
531
3.44k
  for (auto pair : symMap) {
532
3.44k
    StringRef name = pair.first.val();
533
3.44k
    if (name.startswith(prefix) || 
name.startswith(prefix.drop_front())3.32k
||
534
3.44k
        
name.drop_front().startswith(prefix)3.01k
||
535
3.44k
        
name.drop_front().startswith(prefix.drop_front())2.97k
) {
536
474
      syms.push_back(pair.second);
537
474
    }
538
3.44k
  }
539
931
  return syms;
540
931
}
541
542
1.21k
Symbol *SymbolTable::findMangle(StringRef name) {
543
1.21k
  if (Symbol *sym = find(name))
544
347
    if (!isa<Undefined>(sym))
545
281
      return sym;
546
931
547
931
  // Efficient fuzzy string lookup is impossible with a hash table, so iterate
548
931
  // the symbol table once and collect all possibly matching symbols into this
549
931
  // vector. Then compare each possibly matching symbol with each possible
550
931
  // mangling.
551
931
  std::vector<Symbol *> syms = getSymsWithPrefix(name);
552
1.36k
  auto findByPrefix = [&syms](const Twine &t) -> Symbol * {
553
1.36k
    std::string prefix = t.str();
554
1.36k
    for (auto *s : syms)
555
548
      if (s->getName().startswith(prefix))
556
30
        return s;
557
1.36k
    
return nullptr1.33k
;
558
1.36k
  };
559
931
560
931
  // For non-x86, just look for C++ functions.
561
931
  if (config->machine != I386)
562
768
    return findByPrefix("?" + name + "@@Y");
563
163
564
163
  if (!name.startswith("_"))
565
0
    return nullptr;
566
163
  // Search for x86 stdcall function.
567
163
  if (Symbol *s = findByPrefix(name + "@"))
568
16
    return s;
569
147
  // Search for x86 fastcall function.
570
147
  if (Symbol *s = findByPrefix("@" + name.substr(1) + "@"))
571
2
    return s;
572
145
  // Search for x86 vectorcall function.
573
145
  if (Symbol *s = findByPrefix(name.substr(1) + "@@"))
574
1
    return s;
575
144
  // Search for x86 C++ non-member function.
576
144
  return findByPrefix("?" + name.substr(1) + "@@Y");
577
144
}
578
579
895
Symbol *SymbolTable::addUndefined(StringRef name) {
580
895
  return addUndefined(name, nullptr, false);
581
895
}
582
583
52
std::vector<StringRef> SymbolTable::compileBitcodeFiles() {
584
52
  lto.reset(new BitcodeCompiler);
585
52
  for (BitcodeFile *f : BitcodeFile::instances)
586
74
    lto->add(*f);
587
52
  return lto->compile();
588
52
}
589
590
614
void SymbolTable::addCombinedLTOObjects() {
591
614
  if (BitcodeFile::instances.empty())
592
562
    return;
593
52
594
52
  ScopedTimer t(ltoTimer);
595
61
  for (StringRef object : compileBitcodeFiles()) {
596
61
    auto *obj = make<ObjFile>(MemoryBufferRef(object, "lto.tmp"));
597
61
    obj->parse();
598
61
    ObjFile::instances.push_back(obj);
599
61
  }
600
52
}
601
602
} // namespace coff
603
} // namespace lld