1 //===- InputFiles.cpp -----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "InputFiles.h"
12 #include "DebugTypes.h"
14 #include "SymbolTable.h"
16 #include "lld/Common/DWARF.h"
17 #include "lld/Common/ErrorHandler.h"
18 #include "lld/Common/Memory.h"
19 #include "llvm-c/lto.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/BinaryFormat/COFF.h"
24 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
25 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
26 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
27 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
28 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
29 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
30 #include "llvm/LTO/LTO.h"
31 #include "llvm/Object/Binary.h"
32 #include "llvm/Object/COFF.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/Error.h"
36 #include "llvm/Support/ErrorOr.h"
37 #include "llvm/Support/FileSystem.h"
38 #include "llvm/Support/Path.h"
39 #include "llvm/Target/TargetOptions.h"
41 #include <system_error>
45 using namespace llvm::COFF;
46 using namespace llvm::codeview;
47 using namespace llvm::object;
48 using namespace llvm::support::endian;
50 using namespace lld::coff;
53 using llvm::support::ulittle32_t;
55 // Returns the last element of a path, which is supposed to be a filename.
56 static StringRef getBasename(StringRef path) {
57 return sys::path::filename(path, sys::path::Style::windows);
60 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
61 std::string lld::toString(const coff::InputFile *file) {
64 if (file->parentName.empty() || file->kind() == coff::InputFile::ImportKind)
65 return std::string(file->getName());
67 return (getBasename(file->parentName) + "(" + getBasename(file->getName()) +
72 std::vector<ObjFile *> ObjFile::instances;
73 std::map<std::string, PDBInputFile *> PDBInputFile::instances;
74 std::vector<ImportFile *> ImportFile::instances;
75 std::vector<BitcodeFile *> BitcodeFile::instances;
77 /// Checks that Source is compatible with being a weak alias to Target.
78 /// If Source is Undefined and has no weak alias set, makes it a weak
80 static void checkAndSetWeakAlias(SymbolTable *symtab, InputFile *f,
81 Symbol *source, Symbol *target) {
82 if (auto *u = dyn_cast<Undefined>(source)) {
83 if (u->weakAlias && u->weakAlias != target) {
84 // Weak aliases as produced by GCC are named in the form
85 // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
86 // of another symbol emitted near the weak symbol.
87 // Just use the definition from the first object file that defined
91 symtab->reportDuplicate(source, f);
93 u->weakAlias = target;
97 static bool ignoredSymbolName(StringRef name) {
98 return name == "@feat.00" || name == "@comp.id";
101 ArchiveFile::ArchiveFile(MemoryBufferRef m) : InputFile(ArchiveKind, m) {}
103 void ArchiveFile::parse() {
104 // Parse a MemoryBufferRef as an archive file.
105 file = CHECK(Archive::create(mb), this);
107 // Read the symbol table to construct Lazy objects.
108 for (const Archive::Symbol &sym : file->symbols())
109 symtab->addLazyArchive(this, sym);
112 // Returns a buffer pointing to a member file containing a given symbol.
113 void ArchiveFile::addMember(const Archive::Symbol &sym) {
114 const Archive::Child &c =
115 CHECK(sym.getMember(),
116 "could not get the member for symbol " + toCOFFString(sym));
118 // Return an empty buffer if we have already returned the same buffer.
119 if (!seen.insert(c.getChildOffset()).second)
122 driver->enqueueArchiveMember(c, sym, getName());
125 std::vector<MemoryBufferRef> lld::coff::getArchiveMembers(Archive *file) {
126 std::vector<MemoryBufferRef> v;
127 Error err = Error::success();
128 for (const Archive::Child &c : file->children(err)) {
129 MemoryBufferRef mbref =
130 CHECK(c.getMemoryBufferRef(),
131 file->getFileName() +
132 ": could not get the buffer for a child of the archive");
136 fatal(file->getFileName() +
137 ": Archive::children failed: " + toString(std::move(err)));
141 void LazyObjFile::fetch() {
142 if (mb.getBuffer().empty())
147 file = make<BitcodeFile>(mb, "", 0, std::move(symbols));
149 file = make<ObjFile>(mb, std::move(symbols));
151 symtab->addFile(file);
154 void LazyObjFile::parse() {
155 if (isBitcode(this->mb)) {
157 std::unique_ptr<lto::InputFile> obj =
158 CHECK(lto::InputFile::create(this->mb), this);
159 for (const lto::InputFile::Symbol &sym : obj->symbols()) {
160 if (!sym.isUndefined())
161 symtab->addLazyObject(this, sym.getName());
166 // Native object file.
167 std::unique_ptr<Binary> coffObjPtr = CHECK(createBinary(mb), this);
168 COFFObjectFile *coffObj = cast<COFFObjectFile>(coffObjPtr.get());
169 uint32_t numSymbols = coffObj->getNumberOfSymbols();
170 for (uint32_t i = 0; i < numSymbols; ++i) {
171 COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
172 if (coffSym.isUndefined() || !coffSym.isExternal() ||
173 coffSym.isWeakExternal())
175 StringRef name = check(coffObj->getSymbolName(coffSym));
176 if (coffSym.isAbsolute() && ignoredSymbolName(name))
178 symtab->addLazyObject(this, name);
179 i += coffSym.getNumberOfAuxSymbols();
183 void ObjFile::parse() {
184 // Parse a memory buffer as a COFF file.
185 std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
187 if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
191 fatal(toString(this) + " is not a COFF file");
194 // Read section and symbol tables.
198 initializeDependencies();
201 const coff_section *ObjFile::getSection(uint32_t i) {
202 auto sec = coffObj->getSection(i);
204 fatal("getSection failed: #" + Twine(i) + ": " + toString(sec.takeError()));
208 // We set SectionChunk pointers in the SparseChunks vector to this value
209 // temporarily to mark comdat sections as having an unknown resolution. As we
210 // walk the object file's symbol table, once we visit either a leader symbol or
211 // an associative section definition together with the parent comdat's leader,
212 // we set the pointer to either nullptr (to mark the section as discarded) or a
213 // valid SectionChunk for that section.
214 static SectionChunk *const pendingComdat = reinterpret_cast<SectionChunk *>(1);
216 void ObjFile::initializeChunks() {
217 uint32_t numSections = coffObj->getNumberOfSections();
218 sparseChunks.resize(numSections + 1);
219 for (uint32_t i = 1; i < numSections + 1; ++i) {
220 const coff_section *sec = getSection(i);
221 if (sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
222 sparseChunks[i] = pendingComdat;
224 sparseChunks[i] = readSection(i, nullptr, "");
228 SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
229 const coff_aux_section_definition *def,
230 StringRef leaderName) {
231 const coff_section *sec = getSection(sectionNumber);
234 if (Expected<StringRef> e = coffObj->getSectionName(sec))
237 fatal("getSectionName failed: #" + Twine(sectionNumber) + ": " +
238 toString(e.takeError()));
240 if (name == ".drectve") {
241 ArrayRef<uint8_t> data;
242 cantFail(coffObj->getSectionContents(sec, data));
243 directives = StringRef((const char *)data.data(), data.size());
247 if (name == ".llvm_addrsig") {
252 // Object files may have DWARF debug info or MS CodeView debug info
255 // DWARF sections don't need any special handling from the perspective
256 // of the linker; they are just a data section containing relocations.
257 // We can just link them to complete debug info.
259 // CodeView needs linker support. We need to interpret debug info,
260 // and then write it to a separate .pdb file.
262 // Ignore DWARF debug info unless /debug is given.
263 if (!config->debug && name.startswith(".debug_"))
266 if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
268 auto *c = make<SectionChunk>(this, sec);
270 c->checksum = def->CheckSum;
272 // CodeView sections are stored to a different vector because they are not
273 // linked in the regular manner.
275 debugChunks.push_back(c);
276 else if (name == ".gfids$y")
277 guardFidChunks.push_back(c);
278 else if (name == ".gljmp$y")
279 guardLJmpChunks.push_back(c);
280 else if (name == ".sxdata")
281 sxDataChunks.push_back(c);
282 else if (config->tailMerge && sec->NumberOfRelocations == 0 &&
283 name == ".rdata" && leaderName.startswith("??_C@"))
284 // COFF sections that look like string literal sections (i.e. no
285 // relocations, in .rdata, leader symbol name matches the MSVC name mangling
286 // for string literals) are subject to string tail merging.
287 MergeChunk::addSection(c);
288 else if (name == ".rsrc" || name.startswith(".rsrc$"))
289 resourceChunks.push_back(c);
296 void ObjFile::includeResourceChunks() {
297 chunks.insert(chunks.end(), resourceChunks.begin(), resourceChunks.end());
300 void ObjFile::readAssociativeDefinition(
301 COFFSymbolRef sym, const coff_aux_section_definition *def) {
302 readAssociativeDefinition(sym, def, def->getNumber(sym.isBigObj()));
305 void ObjFile::readAssociativeDefinition(COFFSymbolRef sym,
306 const coff_aux_section_definition *def,
307 uint32_t parentIndex) {
308 SectionChunk *parent = sparseChunks[parentIndex];
309 int32_t sectionNumber = sym.getSectionNumber();
312 StringRef name = check(coffObj->getSymbolName(sym));
314 StringRef parentName;
315 const coff_section *parentSec = getSection(parentIndex);
316 if (Expected<StringRef> e = coffObj->getSectionName(parentSec))
318 error(toString(this) + ": associative comdat " + name + " (sec " +
319 Twine(sectionNumber) + ") has invalid reference to section " +
320 parentName + " (sec " + Twine(parentIndex) + ")");
323 if (parent == pendingComdat) {
324 // This can happen if an associative comdat refers to another associative
325 // comdat that appears after it (invalid per COFF spec) or to a section
326 // without any symbols.
331 // Check whether the parent is prevailing. If it is, so are we, and we read
332 // the section; otherwise mark it as discarded.
334 SectionChunk *c = readSection(sectionNumber, def, "");
335 sparseChunks[sectionNumber] = c;
337 c->selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
338 parent->addAssociative(c);
341 sparseChunks[sectionNumber] = nullptr;
345 void ObjFile::recordPrevailingSymbolForMingw(
346 COFFSymbolRef sym, DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
347 // For comdat symbols in executable sections, where this is the copy
348 // of the section chunk we actually include instead of discarding it,
349 // add the symbol to a map to allow using it for implicitly
350 // associating .[px]data$<func> sections to it.
351 int32_t sectionNumber = sym.getSectionNumber();
352 SectionChunk *sc = sparseChunks[sectionNumber];
353 if (sc && sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
355 name = check(coffObj->getSymbolName(sym));
356 if (getMachineType() == I386)
357 name.consume_front("_");
358 prevailingSectionMap[name] = sectionNumber;
362 void ObjFile::maybeAssociateSEHForMingw(
363 COFFSymbolRef sym, const coff_aux_section_definition *def,
364 const DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
365 StringRef name = check(coffObj->getSymbolName(sym));
366 if (name.consume_front(".pdata$") || name.consume_front(".xdata$") ||
367 name.consume_front(".eh_frame$")) {
368 // For MinGW, treat .[px]data$<func> and .eh_frame$<func> as implicitly
369 // associative to the symbol <func>.
370 auto parentSym = prevailingSectionMap.find(name);
371 if (parentSym != prevailingSectionMap.end())
372 readAssociativeDefinition(sym, def, parentSym->second);
376 Symbol *ObjFile::createRegular(COFFSymbolRef sym) {
377 SectionChunk *sc = sparseChunks[sym.getSectionNumber()];
378 if (sym.isExternal()) {
379 StringRef name = check(coffObj->getSymbolName(sym));
381 return symtab->addRegular(this, name, sym.getGeneric(), sc,
383 // For MinGW symbols named .weak.* that point to a discarded section,
384 // don't create an Undefined symbol. If nothing ever refers to the symbol,
385 // everything should be fine. If something actually refers to the symbol
386 // (e.g. the undefined weak alias), linking will fail due to undefined
387 // references at the end.
388 if (config->mingw && name.startswith(".weak."))
390 return symtab->addUndefined(name, this, false);
393 return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
394 /*IsExternal*/ false, sym.getGeneric(), sc);
398 void ObjFile::initializeSymbols() {
399 uint32_t numSymbols = coffObj->getNumberOfSymbols();
400 symbols.resize(numSymbols);
402 SmallVector<std::pair<Symbol *, uint32_t>, 8> weakAliases;
403 std::vector<uint32_t> pendingIndexes;
404 pendingIndexes.reserve(numSymbols);
406 DenseMap<StringRef, uint32_t> prevailingSectionMap;
407 std::vector<const coff_aux_section_definition *> comdatDefs(
408 coffObj->getNumberOfSections() + 1);
410 for (uint32_t i = 0; i < numSymbols; ++i) {
411 COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
412 bool prevailingComdat;
413 if (coffSym.isUndefined()) {
414 symbols[i] = createUndefined(coffSym);
415 } else if (coffSym.isWeakExternal()) {
416 symbols[i] = createUndefined(coffSym);
417 uint32_t tagIndex = coffSym.getAux<coff_aux_weak_external>()->TagIndex;
418 weakAliases.emplace_back(symbols[i], tagIndex);
419 } else if (Optional<Symbol *> optSym =
420 createDefined(coffSym, comdatDefs, prevailingComdat)) {
421 symbols[i] = *optSym;
422 if (config->mingw && prevailingComdat)
423 recordPrevailingSymbolForMingw(coffSym, prevailingSectionMap);
425 // createDefined() returns None if a symbol belongs to a section that
426 // was pending at the point when the symbol was read. This can happen in
428 // 1) section definition symbol for a comdat leader;
429 // 2) symbol belongs to a comdat section associated with another section.
430 // In both of these cases, we can expect the section to be resolved by
431 // the time we finish visiting the remaining symbols in the symbol
432 // table. So we postpone the handling of this symbol until that time.
433 pendingIndexes.push_back(i);
435 i += coffSym.getNumberOfAuxSymbols();
438 for (uint32_t i : pendingIndexes) {
439 COFFSymbolRef sym = check(coffObj->getSymbol(i));
440 if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
441 if (def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
442 readAssociativeDefinition(sym, def);
443 else if (config->mingw)
444 maybeAssociateSEHForMingw(sym, def, prevailingSectionMap);
446 if (sparseChunks[sym.getSectionNumber()] == pendingComdat) {
447 StringRef name = check(coffObj->getSymbolName(sym));
448 log("comdat section " + name +
449 " without leader and unassociated, discarding");
452 symbols[i] = createRegular(sym);
455 for (auto &kv : weakAliases) {
456 Symbol *sym = kv.first;
457 uint32_t idx = kv.second;
458 checkAndSetWeakAlias(symtab, this, sym, symbols[idx]);
461 // Free the memory used by sparseChunks now that symbol loading is finished.
462 decltype(sparseChunks)().swap(sparseChunks);
465 Symbol *ObjFile::createUndefined(COFFSymbolRef sym) {
466 StringRef name = check(coffObj->getSymbolName(sym));
467 return symtab->addUndefined(name, this, sym.isWeakExternal());
470 void ObjFile::handleComdatSelection(COFFSymbolRef sym, COMDATType &selection,
471 bool &prevailing, DefinedRegular *leader) {
474 // There's already an existing comdat for this symbol: `Leader`.
475 // Use the comdats's selection field to determine if the new
476 // symbol in `Sym` should be discarded, produce a duplicate symbol
479 SectionChunk *leaderChunk = nullptr;
480 COMDATType leaderSelection = IMAGE_COMDAT_SELECT_ANY;
483 leaderChunk = leader->getChunk();
484 leaderSelection = leaderChunk->selection;
486 // FIXME: comdats from LTO files don't know their selection; treat them
488 selection = leaderSelection;
491 if ((selection == IMAGE_COMDAT_SELECT_ANY &&
492 leaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
493 (selection == IMAGE_COMDAT_SELECT_LARGEST &&
494 leaderSelection == IMAGE_COMDAT_SELECT_ANY)) {
495 // cl.exe picks "any" for vftables when building with /GR- and
496 // "largest" when building with /GR. To be able to link object files
497 // compiled with each flag, "any" and "largest" are merged as "largest".
498 leaderSelection = selection = IMAGE_COMDAT_SELECT_LARGEST;
501 // GCCs __declspec(selectany) doesn't actually pick "any" but "same size as".
502 // Clang on the other hand picks "any". To be able to link two object files
503 // with a __declspec(selectany) declaration, one compiled with gcc and the
504 // other with clang, we merge them as proper "same size as"
505 if (config->mingw && ((selection == IMAGE_COMDAT_SELECT_ANY &&
506 leaderSelection == IMAGE_COMDAT_SELECT_SAME_SIZE) ||
507 (selection == IMAGE_COMDAT_SELECT_SAME_SIZE &&
508 leaderSelection == IMAGE_COMDAT_SELECT_ANY))) {
509 leaderSelection = selection = IMAGE_COMDAT_SELECT_SAME_SIZE;
512 // Other than that, comdat selections must match. This is a bit more
513 // strict than link.exe which allows merging "any" and "largest" if "any"
514 // is the first symbol the linker sees, and it allows merging "largest"
515 // with everything (!) if "largest" is the first symbol the linker sees.
516 // Making this symmetric independent of which selection is seen first
517 // seems better though.
518 // (This behavior matches ModuleLinker::getComdatResult().)
519 if (selection != leaderSelection) {
520 log(("conflicting comdat type for " + toString(*leader) + ": " +
521 Twine((int)leaderSelection) + " in " + toString(leader->getFile()) +
522 " and " + Twine((int)selection) + " in " + toString(this))
524 symtab->reportDuplicate(leader, this);
529 case IMAGE_COMDAT_SELECT_NODUPLICATES:
530 symtab->reportDuplicate(leader, this);
533 case IMAGE_COMDAT_SELECT_ANY:
537 case IMAGE_COMDAT_SELECT_SAME_SIZE:
538 if (leaderChunk->getSize() != getSection(sym)->SizeOfRawData)
539 symtab->reportDuplicate(leader, this);
542 case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
543 SectionChunk newChunk(this, getSection(sym));
544 // link.exe only compares section contents here and doesn't complain
545 // if the two comdat sections have e.g. different alignment.
547 if (leaderChunk->getContents() != newChunk.getContents())
548 symtab->reportDuplicate(leader, this, &newChunk, sym.getValue());
552 case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
553 // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
554 // (This means lld-link doesn't produce duplicate symbol errors for
555 // associative comdats while link.exe does, but associate comdats
556 // are never extern in practice.)
557 llvm_unreachable("createDefined not called for associative comdats");
559 case IMAGE_COMDAT_SELECT_LARGEST:
560 if (leaderChunk->getSize() < getSection(sym)->SizeOfRawData) {
561 // Replace the existing comdat symbol with the new one.
562 StringRef name = check(coffObj->getSymbolName(sym));
563 // FIXME: This is incorrect: With /opt:noref, the previous sections
564 // make it into the final executable as well. Correct handling would
565 // be to undo reading of the whole old section that's being replaced,
566 // or doing one pass that determines what the final largest comdat
567 // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
568 // only the largest one.
569 replaceSymbol<DefinedRegular>(leader, this, name, /*IsCOMDAT*/ true,
570 /*IsExternal*/ true, sym.getGeneric(),
576 case IMAGE_COMDAT_SELECT_NEWEST:
577 llvm_unreachable("should have been rejected earlier");
581 Optional<Symbol *> ObjFile::createDefined(
583 std::vector<const coff_aux_section_definition *> &comdatDefs,
586 auto getName = [&]() { return check(coffObj->getSymbolName(sym)); };
588 if (sym.isCommon()) {
589 auto *c = make<CommonChunk>(sym);
591 return symtab->addCommon(this, getName(), sym.getValue(), sym.getGeneric(),
595 if (sym.isAbsolute()) {
596 StringRef name = getName();
598 if (name == "@feat.00")
599 feat00Flags = sym.getValue();
600 // Skip special symbols.
601 if (ignoredSymbolName(name))
604 if (sym.isExternal())
605 return symtab->addAbsolute(name, sym);
606 return make<DefinedAbsolute>(name, sym);
609 int32_t sectionNumber = sym.getSectionNumber();
610 if (sectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
613 if (llvm::COFF::isReservedSectionNumber(sectionNumber))
614 fatal(toString(this) + ": " + getName() +
615 " should not refer to special section " + Twine(sectionNumber));
617 if ((uint32_t)sectionNumber >= sparseChunks.size())
618 fatal(toString(this) + ": " + getName() +
619 " should not refer to non-existent section " + Twine(sectionNumber));
622 // A comdat symbol consists of two symbol table entries.
623 // The first symbol entry has the name of the section (e.g. .text), fixed
624 // values for the other fields, and one auxiliary record.
625 // The second symbol entry has the name of the comdat symbol, called the
627 // When this function is called for the first symbol entry of a comdat,
628 // it sets comdatDefs and returns None, and when it's called for the second
629 // symbol entry it reads comdatDefs and then sets it back to nullptr.
631 // Handle comdat leader.
632 if (const coff_aux_section_definition *def = comdatDefs[sectionNumber]) {
633 comdatDefs[sectionNumber] = nullptr;
634 DefinedRegular *leader;
636 if (sym.isExternal()) {
637 std::tie(leader, prevailing) =
638 symtab->addComdat(this, getName(), sym.getGeneric());
640 leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
641 /*IsExternal*/ false, sym.getGeneric());
645 if (def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
646 // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
647 // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
648 def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
649 fatal("unknown comdat type " + std::to_string((int)def->Selection) +
650 " for " + getName() + " in " + toString(this));
652 COMDATType selection = (COMDATType)def->Selection;
654 if (leader->isCOMDAT)
655 handleComdatSelection(sym, selection, prevailing, leader);
658 SectionChunk *c = readSection(sectionNumber, def, getName());
659 sparseChunks[sectionNumber] = c;
660 c->sym = cast<DefinedRegular>(leader);
661 c->selection = selection;
662 cast<DefinedRegular>(leader)->data = &c->repl;
664 sparseChunks[sectionNumber] = nullptr;
669 // Prepare to handle the comdat leader symbol by setting the section's
670 // ComdatDefs pointer if we encounter a non-associative comdat.
671 if (sparseChunks[sectionNumber] == pendingComdat) {
672 if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
673 if (def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
674 comdatDefs[sectionNumber] = def;
679 return createRegular(sym);
682 MachineTypes ObjFile::getMachineType() {
684 return static_cast<MachineTypes>(coffObj->getMachine());
685 return IMAGE_FILE_MACHINE_UNKNOWN;
688 ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {
689 if (SectionChunk *sec = SectionChunk::findByName(debugChunks, secName))
690 return sec->consumeDebugMagic();
694 // OBJ files systematically store critical information in a .debug$S stream,
695 // even if the TU was compiled with no debug info. At least two records are
696 // always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
697 // PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
698 // currently used to initialize the hotPatchable member.
699 void ObjFile::initializeFlags() {
700 ArrayRef<uint8_t> data = getDebugSection(".debug$S");
704 DebugSubsectionArray subsections;
706 BinaryStreamReader reader(data, support::little);
707 ExitOnError exitOnErr;
708 exitOnErr(reader.readArray(subsections, data.size()));
710 for (const DebugSubsectionRecord &ss : subsections) {
711 if (ss.kind() != DebugSubsectionKind::Symbols)
716 // Only parse the first two records. We are only looking for S_OBJNAME
717 // and S_COMPILE3, and they usually appear at the beginning of the
719 for (unsigned i = 0; i < 2; ++i) {
720 Expected<CVSymbol> sym = readSymbolFromStream(ss.getRecordData(), offset);
722 consumeError(sym.takeError());
725 if (sym->kind() == SymbolKind::S_COMPILE3) {
727 cantFail(SymbolDeserializer::deserializeAs<Compile3Sym>(sym.get()));
729 (cs.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
731 if (sym->kind() == SymbolKind::S_OBJNAME) {
732 auto objName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
734 pchSignature = objName.Signature;
736 offset += sym->length();
741 // Depending on the compilation flags, OBJs can refer to external files,
742 // necessary to merge this OBJ into the final PDB. We currently support two
743 // types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
744 // And PDB type servers, when compiling with /Zi. This function extracts these
745 // dependencies and makes them available as a TpiSource interface (see
746 // DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
747 // output even with /Yc and /Yu and with /Zi.
748 void ObjFile::initializeDependencies() {
754 ArrayRef<uint8_t> data = getDebugSection(".debug$P");
758 data = getDebugSection(".debug$T");
763 // Get the first type record. It will indicate if this object uses a type
764 // server (/Zi) or a PCH file (/Yu).
766 BinaryStreamReader reader(data, support::little);
767 cantFail(reader.readArray(types, reader.getLength()));
768 CVTypeArray::Iterator firstType = types.begin();
769 if (firstType == types.end())
772 // Remember the .debug$T or .debug$P section.
775 // This object file is a PCH file that others will depend on.
777 debugTypesObj = makePrecompSource(this);
781 // This object file was compiled with /Zi. Enqueue the PDB dependency.
782 if (firstType->kind() == LF_TYPESERVER2) {
783 TypeServer2Record ts = cantFail(
784 TypeDeserializer::deserializeAs<TypeServer2Record>(firstType->data()));
785 debugTypesObj = makeUseTypeServerSource(this, ts);
786 PDBInputFile::enqueue(ts.getName(), this);
790 // This object was compiled with /Yu. It uses types from another object file
791 // with a matching signature.
792 if (firstType->kind() == LF_PRECOMP) {
793 PrecompRecord precomp = cantFail(
794 TypeDeserializer::deserializeAs<PrecompRecord>(firstType->data()));
795 debugTypesObj = makeUsePrecompSource(this, precomp);
799 // This is a plain old object file.
800 debugTypesObj = makeTpiSource(this);
803 // Make a PDB path assuming the PDB is in the same folder as the OBJ
804 static std::string getPdbBaseName(ObjFile *file, StringRef tSPath) {
805 StringRef localPath =
806 !file->parentName.empty() ? file->parentName : file->getName();
807 SmallString<128> path = sys::path::parent_path(localPath);
809 // Currently, type server PDBs are only created by MSVC cl, which only runs
810 // on Windows, so we can assume type server paths are Windows style.
811 sys::path::append(path,
812 sys::path::filename(tSPath, sys::path::Style::windows));
813 return std::string(path.str());
816 // The casing of the PDB path stamped in the OBJ can differ from the actual path
817 // on disk. With this, we ensure to always use lowercase as a key for the
818 // PDBInputFile::instances map, at least on Windows.
819 static std::string normalizePdbPath(StringRef path) {
823 return std::string(path);
827 // If existing, return the actual PDB path on disk.
828 static Optional<std::string> findPdbPath(StringRef pdbPath,
829 ObjFile *dependentFile) {
830 // Ensure the file exists before anything else. In some cases, if the path
831 // points to a removable device, Driver::enqueuePath() would fail with an
832 // error (EAGAIN, "resource unavailable try again") which we want to skip
834 if (llvm::sys::fs::exists(pdbPath))
835 return normalizePdbPath(pdbPath);
836 std::string ret = getPdbBaseName(dependentFile, pdbPath);
837 if (llvm::sys::fs::exists(ret))
838 return normalizePdbPath(ret);
842 PDBInputFile::PDBInputFile(MemoryBufferRef m) : InputFile(PDBKind, m) {}
844 PDBInputFile::~PDBInputFile() = default;
846 PDBInputFile *PDBInputFile::findFromRecordPath(StringRef path,
848 auto p = findPdbPath(path.str(), fromFile);
851 auto it = PDBInputFile::instances.find(*p);
852 if (it != PDBInputFile::instances.end())
857 void PDBInputFile::enqueue(StringRef path, ObjFile *fromFile) {
858 auto p = findPdbPath(path.str(), fromFile);
861 auto it = PDBInputFile::instances.emplace(*p, nullptr);
863 return; // already scheduled for load
864 driver->enqueuePDB(*p);
867 void PDBInputFile::parse() {
868 PDBInputFile::instances[mb.getBufferIdentifier().str()] = this;
870 std::unique_ptr<pdb::IPDBSession> thisSession;
871 loadErr.emplace(pdb::NativeSession::createFromPdb(
872 MemoryBuffer::getMemBuffer(mb, false), thisSession));
874 return; // fail silently at this point - the error will be handled later,
875 // when merging the debug type stream
877 session.reset(static_cast<pdb::NativeSession *>(thisSession.release()));
879 pdb::PDBFile &pdbFile = session->getPDBFile();
880 auto expectedInfo = pdbFile.getPDBInfoStream();
881 // All PDB Files should have an Info stream.
883 loadErr.emplace(expectedInfo.takeError());
886 debugTypesObj = makeTypeServerSource(this);
889 // Used only for DWARF debug info, which is not common (except in MinGW
890 // environments). This returns an optional pair of file name and line
891 // number for where the variable was defined.
892 Optional<std::pair<StringRef, uint32_t>>
893 ObjFile::getVariableLocation(StringRef var) {
895 dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
899 if (config->machine == I386)
900 var.consume_front("_");
901 Optional<std::pair<std::string, unsigned>> ret = dwarf->getVariableLoc(var);
904 return std::make_pair(saver.save(ret->first), ret->second);
907 // Used only for DWARF debug info, which is not common (except in MinGW
909 Optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
910 uint32_t sectionIndex) {
912 dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
917 return dwarf->getDILineInfo(offset, sectionIndex);
920 static StringRef ltrim1(StringRef s, const char *chars) {
921 if (!s.empty() && strchr(chars, s[0]))
926 void ImportFile::parse() {
927 const char *buf = mb.getBufferStart();
928 const auto *hdr = reinterpret_cast<const coff_import_header *>(buf);
930 // Check if the total size is valid.
931 if (mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
932 fatal("broken import library");
934 // Read names and create an __imp_ symbol.
935 StringRef name = saver.save(StringRef(buf + sizeof(*hdr)));
936 StringRef impName = saver.save("__imp_" + name);
937 const char *nameStart = buf + sizeof(coff_import_header) + name.size() + 1;
938 dllName = std::string(StringRef(nameStart));
940 switch (hdr->getNameType()) {
947 case IMPORT_NAME_NOPREFIX:
948 extName = ltrim1(name, "?@_");
950 case IMPORT_NAME_UNDECORATE:
951 extName = ltrim1(name, "?@_");
952 extName = extName.substr(0, extName.find('@'));
957 externalName = extName;
959 impSym = symtab->addImportData(impName, this);
960 // If this was a duplicate, we logged an error but may continue;
961 // in this case, impSym is nullptr.
965 if (hdr->getType() == llvm::COFF::IMPORT_CONST)
966 static_cast<void>(symtab->addImportData(name, this));
968 // If type is function, we need to create a thunk which jump to an
969 // address pointed by the __imp_ symbol. (This allows you to call
970 // DLL functions just like regular non-DLL functions.)
971 if (hdr->getType() == llvm::COFF::IMPORT_CODE)
972 thunkSym = symtab->addImportThunk(
973 name, cast_or_null<DefinedImportData>(impSym), hdr->Machine);
976 BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
977 uint64_t offsetInArchive)
978 : BitcodeFile(mb, archiveName, offsetInArchive, {}) {}
980 BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
981 uint64_t offsetInArchive,
982 std::vector<Symbol *> &&symbols)
983 : InputFile(BitcodeKind, mb), symbols(std::move(symbols)) {
984 std::string path = mb.getBufferIdentifier().str();
985 if (config->thinLTOIndexOnly)
986 path = replaceThinLTOSuffix(mb.getBufferIdentifier());
988 // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
989 // name. If two archives define two members with the same name, this
990 // causes a collision which result in only one of the objects being taken
991 // into consideration at LTO time (which very likely causes undefined
992 // symbols later in the link stage). So we append file offset to make
994 MemoryBufferRef mbref(
996 saver.save(archiveName.empty() ? path
997 : archiveName + sys::path::filename(path) +
998 utostr(offsetInArchive)));
1000 obj = check(lto::InputFile::create(mbref));
1003 BitcodeFile::~BitcodeFile() = default;
1005 void BitcodeFile::parse() {
1006 std::vector<std::pair<Symbol *, bool>> comdat(obj->getComdatTable().size());
1007 for (size_t i = 0; i != obj->getComdatTable().size(); ++i)
1008 // FIXME: lto::InputFile doesn't keep enough data to do correct comdat
1009 // selection handling.
1010 comdat[i] = symtab->addComdat(this, saver.save(obj->getComdatTable()[i]));
1011 for (const lto::InputFile::Symbol &objSym : obj->symbols()) {
1012 StringRef symName = saver.save(objSym.getName());
1013 int comdatIndex = objSym.getComdatIndex();
1015 if (objSym.isUndefined()) {
1016 sym = symtab->addUndefined(symName, this, false);
1017 } else if (objSym.isCommon()) {
1018 sym = symtab->addCommon(this, symName, objSym.getCommonSize());
1019 } else if (objSym.isWeak() && objSym.isIndirect()) {
1021 sym = symtab->addUndefined(symName, this, true);
1022 std::string fallback = std::string(objSym.getCOFFWeakExternalFallback());
1023 Symbol *alias = symtab->addUndefined(saver.save(fallback));
1024 checkAndSetWeakAlias(symtab, this, sym, alias);
1025 } else if (comdatIndex != -1) {
1026 if (symName == obj->getComdatTable()[comdatIndex])
1027 sym = comdat[comdatIndex].first;
1028 else if (comdat[comdatIndex].second)
1029 sym = symtab->addRegular(this, symName);
1031 sym = symtab->addUndefined(symName, this, false);
1033 sym = symtab->addRegular(this, symName);
1035 symbols.push_back(sym);
1036 if (objSym.isUsed())
1037 config->gcroot.push_back(sym);
1039 directives = obj->getCOFFLinkerOpts();
1042 MachineTypes BitcodeFile::getMachineType() {
1043 switch (Triple(obj->getTargetTriple()).getArch()) {
1044 case Triple::x86_64:
1050 case Triple::aarch64:
1053 return IMAGE_FILE_MACHINE_UNKNOWN;
1057 std::string lld::coff::replaceThinLTOSuffix(StringRef path) {
1058 StringRef suffix = config->thinLTOObjectSuffixReplace.first;
1059 StringRef repl = config->thinLTOObjectSuffixReplace.second;
1061 if (path.consume_back(suffix))
1062 return (path + repl).str();
1063 return std::string(path);