DbiStreamBuilder.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. //===- DbiStreamBuilder.cpp - PDB Dbi Stream Creation -----------*- C++ -*-===//
  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/DbiStreamBuilder.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/BinaryFormat/COFF.h"
  11. #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h"
  12. #include "llvm/DebugInfo/MSF/MSFBuilder.h"
  13. #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
  14. #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
  15. #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
  16. #include "llvm/DebugInfo/PDB/Native/RawError.h"
  17. #include "llvm/Object/COFF.h"
  18. #include "llvm/Support/BinaryStreamWriter.h"
  19. #include "llvm/Support/Parallel.h"
  20. using namespace llvm;
  21. using namespace llvm::codeview;
  22. using namespace llvm::msf;
  23. using namespace llvm::pdb;
  24. DbiStreamBuilder::DbiStreamBuilder(msf::MSFBuilder &Msf)
  25. : Msf(Msf), Allocator(Msf.getAllocator()), Age(1), BuildNumber(0),
  26. PdbDllVersion(0), PdbDllRbld(0), Flags(0), MachineType(PDB_Machine::x86),
  27. Header(nullptr) {}
  28. DbiStreamBuilder::~DbiStreamBuilder() {}
  29. void DbiStreamBuilder::setVersionHeader(PdbRaw_DbiVer V) { VerHeader = V; }
  30. void DbiStreamBuilder::setAge(uint32_t A) { Age = A; }
  31. void DbiStreamBuilder::setBuildNumber(uint16_t B) { BuildNumber = B; }
  32. void DbiStreamBuilder::setBuildNumber(uint8_t Major, uint8_t Minor) {
  33. BuildNumber = (uint16_t(Major) << DbiBuildNo::BuildMajorShift) &
  34. DbiBuildNo::BuildMajorMask;
  35. BuildNumber |= (uint16_t(Minor) << DbiBuildNo::BuildMinorShift) &
  36. DbiBuildNo::BuildMinorMask;
  37. BuildNumber |= DbiBuildNo::NewVersionFormatMask;
  38. }
  39. void DbiStreamBuilder::setPdbDllVersion(uint16_t V) { PdbDllVersion = V; }
  40. void DbiStreamBuilder::setPdbDllRbld(uint16_t R) { PdbDllRbld = R; }
  41. void DbiStreamBuilder::setFlags(uint16_t F) { Flags = F; }
  42. void DbiStreamBuilder::setMachineType(PDB_Machine M) { MachineType = M; }
  43. void DbiStreamBuilder::setMachineType(COFF::MachineTypes M) {
  44. // These enums are mirrors of each other, so we can just cast the value.
  45. MachineType = static_cast<pdb::PDB_Machine>(static_cast<unsigned>(M));
  46. }
  47. void DbiStreamBuilder::setGlobalsStreamIndex(uint32_t Index) {
  48. GlobalsStreamIndex = Index;
  49. }
  50. void DbiStreamBuilder::setSymbolRecordStreamIndex(uint32_t Index) {
  51. SymRecordStreamIndex = Index;
  52. }
  53. void DbiStreamBuilder::setPublicsStreamIndex(uint32_t Index) {
  54. PublicsStreamIndex = Index;
  55. }
  56. void DbiStreamBuilder::addNewFpoData(const codeview::FrameData &FD) {
  57. if (!NewFpoData.hasValue())
  58. NewFpoData.emplace(false);
  59. NewFpoData->addFrameData(FD);
  60. }
  61. void DbiStreamBuilder::addOldFpoData(const object::FpoData &FD) {
  62. OldFpoData.push_back(FD);
  63. }
  64. Error DbiStreamBuilder::addDbgStream(pdb::DbgHeaderType Type,
  65. ArrayRef<uint8_t> Data) {
  66. assert(Type != DbgHeaderType::NewFPO &&
  67. "NewFPO data should be written via addFrameData()!");
  68. DbgStreams[(int)Type].emplace();
  69. DbgStreams[(int)Type]->Size = Data.size();
  70. DbgStreams[(int)Type]->WriteFn = [Data](BinaryStreamWriter &Writer) {
  71. return Writer.writeArray(Data);
  72. };
  73. return Error::success();
  74. }
  75. uint32_t DbiStreamBuilder::addECName(StringRef Name) {
  76. return ECNamesBuilder.insert(Name);
  77. }
  78. uint32_t DbiStreamBuilder::calculateSerializedLength() const {
  79. // For now we only support serializing the header.
  80. return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
  81. calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
  82. calculateSectionMapStreamSize() + calculateDbgStreamsSize() +
  83. ECNamesBuilder.calculateSerializedSize();
  84. }
  85. Expected<DbiModuleDescriptorBuilder &>
  86. DbiStreamBuilder::addModuleInfo(StringRef ModuleName) {
  87. uint32_t Index = ModiList.size();
  88. ModiList.push_back(
  89. std::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf));
  90. return *ModiList.back();
  91. }
  92. Error DbiStreamBuilder::addModuleSourceFile(DbiModuleDescriptorBuilder &Module,
  93. StringRef File) {
  94. uint32_t Index = SourceFileNames.size();
  95. SourceFileNames.insert(std::make_pair(File, Index));
  96. Module.addSourceFile(File);
  97. return Error::success();
  98. }
  99. Expected<uint32_t> DbiStreamBuilder::getSourceFileNameIndex(StringRef File) {
  100. auto NameIter = SourceFileNames.find(File);
  101. if (NameIter == SourceFileNames.end())
  102. return make_error<RawError>(raw_error_code::no_entry,
  103. "The specified source file was not found");
  104. return NameIter->getValue();
  105. }
  106. uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
  107. uint32_t Size = 0;
  108. for (const auto &M : ModiList)
  109. Size += M->calculateSerializedLength();
  110. return Size;
  111. }
  112. uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
  113. if (SectionContribs.empty())
  114. return 0;
  115. return sizeof(enum PdbRaw_DbiSecContribVer) +
  116. sizeof(SectionContribs[0]) * SectionContribs.size();
  117. }
  118. uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
  119. if (SectionMap.empty())
  120. return 0;
  121. return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
  122. }
  123. uint32_t DbiStreamBuilder::calculateNamesOffset() const {
  124. uint32_t Offset = 0;
  125. Offset += sizeof(ulittle16_t); // NumModules
  126. Offset += sizeof(ulittle16_t); // NumSourceFiles
  127. Offset += ModiList.size() * sizeof(ulittle16_t); // ModIndices
  128. Offset += ModiList.size() * sizeof(ulittle16_t); // ModFileCounts
  129. uint32_t NumFileInfos = 0;
  130. for (const auto &M : ModiList)
  131. NumFileInfos += M->source_files().size();
  132. Offset += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
  133. return Offset;
  134. }
  135. uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
  136. uint32_t Size = calculateNamesOffset();
  137. Size += calculateNamesBufferSize();
  138. return alignTo(Size, sizeof(uint32_t));
  139. }
  140. uint32_t DbiStreamBuilder::calculateNamesBufferSize() const {
  141. uint32_t Size = 0;
  142. for (const auto &F : SourceFileNames) {
  143. Size += F.getKeyLength() + 1; // Names[I];
  144. }
  145. return Size;
  146. }
  147. uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
  148. return DbgStreams.size() * sizeof(uint16_t);
  149. }
  150. Error DbiStreamBuilder::generateFileInfoSubstream() {
  151. uint32_t Size = calculateFileInfoSubstreamSize();
  152. auto Data = Allocator.Allocate<uint8_t>(Size);
  153. uint32_t NamesOffset = calculateNamesOffset();
  154. FileInfoBuffer = MutableBinaryByteStream(MutableArrayRef<uint8_t>(Data, Size),
  155. llvm::support::little);
  156. WritableBinaryStreamRef MetadataBuffer =
  157. WritableBinaryStreamRef(FileInfoBuffer).keep_front(NamesOffset);
  158. BinaryStreamWriter MetadataWriter(MetadataBuffer);
  159. uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
  160. uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
  161. if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
  162. return EC;
  163. if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
  164. return EC;
  165. for (uint16_t I = 0; I < ModiCount; ++I) {
  166. if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
  167. return EC;
  168. }
  169. for (const auto &MI : ModiList) {
  170. FileCount = static_cast<uint16_t>(MI->source_files().size());
  171. if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
  172. return EC;
  173. }
  174. // Before writing the FileNameOffsets array, write the NamesBuffer array.
  175. // A side effect of this is that this will actually compute the various
  176. // file name offsets, so we can then go back and write the FileNameOffsets
  177. // array to the other substream.
  178. NamesBuffer = WritableBinaryStreamRef(FileInfoBuffer).drop_front(NamesOffset);
  179. BinaryStreamWriter NameBufferWriter(NamesBuffer);
  180. for (auto &Name : SourceFileNames) {
  181. Name.second = NameBufferWriter.getOffset();
  182. if (auto EC = NameBufferWriter.writeCString(Name.getKey()))
  183. return EC;
  184. }
  185. for (const auto &MI : ModiList) {
  186. for (StringRef Name : MI->source_files()) {
  187. auto Result = SourceFileNames.find(Name);
  188. if (Result == SourceFileNames.end())
  189. return make_error<RawError>(raw_error_code::no_entry,
  190. "The source file was not found.");
  191. if (auto EC = MetadataWriter.writeInteger(Result->second))
  192. return EC;
  193. }
  194. }
  195. if (auto EC = NameBufferWriter.padToAlignment(sizeof(uint32_t)))
  196. return EC;
  197. if (NameBufferWriter.bytesRemaining() > 0)
  198. return make_error<RawError>(raw_error_code::invalid_format,
  199. "The names buffer contained unexpected data.");
  200. if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
  201. return make_error<RawError>(
  202. raw_error_code::invalid_format,
  203. "The metadata buffer contained unexpected data.");
  204. return Error::success();
  205. }
  206. Error DbiStreamBuilder::finalize() {
  207. if (Header)
  208. return Error::success();
  209. for (auto &MI : ModiList)
  210. MI->finalize();
  211. if (auto EC = generateFileInfoSubstream())
  212. return EC;
  213. DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
  214. ::memset(H, 0, sizeof(DbiStreamHeader));
  215. H->VersionHeader = *VerHeader;
  216. H->VersionSignature = -1;
  217. H->Age = Age;
  218. H->BuildNumber = BuildNumber;
  219. H->Flags = Flags;
  220. H->PdbDllRbld = PdbDllRbld;
  221. H->PdbDllVersion = PdbDllVersion;
  222. H->MachineType = static_cast<uint16_t>(MachineType);
  223. H->ECSubstreamSize = ECNamesBuilder.calculateSerializedSize();
  224. H->FileInfoSize = FileInfoBuffer.getLength();
  225. H->ModiSubstreamSize = calculateModiSubstreamSize();
  226. H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
  227. H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
  228. H->SectionMapSize = calculateSectionMapStreamSize();
  229. H->TypeServerSize = 0;
  230. H->SymRecordStreamIndex = SymRecordStreamIndex;
  231. H->PublicSymbolStreamIndex = PublicsStreamIndex;
  232. H->MFCTypeServerIndex = 0; // Not sure what this is, but link.exe writes 0.
  233. H->GlobalSymbolStreamIndex = GlobalsStreamIndex;
  234. Header = H;
  235. return Error::success();
  236. }
  237. Error DbiStreamBuilder::finalizeMsfLayout() {
  238. if (NewFpoData.hasValue()) {
  239. DbgStreams[(int)DbgHeaderType::NewFPO].emplace();
  240. DbgStreams[(int)DbgHeaderType::NewFPO]->Size =
  241. NewFpoData->calculateSerializedSize();
  242. DbgStreams[(int)DbgHeaderType::NewFPO]->WriteFn =
  243. [this](BinaryStreamWriter &Writer) {
  244. return NewFpoData->commit(Writer);
  245. };
  246. }
  247. if (!OldFpoData.empty()) {
  248. DbgStreams[(int)DbgHeaderType::FPO].emplace();
  249. DbgStreams[(int)DbgHeaderType::FPO]->Size =
  250. sizeof(object::FpoData) * OldFpoData.size();
  251. DbgStreams[(int)DbgHeaderType::FPO]->WriteFn =
  252. [this](BinaryStreamWriter &Writer) {
  253. return Writer.writeArray(makeArrayRef(OldFpoData));
  254. };
  255. }
  256. for (auto &S : DbgStreams) {
  257. if (!S.hasValue())
  258. continue;
  259. auto ExpectedIndex = Msf.addStream(S->Size);
  260. if (!ExpectedIndex)
  261. return ExpectedIndex.takeError();
  262. S->StreamNumber = *ExpectedIndex;
  263. }
  264. for (auto &MI : ModiList) {
  265. if (auto EC = MI->finalizeMsfLayout())
  266. return EC;
  267. }
  268. uint32_t Length = calculateSerializedLength();
  269. if (auto EC = Msf.setStreamSize(StreamDBI, Length))
  270. return EC;
  271. return Error::success();
  272. }
  273. static uint16_t toSecMapFlags(uint32_t Flags) {
  274. uint16_t Ret = 0;
  275. if (Flags & COFF::IMAGE_SCN_MEM_READ)
  276. Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read);
  277. if (Flags & COFF::IMAGE_SCN_MEM_WRITE)
  278. Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write);
  279. if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
  280. Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
  281. if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT))
  282. Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit);
  283. // This seems always 1.
  284. Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector);
  285. return Ret;
  286. }
  287. // Populate the Section Map from COFF section headers.
  288. //
  289. // A Section Map seem to be a copy of a COFF section list in other format.
  290. // I don't know why a PDB file contains both a COFF section header and
  291. // a Section Map, but it seems it must be present in a PDB.
  292. void DbiStreamBuilder::createSectionMap(
  293. ArrayRef<llvm::object::coff_section> SecHdrs) {
  294. int Idx = 0;
  295. auto Add = [&]() -> SecMapEntry & {
  296. SectionMap.emplace_back();
  297. auto &Entry = SectionMap.back();
  298. memset(&Entry, 0, sizeof(Entry));
  299. Entry.Frame = Idx + 1;
  300. // We don't know the meaning of these fields yet.
  301. Entry.SecName = UINT16_MAX;
  302. Entry.ClassName = UINT16_MAX;
  303. return Entry;
  304. };
  305. for (auto &Hdr : SecHdrs) {
  306. auto &Entry = Add();
  307. Entry.Flags = toSecMapFlags(Hdr.Characteristics);
  308. Entry.SecByteLength = Hdr.VirtualSize;
  309. ++Idx;
  310. }
  311. // The last entry is for absolute symbols.
  312. auto &Entry = Add();
  313. Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) |
  314. static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress);
  315. Entry.SecByteLength = UINT32_MAX;
  316. }
  317. Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
  318. WritableBinaryStreamRef MsfBuffer) {
  319. if (auto EC = finalize())
  320. return EC;
  321. auto DbiS = WritableMappedBlockStream::createIndexedStream(
  322. Layout, MsfBuffer, StreamDBI, Allocator);
  323. BinaryStreamWriter Writer(*DbiS);
  324. if (auto EC = Writer.writeObject(*Header))
  325. return EC;
  326. for (auto &M : ModiList) {
  327. if (auto EC = M->commit(Writer))
  328. return EC;
  329. }
  330. // Commit symbol streams. This is a lot of data, so do it in parallel.
  331. if (auto EC = parallelForEachError(
  332. ModiList, [&](std::unique_ptr<DbiModuleDescriptorBuilder> &M) {
  333. return M->commitSymbolStream(Layout, MsfBuffer);
  334. }))
  335. return EC;
  336. if (!SectionContribs.empty()) {
  337. if (auto EC = Writer.writeEnum(DbiSecContribVer60))
  338. return EC;
  339. if (auto EC = Writer.writeArray(makeArrayRef(SectionContribs)))
  340. return EC;
  341. }
  342. if (!SectionMap.empty()) {
  343. ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size());
  344. SecMapHeader SMHeader = {Size, Size};
  345. if (auto EC = Writer.writeObject(SMHeader))
  346. return EC;
  347. if (auto EC = Writer.writeArray(makeArrayRef(SectionMap)))
  348. return EC;
  349. }
  350. if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
  351. return EC;
  352. if (auto EC = ECNamesBuilder.commit(Writer))
  353. return EC;
  354. for (auto &Stream : DbgStreams) {
  355. uint16_t StreamNumber = kInvalidStreamIndex;
  356. if (Stream.hasValue())
  357. StreamNumber = Stream->StreamNumber;
  358. if (auto EC = Writer.writeInteger(StreamNumber))
  359. return EC;
  360. }
  361. for (auto &Stream : DbgStreams) {
  362. if (!Stream.hasValue())
  363. continue;
  364. assert(Stream->StreamNumber != kInvalidStreamIndex);
  365. auto WritableStream = WritableMappedBlockStream::createIndexedStream(
  366. Layout, MsfBuffer, Stream->StreamNumber, Allocator);
  367. BinaryStreamWriter DbgStreamWriter(*WritableStream);
  368. if (auto EC = Stream->WriteFn(DbgStreamWriter))
  369. return EC;
  370. }
  371. if (Writer.bytesRemaining() > 0)
  372. return make_error<RawError>(raw_error_code::invalid_format,
  373. "Unexpected bytes found in DBI Stream");
  374. return Error::success();
  375. }