10b57cec5SDimitry Andric //===- InputFiles.cpp -----------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "InputFiles.h"
10349cc55cSDimitry Andric #include "COFFLinkerContext.h"
110b57cec5SDimitry Andric #include "Chunks.h"
120b57cec5SDimitry Andric #include "Config.h"
130b57cec5SDimitry Andric #include "DebugTypes.h"
140b57cec5SDimitry Andric #include "Driver.h"
150b57cec5SDimitry Andric #include "SymbolTable.h"
160b57cec5SDimitry Andric #include "Symbols.h"
17480093f4SDimitry Andric #include "lld/Common/DWARF.h"
180b57cec5SDimitry Andric #include "llvm-c/lto.h"
190b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
200b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
210b57cec5SDimitry Andric #include "llvm/BinaryFormat/COFF.h"
220b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
230b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
240b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
250b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
265ffd83dbSDimitry Andric #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
275ffd83dbSDimitry Andric #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
28480093f4SDimitry Andric #include "llvm/LTO/LTO.h"
290b57cec5SDimitry Andric #include "llvm/Object/Binary.h"
300b57cec5SDimitry Andric #include "llvm/Object/COFF.h"
310b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
320b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
330b57cec5SDimitry Andric #include "llvm/Support/Error.h"
340b57cec5SDimitry Andric #include "llvm/Support/ErrorOr.h"
350b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
360b57cec5SDimitry Andric #include "llvm/Support/Path.h"
370b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
3806c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
390b57cec5SDimitry Andric #include <cstring>
40bdd1243dSDimitry Andric #include <optional>
410b57cec5SDimitry Andric #include <system_error>
420b57cec5SDimitry Andric #include <utility>
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric using namespace llvm;
450b57cec5SDimitry Andric using namespace llvm::COFF;
460b57cec5SDimitry Andric using namespace llvm::codeview;
470b57cec5SDimitry Andric using namespace llvm::object;
480b57cec5SDimitry Andric using namespace llvm::support::endian;
495ffd83dbSDimitry Andric using namespace lld;
505ffd83dbSDimitry Andric using namespace lld::coff;
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric using llvm::Triple;
530b57cec5SDimitry Andric using llvm::support::ulittle32_t;
540b57cec5SDimitry Andric
5585868e8aSDimitry Andric // Returns the last element of a path, which is supposed to be a filename.
getBasename(StringRef path)5685868e8aSDimitry Andric static StringRef getBasename(StringRef path) {
5785868e8aSDimitry Andric return sys::path::filename(path, sys::path::Style::windows);
5885868e8aSDimitry Andric }
5985868e8aSDimitry Andric
6085868e8aSDimitry Andric // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
toString(const coff::InputFile * file)615ffd83dbSDimitry Andric std::string lld::toString(const coff::InputFile *file) {
6285868e8aSDimitry Andric if (!file)
6385868e8aSDimitry Andric return "<internal>";
6485868e8aSDimitry Andric if (file->parentName.empty() || file->kind() == coff::InputFile::ImportKind)
655ffd83dbSDimitry Andric return std::string(file->getName());
6685868e8aSDimitry Andric
6785868e8aSDimitry Andric return (getBasename(file->parentName) + "(" + getBasename(file->getName()) +
6885868e8aSDimitry Andric ")")
6985868e8aSDimitry Andric .str();
7085868e8aSDimitry Andric }
7185868e8aSDimitry Andric
720b57cec5SDimitry Andric /// Checks that Source is compatible with being a weak alias to Target.
730b57cec5SDimitry Andric /// If Source is Undefined and has no weak alias set, makes it a weak
740b57cec5SDimitry Andric /// alias to Target.
checkAndSetWeakAlias(COFFLinkerContext & ctx,InputFile * f,Symbol * source,Symbol * target)75bdd1243dSDimitry Andric static void checkAndSetWeakAlias(COFFLinkerContext &ctx, InputFile *f,
760b57cec5SDimitry Andric Symbol *source, Symbol *target) {
770b57cec5SDimitry Andric if (auto *u = dyn_cast<Undefined>(source)) {
780b57cec5SDimitry Andric if (u->weakAlias && u->weakAlias != target) {
790b57cec5SDimitry Andric // Weak aliases as produced by GCC are named in the form
800b57cec5SDimitry Andric // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
810b57cec5SDimitry Andric // of another symbol emitted near the weak symbol.
820b57cec5SDimitry Andric // Just use the definition from the first object file that defined
830b57cec5SDimitry Andric // this weak symbol.
845f757f3fSDimitry Andric if (ctx.config.allowDuplicateWeak)
850b57cec5SDimitry Andric return;
86bdd1243dSDimitry Andric ctx.symtab.reportDuplicate(source, f);
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric u->weakAlias = target;
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric
ignoredSymbolName(StringRef name)9285868e8aSDimitry Andric static bool ignoredSymbolName(StringRef name) {
9385868e8aSDimitry Andric return name == "@feat.00" || name == "@comp.id";
9485868e8aSDimitry Andric }
9585868e8aSDimitry Andric
ArchiveFile(COFFLinkerContext & ctx,MemoryBufferRef m)96349cc55cSDimitry Andric ArchiveFile::ArchiveFile(COFFLinkerContext &ctx, MemoryBufferRef m)
97349cc55cSDimitry Andric : InputFile(ctx, ArchiveKind, m) {}
980b57cec5SDimitry Andric
parse()990b57cec5SDimitry Andric void ArchiveFile::parse() {
1000b57cec5SDimitry Andric // Parse a MemoryBufferRef as an archive file.
1010b57cec5SDimitry Andric file = CHECK(Archive::create(mb), this);
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric // Read the symbol table to construct Lazy objects.
1040b57cec5SDimitry Andric for (const Archive::Symbol &sym : file->symbols())
105349cc55cSDimitry Andric ctx.symtab.addLazyArchive(this, sym);
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andric // Returns a buffer pointing to a member file containing a given symbol.
addMember(const Archive::Symbol & sym)1090b57cec5SDimitry Andric void ArchiveFile::addMember(const Archive::Symbol &sym) {
1100b57cec5SDimitry Andric const Archive::Child &c =
1110b57cec5SDimitry Andric CHECK(sym.getMember(),
112bdd1243dSDimitry Andric "could not get the member for symbol " + toCOFFString(ctx, sym));
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric // Return an empty buffer if we have already returned the same buffer.
1150b57cec5SDimitry Andric if (!seen.insert(c.getChildOffset()).second)
1160b57cec5SDimitry Andric return;
1170b57cec5SDimitry Andric
118bdd1243dSDimitry Andric ctx.driver.enqueueArchiveMember(c, sym, getName());
1190b57cec5SDimitry Andric }
1200b57cec5SDimitry Andric
getArchiveMembers(Archive * file)1215ffd83dbSDimitry Andric std::vector<MemoryBufferRef> lld::coff::getArchiveMembers(Archive *file) {
1220b57cec5SDimitry Andric std::vector<MemoryBufferRef> v;
1230b57cec5SDimitry Andric Error err = Error::success();
124480093f4SDimitry Andric for (const Archive::Child &c : file->children(err)) {
1250b57cec5SDimitry Andric MemoryBufferRef mbref =
1260b57cec5SDimitry Andric CHECK(c.getMemoryBufferRef(),
1270b57cec5SDimitry Andric file->getFileName() +
1280b57cec5SDimitry Andric ": could not get the buffer for a child of the archive");
1290b57cec5SDimitry Andric v.push_back(mbref);
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric if (err)
1320b57cec5SDimitry Andric fatal(file->getFileName() +
1330b57cec5SDimitry Andric ": Archive::children failed: " + toString(std::move(err)));
1340b57cec5SDimitry Andric return v;
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric
parseLazy()13704eeddc0SDimitry Andric void ObjFile::parseLazy() {
13885868e8aSDimitry Andric // Native object file.
13985868e8aSDimitry Andric std::unique_ptr<Binary> coffObjPtr = CHECK(createBinary(mb), this);
14085868e8aSDimitry Andric COFFObjectFile *coffObj = cast<COFFObjectFile>(coffObjPtr.get());
14185868e8aSDimitry Andric uint32_t numSymbols = coffObj->getNumberOfSymbols();
14285868e8aSDimitry Andric for (uint32_t i = 0; i < numSymbols; ++i) {
14385868e8aSDimitry Andric COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
14485868e8aSDimitry Andric if (coffSym.isUndefined() || !coffSym.isExternal() ||
14585868e8aSDimitry Andric coffSym.isWeakExternal())
14685868e8aSDimitry Andric continue;
1475ffd83dbSDimitry Andric StringRef name = check(coffObj->getSymbolName(coffSym));
14885868e8aSDimitry Andric if (coffSym.isAbsolute() && ignoredSymbolName(name))
14985868e8aSDimitry Andric continue;
150349cc55cSDimitry Andric ctx.symtab.addLazyObject(this, name);
15185868e8aSDimitry Andric i += coffSym.getNumberOfAuxSymbols();
15285868e8aSDimitry Andric }
15385868e8aSDimitry Andric }
15485868e8aSDimitry Andric
155*0fca6ea1SDimitry Andric struct ECMapEntry {
156*0fca6ea1SDimitry Andric ulittle32_t src;
157*0fca6ea1SDimitry Andric ulittle32_t dst;
158*0fca6ea1SDimitry Andric ulittle32_t type;
159*0fca6ea1SDimitry Andric };
160*0fca6ea1SDimitry Andric
initializeECThunks()161*0fca6ea1SDimitry Andric void ObjFile::initializeECThunks() {
162*0fca6ea1SDimitry Andric for (SectionChunk *chunk : hybmpChunks) {
163*0fca6ea1SDimitry Andric if (chunk->getContents().size() % sizeof(ECMapEntry)) {
164*0fca6ea1SDimitry Andric error("Invalid .hybmp chunk size " + Twine(chunk->getContents().size()));
165*0fca6ea1SDimitry Andric continue;
166*0fca6ea1SDimitry Andric }
167*0fca6ea1SDimitry Andric
168*0fca6ea1SDimitry Andric const uint8_t *end =
169*0fca6ea1SDimitry Andric chunk->getContents().data() + chunk->getContents().size();
170*0fca6ea1SDimitry Andric for (const uint8_t *iter = chunk->getContents().data(); iter != end;
171*0fca6ea1SDimitry Andric iter += sizeof(ECMapEntry)) {
172*0fca6ea1SDimitry Andric auto entry = reinterpret_cast<const ECMapEntry *>(iter);
173*0fca6ea1SDimitry Andric switch (entry->type) {
174*0fca6ea1SDimitry Andric case Arm64ECThunkType::Entry:
175*0fca6ea1SDimitry Andric ctx.symtab.addEntryThunk(getSymbol(entry->src), getSymbol(entry->dst));
176*0fca6ea1SDimitry Andric break;
177*0fca6ea1SDimitry Andric case Arm64ECThunkType::Exit:
178*0fca6ea1SDimitry Andric case Arm64ECThunkType::GuestExit:
179*0fca6ea1SDimitry Andric break;
180*0fca6ea1SDimitry Andric default:
181*0fca6ea1SDimitry Andric warn("Ignoring unknown EC thunk type " + Twine(entry->type));
182*0fca6ea1SDimitry Andric }
183*0fca6ea1SDimitry Andric }
184*0fca6ea1SDimitry Andric }
185*0fca6ea1SDimitry Andric }
186*0fca6ea1SDimitry Andric
parse()1870b57cec5SDimitry Andric void ObjFile::parse() {
1880b57cec5SDimitry Andric // Parse a memory buffer as a COFF file.
1890b57cec5SDimitry Andric std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
1920b57cec5SDimitry Andric bin.release();
1930b57cec5SDimitry Andric coffObj.reset(obj);
1940b57cec5SDimitry Andric } else {
1950b57cec5SDimitry Andric fatal(toString(this) + " is not a COFF file");
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric // Read section and symbol tables.
1990b57cec5SDimitry Andric initializeChunks();
2000b57cec5SDimitry Andric initializeSymbols();
2010b57cec5SDimitry Andric initializeFlags();
2020b57cec5SDimitry Andric initializeDependencies();
203*0fca6ea1SDimitry Andric initializeECThunks();
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric
getSection(uint32_t i)2060b57cec5SDimitry Andric const coff_section *ObjFile::getSection(uint32_t i) {
2075ffd83dbSDimitry Andric auto sec = coffObj->getSection(i);
2085ffd83dbSDimitry Andric if (!sec)
2095ffd83dbSDimitry Andric fatal("getSection failed: #" + Twine(i) + ": " + toString(sec.takeError()));
2105ffd83dbSDimitry Andric return *sec;
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric
2130b57cec5SDimitry Andric // We set SectionChunk pointers in the SparseChunks vector to this value
2140b57cec5SDimitry Andric // temporarily to mark comdat sections as having an unknown resolution. As we
2150b57cec5SDimitry Andric // walk the object file's symbol table, once we visit either a leader symbol or
2160b57cec5SDimitry Andric // an associative section definition together with the parent comdat's leader,
2170b57cec5SDimitry Andric // we set the pointer to either nullptr (to mark the section as discarded) or a
2180b57cec5SDimitry Andric // valid SectionChunk for that section.
2190b57cec5SDimitry Andric static SectionChunk *const pendingComdat = reinterpret_cast<SectionChunk *>(1);
2200b57cec5SDimitry Andric
initializeChunks()2210b57cec5SDimitry Andric void ObjFile::initializeChunks() {
2220b57cec5SDimitry Andric uint32_t numSections = coffObj->getNumberOfSections();
2230b57cec5SDimitry Andric sparseChunks.resize(numSections + 1);
2240b57cec5SDimitry Andric for (uint32_t i = 1; i < numSections + 1; ++i) {
2250b57cec5SDimitry Andric const coff_section *sec = getSection(i);
2260b57cec5SDimitry Andric if (sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
2270b57cec5SDimitry Andric sparseChunks[i] = pendingComdat;
2280b57cec5SDimitry Andric else
2290b57cec5SDimitry Andric sparseChunks[i] = readSection(i, nullptr, "");
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric
readSection(uint32_t sectionNumber,const coff_aux_section_definition * def,StringRef leaderName)2330b57cec5SDimitry Andric SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
2340b57cec5SDimitry Andric const coff_aux_section_definition *def,
2350b57cec5SDimitry Andric StringRef leaderName) {
2360b57cec5SDimitry Andric const coff_section *sec = getSection(sectionNumber);
2370b57cec5SDimitry Andric
2380b57cec5SDimitry Andric StringRef name;
2390b57cec5SDimitry Andric if (Expected<StringRef> e = coffObj->getSectionName(sec))
2400b57cec5SDimitry Andric name = *e;
2410b57cec5SDimitry Andric else
2420b57cec5SDimitry Andric fatal("getSectionName failed: #" + Twine(sectionNumber) + ": " +
2430b57cec5SDimitry Andric toString(e.takeError()));
2440b57cec5SDimitry Andric
2450b57cec5SDimitry Andric if (name == ".drectve") {
2460b57cec5SDimitry Andric ArrayRef<uint8_t> data;
2470b57cec5SDimitry Andric cantFail(coffObj->getSectionContents(sec, data));
2480b57cec5SDimitry Andric directives = StringRef((const char *)data.data(), data.size());
2490b57cec5SDimitry Andric return nullptr;
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andric if (name == ".llvm_addrsig") {
2530b57cec5SDimitry Andric addrsigSec = sec;
2540b57cec5SDimitry Andric return nullptr;
2550b57cec5SDimitry Andric }
2560b57cec5SDimitry Andric
257e8d8bef9SDimitry Andric if (name == ".llvm.call-graph-profile") {
258e8d8bef9SDimitry Andric callgraphSec = sec;
259e8d8bef9SDimitry Andric return nullptr;
260e8d8bef9SDimitry Andric }
261e8d8bef9SDimitry Andric
2620b57cec5SDimitry Andric // Object files may have DWARF debug info or MS CodeView debug info
2630b57cec5SDimitry Andric // (or both).
2640b57cec5SDimitry Andric //
2650b57cec5SDimitry Andric // DWARF sections don't need any special handling from the perspective
2660b57cec5SDimitry Andric // of the linker; they are just a data section containing relocations.
2670b57cec5SDimitry Andric // We can just link them to complete debug info.
2680b57cec5SDimitry Andric //
2690b57cec5SDimitry Andric // CodeView needs linker support. We need to interpret debug info,
2700b57cec5SDimitry Andric // and then write it to a separate .pdb file.
2710b57cec5SDimitry Andric
2725f757f3fSDimitry Andric // Ignore DWARF debug info unless requested to be included.
2735f757f3fSDimitry Andric if (!ctx.config.includeDwarfChunks && name.starts_with(".debug_"))
2740b57cec5SDimitry Andric return nullptr;
2750b57cec5SDimitry Andric
2760b57cec5SDimitry Andric if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
2770b57cec5SDimitry Andric return nullptr;
278*0fca6ea1SDimitry Andric SectionChunk *c;
279*0fca6ea1SDimitry Andric if (isArm64EC(getMachineType()))
280*0fca6ea1SDimitry Andric c = make<SectionChunkEC>(this, sec);
281*0fca6ea1SDimitry Andric else
282*0fca6ea1SDimitry Andric c = make<SectionChunk>(this, sec);
2830b57cec5SDimitry Andric if (def)
2840b57cec5SDimitry Andric c->checksum = def->CheckSum;
2850b57cec5SDimitry Andric
2860b57cec5SDimitry Andric // CodeView sections are stored to a different vector because they are not
2870b57cec5SDimitry Andric // linked in the regular manner.
2880b57cec5SDimitry Andric if (c->isCodeView())
2890b57cec5SDimitry Andric debugChunks.push_back(c);
2900b57cec5SDimitry Andric else if (name == ".gfids$y")
2910b57cec5SDimitry Andric guardFidChunks.push_back(c);
292e8d8bef9SDimitry Andric else if (name == ".giats$y")
293e8d8bef9SDimitry Andric guardIATChunks.push_back(c);
2940b57cec5SDimitry Andric else if (name == ".gljmp$y")
2950b57cec5SDimitry Andric guardLJmpChunks.push_back(c);
296fe6060f1SDimitry Andric else if (name == ".gehcont$y")
297fe6060f1SDimitry Andric guardEHContChunks.push_back(c);
2980b57cec5SDimitry Andric else if (name == ".sxdata")
2995ffd83dbSDimitry Andric sxDataChunks.push_back(c);
300*0fca6ea1SDimitry Andric else if (isArm64EC(getMachineType()) && name == ".hybmp$x")
301*0fca6ea1SDimitry Andric hybmpChunks.push_back(c);
302bdd1243dSDimitry Andric else if (ctx.config.tailMerge && sec->NumberOfRelocations == 0 &&
30306c3fb27SDimitry Andric name == ".rdata" && leaderName.starts_with("??_C@"))
3040b57cec5SDimitry Andric // COFF sections that look like string literal sections (i.e. no
3050b57cec5SDimitry Andric // relocations, in .rdata, leader symbol name matches the MSVC name mangling
3060b57cec5SDimitry Andric // for string literals) are subject to string tail merging.
307349cc55cSDimitry Andric MergeChunk::addSection(ctx, c);
30806c3fb27SDimitry Andric else if (name == ".rsrc" || name.starts_with(".rsrc$"))
30985868e8aSDimitry Andric resourceChunks.push_back(c);
3100b57cec5SDimitry Andric else
3110b57cec5SDimitry Andric chunks.push_back(c);
3120b57cec5SDimitry Andric
3130b57cec5SDimitry Andric return c;
3140b57cec5SDimitry Andric }
3150b57cec5SDimitry Andric
includeResourceChunks()31685868e8aSDimitry Andric void ObjFile::includeResourceChunks() {
31785868e8aSDimitry Andric chunks.insert(chunks.end(), resourceChunks.begin(), resourceChunks.end());
31885868e8aSDimitry Andric }
31985868e8aSDimitry Andric
readAssociativeDefinition(COFFSymbolRef sym,const coff_aux_section_definition * def)3200b57cec5SDimitry Andric void ObjFile::readAssociativeDefinition(
3210b57cec5SDimitry Andric COFFSymbolRef sym, const coff_aux_section_definition *def) {
3220b57cec5SDimitry Andric readAssociativeDefinition(sym, def, def->getNumber(sym.isBigObj()));
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric
readAssociativeDefinition(COFFSymbolRef sym,const coff_aux_section_definition * def,uint32_t parentIndex)3250b57cec5SDimitry Andric void ObjFile::readAssociativeDefinition(COFFSymbolRef sym,
3260b57cec5SDimitry Andric const coff_aux_section_definition *def,
3270b57cec5SDimitry Andric uint32_t parentIndex) {
3280b57cec5SDimitry Andric SectionChunk *parent = sparseChunks[parentIndex];
3290b57cec5SDimitry Andric int32_t sectionNumber = sym.getSectionNumber();
3300b57cec5SDimitry Andric
3310b57cec5SDimitry Andric auto diag = [&]() {
3325ffd83dbSDimitry Andric StringRef name = check(coffObj->getSymbolName(sym));
3330b57cec5SDimitry Andric
3345ffd83dbSDimitry Andric StringRef parentName;
3350b57cec5SDimitry Andric const coff_section *parentSec = getSection(parentIndex);
3360b57cec5SDimitry Andric if (Expected<StringRef> e = coffObj->getSectionName(parentSec))
3370b57cec5SDimitry Andric parentName = *e;
3380b57cec5SDimitry Andric error(toString(this) + ": associative comdat " + name + " (sec " +
3390b57cec5SDimitry Andric Twine(sectionNumber) + ") has invalid reference to section " +
3400b57cec5SDimitry Andric parentName + " (sec " + Twine(parentIndex) + ")");
3410b57cec5SDimitry Andric };
3420b57cec5SDimitry Andric
3430b57cec5SDimitry Andric if (parent == pendingComdat) {
3440b57cec5SDimitry Andric // This can happen if an associative comdat refers to another associative
3450b57cec5SDimitry Andric // comdat that appears after it (invalid per COFF spec) or to a section
3460b57cec5SDimitry Andric // without any symbols.
3470b57cec5SDimitry Andric diag();
3480b57cec5SDimitry Andric return;
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric
3510b57cec5SDimitry Andric // Check whether the parent is prevailing. If it is, so are we, and we read
3520b57cec5SDimitry Andric // the section; otherwise mark it as discarded.
3530b57cec5SDimitry Andric if (parent) {
3540b57cec5SDimitry Andric SectionChunk *c = readSection(sectionNumber, def, "");
3550b57cec5SDimitry Andric sparseChunks[sectionNumber] = c;
3560b57cec5SDimitry Andric if (c) {
3570b57cec5SDimitry Andric c->selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
3580b57cec5SDimitry Andric parent->addAssociative(c);
3590b57cec5SDimitry Andric }
3600b57cec5SDimitry Andric } else {
3610b57cec5SDimitry Andric sparseChunks[sectionNumber] = nullptr;
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric }
3640b57cec5SDimitry Andric
recordPrevailingSymbolForMingw(COFFSymbolRef sym,DenseMap<StringRef,uint32_t> & prevailingSectionMap)3650b57cec5SDimitry Andric void ObjFile::recordPrevailingSymbolForMingw(
3660b57cec5SDimitry Andric COFFSymbolRef sym, DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
3670b57cec5SDimitry Andric // For comdat symbols in executable sections, where this is the copy
3680b57cec5SDimitry Andric // of the section chunk we actually include instead of discarding it,
3690b57cec5SDimitry Andric // add the symbol to a map to allow using it for implicitly
3700b57cec5SDimitry Andric // associating .[px]data$<func> sections to it.
371979e22ffSDimitry Andric // Use the suffix from the .text$<func> instead of the leader symbol
372979e22ffSDimitry Andric // name, for cases where the names differ (i386 mangling/decorations,
373979e22ffSDimitry Andric // cases where the leader is a weak symbol named .weak.func.default*).
3740b57cec5SDimitry Andric int32_t sectionNumber = sym.getSectionNumber();
3750b57cec5SDimitry Andric SectionChunk *sc = sparseChunks[sectionNumber];
3760b57cec5SDimitry Andric if (sc && sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
377979e22ffSDimitry Andric StringRef name = sc->getSectionName().split('$').second;
3780b57cec5SDimitry Andric prevailingSectionMap[name] = sectionNumber;
3790b57cec5SDimitry Andric }
3800b57cec5SDimitry Andric }
3810b57cec5SDimitry Andric
maybeAssociateSEHForMingw(COFFSymbolRef sym,const coff_aux_section_definition * def,const DenseMap<StringRef,uint32_t> & prevailingSectionMap)3820b57cec5SDimitry Andric void ObjFile::maybeAssociateSEHForMingw(
3830b57cec5SDimitry Andric COFFSymbolRef sym, const coff_aux_section_definition *def,
3840b57cec5SDimitry Andric const DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
3855ffd83dbSDimitry Andric StringRef name = check(coffObj->getSymbolName(sym));
3860b57cec5SDimitry Andric if (name.consume_front(".pdata$") || name.consume_front(".xdata$") ||
3870b57cec5SDimitry Andric name.consume_front(".eh_frame$")) {
3880b57cec5SDimitry Andric // For MinGW, treat .[px]data$<func> and .eh_frame$<func> as implicitly
3890b57cec5SDimitry Andric // associative to the symbol <func>.
3900b57cec5SDimitry Andric auto parentSym = prevailingSectionMap.find(name);
3910b57cec5SDimitry Andric if (parentSym != prevailingSectionMap.end())
3920b57cec5SDimitry Andric readAssociativeDefinition(sym, def, parentSym->second);
3930b57cec5SDimitry Andric }
3940b57cec5SDimitry Andric }
3950b57cec5SDimitry Andric
createRegular(COFFSymbolRef sym)3960b57cec5SDimitry Andric Symbol *ObjFile::createRegular(COFFSymbolRef sym) {
3970b57cec5SDimitry Andric SectionChunk *sc = sparseChunks[sym.getSectionNumber()];
3980b57cec5SDimitry Andric if (sym.isExternal()) {
3995ffd83dbSDimitry Andric StringRef name = check(coffObj->getSymbolName(sym));
4000b57cec5SDimitry Andric if (sc)
401349cc55cSDimitry Andric return ctx.symtab.addRegular(this, name, sym.getGeneric(), sc,
40285868e8aSDimitry Andric sym.getValue());
4030b57cec5SDimitry Andric // For MinGW symbols named .weak.* that point to a discarded section,
4040b57cec5SDimitry Andric // don't create an Undefined symbol. If nothing ever refers to the symbol,
4050b57cec5SDimitry Andric // everything should be fine. If something actually refers to the symbol
4060b57cec5SDimitry Andric // (e.g. the undefined weak alias), linking will fail due to undefined
4070b57cec5SDimitry Andric // references at the end.
40806c3fb27SDimitry Andric if (ctx.config.mingw && name.starts_with(".weak."))
4090b57cec5SDimitry Andric return nullptr;
410349cc55cSDimitry Andric return ctx.symtab.addUndefined(name, this, false);
4110b57cec5SDimitry Andric }
4120b57cec5SDimitry Andric if (sc)
4130b57cec5SDimitry Andric return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
4140b57cec5SDimitry Andric /*IsExternal*/ false, sym.getGeneric(), sc);
4150b57cec5SDimitry Andric return nullptr;
4160b57cec5SDimitry Andric }
4170b57cec5SDimitry Andric
initializeSymbols()4180b57cec5SDimitry Andric void ObjFile::initializeSymbols() {
4190b57cec5SDimitry Andric uint32_t numSymbols = coffObj->getNumberOfSymbols();
4200b57cec5SDimitry Andric symbols.resize(numSymbols);
4210b57cec5SDimitry Andric
4220b57cec5SDimitry Andric SmallVector<std::pair<Symbol *, uint32_t>, 8> weakAliases;
4230b57cec5SDimitry Andric std::vector<uint32_t> pendingIndexes;
4240b57cec5SDimitry Andric pendingIndexes.reserve(numSymbols);
4250b57cec5SDimitry Andric
4260b57cec5SDimitry Andric DenseMap<StringRef, uint32_t> prevailingSectionMap;
4270b57cec5SDimitry Andric std::vector<const coff_aux_section_definition *> comdatDefs(
4280b57cec5SDimitry Andric coffObj->getNumberOfSections() + 1);
4290b57cec5SDimitry Andric
4300b57cec5SDimitry Andric for (uint32_t i = 0; i < numSymbols; ++i) {
4310b57cec5SDimitry Andric COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
4320b57cec5SDimitry Andric bool prevailingComdat;
4330b57cec5SDimitry Andric if (coffSym.isUndefined()) {
4340b57cec5SDimitry Andric symbols[i] = createUndefined(coffSym);
4350b57cec5SDimitry Andric } else if (coffSym.isWeakExternal()) {
4360b57cec5SDimitry Andric symbols[i] = createUndefined(coffSym);
4370b57cec5SDimitry Andric uint32_t tagIndex = coffSym.getAux<coff_aux_weak_external>()->TagIndex;
4380b57cec5SDimitry Andric weakAliases.emplace_back(symbols[i], tagIndex);
439bdd1243dSDimitry Andric } else if (std::optional<Symbol *> optSym =
4400b57cec5SDimitry Andric createDefined(coffSym, comdatDefs, prevailingComdat)) {
4410b57cec5SDimitry Andric symbols[i] = *optSym;
442bdd1243dSDimitry Andric if (ctx.config.mingw && prevailingComdat)
4430b57cec5SDimitry Andric recordPrevailingSymbolForMingw(coffSym, prevailingSectionMap);
4440b57cec5SDimitry Andric } else {
445bdd1243dSDimitry Andric // createDefined() returns std::nullopt if a symbol belongs to a section
446bdd1243dSDimitry Andric // that was pending at the point when the symbol was read. This can happen
447bdd1243dSDimitry Andric // in two cases:
4480b57cec5SDimitry Andric // 1) section definition symbol for a comdat leader;
4490b57cec5SDimitry Andric // 2) symbol belongs to a comdat section associated with another section.
4500b57cec5SDimitry Andric // In both of these cases, we can expect the section to be resolved by
4510b57cec5SDimitry Andric // the time we finish visiting the remaining symbols in the symbol
4520b57cec5SDimitry Andric // table. So we postpone the handling of this symbol until that time.
4530b57cec5SDimitry Andric pendingIndexes.push_back(i);
4540b57cec5SDimitry Andric }
4550b57cec5SDimitry Andric i += coffSym.getNumberOfAuxSymbols();
4560b57cec5SDimitry Andric }
4570b57cec5SDimitry Andric
4580b57cec5SDimitry Andric for (uint32_t i : pendingIndexes) {
4590b57cec5SDimitry Andric COFFSymbolRef sym = check(coffObj->getSymbol(i));
4600b57cec5SDimitry Andric if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
4610b57cec5SDimitry Andric if (def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
4620b57cec5SDimitry Andric readAssociativeDefinition(sym, def);
463bdd1243dSDimitry Andric else if (ctx.config.mingw)
4640b57cec5SDimitry Andric maybeAssociateSEHForMingw(sym, def, prevailingSectionMap);
4650b57cec5SDimitry Andric }
4660b57cec5SDimitry Andric if (sparseChunks[sym.getSectionNumber()] == pendingComdat) {
4675ffd83dbSDimitry Andric StringRef name = check(coffObj->getSymbolName(sym));
4680b57cec5SDimitry Andric log("comdat section " + name +
4690b57cec5SDimitry Andric " without leader and unassociated, discarding");
4700b57cec5SDimitry Andric continue;
4710b57cec5SDimitry Andric }
4720b57cec5SDimitry Andric symbols[i] = createRegular(sym);
4730b57cec5SDimitry Andric }
4740b57cec5SDimitry Andric
4750b57cec5SDimitry Andric for (auto &kv : weakAliases) {
4760b57cec5SDimitry Andric Symbol *sym = kv.first;
4770b57cec5SDimitry Andric uint32_t idx = kv.second;
478bdd1243dSDimitry Andric checkAndSetWeakAlias(ctx, this, sym, symbols[idx]);
4790b57cec5SDimitry Andric }
4805ffd83dbSDimitry Andric
4815ffd83dbSDimitry Andric // Free the memory used by sparseChunks now that symbol loading is finished.
4825ffd83dbSDimitry Andric decltype(sparseChunks)().swap(sparseChunks);
4830b57cec5SDimitry Andric }
4840b57cec5SDimitry Andric
createUndefined(COFFSymbolRef sym)4850b57cec5SDimitry Andric Symbol *ObjFile::createUndefined(COFFSymbolRef sym) {
4865ffd83dbSDimitry Andric StringRef name = check(coffObj->getSymbolName(sym));
487349cc55cSDimitry Andric return ctx.symtab.addUndefined(name, this, sym.isWeakExternal());
4880b57cec5SDimitry Andric }
4890b57cec5SDimitry Andric
findSectionDef(COFFObjectFile * obj,int32_t section)490e8d8bef9SDimitry Andric static const coff_aux_section_definition *findSectionDef(COFFObjectFile *obj,
491e8d8bef9SDimitry Andric int32_t section) {
492e8d8bef9SDimitry Andric uint32_t numSymbols = obj->getNumberOfSymbols();
493e8d8bef9SDimitry Andric for (uint32_t i = 0; i < numSymbols; ++i) {
494e8d8bef9SDimitry Andric COFFSymbolRef sym = check(obj->getSymbol(i));
495e8d8bef9SDimitry Andric if (sym.getSectionNumber() != section)
496e8d8bef9SDimitry Andric continue;
497e8d8bef9SDimitry Andric if (const coff_aux_section_definition *def = sym.getSectionDefinition())
498e8d8bef9SDimitry Andric return def;
499e8d8bef9SDimitry Andric }
500e8d8bef9SDimitry Andric return nullptr;
501e8d8bef9SDimitry Andric }
502e8d8bef9SDimitry Andric
handleComdatSelection(COFFSymbolRef sym,COMDATType & selection,bool & prevailing,DefinedRegular * leader,const llvm::object::coff_aux_section_definition * def)503e8d8bef9SDimitry Andric void ObjFile::handleComdatSelection(
504e8d8bef9SDimitry Andric COFFSymbolRef sym, COMDATType &selection, bool &prevailing,
505e8d8bef9SDimitry Andric DefinedRegular *leader,
506e8d8bef9SDimitry Andric const llvm::object::coff_aux_section_definition *def) {
5070b57cec5SDimitry Andric if (prevailing)
5080b57cec5SDimitry Andric return;
5090b57cec5SDimitry Andric // There's already an existing comdat for this symbol: `Leader`.
5100b57cec5SDimitry Andric // Use the comdats's selection field to determine if the new
5110b57cec5SDimitry Andric // symbol in `Sym` should be discarded, produce a duplicate symbol
5120b57cec5SDimitry Andric // error, etc.
5130b57cec5SDimitry Andric
514fe6060f1SDimitry Andric SectionChunk *leaderChunk = leader->getChunk();
515fe6060f1SDimitry Andric COMDATType leaderSelection = leaderChunk->selection;
5160b57cec5SDimitry Andric
517fe6060f1SDimitry Andric assert(leader->data && "Comdat leader without SectionChunk?");
518fe6060f1SDimitry Andric if (isa<BitcodeFile>(leader->file)) {
519fe6060f1SDimitry Andric // If the leader is only a LTO symbol, we don't know e.g. its final size
520fe6060f1SDimitry Andric // yet, so we can't do the full strict comdat selection checking yet.
521fe6060f1SDimitry Andric selection = leaderSelection = IMAGE_COMDAT_SELECT_ANY;
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric
5240b57cec5SDimitry Andric if ((selection == IMAGE_COMDAT_SELECT_ANY &&
5250b57cec5SDimitry Andric leaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
5260b57cec5SDimitry Andric (selection == IMAGE_COMDAT_SELECT_LARGEST &&
5270b57cec5SDimitry Andric leaderSelection == IMAGE_COMDAT_SELECT_ANY)) {
5280b57cec5SDimitry Andric // cl.exe picks "any" for vftables when building with /GR- and
5290b57cec5SDimitry Andric // "largest" when building with /GR. To be able to link object files
5300b57cec5SDimitry Andric // compiled with each flag, "any" and "largest" are merged as "largest".
5310b57cec5SDimitry Andric leaderSelection = selection = IMAGE_COMDAT_SELECT_LARGEST;
5320b57cec5SDimitry Andric }
5330b57cec5SDimitry Andric
53455e4f9d5SDimitry Andric // GCCs __declspec(selectany) doesn't actually pick "any" but "same size as".
53555e4f9d5SDimitry Andric // Clang on the other hand picks "any". To be able to link two object files
53655e4f9d5SDimitry Andric // with a __declspec(selectany) declaration, one compiled with gcc and the
53755e4f9d5SDimitry Andric // other with clang, we merge them as proper "same size as"
538bdd1243dSDimitry Andric if (ctx.config.mingw && ((selection == IMAGE_COMDAT_SELECT_ANY &&
53955e4f9d5SDimitry Andric leaderSelection == IMAGE_COMDAT_SELECT_SAME_SIZE) ||
54055e4f9d5SDimitry Andric (selection == IMAGE_COMDAT_SELECT_SAME_SIZE &&
54155e4f9d5SDimitry Andric leaderSelection == IMAGE_COMDAT_SELECT_ANY))) {
54255e4f9d5SDimitry Andric leaderSelection = selection = IMAGE_COMDAT_SELECT_SAME_SIZE;
54355e4f9d5SDimitry Andric }
54455e4f9d5SDimitry Andric
5450b57cec5SDimitry Andric // Other than that, comdat selections must match. This is a bit more
5460b57cec5SDimitry Andric // strict than link.exe which allows merging "any" and "largest" if "any"
5470b57cec5SDimitry Andric // is the first symbol the linker sees, and it allows merging "largest"
5480b57cec5SDimitry Andric // with everything (!) if "largest" is the first symbol the linker sees.
5490b57cec5SDimitry Andric // Making this symmetric independent of which selection is seen first
5500b57cec5SDimitry Andric // seems better though.
5510b57cec5SDimitry Andric // (This behavior matches ModuleLinker::getComdatResult().)
5520b57cec5SDimitry Andric if (selection != leaderSelection) {
553bdd1243dSDimitry Andric log(("conflicting comdat type for " + toString(ctx, *leader) + ": " +
5540b57cec5SDimitry Andric Twine((int)leaderSelection) + " in " + toString(leader->getFile()) +
5550b57cec5SDimitry Andric " and " + Twine((int)selection) + " in " + toString(this))
5560b57cec5SDimitry Andric .str());
557349cc55cSDimitry Andric ctx.symtab.reportDuplicate(leader, this);
5580b57cec5SDimitry Andric return;
5590b57cec5SDimitry Andric }
5600b57cec5SDimitry Andric
5610b57cec5SDimitry Andric switch (selection) {
5620b57cec5SDimitry Andric case IMAGE_COMDAT_SELECT_NODUPLICATES:
563349cc55cSDimitry Andric ctx.symtab.reportDuplicate(leader, this);
5640b57cec5SDimitry Andric break;
5650b57cec5SDimitry Andric
5660b57cec5SDimitry Andric case IMAGE_COMDAT_SELECT_ANY:
5670b57cec5SDimitry Andric // Nothing to do.
5680b57cec5SDimitry Andric break;
5690b57cec5SDimitry Andric
5700b57cec5SDimitry Andric case IMAGE_COMDAT_SELECT_SAME_SIZE:
571e8d8bef9SDimitry Andric if (leaderChunk->getSize() != getSection(sym)->SizeOfRawData) {
572bdd1243dSDimitry Andric if (!ctx.config.mingw) {
573349cc55cSDimitry Andric ctx.symtab.reportDuplicate(leader, this);
574e8d8bef9SDimitry Andric } else {
575fe6060f1SDimitry Andric const coff_aux_section_definition *leaderDef = nullptr;
576fe6060f1SDimitry Andric if (leaderChunk->file)
577fe6060f1SDimitry Andric leaderDef = findSectionDef(leaderChunk->file->getCOFFObj(),
578fe6060f1SDimitry Andric leaderChunk->getSectionNumber());
579e8d8bef9SDimitry Andric if (!leaderDef || leaderDef->Length != def->Length)
580349cc55cSDimitry Andric ctx.symtab.reportDuplicate(leader, this);
581e8d8bef9SDimitry Andric }
582e8d8bef9SDimitry Andric }
5830b57cec5SDimitry Andric break;
5840b57cec5SDimitry Andric
5850b57cec5SDimitry Andric case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
5860b57cec5SDimitry Andric SectionChunk newChunk(this, getSection(sym));
5870b57cec5SDimitry Andric // link.exe only compares section contents here and doesn't complain
5880b57cec5SDimitry Andric // if the two comdat sections have e.g. different alignment.
5890b57cec5SDimitry Andric // Match that.
5900b57cec5SDimitry Andric if (leaderChunk->getContents() != newChunk.getContents())
591349cc55cSDimitry Andric ctx.symtab.reportDuplicate(leader, this, &newChunk, sym.getValue());
5920b57cec5SDimitry Andric break;
5930b57cec5SDimitry Andric }
5940b57cec5SDimitry Andric
5950b57cec5SDimitry Andric case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
5960b57cec5SDimitry Andric // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
5970b57cec5SDimitry Andric // (This means lld-link doesn't produce duplicate symbol errors for
5980b57cec5SDimitry Andric // associative comdats while link.exe does, but associate comdats
5990b57cec5SDimitry Andric // are never extern in practice.)
6000b57cec5SDimitry Andric llvm_unreachable("createDefined not called for associative comdats");
6010b57cec5SDimitry Andric
6020b57cec5SDimitry Andric case IMAGE_COMDAT_SELECT_LARGEST:
6030b57cec5SDimitry Andric if (leaderChunk->getSize() < getSection(sym)->SizeOfRawData) {
6040b57cec5SDimitry Andric // Replace the existing comdat symbol with the new one.
6055ffd83dbSDimitry Andric StringRef name = check(coffObj->getSymbolName(sym));
6060b57cec5SDimitry Andric // FIXME: This is incorrect: With /opt:noref, the previous sections
6070b57cec5SDimitry Andric // make it into the final executable as well. Correct handling would
6080b57cec5SDimitry Andric // be to undo reading of the whole old section that's being replaced,
6090b57cec5SDimitry Andric // or doing one pass that determines what the final largest comdat
6100b57cec5SDimitry Andric // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
6110b57cec5SDimitry Andric // only the largest one.
6120b57cec5SDimitry Andric replaceSymbol<DefinedRegular>(leader, this, name, /*IsCOMDAT*/ true,
6130b57cec5SDimitry Andric /*IsExternal*/ true, sym.getGeneric(),
6140b57cec5SDimitry Andric nullptr);
6150b57cec5SDimitry Andric prevailing = true;
6160b57cec5SDimitry Andric }
6170b57cec5SDimitry Andric break;
6180b57cec5SDimitry Andric
6190b57cec5SDimitry Andric case IMAGE_COMDAT_SELECT_NEWEST:
6200b57cec5SDimitry Andric llvm_unreachable("should have been rejected earlier");
6210b57cec5SDimitry Andric }
6220b57cec5SDimitry Andric }
6230b57cec5SDimitry Andric
createDefined(COFFSymbolRef sym,std::vector<const coff_aux_section_definition * > & comdatDefs,bool & prevailing)624bdd1243dSDimitry Andric std::optional<Symbol *> ObjFile::createDefined(
6250b57cec5SDimitry Andric COFFSymbolRef sym,
6260b57cec5SDimitry Andric std::vector<const coff_aux_section_definition *> &comdatDefs,
6270b57cec5SDimitry Andric bool &prevailing) {
6280b57cec5SDimitry Andric prevailing = false;
6295ffd83dbSDimitry Andric auto getName = [&]() { return check(coffObj->getSymbolName(sym)); };
6300b57cec5SDimitry Andric
6310b57cec5SDimitry Andric if (sym.isCommon()) {
6320b57cec5SDimitry Andric auto *c = make<CommonChunk>(sym);
6330b57cec5SDimitry Andric chunks.push_back(c);
634349cc55cSDimitry Andric return ctx.symtab.addCommon(this, getName(), sym.getValue(),
635349cc55cSDimitry Andric sym.getGeneric(), c);
6360b57cec5SDimitry Andric }
6370b57cec5SDimitry Andric
6380b57cec5SDimitry Andric if (sym.isAbsolute()) {
6390b57cec5SDimitry Andric StringRef name = getName();
6400b57cec5SDimitry Andric
64185868e8aSDimitry Andric if (name == "@feat.00")
6420b57cec5SDimitry Andric feat00Flags = sym.getValue();
64385868e8aSDimitry Andric // Skip special symbols.
64485868e8aSDimitry Andric if (ignoredSymbolName(name))
6450b57cec5SDimitry Andric return nullptr;
6460b57cec5SDimitry Andric
6470b57cec5SDimitry Andric if (sym.isExternal())
648349cc55cSDimitry Andric return ctx.symtab.addAbsolute(name, sym);
649bdd1243dSDimitry Andric return make<DefinedAbsolute>(ctx, name, sym);
6500b57cec5SDimitry Andric }
6510b57cec5SDimitry Andric
6520b57cec5SDimitry Andric int32_t sectionNumber = sym.getSectionNumber();
6530b57cec5SDimitry Andric if (sectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
6540b57cec5SDimitry Andric return nullptr;
6550b57cec5SDimitry Andric
6560b57cec5SDimitry Andric if (llvm::COFF::isReservedSectionNumber(sectionNumber))
6570b57cec5SDimitry Andric fatal(toString(this) + ": " + getName() +
6580b57cec5SDimitry Andric " should not refer to special section " + Twine(sectionNumber));
6590b57cec5SDimitry Andric
6600b57cec5SDimitry Andric if ((uint32_t)sectionNumber >= sparseChunks.size())
6610b57cec5SDimitry Andric fatal(toString(this) + ": " + getName() +
6620b57cec5SDimitry Andric " should not refer to non-existent section " + Twine(sectionNumber));
6630b57cec5SDimitry Andric
6640b57cec5SDimitry Andric // Comdat handling.
6650b57cec5SDimitry Andric // A comdat symbol consists of two symbol table entries.
6660b57cec5SDimitry Andric // The first symbol entry has the name of the section (e.g. .text), fixed
66785868e8aSDimitry Andric // values for the other fields, and one auxiliary record.
6680b57cec5SDimitry Andric // The second symbol entry has the name of the comdat symbol, called the
6690b57cec5SDimitry Andric // "comdat leader".
6700b57cec5SDimitry Andric // When this function is called for the first symbol entry of a comdat,
671bdd1243dSDimitry Andric // it sets comdatDefs and returns std::nullopt, and when it's called for the
672bdd1243dSDimitry Andric // second symbol entry it reads comdatDefs and then sets it back to nullptr.
6730b57cec5SDimitry Andric
6740b57cec5SDimitry Andric // Handle comdat leader.
6750b57cec5SDimitry Andric if (const coff_aux_section_definition *def = comdatDefs[sectionNumber]) {
6760b57cec5SDimitry Andric comdatDefs[sectionNumber] = nullptr;
6770b57cec5SDimitry Andric DefinedRegular *leader;
6780b57cec5SDimitry Andric
6790b57cec5SDimitry Andric if (sym.isExternal()) {
6800b57cec5SDimitry Andric std::tie(leader, prevailing) =
681349cc55cSDimitry Andric ctx.symtab.addComdat(this, getName(), sym.getGeneric());
6820b57cec5SDimitry Andric } else {
6830b57cec5SDimitry Andric leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
6840b57cec5SDimitry Andric /*IsExternal*/ false, sym.getGeneric());
6850b57cec5SDimitry Andric prevailing = true;
6860b57cec5SDimitry Andric }
6870b57cec5SDimitry Andric
6880b57cec5SDimitry Andric if (def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
6890b57cec5SDimitry Andric // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
6900b57cec5SDimitry Andric // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
6910b57cec5SDimitry Andric def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
6920b57cec5SDimitry Andric fatal("unknown comdat type " + std::to_string((int)def->Selection) +
6930b57cec5SDimitry Andric " for " + getName() + " in " + toString(this));
6940b57cec5SDimitry Andric }
6950b57cec5SDimitry Andric COMDATType selection = (COMDATType)def->Selection;
6960b57cec5SDimitry Andric
6970b57cec5SDimitry Andric if (leader->isCOMDAT)
698e8d8bef9SDimitry Andric handleComdatSelection(sym, selection, prevailing, leader, def);
6990b57cec5SDimitry Andric
7000b57cec5SDimitry Andric if (prevailing) {
7010b57cec5SDimitry Andric SectionChunk *c = readSection(sectionNumber, def, getName());
7020b57cec5SDimitry Andric sparseChunks[sectionNumber] = c;
703bdb86d1aSDimitry Andric if (!c)
704bdb86d1aSDimitry Andric return nullptr;
7050b57cec5SDimitry Andric c->sym = cast<DefinedRegular>(leader);
7060b57cec5SDimitry Andric c->selection = selection;
7070b57cec5SDimitry Andric cast<DefinedRegular>(leader)->data = &c->repl;
7080b57cec5SDimitry Andric } else {
7090b57cec5SDimitry Andric sparseChunks[sectionNumber] = nullptr;
7100b57cec5SDimitry Andric }
7110b57cec5SDimitry Andric return leader;
7120b57cec5SDimitry Andric }
7130b57cec5SDimitry Andric
7140b57cec5SDimitry Andric // Prepare to handle the comdat leader symbol by setting the section's
7150b57cec5SDimitry Andric // ComdatDefs pointer if we encounter a non-associative comdat.
7160b57cec5SDimitry Andric if (sparseChunks[sectionNumber] == pendingComdat) {
7170b57cec5SDimitry Andric if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
7180b57cec5SDimitry Andric if (def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
7190b57cec5SDimitry Andric comdatDefs[sectionNumber] = def;
7200b57cec5SDimitry Andric }
721bdd1243dSDimitry Andric return std::nullopt;
7220b57cec5SDimitry Andric }
7230b57cec5SDimitry Andric
7240b57cec5SDimitry Andric return createRegular(sym);
7250b57cec5SDimitry Andric }
7260b57cec5SDimitry Andric
getMachineType()7270b57cec5SDimitry Andric MachineTypes ObjFile::getMachineType() {
7280b57cec5SDimitry Andric if (coffObj)
7290b57cec5SDimitry Andric return static_cast<MachineTypes>(coffObj->getMachine());
7300b57cec5SDimitry Andric return IMAGE_FILE_MACHINE_UNKNOWN;
7310b57cec5SDimitry Andric }
7320b57cec5SDimitry Andric
getDebugSection(StringRef secName)7330b57cec5SDimitry Andric ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {
7340b57cec5SDimitry Andric if (SectionChunk *sec = SectionChunk::findByName(debugChunks, secName))
7350b57cec5SDimitry Andric return sec->consumeDebugMagic();
7360b57cec5SDimitry Andric return {};
7370b57cec5SDimitry Andric }
7380b57cec5SDimitry Andric
73985868e8aSDimitry Andric // OBJ files systematically store critical information in a .debug$S stream,
7400b57cec5SDimitry Andric // even if the TU was compiled with no debug info. At least two records are
7410b57cec5SDimitry Andric // always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
7420b57cec5SDimitry Andric // PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
7430b57cec5SDimitry Andric // currently used to initialize the hotPatchable member.
initializeFlags()7440b57cec5SDimitry Andric void ObjFile::initializeFlags() {
7450b57cec5SDimitry Andric ArrayRef<uint8_t> data = getDebugSection(".debug$S");
7460b57cec5SDimitry Andric if (data.empty())
7470b57cec5SDimitry Andric return;
7480b57cec5SDimitry Andric
7490b57cec5SDimitry Andric DebugSubsectionArray subsections;
7500b57cec5SDimitry Andric
7515f757f3fSDimitry Andric BinaryStreamReader reader(data, llvm::endianness::little);
7520b57cec5SDimitry Andric ExitOnError exitOnErr;
7530b57cec5SDimitry Andric exitOnErr(reader.readArray(subsections, data.size()));
7540b57cec5SDimitry Andric
7550b57cec5SDimitry Andric for (const DebugSubsectionRecord &ss : subsections) {
7560b57cec5SDimitry Andric if (ss.kind() != DebugSubsectionKind::Symbols)
7570b57cec5SDimitry Andric continue;
7580b57cec5SDimitry Andric
7590b57cec5SDimitry Andric unsigned offset = 0;
7600b57cec5SDimitry Andric
7610b57cec5SDimitry Andric // Only parse the first two records. We are only looking for S_OBJNAME
7620b57cec5SDimitry Andric // and S_COMPILE3, and they usually appear at the beginning of the
7630b57cec5SDimitry Andric // stream.
7640b57cec5SDimitry Andric for (unsigned i = 0; i < 2; ++i) {
7650b57cec5SDimitry Andric Expected<CVSymbol> sym = readSymbolFromStream(ss.getRecordData(), offset);
7660b57cec5SDimitry Andric if (!sym) {
7670b57cec5SDimitry Andric consumeError(sym.takeError());
7680b57cec5SDimitry Andric return;
7690b57cec5SDimitry Andric }
7700b57cec5SDimitry Andric if (sym->kind() == SymbolKind::S_COMPILE3) {
7710b57cec5SDimitry Andric auto cs =
7720b57cec5SDimitry Andric cantFail(SymbolDeserializer::deserializeAs<Compile3Sym>(sym.get()));
7730b57cec5SDimitry Andric hotPatchable =
7740b57cec5SDimitry Andric (cs.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
7750b57cec5SDimitry Andric }
7760b57cec5SDimitry Andric if (sym->kind() == SymbolKind::S_OBJNAME) {
7770b57cec5SDimitry Andric auto objName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
7780b57cec5SDimitry Andric sym.get()));
779bdd1243dSDimitry Andric if (objName.Signature)
7800b57cec5SDimitry Andric pchSignature = objName.Signature;
7810b57cec5SDimitry Andric }
7820b57cec5SDimitry Andric offset += sym->length();
7830b57cec5SDimitry Andric }
7840b57cec5SDimitry Andric }
7850b57cec5SDimitry Andric }
7860b57cec5SDimitry Andric
7870b57cec5SDimitry Andric // Depending on the compilation flags, OBJs can refer to external files,
7880b57cec5SDimitry Andric // necessary to merge this OBJ into the final PDB. We currently support two
7890b57cec5SDimitry Andric // types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
7900b57cec5SDimitry Andric // And PDB type servers, when compiling with /Zi. This function extracts these
7910b57cec5SDimitry Andric // dependencies and makes them available as a TpiSource interface (see
7920b57cec5SDimitry Andric // DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
7930b57cec5SDimitry Andric // output even with /Yc and /Yu and with /Zi.
initializeDependencies()7940b57cec5SDimitry Andric void ObjFile::initializeDependencies() {
795bdd1243dSDimitry Andric if (!ctx.config.debug)
7960b57cec5SDimitry Andric return;
7970b57cec5SDimitry Andric
7980b57cec5SDimitry Andric bool isPCH = false;
7990b57cec5SDimitry Andric
8000b57cec5SDimitry Andric ArrayRef<uint8_t> data = getDebugSection(".debug$P");
8010b57cec5SDimitry Andric if (!data.empty())
8020b57cec5SDimitry Andric isPCH = true;
8030b57cec5SDimitry Andric else
8040b57cec5SDimitry Andric data = getDebugSection(".debug$T");
8050b57cec5SDimitry Andric
806e8d8bef9SDimitry Andric // symbols but no types, make a plain, empty TpiSource anyway, because it
807e8d8bef9SDimitry Andric // simplifies adding the symbols later.
808e8d8bef9SDimitry Andric if (data.empty()) {
809e8d8bef9SDimitry Andric if (!debugChunks.empty())
810349cc55cSDimitry Andric debugTypesObj = makeTpiSource(ctx, this);
8110b57cec5SDimitry Andric return;
812e8d8bef9SDimitry Andric }
8130b57cec5SDimitry Andric
8145ffd83dbSDimitry Andric // Get the first type record. It will indicate if this object uses a type
8155ffd83dbSDimitry Andric // server (/Zi) or a PCH file (/Yu).
8160b57cec5SDimitry Andric CVTypeArray types;
8175f757f3fSDimitry Andric BinaryStreamReader reader(data, llvm::endianness::little);
8180b57cec5SDimitry Andric cantFail(reader.readArray(types, reader.getLength()));
8190b57cec5SDimitry Andric CVTypeArray::Iterator firstType = types.begin();
8200b57cec5SDimitry Andric if (firstType == types.end())
8210b57cec5SDimitry Andric return;
8220b57cec5SDimitry Andric
823480093f4SDimitry Andric // Remember the .debug$T or .debug$P section.
824480093f4SDimitry Andric debugTypes = data;
8250b57cec5SDimitry Andric
8265ffd83dbSDimitry Andric // This object file is a PCH file that others will depend on.
8270b57cec5SDimitry Andric if (isPCH) {
828349cc55cSDimitry Andric debugTypesObj = makePrecompSource(ctx, this);
8290b57cec5SDimitry Andric return;
8300b57cec5SDimitry Andric }
8310b57cec5SDimitry Andric
8325ffd83dbSDimitry Andric // This object file was compiled with /Zi. Enqueue the PDB dependency.
8330b57cec5SDimitry Andric if (firstType->kind() == LF_TYPESERVER2) {
8340b57cec5SDimitry Andric TypeServer2Record ts = cantFail(
8350b57cec5SDimitry Andric TypeDeserializer::deserializeAs<TypeServer2Record>(firstType->data()));
836349cc55cSDimitry Andric debugTypesObj = makeUseTypeServerSource(ctx, this, ts);
837349cc55cSDimitry Andric enqueuePdbFile(ts.getName(), this);
8380b57cec5SDimitry Andric return;
8390b57cec5SDimitry Andric }
8400b57cec5SDimitry Andric
8415ffd83dbSDimitry Andric // This object was compiled with /Yu. It uses types from another object file
8425ffd83dbSDimitry Andric // with a matching signature.
8430b57cec5SDimitry Andric if (firstType->kind() == LF_PRECOMP) {
8440b57cec5SDimitry Andric PrecompRecord precomp = cantFail(
8450b57cec5SDimitry Andric TypeDeserializer::deserializeAs<PrecompRecord>(firstType->data()));
846bdd1243dSDimitry Andric // We're better off trusting the LF_PRECOMP signature. In some cases the
847bdd1243dSDimitry Andric // S_OBJNAME record doesn't contain a valid PCH signature.
848bdd1243dSDimitry Andric if (precomp.Signature)
849bdd1243dSDimitry Andric pchSignature = precomp.Signature;
850349cc55cSDimitry Andric debugTypesObj = makeUsePrecompSource(ctx, this, precomp);
851e8d8bef9SDimitry Andric // Drop the LF_PRECOMP record from the input stream.
852e8d8bef9SDimitry Andric debugTypes = debugTypes.drop_front(firstType->RecordData.size());
8530b57cec5SDimitry Andric return;
8540b57cec5SDimitry Andric }
8550b57cec5SDimitry Andric
8565ffd83dbSDimitry Andric // This is a plain old object file.
857349cc55cSDimitry Andric debugTypesObj = makeTpiSource(ctx, this);
8580b57cec5SDimitry Andric }
8590b57cec5SDimitry Andric
8605ffd83dbSDimitry Andric // The casing of the PDB path stamped in the OBJ can differ from the actual path
8615ffd83dbSDimitry Andric // on disk. With this, we ensure to always use lowercase as a key for the
862349cc55cSDimitry Andric // pdbInputFileInstances map, at least on Windows.
normalizePdbPath(StringRef path)8635ffd83dbSDimitry Andric static std::string normalizePdbPath(StringRef path) {
8645ffd83dbSDimitry Andric #if defined(_WIN32)
8655ffd83dbSDimitry Andric return path.lower();
8665ffd83dbSDimitry Andric #else // LINUX
8675ffd83dbSDimitry Andric return std::string(path);
8685ffd83dbSDimitry Andric #endif
8695ffd83dbSDimitry Andric }
8705ffd83dbSDimitry Andric
8715ffd83dbSDimitry Andric // If existing, return the actual PDB path on disk.
872*0fca6ea1SDimitry Andric static std::optional<std::string>
findPdbPath(StringRef pdbPath,ObjFile * dependentFile,StringRef outputPath)873*0fca6ea1SDimitry Andric findPdbPath(StringRef pdbPath, ObjFile *dependentFile, StringRef outputPath) {
8745ffd83dbSDimitry Andric // Ensure the file exists before anything else. In some cases, if the path
8755ffd83dbSDimitry Andric // points to a removable device, Driver::enqueuePath() would fail with an
8765ffd83dbSDimitry Andric // error (EAGAIN, "resource unavailable try again") which we want to skip
8775ffd83dbSDimitry Andric // silently.
8785ffd83dbSDimitry Andric if (llvm::sys::fs::exists(pdbPath))
8795ffd83dbSDimitry Andric return normalizePdbPath(pdbPath);
880*0fca6ea1SDimitry Andric
881*0fca6ea1SDimitry Andric StringRef objPath = !dependentFile->parentName.empty()
882*0fca6ea1SDimitry Andric ? dependentFile->parentName
883*0fca6ea1SDimitry Andric : dependentFile->getName();
884*0fca6ea1SDimitry Andric
885*0fca6ea1SDimitry Andric // Currently, type server PDBs are only created by MSVC cl, which only runs
886*0fca6ea1SDimitry Andric // on Windows, so we can assume type server paths are Windows style.
887*0fca6ea1SDimitry Andric StringRef pdbName = sys::path::filename(pdbPath, sys::path::Style::windows);
888*0fca6ea1SDimitry Andric
889*0fca6ea1SDimitry Andric // Check if the PDB is in the same folder as the OBJ.
890*0fca6ea1SDimitry Andric SmallString<128> path;
891*0fca6ea1SDimitry Andric sys::path::append(path, sys::path::parent_path(objPath), pdbName);
892*0fca6ea1SDimitry Andric if (llvm::sys::fs::exists(path))
893*0fca6ea1SDimitry Andric return normalizePdbPath(path);
894*0fca6ea1SDimitry Andric
895*0fca6ea1SDimitry Andric // Check if the PDB is in the output folder.
896*0fca6ea1SDimitry Andric path.clear();
897*0fca6ea1SDimitry Andric sys::path::append(path, sys::path::parent_path(outputPath), pdbName);
898*0fca6ea1SDimitry Andric if (llvm::sys::fs::exists(path))
899*0fca6ea1SDimitry Andric return normalizePdbPath(path);
900*0fca6ea1SDimitry Andric
901bdd1243dSDimitry Andric return std::nullopt;
9025ffd83dbSDimitry Andric }
9035ffd83dbSDimitry Andric
PDBInputFile(COFFLinkerContext & ctx,MemoryBufferRef m)904349cc55cSDimitry Andric PDBInputFile::PDBInputFile(COFFLinkerContext &ctx, MemoryBufferRef m)
905349cc55cSDimitry Andric : InputFile(ctx, PDBKind, m) {}
9065ffd83dbSDimitry Andric
9075ffd83dbSDimitry Andric PDBInputFile::~PDBInputFile() = default;
9085ffd83dbSDimitry Andric
findFromRecordPath(const COFFLinkerContext & ctx,StringRef path,ObjFile * fromFile)909349cc55cSDimitry Andric PDBInputFile *PDBInputFile::findFromRecordPath(const COFFLinkerContext &ctx,
910349cc55cSDimitry Andric StringRef path,
9115ffd83dbSDimitry Andric ObjFile *fromFile) {
912*0fca6ea1SDimitry Andric auto p = findPdbPath(path.str(), fromFile, ctx.config.outputFile);
9135ffd83dbSDimitry Andric if (!p)
9145ffd83dbSDimitry Andric return nullptr;
915349cc55cSDimitry Andric auto it = ctx.pdbInputFileInstances.find(*p);
916349cc55cSDimitry Andric if (it != ctx.pdbInputFileInstances.end())
9175ffd83dbSDimitry Andric return it->second;
9185ffd83dbSDimitry Andric return nullptr;
9195ffd83dbSDimitry Andric }
9205ffd83dbSDimitry Andric
parse()9215ffd83dbSDimitry Andric void PDBInputFile::parse() {
922349cc55cSDimitry Andric ctx.pdbInputFileInstances[mb.getBufferIdentifier().str()] = this;
9235ffd83dbSDimitry Andric
9245ffd83dbSDimitry Andric std::unique_ptr<pdb::IPDBSession> thisSession;
925bdd1243dSDimitry Andric Error E = pdb::NativeSession::createFromPdb(
926bdd1243dSDimitry Andric MemoryBuffer::getMemBuffer(mb, false), thisSession);
927bdd1243dSDimitry Andric if (E) {
928bdd1243dSDimitry Andric loadErrorStr.emplace(toString(std::move(E)));
9295ffd83dbSDimitry Andric return; // fail silently at this point - the error will be handled later,
9305ffd83dbSDimitry Andric // when merging the debug type stream
931bdd1243dSDimitry Andric }
9325ffd83dbSDimitry Andric
9335ffd83dbSDimitry Andric session.reset(static_cast<pdb::NativeSession *>(thisSession.release()));
9345ffd83dbSDimitry Andric
9355ffd83dbSDimitry Andric pdb::PDBFile &pdbFile = session->getPDBFile();
9365ffd83dbSDimitry Andric auto expectedInfo = pdbFile.getPDBInfoStream();
9375ffd83dbSDimitry Andric // All PDB Files should have an Info stream.
9385ffd83dbSDimitry Andric if (!expectedInfo) {
939bdd1243dSDimitry Andric loadErrorStr.emplace(toString(expectedInfo.takeError()));
9405ffd83dbSDimitry Andric return;
9415ffd83dbSDimitry Andric }
942349cc55cSDimitry Andric debugTypesObj = makeTypeServerSource(ctx, this);
9435ffd83dbSDimitry Andric }
9445ffd83dbSDimitry Andric
94585868e8aSDimitry Andric // Used only for DWARF debug info, which is not common (except in MinGW
94685868e8aSDimitry Andric // environments). This returns an optional pair of file name and line
94785868e8aSDimitry Andric // number for where the variable was defined.
948bdd1243dSDimitry Andric std::optional<std::pair<StringRef, uint32_t>>
getVariableLocation(StringRef var)94985868e8aSDimitry Andric ObjFile::getVariableLocation(StringRef var) {
95085868e8aSDimitry Andric if (!dwarf) {
95185868e8aSDimitry Andric dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
95285868e8aSDimitry Andric if (!dwarf)
953bdd1243dSDimitry Andric return std::nullopt;
95485868e8aSDimitry Andric }
955bdd1243dSDimitry Andric if (ctx.config.machine == I386)
95685868e8aSDimitry Andric var.consume_front("_");
957bdd1243dSDimitry Andric std::optional<std::pair<std::string, unsigned>> ret =
958bdd1243dSDimitry Andric dwarf->getVariableLoc(var);
95985868e8aSDimitry Andric if (!ret)
960bdd1243dSDimitry Andric return std::nullopt;
96104eeddc0SDimitry Andric return std::make_pair(saver().save(ret->first), ret->second);
96285868e8aSDimitry Andric }
96385868e8aSDimitry Andric
96485868e8aSDimitry Andric // Used only for DWARF debug info, which is not common (except in MinGW
96585868e8aSDimitry Andric // environments).
getDILineInfo(uint32_t offset,uint32_t sectionIndex)966bdd1243dSDimitry Andric std::optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
96785868e8aSDimitry Andric uint32_t sectionIndex) {
96885868e8aSDimitry Andric if (!dwarf) {
96985868e8aSDimitry Andric dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
97085868e8aSDimitry Andric if (!dwarf)
971bdd1243dSDimitry Andric return std::nullopt;
97285868e8aSDimitry Andric }
97385868e8aSDimitry Andric
97485868e8aSDimitry Andric return dwarf->getDILineInfo(offset, sectionIndex);
97585868e8aSDimitry Andric }
97685868e8aSDimitry Andric
enqueuePdbFile(StringRef path,ObjFile * fromFile)977349cc55cSDimitry Andric void ObjFile::enqueuePdbFile(StringRef path, ObjFile *fromFile) {
978*0fca6ea1SDimitry Andric auto p = findPdbPath(path.str(), fromFile, ctx.config.outputFile);
979349cc55cSDimitry Andric if (!p)
980349cc55cSDimitry Andric return;
981349cc55cSDimitry Andric auto it = ctx.pdbInputFileInstances.emplace(*p, nullptr);
982349cc55cSDimitry Andric if (!it.second)
983349cc55cSDimitry Andric return; // already scheduled for load
984bdd1243dSDimitry Andric ctx.driver.enqueuePDB(*p);
985349cc55cSDimitry Andric }
986349cc55cSDimitry Andric
ImportFile(COFFLinkerContext & ctx,MemoryBufferRef m)987bdd1243dSDimitry Andric ImportFile::ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m)
988bdd1243dSDimitry Andric : InputFile(ctx, ImportKind, m), live(!ctx.config.doGC), thunkLive(live) {}
989bdd1243dSDimitry Andric
parse()9900b57cec5SDimitry Andric void ImportFile::parse() {
991*0fca6ea1SDimitry Andric const auto *hdr =
992*0fca6ea1SDimitry Andric reinterpret_cast<const coff_import_header *>(mb.getBufferStart());
9930b57cec5SDimitry Andric
9940b57cec5SDimitry Andric // Check if the total size is valid.
995*0fca6ea1SDimitry Andric if (mb.getBufferSize() < sizeof(*hdr) ||
996*0fca6ea1SDimitry Andric mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
9970b57cec5SDimitry Andric fatal("broken import library");
9980b57cec5SDimitry Andric
9990b57cec5SDimitry Andric // Read names and create an __imp_ symbol.
1000*0fca6ea1SDimitry Andric StringRef buf = mb.getBuffer().substr(sizeof(*hdr));
1001*0fca6ea1SDimitry Andric StringRef name = saver().save(buf.split('\0').first);
100204eeddc0SDimitry Andric StringRef impName = saver().save("__imp_" + name);
1003*0fca6ea1SDimitry Andric buf = buf.substr(name.size() + 1);
1004*0fca6ea1SDimitry Andric dllName = buf.split('\0').first;
10050b57cec5SDimitry Andric StringRef extName;
10060b57cec5SDimitry Andric switch (hdr->getNameType()) {
10070b57cec5SDimitry Andric case IMPORT_ORDINAL:
10080b57cec5SDimitry Andric extName = "";
10090b57cec5SDimitry Andric break;
10100b57cec5SDimitry Andric case IMPORT_NAME:
10110b57cec5SDimitry Andric extName = name;
10120b57cec5SDimitry Andric break;
10130b57cec5SDimitry Andric case IMPORT_NAME_NOPREFIX:
10140b57cec5SDimitry Andric extName = ltrim1(name, "?@_");
10150b57cec5SDimitry Andric break;
10160b57cec5SDimitry Andric case IMPORT_NAME_UNDECORATE:
10170b57cec5SDimitry Andric extName = ltrim1(name, "?@_");
10180b57cec5SDimitry Andric extName = extName.substr(0, extName.find('@'));
10190b57cec5SDimitry Andric break;
1020*0fca6ea1SDimitry Andric case IMPORT_NAME_EXPORTAS:
1021*0fca6ea1SDimitry Andric extName = buf.substr(dllName.size() + 1).split('\0').first;
1022*0fca6ea1SDimitry Andric break;
10230b57cec5SDimitry Andric }
10240b57cec5SDimitry Andric
10250b57cec5SDimitry Andric this->hdr = hdr;
10260b57cec5SDimitry Andric externalName = extName;
10270b57cec5SDimitry Andric
1028349cc55cSDimitry Andric impSym = ctx.symtab.addImportData(impName, this);
10290b57cec5SDimitry Andric // If this was a duplicate, we logged an error but may continue;
10300b57cec5SDimitry Andric // in this case, impSym is nullptr.
10310b57cec5SDimitry Andric if (!impSym)
10320b57cec5SDimitry Andric return;
10330b57cec5SDimitry Andric
10340b57cec5SDimitry Andric if (hdr->getType() == llvm::COFF::IMPORT_CONST)
1035349cc55cSDimitry Andric static_cast<void>(ctx.symtab.addImportData(name, this));
10360b57cec5SDimitry Andric
10370b57cec5SDimitry Andric // If type is function, we need to create a thunk which jump to an
10380b57cec5SDimitry Andric // address pointed by the __imp_ symbol. (This allows you to call
10390b57cec5SDimitry Andric // DLL functions just like regular non-DLL functions.)
10400b57cec5SDimitry Andric if (hdr->getType() == llvm::COFF::IMPORT_CODE)
1041349cc55cSDimitry Andric thunkSym = ctx.symtab.addImportThunk(
10420b57cec5SDimitry Andric name, cast_or_null<DefinedImportData>(impSym), hdr->Machine);
10430b57cec5SDimitry Andric }
10440b57cec5SDimitry Andric
BitcodeFile(COFFLinkerContext & ctx,MemoryBufferRef mb,StringRef archiveName,uint64_t offsetInArchive,bool lazy)1045349cc55cSDimitry Andric BitcodeFile::BitcodeFile(COFFLinkerContext &ctx, MemoryBufferRef mb,
1046349cc55cSDimitry Andric StringRef archiveName, uint64_t offsetInArchive,
104704eeddc0SDimitry Andric bool lazy)
104804eeddc0SDimitry Andric : InputFile(ctx, BitcodeKind, mb, lazy) {
10490b57cec5SDimitry Andric std::string path = mb.getBufferIdentifier().str();
1050bdd1243dSDimitry Andric if (ctx.config.thinLTOIndexOnly)
1051bdd1243dSDimitry Andric path = replaceThinLTOSuffix(mb.getBufferIdentifier(),
1052bdd1243dSDimitry Andric ctx.config.thinLTOObjectSuffixReplace.first,
1053bdd1243dSDimitry Andric ctx.config.thinLTOObjectSuffixReplace.second);
10540b57cec5SDimitry Andric
10550b57cec5SDimitry Andric // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
10560b57cec5SDimitry Andric // name. If two archives define two members with the same name, this
10570b57cec5SDimitry Andric // causes a collision which result in only one of the objects being taken
10580b57cec5SDimitry Andric // into consideration at LTO time (which very likely causes undefined
10590b57cec5SDimitry Andric // symbols later in the link stage). So we append file offset to make
10600b57cec5SDimitry Andric // filename unique.
106104eeddc0SDimitry Andric MemoryBufferRef mbref(mb.getBuffer(),
106204eeddc0SDimitry Andric saver().save(archiveName.empty()
106304eeddc0SDimitry Andric ? path
106404eeddc0SDimitry Andric : archiveName +
106504eeddc0SDimitry Andric sys::path::filename(path) +
10665ffd83dbSDimitry Andric utostr(offsetInArchive)));
10670b57cec5SDimitry Andric
10680b57cec5SDimitry Andric obj = check(lto::InputFile::create(mbref));
10690b57cec5SDimitry Andric }
10700b57cec5SDimitry Andric
1071480093f4SDimitry Andric BitcodeFile::~BitcodeFile() = default;
1072480093f4SDimitry Andric
parse()10730b57cec5SDimitry Andric void BitcodeFile::parse() {
107404eeddc0SDimitry Andric llvm::StringSaver &saver = lld::saver();
1075bdd1243dSDimitry Andric
10760b57cec5SDimitry Andric std::vector<std::pair<Symbol *, bool>> comdat(obj->getComdatTable().size());
10770b57cec5SDimitry Andric for (size_t i = 0; i != obj->getComdatTable().size(); ++i)
1078fe6060f1SDimitry Andric // FIXME: Check nodeduplicate
1079fe6060f1SDimitry Andric comdat[i] =
1080349cc55cSDimitry Andric ctx.symtab.addComdat(this, saver.save(obj->getComdatTable()[i].first));
10810b57cec5SDimitry Andric for (const lto::InputFile::Symbol &objSym : obj->symbols()) {
10820b57cec5SDimitry Andric StringRef symName = saver.save(objSym.getName());
10830b57cec5SDimitry Andric int comdatIndex = objSym.getComdatIndex();
10840b57cec5SDimitry Andric Symbol *sym;
1085fe6060f1SDimitry Andric SectionChunk *fakeSC = nullptr;
1086fe6060f1SDimitry Andric if (objSym.isExecutable())
1087bdd1243dSDimitry Andric fakeSC = &ctx.ltoTextSectionChunk.chunk;
1088fe6060f1SDimitry Andric else
1089bdd1243dSDimitry Andric fakeSC = &ctx.ltoDataSectionChunk.chunk;
10900b57cec5SDimitry Andric if (objSym.isUndefined()) {
1091349cc55cSDimitry Andric sym = ctx.symtab.addUndefined(symName, this, false);
10925f757f3fSDimitry Andric if (objSym.isWeak())
10935f757f3fSDimitry Andric sym->deferUndefined = true;
10945f757f3fSDimitry Andric // If one LTO object file references (i.e. has an undefined reference to)
10955f757f3fSDimitry Andric // a symbol with an __imp_ prefix, the LTO compilation itself sees it
10965f757f3fSDimitry Andric // as unprefixed but with a dllimport attribute instead, and doesn't
10975f757f3fSDimitry Andric // understand the relation to a concrete IR symbol with the __imp_ prefix.
10985f757f3fSDimitry Andric //
10995f757f3fSDimitry Andric // For such cases, mark the symbol as used in a regular object (i.e. the
11005f757f3fSDimitry Andric // symbol must be retained) so that the linker can associate the
11015f757f3fSDimitry Andric // references in the end. If the symbol is defined in an import library
11025f757f3fSDimitry Andric // or in a regular object file, this has no effect, but if it is defined
11035f757f3fSDimitry Andric // in another LTO object file, this makes sure it is kept, to fulfill
11045f757f3fSDimitry Andric // the reference when linking the output of the LTO compilation.
11055f757f3fSDimitry Andric if (symName.starts_with("__imp_"))
11065f757f3fSDimitry Andric sym->isUsedInRegularObj = true;
11070b57cec5SDimitry Andric } else if (objSym.isCommon()) {
1108349cc55cSDimitry Andric sym = ctx.symtab.addCommon(this, symName, objSym.getCommonSize());
11090b57cec5SDimitry Andric } else if (objSym.isWeak() && objSym.isIndirect()) {
11100b57cec5SDimitry Andric // Weak external.
1111349cc55cSDimitry Andric sym = ctx.symtab.addUndefined(symName, this, true);
11125ffd83dbSDimitry Andric std::string fallback = std::string(objSym.getCOFFWeakExternalFallback());
1113349cc55cSDimitry Andric Symbol *alias = ctx.symtab.addUndefined(saver.save(fallback));
1114bdd1243dSDimitry Andric checkAndSetWeakAlias(ctx, this, sym, alias);
11150b57cec5SDimitry Andric } else if (comdatIndex != -1) {
1116fe6060f1SDimitry Andric if (symName == obj->getComdatTable()[comdatIndex].first) {
11170b57cec5SDimitry Andric sym = comdat[comdatIndex].first;
1118fe6060f1SDimitry Andric if (cast<DefinedRegular>(sym)->data == nullptr)
1119fe6060f1SDimitry Andric cast<DefinedRegular>(sym)->data = &fakeSC->repl;
1120fe6060f1SDimitry Andric } else if (comdat[comdatIndex].second) {
1121349cc55cSDimitry Andric sym = ctx.symtab.addRegular(this, symName, nullptr, fakeSC);
11220b57cec5SDimitry Andric } else {
1123349cc55cSDimitry Andric sym = ctx.symtab.addUndefined(symName, this, false);
1124fe6060f1SDimitry Andric }
1125fe6060f1SDimitry Andric } else {
1126bdd1243dSDimitry Andric sym = ctx.symtab.addRegular(this, symName, nullptr, fakeSC, 0,
1127bdd1243dSDimitry Andric objSym.isWeak());
11280b57cec5SDimitry Andric }
11290b57cec5SDimitry Andric symbols.push_back(sym);
11300b57cec5SDimitry Andric if (objSym.isUsed())
1131bdd1243dSDimitry Andric ctx.config.gcroot.push_back(sym);
11320b57cec5SDimitry Andric }
11337a6dacacSDimitry Andric directives = saver.save(obj->getCOFFLinkerOpts());
11340b57cec5SDimitry Andric }
11350b57cec5SDimitry Andric
parseLazy()113604eeddc0SDimitry Andric void BitcodeFile::parseLazy() {
113704eeddc0SDimitry Andric for (const lto::InputFile::Symbol &sym : obj->symbols())
113804eeddc0SDimitry Andric if (!sym.isUndefined())
113904eeddc0SDimitry Andric ctx.symtab.addLazyObject(this, sym.getName());
114004eeddc0SDimitry Andric }
114104eeddc0SDimitry Andric
getMachineType()11420b57cec5SDimitry Andric MachineTypes BitcodeFile::getMachineType() {
11430b57cec5SDimitry Andric switch (Triple(obj->getTargetTriple()).getArch()) {
11440b57cec5SDimitry Andric case Triple::x86_64:
11450b57cec5SDimitry Andric return AMD64;
11460b57cec5SDimitry Andric case Triple::x86:
11470b57cec5SDimitry Andric return I386;
11480b57cec5SDimitry Andric case Triple::arm:
11495f757f3fSDimitry Andric case Triple::thumb:
11500b57cec5SDimitry Andric return ARMNT;
11510b57cec5SDimitry Andric case Triple::aarch64:
11520b57cec5SDimitry Andric return ARM64;
11530b57cec5SDimitry Andric default:
11540b57cec5SDimitry Andric return IMAGE_FILE_MACHINE_UNKNOWN;
11550b57cec5SDimitry Andric }
11560b57cec5SDimitry Andric }
11570b57cec5SDimitry Andric
replaceThinLTOSuffix(StringRef path,StringRef suffix,StringRef repl)1158bdd1243dSDimitry Andric std::string lld::coff::replaceThinLTOSuffix(StringRef path, StringRef suffix,
1159bdd1243dSDimitry Andric StringRef repl) {
11600b57cec5SDimitry Andric if (path.consume_back(suffix))
11610b57cec5SDimitry Andric return (path + repl).str();
11625ffd83dbSDimitry Andric return std::string(path);
11630b57cec5SDimitry Andric }
1164fe6060f1SDimitry Andric
isRVACode(COFFObjectFile * coffObj,uint64_t rva,InputFile * file)1165fe6060f1SDimitry Andric static bool isRVACode(COFFObjectFile *coffObj, uint64_t rva, InputFile *file) {
1166fe6060f1SDimitry Andric for (size_t i = 1, e = coffObj->getNumberOfSections(); i <= e; i++) {
1167fe6060f1SDimitry Andric const coff_section *sec = CHECK(coffObj->getSection(i), file);
1168fe6060f1SDimitry Andric if (rva >= sec->VirtualAddress &&
1169fe6060f1SDimitry Andric rva <= sec->VirtualAddress + sec->VirtualSize) {
1170fe6060f1SDimitry Andric return (sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE) != 0;
1171fe6060f1SDimitry Andric }
1172fe6060f1SDimitry Andric }
1173fe6060f1SDimitry Andric return false;
1174fe6060f1SDimitry Andric }
1175fe6060f1SDimitry Andric
parse()1176fe6060f1SDimitry Andric void DLLFile::parse() {
1177fe6060f1SDimitry Andric // Parse a memory buffer as a PE-COFF executable.
1178fe6060f1SDimitry Andric std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
1179fe6060f1SDimitry Andric
1180fe6060f1SDimitry Andric if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
1181fe6060f1SDimitry Andric bin.release();
1182fe6060f1SDimitry Andric coffObj.reset(obj);
1183fe6060f1SDimitry Andric } else {
1184fe6060f1SDimitry Andric error(toString(this) + " is not a COFF file");
1185fe6060f1SDimitry Andric return;
1186fe6060f1SDimitry Andric }
1187fe6060f1SDimitry Andric
1188fe6060f1SDimitry Andric if (!coffObj->getPE32Header() && !coffObj->getPE32PlusHeader()) {
1189fe6060f1SDimitry Andric error(toString(this) + " is not a PE-COFF executable");
1190fe6060f1SDimitry Andric return;
1191fe6060f1SDimitry Andric }
1192fe6060f1SDimitry Andric
1193fe6060f1SDimitry Andric for (const auto &exp : coffObj->export_directories()) {
1194fe6060f1SDimitry Andric StringRef dllName, symbolName;
1195fe6060f1SDimitry Andric uint32_t exportRVA;
1196fe6060f1SDimitry Andric checkError(exp.getDllName(dllName));
1197fe6060f1SDimitry Andric checkError(exp.getSymbolName(symbolName));
1198fe6060f1SDimitry Andric checkError(exp.getExportRVA(exportRVA));
1199fe6060f1SDimitry Andric
1200fe6060f1SDimitry Andric if (symbolName.empty())
1201fe6060f1SDimitry Andric continue;
1202fe6060f1SDimitry Andric
1203fe6060f1SDimitry Andric bool code = isRVACode(coffObj.get(), exportRVA, this);
1204fe6060f1SDimitry Andric
1205fe6060f1SDimitry Andric Symbol *s = make<Symbol>();
1206fe6060f1SDimitry Andric s->dllName = dllName;
1207fe6060f1SDimitry Andric s->symbolName = symbolName;
1208fe6060f1SDimitry Andric s->importType = code ? ImportType::IMPORT_CODE : ImportType::IMPORT_DATA;
1209fe6060f1SDimitry Andric s->nameType = ImportNameType::IMPORT_NAME;
1210fe6060f1SDimitry Andric
1211fe6060f1SDimitry Andric if (coffObj->getMachine() == I386) {
121204eeddc0SDimitry Andric s->symbolName = symbolName = saver().save("_" + symbolName);
1213fe6060f1SDimitry Andric s->nameType = ImportNameType::IMPORT_NAME_NOPREFIX;
1214fe6060f1SDimitry Andric }
1215fe6060f1SDimitry Andric
121604eeddc0SDimitry Andric StringRef impName = saver().save("__imp_" + symbolName);
1217349cc55cSDimitry Andric ctx.symtab.addLazyDLLSymbol(this, s, impName);
1218fe6060f1SDimitry Andric if (code)
1219349cc55cSDimitry Andric ctx.symtab.addLazyDLLSymbol(this, s, symbolName);
1220fe6060f1SDimitry Andric }
1221fe6060f1SDimitry Andric }
1222fe6060f1SDimitry Andric
getMachineType()1223fe6060f1SDimitry Andric MachineTypes DLLFile::getMachineType() {
1224fe6060f1SDimitry Andric if (coffObj)
1225fe6060f1SDimitry Andric return static_cast<MachineTypes>(coffObj->getMachine());
1226fe6060f1SDimitry Andric return IMAGE_FILE_MACHINE_UNKNOWN;
1227fe6060f1SDimitry Andric }
1228fe6060f1SDimitry Andric
makeImport(DLLFile::Symbol * s)1229fe6060f1SDimitry Andric void DLLFile::makeImport(DLLFile::Symbol *s) {
1230fe6060f1SDimitry Andric if (!seen.insert(s->symbolName).second)
1231fe6060f1SDimitry Andric return;
1232fe6060f1SDimitry Andric
1233fe6060f1SDimitry Andric size_t impSize = s->dllName.size() + s->symbolName.size() + 2; // +2 for NULs
1234fe6060f1SDimitry Andric size_t size = sizeof(coff_import_header) + impSize;
123504eeddc0SDimitry Andric char *buf = bAlloc().Allocate<char>(size);
1236fe6060f1SDimitry Andric memset(buf, 0, size);
1237fe6060f1SDimitry Andric char *p = buf;
1238fe6060f1SDimitry Andric auto *imp = reinterpret_cast<coff_import_header *>(p);
1239fe6060f1SDimitry Andric p += sizeof(*imp);
1240fe6060f1SDimitry Andric imp->Sig2 = 0xFFFF;
1241fe6060f1SDimitry Andric imp->Machine = coffObj->getMachine();
1242fe6060f1SDimitry Andric imp->SizeOfData = impSize;
1243fe6060f1SDimitry Andric imp->OrdinalHint = 0; // Only linking by name
1244fe6060f1SDimitry Andric imp->TypeInfo = (s->nameType << 2) | s->importType;
1245fe6060f1SDimitry Andric
1246fe6060f1SDimitry Andric // Write symbol name and DLL name.
1247fe6060f1SDimitry Andric memcpy(p, s->symbolName.data(), s->symbolName.size());
1248fe6060f1SDimitry Andric p += s->symbolName.size() + 1;
1249fe6060f1SDimitry Andric memcpy(p, s->dllName.data(), s->dllName.size());
1250fe6060f1SDimitry Andric MemoryBufferRef mbref = MemoryBufferRef(StringRef(buf, size), s->dllName);
1251349cc55cSDimitry Andric ImportFile *impFile = make<ImportFile>(ctx, mbref);
1252349cc55cSDimitry Andric ctx.symtab.addFile(impFile);
1253fe6060f1SDimitry Andric }
1254