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