Coverage Report

Created: 2019-05-22 02:55

/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
using namespace lld::coff;
22
23
static_assert(sizeof(SymbolUnion) <= 48,
24
              "symbols should be optimized for memory usage");
25
26
// Returns a symbol name for an error message.
27
82
std::string lld::toString(coff::Symbol &B) {
28
82
  if (Config->Demangle)
29
71
    if (Optional<std::string> S = lld::demangleMSVC(B.getName()))
30
2
      return *S;
31
80
  return B.getName();
32
80
}
33
34
namespace lld {
35
namespace coff {
36
37
1.14k
StringRef Symbol::getName() {
38
1.14k
  // COFF symbol names are read lazily for a performance reason.
39
1.14k
  // Non-external symbol names are never used by the linker except for logging
40
1.14k
  // or debugging. Their internal references are resolved not by name but by
41
1.14k
  // symbol index. And because they are not external, no one can refer them by
42
1.14k
  // name. Object files contain lots of non-external symbols, and creating
43
1.14k
  // StringRefs for them (which involves lots of strlen() on the string table)
44
1.14k
  // is a waste of time.
45
1.14k
  if (NameData == nullptr) {
46
226
    auto *D = cast<DefinedCOFF>(this);
47
226
    StringRef NameStr;
48
226
    cast<ObjFile>(D->File)->getCOFFObj()->getSymbolName(D->Sym, NameStr);
49
226
    NameData = NameStr.data();
50
226
    NameSize = NameStr.size();
51
226
    assert(NameSize == NameStr.size() && "name length truncated");
52
226
  }
53
1.14k
  return StringRef(NameData, NameSize);
54
1.14k
}
55
56
106
InputFile *Symbol::getFile() {
57
106
  if (auto *Sym = dyn_cast<DefinedCOFF>(this))
58
103
    return Sym->File;
59
3
  if (auto *Sym = dyn_cast<Lazy>(this))
60
0
    return Sym->File;
61
3
  return nullptr;
62
3
}
63
64
699
bool Symbol::isLive() const {
65
699
  if (auto *R = dyn_cast<DefinedRegular>(this))
66
136
    return R->getChunk()->Live;
67
563
  if (auto *Imp = dyn_cast<DefinedImportData>(this))
68
8
    return Imp->File->Live;
69
555
  if (auto *Imp = dyn_cast<DefinedImportThunk>(this))
70
9
    return Imp->WrappedSym->File->ThunkLive;
71
546
  // Assume any other kind of symbol is live.
72
546
  return true;
73
546
}
74
75
// MinGW specific.
76
5
void Symbol::replaceKeepingName(Symbol *Other, size_t Size) {
77
5
  StringRef OrigName = getName();
78
5
  memcpy(this, Other, Size);
79
5
  NameData = OrigName.data();
80
5
  NameSize = OrigName.size();
81
5
}
82
83
410
COFFSymbolRef DefinedCOFF::getCOFFSymbol() {
84
410
  size_t SymSize = cast<ObjFile>(File)->getCOFFObj()->getSymbolTableEntrySize();
85
410
  if (SymSize == sizeof(coff_symbol16))
86
410
    return COFFSymbolRef(reinterpret_cast<const coff_symbol16 *>(Sym));
87
0
  assert(SymSize == sizeof(coff_symbol32));
88
0
  return COFFSymbolRef(reinterpret_cast<const coff_symbol32 *>(Sym));
89
0
}
90
91
uint16_t DefinedAbsolute::NumOutputSections;
92
93
51
static Chunk *makeImportThunk(DefinedImportData *S, uint16_t Machine) {
94
51
  if (Machine == AMD64)
95
32
    return make<ImportThunkChunkX64>(S);
96
19
  if (Machine == I386)
97
16
    return make<ImportThunkChunkX86>(S);
98
3
  if (Machine == ARM64)
99
2
    return make<ImportThunkChunkARM64>(S);
100
1
  assert(Machine == ARMNT);
101
1
  return make<ImportThunkChunkARM>(S);
102
1
}
103
104
DefinedImportThunk::DefinedImportThunk(StringRef Name, DefinedImportData *S,
105
                                       uint16_t Machine)
106
    : Defined(DefinedImportThunkKind, Name), WrappedSym(S),
107
51
      Data(makeImportThunk(S, Machine)) {}
108
109
60
Defined *Undefined::getWeakAlias() {
110
60
  // A weak alias may be a weak alias to another symbol, so check recursively.
111
60
  for (Symbol *A = WeakAlias; A; 
A = cast<Undefined>(A)->WeakAlias0
)
112
18
    if (auto *D = dyn_cast<Defined>(A))
113
18
      return D;
114
60
  
return nullptr42
;
115
60
}
116
} // namespace coff
117
} // namespace lld