DbiModuleList.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //===- DbiModuleList.cpp - PDB module information list --------------------===//
  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/DbiModuleList.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/ADT/iterator_range.h"
  11. #include "llvm/DebugInfo/PDB/Native/RawError.h"
  12. #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
  13. #include "llvm/Support/BinaryStreamReader.h"
  14. #include "llvm/Support/Error.h"
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <cstddef>
  18. #include <cstdint>
  19. using namespace llvm;
  20. using namespace llvm::pdb;
  21. DbiModuleSourceFilesIterator::DbiModuleSourceFilesIterator(
  22. const DbiModuleList &Modules, uint32_t Modi, uint16_t Filei)
  23. : Modules(&Modules), Modi(Modi), Filei(Filei) {
  24. setValue();
  25. }
  26. bool DbiModuleSourceFilesIterator::
  27. operator==(const DbiModuleSourceFilesIterator &R) const {
  28. // incompatible iterators are never equal
  29. if (!isCompatible(R))
  30. return false;
  31. // If they're compatible, and they're both ends, then they're equal.
  32. if (isEnd() && R.isEnd())
  33. return true;
  34. // If one is an end and the other is not, they're not equal.
  35. if (isEnd() != R.isEnd())
  36. return false;
  37. // Now we know:
  38. // - They're compatible
  39. // - They're not *both* end iterators
  40. // - Their endness is the same.
  41. // Thus, they're compatible iterators pointing to a valid file on the same
  42. // module. All we need to check are the file indices.
  43. assert(Modules == R.Modules);
  44. assert(Modi == R.Modi);
  45. assert(!isEnd());
  46. assert(!R.isEnd());
  47. return (Filei == R.Filei);
  48. }
  49. bool DbiModuleSourceFilesIterator::
  50. operator<(const DbiModuleSourceFilesIterator &R) const {
  51. assert(isCompatible(R));
  52. // It's not sufficient to compare the file indices, because default
  53. // constructed iterators could be equal to iterators with valid indices. To
  54. // account for this, early-out if they're equal.
  55. if (*this == R)
  56. return false;
  57. return Filei < R.Filei;
  58. }
  59. std::ptrdiff_t DbiModuleSourceFilesIterator::
  60. operator-(const DbiModuleSourceFilesIterator &R) const {
  61. assert(isCompatible(R));
  62. assert(!(*this < R));
  63. // If they're both end iterators, the distance is 0.
  64. if (isEnd() && R.isEnd())
  65. return 0;
  66. assert(!R.isEnd());
  67. // At this point, R cannot be end, but *this can, which means that *this
  68. // might be a universal end iterator with none of its fields set. So in that
  69. // case have to rely on R as the authority to figure out how many files there
  70. // are to compute the distance.
  71. uint32_t Thisi = Filei;
  72. if (isEnd()) {
  73. uint32_t RealModi = R.Modi;
  74. Thisi = R.Modules->getSourceFileCount(RealModi);
  75. }
  76. assert(Thisi >= R.Filei);
  77. return Thisi - R.Filei;
  78. }
  79. DbiModuleSourceFilesIterator &DbiModuleSourceFilesIterator::
  80. operator+=(std::ptrdiff_t N) {
  81. assert(!isEnd());
  82. Filei += N;
  83. assert(Filei <= Modules->getSourceFileCount(Modi));
  84. setValue();
  85. return *this;
  86. }
  87. DbiModuleSourceFilesIterator &DbiModuleSourceFilesIterator::
  88. operator-=(std::ptrdiff_t N) {
  89. // Note that we can subtract from an end iterator, but not a universal end
  90. // iterator.
  91. assert(!isUniversalEnd());
  92. assert(N <= Filei);
  93. Filei -= N;
  94. return *this;
  95. }
  96. void DbiModuleSourceFilesIterator::setValue() {
  97. if (isEnd()) {
  98. ThisValue = "";
  99. return;
  100. }
  101. uint32_t Off = Modules->ModuleInitialFileIndex[Modi] + Filei;
  102. auto ExpectedValue = Modules->getFileName(Off);
  103. if (!ExpectedValue) {
  104. consumeError(ExpectedValue.takeError());
  105. Filei = Modules->getSourceFileCount(Modi);
  106. } else
  107. ThisValue = *ExpectedValue;
  108. }
  109. bool DbiModuleSourceFilesIterator::isEnd() const {
  110. if (isUniversalEnd())
  111. return true;
  112. assert(Modules);
  113. assert(Modi <= Modules->getModuleCount());
  114. assert(Filei <= Modules->getSourceFileCount(Modi));
  115. if (Modi == Modules->getModuleCount())
  116. return true;
  117. if (Filei == Modules->getSourceFileCount(Modi))
  118. return true;
  119. return false;
  120. }
  121. bool DbiModuleSourceFilesIterator::isUniversalEnd() const { return !Modules; }
  122. bool DbiModuleSourceFilesIterator::isCompatible(
  123. const DbiModuleSourceFilesIterator &R) const {
  124. // Universal iterators are compatible with any other iterator.
  125. if (isUniversalEnd() || R.isUniversalEnd())
  126. return true;
  127. // At this point, neither iterator is a universal end iterator, although one
  128. // or both might be non-universal end iterators. Regardless, the module index
  129. // is valid, so they are compatible if and only if they refer to the same
  130. // module.
  131. return Modi == R.Modi;
  132. }
  133. Error DbiModuleList::initialize(BinaryStreamRef ModInfo,
  134. BinaryStreamRef FileInfo) {
  135. if (auto EC = initializeModInfo(ModInfo))
  136. return EC;
  137. if (auto EC = initializeFileInfo(FileInfo))
  138. return EC;
  139. return Error::success();
  140. }
  141. Error DbiModuleList::initializeModInfo(BinaryStreamRef ModInfo) {
  142. ModInfoSubstream = ModInfo;
  143. if (ModInfo.getLength() == 0)
  144. return Error::success();
  145. BinaryStreamReader Reader(ModInfo);
  146. if (auto EC = Reader.readArray(Descriptors, ModInfo.getLength()))
  147. return EC;
  148. return Error::success();
  149. }
  150. Error DbiModuleList::initializeFileInfo(BinaryStreamRef FileInfo) {
  151. FileInfoSubstream = FileInfo;
  152. if (FileInfo.getLength() == 0)
  153. return Error::success();
  154. BinaryStreamReader FISR(FileInfo);
  155. if (auto EC = FISR.readObject(FileInfoHeader))
  156. return EC;
  157. // First is an array of `NumModules` module indices. This does not seem to be
  158. // used for anything meaningful, so we ignore it.
  159. FixedStreamArray<support::ulittle16_t> ModuleIndices;
  160. if (auto EC = FISR.readArray(ModuleIndices, FileInfoHeader->NumModules))
  161. return EC;
  162. if (auto EC = FISR.readArray(ModFileCountArray, FileInfoHeader->NumModules))
  163. return EC;
  164. // Compute the real number of source files. We can't trust the value in
  165. // `FileInfoHeader->NumSourceFiles` because it is a unit16, and the sum of all
  166. // source file counts might be larger than a unit16. So we compute the real
  167. // count by summing up the individual counts.
  168. uint32_t NumSourceFiles = 0;
  169. for (auto Count : ModFileCountArray)
  170. NumSourceFiles += Count;
  171. // In the reference implementation, this array is where the pointer documented
  172. // at the definition of ModuleInfoHeader::FileNameOffs points to. Note that
  173. // although the field in ModuleInfoHeader is ignored this array is not, as it
  174. // is the authority on where each filename begins in the names buffer.
  175. if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
  176. return EC;
  177. if (auto EC = FISR.readStreamRef(NamesBuffer))
  178. return EC;
  179. auto DescriptorIter = Descriptors.begin();
  180. uint32_t NextFileIndex = 0;
  181. ModuleInitialFileIndex.resize(FileInfoHeader->NumModules);
  182. ModuleDescriptorOffsets.resize(FileInfoHeader->NumModules);
  183. for (size_t I = 0; I < FileInfoHeader->NumModules; ++I) {
  184. assert(DescriptorIter != Descriptors.end());
  185. ModuleInitialFileIndex[I] = NextFileIndex;
  186. ModuleDescriptorOffsets[I] = DescriptorIter.offset();
  187. NextFileIndex += ModFileCountArray[I];
  188. ++DescriptorIter;
  189. }
  190. assert(DescriptorIter == Descriptors.end());
  191. assert(NextFileIndex == NumSourceFiles);
  192. return Error::success();
  193. }
  194. uint32_t DbiModuleList::getModuleCount() const {
  195. return FileInfoHeader->NumModules;
  196. }
  197. uint32_t DbiModuleList::getSourceFileCount() const {
  198. return FileNameOffsets.size();
  199. }
  200. uint16_t DbiModuleList::getSourceFileCount(uint32_t Modi) const {
  201. return ModFileCountArray[Modi];
  202. }
  203. DbiModuleDescriptor DbiModuleList::getModuleDescriptor(uint32_t Modi) const {
  204. assert(Modi < getModuleCount());
  205. uint32_t Offset = ModuleDescriptorOffsets[Modi];
  206. auto Iter = Descriptors.at(Offset);
  207. assert(Iter != Descriptors.end());
  208. return *Iter;
  209. }
  210. iterator_range<DbiModuleSourceFilesIterator>
  211. DbiModuleList::source_files(uint32_t Modi) const {
  212. return make_range<DbiModuleSourceFilesIterator>(
  213. DbiModuleSourceFilesIterator(*this, Modi, 0),
  214. DbiModuleSourceFilesIterator());
  215. }
  216. Expected<StringRef> DbiModuleList::getFileName(uint32_t Index) const {
  217. BinaryStreamReader Names(NamesBuffer);
  218. if (Index >= getSourceFileCount())
  219. return make_error<RawError>(raw_error_code::index_out_of_bounds);
  220. uint32_t FileOffset = FileNameOffsets[Index];
  221. Names.setOffset(FileOffset);
  222. StringRef Name;
  223. if (auto EC = Names.readCString(Name))
  224. return std::move(EC);
  225. return Name;
  226. }