Coverage Report

Created: 2018-10-20 06:24

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/COFF/InputFiles.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- InputFiles.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 "InputFiles.h"
11
#include "Chunks.h"
12
#include "Config.h"
13
#include "Driver.h"
14
#include "SymbolTable.h"
15
#include "Symbols.h"
16
#include "lld/Common/ErrorHandler.h"
17
#include "lld/Common/Memory.h"
18
#include "llvm-c/lto.h"
19
#include "llvm/ADT/SmallVector.h"
20
#include "llvm/ADT/Triple.h"
21
#include "llvm/ADT/Twine.h"
22
#include "llvm/BinaryFormat/COFF.h"
23
#include "llvm/Object/Binary.h"
24
#include "llvm/Object/COFF.h"
25
#include "llvm/Support/Casting.h"
26
#include "llvm/Support/Endian.h"
27
#include "llvm/Support/Error.h"
28
#include "llvm/Support/ErrorOr.h"
29
#include "llvm/Support/FileSystem.h"
30
#include "llvm/Support/Path.h"
31
#include "llvm/Target/TargetOptions.h"
32
#include <cstring>
33
#include <system_error>
34
#include <utility>
35
36
using namespace llvm;
37
using namespace llvm::COFF;
38
using namespace llvm::object;
39
using namespace llvm::support::endian;
40
41
using llvm::Triple;
42
using llvm::support::ulittle32_t;
43
44
namespace lld {
45
namespace coff {
46
47
std::vector<ObjFile *> ObjFile::Instances;
48
std::vector<ImportFile *> ImportFile::Instances;
49
std::vector<BitcodeFile *> BitcodeFile::Instances;
50
51
/// Checks that Source is compatible with being a weak alias to Target.
52
/// If Source is Undefined and has no weak alias set, makes it a weak
53
/// alias to Target.
54
static void checkAndSetWeakAlias(SymbolTable *Symtab, InputFile *F,
55
11
                                 Symbol *Source, Symbol *Target) {
56
11
  if (auto *U = dyn_cast<Undefined>(Source)) {
57
10
    if (U->WeakAlias && 
U->WeakAlias != Target2
) {
58
2
      // Weak aliases as produced by GCC are named in the form
59
2
      // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
60
2
      // of another symbol emitted near the weak symbol.
61
2
      // Just use the definition from the first object file that defined
62
2
      // this weak symbol.
63
2
      if (Config->MinGW)
64
2
        return;
65
0
      Symtab->reportDuplicate(Source, F);
66
0
    }
67
10
    U->WeakAlias = Target;
68
8
  }
69
11
}
70
71
84
ArchiveFile::ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
72
73
84
void ArchiveFile::parse() {
74
84
  // Parse a MemoryBufferRef as an archive file.
75
84
  File = CHECK(Archive::create(MB), this);
76
84
77
84
  // Read the symbol table to construct Lazy objects.
78
84
  for (const Archive::Symbol &Sym : File->symbols())
79
452
    Symtab->addLazy(this, Sym);
80
84
}
81
82
// Returns a buffer pointing to a member file containing a given symbol.
83
139
void ArchiveFile::addMember(const Archive::Symbol *Sym) {
84
139
  const Archive::Child &C =
85
139
      CHECK(Sym->getMember(),
86
139
            "could not get the member for symbol " + Sym->getName());
87
139
88
139
  // Return an empty buffer if we have already returned the same buffer.
89
139
  if (!Seen.insert(C.getChildOffset()).second)
90
1
    return;
91
138
92
138
  Driver->enqueueArchiveMember(C, Sym->getName(), getName());
93
138
}
94
95
5
std::vector<MemoryBufferRef> getArchiveMembers(Archive *File) {
96
5
  std::vector<MemoryBufferRef> V;
97
5
  Error Err = Error::success();
98
5
  for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
99
5
    Archive::Child C =
100
5
        CHECK(COrErr,
101
5
              File->getFileName() + ": could not get the child of the archive");
102
5
    MemoryBufferRef MBRef =
103
5
        CHECK(C.getMemoryBufferRef(),
104
5
              File->getFileName() +
105
5
                  ": could not get the buffer for a child of the archive");
106
5
    V.push_back(MBRef);
107
5
  }
108
5
  if (Err)
109
0
    fatal(File->getFileName() +
110
0
          ": Archive::children failed: " + toString(std::move(Err)));
111
5
  return V;
112
5
}
113
114
649
void ObjFile::parse() {
115
649
  // Parse a memory buffer as a COFF file.
116
649
  std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), this);
