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