189cb50c9SDimitry Andric //===- DebugStringTableSubsection.cpp - CodeView String Table ---*- C++ -*-===// 289cb50c9SDimitry Andric // 389cb50c9SDimitry Andric // The LLVM Compiler Infrastructure 489cb50c9SDimitry Andric // 589cb50c9SDimitry Andric // This file is distributed under the University of Illinois Open Source 689cb50c9SDimitry Andric // License. See LICENSE.TXT for details. 789cb50c9SDimitry Andric // 889cb50c9SDimitry Andric //===----------------------------------------------------------------------===// 989cb50c9SDimitry Andric 1089cb50c9SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h" 1189cb50c9SDimitry Andric 1289cb50c9SDimitry Andric #include "llvm/Support/BinaryStream.h" 1389cb50c9SDimitry Andric #include "llvm/Support/BinaryStreamReader.h" 1489cb50c9SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h" 1589cb50c9SDimitry Andric 1689cb50c9SDimitry Andric using namespace llvm; 1789cb50c9SDimitry Andric using namespace llvm::codeview; 1889cb50c9SDimitry Andric 1989cb50c9SDimitry Andric DebugStringTableSubsectionRef::DebugStringTableSubsectionRef() 2089cb50c9SDimitry Andric : DebugSubsectionRef(DebugSubsectionKind::StringTable) {} 2189cb50c9SDimitry Andric 2289cb50c9SDimitry Andric Error DebugStringTableSubsectionRef::initialize(BinaryStreamRef Contents) { 2389cb50c9SDimitry Andric Stream = Contents; 2489cb50c9SDimitry Andric return Error::success(); 2589cb50c9SDimitry Andric } 26db17bf38SDimitry Andric Error DebugStringTableSubsectionRef::initialize(BinaryStreamReader &Reader) { 27db17bf38SDimitry Andric return Reader.readStreamRef(Stream); 28db17bf38SDimitry Andric } 2989cb50c9SDimitry Andric 3089cb50c9SDimitry Andric Expected<StringRef> 3189cb50c9SDimitry Andric DebugStringTableSubsectionRef::getString(uint32_t Offset) const { 3289cb50c9SDimitry Andric BinaryStreamReader Reader(Stream); 3389cb50c9SDimitry Andric Reader.setOffset(Offset); 3489cb50c9SDimitry Andric StringRef Result; 3589cb50c9SDimitry Andric if (auto EC = Reader.readCString(Result)) 3689cb50c9SDimitry Andric return std::move(EC); 3789cb50c9SDimitry Andric return Result; 3889cb50c9SDimitry Andric } 3989cb50c9SDimitry Andric 4089cb50c9SDimitry Andric DebugStringTableSubsection::DebugStringTableSubsection() 4189cb50c9SDimitry Andric : DebugSubsection(DebugSubsectionKind::StringTable) {} 4289cb50c9SDimitry Andric 4389cb50c9SDimitry Andric uint32_t DebugStringTableSubsection::insert(StringRef S) { 4489cb50c9SDimitry Andric auto P = Strings.insert({S, StringSize}); 4589cb50c9SDimitry Andric 4689cb50c9SDimitry Andric // If a given string didn't exist in the string table, we want to increment 4789cb50c9SDimitry Andric // the string table size. 4889cb50c9SDimitry Andric if (P.second) 4989cb50c9SDimitry Andric StringSize += S.size() + 1; // +1 for '\0' 5089cb50c9SDimitry Andric return P.first->second; 5189cb50c9SDimitry Andric } 5289cb50c9SDimitry Andric 5389cb50c9SDimitry Andric uint32_t DebugStringTableSubsection::calculateSerializedSize() const { 5489cb50c9SDimitry Andric return StringSize; 5589cb50c9SDimitry Andric } 5689cb50c9SDimitry Andric 5789cb50c9SDimitry Andric Error DebugStringTableSubsection::commit(BinaryStreamWriter &Writer) const { 58db17bf38SDimitry Andric uint32_t Begin = Writer.getOffset(); 59db17bf38SDimitry Andric uint32_t End = Begin + StringSize; 6089cb50c9SDimitry Andric 6189cb50c9SDimitry Andric for (auto &Pair : Strings) { 6289cb50c9SDimitry Andric StringRef S = Pair.getKey(); 63db17bf38SDimitry Andric uint32_t Offset = Begin + Pair.getValue(); 6489cb50c9SDimitry Andric Writer.setOffset(Offset); 6589cb50c9SDimitry Andric if (auto EC = Writer.writeCString(S)) 6689cb50c9SDimitry Andric return EC; 67db17bf38SDimitry Andric assert(Writer.getOffset() <= End); 6889cb50c9SDimitry Andric } 6989cb50c9SDimitry Andric 70db17bf38SDimitry Andric Writer.setOffset(End); 7189cb50c9SDimitry Andric return Error::success(); 7289cb50c9SDimitry Andric } 7389cb50c9SDimitry Andric 7489cb50c9SDimitry Andric uint32_t DebugStringTableSubsection::size() const { return Strings.size(); } 7589cb50c9SDimitry Andric 7689cb50c9SDimitry Andric uint32_t DebugStringTableSubsection::getStringId(StringRef S) const { 776d97bb29SDimitry Andric auto Iter = Strings.find(S); 786d97bb29SDimitry Andric assert(Iter != Strings.end()); 796d97bb29SDimitry Andric return Iter->second; 8089cb50c9SDimitry Andric } 81