1 //===- InputFiles.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 "InputFiles.h"
10 #include "Chunks.h"
11 #include "Config.h"
12 #include "DebugTypes.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/DebugInfo/CodeView/DebugSubsectionRecord.h"
24 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
25 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
26 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
27 #include "llvm/Object/Binary.h"
28 #include "llvm/Object/COFF.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/Endian.h"
31 #include "llvm/Support/Error.h"
32 #include "llvm/Support/ErrorOr.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/Path.h"
35 #include "llvm/Target/TargetOptions.h"
36 #include <cstring>
37 #include <system_error>
38 #include <utility>
39 
40 using namespace llvm;
41 using namespace llvm::COFF;
42 using namespace llvm::codeview;
43 using namespace llvm::object;
44 using namespace llvm::support::endian;
45 
46 using llvm::Triple;
47 using llvm::support::ulittle32_t;
48 
49 namespace lld {
50 namespace coff {
51 
52 std::vector<ObjFile *> ObjFile::Instances;
53 std::vector<ImportFile *> ImportFile::Instances;
54 std::vector<BitcodeFile *> BitcodeFile::Instances;
55 
56 /// Checks that Source is compatible with being a weak alias to Target.
57 /// If Source is Undefined and has no weak alias set, makes it a weak
58 /// alias to Target.
59 static void checkAndSetWeakAlias(SymbolTable *Symtab, InputFile *F,
60                                  Symbol *Source, Symbol *Target) {
61   if (auto *U = dyn_cast<Undefined>(Source)) {
62     if (U->WeakAlias && U->WeakAlias != Target) {
63       // Weak aliases as produced by GCC are named in the form
64       // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
65       // of another symbol emitted near the weak symbol.
66       // Just use the definition from the first object file that defined
67       // this weak symbol.
68       if (Config->MinGW)
69         return;
70       Symtab->reportDuplicate(Source, F);
71     }
72     U->WeakAlias = Target;
73   }
74 }
75 
76 ArchiveFile::ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
77 
78 void ArchiveFile::parse() {
79   // Parse a MemoryBufferRef as an archive file.
80   File = CHECK(Archive::create(MB), this);
81 
82   // Read the symbol table to construct Lazy objects.
83   for (const Archive::Symbol &Sym : File->symbols())
84     Symtab->addLazy(this, Sym);
85 }
86 
87 // Returns a buffer pointing to a member file containing a given symbol.
88 void ArchiveFile::addMember(const Archive::Symbol *Sym) {
89   const Archive::Child &C =
90       CHECK(Sym->getMember(),
91             "could not get the member for symbol " + Sym->getName());
92 
93   // Return an empty buffer if we have already returned the same buffer.
94   if (!Seen.insert(C.getChildOffset()).second)
95     return;
96 
97   Driver->enqueueArchiveMember(C, Sym->getName(), getName());
98 }
99 
100 std::vector<MemoryBufferRef> getArchiveMembers(Archive *File) {
101   std::vector<MemoryBufferRef> V;
102   Error Err = Error::success();
103   for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
104     Archive::Child C =
105         CHECK(COrErr,
106               File->getFileName() + ": could not get the child of the archive");
107     MemoryBufferRef MBRef =
108         CHECK(C.getMemoryBufferRef(),
109               File->getFileName() +
110                   ": could not get the buffer for a child of the archive");
111     V.push_back(MBRef);
112   }
113   if (Err)
114     fatal(File->getFileName() +
115           ": Archive::children failed: " + toString(std::move(Err)));
116   return V;
117 }
118 
119 void ObjFile::parse() {
120   // Parse a memory buffer as a COFF file.
121   std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), this);
122 
123   if (auto *Obj = dyn_cast<COFFObjectFile>(Bin.get())) {
124     Bin.release();
125     COFFObj.reset(Obj);
126   } else {
127     fatal(toString(this) + " is not a COFF file");
128   }
129 
130   // Read section and symbol tables.
131   initializeChunks();
132   initializeSymbols();
133   initializeFlags();
134   initializeDependencies();
135 }
136 
137 const coff_section* ObjFile::getSection(uint32_t I) {
138   const coff_section *Sec;
139   if (auto EC = COFFObj->getSection(I, Sec))
140     fatal("getSection failed: #" + Twine(I) + ": " + EC.message());
141   return Sec;
142 }
143 
144 // We set SectionChunk pointers in the SparseChunks vector to this value
145 // temporarily to mark comdat sections as having an unknown resolution. As we
146 // walk the object file's symbol table, once we visit either a leader symbol or
147 // an associative section definition together with the parent comdat's leader,
148 // we set the pointer to either nullptr (to mark the section as discarded) or a
149 // valid SectionChunk for that section.
150 static SectionChunk *const PendingComdat = reinterpret_cast<SectionChunk *>(1);
151 
152 void ObjFile::initializeChunks() {
153   uint32_t NumSections = COFFObj->getNumberOfSections();
154   Chunks.reserve(NumSections);
155   SparseChunks.resize(NumSections + 1);
156   for (uint32_t I = 1; I < NumSections + 1; ++I) {
157     const coff_section *Sec = getSection(I);
158     if (Sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
159       SparseChunks[I] = PendingComdat;
160     else
161       SparseChunks[I] = readSection(I, nullptr, "");
162   }
163 }
164 
165 SectionChunk *ObjFile::readSection(uint32_t SectionNumber,
166                                    const coff_aux_section_definition *Def,
167                                    StringRef LeaderName) {
168   const coff_section *Sec = getSection(SectionNumber);
169 
170   StringRef Name;
171   if (Expected<StringRef> E = COFFObj->getSectionName(Sec))
172     Name = *E;
173   else
174     fatal("getSectionName failed: #" + Twine(SectionNumber) + ": " +
175           toString(E.takeError()));
176 
177   if (Name == ".drectve") {
178     ArrayRef<uint8_t> Data;
179     cantFail(COFFObj->getSectionContents(Sec, Data));
180     Directives = StringRef((const char *)Data.data(), Data.size());
181     return nullptr;
182   }
183 
184   if (Name == ".llvm_addrsig") {
185     AddrsigSec = Sec;
186     return nullptr;
187   }
188 
189   // Object files may have DWARF debug info or MS CodeView debug info
190   // (or both).
191   //
192   // DWARF sections don't need any special handling from the perspective
193   // of the linker; they are just a data section containing relocations.
194   // We can just link them to complete debug info.
195   //
196   // CodeView needs linker support. We need to interpret debug info,
197   // and then write it to a separate .pdb file.
198 
199   // Ignore DWARF debug info unless /debug is given.
200   if (!Config->Debug && Name.startswith(".debug_"))
201     return nullptr;
202 
203   if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
204     return nullptr;
205   auto *C = make<SectionChunk>(this, Sec);
206   if (Def)
207     C->Checksum = Def->CheckSum;
208 
209   // CodeView sections are stored to a different vector because they are not
210   // linked in the regular manner.
211   if (C->isCodeView())
212     DebugChunks.push_back(C);
213   else if (Name == ".gfids$y")
214     GuardFidChunks.push_back(C);
215   else if (Name == ".gljmp$y")
216     GuardLJmpChunks.push_back(C);
217   else if (Name == ".sxdata")
218     SXDataChunks.push_back(C);
219   else if (Config->TailMerge && Sec->NumberOfRelocations == 0 &&
220            Name == ".rdata" && LeaderName.startswith("??_C@"))
221     // COFF sections that look like string literal sections (i.e. no
222     // relocations, in .rdata, leader symbol name matches the MSVC name mangling
223     // for string literals) are subject to string tail merging.
224     MergeChunk::addSection(C);
225   else
226     Chunks.push_back(C);
227 
228   return C;
229 }
230 
231 void ObjFile::readAssociativeDefinition(
232     COFFSymbolRef Sym, const coff_aux_section_definition *Def) {
233   readAssociativeDefinition(Sym, Def, Def->getNumber(Sym.isBigObj()));
234 }
235 
236 void ObjFile::readAssociativeDefinition(COFFSymbolRef Sym,
237                                         const coff_aux_section_definition *Def,
238                                         uint32_t ParentIndex) {
239   SectionChunk *Parent = SparseChunks[ParentIndex];
240   int32_t SectionNumber = Sym.getSectionNumber();
241 
242   auto Diag = [&]() {
243     StringRef Name, ParentName;
244     COFFObj->getSymbolName(Sym, Name);
245 
246     const coff_section *ParentSec = getSection(ParentIndex);
247     if (Expected<StringRef> E = COFFObj->getSectionName(ParentSec))
248       ParentName = *E;
249     error(toString(this) + ": associative comdat " + Name + " (sec " +
250           Twine(SectionNumber) + ") has invalid reference to section " +
251           ParentName + " (sec " + Twine(ParentIndex) + ")");
252   };
253 
254   if (Parent == PendingComdat) {
255     // This can happen if an associative comdat refers to another associative
256     // comdat that appears after it (invalid per COFF spec) or to a section
257     // without any symbols.
258     Diag();
259     return;
260   }
261 
262   // Check whether the parent is prevailing. If it is, so are we, and we read
263   // the section; otherwise mark it as discarded.
264   if (Parent) {
265     SectionChunk *C = readSection(SectionNumber, Def, "");
266     SparseChunks[SectionNumber] = C;
267     if (C) {
268       C->Selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
269       Parent->addAssociative(C);
270     }
271   } else {
272     SparseChunks[SectionNumber] = nullptr;
273   }
274 }
275 
276 void ObjFile::recordPrevailingSymbolForMingw(
277     COFFSymbolRef Sym, DenseMap<StringRef, uint32_t> &PrevailingSectionMap) {
278   // For comdat symbols in executable sections, where this is the copy
279   // of the section chunk we actually include instead of discarding it,
280   // add the symbol to a map to allow using it for implicitly
281   // associating .[px]data$<func> sections to it.
282   int32_t SectionNumber = Sym.getSectionNumber();
283   SectionChunk *SC = SparseChunks[SectionNumber];
284   if (SC && SC->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
285     StringRef Name;
286     COFFObj->getSymbolName(Sym, Name);
287     PrevailingSectionMap[Name] = SectionNumber;
288   }
289 }
290 
291 void ObjFile::maybeAssociateSEHForMingw(
292     COFFSymbolRef Sym, const coff_aux_section_definition *Def,
293     const DenseMap<StringRef, uint32_t> &PrevailingSectionMap) {
294   StringRef Name;
295   COFFObj->getSymbolName(Sym, Name);
296   if (Name.consume_front(".pdata$") || Name.consume_front(".xdata$")) {
297     // For MinGW, treat .[px]data$<func> as implicitly associative to
298     // the symbol <func>.
299     auto ParentSym = PrevailingSectionMap.find(Name);
300     if (ParentSym != PrevailingSectionMap.end())
301       readAssociativeDefinition(Sym, Def, ParentSym->second);
302   }
303 }
304 
305 Symbol *ObjFile::createRegular(COFFSymbolRef Sym) {
306   SectionChunk *SC = SparseChunks[Sym.getSectionNumber()];
307   if (Sym.isExternal()) {
308     StringRef Name;
309     COFFObj->getSymbolName(Sym, Name);
310     if (SC)
311       return Symtab->addRegular(this, Name, Sym.getGeneric(), SC);
312     // For MinGW symbols named .weak.* that point to a discarded section,
313     // don't create an Undefined symbol. If nothing ever refers to the symbol,
314     // everything should be fine. If something actually refers to the symbol
315     // (e.g. the undefined weak alias), linking will fail due to undefined
316     // references at the end.
317     if (Config->MinGW && Name.startswith(".weak."))
318       return nullptr;
319     return Symtab->addUndefined(Name, this, false);
320   }
321   if (SC)
322     return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
323                                 /*IsExternal*/ false, Sym.getGeneric(), SC);
324   return nullptr;
325 }
326 
327 void ObjFile::initializeSymbols() {
328   uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
329   Symbols.resize(NumSymbols);
330 
331   SmallVector<std::pair<Symbol *, uint32_t>, 8> WeakAliases;
332   std::vector<uint32_t> PendingIndexes;
333   PendingIndexes.reserve(NumSymbols);
334 
335   DenseMap<StringRef, uint32_t> PrevailingSectionMap;
336   std::vector<const coff_aux_section_definition *> ComdatDefs(
337       COFFObj->getNumberOfSections() + 1);
338 
339   for (uint32_t I = 0; I < NumSymbols; ++I) {
340     COFFSymbolRef COFFSym = check(COFFObj->getSymbol(I));
341     bool PrevailingComdat;
342     if (COFFSym.isUndefined()) {
343       Symbols[I] = createUndefined(COFFSym);
344     } else if (COFFSym.isWeakExternal()) {
345       Symbols[I] = createUndefined(COFFSym);
346       uint32_t TagIndex = COFFSym.getAux<coff_aux_weak_external>()->TagIndex;
347       WeakAliases.emplace_back(Symbols[I], TagIndex);
348     } else if (Optional<Symbol *> OptSym =
349                    createDefined(COFFSym, ComdatDefs, PrevailingComdat)) {
350       Symbols[I] = *OptSym;
351       if (Config->MinGW && PrevailingComdat)
352         recordPrevailingSymbolForMingw(COFFSym, PrevailingSectionMap);
353     } else {
354       // createDefined() returns None if a symbol belongs to a section that
355       // was pending at the point when the symbol was read. This can happen in
356       // two cases:
357       // 1) section definition symbol for a comdat leader;
358       // 2) symbol belongs to a comdat section associated with another section.
359       // In both of these cases, we can expect the section to be resolved by
360       // the time we finish visiting the remaining symbols in the symbol
361       // table. So we postpone the handling of this symbol until that time.
362       PendingIndexes.push_back(I);
363     }
364     I += COFFSym.getNumberOfAuxSymbols();
365   }
366 
367   for (uint32_t I : PendingIndexes) {
368     COFFSymbolRef Sym = check(COFFObj->getSymbol(I));
369     if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) {
370       if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
371         readAssociativeDefinition(Sym, Def);
372       else if (Config->MinGW)
373         maybeAssociateSEHForMingw(Sym, Def, PrevailingSectionMap);
374     }
375     if (SparseChunks[Sym.getSectionNumber()] == PendingComdat) {
376       StringRef Name;
377       COFFObj->getSymbolName(Sym, Name);
378       log("comdat section " + Name +
379           " without leader and unassociated, discarding");
380       continue;
381     }
382     Symbols[I] = createRegular(Sym);
383   }
384 
385   for (auto &KV : WeakAliases) {
386     Symbol *Sym = KV.first;
387     uint32_t Idx = KV.second;
388     checkAndSetWeakAlias(Symtab, this, Sym, Symbols[Idx]);
389   }
390 }
391 
392 Symbol *ObjFile::createUndefined(COFFSymbolRef Sym) {
393   StringRef Name;
394   COFFObj->getSymbolName(Sym, Name);
395   return Symtab->addUndefined(Name, this, Sym.isWeakExternal());
396 }
397 
398 void ObjFile::handleComdatSelection(COFFSymbolRef Sym, COMDATType &Selection,
399                                     bool &Prevailing, DefinedRegular *Leader) {
400   if (Prevailing)
401     return;
402   // There's already an existing comdat for this symbol: `Leader`.
403   // Use the comdats's selection field to determine if the new
404   // symbol in `Sym` should be discarded, produce a duplicate symbol
405   // error, etc.
406 
407   SectionChunk *LeaderChunk = nullptr;
408   COMDATType LeaderSelection = IMAGE_COMDAT_SELECT_ANY;
409 
410   if (Leader->Data) {
411     LeaderChunk = Leader->getChunk();
412     LeaderSelection = LeaderChunk->Selection;
413   } else {
414     // FIXME: comdats from LTO files don't know their selection; treat them
415     // as "any".
416     Selection = LeaderSelection;
417   }
418 
419   if ((Selection == IMAGE_COMDAT_SELECT_ANY &&
420        LeaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
421       (Selection == IMAGE_COMDAT_SELECT_LARGEST &&
422        LeaderSelection == IMAGE_COMDAT_SELECT_ANY)) {
423     // cl.exe picks "any" for vftables when building with /GR- and
424     // "largest" when building with /GR. To be able to link object files
425     // compiled with each flag, "any" and "largest" are merged as "largest".
426     LeaderSelection = Selection = IMAGE_COMDAT_SELECT_LARGEST;
427   }
428 
429   // Other than that, comdat selections must match.  This is a bit more
430   // strict than link.exe which allows merging "any" and "largest" if "any"
431   // is the first symbol the linker sees, and it allows merging "largest"
432   // with everything (!) if "largest" is the first symbol the linker sees.
433   // Making this symmetric independent of which selection is seen first
434   // seems better though.
435   // (This behavior matches ModuleLinker::getComdatResult().)
436   if (Selection != LeaderSelection) {
437     log(("conflicting comdat type for " + toString(*Leader) + ": " +
438          Twine((int)LeaderSelection) + " in " + toString(Leader->getFile()) +
439          " and " + Twine((int)Selection) + " in " + toString(this))
440             .str());
441     Symtab->reportDuplicate(Leader, this);
442     return;
443   }
444 
445   switch (Selection) {
446   case IMAGE_COMDAT_SELECT_NODUPLICATES:
447     Symtab->reportDuplicate(Leader, this);
448     break;
449 
450   case IMAGE_COMDAT_SELECT_ANY:
451     // Nothing to do.
452     break;
453 
454   case IMAGE_COMDAT_SELECT_SAME_SIZE:
455     if (LeaderChunk->getSize() != getSection(Sym)->SizeOfRawData)
456       Symtab->reportDuplicate(Leader, this);
457     break;
458 
459   case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
460     SectionChunk NewChunk(this, getSection(Sym));
461     // link.exe only compares section contents here and doesn't complain
462     // if the two comdat sections have e.g. different alignment.
463     // Match that.
464     if (LeaderChunk->getContents() != NewChunk.getContents())
465       Symtab->reportDuplicate(Leader, this);
466     break;
467   }
468 
469   case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
470     // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
471     // (This means lld-link doesn't produce duplicate symbol errors for
472     // associative comdats while link.exe does, but associate comdats
473     // are never extern in practice.)
474     llvm_unreachable("createDefined not called for associative comdats");
475 
476   case IMAGE_COMDAT_SELECT_LARGEST:
477     if (LeaderChunk->getSize() < getSection(Sym)->SizeOfRawData) {
478       // Replace the existing comdat symbol with the new one.
479       StringRef Name;
480       COFFObj->getSymbolName(Sym, Name);
481       // FIXME: This is incorrect: With /opt:noref, the previous sections
482       // make it into the final executable as well. Correct handling would
483       // be to undo reading of the whole old section that's being replaced,
484       // or doing one pass that determines what the final largest comdat
485       // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
486       // only the largest one.
487       replaceSymbol<DefinedRegular>(Leader, this, Name, /*IsCOMDAT*/ true,
488                                     /*IsExternal*/ true, Sym.getGeneric(),
489                                     nullptr);
490       Prevailing = true;
491     }
492     break;
493 
494   case IMAGE_COMDAT_SELECT_NEWEST:
495     llvm_unreachable("should have been rejected earlier");
496   }
497 }
498 
499 Optional<Symbol *> ObjFile::createDefined(
500     COFFSymbolRef Sym,
501     std::vector<const coff_aux_section_definition *> &ComdatDefs,
502     bool &Prevailing) {
503   Prevailing = false;
504   auto GetName = [&]() {
505     StringRef S;
506     COFFObj->getSymbolName(Sym, S);
507     return S;
508   };
509 
510   if (Sym.isCommon()) {
511     auto *C = make<CommonChunk>(Sym);
512     Chunks.push_back(C);
513     return Symtab->addCommon(this, GetName(), Sym.getValue(), Sym.getGeneric(),
514                              C);
515   }
516 
517   if (Sym.isAbsolute()) {
518     StringRef Name = GetName();
519 
520     // Skip special symbols.
521     if (Name == "@comp.id")
522       return nullptr;
523     if (Name == "@feat.00") {
524       Feat00Flags = Sym.getValue();
525       return nullptr;
526     }
527 
528     if (Sym.isExternal())
529       return Symtab->addAbsolute(Name, Sym);
530     return make<DefinedAbsolute>(Name, Sym);
531   }
532 
533   int32_t SectionNumber = Sym.getSectionNumber();
534   if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
535     return nullptr;
536 
537   if (llvm::COFF::isReservedSectionNumber(SectionNumber))
538     fatal(toString(this) + ": " + GetName() +
539           " should not refer to special section " + Twine(SectionNumber));
540 
541   if ((uint32_t)SectionNumber >= SparseChunks.size())
542     fatal(toString(this) + ": " + GetName() +
543           " should not refer to non-existent section " + Twine(SectionNumber));
544 
545   // Comdat handling.
546   // A comdat symbol consists of two symbol table entries.
547   // The first symbol entry has the name of the section (e.g. .text), fixed
548   // values for the other fields, and one auxilliary record.
549   // The second symbol entry has the name of the comdat symbol, called the
550   // "comdat leader".
551   // When this function is called for the first symbol entry of a comdat,
552   // it sets ComdatDefs and returns None, and when it's called for the second
553   // symbol entry it reads ComdatDefs and then sets it back to nullptr.
554 
555   // Handle comdat leader.
556   if (const coff_aux_section_definition *Def = ComdatDefs[SectionNumber]) {
557     ComdatDefs[SectionNumber] = nullptr;
558     DefinedRegular *Leader;
559 
560     if (Sym.isExternal()) {
561       std::tie(Leader, Prevailing) =
562           Symtab->addComdat(this, GetName(), Sym.getGeneric());
563     } else {
564       Leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
565                                     /*IsExternal*/ false, Sym.getGeneric());
566       Prevailing = true;
567     }
568 
569     if (Def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
570         // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
571         // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
572         Def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
573       fatal("unknown comdat type " + std::to_string((int)Def->Selection) +
574             " for " + GetName() + " in " + toString(this));
575     }
576     COMDATType Selection = (COMDATType)Def->Selection;
577 
578     if (Leader->isCOMDAT())
579       handleComdatSelection(Sym, Selection, Prevailing, Leader);
580 
581     if (Prevailing) {
582       SectionChunk *C = readSection(SectionNumber, Def, GetName());
583       SparseChunks[SectionNumber] = C;
584       C->Sym = cast<DefinedRegular>(Leader);
585       C->Selection = Selection;
586       cast<DefinedRegular>(Leader)->Data = &C->Repl;
587     } else {
588       SparseChunks[SectionNumber] = nullptr;
589     }
590     return Leader;
591   }
592 
593   // Prepare to handle the comdat leader symbol by setting the section's
594   // ComdatDefs pointer if we encounter a non-associative comdat.
595   if (SparseChunks[SectionNumber] == PendingComdat) {
596     if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) {
597       if (Def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
598         ComdatDefs[SectionNumber] = Def;
599     }
600     return None;
601   }
602 
603   return createRegular(Sym);
604 }
605 
606 MachineTypes ObjFile::getMachineType() {
607   if (COFFObj)
608     return static_cast<MachineTypes>(COFFObj->getMachine());
609   return IMAGE_FILE_MACHINE_UNKNOWN;
610 }
611 
612 ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef SecName) {
613   if (SectionChunk *Sec = SectionChunk::findByName(DebugChunks, SecName))
614     return Sec->consumeDebugMagic();
615   return {};
616 }
617 
618 // OBJ files systematically store critical informations in a .debug$S stream,
619 // even if the TU was compiled with no debug info. At least two records are
620 // always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
621 // PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
622 // currently used to initialize the HotPatchable member.
623 void ObjFile::initializeFlags() {
624   ArrayRef<uint8_t> Data = getDebugSection(".debug$S");
625   if (Data.empty())
626     return;
627 
628   DebugSubsectionArray Subsections;
629 
630   BinaryStreamReader Reader(Data, support::little);
631   ExitOnError ExitOnErr;
632   ExitOnErr(Reader.readArray(Subsections, Data.size()));
633 
634   for (const DebugSubsectionRecord &SS : Subsections) {
635     if (SS.kind() != DebugSubsectionKind::Symbols)
636       continue;
637 
638     unsigned Offset = 0;
639 
640     // Only parse the first two records. We are only looking for S_OBJNAME
641     // and S_COMPILE3, and they usually appear at the beginning of the
642     // stream.
643     for (unsigned I = 0; I < 2; ++I) {
644       Expected<CVSymbol> Sym = readSymbolFromStream(SS.getRecordData(), Offset);
645       if (!Sym) {
646         consumeError(Sym.takeError());
647         return;
648       }
649       if (Sym->kind() == SymbolKind::S_COMPILE3) {
650         auto CS =
651             cantFail(SymbolDeserializer::deserializeAs<Compile3Sym>(Sym.get()));
652         HotPatchable =
653             (CS.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
654       }
655       if (Sym->kind() == SymbolKind::S_OBJNAME) {
656         auto ObjName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
657             Sym.get()));
658         PCHSignature = ObjName.Signature;
659       }
660       Offset += Sym->length();
661     }
662   }
663 }
664 
665 // Depending on the compilation flags, OBJs can refer to external files,
666 // necessary to merge this OBJ into the final PDB. We currently support two
667 // types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
668 // And PDB type servers, when compiling with /Zi. This function extracts these
669 // dependencies and makes them available as a TpiSource interface (see
670 // DebugTypes.h).
671 void ObjFile::initializeDependencies() {
672   if (!Config->Debug)
673     return;
674 
675   bool IsPCH = false;
676 
677   ArrayRef<uint8_t> Data = getDebugSection(".debug$P");
678   if (!Data.empty())
679     IsPCH = true;
680   else
681     Data = getDebugSection(".debug$T");
682 
683   if (Data.empty())
684     return;
685 
686   CVTypeArray Types;
687   BinaryStreamReader Reader(Data, support::little);
688   cantFail(Reader.readArray(Types, Reader.getLength()));
689 
690   CVTypeArray::Iterator FirstType = Types.begin();
691   if (FirstType == Types.end())
692     return;
693 
694   DebugTypes.emplace(Types);
695 
696   if (IsPCH) {
697     DebugTypesObj = makePrecompSource(this);
698     return;
699   }
700 
701   if (FirstType->kind() == LF_TYPESERVER2) {
702     TypeServer2Record TS = cantFail(
703         TypeDeserializer::deserializeAs<TypeServer2Record>(FirstType->data()));
704     DebugTypesObj = makeUseTypeServerSource(this, &TS);
705     return;
706   }
707 
708   if (FirstType->kind() == LF_PRECOMP) {
709     PrecompRecord Precomp = cantFail(
710         TypeDeserializer::deserializeAs<PrecompRecord>(FirstType->data()));
711     DebugTypesObj = makeUsePrecompSource(this, &Precomp);
712     return;
713   }
714 
715   DebugTypesObj = makeTpiSource(this);
716 }
717 
718 StringRef ltrim1(StringRef S, const char *Chars) {
719   if (!S.empty() && strchr(Chars, S[0]))
720     return S.substr(1);
721   return S;
722 }
723 
724 void ImportFile::parse() {
725   const char *Buf = MB.getBufferStart();
726   const char *End = MB.getBufferEnd();
727   const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf);
728 
729   // Check if the total size is valid.
730   if ((size_t)(End - Buf) != (sizeof(*Hdr) + Hdr->SizeOfData))
731     fatal("broken import library");
732 
733   // Read names and create an __imp_ symbol.
734   StringRef Name = Saver.save(StringRef(Buf + sizeof(*Hdr)));
735   StringRef ImpName = Saver.save("__imp_" + Name);
736   const char *NameStart = Buf + sizeof(coff_import_header) + Name.size() + 1;
737   DLLName = StringRef(NameStart);
738   StringRef ExtName;
739   switch (Hdr->getNameType()) {
740   case IMPORT_ORDINAL:
741     ExtName = "";
742     break;
743   case IMPORT_NAME:
744     ExtName = Name;
745     break;
746   case IMPORT_NAME_NOPREFIX:
747     ExtName = ltrim1(Name, "?@_");
748     break;
749   case IMPORT_NAME_UNDECORATE:
750     ExtName = ltrim1(Name, "?@_");
751     ExtName = ExtName.substr(0, ExtName.find('@'));
752     break;
753   }
754 
755   this->Hdr = Hdr;
756   ExternalName = ExtName;
757 
758   ImpSym = Symtab->addImportData(ImpName, this);
759   // If this was a duplicate, we logged an error but may continue;
760   // in this case, ImpSym is nullptr.
761   if (!ImpSym)
762     return;
763 
764   if (Hdr->getType() == llvm::COFF::IMPORT_CONST)
765     static_cast<void>(Symtab->addImportData(Name, this));
766 
767   // If type is function, we need to create a thunk which jump to an
768   // address pointed by the __imp_ symbol. (This allows you to call
769   // DLL functions just like regular non-DLL functions.)
770   if (Hdr->getType() == llvm::COFF::IMPORT_CODE)
771     ThunkSym = Symtab->addImportThunk(
772         Name, cast_or_null<DefinedImportData>(ImpSym), Hdr->Machine);
773 }
774 
775 BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
776                          uint64_t OffsetInArchive)
777     : InputFile(BitcodeKind, MB) {
778   std::string Path = MB.getBufferIdentifier().str();
779 
780   // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
781   // name. If two archives define two members with the same name, this
782   // causes a collision which result in only one of the objects being taken
783   // into consideration at LTO time (which very likely causes undefined
784   // symbols later in the link stage). So we append file offset to make
785   // filename unique.
786   MemoryBufferRef MBRef(
787       MB.getBuffer(),
788       Saver.save(ArchiveName + Path +
789                  (ArchiveName.empty() ? "" : utostr(OffsetInArchive))));
790 
791   Obj = check(lto::InputFile::create(MBRef));
792 }
793 
794 void BitcodeFile::parse() {
795   std::vector<std::pair<Symbol *, bool>> Comdat(Obj->getComdatTable().size());
796   for (size_t I = 0; I != Obj->getComdatTable().size(); ++I)
797     // FIXME: lto::InputFile doesn't keep enough data to do correct comdat
798     // selection handling.
799     Comdat[I] = Symtab->addComdat(this, Saver.save(Obj->getComdatTable()[I]));
800   for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) {
801     StringRef SymName = Saver.save(ObjSym.getName());
802     int ComdatIndex = ObjSym.getComdatIndex();
803     Symbol *Sym;
804     if (ObjSym.isUndefined()) {
805       Sym = Symtab->addUndefined(SymName, this, false);
806     } else if (ObjSym.isCommon()) {
807       Sym = Symtab->addCommon(this, SymName, ObjSym.getCommonSize());
808     } else if (ObjSym.isWeak() && ObjSym.isIndirect()) {
809       // Weak external.
810       Sym = Symtab->addUndefined(SymName, this, true);
811       std::string Fallback = ObjSym.getCOFFWeakExternalFallback();
812       Symbol *Alias = Symtab->addUndefined(Saver.save(Fallback));
813       checkAndSetWeakAlias(Symtab, this, Sym, Alias);
814     } else if (ComdatIndex != -1) {
815       if (SymName == Obj->getComdatTable()[ComdatIndex])
816         Sym = Comdat[ComdatIndex].first;
817       else if (Comdat[ComdatIndex].second)
818         Sym = Symtab->addRegular(this, SymName);
819       else
820         Sym = Symtab->addUndefined(SymName, this, false);
821     } else {
822       Sym = Symtab->addRegular(this, SymName);
823     }
824     Symbols.push_back(Sym);
825     if (ObjSym.isUsed())
826       Config->GCRoot.push_back(Sym);
827   }
828   Directives = Obj->getCOFFLinkerOpts();
829 }
830 
831 MachineTypes BitcodeFile::getMachineType() {
832   switch (Triple(Obj->getTargetTriple()).getArch()) {
833   case Triple::x86_64:
834     return AMD64;
835   case Triple::x86:
836     return I386;
837   case Triple::arm:
838     return ARMNT;
839   case Triple::aarch64:
840     return ARM64;
841   default:
842     return IMAGE_FILE_MACHINE_UNKNOWN;
843   }
844 }
845 } // namespace coff
846 } // namespace lld
847 
848 // Returns the last element of a path, which is supposed to be a filename.
849 static StringRef getBasename(StringRef Path) {
850   return sys::path::filename(Path, sys::path::Style::windows);
851 }
852 
853 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
854 std::string lld::toString(const coff::InputFile *File) {
855   if (!File)
856     return "<internal>";
857   if (File->ParentName.empty() || File->kind() == coff::InputFile::ImportKind)
858     return File->getName();
859 
860   return (getBasename(File->ParentName) + "(" + getBasename(File->getName()) +
861           ")")
862       .str();
863 }
864