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.
getBasename(StringRef path)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)".
toString(const coff::InputFile * file)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.
checkAndSetWeakAlias(SymbolTable * symtab,InputFile * f,Symbol * source,Symbol * 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 
ignoredSymbolName(StringRef name)97 static bool ignoredSymbolName(StringRef name) {
98   return name == "@feat.00" || name == "@comp.id";
99 }
100 
ArchiveFile(MemoryBufferRef m)101 ArchiveFile::ArchiveFile(MemoryBufferRef m) : InputFile(ArchiveKind, m) {}
102 
parse()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.
addMember(const Archive::Symbol & sym)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 
getArchiveMembers(Archive * file)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 
fetch()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 
parse()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 
parse()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 
getSection(uint32_t i)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 
initializeChunks()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 
readSection(uint32_t sectionNumber,const coff_aux_section_definition * def,StringRef leaderName)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   // Object files may have DWARF debug info or MS CodeView debug info
253   // (or both).
254   //
255   // DWARF sections don't need any special handling from the perspective
256   // of the linker; they are just a data section containing relocations.
257   // We can just link them to complete debug info.
258   //
259   // CodeView needs linker support. We need to interpret debug info,
260   // and then write it to a separate .pdb file.
261 
262   // Ignore DWARF debug info unless /debug is given.
263   if (!config->debug && name.startswith(".debug_"))
264     return nullptr;
265 
266   if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
267     return nullptr;
268   auto *c = make<SectionChunk>(this, sec);
269   if (def)
270     c->checksum = def->CheckSum;
271 
272   // CodeView sections are stored to a different vector because they are not
273   // linked in the regular manner.
274   if (c->isCodeView())
275     debugChunks.push_back(c);
276   else if (name == ".gfids$y")
277     guardFidChunks.push_back(c);
278   else if (name == ".gljmp$y")
279     guardLJmpChunks.push_back(c);
280   else if (name == ".sxdata")
281     sxDataChunks.push_back(c);
282   else if (config->tailMerge && sec->NumberOfRelocations == 0 &&
283            name == ".rdata" && leaderName.startswith("??_C@"))
284     // COFF sections that look like string literal sections (i.e. no
285     // relocations, in .rdata, leader symbol name matches the MSVC name mangling
286     // for string literals) are subject to string tail merging.
287     MergeChunk::addSection(c);
288   else if (name == ".rsrc" || name.startswith(".rsrc$"))
289     resourceChunks.push_back(c);
290   else
291     chunks.push_back(c);
292 
293   return c;
294 }
295 
includeResourceChunks()296 void ObjFile::includeResourceChunks() {
297   chunks.insert(chunks.end(), resourceChunks.begin(), resourceChunks.end());
298 }
299 
readAssociativeDefinition(COFFSymbolRef sym,const coff_aux_section_definition * def)300 void ObjFile::readAssociativeDefinition(
301     COFFSymbolRef sym, const coff_aux_section_definition *def) {
302   readAssociativeDefinition(sym, def, def->getNumber(sym.isBigObj()));
303 }
304 
readAssociativeDefinition(COFFSymbolRef sym,const coff_aux_section_definition * def,uint32_t parentIndex)305 void ObjFile::readAssociativeDefinition(COFFSymbolRef sym,
306                                         const coff_aux_section_definition *def,
307                                         uint32_t parentIndex) {
308   SectionChunk *parent = sparseChunks[parentIndex];
309   int32_t sectionNumber = sym.getSectionNumber();
310 
311   auto diag = [&]() {
312     StringRef name = check(coffObj->getSymbolName(sym));
313 
314     StringRef parentName;
315     const coff_section *parentSec = getSection(parentIndex);
316     if (Expected<StringRef> e = coffObj->getSectionName(parentSec))
317       parentName = *e;
318     error(toString(this) + ": associative comdat " + name + " (sec " +
319           Twine(sectionNumber) + ") has invalid reference to section " +
320           parentName + " (sec " + Twine(parentIndex) + ")");
321   };
322 
323   if (parent == pendingComdat) {
324     // This can happen if an associative comdat refers to another associative
325     // comdat that appears after it (invalid per COFF spec) or to a section
326     // without any symbols.
327     diag();
328     return;
329   }
330 
331   // Check whether the parent is prevailing. If it is, so are we, and we read
332   // the section; otherwise mark it as discarded.
333   if (parent) {
334     SectionChunk *c = readSection(sectionNumber, def, "");
335     sparseChunks[sectionNumber] = c;
336     if (c) {
337       c->selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
338       parent->addAssociative(c);
339     }
340   } else {
341     sparseChunks[sectionNumber] = nullptr;
342   }
343 }
344 
recordPrevailingSymbolForMingw(COFFSymbolRef sym,DenseMap<StringRef,uint32_t> & prevailingSectionMap)345 void ObjFile::recordPrevailingSymbolForMingw(
346     COFFSymbolRef sym, DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
347   // For comdat symbols in executable sections, where this is the copy
348   // of the section chunk we actually include instead of discarding it,
349   // add the symbol to a map to allow using it for implicitly
350   // associating .[px]data$<func> sections to it.
351   int32_t sectionNumber = sym.getSectionNumber();
352   SectionChunk *sc = sparseChunks[sectionNumber];
353   if (sc && sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
354     StringRef name;
355     name = check(coffObj->getSymbolName(sym));
356     if (getMachineType() == I386)
357       name.consume_front("_");
358     prevailingSectionMap[name] = sectionNumber;
359   }
360 }
361 
maybeAssociateSEHForMingw(COFFSymbolRef sym,const coff_aux_section_definition * def,const DenseMap<StringRef,uint32_t> & prevailingSectionMap)362 void ObjFile::maybeAssociateSEHForMingw(
363     COFFSymbolRef sym, const coff_aux_section_definition *def,
364     const DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
365   StringRef name = check(coffObj->getSymbolName(sym));
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 
createRegular(COFFSymbolRef sym)376 Symbol *ObjFile::createRegular(COFFSymbolRef sym) {
377   SectionChunk *sc = sparseChunks[sym.getSectionNumber()];
378   if (sym.isExternal()) {
379     StringRef name = check(coffObj->getSymbolName(sym));
380     if (sc)
381       return symtab->addRegular(this, name, sym.getGeneric(), sc,
382                                 sym.getValue());
383     // For MinGW symbols named .weak.* that point to a discarded section,
384     // don't create an Undefined symbol. If nothing ever refers to the symbol,
385     // everything should be fine. If something actually refers to the symbol
386     // (e.g. the undefined weak alias), linking will fail due to undefined
387     // references at the end.
388     if (config->mingw && name.startswith(".weak."))
389       return nullptr;
390     return symtab->addUndefined(name, this, false);
391   }
392   if (sc)
393     return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
394                                 /*IsExternal*/ false, sym.getGeneric(), sc);
395   return nullptr;
396 }
397 
initializeSymbols()398 void ObjFile::initializeSymbols() {
399   uint32_t numSymbols = coffObj->getNumberOfSymbols();
400   symbols.resize(numSymbols);
401 
402   SmallVector<std::pair<Symbol *, uint32_t>, 8> weakAliases;
403   std::vector<uint32_t> pendingIndexes;
404   pendingIndexes.reserve(numSymbols);
405 
406   DenseMap<StringRef, uint32_t> prevailingSectionMap;
407   std::vector<const coff_aux_section_definition *> comdatDefs(
408       coffObj->getNumberOfSections() + 1);
409 
410   for (uint32_t i = 0; i < numSymbols; ++i) {
411     COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
412     bool prevailingComdat;
413     if (coffSym.isUndefined()) {
414       symbols[i] = createUndefined(coffSym);
415     } else if (coffSym.isWeakExternal()) {
416       symbols[i] = createUndefined(coffSym);
417       uint32_t tagIndex = coffSym.getAux<coff_aux_weak_external>()->TagIndex;
418       weakAliases.emplace_back(symbols[i], tagIndex);
419     } else if (Optional<Symbol *> optSym =
420                    createDefined(coffSym, comdatDefs, prevailingComdat)) {
421       symbols[i] = *optSym;
422       if (config->mingw && prevailingComdat)
423         recordPrevailingSymbolForMingw(coffSym, prevailingSectionMap);
424     } else {
425       // createDefined() returns None if a symbol belongs to a section that
426       // was pending at the point when the symbol was read. This can happen in
427       // two cases:
428       // 1) section definition symbol for a comdat leader;
429       // 2) symbol belongs to a comdat section associated with another section.
430       // In both of these cases, we can expect the section to be resolved by
431       // the time we finish visiting the remaining symbols in the symbol
432       // table. So we postpone the handling of this symbol until that time.
433       pendingIndexes.push_back(i);
434     }
435     i += coffSym.getNumberOfAuxSymbols();
436   }
437 
438   for (uint32_t i : pendingIndexes) {
439     COFFSymbolRef sym = check(coffObj->getSymbol(i));
440     if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
441       if (def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
442         readAssociativeDefinition(sym, def);
443       else if (config->mingw)
444         maybeAssociateSEHForMingw(sym, def, prevailingSectionMap);
445     }
446     if (sparseChunks[sym.getSectionNumber()] == pendingComdat) {
447       StringRef name = check(coffObj->getSymbolName(sym));
448       log("comdat section " + name +
449           " without leader and unassociated, discarding");
450       continue;
451     }
452     symbols[i] = createRegular(sym);
453   }
454 
455   for (auto &kv : weakAliases) {
456     Symbol *sym = kv.first;
457     uint32_t idx = kv.second;
458     checkAndSetWeakAlias(symtab, this, sym, symbols[idx]);
459   }
460 
461   // Free the memory used by sparseChunks now that symbol loading is finished.
462   decltype(sparseChunks)().swap(sparseChunks);
463 }
464 
createUndefined(COFFSymbolRef sym)465 Symbol *ObjFile::createUndefined(COFFSymbolRef sym) {
466   StringRef name = check(coffObj->getSymbolName(sym));
467   return symtab->addUndefined(name, this, sym.isWeakExternal());
468 }
469 
handleComdatSelection(COFFSymbolRef sym,COMDATType & selection,bool & prevailing,DefinedRegular * leader)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 = check(coffObj->getSymbolName(sym));
563       // FIXME: This is incorrect: With /opt:noref, the previous sections
564       // make it into the final executable as well. Correct handling would
565       // be to undo reading of the whole old section that's being replaced,
566       // or doing one pass that determines what the final largest comdat
567       // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
568       // only the largest one.
569       replaceSymbol<DefinedRegular>(leader, this, name, /*IsCOMDAT*/ true,
570                                     /*IsExternal*/ true, sym.getGeneric(),
571                                     nullptr);
572       prevailing = true;
573     }
574     break;
575 
576   case IMAGE_COMDAT_SELECT_NEWEST:
577     llvm_unreachable("should have been rejected earlier");
578   }
579 }
580 
createDefined(COFFSymbolRef sym,std::vector<const coff_aux_section_definition * > & comdatDefs,bool & prevailing)581 Optional<Symbol *> ObjFile::createDefined(
582     COFFSymbolRef sym,
583     std::vector<const coff_aux_section_definition *> &comdatDefs,
584     bool &prevailing) {
585   prevailing = false;
586   auto getName = [&]() { return check(coffObj->getSymbolName(sym)); };
587 
588   if (sym.isCommon()) {
589     auto *c = make<CommonChunk>(sym);
590     chunks.push_back(c);
591     return symtab->addCommon(this, getName(), sym.getValue(), sym.getGeneric(),
592                              c);
593   }
594 
595   if (sym.isAbsolute()) {
596     StringRef name = getName();
597 
598     if (name == "@feat.00")
599       feat00Flags = sym.getValue();
600     // Skip special symbols.
601     if (ignoredSymbolName(name))
602       return nullptr;
603 
604     if (sym.isExternal())
605       return symtab->addAbsolute(name, sym);
606     return make<DefinedAbsolute>(name, sym);
607   }
608 
609   int32_t sectionNumber = sym.getSectionNumber();
610   if (sectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
611     return nullptr;
612 
613   if (llvm::COFF::isReservedSectionNumber(sectionNumber))
614     fatal(toString(this) + ": " + getName() +
615           " should not refer to special section " + Twine(sectionNumber));
616 
617   if ((uint32_t)sectionNumber >= sparseChunks.size())
618     fatal(toString(this) + ": " + getName() +
619           " should not refer to non-existent section " + Twine(sectionNumber));
620 
621   // Comdat handling.
622   // A comdat symbol consists of two symbol table entries.
623   // The first symbol entry has the name of the section (e.g. .text), fixed
624   // values for the other fields, and one auxiliary record.
625   // The second symbol entry has the name of the comdat symbol, called the
626   // "comdat leader".
627   // When this function is called for the first symbol entry of a comdat,
628   // it sets comdatDefs and returns None, and when it's called for the second
629   // symbol entry it reads comdatDefs and then sets it back to nullptr.
630 
631   // Handle comdat leader.
632   if (const coff_aux_section_definition *def = comdatDefs[sectionNumber]) {
633     comdatDefs[sectionNumber] = nullptr;
634     DefinedRegular *leader;
635 
636     if (sym.isExternal()) {
637       std::tie(leader, prevailing) =
638           symtab->addComdat(this, getName(), sym.getGeneric());
639     } else {
640       leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
641                                     /*IsExternal*/ false, sym.getGeneric());
642       prevailing = true;
643     }
644 
645     if (def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
646         // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
647         // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
648         def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
649       fatal("unknown comdat type " + std::to_string((int)def->Selection) +
650             " for " + getName() + " in " + toString(this));
651     }
652     COMDATType selection = (COMDATType)def->Selection;
653 
654     if (leader->isCOMDAT)
655       handleComdatSelection(sym, selection, prevailing, leader);
656 
657     if (prevailing) {
658       SectionChunk *c = readSection(sectionNumber, def, getName());
659       sparseChunks[sectionNumber] = c;
660       c->sym = cast<DefinedRegular>(leader);
661       c->selection = selection;
662       cast<DefinedRegular>(leader)->data = &c->repl;
663     } else {
664       sparseChunks[sectionNumber] = nullptr;
665     }
666     return leader;
667   }
668 
669   // Prepare to handle the comdat leader symbol by setting the section's
670   // ComdatDefs pointer if we encounter a non-associative comdat.
671   if (sparseChunks[sectionNumber] == pendingComdat) {
672     if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
673       if (def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
674         comdatDefs[sectionNumber] = def;
675     }
676     return None;
677   }
678 
679   return createRegular(sym);
680 }
681 
getMachineType()682 MachineTypes ObjFile::getMachineType() {
683   if (coffObj)
684     return static_cast<MachineTypes>(coffObj->getMachine());
685   return IMAGE_FILE_MACHINE_UNKNOWN;
686 }
687 
getDebugSection(StringRef secName)688 ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {
689   if (SectionChunk *sec = SectionChunk::findByName(debugChunks, secName))
690     return sec->consumeDebugMagic();
691   return {};
692 }
693 
694 // OBJ files systematically store critical information in a .debug$S stream,
695 // even if the TU was compiled with no debug info. At least two records are
696 // always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
697 // PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
698 // currently used to initialize the hotPatchable member.
initializeFlags()699 void ObjFile::initializeFlags() {
700   ArrayRef<uint8_t> data = getDebugSection(".debug$S");
701   if (data.empty())
702     return;
703 
704   DebugSubsectionArray subsections;
705 
706   BinaryStreamReader reader(data, support::little);
707   ExitOnError exitOnErr;
708   exitOnErr(reader.readArray(subsections, data.size()));
709 
710   for (const DebugSubsectionRecord &ss : subsections) {
711     if (ss.kind() != DebugSubsectionKind::Symbols)
712       continue;
713 
714     unsigned offset = 0;
715 
716     // Only parse the first two records. We are only looking for S_OBJNAME
717     // and S_COMPILE3, and they usually appear at the beginning of the
718     // stream.
719     for (unsigned i = 0; i < 2; ++i) {
720       Expected<CVSymbol> sym = readSymbolFromStream(ss.getRecordData(), offset);
721       if (!sym) {
722         consumeError(sym.takeError());
723         return;
724       }
725       if (sym->kind() == SymbolKind::S_COMPILE3) {
726         auto cs =
727             cantFail(SymbolDeserializer::deserializeAs<Compile3Sym>(sym.get()));
728         hotPatchable =
729             (cs.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
730       }
731       if (sym->kind() == SymbolKind::S_OBJNAME) {
732         auto objName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
733             sym.get()));
734         pchSignature = objName.Signature;
735       }
736       offset += sym->length();
737     }
738   }
739 }
740 
741 // Depending on the compilation flags, OBJs can refer to external files,
742 // necessary to merge this OBJ into the final PDB. We currently support two
743 // types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
744 // And PDB type servers, when compiling with /Zi. This function extracts these
745 // dependencies and makes them available as a TpiSource interface (see
746 // DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
747 // output even with /Yc and /Yu and with /Zi.
initializeDependencies()748 void ObjFile::initializeDependencies() {
749   if (!config->debug)
750     return;
751 
752   bool isPCH = false;
753 
754   ArrayRef<uint8_t> data = getDebugSection(".debug$P");
755   if (!data.empty())
756     isPCH = true;
757   else
758     data = getDebugSection(".debug$T");
759 
760   if (data.empty())
761     return;
762 
763   // Get the first type record. It will indicate if this object uses a type
764   // server (/Zi) or a PCH file (/Yu).
765   CVTypeArray types;
766   BinaryStreamReader reader(data, support::little);
767   cantFail(reader.readArray(types, reader.getLength()));
768   CVTypeArray::Iterator firstType = types.begin();
769   if (firstType == types.end())
770     return;
771 
772   // Remember the .debug$T or .debug$P section.
773   debugTypes = data;
774 
775   // This object file is a PCH file that others will depend on.
776   if (isPCH) {
777     debugTypesObj = makePrecompSource(this);
778     return;
779   }
780 
781   // This object file was compiled with /Zi. Enqueue the PDB dependency.
782   if (firstType->kind() == LF_TYPESERVER2) {
783     TypeServer2Record ts = cantFail(
784         TypeDeserializer::deserializeAs<TypeServer2Record>(firstType->data()));
785     debugTypesObj = makeUseTypeServerSource(this, ts);
786     PDBInputFile::enqueue(ts.getName(), this);
787     return;
788   }
789 
790   // This object was compiled with /Yu. It uses types from another object file
791   // with a matching signature.
792   if (firstType->kind() == LF_PRECOMP) {
793     PrecompRecord precomp = cantFail(
794         TypeDeserializer::deserializeAs<PrecompRecord>(firstType->data()));
795     debugTypesObj = makeUsePrecompSource(this, precomp);
796     return;
797   }
798 
799   // This is a plain old object file.
800   debugTypesObj = makeTpiSource(this);
801 }
802 
803 // Make a PDB path assuming the PDB is in the same folder as the OBJ
getPdbBaseName(ObjFile * file,StringRef tSPath)804 static std::string getPdbBaseName(ObjFile *file, StringRef tSPath) {
805   StringRef localPath =
806       !file->parentName.empty() ? file->parentName : file->getName();
807   SmallString<128> path = sys::path::parent_path(localPath);
808 
809   // Currently, type server PDBs are only created by MSVC cl, which only runs
810   // on Windows, so we can assume type server paths are Windows style.
811   sys::path::append(path,
812                     sys::path::filename(tSPath, sys::path::Style::windows));
813   return std::string(path.str());
814 }
815 
816 // The casing of the PDB path stamped in the OBJ can differ from the actual path
817 // on disk. With this, we ensure to always use lowercase as a key for the
818 // PDBInputFile::instances map, at least on Windows.
normalizePdbPath(StringRef path)819 static std::string normalizePdbPath(StringRef path) {
820 #if defined(_WIN32)
821   return path.lower();
822 #else // LINUX
823   return std::string(path);
824 #endif
825 }
826 
827 // If existing, return the actual PDB path on disk.
findPdbPath(StringRef pdbPath,ObjFile * dependentFile)828 static Optional<std::string> findPdbPath(StringRef pdbPath,
829                                          ObjFile *dependentFile) {
830   // Ensure the file exists before anything else. In some cases, if the path
831   // points to a removable device, Driver::enqueuePath() would fail with an
832   // error (EAGAIN, "resource unavailable try again") which we want to skip
833   // silently.
834   if (llvm::sys::fs::exists(pdbPath))
835     return normalizePdbPath(pdbPath);
836   std::string ret = getPdbBaseName(dependentFile, pdbPath);
837   if (llvm::sys::fs::exists(ret))
838     return normalizePdbPath(ret);
839   return None;
840 }
841 
PDBInputFile(MemoryBufferRef m)842 PDBInputFile::PDBInputFile(MemoryBufferRef m) : InputFile(PDBKind, m) {}
843 
844 PDBInputFile::~PDBInputFile() = default;
845 
findFromRecordPath(StringRef path,ObjFile * fromFile)846 PDBInputFile *PDBInputFile::findFromRecordPath(StringRef path,
847                                                ObjFile *fromFile) {
848   auto p = findPdbPath(path.str(), fromFile);
849   if (!p)
850     return nullptr;
851   auto it = PDBInputFile::instances.find(*p);
852   if (it != PDBInputFile::instances.end())
853     return it->second;
854   return nullptr;
855 }
856 
enqueue(StringRef path,ObjFile * fromFile)857 void PDBInputFile::enqueue(StringRef path, ObjFile *fromFile) {
858   auto p = findPdbPath(path.str(), fromFile);
859   if (!p)
860     return;
861   auto it = PDBInputFile::instances.emplace(*p, nullptr);
862   if (!it.second)
863     return; // already scheduled for load
864   driver->enqueuePDB(*p);
865 }
866 
parse()867 void PDBInputFile::parse() {
868   PDBInputFile::instances[mb.getBufferIdentifier().str()] = this;
869 
870   std::unique_ptr<pdb::IPDBSession> thisSession;
871   loadErr.emplace(pdb::NativeSession::createFromPdb(
872       MemoryBuffer::getMemBuffer(mb, false), thisSession));
873   if (*loadErr)
874     return; // fail silently at this point - the error will be handled later,
875             // when merging the debug type stream
876 
877   session.reset(static_cast<pdb::NativeSession *>(thisSession.release()));
878 
879   pdb::PDBFile &pdbFile = session->getPDBFile();
880   auto expectedInfo = pdbFile.getPDBInfoStream();
881   // All PDB Files should have an Info stream.
882   if (!expectedInfo) {
883     loadErr.emplace(expectedInfo.takeError());
884     return;
885   }
886   debugTypesObj = makeTypeServerSource(this);
887 }
888 
889 // Used only for DWARF debug info, which is not common (except in MinGW
890 // environments). This returns an optional pair of file name and line
891 // number for where the variable was defined.
892 Optional<std::pair<StringRef, uint32_t>>
getVariableLocation(StringRef var)893 ObjFile::getVariableLocation(StringRef var) {
894   if (!dwarf) {
895     dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
896     if (!dwarf)
897       return None;
898   }
899   if (config->machine == I386)
900     var.consume_front("_");
901   Optional<std::pair<std::string, unsigned>> ret = dwarf->getVariableLoc(var);
902   if (!ret)
903     return None;
904   return std::make_pair(saver.save(ret->first), ret->second);
905 }
906 
907 // Used only for DWARF debug info, which is not common (except in MinGW
908 // environments).
getDILineInfo(uint32_t offset,uint32_t sectionIndex)909 Optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
910                                             uint32_t sectionIndex) {
911   if (!dwarf) {
912     dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
913     if (!dwarf)
914       return None;
915   }
916 
917   return dwarf->getDILineInfo(offset, sectionIndex);
918 }
919 
ltrim1(StringRef s,const char * chars)920 static StringRef ltrim1(StringRef s, const char *chars) {
921   if (!s.empty() && strchr(chars, s[0]))
922     return s.substr(1);
923   return s;
924 }
925 
parse()926 void ImportFile::parse() {
927   const char *buf = mb.getBufferStart();
928   const auto *hdr = reinterpret_cast<const coff_import_header *>(buf);
929 
930   // Check if the total size is valid.
931   if (mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
932     fatal("broken import library");
933 
934   // Read names and create an __imp_ symbol.
935   StringRef name = saver.save(StringRef(buf + sizeof(*hdr)));
936   StringRef impName = saver.save("__imp_" + name);
937   const char *nameStart = buf + sizeof(coff_import_header) + name.size() + 1;
938   dllName = std::string(StringRef(nameStart));
939   StringRef extName;
940   switch (hdr->getNameType()) {
941   case IMPORT_ORDINAL:
942     extName = "";
943     break;
944   case IMPORT_NAME:
945     extName = name;
946     break;
947   case IMPORT_NAME_NOPREFIX:
948     extName = ltrim1(name, "?@_");
949     break;
950   case IMPORT_NAME_UNDECORATE:
951     extName = ltrim1(name, "?@_");
952     extName = extName.substr(0, extName.find('@'));
953     break;
954   }
955 
956   this->hdr = hdr;
957   externalName = extName;
958 
959   impSym = symtab->addImportData(impName, this);
960   // If this was a duplicate, we logged an error but may continue;
961   // in this case, impSym is nullptr.
962   if (!impSym)
963     return;
964 
965   if (hdr->getType() == llvm::COFF::IMPORT_CONST)
966     static_cast<void>(symtab->addImportData(name, this));
967 
968   // If type is function, we need to create a thunk which jump to an
969   // address pointed by the __imp_ symbol. (This allows you to call
970   // DLL functions just like regular non-DLL functions.)
971   if (hdr->getType() == llvm::COFF::IMPORT_CODE)
972     thunkSym = symtab->addImportThunk(
973         name, cast_or_null<DefinedImportData>(impSym), hdr->Machine);
974 }
975 
BitcodeFile(MemoryBufferRef mb,StringRef archiveName,uint64_t offsetInArchive)976 BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
977                          uint64_t offsetInArchive)
978     : BitcodeFile(mb, archiveName, offsetInArchive, {}) {}
979 
BitcodeFile(MemoryBufferRef mb,StringRef archiveName,uint64_t offsetInArchive,std::vector<Symbol * > && symbols)980 BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
981                          uint64_t offsetInArchive,
982                          std::vector<Symbol *> &&symbols)
983     : InputFile(BitcodeKind, mb), symbols(std::move(symbols)) {
984   std::string path = mb.getBufferIdentifier().str();
985   if (config->thinLTOIndexOnly)
986     path = replaceThinLTOSuffix(mb.getBufferIdentifier());
987 
988   // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
989   // name. If two archives define two members with the same name, this
990   // causes a collision which result in only one of the objects being taken
991   // into consideration at LTO time (which very likely causes undefined
992   // symbols later in the link stage). So we append file offset to make
993   // filename unique.
994   MemoryBufferRef mbref(
995       mb.getBuffer(),
996       saver.save(archiveName.empty() ? path
997                                      : archiveName + sys::path::filename(path) +
998                                            utostr(offsetInArchive)));
999 
1000   obj = check(lto::InputFile::create(mbref));
1001 }
1002 
1003 BitcodeFile::~BitcodeFile() = default;
1004 
parse()1005 void BitcodeFile::parse() {
1006   std::vector<std::pair<Symbol *, bool>> comdat(obj->getComdatTable().size());
1007   for (size_t i = 0; i != obj->getComdatTable().size(); ++i)
1008     // FIXME: lto::InputFile doesn't keep enough data to do correct comdat
1009     // selection handling.
1010     comdat[i] = symtab->addComdat(this, saver.save(obj->getComdatTable()[i]));
1011   for (const lto::InputFile::Symbol &objSym : obj->symbols()) {
1012     StringRef symName = saver.save(objSym.getName());
1013     int comdatIndex = objSym.getComdatIndex();
1014     Symbol *sym;
1015     if (objSym.isUndefined()) {
1016       sym = symtab->addUndefined(symName, this, false);
1017     } else if (objSym.isCommon()) {
1018       sym = symtab->addCommon(this, symName, objSym.getCommonSize());
1019     } else if (objSym.isWeak() && objSym.isIndirect()) {
1020       // Weak external.
1021       sym = symtab->addUndefined(symName, this, true);
1022       std::string fallback = std::string(objSym.getCOFFWeakExternalFallback());
1023       Symbol *alias = symtab->addUndefined(saver.save(fallback));
1024       checkAndSetWeakAlias(symtab, this, sym, alias);
1025     } else if (comdatIndex != -1) {
1026       if (symName == obj->getComdatTable()[comdatIndex])
1027         sym = comdat[comdatIndex].first;
1028       else if (comdat[comdatIndex].second)
1029         sym = symtab->addRegular(this, symName);
1030       else
1031         sym = symtab->addUndefined(symName, this, false);
1032     } else {
1033       sym = symtab->addRegular(this, symName);
1034     }
1035     symbols.push_back(sym);
1036     if (objSym.isUsed())
1037       config->gcroot.push_back(sym);
1038   }
1039   directives = obj->getCOFFLinkerOpts();
1040 }
1041 
getMachineType()1042 MachineTypes BitcodeFile::getMachineType() {
1043   switch (Triple(obj->getTargetTriple()).getArch()) {
1044   case Triple::x86_64:
1045     return AMD64;
1046   case Triple::x86:
1047     return I386;
1048   case Triple::arm:
1049     return ARMNT;
1050   case Triple::aarch64:
1051     return ARM64;
1052   default:
1053     return IMAGE_FILE_MACHINE_UNKNOWN;
1054   }
1055 }
1056 
replaceThinLTOSuffix(StringRef path)1057 std::string lld::coff::replaceThinLTOSuffix(StringRef path) {
1058   StringRef suffix = config->thinLTOObjectSuffixReplace.first;
1059   StringRef repl = config->thinLTOObjectSuffixReplace.second;
1060 
1061   if (path.consume_back(suffix))
1062     return (path + repl).str();
1063   return std::string(path);
1064 }
1065