Coverage Report

Created: 2018-01-17 21:32

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/lib/Core/SymbolTable.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Core/SymbolTable.cpp - Main Symbol Table ---------------------------===//
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 "lld/Core/SymbolTable.h"
11
#include "lld/Common/LLVM.h"
12
#include "lld/Core/AbsoluteAtom.h"
13
#include "lld/Core/Atom.h"
14
#include "lld/Core/DefinedAtom.h"
15
#include "lld/Core/File.h"
16
#include "lld/Core/LinkingContext.h"
17
#include "lld/Core/Resolver.h"
18
#include "lld/Core/SharedLibraryAtom.h"
19
#include "lld/Core/UndefinedAtom.h"
20
#include "llvm/ADT/ArrayRef.h"
21
#include "llvm/ADT/DenseMapInfo.h"
22
#include "llvm/ADT/Hashing.h"
23
#include "llvm/Support/ErrorHandling.h"
24
#include "llvm/Support/raw_ostream.h"
25
#include <algorithm>
26
#include <cassert>
27
#include <cstdlib>
28
#include <vector>
29
30
namespace lld {
31
166
bool SymbolTable::add(const UndefinedAtom &atom) { return addByName(atom); }
32
33
67
bool SymbolTable::add(const SharedLibraryAtom &atom) { return addByName(atom); }
34
35
0
bool SymbolTable::add(const AbsoluteAtom &atom) { return addByName(atom); }
36
37
349
bool SymbolTable::add(const DefinedAtom &atom) {
38
349
  if (!atom.name().empty() &&
39
349
      
atom.scope() != DefinedAtom::scopeTranslationUnit270
) {
40
224
    // Named atoms cannot be merged by content.
41
224
    assert(atom.merge() != DefinedAtom::mergeByContent);
42
224
    // Track named atoms that are not scoped to file (static).
43
224
    return addByName(atom);
44
224
  }
45
125
  if (atom.merge() == DefinedAtom::mergeByContent) {
46
16
    // Named atoms cannot be merged by content.
47
16
    assert(atom.name().empty());
48
16
    // Currently only read-only constants can be merged.
49
16
    if (atom.permissions() == DefinedAtom::permR__)
50
11
      return addByContent(atom);
51
114
    // TODO: support mergeByContent of data atoms by comparing content & fixups.
52
114
  }
53
114
  return false;
54
114
}
55
56
enum NameCollisionResolution {
57
  NCR_First,
58
  NCR_Second,
59
  NCR_DupDef,
60
  NCR_DupUndef,
61
  NCR_DupShLib,
62
  NCR_Error
63
};
64
65
static NameCollisionResolution cases[4][4] = {
66
  //regular     absolute    undef      sharedLib
67
  {
68
    // first is regular
69
    NCR_DupDef, NCR_Error,   NCR_First, NCR_First
70
  },
71
  {
72
    // first is absolute
73
    NCR_Error,  NCR_Error,  NCR_First, NCR_First
74
  },
75
  {
76
    // first is undef
77
    NCR_Second, NCR_Second, NCR_DupUndef, NCR_Second
78
  },
79
  {
80
    // first is sharedLib
81
    NCR_Second, NCR_Second, NCR_First, NCR_DupShLib
82
  }
83
};
84
85
static NameCollisionResolution collide(Atom::Definition first,
86
137
                                       Atom::Definition second) {
87
137
  return cases[first][second];
88
137
}
89
90
enum MergeResolution {
91
  MCR_First,
92
  MCR_Second,
93
  MCR_Largest,
94
  MCR_SameSize,
95
  MCR_Error
96
};
97
98
static MergeResolution mergeCases[][6] = {
99
  // no          tentative      weak          weakAddress   sameNameAndSize largest
100
  {MCR_Error,    MCR_First,     MCR_First,    MCR_First,    MCR_SameSize,   MCR_Largest},  // no
101
  {MCR_Second,   MCR_Largest,   MCR_Second,   MCR_Second,   MCR_SameSize,   MCR_Largest},  // tentative
102
  {MCR_Second,   MCR_First,     MCR_First,    MCR_Second,   MCR_SameSize,   MCR_Largest},  // weak
103
  {MCR_Second,   MCR_First,     MCR_First,    MCR_First,    MCR_SameSize,   MCR_Largest},  // weakAddress
104
  {MCR_SameSize, MCR_SameSize,  MCR_SameSize, MCR_SameSize, MCR_SameSize,   MCR_SameSize}, // sameSize
105
  {MCR_Largest,  MCR_Largest,   MCR_Largest,  MCR_Largest,  MCR_SameSize,   MCR_Largest},  // largest
106
};
107
108
static MergeResolution mergeSelect(DefinedAtom::Merge first,
109
0
                                   DefinedAtom::Merge second) {
110
0
  assert(first != DefinedAtom::mergeByContent);
111
0
  assert(second != DefinedAtom::mergeByContent);
112
0
  return mergeCases[first][second];
113
0
}
114
115
457
bool SymbolTable::addByName(const Atom &newAtom) {
116
457
  StringRef name = newAtom.name();
117
457
  assert(!name.empty());
118
457
  const Atom *existing = findByName(name);
119
457
  if (existing == nullptr) {
120
320
    // Name is not in symbol table yet, add it associate with this atom.
121
320
    _nameTable[name] = &newAtom;
122
320
    return true;
123
320
  }
124
137
125
137
  // Do nothing if the same object is added more than once.
126
137
  if (existing == &newAtom)
127
0
    return false;
128
137
129
137
  // Name is already in symbol table and associated with another atom.
130
137
  bool useNew = true;
131
137
  switch (collide(existing->definition(), newAtom.definition())) {
132
137
  case NCR_First:
133
6
    useNew = false;
134
6
    break;
135
137
  case NCR_Second:
136
103
    useNew = true;
137
103
    break;
138
137
  case NCR_DupDef: {
139
0
    const auto *existingDef = cast<DefinedAtom>(existing);
140
0
    const auto *newDef = cast<DefinedAtom>(&newAtom);
141
0
    switch (mergeSelect(existingDef->merge(), newDef->merge())) {
142
0
    case MCR_First:
143
0
      useNew = false;
144
0
      break;
145
0
    case MCR_Second:
146
0
      useNew = true;
147
0
      break;
148
0
    case MCR_Largest: {
149
0
      uint64_t existingSize = existingDef->sectionSize();
150
0
      uint64_t newSize = newDef->sectionSize();
151
0
      useNew = (newSize >= existingSize);
152
0
      break;
153
0
    }
154
0
    case MCR_SameSize: {
155
0
      uint64_t existingSize = existingDef->sectionSize();
156
0
      uint64_t newSize = newDef->sectionSize();
157
0
      if (existingSize == newSize) {
158
0
        useNew = true;
159
0
        break;
160
0
      }
161
0
      llvm::errs() << "Size mismatch: "
162
0
                   << existing->name() << " (" << existingSize << ") "
163
0
                   << newAtom.name() << " (" << newSize << ")\n";
164
0
      LLVM_FALLTHROUGH;
165
0
    }
166
0
    case MCR_Error:
167
0
      llvm::errs() << "Duplicate symbols: "
168
0
                   << existing->name()
169
0
                   << ":"
170
0
                   << existing->file().path()
171
0
                   << " and "
172
0
                   << newAtom.name()
173
0
                   << ":"
174
0
                   << newAtom.file().path()
175
0
                   << "\n";
176
0
      llvm::report_fatal_error("duplicate symbol error");
177
0
      break;
178
0
    }
179
0
    break;
180
0
  }
181
28
  case NCR_DupUndef: {
182
28
    const UndefinedAtom* existingUndef = cast<UndefinedAtom>(existing);
183
28
    const UndefinedAtom* newUndef = cast<UndefinedAtom>(&newAtom);
184
28
185
28
    bool sameCanBeNull = (existingUndef->canBeNull() == newUndef->canBeNull());
186
28
    if (sameCanBeNull)
187
28
      useNew = false;
188
0
    else
189
0
      useNew = (newUndef->canBeNull() < existingUndef->canBeNull());
190
28
    break;
191
0
  }
192
0
  case NCR_DupShLib: {
193
0
    useNew = false;
194
0
    break;
195
0
  }
196
0
  case NCR_Error:
197
0
    llvm::errs() << "SymbolTable: error while merging " << name << "\n";
198
0
    llvm::report_fatal_error("duplicate symbol error");
199
0
    break;
200
137
  }
201
137
202
137
  if (useNew) {
203
103
    // Update name table to use new atom.
204
103
    _nameTable[name] = &newAtom;
205
103
    // Add existing atom to replacement table.
206
103
    _replacedAtoms[existing] = &newAtom;
207
103
  } else {
208
34
    // New atom is not being used.  Add it to replacement table.
209
34
    _replacedAtoms[&newAtom] = existing;
210
34
  }
211
137
  return false;
212
137
}
213
214
13
unsigned SymbolTable::AtomMappingInfo::getHashValue(const DefinedAtom *atom) {
215
13
  auto content = atom->rawContent();
216
13
  return llvm::hash_combine(atom->size(),
217
13
                            atom->contentType(),
218
13
                            llvm::hash_combine_range(content.begin(),
219
13
                                                     content.end()));
220
13
}
221
222
bool SymbolTable::AtomMappingInfo::isEqual(const DefinedAtom * const l,
223
624
                                           const DefinedAtom * const r) {
224
624
  if (l == r)
225
589
    return true;
226
35
  if (l == getEmptyKey() || r == getEmptyKey())
227
24
    return false;
228
11
  if (l == getTombstoneKey() || r == getTombstoneKey())
229
11
    return false;
230
0
  if (l->contentType() != r->contentType())
231
0
    return false;
232
0
  if (l->size() != r->size())
233
0
    return false;
234
0
  if (l->sectionChoice() != r->sectionChoice())
235
0
    return false;
236
0
  if (l->sectionChoice() == DefinedAtom::sectionCustomRequired) {
237
0
    if (!l->customSectionName().equals(r->customSectionName()))
238
0
      return false;
239
0
  }
240
0
  ArrayRef<uint8_t> lc = l->rawContent();
241
0
  ArrayRef<uint8_t> rc = r->rawContent();
242
0
  return memcmp(lc.data(), rc.data(), lc.size()) == 0;
243
0
}
244
245
11
bool SymbolTable::addByContent(const DefinedAtom &newAtom) {
246
11
  AtomContentSet::iterator pos = _contentTable.find(&newAtom);
247
11
  if (pos == _contentTable.end()) {
248
11
    _contentTable.insert(&newAtom);
249
11
    return true;
250
11
  }
251
0
  const Atom* existing = *pos;
252
0
  // New atom is not being used.  Add it to replacement table.
253
0
  _replacedAtoms[&newAtom] = existing;
254
0
  return false;
255
0
}
256
257
597
const Atom *SymbolTable::findByName(StringRef sym) {
258
597
  NameToAtom::iterator pos = _nameTable.find(sym);
259
597
  if (pos == _nameTable.end())
260
320
    return nullptr;
261
277
  return pos->second;
262
277
}
263
264
308
const Atom *SymbolTable::replacement(const Atom *atom) {
265
308
  // Find the replacement for a given atom. Atoms in _replacedAtoms
266
308
  // may be chained, so find the last one.
267
338
  for (;;) {
268
338
    AtomToAtom::iterator pos = _replacedAtoms.find(atom);
269
338
    if (pos == _replacedAtoms.end())
270
308
      return atom;
271
30
    atom = pos->second;
272
30
  }
273
308
}
274
275
658
bool SymbolTable::isCoalescedAway(const Atom *atom) {
276
658
  return _replacedAtoms.count(atom) > 0;
277
658
}
278
279
83
std::vector<const UndefinedAtom *> SymbolTable::undefines() {
280
83
  std::vector<const UndefinedAtom *> ret;
281
320
  for (auto it : _nameTable) {
282
320
    const Atom *atom = it.second;
283
320
    assert(atom != nullptr);
284
320
    if (const auto *undef = dyn_cast<const UndefinedAtom>(atom))
285
29
      if (_replacedAtoms.count(undef) == 0)
286
29
        ret.push_back(undef);
287
320
  }
288
83
  return ret;
289
83
}
290
291
} // namespace lld