DebugCrossImpSubsection.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //===- DebugCrossImpSubsection.cpp ----------------------------------------===//
  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/CodeView/DebugCrossImpSubsection.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/DebugInfo/CodeView/CodeViewError.h"
  11. #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
  12. #include "llvm/Support/BinaryStreamReader.h"
  13. #include "llvm/Support/BinaryStreamWriter.h"
  14. #include "llvm/Support/Endian.h"
  15. #include "llvm/Support/Error.h"
  16. #include <algorithm>
  17. #include <cstdint>
  18. #include <utility>
  19. #include <vector>
  20. using namespace llvm;
  21. using namespace llvm::codeview;
  22. Error VarStreamArrayExtractor<CrossModuleImportItem>::
  23. operator()(BinaryStreamRef Stream, uint32_t &Len,
  24. codeview::CrossModuleImportItem &Item) {
  25. BinaryStreamReader Reader(Stream);
  26. if (Reader.bytesRemaining() < sizeof(CrossModuleImport))
  27. return make_error<CodeViewError>(
  28. cv_error_code::insufficient_buffer,
  29. "Not enough bytes for a Cross Module Import Header!");
  30. if (auto EC = Reader.readObject(Item.Header))
  31. return EC;
  32. if (Reader.bytesRemaining() < Item.Header->Count * sizeof(uint32_t))
  33. return make_error<CodeViewError>(
  34. cv_error_code::insufficient_buffer,
  35. "Not enough to read specified number of Cross Module References!");
  36. if (auto EC = Reader.readArray(Item.Imports, Item.Header->Count))
  37. return EC;
  38. return Error::success();
  39. }
  40. Error DebugCrossModuleImportsSubsectionRef::initialize(
  41. BinaryStreamReader Reader) {
  42. return Reader.readArray(References, Reader.bytesRemaining());
  43. }
  44. Error DebugCrossModuleImportsSubsectionRef::initialize(BinaryStreamRef Stream) {
  45. BinaryStreamReader Reader(Stream);
  46. return initialize(Reader);
  47. }
  48. void DebugCrossModuleImportsSubsection::addImport(StringRef Module,
  49. uint32_t ImportId) {
  50. Strings.insert(Module);
  51. std::vector<support::ulittle32_t> Targets = {support::ulittle32_t(ImportId)};
  52. auto Result = Mappings.insert(std::make_pair(Module, Targets));
  53. if (!Result.second)
  54. Result.first->getValue().push_back(Targets[0]);
  55. }
  56. uint32_t DebugCrossModuleImportsSubsection::calculateSerializedSize() const {
  57. uint32_t Size = 0;
  58. for (const auto &Item : Mappings) {
  59. Size += sizeof(CrossModuleImport);
  60. Size += sizeof(support::ulittle32_t) * Item.second.size();
  61. }
  62. return Size;
  63. }
  64. Error DebugCrossModuleImportsSubsection::commit(
  65. BinaryStreamWriter &Writer) const {
  66. using T = decltype(&*Mappings.begin());
  67. std::vector<T> Ids;
  68. Ids.reserve(Mappings.size());
  69. for (const auto &M : Mappings)
  70. Ids.push_back(&M);
  71. llvm::sort(Ids, [this](const T &L1, const T &L2) {
  72. return Strings.getIdForString(L1->getKey()) <
  73. Strings.getIdForString(L2->getKey());
  74. });
  75. for (const auto &Item : Ids) {
  76. CrossModuleImport Imp;
  77. Imp.ModuleNameOffset = Strings.getIdForString(Item->getKey());
  78. Imp.Count = Item->getValue().size();
  79. if (auto EC = Writer.writeObject(Imp))
  80. return EC;
  81. if (auto EC = Writer.writeArray(makeArrayRef(Item->getValue())))
  82. return EC;
  83. }
  84. return Error::success();
  85. }