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