LLVM 23.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/IR/LLVMContext.h"
21#include "llvm/Object/COFF.h"
22
23using namespace llvm;
24using namespace llvm::object;
25using namespace llvm::pdb;
26using namespace llvm::logicalview;
27
28#define DEBUG_TYPE "ReaderHandler"
29
30static constexpr StringRef IRFileFormatName = "LLVM IR";
31
33 if (Error Err = createReaders())
34 return Err;
35 if (Error Err = printReaders())
36 return Err;
37 if (Error Err = compareReaders())
38 return Err;
39
40 return Error::success();
41}
42
43Error LVReaderHandler::createReader(StringRef Filename, LVReaders &Readers,
45 StringRef FileFormatName,
46 StringRef ExePath) {
47 auto CreateOneReader = [&]() -> std::unique_ptr<LVReader> {
48 if (auto ObjPtrPtr = std::get_if<ObjectFile *>(&Input)) {
49 ObjectFile &Obj = **ObjPtrPtr;
50 if (Obj.isCOFF()) {
52 return std::make_unique<LVCodeViewReader>(Filename, FileFormatName,
53 *COFF, W, ExePath);
54 }
55 if (Obj.isELF() || Obj.isMachO() || Obj.isWasm())
56 return std::make_unique<LVDWARFReader>(Filename, FileFormatName, Obj,
57 W);
58 }
59 if (auto PdbPtrPtr = std::get_if<PDBFile *>(&Input)) {
60 PDBFile &Pdb = **PdbPtrPtr;
61 return std::make_unique<LVCodeViewReader>(Filename, FileFormatName, Pdb,
62 W, ExePath);
63 }
64 if (auto Ir = std::get_if<IRObjectFile *>(&Input))
65 return std::make_unique<LVIRReader>(Filename, FileFormatName, *Ir, W);
66 if (auto MemBuf = std::get_if<MemoryBufferRef *>(&Input)) {
67 // If the filename extension is '.ll' create an IR reader.
68 constexpr StringRef IRFileExt = ".ll";
69 if (llvm::sys::path::extension(Filename) == IRFileExt)
70 return std::make_unique<LVIRReader>(Filename, IRFileFormatName, *MemBuf,
71 W);
72 }
73 return nullptr;
74 };
75
76 std::unique_ptr<LVReader> ReaderObj = CreateOneReader();
77 if (!ReaderObj)
79 "unable to create reader for: '%s'",
80 Filename.str().c_str());
81
82 LVReader *Reader = ReaderObj.get();
83 Readers.emplace_back(std::move(ReaderObj));
84 return Reader->doLoad();
85}
86
87Error LVReaderHandler::handleArchive(LVReaders &Readers, StringRef Filename,
88 Archive &Arch) {
89 Error Err = Error::success();
90 for (const Archive::Child &Child : Arch.children(Err)) {
91 Expected<MemoryBufferRef> BuffOrErr = Child.getMemoryBufferRef();
92 if (Error Err = BuffOrErr.takeError())
93 return createStringError(errorToErrorCode(std::move(Err)), "%s",
94 Filename.str().c_str());
95 Expected<StringRef> NameOrErr = Child.getName();
96 if (Error Err = NameOrErr.takeError())
97 return createStringError(errorToErrorCode(std::move(Err)), "%s",
98 Filename.str().c_str());
99 std::string Name = (Filename + "(" + NameOrErr.get() + ")").str();
100 if (Error Err = handleBuffer(Readers, Name, BuffOrErr.get()))
101 return createStringError(errorToErrorCode(std::move(Err)), "%s",
102 Filename.str().c_str());
103 }
104
105 if (Err)
106 return createStringError(errorToErrorCode(std::move(Err)), "%s",
107 Filename.str().c_str());
108 return Error::success();
109}
110
111// Search for a matching executable image for the given PDB path.
112static std::string searchForExe(const StringRef Path,
113 const StringRef Extension) {
114 SmallString<128> ExePath(Path);
115 llvm::sys::path::replace_extension(ExePath, Extension);
116
117 std::unique_ptr<IPDBSession> Session;
118 if (Error Err = loadDataForEXE(PDB_ReaderType::Native, ExePath, Session)) {
119 consumeError(std::move(Err));
120 return {};
121 }
122 // We have a candidate for the executable image.
123 Expected<std::string> PdbPathOrErr = NativeSession::searchForPdb({ExePath});
124 if (!PdbPathOrErr) {
125 consumeError(PdbPathOrErr.takeError());
126 return {};
127 }
128 // Convert any Windows backslashes into forward slashes to get the path.
129 std::string ConvertedPath = sys::path::convert_to_slash(
130 PdbPathOrErr.get(), sys::path::Style::windows);
131 if (ConvertedPath == Path)
132 return std::string(ExePath);
133
134 return {};
135}
136
137// Search for a matching object image for the given PDB path.
138static std::string searchForObj(const StringRef Path,
139 const StringRef Extension) {
140 SmallString<128> ObjPath(Path);
141 llvm::sys::path::replace_extension(ObjPath, Extension);
142 if (llvm::sys::fs::exists(ObjPath)) {
145 if (!BuffOrErr)
146 return {};
147 return std::string(ObjPath);
148 }
149
150 return {};
151}
152
153Error LVReaderHandler::handleBuffer(LVReaders &Readers, StringRef Filename,
154 MemoryBufferRef Buffer, StringRef ExePath) {
155 // As PDB does not support the Binary interface, at this point we can check
156 // if the buffer corresponds to a PDB or PE file.
157 file_magic FileMagic = identify_magic(Buffer.getBuffer());
158 if (FileMagic == file_magic::pdb) {
159 if (!ExePath.empty())
160 return handleObject(Readers, Filename, Buffer.getBuffer(), ExePath);
161
162 // Search in the directory derived from the given 'Filename' for a
163 // matching object file (.o, .obj, .lib) or a matching executable file
164 // (.exe/.dll) and try to create the reader based on the matched file.
165 // If no matching file is found then we load the original PDB file.
166 std::vector<StringRef> ExecutableExtensions = {"exe", "dll"};
167 for (StringRef Extension : ExecutableExtensions) {
168 std::string ExecutableImage = searchForExe(Filename, Extension);
169 if (ExecutableImage.empty())
170 continue;
171 if (Error Err = handleObject(Readers, Filename, Buffer.getBuffer(),
172 ExecutableImage)) {
173 consumeError(std::move(Err));
174 continue;
175 }
176 return Error::success();
177 }
178
179 std::vector<StringRef> ObjectExtensions = {"o", "obj", "lib"};
180 for (StringRef Extension : ObjectExtensions) {
181 std::string ObjectImage = searchForObj(Filename, Extension);
182 if (ObjectImage.empty())
183 continue;
184 if (Error Err = handleFile(Readers, ObjectImage)) {
185 consumeError(std::move(Err));
186 continue;
187 }
188 return Error::success();
189 }
190
191 // No matching executable/object image was found. Load the given PDB.
192 return handleObject(Readers, Filename, Buffer.getBuffer(), ExePath);
193 }
194 if (FileMagic == file_magic::pecoff_executable) {
195 // If we have a valid executable, try to find a matching PDB file.
196 Expected<std::string> PdbPath = NativeSession::searchForPdb({Filename});
197 if (errorToErrorCode(PdbPath.takeError())) {
198 return createStringError(
200 "Binary object format in '%s' does not have debug info.",
201 Filename.str().c_str());
202 }
203 // Process the matching PDB file and pass the executable filename.
204 return handleFile(Readers, PdbPath.get(), Filename);
205 }
206
207 LLVMContext Context;
208 Expected<std::unique_ptr<Binary>> BinOrErr = createBinary(Buffer, &Context);
209 if (errorToErrorCode(BinOrErr.takeError())) {
210 // Assume it is LLVM IR.
211 return handleObject(Readers, Filename, Buffer);
212 }
213 return handleObject(Readers, Filename, *BinOrErr.get());
214}
215
216Error LVReaderHandler::handleFile(LVReaders &Readers, StringRef Filename,
217 StringRef ExePath) {
218 // Convert any Windows backslashes into forward slashes to get the path.
219 std::string ConvertedPath =
221 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
222 MemoryBuffer::getFileOrSTDIN(ConvertedPath);
223 if (BuffOrErr.getError()) {
225 "File '%s' does not exist.",
226 ConvertedPath.c_str());
227 }
228 std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get());
229 return handleBuffer(Readers, ConvertedPath, *Buffer, ExePath);
230}
231
232Error LVReaderHandler::handleMach(LVReaders &Readers, StringRef Filename,
233 MachOUniversalBinary &Mach) {
234 for (const MachOUniversalBinary::ObjectForArch &ObjForArch : Mach.objects()) {
235 std::string ObjName = (Twine(Filename) + Twine("(") +
236 Twine(ObjForArch.getArchFlagName()) + Twine(")"))
237 .str();
238 if (Expected<std::unique_ptr<MachOObjectFile>> MachOOrErr =
239 ObjForArch.getAsObjectFile()) {
240 MachOObjectFile &Obj = **MachOOrErr;
241 InputHandle Input = &Obj;
242 if (Error Err =
243 createReader(Filename, Readers, Input, Obj.getFileFormatName()))
244 return Err;
245 continue;
246 } else
247 consumeError(MachOOrErr.takeError());
248 if (Expected<std::unique_ptr<Archive>> ArchiveOrErr =
249 ObjForArch.getAsArchive()) {
250 if (Error Err = handleArchive(Readers, ObjName, *ArchiveOrErr.get()))
251 return Err;
252 continue;
253 } else
254 consumeError(ArchiveOrErr.takeError());
255 }
256 return Error::success();
257}
258
259Error LVReaderHandler::handleObject(LVReaders &Readers, StringRef Filename,
260 Binary &Binary) {
261 if (ObjectFile *ObjFile = dyn_cast<ObjectFile>(&Binary)) {
262 InputHandle Input = ObjFile;
263 return createReader(Filename, Readers, Input, ObjFile->getFileFormatName());
264 }
265
266 if (MachOUniversalBinary *Fat = dyn_cast<MachOUniversalBinary>(&Binary))
267 return handleMach(Readers, Filename, *Fat);
268
269 if (Archive *Arch = dyn_cast<Archive>(&Binary))
270 return handleArchive(Readers, Filename, *Arch);
271
272 if (IRObjectFile *IRObjFile = dyn_cast<IRObjectFile>(&Binary)) {
273 InputHandle Input = IRObjFile;
274 return createReader(Filename, Readers, Input, "Bitcode IR");
275 }
276
278 "Binary object format in '%s' is not supported.",
279 Filename.str().c_str());
280}
281
282Error LVReaderHandler::handleObject(LVReaders &Readers, StringRef Filename,
283 StringRef Buffer, StringRef ExePath) {
284 std::unique_ptr<IPDBSession> Session;
285 if (Error Err = loadDataForPDB(PDB_ReaderType::Native, Filename, Session))
286 return createStringError(errorToErrorCode(std::move(Err)), "%s",
287 Filename.str().c_str());
288
289 std::unique_ptr<NativeSession> PdbSession;
290 PdbSession.reset(static_cast<NativeSession *>(Session.release()));
291 InputHandle Input = &PdbSession->getPDBFile();
292 StringRef FileFormatName;
293 size_t Pos = Buffer.find_first_of("\r\n");
294 if (Pos)
295 FileFormatName = Buffer.substr(0, Pos - 1);
296 return createReader(Filename, Readers, Input, FileFormatName, ExePath);
297}
298
299Error LVReaderHandler::handleObject(LVReaders &Readers, StringRef Filename,
300 MemoryBufferRef Buffer) {
301 InputHandle Input = cast<MemoryBufferRef>(&Buffer);
302 return createReader(Filename, Readers, Input, IRFileFormatName);
303}
304
305Error LVReaderHandler::createReaders() {
306 LLVM_DEBUG(dbgs() << "createReaders\n");
307 for (std::string &Object : Objects) {
308 LVReaders Readers;
309 if (Error Err = createReader(Object, Readers))
310 return Err;
311 TheReaders.insert(TheReaders.end(),
312 std::make_move_iterator(Readers.begin()),
313 std::make_move_iterator(Readers.end()));
314 }
315
316 return Error::success();
317}
318
319Error LVReaderHandler::printReaders() {
320 LLVM_DEBUG(dbgs() << "printReaders\n");
321 if (options().getPrintExecute())
322 for (const std::unique_ptr<LVReader> &Reader : TheReaders)
323 if (Error Err = Reader->doPrint())
324 return Err;
325
326 return Error::success();
327}
328
329Error LVReaderHandler::compareReaders() {
330 LLVM_DEBUG(dbgs() << "compareReaders\n");
331 size_t ReadersCount = TheReaders.size();
332 if (options().getCompareExecute() && ReadersCount >= 2) {
333 // If we have more than 2 readers, compare them by pairs.
334 size_t ViewPairs = ReadersCount / 2;
335 LVCompare Compare(OS);
336 for (size_t Pair = 0, Index = 0; Pair < ViewPairs; ++Pair) {
337 if (Error Err = Compare.execute(TheReaders[Index].get(),
338 TheReaders[Index + 1].get()))
339 return Err;
340 Index += 2;
341 }
342 }
343
344 return Error::success();
345}
346
347void LVReaderHandler::print(raw_ostream &OS) const { OS << "ReaderHandler\n"; }
static constexpr StringRef IRFileFormatName
static std::string searchForObj(const StringRef Path, const StringRef Extension)
static std::string searchForExe(const StringRef Path, const StringRef Extension)
static constexpr StringLiteral Filename
#define LLVM_DEBUG(...)
Definition Debug.h:119
The Input class is used to parse a yaml document into in-memory structs and vectors.
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:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
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 "-".
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
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:396
LLVM_ABI void print(raw_ostream &OS) const
iterator_range< child_iterator > children(Error &Err, bool SkipInternal=true) const
Definition Archive.h:404
iterator_range< object_iterator > objects() const
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:53
std::vector< std::unique_ptr< LVReader > > LVReaders
LVOptions & options()
Definition LVOptions.h:448
std::variant< StringRef *, MemoryBufferRef *, object::ObjectFile *, object::IRObjectFile *, pdb::PDBFile * > InputHandle
LLVM_ABI 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
LLVM_ABI Error loadDataForEXE(PDB_ReaderType Type, StringRef Path, std::unique_ptr< IPDBSession > &Session)
Definition PDB.cpp:35
LLVM_ABI Error loadDataForPDB(PDB_ReaderType Type, StringRef Path, std::unique_ptr< IPDBSession > &Session)
Definition PDB.cpp:22
LLVM_ABI bool exists(const basic_file_status &status)
Does file exist?
Definition Path.cpp:1107
LLVM_ABI void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
Definition Path.cpp:491
LLVM_ABI std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
Definition Path.cpp:585
LLVM_ABI StringRef extension(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get extension.
Definition Path.cpp:607
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition Magic.cpp:33
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
@ bad_file_descriptor
Definition Errc.h:39
@ not_supported
Definition Errc.h:69
@ invalid_argument
Definition Errc.h:56
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition Error.cpp:113
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
@ pdb
Windows PDB debug info file.
Definition Magic.h:55
@ pecoff_executable
PECOFF executable file.
Definition Magic.h:50