xref: /freebsd/contrib/llvm-project/lld/COFF/InputFiles.cpp (revision 77013d11e6483b970af25e13c9b892075742f7e5)
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/DWARF.h"
17 #include "lld/Common/ErrorHandler.h"
18 #include "lld/Common/Memory.h"
19 #include "llvm-c/lto.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/BinaryFormat/COFF.h"
24 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
25 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
26 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
27 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
28 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
29 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
30 #include "llvm/LTO/LTO.h"
31 #include "llvm/Object/Binary.h"
32 #include "llvm/Object/COFF.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/Error.h"
36 #include "llvm/Support/ErrorOr.h"
37 #include "llvm/Support/FileSystem.h"
38 #include "llvm/Support/Path.h"
39 #include "llvm/Target/TargetOptions.h"
40 #include <cstring>
41 #include <system_error>
42 #include <utility>
43 
44 using namespace llvm;
45 using namespace llvm::COFF;
46 using namespace llvm::codeview;
47 using namespace llvm::object;
48 using namespace llvm::support::endian;
49 using namespace lld;
50 using namespace lld::coff;
51 
52 using llvm::Triple;
53 using llvm::support::ulittle32_t;
54 
55 // Returns the last element of a path, which is supposed to be a filename.
56 static StringRef getBasename(StringRef path) {
57   return sys::path::filename(path, sys::path::Style::windows);
58 }
59 
60 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
61 std::string lld::toString(const coff::InputFile *file) {
62   if (!file)
63     return "<internal>";
64   if (file->parentName.empty() || file->kind() == coff::InputFile::ImportKind)
65     return std::string(file->getName());
66 
67   return (getBasename(file->parentName) + "(" + getBasename(file->getName()) +
68           ")")
69       .str();
70 }
71 
72 std::vector<ObjFile *> ObjFile::instances;
73 std::map<std::string, PDBInputFile *> PDBInputFile::instances;
74 std::vector<ImportFile *> ImportFile::instances;
75 std::vector<BitcodeFile *> BitcodeFile::instances;
76 
77 /// Checks that Source is compatible with being a weak alias to Target.
78 /// If Source is Undefined and has no weak alias set, makes it a weak
79 /// alias to Target.
80 static void checkAndSetWeakAlias(SymbolTable *symtab, InputFile *f,
81                                  Symbol *source, Symbol *target) {
82   if (auto *u = dyn_cast<Undefined>(source)) {
83     if (u->weakAlias && u->weakAlias != target) {
84       // Weak aliases as produced by GCC are named in the form
85       // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
86       // of another symbol emitted near the weak symbol.
87       // Just use the definition from the first object file that defined
88       // this weak symbol.
89       if (config->mingw)
90         return;
91       symtab->reportDuplicate(source, f);
92     }
93     u->weakAlias = target;
94   }
95 }
96 
97 static bool ignoredSymbolName(StringRef name) {
98   return name == "@feat.00" || name == "@comp.id";
99 }
100 
101 ArchiveFile::ArchiveFile(MemoryBufferRef m) : InputFile(ArchiveKind, m) {}
102 
103 void ArchiveFile::parse() {
104   // Parse a MemoryBufferRef as an archive file.
105   file = CHECK(Archive::create(mb), this);
106 
107   // Read the symbol table to construct Lazy objects.
108   for (const Archive::Symbol &sym : file->symbols())
109     symtab->addLazyArchive(this, sym);
110 }
111 
112 // Returns a buffer pointing to a member file containing a given symbol.
113 void ArchiveFile::addMember(const Archive::Symbol &sym) {
114   const Archive::Child &c =
115       CHECK(sym.getMember(),
116             "could not get the member for symbol " + toCOFFString(sym));
117 
118   // Return an empty buffer if we have already returned the same buffer.
119   if (!seen.insert(c.getChildOffset()).second)
120     return;
121 
122   driver->enqueueArchiveMember(c, sym, getName());
123 }
124 
125 std::vector<MemoryBufferRef> lld::coff::getArchiveMembers(Archive *file) {
126   std::vector<MemoryBufferRef> v;
127   Error err = Error::success();
128   for (const Archive::Child &c : file->children(err)) {
129     MemoryBufferRef mbref =
130         CHECK(c.getMemoryBufferRef(),
131               file->getFileName() +
132                   ": could not get the buffer for a child of the archive");
133     v.push_back(mbref);
134   }
135   if (err)
136     fatal(file->getFileName() +
137           ": Archive::children failed: " + toString(std::move(err)));
138   return v;
139 }
140 
141 void LazyObjFile::fetch() {
142   if (mb.getBuffer().empty())
143     return;
144 
145   InputFile *file;
146   if (isBitcode(mb))
147     file = make<BitcodeFile>(mb, "", 0, std::move(symbols));
148   else
149     file = make<ObjFile>(mb, std::move(symbols));
150   mb = {};
151   symtab->addFile(file);
152 }
153 
154 void LazyObjFile::parse() {
155   if (isBitcode(this->mb)) {
156     // Bitcode file.
157     std::unique_ptr<lto::InputFile> obj =
158         CHECK(lto::InputFile::create(this->mb), this);
159     for (const lto::InputFile::Symbol &sym : obj->symbols()) {
160       if (!sym.isUndefined())
161         symtab->addLazyObject(this, sym.getName());
162     }
163     return;
164   }
165 
166   // Native object file.
167   std::unique_ptr<Binary> coffObjPtr = CHECK(createBinary(mb), this);
168   COFFObjectFile *coffObj = cast<COFFObjectFile>(coffObjPtr.get());
169   uint32_t numSymbols = coffObj->getNumberOfSymbols();
170   for (uint32_t i = 0; i < numSymbols; ++i) {
171     COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
172     if (coffSym.isUndefined() || !coffSym.isExternal() ||
173         coffSym.isWeakExternal())
174       continue;
175     StringRef name = check(coffObj->getSymbolName(coffSym));
176     if (coffSym.isAbsolute() && ignoredSymbolName(name))
177       continue;
178     symtab->addLazyObject(this, name);
179     i += coffSym.getNumberOfAuxSymbols();
180   }
181 }
182 
183 void ObjFile::parse() {
184   // Parse a memory buffer as a COFF file.
185   std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
186 
187   if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
188     bin.release();
189     coffObj.reset(obj);
190   } else {
191     fatal(toString(this) + " is not a COFF file");
192   }
193 
194   // Read section and symbol tables.
195   initializeChunks();
196   initializeSymbols();
197   initializeFlags();
198   initializeDependencies();
199 }
200 
201 const coff_section *ObjFile::getSection(uint32_t i) {
202   auto sec = coffObj->getSection(i);
203   if (!sec)
204     fatal("getSection failed: #" + Twine(i) + ": " + toString(sec.takeError()));
205   return *sec;
206 }
207 
208 // We set SectionChunk pointers in the SparseChunks vector to this value
209 // temporarily to mark comdat sections as having an unknown resolution. As we
210 // walk the object file's symbol table, once we visit either a leader symbol or
211 // an associative section definition together with the parent comdat's leader,
212 // we set the pointer to either nullptr (to mark the section as discarded) or a
213 // valid SectionChunk for that section.
214 static SectionChunk *const pendingComdat = reinterpret_cast<SectionChunk *>(1);
215 
216 void ObjFile::initializeChunks() {
217   uint32_t numSections = coffObj->getNumberOfSections();
218   sparseChunks.resize(numSections + 1);
219   for (uint32_t i = 1; i < numSections + 1; ++i) {
220     const coff_section *sec = getSection(i);
221     if (sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
222       sparseChunks[i] = pendingComdat;
223     else
224       sparseChunks[i] = readSection(i, nullptr, "");
225   }
226 }
227 
228 SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
229                                    const coff_aux_section_definition *def,
230                                    StringRef leaderName) {
231   const coff_section *sec = getSection(sectionNumber);
232 
233   StringRef name;
234   if (Expected<StringRef> e = coffObj->getSectionName(sec))
235     name = *e;
236   else
237     fatal("getSectionName failed: #" + Twine(sectionNumber) + ": " +
238           toString(e.takeError()));
239 
240   if (name == ".drectve") {
241     ArrayRef<uint8_t> data;
242     cantFail(coffObj->getSectionContents(sec, data));
243     directives = StringRef((const char *)data.data(), data.size());
244     return nullptr;
245   }
246 
247   if (name == ".llvm_addrsig") {
248     addrsigSec = sec;
249     return nullptr;
250   }
251 
252   if (name == ".llvm.call-graph-profile") {
253     callgraphSec = sec;
254     return nullptr;
255   }
256 
257   // Object files may have DWARF debug info or MS CodeView debug info
258   // (or both).
259   //
260   // DWARF sections don't need any special handling from the perspective
261   // of the linker; they are just a data section containing relocations.
262   // We can just link them to complete debug info.
263   //
264   // CodeView needs linker support. We need to interpret debug info,
265   // and then write it to a separate .pdb file.
266 
267   // Ignore DWARF debug info unless /debug is given.
268   if (!config->debug && name.startswith(".debug_"))
269     return nullptr;
270 
271   if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
272     return nullptr;
273   auto *c = make<SectionChunk>(this, sec);
274   if (def)
275     c->checksum = def->CheckSum;
276 
277   // CodeView sections are stored to a different vector because they are not
278   // linked in the regular manner.
279   if (c->isCodeView())
280     debugChunks.push_back(c);
281   else if (name == ".gfids$y")
282     guardFidChunks.push_back(c);
283   else if (name == ".giats$y")
284     guardIATChunks.push_back(c);
285   else if (name == ".gljmp$y")
286     guardLJmpChunks.push_back(c);
287   else if (name == ".sxdata")
288     sxDataChunks.push_back(c);
289   else if (config->tailMerge && sec->NumberOfRelocations == 0 &&
290            name == ".rdata" && leaderName.startswith("??_C@"))
291     // COFF sections that look like string literal sections (i.e. no
292     // relocations, in .rdata, leader symbol name matches the MSVC name mangling
293     // for string literals) are subject to string tail merging.
294     MergeChunk::addSection(c);
295   else if (name == ".rsrc" || name.startswith(".rsrc$"))
296     resourceChunks.push_back(c);
297   else
298     chunks.push_back(c);
299 
300   return c;
301 }
302 
303 void ObjFile::includeResourceChunks() {
304   chunks.insert(chunks.end(), resourceChunks.begin(), resourceChunks.end());
305 }
306 
307 void ObjFile::readAssociativeDefinition(
308     COFFSymbolRef sym, const coff_aux_section_definition *def) {
309   readAssociativeDefinition(sym, def, def->getNumber(sym.isBigObj()));
310 }
311 
312 void ObjFile::readAssociativeDefinition(COFFSymbolRef sym,
313                                         const coff_aux_section_definition *def,
314                                         uint32_t parentIndex) {
315   SectionChunk *parent = sparseChunks[parentIndex];
316   int32_t sectionNumber = sym.getSectionNumber();
317 
318   auto diag = [&]() {
319     StringRef name = check(coffObj->getSymbolName(sym));
320 
321     StringRef parentName;
322     const coff_section *parentSec = getSection(parentIndex);
323     if (Expected<StringRef> e = coffObj->getSectionName(parentSec))
324       parentName = *e;
325     error(toString(this) + ": associative comdat " + name + " (sec " +
326           Twine(sectionNumber) + ") has invalid reference to section " +
327           parentName + " (sec " + Twine(parentIndex) + ")");
328   };
329 
330   if (parent == pendingComdat) {
331     // This can happen if an associative comdat refers to another associative
332     // comdat that appears after it (invalid per COFF spec) or to a section
333     // without any symbols.
334     diag();
335     return;
336   }
337 
338   // Check whether the parent is prevailing. If it is, so are we, and we read
339   // the section; otherwise mark it as discarded.
340   if (parent) {
341     SectionChunk *c = readSection(sectionNumber, def, "");
342     sparseChunks[sectionNumber] = c;
343     if (c) {
344       c->selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
345       parent->addAssociative(c);
346     }
347   } else {
348     sparseChunks[sectionNumber] = nullptr;
349   }
350 }
351 
352 void ObjFile::recordPrevailingSymbolForMingw(
353     COFFSymbolRef sym, DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
354   // For comdat symbols in executable sections, where this is the copy
355   // of the section chunk we actually include instead of discarding it,
356   // add the symbol to a map to allow using it for implicitly
357   // associating .[px]data$<func> sections to it.
358   // Use the suffix from the .text$<func> instead of the leader symbol
359   // name, for cases where the names differ (i386 mangling/decorations,
360   // cases where the leader is a weak symbol named .weak.func.default*).
361   int32_t sectionNumber = sym.getSectionNumber();
362   SectionChunk *sc = sparseChunks[sectionNumber];
363   if (sc && sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
364     StringRef name = sc->getSectionName().split('$').second;
365     prevailingSectionMap[name] = sectionNumber;
366   }
367 }
368 
369 void ObjFile::maybeAssociateSEHForMingw(
370     COFFSymbolRef sym, const coff_aux_section_definition *def,
371     const DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
372   StringRef name = check(coffObj->getSymbolName(sym));
373   if (name.consume_front(".pdata$") || name.consume_front(".xdata$") ||
374       name.consume_front(".eh_frame$")) {
375     // For MinGW, treat .[px]data$<func> and .eh_frame$<func> as implicitly
376     // associative to the symbol <func>.
377     auto parentSym = prevailingSectionMap.find(name);
378     if (parentSym != prevailingSectionMap.end())
379       readAssociativeDefinition(sym, def, parentSym->second);
380   }
381 }
382 
383 Symbol *ObjFile::createRegular(COFFSymbolRef sym) {
384   SectionChunk *sc = sparseChunks[sym.getSectionNumber()];
385   if (sym.isExternal()) {
386     StringRef name = check(coffObj->getSymbolName(sym));
387     if (sc)
388       return symtab->addRegular(this, name, sym.getGeneric(), sc,
389                                 sym.getValue());
390     // For MinGW symbols named .weak.* that point to a discarded section,
391     // don't create an Undefined symbol. If nothing ever refers to the symbol,
392     // everything should be fine. If something actually refers to the symbol
393     // (e.g. the undefined weak alias), linking will fail due to undefined
394     // references at the end.
395     if (config->mingw && name.startswith(".weak."))
396       return nullptr;
397     return symtab->addUndefined(name, this, false);
398   }
399   if (sc)
400     return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
401                                 /*IsExternal*/ false, sym.getGeneric(), sc);
402   return nullptr;
403 }
404 
405 void ObjFile::initializeSymbols() {
406   uint32_t numSymbols = coffObj->getNumberOfSymbols();
407   symbols.resize(numSymbols);
408 
409   SmallVector<std::pair<Symbol *, uint32_t>, 8> weakAliases;
410   std::vector<uint32_t> pendingIndexes;
411   pendingIndexes.reserve(numSymbols);
412 
413   DenseMap<StringRef, uint32_t> prevailingSectionMap;
414   std::vector<const coff_aux_section_definition *> comdatDefs(
415       coffObj->getNumberOfSections() + 1);
416 
417   for (uint32_t i = 0; i < numSymbols; ++i) {
418     COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
419     bool prevailingComdat;
420     if (coffSym.isUndefined()) {
421       symbols[i] = createUndefined(coffSym);
422     } else if (coffSym.isWeakExternal()) {
423       symbols[i] = createUndefined(coffSym);
424       uint32_t tagIndex = coffSym.getAux<coff_aux_weak_external>()->TagIndex;
425       weakAliases.emplace_back(symbols[i], tagIndex);
426     } else if (Optional<Symbol *> optSym =
427                    createDefined(coffSym, comdatDefs, prevailingComdat)) {
428       symbols[i] = *optSym;
429       if (config->mingw && prevailingComdat)
430         recordPrevailingSymbolForMingw(coffSym, prevailingSectionMap);
431     } else {
432       // createDefined() returns None if a symbol belongs to a section that
433       // was pending at the point when the symbol was read. This can happen in
434       // two cases:
435       // 1) section definition symbol for a comdat leader;
436       // 2) symbol belongs to a comdat section associated with another section.
437       // In both of these cases, we can expect the section to be resolved by
438       // the time we finish visiting the remaining symbols in the symbol
439       // table. So we postpone the handling of this symbol until that time.
440       pendingIndexes.push_back(i);
441     }
442     i += coffSym.getNumberOfAuxSymbols();
443   }
444 
445   for (uint32_t i : pendingIndexes) {
446     COFFSymbolRef sym = check(coffObj->getSymbol(i));
447     if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
448       if (def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
449         readAssociativeDefinition(sym, def);
450       else if (config->mingw)
451         maybeAssociateSEHForMingw(sym, def, prevailingSectionMap);
452     }
453     if (sparseChunks[sym.getSectionNumber()] == pendingComdat) {
454       StringRef name = check(coffObj->getSymbolName(sym));
455       log("comdat section " + name +
456           " without leader and unassociated, discarding");
457       continue;
458     }
459     symbols[i] = createRegular(sym);
460   }
461 
462   for (auto &kv : weakAliases) {
463     Symbol *sym = kv.first;
464     uint32_t idx = kv.second;
465     checkAndSetWeakAlias(symtab, this, sym, symbols[idx]);
466   }
467 
468   // Free the memory used by sparseChunks now that symbol loading is finished.
469   decltype(sparseChunks)().swap(sparseChunks);
470 }
471 
472 Symbol *ObjFile::createUndefined(COFFSymbolRef sym) {
473   StringRef name = check(coffObj->getSymbolName(sym));
474   return symtab->addUndefined(name, this, sym.isWeakExternal());
475 }
476 
477 static const coff_aux_section_definition *findSectionDef(COFFObjectFile *obj,
478                                                          int32_t section) {
479   uint32_t numSymbols = obj->getNumberOfSymbols();
480   for (uint32_t i = 0; i < numSymbols; ++i) {
481     COFFSymbolRef sym = check(obj->getSymbol(i));
482     if (sym.getSectionNumber() != section)
483       continue;
484     if (const coff_aux_section_definition *def = sym.getSectionDefinition())
485       return def;
486   }
487   return nullptr;
488 }
489 
490 void ObjFile::handleComdatSelection(
491     COFFSymbolRef sym, COMDATType &selection, bool &prevailing,
492     DefinedRegular *leader,
493     const llvm::object::coff_aux_section_definition *def) {
494   if (prevailing)
495     return;
496   // There's already an existing comdat for this symbol: `Leader`.
497   // Use the comdats's selection field to determine if the new
498   // symbol in `Sym` should be discarded, produce a duplicate symbol
499   // error, etc.
500 
501   SectionChunk *leaderChunk = nullptr;
502   COMDATType leaderSelection = IMAGE_COMDAT_SELECT_ANY;
503 
504   if (leader->data) {
505     leaderChunk = leader->getChunk();
506     leaderSelection = leaderChunk->selection;
507   } else {
508     // FIXME: comdats from LTO files don't know their selection; treat them
509     // as "any".
510     selection = leaderSelection;
511   }
512 
513   if ((selection == IMAGE_COMDAT_SELECT_ANY &&
514        leaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
515       (selection == IMAGE_COMDAT_SELECT_LARGEST &&
516        leaderSelection == IMAGE_COMDAT_SELECT_ANY)) {
517     // cl.exe picks "any" for vftables when building with /GR- and
518     // "largest" when building with /GR. To be able to link object files
519     // compiled with each flag, "any" and "largest" are merged as "largest".
520     leaderSelection = selection = IMAGE_COMDAT_SELECT_LARGEST;
521   }
522 
523   // GCCs __declspec(selectany) doesn't actually pick "any" but "same size as".
524   // Clang on the other hand picks "any". To be able to link two object files
525   // with a __declspec(selectany) declaration, one compiled with gcc and the
526   // other with clang, we merge them as proper "same size as"
527   if (config->mingw && ((selection == IMAGE_COMDAT_SELECT_ANY &&
528                          leaderSelection == IMAGE_COMDAT_SELECT_SAME_SIZE) ||
529                         (selection == IMAGE_COMDAT_SELECT_SAME_SIZE &&
530                          leaderSelection == IMAGE_COMDAT_SELECT_ANY))) {
531     leaderSelection = selection = IMAGE_COMDAT_SELECT_SAME_SIZE;
532   }
533 
534   // Other than that, comdat selections must match.  This is a bit more
535   // strict than link.exe which allows merging "any" and "largest" if "any"
536   // is the first symbol the linker sees, and it allows merging "largest"
537   // with everything (!) if "largest" is the first symbol the linker sees.
538   // Making this symmetric independent of which selection is seen first
539   // seems better though.
540   // (This behavior matches ModuleLinker::getComdatResult().)
541   if (selection != leaderSelection) {
542     log(("conflicting comdat type for " + toString(*leader) + ": " +
543          Twine((int)leaderSelection) + " in " + toString(leader->getFile()) +
544          " and " + Twine((int)selection) + " in " + toString(this))
545             .str());
546     symtab->reportDuplicate(leader, this);
547     return;
548   }
549 
550   switch (selection) {
551   case IMAGE_COMDAT_SELECT_NODUPLICATES:
552     symtab->reportDuplicate(leader, this);
553     break;
554 
555   case IMAGE_COMDAT_SELECT_ANY:
556     // Nothing to do.
557     break;
558 
559   case IMAGE_COMDAT_SELECT_SAME_SIZE:
560     if (leaderChunk->getSize() != getSection(sym)->SizeOfRawData) {
561       if (!config->mingw) {
562         symtab->reportDuplicate(leader, this);
563       } else {
564         const coff_aux_section_definition *leaderDef = findSectionDef(
565             leaderChunk->file->getCOFFObj(), leaderChunk->getSectionNumber());
566         if (!leaderDef || leaderDef->Length != def->Length)
567           symtab->reportDuplicate(leader, this);
568       }
569     }
570     break;
571 
572   case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
573     SectionChunk newChunk(this, getSection(sym));
574     // link.exe only compares section contents here and doesn't complain
575     // if the two comdat sections have e.g. different alignment.
576     // Match that.
577     if (leaderChunk->getContents() != newChunk.getContents())
578       symtab->reportDuplicate(leader, this, &newChunk, sym.getValue());
579     break;
580   }
581 
582   case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
583     // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
584     // (This means lld-link doesn't produce duplicate symbol errors for
585     // associative comdats while link.exe does, but associate comdats
586     // are never extern in practice.)
587     llvm_unreachable("createDefined not called for associative comdats");
588 
589   case IMAGE_COMDAT_SELECT_LARGEST:
590     if (leaderChunk->getSize() < getSection(sym)->SizeOfRawData) {
591       // Replace the existing comdat symbol with the new one.
592       StringRef name = check(coffObj->getSymbolName(sym));
593       // FIXME: This is incorrect: With /opt:noref, the previous sections
594       // make it into the final executable as well. Correct handling would
595       // be to undo reading of the whole old section that's being replaced,
596       // or doing one pass that determines what the final largest comdat
597       // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
598       // only the largest one.
599       replaceSymbol<DefinedRegular>(leader, this, name, /*IsCOMDAT*/ true,
600                                     /*IsExternal*/ true, sym.getGeneric(),
601                                     nullptr);
602       prevailing = true;
603     }
604     break;
605 
606   case IMAGE_COMDAT_SELECT_NEWEST:
607     llvm_unreachable("should have been rejected earlier");
608   }
609 }
610 
611 Optional<Symbol *> ObjFile::createDefined(
612     COFFSymbolRef sym,
613     std::vector<const coff_aux_section_definition *> &comdatDefs,
614     bool &prevailing) {
615   prevailing = false;
616   auto getName = [&]() { return check(coffObj->getSymbolName(sym)); };
617 
618   if (sym.isCommon()) {
619     auto *c = make<CommonChunk>(sym);
620     chunks.push_back(c);
621     return symtab->addCommon(this, getName(), sym.getValue(), sym.getGeneric(),
622                              c);
623   }
624 
625   if (sym.isAbsolute()) {
626     StringRef name = getName();
627 
628     if (name == "@feat.00")
629       feat00Flags = sym.getValue();
630     // Skip special symbols.
631     if (ignoredSymbolName(name))
632       return nullptr;
633 
634     if (sym.isExternal())
635       return symtab->addAbsolute(name, sym);
636     return make<DefinedAbsolute>(name, sym);
637   }
638 
639   int32_t sectionNumber = sym.getSectionNumber();
640   if (sectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
641     return nullptr;
642 
643   if (llvm::COFF::isReservedSectionNumber(sectionNumber))
644     fatal(toString(this) + ": " + getName() +
645           " should not refer to special section " + Twine(sectionNumber));
646 
647   if ((uint32_t)sectionNumber >= sparseChunks.size())
648     fatal(toString(this) + ": " + getName() +
649           " should not refer to non-existent section " + Twine(sectionNumber));
650 
651   // Comdat handling.
652   // A comdat symbol consists of two symbol table entries.
653   // The first symbol entry has the name of the section (e.g. .text), fixed
654   // values for the other fields, and one auxiliary record.
655   // The second symbol entry has the name of the comdat symbol, called the
656   // "comdat leader".
657   // When this function is called for the first symbol entry of a comdat,
658   // it sets comdatDefs and returns None, and when it's called for the second
659   // symbol entry it reads comdatDefs and then sets it back to nullptr.
660 
661   // Handle comdat leader.
662   if (const coff_aux_section_definition *def = comdatDefs[sectionNumber]) {
663     comdatDefs[sectionNumber] = nullptr;
664     DefinedRegular *leader;
665 
666     if (sym.isExternal()) {
667       std::tie(leader, prevailing) =
668           symtab->addComdat(this, getName(), sym.getGeneric());
669     } else {
670       leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
671                                     /*IsExternal*/ false, sym.getGeneric());
672       prevailing = true;
673     }
674 
675     if (def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
676         // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
677         // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
678         def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
679       fatal("unknown comdat type " + std::to_string((int)def->Selection) +
680             " for " + getName() + " in " + toString(this));
681     }
682     COMDATType selection = (COMDATType)def->Selection;
683 
684     if (leader->isCOMDAT)
685       handleComdatSelection(sym, selection, prevailing, leader, def);
686 
687     if (prevailing) {
688       SectionChunk *c = readSection(sectionNumber, def, getName());
689       sparseChunks[sectionNumber] = c;
690       c->sym = cast<DefinedRegular>(leader);
691       c->selection = selection;
692       cast<DefinedRegular>(leader)->data = &c->repl;
693     } else {
694       sparseChunks[sectionNumber] = nullptr;
695     }
696     return leader;
697   }
698 
699   // Prepare to handle the comdat leader symbol by setting the section's
700   // ComdatDefs pointer if we encounter a non-associative comdat.
701   if (sparseChunks[sectionNumber] == pendingComdat) {
702     if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
703       if (def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
704         comdatDefs[sectionNumber] = def;
705     }
706     return None;
707   }
708 
709   return createRegular(sym);
710 }
711 
712 MachineTypes ObjFile::getMachineType() {
713   if (coffObj)
714     return static_cast<MachineTypes>(coffObj->getMachine());
715   return IMAGE_FILE_MACHINE_UNKNOWN;
716 }
717 
718 ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {
719   if (SectionChunk *sec = SectionChunk::findByName(debugChunks, secName))
720     return sec->consumeDebugMagic();
721   return {};
722 }
723 
724 // OBJ files systematically store critical information in a .debug$S stream,
725 // even if the TU was compiled with no debug info. At least two records are
726 // always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
727 // PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
728 // currently used to initialize the hotPatchable member.
729 void ObjFile::initializeFlags() {
730   ArrayRef<uint8_t> data = getDebugSection(".debug$S");
731   if (data.empty())
732     return;
733 
734   DebugSubsectionArray subsections;
735 
736   BinaryStreamReader reader(data, support::little);
737   ExitOnError exitOnErr;
738   exitOnErr(reader.readArray(subsections, data.size()));
739 
740   for (const DebugSubsectionRecord &ss : subsections) {
741     if (ss.kind() != DebugSubsectionKind::Symbols)
742       continue;
743 
744     unsigned offset = 0;
745 
746     // Only parse the first two records. We are only looking for S_OBJNAME
747     // and S_COMPILE3, and they usually appear at the beginning of the
748     // stream.
749     for (unsigned i = 0; i < 2; ++i) {
750       Expected<CVSymbol> sym = readSymbolFromStream(ss.getRecordData(), offset);
751       if (!sym) {
752         consumeError(sym.takeError());
753         return;
754       }
755       if (sym->kind() == SymbolKind::S_COMPILE3) {
756         auto cs =
757             cantFail(SymbolDeserializer::deserializeAs<Compile3Sym>(sym.get()));
758         hotPatchable =
759             (cs.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
760       }
761       if (sym->kind() == SymbolKind::S_OBJNAME) {
762         auto objName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
763             sym.get()));
764         pchSignature = objName.Signature;
765       }
766       offset += sym->length();
767     }
768   }
769 }
770 
771 // Depending on the compilation flags, OBJs can refer to external files,
772 // necessary to merge this OBJ into the final PDB. We currently support two
773 // types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
774 // And PDB type servers, when compiling with /Zi. This function extracts these
775 // dependencies and makes them available as a TpiSource interface (see
776 // DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
777 // output even with /Yc and /Yu and with /Zi.
778 void ObjFile::initializeDependencies() {
779   if (!config->debug)
780     return;
781 
782   bool isPCH = false;
783 
784   ArrayRef<uint8_t> data = getDebugSection(".debug$P");
785   if (!data.empty())
786     isPCH = true;
787   else
788     data = getDebugSection(".debug$T");
789 
790   // Don't make a TpiSource for objects with no debug info. If the object has
791   // symbols but no types, make a plain, empty TpiSource anyway, because it
792   // simplifies adding the symbols later.
793   if (data.empty()) {
794     if (!debugChunks.empty())
795       debugTypesObj = makeTpiSource(this);
796     return;
797   }
798 
799   // Get the first type record. It will indicate if this object uses a type
800   // server (/Zi) or a PCH file (/Yu).
801   CVTypeArray types;
802   BinaryStreamReader reader(data, support::little);
803   cantFail(reader.readArray(types, reader.getLength()));
804   CVTypeArray::Iterator firstType = types.begin();
805   if (firstType == types.end())
806     return;
807 
808   // Remember the .debug$T or .debug$P section.
809   debugTypes = data;
810 
811   // This object file is a PCH file that others will depend on.
812   if (isPCH) {
813     debugTypesObj = makePrecompSource(this);
814     return;
815   }
816 
817   // This object file was compiled with /Zi. Enqueue the PDB dependency.
818   if (firstType->kind() == LF_TYPESERVER2) {
819     TypeServer2Record ts = cantFail(
820         TypeDeserializer::deserializeAs<TypeServer2Record>(firstType->data()));
821     debugTypesObj = makeUseTypeServerSource(this, ts);
822     PDBInputFile::enqueue(ts.getName(), this);
823     return;
824   }
825 
826   // This object was compiled with /Yu. It uses types from another object file
827   // with a matching signature.
828   if (firstType->kind() == LF_PRECOMP) {
829     PrecompRecord precomp = cantFail(
830         TypeDeserializer::deserializeAs<PrecompRecord>(firstType->data()));
831     debugTypesObj = makeUsePrecompSource(this, precomp);
832     // Drop the LF_PRECOMP record from the input stream.
833     debugTypes = debugTypes.drop_front(firstType->RecordData.size());
834     return;
835   }
836 
837   // This is a plain old object file.
838   debugTypesObj = makeTpiSource(this);
839 }
840 
841 // Make a PDB path assuming the PDB is in the same folder as the OBJ
842 static std::string getPdbBaseName(ObjFile *file, StringRef tSPath) {
843   StringRef localPath =
844       !file->parentName.empty() ? file->parentName : file->getName();
845   SmallString<128> path = sys::path::parent_path(localPath);
846 
847   // Currently, type server PDBs are only created by MSVC cl, which only runs
848   // on Windows, so we can assume type server paths are Windows style.
849   sys::path::append(path,
850                     sys::path::filename(tSPath, sys::path::Style::windows));
851   return std::string(path.str());
852 }
853 
854 // The casing of the PDB path stamped in the OBJ can differ from the actual path
855 // on disk. With this, we ensure to always use lowercase as a key for the
856 // PDBInputFile::instances map, at least on Windows.
857 static std::string normalizePdbPath(StringRef path) {
858 #if defined(_WIN32)
859   return path.lower();
860 #else // LINUX
861   return std::string(path);
862 #endif
863 }
864 
865 // If existing, return the actual PDB path on disk.
866 static Optional<std::string> findPdbPath(StringRef pdbPath,
867                                          ObjFile *dependentFile) {
868   // Ensure the file exists before anything else. In some cases, if the path
869   // points to a removable device, Driver::enqueuePath() would fail with an
870   // error (EAGAIN, "resource unavailable try again") which we want to skip
871   // silently.
872   if (llvm::sys::fs::exists(pdbPath))
873     return normalizePdbPath(pdbPath);
874   std::string ret = getPdbBaseName(dependentFile, pdbPath);
875   if (llvm::sys::fs::exists(ret))
876     return normalizePdbPath(ret);
877   return None;
878 }
879 
880 PDBInputFile::PDBInputFile(MemoryBufferRef m) : InputFile(PDBKind, m) {}
881 
882 PDBInputFile::~PDBInputFile() = default;
883 
884 PDBInputFile *PDBInputFile::findFromRecordPath(StringRef path,
885                                                ObjFile *fromFile) {
886   auto p = findPdbPath(path.str(), fromFile);
887   if (!p)
888     return nullptr;
889   auto it = PDBInputFile::instances.find(*p);
890   if (it != PDBInputFile::instances.end())
891     return it->second;
892   return nullptr;
893 }
894 
895 void PDBInputFile::enqueue(StringRef path, ObjFile *fromFile) {
896   auto p = findPdbPath(path.str(), fromFile);
897   if (!p)
898     return;
899   auto it = PDBInputFile::instances.emplace(*p, nullptr);
900   if (!it.second)
901     return; // already scheduled for load
902   driver->enqueuePDB(*p);
903 }
904 
905 void PDBInputFile::parse() {
906   PDBInputFile::instances[mb.getBufferIdentifier().str()] = this;
907 
908   std::unique_ptr<pdb::IPDBSession> thisSession;
909   loadErr.emplace(pdb::NativeSession::createFromPdb(
910       MemoryBuffer::getMemBuffer(mb, false), thisSession));
911   if (*loadErr)
912     return; // fail silently at this point - the error will be handled later,
913             // when merging the debug type stream
914 
915   session.reset(static_cast<pdb::NativeSession *>(thisSession.release()));
916 
917   pdb::PDBFile &pdbFile = session->getPDBFile();
918   auto expectedInfo = pdbFile.getPDBInfoStream();
919   // All PDB Files should have an Info stream.
920   if (!expectedInfo) {
921     loadErr.emplace(expectedInfo.takeError());
922     return;
923   }
924   debugTypesObj = makeTypeServerSource(this);
925 }
926 
927 // Used only for DWARF debug info, which is not common (except in MinGW
928 // environments). This returns an optional pair of file name and line
929 // number for where the variable was defined.
930 Optional<std::pair<StringRef, uint32_t>>
931 ObjFile::getVariableLocation(StringRef var) {
932   if (!dwarf) {
933     dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
934     if (!dwarf)
935       return None;
936   }
937   if (config->machine == I386)
938     var.consume_front("_");
939   Optional<std::pair<std::string, unsigned>> ret = dwarf->getVariableLoc(var);
940   if (!ret)
941     return None;
942   return std::make_pair(saver.save(ret->first), ret->second);
943 }
944 
945 // Used only for DWARF debug info, which is not common (except in MinGW
946 // environments).
947 Optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
948                                             uint32_t sectionIndex) {
949   if (!dwarf) {
950     dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
951     if (!dwarf)
952       return None;
953   }
954 
955   return dwarf->getDILineInfo(offset, sectionIndex);
956 }
957 
958 static StringRef ltrim1(StringRef s, const char *chars) {
959   if (!s.empty() && strchr(chars, s[0]))
960     return s.substr(1);
961   return s;
962 }
963 
964 void ImportFile::parse() {
965   const char *buf = mb.getBufferStart();
966   const auto *hdr = reinterpret_cast<const coff_import_header *>(buf);
967 
968   // Check if the total size is valid.
969   if (mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
970     fatal("broken import library");
971 
972   // Read names and create an __imp_ symbol.
973   StringRef name = saver.save(StringRef(buf + sizeof(*hdr)));
974   StringRef impName = saver.save("__imp_" + name);
975   const char *nameStart = buf + sizeof(coff_import_header) + name.size() + 1;
976   dllName = std::string(StringRef(nameStart));
977   StringRef extName;
978   switch (hdr->getNameType()) {
979   case IMPORT_ORDINAL:
980     extName = "";
981     break;
982   case IMPORT_NAME:
983     extName = name;
984     break;
985   case IMPORT_NAME_NOPREFIX:
986     extName = ltrim1(name, "?@_");
987     break;
988   case IMPORT_NAME_UNDECORATE:
989     extName = ltrim1(name, "?@_");
990     extName = extName.substr(0, extName.find('@'));
991     break;
992   }
993 
994   this->hdr = hdr;
995   externalName = extName;
996 
997   impSym = symtab->addImportData(impName, this);
998   // If this was a duplicate, we logged an error but may continue;
999   // in this case, impSym is nullptr.
1000   if (!impSym)
1001     return;
1002 
1003   if (hdr->getType() == llvm::COFF::IMPORT_CONST)
1004     static_cast<void>(symtab->addImportData(name, this));
1005 
1006   // If type is function, we need to create a thunk which jump to an
1007   // address pointed by the __imp_ symbol. (This allows you to call
1008   // DLL functions just like regular non-DLL functions.)
1009   if (hdr->getType() == llvm::COFF::IMPORT_CODE)
1010     thunkSym = symtab->addImportThunk(
1011         name, cast_or_null<DefinedImportData>(impSym), hdr->Machine);
1012 }
1013 
1014 BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
1015                          uint64_t offsetInArchive)
1016     : BitcodeFile(mb, archiveName, offsetInArchive, {}) {}
1017 
1018 BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
1019                          uint64_t offsetInArchive,
1020                          std::vector<Symbol *> &&symbols)
1021     : InputFile(BitcodeKind, mb), symbols(std::move(symbols)) {
1022   std::string path = mb.getBufferIdentifier().str();
1023   if (config->thinLTOIndexOnly)
1024     path = replaceThinLTOSuffix(mb.getBufferIdentifier());
1025 
1026   // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
1027   // name. If two archives define two members with the same name, this
1028   // causes a collision which result in only one of the objects being taken
1029   // into consideration at LTO time (which very likely causes undefined
1030   // symbols later in the link stage). So we append file offset to make
1031   // filename unique.
1032   MemoryBufferRef mbref(
1033       mb.getBuffer(),
1034       saver.save(archiveName.empty() ? path
1035                                      : archiveName + sys::path::filename(path) +
1036                                            utostr(offsetInArchive)));
1037 
1038   obj = check(lto::InputFile::create(mbref));
1039 }
1040 
1041 BitcodeFile::~BitcodeFile() = default;
1042 
1043 void BitcodeFile::parse() {
1044   std::vector<std::pair<Symbol *, bool>> comdat(obj->getComdatTable().size());
1045   for (size_t i = 0; i != obj->getComdatTable().size(); ++i)
1046     // FIXME: lto::InputFile doesn't keep enough data to do correct comdat
1047     // selection handling.
1048     comdat[i] = symtab->addComdat(this, saver.save(obj->getComdatTable()[i]));
1049   for (const lto::InputFile::Symbol &objSym : obj->symbols()) {
1050     StringRef symName = saver.save(objSym.getName());
1051     int comdatIndex = objSym.getComdatIndex();
1052     Symbol *sym;
1053     if (objSym.isUndefined()) {
1054       sym = symtab->addUndefined(symName, this, false);
1055     } else if (objSym.isCommon()) {
1056       sym = symtab->addCommon(this, symName, objSym.getCommonSize());
1057     } else if (objSym.isWeak() && objSym.isIndirect()) {
1058       // Weak external.
1059       sym = symtab->addUndefined(symName, this, true);
1060       std::string fallback = std::string(objSym.getCOFFWeakExternalFallback());
1061       Symbol *alias = symtab->addUndefined(saver.save(fallback));
1062       checkAndSetWeakAlias(symtab, this, sym, alias);
1063     } else if (comdatIndex != -1) {
1064       if (symName == obj->getComdatTable()[comdatIndex])
1065         sym = comdat[comdatIndex].first;
1066       else if (comdat[comdatIndex].second)
1067         sym = symtab->addRegular(this, symName);
1068       else
1069         sym = symtab->addUndefined(symName, this, false);
1070     } else {
1071       sym = symtab->addRegular(this, symName);
1072     }
1073     symbols.push_back(sym);
1074     if (objSym.isUsed())
1075       config->gcroot.push_back(sym);
1076   }
1077   directives = obj->getCOFFLinkerOpts();
1078 }
1079 
1080 MachineTypes BitcodeFile::getMachineType() {
1081   switch (Triple(obj->getTargetTriple()).getArch()) {
1082   case Triple::x86_64:
1083     return AMD64;
1084   case Triple::x86:
1085     return I386;
1086   case Triple::arm:
1087     return ARMNT;
1088   case Triple::aarch64:
1089     return ARM64;
1090   default:
1091     return IMAGE_FILE_MACHINE_UNKNOWN;
1092   }
1093 }
1094 
1095 std::string lld::coff::replaceThinLTOSuffix(StringRef path) {
1096   StringRef suffix = config->thinLTOObjectSuffixReplace.first;
1097   StringRef repl = config->thinLTOObjectSuffixReplace.second;
1098 
1099   if (path.consume_back(suffix))
1100     return (path + repl).str();
1101   return std::string(path);
1102 }
1103