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