Coverage Report

Created: 2019-01-21 03:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/COFF/Symbols.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Symbols.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 "Symbols.h"
10
#include "InputFiles.h"
11
#include "lld/Common/ErrorHandler.h"
12
#include "lld/Common/Memory.h"
13
#include "lld/Common/Strings.h"
14
#include "llvm/ADT/STLExtras.h"
15
#include "llvm/Support/Debug.h"
16
#include "llvm/Support/raw_ostream.h"
17
18
using namespace llvm;
19
using namespace llvm::object;
20
21
// Returns a symbol name for an error message.
22
111
std::string lld::toString(coff::Symbol &B) {
23
111
  if (Optional<std::string> S = lld::demangleMSVC(B.getName()))
24
6
    return ("\"" + *S + "\" (" + B.getName() + ")").str();
25
105
  return B.getName();
26
105
}
27
28
namespace lld {
29
namespace coff {
30
31
2.03k
StringRef Symbol::getName() {
32
2.03k
  // COFF symbol names are read lazily for a performance reason.
33
2.03k
  // Non-external symbol names are never used by the linker except for logging
34
2.03k
  // or debugging. Their internal references are resolved not by name but by
35
2.03k
  // symbol index. And because they are not external, no one can refer them by
36
2.03k
  // name. Object files contain lots of non-external symbols, and creating
37
2.03k
  // StringRefs for them (which involves lots of strlen() on the string table)
38
2.03k
  // is a waste of time.
39
2.03k
  if (Name.empty()) {
40
267
    auto *D = cast<DefinedCOFF>(this);
41
267
    cast<ObjFile>(D->File)->getCOFFObj()->getSymbolName(D->Sym, Name);
42
267
  }
43
2.03k
  return Name;
44
2.03k
}
45
46
186
InputFile *Symbol::getFile() {
47
186
  if (auto *Sym = dyn_cast<DefinedCOFF>(this))
48
181
    return Sym->File;
49
5
  if (auto *Sym = dyn_cast<Lazy>(this))
50
0
    return Sym->File;
51
5
  return nullptr;
52
5
}
53
54
1.44k
bool Symbol::isLive() const {
55
1.44k
  if (auto *R = dyn_cast<DefinedRegular>(this))
56
289
    return R->getChunk()->Live;
57
1.15k
  if (auto *Imp = dyn_cast<DefinedImportData>(this))
58
14
    return Imp->File->Live;
59
1.13k
  if (auto *Imp = dyn_cast<DefinedImportThunk>(this))
60
14
    return Imp->WrappedSym->File->ThunkLive;
61
1.12k
  // Assume any other kind of symbol is live.
62
1.12k
  return true;
63
1.12k
}
64
65
// MinGW specific.
66
8
void Symbol::replaceKeepingName(Symbol *Other, size_t Size) {
67
8
  StringRef OrigName = Name;
68
8
  memcpy(this, Other, Size);
69
8
  Name = OrigName;
70
8
}
71
72
619
COFFSymbolRef DefinedCOFF::getCOFFSymbol() {
73
619
  size_t SymSize = cast<ObjFile>(File)->getCOFFObj()->getSymbolTableEntrySize();
74
619
  if (SymSize == sizeof(coff_symbol16))
75
619
    return COFFSymbolRef(reinterpret_cast<const coff_symbol16 *>(Sym));
76
0
  assert(SymSize == sizeof(coff_symbol32));
77
0
  return COFFSymbolRef(reinterpret_cast<const coff_symbol32 *>(Sym));
78
0
}
79
80
uint16_t DefinedAbsolute::NumOutputSections;
81
82
88
static Chunk *makeImportThunk(DefinedImportData *S, uint16_t Machine) {
83
88
  if (Machine == AMD64)
84
64
    return make<ImportThunkChunkX64>(S);
85
24
  if (Machine == I386)
86
18
    return make<ImportThunkChunkX86>(S);
87
6
  if (Machine == ARM64)
88
4
    return make<ImportThunkChunkARM64>(S);
89
2
  assert(Machine == ARMNT);
90
2
  return make<ImportThunkChunkARM>(S);
91
2
}
92
93
DefinedImportThunk::DefinedImportThunk(StringRef Name, DefinedImportData *S,
94
                                       uint16_t Machine)
95
    : Defined(DefinedImportThunkKind, Name), WrappedSym(S),
96
88
      Data(makeImportThunk(S, Machine)) {}
97
98
89
Defined *Undefined::getWeakAlias() {
99
89
  // A weak alias may be a weak alias to another symbol, so check recursively.
100
89
  for (Symbol *A = WeakAlias; A; 
A = cast<Undefined>(A)->WeakAlias0
)
101
33
    if (auto *D = dyn_cast<Defined>(A))
102
33
      return D;
103
89
  
return nullptr56
;
104
89
}
105
} // namespace coff
106
} // namespace lld