LLVM 19.0.0git
LVReaderHandler.cpp
Go to the documentation of this file.
1//===-- LVReaderHandler.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This class implements the Reader Handler.
10//
11//===----------------------------------------------------------------------===//
12
20#include "llvm/Object/COFF.h"
21
22using namespace llvm;
23using namespace llvm::object;
24using namespace llvm::pdb;
25using namespace llvm::logicalview;
26
27#define DEBUG_TYPE "ReaderHandler"
28
30 if (Error Err = createReaders())
31 return Err;
32 if (Error Err = printReaders())
33 return Err;
34 if (Error Err = compareReaders())
35 return Err;
36
37 return Error::success();
38}
39
40Error LVReaderHandler::createReader(StringRef Filename, LVReaders &Readers,
41 PdbOrObj &Input, StringRef FileFormatName,
42 StringRef ExePath) {
43 auto CreateOneReader = [&]() -> std::unique_ptr<LVReader> {
44 if (isa<ObjectFile *>(Input)) {
45 ObjectFile &Obj = *cast<ObjectFile *>(Input);
46 if (Obj.isCOFF()) {
47 COFFObjectFile *COFF = cast<COFFObjectFile>(&Obj);
48 return std::make_unique<LVCodeViewReader>(Filename, FileFormatName,
49 *COFF, W, ExePath);
50 }
51 if (Obj.isELF() || Obj.isMachO() || Obj.isWasm())
52 return std::make_unique<LVDWARFReader>(Filename, FileFormatName, Obj,
53 W);
54 }
55 if (isa<PDBFile *>(Input)) {
56 PDBFile &Pdb = *cast<PDBFile *>(Input);
57 return std::make_unique<LVCodeViewReader>(Filename, FileFormatName, Pdb,
58 W, ExePath);
59 }
60 return nullptr;
61 };
62
63 std::unique_ptr<LVReader> ReaderObj = CreateOneReader();
64 if (!ReaderObj)
66 "unable to create reader for: '%s'",
67 Filename.str().c_str());
68
69 LVReader *Reader = ReaderObj.get();
70 Readers.emplace_back(std::move(ReaderObj));
71 return Reader->doLoad();
72}
73
74Error LVReaderHandler::handleArchive(LVReaders &Readers, StringRef Filename,
75 Archive &Arch) {
76 Error Err = Error::success();
77 for (const Archive::Child &Child : Arch.children(Err)) {
78 Expected<MemoryBufferRef> BuffOrErr = Child.getMemoryBufferRef();
79 if (Error Err = BuffOrErr.takeError())
80 return createStringError(errorToErrorCode(std::move(Err)), "%s",
81 Filename.str().c_str());
82 Expected<StringRef> NameOrErr = Child.getName();
83 if (Error Err = NameOrErr.takeError())
84 return createStringError(errorToErrorCode(std::move(Err)), "%s",
85 Filename.str().c_str());
86 std::string Name = (Filename + "(" + NameOrErr.get() + ")").str();
87 if (Error Err = handleBuffer(Readers, Name, BuffOrErr.get()))
88 return createStringError(errorToErrorCode(std::move(Err)), "%s",
89 Filename.str().c_str());
90 }
91
92 return Error::success();
93}
94
95// Search for a matching executable image for the given PDB path.
96static std::string searchForExe(const StringRef Path,
97 const StringRef Extension) {
98 SmallString<128> ExePath(Path);
100
101 std::unique_ptr<IPDBSession> Session;
102 if (Error Err = loadDataForEXE(PDB_ReaderType::Native, ExePath, Session)) {
103 consumeError(std::move(Err));
104 return {};
105 }
106 // We have a candidate for the executable image.
107 Expected<std::string> PdbPathOrErr = NativeSession::searchForPdb({ExePath});
108 if (!PdbPathOrErr) {
109 consumeError(PdbPathOrErr.takeError());
110 return {};
111 }
112 // Convert any Windows backslashes into forward slashes to get the path.
113 std::string ConvertedPath = sys::path::convert_to_slash(
114 PdbPathOrErr.get(), sys::path::Style::windows);
115 if (ConvertedPath == Path)
116 return std::string(ExePath);
117
118 return {};
119}
120
121// Search for a matching object image for the given PDB path.
122static std::string searchForObj(const StringRef Path,
123 const StringRef Extension) {
124 SmallString<128> ObjPath(Path);
126 if (llvm::sys::fs::exists(ObjPath)) {
129 if (!BuffOrErr)
130 return {};
131 return std::string(ObjPath);
132 }
133
134 return {};
135}
136
137Error LVReaderHandler::handleBuffer(LVReaders &Readers, StringRef Filename,
138 MemoryBufferRef Buffer, StringRef ExePath) {
139 // As PDB does not support the Binary interface, at this point we can check
140 // if the buffer corresponds to a PDB or PE file.
141 file_magic FileMagic = identify_magic(Buffer.getBuffer());
142 if (FileMagic == file_magic::pdb) {
143 if (!ExePath.empty())
144 return handleObject(Readers, Filename, Buffer.getBuffer(), ExePath);
145
146 // Search in the directory derived from the given 'Filename' for a
147 // matching object file (.o, .obj, .lib) or a matching executable file
148 // (.exe/.dll) and try to create the reader based on the matched file.
149 // If no matching file is found then we load the original PDB file.
150 std::vector<StringRef> ExecutableExtensions = {"exe", "dll"};
151 for (StringRef Extension : ExecutableExtensions) {
152 std::string ExecutableImage = searchForExe(Filename, Extension);
153 if (ExecutableImage.empty())
154 continue;
155 if (Error Err = handleObject(Readers, Filename, Buffer.getBuffer(),
156 ExecutableImage)) {
157 consumeError(std::move(Err));
158 continue;
159 }
160 return Error::success();
161 }
162
163 std::vector<StringRef> ObjectExtensions = {"o", "obj", "lib"};
164 for (StringRef Extension : ObjectExtensions) {
165 std::string ObjectImage = searchForObj(Filename, Extension);
166 if (ObjectImage.empty())
167 continue;
168 if (Error Err = handleFile(Readers, ObjectImage)) {
169 consumeError(std::move(Err));
170 continue;
171 }
172 return Error::success();
173 }
174
175 // No matching executable/object image was found. Load the given PDB.
176 return handleObject(Readers, Filename, Buffer.getBuffer(), ExePath);
177 }
178 if (FileMagic == file_magic::pecoff_executable) {
179 // If we have a valid executable, try to find a matching PDB file.
181 if (errorToErrorCode(PdbPath.takeError())) {
182 return createStringError(
184 "Binary object format in '%s' does not have debug info.",
185 Filename.str().c_str());
186 }
187 // Process the matching PDB file and pass the executable filename.
188 return handleFile(Readers, PdbPath.get(), Filename);
189 }
190
192 if (errorToErrorCode(BinOrErr.takeError())) {
194 "Binary object format in '%s' is not supported.",
195 Filename.str().c_str());
196 }
197 return handleObject(Readers, Filename, *BinOrErr.get());
198}
199
200Error LVReaderHandler::handleFile(LVReaders &Readers, StringRef Filename,
201 StringRef ExePath) {
202 // Convert any Windows backslashes into forward slashes to get the path.
203 std::string ConvertedPath =
206 MemoryBuffer::getFileOrSTDIN(ConvertedPath);
207 if (BuffOrErr.getError()) {
209 "File '%s' does not exist.",
210 ConvertedPath.c_str());
211 }
212 std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get());
213 return handleBuffer(Readers, ConvertedPath, *Buffer, ExePath);
214}
215
216Error LVReaderHandler::handleMach(LVReaders &Readers, StringRef Filename,
217 MachOUniversalBinary &Mach) {
218 for (const MachOUniversalBinary::ObjectForArch &ObjForArch : Mach.objects()) {
219 std::string ObjName = (Twine(Filename) + Twine("(") +
220 Twine(ObjForArch.getArchFlagName()) + Twine(")"))
221 .str();
222 if (Expected<std::unique_ptr<MachOObjectFile>> MachOOrErr =
223 ObjForArch.getAsObjectFile()) {
224 MachOObjectFile &Obj = **MachOOrErr;
225 PdbOrObj Input = &Obj;
226 if (Error Err =
227 createReader(Filename, Readers, Input, Obj.getFileFormatName()))
228 return Err;
229 continue;
230 } else
231 consumeError(MachOOrErr.takeError());
232 if (Expected<std::unique_ptr<Archive>> ArchiveOrErr =
233 ObjForArch.getAsArchive()) {
234 if (Error Err = handleArchive(Readers, ObjName, *ArchiveOrErr.get()))
235 return Err;
236 continue;
237 } else
238 consumeError(ArchiveOrErr.takeError());
239 }
240 return Error::success();
241}
242
243Error LVReaderHandler::handleObject(LVReaders &Readers, StringRef Filename,
244 Binary &Binary) {
245 if (PdbOrObj Input = dyn_cast<ObjectFile>(&Binary))
246 return createReader(Filename, Readers, Input,
247 cast<ObjectFile *>(Input)->getFileFormatName());
248
249 if (MachOUniversalBinary *Fat = dyn_cast<MachOUniversalBinary>(&Binary))
250 return handleMach(Readers, Filename, *Fat);
251
252 if (Archive *Arch = dyn_cast<Archive>(&Binary))
253 return handleArchive(Readers, Filename, *Arch);
254
256 "Binary object format in '%s' is not supported.",
257 Filename.str().c_str());
258}
259
260Error LVReaderHandler::handleObject(LVReaders &Readers, StringRef Filename,
261 StringRef Buffer, StringRef ExePath) {
262 std::unique_ptr<IPDBSession> Session;
263 if (Error Err = loadDataForPDB(PDB_ReaderType::Native, Filename, Session))
264 return createStringError(errorToErrorCode(std::move(Err)), "%s",
265 Filename.str().c_str());
266
267 std::unique_ptr<NativeSession> PdbSession;
268 PdbSession.reset(static_cast<NativeSession *>(Session.release()));
269 PdbOrObj Input = &PdbSession->getPDBFile();
270 StringRef FileFormatName;
271 size_t Pos = Buffer.find_first_of("\r\n");
272 if (Pos)
273 FileFormatName = Buffer.substr(0, Pos - 1);
274 return createReader(Filename, Readers, Input, FileFormatName, ExePath);
275}
276
277Error LVReaderHandler::createReaders() {
278 LLVM_DEBUG(dbgs() << "createReaders\n");
279 for (std::string &Object : Objects) {
280 LVReaders Readers;
281 if (Error Err = createReader(Object, Readers))
282 return Err;
283 TheReaders.insert(TheReaders.end(),
284 std::make_move_iterator(Readers.begin()),
285 std::make_move_iterator(Readers.end()));
286 }
287
288 return Error::success();
289}
290
291Error LVReaderHandler::printReaders() {
292 LLVM_DEBUG(dbgs() << "printReaders\n");
293 if (options().getPrintExecute())
294 for (const std::unique_ptr<LVReader> &Reader : TheReaders)
295 if (Error Err = Reader->doPrint())
296 return Err;
297
298 return Error::success();
299}
300
301Error LVReaderHandler::compareReaders() {
302 LLVM_DEBUG(dbgs() << "compareReaders\n");
303 size_t ReadersCount = TheReaders.size();
304 if (options().getCompareExecute() && ReadersCount >= 2) {
305 // If we have more than 2 readers, compare them by pairs.
306 size_t ViewPairs = ReadersCount / 2;
307 LVCompare Compare(OS);
308 for (size_t Pair = 0, Index = 0; Pair < ViewPairs; ++Pair) {
309 if (Error Err = Compare.execute(TheReaders[Index].get(),
310 TheReaders[Index + 1].get()))
311 return Err;
312 Index += 2;
313 }
314 }
315
316 return Error::success();
317}
318
319void LVReaderHandler::print(raw_ostream &OS) const { OS << "ReaderHandler\n"; }
#define LLVM_DEBUG(X)
Definition: Debug.h:101
static std::string searchForObj(const StringRef Path, const StringRef Extension)
static std::string searchForExe(const StringRef Path, const StringRef Extension)
raw_pwrite_stream & OS
Represents either an error or a value T.
Definition: ErrorOr.h:56
reference get()
Definition: ErrorOr.h:149
std::error_code getError() const
Definition: ErrorOr.h:152
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
StringRef getBuffer() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:557
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:363
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
void print(raw_ostream &OS) const
The logical reader owns of all the logical elements created during the debug information parsing.
Definition: LVReader.h:60
iterator_range< child_iterator > children(Error &Err, bool SkipInternal=true) const
Definition: Archive.h:346
bool isWasm() const
Definition: Binary.h:137
bool isMachO() const
Definition: Binary.h:127
bool isCOFF() const
Definition: Binary.h:131
bool isELF() const
Definition: Binary.h:123
StringRef getFileFormatName() const override
iterator_range< object_iterator > objects() const
This class is the base class for all object file types.
Definition: ObjectFile.h:229
static Expected< std::string > searchForPdb(const PdbSearchOptions &Opts)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
LVOptions & options()
Definition: LVOptions.h:445
std::vector< std::unique_ptr< LVReader > > LVReaders
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
Error loadDataForEXE(PDB_ReaderType Type, StringRef Path, std::unique_ptr< IPDBSession > &Session)
Definition: PDB.cpp:35
Error loadDataForPDB(PDB_ReaderType Type, StringRef Path, std::unique_ptr< IPDBSession > &Session)
Definition: PDB.cpp:22
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1078
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
Definition: Path.cpp:481
std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
Definition: Path.cpp:569
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:33
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1258
@ bad_file_descriptor
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:109
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20
@ pdb
Windows PDB debug info file.
Definition: Magic.h:54
@ pecoff_executable
PECOFF executable file.
Definition: Magic.h:49