1 //===- CodeViewYAMLTypeHashing.cpp - CodeView YAMLIO type hashing ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines classes for handling the YAML representation of CodeView 11 // Debug Info. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/ObjectYAML/CodeViewYAMLTypeHashing.h" 16 #include "llvm/Support/BinaryByteStream.h" 17 #include "llvm/Support/BinaryStreamReader.h" 18 #include "llvm/Support/BinaryStreamWriter.h" 19 20 using namespace llvm; 21 using namespace llvm::codeview; 22 using namespace llvm::CodeViewYAML; 23 using namespace llvm::yaml; 24 25 namespace llvm { 26 namespace yaml { 27 28 void MappingTraits<DebugHSection>::mapping(IO &io, DebugHSection &DebugH) { 29 io.mapRequired("Version", DebugH.Version); 30 io.mapRequired("HashAlgorithm", DebugH.HashAlgorithm); 31 io.mapOptional("HashValues", DebugH.Hashes); 32 } 33 34 void ScalarTraits<GlobalHash>::output(const GlobalHash &GH, void *Ctx, 35 raw_ostream &OS) { 36 ScalarTraits<BinaryRef>::output(GH.Hash, Ctx, OS); 37 } 38 39 StringRef ScalarTraits<GlobalHash>::input(StringRef Scalar, void *Ctx, 40 GlobalHash &GH) { 41 return ScalarTraits<BinaryRef>::input(Scalar, Ctx, GH.Hash); 42 } 43 44 } // end namespace yaml 45 } // end namespace llvm 46 47 DebugHSection llvm::CodeViewYAML::fromDebugH(ArrayRef<uint8_t> DebugH) { 48 assert(DebugH.size() >= 8); 49 assert((DebugH.size() - 8) % 20 == 0); 50 51 BinaryStreamReader Reader(DebugH, llvm::support::little); 52 DebugHSection DHS; 53 cantFail(Reader.readInteger(DHS.Magic)); 54 cantFail(Reader.readInteger(DHS.Version)); 55 cantFail(Reader.readInteger(DHS.HashAlgorithm)); 56 while (Reader.bytesRemaining() != 0) { 57 ArrayRef<uint8_t> S; 58 cantFail(Reader.readBytes(S, 20)); 59 DHS.Hashes.emplace_back(S); 60 } 61 assert(Reader.bytesRemaining() == 0); 62 return DHS; 63 } 64 65 ArrayRef<uint8_t> llvm::CodeViewYAML::toDebugH(const DebugHSection &DebugH, 66 BumpPtrAllocator &Alloc) { 67 uint32_t Size = 8 + 20 * DebugH.Hashes.size(); 68 uint8_t *Data = Alloc.Allocate<uint8_t>(Size); 69 MutableArrayRef<uint8_t> Buffer(Data, Size); 70 BinaryStreamWriter Writer(Buffer, llvm::support::little); 71 cantFail(Writer.writeInteger(DebugH.Magic)); 72 cantFail(Writer.writeInteger(DebugH.Version)); 73 cantFail(Writer.writeInteger(DebugH.HashAlgorithm)); 74 SmallString<20> Hash; 75 for (const auto &H : DebugH.Hashes) { 76 Hash.clear(); 77 raw_svector_ostream OS(Hash); 78 H.Hash.writeAsBinary(OS); 79 assert((Hash.size() == 20) && "Invalid hash size!"); 80 cantFail(Writer.writeFixedString(Hash)); 81 } 82 assert(Writer.bytesRemaining() == 0); 83 return Buffer; 84 } 85