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