blob: 8319fc550d6b25c4f9524766c97416c5e409dc39 [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();
44 auto Modi = Modules.getModuleDescriptor(Index);
45
46 ModuleName = Modi.getModuleName();
47
48 uint16_t ModiStream = Modi.getModuleStreamIndex();
49 if (ModiStream == kInvalidStreamIndex)
50 return make_error<RawError>(raw_error_code::no_stream,
51 "Module stream not present");
52
53 auto ModStreamData = File.createIndexedStream(ModiStream);
54
55 ModuleDebugStreamRef ModS(Modi, std::move(ModStreamData));
56 if (auto EC = ModS.reload())
57 return make_error<RawError>(raw_error_code::corrupt_file,
58 "Invalid module stream");
59
60 return std::move(ModS);
61}
62
63static inline bool isCodeViewDebugSubsection(object::SectionRef Section,
64 StringRef Name,
65 BinaryStreamReader &Reader) {
66 StringRef SectionName, Contents;
Zachary Turnere31b9dc2017-09-02 00:09:43 +000067 if (Section.getName(SectionName))
Zachary Turnerabb17cc2017-09-01 20:06:56 +000068 return false;
69
70 if (SectionName != Name)
71 return false;
72
Zachary Turnere31b9dc2017-09-02 00:09:43 +000073 if (Section.getContents(Contents))
Zachary Turnerabb17cc2017-09-01 20:06:56 +000074 return false;
75
76 Reader = BinaryStreamReader(Contents, support::little);
77 uint32_t Magic;
78 if (Reader.bytesRemaining() < sizeof(uint32_t))
79 return false;
80 cantFail(Reader.readInteger(Magic));
81 if (Magic != COFF::DEBUG_SECTION_MAGIC)
82 return false;
83 return true;
84}
85
86static inline bool isDebugSSection(object::SectionRef Section,
87 DebugSubsectionArray &Subsections) {
88 BinaryStreamReader Reader;
89 if (!isCodeViewDebugSubsection(Section, ".debug$S", Reader))
90 return false;
91
92 cantFail(Reader.readArray(Subsections, Reader.bytesRemaining()));
93 return true;
94}
95
96static bool isDebugTSection(SectionRef Section, CVTypeArray &Types) {
97 BinaryStreamReader Reader;
98 if (!isCodeViewDebugSubsection(Section, ".debug$T", Reader))
99 return false;
100 cantFail(Reader.readArray(Types, Reader.bytesRemaining()));
101 return true;
102}
103
104static std::string formatChecksumKind(FileChecksumKind Kind) {
105 switch (Kind) {
106 RETURN_CASE(FileChecksumKind, None, "None");
107 RETURN_CASE(FileChecksumKind, MD5, "MD5");
108 RETURN_CASE(FileChecksumKind, SHA1, "SHA-1");
109 RETURN_CASE(FileChecksumKind, SHA256, "SHA-256");
110 }
111 return formatUnknownEnum(Kind);
112}
113
114static const DebugStringTableSubsectionRef &extractStringTable(PDBFile &File) {
115 return cantFail(File.getStringTable()).getStringTable();
116}
117
118template <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.
166 if (!SC.hasStrings())
167 SC.setStrings(extractStringTable(File->pdb()));
168
169 SC.resetChecksums();
170 auto MDS = getModuleDebugStream(File->pdb(), Name, Modi);
171 if (!MDS) {
172 consumeError(MDS.takeError());
173 return;
174 }
175
176 DebugStream = std::make_shared<ModuleDebugStreamRef>(std::move(*MDS));
177 Subsections = DebugStream->getSubsectionsArray();
178 SC.initialize(Subsections);
179 rebuildChecksumMap();
180}
181
182void SymbolGroup::rebuildChecksumMap() {
183 if (!SC.hasChecksums())
184 return;
185
186 for (const auto &Entry : SC.checksums()) {
187 auto S = SC.strings().getString(Entry.FileNameOffset);
188 if (!S)
189 continue;
190 ChecksumsByFile[*S] = Entry;
191 }
192}
193
194const ModuleDebugStreamRef &SymbolGroup::getPdbModuleStream() const {
195 assert(File && File->isPdb() && DebugStream);
196 return *DebugStream;
197}
198
199Expected<StringRef> SymbolGroup::getNameFromStringTable(uint32_t Offset) const {
200 return SC.strings().getString(Offset);
201}
202
203void SymbolGroup::formatFromFileName(LinePrinter &Printer, StringRef File,
204 bool Append) const {
205 auto FC = ChecksumsByFile.find(File);
206 if (FC == ChecksumsByFile.end()) {
207 formatInternal(Printer, Append, "- (no checksum) {0}", File);
208 return;
209 }
210
211 formatInternal(Printer, Append, "- ({0}: {1}) {2}",
212 formatChecksumKind(FC->getValue().Kind),
213 toHex(FC->getValue().Checksum), File);
214}
215
216void SymbolGroup::formatFromChecksumsOffset(LinePrinter &Printer,
217 uint32_t Offset,
218 bool Append) const {
219 if (!SC.hasChecksums()) {
220 formatInternal(Printer, Append, "(unknown file name offset {0})", Offset);
221 return;
222 }
223
224 auto Iter = SC.checksums().getArray().at(Offset);
225 if (Iter == SC.checksums().getArray().end()) {
226 formatInternal(Printer, Append, "(unknown file name offset {0})", Offset);
227 return;
228 }
229
230 uint32_t FO = Iter->FileNameOffset;
231 auto ExpectedFile = getNameFromStringTable(FO);
232 if (!ExpectedFile) {
233 formatInternal(Printer, Append, "(unknown file name offset {0})", Offset);
234 consumeError(ExpectedFile.takeError());
235 return;
236 }
237 if (Iter->Kind == FileChecksumKind::None) {
238 formatInternal(Printer, Append, "{0} (no checksum)", *ExpectedFile);
239 } else {
240 formatInternal(Printer, Append, "{0} ({1}: {2})", *ExpectedFile,
241 formatChecksumKind(Iter->Kind), toHex(Iter->Checksum));
242 }
243}
244
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000245Expected<InputFile> InputFile::open(StringRef Path, bool AllowUnknownFile) {
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000246 InputFile IF;
247 if (!llvm::sys::fs::exists(Path))
248 return make_error<StringError>(formatv("File {0} not found", Path),
249 inconvertibleErrorCode());
250
251 file_magic Magic;
252 if (auto EC = identify_magic(Path, Magic))
253 return make_error<StringError>(
254 formatv("Unable to identify file type for file {0}", Path), EC);
255
256 if (Magic == file_magic::coff_object) {
257 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Path);
258 if (!BinaryOrErr)
259 return BinaryOrErr.takeError();
260
261 IF.CoffObject = std::move(*BinaryOrErr);
262 IF.PdbOrObj = llvm::cast<COFFObjectFile>(IF.CoffObject.getBinary());
263 return std::move(IF);
264 }
265
Zachary Turner49f86742018-03-07 19:12:36 +0000266 if (Magic == file_magic::pdb) {
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000267 std::unique_ptr<IPDBSession> Session;
268 if (auto Err = loadDataForPDB(PDB_ReaderType::Native, Path, Session))
269 return std::move(Err);
270
271 IF.PdbSession.reset(static_cast<NativeSession *>(Session.release()));
272 IF.PdbOrObj = &IF.PdbSession->getPDBFile();
273
274 return std::move(IF);
275 }
276
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000277 if (!AllowUnknownFile)
278 return make_error<StringError>(
279 formatv("File {0} is not a supported file type", Path),
280 inconvertibleErrorCode());
281
282 auto Result = MemoryBuffer::getFile(Path, -1i64, false);
283 if (!Result)
284 return make_error<StringError>(
285 formatv("File {0} could not be opened", Path), Result.getError());
286
287 IF.UnknownFile = std::move(*Result);
288 IF.PdbOrObj = IF.UnknownFile.get();
289 return std::move(IF);
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000290}
291
292PDBFile &InputFile::pdb() {
293 assert(isPdb());
294 return *PdbOrObj.get<PDBFile *>();
295}
296
297const PDBFile &InputFile::pdb() const {
298 assert(isPdb());
299 return *PdbOrObj.get<PDBFile *>();
300}
301
302object::COFFObjectFile &InputFile::obj() {
303 assert(isObj());
304 return *PdbOrObj.get<object::COFFObjectFile *>();
305}
306
307const object::COFFObjectFile &InputFile::obj() const {
308 assert(isObj());
309 return *PdbOrObj.get<object::COFFObjectFile *>();
310}
311
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000312MemoryBuffer &InputFile::unknown() {
313 assert(isUnknown());
314 return *PdbOrObj.get<MemoryBuffer *>();
315}
316
317const MemoryBuffer &InputFile::unknown() const {
318 assert(isUnknown());
319 return *PdbOrObj.get<MemoryBuffer *>();
320}
321
322StringRef InputFile::getFilePath() const {
323 if (isPdb())
324 return pdb().getFilePath();
325 if (isObj())
326 return obj().getFileName();
327 assert(isUnknown());
328 return unknown().getBufferIdentifier();
329}
330
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000331bool InputFile::hasTypes() const {
332 if (isPdb())
333 return pdb().hasPDBTpiStream();
334
335 for (const auto &Section : obj().sections()) {
336 CVTypeArray Types;
337 if (isDebugTSection(Section, Types))
338 return true;
339 }
340 return false;
341}
342
343bool InputFile::hasIds() const {
344 if (isObj())
345 return false;
346 return pdb().hasPDBIpiStream();
347}
348
349bool InputFile::isPdb() const { return PdbOrObj.is<PDBFile *>(); }
350
351bool InputFile::isObj() const {
352 return PdbOrObj.is<object::COFFObjectFile *>();
353}
354
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000355bool InputFile::isUnknown() const { return PdbOrObj.is<MemoryBuffer *>(); }
356
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000357codeview::LazyRandomTypeCollection &
358InputFile::getOrCreateTypeCollection(TypeCollectionKind Kind) {
359 if (Types && Kind == kTypes)
360 return *Types;
361 if (Ids && Kind == kIds)
362 return *Ids;
363
364 if (Kind == kIds) {
365 assert(isPdb() && pdb().hasPDBIpiStream());
366 }
367
368 // If the collection was already initialized, we should have just returned it
369 // in step 1.
370 if (isPdb()) {
371 TypeCollectionPtr &Collection = (Kind == kIds) ? Ids : Types;
372 auto &Stream = cantFail((Kind == kIds) ? pdb().getPDBIpiStream()
373 : pdb().getPDBTpiStream());
374
375 auto &Array = Stream.typeArray();
376 uint32_t Count = Stream.getNumTypeRecords();
377 auto Offsets = Stream.getTypeIndexOffsets();
378 Collection =
379 llvm::make_unique<LazyRandomTypeCollection>(Array, Count, Offsets);
380 return *Collection;
381 }
382
383 assert(isObj());
384 assert(Kind == kTypes);
385 assert(!Types);
386
387 for (const auto &Section : obj().sections()) {
388 CVTypeArray Records;
389 if (!isDebugTSection(Section, Records))
390 continue;
391
392 Types = llvm::make_unique<LazyRandomTypeCollection>(Records, 100);
393 return *Types;
394 }
395
396 Types = llvm::make_unique<LazyRandomTypeCollection>(100);
397 return *Types;
398}
399
400codeview::LazyRandomTypeCollection &InputFile::types() {
401 return getOrCreateTypeCollection(kTypes);
402}
403
404codeview::LazyRandomTypeCollection &InputFile::ids() {
405 // Object files have only one type stream that contains both types and ids.
406 // Similarly, some PDBs don't contain an IPI stream, and for those both types
407 // and IDs are in the same stream.
408 if (isObj() || !pdb().hasPDBIpiStream())
409 return types();
410
411 return getOrCreateTypeCollection(kIds);
412}
413
414iterator_range<SymbolGroupIterator> InputFile::symbol_groups() {
415 return make_range<SymbolGroupIterator>(symbol_groups_begin(),
416 symbol_groups_end());
417}
418
419SymbolGroupIterator InputFile::symbol_groups_begin() {
420 return SymbolGroupIterator(*this);
421}
422
423SymbolGroupIterator InputFile::symbol_groups_end() {
424 return SymbolGroupIterator();
425}
426
427SymbolGroupIterator::SymbolGroupIterator() : Value(nullptr) {}
428
429SymbolGroupIterator::SymbolGroupIterator(InputFile &File) : Value(&File) {
430 if (File.isObj()) {
431 SectionIter = File.obj().section_begin();
432 scanToNextDebugS();
433 }
434}
435
436bool SymbolGroupIterator::operator==(const SymbolGroupIterator &R) const {
437 bool E = isEnd();
438 bool RE = R.isEnd();
439 if (E || RE)
440 return E == RE;
441
442 if (Value.File != R.Value.File)
443 return false;
444 return Index == R.Index;
445}
446
447const SymbolGroup &SymbolGroupIterator::operator*() const {
448 assert(!isEnd());
449 return Value;
450}
451SymbolGroup &SymbolGroupIterator::operator*() {
452 assert(!isEnd());
453 return Value;
454}
455
456SymbolGroupIterator &SymbolGroupIterator::operator++() {
457 assert(Value.File && !isEnd());
458 ++Index;
459 if (isEnd())
460 return *this;
461
462 if (Value.File->isPdb()) {
463 Value.updatePdbModi(Index);
464 return *this;
465 }
466
467 scanToNextDebugS();
468 return *this;
469}
470
471void SymbolGroupIterator::scanToNextDebugS() {
472 assert(SectionIter.hasValue());
473 auto End = Value.File->obj().section_end();
474 auto &Iter = *SectionIter;
475 assert(!isEnd());
476
477 while (++Iter != End) {
478 DebugSubsectionArray SS;
479 SectionRef SR = *Iter;
480 if (!isDebugSSection(SR, SS))
481 continue;
482
483 Value.updateDebugS(SS);
484 return;
485 }
486}
487
488bool SymbolGroupIterator::isEnd() const {
489 if (!Value.File)
490 return true;
491 if (Value.File->isPdb()) {
492 auto &Dbi = cantFail(Value.File->pdb().getPDBDbiStream());
493 uint32_t Count = Dbi.modules().getModuleCount();
494 assert(Index <= Count);
495 return Index == Count;
496 }
497
498 assert(SectionIter.hasValue());
499 return *SectionIter == Value.File->obj().section_end();
500}