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 17 #include "llvm/BinaryFormat/COFF.h" 18 #include "llvm/Support/BinaryByteStream.h" 19 #include "llvm/Support/BinaryStreamReader.h" 20 #include "llvm/Support/BinaryStreamWriter.h" 21 22 using namespace llvm; 23 using namespace llvm::codeview; 24 using namespace llvm::CodeViewYAML; 25 using namespace llvm::yaml; 26 27 namespace llvm { 28 namespace yaml { 29 30 void MappingTraits<DebugHSection>::mapping(IO &io, DebugHSection &DebugH) { 31 io.mapRequired("Version", DebugH.Version); 32 io.mapRequired("HashAlgorithm", DebugH.HashAlgorithm); 33 io.mapOptional("HashValues", DebugH.Hashes); 34 } 35 36 void ScalarTraits<GlobalHash>::output(const GlobalHash &GH, void *Ctx, 37 raw_ostream &OS) { 38 ScalarTraits<BinaryRef>::output(GH.Hash, Ctx, OS); 39 } 40 41 StringRef ScalarTraits<GlobalHash>::input(StringRef Scalar, void *Ctx, 42 GlobalHash &GH) { 43 return ScalarTraits<BinaryRef>::input(Scalar, Ctx, GH.Hash); 44 } 45 46 } // end namespace yaml 47 } // end namespace llvm 48 49 DebugHSection llvm::CodeViewYAML::fromDebugH(ArrayRef<uint8_t> DebugH) { 50 assert(DebugH.size() >= 8); 51 assert((DebugH.size() - 8) % 8 == 0); 52 53 BinaryStreamReader Reader(DebugH, llvm::support::little); 54 DebugHSection DHS; 55 cantFail(Reader.readInteger(DHS.Magic)); 56 cantFail(Reader.readInteger(DHS.Version)); 57 cantFail(Reader.readInteger(DHS.HashAlgorithm)); 58 59 while (Reader.bytesRemaining() != 0) { 60 ArrayRef<uint8_t> S; 61 cantFail(Reader.readBytes(S, 8)); 62 DHS.Hashes.emplace_back(S); 63 } 64 assert(Reader.bytesRemaining() == 0); 65 return DHS; 66 } 67 68 ArrayRef<uint8_t> llvm::CodeViewYAML::toDebugH(const DebugHSection &DebugH, 69 BumpPtrAllocator &Alloc) { 70 uint32_t Size = 8 + 8 * DebugH.Hashes.size(); 71 uint8_t *Data = Alloc.Allocate<uint8_t>(Size); 72 MutableArrayRef<uint8_t> Buffer(Data, Size); 73 BinaryStreamWriter Writer(Buffer, llvm::support::little); 74 75 cantFail(Writer.writeInteger(DebugH.Magic)); 76 cantFail(Writer.writeInteger(DebugH.Version)); 77 cantFail(Writer.writeInteger(DebugH.HashAlgorithm)); 78 SmallString<8> Hash; 79 for (const auto &H : DebugH.Hashes) { 80 Hash.clear(); 81 raw_svector_ostream OS(Hash); 82 H.Hash.writeAsBinary(OS); 83 assert((Hash.size() == 8) && "Invalid hash size!"); 84 cantFail(Writer.writeFixedString(Hash)); 85 } 86 assert(Writer.bytesRemaining() == 0); 87 return Buffer; 88 } 89