Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/COFF/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 "Driver.h"
13
#include "Error.h"
14
#include "LTO.h"
15
#include "Memory.h"
16
#include "Symbols.h"
17
#include "llvm/IR/LLVMContext.h"
18
#include "llvm/Support/Debug.h"
19
#include "llvm/Support/raw_ostream.h"
20
#include <utility>
21
22
using namespace llvm;
23
24
namespace lld {
25
namespace coff {
26
27
enum SymbolPreference {
28
  SP_EXISTING = -1,
29
  SP_CONFLICT = 0,
30
  SP_NEW = 1,
31
};
32
33
/// Checks if an existing symbol S should be kept or replaced by a new symbol.
34
/// Returns SP_EXISTING when S should be kept, SP_NEW when the new symbol
35
/// should be kept, and SP_CONFLICT if no valid resolution exists.
36
static SymbolPreference compareDefined(Symbol *S, bool WasInserted,
37
578
                                       bool NewIsCOMDAT) {
38
578
  // If the symbol wasn't previously known, the new symbol wins by default.
39
578
  if (
WasInserted || 578
!isa<Defined>(S->body())132
)
40
561
    return SP_NEW;
41
17
42
17
  // If the existing symbol is a DefinedRegular, both it and the new symbol
43
17
  // must be comdats. In that case, we have no reason to prefer one symbol
44
17
  // over the other, and we keep the existing one. If one of the symbols
45
17
  // is not a comdat, we report a conflict.
46
17
  
if (auto *17
R17
= dyn_cast<DefinedRegular>(S->body())) {
47
17
    if (
NewIsCOMDAT && 17
R->isCOMDAT()17
)
48
17
      return SP_EXISTING;
49
17
    else
50
0
      return SP_CONFLICT;
51
0
  }
52
0
53
0
  // Existing symbol is not a DefinedRegular; new symbol wins.
54
0
  return SP_NEW;
55
0
}
56
57
SymbolTable *Symtab;
58
59
445
void SymbolTable::addFile(InputFile *File) {
60
445
  log("Reading " + toString(File));
61
445
  File->parse();
62
445
63
445
  MachineTypes MT = File->getMachineType();
64
445
  if (
Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN445
) {
65
253
    Config->Machine = MT;
66
445
  } else 
if (192
MT != IMAGE_FILE_MACHINE_UNKNOWN && 192
Config->Machine != MT74
) {
67
0
    fatal(toString(File) + ": machine type " + machineToStr(MT) +
68
0
          " conflicts with " + machineToStr(Config->Machine));
69
0
  }
70
445
71
445
  
if (auto *445
F445
= dyn_cast<ObjFile>(File)) {
72
275
    ObjFile::Instances.push_back(F);
73
445
  } else 
if (auto *170
F170
= dyn_cast<BitcodeFile>(File)) {
74
49
    BitcodeFile::Instances.push_back(F);
75
170
  } else 
if (auto *121
F121
= dyn_cast<ImportFile>(File)) {
76
66
    ImportFile::Instances.push_back(F);
77
66
  }
78
445
79
445
  StringRef S = File->getDirectives();
80
445
  if (S.empty())
81
368
    return;
82
77
83
77
  log("Directives: " + toString(File) + ": " + S);
84
77
  Driver->parseDirectives(S);
85
77
}
86
87
258
void SymbolTable::reportRemainingUndefines() {
88
258
  SmallPtrSet<SymbolBody *, 8> Undefs;
89
2.94k
  for (auto &I : Symtab) {
90
2.94k
    Symbol *Sym = I.second;
91
2.94k
    auto *Undef = dyn_cast<Undefined>(Sym->body());
92
2.94k
    if (!Undef)
93
2.90k
      continue;
94
46
    
if (46
!Sym->IsUsedInRegularObj46
)
95
19
      continue;
96
27
    StringRef Name = Undef->getName();
97
27
    // A weak alias may have been resolved, so check for that.
98
27
    if (Defined *
D27
= Undef->getWeakAlias()) {
99
24
      // We resolve weak aliases by replacing the alias's SymbolBody with the
100
24
      // target's SymbolBody. This causes all SymbolBody pointers referring to
101
24
      // the old symbol to instead refer to the new symbol. However, we can't
102
24
      // just blindly copy sizeof(Symbol::Body) bytes from D to Sym->Body
103
24
      // because D may be an internal symbol, and internal symbols are stored as
104
24
      // "unparented" SymbolBodies. For that reason we need to check which type
105
24
      // of symbol we are dealing with and copy the correct number of bytes.
106
24
      if (isa<DefinedRegular>(D))
107
22
        memcpy(Sym->Body.buffer, D, sizeof(DefinedRegular));
108
2
      else 
if (2
isa<DefinedAbsolute>(D)2
)
109
0
        memcpy(Sym->Body.buffer, D, sizeof(DefinedAbsolute));
110
2
      else
111
2
        // No other internal symbols are possible.
112
2
        Sym->Body = D->symbol()->Body;
113
24
      continue;
114
24
    }
115
3
    // If we can resolve a symbol by removing __imp_ prefix, do that.
116
3
    // This odd rule is for compatibility with MSVC linker.
117
3
    
if (3
Name.startswith("__imp_")3
) {
118
2
      Symbol *Imp = find(Name.substr(strlen("__imp_")));
119
2
      if (
Imp && 2
isa<Defined>(Imp->body())2
) {
120
2
        auto *D = cast<Defined>(Imp->body());
121
2
        replaceBody<DefinedLocalImport>(Sym, Name, D);
122
2
        LocalImportChunks.push_back(
123
2
            cast<DefinedLocalImport>(Sym->body())->getChunk());
124
2
        continue;
125
2
      }
126
1
    }
127
1
    // Remaining undefined symbols are not fatal if /force is specified.
128
1
    // They are replaced with dummy defined symbols.
129
1
    
if (1
Config->Force1
)
130
1
      replaceBody<DefinedAbsolute>(Sym, Name, 0);
131
2.94k
    Undefs.insert(Sym->body());
132
2.94k
  }
133
258
  if (Undefs.empty())
134
257
    return;
135
1
  for (SymbolBody *B : Config->GCRoot)
136
1
    
if (1
Undefs.count(B)1
)
137
0
      warn("<root>: undefined symbol: " + B->getName());
138
1
  for (ObjFile *File : ObjFile::Instances)
139
1
    for (SymbolBody *Sym : File->getSymbols())
140
3
      
if (3
Undefs.count(Sym)3
)
141
1
        warn(toString(File) + ": undefined symbol: " + Sym->getName());
142
1
  if (!Config->Force)
143
0
    fatal("link failed");
144
1
}
145
146
3.76k
std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
147
3.76k
  Symbol *&Sym = Symtab[CachedHashStringRef(Name)];
148
3.76k
  if (Sym)
149
760
    return {Sym, false};
150
3.00k
  Sym = make<Symbol>();
151
3.00k
  Sym->IsUsedInRegularObj = false;
152
3.00k
  Sym->PendingArchiveLoad = false;
153
3.00k
  return {Sym, true};
154
3.00k
}
155
156
Symbol *SymbolTable::addUndefined(StringRef Name, InputFile *F,
157
561
                                  bool IsWeakAlias) {
158
561
  Symbol *S;
159
561
  bool WasInserted;
160
561
  std::tie(S, WasInserted) = insert(Name);
161
561
  if (
!F || 561
!isa<BitcodeFile>(F)158
)
162
538
    S->IsUsedInRegularObj = true;
163
561
  if (
WasInserted || 561
(isa<Lazy>(S->body()) && 389
IsWeakAlias13
)) {
164
172
    replaceBody<Undefined>(S, Name);
165
172
    return S;
166
172
  }
167
389
  
if (auto *389
L389
= dyn_cast<Lazy>(S->body())) {
168
13
    if (
!S->PendingArchiveLoad13
) {
169
13
      S->PendingArchiveLoad = true;
170
13
      L->File->addMember(&L->Sym);
171
13
    }
172
13
  }
173
561
  return S;
174
561
}
175
176
295
void SymbolTable::addLazy(ArchiveFile *F, const Archive::Symbol Sym) {
177
295
  StringRef Name = Sym.getName();
178
295
  Symbol *S;
179
295
  bool WasInserted;
180
295
  std::tie(S, WasInserted) = insert(Name);
181
295
  if (
WasInserted295
) {
182
210
    replaceBody<Lazy>(S, F, Sym);
183
210
    return;
184
210
  }
185
85
  auto *U = dyn_cast<Undefined>(S->body());
186
85
  if (
!U || 85
U->WeakAlias74
||
S->PendingArchiveLoad74
)
187
11
    return;
188
74
  S->PendingArchiveLoad = true;
189
74
  F->addMember(&Sym);
190
74
}
191
192
2
void SymbolTable::reportDuplicate(Symbol *Existing, InputFile *NewFile) {
193
2
  error("duplicate symbol: " + toString(*Existing->body()) + " in " +
194
2
        toString(Existing->body()->getFile()) + " and in " +
195
2
        (NewFile ? 
toString(NewFile)2
:
"(internal)"0
));
196
2
}
197
198
6
Symbol *SymbolTable::addAbsolute(StringRef N, COFFSymbolRef Sym) {
199
6
  Symbol *S;
200
6
  bool WasInserted;
201
6
  std::tie(S, WasInserted) = insert(N);
202
6
  S->IsUsedInRegularObj = true;
203
6
  if (
WasInserted || 6
isa<Undefined>(S->body())0
||
isa<Lazy>(S->body())0
)
204
6
    replaceBody<DefinedAbsolute>(S, N, Sym);
205
0
  else 
if (0
!isa<DefinedCOFF>(S->body())0
)
206
0
    reportDuplicate(S, nullptr);
207
6
  return S;
208
6
}
209
210
1.92k
Symbol *SymbolTable::addAbsolute(StringRef N, uint64_t VA) {
211
1.92k
  Symbol *S;
212
1.92k
  bool WasInserted;
213
1.92k
  std::tie(S, WasInserted) = insert(N);
214
1.92k
  S->IsUsedInRegularObj = true;
215
1.92k
  if (
WasInserted || 1.92k
isa<Undefined>(S->body())14
||
isa<Lazy>(S->body())0
)
216
1.92k
    replaceBody<DefinedAbsolute>(S, N, VA);
217
0
  else 
if (0
!isa<DefinedCOFF>(S->body())0
)
218
0
    reportDuplicate(S, nullptr);
219
1.92k
  return S;
220
1.92k
}
221
222
263
Symbol *SymbolTable::addSynthetic(StringRef N, Chunk *C) {
223
263
  Symbol *S;
224
263
  bool WasInserted;
225
263
  std::tie(S, WasInserted) = insert(N);
226
263
  S->IsUsedInRegularObj = true;
227
263
  if (
WasInserted || 263
isa<Undefined>(S->body())9
||
isa<Lazy>(S->body())0
)
228
263
    replaceBody<DefinedSynthetic>(S, N, C);
229
0
  else 
if (0
!isa<DefinedCOFF>(S->body())0
)
230
0
    reportDuplicate(S, nullptr);
231
263
  return S;
232
263
}
233
234
Symbol *SymbolTable::addRegular(InputFile *F, StringRef N, bool IsCOMDAT,
235
                                const coff_symbol_generic *Sym,
236
578
                                SectionChunk *C) {
237
578
  Symbol *S;
238
578
  bool WasInserted;
239
578
  std::tie(S, WasInserted) = insert(N);
240
578
  if (!isa<BitcodeFile>(F))
241
500
    S->IsUsedInRegularObj = true;
242
578
  SymbolPreference SP = compareDefined(S, WasInserted, IsCOMDAT);
243
578
  if (
SP == SP_CONFLICT578
) {
244
0
    reportDuplicate(S, F);
245
578
  } else 
if (578
SP == SP_NEW578
) {
246
561
    replaceBody<DefinedRegular>(S, F, N, IsCOMDAT, /*IsExternal*/ true, Sym, C);
247
578
  } else 
if (17
SP == SP_EXISTING && 17
IsCOMDAT17
&&
C17
) {
248
9
    C->markDiscarded();
249
9
    // Discard associative chunks that we've parsed so far. No need to recurse
250
9
    // because an associative section cannot have children.
251
9
    for (SectionChunk *Child : C->children())
252
4
      Child->markDiscarded();
253
578
  }
254
578
  return S;
255
578
}
256
257
Symbol *SymbolTable::addCommon(InputFile *F, StringRef N, uint64_t Size,
258
10
                               const coff_symbol_generic *Sym, CommonChunk *C) {
259
10
  Symbol *S;
260
10
  bool WasInserted;
261
10
  std::tie(S, WasInserted) = insert(N);
262
10
  if (!isa<BitcodeFile>(F))
263
10
    S->IsUsedInRegularObj = true;
264
10
  if (
WasInserted || 10
!isa<DefinedCOFF>(S->body())0
)
265
10
    replaceBody<DefinedCommon>(S, F, N, Size, Sym, C);
266
0
  else 
if (auto *0
DC0
= dyn_cast<DefinedCommon>(S->body()))
267
0
    
if (0
Size > DC->getSize()0
)
268
0
      replaceBody<DefinedCommon>(S, F, N, Size, Sym, C);
269
10
  return S;
270
10
}
271
272
67
DefinedImportData *SymbolTable::addImportData(StringRef N, ImportFile *F) {
273
67
  Symbol *S;
274
67
  bool WasInserted;
275
67
  std::tie(S, WasInserted) = insert(N);
276
67
  S->IsUsedInRegularObj = true;
277
67
  if (
WasInserted || 67
isa<Undefined>(S->body())67
||
isa<Lazy>(S->body())62
) {
278
66
    replaceBody<DefinedImportData>(S, N, F);
279
66
    return cast<DefinedImportData>(S->body());
280
66
  }
281
1
282
1
  reportDuplicate(S, F);
283
1
  return nullptr;
284
1
}
285
286
DefinedImportThunk *SymbolTable::addImportThunk(StringRef Name,
287
                                               DefinedImportData *ID,
288
64
                                               uint16_t Machine) {
289
64
  Symbol *S;
290
64
  bool WasInserted;
291
64
  std::tie(S, WasInserted) = insert(Name);
292
64
  S->IsUsedInRegularObj = true;
293
64
  if (
WasInserted || 64
isa<Undefined>(S->body())64
||
isa<Lazy>(S->body())9
) {
294
63
    replaceBody<DefinedImportThunk>(S, Name, ID, Machine);
295
63
    return cast<DefinedImportThunk>(S->body());
296
63
  }
297
1
298
1
  reportDuplicate(S, ID->File);
299
1
  return nullptr;
300
1
}
301
302
756
std::vector<Chunk *> SymbolTable::getChunks() {
303
756
  std::vector<Chunk *> Res;
304
923
  for (ObjFile *File : ObjFile::Instances) {
305
923
    std::vector<Chunk *> &V = File->getChunks();
306
923
    Res.insert(Res.end(), V.begin(), V.end());
307
923
  }
308
756
  return Res;
309
756
}
310
311
952
Symbol *SymbolTable::find(StringRef Name) {
312
952
  auto It = Symtab.find(CachedHashStringRef(Name));
313
952
  if (It == Symtab.end())
314
782
    return nullptr;
315
170
  return It->second;
316
170
}
317
318
905
Symbol *SymbolTable::findUnderscore(StringRef Name) {
319
905
  if (Config->Machine == I386)
320
129
    return find(("_" + Name).str());
321
776
  return find(Name);
322
776
}
323
324
27
StringRef SymbolTable::findByPrefix(StringRef Prefix) {
325
237
  for (auto Pair : Symtab) {
326
237
    StringRef Name = Pair.first.val();
327
237
    if (Name.startswith(Prefix))
328
13
      return Name;
329
14
  }
330
14
  return "";
331
14
}
332
333
29
StringRef SymbolTable::findMangle(StringRef Name) {
334
29
  if (Symbol *Sym = find(Name))
335
23
    
if (23
!isa<Undefined>(Sym->body())23
)
336
4
      return Name;
337
25
  
if (25
Config->Machine != I38625
)
338
16
    return findByPrefix(("?" + Name + "@@Y").str());
339
9
  
if (9
!Name.startswith("_")9
)
340
0
    return "";
341
9
  // Search for x86 C function.
342
9
  StringRef S = findByPrefix((Name + "@").str());
343
9
  if (!S.empty())
344
7
    return S;
345
2
  // Search for x86 C++ non-member function.
346
2
  return findByPrefix(("?" + Name.substr(1) + "@@Y").str());
347
2
}
348
349
317
void SymbolTable::mangleMaybe(SymbolBody *B) {
350
317
  auto *U = dyn_cast<Undefined>(B);
351
317
  if (
!U || 317
U->WeakAlias24
)
352
298
    return;
353
19
  StringRef Alias = findMangle(U->getName());
354
19
  if (!Alias.empty())
355
13
    U->WeakAlias = addUndefined(Alias);
356
317
}
357
358
403
SymbolBody *SymbolTable::addUndefined(StringRef Name) {
359
403
  return addUndefined(Name, nullptr, false)->body();
360
403
}
361
362
36
std::vector<StringRef> SymbolTable::compileBitcodeFiles() {
363
36
  LTO.reset(new BitcodeCompiler);
364
36
  for (BitcodeFile *F : BitcodeFile::Instances)
365
49
    LTO->add(*F);
366
36
  return LTO->compile();
367
36
}
368
369
258
void SymbolTable::addCombinedLTOObjects() {
370
258
  if (BitcodeFile::Instances.empty())
371
222
    return;
372
36
  
for (StringRef Object : compileBitcodeFiles()) 36
{
373
43
    auto *Obj = make<ObjFile>(MemoryBufferRef(Object, "lto.tmp"));
374
43
    Obj->parse();
375
43
    ObjFile::Instances.push_back(Obj);
376
43
  }
377
258
}
378
379
} // namespace coff
380
} // namespace lld