blob: b2019642b2b0ae9b08be06742ffbb3e3bd93533d [file] [log] [blame]
Zachary Turnerabb17cc2017-09-01 20:06:56 +00001//===- InputFile.cpp ------------------------------------------ *- C++ --*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "InputFile.h"
11
12#include "FormatUtil.h"
13#include "LinePrinter.h"
14
15#include "llvm/BinaryFormat/Magic.h"
16#include "llvm/DebugInfo/CodeView/CodeView.h"
17#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
18#include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
19#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
20#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
21#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
22#include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
23#include "llvm/DebugInfo/PDB/Native/RawError.h"
24#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
25#include "llvm/DebugInfo/PDB/PDB.h"
26#include "llvm/Object/COFF.h"
27#include "llvm/Support/FileSystem.h"
28#include "llvm/Support/FormatVariadic.h"
29
30using namespace llvm;
31using namespace llvm::codeview;
32using namespace llvm::object;
33using namespace llvm::pdb;
34
35InputFile::InputFile() {}
36InputFile::~InputFile() {}
37
38static Expected<ModuleDebugStreamRef>
39getModuleDebugStream(PDBFile &File, StringRef &ModuleName, uint32_t Index) {
40 ExitOnError Err("Unexpected error: ");
41
42 auto &Dbi = Err(File.getPDBDbiStream());
43 const auto &Modules = Dbi.modules();
Alexandre Ganea741cc352018-08-06 19:35:00 +000044 if (Index >= Modules.getModuleCount())
45 return make_error<RawError>(raw_error_code::index_out_of_bounds,
46 "Invalid module index");
47
Zachary Turnerabb17cc2017-09-01 20:06:56 +000048 auto Modi = Modules.getModuleDescriptor(Index);
49
50 ModuleName = Modi.getModuleName();
51
52 uint16_t ModiStream = Modi.getModuleStreamIndex();
53 if (ModiStream == kInvalidStreamIndex)
54 return make_error<RawError>(raw_error_code::no_stream,
55 "Module stream not present");
56
57 auto ModStreamData = File.createIndexedStream(ModiStream);
58
59 ModuleDebugStreamRef ModS(Modi, std::move(ModStreamData));
60 if (auto EC = ModS.reload())
61 return make_error<RawError>(raw_error_code::corrupt_file,
62 "Invalid module stream");
63
64 return std::move(ModS);
65}
66
67static inline bool isCodeViewDebugSubsection(object::SectionRef Section,
68 StringRef Name,
69 BinaryStreamReader &Reader) {
70 StringRef SectionName, Contents;
Zachary Turnere31b9dc2017-09-02 00:09:43 +000071 if (Section.getName(SectionName))
Zachary Turnerabb17cc2017-09-01 20:06:56 +000072 return false;
73
74 if (SectionName != Name)
75 return false;
76
Zachary Turnere31b9dc2017-09-02 00:09:43 +000077 if (Section.getContents(Contents))
Zachary Turnerabb17cc2017-09-01 20:06:56 +000078 return false;
79
80 Reader = BinaryStreamReader(Contents, support::little);
81 uint32_t Magic;
82 if (Reader.bytesRemaining() < sizeof(uint32_t))
83 return false;
84 cantFail(Reader.readInteger(Magic));
85 if (Magic != COFF::DEBUG_SECTION_MAGIC)
86 return false;
87 return true;
88}
89
90static inline bool isDebugSSection(object::SectionRef Section,
91 DebugSubsectionArray &Subsections) {
92 BinaryStreamReader Reader;
93 if (!isCodeViewDebugSubsection(Section, ".debug$S", Reader))
94 return false;
95
96 cantFail(Reader.readArray(Subsections, Reader.bytesRemaining()));
97 return true;
98}
99
100static bool isDebugTSection(SectionRef Section, CVTypeArray &Types) {
101 BinaryStreamReader Reader;
Alexandre Ganead9e96742018-04-09 20:17:56 +0000102 if (!isCodeViewDebugSubsection(Section, ".debug$T", Reader) &&
103 !isCodeViewDebugSubsection(Section, ".debug$P", Reader))
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000104 return false;
105 cantFail(Reader.readArray(Types, Reader.bytesRemaining()));
106 return true;
107}
108
109static std::string formatChecksumKind(FileChecksumKind Kind) {
110 switch (Kind) {
111 RETURN_CASE(FileChecksumKind, None, "None");
112 RETURN_CASE(FileChecksumKind, MD5, "MD5");
113 RETURN_CASE(FileChecksumKind, SHA1, "SHA-1");
114 RETURN_CASE(FileChecksumKind, SHA256, "SHA-256");
115 }
116 return formatUnknownEnum(Kind);
117}
118
119static const DebugStringTableSubsectionRef &extractStringTable(PDBFile &File) {
120 return cantFail(File.getStringTable()).getStringTable();
121}
122
123template <typename... Args>
124static void formatInternal(LinePrinter &Printer, bool Append, Args &&... args) {
125 if (Append)
126 Printer.format(std::forward<Args>(args)...);
127 else
128 Printer.formatLine(std::forward<Args>(args)...);
129}
130
131SymbolGroup::SymbolGroup(InputFile *File, uint32_t GroupIndex) : File(File) {
132 if (!File)
133 return;
134
135 if (File->isPdb())
136 initializeForPdb(GroupIndex);
137 else {
138 Name = ".debug$S";
139 uint32_t I = 0;
140 for (const auto &S : File->obj().sections()) {
141 DebugSubsectionArray SS;
142 if (!isDebugSSection(S, SS))
143 continue;
144
145 if (!SC.hasChecksums() || !SC.hasStrings())
146 SC.initialize(SS);
147
148 if (I == GroupIndex)
149 Subsections = SS;
150
151 if (SC.hasChecksums() && SC.hasStrings())
152 break;
153 }
154 rebuildChecksumMap();
155 }
156}
157
158StringRef SymbolGroup::name() const { return Name; }
159
160void SymbolGroup::updateDebugS(const codeview::DebugSubsectionArray &SS) {
161 Subsections = SS;
162}
163
164void SymbolGroup::updatePdbModi(uint32_t Modi) { initializeForPdb(Modi); }
165
166void SymbolGroup::initializeForPdb(uint32_t Modi) {
167 assert(File && File->isPdb());
168
169 // PDB always uses the same string table, but each module has its own
170 // checksums. So we only set the strings if they're not already set.
171 if (!SC.hasStrings())
172 SC.setStrings(extractStringTable(File->pdb()));
173
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}