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