117
649
118
649
  if (auto *Obj = dyn_cast<COFFObjectFile>(Bin.get())) {
119
649
    Bin.release();
120
649
    COFFObj.reset(Obj);
121
649
  } else {
122
0
    fatal(toString(this) + " is not a COFF file");
123
0
  }
124
649
125
649
  // Read section and symbol tables.
126
649
  initializeChunks();
127
649
  initializeSymbols();
128
649
}
129
130
// We set SectionChunk pointers in the SparseChunks vector to this value
131
// temporarily to mark comdat sections as having an unknown resolution. As we
132
// walk the object file's symbol table, once we visit either a leader symbol or
133
// an associative section definition together with the parent comdat's leader,
134
// we set the pointer to either nullptr (to mark the section as discarded) or a
135
// valid SectionChunk for that section.
136
static SectionChunk *const PendingComdat = reinterpret_cast<SectionChunk *>(1);
137
138
649
void ObjFile::initializeChunks() {
139
649
  uint32_t NumSections = COFFObj->getNumberOfSections();
140
649
  Chunks.reserve(NumSections);
141
649
  SparseChunks.resize(NumSections + 1);
142
2.94k
  for (uint32_t I = 1; I < NumSections + 1; 
++I2.29k
) {
143
2.29k
    const coff_section *Sec;
144
2.29k
    if (auto EC = COFFObj->getSection(I, Sec))
145
0
      fatal("getSection failed: #" + Twine(I) + ": " + EC.message());
146
2.29k
147
2.29k
    if (Sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
148
409
      SparseChunks[I] = PendingComdat;
149
1.88k
    else
150
1.88k
      SparseChunks[I] = readSection(I, nullptr, "");
151
2.29k
  }
152
649
}
153
154
SectionChunk *ObjFile::readSection(uint32_t SectionNumber,
155
                                   const coff_aux_section_definition *Def,
156
2.26k
                                   StringRef LeaderName) {
157
2.26k
  const coff_section *Sec;
158
2.26k
  StringRef Name;
159
2.26k
  if (auto EC = COFFObj->getSection(SectionNumber, Sec))
160
0
    fatal("getSection failed: #" + Twine(SectionNumber) + ": " + EC.message());
161
2.26k
  if (auto EC = COFFObj->getSectionName(Sec, Name))
162
0
    fatal("getSectionName failed: #" + Twine(SectionNumber) + ": " +
163
0
          EC.message());
164
2.26k
165
2.26k
  if (Name == ".drectve") {
166
156
    ArrayRef<uint8_t> Data;
167
156
    COFFObj->getSectionContents(Sec, Data);
168
156
    Directives = std::string((const char *)Data.data(), Data.size());
169
156
    return nullptr;
170
156
  }
171
2.10k
172
2.10k
  if (Name == ".llvm_addrsig") {
173
3
    AddrsigSec = Sec;
174
3
    return nullptr;
175
3
  }
176
2.10k
177
2.10k
  // Object files may have DWARF debug info or MS CodeView debug info
178
2.10k
  // (or both).
179
2.10k
  //
180
2.10k
  // DWARF sections don't need any special handling from the perspective
181
2.10k
  // of the linker; they are just a data section containing relocations.
182
2.10k
  // We can just link them to complete debug info.
183
2.10k
  //
184
2.10k
  // CodeView needs a linker support. We need to interpret and debug
185
2.10k
  // info, and then write it to a separate .pdb file.
186
2.10k
187
2.10k
  // Ignore DWARF debug info unless /debug is given.
188
2.10k
  if (!Config->Debug && 
Name.startswith(".debug_")1.58k
)
189
16
    return nullptr;
190
2.08k
191
2.08k
  if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
192
0
    return nullptr;
193
2.08k
  auto *C = make<SectionChunk>(this, Sec);
194
2.08k
  if (Def)
195
378
    C->Checksum = Def->CheckSum;
196
2.08k
197
2.08k
  // CodeView sections are stored to a different vector because they are not
198
2.08k
  // linked in the regular manner.
199
2.08k
  if (C->isCodeView())
200
184
    DebugChunks.push_back(C);
201
1.90k
  else if (Config->GuardCF != GuardCFLevel::Off && 
Name == ".gfids$y"104
)
202
10
    GuardFidChunks.push_back(C);
203
1.89k
  else if (Config->GuardCF != GuardCFLevel::Off && 
Name == ".gljmp$y"94
)
204
1
    GuardLJmpChunks.push_back(C);
205
1.89k
  else if (Name == ".sxdata")
206
8
    SXDataChunks.push_back(C);
207
1.88k
  else if (Config->TailMerge && 
Sec->NumberOfRelocations == 01.40k
&&
208
1.88k
           
Name == ".rdata"1.14k
&&
LeaderName.startswith("??_C@")43
)
209
12
    // COFF sections that look like string literal sections (i.e. no
210
12
    // relocations, in .rdata, leader symbol name matches the MSVC name mangling
211
12
    // for string literals) are subject to string tail merging.
212
12
    MergeChunk::addSection(C);
213
1.87k
  else
214
1.87k
    Chunks.push_back(C);
215
2.08k
216
2.08k
  return C;
217
2.08k
}
218
219
void ObjFile::readAssociativeDefinition(
220
113
    COFFSymbolRef Sym, const coff_aux_section_definition *Def) {
221
113
  readAssociativeDefinition(Sym, Def, Def->getNumber(Sym.isBigObj()));
222
113
}
223
224
void ObjFile::readAssociativeDefinition(COFFSymbolRef Sym,
225
                                        const coff_aux_section_definition *Def,
226
117
                                        uint32_t ParentSection) {
227
117
  SectionChunk *Parent = SparseChunks[ParentSection];
228
117
229
117
  // If the parent is pending, it probably means that its section definition
230
117
  // appears after us in the symbol table. Leave the associated section as
231
117
  // pending; we will handle it during the second pass in initializeSymbols().
232
117
  if (Parent == PendingComdat)
233
19
    return;
234
98
235
98
  // Check whether the parent is prevailing. If it is, so are we, and we read
236
98
  // the section; otherwise mark it as discarded.
237
98
  int32_t SectionNumber = Sym.getSectionNumber();
238
98
  if (Parent) {
239
91
    SparseChunks[SectionNumber] = readSection(SectionNumber, Def, "");
240
91
    if (SparseChunks[SectionNumber])
241
91
      Parent->addAssociative(SparseChunks[SectionNumber]);
242
91
  } else {
243
7
    SparseChunks[SectionNumber] = nullptr;
244
7
  }
245
98
}
246
247
void ObjFile::recordPrevailingSymbolForMingw(
248
6
    COFFSymbolRef Sym, DenseMap<StringRef, uint32_t> &PrevailingSectionMap) {
249
6
  // For comdat symbols in executable sections, where this is the copy
250
6
  // of the section chunk we actually include instead of discarding it,
251
6
  // add the symbol to a map to allow using it for implicitly
252
6
  // associating .[px]data$<func> sections to it.
253
6
  int32_t SectionNumber = Sym.getSectionNumber();
254
6
  SectionChunk *SC = SparseChunks[SectionNumber];
255
6
  if (SC && SC->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
256
4
    StringRef Name;
257
4
    COFFObj->getSymbolName(Sym, Name);
258
4
    PrevailingSectionMap[Name] = SectionNumber;
259
4
  }
260
6
}
261
262
void ObjFile::maybeAssociateSEHForMingw(
263
    COFFSymbolRef Sym, const coff_aux_section_definition *Def,
264
21
    const DenseMap<StringRef, uint32_t> &PrevailingSectionMap) {
265
21
  StringRef Name;
266
21
  COFFObj->getSymbolName(Sym, Name);
267
21
  if (Name.consume_front(".pdata$") || 
Name.consume_front(".xdata$")19
) {
268
10
    // For MinGW, treat .[px]data$<func> as implicitly associative to
269
10
    // the symbol <func>.
270
10
    auto ParentSym = PrevailingSectionMap.find(Name);
271
10
    if (ParentSym != PrevailingSectionMap.end())
272
4
      readAssociativeDefinition(Sym, Def, ParentSym->second);
273
10
  }
274
21
}
275
276
3.52k
Symbol *ObjFile::createRegular(COFFSymbolRef Sym) {
277
3.52k
  SectionChunk *SC = SparseChunks[Sym.getSectionNumber()];
278
3.52k
  if (Sym.isExternal()) {
279
933
    StringRef Name;
280
933
    COFFObj->getSymbolName(Sym, Name);
281
933
    if (SC)
282
932
      return Symtab->addRegular(this, Name, Sym.getGeneric(), SC);
283
1
    // For MinGW symbols named .weak.* that point to a discarded section,
284
1
    // don't create an Undefined symbol. If nothing ever refers to the symbol,
285
1
    // everything should be fine. If something actually refers to the symbol
286
1
    // (e.g. the undefined weak alias), linking will fail due to undefined
287
1
    // references at the end.
288
1
    if (Config->MinGW && Name.startswith(".weak."))
289
1
      return nullptr;
290
0
    return Symtab->addUndefined(Name, this, false);
291
0
  }
292
2.59k
  if (SC)
293
2.47k
    return make<DefinedRegular>(this, /*Name*/ "", false,
294
2.47k
                                /*IsExternal*/ false, Sym.getGeneric(), SC);
295
125
  return nullptr;
296
125
}
297
298
649
void ObjFile::initializeSymbols() {
299
649
  uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
300
649
  Symbols.resize(NumSymbols);
301
649
302
649
  SmallVector<std::pair<Symbol *, uint32_t>, 8> WeakAliases;
303
649
  std::vector<uint32_t> PendingIndexes;
304
649
  PendingIndexes.reserve(NumSymbols);
305
649
306
649
  DenseMap<StringRef, uint32_t> PrevailingSectionMap;
307
649
  std::vector<const coff_aux_section_definition *> ComdatDefs(
308
649
      COFFObj->getNumberOfSections() + 1);
309
649
310
5.05k
  for (uint32_t I = 0; I < NumSymbols; 
++I4.40k
) {
311
4.40k
    COFFSymbolRef COFFSym = check(COFFObj->getSymbol(I));
312
4.40k
    bool PrevailingComdat;
313
4.40k
    if (COFFSym.isUndefined()) {
314
283
      Symbols[I] = createUndefined(COFFSym);
315
4.12k
    } else if (COFFSym.isWeakExternal()) {
316
9
      Symbols[I] = createUndefined(COFFSym);
317
9
      uint32_t TagIndex = COFFSym.getAux<coff_aux_weak_external>()->TagIndex;
318
9
      WeakAliases.emplace_back(Symbols[I], TagIndex);
319
4.11k
    } else if (Optional<Symbol *> OptSym =
320
3.78k
                   createDefined(COFFSym, ComdatDefs, PrevailingComdat)) {
321
3.78k
      Symbols[I] = *OptSym;
322
3.78k
      if (Config->MinGW && 
PrevailingComdat381
)
323
6
        recordPrevailingSymbolForMingw(COFFSym, PrevailingSectionMap);
324
3.78k
    } else {
325
334
      // createDefined() returns None if a symbol belongs to a section that
326
334
      // was pending at the point when the symbol was read. This can happen in
327
334
      // two cases:
328
334
      // 1) section definition symbol for a comdat leader;
329
334
      // 2) symbol belongs to a comdat section associated with a section whose
330
334
      //    section definition symbol appears later in the symbol table.
331
334
      // In both of these cases, we can expect the section to be resolved by
332
334
      // the time we finish visiting the remaining symbols in the symbol
333
334
      // table. So we postpone the handling of this symbol until that time.
334
334
      PendingIndexes.push_back(I);
335
334
    }
336
4.40k
    I += COFFSym.getNumberOfAuxSymbols();
337
4.40k
  }
338
649
339
649
  for (uint32_t I : PendingIndexes) {
340
334
    COFFSymbolRef Sym = check(COFFObj->getSymbol(I));
341
334
    if (auto *Def = Sym.getSectionDefinition()) {
342
334
      if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
343
19
        readAssociativeDefinition(Sym, Def);
344
315
      else if (Config->MinGW)
345
21
        maybeAssociateSEHForMingw(Sym, Def, PrevailingSectionMap);
346
334
    }
347
334
    if (SparseChunks[Sym.getSectionNumber()] == PendingComdat) {
348
7
      StringRef Name;
349
7
      COFFObj->getSymbolName(Sym, Name);
350
7
      log("comdat section " + Name +
351
7
          " without leader and unassociated, discarding");
352
7
      continue;
353
7
    }
354
327
    Symbols[I] = createRegular(Sym);
355
327
  }
356
649
357
649
  for (auto &KV : WeakAliases) {
358
9
    Symbol *Sym = KV.first;
359
9
    uint32_t Idx = KV.second;
360
9
    checkAndSetWeakAlias(Symtab, this, Sym, Symbols[Idx]);
361
9
  }
362
649
}
363
364
292
Symbol *ObjFile::createUndefined(COFFSymbolRef Sym) {
365
292
  StringRef Name;
366
292
  COFFObj->getSymbolName(Sym, Name);
367
292
  return Symtab->addUndefined(Name, this, Sym.isWeakExternal());
368
292
}
369
370
Optional<Symbol *> ObjFile::createDefined(
371
    COFFSymbolRef Sym,
372
    std::vector<const coff_aux_section_definition *> &ComdatDefs,
373
4.11k
    bool &Prevailing) {
374
4.11k
  Prevailing = false;
375
4.11k
  auto GetName = [&]() {
376
839
    StringRef S;
377
839
    COFFObj->getSymbolName(Sym, S);
378
839
    return S;
379
839
  };
380
4.11k
381
4.11k
  if (Sym.isCommon()) {
382
12
    auto *C = make<CommonChunk>(Sym);
383
12
    Chunks.push_back(C);
384
12
    return Symtab->addCommon(this, GetName(), Sym.getValue(), Sym.getGeneric(),
385
12
                             C);
386
12
  }
387
4.10k
388
4.10k
  if (Sym.isAbsolute()) {
389
258
    StringRef Name = GetName();
390
258
391
258
    // Skip special symbols.
392
258
    if (Name == "@comp.id")
393
68
      return nullptr;
394
190
    if (Name == "@feat.00") {
395
169
      Feat00Flags = Sym.getValue();
396
169
      return nullptr;
397
169
    }
398
21
399
21
    if (Sym.isExternal())
400
17
      return Symtab->addAbsolute(Name, Sym);
401
4
    return make<DefinedAbsolute>(Name, Sym);
402
4
  }
403
3.84k
404
3.84k
  int32_t SectionNumber = Sym.getSectionNumber();
405
3.84k
  if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
406
5
    return nullptr;
407
3.83k
408
3.83k
  if (llvm::COFF::isReservedSectionNumber(SectionNumber))
409
0
    fatal(toString(this) + ": " + GetName() +
410
0
          " should not refer to special section " + Twine(SectionNumber));
411
3.83k
412
3.83k
  if ((uint32_t)SectionNumber >= SparseChunks.size())
413
0
    fatal(toString(this) + ": " + GetName() +
414
0
          " should not refer to non-existent section " + Twine(SectionNumber));
415
3.83k
416
3.83k
  // Handle comdat leader symbols.
417
3.83k
  if (const coff_aux_section_definition *Def = ComdatDefs[SectionNumber]) {
418
304
    ComdatDefs[SectionNumber] = nullptr;
419
304
    Symbol *Leader;
420
304
    if (Sym.isExternal()) {
421
282
      std::tie(Leader, Prevailing) =
422
282
          Symtab->addComdat(this, GetName(), Sym.getGeneric());
423
282
    } else {
424
22
      Leader = make<DefinedRegular>(this, /*Name*/ "", false,
425
22
                                    /*IsExternal*/ false, Sym.getGeneric());
426
22
      Prevailing = true;
427
22
    }
428
304
429
304
    if (Prevailing) {
430
287
      SectionChunk *C = readSection(SectionNumber, Def, GetName());
431
287
      SparseChunks[SectionNumber] = C;
432
287
      C->Sym = cast<DefinedRegular>(Leader);
433
287
      cast<DefinedRegular>(Leader)->Data = &C->Repl;
434
287
    } else {
435
17
      SparseChunks[SectionNumber] = nullptr;
436
17
    }
437
304
    return Leader;
438
304
  }
439
3.53k
440
3.53k
  // Read associative section definitions and prepare to handle the comdat
441
3.53k
  // leader symbol by setting the section's ComdatDefs pointer if we encounter a
442
3.53k
  // non-associative comdat.
443
3.53k
  if (SparseChunks[SectionNumber] == PendingComdat) {
444
409
    if (auto *Def = Sym.getSectionDefinition()) {
445
409
      if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
446
94
        readAssociativeDefinition(Sym, Def);
447
315
      else
448
315
        ComdatDefs[SectionNumber] = Def;
449
409
    }
450
409
  }
451
3.53k
452
3.53k
  if (SparseChunks[SectionNumber] == PendingComdat)
453
334
    return None;
454
3.20k
  return createRegular(Sym);
455
3.20k
}
456
457
600
MachineTypes ObjFile::getMachineType() {
458
600
  if (COFFObj)
459
600
    return static_cast<MachineTypes>(COFFObj->getMachine());
460
0
  return IMAGE_FILE_MACHINE_UNKNOWN;
461
0
}
462
463
21
StringRef ltrim1(StringRef S, const char *Chars) {
464
21
  if (!S.empty() && strchr(Chars, S[0]))
465
21
    return S.substr(1);
466
0
  return S;
467
0
}
468
469
99
void ImportFile::parse() {
470
99
  const char *Buf = MB.getBufferStart();
471
99
  const char *End = MB.getBufferEnd();
472
99
  const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf);
473
99
474
99
  // Check if the total size is valid.
475
99
  if ((size_t)(End - Buf) != (sizeof(*Hdr) + Hdr->SizeOfData))
476
0
    fatal("broken import library");
477
99
478
99
  // Read names and create an __imp_ symbol.
479
99
  StringRef Name = Saver.save(StringRef(Buf + sizeof(*Hdr)));
480
99
  StringRef ImpName = Saver.save("__imp_" + Name);
481
99
  const char *NameStart = Buf + sizeof(coff_import_header) + Name.size() + 1;
482
99
  DLLName = StringRef(NameStart);
483
99
  StringRef ExtName;
484
99
  switch (Hdr->getNameType()) {
485
99
  case IMPORT_ORDINAL:
486
12
    ExtName = "";
487
12
    break;
488
99
  case IMPORT_NAME:
489
66
    ExtName = Name;
490
66
    break;
491
99
  case IMPORT_NAME_NOPREFIX:
492
2
    ExtName = ltrim1(Name, "?@_");
493
2
    break;
494
99
  case IMPORT_NAME_UNDECORATE:
495
19
    ExtName = ltrim1(Name, "?@_");
496
19
    ExtName = ExtName.substr(0, ExtName.find('@'));
497
19
    break;
498
99
  }
499
99
500
99
  this->Hdr = Hdr;
501
99
  ExternalName = ExtName;
502
99
503
99
  ImpSym = Symtab->addImportData(ImpName, this);
504
99
  // If this was a duplicate, we logged an error but may continue;
505
99
  // in this case, ImpSym is nullptr.
506
99
  if (!ImpSym)
507
2
    return;
508
97
509
97
  if (Hdr->getType() == llvm::COFF::IMPORT_CONST)
510
1
    static_cast<void>(Symtab->addImportData(Name, this));
511
97
512
97
  // If type is function, we need to create a thunk which jump to an
513
97
  // address pointed by the __imp_ symbol. (This allows you to call
514
97
  // DLL functions just like regular non-DLL functions.)
515
97
  if (Hdr->getType() == llvm::COFF::IMPORT_CODE)
516
89
    ThunkSym = Symtab->addImportThunk(
517
89
        Name, cast_or_null<DefinedImportData>(ImpSym), Hdr->Machine);
518
97
}
519
520
54
void BitcodeFile::parse() {
521
54
  Obj = check(lto::InputFile::create(MemoryBufferRef(
522
54
      MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
523
54
  std::vector<std::pair<Symbol *, bool>> Comdat(Obj->getComdatTable().size());
524
77
  for (size_t I = 0; I != Obj->getComdatTable().size(); 
++I23
)
525
23
    Comdat[I] = Symtab->addComdat(this, Saver.save(Obj->getComdatTable()[I]));
526
110
  for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) {
527
110
    StringRef SymName = Saver.save(ObjSym.getName());
528
110
    int ComdatIndex = ObjSym.getComdatIndex();
529
110
    Symbol *Sym;
530
110
    if (ObjSym.isUndefined()) {
531
22
      Sym = Symtab->addUndefined(SymName, this, false);
532
88
    } else if (ObjSym.isCommon()) {
533
0
      Sym = Symtab->addCommon(this, SymName, ObjSym.getCommonSize());
534
88
    } else if (ObjSym.isWeak() && 
ObjSym.isIndirect()17
) {
535
2
      // Weak external.
536
2
      Sym = Symtab->addUndefined(SymName, this, true);
537
2
      std::string Fallback = ObjSym.getCOFFWeakExternalFallback();
538
2
      Symbol *Alias = Symtab->addUndefined(Saver.save(Fallback));
539
2
      checkAndSetWeakAlias(Symtab, this, Sym, Alias);
540
86
    } else if (ComdatIndex != -1) {
541
23
      if (SymName == Obj->getComdatTable()[ComdatIndex])
542
23
        Sym = Comdat[ComdatIndex].first;
543
0
      else if (Comdat[ComdatIndex].second)
544
0
        Sym = Symtab->addRegular(this, SymName);
545
0
      else
546
0
        Sym = Symtab->addUndefined(SymName, this, false);
547
63
    } else {
548
63
      Sym = Symtab->addRegular(this, SymName);
549
63
    }
550
110
    Symbols.push_back(Sym);
551
110
  }
552
54
  Directives = Obj->getCOFFLinkerOpts();
553
54
}
554
555
54
MachineTypes BitcodeFile::getMachineType() {
556
54
  switch (Triple(Obj->getTargetTriple()).getArch()) {
557
54
  case Triple::x86_64:
558
52
    return AMD64;
559
54
  case Triple::x86:
560
2
    return I386;
561
54
  case Triple::arm:
562
0
    return ARMNT;
563
54
  case Triple::aarch64:
564
0
    return ARM64;
565
54
  default:
566
0
    return IMAGE_FILE_MACHINE_UNKNOWN;
567
54
  }
568
54
}
569
} // namespace coff
570
} // namespace lld
571
572
// Returns the last element of a path, which is supposed to be a filename.
573
198
static StringRef getBasename(StringRef Path) {
574
198
  return sys::path::filename(Path, sys::path::Style::windows);
575
198
}
576
577
// Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
578
1.09k
std::string lld::toString(const coff::InputFile *File) {
579
1.09k
  if (!File)
580
0
    return "<internal>";
581
1.09k
  if (File->ParentName.empty())
582
993
    return File->getName();
583
99
584
99
  return (getBasename(File->ParentName) + "(" + getBasename(File->getName()) +
585
99
          ")")
586
99
      .str();
587
99
}