DbiStream.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //===- DbiStream.cpp - PDB Dbi Stream (Stream 3) Access -------------------===//
  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. #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
  11. #include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h"
  12. #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
  13. #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
  14. #include "llvm/DebugInfo/PDB/Native/RawError.h"
  15. #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
  16. #include "llvm/DebugInfo/PDB/PDBTypes.h"
  17. #include "llvm/Object/COFF.h"
  18. #include "llvm/Support/BinaryStreamArray.h"
  19. #include "llvm/Support/BinaryStreamReader.h"
  20. #include "llvm/Support/Error.h"
  21. #include <cstddef>
  22. #include <cstdint>
  23. using namespace llvm;
  24. using namespace llvm::codeview;
  25. using namespace llvm::msf;
  26. using namespace llvm::pdb;
  27. using namespace llvm::support;
  28. template <typename ContribType>
  29. static Error loadSectionContribs(FixedStreamArray<ContribType> &Output,
  30. BinaryStreamReader &Reader) {
  31. if (Reader.bytesRemaining() % sizeof(ContribType) != 0)
  32. return make_error<RawError>(
  33. raw_error_code::corrupt_file,
  34. "Invalid number of bytes of section contributions");
  35. uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType);
  36. if (auto EC = Reader.readArray(Output, Count))
  37. return EC;
  38. return Error::success();
  39. }
  40. DbiStream::DbiStream(std::unique_ptr<BinaryStream> Stream)
  41. : Stream(std::move(Stream)), Header(nullptr) {}
  42. DbiStream::~DbiStream() = default;
  43. Error DbiStream::reload(PDBFile *Pdb) {
  44. BinaryStreamReader Reader(*Stream);
  45. if (Stream->getLength() < sizeof(DbiStreamHeader))
  46. return make_error<RawError>(raw_error_code::corrupt_file,
  47. "DBI Stream does not contain a header.");
  48. if (auto EC = Reader.readObject(Header))
  49. return make_error<RawError>(raw_error_code::corrupt_file,
  50. "DBI Stream does not contain a header.");
  51. if (Header->VersionSignature != -1)
  52. return make_error<RawError>(raw_error_code::corrupt_file,
  53. "Invalid DBI version signature.");
  54. // Require at least version 7, which should be present in all PDBs
  55. // produced in the last decade and allows us to avoid having to
  56. // special case all kinds of complicated arcane formats.
  57. if (Header->VersionHeader < PdbDbiV70)
  58. return make_error<RawError>(raw_error_code::feature_unsupported,
  59. "Unsupported DBI version.");
  60. if (Stream->getLength() !=
  61. sizeof(DbiStreamHeader) + Header->ModiSubstreamSize +
  62. Header->SecContrSubstreamSize + Header->SectionMapSize +
  63. Header->FileInfoSize + Header->TypeServerSize +
  64. Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
  65. return make_error<RawError>(raw_error_code::corrupt_file,
  66. "DBI Length does not equal sum of substreams.");
  67. // Only certain substreams are guaranteed to be aligned. Validate
  68. // them here.
  69. if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
  70. return make_error<RawError>(raw_error_code::corrupt_file,
  71. "DBI MODI substream not aligned.");
  72. if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
  73. return make_error<RawError>(
  74. raw_error_code::corrupt_file,
  75. "DBI section contribution substream not aligned.");
  76. if (Header->SectionMapSize % sizeof(uint32_t) != 0)
  77. return make_error<RawError>(raw_error_code::corrupt_file,
  78. "DBI section map substream not aligned.");
  79. if (Header->FileInfoSize % sizeof(uint32_t) != 0)
  80. return make_error<RawError>(raw_error_code::corrupt_file,
  81. "DBI file info substream not aligned.");
  82. if (Header->TypeServerSize % sizeof(uint32_t) != 0)
  83. return make_error<RawError>(raw_error_code::corrupt_file,
  84. "DBI type server substream not aligned.");
  85. if (auto EC = Reader.readSubstream(ModiSubstream, Header->ModiSubstreamSize))
  86. return EC;
  87. if (auto EC = Reader.readSubstream(SecContrSubstream,
  88. Header->SecContrSubstreamSize))
  89. return EC;
  90. if (auto EC = Reader.readSubstream(SecMapSubstream, Header->SectionMapSize))
  91. return EC;
  92. if (auto EC = Reader.readSubstream(FileInfoSubstream, Header->FileInfoSize))
  93. return EC;
  94. if (auto EC =
  95. Reader.readSubstream(TypeServerMapSubstream, Header->TypeServerSize))
  96. return EC;
  97. if (auto EC = Reader.readSubstream(ECSubstream, Header->ECSubstreamSize))
  98. return EC;
  99. if (auto EC = Reader.readArray(
  100. DbgStreams, Header->OptionalDbgHdrSize / sizeof(ulittle16_t)))
  101. return EC;
  102. if (auto EC = Modules.initialize(ModiSubstream.StreamData,
  103. FileInfoSubstream.StreamData))
  104. return EC;
  105. if (auto EC = initializeSectionContributionData())
  106. return EC;
  107. if (auto EC = initializeSectionHeadersData(Pdb))
  108. return EC;
  109. if (auto EC = initializeSectionMapData())
  110. return EC;
  111. if (auto EC = initializeOldFpoRecords(Pdb))
  112. return EC;
  113. if (auto EC = initializeNewFpoRecords(Pdb))
  114. return EC;
  115. if (Reader.bytesRemaining() > 0)
  116. return make_error<RawError>(raw_error_code::corrupt_file,
  117. "Found unexpected bytes in DBI Stream.");
  118. if (!ECSubstream.empty()) {
  119. BinaryStreamReader ECReader(ECSubstream.StreamData);
  120. if (auto EC = ECNames.reload(ECReader))
  121. return EC;
  122. }
  123. return Error::success();
  124. }
  125. PdbRaw_DbiVer DbiStream::getDbiVersion() const {
  126. uint32_t Value = Header->VersionHeader;
  127. return static_cast<PdbRaw_DbiVer>(Value);
  128. }
  129. uint32_t DbiStream::getAge() const { return Header->Age; }
  130. uint16_t DbiStream::getPublicSymbolStreamIndex() const {
  131. return Header->PublicSymbolStreamIndex;
  132. }
  133. uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
  134. return Header->GlobalSymbolStreamIndex;
  135. }
  136. uint16_t DbiStream::getFlags() const { return Header->Flags; }
  137. bool DbiStream::isIncrementallyLinked() const {
  138. return (Header->Flags & DbiFlags::FlagIncrementalMask) != 0;
  139. }
  140. bool DbiStream::hasCTypes() const {
  141. return (Header->Flags & DbiFlags::FlagHasCTypesMask) != 0;
  142. }
  143. bool DbiStream::isStripped() const {
  144. return (Header->Flags & DbiFlags::FlagStrippedMask) != 0;
  145. }
  146. uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; }
  147. uint16_t DbiStream::getBuildMajorVersion() const {
  148. return (Header->BuildNumber & DbiBuildNo::BuildMajorMask) >>
  149. DbiBuildNo::BuildMajorShift;
  150. }
  151. uint16_t DbiStream::getBuildMinorVersion() const {
  152. return (Header->BuildNumber & DbiBuildNo::BuildMinorMask) >>
  153. DbiBuildNo::BuildMinorShift;
  154. }
  155. uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; }
  156. uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
  157. uint32_t DbiStream::getSymRecordStreamIndex() const {
  158. return Header->SymRecordStreamIndex;
  159. }
  160. PDB_Machine DbiStream::getMachineType() const {
  161. uint16_t Machine = Header->MachineType;
  162. return static_cast<PDB_Machine>(Machine);
  163. }
  164. FixedStreamArray<object::coff_section> DbiStream::getSectionHeaders() const {
  165. return SectionHeaders;
  166. }
  167. bool DbiStream::hasOldFpoRecords() const { return OldFpoStream != nullptr; }
  168. FixedStreamArray<object::FpoData> DbiStream::getOldFpoRecords() const {
  169. return OldFpoRecords;
  170. }
  171. bool DbiStream::hasNewFpoRecords() const { return NewFpoStream != nullptr; }
  172. const DebugFrameDataSubsectionRef &DbiStream::getNewFpoRecords() const {
  173. return NewFpoRecords;
  174. }
  175. const DbiModuleList &DbiStream::modules() const { return Modules; }
  176. FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
  177. return SectionMap;
  178. }
  179. void DbiStream::visitSectionContributions(
  180. ISectionContribVisitor &Visitor) const {
  181. if (!SectionContribs.empty()) {
  182. assert(SectionContribVersion == DbiSecContribVer60);
  183. for (auto &SC : SectionContribs)
  184. Visitor.visit(SC);
  185. } else if (!SectionContribs2.empty()) {
  186. assert(SectionContribVersion == DbiSecContribV2);
  187. for (auto &SC : SectionContribs2)
  188. Visitor.visit(SC);
  189. }
  190. }
  191. Expected<StringRef> DbiStream::getECName(uint32_t NI) const {
  192. return ECNames.getStringForID(NI);
  193. }
  194. Error DbiStream::initializeSectionContributionData() {
  195. if (SecContrSubstream.empty())
  196. return Error::success();
  197. BinaryStreamReader SCReader(SecContrSubstream.StreamData);
  198. if (auto EC = SCReader.readEnum(SectionContribVersion))
  199. return EC;
  200. if (SectionContribVersion == DbiSecContribVer60)
  201. return loadSectionContribs<SectionContrib>(SectionContribs, SCReader);
  202. if (SectionContribVersion == DbiSecContribV2)
  203. return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader);
  204. return make_error<RawError>(raw_error_code::feature_unsupported,
  205. "Unsupported DBI Section Contribution version");
  206. }
  207. // Initializes this->SectionHeaders.
  208. Error DbiStream::initializeSectionHeadersData(PDBFile *Pdb) {
  209. Expected<std::unique_ptr<msf::MappedBlockStream>> ExpectedStream =
  210. createIndexedStreamForHeaderType(Pdb, DbgHeaderType::SectionHdr);
  211. if (auto EC = ExpectedStream.takeError())
  212. return EC;
  213. auto &SHS = *ExpectedStream;
  214. if (!SHS)
  215. return Error::success();
  216. size_t StreamLen = SHS->getLength();
  217. if (StreamLen % sizeof(object::coff_section))
  218. return make_error<RawError>(raw_error_code::corrupt_file,
  219. "Corrupted section header stream.");
  220. size_t NumSections = StreamLen / sizeof(object::coff_section);
  221. BinaryStreamReader Reader(*SHS);
  222. if (auto EC = Reader.readArray(SectionHeaders, NumSections))
  223. return make_error<RawError>(raw_error_code::corrupt_file,
  224. "Could not read a bitmap.");
  225. SectionHeaderStream = std::move(SHS);
  226. return Error::success();
  227. }
  228. // Initializes this->Fpos.
  229. Error DbiStream::initializeOldFpoRecords(PDBFile *Pdb) {
  230. Expected<std::unique_ptr<msf::MappedBlockStream>> ExpectedStream =
  231. createIndexedStreamForHeaderType(Pdb, DbgHeaderType::FPO);
  232. if (auto EC = ExpectedStream.takeError())
  233. return EC;
  234. auto &FS = *ExpectedStream;
  235. if (!FS)
  236. return Error::success();
  237. size_t StreamLen = FS->getLength();
  238. if (StreamLen % sizeof(object::FpoData))
  239. return make_error<RawError>(raw_error_code::corrupt_file,
  240. "Corrupted Old FPO stream.");
  241. size_t NumRecords = StreamLen / sizeof(object::FpoData);
  242. BinaryStreamReader Reader(*FS);
  243. if (auto EC = Reader.readArray(OldFpoRecords, NumRecords))
  244. return make_error<RawError>(raw_error_code::corrupt_file,
  245. "Corrupted Old FPO stream.");
  246. OldFpoStream = std::move(FS);
  247. return Error::success();
  248. }
  249. Error DbiStream::initializeNewFpoRecords(PDBFile *Pdb) {
  250. Expected<std::unique_ptr<msf::MappedBlockStream>> ExpectedStream =
  251. createIndexedStreamForHeaderType(Pdb, DbgHeaderType::NewFPO);
  252. if (auto EC = ExpectedStream.takeError())
  253. return EC;
  254. auto &FS = *ExpectedStream;
  255. if (!FS)
  256. return Error::success();
  257. if (auto EC = NewFpoRecords.initialize(*FS))
  258. return EC;
  259. NewFpoStream = std::move(FS);
  260. return Error::success();
  261. }
  262. Expected<std::unique_ptr<msf::MappedBlockStream>>
  263. DbiStream::createIndexedStreamForHeaderType(PDBFile *Pdb,
  264. DbgHeaderType Type) const {
  265. if (!Pdb)
  266. return nullptr;
  267. if (DbgStreams.empty())
  268. return nullptr;
  269. uint32_t StreamNum = getDebugStreamIndex(Type);
  270. // This means there is no such stream.
  271. if (StreamNum == kInvalidStreamIndex)
  272. return nullptr;
  273. return Pdb->safelyCreateIndexedStream(StreamNum);
  274. }
  275. BinarySubstreamRef DbiStream::getSectionContributionData() const {
  276. return SecContrSubstream;
  277. }
  278. BinarySubstreamRef DbiStream::getSecMapSubstreamData() const {
  279. return SecMapSubstream;
  280. }
  281. BinarySubstreamRef DbiStream::getModiSubstreamData() const {
  282. return ModiSubstream;
  283. }
  284. BinarySubstreamRef DbiStream::getFileInfoSubstreamData() const {
  285. return FileInfoSubstream;
  286. }
  287. BinarySubstreamRef DbiStream::getTypeServerMapSubstreamData() const {
  288. return TypeServerMapSubstream;
  289. }
  290. BinarySubstreamRef DbiStream::getECSubstreamData() const { return ECSubstream; }
  291. Error DbiStream::initializeSectionMapData() {
  292. if (SecMapSubstream.empty())
  293. return Error::success();
  294. BinaryStreamReader SMReader(SecMapSubstream.StreamData);
  295. const SecMapHeader *Header;
  296. if (auto EC = SMReader.readObject(Header))
  297. return EC;
  298. if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
  299. return EC;
  300. return Error::success();
  301. }
  302. uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
  303. uint16_t T = static_cast<uint16_t>(Type);
  304. if (T >= DbgStreams.size())
  305. return kInvalidStreamIndex;
  306. return DbgStreams[T];
  307. }