InfoStreamBuilder.cpp 2.6 KB

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