xref: /llvm-project/lld/COFF/InputFiles.cpp (revision fb01a289038c16e13c6133ee602a58254b349411)
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 "COFFLinkerContext.h"
11 #include "Chunks.h"
12 #include "Config.h"
13 #include "DebugTypes.h"
14 #include "Driver.h"
15 #include "SymbolTable.h"
16 #include "Symbols.h"
17 #include "lld/Common/DWARF.h"
18 #include "llvm-c/lto.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/BinaryFormat/COFF.h"
22 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
23 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
24 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
25 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
26 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
27 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
28 #include "llvm/IR/Mangler.h"
29 #include "llvm/LTO/LTO.h"
30 #include "llvm/Object/Binary.h"
31 #include "llvm/Object/COFF.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/Endian.h"
34 #include "llvm/Support/Error.h"
35 #include "llvm/Support/ErrorOr.h"
36 #include "llvm/Support/FileSystem.h"
37 #include "llvm/Support/Path.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include "llvm/TargetParser/Triple.h"
40 #include <cstring>
41 #include <optional>
42 #include <system_error>
43 #include <utility>
44 
45 using namespace llvm;
46 using namespace llvm::COFF;
47 using namespace llvm::codeview;
48 using namespace llvm::object;
49 using namespace llvm::support::endian;
50 using namespace lld;
51 using namespace lld::coff;
52 
53 using llvm::Triple;
54 using llvm::support::ulittle32_t;
55 
56 // Returns the last element of a path, which is supposed to be a filename.
57 static StringRef getBasename(StringRef path) {
58   return sys::path::filename(path, sys::path::Style::windows);
59 }
60 
61 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
62 std::string lld::toString(const coff::InputFile *file) {
63   if (!file)
64     return "<internal>";
65   if (file->parentName.empty())
66     return std::string(file->getName());
67 
68   return (getBasename(file->parentName) + "(" + getBasename(file->getName()) +
69           ")")
70       .str();
71 }
72 
73 const COFFSyncStream &coff::operator<<(const COFFSyncStream &s,
74                                        const InputFile *f) {
75   return s << toString(f);
76 }
77 
78 /// Checks that Source is compatible with being a weak alias to Target.
79 /// If Source is Undefined and has no weak alias set, makes it a weak
80 /// alias to Target.
81 static void checkAndSetWeakAlias(SymbolTable &symtab, InputFile *f,
82                                  Symbol *source, Symbol *target,
83                                  bool isAntiDep) {
84   if (auto *u = dyn_cast<Undefined>(source)) {
85     if (u->weakAlias && u->weakAlias != target) {
86       // Ignore duplicated anti-dependency symbols.
87       if (isAntiDep)
88         return;
89       if (!u->isAntiDep) {
90         // Weak aliases as produced by GCC are named in the form
91         // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
92         // of another symbol emitted near the weak symbol.
93         // Just use the definition from the first object file that defined
94         // this weak symbol.
95         if (symtab.ctx.config.allowDuplicateWeak)
96           return;
97         symtab.reportDuplicate(source, f);
98       }
99     }
100     u->setWeakAlias(target, isAntiDep);
101   }
102 }
103 
104 static bool ignoredSymbolName(StringRef name) {
105   return name == "@feat.00" || name == "@comp.id";
106 }
107 
108 static coff_symbol_generic *cloneSymbol(COFFSymbolRef sym) {
109   if (sym.isBigObj()) {
110     auto *copy = make<coff_symbol32>(
111         *reinterpret_cast<const coff_symbol32 *>(sym.getRawPtr()));
112     return reinterpret_cast<coff_symbol_generic *>(copy);
113   } else {
114     auto *copy = make<coff_symbol16>(
115         *reinterpret_cast<const coff_symbol16 *>(sym.getRawPtr()));
116     return reinterpret_cast<coff_symbol_generic *>(copy);
117   }
118 }
119 
120 ArchiveFile::ArchiveFile(COFFLinkerContext &ctx, MemoryBufferRef m)
121     : InputFile(ctx.symtab, ArchiveKind, m) {}
122 
123 void ArchiveFile::parse() {
124   COFFLinkerContext &ctx = symtab.ctx;
125   // Parse a MemoryBufferRef as an archive file.
126   file = CHECK(Archive::create(mb), this);
127 
128   // Try to read symbols from ECSYMBOLS section on ARM64EC.
129   if (ctx.symtabEC) {
130     iterator_range<Archive::symbol_iterator> symbols =
131         CHECK(file->ec_symbols(), this);
132     if (!symbols.empty()) {
133       for (const Archive::Symbol &sym : symbols)
134         ctx.symtabEC->addLazyArchive(this, sym);
135 
136       // Read both EC and native symbols on ARM64X.
137       if (!ctx.hybridSymtab)
138         return;
139     }
140   }
141 
142   // Read the symbol table to construct Lazy objects.
143   for (const Archive::Symbol &sym : file->symbols())
144     ctx.symtab.addLazyArchive(this, sym);
145 }
146 
147 // Returns a buffer pointing to a member file containing a given symbol.
148 void ArchiveFile::addMember(const Archive::Symbol &sym) {
149   const Archive::Child &c =
150       CHECK(sym.getMember(), "could not get the member for symbol " +
151                                  toCOFFString(symtab.ctx, sym));
152 
153   // Return an empty buffer if we have already returned the same buffer.
154   // FIXME: Remove this once we resolve all defineds before all undefineds in
155   //        ObjFile::initializeSymbols().
156   if (!seen.insert(c.getChildOffset()).second)
157     return;
158 
159   symtab.ctx.driver.enqueueArchiveMember(c, sym, getName());
160 }
161 
162 std::vector<MemoryBufferRef>
163 lld::coff::getArchiveMembers(COFFLinkerContext &ctx, Archive *file) {
164   std::vector<MemoryBufferRef> v;
165   Error err = Error::success();
166 
167   // Thin archives refer to .o files, so --reproduces needs the .o files too.
168   bool addToTar = file->isThin() && ctx.driver.tar;
169 
170   for (const Archive::Child &c : file->children(err)) {
171     MemoryBufferRef mbref =
172         CHECK(c.getMemoryBufferRef(),
173               file->getFileName() +
174                   ": could not get the buffer for a child of the archive");
175     if (addToTar) {
176       ctx.driver.tar->append(relativeToRoot(check(c.getFullName())),
177                              mbref.getBuffer());
178     }
179     v.push_back(mbref);
180   }
181   if (err)
182     Fatal(ctx) << file->getFileName()
183                << ": Archive::children failed: " << toString(std::move(err));
184   return v;
185 }
186 
187 ObjFile::ObjFile(SymbolTable &symtab, COFFObjectFile *coffObj, bool lazy)
188     : InputFile(symtab, ObjectKind, coffObj->getMemoryBufferRef(), lazy),
189       coffObj(coffObj) {}
190 
191 ObjFile *ObjFile::create(COFFLinkerContext &ctx, MemoryBufferRef m, bool lazy) {
192   // Parse a memory buffer as a COFF file.
193   Expected<std::unique_ptr<Binary>> bin = createBinary(m);
194   if (!bin)
195     Fatal(ctx) << "Could not parse " << m.getBufferIdentifier();
196 
197   auto *obj = dyn_cast<COFFObjectFile>(bin->get());
198   if (!obj)
199     Fatal(ctx) << m.getBufferIdentifier() << " is not a COFF file";
200 
201   bin->release();
202   return make<ObjFile>(ctx.getSymtab(MachineTypes(obj->getMachine())), obj,
203                        lazy);
204 }
205 
206 void ObjFile::parseLazy() {
207   // Native object file.
208   uint32_t numSymbols = coffObj->getNumberOfSymbols();
209   for (uint32_t i = 0; i < numSymbols; ++i) {
210     COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
211     if (coffSym.isUndefined() || !coffSym.isExternal() ||
212         coffSym.isWeakExternal())
213       continue;
214     StringRef name = check(coffObj->getSymbolName(coffSym));
215     if (coffSym.isAbsolute() && ignoredSymbolName(name))
216       continue;
217     symtab.addLazyObject(this, name);
218     if (!lazy)
219       return;
220     i += coffSym.getNumberOfAuxSymbols();
221   }
222 }
223 
224 struct ECMapEntry {
225   ulittle32_t src;
226   ulittle32_t dst;
227   ulittle32_t type;
228 };
229 
230 void ObjFile::initializeECThunks() {
231   for (SectionChunk *chunk : hybmpChunks) {
232     if (chunk->getContents().size() % sizeof(ECMapEntry)) {
233       Err(symtab.ctx) << "Invalid .hybmp chunk size "
234                       << chunk->getContents().size();
235       continue;
236     }
237 
238     const uint8_t *end =
239         chunk->getContents().data() + chunk->getContents().size();
240     for (const uint8_t *iter = chunk->getContents().data(); iter != end;
241          iter += sizeof(ECMapEntry)) {
242       auto entry = reinterpret_cast<const ECMapEntry *>(iter);
243       switch (entry->type) {
244       case Arm64ECThunkType::Entry:
245         symtab.addEntryThunk(getSymbol(entry->src), getSymbol(entry->dst));
246         break;
247       case Arm64ECThunkType::Exit:
248         symtab.addExitThunk(getSymbol(entry->src), getSymbol(entry->dst));
249         break;
250       case Arm64ECThunkType::GuestExit:
251         break;
252       default:
253         Warn(symtab.ctx) << "Ignoring unknown EC thunk type " << entry->type;
254       }
255     }
256   }
257 }
258 
259 void ObjFile::parse() {
260   // Read section and symbol tables.
261   initializeChunks();
262   initializeSymbols();
263   initializeFlags();
264   initializeDependencies();
265   initializeECThunks();
266 }
267 
268 const coff_section *ObjFile::getSection(uint32_t i) {
269   auto sec = coffObj->getSection(i);
270   if (!sec)
271     Fatal(symtab.ctx) << "getSection failed: #" << i << ": " << sec.takeError();
272   return *sec;
273 }
274 
275 // We set SectionChunk pointers in the SparseChunks vector to this value
276 // temporarily to mark comdat sections as having an unknown resolution. As we
277 // walk the object file's symbol table, once we visit either a leader symbol or
278 // an associative section definition together with the parent comdat's leader,
279 // we set the pointer to either nullptr (to mark the section as discarded) or a
280 // valid SectionChunk for that section.
281 static SectionChunk *const pendingComdat = reinterpret_cast<SectionChunk *>(1);
282 
283 void ObjFile::initializeChunks() {
284   uint32_t numSections = coffObj->getNumberOfSections();
285   sparseChunks.resize(numSections + 1);
286   for (uint32_t i = 1; i < numSections + 1; ++i) {
287     const coff_section *sec = getSection(i);
288     if (sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
289       sparseChunks[i] = pendingComdat;
290     else
291       sparseChunks[i] = readSection(i, nullptr, "");
292   }
293 }
294 
295 SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
296                                    const coff_aux_section_definition *def,
297                                    StringRef leaderName) {
298   const coff_section *sec = getSection(sectionNumber);
299 
300   StringRef name;
301   if (Expected<StringRef> e = coffObj->getSectionName(sec))
302     name = *e;
303   else
304     Fatal(symtab.ctx) << "getSectionName failed: #" << sectionNumber << ": "
305                       << e.takeError();
306 
307   if (name == ".drectve") {
308     ArrayRef<uint8_t> data;
309     cantFail(coffObj->getSectionContents(sec, data));
310     directives = StringRef((const char *)data.data(), data.size());
311     return nullptr;
312   }
313 
314   if (name == ".llvm_addrsig") {
315     addrsigSec = sec;
316     return nullptr;
317   }
318 
319   if (name == ".llvm.call-graph-profile") {
320     callgraphSec = sec;
321     return nullptr;
322   }
323 
324   // Object files may have DWARF debug info or MS CodeView debug info
325   // (or both).
326   //
327   // DWARF sections don't need any special handling from the perspective
328   // of the linker; they are just a data section containing relocations.
329   // We can just link them to complete debug info.
330   //
331   // CodeView needs linker support. We need to interpret debug info,
332   // and then write it to a separate .pdb file.
333 
334   // Ignore DWARF debug info unless requested to be included.
335   if (!symtab.ctx.config.includeDwarfChunks && name.starts_with(".debug_"))
336     return nullptr;
337 
338   if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
339     return nullptr;
340   SectionChunk *c;
341   if (isArm64EC(getMachineType()))
342     c = make<SectionChunkEC>(this, sec);
343   else
344     c = make<SectionChunk>(this, sec);
345   if (def)
346     c->checksum = def->CheckSum;
347 
348   // CodeView sections are stored to a different vector because they are not
349   // linked in the regular manner.
350   if (c->isCodeView())
351     debugChunks.push_back(c);
352   else if (name == ".gfids$y")
353     guardFidChunks.push_back(c);
354   else if (name == ".giats$y")
355     guardIATChunks.push_back(c);
356   else if (name == ".gljmp$y")
357     guardLJmpChunks.push_back(c);
358   else if (name == ".gehcont$y")
359     guardEHContChunks.push_back(c);
360   else if (name == ".sxdata")
361     sxDataChunks.push_back(c);
362   else if (isArm64EC(getMachineType()) && name == ".hybmp$x")
363     hybmpChunks.push_back(c);
364   else if (symtab.ctx.config.tailMerge && sec->NumberOfRelocations == 0 &&
365            name == ".rdata" && leaderName.starts_with("??_C@"))
366     // COFF sections that look like string literal sections (i.e. no
367     // relocations, in .rdata, leader symbol name matches the MSVC name mangling
368     // for string literals) are subject to string tail merging.
369     MergeChunk::addSection(symtab.ctx, c);
370   else if (name == ".rsrc" || name.starts_with(".rsrc$"))
371     resourceChunks.push_back(c);
372   else if (!(sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_INFO))
373     chunks.push_back(c);
374 
375   return c;
376 }
377 
378 void ObjFile::includeResourceChunks() {
379   chunks.insert(chunks.end(), resourceChunks.begin(), resourceChunks.end());
380 }
381 
382 void ObjFile::readAssociativeDefinition(
383     COFFSymbolRef sym, const coff_aux_section_definition *def) {
384   readAssociativeDefinition(sym, def, def->getNumber(sym.isBigObj()));
385 }
386 
387 void ObjFile::readAssociativeDefinition(COFFSymbolRef sym,
388                                         const coff_aux_section_definition *def,
389                                         uint32_t parentIndex) {
390   SectionChunk *parent = sparseChunks[parentIndex];
391   int32_t sectionNumber = sym.getSectionNumber();
392 
393   auto diag = [&]() {
394     StringRef name = check(coffObj->getSymbolName(sym));
395 
396     StringRef parentName;
397     const coff_section *parentSec = getSection(parentIndex);
398     if (Expected<StringRef> e = coffObj->getSectionName(parentSec))
399       parentName = *e;
400     Err(symtab.ctx) << toString(this) << ": associative comdat " << name
401                     << " (sec " << sectionNumber
402                     << ") has invalid reference to section " << parentName
403                     << " (sec " << parentIndex << ")";
404   };
405 
406   if (parent == pendingComdat) {
407     // This can happen if an associative comdat refers to another associative
408     // comdat that appears after it (invalid per COFF spec) or to a section
409     // without any symbols.
410     diag();
411     return;
412   }
413 
414   // Check whether the parent is prevailing. If it is, so are we, and we read
415   // the section; otherwise mark it as discarded.
416   if (parent) {
417     SectionChunk *c = readSection(sectionNumber, def, "");
418     sparseChunks[sectionNumber] = c;
419     if (c) {
420       c->selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
421       parent->addAssociative(c);
422     }
423   } else {
424     sparseChunks[sectionNumber] = nullptr;
425   }
426 }
427 
428 void ObjFile::recordPrevailingSymbolForMingw(
429     COFFSymbolRef sym, DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
430   // For comdat symbols in executable sections, where this is the copy
431   // of the section chunk we actually include instead of discarding it,
432   // add the symbol to a map to allow using it for implicitly
433   // associating .[px]data$<func> sections to it.
434   // Use the suffix from the .text$<func> instead of the leader symbol
435   // name, for cases where the names differ (i386 mangling/decorations,
436   // cases where the leader is a weak symbol named .weak.func.default*).
437   int32_t sectionNumber = sym.getSectionNumber();
438   SectionChunk *sc = sparseChunks[sectionNumber];
439   if (sc && sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
440     StringRef name = sc->getSectionName().split('$').second;
441     prevailingSectionMap[name] = sectionNumber;
442   }
443 }
444 
445 void ObjFile::maybeAssociateSEHForMingw(
446     COFFSymbolRef sym, const coff_aux_section_definition *def,
447     const DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
448   StringRef name = check(coffObj->getSymbolName(sym));
449   if (name.consume_front(".pdata$") || name.consume_front(".xdata$") ||
450       name.consume_front(".eh_frame$")) {
451     // For MinGW, treat .[px]data$<func> and .eh_frame$<func> as implicitly
452     // associative to the symbol <func>.
453     auto parentSym = prevailingSectionMap.find(name);
454     if (parentSym != prevailingSectionMap.end())
455       readAssociativeDefinition(sym, def, parentSym->second);
456   }
457 }
458 
459 Symbol *ObjFile::createRegular(COFFSymbolRef sym) {
460   SectionChunk *sc = sparseChunks[sym.getSectionNumber()];
461   if (sym.isExternal()) {
462     StringRef name = check(coffObj->getSymbolName(sym));
463     if (sc)
464       return symtab.addRegular(this, name, sym.getGeneric(), sc,
465                                sym.getValue());
466     // For MinGW symbols named .weak.* that point to a discarded section,
467     // don't create an Undefined symbol. If nothing ever refers to the symbol,
468     // everything should be fine. If something actually refers to the symbol
469     // (e.g. the undefined weak alias), linking will fail due to undefined
470     // references at the end.
471     if (symtab.ctx.config.mingw && name.starts_with(".weak."))
472       return nullptr;
473     return symtab.addUndefined(name, this, false);
474   }
475   if (sc) {
476     const coff_symbol_generic *symGen = sym.getGeneric();
477     if (sym.isSection()) {
478       auto *customSymGen = cloneSymbol(sym);
479       customSymGen->Value = 0;
480       symGen = customSymGen;
481     }
482     return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
483                                 /*IsExternal*/ false, symGen, sc);
484   }
485   return nullptr;
486 }
487 
488 void ObjFile::initializeSymbols() {
489   uint32_t numSymbols = coffObj->getNumberOfSymbols();
490   symbols.resize(numSymbols);
491 
492   SmallVector<std::pair<Symbol *, const coff_aux_weak_external *>, 8>
493       weakAliases;
494   std::vector<uint32_t> pendingIndexes;
495   pendingIndexes.reserve(numSymbols);
496 
497   DenseMap<StringRef, uint32_t> prevailingSectionMap;
498   std::vector<const coff_aux_section_definition *> comdatDefs(
499       coffObj->getNumberOfSections() + 1);
500   COFFLinkerContext &ctx = symtab.ctx;
501 
502   for (uint32_t i = 0; i < numSymbols; ++i) {
503     COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
504     bool prevailingComdat;
505     if (coffSym.isUndefined()) {
506       symbols[i] = createUndefined(coffSym, false);
507     } else if (coffSym.isWeakExternal()) {
508       auto aux = coffSym.getAux<coff_aux_weak_external>();
509       bool overrideLazy = true;
510 
511       // On ARM64EC, external function calls emit a pair of weak-dependency
512       // aliases: func to #func and #func to the func guess exit thunk
513       // (instead of a single undefined func symbol, which would be emitted on
514       // other targets). Allow such aliases to be overridden by lazy archive
515       // symbols, just as we would for undefined symbols.
516       if (isArm64EC(getMachineType()) &&
517           aux->Characteristics == IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY) {
518         COFFSymbolRef targetSym = check(coffObj->getSymbol(aux->TagIndex));
519         if (!targetSym.isAnyUndefined()) {
520           // If the target is defined, it may be either a guess exit thunk or
521           // the actual implementation. If it's the latter, consider the alias
522           // to be part of the implementation and override potential lazy
523           // archive symbols.
524           StringRef targetName = check(coffObj->getSymbolName(targetSym));
525           StringRef name = check(coffObj->getSymbolName(coffSym));
526           std::optional<std::string> mangledName =
527               getArm64ECMangledFunctionName(name);
528           overrideLazy = mangledName == targetName;
529         } else {
530           overrideLazy = false;
531         }
532       }
533       symbols[i] = createUndefined(coffSym, overrideLazy);
534       weakAliases.emplace_back(symbols[i], aux);
535     } else if (std::optional<Symbol *> optSym =
536                    createDefined(coffSym, comdatDefs, prevailingComdat)) {
537       symbols[i] = *optSym;
538       if (ctx.config.mingw && prevailingComdat)
539         recordPrevailingSymbolForMingw(coffSym, prevailingSectionMap);
540     } else {
541       // createDefined() returns std::nullopt if a symbol belongs to a section
542       // that was pending at the point when the symbol was read. This can happen
543       // in two cases:
544       // 1) section definition symbol for a comdat leader;
545       // 2) symbol belongs to a comdat section associated with another section.
546       // In both of these cases, we can expect the section to be resolved by
547       // the time we finish visiting the remaining symbols in the symbol
548       // table. So we postpone the handling of this symbol until that time.
549       pendingIndexes.push_back(i);
550     }
551     i += coffSym.getNumberOfAuxSymbols();
552   }
553 
554   for (uint32_t i : pendingIndexes) {
555     COFFSymbolRef sym = check(coffObj->getSymbol(i));
556     if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
557       if (def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
558         readAssociativeDefinition(sym, def);
559       else if (ctx.config.mingw)
560         maybeAssociateSEHForMingw(sym, def, prevailingSectionMap);
561     }
562     if (sparseChunks[sym.getSectionNumber()] == pendingComdat) {
563       StringRef name = check(coffObj->getSymbolName(sym));
564       Log(ctx) << "comdat section " << name
565                << " without leader and unassociated, discarding";
566       continue;
567     }
568     symbols[i] = createRegular(sym);
569   }
570 
571   for (auto &kv : weakAliases) {
572     Symbol *sym = kv.first;
573     const coff_aux_weak_external *aux = kv.second;
574     checkAndSetWeakAlias(symtab, this, sym, symbols[aux->TagIndex],
575                          aux->Characteristics ==
576                              IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY);
577   }
578 
579   // Free the memory used by sparseChunks now that symbol loading is finished.
580   decltype(sparseChunks)().swap(sparseChunks);
581 }
582 
583 Symbol *ObjFile::createUndefined(COFFSymbolRef sym, bool overrideLazy) {
584   StringRef name = check(coffObj->getSymbolName(sym));
585   Symbol *s = symtab.addUndefined(name, this, overrideLazy);
586 
587   // Add an anti-dependency alias for undefined AMD64 symbols on the ARM64EC
588   // target.
589   if (symtab.isEC() && getMachineType() == AMD64) {
590     auto u = dyn_cast<Undefined>(s);
591     if (u && !u->weakAlias) {
592       if (std::optional<std::string> mangledName =
593               getArm64ECMangledFunctionName(name)) {
594         Symbol *m = symtab.addUndefined(saver().save(*mangledName), this,
595                                         /*overrideLazy=*/false);
596         u->setWeakAlias(m, /*antiDep=*/true);
597       }
598     }
599   }
600   return s;
601 }
602 
603 static const coff_aux_section_definition *findSectionDef(COFFObjectFile *obj,
604                                                          int32_t section) {
605   uint32_t numSymbols = obj->getNumberOfSymbols();
606   for (uint32_t i = 0; i < numSymbols; ++i) {
607     COFFSymbolRef sym = check(obj->getSymbol(i));
608     if (sym.getSectionNumber() != section)
609       continue;
610     if (const coff_aux_section_definition *def = sym.getSectionDefinition())
611       return def;
612   }
613   return nullptr;
614 }
615 
616 void ObjFile::handleComdatSelection(
617     COFFSymbolRef sym, COMDATType &selection, bool &prevailing,
618     DefinedRegular *leader,
619     const llvm::object::coff_aux_section_definition *def) {
620   if (prevailing)
621     return;
622   // There's already an existing comdat for this symbol: `Leader`.
623   // Use the comdats's selection field to determine if the new
624   // symbol in `Sym` should be discarded, produce a duplicate symbol
625   // error, etc.
626 
627   SectionChunk *leaderChunk = leader->getChunk();
628   COMDATType leaderSelection = leaderChunk->selection;
629   COFFLinkerContext &ctx = symtab.ctx;
630 
631   assert(leader->data && "Comdat leader without SectionChunk?");
632   if (isa<BitcodeFile>(leader->file)) {
633     // If the leader is only a LTO symbol, we don't know e.g. its final size
634     // yet, so we can't do the full strict comdat selection checking yet.
635     selection = leaderSelection = IMAGE_COMDAT_SELECT_ANY;
636   }
637 
638   if ((selection == IMAGE_COMDAT_SELECT_ANY &&
639        leaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
640       (selection == IMAGE_COMDAT_SELECT_LARGEST &&
641        leaderSelection == IMAGE_COMDAT_SELECT_ANY)) {
642     // cl.exe picks "any" for vftables when building with /GR- and
643     // "largest" when building with /GR. To be able to link object files
644     // compiled with each flag, "any" and "largest" are merged as "largest".
645     leaderSelection = selection = IMAGE_COMDAT_SELECT_LARGEST;
646   }
647 
648   // GCCs __declspec(selectany) doesn't actually pick "any" but "same size as".
649   // Clang on the other hand picks "any". To be able to link two object files
650   // with a __declspec(selectany) declaration, one compiled with gcc and the
651   // other with clang, we merge them as proper "same size as"
652   if (ctx.config.mingw && ((selection == IMAGE_COMDAT_SELECT_ANY &&
653                             leaderSelection == IMAGE_COMDAT_SELECT_SAME_SIZE) ||
654                            (selection == IMAGE_COMDAT_SELECT_SAME_SIZE &&
655                             leaderSelection == IMAGE_COMDAT_SELECT_ANY))) {
656     leaderSelection = selection = IMAGE_COMDAT_SELECT_SAME_SIZE;
657   }
658 
659   // Other than that, comdat selections must match.  This is a bit more
660   // strict than link.exe which allows merging "any" and "largest" if "any"
661   // is the first symbol the linker sees, and it allows merging "largest"
662   // with everything (!) if "largest" is the first symbol the linker sees.
663   // Making this symmetric independent of which selection is seen first
664   // seems better though.
665   // (This behavior matches ModuleLinker::getComdatResult().)
666   if (selection != leaderSelection) {
667     Log(ctx) << "conflicting comdat type for " << leader << ": "
668              << (int)leaderSelection << " in " << leader->getFile() << " and "
669              << (int)selection << " in " << this;
670     symtab.reportDuplicate(leader, this);
671     return;
672   }
673 
674   switch (selection) {
675   case IMAGE_COMDAT_SELECT_NODUPLICATES:
676     symtab.reportDuplicate(leader, this);
677     break;
678 
679   case IMAGE_COMDAT_SELECT_ANY:
680     // Nothing to do.
681     break;
682 
683   case IMAGE_COMDAT_SELECT_SAME_SIZE:
684     if (leaderChunk->getSize() != getSection(sym)->SizeOfRawData) {
685       if (!ctx.config.mingw) {
686         symtab.reportDuplicate(leader, this);
687       } else {
688         const coff_aux_section_definition *leaderDef = nullptr;
689         if (leaderChunk->file)
690           leaderDef = findSectionDef(leaderChunk->file->getCOFFObj(),
691                                      leaderChunk->getSectionNumber());
692         if (!leaderDef || leaderDef->Length != def->Length)
693           symtab.reportDuplicate(leader, this);
694       }
695     }
696     break;
697 
698   case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
699     SectionChunk newChunk(this, getSection(sym));
700     // link.exe only compares section contents here and doesn't complain
701     // if the two comdat sections have e.g. different alignment.
702     // Match that.
703     if (leaderChunk->getContents() != newChunk.getContents())
704       symtab.reportDuplicate(leader, this, &newChunk, sym.getValue());
705     break;
706   }
707 
708   case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
709     // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
710     // (This means lld-link doesn't produce duplicate symbol errors for
711     // associative comdats while link.exe does, but associate comdats
712     // are never extern in practice.)
713     llvm_unreachable("createDefined not called for associative comdats");
714 
715   case IMAGE_COMDAT_SELECT_LARGEST:
716     if (leaderChunk->getSize() < getSection(sym)->SizeOfRawData) {
717       // Replace the existing comdat symbol with the new one.
718       StringRef name = check(coffObj->getSymbolName(sym));
719       // FIXME: This is incorrect: With /opt:noref, the previous sections
720       // make it into the final executable as well. Correct handling would
721       // be to undo reading of the whole old section that's being replaced,
722       // or doing one pass that determines what the final largest comdat
723       // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
724       // only the largest one.
725       replaceSymbol<DefinedRegular>(leader, this, name, /*IsCOMDAT*/ true,
726                                     /*IsExternal*/ true, sym.getGeneric(),
727                                     nullptr);
728       prevailing = true;
729     }
730     break;
731 
732   case IMAGE_COMDAT_SELECT_NEWEST:
733     llvm_unreachable("should have been rejected earlier");
734   }
735 }
736 
737 std::optional<Symbol *> ObjFile::createDefined(
738     COFFSymbolRef sym,
739     std::vector<const coff_aux_section_definition *> &comdatDefs,
740     bool &prevailing) {
741   prevailing = false;
742   auto getName = [&]() { return check(coffObj->getSymbolName(sym)); };
743 
744   if (sym.isCommon()) {
745     auto *c = make<CommonChunk>(sym);
746     chunks.push_back(c);
747     return symtab.addCommon(this, getName(), sym.getValue(), sym.getGeneric(),
748                             c);
749   }
750 
751   COFFLinkerContext &ctx = symtab.ctx;
752   if (sym.isAbsolute()) {
753     StringRef name = getName();
754 
755     if (name == "@feat.00")
756       feat00Flags = sym.getValue();
757     // Skip special symbols.
758     if (ignoredSymbolName(name))
759       return nullptr;
760 
761     if (sym.isExternal())
762       return symtab.addAbsolute(name, sym);
763     return make<DefinedAbsolute>(ctx, name, sym);
764   }
765 
766   int32_t sectionNumber = sym.getSectionNumber();
767   if (sectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
768     return nullptr;
769 
770   if (sym.isEmptySectionDeclaration()) {
771     // As there is no coff_section in the object file for these, make a
772     // new virtual one, with everything zeroed out (i.e. an empty section),
773     // with only the name and characteristics set.
774     StringRef name = getName();
775     auto *hdr = make<coff_section>();
776     memset(hdr, 0, sizeof(*hdr));
777     strncpy(hdr->Name, name.data(),
778             std::min(name.size(), (size_t)COFF::NameSize));
779     // The Value field in a section symbol may contain the characteristics,
780     // or it may be zero, where we make something up (that matches what is
781     // used in .idata sections in the regular object files in import libraries).
782     if (sym.getValue())
783       hdr->Characteristics = sym.getValue() | IMAGE_SCN_ALIGN_4BYTES;
784     else
785       hdr->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA |
786                              IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE |
787                              IMAGE_SCN_ALIGN_4BYTES;
788     auto *sc = make<SectionChunk>(this, hdr);
789     chunks.push_back(sc);
790 
791     auto *symGen = cloneSymbol(sym);
792     // Ignore the Value offset of these symbols, as it may be a bitmask.
793     symGen->Value = 0;
794     return make<DefinedRegular>(this, /*name=*/"", /*isCOMDAT=*/false,
795                                 /*isExternal=*/false, symGen, sc);
796   }
797 
798   if (llvm::COFF::isReservedSectionNumber(sectionNumber))
799     Fatal(ctx) << toString(this) << ": " << getName()
800                << " should not refer to special section "
801                << Twine(sectionNumber);
802 
803   if ((uint32_t)sectionNumber >= sparseChunks.size())
804     Fatal(ctx) << toString(this) << ": " << getName()
805                << " should not refer to non-existent section "
806                << Twine(sectionNumber);
807 
808   // Comdat handling.
809   // A comdat symbol consists of two symbol table entries.
810   // The first symbol entry has the name of the section (e.g. .text), fixed
811   // values for the other fields, and one auxiliary record.
812   // The second symbol entry has the name of the comdat symbol, called the
813   // "comdat leader".
814   // When this function is called for the first symbol entry of a comdat,
815   // it sets comdatDefs and returns std::nullopt, and when it's called for the
816   // second symbol entry it reads comdatDefs and then sets it back to nullptr.
817 
818   // Handle comdat leader.
819   if (const coff_aux_section_definition *def = comdatDefs[sectionNumber]) {
820     comdatDefs[sectionNumber] = nullptr;
821     DefinedRegular *leader;
822 
823     if (sym.isExternal()) {
824       std::tie(leader, prevailing) =
825           symtab.addComdat(this, getName(), sym.getGeneric());
826     } else {
827       leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
828                                     /*IsExternal*/ false, sym.getGeneric());
829       prevailing = true;
830     }
831 
832     if (def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
833         // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
834         // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
835         def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
836       Fatal(ctx) << "unknown comdat type "
837                  << std::to_string((int)def->Selection) << " for " << getName()
838                  << " in " << toString(this);
839     }
840     COMDATType selection = (COMDATType)def->Selection;
841 
842     if (leader->isCOMDAT)
843       handleComdatSelection(sym, selection, prevailing, leader, def);
844 
845     if (prevailing) {
846       SectionChunk *c = readSection(sectionNumber, def, getName());
847       sparseChunks[sectionNumber] = c;
848       if (!c)
849         return nullptr;
850       c->sym = cast<DefinedRegular>(leader);
851       c->selection = selection;
852       cast<DefinedRegular>(leader)->data = &c->repl;
853     } else {
854       sparseChunks[sectionNumber] = nullptr;
855     }
856     return leader;
857   }
858 
859   // Prepare to handle the comdat leader symbol by setting the section's
860   // ComdatDefs pointer if we encounter a non-associative comdat.
861   if (sparseChunks[sectionNumber] == pendingComdat) {
862     if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
863       if (def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
864         comdatDefs[sectionNumber] = def;
865     }
866     return std::nullopt;
867   }
868 
869   return createRegular(sym);
870 }
871 
872 MachineTypes ObjFile::getMachineType() const {
873   return static_cast<MachineTypes>(coffObj->getMachine());
874 }
875 
876 ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {
877   if (SectionChunk *sec = SectionChunk::findByName(debugChunks, secName))
878     return sec->consumeDebugMagic();
879   return {};
880 }
881 
882 // OBJ files systematically store critical information in a .debug$S stream,
883 // even if the TU was compiled with no debug info. At least two records are
884 // always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
885 // PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
886 // currently used to initialize the hotPatchable member.
887 void ObjFile::initializeFlags() {
888   ArrayRef<uint8_t> data = getDebugSection(".debug$S");
889   if (data.empty())
890     return;
891 
892   DebugSubsectionArray subsections;
893 
894   BinaryStreamReader reader(data, llvm::endianness::little);
895   ExitOnError exitOnErr;
896   exitOnErr(reader.readArray(subsections, data.size()));
897 
898   for (const DebugSubsectionRecord &ss : subsections) {
899     if (ss.kind() != DebugSubsectionKind::Symbols)
900       continue;
901 
902     unsigned offset = 0;
903 
904     // Only parse the first two records. We are only looking for S_OBJNAME
905     // and S_COMPILE3, and they usually appear at the beginning of the
906     // stream.
907     for (unsigned i = 0; i < 2; ++i) {
908       Expected<CVSymbol> sym = readSymbolFromStream(ss.getRecordData(), offset);
909       if (!sym) {
910         consumeError(sym.takeError());
911         return;
912       }
913       if (sym->kind() == SymbolKind::S_COMPILE3) {
914         auto cs =
915             cantFail(SymbolDeserializer::deserializeAs<Compile3Sym>(sym.get()));
916         hotPatchable =
917             (cs.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
918       }
919       if (sym->kind() == SymbolKind::S_OBJNAME) {
920         auto objName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
921             sym.get()));
922         if (objName.Signature)
923           pchSignature = objName.Signature;
924       }
925       offset += sym->length();
926     }
927   }
928 }
929 
930 // Depending on the compilation flags, OBJs can refer to external files,
931 // necessary to merge this OBJ into the final PDB. We currently support two
932 // types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
933 // And PDB type servers, when compiling with /Zi. This function extracts these
934 // dependencies and makes them available as a TpiSource interface (see
935 // DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
936 // output even with /Yc and /Yu and with /Zi.
937 void ObjFile::initializeDependencies() {
938   COFFLinkerContext &ctx = symtab.ctx;
939   if (!ctx.config.debug)
940     return;
941 
942   bool isPCH = false;
943 
944   ArrayRef<uint8_t> data = getDebugSection(".debug$P");
945   if (!data.empty())
946     isPCH = true;
947   else
948     data = getDebugSection(".debug$T");
949 
950   // symbols but no types, make a plain, empty TpiSource anyway, because it
951   // simplifies adding the symbols later.
952   if (data.empty()) {
953     if (!debugChunks.empty())
954       debugTypesObj = makeTpiSource(ctx, this);
955     return;
956   }
957 
958   // Get the first type record. It will indicate if this object uses a type
959   // server (/Zi) or a PCH file (/Yu).
960   CVTypeArray types;
961   BinaryStreamReader reader(data, llvm::endianness::little);
962   cantFail(reader.readArray(types, reader.getLength()));
963   CVTypeArray::Iterator firstType = types.begin();
964   if (firstType == types.end())
965     return;
966 
967   // Remember the .debug$T or .debug$P section.
968   debugTypes = data;
969 
970   // This object file is a PCH file that others will depend on.
971   if (isPCH) {
972     debugTypesObj = makePrecompSource(ctx, this);
973     return;
974   }
975 
976   // This object file was compiled with /Zi. Enqueue the PDB dependency.
977   if (firstType->kind() == LF_TYPESERVER2) {
978     TypeServer2Record ts = cantFail(
979         TypeDeserializer::deserializeAs<TypeServer2Record>(firstType->data()));
980     debugTypesObj = makeUseTypeServerSource(ctx, this, ts);
981     enqueuePdbFile(ts.getName(), this);
982     return;
983   }
984 
985   // This object was compiled with /Yu. It uses types from another object file
986   // with a matching signature.
987   if (firstType->kind() == LF_PRECOMP) {
988     PrecompRecord precomp = cantFail(
989         TypeDeserializer::deserializeAs<PrecompRecord>(firstType->data()));
990     // We're better off trusting the LF_PRECOMP signature. In some cases the
991     // S_OBJNAME record doesn't contain a valid PCH signature.
992     if (precomp.Signature)
993       pchSignature = precomp.Signature;
994     debugTypesObj = makeUsePrecompSource(ctx, this, precomp);
995     // Drop the LF_PRECOMP record from the input stream.
996     debugTypes = debugTypes.drop_front(firstType->RecordData.size());
997     return;
998   }
999 
1000   // This is a plain old object file.
1001   debugTypesObj = makeTpiSource(ctx, this);
1002 }
1003 
1004 // The casing of the PDB path stamped in the OBJ can differ from the actual path
1005 // on disk. With this, we ensure to always use lowercase as a key for the
1006 // pdbInputFileInstances map, at least on Windows.
1007 static std::string normalizePdbPath(StringRef path) {
1008 #if defined(_WIN32)
1009   return path.lower();
1010 #else // LINUX
1011   return std::string(path);
1012 #endif
1013 }
1014 
1015 // If existing, return the actual PDB path on disk.
1016 static std::optional<std::string>
1017 findPdbPath(StringRef pdbPath, ObjFile *dependentFile, StringRef outputPath) {
1018   // Ensure the file exists before anything else. In some cases, if the path
1019   // points to a removable device, Driver::enqueuePath() would fail with an
1020   // error (EAGAIN, "resource unavailable try again") which we want to skip
1021   // silently.
1022   if (llvm::sys::fs::exists(pdbPath))
1023     return normalizePdbPath(pdbPath);
1024 
1025   StringRef objPath = !dependentFile->parentName.empty()
1026                           ? dependentFile->parentName
1027                           : dependentFile->getName();
1028 
1029   // Currently, type server PDBs are only created by MSVC cl, which only runs
1030   // on Windows, so we can assume type server paths are Windows style.
1031   StringRef pdbName = sys::path::filename(pdbPath, sys::path::Style::windows);
1032 
1033   // Check if the PDB is in the same folder as the OBJ.
1034   SmallString<128> path;
1035   sys::path::append(path, sys::path::parent_path(objPath), pdbName);
1036   if (llvm::sys::fs::exists(path))
1037     return normalizePdbPath(path);
1038 
1039   // Check if the PDB is in the output folder.
1040   path.clear();
1041   sys::path::append(path, sys::path::parent_path(outputPath), pdbName);
1042   if (llvm::sys::fs::exists(path))
1043     return normalizePdbPath(path);
1044 
1045   return std::nullopt;
1046 }
1047 
1048 PDBInputFile::PDBInputFile(COFFLinkerContext &ctx, MemoryBufferRef m)
1049     : InputFile(ctx.symtab, PDBKind, m) {}
1050 
1051 PDBInputFile::~PDBInputFile() = default;
1052 
1053 PDBInputFile *PDBInputFile::findFromRecordPath(const COFFLinkerContext &ctx,
1054                                                StringRef path,
1055                                                ObjFile *fromFile) {
1056   auto p = findPdbPath(path.str(), fromFile, ctx.config.outputFile);
1057   if (!p)
1058     return nullptr;
1059   auto it = ctx.pdbInputFileInstances.find(*p);
1060   if (it != ctx.pdbInputFileInstances.end())
1061     return it->second;
1062   return nullptr;
1063 }
1064 
1065 void PDBInputFile::parse() {
1066   symtab.ctx.pdbInputFileInstances[mb.getBufferIdentifier().str()] = this;
1067 
1068   std::unique_ptr<pdb::IPDBSession> thisSession;
1069   Error E = pdb::NativeSession::createFromPdb(
1070       MemoryBuffer::getMemBuffer(mb, false), thisSession);
1071   if (E) {
1072     loadErrorStr.emplace(toString(std::move(E)));
1073     return; // fail silently at this point - the error will be handled later,
1074             // when merging the debug type stream
1075   }
1076 
1077   session.reset(static_cast<pdb::NativeSession *>(thisSession.release()));
1078 
1079   pdb::PDBFile &pdbFile = session->getPDBFile();
1080   auto expectedInfo = pdbFile.getPDBInfoStream();
1081   // All PDB Files should have an Info stream.
1082   if (!expectedInfo) {
1083     loadErrorStr.emplace(toString(expectedInfo.takeError()));
1084     return;
1085   }
1086   debugTypesObj = makeTypeServerSource(symtab.ctx, this);
1087 }
1088 
1089 // Used only for DWARF debug info, which is not common (except in MinGW
1090 // environments). This returns an optional pair of file name and line
1091 // number for where the variable was defined.
1092 std::optional<std::pair<StringRef, uint32_t>>
1093 ObjFile::getVariableLocation(StringRef var) {
1094   if (!dwarf) {
1095     dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
1096     if (!dwarf)
1097       return std::nullopt;
1098   }
1099   if (symtab.machine == I386)
1100     var.consume_front("_");
1101   std::optional<std::pair<std::string, unsigned>> ret =
1102       dwarf->getVariableLoc(var);
1103   if (!ret)
1104     return std::nullopt;
1105   return std::make_pair(saver().save(ret->first), ret->second);
1106 }
1107 
1108 // Used only for DWARF debug info, which is not common (except in MinGW
1109 // environments).
1110 std::optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
1111                                                  uint32_t sectionIndex) {
1112   if (!dwarf) {
1113     dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
1114     if (!dwarf)
1115       return std::nullopt;
1116   }
1117 
1118   return dwarf->getDILineInfo(offset, sectionIndex);
1119 }
1120 
1121 void ObjFile::enqueuePdbFile(StringRef path, ObjFile *fromFile) {
1122   auto p = findPdbPath(path.str(), fromFile, symtab.ctx.config.outputFile);
1123   if (!p)
1124     return;
1125   auto it = symtab.ctx.pdbInputFileInstances.emplace(*p, nullptr);
1126   if (!it.second)
1127     return; // already scheduled for load
1128   symtab.ctx.driver.enqueuePDB(*p);
1129 }
1130 
1131 ImportFile::ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m)
1132     : InputFile(ctx.getSymtab(getMachineType(m)), ImportKind, m),
1133       live(!ctx.config.doGC) {}
1134 
1135 MachineTypes ImportFile::getMachineType(MemoryBufferRef m) {
1136   uint16_t machine =
1137       reinterpret_cast<const coff_import_header *>(m.getBufferStart())->Machine;
1138   return MachineTypes(machine);
1139 }
1140 
1141 bool ImportFile::isSameImport(const ImportFile *other) const {
1142   if (!externalName.empty())
1143     return other->externalName == externalName;
1144   return hdr->OrdinalHint == other->hdr->OrdinalHint;
1145 }
1146 
1147 ImportThunkChunk *ImportFile::makeImportThunk() {
1148   switch (hdr->Machine) {
1149   case AMD64:
1150     return make<ImportThunkChunkX64>(symtab.ctx, impSym);
1151   case I386:
1152     return make<ImportThunkChunkX86>(symtab.ctx, impSym);
1153   case ARM64:
1154     return make<ImportThunkChunkARM64>(symtab.ctx, impSym, ARM64);
1155   case ARMNT:
1156     return make<ImportThunkChunkARM>(symtab.ctx, impSym);
1157   }
1158   llvm_unreachable("unknown machine type");
1159 }
1160 
1161 void ImportFile::parse() {
1162   const auto *hdr =
1163       reinterpret_cast<const coff_import_header *>(mb.getBufferStart());
1164 
1165   // Check if the total size is valid.
1166   if (mb.getBufferSize() < sizeof(*hdr) ||
1167       mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
1168     Fatal(symtab.ctx) << "broken import library";
1169 
1170   // Read names and create an __imp_ symbol.
1171   StringRef buf = mb.getBuffer().substr(sizeof(*hdr));
1172   auto split = buf.split('\0');
1173   buf = split.second;
1174   StringRef name;
1175   if (isArm64EC(hdr->Machine)) {
1176     if (std::optional<std::string> demangledName =
1177             getArm64ECDemangledFunctionName(split.first))
1178       name = saver().save(*demangledName);
1179   }
1180   if (name.empty())
1181     name = saver().save(split.first);
1182   StringRef impName = saver().save("__imp_" + name);
1183   dllName = buf.split('\0').first;
1184   StringRef extName;
1185   switch (hdr->getNameType()) {
1186   case IMPORT_ORDINAL:
1187     extName = "";
1188     break;
1189   case IMPORT_NAME:
1190     extName = name;
1191     break;
1192   case IMPORT_NAME_NOPREFIX:
1193     extName = ltrim1(name, "?@_");
1194     break;
1195   case IMPORT_NAME_UNDECORATE:
1196     extName = ltrim1(name, "?@_");
1197     extName = extName.substr(0, extName.find('@'));
1198     break;
1199   case IMPORT_NAME_EXPORTAS:
1200     extName = buf.substr(dllName.size() + 1).split('\0').first;
1201     break;
1202   }
1203 
1204   this->hdr = hdr;
1205   externalName = extName;
1206 
1207   bool isCode = hdr->getType() == llvm::COFF::IMPORT_CODE;
1208 
1209   if (!symtab.isEC()) {
1210     impSym = symtab.addImportData(impName, this, location);
1211   } else {
1212     // In addition to the regular IAT, ARM64EC also contains an auxiliary IAT,
1213     // which holds addresses that are guaranteed to be callable directly from
1214     // ARM64 code. Function symbol naming is swapped: __imp_ symbols refer to
1215     // the auxiliary IAT, while __imp_aux_ symbols refer to the regular IAT. For
1216     // data imports, the naming is reversed.
1217     StringRef auxImpName = saver().save("__imp_aux_" + name);
1218     if (isCode) {
1219       impSym = symtab.addImportData(auxImpName, this, location);
1220       impECSym = symtab.addImportData(impName, this, auxLocation);
1221     } else {
1222       impSym = symtab.addImportData(impName, this, location);
1223       impECSym = symtab.addImportData(auxImpName, this, auxLocation);
1224     }
1225     if (!impECSym)
1226       return;
1227 
1228     StringRef auxImpCopyName = saver().save("__auximpcopy_" + name);
1229     auxImpCopySym = symtab.addImportData(auxImpCopyName, this, auxCopyLocation);
1230     if (!auxImpCopySym)
1231       return;
1232   }
1233   // If this was a duplicate, we logged an error but may continue;
1234   // in this case, impSym is nullptr.
1235   if (!impSym)
1236     return;
1237 
1238   if (hdr->getType() == llvm::COFF::IMPORT_CONST)
1239     static_cast<void>(symtab.addImportData(name, this, location));
1240 
1241   // If type is function, we need to create a thunk which jump to an
1242   // address pointed by the __imp_ symbol. (This allows you to call
1243   // DLL functions just like regular non-DLL functions.)
1244   if (isCode) {
1245     if (!symtab.isEC()) {
1246       thunkSym = symtab.addImportThunk(name, impSym, makeImportThunk());
1247     } else {
1248       thunkSym = symtab.addImportThunk(
1249           name, impSym, make<ImportThunkChunkX64>(symtab.ctx, impSym));
1250 
1251       if (std::optional<std::string> mangledName =
1252               getArm64ECMangledFunctionName(name)) {
1253         StringRef auxThunkName = saver().save(*mangledName);
1254         auxThunkSym = symtab.addImportThunk(
1255             auxThunkName, impECSym,
1256             make<ImportThunkChunkARM64>(symtab.ctx, impECSym, ARM64EC));
1257       }
1258 
1259       StringRef impChkName = saver().save("__impchk_" + name);
1260       impchkThunk = make<ImportThunkChunkARM64EC>(this);
1261       impchkThunk->sym = symtab.addImportThunk(impChkName, impSym, impchkThunk);
1262       symtab.ctx.driver.pullArm64ECIcallHelper();
1263     }
1264   }
1265 }
1266 
1267 BitcodeFile::BitcodeFile(SymbolTable &symtab, MemoryBufferRef mb,
1268                          std::unique_ptr<lto::InputFile> &o, bool lazy)
1269     : InputFile(symtab, BitcodeKind, mb, lazy) {
1270   obj.swap(o);
1271 }
1272 
1273 BitcodeFile *BitcodeFile::create(COFFLinkerContext &ctx, MemoryBufferRef mb,
1274                                  StringRef archiveName,
1275                                  uint64_t offsetInArchive, bool lazy) {
1276   std::string path = mb.getBufferIdentifier().str();
1277   if (ctx.config.thinLTOIndexOnly)
1278     path = replaceThinLTOSuffix(mb.getBufferIdentifier(),
1279                                 ctx.config.thinLTOObjectSuffixReplace.first,
1280                                 ctx.config.thinLTOObjectSuffixReplace.second);
1281 
1282   // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
1283   // name. If two archives define two members with the same name, this
1284   // causes a collision which result in only one of the objects being taken
1285   // into consideration at LTO time (which very likely causes undefined
1286   // symbols later in the link stage). So we append file offset to make
1287   // filename unique.
1288   MemoryBufferRef mbref(mb.getBuffer(),
1289                         saver().save(archiveName.empty()
1290                                          ? path
1291                                          : archiveName +
1292                                                sys::path::filename(path) +
1293                                                utostr(offsetInArchive)));
1294 
1295   std::unique_ptr<lto::InputFile> obj = check(lto::InputFile::create(mbref));
1296   return make<BitcodeFile>(ctx.getSymtab(getMachineType(obj.get())), mb, obj,
1297                            lazy);
1298 }
1299 
1300 BitcodeFile::~BitcodeFile() = default;
1301 
1302 void BitcodeFile::parse() {
1303   llvm::StringSaver &saver = lld::saver();
1304 
1305   std::vector<std::pair<Symbol *, bool>> comdat(obj->getComdatTable().size());
1306   for (size_t i = 0; i != obj->getComdatTable().size(); ++i)
1307     // FIXME: Check nodeduplicate
1308     comdat[i] =
1309         symtab.addComdat(this, saver.save(obj->getComdatTable()[i].first));
1310   for (const lto::InputFile::Symbol &objSym : obj->symbols()) {
1311     StringRef symName = saver.save(objSym.getName());
1312     int comdatIndex = objSym.getComdatIndex();
1313     Symbol *sym;
1314     SectionChunk *fakeSC = nullptr;
1315     if (objSym.isExecutable())
1316       fakeSC = &symtab.ctx.ltoTextSectionChunk.chunk;
1317     else
1318       fakeSC = &symtab.ctx.ltoDataSectionChunk.chunk;
1319     if (objSym.isUndefined()) {
1320       sym = symtab.addUndefined(symName, this, false);
1321       if (objSym.isWeak())
1322         sym->deferUndefined = true;
1323       // If one LTO object file references (i.e. has an undefined reference to)
1324       // a symbol with an __imp_ prefix, the LTO compilation itself sees it
1325       // as unprefixed but with a dllimport attribute instead, and doesn't
1326       // understand the relation to a concrete IR symbol with the __imp_ prefix.
1327       //
1328       // For such cases, mark the symbol as used in a regular object (i.e. the
1329       // symbol must be retained) so that the linker can associate the
1330       // references in the end. If the symbol is defined in an import library
1331       // or in a regular object file, this has no effect, but if it is defined
1332       // in another LTO object file, this makes sure it is kept, to fulfill
1333       // the reference when linking the output of the LTO compilation.
1334       if (symName.starts_with("__imp_"))
1335         sym->isUsedInRegularObj = true;
1336     } else if (objSym.isCommon()) {
1337       sym = symtab.addCommon(this, symName, objSym.getCommonSize());
1338     } else if (objSym.isWeak() && objSym.isIndirect()) {
1339       // Weak external.
1340       sym = symtab.addUndefined(symName, this, true);
1341       std::string fallback = std::string(objSym.getCOFFWeakExternalFallback());
1342       Symbol *alias = symtab.addUndefined(saver.save(fallback));
1343       checkAndSetWeakAlias(symtab, this, sym, alias, false);
1344     } else if (comdatIndex != -1) {
1345       if (symName == obj->getComdatTable()[comdatIndex].first) {
1346         sym = comdat[comdatIndex].first;
1347         if (cast<DefinedRegular>(sym)->data == nullptr)
1348           cast<DefinedRegular>(sym)->data = &fakeSC->repl;
1349       } else if (comdat[comdatIndex].second) {
1350         sym = symtab.addRegular(this, symName, nullptr, fakeSC);
1351       } else {
1352         sym = symtab.addUndefined(symName, this, false);
1353       }
1354     } else {
1355       sym =
1356           symtab.addRegular(this, symName, nullptr, fakeSC, 0, objSym.isWeak());
1357     }
1358     symbols.push_back(sym);
1359     if (objSym.isUsed())
1360       symtab.ctx.config.gcroot.push_back(sym);
1361   }
1362   directives = saver.save(obj->getCOFFLinkerOpts());
1363 }
1364 
1365 void BitcodeFile::parseLazy() {
1366   for (const lto::InputFile::Symbol &sym : obj->symbols())
1367     if (!sym.isUndefined()) {
1368       symtab.addLazyObject(this, sym.getName());
1369       if (!lazy)
1370         return;
1371     }
1372 }
1373 
1374 MachineTypes BitcodeFile::getMachineType(const llvm::lto::InputFile *obj) {
1375   Triple t(obj->getTargetTriple());
1376   switch (t.getArch()) {
1377   case Triple::x86_64:
1378     return AMD64;
1379   case Triple::x86:
1380     return I386;
1381   case Triple::arm:
1382   case Triple::thumb:
1383     return ARMNT;
1384   case Triple::aarch64:
1385     return t.isWindowsArm64EC() ? ARM64EC : ARM64;
1386   default:
1387     return IMAGE_FILE_MACHINE_UNKNOWN;
1388   }
1389 }
1390 
1391 std::string lld::coff::replaceThinLTOSuffix(StringRef path, StringRef suffix,
1392                                             StringRef repl) {
1393   if (path.consume_back(suffix))
1394     return (path + repl).str();
1395   return std::string(path);
1396 }
1397 
1398 static bool isRVACode(COFFObjectFile *coffObj, uint64_t rva, InputFile *file) {
1399   for (size_t i = 1, e = coffObj->getNumberOfSections(); i <= e; i++) {
1400     const coff_section *sec = CHECK(coffObj->getSection(i), file);
1401     if (rva >= sec->VirtualAddress &&
1402         rva <= sec->VirtualAddress + sec->VirtualSize) {
1403       return (sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE) != 0;
1404     }
1405   }
1406   return false;
1407 }
1408 
1409 void DLLFile::parse() {
1410   // Parse a memory buffer as a PE-COFF executable.
1411   std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
1412 
1413   if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
1414     bin.release();
1415     coffObj.reset(obj);
1416   } else {
1417     Err(symtab.ctx) << toString(this) << " is not a COFF file";
1418     return;
1419   }
1420 
1421   if (!coffObj->getPE32Header() && !coffObj->getPE32PlusHeader()) {
1422     Err(symtab.ctx) << toString(this) << " is not a PE-COFF executable";
1423     return;
1424   }
1425 
1426   for (const auto &exp : coffObj->export_directories()) {
1427     StringRef dllName, symbolName;
1428     uint32_t exportRVA;
1429     checkError(exp.getDllName(dllName));
1430     checkError(exp.getSymbolName(symbolName));
1431     checkError(exp.getExportRVA(exportRVA));
1432 
1433     if (symbolName.empty())
1434       continue;
1435 
1436     bool code = isRVACode(coffObj.get(), exportRVA, this);
1437 
1438     Symbol *s = make<Symbol>();
1439     s->dllName = dllName;
1440     s->symbolName = symbolName;
1441     s->importType = code ? ImportType::IMPORT_CODE : ImportType::IMPORT_DATA;
1442     s->nameType = ImportNameType::IMPORT_NAME;
1443 
1444     if (coffObj->getMachine() == I386) {
1445       s->symbolName = symbolName = saver().save("_" + symbolName);
1446       s->nameType = ImportNameType::IMPORT_NAME_NOPREFIX;
1447     }
1448 
1449     StringRef impName = saver().save("__imp_" + symbolName);
1450     symtab.addLazyDLLSymbol(this, s, impName);
1451     if (code)
1452       symtab.addLazyDLLSymbol(this, s, symbolName);
1453   }
1454 }
1455 
1456 MachineTypes DLLFile::getMachineType() const {
1457   if (coffObj)
1458     return static_cast<MachineTypes>(coffObj->getMachine());
1459   return IMAGE_FILE_MACHINE_UNKNOWN;
1460 }
1461 
1462 void DLLFile::makeImport(DLLFile::Symbol *s) {
1463   if (!seen.insert(s->symbolName).second)
1464     return;
1465 
1466   size_t impSize = s->dllName.size() + s->symbolName.size() + 2; // +2 for NULs
1467   size_t size = sizeof(coff_import_header) + impSize;
1468   char *buf = bAlloc().Allocate<char>(size);
1469   memset(buf, 0, size);
1470   char *p = buf;
1471   auto *imp = reinterpret_cast<coff_import_header *>(p);
1472   p += sizeof(*imp);
1473   imp->Sig2 = 0xFFFF;
1474   imp->Machine = coffObj->getMachine();
1475   imp->SizeOfData = impSize;
1476   imp->OrdinalHint = 0; // Only linking by name
1477   imp->TypeInfo = (s->nameType << 2) | s->importType;
1478 
1479   // Write symbol name and DLL name.
1480   memcpy(p, s->symbolName.data(), s->symbolName.size());
1481   p += s->symbolName.size() + 1;
1482   memcpy(p, s->dllName.data(), s->dllName.size());
1483   MemoryBufferRef mbref = MemoryBufferRef(StringRef(buf, size), s->dllName);
1484   ImportFile *impFile = make<ImportFile>(symtab.ctx, mbref);
1485   symtab.ctx.driver.addFile(impFile);
1486 }
1487