InfoStreamBuilder.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //===- InfoStreamBuilder.cpp - PDB Info 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/InfoStreamBuilder.h"
  9. #include "llvm/DebugInfo/MSF/MSFBuilder.h"
  10. #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
  11. #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
  12. #include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h"
  13. #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
  14. #include "llvm/DebugInfo/PDB/Native/RawError.h"
  15. #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
  16. #include "llvm/Support/BinaryStreamWriter.h"
  17. using namespace llvm;
  18. using namespace llvm::codeview;
  19. using namespace llvm::msf;
  20. using namespace llvm::pdb;
  21. InfoStreamBuilder::InfoStreamBuilder(msf::MSFBuilder &Msf,
  22. NamedStreamMap &NamedStreams)
  23. : Msf(Msf), Ver(PdbRaw_ImplVer::PdbImplVC70), Age(0),
  24. NamedStreams(NamedStreams) {
  25. ::memset(&Guid, 0, sizeof(Guid));
  26. }
  27. void InfoStreamBuilder::setVersion(PdbRaw_ImplVer V) { Ver = V; }
  28. void InfoStreamBuilder::addFeature(PdbRaw_FeatureSig Sig) {
  29. Features.push_back(Sig);
  30. }
  31. void InfoStreamBuilder::setHashPDBContentsToGUID(bool B) {
  32. HashPDBContentsToGUID = B;
  33. }
  34. void InfoStreamBuilder::setAge(uint32_t A) { Age = A; }
  35. void InfoStreamBuilder::setSignature(uint32_t S) { Signature = S; }
  36. void InfoStreamBuilder::setGuid(GUID G) { Guid = G; }
  37. Error InfoStreamBuilder::finalizeMsfLayout() {
  38. uint32_t Length = sizeof(InfoStreamHeader) +
  39. NamedStreams.calculateSerializedLength() +
  40. (Features.size() + 1) * sizeof(uint32_t);
  41. if (auto EC = Msf.setStreamSize(StreamPDB, Length))
  42. return EC;
  43. return Error::success();
  44. }
  45. Error InfoStreamBuilder::commit(const msf::MSFLayout &Layout,
  46. WritableBinaryStreamRef Buffer) const {
  47. auto InfoS = WritableMappedBlockStream::createIndexedStream(
  48. Layout, Buffer, StreamPDB, Msf.getAllocator());
  49. BinaryStreamWriter Writer(*InfoS);
  50. InfoStreamHeader H;
  51. // Leave the build id fields 0 so they can be set as the last step before
  52. // committing the file to disk.
  53. ::memset(&H, 0, sizeof(H));
  54. H.Version = Ver;
  55. if (auto EC = Writer.writeObject(H))
  56. return EC;
  57. if (auto EC = NamedStreams.commit(Writer))
  58. return EC;
  59. if (auto EC = Writer.writeInteger(0))
  60. return EC;
  61. for (auto E : Features) {
  62. if (auto EC = Writer.writeEnum(E))
  63. return EC;
  64. }
  65. assert(Writer.bytesRemaining() == 0);
  66. return Error::success();
  67. }