1 //===- DebugTypes.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 "DebugTypes.h"
11 #include "InputFiles.h"
12 #include "lld/Common/ErrorHandler.h"
13 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
14 #include "llvm/DebugInfo/PDB/GenericError.h"
15 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
16 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
17 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
18 #include "llvm/Support/Path.h"
21 using namespace lld::coff;
23 using namespace llvm::codeview;
26 // The TypeServerSource class represents a PDB type server, a file referenced by
27 // OBJ files compiled with MSVC /Zi. A single PDB can be shared by several OBJ
28 // files, therefore there must be only once instance per OBJ lot. The file path
29 // is discovered from the dependent OBJ's debug type stream. The
30 // TypeServerSource object is then queued and loaded by the COFF Driver. The
31 // debug type stream for such PDB files will be merged first in the final PDB,
32 // before any dependent OBJ.
33 class TypeServerSource : public TpiSource {
35 explicit TypeServerSource(MemoryBufferRef m, llvm::pdb::NativeSession *s)
36 : TpiSource(PDB, nullptr), session(s), mb(m) {}
38 // Queue a PDB type server for loading in the COFF Driver
39 static void enqueue(const ObjFile *dependentFile,
40 const TypeServer2Record &ts);
43 static Expected<TypeServerSource *> getInstance(MemoryBufferRef m);
45 // Fetch the PDB instance loaded for a corresponding dependent OBJ.
46 static Expected<TypeServerSource *>
47 findFromFile(const ObjFile *dependentFile);
49 static std::map<std::string, std::pair<std::string, TypeServerSource *>>
52 // The interface to the PDB (if it was opened successfully)
53 std::unique_ptr<llvm::pdb::NativeSession> session;
59 // This class represents the debug type stream of an OBJ file that depends on a
60 // PDB type server (see TypeServerSource).
61 class UseTypeServerSource : public TpiSource {
63 UseTypeServerSource(const ObjFile *f, const TypeServer2Record *ts)
64 : TpiSource(UsingPDB, f), typeServerDependency(*ts) {}
66 // Information about the PDB type server dependency, that needs to be loaded
67 // in before merging this OBJ.
68 TypeServer2Record typeServerDependency;
71 // This class represents the debug type stream of a Microsoft precompiled
72 // headers OBJ (PCH OBJ). This OBJ kind needs to be merged first in the output
73 // PDB, before any other OBJs that depend on this. Note that only MSVC generate
74 // such files, clang does not.
75 class PrecompSource : public TpiSource {
77 PrecompSource(const ObjFile *f) : TpiSource(PCH, f) {}
80 // This class represents the debug type stream of an OBJ file that depends on a
81 // Microsoft precompiled headers OBJ (see PrecompSource).
82 class UsePrecompSource : public TpiSource {
84 UsePrecompSource(const ObjFile *f, const PrecompRecord *precomp)
85 : TpiSource(UsingPCH, f), precompDependency(*precomp) {}
87 // Information about the Precomp OBJ dependency, that needs to be loaded in
88 // before merging this OBJ.
89 PrecompRecord precompDependency;
93 static std::vector<std::unique_ptr<TpiSource>> GC;
95 TpiSource::TpiSource(TpiKind k, const ObjFile *f) : kind(k), file(f) {
96 GC.push_back(std::unique_ptr<TpiSource>(this));
99 TpiSource *lld::coff::makeTpiSource(const ObjFile *f) {
100 return new TpiSource(TpiSource::Regular, f);
103 TpiSource *lld::coff::makeUseTypeServerSource(const ObjFile *f,
104 const TypeServer2Record *ts) {
105 TypeServerSource::enqueue(f, *ts);
106 return new UseTypeServerSource(f, ts);
109 TpiSource *lld::coff::makePrecompSource(const ObjFile *f) {
110 return new PrecompSource(f);
113 TpiSource *lld::coff::makeUsePrecompSource(const ObjFile *f,
114 const PrecompRecord *precomp) {
115 return new UsePrecompSource(f, precomp);
121 const PrecompRecord &retrieveDependencyInfo(const TpiSource *source) {
122 assert(source->kind == TpiSource::UsingPCH);
123 return ((const UsePrecompSource *)source)->precompDependency;
127 const TypeServer2Record &retrieveDependencyInfo(const TpiSource *source) {
128 assert(source->kind == TpiSource::UsingPDB);
129 return ((const UseTypeServerSource *)source)->typeServerDependency;
134 std::map<std::string, std::pair<std::string, TypeServerSource *>>
135 TypeServerSource::instances;
137 // Make a PDB path assuming the PDB is in the same folder as the OBJ
138 static std::string getPdbBaseName(const ObjFile *file, StringRef tSPath) {
139 StringRef localPath =
140 !file->parentName.empty() ? file->parentName : file->getName();
141 SmallString<128> path = sys::path::parent_path(localPath);
143 // Currently, type server PDBs are only created by MSVC cl, which only runs
144 // on Windows, so we can assume type server paths are Windows style.
145 sys::path::append(path, sys::path::filename(tSPath, sys::path::Style::windows));
149 // The casing of the PDB path stamped in the OBJ can differ from the actual path
150 // on disk. With this, we ensure to always use lowercase as a key for the
151 // PDBInputFile::Instances map, at least on Windows.
152 static std::string normalizePdbPath(StringRef path) {
160 // If existing, return the actual PDB path on disk.
161 static Optional<std::string> findPdbPath(StringRef pdbPath,
162 const ObjFile *dependentFile) {
163 // Ensure the file exists before anything else. In some cases, if the path
164 // points to a removable device, Driver::enqueuePath() would fail with an
165 // error (EAGAIN, "resource unavailable try again") which we want to skip
167 if (llvm::sys::fs::exists(pdbPath))
168 return normalizePdbPath(pdbPath);
169 std::string ret = getPdbBaseName(dependentFile, pdbPath);
170 if (llvm::sys::fs::exists(ret))
171 return normalizePdbPath(ret);
175 // Fetch the PDB instance that was already loaded by the COFF Driver.
176 Expected<TypeServerSource *>
177 TypeServerSource::findFromFile(const ObjFile *dependentFile) {
178 const TypeServer2Record &ts =
179 retrieveDependencyInfo<TypeServer2Record>(dependentFile->debugTypesObj);
181 Optional<std::string> p = findPdbPath(ts.Name, dependentFile);
183 return createFileError(ts.Name, errorCodeToError(std::error_code(
184 ENOENT, std::generic_category())));
186 auto it = TypeServerSource::instances.find(*p);
187 // The PDB file exists on disk, at this point we expect it to have been
188 // inserted in the map by TypeServerSource::loadPDB()
189 assert(it != TypeServerSource::instances.end());
191 std::pair<std::string, TypeServerSource *> &pdb = it->second;
194 return createFileError(
195 *p, createStringError(inconvertibleErrorCode(), pdb.first.c_str()));
197 pdb::PDBFile &pdbFile = (pdb.second)->session->getPDBFile();
198 pdb::InfoStream &info = cantFail(pdbFile.getPDBInfoStream());
200 // Just because a file with a matching name was found doesn't mean it can be
201 // used. The GUID must match between the PDB header and the OBJ
202 // TypeServer2 record. The 'Age' is used by MSVC incremental compilation.
203 if (info.getGuid() != ts.getGuid())
204 return createFileError(
206 make_error<pdb::PDBError>(pdb::pdb_error_code::signature_out_of_date));
211 // FIXME: Temporary interface until PDBLinker::maybeMergeTypeServerPDB() is
213 Expected<llvm::pdb::NativeSession *>
214 lld::coff::findTypeServerSource(const ObjFile *f) {
215 Expected<TypeServerSource *> ts = TypeServerSource::findFromFile(f);
217 return ts.takeError();
218 return ts.get()->session.get();
221 // Queue a PDB type server for loading in the COFF Driver
222 void TypeServerSource::enqueue(const ObjFile *dependentFile,
223 const TypeServer2Record &ts) {
224 // Start by finding where the PDB is located (either the record path or next
226 Optional<std::string> p = findPdbPath(ts.Name, dependentFile);
229 auto it = TypeServerSource::instances.emplace(
230 *p, std::pair<std::string, TypeServerSource *>{});
232 return; // another OBJ already scheduled this PDB for load
234 driver->enqueuePath(*p, false);
237 // Create an instance of TypeServerSource or an error string if the PDB couldn't
238 // be loaded. The error message will be displayed later, when the referring OBJ
239 // will be merged in. NOTE - a PDB load failure is not a link error: some
240 // debug info will simply be missing from the final PDB - that is the default
241 // accepted behavior.
242 void lld::coff::loadTypeServerSource(llvm::MemoryBufferRef m) {
243 std::string path = normalizePdbPath(m.getBufferIdentifier());
245 Expected<TypeServerSource *> ts = TypeServerSource::getInstance(m);
247 TypeServerSource::instances[path] = {toString(ts.takeError()), nullptr};
249 TypeServerSource::instances[path] = {{}, *ts};
252 Expected<TypeServerSource *> TypeServerSource::getInstance(MemoryBufferRef m) {
253 std::unique_ptr<llvm::pdb::IPDBSession> iSession;
254 Error err = pdb::NativeSession::createFromPdb(
255 MemoryBuffer::getMemBuffer(m, false), iSession);
257 return std::move(err);
259 std::unique_ptr<llvm::pdb::NativeSession> session(
260 static_cast<pdb::NativeSession *>(iSession.release()));
262 pdb::PDBFile &pdbFile = session->getPDBFile();
263 Expected<pdb::InfoStream &> info = pdbFile.getPDBInfoStream();
264 // All PDB Files should have an Info stream.
266 return info.takeError();
267 return new TypeServerSource(m, session.release());