blob: f61d89bd70c395147a48bb85e7a5b8dde812cc7e [file] [log] [blame]
Zachary Turnerabb17cc2017-09-01 20:06:56 +00001//===- InputFile.cpp ------------------------------------------ *- C++ --*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Zachary Turnerabb17cc2017-09-01 20:06:56 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "InputFile.h"
10
11#include "FormatUtil.h"
12#include "LinePrinter.h"
13
14#include "llvm/BinaryFormat/Magic.h"
15#include "llvm/DebugInfo/CodeView/CodeView.h"
16#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
17#include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
18#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
19#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
20#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
21#include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
22#include "llvm/DebugInfo/PDB/Native/RawError.h"
23#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
24#include "llvm/DebugInfo/PDB/PDB.h"
25#include "llvm/Object/COFF.h"
26#include "llvm/Support/FileSystem.h"
27#include "llvm/Support/FormatVariadic.h"
28
29using namespace llvm;
30using namespace llvm::codeview;
31using namespace llvm::object;
32using namespace llvm::pdb;
33
34InputFile::InputFile() {}
35InputFile::~InputFile() {}
36
37static Expected<ModuleDebugStreamRef>
38getModuleDebugStream(PDBFile &File, StringRef &ModuleName, uint32_t Index) {
39 ExitOnError Err("Unexpected error: ");
40
41 auto &Dbi = Err(File.getPDBDbiStream());
42 const auto &Modules = Dbi.modules();
Alexandre Ganea741cc352018-08-06 19:35:00 +000043 if (Index >= Modules.getModuleCount())
44 return make_error<RawError>(raw_error_code::index_out_of_bounds,
45 "Invalid module index");
46
Zachary Turnerabb17cc2017-09-01 20:06:56 +000047 auto Modi = Modules.getModuleDescriptor(Index);
48
49 ModuleName = Modi.getModuleName();
50
51 uint16_t ModiStream = Modi.getModuleStreamIndex();
52 if (ModiStream == kInvalidStreamIndex)
53 return make_error<RawError>(raw_error_code::no_stream,
54 "Module stream not present");
55
56 auto ModStreamData = File.createIndexedStream(ModiStream);
57
58 ModuleDebugStreamRef ModS(Modi, std::move(ModStreamData));
59 if (auto EC = ModS.reload())
60 return make_error<RawError>(raw_error_code::corrupt_file,
61 "Invalid module stream");
62
63 return std::move(ModS);
64}
65
66static inline bool isCodeViewDebugSubsection(object::SectionRef Section,
67 StringRef Name,
68 BinaryStreamReader &Reader) {
Hans Wennborg4da9ff92019-05-16 12:08:34 +000069 StringRef SectionName, Contents;
Zachary Turnere31b9dc2017-09-02 00:09:43 +000070 if (Section.getName(SectionName))
Zachary Turnerabb17cc2017-09-01 20:06:56 +000071 return false;
72
73 if (SectionName != Name)
74 return false;
75
Hans Wennborg4da9ff92019-05-16 12:08:34 +000076 if (Section.getContents(Contents))
Zachary Turnerabb17cc2017-09-01 20:06:56 +000077 return false;
78
Hans Wennborg4da9ff92019-05-16 12:08:34 +000079 Reader = BinaryStreamReader(Contents, support::little);
Zachary Turnerabb17cc2017-09-01 20:06:56 +000080 uint32_t Magic;
81 if (Reader.bytesRemaining() < sizeof(uint32_t))
82 return false;
83 cantFail(Reader.readInteger(Magic));
84 if (Magic != COFF::DEBUG_SECTION_MAGIC)
85 return false;
86 return true;
87}
88
89static inline bool isDebugSSection(object::SectionRef Section,
90 DebugSubsectionArray &Subsections) {
91 BinaryStreamReader Reader;
92 if (!isCodeViewDebugSubsection(Section, ".debug$S", Reader))
93 return false;
94
95 cantFail(Reader.readArray(Subsections, Reader.bytesRemaining()));
96 return true;
97}
98
99static bool isDebugTSection(SectionRef Section, CVTypeArray &Types) {
100 BinaryStreamReader Reader;
Alexandre Ganead9e96742018-04-09 20:17:56 +0000101 if (!isCodeViewDebugSubsection(Section, ".debug$T", Reader) &&
102 !isCodeViewDebugSubsection(Section, ".debug$P", Reader))
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000103 return false;
104 cantFail(Reader.readArray(Types, Reader.bytesRemaining()));
105 return true;
106}
107
108static std::string formatChecksumKind(FileChecksumKind Kind) {
109 switch (Kind) {
110 RETURN_CASE(FileChecksumKind, None, "None");
111 RETURN_CASE(FileChecksumKind, MD5, "MD5");
112 RETURN_CASE(FileChecksumKind, SHA1, "SHA-1");
113 RETURN_CASE(FileChecksumKind, SHA256, "SHA-256");
114 }
115 return formatUnknownEnum(Kind);
116}
117
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000118template <typename... Args>
119static void formatInternal(LinePrinter &Printer, bool Append, Args &&... args) {
120 if (Append)
121 Printer.format(std::forward<Args>(args)...);
122 else
123 Printer.formatLine(std::forward<Args>(args)...);
124}
125
126SymbolGroup::SymbolGroup(InputFile *File, uint32_t GroupIndex) : File(File) {
127 if (!File)
128 return;
129
130 if (File->isPdb())
131 initializeForPdb(GroupIndex);
132 else {
133 Name = ".debug$S";
134 uint32_t I = 0;
135 for (const auto &S : File->obj().sections()) {
136 DebugSubsectionArray SS;
137 if (!isDebugSSection(S, SS))
138 continue;
139
140 if (!SC.hasChecksums() || !SC.hasStrings())
141 SC.initialize(SS);
142
143 if (I == GroupIndex)
144 Subsections = SS;
145
146 if (SC.hasChecksums() && SC.hasStrings())
147 break;
148 }
149 rebuildChecksumMap();
150 }
151}
152
153StringRef SymbolGroup::name() const { return Name; }
154
155void SymbolGroup::updateDebugS(const codeview::DebugSubsectionArray &SS) {
156 Subsections = SS;
157}
158
159void SymbolGroup::updatePdbModi(uint32_t Modi) { initializeForPdb(Modi); }
160
161void SymbolGroup::initializeForPdb(uint32_t Modi) {
162 assert(File && File->isPdb());
163
164 // PDB always uses the same string table, but each module has its own
165 // checksums. So we only set the strings if they're not already set.
Leonard Mosescu4bdbea32018-11-02 18:00:37 +0000166 if (!SC.hasStrings()) {
167 auto StringTable = File->pdb().getStringTable();
168 if (StringTable)
169 SC.setStrings(StringTable->getStringTable());
170 else
171 consumeError(StringTable.takeError());
172 }
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000173
174 SC.resetChecksums();
175 auto MDS = getModuleDebugStream(File->pdb(), Name, Modi);
176 if (!MDS) {
177 consumeError(MDS.takeError());
178 return;
179 }
180
181 DebugStream = std::make_shared<ModuleDebugStreamRef>(std::move(*MDS));
182 Subsections = DebugStream->getSubsectionsArray();
183 SC.initialize(Subsections);
184 rebuildChecksumMap();
185}
186
187void SymbolGroup::rebuildChecksumMap() {
188 if (!SC.hasChecksums())
189 return;
190
191 for (const auto &Entry : SC.checksums()) {
192 auto S = SC.strings().getString(Entry.FileNameOffset);
193 if (!S)
194 continue;
195 ChecksumsByFile[*S] = Entry;
196 }
197}
198
199const ModuleDebugStreamRef &SymbolGroup::getPdbModuleStream() const {
200 assert(File && File->isPdb() && DebugStream);
201 return *DebugStream;
202}
203
204Expected<StringRef> SymbolGroup::getNameFromStringTable(uint32_t Offset) const {
205 return SC.strings().getString(Offset);
206}
207
208void SymbolGroup::formatFromFileName(LinePrinter &Printer, StringRef File,
209 bool Append) const {
210 auto FC = ChecksumsByFile.find(File);
211 if (FC == ChecksumsByFile.end()) {
212 formatInternal(Printer, Append, "- (no checksum) {0}", File);
213 return;
214 }
215
216 formatInternal(Printer, Append, "- ({0}: {1}) {2}",
217 formatChecksumKind(FC->getValue().Kind),
218 toHex(FC->getValue().Checksum), File);
219}
220
221void SymbolGroup::formatFromChecksumsOffset(LinePrinter &Printer,
222 uint32_t Offset,
223 bool Append) const {
224 if (!SC.hasChecksums()) {
225 formatInternal(Printer, Append, "(unknown file name offset {0})", Offset);
226 return;
227 }
228
229 auto Iter = SC.checksums().getArray().at(Offset);
230 if (Iter == SC.checksums().getArray().end()) {
231 formatInternal(Printer, Append, "(unknown file name offset {0})", Offset);
232 return;
233 }
234
235 uint32_t FO = Iter->FileNameOffset;
236 auto ExpectedFile = getNameFromStringTable(FO);
237 if (!ExpectedFile) {
238 formatInternal(Printer, Append, "(unknown file name offset {0})", Offset);
239 consumeError(ExpectedFile.takeError());
240 return;
241 }
242 if (Iter->Kind == FileChecksumKind::None) {
243 formatInternal(Printer, Append, "{0} (no checksum)", *ExpectedFile);
244 } else {
245 formatInternal(Printer, Append, "{0} ({1}: {2})", *ExpectedFile,
246 formatChecksumKind(Iter->Kind), toHex(Iter->Checksum));
247 }
248}
249
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000250Expected<InputFile> InputFile::open(StringRef Path, bool AllowUnknownFile) {
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000251 InputFile IF;
252 if (!llvm::sys::fs::exists(Path))
253 return make_error<StringError>(formatv("File {0} not found", Path),
254 inconvertibleErrorCode());
255
256 file_magic Magic;
257 if (auto EC = identify_magic(Path, Magic))
258 return make_error<StringError>(
259 formatv("Unable to identify file type for file {0}", Path), EC);
260
261 if (Magic == file_magic::coff_object) {
262 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Path);
263 if (!BinaryOrErr)
264 return BinaryOrErr.takeError();
265
266 IF.CoffObject = std::move(*BinaryOrErr);
267 IF.PdbOrObj = llvm::cast<COFFObjectFile>(IF.CoffObject.getBinary());
268 return std::move(IF);
269 }
270
Zachary Turner49f86742018-03-07 19:12:36 +0000271 if (Magic == file_magic::pdb) {
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000272 std::unique_ptr<IPDBSession> Session;
273 if (auto Err = loadDataForPDB(PDB_ReaderType::Native, Path, Session))
274 return std::move(Err);
275
276 IF.PdbSession.reset(static_cast<NativeSession *>(Session.release()));
277 IF.PdbOrObj = &IF.PdbSession->getPDBFile();
278
279 return std::move(IF);
280 }
281
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000282 if (!AllowUnknownFile)
283 return make_error<StringError>(
284 formatv("File {0} is not a supported file type", Path),
285 inconvertibleErrorCode());
286
Zachary Turner4703a3f2018-04-04 17:41:05 +0000287 auto Result = MemoryBuffer::getFile(Path, -1LL, false);
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000288 if (!Result)
289 return make_error<StringError>(
290 formatv("File {0} could not be opened", Path), Result.getError());
291
292 IF.UnknownFile = std::move(*Result);
293 IF.PdbOrObj = IF.UnknownFile.get();
294 return std::move(IF);
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000295}
296
297PDBFile &InputFile::pdb() {
298 assert(isPdb());
299 return *PdbOrObj.get<PDBFile *>();
300}
301
302const PDBFile &InputFile::pdb() const {
303 assert(isPdb());
304 return *PdbOrObj.get<PDBFile *>();
305}
306
307object::COFFObjectFile &InputFile::obj() {
308 assert(isObj());
309 return *PdbOrObj.get<object::COFFObjectFile *>();
310}
311
312const object::COFFObjectFile &InputFile::obj() const {
313 assert(isObj());
314 return *PdbOrObj.get<object::COFFObjectFile *>();
315}
316
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000317MemoryBuffer &InputFile::unknown() {
318 assert(isUnknown());
319 return *PdbOrObj.get<MemoryBuffer *>();
320}
321
322const MemoryBuffer &InputFile::unknown() const {
323 assert(isUnknown());
324 return *PdbOrObj.get<MemoryBuffer *>();
325}
326
327StringRef InputFile::getFilePath() const {
328 if (isPdb())
329 return pdb().getFilePath();
330 if (isObj())
331 return obj().getFileName();
332 assert(isUnknown());
333 return unknown().getBufferIdentifier();
334}
335
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000336bool InputFile::hasTypes() const {
337 if (isPdb())
338 return pdb().hasPDBTpiStream();
339
340 for (const auto &Section : obj().sections()) {
341 CVTypeArray Types;
342 if (isDebugTSection(Section, Types))
343 return true;
344 }
345 return false;
346}
347
348bool InputFile::hasIds() const {
349 if (isObj())
350 return false;
351 return pdb().hasPDBIpiStream();
352}
353
354bool InputFile::isPdb() const { return PdbOrObj.is<PDBFile *>(); }
355
356bool InputFile::isObj() const {
357 return PdbOrObj.is<object::COFFObjectFile *>();
358}
359
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000360bool InputFile::isUnknown() const { return PdbOrObj.is<MemoryBuffer *>(); }
361
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000362codeview::LazyRandomTypeCollection &
363InputFile::getOrCreateTypeCollection(TypeCollectionKind Kind) {
364 if (Types && Kind == kTypes)
365 return *Types;
366 if (Ids && Kind == kIds)
367 return *Ids;
368
369 if (Kind == kIds) {
370 assert(isPdb() && pdb().hasPDBIpiStream());
371 }
372
373 // If the collection was already initialized, we should have just returned it
374 // in step 1.
375 if (isPdb()) {
376 TypeCollectionPtr &Collection = (Kind == kIds) ? Ids : Types;
377 auto &Stream = cantFail((Kind == kIds) ? pdb().getPDBIpiStream()
378 : pdb().getPDBTpiStream());
379
380 auto &Array = Stream.typeArray();
381 uint32_t Count = Stream.getNumTypeRecords();
382 auto Offsets = Stream.getTypeIndexOffsets();
383 Collection =
384 llvm::make_unique<LazyRandomTypeCollection>(Array, Count, Offsets);
385 return *Collection;
386 }
387
388 assert(isObj());
389 assert(Kind == kTypes);
390 assert(!Types);
391
392 for (const auto &Section : obj().sections()) {
393 CVTypeArray Records;
394 if (!isDebugTSection(Section, Records))
395 continue;
396
397 Types = llvm::make_unique<LazyRandomTypeCollection>(Records, 100);
398 return *Types;
399 }
400
401 Types = llvm::make_unique<LazyRandomTypeCollection>(100);
402 return *Types;
403}
404
405codeview::LazyRandomTypeCollection &InputFile::types() {
406 return getOrCreateTypeCollection(kTypes);
407}
408
409codeview::LazyRandomTypeCollection &InputFile::ids() {
410 // Object files have only one type stream that contains both types and ids.
411 // Similarly, some PDBs don't contain an IPI stream, and for those both types
412 // and IDs are in the same stream.
413 if (isObj() || !pdb().hasPDBIpiStream())
414 return types();
415
416 return getOrCreateTypeCollection(kIds);
417}
418
419iterator_range<SymbolGroupIterator> InputFile::symbol_groups() {
420 return make_range<SymbolGroupIterator>(symbol_groups_begin(),
421 symbol_groups_end());
422}
423
424SymbolGroupIterator InputFile::symbol_groups_begin() {
425 return SymbolGroupIterator(*this);
426}
427
428SymbolGroupIterator InputFile::symbol_groups_end() {
429 return SymbolGroupIterator();
430}
431
432SymbolGroupIterator::SymbolGroupIterator() : Value(nullptr) {}
433
434SymbolGroupIterator::SymbolGroupIterator(InputFile &File) : Value(&File) {
435 if (File.isObj()) {
436 SectionIter = File.obj().section_begin();
437 scanToNextDebugS();
438 }
439}
440
441bool SymbolGroupIterator::operator==(const SymbolGroupIterator &R) const {
442 bool E = isEnd();
443 bool RE = R.isEnd();
444 if (E || RE)
445 return E == RE;
446
447 if (Value.File != R.Value.File)
448 return false;
449 return Index == R.Index;
450}
451
452const SymbolGroup &SymbolGroupIterator::operator*() const {
453 assert(!isEnd());
454 return Value;
455}
456SymbolGroup &SymbolGroupIterator::operator*() {
457 assert(!isEnd());
458 return Value;
459}
460
461SymbolGroupIterator &SymbolGroupIterator::operator++() {
462 assert(Value.File && !isEnd());
463 ++Index;
464 if (isEnd())
465 return *this;
466
467 if (Value.File->isPdb()) {
468 Value.updatePdbModi(Index);
469 return *this;
470 }
471
472 scanToNextDebugS();
473 return *this;
474}
475
476void SymbolGroupIterator::scanToNextDebugS() {
477 assert(SectionIter.hasValue());
478 auto End = Value.File->obj().section_end();
479 auto &Iter = *SectionIter;
480 assert(!isEnd());
481
482 while (++Iter != End) {
483 DebugSubsectionArray SS;
484 SectionRef SR = *Iter;
485 if (!isDebugSSection(SR, SS))
486 continue;
487
488 Value.updateDebugS(SS);
489 return;
490 }
491}
492
493bool SymbolGroupIterator::isEnd() const {
494 if (!Value.File)
495 return true;
496 if (Value.File->isPdb()) {
497 auto &Dbi = cantFail(Value.File->pdb().getPDBDbiStream());
498 uint32_t Count = Dbi.modules().getModuleCount();
499 assert(Index <= Count);
500 return Index == Count;
501 }
502
503 assert(SectionIter.hasValue());
504 return *SectionIter == Value.File->obj().section_end();
505}