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