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