blob: bd23bfdbe31a2b35306c7881d26eb9f24c56ad17 [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) {
Fangrui Songa076ec52019-05-16 11:33:48 +000069 StringRef SectionName;
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
Fangrui Songa076ec52019-05-16 11:33:48 +000076 Expected<StringRef> ContentsOrErr = Section.getContents();
77 if (!ContentsOrErr) {
78 consumeError(ContentsOrErr.takeError());
Zachary Turnerabb17cc2017-09-01 20:06:56 +000079 return false;
Fangrui Songa076ec52019-05-16 11:33:48 +000080 }
Zachary Turnerabb17cc2017-09-01 20:06:56 +000081
Fangrui Songa076ec52019-05-16 11:33:48 +000082 Reader = BinaryStreamReader(*ContentsOrErr, support::little);
Zachary Turnerabb17cc2017-09-01 20:06:56 +000083 uint32_t Magic;
84 if (Reader.bytesRemaining() < sizeof(uint32_t))
85 return false;
86 cantFail(Reader.readInteger(Magic));
87 if (Magic != COFF::DEBUG_SECTION_MAGIC)
88 return false;
89 return true;
90}
91
92static inline bool isDebugSSection(object::SectionRef Section,
93 DebugSubsectionArray &Subsections) {
94 BinaryStreamReader Reader;
95 if (!isCodeViewDebugSubsection(Section, ".debug$S", Reader))
96 return false;
97
98 cantFail(Reader.readArray(Subsections, Reader.bytesRemaining()));
99 return true;
100}
101
102static bool isDebugTSection(SectionRef Section, CVTypeArray &Types) {
103 BinaryStreamReader Reader;
Alexandre Ganead9e96742018-04-09 20:17:56 +0000104 if (!isCodeViewDebugSubsection(Section, ".debug$T", Reader) &&
105 !isCodeViewDebugSubsection(Section, ".debug$P", Reader))
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000106 return false;
107 cantFail(Reader.readArray(Types, Reader.bytesRemaining()));
108 return true;
109}
110
111static std::string formatChecksumKind(FileChecksumKind Kind) {
112 switch (Kind) {
113 RETURN_CASE(FileChecksumKind, None, "None");
114 RETURN_CASE(FileChecksumKind, MD5, "MD5");
115 RETURN_CASE(FileChecksumKind, SHA1, "SHA-1");
116 RETURN_CASE(FileChecksumKind, SHA256, "SHA-256");
117 }
118 return formatUnknownEnum(Kind);
119}
120
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000121template <typename... Args>
122static void formatInternal(LinePrinter &Printer, bool Append, Args &&... args) {
123 if (Append)
124 Printer.format(std::forward<Args>(args)...);
125 else
126 Printer.formatLine(std::forward<Args>(args)...);
127}
128
129SymbolGroup::SymbolGroup(InputFile *File, uint32_t GroupIndex) : File(File) {
130 if (!File)
131 return;
132
133 if (File->isPdb())
134 initializeForPdb(GroupIndex);
135 else {
136 Name = ".debug$S";
137 uint32_t I = 0;
138 for (const auto &S : File->obj().sections()) {
139 DebugSubsectionArray SS;
140 if (!isDebugSSection(S, SS))
141 continue;
142
143 if (!SC.hasChecksums() || !SC.hasStrings())
144 SC.initialize(SS);
145
146 if (I == GroupIndex)
147 Subsections = SS;
148
149 if (SC.hasChecksums() && SC.hasStrings())
150 break;
151 }
152 rebuildChecksumMap();
153 }
154}
155
156StringRef SymbolGroup::name() const { return Name; }
157
158void SymbolGroup::updateDebugS(const codeview::DebugSubsectionArray &SS) {
159 Subsections = SS;
160}
161
162void SymbolGroup::updatePdbModi(uint32_t Modi) { initializeForPdb(Modi); }
163
164void SymbolGroup::initializeForPdb(uint32_t Modi) {
165 assert(File && File->isPdb());
166
167 // PDB always uses the same string table, but each module has its own
168 // checksums. So we only set the strings if they're not already set.
Leonard Mosescu4bdbea32018-11-02 18:00:37 +0000169 if (!SC.hasStrings()) {
170 auto StringTable = File->pdb().getStringTable();
171 if (StringTable)
172 SC.setStrings(StringTable->getStringTable());
173 else
174 consumeError(StringTable.takeError());
175 }
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000176
177 SC.resetChecksums();
178 auto MDS = getModuleDebugStream(File->pdb(), Name, Modi);
179 if (!MDS) {
180 consumeError(MDS.takeError());
181 return;
182 }
183
184 DebugStream = std::make_shared<ModuleDebugStreamRef>(std::move(*MDS));
185 Subsections = DebugStream->getSubsectionsArray();
186 SC.initialize(Subsections);
187 rebuildChecksumMap();
188}
189
190void SymbolGroup::rebuildChecksumMap() {
191 if (!SC.hasChecksums())
192 return;
193
194 for (const auto &Entry : SC.checksums()) {
195 auto S = SC.strings().getString(Entry.FileNameOffset);
196 if (!S)
197 continue;
198 ChecksumsByFile[*S] = Entry;
199 }
200}
201
202const ModuleDebugStreamRef &SymbolGroup::getPdbModuleStream() const {
203 assert(File && File->isPdb() && DebugStream);
204 return *DebugStream;
205}
206
207Expected<StringRef> SymbolGroup::getNameFromStringTable(uint32_t Offset) const {
208 return SC.strings().getString(Offset);
209}
210
211void SymbolGroup::formatFromFileName(LinePrinter &Printer, StringRef File,
212 bool Append) const {
213 auto FC = ChecksumsByFile.find(File);
214 if (FC == ChecksumsByFile.end()) {
215 formatInternal(Printer, Append, "- (no checksum) {0}", File);
216 return;
217 }
218
219 formatInternal(Printer, Append, "- ({0}: {1}) {2}",
220 formatChecksumKind(FC->getValue().Kind),
221 toHex(FC->getValue().Checksum), File);
222}
223
224void SymbolGroup::formatFromChecksumsOffset(LinePrinter &Printer,
225 uint32_t Offset,
226 bool Append) const {
227 if (!SC.hasChecksums()) {
228 formatInternal(Printer, Append, "(unknown file name offset {0})", Offset);
229 return;
230 }
231
232 auto Iter = SC.checksums().getArray().at(Offset);
233 if (Iter == SC.checksums().getArray().end()) {
234 formatInternal(Printer, Append, "(unknown file name offset {0})", Offset);
235 return;
236 }
237
238 uint32_t FO = Iter->FileNameOffset;
239 auto ExpectedFile = getNameFromStringTable(FO);
240 if (!ExpectedFile) {
241 formatInternal(Printer, Append, "(unknown file name offset {0})", Offset);
242 consumeError(ExpectedFile.takeError());
243 return;
244 }
245 if (Iter->Kind == FileChecksumKind::None) {
246 formatInternal(Printer, Append, "{0} (no checksum)", *ExpectedFile);
247 } else {
248 formatInternal(Printer, Append, "{0} ({1}: {2})", *ExpectedFile,
249 formatChecksumKind(Iter->Kind), toHex(Iter->Checksum));
250 }
251}
252
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000253Expected<InputFile> InputFile::open(StringRef Path, bool AllowUnknownFile) {
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000254 InputFile IF;
255 if (!llvm::sys::fs::exists(Path))
256 return make_error<StringError>(formatv("File {0} not found", Path),
257 inconvertibleErrorCode());
258
259 file_magic Magic;
260 if (auto EC = identify_magic(Path, Magic))
261 return make_error<StringError>(
262 formatv("Unable to identify file type for file {0}", Path), EC);
263
264 if (Magic == file_magic::coff_object) {
265 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Path);
266 if (!BinaryOrErr)
267 return BinaryOrErr.takeError();
268
269 IF.CoffObject = std::move(*BinaryOrErr);
270 IF.PdbOrObj = llvm::cast<COFFObjectFile>(IF.CoffObject.getBinary());
271 return std::move(IF);
272 }
273
Zachary Turner49f86742018-03-07 19:12:36 +0000274 if (Magic == file_magic::pdb) {
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000275 std::unique_ptr<IPDBSession> Session;
276 if (auto Err = loadDataForPDB(PDB_ReaderType::Native, Path, Session))
277 return std::move(Err);
278
279 IF.PdbSession.reset(static_cast<NativeSession *>(Session.release()));
280 IF.PdbOrObj = &IF.PdbSession->getPDBFile();
281
282 return std::move(IF);
283 }
284
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000285 if (!AllowUnknownFile)
286 return make_error<StringError>(
287 formatv("File {0} is not a supported file type", Path),
288 inconvertibleErrorCode());
289
Zachary Turner4703a3f2018-04-04 17:41:05 +0000290 auto Result = MemoryBuffer::getFile(Path, -1LL, false);
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000291 if (!Result)
292 return make_error<StringError>(
293 formatv("File {0} could not be opened", Path), Result.getError());
294
295 IF.UnknownFile = std::move(*Result);
296 IF.PdbOrObj = IF.UnknownFile.get();
297 return std::move(IF);
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000298}
299
300PDBFile &InputFile::pdb() {
301 assert(isPdb());
302 return *PdbOrObj.get<PDBFile *>();
303}
304
305const PDBFile &InputFile::pdb() const {
306 assert(isPdb());
307 return *PdbOrObj.get<PDBFile *>();
308}
309
310object::COFFObjectFile &InputFile::obj() {
311 assert(isObj());
312 return *PdbOrObj.get<object::COFFObjectFile *>();
313}
314
315const object::COFFObjectFile &InputFile::obj() const {
316 assert(isObj());
317 return *PdbOrObj.get<object::COFFObjectFile *>();
318}
319
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000320MemoryBuffer &InputFile::unknown() {
321 assert(isUnknown());
322 return *PdbOrObj.get<MemoryBuffer *>();
323}
324
325const MemoryBuffer &InputFile::unknown() const {
326 assert(isUnknown());
327 return *PdbOrObj.get<MemoryBuffer *>();
328}
329
330StringRef InputFile::getFilePath() const {
331 if (isPdb())
332 return pdb().getFilePath();
333 if (isObj())
334 return obj().getFileName();
335 assert(isUnknown());
336 return unknown().getBufferIdentifier();
337}
338
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000339bool InputFile::hasTypes() const {
340 if (isPdb())
341 return pdb().hasPDBTpiStream();
342
343 for (const auto &Section : obj().sections()) {
344 CVTypeArray Types;
345 if (isDebugTSection(Section, Types))
346 return true;
347 }
348 return false;
349}
350
351bool InputFile::hasIds() const {
352 if (isObj())
353 return false;
354 return pdb().hasPDBIpiStream();
355}
356
357bool InputFile::isPdb() const { return PdbOrObj.is<PDBFile *>(); }
358
359bool InputFile::isObj() const {
360 return PdbOrObj.is<object::COFFObjectFile *>();
361}
362
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000363bool InputFile::isUnknown() const { return PdbOrObj.is<MemoryBuffer *>(); }
364
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000365codeview::LazyRandomTypeCollection &
366InputFile::getOrCreateTypeCollection(TypeCollectionKind Kind) {
367 if (Types && Kind == kTypes)
368 return *Types;
369 if (Ids && Kind == kIds)
370 return *Ids;
371
372 if (Kind == kIds) {
373 assert(isPdb() && pdb().hasPDBIpiStream());
374 }
375
376 // If the collection was already initialized, we should have just returned it
377 // in step 1.
378 if (isPdb()) {
379 TypeCollectionPtr &Collection = (Kind == kIds) ? Ids : Types;
380 auto &Stream = cantFail((Kind == kIds) ? pdb().getPDBIpiStream()
381 : pdb().getPDBTpiStream());
382
383 auto &Array = Stream.typeArray();
384 uint32_t Count = Stream.getNumTypeRecords();
385 auto Offsets = Stream.getTypeIndexOffsets();
386 Collection =
387 llvm::make_unique<LazyRandomTypeCollection>(Array, Count, Offsets);
388 return *Collection;
389 }
390
391 assert(isObj());
392 assert(Kind == kTypes);
393 assert(!Types);
394
395 for (const auto &Section : obj().sections()) {
396 CVTypeArray Records;
397 if (!isDebugTSection(Section, Records))
398 continue;
399
400 Types = llvm::make_unique<LazyRandomTypeCollection>(Records, 100);
401 return *Types;
402 }
403
404 Types = llvm::make_unique<LazyRandomTypeCollection>(100);
405 return *Types;
406}
407
408codeview::LazyRandomTypeCollection &InputFile::types() {
409 return getOrCreateTypeCollection(kTypes);
410}
411
412codeview::LazyRandomTypeCollection &InputFile::ids() {
413 // Object files have only one type stream that contains both types and ids.
414 // Similarly, some PDBs don't contain an IPI stream, and for those both types
415 // and IDs are in the same stream.
416 if (isObj() || !pdb().hasPDBIpiStream())
417 return types();
418
419 return getOrCreateTypeCollection(kIds);
420}
421
422iterator_range<SymbolGroupIterator> InputFile::symbol_groups() {
423 return make_range<SymbolGroupIterator>(symbol_groups_begin(),
424 symbol_groups_end());
425}
426
427SymbolGroupIterator InputFile::symbol_groups_begin() {
428 return SymbolGroupIterator(*this);
429}
430
431SymbolGroupIterator InputFile::symbol_groups_end() {
432 return SymbolGroupIterator();
433}
434
435SymbolGroupIterator::SymbolGroupIterator() : Value(nullptr) {}
436
437SymbolGroupIterator::SymbolGroupIterator(InputFile &File) : Value(&File) {
438 if (File.isObj()) {
439 SectionIter = File.obj().section_begin();
440 scanToNextDebugS();
441 }
442}
443
444bool SymbolGroupIterator::operator==(const SymbolGroupIterator &R) const {
445 bool E = isEnd();
446 bool RE = R.isEnd();
447 if (E || RE)
448 return E == RE;
449
450 if (Value.File != R.Value.File)
451 return false;
452 return Index == R.Index;
453}
454
455const SymbolGroup &SymbolGroupIterator::operator*() const {
456 assert(!isEnd());
457 return Value;
458}
459SymbolGroup &SymbolGroupIterator::operator*() {
460 assert(!isEnd());
461 return Value;
462}
463
464SymbolGroupIterator &SymbolGroupIterator::operator++() {
465 assert(Value.File && !isEnd());
466 ++Index;
467 if (isEnd())
468 return *this;
469
470 if (Value.File->isPdb()) {
471 Value.updatePdbModi(Index);
472 return *this;
473 }
474
475 scanToNextDebugS();
476 return *this;
477}
478
479void SymbolGroupIterator::scanToNextDebugS() {
480 assert(SectionIter.hasValue());
481 auto End = Value.File->obj().section_end();
482 auto &Iter = *SectionIter;
483 assert(!isEnd());
484
485 while (++Iter != End) {
486 DebugSubsectionArray SS;
487 SectionRef SR = *Iter;
488 if (!isDebugSSection(SR, SS))
489 continue;
490
491 Value.updateDebugS(SS);
492 return;
493 }
494}
495
496bool SymbolGroupIterator::isEnd() const {
497 if (!Value.File)
498 return true;
499 if (Value.File->isPdb()) {
500 auto &Dbi = cantFail(Value.File->pdb().getPDBDbiStream());
501 uint32_t Count = Dbi.modules().getModuleCount();
502 assert(Index <= Count);
503 return Index == Count;
504 }
505
506 assert(SectionIter.hasValue());
507 return *SectionIter == Value.File->obj().section_end();
508}