Coverage Report

Created: 2018-08-19 21:11

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