blob: de02525270c456d2a3e7b4a800a1a20febd77b81 [file] [log] [blame]
Zachary Turner591312c2017-05-30 17:13:33 +00001//===- DebugStringTableSubsection.cpp - CodeView String Table ---*- C++ -*-===//
Zachary Turnerc504ae32017-05-03 15:58:37 +00002//
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
Zachary Turner591312c2017-05-30 17:13:33 +000010#include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
Zachary Turnerc504ae32017-05-03 15:58:37 +000011
12#include "llvm/Support/BinaryStream.h"
13#include "llvm/Support/BinaryStreamReader.h"
14#include "llvm/Support/BinaryStreamWriter.h"
15
16using namespace llvm;
17using namespace llvm::codeview;
18
Zachary Turner591312c2017-05-30 17:13:33 +000019DebugStringTableSubsectionRef::DebugStringTableSubsectionRef()
20 : DebugSubsectionRef(DebugSubsectionKind::StringTable) {}
Zachary Turnerc504ae32017-05-03 15:58:37 +000021
Zachary Turner591312c2017-05-30 17:13:33 +000022Error DebugStringTableSubsectionRef::initialize(BinaryStreamRef Contents) {
Zachary Turner2d5c2cd2017-05-03 17:11:11 +000023 Stream = Contents;
24 return Error::success();
Zachary Turnerc504ae32017-05-03 15:58:37 +000025}
Zachary Turner1bf77622017-06-08 23:49:01 +000026Error DebugStringTableSubsectionRef::initialize(BinaryStreamReader &Reader) {
Zachary Turnerdeb39132017-06-09 00:28:08 +000027 return Reader.readStreamRef(Stream);
Zachary Turner1bf77622017-06-08 23:49:01 +000028}
Zachary Turnerc504ae32017-05-03 15:58:37 +000029
Zachary Turner591312c2017-05-30 17:13:33 +000030Expected<StringRef>
31DebugStringTableSubsectionRef::getString(uint32_t Offset) const {
Zachary Turnerc504ae32017-05-03 15:58:37 +000032 BinaryStreamReader Reader(Stream);
33 Reader.setOffset(Offset);
34 StringRef Result;
Zachary Turner2d5c2cd2017-05-03 17:11:11 +000035 if (auto EC = Reader.readCString(Result))
36 return std::move(EC);
Zachary Turnerc504ae32017-05-03 15:58:37 +000037 return Result;
38}
39
Zachary Turner591312c2017-05-30 17:13:33 +000040DebugStringTableSubsection::DebugStringTableSubsection()
41 : DebugSubsection(DebugSubsectionKind::StringTable) {}
42
43uint32_t DebugStringTableSubsection::insert(StringRef S) {
Zachary Turnerc504ae32017-05-03 15:58:37 +000044 auto P = Strings.insert({S, StringSize});
45
46 // If a given string didn't exist in the string table, we want to increment
47 // the string table size.
48 if (P.second)
49 StringSize += S.size() + 1; // +1 for '\0'
50 return P.first->second;
51}
52
Zachary Turner591312c2017-05-30 17:13:33 +000053uint32_t DebugStringTableSubsection::calculateSerializedSize() const {
54 return StringSize;
55}
Zachary Turnerc504ae32017-05-03 15:58:37 +000056
Zachary Turner591312c2017-05-30 17:13:33 +000057Error DebugStringTableSubsection::commit(BinaryStreamWriter &Writer) const {
Zachary Turnerdeb39132017-06-09 00:28:08 +000058 uint32_t Begin = Writer.getOffset();
59 uint32_t End = Begin + StringSize;
Zachary Turnerc504ae32017-05-03 15:58:37 +000060
Zachary Turnera8cfc292017-06-14 15:59:27 +000061 // Write a null string at the beginning.
62 if (auto EC = Writer.writeCString(StringRef()))
63 return EC;
64
Zachary Turnerc504ae32017-05-03 15:58:37 +000065 for (auto &Pair : Strings) {
66 StringRef S = Pair.getKey();
Zachary Turnerdeb39132017-06-09 00:28:08 +000067 uint32_t Offset = Begin + Pair.getValue();
Zachary Turnerc504ae32017-05-03 15:58:37 +000068 Writer.setOffset(Offset);
69 if (auto EC = Writer.writeCString(S))
70 return EC;
Zachary Turnerdeb39132017-06-09 00:28:08 +000071 assert(Writer.getOffset() <= End);
Zachary Turnerc504ae32017-05-03 15:58:37 +000072 }
73
Zachary Turnerdeb39132017-06-09 00:28:08 +000074 Writer.setOffset(End);
Zachary Turnera8cfc292017-06-14 15:59:27 +000075 assert((End - Begin) == StringSize);
Zachary Turnerc504ae32017-05-03 15:58:37 +000076 return Error::success();
77}
78
Zachary Turner591312c2017-05-30 17:13:33 +000079uint32_t DebugStringTableSubsection::size() const { return Strings.size(); }
Zachary Turnercf468d82017-05-03 17:11:40 +000080
Zachary Turner591312c2017-05-30 17:13:33 +000081uint32_t DebugStringTableSubsection::getStringId(StringRef S) const {
Zachary Turner92dcdda2017-06-02 19:49:14 +000082 auto Iter = Strings.find(S);
83 assert(Iter != Strings.end());
84 return Iter->second;
Zachary Turnercf468d82017-05-03 17:11:40 +000085}